diff --git a/RELEASES.md b/RELEASES.md index 640c23360..471bba075 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -25,6 +25,7 @@ - Fix `ot.gaussian.bures_wasserstein_distance(..., paired=True)` returning incorrectly broadcast distances or raising an error for unequal batch sizes, instead of returning all crossed distances as documented. (PR #909) - Fix dead links in the README (three example gallery pages and the reference [39]), the user guide and the contributing guide (PR #928) - Fix ordinary JAX gradients for dense `ot.emd2` and preserve the magnitude and higher-order dependence of gradients supplied to `JaxBackend.set_gradients`, including the envelope gradients in `ot.solve` (Issue #625) +- Fix oversized `max_nz` budgets in `ot.utils.projection_sparse_simplex` so they return the documented unconstrained simplex projection for all axis modes (PR #876, Issue #875). - Fix `ot.semidiscrete_wasserstein2_unif_circle` pairing sorted sample locations with unsorted weights, which broke permutation and rotation invariance for nonuniform weights (Issue #921) - Fix an out-of-bounds read in `ot.bsp.merge_bijections`: the number of plans was taken from `perms.shape[1]` (the number of samples) instead of `perms.shape[0]`, so merging fewer plans than samples read past the array and merging more silently dropped plans. A `perms` outside the documented `(n_plans, n_samples)` layout, an empty plan stack and rows that are not permutations are now rejected (PR #911, Issue #910) - Fix the misleading error message of `ot.solve_sample` with `lazy=True`, `reg>0` and `unbalanced`, which blamed the regularization while the limitation comes from the lazy solver (PR #941) diff --git a/ot/utils.py b/ot/utils.py index 69a4854b5..65f2b6e67 100644 --- a/ot/utils.py +++ b/ot/utils.py @@ -193,6 +193,8 @@ def projection_sparse_simplex(V, max_nz, z=1, axis=None, nx=None): raise ValueError("V.ndim must be <= 2") if axis == 1: + if max_nz >= V.shape[1]: + return proj_simplex(V.T, z).T # For each row of V, find top max_nz values; arrange the # corresponding column indices such that their values are # in a descending order. diff --git a/test/test_utils.py b/test/test_utils.py index 9c93d520a..c4593dd28 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -142,6 +142,45 @@ def double_sort_projection_sparse_simplex(X, max_nz, z=1, axis=None): np.testing.assert_allclose(slow_sparse_proj, fast_sparse_proj) +@pytest.mark.parametrize("axis", [None, 0, 1]) +@pytest.mark.parametrize("extra_budget", [0, 2]) +def test_projection_sparse_simplex_unconstrained_budget(nx, axis, extra_budget): + values = np.array([[0.8, 0.1, -1.0], [0.4, 0.3, 0.2]]) + z = 0.5 + if axis is None: + dimension = values.size + expected = np.array([0.45, 0.0, 0.0, 0.05, 0.0, 0.0]) + elif axis == 0: + dimension = values.shape[0] + expected = np.array([[0.45, 0.15, 0.0], [0.05, 0.35, 0.5]]) + else: + dimension = values.shape[1] + expected = np.array([[0.5, 0.0, 0.0], [4 / 15, 1 / 6, 1 / 15]]) + result = ot.utils.projection_sparse_simplex( + nx.from_numpy(values), dimension + extra_budget, z=z, axis=axis + ) + np.testing.assert_allclose(nx.to_numpy(result), expected, atol=1e-12) + + +@pytest.mark.parametrize("axis", [0, 1]) +@pytest.mark.parametrize("extra_budget", [0, 2]) +def test_projection_sparse_simplex_unconstrained_vector_mass(nx, axis, extra_budget): + values = np.array([[0.8, 0.1, -1.0], [0.4, 0.3, 0.2]]) + if axis == 0: + mass = np.array([0.5, 1.0, 0.2]) + expected = np.array([[0.45, 0.4, 0.0], [0.05, 0.6, 0.2]]) + else: + mass = np.array([0.5, 1.0]) + expected = np.array([[0.5, 0.0, 0.0], [13 / 30, 1 / 3, 7 / 30]]) + result = ot.utils.projection_sparse_simplex( + nx.from_numpy(values), + values.shape[axis] + extra_budget, + z=nx.from_numpy(mass), + axis=axis, + ) + np.testing.assert_allclose(nx.to_numpy(result), expected, atol=1e-12) + + def test_parmap(): n = 10