altered CNN, encoder and loss function to achieve better stability, and fixed morphing of the images lower than the desired resolution

This commit is contained in:
Pixelguy14
2026-06-04 11:58:02 -06:00
parent 242fc6a445
commit d98cfccb5f
48 changed files with 1339 additions and 1366 deletions
@@ -0,0 +1,64 @@
using MSI_src
using Statistics, Plots, Images
# 1. Definir función de contraste (si no la tienes ya en la sesión)
function apply_triq(img_matrix)
vals = filter(x -> x > 0 && isfinite(x), img_matrix)
if isempty(vals) return img_matrix end
q = quantile(vals, 0.98)
if q == 0 q = maximum(vals) end
return clamp.(img_matrix ./ q, 0.0, 1.0)
end
# 2. Configuración de rutas
path_data = "Hoja.imzML"
path_mask = "region_interes.png"
output_folder = "resultados_expertos/batch_triq"
mkpath(output_folder)
# 3. Carga de datos
data = OpenMSIData(path_data)
# 4. Obtener espectro promedio SOLO de la zona de la máscara (ROI)
println("Calculando espectro promedio en la ROI...")
# Nota: Usamos la función de MSI_src para obtener el promedio filtrado
mzs, avg_ints = MSI_src.get_average_spectrum(data, mask_path=path_mask)
# 5. Identificar picos importantes (ej. > 2% del pico base)
max_int = maximum(avg_ints)
umbral = max_int * 0.02 # Ajusta este valor (0.01 para 1%, 0.05 para 5%)
indices_picos = findall(x -> x > umbral, avg_ints)
println("Se encontraron $(length(indices_picos)) iones que superan el umbral.")
# 6. Bucle para generar imágenes con TrIQ
for idx in indices_picos
mz_val = mzs[idx]
# Validar rango (ejemplo: m/z 50 a 700)
if mz_val < 50.0 || mz_val > 700.0
continue
end
println("Procesando m/z: $(round(mz_val, digits=2))")
# A. Extraer los datos crudos con la máscara aplicada
raw_slice = MSI_src.get_mz_slice(data, mz_val, 0.05, mask_path=path_mask)
# B. Aplicar contraste TrIQ
enhanced_slice = apply_triq(raw_slice)
# C. Crear el mapa de calor (Heatmap)
p = heatmap(enhanced_slice,
title="Ion m/z: $(round(mz_val, digits=2))",
color=:viridis,
aspect_ratio=:equal,
yflip=true,
xaxis=false, yaxis=false)
# D. Guardar
nombre_archivo = "ion_$(round(mz_val, digits=2))_triq.png"
savefig(p, joinpath(output_folder, nombre_archivo))
end
println("--- Proceso completado. Revisa la carpeta: $output_folder ---")
@@ -0,0 +1,42 @@
using MSI_src
using Statistics, Images, Interpolations
# 1. Configuration
data_path = "Leaf.imzML"
mask_path = "region_of_interest.png"
output_folder = "datos_para_ai"
img_size = (224, 224) # Standard size for CNN
mkpath(output_folder)
# 2. Data loading
data = OpenMSIData(data_path)
# 3. Get important peaks (you can use your previous logic)
mzs, avg_ints = MSI_src.get_average_spectrum(data, mask_path=mask_path)
threshold = maximum(avg_ints) * 0.02
peak_indices = findall(x -> x > threshold, avg_ints)
println("Exporting $(length(peak_indices)) ions...")
for idx in peak_indices
mz_val = mzs[idx]
if mz_val < 50.0 || mz_val > 700.0 continue end
# A. Extract slice (raw, no plots)
raw_slice = MSI_src.get_mz_slice(data, mz_val, 0.05, mask_path=mask_path)
# B. TrIQ Normalization (as in your script)
vals = filter(x -> x > 0 && isfinite(x), raw_slice)
if isempty(vals) continue end
q = quantile(vals, 0.98)
q = q == 0 ? maximum(vals) : q
enhanced = clamp.(raw_slice ./ q, 0.0, 1.0)
# C. Rescale to 224x224 (Crucial for CNN)
# We use imresize from Images.jl
img_resized = imresize(enhanced, img_size)
# D. Save as raw PNG (Grayscale, no axes)
filename = "ion_$(round(mz_val, digits=2)).png"
save(joinpath(output_folder, filename), Gray.(img_resized))
end