Skip to content

Commit

Permalink
Detect if ptp is enabled based on rec header
Browse files Browse the repository at this point in the history
  • Loading branch information
edeno committed Jul 6, 2023
1 parent 626c9d6 commit fd2c6b9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
21 changes: 20 additions & 1 deletion rec_to_nwb/processing/builder/nwb_file_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
21 changes: 8 additions & 13 deletions rec_to_nwb/processing/builder/originators/position_originator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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}",
Expand All @@ -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}",
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand Down

0 comments on commit fd2c6b9

Please sign in to comment.