diff --git a/pipeline/stage03_trigger_detection/Snakefile b/pipeline/stage03_trigger_detection/Snakefile index 4b487399..59e7ae3d 100644 --- a/pipeline/stage03_trigger_detection/Snakefile +++ b/pipeline/stage03_trigger_detection/Snakefile @@ -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: diff --git a/pipeline/stage03_trigger_detection/configs/config_template.yaml b/pipeline/stage03_trigger_detection/configs/config_template.yaml index fe12b898..c108f08f 100644 --- a/pipeline/stage03_trigger_detection/configs/config_template.yaml +++ b/pipeline/stage03_trigger_detection/configs/config_template.yaml @@ -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 diff --git a/pipeline/stage03_trigger_detection/scripts/minima.py b/pipeline/stage03_trigger_detection/scripts/minima.py index 4f88781f..969f023a 100644 --- a/pipeline/stage03_trigger_detection/scripts/minima.py +++ b/pipeline/stage03_trigger_detection/scripts/minima.py @@ -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}') @@ -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, @@ -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) @@ -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}')