Better image search, 3d images ongoing

This commit is contained in:
Pixelguy14
2025-01-13 00:56:12 -06:00
parent 04f7a107da
commit e5b796fce0
4 changed files with 313 additions and 123 deletions
+244 -81
View File
@@ -8,12 +8,33 @@ using CairoMakie
using Colors
using julia_mzML_imzML
using Statistics
using NaturalSort
# using ImageMagick
@genietools
# == Code import ==
# add your data analysis code here or in the lib folder. Code in lib/ will be
# automatically loaded
rgb_ViridisPalette =reinterpret(ColorTypes.RGB24, ViridisPalette)
rgb_ViridisPalette = reinterpret(ColorTypes.RGB24, ViridisPalette)
# == Search functions ==
function increment_image(current_image, image_list)
current_index = findfirst(isequal(current_image), image_list)
if current_index == nothing || current_index == length(image_list) || current_image === ""
return image_list[length(image_list)] # Return the current image if it's the last one or not found
else
return image_list[current_index + 1] # Move to the next image
end
end
function decrement_image(current_image, image_list)
current_index = findfirst(isequal(current_image), image_list)
if current_index == nothing || current_index == 1 || current_image === ""
return image_list[1] # Return the current image if it's the first one or not found
else
return image_list[current_index - 1] # Move to the previous image
end
end
# == Reactive code ==
# reactive code to make the UI interactive
@@ -27,7 +48,7 @@ rgb_ViridisPalette =reinterpret(ColorTypes.RGB24, ViridisPalette)
# Interface non Variables
@out warning_fr = ""
@out btnStartDisable = false
@out btnStartDisable = true
@out btnPlotDisable = true
@in warning_msg = false
@@ -40,8 +61,10 @@ rgb_ViridisPalette =reinterpret(ColorTypes.RGB24, ViridisPalette)
@in triqColor = 0
# Interface Buttons
@in Main_Process = false # To generate images
@in Generate_Plot = false # To generate plot
@in mainProcess = false # To generate images
@in createSumPlot = false # To generate sum spectrum plot
@in image3dPlot = false # To generate 3d plot based on current image
@in triq3dPlot = false # To generate 3d plot based on current triq image
@in progress = false
@in progressPlot = false
@in triqEnabled = false
@@ -50,12 +73,6 @@ rgb_ViridisPalette =reinterpret(ColorTypes.RGB24, ViridisPalette)
@in ImgPlusT = false
@in ImgMinusT = false
@out indeximg = 0
@out indeximgTriq = 0
@out lastimg = 0
@out lastimgTriq = 0
# Interface Images
@out imgInt = "/.bmp" # image Interface
@out imgIntT = "/.bmp" # image Interface TrIQ
@@ -70,11 +87,28 @@ rgb_ViridisPalette =reinterpret(ColorTypes.RGB24, ViridisPalette)
@out full_routeMz = ""
@out full_routeMz2 = ""
# WIP
@out int_nmass = 0
@out dec_nmass = 0.0
# For the creation of images with a more specific mass charge
@out text_nmass = ""
# Interface Plot
# For image search
# Image lists we apply a filter that searches specific type of images into our public folder, then we sort it in a "numerical" order
@in msi_bmp = sort(filter(filename -> startswith(filename, "MSI_") && endswith(filename, ".bmp"), readdir("public")),lt=natural)
@in col_msi_png = sort(filter(filename -> startswith(filename, "colorbar_MSI_") && endswith(filename, ".png"), readdir("public")),lt=natural)
@in triq_bmp = sort(filter(filename -> startswith(filename, "TrIQ_") && endswith(filename, ".bmp"), readdir("public")),lt=natural)
@in col_triq_png = sort(filter(filename -> startswith(filename, "colorbar_TrIQ_") && endswith(filename, ".png"), readdir("public")),lt=natural)
# Set current image for the list
@out current_msi = ""
@out current_col_msi = ""
@out current_triq = ""
@out current_col_triq = ""
@out indeximg = 0
@out indeximgTriq = 0
@out lastimg = 0
@out lastimgTriq = 0
# Interface Plot 2d
layoutSpectra = PlotlyBase.Layout(
title = "SUM Spectrum plot",
xaxis = PlotlyBase.attr(
@@ -86,22 +120,42 @@ rgb_ViridisPalette =reinterpret(ColorTypes.RGB24, ViridisPalette)
showgrid = true
)
)
# Dummy 2D surface plot
traceSpectra = PlotlyBase.scatter(x=[], y=[], mode="lines")
# Create conection to frontend
@out plotdata = [traceSpectra]
@out plotlayout = layoutSpectra
# Interface Plot 3d
# Define the layout for the 3D plot
layout3D = PlotlyBase.Layout(
title = "3D Surface Plot",
scene = attr(
xaxis_title = "X",
yaxis_title = "Y",
zaxis_title = "Z"
)
)
# Dummy 3D surface plot
trace3D = PlotlyBase.surface(x=[1:10], y=[1:10], z=[1:10], colorscale="Viridis")
# Create conection to frontend
@out plotdata3d = [trace3D]
@out plotlayout3d = layout3D
# println("3D trace defined: ", trace3D)
# == Reactive handlers ==
# Reactive handlers watch a variable and execute a block of code when its value changes
# The onbutton handler will set the variable to false after the block is executed
"""
@onchange triqEnabled begin
if !triqEnabled
#triqProb = 0.0
#triqColor = 0
end
# just in case that they remove an image manually, we use the least intensive button to get all images and re-order them.
msi_bmp = sort(filter(filename -> startswith(filename, "MSI_") && endswith(filename, ".bmp"), readdir("public")),lt=natural)
col_msi_png = sort(filter(filename -> startswith(filename, "colorbar_MSI_") && endswith(filename, ".png"), readdir("public")),lt=natural)
triq_bmp = sort(filter(filename -> startswith(filename, "TrIQ_") && endswith(filename, ".bmp"), readdir("public")),lt=natural)
col_triq_png = sort(filter(filename -> startswith(filename, "colorbar_TrIQ_") && endswith(filename, ".png"), readdir("public")),lt=natural)
end
"""
@onchange file_name begin
msg = ""
@@ -136,10 +190,11 @@ rgb_ViridisPalette =reinterpret(ColorTypes.RGB24, ViridisPalette)
end
"""
@onbutton Main_Process begin
@onbutton mainProcess begin
progress = true # Start progress button animation
btnStartDisable = true # We disable the button to avoid multiple requests
indeximg = floor(Int, Nmass)
text_nmass = replace(string(Nmass), "." => "_")
full_route = joinpath(file_route, file_name)
if isfile(full_route) && Nmass > 0 && Tol > 0 && Tol <= 1
msg = "File exists, Nmass=$(Nmass) Tol=$(Tol). Loading file will begin, please be patient."
@@ -155,26 +210,48 @@ rgb_ViridisPalette =reinterpret(ColorTypes.RGB24, ViridisPalette)
msg = "Incorrect TrIQ values, please adjust accordingly and try again."
warning_msg = true
else
SaveBitmap(joinpath("public", "TrIQ_$(floor(Int, Nmass)).bmp"),TrIQ(slice, Int(triqColor), triqProb),ViridisPalette)
SaveBitmap(joinpath("public", "TrIQ_$(text_nmass).bmp"),TrIQ(slice, Int(triqColor), triqProb),ViridisPalette)
# Use timestamp to refresh image interface container
imgIntT = "/TrIQ_$(floor(Int, Nmass)).bmp?t=$(timestamp)"
msgtriq = "TrIQ image with the Nmass of $(floor(Int, Nmass))"
imgIntT = "/TrIQ_$(text_nmass).bmp?t=$(timestamp)"
# Get current image
current_triq = "TrIQ_$(text_nmass).bmp"
msgtriq = "TrIQ image with the Nmass of $(replace(text_nmass, "_" => "."))"
# Create colorbar
ticks = round.(range(0, stop = maximum(TrIQ(slice, Int(triqColor), triqProb)), length = 10), digits = 2)
Colorbar(fig[1, 1], colormap = rgb_ViridisPalette, limits = (0, maximum(TrIQ(slice, Int(triqColor), triqProb))),ticks = ticks, label = "Intensity")
save("public/colorbar_TrIQ_$(floor(Int, Nmass)).png", fig)
save("public/colorbar_TrIQ_$(text_nmass).png", fig)
colorbarT = "/colorbar_TrIQ_$(text_nmass).png?t=$(timestamp)"
# Get current colorbar
current_col_triq = "colorbar_TrIQ_$(text_nmass).png"
# We update the directory to include the new placed images.
triq_bmp = sort(filter(filename -> startswith(filename, "TrIQ_") && endswith(filename, ".bmp"), readdir("public")),lt=natural)
col_triq_png = sort(filter(filename -> startswith(filename, "colorbar_TrIQ_") && endswith(filename, ".png"), readdir("public")),lt=natural)
msg = "The file has been created successfully inside the 'public' folder of the app."
colorbarT = "/colorbar_TrIQ_$(floor(Int, Nmass)).png?t=$(timestamp)"
#println("all msi in folder = ",triq_bmp)
#println("all col msi in folder= ",col_triq_png)
end
else # If we don't use TrIQ
SaveBitmap(joinpath("public", "MSI_$(floor(Int, Nmass)).bmp"),IntQuant(slice),ViridisPalette)
SaveBitmap(joinpath("public", "MSI_$(text_nmass).bmp"),IntQuant(slice),ViridisPalette)
# Use timestamp to refresh image interface container
imgInt = "/MSI_$(floor(Int, Nmass)).bmp?t=$(timestamp)"
msgimg = "image with the Nmass of $(floor(Int, Nmass))"
imgInt = "/MSI_$(text_nmass).bmp?t=$(timestamp)"
# Get current image
current_msi = "MSI_$(text_nmass).bmp"
msgimg = "image with the Nmass of $(replace(text_nmass, "_" => "."))"
# Create colorbar
ticks = round.(range(0, stop = maximum(slice), length = 10), digits = 2)
Colorbar(fig[1, 1], colormap = rgb_ViridisPalette, limits = (0, maximum(slice)),ticks = ticks, label = "Intensity")
save("public/colorbar_MSI_$(floor(Int, Nmass)).png", fig)
save("public/colorbar_MSI_$(text_nmass).png", fig)
colorbar = "/colorbar_MSI_$(text_nmass).png?t=$(timestamp)"
# Get current colorbar
current_col_msi = "colorbar_MSI_$(text_nmass).png"
# We update the directory to include the new placed images.
msi_bmp = sort(filter(filename -> startswith(filename, "MSI_") && endswith(filename, ".bmp"), readdir("public")),lt=natural)
col_msi_png = sort(filter(filename -> startswith(filename, "colorbar_MSI_") && endswith(filename, ".png"), readdir("public")),lt=natural)
msg = "The file has been created successfully inside the 'public' folder of the app."
colorbar = "/colorbar_MSI_$(floor(Int, Nmass)).png?t=$(timestamp)"
#println("all msi in folder = ",msi_bmp)
#println("all col msi in folder= ",col_msi_png)
end
catch e
msg = "There was an error loading the ImzML file, please verify the file accordingly and try again. $(e)"
@@ -194,15 +271,16 @@ rgb_ViridisPalette =reinterpret(ColorTypes.RGB24, ViridisPalette)
progress = false
end
@onbutton Generate_Plot begin
msg = ""
@onbutton createSumPlot begin
msg = "Sum spectrum plot selected"
full_route = joinpath( file_route, file_name )
progressPlot = true
progressPlot = true
if isfile(full_route) # Check if the file exists
btnPlotDisable = false
btnStartDisable = false
full_routeMz = split( full_route, "." )[1] * ".mzML" # Splitting the route from imzml to mzml so the plotting can work
if isfile(full_routeMz) && (full_routeMz2 == "" || full_routeMz2 != full_routeMz) # Check if the mzml exists
println("I'm working as intended")
btnPlotDisable = true
msg = "Loading plot..."
spectraMz = LoadMzml(full_routeMz)
@@ -227,61 +305,146 @@ rgb_ViridisPalette =reinterpret(ColorTypes.RGB24, ViridisPalette)
progressPlot = false
end
# WIP THESE NEED TO CHANGE, A BETTER BLIND SEARCH AND ALLOWANCE FOR DECIMAL IMAGES
# Also needs to implement timestamp to force reload of image and update if needed.
# Image loaders based on the position of the current image (increment and decrement for both normal and filter)
# And a pre-generated list from all image files from /public folder
@onbutton ImgMinus begin
indeximg-=1
while !isfile("public/MSI_$(indeximg).bmp") && indeximg > 0
indeximg -= 1
end
if(indeximg <= 0) # If it doesn't find a lower value image
indeximg = lastimg
end
imgInt = "/MSI_$(indeximg).bmp"
colorbar = "/colorbar_MSI_$(indeximg).png"
msgimg = "image with the Nmass of $(indeximg)"
lastimg = indeximg
# Append a query string to force the image to refresh
timestamp = string(time_ns())
new_msi = decrement_image(current_msi, msi_bmp)
new_col_msi = decrement_image(current_col_msi, col_msi_png)
current_msi = new_msi
current_col_msi = new_col_msi
imgInt = "/$(current_msi)?t=$(timestamp)"
colorbar = "/$(current_col_msi)?t=$(timestamp)"
text_nmass = replace(current_msi, "MSI_" => "")
text_nmass = replace(text_nmass, ".bmp" => "")
msgimg = "image with the Nmass of $(replace(text_nmass, "_" => "."))"
end
@onbutton ImgPlus begin
indeximg+=1
while !isfile("public/MSI_$(indeximg).bmp") && indeximg < 2001
indeximg += 1
end
if(indeximg >= 2001)
indeximg = lastimg
end
imgInt = "/MSI_$(indeximg).bmp"
colorbar = "/colorbar_MSI_$(indeximg).png"
msgimg = "image with the Nmass of $(indeximg)"
lastimg = indeximg
# Append a query string to force the image to refresh
timestamp = string(time_ns())
new_msi = increment_image(current_msi, msi_bmp)
new_col_msi = increment_image(current_col_msi, col_msi_png)
current_msi = new_msi
current_col_msi = new_col_msi
imgInt = "/$(current_msi)?t=$(timestamp)"
colorbar = "/$(current_col_msi)?t=$(timestamp)"
text_nmass = replace(current_msi, "MSI_" => "")
text_nmass = replace(text_nmass, ".bmp" => "")
msgimg = "image with the Nmass of $(replace(text_nmass, "_" => "."))"
end
@onbutton ImgMinusT begin
indeximgTriq-=1
while !isfile("public/TrIQ_$(indeximgTriq).bmp") && indeximgTriq > 0
indeximgTriq -= 1
end
if(indeximgTriq <= 0) # If it doesn't find a lower value image
indeximgTriq = lastimgTriq
end
imgIntT = "/TrIQ_$(indeximgTriq).bmp"
colorbarT = "/colorbar_TrIQ_$(indeximgTriq).png"
msgtriq = "TrIQ image with the Nmass of $(indeximgTriq)"
lastimgTriq = indeximgTriq
# Append a query string to force the image to refresh
timestamp = string(time_ns())
new_msi = decrement_image(current_triq, triq_bmp)
new_col_msi = decrement_image(current_col_triq, col_triq_png)
current_triq = new_msi
current_col_triq = new_col_msi
imgIntT = "/$(current_triq)?t=$(timestamp)"
colorbarT = "/$(current_col_triq)?t=$(timestamp)"
text_nmass = replace(current_triq, "TrIQ_" => "")
text_nmass = replace(text_nmass, ".bmp" => "")
msgtriq = "image with the Nmass of $(replace(text_nmass, "_" => "."))"
end
@onbutton ImgPlusT begin
indeximgTriq+=1
while !isfile("public/TrIQ_$(indeximgTriq).bmp") && indeximgTriq < 2001
indeximgTriq += 1
end
if(indeximgTriq >= 2001)
indeximgTriq = lastimgTriq
end
imgIntT = "/TrIQ_$(indeximgTriq).bmp"
colorbarT = "/colorbar_TrIQ_$(indeximgTriq).png"
msgtriq = "TrIQ image with the Nmass of $(indeximgTriq)"
lastimgTriq = indeximgTriq
# Append a query string to force the image to refresh
timestamp = string(time_ns())
new_msi = increment_image(current_triq, triq_bmp)
new_col_msi = increment_image(current_col_triq, col_triq_png)
current_triq = new_msi
current_col_triq = new_col_msi
imgIntT = "/$(current_triq)"
colorbarT = "/$(current_col_triq)"
text_nmass = replace(current_triq, "TrIQ_" => "")
text_nmass = replace(text_nmass, ".bmp" => "")
msgtriq = "image with the Nmass of $(replace(text_nmass, "_" => "."))"
end
# 3d plot
@onbutton image3dPlot begin
msg = "Image 3D plot selected"
cleaned_imgInt = replace(imgInt, r"\?.*" => "")
cleaned_imgInt = lstrip(cleaned_imgInt, '/')
var1 = joinpath( "public", cleaned_imgInt )
if isfile(var1)
try
img = FileIO.load(File{DataFormat{:BMP}}(var1))
img_gray = Gray.(img) # Convert to grayscale
img_array = Array(img_gray)
catch e
msg = "Failed to load and process image: $e"
warning_msg = true
println(msg)
end
elevation = img_array / 255.0 # Normalize between 0 and 1
# Smooth the image
sigma = 4.0
elevation_smoothed = imfilter(elevation, Kernel.gaussian(sigma))
# Create the X, Y meshgrid coordinates
x = 1:size(elevation_smoothed, 2)
y = 1:size(elevation_smoothed, 1)
X, Y = ndgrid(x, y)
trace3D = PlotlyBase.scatter(x = X[1, :], y =Y[:, 1], z=elevation_smoothed, colorscale="Viridis")
plotdata3d = [trace3D] # We add the data from the image to the plot
spectraMz = nothing # Important for memory cleaning
GC.gc() # Trigger garbage collection
if Sys.islinux()
ccall(:malloc_trim, Int32, (Int32,), 0) # Ensure julia returns the freed memory to OS
end
msg = "Plot loaded."
else
msg = "image could not be 3d plotted"
warning_msg = true
end
end
@onbutton triq3dPlot begin
msg = "TrIQ 3D plot selected"
cleaned_imgIntT = replace(imgIntT, r"\?.*" => "")
cleaned_imgIntT = lstrip(cleaned_imgIntT, '/')
var1 = joinpath( "public", cleaned_imgIntT )
if isfile(var1)
try
img = FileIO.load(File{DataFormat{:BMP}}(var1))
img_gray = Gray.(img) # Convert to grayscale
img_array = Array(img_gray)
catch e
msg = "Failed to load and process image: $e"
warning_msg = true
println(msg)
end
elevation = img_array / 255.0 # Normalize between 0 and 1
# Smooth the image
sigma = 4.0
elevation_smoothed = imfilter(elevation, Kernel.gaussian(sigma))
# Create the X, Y meshgrid coordinates
x = 1:size(elevation_smoothed, 2)
y = 1:size(elevation_smoothed, 1)
X, Y = ndgrid(x, y)
trace3D = PlotlyBase.scatter(x = X[1, :], y =Y[:, 1], z=elevation_smoothed, colorscale="Viridis")
plotdata3d = [trace3D] # We add the data from the image to the plot
spectraMz = nothing # Important for memory cleaning
GC.gc() # Trigger garbage collection
if Sys.islinux()
ccall(:malloc_trim, Int32, (Int32,), 0) # Ensure julia returns the freed memory to OS
end
msg = "Plot loaded."
else
msg = "image could not be 3d plotted"
warning_msg = true
end
end
GC.gc() # Trigger garbage collection
if Sys.islinux()
ccall(:malloc_trim, Int32, (Int32,), 0) # Ensure julia returns the freed memory to OS