de-Rodrigo commited on
Commit
cbe4d30
1 Parent(s): 3b16040

Lighter App

Browse files
Files changed (1) hide show
  1. app.py +102 -102
app.py CHANGED
@@ -718,36 +718,36 @@ def run_model(model_name):
718
  st.write(f"Trustworthiness: {result['trustworthiness']:.4f}")
719
  st.write(f"Continuity: {result['continuity']:.4f}")
720
 
721
- # Mostrar los plots de loadings si se us贸 PCA (para el conjunto combinado)
722
- if reduction_method == "PCA" and result.get("pca_model") is not None:
723
- # pca_model = result["pca_model"]
724
- pca_model = result["pca_model"].named_steps["pca"]
725
- components = pca_model.components_ # Shape: (n_components, n_features)
726
 
727
- st.subheader("Pesos de las Componentes Principales (Loadings) - Conjunto Combinado")
728
- for i, comp in enumerate(components):
729
- source = ColumnDataSource(data=dict(
730
- dimensions=embedding_cols,
731
- weight=comp
732
- ))
733
- p = figure(x_range=embedding_cols, title=f"Componente Principal {i+1}",
734
- plot_height=400, plot_width=600,
735
- toolbar_location="above",
736
- tools="pan,wheel_zoom,reset,save,hover",
737
- active_scroll="wheel_zoom")
738
 
739
- # Establecer fondo blanco
740
- p.background_fill_color = "white"
741
- # Mostrar solo grilla horizontal
742
- p.xgrid.grid_line_color = None
743
- p.ygrid.grid_line_color = "gray"
744
- p.vbar(x='dimensions', top='weight', width=0.8, source=source)
745
- p.xaxis.major_label_text_font_size = '0pt'
746
- hover = HoverTool(tooltips=[("Dimensi贸n", "@dimensions"), ("Peso", "@weight")])
747
- p.add_tools(hover)
748
- p.xaxis.axis_label = "Dimensiones originales"
749
- p.yaxis.axis_label = "Peso"
750
- st.bokeh_chart(p)
751
 
752
  data_table, df_table, source_table = create_table(result["df_distances"])
753
  real_subset_names = list(df_table.columns[1:])
@@ -755,56 +755,56 @@ def run_model(model_name):
755
  reset_button = Button(label="Reset Colors", button_type="primary")
756
  line_source = ColumnDataSource(data={'x': [], 'y': []})
757
 
758
- if (reduction_method == "t-SNE" and N_COMPONENTS == 2) or (reduction_method == "PCA" and N_COMPONENTS == 2):
759
- fig, real_renderers, synthetic_renderers, pretrained_renderers = create_figure(
760
- result["dfs_reduced"],
761
- result["unique_subsets"],
762
- get_color_maps(result["unique_subsets"]),
763
- model_name
764
- )
765
- fig.line('x', 'y', source=line_source, line_width=2, line_color='black')
766
- centers_real = calculate_cluster_centers(result["dfs_reduced"]["real"], result["unique_subsets"]["real"])
767
- real_centers_js = {k: [v[0], v[1]] for k, v in centers_real.items()}
768
- synthetic_centers = {}
769
- synth_labels = sorted(result["dfs_reduced"]["synthetic"]['label'].unique().tolist())
770
- for label in synth_labels:
771
- subset = result["dfs_reduced"]["synthetic"][result["dfs_reduced"]["synthetic"]['label'] == label]
772
- if 'x' in subset.columns and 'y' in subset.columns:
773
- synthetic_centers[label] = [subset['x'].mean(), subset['y'].mean()]
774
- callback = CustomJS(args=dict(source=source_table, line_source=line_source,
775
- synthetic_centers=synthetic_centers,
776
- real_centers=real_centers_js,
777
- real_select=real_select),
778
- code="""
779
- var selected = source.selected.indices;
780
- if (selected.length > 0) {
781
- var idx = selected[0];
782
- var data = source.data;
783
- var synth_label = data['Synthetic'][idx];
784
- var real_label = real_select.value;
785
- var syn_coords = synthetic_centers[synth_label];
786
- var real_coords = real_centers[real_label];
787
- line_source.data = {'x': [syn_coords[0], real_coords[0]], 'y': [syn_coords[1], real_coords[1]]};
788
- line_source.change.emit();
789
- } else {
790
- line_source.data = {'x': [], 'y': []};
791
- line_source.change.emit();
792
- }
793
- """)
794
- source_table.selected.js_on_change('indices', callback)
795
- real_select.js_on_change('value', callback)
796
 
