diff --git a/aeon/segmentation/_hidalgo.py b/aeon/segmentation/_hidalgo.py index c298873ab9..2362dbc44e 100644 --- a/aeon/segmentation/_hidalgo.py +++ b/aeon/segmentation/_hidalgo.py @@ -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): diff --git a/aeon/segmentation/tests/test_hidalgo.py b/aeon/segmentation/tests/test_hidalgo.py index f216dcc54a..e055407b45 100644 --- a/aeon/segmentation/tests/test_hidalgo.py +++ b/aeon/segmentation/tests/test_hidalgo.py @@ -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(): @@ -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)