From 98814ce2d53d1e8bdcd22ca85e7cb66d628c899a Mon Sep 17 00:00:00 2001
From: Michiel Cottaar <michiel.cottaar@ndcn.ox.ac.uk>
Date: Wed, 14 Feb 2024 17:09:50 +0000
Subject: [PATCH] Support for building-block containing just single
 EventComponent

---
 src/all_building_blocks/building_blocks.jl | 28 +++++++++++++++++++---
 src/all_sequences/sequences.jl             |  6 +++--
 2 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/src/all_building_blocks/building_blocks.jl b/src/all_building_blocks/building_blocks.jl
index 843b0f7..8f92521 100644
--- a/src/all_building_blocks/building_blocks.jl
+++ b/src/all_building_blocks/building_blocks.jl
@@ -1,8 +1,8 @@
 module BuildingBlocks
 import LinearAlgebra: norm
 import ..BaseBuildingBlocks: BaseBuildingBlock, events, waveform_sequence
-import ...Variables: VariableType, duration, make_generic
-import ...Components: BaseComponent, DelayedEvent
+import ...Variables: VariableType, duration, make_generic, get_pulse, get_readout
+import ...Components: BaseComponent, DelayedEvent, RFPulseComponent, ReadoutComponent
 import ...Readouts: InstantReadout, ADC
 import ...Pulses: RFPulseBlock
 import ...Gradients: GradientBlock
@@ -70,6 +70,28 @@ make_generic(other_block::BaseBuildingBlock) = BuildingBlock(duration(other_bloc
 Base.keys(bb::BuildingBlock) = 1:length(bb.parts)
 Base.getindex(bb::BuildingBlock, i::Integer) = bb.parts[i]
 
-duration(go::BuildingBlock) = go.duration
+duration(bb::BuildingBlock) = sum(duration, waveform_sequence(bb); init=0.)
+
+function get_pulse(bb::BuildingBlock)
+    pulses = [p for p in events(bb) if p isa RFPulseComponent]
+    if length(pulses) == 0
+        error("BuildingBlock does not contain any pulses.")
+    end
+    if length(pulses) == 1
+        return pulses[1]
+    else
+    error("BuildingBlock contains more than one pulse. Not sure which one to return.")
+end
+
+function get_readout(bb::BuildingBlock)
+    readouts = [r for r in events(bb) if r isa ReadoutComponent]
+    if length(readouts) == 0
+        error("BuildingBlock does not contain any readouts.")
+    end
+    if length(readouts) == 1
+        return readouts[1]
+    else
+    error("BuildingBlock contains more than one readout. Not sure which one to return.")
+end
 
 end
\ No newline at end of file
diff --git a/src/all_sequences/sequences.jl b/src/all_sequences/sequences.jl
index 423950e..3abf1ee 100644
--- a/src/all_sequences/sequences.jl
+++ b/src/all_sequences/sequences.jl
@@ -2,8 +2,9 @@ module Sequences
 import StaticArrays: SVector
 import JuMP: @constraint
 import ...Variables: get_free_variable, TR, VariableType
-import ...BuildingBlocks: WaitBlock
+import ...BuildingBlocks: WaitBlock, BuildingBlock
 import ...BuildSequences: get_global_model
+import ...Components: EventComponent
 import ..BaseSequences: ContainerBlock, BaseSequence, nrepeat
 
 """
@@ -42,11 +43,12 @@ Base.getindex(s::Sequence, i::Integer) = s.blocks[i]
 Converst object into something that can be included in the sequence:
 - :min/:max/number/variable/nothing => [`WaitBlock`](@ref).
 - `building_block` or `sequence` => no change.
+- RF pulse/readout => will be embedded within a [`BuildingBlock`](@ref).
 """
 to_block(cb::ContainerBlock) = cb
 to_block(s::Symbol) = to_block(Val(s))
 to_block(s::Union{VariableType, Nothing, Val{:min}, Val{:max}}) = WaitBlock(s)
-
+to_block(ec::EventComponent) = BuildingBlock([], [(0, ec)]; duration=duration(ec))
 
 
 end
\ No newline at end of file
-- 
GitLab