ignite icon indicating copy to clipboard operation
ignite copied to clipboard

Refactor SpearmanRankCorrelation to native PyTorch and remove SciPy dependency

Open Prathamesh8989 opened this issue 8 months ago • 12 comments

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:

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

Prathamesh8989 avatar Mar 15 '26 13:03 Prathamesh8989

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.

Prathamesh8989 avatar Mar 25 '26 22:03 Prathamesh8989

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.

Prathamesh8989 avatar Mar 27 '26 16:03 Prathamesh8989

@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.

Prathamesh8989 avatar Mar 27 '26 17:03 Prathamesh8989

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

TahaZahid05 avatar Apr 07 '26 04:04 TahaZahid05

yeah sure , fixing them.

Prathamesh8989 avatar Apr 08 '26 15:04 Prathamesh8989

@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.

TahaZahid05 avatar Apr 15 '26 14:04 TahaZahid05

sure, i will get through it by tomorrow.

Prathamesh8989 avatar Apr 15 '26 17:04 Prathamesh8989

@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?

TahaZahid05 avatar Apr 17 '26 16:04 TahaZahid05

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 avatar Apr 18 '26 19:04 Prathamesh8989

@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

TahaZahid05 avatar Apr 21 '26 16:04 TahaZahid05

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

vfdev-5 avatar Apr 21 '26 21:04 vfdev-5

@Prathamesh8989 hi! are you still working on this?

TahaZahid05 avatar May 10 '26 12:05 TahaZahid05