From 38c40462dba66f6771e6f10766c22cd83913d224 Mon Sep 17 00:00:00 2001 From: Michiel Cottaar <michiel.cottaar@ndcn.ox.ac.uk> Date: Tue, 20 Feb 2024 14:32:27 +0000 Subject: [PATCH] Set start_time/effective_time for BuildingBlock --- src/containers/building_blocks.jl | 27 ++++++++++++++++++++++++--- src/parts/trapezoids.jl | 2 ++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/containers/building_blocks.jl b/src/containers/building_blocks.jl index f4b2624..a7f7f37 100644 --- a/src/containers/building_blocks.jl +++ b/src/containers/building_blocks.jl @@ -4,7 +4,7 @@ Defines [`BaseBuildingBlock`](@ref), [`BuildingBlock`](@ref) and [`Wait`](@ref). module BuildingBlocks import LinearAlgebra: norm import JuMP: @constraint -import ..Abstract: ContainerBlock +import ..Abstract: ContainerBlock, start_time import ...BuildSequences: global_model import ...Components: BaseComponent, GradientWaveform, EventComponent, NoGradient, ChangingGradient, ConstantGradient, split_gradient, DelayedEvent, RFPulseComponent, ReadoutComponent import ...Variables: qval, bmat_gradient, effective_time, get_free_variable @@ -33,6 +33,7 @@ Base.length(c::BaseBuildingBlock) = length(keys(c)) Base.eltype(::Type{<:BaseBuildingBlock}) = BaseComponent Base.iterate(c::BaseBuildingBlock) = Base.iterate(c, 1) Base.iterate(c::BaseBuildingBlock, index::Integer) = length(c) < index ? nothing : (c[keys(c)[index]], index + 1) +Base.getindex(bb::BaseBuildingBlock, s::Symbol) = bb[Val(s)] """ @@ -104,6 +105,26 @@ function waveform(bb::BaseBuildingBlock) return result end +equal_key(i1::Val, i2) = i1 == Val(i2) +equal_key(i1, i2::Val) = Val(i1) == i2 +equal_key(i1::Val, i2::Val) = i1 == i2 +equal_key(i1, i2) = i1 == i2 + +function start_time(building_block::BaseBuildingBlock, index) + time = 0. + prev_time = 0. + for key in keys(building_block) + if equal_key(key, index) + return prev_time + end + component = building_block[key] + if component isa GradientWaveform + prev_time = time + time += duration(component) + end + end + error("Building block with index '$index' not found") +end # Pathway support """ @@ -126,12 +147,12 @@ function waveform_sequence(bb::BaseBuildingBlock, first, last) current_grad_key = key current_start = nothing end - if key == first || key == Val(first) + if equal_key(key, first) @assert !started started = true current_start = effective_time(bb[key]) end - if key == last || key == Val(last) + if equal_key(key, last) @assert started if isnothing(current_start) push!(parts, (current_grad_key, split_gradient(bb[current_grad_key], effective_time(bb[key]))[1])) diff --git a/src/parts/trapezoids.jl b/src/parts/trapezoids.jl index 4a0a64d..44d404d 100644 --- a/src/parts/trapezoids.jl +++ b/src/parts/trapezoids.jl @@ -150,6 +150,7 @@ inverse_slice_thickness(ss::SliceSelect) = 1e3 * gradient_strength(ss.trapezoid) get_pulse(ss::SliceSelect) = ss.pulse get_gradient(ss::SliceSelect) = ss.trapezoid +effective_time(ss::SliceSelect) = effective_time(lr, :pulse) """ LineReadout(adc; ramp_overlap=1., orientation=nothing, group=nothing, variables...) @@ -193,6 +194,7 @@ Base.getindex(lr::LineReadout, ::Val{:adc}) = lr.adc ramp_overlap(lr::LineReadout) = lr.ramp_overlap inverse_fov(lr::LineReadout) = 1e3 * dwell_time(lr.adc) * gradient_strength(lr.trapezoid) inverse_voxel_size(lr::LineReadout) = 1e3 * duration(lr.adc) * gradient_strength(lr.trapezoid) +effective_time(lr::LineReadout) = effective_time(lr, :adc) get_readout(lr::LineReadout) = lr.adc get_gradient(lr::LineReadout) = lr.trapezoid -- GitLab