From 138d9a51fb8dbe2dec91306c46be71e59bb51fe7 Mon Sep 17 00:00:00 2001 From: Joep Vanlier Date: Fri, 13 Sep 2024 16:37:49 +0200 Subject: [PATCH] slice: add `__array__` to `Slice` this allows users to call numpy functions directly on the slice --- changelog.md | 1 + lumicks/pylake/channel.py | 3 +++ lumicks/pylake/tests/test_channels/test_channels.py | 6 ++++++ 3 files changed, 10 insertions(+) diff --git a/changelog.md b/changelog.md index 25a813dd2..54f41486a 100644 --- a/changelog.md +++ b/changelog.md @@ -6,6 +6,7 @@ * Added model to correct for bead-bead coupling when using active calibration deep in bulk with two beads. See [`tutorial`](https://lumicks-pylake.readthedocs.io/en/latest/tutorial/force_calibration.html#active-calibration-with-two-beads-far-away-from-the-surface) and [`theory`](https://lumicks-pylake.readthedocs.io/en/latest/theory/force_calibration/active.html#bead-bead-coupling) for more information. * Added option to highlight a region on a time plot using [`Slice.highlight_time_range()`](https://lumicks-pylake.readthedocs.io/en/latest/_api/lumicks.pylake.channel.Slice.html#lumicks.pylake.channel.Slice.highlight_time_range). For more information see the [`tutorial`](https://lumicks-pylake.readthedocs.io/en/latest/tutorial/file.html#highlight-time-range). +* Added `__array__` to [`Slice`](https://lumicks-pylake.readthedocs.io/en/latest/_api/lumicks.pylake.channel.Slice.html). This allows passing slices directly to `numpy` functions such as `np.mean()`or `np.sum()`. * Added parameter `allow_overwrite` to [`lk.download_from_doi()`](https://lumicks-pylake.readthedocs.io/en/latest/_api/lumicks.pylake.download_from_doi.html#lumicks.pylake.download_from_doi) to allow re-downloading only those files where the checksum does not match. * Added force calibration information to channels accessed directly via the square bracket notation (e.g. `file["Force HF"]["Force 1x"].calibration`). * Calibration results and parameters are now accessible via properties which are listed when items are printed. See [calibration results](https://lumicks-pylake.readthedocs.io/en/latest/_api/lumicks.pylake.force_calibration.power_spectrum_calibration.CalibrationResults.html) and [calibration item](https://lumicks-pylake.readthedocs.io/en/latest/_api/lumicks.pylake.calibration.ForceCalibrationItem.html) API documentation for more information. diff --git a/lumicks/pylake/channel.py b/lumicks/pylake/channel.py index 77bbd9735..83b63d679 100644 --- a/lumicks/pylake/channel.py +++ b/lumicks/pylake/channel.py @@ -170,6 +170,9 @@ def __rmul__(self, other): labels=self._generate_labels(other, "*", self, keep_unit=False), ) + def __array__(self, *args, **kwargs): + return np.asarray(self.data, *args, **kwargs) + @property def start(self): """Starting timestamp of this time series in nanoseconds""" diff --git a/lumicks/pylake/tests/test_channels/test_channels.py b/lumicks/pylake/tests/test_channels/test_channels.py index f8bfd67d6..9e121b4c0 100644 --- a/lumicks/pylake/tests/test_channels/test_channels.py +++ b/lumicks/pylake/tests/test_channels/test_channels.py @@ -451,6 +451,8 @@ def test_channel(channel_h5_file): np.testing.assert_equal(force.start, 1) np.testing.assert_equal(force.stop, 41 + 10) np.testing.assert_equal(force.sample_rate, 1e9 / int(1e9 / dset.attrs["Sample rate (Hz)"])) + assert id(force.data) == id(np.asarray(force.data)) + np.testing.assert_equal(np.mean(force), np.mean(force.data)) downsampled = channel.TimeSeries.from_dataset(channel_h5_file["Force LF"]["Force 1x"]) np.testing.assert_equal(len(downsampled), 2) @@ -459,6 +461,8 @@ def test_channel(channel_h5_file): np.testing.assert_equal(downsampled.start, 1) np.testing.assert_equal(downsampled.stop, 2 + 1) np.testing.assert_equal(downsampled.sample_rate, 1e9 / 1) + assert id(downsampled.data) == id(np.asarray(downsampled.data)) + np.testing.assert_equal(np.mean(downsampled), np.mean(downsampled.data)) variable = channel.TimeSeries.from_dataset(channel_h5_file["Force LF variable"]["Force 1x"]) np.testing.assert_equal(len(variable), 3) @@ -473,6 +477,8 @@ def test_channel(channel_h5_file): np.testing.assert_equal(len(timetags), 9) assert np.all(np.equal(timetags.data, [10, 20, 30, 40, 50, 60, 70, 80, 90])) assert np.all(np.equal(timetags.timestamps, [10, 20, 30, 40, 50, 60, 70, 80, 90])) + assert id(timetags.data) == id(np.asarray(timetags.data)) + np.testing.assert_equal(np.mean(timetags), np.mean(timetags.data)) def test_downsampling():