Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix video alignment for opto sessions #21

Merged
merged 3 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ classifiers = [
dependencies = [
"neuroconv==0.6.6",
"ndx-events==0.2.0",
"pymatreader==1.0.0"
"pymatreader==1.0.0",
"spikeinterface==0.101.2"
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ def get_metadata_schema(self) -> dict:
}
return metadata_schema


def add_to_nwbfile(
self, nwbfile: NWBFile, metadata: dict, normalize_timestamps: bool = False, verbose: bool = False
):
Expand All @@ -110,7 +109,7 @@ def add_to_nwbfile(
file_path = self.source_data["file_path"]
file = read_mat(file_path)
behavioral_time_series, name_to_times, name_to_values, name_to_trial_array = [], dict(), dict(), dict()
starting_timestamp = file["continuous"][metadata["Behavior"]["TimeSeries"][0]["name"]]["time"][0]
starting_timestamp = get_starting_timestamp(file)
for time_series_dict in metadata["Behavior"]["TimeSeries"]:
name = time_series_dict["name"]
timestamps = np.array(file["continuous"][name]["time"]).squeeze()
Expand Down Expand Up @@ -232,3 +231,15 @@ def add_to_nwbfile(
for device_kwargs in metadata["Behavior"]["Devices"]:
device = Device(**device_kwargs)
nwbfile.add_device(device)


def get_starting_timestamp(mat_file: dict):
starting_timestamp = np.min(
[
mat_file["continuous"]["encoder"]["time"][0],
mat_file["continuous"]["lick"]["time"][0],
mat_file["continuous"]["cam"]["time"][0][0],
mat_file["continuous"]["cam"]["time"][1][0],
]
)
return starting_timestamp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
Zempolich2024OptogeneticInterface,
Zempolich2024IntrinsicSignalOpticalImagingInterface,
)
from schneider_lab_to_nwb.zempolich_2024.zempolich_2024_behaviorinterface import get_starting_timestamp


class Zempolich2024NWBConverter(NWBConverter):
Expand All @@ -28,7 +29,7 @@ class Zempolich2024NWBConverter(NWBConverter):
ISOI=Zempolich2024IntrinsicSignalOpticalImagingInterface,
)

def temporally_align_data_interfaces(self):
def temporally_align_data_interfaces(self) -> None:
"""Align timestamps between data interfaces.

It is called by run_conversion() after the data interfaces have been initialized but before the data is added
Expand All @@ -39,5 +40,16 @@ def temporally_align_data_interfaces(self):
behavior_file_path = Path(behavior_interface.source_data["file_path"])
file = read_mat(behavior_file_path)
cam1_timestamps, cam2_timestamps = file["continuous"]["cam"]["time"]
if self.conversion_options["Behavior"].get("normalize_timestamps", False):
starting_timestamp = get_starting_timestamp(mat_file=file)
cam1_timestamps -= starting_timestamp
cam2_timestamps -= starting_timestamp
self.data_interface_objects["VideoCamera1"].set_aligned_timestamps([cam1_timestamps])
self.data_interface_objects["VideoCamera2"].set_aligned_timestamps([cam2_timestamps])

# NOTE: passing in conversion_options as an attribute is a temporary solution until the neuroconv library is updated
# to allow for easier customization of the conversion process
# (see https://github.com/catalystneuro/neuroconv/pull/1162).
def run_conversion(self, **kwargs):
self.conversion_options = kwargs["conversion_options"]
super().run_conversion(**kwargs)
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

from neuroconv.basedatainterface import BaseDataInterface

from .zempolich_2024_behaviorinterface import get_starting_timestamp


class Zempolich2024OptogeneticInterface(BaseDataInterface):
"""Optogenetic interface for schneider_2024 conversion"""
Expand Down Expand Up @@ -57,7 +59,7 @@ def add_to_nwbfile(
np.logical_not(np.isnan(offset_times))
), "Some of the offset times are nan when onset times are not nan."
power = metadata["Optogenetics"]["OptogeneticSeries"]["power"]
starting_timestamp = file["continuous"][metadata["Behavior"]["TimeSeries"][0]["name"]]["time"][0]
starting_timestamp = get_starting_timestamp(mat_file=file)
if normalize_timestamps:
onset_times = onset_times - starting_timestamp
offset_times = offset_times - starting_timestamp
Expand Down