From aeee3f9c8bb2c7c53921be31bfe43d8ded0486b8 Mon Sep 17 00:00:00 2001 From: Heberto Mayorquin Date: Fri, 13 Dec 2024 14:11:47 -0600 Subject: [PATCH 1/5] improve handling of numpy scalars in numpy 2.0 --- neo/rawio/plexonrawio.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/neo/rawio/plexonrawio.py b/neo/rawio/plexonrawio.py index 5593ab2e9..b989ecbab 100644 --- a/neo/rawio/plexonrawio.py +++ b/neo/rawio/plexonrawio.py @@ -209,6 +209,10 @@ def _parse_header(self): for index, pos in enumerate(positions): bl_header = data[pos : pos + 16].view(DataBlockHeader)[0] + # To avoid overflow errors when doing arithmetic operations on numpy scalars + np_scalar_to_python_scalar = lambda x: x.item() if isinstance(x, np.generic) else x + bl_header = {key: np_scalar_to_python_scalar(bl_header[key]) for key in bl_header.dtype.names} + timestamp = bl_header["UpperByteOf5ByteTimestamp"] * 2**32 + bl_header["TimeStamp"] n1 = bl_header["NumberOfWaveforms"] n2 = bl_header["NumberOfWordsInWaveform"] @@ -253,21 +257,26 @@ def _parse_header(self): else: chan_loop = range(nb_sig_chan) for chan_index in chan_loop: - h = slowChannelHeaders[chan_index] - name = h["Name"].decode("utf8") - chan_id = h["Channel"] + channel_headers = slowChannelHeaders[chan_index] + + # To avoid overflow errors when doing arithmetic operations on numpy scalars + np_scalar_to_python_scalar = lambda x: x.item() if isinstance(x, np.generic) else x + channel_headers = {key: np_scalar_to_python_scalar(channel_headers[key]) for key in channel_headers.dtype.names} + + name = channel_headers["Name"].decode("utf8") + chan_id = channel_headers["Channel"] length = self._data_blocks[5][chan_id]["size"].sum() // 2 if length == 0: continue # channel not added - source_id.append(h["SrcId"]) + source_id.append(channel_headers["SrcId"]) channel_num_samples.append(length) - sampling_rate = float(h["ADFreq"]) + sampling_rate = float(channel_headers["ADFreq"]) sig_dtype = "int16" units = "" # I don't know units if global_header["Version"] in [100, 101]: - gain = 5000.0 / (2048 * h["Gain"] * 1000.0) + gain = 5000.0 / (2048 * channel_headers["Gain"] * 1000.0) elif global_header["Version"] in [102]: - gain = 5000.0 / (2048 * h["Gain"] * h["PreampGain"]) + gain = 5000.0 / (2048 * channel_headers["Gain"] * channel_headers["PreampGain"]) elif global_header["Version"] >= 103: gain = global_header["SlowMaxMagnitudeMV"] / ( 0.5 * (2 ** global_header["BitsPerSpikeSample"]) * h["Gain"] * h["PreampGain"] @@ -574,7 +583,7 @@ def read_as_dict(fid, dtype, offset=None): v = v.replace("\x03", "") v = v.replace("\x00", "") - info[k] = v + info[k] = v.item() if isinstance(v, np.generic) else v return info From 29e96679a31d3dea9c366f319be015db5e5f585f Mon Sep 17 00:00:00 2001 From: Heberto Mayorquin Date: Fri, 13 Dec 2024 14:15:15 -0600 Subject: [PATCH 2/5] one less single variable name that is used in complicated operations --- neo/rawio/plexonrawio.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/neo/rawio/plexonrawio.py b/neo/rawio/plexonrawio.py index b989ecbab..f230c10ff 100644 --- a/neo/rawio/plexonrawio.py +++ b/neo/rawio/plexonrawio.py @@ -365,8 +365,8 @@ def _parse_header(self): unit_loop = enumerate(self.internal_unit_ids) for unit_index, (chan_id, unit_id) in unit_loop: - c = np.nonzero(dspChannelHeaders["Channel"] == chan_id)[0][0] - h = dspChannelHeaders[c] + channel_index = np.nonzero(dspChannelHeaders["Channel"] == chan_id)[0][0] + dsp_channel_headers = dspChannelHeaders[channel_index] name = h["Name"].decode("utf8") _id = f"ch{chan_id}#{unit_id}" @@ -375,11 +375,11 @@ def _parse_header(self): wf_gain = 3000.0 / (2048 * h["Gain"] * 1000.0) elif 103 <= global_header["Version"] < 105: wf_gain = global_header["SpikeMaxMagnitudeMV"] / ( - 0.5 * 2.0 ** (global_header["BitsPerSpikeSample"]) * h["Gain"] * 1000.0 + 0.5 * 2.0 ** (global_header["BitsPerSpikeSample"]) * dsp_channel_headers["Gain"] * 1000.0 ) elif global_header["Version"] >= 105: wf_gain = global_header["SpikeMaxMagnitudeMV"] / ( - 0.5 * 2.0 ** (global_header["BitsPerSpikeSample"]) * h["Gain"] * global_header["SpikePreAmpGain"] + 0.5 * 2.0 ** (global_header["BitsPerSpikeSample"]) * dsp_channel_headers["Gain"] * global_header["SpikePreAmpGain"] ) wf_offset = 0.0 wf_left_sweep = -1 # DONT KNOWN From e98f4c86ebf0e26582e2904647a31f076d4b48de Mon Sep 17 00:00:00 2001 From: Heberto Mayorquin Date: Fri, 13 Dec 2024 14:37:47 -0600 Subject: [PATCH 3/5] variable naming --- neo/rawio/plexonrawio.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/neo/rawio/plexonrawio.py b/neo/rawio/plexonrawio.py index f230c10ff..03c31e8bf 100644 --- a/neo/rawio/plexonrawio.py +++ b/neo/rawio/plexonrawio.py @@ -368,11 +368,11 @@ def _parse_header(self): channel_index = np.nonzero(dspChannelHeaders["Channel"] == chan_id)[0][0] dsp_channel_headers = dspChannelHeaders[channel_index] - name = h["Name"].decode("utf8") + name = dsp_channel_headers["Name"].decode("utf8") _id = f"ch{chan_id}#{unit_id}" wf_units = "" if global_header["Version"] < 103: - wf_gain = 3000.0 / (2048 * h["Gain"] * 1000.0) + wf_gain = 3000.0 / (2048 * dsp_channel_headers["Gain"] * 1000.0) elif 103 <= global_header["Version"] < 105: wf_gain = global_header["SpikeMaxMagnitudeMV"] / ( 0.5 * 2.0 ** (global_header["BitsPerSpikeSample"]) * dsp_channel_headers["Gain"] * 1000.0 From 682bf76a24003d38ba5bf9573bff0a41f2e3606d Mon Sep 17 00:00:00 2001 From: Heberto Mayorquin Date: Fri, 13 Dec 2024 14:45:59 -0600 Subject: [PATCH 4/5] another variable name --- neo/rawio/plexonrawio.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neo/rawio/plexonrawio.py b/neo/rawio/plexonrawio.py index 03c31e8bf..57a5e89a5 100644 --- a/neo/rawio/plexonrawio.py +++ b/neo/rawio/plexonrawio.py @@ -279,7 +279,7 @@ def _parse_header(self): gain = 5000.0 / (2048 * channel_headers["Gain"] * channel_headers["PreampGain"]) elif global_header["Version"] >= 103: gain = global_header["SlowMaxMagnitudeMV"] / ( - 0.5 * (2 ** global_header["BitsPerSpikeSample"]) * h["Gain"] * h["PreampGain"] + 0.5 * (2 ** global_header["BitsPerSpikeSample"]) * channel_headers["Gain"] * channel_headers["PreampGain"] ) offset = 0.0 From f4627c4ad6aa3e65d511eb3d2cb403b2a386f8b3 Mon Sep 17 00:00:00 2001 From: Heberto Mayorquin Date: Thu, 19 Dec 2024 12:03:15 -0600 Subject: [PATCH 5/5] naming chnage --- neo/rawio/plexonrawio.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/neo/rawio/plexonrawio.py b/neo/rawio/plexonrawio.py index 57a5e89a5..aba993304 100644 --- a/neo/rawio/plexonrawio.py +++ b/neo/rawio/plexonrawio.py @@ -257,29 +257,29 @@ def _parse_header(self): else: chan_loop = range(nb_sig_chan) for chan_index in chan_loop: - channel_headers = slowChannelHeaders[chan_index] + slow_channel_headers = slowChannelHeaders[chan_index] # To avoid overflow errors when doing arithmetic operations on numpy scalars np_scalar_to_python_scalar = lambda x: x.item() if isinstance(x, np.generic) else x - channel_headers = {key: np_scalar_to_python_scalar(channel_headers[key]) for key in channel_headers.dtype.names} + slow_channel_headers = {key: np_scalar_to_python_scalar(slow_channel_headers[key]) for key in slow_channel_headers.dtype.names} - name = channel_headers["Name"].decode("utf8") - chan_id = channel_headers["Channel"] + name = slow_channel_headers["Name"].decode("utf8") + chan_id = slow_channel_headers["Channel"] length = self._data_blocks[5][chan_id]["size"].sum() // 2 if length == 0: continue # channel not added - source_id.append(channel_headers["SrcId"]) + source_id.append(slow_channel_headers["SrcId"]) channel_num_samples.append(length) - sampling_rate = float(channel_headers["ADFreq"]) + sampling_rate = float(slow_channel_headers["ADFreq"]) sig_dtype = "int16" units = "" # I don't know units if global_header["Version"] in [100, 101]: - gain = 5000.0 / (2048 * channel_headers["Gain"] * 1000.0) + gain = 5000.0 / (2048 * slow_channel_headers["Gain"] * 1000.0) elif global_header["Version"] in [102]: - gain = 5000.0 / (2048 * channel_headers["Gain"] * channel_headers["PreampGain"]) + gain = 5000.0 / (2048 * slow_channel_headers["Gain"] * slow_channel_headers["PreampGain"]) elif global_header["Version"] >= 103: gain = global_header["SlowMaxMagnitudeMV"] / ( - 0.5 * (2 ** global_header["BitsPerSpikeSample"]) * channel_headers["Gain"] * channel_headers["PreampGain"] + 0.5 * (2 ** global_header["BitsPerSpikeSample"]) * slow_channel_headers["Gain"] * slow_channel_headers["PreampGain"] ) offset = 0.0