Skip to content

Commit

Permalink
Make get_data_in_units work with channel_conversion (#1806)
Browse files Browse the repository at this point in the history
Co-authored-by: Ryan Ly <[email protected]>
  • Loading branch information
h-mayorquin and rly authored Dec 21, 2023
1 parent ac17d17 commit 17b5621
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) and [#1809](https://github.com/NeurodataWithoutBorders/pynwb/pull/1809)
- Expose the offset, conversion and channel conversion parameters in `mock_ElectricalSeries`. @h-mayorquin [#1796](https://github.com/NeurodataWithoutBorders/pynwb/pull/1796)
- 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)
Expand Down
6 changes: 5 additions & 1 deletion src/pynwb/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,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.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)
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/test_ecephys.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -115,6 +116,24 @@ 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]])
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):

Expand Down

0 comments on commit 17b5621

Please sign in to comment.