Skip to content

Commit

Permalink
add interface for multiple edf
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandratrapani committed Nov 14, 2024
1 parent f56edac commit 3337efa
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/cai_lab_to_nwb/zaki_2024/interfaces/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .eztrack_interface import EzTrackFreezingBehaviorInterface
from .zaki_2024_edf_interface import Zaki2024EDFInterface
from .zaki_2024_edf_interface import Zaki2024EDFInterface, Zaki2024MultiEDFInterface
from .minian_interface import MinianSegmentationInterface, MinianMotionCorrectionInterface
from .zaki_2024_sleep_classification_interface import Zaki2024SleepClassificationInterface
from .miniscope_imaging_interface import MiniscopeImagingInterface
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Union
from pathlib import Path
from neuroconv.basedatainterface import BaseDataInterface
from pynwb import NWBFile, TimeSeries
Expand All @@ -10,7 +11,7 @@
class Zaki2024EDFInterface(BaseDataInterface):
def __init__(
self,
file_path: Path,
file_path: Union[Path, str],
verbose: bool = False,
):
self.file_path = Path(file_path)
Expand Down Expand Up @@ -117,3 +118,93 @@ def add_to_nwbfile(
nwbfile.add_device(device)

return nwbfile


class Zaki2024MultiEDFInterface(BaseDataInterface):
def __init__(
self,
file_paths: list[Path],
verbose: bool = False,
):
self.file_paths = file_paths
self.verbose = verbose
super().__init__(file_paths=file_paths)

def add_to_nwbfile(
self,
nwbfile: NWBFile,
stub_test: bool = False,
stub_frames: int = 100,
**conversion_options,
) -> NWBFile:
"""
Adds data from EEG, EMG, temperature, and activity channels to an NWBFile.
Parameters
----------
nwbfile : NWBFile
The NWBFile object to which data will be added.
stub_test : bool, optional
If True, loads only a subset of frames (controlled by `stub_frames` parameter)
to facilitate testing and faster execution. Default is False.
stub_frames : int, optional
The number of frames to load if `stub_test` is True. Default is 100.
Returns
-------
NWBFile
The NWBFile object with added data and metadata from the specified channels.
"""
channels_dict = {
"Temp": {
"name": "TemperatureSignal",
"description": "Temperature signal recorder with HD-X02 wireless telemetry probe",
"unit": "celsius",
},
"EEG": {
"name": "EEGSignal",
"description": "EEG signal recorder with HD-X02 wireless telemetry probe",
"unit": "volts",
},
"EMG": {
"name": "EMGSignal",
"description": "EMG signal recorder with HD-X02 wireless telemetry probe",
"unit": "volts",
},
# TODO: Figure out if the units of activity are correct, the raw format marks Volts
# TODO: My own reading of the header indicates that the physical units is counts
"Activity": {
"name": "ActivitySignal",
"description": "Activity signal recorder with HD-X02 wireless telemetry probe. It refers to the motion of the probe relative to the receiver and it can be used as a proxy for locomotion.",
"unit": "n.a.",
},
}
concatenated_data = []
concatenated_times = []

# Loop over each EDF file and concatenate data and timestamps
for file_path in self.file_paths:
edf_reader = read_raw_edf(input_fname=file_path, verbose=self.verbose)
data, times = edf_reader.get_data(picks=list(channels_dict.keys()), return_times=True)
# Slice the data and timestamps within the time range
if stub_test:
concatenated_data = data[:, :stub_frames]
break
concatenated_data.append(data.astype("float32"))
concatenated_times.extend(times)

for channel_index, channel_name in enumerate(channels_dict.keys()):
time_series_kwargs = channels_dict[channel_name].copy()
time_series_kwargs.update(
data=concatenated_data[channel_index], starting_time=0.0, rate=edf_reader.info["sfreq"]
)
time_series = TimeSeries(**time_series_kwargs)
nwbfile.add_acquisition(time_series)

# Add device
description = "Wireless telemetry probe used to record EEG, EMG, temperature, and activity data"
name = "HD-X02, Data Science International"
device = Device(name=name, description=description)
nwbfile.add_device(device)

return nwbfile
2 changes: 2 additions & 0 deletions src/cai_lab_to_nwb/zaki_2024/zaki_2024_nwbconverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from interfaces import (
MinianSegmentationInterface,
Zaki2024EDFInterface,
Zaki2024MultiEDFInterface,
EzTrackFreezingBehaviorInterface,
Zaki2024SleepClassificationInterface,
MiniscopeImagingInterface,
Expand All @@ -26,6 +27,7 @@ class Zaki2024NWBConverter(NWBConverter):
MinianMotionCorrection=MinianMotionCorrectionInterface,
SleepClassification=Zaki2024SleepClassificationInterface,
EDFSignals=Zaki2024EDFInterface,
MultiEDFSignals=Zaki2024MultiEDFInterface,
FreezingBehavior=EzTrackFreezingBehaviorInterface,
Video=VideoInterface,
)
Expand Down

0 comments on commit 3337efa

Please sign in to comment.