Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -42,9 +42,6 @@ def generar_tabla(n_filas, concentracion_inicial, unidad_medida, n_replicas):
|
|
42 |
for i in range(1, n_replicas + 1):
|
43 |
df[f"Concentración Real {i} ({unidad_medida})"] = None
|
44 |
|
45 |
-
# Añadir columna para incluir o excluir filas
|
46 |
-
df["Incluir"] = True
|
47 |
-
|
48 |
# Las columnas de promedio y desviación estándar se agregarán durante el análisis
|
49 |
return df
|
50 |
|
@@ -270,10 +267,16 @@ Fecha: {datetime.now().strftime('%d/%m/%Y %H:%M')}
|
|
270 |
"""
|
271 |
return informe, evaluacion['estado']
|
272 |
|
273 |
-
def actualizar_analisis(df, n_replicas, unidad_medida):
|
274 |
if df is None or df.empty:
|
275 |
return "Error en los datos", None, "No se pueden generar análisis", df
|
276 |
|
|
|
|
|
|
|
|
|
|
|
|
|
277 |
# Calcular promedio y desviación estándar dependiendo de las réplicas
|
278 |
df = calcular_promedio_desviacion(df, n_replicas, unidad_medida)
|
279 |
|
@@ -286,9 +289,8 @@ def actualizar_analisis(df, n_replicas, unidad_medida):
|
|
286 |
|
287 |
df_valid = df.dropna(subset=[col_predicha_num, col_real_promedio])
|
288 |
|
289 |
-
# Filtrar filas según
|
290 |
-
|
291 |
-
df_valid = df_valid[df_valid['Incluir'] == True]
|
292 |
|
293 |
if len(df_valid) < 2:
|
294 |
return "Se necesitan más datos", None, "Se requieren al menos dos valores reales para el análisis", df
|
@@ -315,7 +317,8 @@ def actualizar_graficos(df, n_replicas, unidad_medida,
|
|
315 |
palette_linea_ajuste, estilo_linea_ajuste,
|
316 |
palette_linea_ideal, estilo_linea_ideal,
|
317 |
palette_barras_error,
|
318 |
-
mostrar_linea_ajuste, mostrar_linea_ideal, mostrar_puntos
|
|
|
319 |
if df is None or df.empty:
|
320 |
return None
|
321 |
|
@@ -331,9 +334,14 @@ def actualizar_graficos(df, n_replicas, unidad_medida,
|
|
331 |
|
332 |
df_valid = df.dropna(subset=[col_predicha_num, col_real_promedio])
|
333 |
|
334 |
-
#
|
335 |
-
if
|
336 |
-
|
|
|
|
|
|
|
|
|
|
|
337 |
|
338 |
if len(df_valid) < 2:
|
339 |
return None
|
@@ -441,7 +449,7 @@ def exportar_informe_latex(df_valid, informe_md):
|
|
441 |
f.write(informe_tex)
|
442 |
return filename
|
443 |
|
444 |
-
def exportar_word(df, informe_md, unidad_medida):
|
445 |
df_valid = df.copy()
|
446 |
col_predicha_num = "Concentración Predicha Numérica"
|
447 |
col_real_promedio = f"Concentración Real Promedio ({unidad_medida})"
|
@@ -452,9 +460,14 @@ def exportar_word(df, informe_md, unidad_medida):
|
|
452 |
|
453 |
df_valid = df_valid.dropna(subset=[col_predicha_num, col_real_promedio])
|
454 |
|
455 |
-
#
|
456 |
-
if
|
457 |
-
|
|
|
|
|
|
|
|
|
|
|
458 |
|
459 |
if df_valid.empty:
|
460 |
return None
|
@@ -463,7 +476,7 @@ def exportar_word(df, informe_md, unidad_medida):
|
|
463 |
|
464 |
return filename # Retornamos el nombre del archivo
|
465 |
|
466 |
-
def exportar_latex(df, informe_md):
|
467 |
df_valid = df.copy()
|
468 |
col_predicha_num = "Concentración Predicha Numérica"
|
469 |
col_real_promedio = [col for col in df_valid.columns if 'Real Promedio' in col][0]
|
@@ -474,9 +487,14 @@ def exportar_latex(df, informe_md):
|
|
474 |
|
475 |
df_valid = df_valid.dropna(subset=[col_predicha_num, col_real_promedio])
|
476 |
|
477 |
-
#
|
478 |
-
if
|
479 |
-
|
|
|
|
|
|
|
|
|
|
|
480 |
|
481 |
if df_valid.empty:
|
482 |
return None
|
@@ -550,15 +568,6 @@ def actualizar_tabla_evento(df, n_filas, concentracion, unidad, n_replicas):
|
|
550 |
if idx in df.index:
|
551 |
df_new.at[idx, col_new] = df.at[idx, col_old]
|
552 |
|
553 |
-
# Mapear la columna "Incluir"
|
554 |
-
if 'Incluir' in df.columns:
|
555 |
-
df_new['Incluir'] = True
|
556 |
-
for idx in df_new.index:
|
557 |
-
if idx in df.index:
|
558 |
-
df_new.at[idx, 'Incluir'] = df.at[idx, 'Incluir']
|
559 |
-
else:
|
560 |
-
df_new['Incluir'] = True
|
561 |
-
|
562 |
return df_new
|
563 |
|
564 |
def cargar_excel(file):
|
@@ -649,6 +658,13 @@ with gr.Blocks(theme=gr.themes.Soft()) as interfaz:
|
|
649 |
estado_output = gr.Textbox(label="Estado", interactive=False)
|
650 |
graficos_output = gr.Plot(label="Gráficos de Análisis")
|
651 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
652 |
# Opciones y botones debajo del gráfico
|
653 |
with gr.Row():
|
654 |
# Paletas de colores disponibles en Seaborn
|
@@ -712,10 +728,24 @@ with gr.Blocks(theme=gr.themes.Soft()) as interfaz:
|
|
712 |
input_components = [tabla_output]
|
713 |
output_components = [estado_output, graficos_output, informe_output, tabla_output]
|
714 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
715 |
# Evento al presionar el botón Calcular
|
716 |
calcular_btn.click(
|
717 |
fn=actualizar_analisis,
|
718 |
-
inputs=[tabla_output, replicas_slider, unidad_input],
|
719 |
outputs=output_components
|
720 |
)
|
721 |
|
@@ -728,7 +758,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as interfaz:
|
|
728 |
palette_linea_ajuste_dropdown, estilo_linea_ajuste_dropdown,
|
729 |
palette_linea_ideal_dropdown, estilo_linea_ideal_dropdown,
|
730 |
palette_barras_error_dropdown,
|
731 |
-
mostrar_linea_ajuste, mostrar_linea_ideal, mostrar_puntos
|
|
|
732 |
],
|
733 |
outputs=graficos_output
|
734 |
)
|
@@ -826,13 +857,13 @@ with gr.Blocks(theme=gr.themes.Soft()) as interfaz:
|
|
826 |
# Eventos de exportar informes
|
827 |
exportar_word_btn.click(
|
828 |
fn=exportar_word,
|
829 |
-
inputs=[tabla_output, informe_output, unidad_input],
|
830 |
outputs=exportar_word_file
|
831 |
)
|
832 |
|
833 |
exportar_latex_btn.click(
|
834 |
fn=exportar_latex,
|
835 |
-
inputs=[tabla_output, informe_output],
|
836 |
outputs=exportar_latex_file
|
837 |
)
|
838 |
|
@@ -842,7 +873,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as interfaz:
|
|
842 |
df = generar_tabla(7, 2000000, "UFC", n_replicas)
|
843 |
# Valores reales de ejemplo
|
844 |
df[f"Concentración Real 1 (UFC)"] = [2000000, 1600000, 1200000, 800000, 400000, 200000, 100000]
|
845 |
-
estado, fig, informe, df = actualizar_analisis(df, n_replicas, "UFC")
|
846 |
return (
|
847 |
2000000,
|
848 |
"UFC",
|
|
|
42 |
for i in range(1, n_replicas + 1):
|
43 |
df[f"Concentración Real {i} ({unidad_medida})"] = None
|
44 |
|
|
|
|
|
|
|
45 |
# Las columnas de promedio y desviación estándar se agregarán durante el análisis
|
46 |
return df
|
47 |
|
|
|
267 |
"""
|
268 |
return informe, evaluacion['estado']
|
269 |
|
270 |
+
def actualizar_analisis(df, n_replicas, unidad_medida, filas_seleccionadas):
|
271 |
if df is None or df.empty:
|
272 |
return "Error en los datos", None, "No se pueden generar análisis", df
|
273 |
|
274 |
+
# Convertir filas_seleccionadas a índices
|
275 |
+
if not filas_seleccionadas:
|
276 |
+
return "Se necesitan más datos", None, "No se han seleccionado filas para el análisis", df
|
277 |
+
|
278 |
+
indices_seleccionados = [int(s.split(' ')[1]) - 1 for s in filas_seleccionadas]
|
279 |
+
|
280 |
# Calcular promedio y desviación estándar dependiendo de las réplicas
|
281 |
df = calcular_promedio_desviacion(df, n_replicas, unidad_medida)
|
282 |
|
|
|
289 |
|
290 |
df_valid = df.dropna(subset=[col_predicha_num, col_real_promedio])
|
291 |
|
292 |
+
# Filtrar filas según las seleccionadas
|
293 |
+
df_valid = df_valid.loc[indices_seleccionados]
|
|
|
294 |
|
295 |
if len(df_valid) < 2:
|
296 |
return "Se necesitan más datos", None, "Se requieren al menos dos valores reales para el análisis", df
|
|
|
317 |
palette_linea_ajuste, estilo_linea_ajuste,
|
318 |
palette_linea_ideal, estilo_linea_ideal,
|
319 |
palette_barras_error,
|
320 |
+
mostrar_linea_ajuste, mostrar_linea_ideal, mostrar_puntos,
|
321 |
+
filas_seleccionadas):
|
322 |
if df is None or df.empty:
|
323 |
return None
|
324 |
|
|
|
334 |
|
335 |
df_valid = df.dropna(subset=[col_predicha_num, col_real_promedio])
|
336 |
|
337 |
+
# Convertir filas_seleccionadas a índices
|
338 |
+
if not filas_seleccionadas:
|
339 |
+
return None
|
340 |
+
|
341 |
+
indices_seleccionados = [int(s.split(' ')[1]) - 1 for s in filas_seleccionadas]
|
342 |
+
|
343 |
+
# Filtrar filas según las seleccionadas
|
344 |
+
df_valid = df_valid.loc[indices_seleccionados]
|
345 |
|
346 |
if len(df_valid) < 2:
|
347 |
return None
|
|
|
449 |
f.write(informe_tex)
|
450 |
return filename
|
451 |
|
452 |
+
def exportar_word(df, informe_md, unidad_medida, filas_seleccionadas):
|
453 |
df_valid = df.copy()
|
454 |
col_predicha_num = "Concentración Predicha Numérica"
|
455 |
col_real_promedio = f"Concentración Real Promedio ({unidad_medida})"
|
|
|
460 |
|
461 |
df_valid = df_valid.dropna(subset=[col_predicha_num, col_real_promedio])
|
462 |
|
463 |
+
# Convertir filas_seleccionadas a índices
|
464 |
+
if not filas_seleccionadas:
|
465 |
+
return None
|
466 |
+
|
467 |
+
indices_seleccionados = [int(s.split(' ')[1]) - 1 for s in filas_seleccionadas]
|
468 |
+
|
469 |
+
# Filtrar filas según las seleccionadas
|
470 |
+
df_valid = df_valid.loc[indices_seleccionados]
|
471 |
|
472 |
if df_valid.empty:
|
473 |
return None
|
|
|
476 |
|
477 |
return filename # Retornamos el nombre del archivo
|
478 |
|
479 |
+
def exportar_latex(df, informe_md, filas_seleccionadas):
|
480 |
df_valid = df.copy()
|
481 |
col_predicha_num = "Concentración Predicha Numérica"
|
482 |
col_real_promedio = [col for col in df_valid.columns if 'Real Promedio' in col][0]
|
|
|
487 |
|
488 |
df_valid = df_valid.dropna(subset=[col_predicha_num, col_real_promedio])
|
489 |
|
490 |
+
# Convertir filas_seleccionadas a índices
|
491 |
+
if not filas_seleccionadas:
|
492 |
+
return None
|
493 |
+
|
494 |
+
indices_seleccionados = [int(s.split(' ')[1]) - 1 for s in filas_seleccionadas]
|
495 |
+
|
496 |
+
# Filtrar filas según las seleccionadas
|
497 |
+
df_valid = df_valid.loc[indices_seleccionados]
|
498 |
|
499 |
if df_valid.empty:
|
500 |
return None
|
|
|
568 |
if idx in df.index:
|
569 |
df_new.at[idx, col_new] = df.at[idx, col_old]
|
570 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
571 |
return df_new
|
572 |
|
573 |
def cargar_excel(file):
|
|
|
658 |
estado_output = gr.Textbox(label="Estado", interactive=False)
|
659 |
graficos_output = gr.Plot(label="Gráficos de Análisis")
|
660 |
|
661 |
+
# Añadimos el Multiselect para seleccionar las filas
|
662 |
+
filas_seleccionadas = gr.Multiselect(
|
663 |
+
label="Seleccione las filas a incluir en el análisis",
|
664 |
+
choices=[],
|
665 |
+
value=[],
|
666 |
+
)
|
667 |
+
|
668 |
# Opciones y botones debajo del gráfico
|
669 |
with gr.Row():
|
670 |
# Paletas de colores disponibles en Seaborn
|
|
|
728 |
input_components = [tabla_output]
|
729 |
output_components = [estado_output, graficos_output, informe_output, tabla_output]
|
730 |
|
731 |
+
# Evento para actualizar las opciones de filas
|
732 |
+
def actualizar_opciones_filas(df):
|
733 |
+
if df is None or df.empty:
|
734 |
+
return gr.update(choices=[], value=[])
|
735 |
+
else:
|
736 |
+
opciones = [f"Fila {i+1}" for i in df.index]
|
737 |
+
return gr.update(choices=opciones, value=opciones)
|
738 |
+
|
739 |
+
tabla_output.change(
|
740 |
+
fn=actualizar_opciones_filas,
|
741 |
+
inputs=[tabla_output],
|
742 |
+
outputs=filas_seleccionadas
|
743 |
+
)
|
744 |
+
|
745 |
# Evento al presionar el botón Calcular
|
746 |
calcular_btn.click(
|
747 |
fn=actualizar_analisis,
|
748 |
+
inputs=[tabla_output, replicas_slider, unidad_input, filas_seleccionadas],
|
749 |
outputs=output_components
|
750 |
)
|
751 |
|
|
|
758 |
palette_linea_ajuste_dropdown, estilo_linea_ajuste_dropdown,
|
759 |
palette_linea_ideal_dropdown, estilo_linea_ideal_dropdown,
|
760 |
palette_barras_error_dropdown,
|
761 |
+
mostrar_linea_ajuste, mostrar_linea_ideal, mostrar_puntos,
|
762 |
+
filas_seleccionadas
|
763 |
],
|
764 |
outputs=graficos_output
|
765 |
)
|
|
|
857 |
# Eventos de exportar informes
|
858 |
exportar_word_btn.click(
|
859 |
fn=exportar_word,
|
860 |
+
inputs=[tabla_output, informe_output, unidad_input, filas_seleccionadas],
|
861 |
outputs=exportar_word_file
|
862 |
)
|
863 |
|
864 |
exportar_latex_btn.click(
|
865 |
fn=exportar_latex,
|
866 |
+
inputs=[tabla_output, informe_output, filas_seleccionadas],
|
867 |
outputs=exportar_latex_file
|
868 |
)
|
869 |
|
|
|
873 |
df = generar_tabla(7, 2000000, "UFC", n_replicas)
|
874 |
# Valores reales de ejemplo
|
875 |
df[f"Concentración Real 1 (UFC)"] = [2000000, 1600000, 1200000, 800000, 400000, 200000, 100000]
|
876 |
+
estado, fig, informe, df = actualizar_analisis(df, n_replicas, "UFC", [f"Fila {i+1}" for i in df.index])
|
877 |
return (
|
878 |
2000000,
|
879 |
"UFC",
|