Negative sakoe_chiba_radius is silently accepted: dtw/ctw/frechet return inf and lcss returns 0

Open
#857 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@MohamedO6 is already working on this.

Since Dec 6, 2026.

  • #858 by @MohamedO6 — open

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, ctw and frechet return inf
  • lcss returns 0.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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from tslearn-team/tslearn

All issues in tslearn-team/tslearn

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.