Skip to content

Commit

Permalink
add better decimate error message (#401)
Browse files Browse the repository at this point in the history
* add decimate error message

* lint
  • Loading branch information
d-chambers authored Jun 13, 2024
1 parent d1dc874 commit 2dee38b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
5 changes: 3 additions & 2 deletions dascore/io/silixah5/core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Core modules for Silixa H5 support.
"""

from __future__ import annotations

import numpy as np
Expand All @@ -16,9 +17,9 @@
class SilixaPatchAttrs(dc.PatchAttrs):
"""Patch Attributes for Silixa hdf5 format."""

gauge_length: float = np.NaN
gauge_length: float = np.nan
gauge_length_units: str = "m"
pulse_width: float = np.NaN
pulse_width: float = np.nan
pulse_width_units: str = "ns"


Expand Down
1 change: 1 addition & 0 deletions dascore/io/silixah5/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Utility functions for AP sensing module.
"""

import pandas as pd

import dascore as dc
Expand Down
27 changes: 24 additions & 3 deletions dascore/proc/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import dascore as dc
import dascore.compat as compat
from dascore.constants import PatchType
from dascore.exceptions import FilterValueError
from dascore.units import get_filter_units
from dascore.utils.patch import (
get_dim_value_from_kwargs,
Expand All @@ -19,6 +20,22 @@
from dascore.utils.time import to_int, to_timedelta64


def _apply_scipy_decimation(patch, factor, ftype, axis):
"""
Apply decimation along axis.
"""
try:
data = scipy_decimate(patch.data, factor, ftype=ftype, axis=axis)
except ValueError as e:
msg = (
"Scipy decimation failed. This can happen for dimensions with"
"few elements. Consider setting filter_type to False. The raised "
f"exception was {e}"
)
raise FilterValueError(msg)
return data


@patch_function()
def decimate(
patch: PatchType,
Expand Down Expand Up @@ -46,8 +63,12 @@ def decimate(
Notes
-----
Simply uses scipy.signal.decimate if filter_type is specified. Otherwise,
just slice data long specified dimension only including every n samples.
- Simply uses scipy.signal.decimate if filter_type is specified.
Otherwise,just slice data long specified dimension only including
every n samples.
- If the decimation dimension is small, this can fail due to lack of
padding values.
Examples
--------
Expand All @@ -62,7 +83,7 @@ def decimate(
coords, slices = patch.coords.decimate(**{dim: int(factor)})
# Apply scipy.signal.decimate and get new coords
if filter_type:
data = scipy_decimate(patch.data, factor, ftype=filter_type, axis=axis)
data = _apply_scipy_decimation(patch.data, factor, ftype=filter_type, axis=axis)
else: # No filter, simply slice along specified dimension.
data = patch.data[slices]
# Need to copy so array isn't a slice and holds onto reference of parent
Expand Down
8 changes: 8 additions & 0 deletions tests/test_proc/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import dascore as dc
from dascore.compat import random_state
from dascore.exceptions import FilterValueError
from dascore.units import Hz, m, s
from dascore.utils.patch import get_start_stop_step

Expand Down Expand Up @@ -124,6 +125,13 @@ def test_float_32_stability(self, random_patch):
decimated_none = patch.decimate(time=10, filter_type=None)
assert not np.any(pd.isnull(decimated_none.data))

def test_decimate_small_dimension(self, random_patch):
"""Ensure decimation raises helpful error on small dimensions."""
small_patch = random_patch.select(distance=(0, 10), samples=True)
match = "Scipy decimation failed."
with pytest.raises(FilterValueError, match=match):
small_patch.decimate(distance=2)


class TestResample:
"""Tests for resampling along a given dimension."""
Expand Down

0 comments on commit 2dee38b

Please sign in to comment.