Skip to content

Commit a9de309

Browse files
committed
Refactor MakieRecipes.jl for improved stimulus timing and scale bar functionality
- Simplified the plotting of stimulus spans by removing redundant checks and logging, enhancing performance and readability. - Updated scale bar parameters to use a ratio for text offset instead of fixed values, improving flexibility in positioning. - Adjusted the plotting functions to ensure consistent handling of axis limits and offsets for better visual representation. - Modified test script to reflect changes in stimulus addition and scale bar configuration, ensuring accurate data visualization.
1 parent 31b88da commit a9de309

File tree

2 files changed

+37
-69
lines changed

2 files changed

+37
-69
lines changed

src/PhysMakie/MakieRecipes.jl

Lines changed: 31 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,8 @@ function Makie.plot!(st::StimulusTiming)
256256
# Ensure these functions are available and work with stim_protocol object
257257
start_times = getStimulusStartTime(stim_protocol)
258258
end_times = getStimulusEndTime(stim_protocol)
259-
259+
# println("Start times: $start_times")
260+
# println("End times: $end_times")
260261
# Plot start time lines if enabled
261262
if st.show_start[]
262263
vlines!(st, start_times,
@@ -277,34 +278,11 @@ function Makie.plot!(st::StimulusTiming)
277278

278279
# Plot stimulus span if enabled
279280
if st.show_span[]
280-
# Ensure start_times and end_times are iterable and of the same length
281-
if length(start_times) != length(end_times)
282-
@warn "Mismatch in number of start and end times. Cannot draw spans."
283-
else
284-
for (idx, (start_val, end_val)) in enumerate(zip(start_times, end_times))
285-
@info "StimulusTiming: Preparing vspan $(idx)"
286-
@info " start_val: $(start_val) (Type: $(typeof(start_val)))"
287-
@info " end_val: $(end_val) (Type: $(typeof(end_val)))"
288-
@info " span_color: $(st.span_color[]) (Type: $(typeof(st.span_color[])))"
289-
@info " span_alpha: $(st.span_alpha[]) (Type: $(typeof(st.span_alpha[])))"
290-
processed_start_val = typeof(start_val) <: Quantity ? ustrip(start_val) : start_val
291-
processed_end_val = typeof(end_val) <: Quantity ? ustrip(end_val) : end_val
292-
@info " Processed start_val: $(processed_start_val) (Type: $(typeof(processed_start_val)))"
293-
@info " Processed end_val: $(processed_end_val) (Type: $(typeof(processed_end_val)))"
294-
295-
current_color_tuple = (st.span_color[], st.span_alpha[])
296-
@info " Color tuple for vspan: $(current_color_tuple) (Type: $(typeof(current_color_tuple)))"
297-
298-
try
299-
vspan!(st, [processed_start_val, processed_end_val],
300-
color = current_color_tuple
301-
)
302-
@info "StimulusTiming: vspan $(idx) plotted successfully."
303-
catch e
304-
@error "StimulusTiming: Error during vspan! call for span $(idx)" exception=(e, catch_backtrace())
305-
# Optionally rethrow or handle, for now, let's log and continue if possible
306-
end
307-
end
281+
for i in eachindex(start_times)
282+
vspan!(st, [start_times[i], end_times[i]],
283+
color = st.span_color[],
284+
alpha = st.span_alpha[]
285+
)
308286
end
309287
end
310288

@@ -329,11 +307,10 @@ A recipe for adding scale bars to plots. This recipe adds:
329307
- `color`: Color of the scale bars (default: :black)
330308
- `linewidth`: Width of the scale bars (default: 2.0)
331309
- `text_color`: Color of the scale bar labels (default: :black)
332-
- `text_size`: Size of the scale bar labels (default: 12)
310+
- `fontsize`: Size of the scale bar labels (default: 12)
333311
- `x_label`: Label for the x-scale bar (default: nothing, will use length_x value)
334312
- `y_label`: Label for the y-scale bar (default: nothing, will use length_y value)
335-
- `x_offset`: Offset for x-scale bar label from the bar (default: 0.1 * length_y)
336-
- `y_offset`: Offset for y-scale bar label from the bar (default: 0.1 * length_x)
313+
- `offset_ratio`: Ratio of axis range to use for text offset (default: 0.02)
337314
338315
# Example
339316
```julia
@@ -354,11 +331,10 @@ scale_bar!(ax, start_x=0.1, length_x=10.0, start_y=0.1, length_y=5.0)
354331
color = :black,
355332
linewidth = 2.0,
356333
text_color = :black,
357-
text_size = 12,
334+
fontsize = 12,
358335
x_label = nothing,
359336
y_label = nothing,
360-
x_offset = Observable(0.1),
361-
y_offset = Observable(0.1)
337+
offset_ratio = 0.02
362338
)
363339
end
364340

@@ -369,67 +345,58 @@ Internal plotting function for ScaleBar recipe. Adds scale bars to the current p
369345
based on the specified positions and lengths.
370346
"""
371347
function Makie.plot!(sb::ScaleBar)
372-
# Get the current axis limits
373-
ax = current_axis()
374-
xlims = ax.limits[][1]
375-
ylims = ax.limits[][2]
348+
# Get the parent axis
349+
ax = sb.parent
350+
351+
# Get current axis limits
352+
xlims = ax.finallimits[].origin[1], ax.finallimits[].origin[1] + ax.finallimits[].widths[1]
353+
ylims = ax.finallimits[].origin[2], ax.finallimits[].origin[2] + ax.finallimits[].widths[2]
376354

377-
# Calculate offsets as fractions of the plot size
355+
# Calculate offsets based on axis ranges
378356
x_range = xlims[2] - xlims[1]
379357
y_range = ylims[2] - ylims[1]
380-
381-
# Update offsets if they're Observables
382-
if sb.x_offset isa Observable
383-
sb.x_offset[] = 0.1 * y_range
384-
end
385-
if sb.y_offset isa Observable
386-
sb.y_offset[] = 0.1 * x_range
387-
end
358+
offset = sb.offset_ratio[] * min(x_range, y_range)
388359

389360
# Plot x-scale bar if start_x is specified
390361
if !isnothing(sb.start_x[])
391362
x_start = sb.start_x[]
392363
x_end = x_start + sb.length_x[]
393-
y_pos = isnothing(sb.start_y[]) ? ylims[1] + 0.1 * y_range : sb.start_y[]
364+
y_pos = sb.start_y[] === nothing ? ylims[1] + offset : sb.start_y[]
394365

395-
# Draw the x-scale bar
396366
lines!(sb, [x_start, x_end], [y_pos, y_pos],
397367
color = sb.color[],
398368
linewidth = sb.linewidth[]
399369
)
400370

401-
# Add x-scale bar label
402-
x_label = isnothing(sb.x_label[]) ? "$(sb.length_x[])" : sb.x_label[]
403-
text!(sb, x_label,
404-
position = (x_start + sb.length_x[]/2, y_pos - sb.x_offset[]),
371+
x_label_text = isnothing(sb.x_label[]) ? "$(sb.length_x[])" : sb.x_label[]
372+
text!(sb, x_label_text,
373+
position = (x_start + sb.length_x[]/2, y_pos - offset),
405374
align = (:center, :top),
406375
color = sb.text_color[],
407-
textsize = sb.text_size[]
376+
fontsize = sb.fontsize[]
408377
)
409378
end
410379

411380
# Plot y-scale bar if start_y is specified
412381
if !isnothing(sb.start_y[])
413382
y_start = sb.start_y[]
414383
y_end = y_start + sb.length_y[]
415-
x_pos = isnothing(sb.start_x[]) ? xlims[1] + 0.1 * x_range : sb.start_x[]
384+
x_pos = sb.start_x[] === nothing ? xlims[1] + offset : sb.start_x[]
416385

417-
# Draw the y-scale bar
418386
lines!(sb, [x_pos, x_pos], [y_start, y_end],
419387
color = sb.color[],
420388
linewidth = sb.linewidth[]
421389
)
422390

423-
# Add y-scale bar label
424-
y_label = isnothing(sb.y_label[]) ? "$(sb.length_y[])" : sb.y_label[]
425-
text!(sb, y_label,
426-
position = (x_pos - sb.y_offset[], y_start + sb.length_y[]/2),
391+
y_label_text = isnothing(sb.y_label[]) ? "$(sb.length_y[])" : sb.y_label[]
392+
text!(sb, y_label_text,
393+
position = (x_pos - offset, y_start + sb.length_y[]/2),
427394
align = (:right, :center),
428395
color = sb.text_color[],
429-
textsize = sb.text_size[],
430-
rotation = -π/2
396+
fontsize = sb.fontsize[],
397+
rotation = -pi/2 # Makie uses radians for rotation
431398
)
432399
end
433400

434-
sb
401+
return sb
435402
end

test/revision.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ stim_fn = raw"F:\Data\Patching\2025-03-26-GRAB-DA_STR\25326050.abf"
1414
img_exp = readImage(img_fn);
1515
deinterleave!(img_exp)
1616
stim_exp = readABF(stim_fn);
17-
addStimulus!(img_exp, stim_exp, "IN 3", flatten_episodic = true, stimulus_threshold = 0.5)
17+
addStimulus!(img_exp, stim_fn, "IN 3"; flatten_episodic = true, stimulus_threshold = 0.5)
18+
1819
stim_protocol = getStimulusProtocol(img_exp)
20+
1921
spike_train_group!(stim_protocol, 3.0)
22+
stim_protocol.timestamps
2023

2124
# Example of using stimulus timing and scale bar recipes
2225
z_profile = project(img_exp, dims=(1,2))[1,1,:,1]
@@ -25,7 +28,7 @@ baseline_trace = PhysiologyAnalysis.baseline_trace(z_profile,
2528
lam = 1e4,
2629
niter = 100
2730
)
28-
time_axis = data2P.t
31+
time_axis = img_exp.t
2932

3033
#%% Plot
3134
fig = Figure(size=(800, 400))
@@ -42,15 +45,13 @@ lines!(ax, time_axis, baseline_trace, color=:green, linewidth=2.5)
4245
stimulustiming!(ax, img_exp,
4346
show_start = true,
4447
show_end = true,
45-
show_span = true,
48+
show_span = false,
4649
start_color = :blue,
4750
end_color = :red,
4851
span_color = :gray,
4952
span_alpha = 0.25
5053
)
51-
display(fig)
5254

53-
#%%
5455
scalebar!(ax,
5556
start_x = 40.0, # Start at 40 seconds
5657
length_x = 25.0, # 25 second scale bar

0 commit comments

Comments
 (0)