Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -517,9 +517,6 @@ class RSM_BoxBehnken:
|
|
| 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:
|
|
@@ -562,7 +559,7 @@ def load_data(x1_name, x2_name, x3_name, y_name, x1_levels_str, x2_levels_str, x
|
|
| 562 |
rsm = RSM_BoxBehnken(data, x1_name, x2_name, x3_name, y_name, x1_levels, x2_levels, x3_levels)
|
| 563 |
|
| 564 |
return data.round(3), x1_name, x2_name, x3_name, y_name, x1_levels, x2_levels, x3_levels, gr.update(visible=True)
|
| 565 |
-
|
| 566 |
except Exception as e:
|
| 567 |
# Mostrar mensaje de error
|
| 568 |
error_message = f"Error al cargar los datos: {str(e)}"
|
|
@@ -571,8 +568,8 @@ def load_data(x1_name, x2_name, x3_name, y_name, x1_levels_str, x2_levels_str, x
|
|
| 571 |
|
| 572 |
def fit_and_optimize_model():
|
| 573 |
if 'rsm' not in globals():
|
| 574 |
-
return [None]*
|
| 575 |
-
|
| 576 |
# Ajustar modelos y optimizar
|
| 577 |
model_completo, pareto_completo = rsm.fit_model()
|
| 578 |
model_simplificado, pareto_simplificado = rsm.fit_simplified_model()
|
|
@@ -605,8 +602,8 @@ def fit_and_optimize_model():
|
|
| 605 |
prediction_table,
|
| 606 |
contribution_table,
|
| 607 |
anova_table,
|
| 608 |
-
zip_path,
|
| 609 |
-
excel_path
|
| 610 |
)
|
| 611 |
|
| 612 |
def show_plot(current_index, all_figures):
|
|
@@ -652,15 +649,18 @@ def download_current_plot(all_figures, current_index):
|
|
| 652 |
|
| 653 |
return temp_path # Retornar solo la ruta
|
| 654 |
|
| 655 |
-
def download_all_plots_zip(
|
| 656 |
"""
|
| 657 |
Descarga todas las figuras en un archivo ZIP.
|
| 658 |
"""
|
| 659 |
-
if not
|
| 660 |
return None
|
| 661 |
zip_path = rsm.save_figures_to_zip()
|
| 662 |
-
|
| 663 |
-
|
|
|
|
|
|
|
|
|
|
| 664 |
|
| 665 |
def download_all_tables_excel():
|
| 666 |
"""
|
|
@@ -669,8 +669,11 @@ def download_all_tables_excel():
|
|
| 669 |
if 'rsm' not in globals():
|
| 670 |
return None
|
| 671 |
excel_path = rsm.save_tables_to_excel()
|
| 672 |
-
|
| 673 |
-
|
|
|
|
|
|
|
|
|
|
| 674 |
|
| 675 |
# --- Crear la interfaz de Gradio ---
|
| 676 |
|
|
@@ -742,7 +745,7 @@ with gr.Blocks() as demo:
|
|
| 742 |
download_all_plots_button = gr.DownloadButton("Descargar Todos los Gráficos (ZIP)")
|
| 743 |
current_index_state = gr.State(0) # Estado para el índice actual
|
| 744 |
all_figures_state = gr.State([]) # Estado para todas las figuras
|
| 745 |
-
|
| 746 |
# Cargar datos
|
| 747 |
load_button.click(
|
| 748 |
load_data,
|
|
@@ -764,27 +767,32 @@ with gr.Blocks() as demo:
|
|
| 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: (
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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=[
|
| 783 |
outputs=[rsm_plot_output, plot_info, current_index_state]
|
| 784 |
)
|
| 785 |
right_button.click(
|
| 786 |
-
navigate_plot,
|
| 787 |
-
inputs=[
|
| 788 |
outputs=[rsm_plot_output, plot_info, current_index_state]
|
| 789 |
)
|
| 790 |
|
|
@@ -798,7 +806,7 @@ with gr.Blocks() as demo:
|
|
| 798 |
# Descargar todos los gráficos en ZIP
|
| 799 |
download_all_plots_button.click(
|
| 800 |
download_all_plots_zip,
|
| 801 |
-
inputs=[
|
| 802 |
outputs=download_all_plots_button
|
| 803 |
)
|
| 804 |
|
|
|
|
| 517 |
"""
|
| 518 |
Guarda todas las tablas en un archivo Excel con múltiples hojas y retorna la ruta del archivo.
|
| 519 |
"""
|
|
|
|
|
|
|
|
|
|
| 520 |
tables = self.get_all_tables()
|
| 521 |
excel_buffer = io.BytesIO()
|
| 522 |
with pd.ExcelWriter(excel_buffer, engine='xlsxwriter') as writer:
|
|
|
|
| 559 |
rsm = RSM_BoxBehnken(data, x1_name, x2_name, x3_name, y_name, x1_levels, x2_levels, x3_levels)
|
| 560 |
|
| 561 |
return data.round(3), x1_name, x2_name, x3_name, y_name, x1_levels, x2_levels, x3_levels, gr.update(visible=True)
|
| 562 |
+
|
| 563 |
except Exception as e:
|
| 564 |
# Mostrar mensaje de error
|
| 565 |
error_message = f"Error al cargar los datos: {str(e)}"
|
|
|
|
| 568 |
|
| 569 |
def fit_and_optimize_model():
|
| 570 |
if 'rsm' not in globals():
|
| 571 |
+
return [None]*11 # Ajustar el número de outputs
|
| 572 |
+
|
| 573 |
# Ajustar modelos y optimizar
|
| 574 |
model_completo, pareto_completo = rsm.fit_model()
|
| 575 |
model_simplificado, pareto_simplificado = rsm.fit_simplified_model()
|
|
|
|
| 602 |
prediction_table,
|
| 603 |
contribution_table,
|
| 604 |
anova_table,
|
| 605 |
+
zip_path, # Ruta del ZIP de gráficos
|
| 606 |
+
excel_path # Ruta del Excel de tablas
|
| 607 |
)
|
| 608 |
|
| 609 |
def show_plot(current_index, all_figures):
|
|
|
|
| 649 |
|
| 650 |
return temp_path # Retornar solo la ruta
|
| 651 |
|
| 652 |
+
def download_all_plots_zip():
|
| 653 |
"""
|
| 654 |
Descarga todas las figuras en un archivo ZIP.
|
| 655 |
"""
|
| 656 |
+
if 'rsm' not in globals():
|
| 657 |
return None
|
| 658 |
zip_path = rsm.save_figures_to_zip()
|
| 659 |
+
if zip_path:
|
| 660 |
+
filename = f"Graficos_RSM_{datetime.now().strftime('%Y%m%d_%H%M%S')}.zip"
|
| 661 |
+
# Renombrar el archivo temporal al nombre deseado
|
| 662 |
+
return (zip_path, filename)
|
| 663 |
+
return None
|
| 664 |
|
| 665 |
def download_all_tables_excel():
|
| 666 |
"""
|
|
|
|
| 669 |
if 'rsm' not in globals():
|
| 670 |
return None
|
| 671 |
excel_path = rsm.save_tables_to_excel()
|
| 672 |
+
if excel_path:
|
| 673 |
+
filename = f"Tablas_RSM_{datetime.now().strftime('%Y%m%d_%H%M%S')}.xlsx"
|
| 674 |
+
# Renombrar el archivo temporal al nombre deseado
|
| 675 |
+
return (excel_path, filename)
|
| 676 |
+
return None
|
| 677 |
|
| 678 |
# --- Crear la interfaz de Gradio ---
|
| 679 |
|
|
|
|
| 745 |
download_all_plots_button = gr.DownloadButton("Descargar Todos los Gráficos (ZIP)")
|
| 746 |
current_index_state = gr.State(0) # Estado para el índice actual
|
| 747 |
all_figures_state = gr.State([]) # Estado para todas las figuras
|
| 748 |
+
|
| 749 |
# Cargar datos
|
| 750 |
load_button.click(
|
| 751 |
load_data,
|
|
|
|
| 767 |
prediction_table_output,
|
| 768 |
contribution_table_output,
|
| 769 |
anova_table_output,
|
| 770 |
+
download_all_plots_button, # Ruta del ZIP de gráficos
|
| 771 |
+
download_excel_button # Ruta del Excel de tablas
|
| 772 |
]
|
| 773 |
)
|
| 774 |
|
| 775 |
# Generar y mostrar los gráficos
|
| 776 |
plot_button.click(
|
| 777 |
+
lambda fixed_var, fixed_lvl: (
|
| 778 |
+
rsm.plot_rsm_individual(fixed_var, fixed_lvl),
|
| 779 |
+
f"Gráfico 1 de {len(rsm.all_figures)}" if rsm.all_figures else "No hay gráficos disponibles.",
|
| 780 |
+
0,
|
| 781 |
+
rsm.all_figures # Actualizar el estado de todas las figuras
|
| 782 |
+
),
|
| 783 |
inputs=[fixed_variable_input, fixed_level_input],
|
| 784 |
+
outputs=[rsm_plot_output, plot_info, current_index_state, all_figures_state]
|
| 785 |
)
|
| 786 |
|
| 787 |
# Navegación de gráficos
|
| 788 |
left_button.click(
|
| 789 |
+
lambda current_index, all_figures: navigate_plot('left', current_index, all_figures),
|
| 790 |
+
inputs=[current_index_state, all_figures_state],
|
| 791 |
outputs=[rsm_plot_output, plot_info, current_index_state]
|
| 792 |
)
|
| 793 |
right_button.click(
|
| 794 |
+
lambda current_index, all_figures: navigate_plot('right', current_index, all_figures),
|
| 795 |
+
inputs=[current_index_state, all_figures_state],
|
| 796 |
outputs=[rsm_plot_output, plot_info, current_index_state]
|
| 797 |
)
|
| 798 |
|
|
|
|
| 806 |
# Descargar todos los gráficos en ZIP
|
| 807 |
download_all_plots_button.click(
|
| 808 |
download_all_plots_zip,
|
| 809 |
+
inputs=[],
|
| 810 |
outputs=download_all_plots_button
|
| 811 |
)
|
| 812 |
|