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

Add subject metadata from excel sheet #15

Merged
merged 6 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 24 additions & 4 deletions src/howe_lab_to_nwb/vu2024/vu2024_convert_all_sessions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
from datetime import datetime
from pathlib import Path

import pandas as pd
Expand Down Expand Up @@ -40,7 +41,10 @@ def convert_all_sessions(

"""

data_table = pd.read_excel(data_table_path)
data_table_dict = pd.read_excel(data_table_path, sheet_name=["Sessions", "Mice"])
data_table = data_table_dict["Sessions"]
subjects_table = data_table_dict["Mice"].astype(str)

folder_path = Path(folder_path)

columns_to_group = ["Mouse", "Date (YYYYMMDD)"]
Expand All @@ -55,8 +59,22 @@ def convert_all_sessions(

for (subject_id, date), table in progress_bar:
experiment_type = "single-wavelength" if len(table) == 1 else "dual-wavelength"
subject_id = str(subject_id).replace("-", "")

mouse_metadata = subjects_table.loc[subjects_table["MouseID"] == str(subject_id)]
mouse_metadata = mouse_metadata.to_dict(orient="records")[0]
date_of_birth = datetime.strptime(mouse_metadata["Date of Birth"], "%Y-%m-%d")
subject_metadata = dict(
Subject=dict(
subject_id=mouse_metadata["MouseID"],
date_of_birth=date_of_birth,
sex=mouse_metadata["Sex"],
genotype=mouse_metadata["Genotype"],
strain=mouse_metadata["Strain"],
species="Mus musculus",
),
)

subject_id = str(subject_id).replace("-", "")
subject_folder_path = folder_path / subject_id

behavior_file_name = table["Raw behavior file"].values[0]
Expand Down Expand Up @@ -143,6 +161,7 @@ def convert_all_sessions(
ttl_stream_names=ttl_stream_names,
behavior_file_paths=behavior_file_paths,
nwbfile_path=nwbfile_path,
subject_metadata=subject_metadata,
stub_test=stub_test,
)
elif experiment_type == "single-wavelength":
Expand All @@ -157,6 +176,7 @@ def convert_all_sessions(
ttl_stream_name=ttl_stream_names[0],
behavior_file_path=behavior_file_paths[0],
nwbfile_path=nwbfile_path,
subject_metadata=subject_metadata,
stub_test=stub_test,
)
progress_bar.update(1)
Expand Down Expand Up @@ -186,12 +206,12 @@ def convert_all_sessions(
nwbfile_folder_path = Path("/Volumes/t7-ssd/Howe/nwbfiles")

# Whether to overwrite existing NWB files, default is False
overwrite = False
overwrite = True
weiglszonja marked this conversation as resolved.
Show resolved Hide resolved

# Whether to run the conversion as a stub test
# When set to True, write only a subset of the data for each session
# When set to False, write the entire data for each session
stub_test = False
stub_test = True
weiglszonja marked this conversation as resolved.
Show resolved Hide resolved

convert_all_sessions(
data_table_path=data_table_excel_file_path,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def dual_wavelength_session_to_nwb(
nwbfile_path: Union[str, Path],
behavior_file_paths: List[Union[str, Path]] = None,
motion_corrected_imaging_file_paths: List[Union[str, Path]] = None,
subject_metadata: dict = None,
stub_test: bool = False,
):
"""
Expand Down Expand Up @@ -46,6 +47,8 @@ def dual_wavelength_session_to_nwb(
The list of paths to the .mat files containing the processed behavior data.
motion_corrected_imaging_file_paths : List[Union[str, Path]], optional
The list of paths to the .tif files containing the motion corrected imaging data.
subject_metadata : dict, optional
The metadata for the subject.
stub_test : bool, optional
Whether to run the conversion as a stub test.
"""
Expand Down Expand Up @@ -77,6 +80,7 @@ def dual_wavelength_session_to_nwb(
ttl_stream_name=ttl_stream_names[0],
motion_corrected_imaging_file_path=motion_corrected_imaging_file_paths[0],
behavior_file_path=behavior_file_paths[0],
subject_metadata=subject_metadata,
stub_test=stub_test,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def single_wavelength_session_to_nwb(
nwbfile_path: Optional[Union[str, Path]] = None,
nwbfile: Optional[NWBFile] = None,
sampling_frequency: float = None,
subject_metadata: Optional[dict] = None,
stub_test: bool = False,
) -> NWBFile:
"""
Expand Down Expand Up @@ -64,6 +65,8 @@ def single_wavelength_session_to_nwb(
sampling_frequency : float, optional
The sampling frequency of the data. If None, the sampling frequency will be read from the .cxd file.
If missing from the file, the sampling frequency must be provided.
subject_metadata : dict, optional
The metadata for the subject.
stub_test : bool, optional
Whether to run a stub test, by default False.
"""
Expand Down Expand Up @@ -157,6 +160,11 @@ def single_wavelength_session_to_nwb(
editable_metadata = load_dict_from_file(editable_metadata_path)
metadata = dict_deep_update(metadata, editable_metadata)

if subject_metadata is not None:
metadata = dict_deep_update(metadata, subject_metadata)
date_of_birth = metadata["Subject"]["date_of_birth"]
metadata["Subject"].update(date_of_birth=date_of_birth.replace(tzinfo=tzinfo))

ophys_metadata = load_dict_from_file(Path(__file__).parent / "metadata" / "vu2024_ophys_metadata.yaml")
metadata = dict_deep_update(metadata, ophys_metadata)

Expand Down
Loading