import os import torch import torch.nn as nn from torch.utils.data import DataLoader, Dataset from torchvision import models, transforms from PIL import Image import numpy as np import random from sklearn.decomposition import PCA from sklearn.manifold import TSNE from sklearn.cluster import KMeans import matplotlib.pyplot as plt import pandas as pd import torch.nn.functional as F import plotly.express as px def set_seed(seed=42): random.seed(seed) np.random.seed(seed) torch.manual_seed(seed) if torch.cuda.is_available(): torch.cuda.manual_seed(seed) torch.cuda.manual_seed_all(seed) set_seed(42) BASE_DIR = os.path.dirname(os.path.abspath(__file__)) IMG_DIR = os.path.join(BASE_DIR, "..", "..", "outputs", "ion_images") ENCODER_PATH = os.path.join(BASE_DIR, "..", "..", "outputs", "models", "msi_encoder_trained.pth") DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu") TARGET_K = 5 class MSI_Structural_Encoder(nn.Module): def __init__(self): super(MSI_Structural_Encoder, self).__init__() self.features = nn.Sequential( nn.Conv2d(1, 32, kernel_size=5, stride=2, padding=2, bias=False), nn.GroupNorm(4, 32), nn.ReLU(), nn.Conv2d(32, 64, kernel_size=3, stride=2, padding=1, bias=False), nn.GroupNorm(8, 64), nn.ReLU(), nn.Conv2d(64, 128, kernel_size=3, stride=2, padding=1, bias=False), nn.GroupNorm(16, 128), nn.ReLU(), nn.Conv2d(128, 256, kernel_size=3, stride=2, padding=1, bias=False), nn.GroupNorm(32, 256), nn.ReLU(), nn.AdaptiveAvgPool2d((1, 1)) ) def forward(self, x): h = self.features(x).squeeze(-1).squeeze(-1) return h def load_trained_encoder(path): if not os.path.exists(path): raise FileNotFoundError(f"Missing model checkpoint: {path}") model = MSI_Structural_Encoder() model.features.load_state_dict(torch.load(path, map_location=DEVICE)) model.to(DEVICE) model.eval() return model class MSIDatasetInference(Dataset): def __init__(self, img_dir): self.img_dir = img_dir self.img_names = sorted([f for f in os.listdir(img_dir) if f.endswith('.png')]) self.transform = transforms.Compose([ transforms.Resize((256, 256)), transforms.ToTensor() ]) def __len__(self): return len(self.img_names) def __getitem__(self, idx): img_path = os.path.join(self.img_dir, self.img_names[idx]) image = Image.open(img_path).convert("L") return self.transform(image), self.img_names[idx] if __name__ == "__main__": print("Loading specialized custom MSI Structural Encoder...") encoder = load_trained_encoder(ENCODER_PATH) dataset = MSIDatasetInference(IMG_DIR) loader = DataLoader(dataset, batch_size=64, shuffle=False, num_workers=2) features = [] ion_names = [] print("Extracting and L2-normalizing features from custom manifold...") with torch.no_grad(): for img, names in loader: img = img.to(DEVICE) feat = encoder(img) feat = F.normalize(feat, p=2, dim=1) features.append(feat.cpu().numpy()) ion_names.extend(names) features = np.concatenate(features, axis=0) print(f" Feature matrix: {features.shape}") # PCA for denoising (retain 95% variance) pca = PCA(n_components=min(50, features.shape[0]), random_state=42) features_pca = pca.fit_transform(features) # Re-normalize after PCA to restore unit hypersphere geometry for Spherical K-Means features_pca = features_pca / np.linalg.norm(features_pca, axis=1, keepdims=True) print(f" PCA components: {features_pca.shape[1]} (explained variance: {pca.explained_variance_ratio_.sum():.3f})") print(f"Executing Spherical K-Means (via re-normalized PCA) strictly constrained to K={TARGET_K}...") clusterer = KMeans(n_clusters=TARGET_K, init='k-means++', n_init=20, random_state=42) labels = clusterer.fit_predict(features_pca) print("Projecting t-SNE map...") tsne = TSNE( n_components=2, perplexity=30, learning_rate='auto', init='pca', random_state=42 ) embeddings_2d = tsne.fit_transform(features_pca) output_dir = os.path.join(BASE_DIR, "..", "..", "outputs_k5") figures_dir = os.path.join(output_dir, "figures") os.makedirs(output_dir, exist_ok=True) os.makedirs(figures_dir, exist_ok=True) plt.figure(figsize=(10, 8)) scatter = plt.scatter(embeddings_2d[:, 0], embeddings_2d[:, 1], c=labels, cmap='tab10', alpha=0.9, edgecolors='none', s=45) cbar = plt.colorbar(scatter, ticks=range(TARGET_K)) cbar.set_label(f'Metabolic Domain ID (K={TARGET_K})') plt.title(f"t-SNE Visualization of MSI Ions (K={TARGET_K})") plt.xlabel("t-SNE Dimension 1") plt.ylabel("t-SNE Dimension 2") plt.grid(True, linestyle='--', alpha=0.3) plt.savefig(os.path.join(output_dir, "msi_tsne_plot.png"), dpi=300, bbox_inches='tight') plt.close() print(f" Static t-SNE saved to: outputs_k5/msi_tsne_plot.png") print("Exporting Interactive Plotly t-SNE...") mz_values = [float(n.replace("ion_", "").replace(".png", "")) for n in ion_names] df_plot = pd.DataFrame({ 't-SNE Dimension 1': embeddings_2d[:, 0], 't-SNE Dimension 2': embeddings_2d[:, 1], 'Cluster': [str(lbl) for lbl in labels], 'm/z': mz_values, 'Filename': ion_names }) fig = px.scatter( df_plot, x='t-SNE Dimension 1', y='t-SNE Dimension 2', color='Cluster', hover_data=['m/z', 'Filename'], title=f"Interactive t-SNE of MSI Ions (K={TARGET_K})", category_orders={"Cluster": [str(i) for i in range(TARGET_K)]} ) plotly_out = os.path.join(figures_dir, "msi_tsne_interactive.html") fig.write_html(plotly_out) print(f" Interactive t-SNE saved to: {plotly_out}") print("Exporting synchronized cluster assignments...") df_results = pd.DataFrame({'ion_image': ion_names, 'cluster': labels}) df_results['mz'] = df_results['ion_image'].str.extract(r'(\d+\.\d+)').astype(float) df_results.to_csv(os.path.join(output_dir, "msi_clusters_results.csv"), index=False) print("Process complete.")