Skip to content

Commit

Permalink
improved xfit, yfit for invert and expfit
Browse files Browse the repository at this point in the history
  • Loading branch information
aris committed Dec 10, 2024
1 parent f51a6c6 commit afb0452
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
14 changes: 8 additions & 6 deletions ext/gui_ext.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ function Makie.plot!(fig::Union{Makie.Figure,Makie.GridPosition}, res::expfit_st
end

for (i, r) in enumerate(res)
xfit = collect(range(0, 1.1 * maximum(r.x), 500))
scatter!(ax, r.x, r.y, markersize=markersize, label= r.title )
lines!(ax, xfit, mexp(r.seq, r.u, xfit), label=getfield(r, normeq == true ? :eqn : :eq))
lines!(ax, r.xfit, r.yfit, label=getfield(r, normeq == true ? :eqn : :eq))
end

axislegend(ax, position=(res[1].seq == IR ? :rb : :rt), nbanks=2)
Expand Down Expand Up @@ -119,7 +118,7 @@ function Makie.plot(res_mat::AbstractVecOrMat{NMRInversions.inv_out_1D}; selecti
linkxaxes!(ax1, ax3)

for r in res_mat
draw_on_axes(ax1, ax2,ax3, r, selections = selections)
draw_on_axes(ax1, ax2, ax3, r, selections = selections)
end

return fig
Expand Down Expand Up @@ -196,11 +195,14 @@ function draw_on_axes(ax1, ax2, ax3, res::NMRInversions.inv_out_1D; selections =

c = length(ax2.scene.plots)

scatter!(ax1, res.x, res.y, colormap=:tab10, colorrange=(1, 10), color=c)
scatter!(ax1, res.x, real.(res.y), colormap=:tab10, colorrange=(1, 10), color=c)
lines!(ax1, res.xfit, res.yfit, colormap=:tab10, colorrange=(1, 10), color=c)
lines!(ax2, res.X, res.f, colormap=:tab10, colorrange=(1, 10), color=c)
scatter!(ax3, res.x, res.r, colormap=:tab10, colorrange=(1, 10), color=c)
lines!(ax3, res.x, res.r, colormap=:tab10, colorrange=(1, 10), color=c)
if length(res.x) == length(res.r)
scatter!(ax3, res.x, res.r, colormap=:tab10, colorrange=(1, 10), color=c)
lines!(ax3, res.x, res.r, colormap=:tab10, colorrange=(1, 10), color=c)
end


if selections

Expand Down
12 changes: 8 additions & 4 deletions src/exp_fits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,13 @@ function expfit(
# Calculate the residuals
r = mexp(seq, u, x) - y

xfit = exp10.(range(log10(1e-8), log10(1.1 * maximum(x)), 512))
yfit = mexp(seq, u, xfit)

if length(u0) == 2
return expfit_struct(seq, x, y, u, u0, r, eq, eq, "")
return expfit_struct(seq, x, y, xfit, yfit, u, u0, r, eq, eq, "")
else
return expfit_struct(seq, x, y, u, u0, r, eq, eqn, "")
return expfit_struct(seq, x, y, xfit, yfit, u, u0, r, eq, eqn, "")
end

end
Expand All @@ -166,8 +169,9 @@ Arguments:
- `n` : Integer specifying the number of exponential terms.
- `data` : `input1D` structure containing the data to be fitted.
"""
function expfit(n::Union{Int, Vector{<:Real}}, res::NMRInversions.input1D; kwargs...)
return expfit(n, res.seq, res.x, res.y; kwargs...)
function expfit(n::Union{Int, Vector{<:Real}}, data::NMRInversions.input1D; kwargs...)
return expfit(n, data.seq, data.x, data.y; kwargs...)
end

4 changes: 2 additions & 2 deletions src/inversion_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ function invert(seq::Type{<:pulse_sequence1D}, x::AbstractArray, y::Vector;

else
error("alpha must be a real number or a smoothing_optimizer type.")

end

x_fit = collect(range(0, x[end] + 0.1 * x[end], 128))
x_fit = exp10.(range(log10(1e-8), log10(1.1 * x[end]), 512))
y_fit = create_kernel(seq, x_fit, X) * f

isreal(y) ? SNR = NaN : SNR = calc_snr(y)
Expand Down
4 changes: 4 additions & 0 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ The fields are as follows:
- `seq`: The pulse sequence
- `x` : The x acquisition values (e.g. time for relaxation or b-factor for diffusion).
- `y` : The y acquisition values.
- `xfit` : An array with fitted x values (for plotting purposes).
- `yfit` : An array with fitted y values (for plotting purposes).
- `u` : The fitted parameters for the `mexp` function.
- `u0` : The initial parameters for the `mexp` function.
- `r` : The residuals.
Expand All @@ -295,6 +297,8 @@ mutable struct expfit_struct
seq::Type{<:NMRInversions.pulse_sequence1D}
x::Vector
y::Vector
xfit::Vector
yfit::Vector
u::Vector
u0::Vector
r::Vector
Expand Down

0 comments on commit afb0452

Please sign in to comment.