diff --git a/RELEASES.md b/RELEASES.md index 03093ac6f..d7d0d2ffb 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -19,6 +19,7 @@ #### Closed issues - 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 `ot.utils.split_sample_ratio` at `ratio=1` and handle `ratio=0` without scalar backend search, returning full/empty distributions while preserving random permutations and selectors (PR #878, Issue #877). - 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 overflow to NaN in `ot.stochastic.coordinate_grad_semi_dual` by shifting before exponentiating, as `ot.stochastic.c_transform_entropic` already does. The factor cancels in the normalisation, so results are unchanged where the old code did not overflow (PR #866, Issue #264) diff --git a/ot/utils.py b/ot/utils.py index 69a4854b5..4056fd782 100644 --- a/ot/utils.py +++ b/ot/utils.py @@ -2157,6 +2157,16 @@ def split_sample_ratio( X_a = X_a[perm] a = a[perm] + if ratio == 1: + sel_a1 = perm if random_split else slice(0, n_a) + sel_a2 = perm[n_a:] if random_split else slice(n_a, n_a) + return X_a, X_a[:0], a, a[:0], sel_a1, sel_a2 + + if ratio == 0: + sel_a1 = perm[:0] if random_split else slice(0, 0) + sel_a2 = perm if random_split else slice(0, n_a) + return X_a[:0], X_a, a[:0], a, sel_a1, sel_a2 + # find the split indices acs = nx.cumsum(a) thr_a = ratio * nx.sum(a) diff --git a/test/test_utils.py b/test/test_utils.py index 9c93d520a..56ece83b2 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -1046,3 +1046,26 @@ def test_split_sample_ratio(nx, ratio, n, random): ot.utils.split_sample_ratio(X, ratio=1.5, a=a, random_state=seed, nx=nx) # + + +@pytest.mark.parametrize("ratio", [0, 1]) +@pytest.mark.parametrize("random", [False, True]) +@pytest.mark.parametrize("weighted", [False, True]) +def test_split_sample_ratio_endpoints(nx, ratio, random, weighted): + values = np.arange(6, dtype=float).reshape(3, 2) + weights = np.array([0.2, 0.0, 0.8]) if weighted else np.full(3, 1 / 3) + X = nx.from_numpy(values) + a = nx.from_numpy(weights) if weighted else None + X1, X2, a1, a2, id1, id2 = ot.utils.split_sample_ratio( + X, a=a, ratio=ratio, random_split=random, random_state=42 + ) + full_X, full_a, full_id = (X1, a1, id1) if ratio == 1 else (X2, a2, id2) + empty_X, empty_a = (X2, a2) if ratio == 1 else (X1, a1) + np.testing.assert_array_equal(nx.to_numpy(full_X), nx.to_numpy(X[full_id])) + order = np.argsort(nx.to_numpy(full_X)[:, 0]) + np.testing.assert_array_equal(nx.to_numpy(full_X)[order], values) + np.testing.assert_allclose(nx.to_numpy(full_a)[order], weights) + assert empty_X.shape == (0, 2) + assert empty_a.shape == (0,) + empty_id = id2 if ratio == 1 else id1 + assert X[empty_id].shape == empty_X.shape