Skip to content

Commit

Permalink
modify conversions to use subject extension
Browse files Browse the repository at this point in the history
  • Loading branch information
weiglszonja committed Dec 1, 2023
1 parent 4de98ec commit c2fb087
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/pinto_lab_to_nwb/general/subject.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,19 @@ def make_subject_metadata(subject_id: str, subject_metadata_file_path: str) -> d
date_of_birth = date_of_birth.replace(tzinfo=tzinfo)

sex_mapping = dict(Male="M", Female="F")
# TODO: ear_tag_id, and zygosity are in the metadata file, but would require an extension of Subject to add, should it be added?
subject_kwargs = dict(
date_of_birth=date_of_birth,
subject_id=subject_id,
sex=sex_mapping[subject_metadata["sex"][subject_ind]],
genotype=subject_metadata["line"][subject_ind],
zygosity=subject_metadata["zygosity"][subject_ind],
)
subject_description = subject_metadata["subject_description"][subject_ind]
if subject_description:
subject_kwargs["description"] = subject_description
ear_tag = subject_metadata["ear_tag_id"][subject_ind]
if ear_tag:
subject_kwargs["ear_tag_id"] = ear_tag

metadata = dict(Subject=subject_kwargs, NWBFile=dict(protocol=subject_metadata["protocol"][subject_ind]))
return metadata
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Primary script to run to convert an entire session for of data using the NWBConverter."""
import re
from pathlib import Path
from typing import Optional

from dateutil import tz
from neuroconv.utils import (
load_dict_from_file,
Expand All @@ -9,6 +11,7 @@
FilePathType,
)

from pinto_lab_to_nwb.general import make_subject_metadata
from pinto_lab_to_nwb.into_the_void import IntoTheVoidNWBConverter
from pinto_lab_to_nwb.into_the_void.into_the_voidnwbconverter import get_default_segmentation_to_imaging_name_mapping

Expand All @@ -17,6 +20,7 @@ def session_to_nwb(
nwbfile_path: FilePathType,
two_photon_imaging_folder_path: FolderPathType,
segmentation_folder_path: FolderPathType,
subject_metadata_file_path: Optional[FilePathType] = None,
segmentation_to_imaging_plane_map: dict = None,
stub_test: bool = False,
):
Expand All @@ -31,6 +35,8 @@ def session_to_nwb(
The folder path that contains the Bruker TIF imaging output (.ome.tif files).
segmentation_folder_path: FolderPathType
The folder that contains the Suite2P segmentation output.
subject_metadata_file_path: FilePathType, optional
The file path to the subject metadata file ('subject_metadata.mat').
segmentation_to_imaging_plane_map: dict, optional
The optional mapping between the imaging and segmentation planes.
stub_test: bool, optional
Expand All @@ -42,7 +48,7 @@ def session_to_nwb(
imaging_folder_path=imaging_folder_path,
segmentation_folder_path=segmentation_folder_path,
segmentation_to_imaging_map=segmentation_to_imaging_plane_map,
verbose=False,
verbose=True,
)

conversion_options = {
Expand Down Expand Up @@ -70,6 +76,15 @@ def session_to_nwb(
metadata["NWBFile"].update(session_id=groups_dict["session_id"].replace("_", "-"))
metadata["Subject"].update(subject_id=groups_dict["subject_id"])

if subject_metadata_file_path:
subject_metadata = make_subject_metadata(
subject_id=groups_dict["subject_id"], subject_metadata_file_path=subject_metadata_file_path
)
metadata = dict_deep_update(metadata, subject_metadata)

# Separate subject metadata from NWBFile metadata to add SubjectExtension
metadata["SubjectExtension"] = metadata.pop("Subject", None)

# Run conversion
converter.run_conversion(
nwbfile_path=nwbfile_path, metadata=metadata, overwrite=True, conversion_options=conversion_options
Expand All @@ -83,6 +98,8 @@ def session_to_nwb(
imaging_folder_path = Path("/Volumes/t7-ssd/Pinto/NCCR32_2022_11_03_IntoTheVoid_t_series-005")
# The folder that contains the Suite2P segmentation output.
segmentation_folder_path = imaging_folder_path / "suite2p"
# The file path to the subject metadata file.
subject_metadata_file_path = "/Volumes/t7-ssd/Pinto/Behavior/subject_metadata.mat"
# The folder path that will contain the NWB files.
nwbfile_folder_path = Path("/Volumes/t7-ssd/Pinto/nwbfiles")
# For testing purposes, when stub_test=True only writes a subset of imaging and segmentation data.
Expand All @@ -99,6 +116,7 @@ def session_to_nwb(
session_to_nwb(
nwbfile_path=nwbfile_path,
two_photon_imaging_folder_path=imaging_folder_path,
subject_metadata_file_path=subject_metadata_file_path,
segmentation_folder_path=segmentation_folder_path,
segmentation_to_imaging_plane_map=plane_map,
stub_test=stub_test,
Expand Down
11 changes: 10 additions & 1 deletion src/pinto_lab_to_nwb/into_the_void/into_the_voidnwbconverter.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""Primary NWBConverter class for this dataset."""
from typing import Optional

