-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathKomaUI.jl
326 lines (288 loc) · 13.3 KB
/
KomaUI.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# Define observables exported observables
sys_ui = Observable{Scanner}(Scanner())
seq_ui = Observable{Sequence}(Sequence())
obj_ui = Observable{Phantom{Float64}}(Phantom{Float64}(x=[0.0]))
raw_ui = Observable{RawAcquisitionData}(setup_raw())
img_ui = Observable{Array{ComplexF64}}([0.0im 0.; 0. 0.])
"""
out = KomaUI(; kwargs...)
Launch the Koma's UI.
# Keywords
- `darkmode`: (`::Bool`, `=true`) define dark mode style for the UI
- `frame`: (`::Bool`, `=true`) display the upper frame of the Blink window
- `phantom_mode`: (`::String`, `="2D"`, opts=[`"2D"`, `"3D"`]) load the default phantom as a
2D or 3D brain example
- `sim`: (`::Dict{String,Any}`, `=Dict{String,Any}()`) simulation parameters dictionary
- `rec`: (`::Dict{Symbol,Any}`, `=Dict{Symbol,Any}()`) reconstruction parameters dictionary
- `return_window`: (`::Bool`, `=false`) make the `out` be either 'nothing' or the Blink window,
depending on whether the `return_window` keyword argument is set to true
- `show_window`: (`::Bool`, `=true`) display the Blink window
# Returns
- `out`: (`::Nothing` or `::Blink.AtomShell.Window`) returns either 'nothing' or the Blink
window, depending on whether the `return_window` keyword argument is set to true.
# Examples
```julia-repl
julia> KomaUI()
```
"""
function KomaUI(; darkmode=true, frame=true, phantom_mode="2D", sim=Dict{String,Any}(), rec=Dict{Symbol,Any}(), return_window=false, show_window=true, dev_tools=false)
# To avoid generating multiple observables
Observables.clear(seq_ui)
Observables.clear(obj_ui)
Observables.clear(sys_ui)
Observables.clear(raw_ui)
Observables.clear(img_ui)
# For phantom sub-buttons
fieldnames_obj = [fieldnames(Phantom)[5:end-3]...]
widgets_button_obj = button.(string.(fieldnames_obj))
# Setup the Blink window
w, index = setup_blink_window(; darkmode, frame, dev_tools, show_window)
# Setup default simulation inputs (they have observables)
@sync begin
@async sys_ui[] = setup_scanner()
@async seq_ui[] = setup_sequence(sys_ui[])
@async obj_ui[] = setup_phantom(; phantom_mode)
@async ( @info "Loaded `RawAcquisitionData` to `raw_ui[]`"; raw_ui[] = setup_raw() )
@async ( @info "Loaded image to `img_ui[]`"; img_ui[] = [0.0im 0.; 0. 0.] )
end
# Define parameters (they are just internal variables)
sim_params = merge(Dict{String,Any}(), sim)
rec_params = merge(Dict{Symbol,Any}(:reco=>"direct"), rec)
mat_folder = tempdir()
# Boleans to indicate first time for precompilation
is_first_sim = true
is_first_rec = true
# Print GPU information
KomaMRICore.print_gpus()
# Handle "View" sidebar buttons
handle(w, "index") do _
content!(w, "div#content", index)
@js_ w document.getElementById("content").dataset.content = "index"
end
handle(w, "pulses_seq") do _
view_ui!(seq_ui[], w; type="sequence", darkmode)
end
handle(w, "pulses_kspace") do _
view_ui!(seq_ui[], w; type="kspace", darkmode)
end
handle(w, "pulses_M0") do _
view_ui!(seq_ui[], w; type="moment0", darkmode)
end
handle(w, "pulses_M1") do _
view_ui!(seq_ui[], w; type="moment1", darkmode)
end
handle(w, "pulses_M2") do _
view_ui!(seq_ui[], w; type="moment2", darkmode)
end
handle(w, "phantom") do _
view_ui!(obj_ui[], w, seq_ui[], widgets_button_obj; key=:ρ, darkmode)
end
handle(w, "scanner") do _
view_ui!(sys_ui[], w)
end
handle(w, "sim_params") do _
view_ui!(sim_params, w)
end
handle(w, "sig") do _
view_ui!(raw_ui[], w; darkmode)
end
handle(w, "rec_params") do _
view_ui!(rec_params, w)
end
handle(w, "reconstruction_absI") do _
view_ui!(img_ui[], w; type="absi", darkmode)
end
handle(w, "reconstruction_angI") do _
view_ui!(img_ui[], w; type="angi", darkmode)
end
handle(w, "reconstruction_absK") do _
view_ui!(img_ui[], w; type="absk", darkmode)
end
# Handle "Save" to mat sidebar buttons
handle(w, "matfolder") do _
save_ui!(w, seq_ui[], obj_ui[], sys_ui[], raw_ui[], img_ui[], rec_params, mat_folder; type="all")
end
handle(w, "matfolderseq") do _
save_ui!(w, seq_ui[], obj_ui[], sys_ui[], raw_ui[], img_ui[], rec_params, mat_folder; type="sequence")
end
handle(w, "matfolderpha") do _
save_ui!(w, seq_ui[], obj_ui[], sys_ui[], raw_ui[], img_ui[], rec_params, mat_folder; type="phantom")
end
handle(w, "matfoldersca") do _
save_ui!(w, seq_ui[], obj_ui[], sys_ui[], raw_ui[], img_ui[], rec_params, mat_folder; type="scanner")
end
handle(w, "matfolderraw") do _
save_ui!(w, seq_ui[], obj_ui[], sys_ui[], raw_ui[], img_ui[], rec_params, mat_folder; type="raw")
end
handle(w, "matfolderima") do _
save_ui!(w, seq_ui[], obj_ui[], sys_ui[], raw_ui[], img_ui[], rec_params, mat_folder; type="image")
end
# Handle "Simulation" sidebar button
handle(w, "simulate") do _
# Display loading and disable simulation button
msg = "Running simulation ..."
if is_first_sim
msg = "Precompiling and running simulation functions ..."
is_first_sim = false
end
display_loading!(w, msg)
@js_ w document.getElementById("simulate!").setAttribute("disabled", true)
# Define progress bar in the simulation button
progressbar = """
<div class="progress" style="background-color: #27292d;">
<div id="simul_progress" class="progress-bar" role="progressbar" style="width: 0%; transition:none;" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100">0%</div>
</div>
"""
@js_ w (@var progressbar = $progressbar; document.getElementById("simulate!").innerHTML=progressbar)
# Perform simulation
raw_aux = simulate(obj_ui[], seq_ui[], sys_ui[]; sim_params, w)
# After simulation, display the text on the simulation button (not the progress bar)
@js_ w document.getElementById("simulate!").innerHTML="Simulate!"
# Save the raw signal to a file in the temporal directory
rawfile = tempdir()*"/Koma_signal.mrd"
@info "Exporting to ISMRMRD file: $rawfile"
save(ISMRMRDFile(rawfile), raw_aux)
# Display message on UI
sim_time = raw_aux.params["userParameters"]["sim_time_sec"]
@js_ w (
@var sim_time = $sim_time;
@var name = $(obj_ui[].name);
document.getElementById("rawname").innerHTML = "Koma_signal.mrd";
Toasty(
"1",
"""Simulation successfull<br>Time: <a id="sim_time"></a> s""",
"""
<ul>
<li>
<button class="btn btn-dark btn-circle btn-circle-sm m-1" onclick="Blink.msg('sig', 1)"><i class="fa fa-search"></i></button>
Updating <b>Raw signal</b> plots ...
</li>
<li>
<button class="btn btn-primary btn-circle btn-circle-sm m-1" onclick="Blink.msg('recon', 1)"><i class="bi bi-caret-right-fill"></i></button>
Ready to <b>reconstruct</b>?
</li>
</ul>
"""
);
document.getElementById("sim_time").innerHTML = sim_time;
)
# Set the dataset content and enable simulation and reconstruction buttons
@js_ w document.getElementById("content").dataset.content = "simulation"
@js_ w document.getElementById("simulate!").removeAttribute("disabled");
# @js_ w document.getElementById("recon!").removeAttribute("disabled");
# Update the value of the raw signal observable
# this calls the view_ui to display the raw signal
raw_ui[] = raw_aux
end
# Handle "Reconstruction" sidebar button
handle(w, "recon") do _
# Display loading
msg = "Running reconstruction ..."
if is_first_rec
msg = "Precompiling and running reconstruction functions ..."
is_first_rec = false
end
display_loading!(w, msg)
# Update loading icon for button
buffericon = """
<div class="spinner-border spinner-border-sm text-light" role="status"></div>
"""
@js_ w (@var buffericon = $buffericon; document.getElementById("recon!").innerHTML = buffericon)
# Get the value of the raw signal and prepare for reconstruction
raw_aux = raw_ui[]
raw_aux.profiles = raw_aux.profiles[getproperty.(getproperty.(raw_aux.profiles, :head), :flags) .!= 268435456] #Extra profile in JEMRIS simulations
acq_data = AcquisitionData(raw_aux)
acq_data.traj[1].circular = false #Removing circular window
acq_data.traj[1].nodes = acq_data.traj[1].nodes[1:2,:] ./ maximum(2*abs.(acq_data.traj[1].nodes[:])) #Normalize k-space to -.5 to .5 for NUFFT
Nx, Ny = raw_aux.params["reconSize"][1:2]
rec_params[:reconSize] = (Nx, Ny)
rec_params[:densityWeighting] = true
# Perform reconstruction
@info "Running reconstruction ..."
rec_aux = @timed reconstruction(acq_data, rec_params)
image = reshape(rec_aux.value.data, Nx, Ny, :)
# After Recon go to Image
recon_time = rec_aux.time
@js_ w (document.getElementById("recon!").innerHTML = "Reconstruct!")
@js_ w (
@var recon_time = $recon_time;
Toasty(
"2",
"""Reconstruction successfull<br>Time: <a id="recon_time"></a> s""",
"""
<ul>
<li>
<button class="btn btn-dark btn-circle btn-circle-sm m-1" onclick="Blink.msg('reconstruction_absI', 1)"><i class="fa fa-search"></i></button>
Updating <b>Reconstruction</b> plots ...
</li>
</ul>
"""
);
document.getElementById("recon_time").innerHTML = recon_time;
)
@js_ w document.getElementById("content").dataset.content = "reconstruction"
# Update the value of the image observable
# this calls the view_ui to display the image
img_ui[] = image
end
# Filepicker SEQ
@sync begin
widget_filepicker_seq = filepicker(".seq (Pulseq)/.seqk (Koma)"; accept=".seq,.seqk")
content!(w, "#seqfilepicker", widget_filepicker_seq, async=true)
map!((filename) -> callback_filepicker(filename, w, seq_ui[]), seq_ui, widget_filepicker_seq)
end
# Filepicker OBJ
@sync begin
widget_filepicker_obj = filepicker(".phantom (Koma)/.h5 (JEMRIS)"; accept=".phantom,.h5")
content!(w, "#phafilepicker", widget_filepicker_obj, async=true)
map!((filename) -> callback_filepicker(filename, w, obj_ui[]), obj_ui, widget_filepicker_obj)
end
# Filepicker RAW
@sync begin
widget_filepicker_raw = filepicker(".h5/.mrd (ISMRMRD)"; accept=".h5,.mrd")
content!(w, "#sigfilepicker", widget_filepicker_raw, async=true)
map!((filename) -> callback_filepicker(filename, w, raw_ui[]), raw_ui, widget_filepicker_raw)
end
# Listeners
on((seq) -> view_ui!(seq, w; type="sequence", darkmode), seq_ui)
on((obj) -> view_ui!(obj, w, seq_ui[], widgets_button_obj; key=:ρ, darkmode), obj_ui)
for (widget_button, key) in zip(widgets_button_obj, fieldnames_obj)
on((cnt) -> view_ui!(cnt, w, obj_ui[], seq_ui[], widgets_button_obj; key, darkmode), widget_button)
end
on((sys) -> view_ui!(sys, w), sys_ui)
on((raw) -> view_ui!(raw, w; darkmode), raw_ui)
on((img) -> view_ui!(img, w; type="absi", darkmode), img_ui)
# Update Koma versions to tooltip
version_ui = string(KomaMRI.__VERSION__)
version_core = string(KomaMRICore.__VERSION__)
version_io = string(KomaMRIFiles.__VERSION__)
version_plots = string(KomaMRIPlots.__VERSION__)
@js_ w (
@var version_ui = $(version_ui);
@var version_core = $(version_core);
@var version_io = $(version_io);
@var version_plots = $(version_plots);
document.getElementById("Github").setAttribute("data-bs-original-title",
"KomaMRI.jl v"+version_ui+"\n"+
"KomaMRICore.jl v"+version_core+"\n"+
"KomaMRIFiles.jl v"+version_io+"\n"+
"KomaMRIPlots.jl v"+version_plots);
)
@info "Currently using package versions" KomaMRI=version_ui KomaMRICore=version_core KomaMRIFiles=version_io KomaMRIPlots=version_plots
# Devtools
if return_window
return w
end
return nothing
end
"""
Auxiliary function to updates KomaUI's simulation progress bar.
"""
function update_blink_window_progress!(w::Window, block, Nblocks)
progress = string(floor(Int, block / Nblocks * 100))
@js_ w (@var progress = $progress;
document.getElementById("simul_progress").style.width = progress + "%";
document.getElementById("simul_progress").innerHTML = progress + "%";
document.getElementById("simul_progress").setAttribute("aria-valuenow", progress))
return nothing
end