Skip to content
Open
Changes from all commits
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
29 changes: 26 additions & 3 deletions src/trodes_to_nwb/convert_ephys.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def __init__(
is_analog: bool = False,
interpolate_dropped_packets: bool = False,
timestamps=None, # Use this if you already have timestamps from intializing another rec iterator on the same files
save_as_microvolts=True,
behavior_only: bool = False,
**kwargs,
):
Expand All @@ -70,6 +71,8 @@ def __init__(
whether to interpolate single dropped packets, by default False
timestamps : [type], optional
timestamps to use. Can provide efficiency improvements by skipping recalculating timestamps from rec files, by default None
save_as_microvolts : bool, optional
convert the ephys data to microvolts prior to saving if True; keep it in raw ADC units if False
behavior_only : bool, optional
indicate if file contains only behavior data (no e-phys), by default False
kwargs : dict
Expand All @@ -79,6 +82,7 @@ def __init__(
raise FileNotFoundError("Must provide at least one rec file path")
logger = logging.getLogger("convert")
self.conversion = conversion
self.save_as_microvolts = save_as_microvolts
self.is_analog = is_analog
self.neo_io = [
SpikeGadgetsRawIO(
Expand Down Expand Up @@ -289,7 +293,11 @@ def _get_data(self, selection: Tuple[slice]) -> np.ndarray:
time_index[-1] - i, # if finished in this stream
)

data = (np.concatenate(data) * self.conversion).astype("int16")
if self.save_as_microvolts:
data = (np.concatenate(data) * self.conversion).astype("int16")
else:
data = np.concatenate(data).astype("int16")

# Handle the appended multiplex data
if (
self.neo_io[0].header["signal_streams"][self.stream_index]["id"]
Expand Down Expand Up @@ -347,7 +355,7 @@ def add_raw_ephys(
electrode_row_indices : list
which electrodes to add to table
metadata : dict, optional
metadata dictionary, useed only for conversion if not in rec, by default None
metadata dictionary, used only for conversion if not in rec, by default None
"""

electrode_table_region = nwbfile.create_electrode_table_region(
Expand All @@ -368,13 +376,23 @@ def add_raw_ephys(
metadata["raw_data_to_volts"] * MICROVOLTS_PER_VOLT
) # Use metadata-provided conversion if not available in rec file

# read metadata
if metadata is None:
save_as_microvolts = True
else:
if "save_as_microvolts" in metadata:
save_as_microvolts = metadata["save_as_microvolts"]
else:
save_as_microvolts = True

# make the data iterator
rec_dci = RecFileDataChunkIterator(
recfile,
nwb_hw_channel_order=nwb_hw_chan_order,
conversion=conversion,
interpolate_dropped_packets=True,
stream_id="trodes",
save_as_microvolts=save_as_microvolts,
) # can set buffer_gb if needed

# (16384, 32) chunks of dtype int16 (2 bytes) is 1 MB, which is recommended
Expand All @@ -390,12 +408,17 @@ def add_raw_ephys(
)

# do we want to pull the timestamps from the rec file? or is there another source?
if save_as_microvolts:
conversion_to_use = VOLTS_PER_MICROVOLT
else:
conversion_to_use = conversion * VOLTS_PER_MICROVOLT

eseries = ElectricalSeries(
name="e-series",
data=data_data_io,
timestamps=rec_dci.timestamps,
electrodes=electrode_table_region, # TODO
conversion=VOLTS_PER_MICROVOLT,
conversion=conversion_to_use,
offset=0.0, # TODO
)

Expand Down