from ndx_pinto_metadata import SubjectExtension
from neuroconv import NWBConverter
from neuroconv.datainterfaces import Suite2pSegmentationInterface, BrukerTiffMultiPlaneImagingInterface
from neuroconv.converters import BrukerTiffSinglePlaneConverter, BrukerTiffMultiPlaneConverter
from neuroconv.utils import FolderPathType, DeepDict, dict_deep_update
from neuroconv.utils import FolderPathType, DeepDict
from pynwb import NWBFile


def get_default_segmentation_to_imaging_name_mapping(
Expand Down Expand Up @@ -150,3 +152,10 @@ def get_metadata(self) -> DeepDict:
metadata["Ophys"]["ImagingPlane"][metadata_ind]["device"] = device_name

return metadata

def add_to_nwbfile(self, nwbfile: NWBFile, metadata, conversion_options: Optional[dict] = None) -> None:
super().add_to_nwbfile(nwbfile=nwbfile, metadata=metadata, conversion_options=conversion_options)

# Add subject (from extension)
if metadata["SubjectExtension"] is not None:
nwbfile.subject = SubjectExtension(**metadata["SubjectExtension"])
2 changes: 2 additions & 0 deletions src/pinto_lab_to_nwb/widefield/widefield_convert_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ def session_to_nwb(
)
metadata = dict_deep_update(metadata, subject_metadata)

# Separate subject metadata from NWBFile metadata to add SubjectExtension
metadata["SubjectExtension"] = metadata.pop("Subject", None)
# Run conversion
converter.run_conversion(
nwbfile_path=nwbfile_path, metadata=metadata, overwrite=True, conversion_options=conversion_options
Expand Down
6 changes: 5 additions & 1 deletion src/pinto_lab_to_nwb/widefield/widefieldnwbconverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

import numpy as np
from natsort import natsorted
from ndx_pinto_metadata import SubjectExtension
from neuroconv import NWBConverter
from pynwb import NWBFile

from pinto_lab_to_nwb.widefield.interfaces import WidefieldImagingInterface, WidefieldProcessedImagingInterface
from pinto_lab_to_nwb.widefield.utils import load_motion_correction_data
from pinto_lab_to_nwb.widefield.utils.motion_correction import add_motion_correction
from pinto_lab_to_nwb.widefield.interfaces import (
Expand Down Expand Up @@ -46,6 +46,10 @@ def __init__(self, source_data: Dict[str, dict], verbose: bool = True):
def add_to_nwbfile(self, nwbfile: NWBFile, metadata, conversion_options: Optional[dict] = None) -> None:
super().add_to_nwbfile(nwbfile=nwbfile, metadata=metadata, conversion_options=conversion_options)

# Add subject (from extension)
if metadata["SubjectExtension"] is not None:
nwbfile.subject = SubjectExtension(**metadata["SubjectExtension"])

# Add motion correction for blue and violet frames
imaging_interface_names = ["ImagingBlue", "ImagingViolet"]
for interface_name in imaging_interface_names:
Expand Down

0 comments on commit c2fb087

Please sign in to comment.