From a92d7df2a6b391159b70e04048a24a4c1a51c264 Mon Sep 17 00:00:00 2001 From: Pablo Villacorta Aylagas Date: Thu, 5 Dec 2024 19:34:39 +0100 Subject: [PATCH 1/4] Remove useless `get_mask` method --- KomaMRICore/src/simulation/Flow.jl | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/KomaMRICore/src/simulation/Flow.jl b/KomaMRICore/src/simulation/Flow.jl index 55f4be49b..a9727b95d 100644 --- a/KomaMRICore/src/simulation/Flow.jl +++ b/KomaMRICore/src/simulation/Flow.jl @@ -81,13 +81,7 @@ function replace_view(replace_by, idx) return replace_by end -function get_mask(spin_reset, t::Real) +function get_mask(spin_reset, t) itp = KomaMRIBase.interpolate(spin_reset, KomaMRIBase.Gridded(KomaMRIBase.Constant{KomaMRIBase.Next}()), Val(size(spin_reset, 1)), t) return KomaMRIBase.resample(itp, t) -end -function get_mask(spin_reset, t::AbstractArray) - itp = KomaMRIBase.interpolate(spin_reset, KomaMRIBase.Gridded(KomaMRIBase.Constant{KomaMRIBase.Next}()), Val(size(spin_reset, 1)), t) - mask = KomaMRIBase.resample(itp, t) - mask .= (cumsum(mask; dims=2) .== 1) - return mask end \ No newline at end of file From a3f66ce8165f17a5c4706b74048abad47d0d2e41 Mon Sep 17 00:00:00 2001 From: Pablo Villacorta Aylagas Date: Tue, 10 Dec 2024 13:11:32 +0100 Subject: [PATCH 2/4] Add jump times to discrete sequence in `discretize` --- .../src/datatypes/simulation/DiscreteSequence.jl | 3 ++- KomaMRIBase/src/motion/Motion.jl | 4 +++- KomaMRIBase/src/motion/MotionList.jl | 10 +++++++++- KomaMRIBase/src/motion/NoMotion.jl | 1 + .../src/motion/actions/arbitraryactions/FlowPath.jl | 3 ++- KomaMRICore/src/simulation/SimulatorCore.jl | 2 +- 6 files changed, 18 insertions(+), 5 deletions(-) diff --git a/KomaMRIBase/src/datatypes/simulation/DiscreteSequence.jl b/KomaMRIBase/src/datatypes/simulation/DiscreteSequence.jl index fe8467db5..62ce535cb 100644 --- a/KomaMRIBase/src/datatypes/simulation/DiscreteSequence.jl +++ b/KomaMRIBase/src/datatypes/simulation/DiscreteSequence.jl @@ -85,8 +85,9 @@ based on simulation parameters. # Returns - `seqd`: (`::DiscreteSequence`) DiscreteSequence struct """ -function discretize(seq::Sequence; sampling_params=default_sampling_params()) +function discretize(seq::Sequence; sampling_params=default_sampling_params(), add_times=[]) t, Δt = get_variable_times(seq; Δt=sampling_params["Δt"], Δt_rf=sampling_params["Δt_rf"]) + sort!(unique!(append!(t, add_times))) B1, Δf = get_rfs(seq, t) Gx, Gy, Gz = get_grads(seq, t) tadc = get_adc_sampling_times(seq) diff --git a/KomaMRIBase/src/motion/Motion.jl b/KomaMRIBase/src/motion/Motion.jl index 6dd7548e4..3ec19bd29 100644 --- a/KomaMRIBase/src/motion/Motion.jl +++ b/KomaMRIBase/src/motion/Motion.jl @@ -202,4 +202,6 @@ end # Auxiliary functions times(m::Motion) = times(m.time) -is_composable(m::Motion) = is_composable(m.action) \ No newline at end of file +is_composable(m::Motion) = is_composable(m.action) +get_jump_times(m::Motion) = get_jump_times(m.action, m.time) +get_jump_times(::AbstractAction, ::AbstractTimeSpan) = [] \ No newline at end of file diff --git a/KomaMRIBase/src/motion/MotionList.jl b/KomaMRIBase/src/motion/MotionList.jl index c94be7f90..91ccfd41f 100644 --- a/KomaMRIBase/src/motion/MotionList.jl +++ b/KomaMRIBase/src/motion/MotionList.jl @@ -1,6 +1,6 @@ -include("Action.jl") include("SpinSpan.jl") include("TimeSpan.jl") +include("Action.jl") include("Motion.jl") """ @@ -202,3 +202,11 @@ function sort_motions!(m::MotionList) sort!(m.motions; by=m -> times(m)[1]) return nothing end + +function get_jump_times(ml::MotionList{T}) where {T<:Real} + jump_times = T[] + for m in ml.motions + append!(jump_times, get_jump_times(m)) + end + return jump_times +end \ No newline at end of file diff --git a/KomaMRIBase/src/motion/NoMotion.jl b/KomaMRIBase/src/motion/NoMotion.jl index 103743c7d..50a8eedb2 100644 --- a/KomaMRIBase/src/motion/NoMotion.jl +++ b/KomaMRIBase/src/motion/NoMotion.jl @@ -49,3 +49,4 @@ function get_spin_coords( ) where {T<:Real} return x, y, z end +get_jump_times(::NoMotion) = [] \ No newline at end of file diff --git a/KomaMRIBase/src/motion/actions/arbitraryactions/FlowPath.jl b/KomaMRIBase/src/motion/actions/arbitraryactions/FlowPath.jl index 5faec8bdb..2367ea71c 100644 --- a/KomaMRIBase/src/motion/actions/arbitraryactions/FlowPath.jl +++ b/KomaMRIBase/src/motion/actions/arbitraryactions/FlowPath.jl @@ -34,4 +34,5 @@ julia> flowpath = FlowPath( dy::AbstractArray{T} dz::AbstractArray{T} spin_reset::AbstractArray{Bool} -end \ No newline at end of file +end +get_jump_times(a::FlowPath, t::AbstractTimeSpan) = (times(t)[end] - times(t)[1])/(size(a.spin_reset)[2]-1) * (getindex.(findall(a.spin_reset .== 1), 2) .- 1) .- 1e-6 \ No newline at end of file diff --git a/KomaMRICore/src/simulation/SimulatorCore.jl b/KomaMRICore/src/simulation/SimulatorCore.jl index 6ba42e583..3bf1b08ea 100644 --- a/KomaMRICore/src/simulation/SimulatorCore.jl +++ b/KomaMRICore/src/simulation/SimulatorCore.jl @@ -336,7 +336,7 @@ function simulate( """ maxlog=1 end # Simulation init - seqd = discretize(seq; sampling_params=sim_params) # Sampling of Sequence waveforms + seqd = discretize(seq; sampling_params=sim_params, add_times=KomaMRIBase.get_jump_times(obj.motion)) # Sampling of Sequence waveforms parts, excitation_bool = get_sim_ranges(seqd; Nblocks=sim_params["Nblocks"]) # Generating simulation blocks t_sim_parts = [seqd.t[p[1]] for p in parts] append!(t_sim_parts, seqd.t[end]) From df42cfdcb4299aea8d9ac3991ecdcea73bbb9efa Mon Sep 17 00:00:00 2001 From: Pablo Villacorta Aylagas Date: Tue, 10 Dec 2024 17:19:31 +0100 Subject: [PATCH 3/4] Remove `unique!` call in `discretize` --- KomaMRIBase/src/datatypes/simulation/DiscreteSequence.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/KomaMRIBase/src/datatypes/simulation/DiscreteSequence.jl b/KomaMRIBase/src/datatypes/simulation/DiscreteSequence.jl index 62ce535cb..6494980e7 100644 --- a/KomaMRIBase/src/datatypes/simulation/DiscreteSequence.jl +++ b/KomaMRIBase/src/datatypes/simulation/DiscreteSequence.jl @@ -87,7 +87,7 @@ based on simulation parameters. """ function discretize(seq::Sequence; sampling_params=default_sampling_params(), add_times=[]) t, Δt = get_variable_times(seq; Δt=sampling_params["Δt"], Δt_rf=sampling_params["Δt_rf"]) - sort!(unique!(append!(t, add_times))) + sort!(append!(t, add_times)) B1, Δf = get_rfs(seq, t) Gx, Gy, Gz = get_grads(seq, t) tadc = get_adc_sampling_times(seq) From 385e4cec5ef249748ed6ae310c4f48188eb7dcdc Mon Sep 17 00:00:00 2001 From: Pablo Villacorta Aylagas Date: Wed, 11 Dec 2024 00:50:14 +0100 Subject: [PATCH 4/4] Add jump times inside `get_variable_times` --- KomaMRIBase/src/datatypes/simulation/DiscreteSequence.jl | 5 ++--- KomaMRIBase/src/motion/Motion.jl | 4 ++-- KomaMRIBase/src/motion/MotionList.jl | 6 ++---- KomaMRIBase/src/motion/NoMotion.jl | 2 +- KomaMRIBase/src/motion/actions/arbitraryactions/FlowPath.jl | 6 +++++- KomaMRIBase/src/timing/TimeStepCalculation.jl | 5 ++++- KomaMRICore/src/simulation/SimulatorCore.jl | 2 +- 7 files changed, 17 insertions(+), 13 deletions(-) diff --git a/KomaMRIBase/src/datatypes/simulation/DiscreteSequence.jl b/KomaMRIBase/src/datatypes/simulation/DiscreteSequence.jl index 6494980e7..cbf6a2be5 100644 --- a/KomaMRIBase/src/datatypes/simulation/DiscreteSequence.jl +++ b/KomaMRIBase/src/datatypes/simulation/DiscreteSequence.jl @@ -85,9 +85,8 @@ based on simulation parameters. # Returns - `seqd`: (`::DiscreteSequence`) DiscreteSequence struct """ -function discretize(seq::Sequence; sampling_params=default_sampling_params(), add_times=[]) - t, Δt = get_variable_times(seq; Δt=sampling_params["Δt"], Δt_rf=sampling_params["Δt_rf"]) - sort!(append!(t, add_times)) +function discretize(seq::Sequence; sampling_params=default_sampling_params(), motion=NoMotion()) + t, Δt = get_variable_times(seq; Δt=sampling_params["Δt"], Δt_rf=sampling_params["Δt_rf"], motion=motion) B1, Δf = get_rfs(seq, t) Gx, Gy, Gz = get_grads(seq, t) tadc = get_adc_sampling_times(seq) diff --git a/KomaMRIBase/src/motion/Motion.jl b/KomaMRIBase/src/motion/Motion.jl index 3ec19bd29..22cd4e3d5 100644 --- a/KomaMRIBase/src/motion/Motion.jl +++ b/KomaMRIBase/src/motion/Motion.jl @@ -203,5 +203,5 @@ end # Auxiliary functions times(m::Motion) = times(m.time) is_composable(m::Motion) = is_composable(m.action) -get_jump_times(m::Motion) = get_jump_times(m.action, m.time) -get_jump_times(::AbstractAction, ::AbstractTimeSpan) = [] \ No newline at end of file +add_jump_times!(t, m::Motion) = add_jump_times!(t, m.action, m.time) +add_jump_times!(t, ::AbstractAction, ::AbstractTimeSpan) = nothing \ No newline at end of file diff --git a/KomaMRIBase/src/motion/MotionList.jl b/KomaMRIBase/src/motion/MotionList.jl index 91ccfd41f..7879f5408 100644 --- a/KomaMRIBase/src/motion/MotionList.jl +++ b/KomaMRIBase/src/motion/MotionList.jl @@ -203,10 +203,8 @@ function sort_motions!(m::MotionList) return nothing end -function get_jump_times(ml::MotionList{T}) where {T<:Real} - jump_times = T[] +function add_jump_times!(t, ml::MotionList) for m in ml.motions - append!(jump_times, get_jump_times(m)) + add_jump_times!(t, m) end - return jump_times end \ No newline at end of file diff --git a/KomaMRIBase/src/motion/NoMotion.jl b/KomaMRIBase/src/motion/NoMotion.jl index 50a8eedb2..233d00a35 100644 --- a/KomaMRIBase/src/motion/NoMotion.jl +++ b/KomaMRIBase/src/motion/NoMotion.jl @@ -49,4 +49,4 @@ function get_spin_coords( ) where {T<:Real} return x, y, z end -get_jump_times(::NoMotion) = [] \ No newline at end of file +add_jump_times!(t, ::NoMotion) = nothing \ No newline at end of file diff --git a/KomaMRIBase/src/motion/actions/arbitraryactions/FlowPath.jl b/KomaMRIBase/src/motion/actions/arbitraryactions/FlowPath.jl index 2367ea71c..391a56839 100644 --- a/KomaMRIBase/src/motion/actions/arbitraryactions/FlowPath.jl +++ b/KomaMRIBase/src/motion/actions/arbitraryactions/FlowPath.jl @@ -35,4 +35,8 @@ julia> flowpath = FlowPath( dz::AbstractArray{T} spin_reset::AbstractArray{Bool} end -get_jump_times(a::FlowPath, t::AbstractTimeSpan) = (times(t)[end] - times(t)[1])/(size(a.spin_reset)[2]-1) * (getindex.(findall(a.spin_reset .== 1), 2) .- 1) .- 1e-6 \ No newline at end of file + +function add_jump_times!(t, a::FlowPath, time_span::AbstractTimeSpan) + jump_times = (times(time_span)[end] - times(time_span)[1])/(size(a.spin_reset)[2]-1) * (getindex.(findall(a.spin_reset .== 1), 2) .- 1) .- 1e-6 + append!(t, jump_times) +end \ No newline at end of file diff --git a/KomaMRIBase/src/timing/TimeStepCalculation.jl b/KomaMRIBase/src/timing/TimeStepCalculation.jl index e1b76ff27..d77e383bb 100644 --- a/KomaMRIBase/src/timing/TimeStepCalculation.jl +++ b/KomaMRIBase/src/timing/TimeStepCalculation.jl @@ -81,13 +81,15 @@ This function returns non-uniform time points that are relevant in the sequence samples for RF excitation (by nominal we mean that the time separation should be at most `Δt_rf` when the samples are regarded by [`KomaMRI.is_RF_on`](@ref), otherwise the time points are not necessary and the separation will be bigger) +- `motion`: (`::Union{NoMotion, Motion, MotionList}`, `=NoMotion()`) phantom motion, + from which it may be necessary to extract key time points relevant for the simulation # Returns - `t`: (`::Vector{Float64}`, `[s]`) time array with non-uniform time values - `Δt`: (`::Vector{Float64}`, `[s]`) delta time array with the separation between two adjacent time points of the `t` time array """ -function get_variable_times(seq; Δt=1e-3, Δt_rf=1e-5) +function get_variable_times(seq; Δt=1e-3, Δt_rf=1e-5, motion=NoMotion()) t = Float64[] ϵ = MIN_RISE_TIME # Small Float64 T0 = get_block_start_times(seq) @@ -120,6 +122,7 @@ function get_variable_times(seq; Δt=1e-3, Δt_rf=1e-5) end append!(t, t_block) end + add_jump_times!(t, motion) # Removing repeated points sort!(unique!(t)) # Fixes a problem with ADC at the start and end of the seq diff --git a/KomaMRICore/src/simulation/SimulatorCore.jl b/KomaMRICore/src/simulation/SimulatorCore.jl index 3bf1b08ea..aff8a56f7 100644 --- a/KomaMRICore/src/simulation/SimulatorCore.jl +++ b/KomaMRICore/src/simulation/SimulatorCore.jl @@ -336,7 +336,7 @@ function simulate( """ maxlog=1 end # Simulation init - seqd = discretize(seq; sampling_params=sim_params, add_times=KomaMRIBase.get_jump_times(obj.motion)) # Sampling of Sequence waveforms + seqd = discretize(seq; sampling_params=sim_params, motion=obj.motion) # Sampling of Sequence waveforms parts, excitation_bool = get_sim_ranges(seqd; Nblocks=sim_params["Nblocks"]) # Generating simulation blocks t_sim_parts = [seqd.t[p[1]] for p in parts] append!(t_sim_parts, seqd.t[end])