Skip to content

Commit

Permalink
GUI and jump options are now extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
aris-mav committed Jul 30, 2024
1 parent 2f25bc3 commit 8b4d4af
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 169 deletions.
12 changes: 5 additions & 7 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,24 @@ version = "1.0.0-DEV"

[deps]
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
GLMakie = "e9467ef8-e4e7-5192-8a1a-b1aee30e663a"
HiGHS = "87dc4568-4c63-4d18-b0c0-bb2238e4078b"
JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
NativeFileDialog = "e1fe445b-aa65-4df4-81c1-2041507f0fd4"
Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba"
OptimizationOptimJL = "36348300-93cb-4f02-beb5-3c3902f8871e"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
PolygonOps = "647866c9-e3ac-4575-94e7-e3d426903924"
RipQP = "1e40b3f8-35eb-4cd8-8edd-3e515bb9de08"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
PolygonOps = "647866c9-e3ac-4575-94e7-e3d426903924"

[weakdeps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
GLMakie = "e9467ef8-e4e7-5192-8a1a-b1aee30e663a"
QuadraticModels = "f468eda6-eac5-11e8-05a5-ff9e497bcd19"
RipQP = "1e40b3f8-35eb-4cd8-8edd-3e515bb9de08"
Tulip = "6dd1b50a-3aae-11e9-10b5-ef983d2400fa"

[extensions]
gui_ext = "GLMakie"
tikhonov_ripqp_ext = ["RipQP", "QuadraticModels"]
tikhonov_jump_ext = "JuMP"

[compat]
julia = "1.9.0"
Expand Down
37 changes: 21 additions & 16 deletions src/gui.jl → ext/gui_ext.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
function select_peaks(file::String = pick_file(pwd()))
module gui_ext

invres = readresults(file)
using NMRInversions, GLMakie, PolygonOps, LinearAlgebra

function NMRInversions.select_peaks(file::String=pick_file(pwd()))

invres = NMRInversions.readresults(file)
dir = invres.dir
indir = invres.indir
f = invres.f
Expand Down Expand Up @@ -34,7 +38,7 @@ function select_peaks(file::String = pick_file(pwd()))
m = GridLayout(f[1, 1:2])
n = GridLayout(f[2:3, 2])
b = GridLayout(f[2, 1])
# ax3d = Axis3(n[1, 1])
ax3d = Axis3(n[1, 1])

# Title textbox
tb = Textbox(f[3, 1], placeholder="Insert a title for the plot, then press enter.",
Expand Down Expand Up @@ -72,7 +76,7 @@ function select_peaks(file::String = pick_file(pwd()))
hidedecorations!(axtop)
hidedecorations!(axright)
hidedecorations!(axmain)
# hidedecorations!(ax3d)
hidedecorations!(ax3d)

colgap!(m, 0)
rowgap!(m, 0)
Expand All @@ -82,7 +86,7 @@ function select_peaks(file::String = pick_file(pwd()))
empty!(axmain)
empty!(axtop)
empty!(axright)
# empty!(ax3d)
empty!(ax3d)

# Static plots
contourf!(axmain, z, colormap=:tempo, levels=50)
Expand All @@ -91,16 +95,16 @@ function select_peaks(file::String = pick_file(pwd()))
# Plot diagonal line
lines!(axmain, [(0, 0), (length(indir), length(dir))], color=:black, linewidth=1)

# surface!(ax3d, x, y, z, colormap=:tempo)
surface!(ax3d, x, y, z, colormap=:tempo)

# Dynamic plots
contourf!(axmain, spo, mode=:relative, levels=range(0.01, 1, 20), extendlow=:transparent, extendhigh=:transparent, colormap=:blues)
lines!(axtop, indir_dist, colormap=:tab10, colorrange=(1, 10), color=8)
lines!(axright, dir_dist, 1:length(dir), colormap=:tab10, colorrange=(1, 10), color=8)

# surface!(ax3d, x, y, spo, colormap=:blues,
# colorrange=(minimum(filter(x -> x != 0, vec(z))), maximum(z)),
# lowclip=:transparent)
surface!(ax3d, x, y, spo, colormap=:blues,
colorrange=(minimum(filter(x -> x != 0, vec(z))), maximum(z)),
lowclip=:transparent)

# Plot the points in the selection vector
scatter!(axmain, selection, colormap=:tab10, colorrange=(1, 10), color=8)
Expand All @@ -119,7 +123,7 @@ function select_peaks(file::String = pick_file(pwd()))

if size(selection[], 1) > 2
polygon[] = vcat(selection[], [selection[][1]])
mask[] = [inpolygon(p, polygon[]; in=1, on=1, out=0) for p in points]
mask[] = [PolygonOps.inpolygon(p, polygon[]; in=1, on=1, out=0) for p in points]
end

end
Expand Down Expand Up @@ -275,23 +279,21 @@ function select_peaks(file::String = pick_file(pwd()))

on(exportb.clicks) do _
if isnothing(tb.stored_string[])
pubfig(title="")
NMRInversions.pubfig(title="")
else
pubfig(title=tb.stored_string[])
NMRInversions.pubfig(title=tb.stored_string[])
end
end



reset_limits!(axmain)
f

end


function pubfig(file="inversion_results.txt"; title="", ppu=2)
function NMRInversions.pubfig(file="inversion_results.txt"; title="", ppu=2)

invres = readresults(file)
invres = NMRInversions.readresults(file)
dir = invres.dir
indir = invres.indir
f = invres.f
Expand Down Expand Up @@ -398,4 +400,7 @@ function pubfig(file="inversion_results.txt"; title="", ppu=2)

Makie.save("Results.png", m, px_per_unit=ppu)

end


end
43 changes: 43 additions & 0 deletions ext/tikhonov_jump_ext.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module tikhonov_jump_ext

using NMRInversions, JuMP, LinearAlgebra, SparseArrays

# import JuMP: @variable
# import JuMP: @variables
# import JuMP: @constraint
# import JuMP: @constraints
# import JuMP: @objective

function NMRInversions.solve_regularization(K::AbstractMatrix, g::AbstractVector, α::Real, solver::Symbol, order::Int=0)

A = sparse([K; (α) .* NMRInversions.Γ(size(K, 2), order)])
b = sparse([g; zeros(size(A, 1) - size(g, 1))])

@eval model = JuMP.Model($(solver).Optimizer)
set_silent(model)
@variable(model, f[1:size(K, 2)] >= 0)
@variable(model, z[1:size(b, 1)])
@constraint(model, z .== A * f - b)
@objective(model, Min, sum(z .^ 2))

JuMP.optimize!(model)

return JuMP.value.(f), vec(K * JuMP.value.(f) .- g)

end


# other qp formulations


# # set_silent(model)
# # @variable(model, f[1:size(K, 2)] >= 0)
# # @variable(model, r[1:size(K, 1)])
# # @constraint(model, r .== g .- K * f)
# # @objective(model, Min, LinearAlgebra.dot(r, r) + α * LinearAlgebra.dot(f, f))
# # optimize!(model)
# # @assert is_solved_and_feasible(model)
# # return value.(f), value.(r)


end
8 changes: 0 additions & 8 deletions ext/tikhonov_ripqp_ext.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,6 @@ function NMRInversions.solve_regularization(K::AbstractMatrix, g::AbstractVector

end

function NMRInversions.foo()
println("Hello, World, updated!")
end

function NMRInversions.foo(a::Int)
println("sdfasdfa!")
end




Expand Down
79 changes: 20 additions & 59 deletions src/NMRInversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,103 +6,64 @@ using LinearAlgebra
using SparseArrays
using NativeFileDialog
using PolygonOps
# using GLMakie
import JuMP
import HiGHS
import Optimization, OptimizationOptimJL

"""
to do list:
- Move the makie gui to extension
- add L curve method
- add SVD to 1D kernels, implement GCV for 1D inversion
- add gcv for reci method
"""

## The following are custom types for multiple dispatch purposes
# Pulse sequences
customtypes = Dict(
:IR => :inversion1D,
:CPMG => :inversion1D,
:PFG => :inversion1D,
:IRCPMG => :inversion2D
)

abstract type inversion1D end
abstract type inversion2D end
export inversion1D, inversion2D

for (a, A) in customtypes
eval(
quote
struct $a <: $A end
export $a
end
)
end
struct IR <: inversion1D end
struct CPMG <: inversion1D end
struct PFG <: inversion1D end
struct IRCPMG <: inversion2D end
export IR, CPMG, PFG, IRCPMG

# Supported solvers
abstract type regularization_solver end
export regularization_solver

for x in [:song, :ripqp, :pdhgm]
eval(
quote
struct $x <: regularization_solver end
export $x
end
)
end
struct song <: regularization_solver end
struct ripqp <: regularization_solver end
struct pdhgm <: regularization_solver end
export song, ripqp, pdhgm

# Supported methods to determine regularization α parameter
abstract type smoothing_optimizer end
export smoothing_optimizer

for x in [:gcv, :brd, :lcurve]
eval(
quote
struct $x <: smoothing_optimizer end
export $x
end
)
end


# The following functions are modified from extension files
ext_functions = Dict(
:select_peaks => :GLMakie,
:pubfig => :GLMakie
)

for (a,b) in ext_functions
eval(
quote
function $a()
@warn("The function $(string($a)) is only available after loading the $(string($b)) extension. \nDo that by typing 'using $(string($b))'")
end
export $a
end
)
end

struct gcv <: smoothing_optimizer end
struct brd <: smoothing_optimizer end
struct lcurve <: smoothing_optimizer end
export gcv, brd, lcurve

## Include the package files
include("inversions_io.jl")
include("kernels.jl")
include("misc.jl")
include("inversions_1D.jl")
include("inversions_2D.jl")
# include("gui.jl")
include("tikhonov_song.jl")
include("tikhonov_jump.jl")

include("L1_regularization.jl")


# Export useful functions
export invert
export create_kernel
export import_1D
export import_spinsolve, import_geospec
export select_peaks

# The following functions are modified from extension files
function select_peaks end
function pubfig end
export select_peaks, pubfig

end
4 changes: 2 additions & 2 deletions src/inversions_2D.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ function invert(
display("Data saved as inversion_results.txt")
end

if plot == true
return select_peaks(joinpath(pwd(), "inversion_results.txt"))
if plot == true && !isnothing(Base.get_extension(NMRInversions, :gui_ext))
return NMRInversions.select_peaks(joinpath(pwd(), "inversion_results.txt"))
end

return f, r
Expand Down
Loading

0 comments on commit 8b4d4af

Please sign in to comment.