diff --git a/graph_cluster_spectra.jl b/graph_cluster_spectra.jl new file mode 100644 index 0000000..6e9d7a7 --- /dev/null +++ b/graph_cluster_spectra.jl @@ -0,0 +1,70 @@ +using MSI_src +using CSV, DataFrames +using Plots +using Statistics +using Plots.Measures + +# 1. Data loading +data_path = "Leaf.imzML" +mask_path = "region_of_interest.png" +clusters_path = "msi_clusters_results.csv" + +println("Loading data...") +data = OpenMSIData(data_path) +clusters_df = CSV.read(clusters_path, DataFrame) + +# 2. Get Average Spectrum (Single reference) +mzs_avg, ints_avg = MSI_src.get_average_spectrum(data, mask_path=mask_path) + +# 3. Configure base plot (Thin, clean gray line) +p = plot(mzs_avg, ints_avg, + linecolor = :darkgray, + linewidth = 0.8, + label = "Average Spectrum (Reference)", + xlabel = "m/z", + ylabel = "Relative Intensity", + title = "Cluster Mapping on Isotopic Pattern", + size = (1200, 600), + legend = :outerright, + grid = false, # Clean article style + framestyle = :box, # Full frame + left_margin = 15mm, + bottom_margin = 15mm +) + +# 4. Distinctive color palette +cluster_colors = palette(:tab10) + +# 5. Color specific peaks (Sticks) +println("Highlighting chemical clusters...") +for cluster_id in sort(unique(clusters_df.cluster)) + sub_df = filter(row -> row.cluster == cluster_id, clusters_df) + + label_ready = false + + for target_mz in sub_df.mz + # Find the index of the real peak in the average spectrum + idx = argmin(abs.(mzs_avg .- target_mz)) + + # Tolerance to center the color on the real peak + if abs(mzs_avg[idx] - target_mz) < 0.1 + m = mzs_avg[idx] + i = ints_avg[idx] + + # Draw the cluster "stick" over the base spectrum + plot!(p, [m, m], [0, i], + linecolor = cluster_colors[cluster_id + 1], + linewidth = 1.2, + alpha = 1.0, # Fully opaque to stand out + label = label_ready ? "" : "Cluster $cluster_id" + ) + label_ready = true + end + end +end + +# 6. Save with high resolution +savefig("espectro_msi_paper_style.png") +println("Final plot saved as 'espectro_msi_paper_style.png'") +savefig("espectro_msi_clusters_julia.png") +println("Plot saved as 'espectro_msi_clusters_julia.png'") diff --git a/prepare_data_simclr.jl b/prepare_data_simclr.jl new file mode 100644 index 0000000..68f9151 --- /dev/null +++ b/prepare_data_simclr.jl @@ -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