From fd2c6b9025db12f85b54c680f787d1bbc0b350a9 Mon Sep 17 00:00:00 2001 From: Eric Denovellis Date: Thu, 6 Jul 2023 16:42:18 -0700 Subject: [PATCH] Detect if ptp is enabled based on rec header --- .../processing/builder/nwb_file_builder.py | 21 ++++++++++++++++++- .../originators/position_originator.py | 21 +++++++------------ 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/rec_to_nwb/processing/builder/nwb_file_builder.py b/rec_to_nwb/processing/builder/nwb_file_builder.py index 451fdee6..5b462d56 100644 --- a/rec_to_nwb/processing/builder/nwb_file_builder.py +++ b/rec_to_nwb/processing/builder/nwb_file_builder.py @@ -357,10 +357,29 @@ def __init__( else: self.analog_originator = AnalogOriginator(self.datasets, self.metadata) + ptp_enabled = self._detect_ptp_from_header() + self.position_originator = PositionOriginator( - self.datasets, self.metadata, self.dataset_names + self.datasets, self.metadata, self.dataset_names, ptp_enabled ) + def _detect_ptp_from_header(self): + mconf = self.header.tree.find("ModuleConfiguration") + ptp_enabled = False + for smconf in mconf.findall("SingleModuleConfiguration"): + if ( + smconf.get("moduleName") == "cameraModule" + or smconf.get("moduleName") == "./cameraModule" + ): + for arg in smconf.findall("Argument"): + ptp_enabled = "-ptpEnabled" in arg.attrib.values() + if ptp_enabled: + break + if ptp_enabled: + break + logger.info("PTP enabled: " + str(ptp_enabled)) + return ptp_enabled + def __extract_datasets(self, animal_name, date): self.data_scanner.extract_data_from_date_folder(date) self.datasets = [ diff --git a/rec_to_nwb/processing/builder/originators/position_originator.py b/rec_to_nwb/processing/builder/originators/position_originator.py index ea2fcacd..5f3e4a08 100644 --- a/rec_to_nwb/processing/builder/originators/position_originator.py +++ b/rec_to_nwb/processing/builder/originators/position_originator.py @@ -26,13 +26,16 @@ class PositionOriginator: @beartype - def __init__(self, datasets: list, metadata: dict, dataset_names: list): + def __init__( + self, datasets: list, metadata: dict, dataset_names: list, ptp_enabled: bool + ): self.datasets = datasets self.dataset_names = dataset_names self.metadata = metadata self.pm_creator = ProcessingModule( "behavior", "Contains all behavior-related data" ) + self.ptp_enabled = ptp_enabled @beartype def make(self, nwb_content: NWBFile): @@ -51,7 +54,7 @@ def make(self, nwb_content: NWBFile): os.path.join(pos_path, "*.pos_online.dat") ) position_df = self.get_position_with_corrected_timestamps( - position_tracking_path[0] + position_tracking_path[0], self.ptp_enabled ) position.create_spatial_series( name=f"series_{dataset_ind}", @@ -67,7 +70,7 @@ def make(self, nwb_content: NWBFile): os.path.join(pos_path, "*.pos_cameraHWFrameCount.dat") ) video_df = self.get_corrected_timestamps_without_position( - video_file_path[0] + video_file_path[0], self.ptp_enabled ) position.create_spatial_series( name=f"series_{dataset_ind}", @@ -87,7 +90,7 @@ def make(self, nwb_content: NWBFile): nwb_content.processing["behavior"].add(position) @staticmethod - def get_position_with_corrected_timestamps(position_tracking_path): + def get_position_with_corrected_timestamps(position_tracking_path, ptp_enabled): logger.info(os.path.split(position_tracking_path)[-1]) # Get position tracking information @@ -123,10 +126,6 @@ def get_position_with_corrected_timestamps(position_tracking_path): # Get the timestamps from the neural data mcu_neural_timestamps = get_mcu_neural_timestamps(position_tracking_path) - # Determine whether the precision time protocol is being used - # which gives accurate timestamps associated with each video frame. - ptp_enabled = detect_ptp(mcu_neural_timestamps, video_info.HWTimestamp) - # Get the camera time from the DIOs dio_camera_ticks = find_camera_dio_channel(position_tracking_path, video_info) is_valid_tick = np.isin(dio_camera_ticks, mcu_neural_timestamps.index) @@ -226,7 +225,7 @@ def get_position_with_corrected_timestamps(position_tracking_path): ) @staticmethod - def get_corrected_timestamps_without_position(hw_frame_count_path): + def get_corrected_timestamps_without_position(hw_frame_count_path, ptp_enabled): video_info = readTrodesExtractedDataFile(hw_frame_count_path) video_info = pd.DataFrame(video_info["data"]).set_index("PosTimestamp") # On AVT cameras, HWFrame counts wraps to 0 above this value. @@ -242,10 +241,6 @@ def get_corrected_timestamps_without_position(hw_frame_count_path): # Get the timestamps from the neural data mcu_neural_timestamps = get_mcu_neural_timestamps(hw_frame_count_path) - # Determine whether the precision time protocol is being used - # which gives accurate timestamps associated with each video frame. - ptp_enabled = detect_ptp(mcu_neural_timestamps, video_info.HWTimestamp) - # Get the camera time from the DIOs try: dio_camera_ticks = find_camera_dio_channel(hw_frame_count_path, video_info)