Refactor SpearmanRankCorrelation to native PyTorch and remove SciPy dependency
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_rankshelper function that calculates ranks using the "average" method for ties, ensuring parity withscipy.stats.spearmanr. - Logic Update: Replaced the SciPy-based
_spearman_rfunction 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)
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.
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.
@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, style checks are failing. Kindly fix them.
yeah sure , fixing them.
@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.
sure, i will get through it by tomorrow.
@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?
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 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
@Prathamesh8989 the failure on MPS is real: https://github.com/pytorch/ignite/actions/runs/24736092143/job/72390524588?pr=3672 MPS does not have f64.
@Prathamesh8989 hi! are you still working on this?