-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create types for MRISequenceEvents to enable multidispatch #362
Labels
Comments
Maybe copy-paste the comment for convenience. |
cncastillo
changed the title
Create types for MRIEvents to enable multidispatch
Create types for MRISequenceEvents to enable multidispatch
Apr 3, 2024
This is the most relevant comment: Just to help, to dispatch based on the using Parameters
abstract type MRISequenceEvent end
NumOrVect = Union{T, Vector{T}} where {T<:Number}
@with_kw_noshow mutable struct Grad{GradAmp<:NumOrVect, TimeDur<:NumOrVect} <: MRISequenceEvent
# Grad properties
A ::GradAmp = zero(eltype(GradAmp))
T ::TimeDur = zero(eltype(TimeDur))
rise ::eltype(TimeDur) = zero(eltype(T))
fall ::eltype(TimeDur) = rise
delay::eltype(TimeDur) = zero(eltype(T))
first::eltype(GradAmp) = zero(eltype(A))
last ::eltype(GradAmp) = first
# Checking validity of Grad
@assert length(A) >= length(T)
@assert all(T .>= zero(eltype(T)))
@assert rise >= zero(eltype(T))
@assert fall >= zero(eltype(T))
@assert delay >= zero(eltype(T))
end
# Gradient type aliases for dispatching
TrapezoidalGrad = Grad{G, T} where {G, T}
UniformlySampledGrad = Grad{Vector{G}, T} where {G, T}
TimeShapedGrad = Grad{Vector{G}, Vector{T}} where {G, T}
ValidGrad = Union{TrapezoidalGrad{G, T}, UniformlySampledGrad{G, T}, TimeShapedGrad{G, T}} where {G, T}
# MRISequenceEvent functions
# Event duration
dur(g::Grad) = g.delay + g.rise + sum(g.T) + g.fall
dur(G::Vector{Grad{G, T}}) where {G, T} = max(dur.(G)...)
# Event amplitude
ampl(g::TrapezoidalGrad{G,T}) where {G, T} = vcat(zero(G), g.first, g.A, g.A, g.last)
ampl(g::TimeShapedGrad{G,T}) where {G, T} = vcat(zero(G), g.first, g.A, g.last)
ampl(g::UniformlySampledGrad{G,T}) where {G, T} = vcat(zero(G), g.first, g.A, g.last)
# Event time
time(g::TrapezoidalGrad{G,T}) where {G, T} = cumsum(vcat(zero(T), g.delay, g.rise, g.T, g.fall))
time(g::TimeShapedGrad{G,T}) where {G, T} = cumsum(vcat(zero(T), g.delay, g.rise, g.T, g.fall))
time(g::UniformlySampledGrad{G,T}) where {G, T} = begin
NA = length(g.A) - 1
ΔT = repeat([g.T / NA], NA)
return cumsum(vcat(zero(T), g.delay, g.rise, ΔT, g.fall))
end This also enables the use Unitful.jl, like using Plots
plot(time(g1), ampl(g1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please view the comments:
The text was updated successfully, but these errors were encountered: