Skip to content

fix(metrics): handle single-segment partitions in hausdorff and meantime#374

Open
gaoflow wants to merge 2 commits into
deepcharles:masterfrom
gaoflow:fix-metrics-empty-partition
Open

fix(metrics): handle single-segment partitions in hausdorff and meantime#374
gaoflow wants to merge 2 commits into
deepcharles:masterfrom
gaoflow:fix-metrics-empty-partition

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 24, 2026

Copy link
Copy Markdown

Bug

hausdorff() and meantime() crash with ValueError when either
partition contains no intermediate breakpoints (i.e. a single-segment
prediction or ground truth):

>>> import ruptures as rpt
>>> rpt.hausdorff([100], [50, 100])
ValueError: zero-size array to reduction operation minimum which has no identity

>>> rpt.meantime([50, 100], [100])
ValueError: zero-size array to reduction operation minimum which has no identity

Both functions call scipy.spatial.distance.cdist() on the arrays of
intermediate breakpoints and then take .min() / .max() of the result.
When one array is empty (no intermediate breakpoints), cdist returns a
zero-row or zero-column matrix and numpy cannot take the min/max of an
empty axis.

The sanity_check() passes for these inputs (both lists end with the same
index), so callers have no way to anticipate the crash.

Fix

Short-circuit before cdist when either intermediate-breakpoint array is
empty:

  • Both empty (both single-segment) → distance 0.0
  • One empty, one not → distance np.inf (incomparable sets, by convention)

Tests

Two new parametrized tests verify the edge cases for hausdorff and
meantime directly. All 20 tests pass.

hausdorff() and meantime() both call cdist() on intermediate-breakpoint
arrays and then reduce over the result with .min() / .max().  When a
partition contains no intermediate breakpoints (i.e. the signal is
predicted to be a single segment), that array is empty.  numpy refuses
to take the min/max of a zero-element array and raises:

    ValueError: zero-size array to reduction operation minimum
                which has no identity

The fix short-circuits before cdist when either array is empty:
- If both are empty (both single-segment) → distance is 0.
- If only one is empty → distance is np.inf (incomparable sets).

Tests added for hausdorff and meantime with single-segment partitions.
@github-actions github-actions Bot added the Type: Fix Bug or Bug fixes label Jun 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Type: Fix Bug or Bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant