de-Rodrigo commited on
Commit
706a810
1 Parent(s): ee8ab4e

Test Heatmaps

Browse files
Files changed (1) hide show
  1. app.py +71 -1
app.py CHANGED
@@ -2,7 +2,7 @@ import streamlit as st
2
  import pandas as pd
3
  import numpy as np
4
  from bokeh.plotting import figure
5
- from bokeh.models import ColumnDataSource, DataTable, TableColumn, CustomJS, Select, Button, HoverTool
6
  from bokeh.layouts import column
7
  from bokeh.palettes import Reds9, Blues9, Oranges9, Purples9, Greys9, BuGn9, Greens9
8
  from sklearn.decomposition import PCA
@@ -11,6 +11,8 @@ from sklearn.metrics import pairwise_distances
11
  import io
12
  import ot
13
  from sklearn.linear_model import LinearRegression
 
 
14
 
15
  N_COMPONENTS = 2
16
  TSNE_NEIGHBOURS = 150
@@ -1054,6 +1056,74 @@ def run_model(model_name):
1054
 
1055
  st.write(f"Regresi贸n global (Nueva PCA): R虏 = {r2_new:.4f}, Slope = {slope_new:.4f}, Intercept = {intercept_new:.4f}")
1056
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1057
 
1058
  def main():
1059
  config_style()
 
2
  import pandas as pd
3
  import numpy as np
4
  from bokeh.plotting import figure
5
+ from bokeh.models import ColumnDataSource, DataTable, TableColumn, CustomJS, Select, Button, HoverTool, LinearColorMapper, ColorBar
6
  from bokeh.layouts import column
7
  from bokeh.palettes import Reds9, Blues9, Oranges9, Purples9, Greys9, BuGn9, Greens9
8
  from sklearn.decomposition import PCA
 
11
  import io
12
  import ot
13
  from sklearn.linear_model import LinearRegression
14
+ from scipy.stats import binned_statistic_2d
15
+
16
 
17
  N_COMPONENTS = 2
18
  TSNE_NEIGHBOURS = 150
 
1056
 
1057
  st.write(f"Regresi贸n global (Nueva PCA): R虏 = {r2_new:.4f}, Slope = {slope_new:.4f}, Intercept = {intercept_new:.4f}")
1058
 
1059
+ # --- INICIO DEL BLOQUE: Heatmap de caracter铆sticas ---
1060
+ st.markdown("## Heatmap de Caracter铆sticas")
1061
+
1062
+ # Cargar el CSV que contiene las caracter铆sticas para el heatmap.
1063
+ # Se asume que el archivo se encuentra en "data/heatmaps.csv"
1064
+ try:
1065
+ df_heat = pd.read_csv("data/heatmaps.csv", sep=";")
1066
+ df_heat.columns = [col.strip("'\"") for col in df_heat.columns]
1067
+ except Exception as e:
1068
+ st.error(f"Error al cargar heatmaps.csv: {e}")
1069
+ df_heat = None
1070
+
1071
+ if df_heat is not None:
1072
+ # Verificamos que la columna 'img' est茅 presente en df_all["real"]
1073
+ if 'img' not in df_all["real"].columns:
1074
+ st.error("La columna 'img' no se encuentra en las muestras reales para hacer el merge con heatmaps.csv.")
1075
+ else:
1076
+ # Crear la columna 'name' extrayendo el nombre sin la extensi贸n '.png'
1077
+ df_all["real"]["name"] = df_all["real"]["img"].apply(
1078
+ lambda x: x.split("/")[-1].replace(".png", "") if isinstance(x, str) else x
1079
+ )
1080
+ print(df_all["real"]["name"])
1081
+ print(df_heat)
1082
+
1083
+ # Hacemos merge de las posiciones reales con el CSV de heatmaps usando la columna 'name'
1084
+ df_heatmap = pd.merge(df_all["real"], df_heat, on="name", how="inner")
1085
+
1086
+ # Extraemos las caracter铆sticas disponibles (excluyendo 'name')
1087
+ feature_options = [col for col in df_heat.columns if col != "name"]
1088
+ selected_feature = st.selectbox("Seleccione la caracter铆stica para el heatmap:", options=feature_options)
1089
+
1090
+ # Determinar el rango de las posiciones (x, y) de las muestras reales
1091
+ x_min, x_max = df_heatmap['x'].min(), df_heatmap['x'].max()
1092
+ y_min, y_max = df_heatmap['y'].min(), df_heatmap['y'].max()
1093
+
1094
+ # Definir resoluci贸n de la rejilla (por ejemplo, 50x50)
1095
+ grid_size = 50
1096
+ x_bins = np.linspace(x_min, x_max, grid_size + 1)
1097
+ y_bins = np.linspace(y_min, y_max, grid_size + 1)
1098
+
1099
+ # Utilizamos binned_statistic_2d para calcular la media de la caracter铆stica seleccionada en cada celda
1100
+ heat_stat, x_edges, y_edges, binnumber = binned_statistic_2d(
1101
+ df_heatmap['x'], df_heatmap['y'], df_heatmap[selected_feature],
1102
+ statistic='mean', bins=[x_bins, y_bins]
1103
+ )
1104
+
1105
+ # La funci贸n image de Bokeh espera una lista de arrays y el arreglo debe estar orientado correctamente
1106
+ heatmap_data = heat_stat.T # Transponemos para alinear los ejes
1107
+
1108
+ # Crear el mapa de color
1109
+ color_mapper = LinearColorMapper(palette="Viridis256", low=np.nanmin(heatmap_data), high=np.nanmax(heatmap_data))
1110
+
1111
+ # Crear la figura para el heatmap
1112
+ heatmap_fig = figure(title=f"Heatmap de '{selected_feature}'",
1113
+ x_range=(x_min, x_max), y_range=(y_min, y_max),
1114
+ width=600, height=600,
1115
+ tools="pan,wheel_zoom,reset,save", active_scroll="wheel_zoom")
1116
+
1117
+ # Dibujar el heatmap usando la imagen (se pasa la lista con el array de datos)
1118
+ heatmap_fig.image(image=[heatmap_data], x=x_min, y=y_min,
1119
+ dw=x_max - x_min, dh=y_max - y_min,
1120
+ color_mapper=color_mapper)
1121
+
1122
+ # Agregar barra de colores
1123
+ color_bar = ColorBar(color_mapper=color_mapper, location=(0, 0))
1124
+ heatmap_fig.add_layout(color_bar, 'right')
1125
+
1126
+ st.bokeh_chart(heatmap_fig)
1127
 
1128
  def main():
1129
  config_style()