Skip to content

[MRG] Ignore NaN subsequences in the numpy matrix profile - #754

Merged
charavelg merged 2 commits into
tslearn-team:mainfrom
SajalDevX:fix/matrix-profile-nan
Nov 20, 2026
Merged

charavelg merged 2 commits into
tslearn-team:mainfrom
SajalDevX:fix/matrix-profile-nan

Conversation

@SajalDevX

Copy link
Copy Markdown
Contributor

Fixes #753

With the default implementation="numpy", MatrixProfile returned a profile full of NaN for any time series containing a NaN, which includes every shorter series of a variable length dataset (they are NaN-padded). pdist gives NaN for every distance to a subsequence that overlaps a NaN, and dists.min(axis=1) then propagates it to all rows.

This PR gives those NaN distances an infinite value, together with the exclusion band, before taking the minimum. That is what stumpy.stump does as well, so both implementations now agree: the profile of a padded series is the same as the profile of the unpadded series, followed by inf for the padded positions.

Changes:

  • tslearn/matrix_profile/matrix_profile.py: dists[band] = np.inf becomes dists[band | np.isnan(dists)] = np.inf.
  • tests/test_matrixprofile.py: new test_variable_length (for scale=True and scale=False) checking that, on a two-series variable length dataset, the shorter series' profile matches its standalone profile on the valid positions and is inf on the padded ones, and that the longer series is unaffected. It does not need stumpy.
  • CHANGELOG.md: entry under "Fixed".

How I tested it:

  • pytest tests/test_matrixprofile.py: the new test fails on main (2 failed, 1 passed) and passes with the fix (3 passed, including test_consistent_with_stumpy with stumpy installed).
  • pytest --doctest-modules tslearn/matrix_profile tests/test_matrixprofile.py: 5 passed.
  • pytest tests/test_estimators.py -k MatrixProfile: 43 passed, 1 skipped, 2 xpassed.
  • pytest tests/test_variablelength.py tests/test_serialize_models.py -k "matrix or variable": 6 passed.
  • I also checked by hand that a NaN in the middle of a series now gives the same output as implementation="stump".
  • flake8 reports nothing new on the changed files (the 3 existing warnings in matrix_profile.py are on untouched lines).

One open question: for the padded tail I kept inf to match stump. If you'd prefer NaN there, so the output stays a NaN-padded variable length dataset, I can change both implementations in this PR.

@codecov

codecov Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.91%. Comparing base (9ee24b5) to head (801e9e2).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #754   +/-   ##
=======================================
  Coverage   96.90%   96.91%           
=======================================
  Files          91       91           
  Lines        9674     9688   +14     
=======================================
+ Hits         9375     9389   +14     
  Misses        299      299           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

MatrixProfile(implementation="numpy") takes the row-wise minimum of the
pairwise distances between subsequences. When a time series contains a
NaN, for instance the padding of a shorter series in a variable length
dataset, every distance to a subsequence that overlaps the NaN is NaN,
and numpy's min() propagates it, so the whole profile of that series
comes out as NaN.

Give those distances an infinite value, like the exclusion band, which
is also what stumpy.stump does. The profile of a padded series is now
the same as the one of the unpadded series, followed by inf for the
padded positions, as with implementation="stump".
@SajalDevX
SajalDevX force-pushed the fix/matrix-profile-nan branch from 01a49d3 to c2efd81 Compare November 12, 2026 08:19
@SajalDevX

Copy link
Copy Markdown
Contributor Author

I rebased the branch onto the latest main and moved the changelog entry into the Towards v0.11.0 Fixed section, since v0.10.0 is already released. The matrix profile tests pass locally after the rebase.

@charavelg
charavelg merged commit 89b66a8 into tslearn-team:main Nov 20, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MatrixProfile (numpy implementation) returns all NaN for a NaN-padded / variable length time series

2 participants