Skip to content

Commit 141949d

Browse files
committed
Add option to save ephys data in raw units
1 parent 73e14e6 commit 141949d

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/trodes_to_nwb/convert_ephys.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def __init__(
4747
is_analog: bool = False,
4848
interpolate_dropped_packets: bool = False,
4949
timestamps=None, # Use this if you already have timestamps from intializing another rec iterator on the same files
50+
save_as_microvolts=True,
5051
**kwargs,
5152
):
5253
"""
@@ -69,13 +70,16 @@ def __init__(
6970
whether to interpolate single dropped packets, by default False
7071
timestamps : [type], optional
7172
timestamps to use. Can provide efficiency improvements by skipping recalculating timestamps from rec files, by default None
73+
save_as_microvolts : bool, optional
74+
convert the ephys data to microvolts prior to saving if True; keep it in raw ADC units if False
7275
kwargs : dict
7376
additional arguments to pass to GenericDataChunkIterator
7477
"""
7578
if not rec_file_path:
7679
raise FileNotFoundError("Must provide at least one rec file path")
7780
logger = logging.getLogger("convert")
7881
self.conversion = conversion
82+
self.save_as_microvolts = save_as_microvolts
7983
self.is_analog = is_analog
8084
self.neo_io = [
8185
SpikeGadgetsRawIO(
@@ -274,7 +278,11 @@ def _get_data(self, selection: Tuple[slice]) -> np.ndarray:
274278
time_index[-1] - i, # if finished in this stream
275279
)
276280

277-
data = (np.concatenate(data) * self.conversion).astype("int16")
281+
if self.save_as_microvolts:
282+
data = (np.concatenate(data) * self.conversion).astype("int16")
283+
else:
284+
data = np.concatenate(data).astype("int16")
285+
278286
# Handle the appended multiplex data
279287
if (
280288
self.neo_io[0].header["signal_streams"][self.stream_index]["id"]
@@ -353,13 +361,20 @@ def add_raw_ephys(
353361
metadata["raw_data_to_volts"] * MICROVOLTS_PER_VOLT
354362
) # Use metadata-provided conversion if not available in rec file
355363

364+
# read metadata
365+
if "save_as_microvolts" in metadata:
366+
save_as_microvolts = metadata["save_as_microvolts"]
367+
else:
368+
save_as_microvolts = True
369+
356370
# make the data iterator
357371
rec_dci = RecFileDataChunkIterator(
358372
recfile,
359373
nwb_hw_channel_order=nwb_hw_chan_order,
360374
conversion=conversion,
361375
interpolate_dropped_packets=True,
362376
stream_id="trodes",
377+
save_as_microvolts=save_as_microvolts,
363378
) # can set buffer_gb if needed
364379

365380
# (16384, 32) chunks of dtype int16 (2 bytes) is 1 MB, which is recommended
@@ -375,12 +390,17 @@ def add_raw_ephys(
375390
)
376391

377392
# do we want to pull the timestamps from the rec file? or is there another source?
393+
if save_as_microvolts:
394+
conversion_to_use = VOLTS_PER_MICROVOLT
395+
else:
396+
conversion_to_use = conversion * VOLTS_PER_MICROVOLT
397+
378398
eseries = ElectricalSeries(
379399
name="e-series",
380400
data=data_data_io,
381401
timestamps=rec_dci.timestamps,
382402
electrodes=electrode_table_region, # TODO
383-
conversion=VOLTS_PER_MICROVOLT,
403+
conversion=conversion_to_use,
384404
offset=0.0, # TODO
385405
)
386406

0 commit comments

Comments
 (0)