Skip to content

Commit

Permalink
use converter.create_nwbfile
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandratrapani committed Nov 28, 2024
1 parent 9eddfdd commit fa226fe
Showing 1 changed file with 13 additions and 24 deletions.
37 changes: 13 additions & 24 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,17 +1,15 @@
"""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 re
import pandas as pd
from datetime import datetime
from mne.io import read_raw_edf

from pynwb import NWBFile
from neuroconv.utils import load_dict_from_file
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, get_session_run_time
Expand Down Expand Up @@ -72,24 +70,22 @@ 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)))

from mne.io import read_raw_edf
converter = Zaki2024NWBConverter(source_data=source_data)
# Add datetime to conversion
metadata = converter.get_metadata()
# 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)

metadata["Subject"]["subject_id"] = subject_id

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

editable_metadata_path = Path(__file__).parent / "zaki_2024_metadata.yaml"
editable_metadata = load_dict_from_file(editable_metadata_path)
nwbfile_kwargs = deepcopy(editable_metadata["NWBFile"])
metadata["NWBFile"]["session_start_time"] = session_start_time

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)
nwbfile = converter.create_nwbfile(metadata=metadata, conversion_options=conversion_options)

# 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"
Expand Down Expand Up @@ -140,14 +136,7 @@ def session_to_nwb(

nwbfile.add_epoch(start_time=start_time, stop_time=stop_time, 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.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:
Expand Down

0 comments on commit fa226fe

Please sign in to comment.