Skip to content

Commit abf159d

Browse files
authored
Merge pull request scipy#21913 from mdhaber/directed_hausdorff_spec7
API: `spatial.directed_hausdorff`: transition to 'rng' keyword (SPEC 7)
2 parents 646e5f8 + 01c96ea commit abf159d

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

scipy/spatial/distance.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112

113113
from collections.abc import Callable
114114
from functools import partial
115-
from scipy._lib._util import _asarray_validated
115+
from scipy._lib._util import _asarray_validated, _transition_to_rng
116116
from scipy._lib.deprecation import _deprecated
117117

118118
from . import _distance_wrap
@@ -309,7 +309,8 @@ def _validate_weights(w, dtype=np.float64):
309309
return w
310310

311311

312-
def directed_hausdorff(u, v, seed=0):
312+
@_transition_to_rng('seed', position_num=2, replace_doc=False)
313+
def directed_hausdorff(u, v, rng=0):
313314
"""
314315
Compute the directed Hausdorff distance between two 2-D arrays.
315316
@@ -321,10 +322,35 @@ def directed_hausdorff(u, v, seed=0):
321322
Input array with M points in N dimensions.
322323
v : (O,N) array_like
323324
Input array with O points in N dimensions.
324-
seed : int or `numpy.random.Generator` or None, optional
325-
Local `numpy.random.RandomState` seed. Default is 0, a random
326-
shuffling of u and v that guarantees reproducibility. Also
327-
accepts a `numpy.random.Generator` object.
325+
rng : int or `numpy.random.Generator` or None, optional
326+
Pseudorandom number generator state. Default is 0 so the
327+
shuffling of `u` and `v` is reproducible.
328+
329+
If `rng` is passed by keyword, types other than `numpy.random.Generator` are
330+
passed to `numpy.random.default_rng` to instantiate a ``Generator``.
331+
If `rng` is already a ``Generator`` instance, then the provided instance is
332+
used.
333+
334+
If this argument is passed by position or `seed` is passed by keyword,
335+
legacy behavior for the argument `seed` applies:
336+
337+
- If `seed` is None, a new ``RandomState`` instance is used. The state is
338+
initialized using data from ``/dev/urandom`` (or the Windows analogue)
339+
if available or from the system clock otherwise.
340+
- If `seed` is an int, a new ``RandomState`` instance is used,
341+
seeded with `seed`.
342+
- If `seed` is already a ``Generator`` or ``RandomState`` instance, then
343+
that instance is used.
344+
345+
.. versionchanged:: 1.15.0
346+
As part of the `SPEC-007 <https://scientific-python.org/specs/spec-0007/>`_
347+
transition from use of `numpy.random.RandomState` to
348+
`numpy.random.Generator`, this keyword was changed from `seed` to `rng`.
349+
For an interim period, both keywords will continue to work, although only
350+
one may be specified at a time. After the interim period, function calls
351+
using the `seed` keyword will emit warnings. The behavior of both `seed`
352+
and `rng` are outlined above, but only the `rng` keyword should be used in
353+
new code.
328354
329355
Returns
330356
-------
@@ -407,7 +433,7 @@ def directed_hausdorff(u, v, seed=0):
407433
if u.shape[1] != v.shape[1]:
408434
raise ValueError('u and v need to have the same '
409435
'number of columns')
410-
result = _hausdorff.directed_hausdorff(u, v, seed)
436+
result = _hausdorff.directed_hausdorff(u, v, rng)
411437
return result
412438

413439

scipy/spatial/tests/test_hausdorff.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ def test_invalid_dimensions(self):
123123
with pytest.raises(ValueError, match=msg):
124124
directed_hausdorff(A, B)
125125

126+
# preserve use of legacy keyword `seed` during SPEC 7 transition
126127
@pytest.mark.parametrize("A, B, seed, expected", [
127128
# the two cases from gh-11332
128129
([(0,0)],
@@ -167,6 +168,11 @@ def test_subsets(self, A, B, seed, expected):
167168
# check indices
168169
assert actual[1:] == expected[1:]
169170

171+
if not isinstance(seed, np.random.RandomState):
172+
# Check that new `rng` keyword is also accepted
173+
actual = directed_hausdorff(u=A, v=B, rng=seed)
174+
assert_allclose(actual[0], expected[0])
175+
170176

171177
@pytest.mark.xslow
172178
def test_massive_arr_overflow():

0 commit comments

Comments
 (0)