From 7d0e575f224ba1c4cfd26548fb7569d0a84fc3c6 Mon Sep 17 00:00:00 2001 From: Greg Knoll Date: Thu, 28 Mar 2024 11:04:36 +0100 Subject: [PATCH 1/3] Fix issue #1441: Read gain scaling from .rec header. Updates spikegadgetsrawio.py:_parse_header() to read the channel (ntrode) gain scaling from the header file elements. --- neo/rawio/spikegadgetsrawio.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/neo/rawio/spikegadgetsrawio.py b/neo/rawio/spikegadgetsrawio.py index 5b1d858c4..3cff1069e 100644 --- a/neo/rawio/spikegadgetsrawio.py +++ b/neo/rawio/spikegadgetsrawio.py @@ -55,10 +55,10 @@ def __init__(self, filename="", selected_streams=None): Notes ----- - This file format has multiple version. New versions include the gain for scaling. - The current implementation does not contain this feature because we don't have - files to test this. So now the gain is "hardcoded" to 1, and so units are - not handled correctly. + This file format has multiple versions: + - Newer versions include the gain for scaling to microvolts [uV]. + - If the scaling is not found in the header, it will be "hardcoded" to 1, + in which case the units are not handled correctly. Examples -------- @@ -173,12 +173,17 @@ def _parse_header(self): chan_ind = 0 for trode in sconf: + if "spikeScalingToUv" in trode.attrib: + gain = float(trode.attrib["spikeScalingToUv"]) + units = "uV" + else: + gain = 1 # revert to hardcoded gain + units = "" + for schan in trode: name = "trode" + trode.attrib["id"] + "chan" + schan.attrib["hwChan"] chan_id = schan.attrib["hwChan"] - # TODO LATER : handle gain correctly according the file version - units = "" - gain = 1.0 + offset = 0.0 signal_channels.append( (name, chan_id, self._sampling_rate, "int16", units, gain, offset, stream_id) From 035b1aa47feb67949aa9e777bbc7c08fa499eebf Mon Sep 17 00:00:00 2001 From: Zach McKenzie <92116279+zm711@users.noreply.github.com> Date: Fri, 29 Mar 2024 06:50:39 -0400 Subject: [PATCH 2/3] Make the note more explicit about what is hardcoded. --- neo/rawio/spikegadgetsrawio.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neo/rawio/spikegadgetsrawio.py b/neo/rawio/spikegadgetsrawio.py index 3cff1069e..e43aebedc 100644 --- a/neo/rawio/spikegadgetsrawio.py +++ b/neo/rawio/spikegadgetsrawio.py @@ -57,7 +57,7 @@ def __init__(self, filename="", selected_streams=None): ----- This file format has multiple versions: - Newer versions include the gain for scaling to microvolts [uV]. - - If the scaling is not found in the header, it will be "hardcoded" to 1, + - If the scaling is not found in the header, the gain will be "hardcoded" to 1, in which case the units are not handled correctly. Examples From 83a84b2601b3aa10685d4ada41a06a49c421540c Mon Sep 17 00:00:00 2001 From: Greg Knoll Date: Tue, 9 Apr 2024 16:44:41 +0200 Subject: [PATCH 3/3] ADD: scaling attribute and warning if not scaleable... ...also adds clarification in docstring. --- neo/rawio/spikegadgetsrawio.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/neo/rawio/spikegadgetsrawio.py b/neo/rawio/spikegadgetsrawio.py index e43aebedc..08e49fa2c 100644 --- a/neo/rawio/spikegadgetsrawio.py +++ b/neo/rawio/spikegadgetsrawio.py @@ -59,6 +59,9 @@ def __init__(self, filename="", selected_streams=None): - Newer versions include the gain for scaling to microvolts [uV]. - If the scaling is not found in the header, the gain will be "hardcoded" to 1, in which case the units are not handled correctly. + This will not affect functions that do not rely on the data having physical units, + e.g., _get_analogsignal_chunk, but functions such as rescale_signal_raw_to_float + will be inaccurate. Examples -------- @@ -172,6 +175,10 @@ def _parse_header(self): self._mask_channels_bytes[stream_id] = [] chan_ind = 0 + self.is_scaleable = "spikeScalingToUv" in sconf[0].attrib + if not self.is_scaleable: + self.logger.warning("Unable to read channel gain scaling (to uV) from .rec header. Data has no physical units!") + for trode in sconf: if "spikeScalingToUv" in trode.attrib: gain = float(trode.attrib["spikeScalingToUv"])