797
- reset_callback = CustomJS(args=dict(line_source=line_source),
798
- code="""
799
- line_source.data = {'x': [], 'y': []};
800
- line_source.change.emit();
801
- """)
802
- reset_button.js_on_event("button_click", reset_callback)
803
- layout = column(fig, result["scatter_fig"], column(real_select, reset_button, data_table))
804
- else:
805
- layout = column(result["scatter_fig"], column(real_select, reset_button, data_table))
806
 
807
- st.bokeh_chart(layout, use_container_width=True)
808
 
809
  buffer = io.BytesIO()
810
  df_table.to_excel(buffer, index=False)
@@ -856,32 +856,32 @@ def run_model(model_name):
856
  st.table(variance_df_real)
857
 
858
  # Mostrar los plots de loadings para cada componente
859
- st.subheader("PCA - Real: Component Loadings")
860
- st.markdown("### Pesos de las Componentes Principales (Loadings) - Conjunto Combinado")
861
- for i, comp in enumerate(reducer_real.named_steps["pca"].components_):
862
- source = ColumnDataSource(data=dict(
863
- dimensions=embedding_cols,
864
- weight=comp
865
- ))
866
- p = figure(
867
- x_range=embedding_cols,
868
- title=f"Componente Principal {i+1}",
869
- plot_height=400,
870
- plot_width=600,
871
- toolbar_location="above",
872
- tools="pan,wheel_zoom,reset,save,hover",
873
- active_scroll="wheel_zoom"
874
- )
875
- p.background_fill_color = "white"
876
- p.xgrid.grid_line_color = None
877
- p.ygrid.grid_line_color = "gray"
878
- p.vbar(x='dimensions', top='weight', width=0.8, source=source,
879
- fill_color="#2b83ba", line_color="#2b83ba")
880
- p.xaxis.axis_label = "Dimensiones Originales"
881
- p.xaxis.major_label_text_font_size = '0pt'
882
- hover = p.select_one(HoverTool)
883
- hover.tooltips = [("Dimensi贸n", "@dimensions"), ("Peso", "@weight")]
884
- st.bokeh_chart(p)
885
 
886
  # -------------------------------------------------------------------------
887
  # 2. Proyecci贸n de todos los subconjuntos usando los loadings de df_real (para PC completos)
 
718
  st.write(f"Trustworthiness: {result['trustworthiness']:.4f}")
719
  st.write(f"Continuity: {result['continuity']:.4f}")
720
 
721
+ # # Mostrar los plots de loadings si se us贸 PCA (para el conjunto combinado)
722
+ # if reduction_method == "PCA" and result.get("pca_model") is not None:
723
+ # # pca_model = result["pca_model"]
724
+ # pca_model = result["pca_model"].named_steps["pca"]
725
+ # components = pca_model.components_ # Shape: (n_components, n_features)
726
 
727
+ # st.subheader("Pesos de las Componentes Principales (Loadings) - Conjunto Combinado")
728
+ # for i, comp in enumerate(components):
729
+ # source = ColumnDataSource(data=dict(
730
+ # dimensions=embedding_cols,
731
+ # weight=comp
732
+ # ))
733
+ # p = figure(x_range=embedding_cols, title=f"Componente Principal {i+1}",
734
+ # plot_height=400, plot_width=600,
735
+ # toolbar_location="above",
736
+ # tools="pan,wheel_zoom,reset,save,hover",
737
+ # active_scroll="wheel_zoom")
738
 
739
+ # # Establecer fondo blanco
740
+ # p.background_fill_color = "white"
741
+ # # Mostrar solo grilla horizontal
742
+ # p.xgrid.grid_line_color = None
743
+ # p.ygrid.grid_line_color = "gray"
744
+ # p.vbar(x='dimensions', top='weight', width=0.8, source=source)
745
+ # p.xaxis.major_label_text_font_size = '0pt'
746
+ # hover = HoverTool(tooltips=[("Dimensi贸n", "@dimensions"), ("Peso", "@weight")])
747
+ # p.add_tools(hover)
748
+ # p.xaxis.axis_label = "Dimensiones originales"
749
+ # p.yaxis.axis_label = "Peso"
750
+ # st.bokeh_chart(p)
751
 
752
  data_table, df_table, source_table = create_table(result["df_distances"])
753
  real_subset_names = list(df_table.columns[1:])
 
755
  reset_button = Button(label="Reset Colors", button_type="primary")
756
  line_source = ColumnDataSource(data={'x': [], 'y': []})
757
 
