Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,22 @@
|
|
| 1 |
# app.py β Explorador geoespacial Vasculitis ANCA (BogotΓ‘)
|
| 2 |
-
# VersiΓ³n
|
| 3 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 4 |
-
# β’ Univariados/bivariados distinguen variables categΓ³ricas β numΓ©ricas.
|
| 5 |
-
# β’ Capas ambientales y βLocalidadβ son categΓ³ricas.
|
| 6 |
-
# β’ Fix: env_df llama a capas_conteos (sin typo).
|
| 7 |
-
# β’ No se usa statsmodels (trendline eliminado).
|
| 8 |
|
| 9 |
import re, unicodedata, warnings, branca, folium, gradio as gr
|
| 10 |
import pandas as pd, geopandas as gpd, numpy as np
|
| 11 |
from shapely.geometry import Point
|
| 12 |
from folium.plugins import HeatMap
|
| 13 |
from sklearn.cluster import DBSCAN
|
| 14 |
-
import plotly.express as px
|
| 15 |
-
import pandas.api.types as ptypes
|
| 16 |
warnings.filterwarnings("ignore")
|
| 17 |
|
| 18 |
-
# ββββββββββββββββββββββββββββ
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
# ββββββββββββββββββββββββββββ
|
| 28 |
-
# 1 Β· Rutas, metadatos
|
| 29 |
-
# ββββββββββββββββββββββββββββ
|
| 30 |
DATA_XLSX = "VasculitisAsociadasA-Bdd3_DATA_LABELS_2025-04-16_1949 (1).xlsx"
|
| 31 |
LOCALIDADES = "loca.json"
|
| 32 |
GEO_AMBIENTALES = {
|
|
@@ -39,32 +29,29 @@ GEO_AMBIENTALES = {
|
|
| 39 |
"WQI": "tramo_wqi.geojson",
|
| 40 |
"Heatmap pacientes": None
|
| 41 |
}
|
| 42 |
-
META_CAPAS={
|
| 43 |
-
"PM10":("conc_pm10","Β΅g/mΒ³", branca.colormap.linear.OrRd_09, "id","Zona"),
|
| 44 |
-
"PM2.5":("conc_pm25","Β΅g/mΒ³",branca.colormap.linear.Reds_09, "id","Zona"),
|
| 45 |
-
"Ozono":("conc_ozono","ppb",
|
| 46 |
-
"Temperatura":("temperatur","Β°C",branca.colormap.linear.YlOrBr_09,"id","Zona"),
|
| 47 |
-
"PrecipitaciΓ³n":("precip_per","mm",branca.colormap.linear.Blues_09,"id","Zona"),
|
| 48 |
-
"Viento":("velocidad","m/s",branca.colormap.linear.GnBu_09,"id","Zona"),
|
| 49 |
-
"WQI":("wqi_val","",branca.colormap.linear.Greens_09,"tramo","Tramo")
|
| 50 |
}
|
| 51 |
ENV_VARS=[k for k in GEO_AMBIENTALES if k not in ("WQI","Heatmap pacientes")]
|
| 52 |
|
| 53 |
-
# ββββββββββββββββββββββββββββ
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
# ββββββββββββββββββββββββββββ
|
| 57 |
-
df_all=pd.read_excel(DATA_XLSX,dtype=str)
|
| 58 |
-
df_all.columns=snake(df_all.columns)
|
| 59 |
|
| 60 |
-
lat_col=next(c for c in df_all.columns if "residencia" in c and "latitud"
|
| 61 |
-
lon_col=next(c for c in df_all.columns if "residencia" in c and "longitud" in c)
|
| 62 |
-
df_all=df_all.rename(columns={lat_col:"latitud_raw", lon_col:"longitud_raw"})
|
| 63 |
-
df_all["latitud"]=pd.to_numeric(df_all["latitud_raw"].str.replace(",", "."),errors="coerce")
|
| 64 |
-
df_all["longitud"]=pd.to_numeric(df_all["longitud_raw"].str.replace(",", "."),errors="coerce")
|
| 65 |
|
| 66 |
-
df_all["genero_cat"]=sin_dato(df_all.get("genero"))
|
| 67 |
-
df_all["estrato_cat"]=sin_dato(df_all.get("estrato_socioeconomico"))
|
| 68 |
|
| 69 |
bins=list(range(0,105,5))
|
| 70 |
age_labels=[f"{b}-{b+4}" for b in bins[:-1]]
|
|
@@ -72,18 +59,18 @@ age_order=age_labels+["Sin dato"]
|
|
| 72 |
df_all["edad"]=pd.to_numeric(df_all.get("edad_en_anos_del_paciente").str.replace(",", "."),errors="coerce")
|
| 73 |
df_all["edad_cat"]=pd.Categorical(
|
| 74 |
sin_dato(pd.cut(df_all["edad"],bins=bins,labels=age_labels,right=False).astype(str)),
|
| 75 |
-
categories=age_order,ordered=True
|
| 76 |
)
|
| 77 |
|
| 78 |
for col in ("ancas","mpo","pr3"):
|
| 79 |
df_all[f"{col}_cat"]=sin_dato(df_all.get(col))
|
| 80 |
|
| 81 |
-
df_all["sindrome_renal"]=sin_dato(df_all.get("sindrome_renal_al_ingreso")).str.capitalize()
|
| 82 |
-
df_all["manifestaciones_extrarenales"]=sin_dato(df_all.get("manifestaciones_extrarenales")).str.capitalize()
|
| 83 |
-
df_all["proteinuria"]=sin_dato(df_all.get("proteinuria")).str.capitalize()
|
| 84 |
-
df_all["creatinina"]=pd.to_numeric(df_all.get("creatinina").str.replace(",", "."),errors="coerce")
|
| 85 |
|
| 86 |
-
ante_cols={
|
| 87 |
"diabetes":"antecedente_personal_de_diabetes",
|
| 88 |
"falla_cardiaca":"antecedente_personal_de_falla_cardiaca",
|
| 89 |
"epoc":"antecedente_personal_de_epoc",
|
|
@@ -92,7 +79,7 @@ ante_cols={
|
|
| 92 |
"autoinmune":"antecedente_personal_de_otra_enfermedad_autoinmune",
|
| 93 |
"cancer":"antecedente_personal_de_cancer"
|
| 94 |
}
|
| 95 |
-
resumen_ante={
|
| 96 |
"diabetes":"Diabetes","falla_cardiaca":"Falla cardΓaca","epoc":"EPOC",
|
| 97 |
"hipertension":"HipertensiΓ³n","vih":"VIH","autoinmune":"Enf. autoinmune","cancer":"CΓ‘ncer"}
|
| 98 |
for k,col in ante_cols.items():
|
|
@@ -120,9 +107,7 @@ df_all["biopsia_patrones"]=df_all.apply(hallar,axis=1)
|
|
| 120 |
df_all["biopsia_patron_str"]=df_all["biopsia_patrones"].apply("; ".join)
|
| 121 |
df_all["biopsia_positiva"]=np.where(df_all["biopsia_patron_str"]=="Sin dato","No","Si")
|
| 122 |
|
| 123 |
-
# ββββββββββββββββββββββββββββ
|
| 124 |
-
# 3 Β· Localidades
|
| 125 |
-
# ββββββββββββββββββββββββββββ
|
| 126 |
geo_loc=gpd.read_file(LOCALIDADES).to_crs("EPSG:4326")
|
| 127 |
geo_loc.columns=snake(geo_loc.columns)
|
| 128 |
loc_col=next(c for c in geo_loc.columns if "localidad" in c or "locnombre" in c)
|
|
@@ -130,38 +115,33 @@ geo_loc=geo_loc.rename(columns={loc_col:"localidad"})
|
|
| 130 |
geo_loc["localidad"]=geo_loc["localidad"].str.upper()
|
| 131 |
|
| 132 |
df_geo=df_all.dropna(subset=["latitud","longitud"]).copy()
|
| 133 |
-
df_geo["geometry"]=
|
|
|
|
| 134 |
df_geo=gpd.GeoDataFrame(df_geo,geometry="geometry",crs="EPSG:4326")
|
| 135 |
df_geo=gpd.sjoin(df_geo,geo_loc[["localidad","geometry"]],how="left",
|
| 136 |
predicate="within").drop(columns="index_right")
|
| 137 |
df_all=df_all.merge(df_geo[["localidad"]],how="left",left_index=True,right_index=True)
|
| 138 |
|
| 139 |
-
# ββββββββββββββββββββββββββββ
|
| 140 |
-
# 4 Β· Capas ambientales (como antes)
|
| 141 |
-
# ββββββββββββββββββββββββββββ
|
| 142 |
def load_gjson(p):
|
| 143 |
g=gpd.read_file(p).to_crs("EPSG:4326"); g.columns=snake(g.columns)
|
| 144 |
for c in g.columns:
|
| 145 |
-
if
|
| 146 |
-
elif g[c].dtype==object:
|
| 147 |
txt=g[c].str.strip()
|
| 148 |
-
g[c]=
|
| 149 |
return g
|
| 150 |
caps_base={k:load_gjson(v) for k,v in GEO_AMBIENTALES.items() if v}
|
| 151 |
|
| 152 |
wqi_bins=[0,20,35,50,70,100]
|
| 153 |
wqi_labels=["Pobre","Marginal","Regular","Buena","Excelente"]
|
| 154 |
-
wqi_colors=["red","olive","purple","green","blue"]
|
| 155 |
g_wqi=caps_base["WQI"].copy()
|
| 156 |
g_wqi["wqi_val"]=pd.to_numeric(g_wqi["wqi"],errors="coerce")
|
| 157 |
g_wqi["wqi_cat"]=pd.cut(g_wqi["wqi_val"],bins=wqi_bins,labels=wqi_labels,include_lowest=True)
|
| 158 |
caps_base["WQI"]=g_wqi
|
| 159 |
-
|
| 160 |
-
|
| 161 |
|
| 162 |
-
# ββββββββββββββββββββββββββββ
|
| 163 |
-
# 5 Β· Filtros (igual)
|
| 164 |
-
# ββββββββββββββββββββββββββββ
|
| 165 |
def filtrar(d,gen,edades,locs,renal,ants,bios,anca,mpo,pr3):
|
| 166 |
d2=d.copy()
|
| 167 |
if gen!="Todos": d2=d2[d2["genero_cat"]==gen]
|
|
@@ -179,9 +159,7 @@ def filtrar(d,gen,edades,locs,renal,ants,bios,anca,mpo,pr3):
|
|
| 179 |
d2=d2[d2[key]==1]
|
| 180 |
return d2
|
| 181 |
|
| 182 |
-
# ββββββββββββββββββββββββββββ
|
| 183 |
-
# 6 Β· Conteos dinΓ‘micos
|
| 184 |
-
# ββββββββββββββββββββββββββββ
|
| 185 |
def capas_conteos(d_pts):
|
| 186 |
caps={}
|
| 187 |
pts=d_pts[["geometry"]]
|
|
@@ -196,65 +174,83 @@ def capas_conteos(d_pts):
|
|
| 196 |
caps[capa]=gdf
|
| 197 |
return caps
|
| 198 |
|
| 199 |
-
# ββββββββββββββββββββββββββββ
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
ants="; ".join(v for k,v in resumen_ante.items() if r.get(k)==1) or "Ninguno"
|
| 206 |
-
return (f"<b>Localidad:</b> {r['localidad']}<br>"
|
| 207 |
-
f"<b>Edad:</b> {edad}<br>"
|
| 208 |
f"<b>GΓ©nero:</b> {r['genero_cat']}<br>"
|
| 209 |
-
f"{
|
| 210 |
-
f"{_linea_pos('MPO',r['mpo_cat'])}"
|
| 211 |
-
f"{_linea_pos('PR3',r['pr3_cat'])}"
|
| 212 |
f"<b>Biopsia:</b> {'; '.join(r['biopsia_patrones'])}<br>"
|
| 213 |
-
f"<b>Antecedentes:</b>
|
| 214 |
|
| 215 |
def choropleth(m,g,val,title,cmap,zfield,zalias):
|
| 216 |
g=g.copy(); g[val]=pd.to_numeric(g[val],errors="coerce")
|
| 217 |
cm=cmap.scale(g[val].min(),g[val].max()); cm.caption=title; cm.add_to(m)
|
| 218 |
is_line=g.geometry.iloc[0].geom_type.startswith("Line")
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
fields=[zfield,val]; aliases=[zalias,title]
|
| 224 |
if "pacientes" in g.columns and val!="pacientes":
|
| 225 |
fields.append("pacientes"); aliases.append("Pacientes")
|
| 226 |
if "wqi_cat" in g.columns:
|
| 227 |
fields.insert(2,"wqi_cat"); aliases.insert(2,"Calidad")
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
folium.GeoJson(g,name=title,style_function=sty,
|
| 231 |
-
highlight_function=lambda f:{"weight":2,"color":"#444","fillOpacity":0.95},
|
| 232 |
-
tooltip=folium.GeoJsonTooltip(fields=fields,aliases=aliases,sticky=True)
|
| 233 |
-
).add_to(m)
|
| 234 |
-
|
| 235 |
-
def capa_clusters(m,d):
|
| 236 |
-
if len(d)<3: return
|
| 237 |
-
coords=np.radians(d[["latitud","longitud"]].astype(float))
|
| 238 |
-
labels=DBSCAN(eps=1/6371,min_samples=3,metric="haversine").fit_predict(coords)
|
| 239 |
-
d=d.copy(); d["cluster"]=labels
|
| 240 |
-
pal=branca.colormap.linear.Set1_09
|
| 241 |
-
fg=folium.FeatureGroup(name="ClΓΊsteres (1 km)",overlay=True)
|
| 242 |
-
for cl in sorted(c for c in d["cluster"].unique() if c!=-1):
|
| 243 |
-
color=pal(cl/max(1,d["cluster"].nunique()-1))
|
| 244 |
-
for _,r in d[d["cluster"]==cl].iterrows():
|
| 245 |
-
folium.CircleMarker(location=(r["latitud"],r["longitud"]),radius=6,
|
| 246 |
-
color=color,fill=True,fill_color=color,
|
| 247 |
-
fill_opacity=0.9,weight=1,
|
| 248 |
-
popup=f"<b>ClΓΊster #{cl}</b><br>"+_popup_paciente(r)
|
| 249 |
-
).add_to(fg)
|
| 250 |
-
fg.add_to(m)
|
| 251 |
|
| 252 |
def crear_mapa(d_filt,capas_sel,ver_cluster):
|
| 253 |
-
d_pts=
|
| 254 |
-
d_filt.dropna(subset=["latitud","longitud"]).copy(),
|
| 255 |
-
geometry=[Point(xy) for xy in zip(d_filt["longitud"].astype(float),
|
| 256 |
-
d_filt["latitud"].astype(float))],
|
| 257 |
-
crs="EPSG:4326")
|
| 258 |
caps=capas_conteos(d_pts)
|
| 259 |
|
| 260 |
g=d_pts.groupby("localidad").size().reset_index(name="pacientes")
|
|
@@ -266,178 +262,95 @@ def crear_mapa(d_filt,capas_sel,ver_cluster):
|
|
| 266 |
for capa in capas_sel:
|
| 267 |
if capa=="Heatmap pacientes": continue
|
| 268 |
if capa=="WQI":
|
| 269 |
-
|
| 270 |
-
choropleth(m,caps["WQI"],"wqi_val","WQI",
|
| 271 |
val,uni,cmap,zf,za=META_CAPAS[capa]
|
| 272 |
-
choropleth(m,caps[capa],val,f"{capa}
|
| 273 |
|
| 274 |
if "Heatmap pacientes" in capas_sel and not d_pts.empty:
|
| 275 |
HeatMap(d_pts[["latitud","longitud"]].astype(float).values,
|
| 276 |
radius=18,name="Heatmap pacientes").add_to(m)
|
| 277 |
|
| 278 |
-
|
| 279 |
for _,r in d_pts.iterrows():
|
| 280 |
-
folium.CircleMarker(
|
| 281 |
fill=True,fill_color="white",fill_opacity=0.85,
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
if ver_cluster
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
folium.LayerControl(collapsed=False).add_to(m)
|
| 287 |
return m._repr_html_()
|
| 288 |
|
| 289 |
-
# ββββββββββββββββββββββββββββ
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 336 |
|
| 337 |
-
def g_bi(x,y,d):
|
| 338 |
-
# Manejo especial: si alguno es ambiental o Localidad β categΓ³ricos
|
| 339 |
-
cat_x=(x in ENV_VARS) or (x=="Localidad") or is_categorical(d[col_of(x)],x)
|
| 340 |
-
cat_y=(y in ENV_VARS) or (y=="Localidad") or is_categorical(d[col_of(y)],y)
|
| 341 |
-
|
| 342 |
-
# Ambiental: convertir a tabla βzona vs pacientesβ
|
| 343 |
-
def prep_env(v):
|
| 344 |
-
d_pts=gpd.GeoDataFrame(
|
| 345 |
-
d.dropna(subset=["latitud","longitud"]).copy(),
|
| 346 |
-
geometry=[Point(xy) for xy in zip(d["longitud"].astype(float),
|
| 347 |
-
d["latitud"].astype(float))],
|
| 348 |
-
crs="EPSG:4326")
|
| 349 |
-
df=env_df(v,d_pts).rename(columns={"zona":v,"pacientes":f"Pac_{v}"})
|
| 350 |
-
return df
|
| 351 |
-
|
| 352 |
-
if x in ENV_VARS and y in ENV_VARS:
|
| 353 |
-
dx=prep_env(x); dy=prep_env(y)
|
| 354 |
-
df=dx.merge(dy,left_index=True,right_index=True)
|
| 355 |
-
return px.scatter(df,x=f"Pac_{x}",y=f"Pac_{y}",
|
| 356 |
-
labels={f"Pac_{x}":f"Pacientes {x}",f"Pac_{y}":f"Pacientes {y}"},
|
| 357 |
-
title=f"{x} vs {y}")
|
| 358 |
-
|
| 359 |
-
# ambos categΓ³ricos β bar apilado
|
| 360 |
-
if cat_x and cat_y:
|
| 361 |
-
d2=d.copy()
|
| 362 |
-
d2["x"]=d2[col_of(x)] if x not in ENV_VARS else \
|
| 363 |
-
prep_env(x).loc[d2.index, x]
|
| 364 |
-
d2["y"]=d2[col_of(y)] if y not in ENV_VARS else \
|
| 365 |
-
prep_env(y).loc[d2.index, y]
|
| 366 |
-
return px.histogram(d2,x="x",color="y",barnorm="percent",title=f"{x} vs {y}",
|
| 367 |
-
labels={"x":x,"y":y})
|
| 368 |
-
|
| 369 |
-
# uno categΓ³rico (x) otro numΓ©rico (y) o viceversa β box
|
| 370 |
-
if cat_x and not cat_y:
|
| 371 |
-
return px.box(d,x=col_of(x),y=col_of(y),points="all",
|
| 372 |
-
title=f"{x} vs {y}",labels={col_of(x):x,col_of(y):y})
|
| 373 |
-
if not cat_x and cat_y:
|
| 374 |
-
return px.box(d,x=col_of(y),y=col_of(x),points="all",
|
| 375 |
-
title=f"{x} vs {y}",labels={col_of(y):y,col_of(x):x})
|
| 376 |
-
|
| 377 |
-
# ambos numΓ©ricos
|
| 378 |
-
return px.scatter(d,x=col_of(x),y=col_of(y),title=f"{x} vs {y}",
|
| 379 |
-
labels={col_of(x):x,col_of(y):y})
|
| 380 |
-
|
| 381 |
-
# ββββββββββββββββββββββββββββ
|
| 382 |
-
# 9 Β· Interfaz (idΓ©ntica salvo lista vars_all)
|
| 383 |
-
# ββββββββββββββββββββββββββββ
|
| 384 |
-
def interfaz():
|
| 385 |
-
gen=["Todos"]+sorted(df_all["genero_cat"].unique())
|
| 386 |
-
ages=list(df_all["edad_cat"].dtype.categories)
|
| 387 |
-
locs=sorted(df_all["localidad"].fillna("Sin dato").unique())
|
| 388 |
-
ancas=["Todos"]+sorted(df_all["ancas_cat"].unique())
|
| 389 |
-
mpos=["Todos"]+sorted(df_all["mpo_cat"].unique())
|
| 390 |
-
pr3s=["Todos"]+sorted(df_all["pr3_cat"].unique())
|
| 391 |
-
|
| 392 |
-
vars_cat=["Localidad"]+ENV_VARS+[
|
| 393 |
-
"genero_cat","estrato_cat","edad_cat","sindrome_renal",
|
| 394 |
-
"manifestaciones_extrarenales","proteinuria",
|
| 395 |
-
"ancas_cat","mpo_cat","pr3_cat",
|
| 396 |
-
"PatrΓ³n biopsia"]+list(resumen_ante.values())
|
| 397 |
-
vars_num=["edad","creatinina"]
|
| 398 |
-
vars_all=vars_cat+vars_num
|
| 399 |
-
|
| 400 |
-
with gr.Blocks(title="Vasculitis ANCA BogotΓ‘") as demo:
|
| 401 |
-
gr.Markdown("## Explorador geoespacial β Vasculitis ANCA (BogotΓ‘)")
|
| 402 |
-
with gr.Row():
|
| 403 |
-
ui_gen=gr.Dropdown(gen,label="GΓ©nero",value="Todos")
|
| 404 |
-
ui_age=gr.CheckboxGroup(ages,label="Edad (quinquenios)")
|
| 405 |
-
ui_loc=gr.Dropdown(locs,multiselect=True,label="Localidades")
|
| 406 |
-
ui_renal=gr.Dropdown(["Todos","Si","No"],value="Todos",label="Compromiso renal")
|
| 407 |
-
ui_ant=gr.CheckboxGroup(["Todos"]+list(resumen_ante.values()),label="Antecedentes")
|
| 408 |
-
ui_bio=gr.CheckboxGroup(["Todos"]+list(raw2short.values()),label="PatrΓ³n biopsia")
|
| 409 |
-
with gr.Row():
|
| 410 |
-
ui_anca=gr.Dropdown(ancas,label="ANCA",value="Todos")
|
| 411 |
-
ui_mpo =gr.Dropdown(mpos,label="MPO",value="Todos")
|
| 412 |
-
ui_pr3 =gr.Dropdown(pr3s,label="PR3",value="Todos")
|
| 413 |
-
ui_capas=gr.CheckboxGroup(list(GEO_AMBIENTALES.keys()),label="Capas mapa")
|
| 414 |
-
ui_clu=gr.Checkbox(label="Mostrar clΓΊsteres (1 km)")
|
| 415 |
-
|
| 416 |
-
with gr.Tab("Mapa"):
|
| 417 |
-
btn_map=gr.Button("Generar mapa"); out_map=gr.HTML()
|
| 418 |
-
btn_map.click(lambda *i:crear_mapa(filtrar(df_all,*i[:-2]),i[-2],i[-1]),
|
| 419 |
-
inputs=[ui_gen,ui_age,ui_loc,ui_renal,
|
| 420 |
-
ui_ant,ui_bio,ui_anca,ui_mpo,ui_pr3,
|
| 421 |
-
ui_capas,ui_clu],
|
| 422 |
-
outputs=out_map)
|
| 423 |
-
|
| 424 |
-
with gr.Tab("Univariado"):
|
| 425 |
-
ui_var=gr.Dropdown(vars_all,label="Variable"); btn_uni=gr.Button("Graficar"); out_uni=gr.Plot()
|
| 426 |
-
btn_uni.click(lambda v,*i:g_uni(v,filtrar(df_all,*i)),
|
| 427 |
-
inputs=[ui_var,ui_gen,ui_age,ui_loc,ui_renal,
|
| 428 |
-
ui_ant,ui_bio,ui_anca,ui_mpo,ui_pr3],
|
| 429 |
-
outputs=out_uni)
|
| 430 |
-
|
| 431 |
-
with gr.Tab("Bivariado"):
|
| 432 |
-
ui_x=gr.Dropdown(vars_all,label="Variable X")
|
| 433 |
-
ui_y=gr.Dropdown(vars_all,label="Variable Y")
|
| 434 |
-
btn_bi=gr.Button("Graficar"); out_bi=gr.Plot()
|
| 435 |
-
btn_bi.click(lambda x,y,*i:g_bi(x,y,filtrar(df_all,*i)),
|
| 436 |
-
inputs=[ui_x,ui_y,ui_gen,ui_age,ui_loc,ui_renal,
|
| 437 |
-
ui_ant,ui_bio,ui_anca,ui_mpo,ui_pr3],
|
| 438 |
-
outputs=out_bi)
|
| 439 |
-
demo.launch()
|
| 440 |
-
|
| 441 |
-
# βββββββββββββββββββββββββββοΏ½οΏ½
|
| 442 |
if __name__=="__main__":
|
| 443 |
-
|
|
|
|
| 1 |
# app.py β Explorador geoespacial Vasculitis ANCA (BogotΓ‘)
|
| 2 |
+
# VersiΓ³n 30-abr-2025
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
import re, unicodedata, warnings, branca, folium, gradio as gr
|
| 5 |
import pandas as pd, geopandas as gpd, numpy as np
|
| 6 |
from shapely.geometry import Point
|
| 7 |
from folium.plugins import HeatMap
|
| 8 |
from sklearn.cluster import DBSCAN
|
| 9 |
+
import plotly.express as px
|
|
|
|
| 10 |
warnings.filterwarnings("ignore")
|
| 11 |
|
| 12 |
+
# βββββββββββββββββββββββββ helpers βββββββββββββββββββββββββ
|
| 13 |
+
norm = lambda t: unicodedata.normalize("NFKD", t).encode("ascii","ignore").decode()
|
| 14 |
+
snake = lambda cols:[re.sub(r"[^\w]+","_",norm(c).strip().lower()).strip("_") for c in cols]
|
| 15 |
+
sin_dato = lambda s:s.fillna("Sin dato").replace("", "Sin dato")
|
| 16 |
+
|
| 17 |
+
NUM_VARS = ["edad", "creatinina"] # β ΓΊnicamente numΓ©ricas
|
| 18 |
+
|
| 19 |
+
# βββββββββββββββββββββββββ rutas / metadatos βββββββββββββββ
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
DATA_XLSX = "VasculitisAsociadasA-Bdd3_DATA_LABELS_2025-04-16_1949 (1).xlsx"
|
| 21 |
LOCALIDADES = "loca.json"
|
| 22 |
GEO_AMBIENTALES = {
|
|
|
|
| 29 |
"WQI": "tramo_wqi.geojson",
|
| 30 |
"Heatmap pacientes": None
|
| 31 |
}
|
| 32 |
+
META_CAPAS = {
|
| 33 |
+
"PM10": ("conc_pm10","Β΅g/mΒ³", branca.colormap.linear.OrRd_09, "id","Zona"),
|
| 34 |
+
"PM2.5": ("conc_pm25","Β΅g/mΒ³", branca.colormap.linear.Reds_09, "id","Zona"),
|
| 35 |
+
"Ozono": ("conc_ozono","ppb", branca.colormap.linear.PuBuGn_09,"id","Zona"),
|
| 36 |
+
"Temperatura": ("temperatur","Β°C", branca.colormap.linear.YlOrBr_09,"id","Zona"),
|
| 37 |
+
"PrecipitaciΓ³n": ("precip_per","mm", branca.colormap.linear.Blues_09,"id","Zona"),
|
| 38 |
+
"Viento": ("velocidad","m/s", branca.colormap.linear.GnBu_09, "id","Zona"),
|
| 39 |
+
"WQI": ("wqi_val","", branca.colormap.linear.Greens_09,"tramo","Tramo")
|
| 40 |
}
|
| 41 |
ENV_VARS=[k for k in GEO_AMBIENTALES if k not in ("WQI","Heatmap pacientes")]
|
| 42 |
|
| 43 |
+
# βββββββββββββββββββββββββ carga de pacientes ββββββββββββββ
|
| 44 |
+
df_all = pd.read_excel(DATA_XLSX, dtype=str)
|
| 45 |
+
df_all.columns = snake(df_all.columns)
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
lat_col = next(c for c in df_all.columns if "residencia" in c and "latitud" in c)
|
| 48 |
+
lon_col = next(c for c in df_all.columns if "residencia" in c and "longitud" in c)
|
| 49 |
+
df_all = df_all.rename(columns={lat_col:"latitud_raw", lon_col:"longitud_raw"})
|
| 50 |
+
df_all["latitud"] = pd.to_numeric(df_all["latitud_raw"].str.replace(",", "."), errors="coerce")
|
| 51 |
+
df_all["longitud"] = pd.to_numeric(df_all["longitud_raw"].str.replace(",", "."), errors="coerce")
|
| 52 |
|
| 53 |
+
df_all["genero_cat"] = sin_dato(df_all.get("genero"))
|
| 54 |
+
df_all["estrato_cat"] = sin_dato(df_all.get("estrato_socioeconomico"))
|
| 55 |
|
| 56 |
bins=list(range(0,105,5))
|
| 57 |
age_labels=[f"{b}-{b+4}" for b in bins[:-1]]
|
|
|
|
| 59 |
df_all["edad"]=pd.to_numeric(df_all.get("edad_en_anos_del_paciente").str.replace(",", "."),errors="coerce")
|
| 60 |
df_all["edad_cat"]=pd.Categorical(
|
| 61 |
sin_dato(pd.cut(df_all["edad"],bins=bins,labels=age_labels,right=False).astype(str)),
|
| 62 |
+
categories=age_order, ordered=True
|
| 63 |
)
|
| 64 |
|
| 65 |
for col in ("ancas","mpo","pr3"):
|
| 66 |
df_all[f"{col}_cat"]=sin_dato(df_all.get(col))
|
| 67 |
|
| 68 |
+
df_all["sindrome_renal"] = sin_dato(df_all.get("sindrome_renal_al_ingreso")).str.capitalize()
|
| 69 |
+
df_all["manifestaciones_extrarenales"] = sin_dato(df_all.get("manifestaciones_extrarenales")).str.capitalize()
|
| 70 |
+
df_all["proteinuria"] = sin_dato(df_all.get("proteinuria")).str.capitalize()
|
| 71 |
+
df_all["creatinina"] = pd.to_numeric(df_all.get("creatinina").str.replace(",", "."),errors="coerce")
|
| 72 |
|
| 73 |
+
ante_cols = {
|
| 74 |
"diabetes":"antecedente_personal_de_diabetes",
|
| 75 |
"falla_cardiaca":"antecedente_personal_de_falla_cardiaca",
|
| 76 |
"epoc":"antecedente_personal_de_epoc",
|
|
|
|
| 79 |
"autoinmune":"antecedente_personal_de_otra_enfermedad_autoinmune",
|
| 80 |
"cancer":"antecedente_personal_de_cancer"
|
| 81 |
}
|
| 82 |
+
resumen_ante = {
|
| 83 |
"diabetes":"Diabetes","falla_cardiaca":"Falla cardΓaca","epoc":"EPOC",
|
| 84 |
"hipertension":"HipertensiΓ³n","vih":"VIH","autoinmune":"Enf. autoinmune","cancer":"CΓ‘ncer"}
|
| 85 |
for k,col in ante_cols.items():
|
|
|
|
| 107 |
df_all["biopsia_patron_str"]=df_all["biopsia_patrones"].apply("; ".join)
|
| 108 |
df_all["biopsia_positiva"]=np.where(df_all["biopsia_patron_str"]=="Sin dato","No","Si")
|
| 109 |
|
| 110 |
+
# βββββββββββββββββββββββββ localidades βββββββββββββββββββββ
|
|
|
|
|
|
|
| 111 |
geo_loc=gpd.read_file(LOCALIDADES).to_crs("EPSG:4326")
|
| 112 |
geo_loc.columns=snake(geo_loc.columns)
|
| 113 |
loc_col=next(c for c in geo_loc.columns if "localidad" in c or "locnombre" in c)
|
|
|
|
| 115 |
geo_loc["localidad"]=geo_loc["localidad"].str.upper()
|
| 116 |
|
| 117 |
df_geo=df_all.dropna(subset=["latitud","longitud"]).copy()
|
| 118 |
+
df_geo["geometry"]=[Point(xy) for xy in zip(df_geo["longitud"].astype(float),
|
| 119 |
+
df_geo["latitud"].astype(float))]
|
| 120 |
df_geo=gpd.GeoDataFrame(df_geo,geometry="geometry",crs="EPSG:4326")
|
| 121 |
df_geo=gpd.sjoin(df_geo,geo_loc[["localidad","geometry"]],how="left",
|
| 122 |
predicate="within").drop(columns="index_right")
|
| 123 |
df_all=df_all.merge(df_geo[["localidad"]],how="left",left_index=True,right_index=True)
|
| 124 |
|
| 125 |
+
# βββββββββββββββββββββββββ cargar capas base βββββββββββββββ
|
|
|
|
|
|
|
| 126 |
def load_gjson(p):
|
| 127 |
g=gpd.read_file(p).to_crs("EPSG:4326"); g.columns=snake(g.columns)
|
| 128 |
for c in g.columns:
|
| 129 |
+
if g[c].dtype==object:
|
|
|
|
| 130 |
txt=g[c].str.strip()
|
| 131 |
+
g[c]=pd.to_numeric(txt,errors="ignore")
|
| 132 |
return g
|
| 133 |
caps_base={k:load_gjson(v) for k,v in GEO_AMBIENTALES.items() if v}
|
| 134 |
|
| 135 |
wqi_bins=[0,20,35,50,70,100]
|
| 136 |
wqi_labels=["Pobre","Marginal","Regular","Buena","Excelente"]
|
|
|
|
| 137 |
g_wqi=caps_base["WQI"].copy()
|
| 138 |
g_wqi["wqi_val"]=pd.to_numeric(g_wqi["wqi"],errors="coerce")
|
| 139 |
g_wqi["wqi_cat"]=pd.cut(g_wqi["wqi_val"],bins=wqi_bins,labels=wqi_labels,include_lowest=True)
|
| 140 |
caps_base["WQI"]=g_wqi
|
| 141 |
+
wqi_cmap=branca.colormap.StepColormap(
|
| 142 |
+
colors=["red","olive","purple","green","blue"],index=wqi_bins,caption="WQI")
|
| 143 |
|
| 144 |
+
# βββββββββββββββββββββββββ filtros βββββββββββββββββββββββββ
|
|
|
|
|
|
|
| 145 |
def filtrar(d,gen,edades,locs,renal,ants,bios,anca,mpo,pr3):
|
| 146 |
d2=d.copy()
|
| 147 |
if gen!="Todos": d2=d2[d2["genero_cat"]==gen]
|
|
|
|
| 159 |
d2=d2[d2[key]==1]
|
| 160 |
return d2
|
| 161 |
|
| 162 |
+
# βββββββββββββββββββββββββ conteos dinΓ‘micos βββββββββββββββ
|
|
|
|
|
|
|
| 163 |
def capas_conteos(d_pts):
|
| 164 |
caps={}
|
| 165 |
pts=d_pts[["geometry"]]
|
|
|
|
| 174 |
caps[capa]=gdf
|
| 175 |
return caps
|
| 176 |
|
| 177 |
+
# βββββββββββββββββββββββββ grΓ‘ficos auxiliares βββββββββββββ
|
| 178 |
+
def prep_pts(d):
|
| 179 |
+
df=d.dropna(subset=["latitud","longitud"]).copy()
|
| 180 |
+
df["geometry"]=[Point(xy) for xy in zip(df["longitud"].astype(float),
|
| 181 |
+
df["latitud"].astype(float))]
|
| 182 |
+
return gpd.GeoDataFrame(df,geometry="geometry",crs="EPSG:4326")
|
| 183 |
+
|
| 184 |
+
def env_df(var,d_pts):
|
| 185 |
+
caps=capas_conteos(d_pts)
|
| 186 |
+
g=caps[var].copy()
|
| 187 |
+
val,uni,*_=META_CAPAS[var]
|
| 188 |
+
g["cat"]=g.apply(lambda r:f"Zona {r['id']} ({r[val]:.1f} {uni})",axis=1)
|
| 189 |
+
return g[["cat","pacientes"]]
|
| 190 |
+
|
| 191 |
+
def is_num(v): return v in NUM_VARS
|
| 192 |
+
|
| 193 |
+
# βββββββββββββββββββββββββ univariado ββββββββββββββββββββββ
|
| 194 |
+
def g_uni(var,d):
|
| 195 |
+
if var in ENV_VARS:
|
| 196 |
+
df=env_df(var,prep_pts(d))
|
| 197 |
+
return px.bar(df,x="cat",y="pacientes",text_auto=True,title=var,
|
| 198 |
+
labels={"cat":"Zona","pacientes":"Pacientes"})
|
| 199 |
+
|
| 200 |
+
if var=="Localidad":
|
| 201 |
+
s=d["localidad"].fillna("Sin dato")
|
| 202 |
+
return px.histogram(s,x=s,text_auto=True,title="Localidad")
|
| 203 |
+
|
| 204 |
+
if is_num(var):
|
| 205 |
+
return px.histogram(d,x=var,nbins=20,title=var)
|
| 206 |
+
|
| 207 |
+
return px.histogram(d,x=var,text_auto=True,title=var)
|
| 208 |
+
|
| 209 |
+
# βββββββββββββββββββββββββ bivariado βββββββββββββββββββββββ
|
| 210 |
+
def g_bi(x,y,d):
|
| 211 |
+
if x in ENV_VARS: d[x]=env_df(x,prep_pts(d)).loc[d.index,"cat"]
|
| 212 |
+
if y in ENV_VARS: d[y]=env_df(y,prep_pts(d)).loc[d.index,"cat"]
|
| 213 |
+
|
| 214 |
+
num_x,num_y=is_num(x),is_num(y)
|
| 215 |
+
|
| 216 |
+
if not num_x and not num_y:
|
| 217 |
+
return px.histogram(d,x=x,color=y,barnorm="percent",
|
| 218 |
+
title=f"{x} vs {y}")
|
| 219 |
+
if num_x and not num_y:
|
| 220 |
+
return px.box(d,x=y,y=x,points="all",title=f"{x} vs {y}")
|
| 221 |
+
if not num_x and num_y:
|
| 222 |
+
return px.box(d,x=x,y=y,points="all",title=f"{x} vs {y}")
|
| 223 |
+
return px.scatter(d,x=x,y=y,title=f"{x} vs {y}")
|
| 224 |
+
|
| 225 |
+
# βββββββββββββββββββββββββ mapas / pop-ups βββββββββββββββββ
|
| 226 |
+
def popup(r):
|
| 227 |
+
sn=lambda k:f"<b>{k}:</b> Positivo<br>" if r[k.lower()+"_cat"].lower()=="positivo" else ""
|
| 228 |
+
edad=f"{int(r['edad'])} aΓ±os" if pd.notna(r['edad']) else "Sin dato edad"
|
| 229 |
ants="; ".join(v for k,v in resumen_ante.items() if r.get(k)==1) or "Ninguno"
|
| 230 |
+
return (f"<b>Localidad:</b> {r['localidad']}<br><b>Edad:</b> {edad}<br>"
|
|
|
|
| 231 |
f"<b>GΓ©nero:</b> {r['genero_cat']}<br>"
|
| 232 |
+
f"{sn('ANCA')}{sn('MPO')}{sn('PR3')}"
|
|
|
|
|
|
|
| 233 |
f"<b>Biopsia:</b> {'; '.join(r['biopsia_patrones'])}<br>"
|
| 234 |
+
f"<b>Antecedentes:</b> {ants}")
|
| 235 |
|
| 236 |
def choropleth(m,g,val,title,cmap,zfield,zalias):
|
| 237 |
g=g.copy(); g[val]=pd.to_numeric(g[val],errors="coerce")
|
| 238 |
cm=cmap.scale(g[val].min(),g[val].max()); cm.caption=title; cm.add_to(m)
|
| 239 |
is_line=g.geometry.iloc[0].geom_type.startswith("Line")
|
| 240 |
+
style=lambda f,vc=val:({"color":cm(f['properties'][vc]),"weight":4,"opacity":0.9}
|
| 241 |
+
if is_line else
|
| 242 |
+
{"fillColor":cm(f['properties'][vc]),"fillOpacity":0.8,
|
| 243 |
+
"color":"black","weight":0.3})
|
| 244 |
fields=[zfield,val]; aliases=[zalias,title]
|
| 245 |
if "pacientes" in g.columns and val!="pacientes":
|
| 246 |
fields.append("pacientes"); aliases.append("Pacientes")
|
| 247 |
if "wqi_cat" in g.columns:
|
| 248 |
fields.insert(2,"wqi_cat"); aliases.insert(2,"Calidad")
|
| 249 |
+
folium.GeoJson(g,name=title,style_function=style,
|
| 250 |
+
tooltip=folium.GeoJsonTooltip(fields,aliases,sticky=True)).add_to(m)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
|
| 252 |
def crear_mapa(d_filt,capas_sel,ver_cluster):
|
| 253 |
+
d_pts=prep_pts(d_filt)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
caps=capas_conteos(d_pts)
|
| 255 |
|
| 256 |
g=d_pts.groupby("localidad").size().reset_index(name="pacientes")
|
|
|
|
| 262 |
for capa in capas_sel:
|
| 263 |
if capa=="Heatmap pacientes": continue
|
| 264 |
if capa=="WQI":
|
| 265 |
+
wqi_cmap.add_to(m)
|
| 266 |
+
choropleth(m,caps["WQI"],"wqi_val","WQI",wqi_cmap,"tramo","Tramo"); continue
|
| 267 |
val,uni,cmap,zf,za=META_CAPAS[capa]
|
| 268 |
+
choropleth(m,caps[capa],val,f"{capa} ({uni})",cmap,zf,za)
|
| 269 |
|
| 270 |
if "Heatmap pacientes" in capas_sel and not d_pts.empty:
|
| 271 |
HeatMap(d_pts[["latitud","longitud"]].astype(float).values,
|
| 272 |
radius=18,name="Heatmap pacientes").add_to(m)
|
| 273 |
|
| 274 |
+
pts=folium.FeatureGroup("Puntos pacientes",overlay=True)
|
| 275 |
for _,r in d_pts.iterrows():
|
| 276 |
+
folium.CircleMarker((r["latitud"],r["longitud"]),radius=5,color="#c00",
|
| 277 |
fill=True,fill_color="white",fill_opacity=0.85,
|
| 278 |
+
popup=popup(r)).add_to(pts)
|
| 279 |
+
pts.add_to(m)
|
| 280 |
+
|
| 281 |
+
if ver_cluster and len(d_pts)>2:
|
| 282 |
+
coords=np.radians(d_pts[["latitud","longitud"]].astype(float))
|
| 283 |
+
lab=DBSCAN(eps=1/6371,min_samples=3,metric="haversine").fit_predict(coords)
|
| 284 |
+
d_pts["cluster"]=lab
|
| 285 |
+
cl_fg=folium.FeatureGroup("ClΓΊsteres (1 km)",overlay=True)
|
| 286 |
+
pal=branca.colormap.linear.Set1_09
|
| 287 |
+
for cl in sorted(c for c in d_pts["cluster"].unique() if c!=-1):
|
| 288 |
+
color=pal(cl/max(1,d_pts["cluster"].nunique()-1))
|
| 289 |
+
for _,r in d_pts[d_pts["cluster"]==cl].iterrows():
|
| 290 |
+
folium.CircleMarker((r["latitud"],r["longitud"]),radius=6,color=color,
|
| 291 |
+
fill=True,fill_color=color,fill_opacity=0.9,
|
| 292 |
+
popup=f"<b>ClΓΊster {cl}</b><br>"+popup(r)
|
| 293 |
+
).add_to(cl_fg)
|
| 294 |
+
cl_fg.add_to(m)
|
| 295 |
+
|
| 296 |
folium.LayerControl(collapsed=False).add_to(m)
|
| 297 |
return m._repr_html_()
|
| 298 |
|
| 299 |
+
# βββββββββββββββββββββββββ interfaz ββββββββββββββββββββββββ
|
| 300 |
+
gen_opts=["Todos"]+sorted(df_all["genero_cat"].unique())
|
| 301 |
+
age_opts=list(df_all["edad_cat"].dtype.categories)
|
| 302 |
+
loc_opts=sorted(df_all["localidad"].fillna("Sin dato").unique())
|
| 303 |
+
anca_opts=["Todos"]+sorted(df_all["ancas_cat"].unique())
|
| 304 |
+
mpo_opts=["Todos"]+sorted(df_all["mpo_cat"].unique())
|
| 305 |
+
pr3_opts=["Todos"]+sorted(df_all["pr3_cat"].unique())
|
| 306 |
+
|
| 307 |
+
vars_cat=["Localidad"]+ENV_VARS+[
|
| 308 |
+
"genero_cat","estrato_cat","edad_cat","sindrome_renal",
|
| 309 |
+
"manifestaciones_extrarenales","proteinuria",
|
| 310 |
+
"ancas_cat","mpo_cat","pr3_cat","PatrΓ³n biopsia"]+list(resumen_ante.values())
|
| 311 |
+
vars_all=vars_cat+NUM_VARS
|
| 312 |
+
|
| 313 |
+
with gr.Blocks(title="Vasculitis ANCA BogotΓ‘") as demo:
|
| 314 |
+
gr.Markdown("## Explorador geoespacial β Vasculitis ANCA (BogotΓ‘)")
|
| 315 |
+
with gr.Row():
|
| 316 |
+
ui_gen=gr.Dropdown(gen_opts,label="GΓ©nero",value="Todos")
|
| 317 |
+
ui_age=gr.CheckboxGroup(age_opts,label="Edad (quinquenios)")
|
| 318 |
+
ui_loc =gr.Dropdown(loc_opts,multiselect=True,label="Localidades")
|
| 319 |
+
ui_renal =gr.Dropdown(["Todos","Si","No"],value="Todos",label="Compromiso renal")
|
| 320 |
+
ui_ant =gr.CheckboxGroup(["Todos"]+list(resumen_ante.values()),label="Antecedentes")
|
| 321 |
+
ui_bio =gr.CheckboxGroup(["Todos"]+list(raw2short.values()),label="PatrΓ³n biopsia")
|
| 322 |
+
with gr.Row():
|
| 323 |
+
ui_anca=gr.Dropdown(anca_opts,label="ANCA",value="Todos")
|
| 324 |
+
ui_mpo =gr.Dropdown(mpo_opts,label="MPO", value="Todos")
|
| 325 |
+
ui_pr3 =gr.Dropdown(pr3_opts,label="PR3", value="Todos")
|
| 326 |
+
|
| 327 |
+
ui_capas=gr.CheckboxGroup(list(GEO_AMBIENTALES.keys()),label="Capas mapa")
|
| 328 |
+
ui_clu =gr.Checkbox(label="Mostrar clΓΊsteres (1 km)")
|
| 329 |
+
|
| 330 |
+
with gr.Tab("Mapa"):
|
| 331 |
+
btn_map=gr.Button("Generar mapa"); out_map=gr.HTML()
|
| 332 |
+
btn_map.click(lambda *i:crear_mapa(filtrar(df_all,*i[:-2]),i[-2],i[-1]),
|
| 333 |
+
inputs=[ui_gen,ui_age,ui_loc,ui_renal,
|
| 334 |
+
ui_ant,ui_bio,ui_anca,ui_mpo,ui_pr3,
|
| 335 |
+
ui_capas,ui_clu],
|
| 336 |
+
outputs=out_map)
|
| 337 |
+
|
| 338 |
+
with gr.Tab("Univariado"):
|
| 339 |
+
ui_var=gr.Dropdown(vars_all,label="Variable"); btn_uni=gr.Button("Graficar"); out_uni=gr.Plot()
|
| 340 |
+
btn_uni.click(lambda v,*i:g_uni(v,filtrar(df_all,*i)),
|
| 341 |
+
inputs=[ui_var,ui_gen,ui_age,ui_loc,ui_renal,
|
| 342 |
+
ui_ant,ui_bio,ui_anca,ui_mpo,ui_pr3],
|
| 343 |
+
outputs=out_uni)
|
| 344 |
+
|
| 345 |
+
with gr.Tab("Bivariado"):
|
| 346 |
+
ui_x=gr.Dropdown(vars_all,label="Variable X")
|
| 347 |
+
ui_y=gr.Dropdown(vars_all,label="Variable Y")
|
| 348 |
+
btn_bi=gr.Button("Graficar"); out_bi=gr.Plot()
|
| 349 |
+
btn_bi.click(lambda x,y,*i:g_bi(x,y,filtrar(df_all,*i)),
|
| 350 |
+
inputs=[ui_x,ui_y,ui_gen,ui_age,ui_loc,ui_renal,
|
| 351 |
+
ui_ant,ui_bio,ui_anca,ui_mpo,ui_pr3],
|
| 352 |
+
outputs=out_bi)
|
| 353 |
+
demo.launch()
|
| 354 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 355 |
if __name__=="__main__":
|
| 356 |
+
demo.launch()
|