Optimize distance calculations using SciPy C implementations and improved vectorization #26
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR addresses the performance optimization request by implementing faster distance calculations throughout DistClassiPy, leveraging SciPy's optimized C implementations where possible and improving vectorization for custom metrics.
Problem Statement
The original issue asked: "Can an implementation like SciPy's optimized C version be possible for all metrics defined in DistClassiPy?" when using SciPy's spatial distance with a metric passed as a string utilizes an optimized C version.
Solution Overview
Yes, this is now partially achieved! This implementation provides significant speed improvements by:
Changes Made
🚀 SciPy Integration (10 metrics now fully optimized)
squared_euclidean→scipy.spatial.distance.sqeuclideanjensenshannon_divergence→scipy.spatial.distance.jensenshannon(with transformation)initialize_metric_function()to prefer SciPy implementations⚡ Custom Metric Optimizations (5 metrics improved)
hellinger: Better vectorization, eliminated unnecessary error state handlingclark: Efficient zero handling with boolean indexing instead ofnansumlorentzian: Uselog1pfor better numerical accuracy and performancesoergel: Streamlined operations by pre-computing intermediate arrayswave_hedges: More efficient zero handling with boolean maskingPerformance Results
Before vs After Comparison
Verification
✅ All optimizations maintain numerical accuracy
✅ Existing functionality preserved
✅ Zero handling and edge cases verified
Usage Example
Impact
This successfully demonstrates that SciPy-like optimized implementations are achievable for DistClassiPy metrics, providing substantial performance gains while maintaining full backward compatibility.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.