758
+ # if (reduction_method == "t-SNE" and N_COMPONENTS == 2) or (reduction_method == "PCA" and N_COMPONENTS == 2):
759
+ # fig, real_renderers, synthetic_renderers, pretrained_renderers = create_figure(
760
+ # result["dfs_reduced"],
761
+ # result["unique_subsets"],
762
+ # get_color_maps(result["unique_subsets"]),
763
+ # model_name
764
+ # )
765
+ # fig.line('x', 'y', source=line_source, line_width=2, line_color='black')
766
+ # centers_real = calculate_cluster_centers(result["dfs_reduced"]["real"], result["unique_subsets"]["real"])
767
+ # real_centers_js = {k: [v[0], v[1]] for k, v in centers_real.items()}
768
+ # synthetic_centers = {}
769
+ # synth_labels = sorted(result["dfs_reduced"]["synthetic"]['label'].unique().tolist())
770
+ # for label in synth_labels:
771
+ # subset = result["dfs_reduced"]["synthetic"][result["dfs_reduced"]["synthetic"]['label'] == label]
772
+ # if 'x' in subset.columns and 'y' in subset.columns:
773
+ # synthetic_centers[label] = [subset['x'].mean(), subset['y'].mean()]
774
+ # callback = CustomJS(args=dict(source=source_table, line_source=line_source,
775
+ # synthetic_centers=synthetic_centers,
776
+ # real_centers=real_centers_js,
777
+ # real_select=real_select),
778
+ # code="""
779
+ # var selected = source.selected.indices;
780
+ # if (selected.length > 0) {
781
+ # var idx = selected[0];
782
+ # var data = source.data;
783
+ # var synth_label = data['Synthetic'][idx];
784
+ # var real_label = real_select.value;
785
+ # var syn_coords = synthetic_centers[synth_label];
786
+ # var real_coords = real_centers[real_label];
787
+ # line_source.data = {'x': [syn_coords[0], real_coords[0]], 'y': [syn_coords[1], real_coords[1]]};
788
+ # line_source.change.emit();
789
+ # } else {
790
+ # line_source.data = {'x': [], 'y': []};
791
+ # line_source.change.emit();
792
+ # }
793
+ # """)
794
+ # source_table.selected.js_on_change('indices', callback)
795
+ # real_select.js_on_change('value', callback)
796
 
797
+ # reset_callback = CustomJS(args=dict(line_source=line_source),
798
+ # code="""
799
+ # line_source.data = {'x': [], 'y': []};
800
+ # line_source.change.emit();
801
+ # """)
802
+ # reset_button.js_on_event("button_click", reset_callback)
803
+ # layout = column(fig, result["scatter_fig"], column(real_select, reset_button, data_table))
804
+ # else:
805
+ # layout = column(result["scatter_fig"], column(real_select, reset_button, data_table))
806
 
807
+ # st.bokeh_chart(layout, use_container_width=True)
808
 
809
  buffer = io.BytesIO()
810
  df_table.to_excel(buffer, index=False)
 
856
  st.table(variance_df_real)
857
 
858
  # Mostrar los plots de loadings para cada componente
859
+ # st.subheader("PCA - Real: Component Loadings")
860
+ # st.markdown("### Pesos de las Componentes Principales (Loadings) - Conjunto Combinado")
861
+ # for i, comp in enumerate(reducer_real.named_steps["pca"].components_):
862
+ # source = ColumnDataSource(data=dict(
863
+ # dimensions=embedding_cols,
864
+ # weight=comp
865
+ # ))
866
+ # p = figure(
867
+ # x_range=embedding_cols,
868
+ # title=f"Componente Principal {i+1}",
869
+ # plot_height=400,
870
+ # plot_width=600,
871
+ # toolbar_location="above",
872
+ # tools="pan,wheel_zoom,reset,save,hover",
873
+ # active_scroll="wheel_zoom"
874
+ # )
875
+ # p.background_fill_color = "white"
876
+ # p.xgrid.grid_line_color = None
877
+ # p.ygrid.grid_line_color = "gray"
878
+ # p.vbar(x='dimensions', top='weight', width=0.8, source=source,
879
+ # fill_color="#2b83ba", line_color="#2b83ba")
880
+ # p.xaxis.axis_label = "Dimensiones Originales"
881
+ # p.xaxis.major_label_text_font_size = '0pt'
882
+ # hover = p.select_one(HoverTool)
883
+ # hover.tooltips = [("Dimensi贸n", "@dimensions"), ("Peso", "@weight")]
884
+ # st.bokeh_chart(p)
885
 
886
  # -------------------------------------------------------------------------
887
  # 2. Proyecci贸n de todos los subconjuntos usando los loadings de df_real (para PC completos)