Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/antropy/entropy.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ def sample_entropy(x, order=2, tolerance=None, metric="chebyshev"):
r = tolerance
x = np.asarray(x, dtype=np.float64)
if metric == "chebyshev" and x.size < 5000:
return _numba_sampen(x, order=order, r=r)
return _numba_sampen(np.ascontiguousarray(x), order=order, r=r)
else:
phi = _app_samp_entropy(x, order=order, r=r, metric=metric, approximate=False)
return -np.log(np.divide(phi[1], phi[0]))
Expand Down
4 changes: 2 additions & 2 deletions src/antropy/fractal.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ def higuchi_fd(x, kmax=10):
"""
x = np.asarray(x, dtype=np.float64)
kmax = int(kmax)
return _higuchi_fd(x, kmax)
return _higuchi_fd(np.ascontiguousarray(x), kmax)


@jit((types.Array(types.float64, 1, "C", readonly=True),), nopython=True)
Expand Down Expand Up @@ -477,4 +477,4 @@ def detrended_fluctuation(x):
2.0390
"""
x = np.asarray(x, dtype=np.float64)
return _dfa(x)
return _dfa(np.ascontiguousarray(x))
45 changes: 45 additions & 0 deletions tests/test_strided_inputs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""Public feature functions accept NumPy views as well as contiguous arrays."""

import numpy as np
import pytest

import antropy as ant


@pytest.mark.parametrize("name", ["sample_entropy", "higuchi_fd", "detrended_fluctuation"])
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
@pytest.mark.parametrize("readonly", [False, True])
@pytest.mark.parametrize("layout", ["step", "reverse", "column", "contiguous"])
def test_numba_features_accept_array_views(name, dtype, readonly, layout):
"""The same samples yield the same feature regardless of physical layout."""
source = np.random.default_rng(42).normal(size=(512, 3)).astype(dtype)
if layout == "step":
data = source[:, 0][::2]
elif layout == "reverse":
data = source[:, 0][::-1]
elif layout == "column":
data = source[:, 1]
else:
data = source[:, 1].copy()
if layout != "contiguous":
assert not data.flags.c_contiguous
data.flags.writeable = not readonly
original = data.copy()
feature = getattr(ant, name)
reference = feature(np.array(data, dtype=np.float64, copy=True, order="C"))

result = feature(data)

assert np.isfinite(reference)
assert result == reference
np.testing.assert_array_equal(data, original)
assert data.flags.writeable == (not readonly)


@pytest.mark.parametrize("metric,size", [("chebyshev", 5000), ("euclidean", 512)])
def test_sample_entropy_strided_kdtree_path(metric, size):
"""The non-Numba routes retain their existing behavior."""
data = np.random.default_rng(52).normal(size=2 * size)[::2]
reference = ant.sample_entropy(data.copy(), metric=metric)

assert ant.sample_entropy(data, metric=metric) == reference