Update app.py
Browse files
app.py
CHANGED
@@ -486,7 +486,13 @@ class RSM_BoxBehnken:
|
|
486 |
img_bytes = fig.to_image(format="png")
|
487 |
zip_file.writestr(f'Grafico_{idx}.png', img_bytes)
|
488 |
zip_buffer.seek(0)
|
489 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
490 |
|
491 |
def save_fig_to_bytes(self, fig):
|
492 |
"""
|
@@ -494,6 +500,41 @@ class RSM_BoxBehnken:
|
|
494 |
"""
|
495 |
return fig.to_image(format="png")
|
496 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
497 |
# --- Funciones para la interfaz de Gradio ---
|
498 |
|
499 |
def load_data(x1_name, x2_name, x3_name, y_name, x1_levels_str, x2_levels_str, x3_levels_str, data_str):
|
@@ -548,6 +589,12 @@ def fit_and_optimize_model():
|
|
548 |
equation_formatted = equation.replace(" + ", "<br>+ ").replace(" ** ", "^").replace("*", " × ")
|
549 |
equation_formatted = f"### Ecuación del Modelo Simplificado:<br>{equation_formatted}"
|
550 |
|
|
|
|
|
|
|
|
|
|
|
|
|
551 |
return (
|
552 |
model_completo.summary().as_html(),
|
553 |
pareto_completo,
|
@@ -558,10 +605,11 @@ def fit_and_optimize_model():
|
|
558 |
prediction_table,
|
559 |
contribution_table,
|
560 |
anova_table,
|
561 |
-
|
|
|
562 |
)
|
563 |
|
564 |
-
def show_plot(
|
565 |
if not all_figures:
|
566 |
return None, "No hay gráficos disponibles.", current_index
|
567 |
selected_fig = all_figures[current_index]
|
@@ -592,7 +640,7 @@ def download_current_plot(all_figures, current_index):
|
|
592 |
Descarga la figura actual como PNG.
|
593 |
"""
|
594 |
if not all_figures:
|
595 |
-
return
|
596 |
fig = all_figures[current_index]
|
597 |
img_bytes = rsm.save_fig_to_bytes(fig)
|
598 |
filename = f"Grafico_RSM_{current_index + 1}.png"
|
@@ -602,49 +650,27 @@ def download_current_plot(all_figures, current_index):
|
|
602 |
temp_file.write(img_bytes)
|
603 |
temp_path = temp_file.name
|
604 |
|
605 |
-
return
|
606 |
|
607 |
def download_all_plots_zip(all_figures):
|
608 |
"""
|
609 |
Descarga todas las figuras en un archivo ZIP.
|
610 |
"""
|
611 |
-
|
612 |
-
|
613 |
-
|
614 |
-
|
615 |
-
|
616 |
-
# Crear un archivo temporal para el ZIP
|
617 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".zip") as temp_file:
|
618 |
-
temp_file.write(zip_bytes)
|
619 |
-
temp_path = temp_file.name
|
620 |
-
|
621 |
-
return (temp_path, zip_filename)
|
622 |
-
return (None, "Graficos_RSM.zip") # Nombre por defecto
|
623 |
|
624 |
def download_all_tables_excel():
|
625 |
"""
|
626 |
Descarga todas las tablas en un archivo Excel con múltiples hojas.
|
627 |
"""
|
628 |
if 'rsm' not in globals():
|
629 |
-
return
|
630 |
-
|
631 |
-
|
632 |
-
|
633 |
-
with pd.ExcelWriter(excel_buffer, engine='xlsxwriter') as writer:
|
634 |
-
for sheet_name, table in tables.items():
|
635 |
-
table.to_excel(writer, sheet_name=sheet_name, index=False)
|
636 |
-
excel_buffer.seek(0)
|
637 |
-
excel_bytes = excel_buffer.read()
|
638 |
-
|
639 |
-
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
640 |
-
excel_filename = f"Tablas_RSM_{timestamp}.xlsx"
|
641 |
-
|
642 |
-
# Crear un archivo temporal para el Excel
|
643 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx") as temp_file:
|
644 |
-
temp_file.write(excel_bytes)
|
645 |
-
temp_path = temp_file.name
|
646 |
-
|
647 |
-
return (temp_path, excel_filename)
|
648 |
|
649 |
# --- Crear la interfaz de Gradio ---
|
650 |
|
@@ -704,7 +730,7 @@ with gr.Blocks() as demo:
|
|
704 |
with gr.Column():
|
705 |
gr.Markdown("## Generar Gráficos de Superficie de Respuesta")
|
706 |
fixed_variable_input = gr.Dropdown(label="Variable Fija", choices=["Glucosa", "Extracto_de_Levadura", "Triptofano"], value="Glucosa")
|
707 |
-
fixed_level_input = gr.Slider(label="Nivel de Variable Fija", minimum
|
708 |
plot_button = gr.Button("Generar Gráficos")
|
709 |
with gr.Row():
|
710 |
left_button = gr.Button("<")
|
@@ -738,26 +764,27 @@ with gr.Blocks() as demo:
|
|
738 |
prediction_table_output,
|
739 |
contribution_table_output,
|
740 |
anova_table_output,
|
741 |
-
|
|
|
742 |
]
|
743 |
)
|
744 |
|
745 |
-
# Generar y mostrar
|
746 |
plot_button.click(
|
747 |
-
lambda
|
748 |
-
inputs=[
|
749 |
outputs=[rsm_plot_output, plot_info, current_index_state]
|
750 |
)
|
751 |
|
752 |
# Navegación de gráficos
|
753 |
left_button.click(
|
754 |
-
|
755 |
-
inputs=[current_index_state, all_figures_state],
|
756 |
outputs=[rsm_plot_output, plot_info, current_index_state]
|
757 |
)
|
758 |
right_button.click(
|
759 |
-
|
760 |
-
inputs=[current_index_state, all_figures_state],
|
761 |
outputs=[rsm_plot_output, plot_info, current_index_state]
|
762 |
)
|
763 |
|
@@ -765,21 +792,21 @@ with gr.Blocks() as demo:
|
|
765 |
download_plot_button.click(
|
766 |
download_current_plot,
|
767 |
inputs=[all_figures_state, current_index_state],
|
768 |
-
outputs=
|
769 |
)
|
770 |
|
771 |
# Descargar todos los gráficos en ZIP
|
772 |
download_all_plots_button.click(
|
773 |
download_all_plots_zip,
|
774 |
inputs=[all_figures_state],
|
775 |
-
outputs=
|
776 |
)
|
777 |
|
778 |
# Descargar todas las tablas en Excel
|
779 |
download_excel_button.click(
|
780 |
download_all_tables_excel,
|
781 |
inputs=[],
|
782 |
-
outputs=
|
783 |
)
|
784 |
|
785 |
# Ejemplo de uso
|
|
|
486 |
img_bytes = fig.to_image(format="png")
|
487 |
zip_file.writestr(f'Grafico_{idx}.png', img_bytes)
|
488 |
zip_buffer.seek(0)
|
489 |
+
|
490 |
+
# Guardar en un archivo temporal
|
491 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".zip") as temp_file:
|
492 |
+
temp_file.write(zip_buffer.read())
|
493 |
+
temp_path = temp_file.name
|
494 |
+
|
495 |
+
return temp_path
|
496 |
|
497 |
def save_fig_to_bytes(self, fig):
|
498 |
"""
|
|
|
500 |
"""
|
501 |
return fig.to_image(format="png")
|
502 |
|
503 |
+
def save_all_figures_png(self):
|
504 |
+
"""
|
505 |
+
Guarda todas las figuras en archivos PNG temporales y retorna las rutas.
|
506 |
+
"""
|
507 |
+
png_paths = []
|
508 |
+
for idx, fig in enumerate(self.all_figures, start=1):
|
509 |
+
img_bytes = fig.to_image(format="png")
|
510 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
|
511 |
+
temp_file.write(img_bytes)
|
512 |
+
temp_path = temp_file.name
|
513 |
+
png_paths.append(temp_path)
|
514 |
+
return png_paths
|
515 |
+
|
516 |
+
def save_tables_to_excel(self):
|
517 |
+
"""
|
518 |
+
Guarda todas las tablas en un archivo Excel con múltiples hojas y retorna la ruta del archivo.
|
519 |
+
"""
|
520 |
+
if 'rsm' not in globals():
|
521 |
+
return None
|
522 |
+
|
523 |
+
tables = self.get_all_tables()
|
524 |
+
excel_buffer = io.BytesIO()
|
525 |
+
with pd.ExcelWriter(excel_buffer, engine='xlsxwriter') as writer:
|
526 |
+
for sheet_name, table in tables.items():
|
527 |
+
table.to_excel(writer, sheet_name=sheet_name, index=False)
|
528 |
+
excel_buffer.seek(0)
|
529 |
+
excel_bytes = excel_buffer.read()
|
530 |
+
|
531 |
+
# Guardar en un archivo temporal
|
532 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx") as temp_file:
|
533 |
+
temp_file.write(excel_bytes)
|
534 |
+
temp_path = temp_file.name
|
535 |
+
|
536 |
+
return temp_path
|
537 |
+
|
538 |
# --- Funciones para la interfaz de Gradio ---
|
539 |
|
540 |
def load_data(x1_name, x2_name, x3_name, y_name, x1_levels_str, x2_levels_str, x3_levels_str, data_str):
|
|
|
589 |
equation_formatted = equation.replace(" + ", "<br>+ ").replace(" ** ", "^").replace("*", " × ")
|
590 |
equation_formatted = f"### Ecuación del Modelo Simplificado:<br>{equation_formatted}"
|
591 |
|
592 |
+
# Guardar las tablas en Excel temporal
|
593 |
+
excel_path = rsm.save_tables_to_excel()
|
594 |
+
|
595 |
+
# Guardar todas las figuras en un ZIP temporal
|
596 |
+
zip_path = rsm.save_figures_to_zip()
|
597 |
+
|
598 |
return (
|
599 |
model_completo.summary().as_html(),
|
600 |
pareto_completo,
|
|
|
605 |
prediction_table,
|
606 |
contribution_table,
|
607 |
anova_table,
|
608 |
+
zip_path, # Ruta del ZIP de gráficos
|
609 |
+
excel_path # Ruta del Excel de tablas
|
610 |
)
|
611 |
|
612 |
+
def show_plot(current_index, all_figures):
|
613 |
if not all_figures:
|
614 |
return None, "No hay gráficos disponibles.", current_index
|
615 |
selected_fig = all_figures[current_index]
|
|
|
640 |
Descarga la figura actual como PNG.
|
641 |
"""
|
642 |
if not all_figures:
|
643 |
+
return None
|
644 |
fig = all_figures[current_index]
|
645 |
img_bytes = rsm.save_fig_to_bytes(fig)
|
646 |
filename = f"Grafico_RSM_{current_index + 1}.png"
|
|
|
650 |
temp_file.write(img_bytes)
|
651 |
temp_path = temp_file.name
|
652 |
|
653 |
+
return temp_path # Retornar solo la ruta
|
654 |
|
655 |
def download_all_plots_zip(all_figures):
|
656 |
"""
|
657 |
Descarga todas las figuras en un archivo ZIP.
|
658 |
"""
|
659 |
+
if not all_figures:
|
660 |
+
return None
|
661 |
+
zip_path = rsm.save_figures_to_zip()
|
662 |
+
filename = f"Graficos_RSM_{datetime.now().strftime('%Y%m%d_%H%M%S')}.zip"
|
663 |
+
return zip_path # Retornar solo la ruta
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
664 |
|
665 |
def download_all_tables_excel():
|
666 |
"""
|
667 |
Descarga todas las tablas en un archivo Excel con múltiples hojas.
|
668 |
"""
|
669 |
if 'rsm' not in globals():
|
670 |
+
return None
|
671 |
+
excel_path = rsm.save_tables_to_excel()
|
672 |
+
filename = f"Tablas_RSM_{datetime.now().strftime('%Y%m%d_%H%M%S')}.xlsx"
|
673 |
+
return excel_path # Retornar solo la ruta
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
674 |
|
675 |
# --- Crear la interfaz de Gradio ---
|
676 |
|
|
|
730 |
with gr.Column():
|
731 |
gr.Markdown("## Generar Gráficos de Superficie de Respuesta")
|
732 |
fixed_variable_input = gr.Dropdown(label="Variable Fija", choices=["Glucosa", "Extracto_de_Levadura", "Triptofano"], value="Glucosa")
|
733 |
+
fixed_level_input = gr.Slider(label="Nivel de Variable Fija", minimum=-1, maximum=1, step=0.01, value=0.0)
|
734 |
plot_button = gr.Button("Generar Gráficos")
|
735 |
with gr.Row():
|
736 |
left_button = gr.Button("<")
|
|
|
764 |
prediction_table_output,
|
765 |
contribution_table_output,
|
766 |
anova_table_output,
|
767 |
+
download_all_plots_button,
|
768 |
+
download_excel_button
|
769 |
]
|
770 |
)
|
771 |
|
772 |
+
# Generar y mostrar los gráficos
|
773 |
plot_button.click(
|
774 |
+
lambda fixed_var, fixed_lvl: (rsm.plot_rsm_individual(fixed_var, fixed_lvl), "Gráfico 1 de " + str(len(rsm.all_figures)), 0),
|
775 |
+
inputs=[fixed_variable_input, fixed_level_input],
|
776 |
outputs=[rsm_plot_output, plot_info, current_index_state]
|
777 |
)
|
778 |
|
779 |
# Navegación de gráficos
|
780 |
left_button.click(
|
781 |
+
navigate_plot,
|
782 |
+
inputs=[gr.Button.get_value(left_button), current_index_state, all_figures_state],
|
783 |
outputs=[rsm_plot_output, plot_info, current_index_state]
|
784 |
)
|
785 |
right_button.click(
|
786 |
+
navigate_plot,
|
787 |
+
inputs=[gr.Button.get_value(right_button), current_index_state, all_figures_state],
|
788 |
outputs=[rsm_plot_output, plot_info, current_index_state]
|
789 |
)
|
790 |
|
|
|
792 |
download_plot_button.click(
|
793 |
download_current_plot,
|
794 |
inputs=[all_figures_state, current_index_state],
|
795 |
+
outputs=download_plot_button
|
796 |
)
|
797 |
|
798 |
# Descargar todos los gráficos en ZIP
|
799 |
download_all_plots_button.click(
|
800 |
download_all_plots_zip,
|
801 |
inputs=[all_figures_state],
|
802 |
+
outputs=download_all_plots_button
|
803 |
)
|
804 |
|
805 |
# Descargar todas las tablas en Excel
|
806 |
download_excel_button.click(
|
807 |
download_all_tables_excel,
|
808 |
inputs=[],
|
809 |
+
outputs=download_excel_button
|
810 |
)
|
811 |
|
812 |
# Ejemplo de uso
|