Skip to content

Commit

Permalink
Change of default behavior for MAXIMA_THRESHOLD_WINDOW
Browse files Browse the repository at this point in the history
- MAXIMA_THRESHOLD_WINDOW has now a default value equal to 'None', corresponding to set it equal to the entire signal duration. Also accepts float values.
- Little fix involving plot_minima function in minima.py script, now plotting the interpolated position of minima when required.
  • Loading branch information
cosimolupo committed Sep 26, 2022
1 parent 16906e3 commit 961e393
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pipeline/stage03_trigger_detection/Snakefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def filtered_triggers(wildcards):
return prev_rule_output(wildcards, rule_list=config.TRIGGER_FILTER,
default_input=default_input)

#### UTILTY BLOCKS ####
#### UTILITY BLOCKS ####

use rule template_all as all with:
input:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ MIN_PEAK_DISTANCE: 0.28
# amplitude fraction to set the threshold detecting local maxima
MAXIMA_THRESHOLD_FRACTION: .5
# time window to use to set the threshold detecting local maxima (s)
MAXIMA_THRESHOLD_WINDOW: 3
# default value 'None' is meant to set the time window equal to the entire signal length
MAXIMA_THRESHOLD_WINDOW: 'None'
# minimum time the signal must be increasing after a minima candidate (s)
MINIMA_PERSISTENCE: 0.16

Expand Down
23 changes: 13 additions & 10 deletions pipeline/stage03_trigger_detection/scripts/minima.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ def plot_minima(asig, event, channel, maxima_threshold_window,

idx_ch = np.where(event.array_annotations['channels'] == channel)[0]

ax.plot(times[peaks], signal[peaks], 'x', color='r', label='detected maxima')
ax.plot(event.times[idx_ch], signal[(event.times[idx_ch]*sampling_rate).astype(int)],
ax.plot(times[peaks], signal[peaks], 'x', color='r', label='detected maxima')
ax.plot(event.times[idx_ch], np.interp(event.times[idx_ch], times, signal),
'x', color='g', label='selected minima')

ax.set_title(f'channel {channel}')
Expand All @@ -164,8 +164,8 @@ def plot_minima(asig, event, channel, maxima_threshold_window,
help="minimum time minima (s)")
CLI.add_argument("--maxima_threshold_fraction", nargs='?', type=float, default=0.5,
help="amplitude fraction to set the threshold detecting local maxima")
CLI.add_argument("--maxima_threshold_window", nargs='?', type=int, default=2,
help="time window to use to set the threshold detecting local maxima [s]")
CLI.add_argument("--maxima_threshold_window", nargs='?', type=none_or_float, default=None,
help="time window to use to set the threshold detecting local maxima (s)")
CLI.add_argument("--min_peak_distance", nargs='?', type=float, default=0.200,
help="minimum distance between peaks (s)")
CLI.add_argument("--img_dir", nargs='?', type=Path,
Expand All @@ -183,13 +183,16 @@ def plot_minima(asig, event, channel, maxima_threshold_window,

block = load_neo(args.data)
asig = block.segments[0].analogsignals[0]

if args.maxima_threshold_window is None or args.maxima_threshold_window > asig.t_stop - asig.t_start:
args.maxima_threshold_window = asig.t_stop - asig.t_start

transition_event = detect_minima(asig,
interpolation_points=args.num_interpolation_points,
maxima_threshold_fraction=args.maxima_threshold_fraction,
maxima_threshold_window=args.maxima_threshold_window,
min_peak_distance=args.min_peak_distance,
minima_persistence=args.minima_persistence)
interpolation_points = args.num_interpolation_points,
maxima_threshold_fraction = args.maxima_threshold_fraction,
maxima_threshold_window = args.maxima_threshold_window,
min_peak_distance = args.min_peak_distance,
minima_persistence = args.minima_persistence)

block.segments[0].events.append(transition_event)

Expand All @@ -200,7 +203,7 @@ def plot_minima(asig, event, channel, maxima_threshold_window,
plot_minima(asig=time_slice(asig, args.plot_tstart, args.plot_tstop),
event=time_slice(transition_event, args.plot_tstart, args.plot_tstop),
channel=int(channel),
maxima_threshold_window = args.maxima_threshold_window,
maxima_threshold_window = min(args.maxima_threshold_window, args.plot_tstop-args.plot_tstart),
maxima_threshold_fraction = args.maxima_threshold_fraction,
min_peak_distance = args.min_peak_distance)
output_path = args.img_dir / args.img_name.replace('_channel0', f'_channel{channel}')
Expand Down

0 comments on commit 961e393

Please sign in to comment.