Skip to content

Refactor SpearmanRankCorrelation to native PyTorch and remove SciPy dependency#3672

Open
Prathamesh8989 wants to merge 6 commits intopytorch:masterfrom
Prathamesh8989:native-spearman-metric
Open

Refactor SpearmanRankCorrelation to native PyTorch and remove SciPy dependency#3672
Prathamesh8989 wants to merge 6 commits intopytorch:masterfrom
Prathamesh8989:native-spearman-metric

Conversation

@Prathamesh8989
Copy link
Copy Markdown
Contributor

Fixes #3663

Description

This PR refactors the SpearmanRankCorrelation metric to use native PyTorch operations, effectively removing the hard dependency on scipy for calculation.

Key Changes:

  • Native Ranking Implementation: Added a _get_ranks helper function that calculates ranks using the "average" method for ties, ensuring parity with scipy.stats.spearmanr.
  • Logic Update: Replaced the SciPy-based _spearman_r function with a pure PyTorch implementation using Pearson correlation on ranked tensors.
  • Robust Testing: Updated the test suite to treat SciPy as an optional "gold standard" check. If SciPy is missing, tests skip gracefully instead of crashing, but the metric remains fully functional.
  • Improved Performance: By keeping operations in PyTorch, we reduce unnecessary CPU/GPU synchronization and data transfers.

Check list:

  • New tests are added (Updated existing tests to handle optional dependency)
  • New doc strings: description and/or example code are in RST format
  • Documentation is updated (Updated "Implementation" section in docstrings)

@github-actions github-actions Bot added the module: metrics Metrics module label Mar 15, 2026
Comment thread ignite/metrics/regression/spearman_correlation.py Outdated
Comment thread ignite/metrics/regression/spearman_correlation.py
Comment thread tests/ignite/metrics/regression/test_spearman_correlation.py Outdated
Comment thread tests/ignite/metrics/regression/test_spearman_correlation.py Outdated
Comment thread tests/ignite/metrics/regression/test_spearman_correlation.py Outdated
@Prathamesh8989 Prathamesh8989 force-pushed the native-spearman-metric branch from cfbf7be to b9f1ba1 Compare March 25, 2026 22:03
@Prathamesh8989
Copy link
Copy Markdown
Contributor Author

Hi @TahaZahid05,

Thank you for the feedback!

I've addressed the suggested changes and updated the implementation accordingly. Please let me know if there are any further adjustments needed from my side.

Comment thread ignite/metrics/regression/spearman_correlation.py Outdated
Comment thread tests/ignite/metrics/regression/test_spearman_correlation.py
@Prathamesh8989 Prathamesh8989 force-pushed the native-spearman-metric branch from b9f1ba1 to 206d5ac Compare March 27, 2026 16:18
@Prathamesh8989
Copy link
Copy Markdown
Contributor Author

Hi @TahaZahid05,

Thanks for the suggestions!

I've made the requested updates:

Switched the rank calculation to use .double() for improved numerical stability.
Added tests to ensure the metric returns NaN when inputs contain NaN values.
Added a test for constant inputs.
Added a test verifying the average rank calculation using [10, 20, 20, 30].

Please let me know if any further adjustments are needed.

Copy link
Copy Markdown
Collaborator

@TahaZahid05 TahaZahid05 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one last change, else everything else is good!

Comment thread ignite/metrics/regression/spearman_correlation.py
@Prathamesh8989 Prathamesh8989 force-pushed the native-spearman-metric branch from 206d5ac to a7ff5b3 Compare March 27, 2026 17:06
@Prathamesh8989
Copy link
Copy Markdown
Contributor Author

Prathamesh8989 commented Mar 27, 2026

@TahaZahid05,
Thanks for the suggestion! I've added the versionchanged:: 0.5.5 entry in the docstring to document the switch to the native PyTorch implementation and removal of the SciPy dependency.

Please let me know if anything else needs adjustment.

Copy link
Copy Markdown
Collaborator

@TahaZahid05 TahaZahid05 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes @Prathamesh8989. @vfdev-5 Looks good to me!

@TahaZahid05
Copy link
Copy Markdown
Collaborator

@Prathamesh8989, style checks are failing. Kindly fix them.

@Prathamesh8989
Copy link
Copy Markdown
Contributor Author

yeah sure , fixing them.

Comment thread requirements-dev.txt Outdated
# Tests
dill
filelock
packaging
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we add packaging here?
We already have it as ignite depedency here:

"packaging"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it gave me an error saying that the tests failed because of not having this , so tried to add this in requirements.txt

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove it

Removed 'packaging' from development requirements.
@TahaZahid05
Copy link
Copy Markdown
Collaborator

@Prathamesh8989 can you please verify your pre-commit installation? The code style checks are still failing. You may take a look at CONTRIBUTING.MD for more reference.

@Prathamesh8989
Copy link
Copy Markdown
Contributor Author

sure, i will get through it by tomorrow.

@Prathamesh8989 Prathamesh8989 force-pushed the native-spearman-metric branch from 983c626 to 07bd89f Compare April 16, 2026 17:12
@github-actions github-actions Bot added the examples Examples label Apr 16, 2026
@TahaZahid05
Copy link
Copy Markdown
Collaborator

@Prathamesh8989 it seems you have pushed unwanted changes when fixing the code style checks. All files in examples are also coming under file changed. Can you fix it?

@Prathamesh8989
Copy link
Copy Markdown
Contributor Author

Hey @TahaZahid05,

I ran pre-commit run --all-files, then staged, committed, and pushed the files. I believe that should be the correct workflow, but I might be missing something. Could you please guide me a bit on this? I haven’t yet figured out the proper way to verify it.

Also, I’ll be having my practical and theory exams from 22nd to 30th April, so my response time might be a bit slow during that period. Kindly excuse the delay in advance.

@Prathamesh8989 Prathamesh8989 force-pushed the native-spearman-metric branch from 07bd89f to 983c626 Compare April 20, 2026 16:01
@TahaZahid05
Copy link
Copy Markdown
Collaborator

TahaZahid05 commented Apr 21, 2026

@Prathamesh8989 Run the following set of commands:

pip install -U pre-commit 
pre-commit clean           
pre-commit install         
pre-commit run --all-files 

You'll see in your terminal:

nb-clean.................................................................Passed
check toml...............................................................Passed
check yaml...............................................................Passed
fix end of files.........................................................Passed
trim trailing whitespace.................................................Passed
prettier.................................................................Passed
ruff check...............................................................Passed
ruff format..............................................................Failed
- hook id: ruff-format
- files were modified by this hook

1 file reformatted, 364 files left unchanged

Then run git add . again to stage the change pre-commit made, and then commit and push. Follow along these steps and let me know where your output is deviating or if you get stuck somewhere, I can help you out from there. You can contact me on Discord if you need further help

@vfdev-5
Copy link
Copy Markdown
Collaborator

vfdev-5 commented Apr 21, 2026

@Prathamesh8989 the failure on MPS is real: https://github.com/pytorch/ignite/actions/runs/24736092143/job/72390524588?pr=3672
MPS does not have f64.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

examples Examples module: metrics Metrics module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Enhancement] Native PyTorch Implementation for SpearmanRankCorrelation

3 participants