From 92e995c4a7ab25d27903900c61ec40d627c4b6a1 Mon Sep 17 00:00:00 2001
From: Michiel Cottaar <michiel.cottaar@ndcn.ox.ac.uk>
Date: Mon, 26 Feb 2024 13:18:24 +0000
Subject: [PATCH] Add cleaner plot_sequence function

---
 ext/MakieMRIBuilder/MakieMRIBuilder.jl | 31 ++++++++++++++++++++++++-
 src/MRIBuilder.jl                      |  3 +++
 src/plot.jl                            | 32 +++++++++-----------------
 3 files changed, 44 insertions(+), 22 deletions(-)

diff --git a/ext/MakieMRIBuilder/MakieMRIBuilder.jl b/ext/MakieMRIBuilder/MakieMRIBuilder.jl
index ab6306c..9805ed5 100644
--- a/ext/MakieMRIBuilder/MakieMRIBuilder.jl
+++ b/ext/MakieMRIBuilder/MakieMRIBuilder.jl
@@ -1,9 +1,27 @@
 module MakieMRIBuilder
 using Makie
 import MakieCore
-import MRIBuilder.Plot: Plot_Sequence, SequenceDiagram, range_line, normalise
+import MakieCore: @recipe, theme, generic_plot_attributes!, Attributes, automatic
+import MRIBuilder.Plot: SequenceDiagram, range_line, normalise, plot_sequence
 import MRIBuilder: BaseSequence, BaseBuildingBlock, duration
 
+@recipe(Plot_Sequence, sequence) do scene
+    attr = Attributes(
+        color = theme(scene, :textcolor),
+        linecolor = automatic,
+        linewidth = 1.5,
+        instant_width = 3.,
+        textcolor = automatic,
+        font = theme(scene, :font),
+        fonts = theme(scene, :fonts),
+        fontsize = theme(scene, :fontsize),
+        fxaa = true,
+    )
+    generic_plot_attributes!(attr)
+    return attr
+end
+
+
 function Makie.plot!(scene:: Plot_Sequence)
     kwargs = Dict([
         key => scene[key] for key in [
@@ -42,4 +60,15 @@ end
 
 Makie.plottype(::Union{BaseBuildingBlock, BaseSequence}) = Plot_Sequence
 
+function plot_sequence(sequence; figure=(), axis=(xgridvisible=false, ygridvisible=false), kwargs...)
+    f = Figure(; figure...)
+    ax = Axis(f[1, 1]; axis...)
+    p = plot!(ax, sequence; kwargs...)
+    ax.xlabel[] = "Time (ms)"
+    hideydecorations!(ax)
+    hidespines!(ax, :l, :r, :t)
+    return Makie.FigureAxisPlot(f, ax, p)
+end
+
+
 end
\ No newline at end of file
diff --git a/src/MRIBuilder.jl b/src/MRIBuilder.jl
index e074a22..59326aa 100644
--- a/src/MRIBuilder.jl
+++ b/src/MRIBuilder.jl
@@ -38,6 +38,9 @@ export dwi_gradients, readout_event, excitation_pulse, refocus_pulse, Trapezoid,
 import .Sequences: GradientEcho, SpinEcho, DiffusionSpinEcho, DW_SE, DWI
 export GradientEcho, SpinEcho, DiffusionSpinEcho, DW_SE, DWI
 
+import .Plot: plot_sequence
+export plot_sequence
+
 import JuMP: @constraint, @objective, objective_function, value, Model
 export @constraint, @objective, objective_function, value, Model
 
diff --git a/src/plot.jl b/src/plot.jl
index 13e06e7..b720d01 100644
--- a/src/plot.jl
+++ b/src/plot.jl
@@ -1,5 +1,5 @@
 module Plot
-import MakieCore: @recipe, theme, generic_plot_attributes!, Attributes, automatic, shading_attributes!, colormap_attributes!
+import MakieCore: generic_plot_attributes!
 import ..Containers: BaseBuildingBlock, BaseSequence, waveform, events, start_time, ndim_grad, waveform_sequence
 import ..Variables: duration, flip_angle, phase, make_generic, gradient_orientation
 import ..Components: RFPulseComponent, ADC, InstantPulse, NoGradient
@@ -159,15 +159,19 @@ function normalise(sd::SequenceDiagram)
     )
 end
 
-
 """
-    plot(sequence; kwargs...)
-    plot!([scene,] sequence; kwargs...)
-    plot_sequence(sequence; kwargs...)
-    plot_sequence!([scene,] sequence; kwargs...)
+    plot_sequence(sequence; figure=(), axis=(), attributes...)
+    plot(sequence; attributes...)
+    plot!([scene,] sequence; attributes...)
 
 Plot the sequence diagram.
 
+Calling `plot_sequence` will result in a much cleaner sequence diagram (recommended).
+However, if you want to combine this diagram with other plots you will have to use `plot` or `plot!` instead.
+
+If called as `plot_sequence` the user can also supply `Makie.Figure` (`figure=(...)`) and `Makie.Axis` (`axis=(...)`) keywords.
+If called using the `plot` or `plot!` interface, only the attributes listed below can be supplied
+
 This function will only work if [`Makie`](https://makie.org) is installed and imported.
 
 ## Attributes
@@ -184,20 +188,6 @@ This function will only work if [`Makie`](https://makie.org) is installed and im
 
 $(Base.Docs.doc(generic_plot_attributes!))
 """
-@recipe(Plot_Sequence, sequence) do scene
-    attr = Attributes(
-        color = theme(scene, :textcolor),
-        linecolor = automatic,
-        linewidth = 1.5,
-        instant_width = 3.,
-        textcolor = automatic,
-        font = theme(scene, :font),
-        fonts = theme(scene, :fonts),
-        fontsize = theme(scene, :fontsize),
-        fxaa = true,
-    )
-    generic_plot_attributes!(attr)
-    return attr
-end
+function plot_sequence end
 
 end
\ No newline at end of file
-- 
GitLab