From 5fe2ae1ee368765cf6dea00e7157b0c58210be21 Mon Sep 17 00:00:00 2001 From: Carlos Date: Tue, 18 Aug 2026 08:01:39 +0200 Subject: [PATCH] Subir archivos a "scripts_python" --- scripts_python/visualize_clusters_k5.py | 46 +++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 scripts_python/visualize_clusters_k5.py diff --git a/scripts_python/visualize_clusters_k5.py b/scripts_python/visualize_clusters_k5.py new file mode 100644 index 0000000..2ee37c0 --- /dev/null +++ b/scripts_python/visualize_clusters_k5.py @@ -0,0 +1,46 @@ +import pandas as pd +import os +import matplotlib.pyplot as plt +from PIL import Image + +BASE_DIR = os.path.dirname(os.path.abspath(__file__)) +CSV_PATH = os.path.join(BASE_DIR, "..", "..", "outputs_k5", "msi_clusters_results.csv") +IMG_DIR = os.path.join(BASE_DIR, "..", "..", "outputs", "ion_images") +OUTPUT_DIR = os.path.join(BASE_DIR, "..", "..", "outputs_k5", "figures", "galleries") +os.makedirs(OUTPUT_DIR, exist_ok=True) + +if os.path.exists(CSV_PATH): + print(f"Loading results from: {CSV_PATH}") + df = pd.read_csv(CSV_PATH) + + possible_cols = ['ion_image', 'ion', 'filename'] + img_col = next((c for c in possible_cols if c in df.columns), None) + + if not img_col: + print(f"Error: Could not find image column in {CSV_PATH}. Available: {df.columns.tolist()}") + else: + for cluster_id in sorted(df['cluster'].unique()): + print(f"Generating gallery for Cluster {cluster_id}...") + cluster_ions = df[df['cluster'] == cluster_id][img_col].head(9).tolist() + + fig, axes = plt.subplots(3, 3, figsize=(10, 10)) + fig.suptitle(f"Cluster {cluster_id} - Ion Morphologies (K=5)", fontsize=16) + + for i, ax in enumerate(axes.flat): + if i < len(cluster_ions): + img_path = os.path.join(IMG_DIR, cluster_ions[i]) + if os.path.exists(img_path): + img = Image.open(img_path) + ax.imshow(img, cmap='viridis') + ax.set_title(cluster_ions[i], fontsize=8) + else: + ax.text(0.5, 0.5, 'Image Not Found', ha='center', va='center', fontsize=6) + ax.axis('off') + + plt.tight_layout() + plt.savefig(f"{OUTPUT_DIR}/Gallery_cluster_{cluster_id}.png") + plt.close() + + print(f"\nGalleries saved in: {OUTPUT_DIR}") +else: + print(f"Error: Missing results file: {CSV_PATH}")