Skip to content

Commit

Permalink
power_spectrum: force integer downsampling factors
Browse files Browse the repository at this point in the history
Since calibration dictionaries coming from BL are usually floats, it can be confusing when you get an error somewhere internally in `calculate_power_spectrum` when you pass a float that is losslessly convertible to integer. This fixes that, while still disallowing non-integer downsampling ratios.
  • Loading branch information
JoepVanlier committed Jan 28, 2025
1 parent 9cd8805 commit 95446cc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

* Added [`PowerSpectrum.compress()`](https://lumicks-pylake.readthedocs.io/en/latest/_api/lumicks.pylake.force_calibration.power_spectrum.PowerSpectrum.html#lumicks.pylake.force_calibration.power_spectrum.PowerSpectrum.compress#) to discard high frequency spectral data for a downsampled spectrum to conserve memory.

#### Improvements

* Force `num_points_per_block` to be integer when down-sampling a [`PowerSpectrum`](https://lumicks-pylake.readthedocs.io/en/latest/_api/lumicks.pylake.force_calibration.power_spectrum.PowerSpectrum.html).

## v1.6.0 | t.b.d.

#### New features
Expand Down
6 changes: 6 additions & 0 deletions lumicks/pylake/force_calibration/power_spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,13 +264,19 @@ def downsampled_by(self, factor, reduce=np.mean) -> "PowerSpectrum":
(Deprecated) Function to use for down-sampling the data. Only `np.mean` will be
supported going forward.
"""
if int(factor) != factor:
raise NotImplementedError("Only integer downsampling factors are supported")
else:
factor = int(factor)

if reduce != np.mean:
warnings.warn(
DeprecationWarning(
"Providing other reduction functions than `np.mean` is deprecated and will be "
"removed in a future version of Pylake"
)
)

return PowerSpectrum(
downsample(self.frequency, factor, reduce),
downsample(self.power, factor, reduce),
Expand Down
12 changes: 12 additions & 0 deletions lumicks/pylake/force_calibration/tests/test_power_spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,15 @@ def test_compress():
assert compressed._num_points_per_block == 100
np.testing.assert_allclose(ps_downsampled.frequency, ps_downsampled.frequency)
np.testing.assert_allclose(compressed.power, ps_downsampled.power)


def test_integer_num_points_per_block():
ps = PowerSpectrum(np.arange(500), np.arange(500), 78125, total_duration=1)
ps_ds = ps.downsampled_by(10.0)
ps_ds_int = ps.downsampled_by(10)
np.testing.assert_allclose(ps_ds.power, ps_ds_int.power)

with pytest.raises(
NotImplementedError, match="Only integer downsampling factors are supported"
):
ps.downsampled_by(10.00001)

0 comments on commit 95446cc

Please sign in to comment.