Negative sakoe_chiba_radius is silently accepted: dtw/ctw/frechet return inf and lcss returns 0
Maintainers usually reply within 1 day
Assessment
This issue has not been assessed yet.
Description
Describe the bug
sakoe_chiba_radius is never checked for negative values. A negative radius makes the Sakoe-Chiba mask empty, and every metric that accepts the constraint then returns a meaningless value instead of raising:
dtw,dtw_path,ctwandfrechetreturninflcssreturns0.0
dtw_path even returns a path together with the infinite score.
To Reproduce
import numpy as np
from tslearn.metrics import sakoe_chiba_mask, dtw, dtw_path, lcss, ctw, frechet
a = np.array([[0.0], [1.0], [2.0], [3.0]])
b = np.array([[0.0], [1.0], [2.5], [3.0]])
print(sakoe_chiba_mask(4, 4, radius=-1).sum())
print(dtw(a, b, global_constraint="sakoe_chiba", sakoe_chiba_radius=-1))
print(dtw_path(a, b, global_constraint="sakoe_chiba", sakoe_chiba_radius=-1))
print(lcss(a, b, eps=1, global_constraint="sakoe_chiba", sakoe_chiba_radius=-1))
print(ctw(a, b, global_constraint="sakoe_chiba", sakoe_chiba_radius=-1))
print(frechet(a, b, global_constraint="sakoe_chiba", sakoe_chiba_radius=-1))
Output:
0
inf
([(0, 0), (1, 1), (2, 2), (3, 3)], inf)
0.0
inf
inf
Expected behavior
A ValueError saying the radius must be non-negative. None already means "no constraint", and the Itakura path already rejects an unfeasible constraint (itakura_mask raises when the mask has an empty row or column), so a negative radius is the one invalid band that goes through silently.
Environment
- OS: Ubuntu 24.04
- tslearn version: 0.11.0.dev, main at 27e3753
- Python 3.12, numpy 2.5.5
Additional context
This also reaches the estimators through metric_params. With a negative radius, TimeSeriesKMeans(metric="dtw", metric_params={"sakoe_chiba_radius": -1}).fit(X) completes and leaves inertia_ at inf, so a radius computed in a tuning loop produces a meaningless clustering instead of an error.
Validating the radius in sakoe_chiba_mask and compute_mask covers every metric and both backends. I have a patch with tests ready and can open a PR.
- Dominant language
- Python
- Stars
- 3.2k
- Forks
- 418
- Avg merge
- 2d 14h
- Merged PRs (30d)
- 24
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from tslearn-team/tslearn
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
tslearn-team/tslearn#854 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 55/100
tslearn-team/tslearn#855 ·
Maintainers usually reply within 1 day
-
PAA / SAX / 1d-SAX silently return NaN or garbage when n_segments is larger than the series lengthPossibly taken @MohamedO6 claimed this 2 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 20/100
tslearn-team/tslearn#852 ·
Maintainers usually reply within 1 day
-
arbitrary code execution when loading a model via from_pickle (CWE-502)Possibly taken @ifaizk5 claimed this 4 days ago. Opendocumentation good first issue
Difficulty 5/5 Over a week Newbie friendliness 25/100
tslearn-team/tslearn#820 · 1 comment ·
Maintainers usually reply within 1 day
-
Time series scalers reject ragged lists despite variable-length supportPossibly taken @sunlishuo25 claimed this 62 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 25/100
tslearn-team/tslearn#751 · 1 comment ·
Maintainers usually reply within 1 day
All issues in tslearn-team/tslearn
Similar issues
-
docs(contributing): the two things that silently block a PRPossibly taken A pull request linked to this issue is open or already merged. Openavailable documentation good first issue
Difficulty 1/5 1-3 hours Newbie friendliness 95/100
Nixitov/Riptide-Native#944 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
JTHCode/secretguard#24 ·
Maintainers usually reply within 1 day
-
area: middleware documentation good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
enhancement good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 1 day
-
good first issue
Difficulty 1/5 Under an hour Newbie friendliness 95/100
omarelshamy25x/shamy-hijri#63 ·
Maintainers usually reply within 1 day