From f095a94f7ca9a527f11d039ec96ef446af100cf6 Mon Sep 17 00:00:00 2001
From: Michiel Cottaar <MichielCottaar@protonmail.com>
Date: Tue, 21 May 2024 14:15:32 +0100
Subject: [PATCH] Fix raster timings and effect on min_duration

---
 src/sequence_io/pulseq.jl | 9 +++++++--
 test/test_IO.jl           | 6 +++---
 2 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/src/sequence_io/pulseq.jl b/src/sequence_io/pulseq.jl
index 6a4a5a6..f0501a6 100644
--- a/src/sequence_io/pulseq.jl
+++ b/src/sequence_io/pulseq.jl
@@ -23,11 +23,14 @@ function BuildingBlock(pulseq::PulseqBlock; version, BlockDurationRaster, Radiof
     stated_duration = pulseq.duration * BlockDurationRaster * 1e3
 
     events = []
+    min_duration = 0.
     if !isnothing(pulseq.rf)
         if isnothing(pulseq.rf.time)
             time = ((1:length(pulseq.rf.magnitude.samples)) .- 0.5) .* RadiofrequencyRasterTime .* 1e3
+            rf_dur = length(pulseq.rf.magnitude.samples) * RadiofrequencyRasterTime * 1e3
         else
             time = pulseq.rf.time.samples .* 1e3
+            rf_dur = maximum(time)
         end
         push!(events, (
             pulseq.rf.delay * 1e-3,
@@ -37,11 +40,12 @@ function BuildingBlock(pulseq::PulseqBlock; version, BlockDurationRaster, Radiof
                 rad2deg.(pulseq.rf.phase.samples .+ pulseq.rf.phase_offset .+ pulseq.rf.frequency .* time .* 2Ï€)
             )
         ))
+        min_duration = max(min_duration, events[end][1] + rf_dur)
     end
     if !isnothing(pulseq.adc)
         dwell_time = pulseq.adc.dwell * 1e-6
         push!(events, (
-            pulseq.adc.delay * 1e-3
+            pulseq.adc.delay * 1e-3,
             ADC(
                 pulseq.adc.num,
                 dwell_time,
@@ -49,11 +53,12 @@ function BuildingBlock(pulseq::PulseqBlock; version, BlockDurationRaster, Radiof
                 1.
             )
         ))
+        min_duration = max(min_duration, events[end][1] + duration(events[end][2]))
     end
 
     grads = [pulseq.gx, pulseq.gy, pulseq.gz]
     min_duration = max(
-        maximum(e[1] + duration(e[2]) for e in events; init=0.),
+        min_duration,
         maximum(vcat(_control_times.(grads, GradientRasterTime)...); init=0.)
     )
 
diff --git a/test/test_IO.jl b/test/test_IO.jl
index 3766b51..68b6c9c 100644
--- a/test/test_IO.jl
+++ b/test/test_IO.jl
@@ -1,5 +1,5 @@
 @testset "test_IO.jl" begin
-    directory = joinpath(pwd(), "example_pulseseq")
+    directory = joinpath(pwd(), "test", "example_pulseseq")
     @testset "read v1.3.1 fiddisp.seq file" begin
         seq = read_sequence(joinpath(directory, "fiddisp_v131.seq"))
 
@@ -9,8 +9,8 @@
         @test length(events(seq[1])) == 1
         (index, pulse) = events(seq[1])[1]
 
-        @test start_time(seq, 1, index) == 0.1
-        @test end_time(seq, 1, index) == 0.22
+        @test start_time(seq, 1, index) ≈ 0.1
+        @test end_time(seq, 1, index) ≈ 0.2195
 
         for (time, ampl, phase_check) in [
             (0.09, 0., isnan),
-- 
GitLab