fix(metrics): handle single-segment partitions in hausdorff and meantime#374
Open
gaoflow wants to merge 2 commits into
Open
fix(metrics): handle single-segment partitions in hausdorff and meantime#374gaoflow wants to merge 2 commits into
gaoflow wants to merge 2 commits into
Conversation
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.
for more information, see https://pre-commit.ci
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
hausdorff()andmeantime()crash withValueErrorwhen eitherpartition contains no intermediate breakpoints (i.e. a single-segment
prediction or ground truth):
Both functions call
scipy.spatial.distance.cdist()on the arrays ofintermediate breakpoints and then take
.min()/.max()of the result.When one array is empty (no intermediate breakpoints),
cdistreturns azero-row or zero-column matrix and
numpycannot take the min/max of anempty axis.
The
sanity_check()passes for these inputs (both lists end with the sameindex), so callers have no way to anticipate the crash.
Fix
Short-circuit before
cdistwhen either intermediate-breakpoint array isempty:
0.0np.inf(incomparable sets, by convention)Tests
Two new parametrized tests verify the edge cases for
hausdorffandmeantimedirectly. All 20 tests pass.