Skip to content

Discord subspace selection returns incorrect dimension indices #1227

Closed

Description

@AryanRG

subspace(..., discords=True) can return the wrong dimension indices. The helper currently uses D[::-1].argsort(), which returns positions in a reversed array.

For distances [1, 3, 2], the returned order is [2, 0, 1]. Selecting the largest distances first should give [1, 2, 0].

This affects normalized and non-normalized subspace selection and the MDL helpers. The naive test helpers contain the same expression, which masks the error in the existing comparisons.

I have a tested fix using a stable sort of -D, plus one hand-computed regression. It also corrects the two naive helpers. The five affected modules pass with 349 passed and 1 skipped, and an independent check of the public APIs agrees with the expected ordering.

PR #1226 was automatically closed because I missed the template under docs/. I’ll keep it closed while we coordinate. Does this approach look right? The full coverage, distributed and CUDA-simulator suites remain to be run.

Prepared with AI assistance.

Activity

  1. seanlaw commented on Nov 17, 2026

    @seanlaw
    Contributor

    @AryanRG Thank you for your inquiry. However, I think there is a fundamental misunderstanding. The function core._subspace assumes that the input distance profile, D, is multi-dimensional so having a D = np.array([1.0, 3.0, 2.0]) is an invalid input (D cannot be one-dimensional).

    A valid input for D might be:

    import numpy as np
    
    # Note that this is a multi-dimensional distance profile!
    D = np.array([[1., 3., 2., 6.],
                  [2., 4., 1., 2.], 
                  [5., 1., 1., 9.]])
    
    sorted_idx = D[::-1].argsort(axis=0, kind="mergesort")  # Discords
    

    which produces:

    # sorted_idx
    array([[2, 0, 0, 1],
           [1, 2, 1, 2],
           [0, 1, 2, 0]])
    

    So, for the first column of D (i.e., D[:, 0] = [1., 2., 5.]), the order of the sorted indices is [2, 1, 0] (which corresponds to the first column of sorted_idx). Thus, what is currently implemented is correct based on this example. Let me know if that makes sense or if you have further questions.

  2. AryanRG commented on Nov 17, 2026

    @AryanRG
    ContributorAuthor

    Thanks @seanlaw. I checked this through the public API too.

    import numpy as np
    import stumpy
    
    T = np.array([[0., 0., 0., 1., 1., 1.],
                  [0., 0., 0., 3., 3., 3.],
                  [0., 0., 0., 2., 2., 2.]])
    print(stumpy.maamp_subspace(T, 3, 0, 3, 0, discords=True))
    # [2]

    The per-dimension distances are approximately [147.2, 441.7, 294.4], so I'd expect [1]. maamp_subspace computes D with np.linalg.norm(..., axis=1) and passes that 1-D array to _subspace.

    In your 2-D example, the second column is [3., 4., 1.]. The current expression picks index 0, while the largest distance is at index 1. Could you take another look at these cases?

  3. seanlaw commented on Nov 17, 2026

    @seanlaw
    Contributor

    Okay, I took a closer look (and also reviewing #188) and I was wrong. I believe that your proposed fix is correct.

    If you would like to resubmit the PR then I would be happy to merge it 👍

  4. AryanRG commented on Nov 17, 2026

    @AryanRG
    ContributorAuthor

    Thanks for taking another look, Sean. I tested the patch against current main and the five affected modules passed (349 passed, one GPU skip). The full local run stopped in the distributed aamp_stimp tests, and I reproduced Dask’s FutureCancelledError on unmodified main with the same seed. Would you be comfortable with reopening #1226 and having hosted CI cover the full run?

  5. added
    bug
    on Nov 18, 2026
  6. added a commit that references this issue on Nov 19, 2026
    34e7412
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions