Skip to content
Open
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
4 changes: 3 additions & 1 deletion aeon/segmentation/_hidalgo.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ def _get_neighbourhood_params(self, X):
n_neighbors=q + 1, algorithm="ball_tree", metric=metric
).fit(X)
distances, Iin = nbrs.kneighbors(X)
mu = np.divide(distances[:, 2], distances[:, 1])
eps = 1e-12
# stabilise r2/r1 ratio; protect against zero or near-zero r1
mu = np.divide(distances[:, 2], distances[:, 1] + eps)

nbrmat = np.zeros((m, m))
for n in range(q):
Expand Down
28 changes: 27 additions & 1 deletion aeon/segmentation/tests/test_hidalgo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Test Hidalgo segmenter."""

from aeon.segmentation._hidalgo import _binom, _partition_function
import numpy as np

from aeon.segmentation._hidalgo import HidalgoSegmenter, _binom, _partition_function


def test_partition_function():
Expand All @@ -9,3 +11,27 @@ def test_partition_function():
assert p == 8.0
b = _binom(10, 2)
assert b == 45.0


def test_hidalgo_zero_distance_duplicate_rows():
"""Test that Hidalgo handles duplicate rows without numerical errors."""
X = np.array(
[
[1.0, 2.0, 3.0],
[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
[7.0, 8.0, 9.0],
]
)
seg = HidalgoSegmenter(
K=2,
q=2,
n_iter=50,
burn_in=0.2,
sampling_rate=5,
seed=1,
)
out = seg.fit_predict(X, axis=0)
assert out is not None
assert isinstance(out, np.ndarray)
assert len(out) == len(X)