From dda869dbd846c28b06d8ec19edb13fbe5e6bc294 Mon Sep 17 00:00:00 2001 From: Heberto Mayorquin Date: Mon, 18 Dec 2023 11:37:01 +0100 Subject: [PATCH 1/7] added test --- tests/unit/test_ecephys.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/unit/test_ecephys.py b/tests/unit/test_ecephys.py index 6f76a5e8c..7abf444aa 100644 --- a/tests/unit/test_ecephys.py +++ b/tests/unit/test_ecephys.py @@ -18,6 +18,7 @@ from pynwb.device import Device from pynwb.file import ElectrodeTable from pynwb.testing import TestCase +from pynwb.testing.mock.ecephys import mock_ElectricalSeries from hdmf.common import DynamicTableRegion @@ -116,6 +117,24 @@ def test_dimensions_warning(self): ) in str(w[-1].message) +def test_get_data_in_units(): + + data = np.asarray([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1]]) + conversion = 1.0 + offset = 3.0 + channel_conversion = np.asarray([2.0, 2.0]) + electrical_series = mock_ElectricalSeries( + data=data, + conversion=conversion, + offset=offset, + channel_conversion=channel_conversion, + ) + + data_in_units = electrical_series.get_data_in_units() + expected_data = data * conversion * channel_conversion[:, np.newaxis] + offset + + np.testing.assert_almost_equal(data_in_units, expected_data) + class SpikeEventSeriesConstructor(TestCase): def _create_table_and_region(self): From 6a119a12b8915fbcd0b8febfe03f73848a15201a Mon Sep 17 00:00:00 2001 From: Heberto Mayorquin Date: Mon, 18 Dec 2023 11:37:47 +0100 Subject: [PATCH 2/7] add fix --- src/pynwb/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pynwb/base.py b/src/pynwb/base.py index 5514288e8..2ad38edd5 100644 --- a/src/pynwb/base.py +++ b/src/pynwb/base.py @@ -296,7 +296,7 @@ def get_timestamps(self): return np.arange(len(self.data)) / self.rate + self.starting_time def get_data_in_units(self): - return np.asarray(self.data) * self.conversion + self.offset + return np.asarray(self.data) * self.conversion * self.channel_conversion[:, np.newaxis] + self.offset @register_class('Image', CORE_NAMESPACE) From 279510978c7cb0ba7ac3ebde7af76f2b1ba27ac4 Mon Sep 17 00:00:00 2001 From: Heberto Mayorquin Date: Mon, 18 Dec 2023 11:52:54 +0100 Subject: [PATCH 3/7] pass other tests --- src/pynwb/base.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/pynwb/base.py b/src/pynwb/base.py index 2ad38edd5..6f051befa 100644 --- a/src/pynwb/base.py +++ b/src/pynwb/base.py @@ -296,7 +296,11 @@ def get_timestamps(self): return np.arange(len(self.data)) / self.rate + self.starting_time def get_data_in_units(self): - return np.asarray(self.data) * self.conversion * self.channel_conversion[:, np.newaxis] + self.offset + if "channel_conversion" in self.fields: + scale_factor = self.conversion * self.channel_conversion[:, np.newaxis] + else: + scale_factor = self.conversion + return np.asarray(self.data) * scale_factor + self.offset @register_class('Image', CORE_NAMESPACE) From 38368ea37dd61a9ec221e94dab035c5bdec3567c Mon Sep 17 00:00:00 2001 From: Heberto Mayorquin Date: Mon, 18 Dec 2023 11:58:51 +0100 Subject: [PATCH 4/7] changelog and flake --- CHANGELOG.md | 1 + src/pynwb/base.py | 2 +- tests/unit/test_ecephys.py | 3 ++- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 09bc731e9..6af9719a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - Add `pynwb.get_nwbfile_version()`. @bendichter [#1703](https://github.com/NeurodataWithoutBorders/pynwb/pull/1703) - Updated timeseries data checks to warn instead of error when reading invalid files. @stephprince [#1793](https://github.com/NeurodataWithoutBorders/pynwb/pull/1793) - Expose the offset, conversion and channel conversion parameters in `mock_ElectricalSeries`. @h-mayorquin [#1796](https://github.com/NeurodataWithoutBorders/pynwb/pull/1796) +- Enchance `get_data_in_units()` to work with objects that have a `channel_conversion` attribute like the `ElectricalSeries` @h-mayorquin [#1806](https://github.com/NeurodataWithoutBorders/pynwb/pull/1806) ### Bug fixes - Fix bug where namespaces were loaded in "w-" mode. @h-mayorquin [#1795](https://github.com/NeurodataWithoutBorders/pynwb/pull/1795) diff --git a/src/pynwb/base.py b/src/pynwb/base.py index 6f051befa..ef094aa61 100644 --- a/src/pynwb/base.py +++ b/src/pynwb/base.py @@ -297,7 +297,7 @@ def get_timestamps(self): def get_data_in_units(self): if "channel_conversion" in self.fields: - scale_factor = self.conversion * self.channel_conversion[:, np.newaxis] + scale_factor = self.conversion * self.channel_conversion[:, np.newaxis] else: scale_factor = self.conversion return np.asarray(self.data) * scale_factor + self.offset diff --git a/tests/unit/test_ecephys.py b/tests/unit/test_ecephys.py index 7abf444aa..11e3ba03c 100644 --- a/tests/unit/test_ecephys.py +++ b/tests/unit/test_ecephys.py @@ -131,10 +131,11 @@ def test_get_data_in_units(): ) data_in_units = electrical_series.get_data_in_units() - expected_data = data * conversion * channel_conversion[:, np.newaxis] + offset + expected_data = data * conversion * channel_conversion[:, np.newaxis] + offset np.testing.assert_almost_equal(data_in_units, expected_data) + class SpikeEventSeriesConstructor(TestCase): def _create_table_and_region(self): From 8eb34d48be8280efd501f4d0c2cb2f0734c29566 Mon Sep 17 00:00:00 2001 From: Heberto Mayorquin Date: Mon, 18 Dec 2023 12:00:38 +0100 Subject: [PATCH 5/7] spelling mistake --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6af9719a6..18f1eb529 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ - Add `pynwb.get_nwbfile_version()`. @bendichter [#1703](https://github.com/NeurodataWithoutBorders/pynwb/pull/1703) - Updated timeseries data checks to warn instead of error when reading invalid files. @stephprince [#1793](https://github.com/NeurodataWithoutBorders/pynwb/pull/1793) - Expose the offset, conversion and channel conversion parameters in `mock_ElectricalSeries`. @h-mayorquin [#1796](https://github.com/NeurodataWithoutBorders/pynwb/pull/1796) -- Enchance `get_data_in_units()` to work with objects that have a `channel_conversion` attribute like the `ElectricalSeries` @h-mayorquin [#1806](https://github.com/NeurodataWithoutBorders/pynwb/pull/1806) +- Enhance `get_data_in_units()` to work with objects that have a `channel_conversion` attribute like the `ElectricalSeries` @h-mayorquin [#1806](https://github.com/NeurodataWithoutBorders/pynwb/pull/1806) ### Bug fixes - Fix bug where namespaces were loaded in "w-" mode. @h-mayorquin [#1795](https://github.com/NeurodataWithoutBorders/pynwb/pull/1795) From 38ea7646f5d957d1ce59c23c7b7fcff76fe23f32 Mon Sep 17 00:00:00 2001 From: Heberto Mayorquin Date: Mon, 18 Dec 2023 13:16:00 +0100 Subject: [PATCH 6/7] move the test inwards --- tests/unit/test_ecephys.py | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/tests/unit/test_ecephys.py b/tests/unit/test_ecephys.py index 11e3ba03c..4e71edc84 100644 --- a/tests/unit/test_ecephys.py +++ b/tests/unit/test_ecephys.py @@ -117,23 +117,23 @@ def test_dimensions_warning(self): ) in str(w[-1].message) -def test_get_data_in_units(): - - data = np.asarray([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1]]) - conversion = 1.0 - offset = 3.0 - channel_conversion = np.asarray([2.0, 2.0]) - electrical_series = mock_ElectricalSeries( - data=data, - conversion=conversion, - offset=offset, - channel_conversion=channel_conversion, - ) - - data_in_units = electrical_series.get_data_in_units() - expected_data = data * conversion * channel_conversion[:, np.newaxis] + offset - - np.testing.assert_almost_equal(data_in_units, expected_data) + def test_get_data_in_units(self): + + data = np.asarray([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1]]) + conversion = 1.0 + offset = 3.0 + channel_conversion = np.asarray([2.0, 2.0]) + electrical_series = mock_ElectricalSeries( + data=data, + conversion=conversion, + offset=offset, + channel_conversion=channel_conversion, + ) + + data_in_units = electrical_series.get_data_in_units() + expected_data = data * conversion * channel_conversion[:, np.newaxis] + offset + + np.testing.assert_almost_equal(data_in_units, expected_data) class SpikeEventSeriesConstructor(TestCase): From 64c2e9b2985d9827c044c65f856cd100af43be41 Mon Sep 17 00:00:00 2001 From: Heberto Mayorquin Date: Mon, 18 Dec 2023 13:21:22 +0100 Subject: [PATCH 7/7] flake --- tests/unit/test_ecephys.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/unit/test_ecephys.py b/tests/unit/test_ecephys.py index 4e71edc84..f81b61f84 100644 --- a/tests/unit/test_ecephys.py +++ b/tests/unit/test_ecephys.py @@ -116,7 +116,6 @@ def test_dimensions_warning(self): "but instead the first does. Data is oriented incorrectly and should be transposed." ) in str(w[-1].message) - def test_get_data_in_units(self): data = np.asarray([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1]])