From cc85b5122d5e4879fe99005125bb8eb5a31258da Mon Sep 17 00:00:00 2001
From: Michiel Cottaar <michiel.cottaar@ndcn.ox.ac.uk>
Date: Mon, 26 Feb 2024 12:55:05 +0000
Subject: [PATCH] Apply consistent normalisation across RF and grad

---
 ext/MakieMRIBuilder/MakieMRIBuilder.jl | 14 ++++++--------
 src/plot.jl                            | 21 +++++++++++++++++++++
 2 files changed, 27 insertions(+), 8 deletions(-)

diff --git a/ext/MakieMRIBuilder/MakieMRIBuilder.jl b/ext/MakieMRIBuilder/MakieMRIBuilder.jl
index 665672a..9e58775 100644
--- a/ext/MakieMRIBuilder/MakieMRIBuilder.jl
+++ b/ext/MakieMRIBuilder/MakieMRIBuilder.jl
@@ -1,7 +1,7 @@
 module MakieMRIBuilder
 using Makie
 import MakieCore
-import MRIBuilder.Plot: Plot_Sequence, SequenceDiagram, range_line
+import MRIBuilder.Plot: Plot_Sequence, SequenceDiagram, range_line, normalise
 import MRIBuilder: BaseSequence, BaseBuildingBlock
 
 function Makie.plot!(scene:: Plot_Sequence)
@@ -20,19 +20,17 @@ function Makie.plot!(scene:: Plot_Sequence)
     instant_width = map((a, c) -> a * c, scene[:linewidth], scene[:instant_width])
 
     lift(scene[:sequence]) do sequence
-        sequence_diagram = SequenceDiagram(sequence)
+        sequence_diagram = normalise(SequenceDiagram(sequence))
         current_y = 0.
         for label in (:ADC, :Gz, :Gy, :Gx, :G, :RFy, :RFx)
             line = getproperty(sequence_diagram, label)
-            full_range = range_line(line)
-            norm = max(abs.(full_range)...)
-            (lower, upper) = full_range ./ norm
-            if norm > 0
+            (lower, upper) = range_line(line)
+            if !(lower ≈ upper)
                 shift = current_y - lower
                 Makie.text!(scene, string(label) * " "; position=(0., shift), align=(:right, :center), text_kwargs..., kwargs...)
-                Makie.lines!(scene, line.times, line.amplitudes ./ norm .+ shift; color=line_color, linewidth=scene[:linewidth], kwargs...)
+                Makie.lines!(scene, line.times, line.amplitudes .+ shift; color=line_color, linewidth=scene[:linewidth], kwargs...)
                 for (time, amplitude) in zip(line.event_times, line.event_amplitudes)
-                    Makie.lines!([time, time], [0., amplitude / norm] .+ shift; color=line_color, linewidth=instant_width, kwargs...)
+                    Makie.lines!([time, time], [0., amplitude] .+ shift; color=line_color, linewidth=instant_width, kwargs...)
                 end
                 current_y += (upper - lower) + 0.1
             end
diff --git a/src/plot.jl b/src/plot.jl
index 9aeb41f..00fec0f 100644
--- a/src/plot.jl
+++ b/src/plot.jl
@@ -56,6 +56,8 @@ range_line(spl::SinglePlotLine) = (
     max(maximum(spl.amplitudes; init=0.), maximum(spl.event_amplitudes; init=0.)),
 )
 
+normalise(spl::SinglePlotLine, value) = iszero(value) ? spl : SinglePlotLine(spl.times, spl.amplitudes ./ value, spl.event_times, spl.event_amplitudes ./ value)
+
 """
     SequenceDiagram(; RFx, RFy, Gx, Gy, Gz, ADC)
 
@@ -138,6 +140,25 @@ function SequenceDiagram(seq::BaseSequence)
         for symbol in [:RFx, :RFy, :G, :Gx, :Gy, :Gz, :ADC]]...)
 end
 
+function normalise(sd::SequenceDiagram)
+    rf_norm = max(abs.(range_line(sd.RFx))..., abs.(range_line(sd.RFy))...)
+    grad_norm = max(
+        abs.(range_line(sd.G))...,
+        abs.(range_line(sd.Gx))...,
+        abs.(range_line(sd.Gy))...,
+        abs.(range_line(sd.Gz))...,
+    )
+    SequenceDiagram(
+        normalise(sd.RFx, rf_norm),
+        normalise(sd.RFy, rf_norm),
+        normalise(sd.G, grad_norm),
+        normalise(sd.Gx, grad_norm),
+        normalise(sd.Gy, grad_norm),
+        normalise(sd.Gz, grad_norm),
+        sd.ADC
+    )
+end
+
 
 """
     plot(sequence; kwargs...)
-- 
GitLab