Skip to content

Conversation

@nathanneike
Copy link
Contributor

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • Improvement (non-breaking change which improves the codebase)

Motivation and context / Related issue

Modernizes the POT backend by migrating from the deprecated scipy.sparse.coo_matrix to the modern scipy.sparse.coo_array API. This ensures future compatibility as SciPy moves away from matrix-based sparse classes in favor of array-based ones.

How has this been tested (if it applies)

  • Updated NumPy backend coo_matrix() method to return coo_array instead of coo_matrix
  • Updated sparse_coo_data() to handle both coo_array and coo_matrix for backward compatibility
  • Added test case verifying coo_array element-wise multiplication functionality
  • All 2972 tests pass successfully

PR checklist

  • I have read the CONTRIBUTING document.
  • The documentation is up-to-date with the changes I made (check build artifacts).
  • All tests passed, and additional code has been covered with new tests.
  • I have added the PR and Issue fix to the RELEASES.md file.

nathanneike and others added 17 commits October 28, 2025 16:01
  - Implement sparse bipartite graph EMD solver in C++
  - Add Python bindings for sparse solver (emd_wrap.pyx, _network_simplex.py)
  - Add unit tests to verify sparse and dense solvers produce identical results
  - Tests use augmented k-NN approach to ensure fair comparison
  - Update setup.py to include sparse solver compilation

  Both test_emd_sparse_vs_dense() and test_emd2_sparse_vs_dense() verify:
    * Identical costs between sparse and dense solvers
    * Marginal constraint satisfaction for both solvers
  This PR implements a sparse bipartite graph EMD solver for memory-efficient
  optimal transport when the cost matrix has many infinite or forbidden edges.

  Changes:
  - Implement sparse bipartite graph EMD solver in C++
  - Add Python bindings for sparse solver (emd_wrap.pyx, _network_simplex.py)
  - Add unit tests to verify sparse and dense solvers produce identical results
  - Tests use augmented k-NN approach to ensure fair comparison

  Tests verify correctness:
    * test_emd_sparse_vs_dense() - verifies identical costs and marginal constraints
    * test_emd2_sparse_vs_dense() - verifies cost-only version

  Status: WIP - seeking feedback on implementation approach
  TODO: Add example script and documentation
…trix parameter from emd and fix linting issues
- Remove tuple format support for sparse matrices (use scipy.sparse only)
- Change index types from int64_t to uint64_t throughout (indices are never negative)
- Refactor emd() and emd2() with clear sparse/dense code path separation
- Add sparse_bipartitegraph.h to MANIFEST.in to fix build
- Add test_emd_sparse_backends() to verify backend compatibility
Refactor sparse optimal transport implementation to work across
different backends (NumPy/scipy.sparse, PyTorch/torch.sparse).

Key changes:
- Add `sparse_coo_data()` method to backend layer for uniform sparse
  matrix handling across NumPy, PyTorch, JAX, and TensorFlow backends
- Update `emd()` and `emd2()` to return transport plans in backend-native
  sparse format (scipy.sparse for NumPy, torch.sparse for PyTorch)
- Refactor `plot2D_samples_mat()` to efficiently visualize both dense
  and sparse transport plans by detecting format and iterating only
  over non-zero entries for sparse matrices
- Update `plot_sparse_emd.py` example to use new plotting function
- Add comprehensive tests for sparse EMD across backends
- Update documentation to reflect backend-agnostic sparse support
- Preserve PyTorch sparse tensors through numpy conversion for autograd
- Verify gradient w.r.t. M equals transport plan
- Add sparse backend compatibility checks and teststhrow error when unsupported backend used for sparse"
- Use sklearn.NearestNeighbors in dist_knn() (1.4x faster)
- Remove redundant test code (~50 lines)
- Migrate coo_matrix → coo_array
- Fix parameter ordering consistency
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants