From d6ab6820b3a57263ea5283a3953945c0a57018ef Mon Sep 17 00:00:00 2001 From: Josh Siegle Date: Thu, 29 Aug 2024 14:55:43 -0700 Subject: [PATCH] Update method for trimming unmatched sync events --- src/aind_ephys_rig_qc/generate_report.py | 14 +++----- src/aind_ephys_rig_qc/parameters.json | 2 +- src/aind_ephys_rig_qc/temporal_alignment.py | 38 +++++++++++++-------- 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/aind_ephys_rig_qc/generate_report.py b/src/aind_ephys_rig_qc/generate_report.py index 69ab037..77460c1 100644 --- a/src/aind_ephys_rig_qc/generate_report.py +++ b/src/aind_ephys_rig_qc/generate_report.py @@ -60,9 +60,6 @@ def generate_qc_report( """ - output_stream = io.StringIO() - sys.stdout = output_stream - pdf = PdfReport("aind-ephys-rig-qc v" + package_version) pdf.add_page() pdf.set_font("Helvetica", "B", size=12) @@ -323,12 +320,9 @@ def create_qc_plots( if __name__ == "__main__": - output_stream = io.StringIO() - sys.stdout = output_stream - output_stream = io.StringIO() - sys.stdout = output_stream - output_stream = io.StringIO() - sys.stdout = output_stream + + + if len(sys.argv) != 3: print("Two input arguments are required:") print(" 1. A data directory") @@ -348,6 +342,8 @@ def create_qc_plots( if not os.path.exists(directory): raise ValueError(f"Data directory {directory} does not exist.") + output_stream = io.StringIO() + sys.stdout = output_stream output_content = output_stream.getvalue() outfile = os.path.join(directory, "ephys-rig-QC_output.txt") diff --git a/src/aind_ephys_rig_qc/parameters.json b/src/aind_ephys_rig_qc/parameters.json index 581d017..56ea289 100644 --- a/src/aind_ephys_rig_qc/parameters.json +++ b/src/aind_ephys_rig_qc/parameters.json @@ -3,5 +3,5 @@ "timestamp_alignment_method" : "harp", "original_timestamp_filename" : "original_timestamps.npy", "plot_drift_map" : false, - "flip_NIDAQ" : false + "flip_NIDAQ" : true } \ No newline at end of file diff --git a/src/aind_ephys_rig_qc/temporal_alignment.py b/src/aind_ephys_rig_qc/temporal_alignment.py index 2803422..78c8f56 100644 --- a/src/aind_ephys_rig_qc/temporal_alignment.py +++ b/src/aind_ephys_rig_qc/temporal_alignment.py @@ -531,7 +531,13 @@ def align_timestamps( # noqa events_for_stream[condition].index ) - # remove inconstant events between main and curr stream + # remove inconsistent events between streams + + print( + f"Before removal: {len(events_for_stream)} " + + f"local times, {len(main_stream_times)} " + + "main times" + ) if len(main_stream_events) != len(events_for_stream): print( @@ -540,23 +546,26 @@ def align_timestamps( # noqa ) first_main_event_ts = ( main_stream_events.sample_number.values[0] - - main_stream_start_sample ) / main_stream_sample_rate + print(first_main_event_ts) first_curr_event_ts = ( events_for_stream.sample_number.values[0] - - sample_numbers[0] ) / sample_rate + print(first_curr_event_ts) offset = np.abs( first_main_event_ts - first_curr_event_ts ) + print(offset) + print( + "First event in main and current stream" + + " are not aligned. Off by " + + f"{offset:.2f} s" + ) + if offset > 0.1: # bigger than 0.1s so that # it should not be the same event - print( - "First event in main and current stream" - + " are not aligned. Off by " - + f"{offset:.2f} s" - ) + # remove first event from the stream # with the most events if len(main_stream_events) > len( @@ -566,6 +575,7 @@ def align_timestamps( # noqa "Removing first event in main stream" ) main_stream_events = main_stream_events[1:] + main_stream_times = main_stream_times[1:] else: print( "Removing first event in" @@ -573,11 +583,7 @@ def align_timestamps( # noqa ) events_for_stream = events_for_stream[1:] else: - print( - "First event in main and current stream" - " are aligned. Off by " - f"{offset:.2f} s" - ) + # remove last event from the stream # with the most events if len(main_stream_events) > len( @@ -587,6 +593,7 @@ def align_timestamps( # noqa main_stream_events = main_stream_events[ :-1 ] + main_stream_times = main_stream_times[:-1] else: print( "Removing last event in current stream" @@ -599,8 +606,9 @@ def align_timestamps( # noqa ) print( - f"Total events for {stream_name}: " - + f"{len(events_for_stream)}" + f"After removal: {len(events_for_stream)} " + + f"local times, {len(main_stream_times)} " + + "main times" ) if pdf is not None: