Subir archivos a "scripts_python"

This commit is contained in:
2026-08-18 08:01:28 +02:00
parent 6dec9330c4
commit 4601a2d6bb
5 changed files with 276 additions and 70 deletions
+9 -12
View File
@@ -5,9 +5,12 @@ import pandas as pd
import numpy as np
from PIL import Image
from torchvision import transforms
# Copy the exact encoder structure from your scripts
import torch.nn as nn
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
IMG_DIR = os.path.join(BASE_DIR, "..", "..", "outputs", "ion_images")
MODEL_PATH = os.path.join(BASE_DIR, "..", "..", "outputs", "models", "msi_encoder_trained.pth")
class MSI_Structural_Encoder(nn.Module):
def __init__(self):
super().__init__()
@@ -30,8 +33,6 @@ class MSI_Structural_Encoder(nn.Module):
return self.features(x).squeeze(-1).squeeze(-1)
DEVICE = torch.device("cpu")
IMG_DIR = "datos_para_ai"
MODEL_PATH = "models/msi_encoder_trained.pth"
if not os.path.exists(MODEL_PATH):
print("Model not found. Please ensure you are in the correct directory.")
@@ -44,19 +45,18 @@ model.eval()
transform = transforms.Compose([transforms.Resize((256, 256)), transforms.ToTensor()])
def get_vector(mz_target):
# Find the file closest to mz_target
files = [f for f in os.listdir(IMG_DIR) if f.endswith('.png')]
mzs = [float(f.replace("ion_", "").replace(".png", "")) for f in files]
idx = np.argmin(np.abs(np.array(mzs) - mz_target))
closest_mz = mzs[idx]
if abs(closest_mz - mz_target) > 0.1:
return None, closest_mz
img_path = os.path.join(IMG_DIR, files[idx])
img = Image.open(img_path).convert("L")
tensor = transform(img).unsqueeze(0)
with torch.no_grad():
feat = model(tensor)
feat_norm = F.normalize(feat, p=2, dim=1)
@@ -64,21 +64,18 @@ def get_vector(mz_target):
print("--- INTERNAL VALIDATION: ISOTOPIC & STRUCTURAL COHERENCE ---")
# Test 1: Isotopic Coherence (Atropine and its +1 Isotope)
v1, mz1 = get_vector(290.02)
v2, mz2 = get_vector(291.00)
if v1 is not None and v2 is not None:
sim = np.dot(v1, v2)
print(f"Isotopic Match -> m/z {mz1:.2f} (Atropine) vs m/z {mz2:.2f} (+1 Isotope): Cosine Similarity = {sim:.4f}")
# Test 2: Structural Coherence (Atropine and Scopolamine - both vascular)
v3, mz3 = get_vector(304.00)
if v1 is not None and v3 is not None:
sim = np.dot(v1, v3)
print(f"Biological Match -> m/z {mz1:.2f} (Atropine) vs m/z {mz3:.2f} (Scopolamine): Cosine Similarity = {sim:.4f}")
# Test 3: Unrelated Coherence (Atropine vs random background or non-vascular)
v4, mz4 = get_vector(142.09) # Tropine (paravascular, should be lower than scopolamine)
v4, mz4 = get_vector(142.09)
if v1 is not None and v4 is not None:
sim = np.dot(v1, v4)
print(f"Divergent Match -> m/z {mz1:.2f} (Atropine) vs m/z {mz4:.2f} (Tropine): Cosine Similarity = {sim:.4f}")