Skip to content

Commit

Permalink
add epoch for sub-session times
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandratrapani committed Nov 20, 2024
1 parent 9ff7763 commit 2f60d56
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 15 deletions.
74 changes: 62 additions & 12 deletions src/cai_lab_to_nwb/zaki_2024/zaki_2024_convert_week_session.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
"""Primary script to run to convert an entire session for of data using the NWBConverter."""

import time
import uuid
from copy import deepcopy
from natsort import natsorted
from pathlib import Path
from typing import Union
import os
import pandas as pd

from pynwb import NWBFile
from pynwb.epoch import TimeIntervals
from neuroconv.utils import load_dict_from_file, dict_deep_update
from neuroconv.tools.nwb_helpers import configure_and_write_nwbfile

from utils import get_session_slicing_time_range
from interfaces.miniscope_imaging_interface import get_miniscope_folder_path
from zaki_2024_nwbconverter import Zaki2024NWBConverter


Expand Down Expand Up @@ -47,7 +56,7 @@ def session_to_nwb(
else:
print(f"No .edf file found in {edf_folder_path}")

# Add Cross session cell registration
# Add Cross session cell registration
main_folder = data_dir_path / f"/Ca_EEG_Calcium/{subject_id}/SpatialFootprints"
file_paths = []
for folder in os.listdir(main_folder):
Expand All @@ -60,28 +69,69 @@ def session_to_nwb(
source_data.update(dict(CellRegistration=dict(file_paths=file_paths)))
conversion_options.update(dict(CellRegistration=dict(stub_test=stub_test, subject_id=subject_id)))

converter = Zaki2024NWBConverter(source_data=source_data)

# Add datetime to conversion
metadata = converter.get_metadata()

from mne.io import read_raw_edf

edf_reader = read_raw_edf(input_fname=edf_file_paths[0], verbose=verbose)
session_start_time = edf_reader.info["meas_date"]
metadata["NWBFile"]["session_start_time"] = session_start_time

# Update default metadata with the editable in the corresponding yaml file
editable_metadata_path = Path(__file__).parent / "zaki_2024_metadata.yaml"
editable_metadata = load_dict_from_file(editable_metadata_path)
metadata = dict_deep_update(metadata, editable_metadata)
nwbfile_kwargs = deepcopy(editable_metadata["NWBFile"])

nwbfile_kwargs.update(
dict(
session_id=f"{subject_id}_week_session",
identifier=str(uuid.uuid4()),
session_start_time=session_start_time,
)
)

nwbfile = NWBFile(**nwbfile_kwargs)

# Add epochs table to store time range of conditioning and offline sessions
sessions_summary_file = data_dir_path / f"Ca_EEG_Experiment/{subject_id}/{subject_id}_SessionTimes.csv"
sessions_summary_df = pd.read_csv(sessions_summary_file)

# Add columns to TimeIntervals
nwbfile.add_epoch_column(name="session_ids", description="ID of the session")

for task, date_str, time_str in zip(
sessions_summary_df["Session"], sessions_summary_df["Date"], sessions_summary_df["Time"]
):
session_id = subject_id + "_" + task
if "Offline" in session_id:
offline_day = session_id.split("Session")[0]
experiment_dir_path = (
data_dir_path / "Ca_EEG_Experiment" / subject_id / (subject_id + "_Offline") / offline_day
)
else:
experiment_dir_path = (
data_dir_path / "Ca_EEG_Experiment" / subject_id / (subject_id + "_Sessions") / session_id
)
folder_path = experiment_dir_path / date_str / time_str
miniscope_folder_path = get_miniscope_folder_path(folder_path)
miniscope_metadata_json = folder_path / "metaData.json"
assert miniscope_metadata_json.exists(), f"General metadata json not found in {folder_path}"
timestamps_file_path = miniscope_folder_path / "timeStamps.csv"
assert timestamps_file_path.exists(), f"Miniscope timestamps file not found in {miniscope_folder_path}"
start_datetime_timestamp, stop_datetime_timestamp = get_session_slicing_time_range(
miniscope_metadata_json=miniscope_metadata_json, timestamps_file_path=timestamps_file_path
)
start_time = start_datetime_timestamp - session_start_time.replace(tzinfo=None)
stop_time = stop_datetime_timestamp - session_start_time.replace(tzinfo=None)
nwbfile.add_epoch(
start_time=start_time.total_seconds(), stop_time=stop_time.total_seconds(), session_ids=session_id
)

converter = Zaki2024NWBConverter(source_data=source_data)

# Add datetime to conversion
metadata = converter.get_metadata()
metadata["Subject"]["subject_id"] = subject_id

# Run conversion
converter.run_conversion(
metadata=metadata, nwbfile_path=nwbfile_path, conversion_options=conversion_options, overwrite=True
)
converter.add_to_nwbfile(metadata=metadata, nwbfile=nwbfile, conversion_options=conversion_options)
configure_and_write_nwbfile(nwbfile=nwbfile, backend="hdf5", output_filepath=nwbfile_path)

if verbose:
stop_time = time.time()
Expand Down
3 changes: 0 additions & 3 deletions src/cai_lab_to_nwb/zaki_2024/zaki_2024_nwbconverter.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
"""Primary NWBConverter class for this dataset."""

from neuroconv import NWBConverter
from pynwb import NWBFile
from neuroconv.datainterfaces import VideoInterface
from typing import Optional
from pathlib import Path

from interfaces import (
MinianSegmentationInterface,
Expand Down

0 comments on commit 2f60d56

Please sign in to comment.