From 6202b2aa686bddc74f8310416cbbaea6ebfeb0c6 Mon Sep 17 00:00:00 2001 From: Pablo Villacorta Aylagas Date: Wed, 16 Oct 2024 22:10:41 +0200 Subject: [PATCH 1/7] First commit --- KomaMRIBase/src/datatypes/Phantom.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/KomaMRIBase/src/datatypes/Phantom.jl b/KomaMRIBase/src/datatypes/Phantom.jl index 5f155f28e..41eb800a0 100644 --- a/KomaMRIBase/src/datatypes/Phantom.jl +++ b/KomaMRIBase/src/datatypes/Phantom.jl @@ -31,7 +31,7 @@ julia> obj.ρ """ @with_kw mutable struct Phantom{T<:Real} name::String = "spins" - x :: AbstractVector{T} + x::AbstractVector{T} = @isdefined(T) ? T[] : Float64[] y::AbstractVector{T} = zeros(eltype(x), size(x)) z::AbstractVector{T} = zeros(eltype(x), size(x)) ρ::AbstractVector{T} = ones(eltype(x), size(x)) From 86a504c04221cfdc0573734ebdd318528d78346a Mon Sep 17 00:00:00 2001 From: Pablo Villacorta Aylagas Date: Thu, 17 Oct 2024 11:47:51 +0200 Subject: [PATCH 2/7] Solve motion-related bug --- KomaMRIBase/src/KomaMRIBase.jl | 3 +- KomaMRIBase/src/datatypes/Phantom.jl | 4 +- KomaMRIBase/src/motion/AbstractMotion.jl | 11 ---- .../src/motion/{motionlist => }/MotionList.jl | 27 ++++++---- KomaMRIBase/src/motion/NoMotion.jl | 53 +++++++++++++++++++ KomaMRIBase/src/motion/motionlist/Motion.jl | 2 +- KomaMRIBase/src/motion/nomotion/NoMotion.jl | 53 ------------------- KomaMRICore/src/simulation/Functors.jl | 1 - KomaMRIPlots/src/ui/DisplayFunctions.jl | 11 ++-- docs/src/reference/2-koma-base.md | 8 +-- examples/3.tutorials/lit-05-SimpleMotion.jl | 2 +- .../pluto-01-gradient-echo-spin-echo.jl | 2 +- 12 files changed, 83 insertions(+), 94 deletions(-) delete mode 100644 KomaMRIBase/src/motion/AbstractMotion.jl rename KomaMRIBase/src/motion/{motionlist => }/MotionList.jl (89%) create mode 100644 KomaMRIBase/src/motion/NoMotion.jl delete mode 100644 KomaMRIBase/src/motion/nomotion/NoMotion.jl diff --git a/KomaMRIBase/src/KomaMRIBase.jl b/KomaMRIBase/src/KomaMRIBase.jl index 385012c57..3b7033ba4 100644 --- a/KomaMRIBase/src/KomaMRIBase.jl +++ b/KomaMRIBase/src/KomaMRIBase.jl @@ -26,7 +26,8 @@ include("timing/KeyValuesCalculation.jl") include("datatypes/Sequence.jl") include("datatypes/sequence/Delay.jl") # Motion -include("motion/AbstractMotion.jl") +include("motion/MotionList.jl") +include("motion/NoMotion.jl") # Phantom include("datatypes/Phantom.jl") # Simulator diff --git a/KomaMRIBase/src/datatypes/Phantom.jl b/KomaMRIBase/src/datatypes/Phantom.jl index 41eb800a0..92f87ee29 100644 --- a/KomaMRIBase/src/datatypes/Phantom.jl +++ b/KomaMRIBase/src/datatypes/Phantom.jl @@ -17,7 +17,7 @@ a property value representing a spin. This struct serves as an input for the sim - `Dλ1`: (`::AbstractVector{T<:Real}`) spin Dλ1 (diffusion) parameter vector - `Dλ2`: (`::AbstractVector{T<:Real}`) spin Dλ2 (diffusion) parameter vector - `Dθ`: (`::AbstractVector{T<:Real}`) spin Dθ (diffusion) parameter vector -- `motion`: (`::AbstractMotion{T<:Real}`) motion +- `motion`: (`::Union{NoMotion, MotionList{T<:Real}}`) motion # Returns - `obj`: (`::Phantom`) Phantom struct @@ -47,7 +47,7 @@ julia> obj.ρ Dθ::AbstractVector{T} = zeros(eltype(x), size(x)) #Diff::Vector{DiffusionModel} #Diffusion map #Motion - motion::AbstractMotion{T} = NoMotion{eltype(x)}() + motion::Union{NoMotion, MotionList{T}} = NoMotion() end const NON_STRING_PHANTOM_FIELDS = Iterators.filter(x -> fieldtype(Phantom, x) != String, fieldnames(Phantom)) diff --git a/KomaMRIBase/src/motion/AbstractMotion.jl b/KomaMRIBase/src/motion/AbstractMotion.jl deleted file mode 100644 index ca896fdcd..000000000 --- a/KomaMRIBase/src/motion/AbstractMotion.jl +++ /dev/null @@ -1,11 +0,0 @@ -abstract type AbstractMotion{T<:Real} end - -# NoMotion -include("nomotion/NoMotion.jl") - -# MotionList -include("motionlist/Action.jl") -include("motionlist/SpinSpan.jl") -include("motionlist/TimeSpan.jl") -include("motionlist/Motion.jl") -include("motionlist/MotionList.jl") \ No newline at end of file diff --git a/KomaMRIBase/src/motion/motionlist/MotionList.jl b/KomaMRIBase/src/motion/MotionList.jl similarity index 89% rename from KomaMRIBase/src/motion/motionlist/MotionList.jl rename to KomaMRIBase/src/motion/MotionList.jl index 75e6460a2..0107e2efd 100644 --- a/KomaMRIBase/src/motion/motionlist/MotionList.jl +++ b/KomaMRIBase/src/motion/MotionList.jl @@ -1,3 +1,8 @@ +include("motionlist/Action.jl") +include("motionlist/SpinSpan.jl") +include("motionlist/TimeSpan.jl") +include("motionlist/Motion.jl") + """ motionlist = MotionList(motions...) @@ -27,7 +32,7 @@ julia> motionlist = MotionList( ) ``` """ -struct MotionList{T<:Real} <: AbstractMotion{T} +struct MotionList{T<:Real} motions::Vector{<:Motion{T}} end @@ -40,14 +45,14 @@ function Base.getindex(mv::MotionList{T}, p) where {T<:Real} for m in mv.motions m[p] !== nothing ? push!(motion_array_aux, m[p]) : nothing end - return length(motion_array_aux) > 0 ? MotionList(motion_array_aux) : NoMotion{T}() + return length(motion_array_aux) > 0 ? MotionList(motion_array_aux) : NoMotion() end function Base.view(mv::MotionList{T}, p) where {T<:Real} motion_array_aux = Motion{T}[] for m in mv.motions @view(m[p]) !== nothing ? push!(motion_array_aux, @view(m[p])) : nothing end - return length(motion_array_aux) > 0 ? MotionList(motion_array_aux) : NoMotion{T}() + return length(motion_array_aux) > 0 ? MotionList(motion_array_aux) : NoMotion() end """ Addition of MotionLists """ @@ -91,7 +96,7 @@ Calculates the position of each spin at a set of arbitrary time instants, i.e. t For each dimension (x, y, z), the output matrix has ``N_{\t{spins}}`` rows and `length(t)` columns. # Arguments -- `motionset`: (`::AbstractMotion{T<:Real}`) phantom motion +- `motion`: (`::Union{NoMotion, MotionList{T<:Real}}`) phantom motion - `x`: (`::AbstractVector{T<:Real}`, `[m]`) spin x-position vector - `y`: (`::AbstractVector{T<:Real}`, `[m]`) spin y-position vector - `z`: (`::AbstractVector{T<:Real}`, `[m]`) spin z-position vector @@ -133,27 +138,27 @@ function get_spin_coords( end """ - times = times(motion) + times = times(motion, T) """ -function times(ml::MotionList{T}) where {T<:Real} - nodes = reduce(vcat, [times(m) for m in ml.motions]) +function times(ml::MotionList{T}, ::Type{T}) where {T<:Real} + nodes = reduce(vcat, [times(m, T) for m in ml.motions]) return unique(sort(nodes)) end """ - sort_motions!(motionset) + sort_motions!(motion) Sorts motions in a list according to their starting time. It modifies the original list. If `motionset::NoMotion`, this function does nothing. If `motionset::MotionList`, this function sorts its motions. # Arguments -- `motionset`: (`::AbstractMotion{T<:Real}`) phantom motion +- `motion`: (`::Union{NoMotion, MotionList{T<:Real}}`) phantom motion # Returns - `nothing` """ -function sort_motions!(m::MotionList) - sort!(m.motions; by=m -> times(m)[1]) +function sort_motions!(m::MotionList{T}) where {T<:Real} + sort!(m.motions; by=m -> times(m, T)[1]) return nothing end diff --git a/KomaMRIBase/src/motion/NoMotion.jl b/KomaMRIBase/src/motion/NoMotion.jl new file mode 100644 index 000000000..4833d4fac --- /dev/null +++ b/KomaMRIBase/src/motion/NoMotion.jl @@ -0,0 +1,53 @@ +""" + nomotion = NoMotion() + +NoMotion struct. It is used to create static phantoms. + +# Returns +- `nomotion`: (`::NoMotion`) NoMotion struct + +# Examples +```julia-repl +julia> nomotion = NoMotion() +``` +""" +struct NoMotion end + +Base.getindex(mv::NoMotion, p) = mv +Base.view(mv::NoMotion, p) = mv + +""" Addition of NoMotions """ +Base.vcat(m1::NoMotion, m2::NoMotion, Ns1, Ns2) = m1 +Base.vcat(m1::MotionList, m2::NoMotion, Ns1, Ns2) = vcat(m2, m1, 0, Ns1) +function Base.vcat(m1::NoMotion, m2::MotionList{T}, Ns1, Ns2) where {T} + mv_aux = Motion{T}[] + for m in m2.motions + m_aux = copy(m) + m_aux.spins = expand(m_aux.spins, Ns2) + m_aux.spins = SpinRange(m_aux.spins.range .+ Ns1) + push!(mv_aux, m_aux) + end + return MotionList(mv_aux) +end + +""" Compare two NoMotions """ +Base.:(==)(m1::NoMotion, m2::NoMotion) = true +Base.:(≈)(m1::NoMotion, m2::NoMotion) = true + +function get_spin_coords( + mv::NoMotion, x::AbstractVector{T}, y::AbstractVector{T}, z::AbstractVector{T}, t +) where {T<:Real} + return x, y, z +end + +""" + times = times(motion, T) +""" +times(::NoMotion, ::Type{T}) where {T<:Real} = [zero(T)] + +""" + sort_motions!(nomotion) +""" +function sort_motions!(::NoMotion) + return nothing +end diff --git a/KomaMRIBase/src/motion/motionlist/Motion.jl b/KomaMRIBase/src/motion/motionlist/Motion.jl index d035b562b..9404e8b78 100644 --- a/KomaMRIBase/src/motion/motionlist/Motion.jl +++ b/KomaMRIBase/src/motion/motionlist/Motion.jl @@ -172,5 +172,5 @@ function Base.view(m::Motion, p) end # Auxiliary functions -times(m::Motion) = times(m.time) +times(m::Motion{T}, ::Type{T}) where {T<:Real} = times(m.time) is_composable(m::Motion) = is_composable(m.action) \ No newline at end of file diff --git a/KomaMRIBase/src/motion/nomotion/NoMotion.jl b/KomaMRIBase/src/motion/nomotion/NoMotion.jl deleted file mode 100644 index aba995c7c..000000000 --- a/KomaMRIBase/src/motion/nomotion/NoMotion.jl +++ /dev/null @@ -1,53 +0,0 @@ -""" - nomotion = NoMotion{T<:Real}() - -NoMotion struct. It is used to create static phantoms. - -# Returns -- `nomotion`: (`::NoMotion`) NoMotion struct - -# Examples -```julia-repl -julia> nomotion = NoMotion{Float64}() -``` -""" -struct NoMotion{T<:Real} <: AbstractMotion{T} end - -Base.getindex(mv::NoMotion, p) = mv -Base.view(mv::NoMotion, p) = mv - -""" Addition of NoMotions """ -Base.vcat(m1::NoMotion, m2::NoMotion, Ns1, Ns2) = m1 -Base.vcat(m1, m2::NoMotion, Ns1, Ns2) = vcat(m2, m1, 0, Ns1) -function Base.vcat(m1::NoMotion{T}, m2, Ns1, Ns2) where {T} - mv_aux = Motion{T}[] - for m in m2.motions - m_aux = copy(m) - m_aux.spins = expand(m_aux.spins, Ns2) - m_aux.spins = SpinRange(m_aux.spins.range .+ Ns1) - push!(mv_aux, m_aux) - end - return MotionList(mv_aux) -end - -""" Compare two NoMotions """ -Base.:(==)(m1::NoMotion{T}, m2::NoMotion{T}) where {T<:Real} = true -Base.:(≈)(m1::NoMotion{T}, m2::NoMotion{T}) where {T<:Real} = true - -function get_spin_coords( - mv::NoMotion{T}, x::AbstractVector{T}, y::AbstractVector{T}, z::AbstractVector{T}, t -) where {T<:Real} - return x, y, z -end - -""" - times = times(motion) -""" -times(mv::NoMotion{T}) where {T<:Real} = [zero(T)] - -""" - sort_motions!(motionset) -""" -function sort_motions!(m::NoMotion) - return nothing -end diff --git a/KomaMRICore/src/simulation/Functors.jl b/KomaMRICore/src/simulation/Functors.jl index ab95565a0..8434818c7 100644 --- a/KomaMRICore/src/simulation/Functors.jl +++ b/KomaMRICore/src/simulation/Functors.jl @@ -100,7 +100,6 @@ f64(m) = paramtype(Float64, m) # Koma motion-related adapts adapt_storage(backend::KA.GPU, xs::MotionList) = MotionList(gpu.(xs.motions, Ref(backend))) adapt_storage(T::Type{<:Real}, xs::MotionList) = MotionList(paramtype.(T, xs.motions)) -adapt_storage(T::Type{<:Real}, xs::NoMotion) = NoMotion{T}() #The functor macro makes it easier to call a function in all the parameters # Phantom diff --git a/KomaMRIPlots/src/ui/DisplayFunctions.jl b/KomaMRIPlots/src/ui/DisplayFunctions.jl index 81e0bb198..f07a46ac3 100644 --- a/KomaMRIPlots/src/ui/DisplayFunctions.jl +++ b/KomaMRIPlots/src/ui/DisplayFunctions.jl @@ -1035,8 +1035,8 @@ function plot_phantom_map( max_time_samples=100, kwargs..., ) - function interpolate_times(motion) - t = times(motion) + function interpolate_times(motion, T) + t = times(motion, T) if length(t)>1 # Interpolate time points (as many as indicated by time_samples) itp = interpolate((1:(time_samples + 1):(length(t) + time_samples * (length(t) - 1)), ), t, Gridded(Linear())) @@ -1045,9 +1045,9 @@ function plot_phantom_map( return t end - function process_times(motion) + function process_times(motion, T) KomaMRIBase.sort_motions!(motion) - t = interpolate_times(motion) + t = interpolate_times(motion, T) # Decimate time points so their number is smaller than max_time_samples ss = length(t) > max_time_samples ? length(t) ÷ max_time_samples : 1 return t[1:ss:end] @@ -1122,7 +1122,8 @@ function plot_phantom_map( cmin_key = get(kwargs, :cmin, factor * cmin_key) cmax_key = get(kwargs, :cmax, factor * cmax_key) - t = process_times(obj.motion) + T = typeof(obj).parameters[1] + t = process_times(obj.motion, T) x, y, z = get_spin_coords(obj.motion, obj.x, obj.y, obj.z, t') x0 = -maximum(abs.([x y z])) * 1e2 diff --git a/docs/src/reference/2-koma-base.md b/docs/src/reference/2-koma-base.md index 9d2d4bb0b..2fc69cf23 100644 --- a/docs/src/reference/2-koma-base.md +++ b/docs/src/reference/2-koma-base.md @@ -22,19 +22,13 @@ heart_phantom ## `Motion`-related functions -### `AbstractMotion` types and related functions ```@docs NoMotion +Motion MotionList get_spin_coords ``` -### `Motion` - -```@docs -Motion -``` - ### `AbstractAction` types ```@docs diff --git a/examples/3.tutorials/lit-05-SimpleMotion.jl b/examples/3.tutorials/lit-05-SimpleMotion.jl index d20929f62..63d77ec4c 100644 --- a/examples/3.tutorials/lit-05-SimpleMotion.jl +++ b/examples/3.tutorials/lit-05-SimpleMotion.jl @@ -5,7 +5,7 @@ using PlotlyJS # hide sys = Scanner() # hide # It can also be interesting to see the effect of the patient's motion during an MRI scan. -# For this, Koma provides the ability to add `motion <: AbstractMotion` to the phantom. +# For this, Koma provides the ability to add `motion` to the phantom. # In this tutorial, we will show how to add a [`Translate`](@ref) motion to a 2D brain phantom. # First, let's load the 2D brain phantom used in the previous tutorials: diff --git a/examples/4.reproducible_notebooks/pluto-01-gradient-echo-spin-echo.jl b/examples/4.reproducible_notebooks/pluto-01-gradient-echo-spin-echo.jl index a77bac691..556f1a343 100644 --- a/examples/4.reproducible_notebooks/pluto-01-gradient-echo-spin-echo.jl +++ b/examples/4.reproducible_notebooks/pluto-01-gradient-echo-spin-echo.jl @@ -222,7 +222,7 @@ In this excercise we will simplify this distribution, but we will obtain a simil # (3.1) Create the new obj_t2star phantom begin # (3.1.1) Create an empty phantom - obj_t2star = Phantom{Float64}(x=[]) + obj_t2star = Phantom() # (3.1.2) Define the linear off-resonance distribution Niso = 20 linear_offresonance_distribution = 2π .* range(-10, 10, Niso) From 82e0f089409d563ceaf94de1f814af4386026831 Mon Sep 17 00:00:00 2001 From: Pablo Villacorta Aylagas Date: Wed, 9 Oct 2024 23:47:00 +0200 Subject: [PATCH 3/7] Solve MotionList(rot) bug --- KomaMRIBase/src/motion/MotionList.jl | 2 +- KomaMRIBase/src/motion/motionlist/Motion.jl | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/KomaMRIBase/src/motion/MotionList.jl b/KomaMRIBase/src/motion/MotionList.jl index 0107e2efd..060dc14ef 100644 --- a/KomaMRIBase/src/motion/MotionList.jl +++ b/KomaMRIBase/src/motion/MotionList.jl @@ -37,7 +37,7 @@ struct MotionList{T<:Real} end """ Constructors """ -MotionList(motions...) = length([motions]) > 0 ? MotionList([motions...]) : @error "You must provide at least one motion as input argument. If you do not want to define motion, use `NoMotion{T}()`" +MotionList(motions::Motion...) = length([motions]) > 0 ? MotionList([motions...]) : @error "You must provide at least one motion as input argument. If you do not want to define motion, use `NoMotion{T}()`" """ MotionList sub-group """ function Base.getindex(mv::MotionList{T}, p) where {T<:Real} diff --git a/KomaMRIBase/src/motion/motionlist/Motion.jl b/KomaMRIBase/src/motion/motionlist/Motion.jl index 9404e8b78..988e0069c 100644 --- a/KomaMRIBase/src/motion/motionlist/Motion.jl +++ b/KomaMRIBase/src/motion/motionlist/Motion.jl @@ -32,6 +32,20 @@ julia> motion = Motion( spins ::AbstractSpinSpan = AllSpins() end +# Main constructors +function Motion(action) + T = first(typeof(action).parameters) + return Motion(action, TimeRange(zero(T)), AllSpins()) +end +function Motion(action, time::AbstractTimeSpan) + T = first(typeof(action).parameters) + return Motion(action, time, AllSpins()) +end +function Motion(action, spins::AbstractSpinSpan) + T = first(typeof(action).parameters) + return Motion(action, TimeRange(zero(T)), spins) +end + # Custom constructors """ translate = Translate(dx, dy, dz, time, spins) From 14c7b08ced8f60053193437c7214a596bab71778 Mon Sep 17 00:00:00 2001 From: Pablo Villacorta Aylagas Date: Thu, 17 Oct 2024 13:13:09 +0200 Subject: [PATCH 4/7] Solve `plot_phantom_map` bug for 2D and 1D phantoms --- KomaMRIBase/src/datatypes/Phantom.jl | 2 +- KomaMRIPlots/src/ui/DisplayFunctions.jl | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/KomaMRIBase/src/datatypes/Phantom.jl b/KomaMRIBase/src/datatypes/Phantom.jl index 92f87ee29..94ee8fa3f 100644 --- a/KomaMRIBase/src/datatypes/Phantom.jl +++ b/KomaMRIBase/src/datatypes/Phantom.jl @@ -115,7 +115,7 @@ function get_dims(obj::Phantom) push!(dims, any(x -> x != 0, obj.x)) push!(dims, any(x -> x != 0, obj.y)) push!(dims, any(x -> x != 0, obj.z)) - return dims + return sum(dims) > 0 ? dims : Bool[1, 0, 0] end """ diff --git a/KomaMRIPlots/src/ui/DisplayFunctions.jl b/KomaMRIPlots/src/ui/DisplayFunctions.jl index f07a46ac3..0b4798565 100644 --- a/KomaMRIPlots/src/ui/DisplayFunctions.jl +++ b/KomaMRIPlots/src/ui/DisplayFunctions.jl @@ -1136,9 +1136,21 @@ function plot_phantom_map( l = Layout(;title=obj.name*": "*string(key)) if view_2d # 2D + function get_displayed_dims(v) + if sum(v) == 1 + idx = argmax(v)[1] + return [idx in [1, 3], idx in [1, 2], idx in [2,3]] + else + return v + end + end + # Layout config + dims = get_displayed_dims(KomaMRIBase.get_dims(obj)) + axis = ["x", "y", "z"][dims] + l[:xaxis] = attr( - title="x", + title=axis[1], range=[x0, xf], ticksuffix=" cm", backgroundcolor=plot_bgcolor, @@ -1147,7 +1159,7 @@ function plot_phantom_map( scaleanchor="y" ) l[:yaxis] = attr( - title="y", + title=axis[2], range=[x0, xf], ticksuffix=" cm", backgroundcolor=plot_bgcolor, @@ -1159,8 +1171,8 @@ function plot_phantom_map( # Add traces for i in 1:length(t) push!(traces, scattergl( - x=(x[:,i])*1e2, - y=(y[:,i])*1e2, + x=dims[1] ? (x[:,i])*1e2 : (y[:,i])*1e2, + y=dims[1] & dims[2] ? (y[:,i])*1e2 : (z[:,i])*1e2, mode="markers", marker=attr(color=getproperty(obj,key)*factor, showscale=colorbar, From cd8f75157b76181b41a3f8a061e9c1cf17cc94a2 Mon Sep 17 00:00:00 2001 From: Pablo Villacorta Aylagas Date: Thu, 24 Oct 2024 16:43:55 +0200 Subject: [PATCH 5/7] Remove parameter T in `times` function and simplify `plot_phantom_map` --- KomaMRIBase/src/motion/MotionList.jl | 8 ++++---- KomaMRIBase/src/motion/NoMotion.jl | 12 ------------ KomaMRIBase/src/motion/motionlist/Motion.jl | 2 +- KomaMRIPlots/src/ui/DisplayFunctions.jl | 18 ++++++++---------- 4 files changed, 13 insertions(+), 27 deletions(-) diff --git a/KomaMRIBase/src/motion/MotionList.jl b/KomaMRIBase/src/motion/MotionList.jl index 060dc14ef..727be16bf 100644 --- a/KomaMRIBase/src/motion/MotionList.jl +++ b/KomaMRIBase/src/motion/MotionList.jl @@ -140,8 +140,8 @@ end """ times = times(motion, T) """ -function times(ml::MotionList{T}, ::Type{T}) where {T<:Real} - nodes = reduce(vcat, [times(m, T) for m in ml.motions]) +function times(ml::MotionList) + nodes = reduce(vcat, [times(m) for m in ml.motions]) return unique(sort(nodes)) end @@ -158,7 +158,7 @@ If `motionset::MotionList`, this function sorts its motions. # Returns - `nothing` """ -function sort_motions!(m::MotionList{T}) where {T<:Real} - sort!(m.motions; by=m -> times(m, T)[1]) +function sort_motions!(m::MotionList) + sort!(m.motions; by=m -> times(m)[1]) return nothing end diff --git a/KomaMRIBase/src/motion/NoMotion.jl b/KomaMRIBase/src/motion/NoMotion.jl index 4833d4fac..43f27cb08 100644 --- a/KomaMRIBase/src/motion/NoMotion.jl +++ b/KomaMRIBase/src/motion/NoMotion.jl @@ -39,15 +39,3 @@ function get_spin_coords( ) where {T<:Real} return x, y, z end - -""" - times = times(motion, T) -""" -times(::NoMotion, ::Type{T}) where {T<:Real} = [zero(T)] - -""" - sort_motions!(nomotion) -""" -function sort_motions!(::NoMotion) - return nothing -end diff --git a/KomaMRIBase/src/motion/motionlist/Motion.jl b/KomaMRIBase/src/motion/motionlist/Motion.jl index 988e0069c..869d0e06c 100644 --- a/KomaMRIBase/src/motion/motionlist/Motion.jl +++ b/KomaMRIBase/src/motion/motionlist/Motion.jl @@ -186,5 +186,5 @@ function Base.view(m::Motion, p) end # Auxiliary functions -times(m::Motion{T}, ::Type{T}) where {T<:Real} = times(m.time) +times(m::Motion) = times(m.time) is_composable(m::Motion) = is_composable(m.action) \ No newline at end of file diff --git a/KomaMRIPlots/src/ui/DisplayFunctions.jl b/KomaMRIPlots/src/ui/DisplayFunctions.jl index 0b4798565..7273d3dfc 100644 --- a/KomaMRIPlots/src/ui/DisplayFunctions.jl +++ b/KomaMRIPlots/src/ui/DisplayFunctions.jl @@ -1035,19 +1035,18 @@ function plot_phantom_map( max_time_samples=100, kwargs..., ) - function interpolate_times(motion, T) - t = times(motion, T) + function process_times(::NoMotion) + return [zero(eltype(obj.x))] + end + + function process_times(motion::MotionList) + KomaMRIBase.sort_motions!(motion) + t = times(motion) if length(t)>1 # Interpolate time points (as many as indicated by time_samples) itp = interpolate((1:(time_samples + 1):(length(t) + time_samples * (length(t) - 1)), ), t, Gridded(Linear())) t = itp.(1:(length(t) + time_samples * (length(t) - 1))) end - return t - end - - function process_times(motion, T) - KomaMRIBase.sort_motions!(motion) - t = interpolate_times(motion, T) # Decimate time points so their number is smaller than max_time_samples ss = length(t) > max_time_samples ? length(t) ÷ max_time_samples : 1 return t[1:ss:end] @@ -1122,8 +1121,7 @@ function plot_phantom_map( cmin_key = get(kwargs, :cmin, factor * cmin_key) cmax_key = get(kwargs, :cmax, factor * cmax_key) - T = typeof(obj).parameters[1] - t = process_times(obj.motion, T) + t = process_times(obj.motion) x, y, z = get_spin_coords(obj.motion, obj.x, obj.y, obj.z, t') x0 = -maximum(abs.([x y z])) * 1e2 From 680d35fb34a5b5c9ce8802e69eadd30a50a3ff3c Mon Sep 17 00:00:00 2001 From: Pablo Villacorta Aylagas Date: Thu, 24 Oct 2024 16:55:01 +0200 Subject: [PATCH 6/7] Fix typo in time prefix for `plot_phantom_map` --- KomaMRIPlots/src/ui/DisplayFunctions.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/KomaMRIPlots/src/ui/DisplayFunctions.jl b/KomaMRIPlots/src/ui/DisplayFunctions.jl index 7273d3dfc..696418669 100644 --- a/KomaMRIPlots/src/ui/DisplayFunctions.jl +++ b/KomaMRIPlots/src/ui/DisplayFunctions.jl @@ -1255,7 +1255,7 @@ function plot_phantom_map( ) for (i, t0) in enumerate(t) ], - currentvalue_prefix="x = ", + currentvalue_prefix="t = ", currentvalue_suffix="ms", )] l[:margin] = attr(t=50, l=0, r=0) From 7ac57fc27616cd49a82d7f94debdb1acd0ff9475 Mon Sep 17 00:00:00 2001 From: Pablo Villacorta Aylagas Date: Fri, 1 Nov 2024 17:45:17 +0100 Subject: [PATCH 7/7] Recover previous folder distribution --- KomaMRIBase/src/KomaMRIBase.jl | 4 ++-- KomaMRIBase/src/motion/{ => motionlist}/MotionList.jl | 10 +++++----- KomaMRIBase/src/motion/{ => nomotion}/NoMotion.jl | 0 3 files changed, 7 insertions(+), 7 deletions(-) rename KomaMRIBase/src/motion/{ => motionlist}/MotionList.jl (97%) rename KomaMRIBase/src/motion/{ => nomotion}/NoMotion.jl (100%) diff --git a/KomaMRIBase/src/KomaMRIBase.jl b/KomaMRIBase/src/KomaMRIBase.jl index 3b7033ba4..2f2744b0e 100644 --- a/KomaMRIBase/src/KomaMRIBase.jl +++ b/KomaMRIBase/src/KomaMRIBase.jl @@ -26,8 +26,8 @@ include("timing/KeyValuesCalculation.jl") include("datatypes/Sequence.jl") include("datatypes/sequence/Delay.jl") # Motion -include("motion/MotionList.jl") -include("motion/NoMotion.jl") +include("motion/motionlist/MotionList.jl") +include("motion/nomotion/NoMotion.jl") # Phantom include("datatypes/Phantom.jl") # Simulator diff --git a/KomaMRIBase/src/motion/MotionList.jl b/KomaMRIBase/src/motion/motionlist/MotionList.jl similarity index 97% rename from KomaMRIBase/src/motion/MotionList.jl rename to KomaMRIBase/src/motion/motionlist/MotionList.jl index 727be16bf..953ad394f 100644 --- a/KomaMRIBase/src/motion/MotionList.jl +++ b/KomaMRIBase/src/motion/motionlist/MotionList.jl @@ -1,7 +1,7 @@ -include("motionlist/Action.jl") -include("motionlist/SpinSpan.jl") -include("motionlist/TimeSpan.jl") -include("motionlist/Motion.jl") +include("Action.jl") +include("SpinSpan.jl") +include("TimeSpan.jl") +include("Motion.jl") """ motionlist = MotionList(motions...) @@ -138,7 +138,7 @@ function get_spin_coords( end """ - times = times(motion, T) + times = times(motion) """ function times(ml::MotionList) nodes = reduce(vcat, [times(m) for m in ml.motions]) diff --git a/KomaMRIBase/src/motion/NoMotion.jl b/KomaMRIBase/src/motion/nomotion/NoMotion.jl similarity index 100% rename from KomaMRIBase/src/motion/NoMotion.jl rename to KomaMRIBase/src/motion/nomotion/NoMotion.jl