Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
I
inventario
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jose Luis Gordillo Ruiz
inventario
Commits
8a8851af
Commit
8a8851af
authored
Jul 09, 2025
by
Jose Luis Gordillo Ruiz
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'endpoints'
parents
5d6d40dc
e3f11c42
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
1 additions
and
335 deletions
+1
-335
estadisticas.py
+1
-335
No files found.
estadisticas.py
View file @
8a8851af
...
@@ -14,39 +14,7 @@ import flask
...
@@ -14,39 +14,7 @@ import flask
server
=
flask
.
Flask
(
__name__
)
server
=
flask
.
Flask
(
__name__
)
def
update_correlacion_macro_iauc
():
datos
=
comidas
.
sort_values
(
by
=
"iAUC"
,
ascending
=
True
)
datos
=
datos
[
np
.
abs
(
stats
.
zscore
(
datos
[
"iAUC"
],
nan_policy
=
'omit'
))
<
3
]
datos
.
reset_index
(
inplace
=
True
)
metricas
=
[
"hc_total"
,
"lipids_total"
,
"protein_total"
,
"fiber_total"
]
pcts
=
[
metrica
+
"_pct"
for
metrica
in
metricas
]
datos
[
"total"
]
=
datos
[
metricas
]
.
sum
(
axis
=
1
)
datos
=
datos
[
datos
[
"total"
]
>
0
]
datos
[
pcts
]
=
datos
[
metricas
]
.
div
(
datos
[
"total"
],
axis
=
0
)
fig
=
make_subplots
(
rows
=
2
,
cols
=
4
,
vertical_spacing
=
0.25
)
lcol
=
1
seq
=
1
for
metrica
in
metricas
:
datosfil
=
datos
[
np
.
abs
(
stats
.
zscore
(
datos
[
metrica
],
nan_policy
=
'omit'
))
<
3
]
fig
.
add_trace
(
go
.
Scatter
(
x
=
datosfil
[
"iAUC"
],
y
=
datosfil
[
metrica
],
name
=
metrica
,
mode
=
'markers'
),
row
=
1
,
col
=
lcol
)
fig
.
add_trace
(
go
.
Scatter
(
x
=
datosfil
[
"iAUC"
],
y
=
datosfil
[
metrica
+
"_pct"
],
name
=
metrica
,
mode
=
'markers'
),
row
=
2
,
col
=
lcol
)
fig
[
"layout"
][
'xaxis'
+
str
(
seq
)][
'title'
]
=
"iAUC"
fig
[
"layout"
][
'yaxis'
+
str
(
seq
)][
'title'
]
=
metrica
fig
[
"layout"
][
'xaxis'
+
str
(
seq
+
4
)][
'title'
]
=
"iAUC"
fig
[
"layout"
][
'yaxis'
+
str
(
seq
+
4
)][
'title'
]
=
"pct de "
+
metrica
lcol
=
lcol
+
1
seq
=
seq
+
1
return
fig
def
datavis
(
datos
):
def
datavis
(
datos
):
medidas
=
[
"imc"
,
"age"
,
"glucose"
,
"hba1c"
,
"ct"
,
"hdl"
]
medidas
=
[
"imc"
,
"age"
,
"glucose"
,
"hba1c"
,
"ct"
,
"hdl"
]
...
@@ -254,151 +222,11 @@ def triareas(pacientes):
...
@@ -254,151 +222,11 @@ def triareas(pacientes):
)
)
return
figura
return
figura
def
comidas_iaucs
(
datos
):
columnas
=
[
"pre-AUC"
,
"post-AUC"
,
"iAUC"
]
datos
[
"N-alimentos"
]
=
datos
[
"foods"
]
.
apply
(
lambda
x
:
len
(
x
))
fig
=
make_subplots
(
cols
=
3
,
rows
=
1
,
vertical_spacing
=
0.25
)
lcol
=
1
seq
=
1
for
columna
in
columnas
:
datosfil
=
datos
[
np
.
abs
(
stats
.
zscore
(
datos
[
columna
],
nan_policy
=
'omit'
))
<
3
]
fig
.
add_trace
(
go
.
Scatter
(
y
=
datosfil
[
columna
],
name
=
columna
,
mode
=
"markers"
),
row
=
1
,
col
=
lcol
)
fig
[
'layout'
][
'yaxis'
+
str
(
seq
)][
'title'
]
=
columna
fig
[
'layout'
][
'xaxis'
+
str
(
seq
)][
'title'
]
=
"Comida ID"
seq
=
seq
+
1
lcol
=
lcol
+
1
fig
.
update_layout
(
showlegend
=
False
)
return
fig
def
comidas_iauc_describe
(
datos
):
columnas
=
[
"pre-AUC"
,
"post-AUC"
,
"iAUC"
]
fig
=
make_subplots
(
cols
=
3
,
rows
=
1
,
vertical_spacing
=
0.25
)
seq
=
1
lcol
=
1
for
columna
in
columnas
:
datosfil
=
datos
[
np
.
abs
(
stats
.
zscore
(
datos
[
columna
],
nan_policy
=
'omit'
))
<
3
]
fig
.
add_trace
(
go
.
Histogram
(
x
=
datosfil
[
columna
],
name
=
columna
),
row
=
1
,
col
=
lcol
)
fig
[
'layout'
][
'yaxis'
+
str
(
seq
)][
'title'
]
=
columna
seq
=
seq
+
1
lcol
=
lcol
+
1
fig
.
update_layout
(
showlegend
=
False
,
bargap
=
0.05
,
# gap between bars of adjacent location coordinates
bargroupgap
=
0.05
# gap between bars of the same location coordinates
)
return
fig
def
comidas_macro_contenido
(
datos
):
columnas
=
[
"hc_total"
,
"lipids_total"
,
"fiber_total"
,
"protein_total"
]
datos
[
"N-alimentos"
]
=
datos
[
"foods"
]
.
apply
(
lambda
x
:
len
(
x
))
figura
=
make_subplots
(
cols
=
4
,
rows
=
1
,
vertical_spacing
=
0.24
)
lcol
=
1
seq
=
1
for
columna
in
columnas
:
datosfil
=
datos
[
np
.
abs
(
stats
.
zscore
(
datos
[
columna
],
nan_policy
=
'omit'
))
<
3
]
figura
.
add_trace
(
go
.
Scatter
(
y
=
datosfil
[
columna
],
name
=
columna
,
mode
=
"markers"
),
row
=
1
,
col
=
lcol
)
figura
[
'layout'
][
'yaxis'
+
str
(
seq
)][
'title'
]
=
columna
figura
[
'layout'
][
'xaxis'
+
str
(
seq
)][
'title'
]
=
"Comida ID"
seq
=
seq
+
1
lcol
=
lcol
+
1
if
lcol
<
4
else
1
figura
.
update_layout
(
showlegend
=
False
)
return
figura
def
comidas_macro_describe
(
datos
):
columnas
=
[
"hc_total"
,
"lipids_total"
,
"fiber_total"
,
"protein_total"
]
fig
=
make_subplots
(
cols
=
4
,
rows
=
1
,
vertical_spacing
=
0.25
)
seq
=
1
lcol
=
1
for
columna
in
columnas
:
datosfil
=
datos
[
np
.
abs
(
stats
.
zscore
(
datos
[
columna
],
nan_policy
=
'omit'
))
<
3
]
fig
.
add_trace
(
go
.
Histogram
(
x
=
datosfil
[
columna
],
name
=
columna
),
row
=
1
,
col
=
lcol
)
fig
[
'layout'
][
'yaxis'
+
str
(
seq
)][
'title'
]
=
columna
seq
=
seq
+
1
lcol
=
lcol
+
1
if
lcol
<
4
else
1
fig
.
update_layout
(
showlegend
=
False
,
bargap
=
0.05
,
# gap between bars of adjacent location coordinates
bargroupgap
=
0.05
# gap between bars of the same location coordinates
)
return
fig
def
comidas_porcentajes
(
datos
):
columnas
=
[
"hc_total"
,
"fiber_total"
,
"protein_total"
,
"lipids_total"
]
datos
[
"total"
]
=
datos
[
columnas
]
.
sum
(
axis
=
1
)
datos
=
datos
[
datos
[
"total"
]
>
0
]
datos
[
columnas
]
=
datos
[
columnas
]
.
div
(
datos
[
"total"
],
axis
=
0
)
figura
=
ff
.
create_distplot
([
datos
[
columna
]
for
columna
in
columnas
],
columnas
,
show_hist
=
False
,
show_rug
=
False
)
return
figura
def
comidas_principales
(
datos
):
dominantes
=
datos
.
groupby
(
"dominante"
)
.
count
()
dominantes
.
sort_values
(
by
=
"patient"
,
ascending
=
False
,
inplace
=
True
)
dominantes
=
dominantes
[
dominantes
[
"patient"
]
>
9
]
figura
=
make_subplots
(
cols
=
1
,
rows
=
1
,
subplot_titles
=
[
"Alimentos"
])
figura
.
add_trace
(
go
.
Scatter
(
x
=
dominantes
.
index
,
y
=
dominantes
[
"patient"
],
mode
=
'markers'
,
name
=
"# de comidas"
),
col
=
1
,
row
=
1
)
figura
[
"layout"
][
'yaxis'
][
'title'
]
=
"# de comidas"
# figura.update_layout(height=2000, width=800)
return
figura
def
comidas_principales_categorias
(
datos
):
categorias
=
datos
.
groupby
(
"dominante_category"
)
.
count
()
categorias
.
sort_values
(
by
=
"patient"
,
ascending
=
False
,
inplace
=
True
)
categorias
=
categorias
[
categorias
[
"patient"
]
>
9
]
figura
=
make_subplots
(
cols
=
1
,
rows
=
1
,
subplot_titles
=
[
"Categorías de alimentos"
])
figura
.
add_trace
(
go
.
Scatter
(
x
=
categorias
.
index
,
y
=
categorias
[
"patient"
],
mode
=
'markers'
),
col
=
1
,
row
=
1
)
figura
[
"layout"
][
'yaxis'
][
'title'
]
=
"# de comidas"
return
figura
def
categorias_promedio_aucs
():
categorias
=
comidas
.
groupby
(
"dominante_category"
)[[
"iAUC"
,
"hc_total"
]]
.
mean
()
print
(
categorias
)
categorias
[
"tamaño"
]
=
comidas
.
groupby
(
"dominante_category"
)[
"iAUC"
]
.
count
()
categorias
.
sort_values
(
ascending
=
False
,
inplace
=
True
,
by
=
"iAUC"
)
# figura = make_subplots(cols=1, rows=1, subplot_titles=["Categorías de alimentos"])
# figura.add_trace(
# go.Scatter(x=categorias.index, y=categorias["iAUC"], mode="markers")
# )
figura
=
px
.
scatter
(
categorias
[
"iAUC"
],
size
=
categorias
[
"tamaño"
])
return
figura
def
categorias_aucs
(
datos
):
datos_fil
=
datos
[
np
.
abs
(
stats
.
zscore
(
datos
[
"iAUC"
],
nan_policy
=
'omit'
))
<
3
]
figura
=
px
.
box
(
datos_fil
,
x
=
"dominante_category"
,
y
=
"iAUC"
)
return
figura
def
aucs_paciente
(
datos
):
figura
=
px
.
box
(
datos
,
x
=
"patient"
,
y
=
"iAUC"
)
return
figura
def
heatmap
(
datos
):
def
heatmap
(
datos
):
...
@@ -418,63 +246,8 @@ def heatmap(datos):
...
@@ -418,63 +246,8 @@ def heatmap(datos):
)
)
return
fig
return
fig
def
comidas_paciente_datos
(
datos
):
columnas
=
[
"hc_total"
,
"kcal_total"
,
"fiber_total"
,
"protein_total"
,
"lipids_total"
]
figura
=
make_subplots
(
cols
=
5
,
rows
=
4
,
subplot_titles
=
(
"hc"
,
"kcal"
,
"fiber"
,
"protein"
,
"lipids"
))
lcol
=
1
for
columna
in
columnas
:
datosfil
=
datos
[
np
.
abs
(
stats
.
zscore
(
datos
[
columna
],
nan_policy
=
'omit'
))
<
3
]
figura
.
add_trace
(
go
.
Scatter
(
y
=
datosfil
.
groupby
(
"patient"
)[
columna
]
.
sum
(),
name
=
"total_"
+
columna
,
mode
=
"markers"
),
row
=
1
,
col
=
lcol
)
figura
.
add_trace
(
go
.
Histogram
(
x
=
datosfil
.
groupby
(
"patient"
)[
columna
]
.
sum
(),
name
=
"total_"
+
columna
),
row
=
2
,
col
=
lcol
)
figura
.
add_trace
(
go
.
Scatter
(
y
=
datosfil
.
groupby
(
"patient"
)[
columna
]
.
mean
(),
name
=
"promedio_"
+
columna
,
mode
=
"markers"
),
row
=
3
,
col
=
lcol
)
figura
.
add_trace
(
go
.
Histogram
(
x
=
datosfil
.
groupby
(
"patient"
)[
columna
]
.
mean
(),
name
=
"promedio_"
+
columna
),
row
=
4
,
col
=
lcol
)
lcol
=
lcol
+
1
figura
.
update_layout
(
showlegend
=
False
)
return
figura
def
comidas_auc_paciente
(
datos
):
columnas
=
[
"pre-AUC"
,
"post-AUC"
,
"iAUC"
]
figura
=
make_subplots
(
cols
=
4
,
rows
=
2
,
subplot_titles
=
(
"pre-AUC"
,
"post-AUC"
,
"iAUC"
,
"N-Comidas"
))
lcol
=
1
for
columna
in
columnas
:
datosfil
=
datos
[
np
.
abs
(
stats
.
zscore
(
datos
[
columna
],
nan_policy
=
'omit'
))
<
3
]
figura
.
add_trace
(
go
.
Scatter
(
y
=
datosfil
.
groupby
(
"patient"
)[
columna
]
.
mean
(),
name
=
"promedio_"
+
columna
,
mode
=
"markers"
),
row
=
1
,
col
=
lcol
)
figura
.
add_trace
(
go
.
Histogram
(
x
=
datosfil
.
groupby
(
"patient"
)[
columna
]
.
mean
(),
name
=
"Distr. "
+
columna
),
row
=
2
,
col
=
lcol
)
lcol
=
lcol
+
1
figura
.
add_trace
(
go
.
Scatter
(
y
=
datosfil
.
groupby
(
"patient"
)[
"iAUC"
]
.
count
(),
name
=
"N-comidas"
,
mode
=
"markers"
),
row
=
1
,
col
=
4
)
figura
.
add_trace
(
go
.
Histogram
(
x
=
datosfil
.
groupby
(
"patient"
)[
"iAUC"
]
.
count
(),
name
=
"Distr. N-comidas"
),
row
=
2
,
col
=
4
)
figura
.
update_layout
(
showlegend
=
False
,
bargap
=
0.05
,
# gap between bars of adjacent location coordinates
bargroupgap
=
0.05
# gap between bars of the same location coordinates
)
return
figura
# algunos parámetros globales
# algunos parámetros globales
url
=
'https://nutricion.c3.unam.mx/nd/'
url
=
'https://nutricion.c3.unam.mx/nd/'
...
@@ -502,61 +275,7 @@ def get_visit_data(visitnumber):
...
@@ -502,61 +275,7 @@ def get_visit_data(visitnumber):
# consulta online a la API para las visitas, que proporcionan la info de
# consulta online a la API para las visitas, que proporcionan la info de
# visitas y microbiota
# visitas y microbiota
visitas_datos
=
[
get_visit_data
(
visita
)
for
visita
in
range
(
1
,
5
)]
visitas_datos
=
[
get_visit_data
(
visita
)
for
visita
in
range
(
1
,
5
)]
comidas
=
pd
.
read_csv
(
"todos-foods.csv"
,
usecols
=
[
"hc_total"
,
"kcal_total"
,
"fiber_total"
,
\
"protein_total"
,
"lipids_total"
,
"fecha_hora"
,
"patient"
,
"visita"
,
\
"glucosa_basal_area"
,
"glucosa_estimulo_area"
,
\
"glucosa_area_efectiva"
,
"glucosa_area_relativa"
,
"foods"
,
"dominante"
,
"dominante_category"
])
comidas
[
"foods"
]
=
comidas
[
"foods"
]
.
apply
(
literal_eval
)
comidas
=
comidas
[
comidas
[
"visita"
]
==
"e1"
]
comidas
=
comidas
[
comidas
[
"hc_total"
]
>
0
]
comidas
.
rename
(
columns
=
{
"glucosa_basal_area"
:
"pre-AUC"
,
"glucosa_estimulo_area"
:
"post-AUC"
,
"glucosa_area_efectiva"
:
"iAUC"
},
inplace
=
True
)
comidas
[
"iAUC"
]
=
comidas
[
"post-AUC"
]
-
comidas
[
"pre-AUC"
]
*
2
print
(
comidas
.
columns
)
# la info de glucómetro y comidas es más compleja. por el momento, se estab armando
# la info offline.
data_o
=
(
pd
.
read_csv
(
"inventario.csv"
)
)
data_o
[
"paciente"
]
=
data_o
[
"paciente"
]
.
astype
(
"str"
)
+
"_p"
data_o
[
"Secuenciaciones"
]
=
0
Total_pacientes
=
len
(
data_o
.
index
)
data
=
data_o
.
loc
[
data_o
[
"incluido"
]]
Total_incluidos
=
len
(
data
)
# gráficas de distribución por indicadores.
visitas_incluidos
=
visitas_datos
[
0
]
.
loc
[
visitas_datos
[
0
][
"include_in_analysis"
]]
visitas_incluidos
=
visitas_datos
[
0
]
.
loc
[
visitas_datos
[
0
][
"include_in_analysis"
]]
# grafica estática de visitas/secuenciaciones
scat
=
px
.
scatter
(
data
,
x
=
"NDias_glucosa"
,
y
=
"Comidas_Aisladas"
,
size
=
"AUCs_glucosa"
)
vis_sec
=
{
"data"
:
[
{
#"x": temp["paciente"],
"x"
:
data
[
"Nvisitas"
]
.
astype
(
"str"
)
+
"_visitas"
,
"type"
:
"histogram"
,
"name"
:
"Visitas"
,
"histnorm"
:
"percent"
,
#"orientation": "v",
},
{
"x"
:
data
[
"Secuenciaciones"
],
"type"
:
"histogram"
,
"name"
:
"Secuenciaciones"
,
"histnorm"
:
"percent"
},
],
"layout"
:
{
#"title": {"text": title + " " + etapalab + str(etapaDet)},
"xaxis"
:
{
"title"
:
{
"text"
:
"Visitas por paciente"
}},
"yaxis"
:
{
"title"
:
{
"text"
:
"
%
de pacientes"
}},
}
}
external_stylesheets
=
[
external_stylesheets
=
[
{
{
"href"
:
(
"href"
:
(
...
@@ -647,59 +366,6 @@ app.layout = html.Div(children=[
...
@@ -647,59 +366,6 @@ app.layout = html.Div(children=[
# html.Div(children= [html.H1("Mapa de calor de iAUC/alimentos controlados", className = "header-title2")]),
# html.Div(children= [html.H1("Mapa de calor de iAUC/alimentos controlados", className = "header-title2")]),
# dcc.Graph(figure=heatmap(visitas_incluidos))
# dcc.Graph(figure=heatmap(visitas_incluidos))
# ,]),
# ,]),
# datos de comidas
html
.
Div
(
children
=
[
html
.
Div
(
children
=
[
html
.
H1
(
"iAUCs de las comidas reportadas"
,
className
=
"header-title2"
)]),
dcc
.
Graph
(
figure
=
comidas_iaucs
(
comidas
)),
dcc
.
Graph
(
figure
=
comidas_iauc_describe
(
comidas
))
,]),
# datos de comidas
html
.
Div
(
children
=
[
html
.
Div
(
children
=
[
html
.
H1
(
"Contenido de macronutrientes de las comidas"
,
className
=
"header-title2"
)]),
dcc
.
Graph
(
figure
=
comidas_macro_contenido
(
comidas
))
,]),
# descripcion de comidas
html
.
Div
(
children
=
[
html
.
Div
(
children
=
[
html
.
H1
(
"Distribución de Macronutrientes de comidas"
,
className
=
"header-title2"
)]),
dcc
.
Graph
(
figure
=
comidas_macro_describe
(
comidas
))
,]),
# descripcion de componentes de macronutrientes de comidas
html
.
Div
(
children
=
[
html
.
Div
(
children
=
[
html
.
H1
(
"Distribución de
%
s de Macronutrientes de comidas"
,
className
=
"header-title2"
)]),
dcc
.
Graph
(
figure
=
comidas_porcentajes
(
comidas
))
,]),
# relación de macronutrientes de comidas con sus iAUC
html
.
Div
(
children
=
[
html
.
Div
(
children
=
[
html
.
H1
(
"Relación de macros / iAUCs de las comidas "
,
className
=
"header-title2"
)]),
dcc
.
Graph
(
figure
=
update_correlacion_macro_iauc
())
,]),
# # de comidas por componente dominante
html
.
Div
(
children
=
[
html
.
Div
(
children
=
[
html
.
H1
(
"Distribución de num. de comidas por componente principal"
,
className
=
"header-title2"
)]),
dcc
.
Graph
(
figure
=
comidas_principales
(
comidas
)),
dcc
.
Graph
(
figure
=
comidas_principales_categorias
(
comidas
)),
dcc
.
Graph
(
figure
=
categorias_promedio_aucs
()),
dcc
.
Graph
(
figure
=
categorias_aucs
(
comidas
))
,]),
# descripcion de comidas por paciente
html
.
Div
(
children
=
[
html
.
Div
(
children
=
[
html
.
H1
(
"Macronutrientes de comidas por paciente"
,
className
=
"header-title2"
)]),
dcc
.
Graph
(
figure
=
comidas_paciente_datos
(
comidas
))
,]),
# descripcion de aucs comidas por paciente
html
.
Div
(
children
=
[
html
.
Div
(
children
=
[
html
.
H1
(
"AUCs promedio de comidas por paciente"
,
className
=
"header-title2"
)]),
dcc
.
Graph
(
figure
=
comidas_auc_paciente
(
comidas
))
,]),
# descripcion de aucs comidas por paciente
html
.
Div
(
children
=
[
html
.
Div
(
children
=
[
html
.
H1
(
"iAUCs de comidas por paciente"
,
className
=
"header-title2"
)]),
dcc
.
Graph
(
figure
=
aucs_paciente
(
comidas
))
,]),
])
# cierre del layout
])
# cierre del layout
...
@@ -751,5 +417,5 @@ def update_correlacion(controlado):
...
@@ -751,5 +417,5 @@ def update_correlacion(controlado):
return
figura
return
figura
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
app
.
run_server
(
debug
=
True
,
port
=
805
1
,
host
=
'0.0.0.0'
)
app
.
run_server
(
debug
=
True
,
port
=
805
2
,
host
=
'0.0.0.0'
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment