Skip to content

Commit

Permalink
slice: add __array__ to Slice
Browse files Browse the repository at this point in the history
this allows users to call numpy functions directly on the slice
  • Loading branch information
JoepVanlier committed Sep 13, 2024
1 parent cde5cc4 commit 138d9a5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions lumicks/pylake/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
6 changes: 6 additions & 0 deletions lumicks/pylake/tests/test_channels/test_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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():
Expand Down

0 comments on commit 138d9a5

Please sign in to comment.