Skip to content

Refactor _find_identical_streamlines to use DIPY FastStreamlineSearch (FSS) and connected components#1311

Open
CSimon-A wants to merge 4 commits intoscilus:masterfrom
CSimon-A:fss-distance-tractogram-operations
Open

Refactor _find_identical_streamlines to use DIPY FastStreamlineSearch (FSS) and connected components#1311
CSimon-A wants to merge 4 commits intoscilus:masterfrom
CSimon-A:fss-distance-tractogram-operations

Conversation

@CSimon-A
Copy link

Quick description

Replaces the custom, greedy streamline matching algorithm in _find_identical_streamlines with a robust, graph-based clustering approach using DIPY's FastStreamlineSearch and SciPy's connected_components.

Motivation & Improvements:

  • Deterministic, Order-Independent Matching: The legacy code used a greedy approach where matches depended on the order of the streamlines in the input lists. By computing an adjacency matrix and using connected components, all overlapping relationships are found simultaneously, guaranteeing deterministic results.
  • Bidirectional Matching: Added a bidirectional parameter (default True). The legacy vector subtraction (streamline - streamlines[j]) failed to match geometrically identical streamlines if their endpoints were flipped (A->B vs. B->A). FSS natively solves this.
  • Resampling for Varying Point Counts: Added nb_resample_pts (default 64). The old code grouped purely by exact point counts (np.unique(lengths)), meaning identical curves sampled at 50 vs. 51 points were missed.
  • Docstring Overhaul: Cleaned up perform_tractogram_operation_on_lines and the robust wrapper functions to accurately document the dispatcher logic, distinguishing between the heuristic extremity-hashing pathway and the rigorous FSS pathway.

Type of change

Check the relevant options.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Command line to test

Run the updated unit test for robust operations to verify that streamline deduplication, union, intersection, and difference logic behaves correctly:

pytest -v -n 0 test_tractogram_operations.py::test_robust_operations

Checklist

  • My code follows the style guidelines of this project (run autopep8)
  • I added relevant citations to scripts, modules and functions docstrings and descriptions
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I moved all functions from the script file (except the argparser and main) to scilpy modules
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Note to Reviewers regarding the CLI (scil_tractogram_math.py):

This refactor adds two new configurable parameters to the robust operations in the core module: nb_resample_pts (default 64) and bidirectional (default True).

I intentionally did not expose these as new argparse arguments in the scil_tractogram_math.py CLI script yet, as I wasn't sure if we want to clutter the terminal interface with them. The CLI will safely fall back to the defaults (64 points, bidirectional matching). Let me know if you would prefer me to add --resample_pts and --bidirectional flags to the CLI in this PR!

@CSimon-A
Copy link
Author

Hey @frheault and @StongeEtienne, the FSS refactor is complete. Ready for your review when you have a moment!

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.

1 participant