From 141949d8dac21ee5339c365ac6c70ada2a01a8da Mon Sep 17 00:00:00 2001 From: Kyu Hyun Lee Date: Fri, 20 Jun 2025 10:11:29 -0700 Subject: [PATCH 1/2] Add option to save ephys data in raw units --- src/trodes_to_nwb/convert_ephys.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/trodes_to_nwb/convert_ephys.py b/src/trodes_to_nwb/convert_ephys.py index 6aff650..8732d50 100644 --- a/src/trodes_to_nwb/convert_ephys.py +++ b/src/trodes_to_nwb/convert_ephys.py @@ -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, **kwargs, ): """ @@ -69,6 +70,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 kwargs : dict additional arguments to pass to GenericDataChunkIterator """ @@ -76,6 +79,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( @@ -274,7 +278,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"] @@ -353,6 +361,12 @@ 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 "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, @@ -360,6 +374,7 @@ def add_raw_ephys( 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 @@ -375,12 +390,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 ) From 4947df7d48a84a89fbe67c614bf061fa7d9f3c3d Mon Sep 17 00:00:00 2001 From: Kyu Hyun Lee Date: Thu, 4 Sep 2025 20:52:06 -0700 Subject: [PATCH 2/2] Check if metatdata is None --- src/trodes_to_nwb/convert_ephys.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/trodes_to_nwb/convert_ephys.py b/src/trodes_to_nwb/convert_ephys.py index 8a23ee2..b5141f1 100644 --- a/src/trodes_to_nwb/convert_ephys.py +++ b/src/trodes_to_nwb/convert_ephys.py @@ -355,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( @@ -377,10 +377,13 @@ def add_raw_ephys( ) # Use metadata-provided conversion if not available in rec file # read metadata - if "save_as_microvolts" in metadata: - save_as_microvolts = metadata["save_as_microvolts"] - else: + 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(