4
4
import numpy as np
5
5
from itertools import combinations , permutations , product
6
6
from collections .abc import Sequence
7
- from dataclasses import dataclass
7
+ from dataclasses import dataclass , field
8
8
import inspect
9
9
10
10
from scipy ._lib ._util import (check_random_state , _rename_parameter , rng_integers ,
@@ -2167,6 +2167,20 @@ def _asdict(self):
2167
2167
rvs = self .rvs )
2168
2168
2169
2169
2170
+ _rs_deprecation = ("Use of attribute `random_state` is deprecated and replaced by "
2171
+ "`rng`. Support for `random_state` will be removed in SciPy 1.19.0. "
2172
+ "To silence this warning and ensure consistent behavior in SciPy "
2173
+ "1.19.0, control the RNG using attribute `rng`. Values set using "
2174
+ "attribute `rng` will be validated by `np.random.default_rng`, so "
2175
+ "the behavior corresponding with a given value may change compared "
2176
+ "to use of `random_state`. For example, 1) `None` will result in "
2177
+ "unpredictable random numbers, 2) an integer will result in a "
2178
+ "different stream of random numbers, (with the same distribution), "
2179
+ "and 3) `np.random` or `RandomState` instances will result in an "
2180
+ "error. See the documentation of `default_rng` for more "
2181
+ "information." )
2182
+
2183
+
2170
2184
@dataclass
2171
2185
class PermutationMethod (ResamplingMethod ):
2172
2186
"""Configuration information for a permutation hypothesis test.
@@ -2184,24 +2198,69 @@ class PermutationMethod(ResamplingMethod):
2184
2198
the statistic. Batch sizes >>1 tend to be faster when the statistic
2185
2199
is vectorized, but memory usage scales linearly with the batch size.
2186
2200
Default is ``None``, which processes all resamples in a single batch.
2187
- random_state : {None, int, `numpy.random.Generator`,
2188
- `numpy.random.RandomState`}, optional
2189
-
2190
- Pseudorandom number generator state used to generate resamples.
2201
+ rng : `numpy.random.Generator`, optional
2202
+ Pseudorandom number generator used to perform resampling.
2203
+
2204
+ If `rng` is passed by keyword to the initializer or the `rng` attribute is used
2205
+ directly, types other than `numpy.random.Generator` are passed to
2206
+ `numpy.random.default_rng` to instantiate a ``Generator``.
2207
+ If `rng` is already a ``Generator`` instance, then the provided instance is
2208
+ used. Specify `rng` for repeatable behavior.
2209
+
2210
+ If this argument is passed by position, if `random_state` is passed by keyword
2211
+ into the initializer, or if the `random_state` attribute is used directly,
2212
+ legacy behavior for `random_state` applies:
2213
+
2214
+ - If `random_state` is None (or `numpy.random`), the `numpy.random.RandomState`
2215
+ singleton is used.
2216
+ - If `random_state` is an int, a new ``RandomState`` instance is used,
2217
+ seeded with `random_state`.
2218
+ - If `random_state` is already a ``Generator`` or ``RandomState`` instance then
2219
+ that instance is used.
2220
+
2221
+ .. versionchanged:: 1.15.0
2222
+
2223
+ As part of the `SPEC-007 <https://scientific-python.org/specs/spec-0007/>`_
2224
+ transition from use of `numpy.random.RandomState` to
2225
+ `numpy.random.Generator`, this attribute name was changed from
2226
+ `random_state` to `rng`. For an interim period, both names will continue to
2227
+ work, although only one may be specified at a time. After the interim
2228
+ period, uses of `random_state` will emit warnings. The behavior of both
2229
+ `random_state` and `rng` are outlined above, but only `rng` should be used
2230
+ in new code.
2191
2231
2192
- If `random_state` is already a ``Generator`` or ``RandomState``
2193
- instance, then that instance is used.
2194
- If `random_state` is an int, a new ``RandomState`` instance is used,
2195
- seeded with `random_state`.
2196
- If `random_state` is ``None`` (default), the
2197
- `numpy.random.RandomState` singleton is used.
2198
2232
"""
2199
- random_state : object = None
2233
+ rng : object # type: ignore[misc]
2234
+ _rng : object = field (init = False , repr = False , default = None ) # type: ignore[assignment]
2235
+
2236
+ @property
2237
+ def random_state (self ):
2238
+ # Uncomment in SciPy 1.17.0
2239
+ # warnings.warn(_rs_deprecation, DeprecationWarning, stacklevel=2)
2240
+ return self ._rng
2241
+
2242
+ @random_state .setter
2243
+ def random_state (self , val ):
2244
+ # Uncomment in SciPy 1.17.0
2245
+ # warnings.warn(_rs_deprecation, DeprecationWarning, stacklevel=2)
2246
+ self ._rng = val
2247
+
2248
+ @property # type: ignore[no-redef]
2249
+ def rng (self ): # noqa: F811
2250
+ return self ._rng
2251
+
2252
+ @random_state .setter
2253
+ def rng (self , val ): # noqa: F811
2254
+ self ._rng = np .random .default_rng (val )
2255
+
2256
+ @_transition_to_rng ('random_state' , position_num = 3 , replace_doc = False )
2257
+ def __init__ (self , n_resamples = 9999 , batch = None , rng = None ):
2258
+ self ._rng = rng # don't validate with `default_rng` during SPEC 7 transition
2259
+ super ().__init__ (n_resamples = n_resamples , batch = batch )
2200
2260
2201
2261
def _asdict (self ):
2202
2262
# `dataclasses.asdict` deepcopies; we don't want that.
2203
- return dict (n_resamples = self .n_resamples , batch = self .batch ,
2204
- random_state = self .random_state )
2263
+ return dict (n_resamples = self .n_resamples , batch = self .batch , rng = self .rng )
2205
2264
2206
2265
2207
2266
@dataclass
@@ -2220,26 +2279,73 @@ class BootstrapMethod(ResamplingMethod):
2220
2279
the statistic. Batch sizes >>1 tend to be faster when the statistic
2221
2280
is vectorized, but memory usage scales linearly with the batch size.
2222
2281
Default is ``None``, which processes all resamples in a single batch.
2223
- random_state : {None, int, `numpy.random.Generator`,
2224
- `numpy.random.RandomState`}, optional
2225
-
2226
- Pseudorandom number generator state used to generate resamples.
2227
-
2228
- If `random_state` is already a ``Generator`` or ``RandomState``
2229
- instance, then that instance is used.
2230
- If `random_state` is an int, a new ``RandomState`` instance is used,
2231
- seeded with `random_state`.
2232
- If `random_state` is ``None`` (default), the
2233
- `numpy.random.RandomState` singleton is used.
2234
-
2235
- method : {'bca', 'percentile', 'basic'}
2282
+ rng : `numpy.random.Generator`, optional
2283
+ Pseudorandom number generator used to perform resampling.
2284
+
2285
+ If `rng` is passed by keyword to the initializer or the `rng` attribute is used
2286
+ directly, types other than `numpy.random.Generator` are passed to
2287
+ `numpy.random.default_rng` to instantiate a ``Generator``.
2288
+ If `rng` is already a ``Generator`` instance, then the provided instance is
2289
+ used. Specify `rng` for repeatable behavior.
2290
+
2291
+ If this argument is passed by position, if `random_state` is passed by keyword
2292
+ into the initializer, or if the `random_state` attribute is used directly,
2293
+ legacy behavior for `random_state` applies:
2294
+
2295
+ - If `random_state` is None (or `numpy.random`), the `numpy.random.RandomState`
2296
+ singleton is used.
2297
+ - If `random_state` is an int, a new ``RandomState`` instance is used,
2298
+ seeded with `random_state`.
2299
+ - If `random_state` is already a ``Generator`` or ``RandomState`` instance then
2300
+ that instance is used.
2301
+
2302
+ .. versionchanged:: 1.15.0
2303
+
2304
+ As part of the `SPEC-007 <https://scientific-python.org/specs/spec-0007/>`_
2305
+ transition from use of `numpy.random.RandomState` to
2306
+ `numpy.random.Generator`, this attribute name was changed from
2307
+ `random_state` to `rng`. For an interim period, both names will continue to
2308
+ work, although only one may be specified at a time. After the interim
2309
+ period, uses of `random_state` will emit warnings. The behavior of both
2310
+ `random_state` and `rng` are outlined above, but only `rng` should be used
2311
+ in new code.
2312
+
2313
+ method : {'BCa', 'percentile', 'basic'}
2236
2314
Whether to use the 'percentile' bootstrap ('percentile'), the 'basic'
2237
2315
(AKA 'reverse') bootstrap ('basic'), or the bias-corrected and
2238
2316
accelerated bootstrap ('BCa', default).
2317
+
2239
2318
"""
2240
- random_state : object = None
2319
+ rng : object # type: ignore[misc]
2320
+ _rng : object = field (init = False , repr = False , default = None ) # type: ignore[assignment]
2241
2321
method : str = 'BCa'
2242
2322
2323
+ @property
2324
+ def random_state (self ):
2325
+ # Uncomment in SciPy 1.17.0
2326
+ # warnings.warn(_rs_deprecation, DeprecationWarning, stacklevel=2)
2327
+ return self ._rng
2328
+
2329
+ @random_state .setter
2330
+ def random_state (self , val ):
2331
+ # Uncomment in SciPy 1.17.0
2332
+ # warnings.warn(_rs_deprecation, DeprecationWarning, stacklevel=2)
2333
+ self ._rng = val
2334
+
2335
+ @property # type: ignore[no-redef]
2336
+ def rng (self ): # noqa: F811
2337
+ return self ._rng
2338
+
2339
+ @random_state .setter
2340
+ def rng (self , val ): # noqa: F811
2341
+ self ._rng = np .random .default_rng (val )
2342
+
2343
+ @_transition_to_rng ('random_state' , position_num = 3 , replace_doc = False )
2344
+ def __init__ (self , n_resamples = 9999 , batch = None , rng = None , method = 'BCa' ):
2345
+ self ._rng = rng # don't validate with `default_rng` during SPEC 7 transition
2346
+ self .method = method
2347
+ super ().__init__ (n_resamples = n_resamples , batch = batch )
2348
+
2243
2349
def _asdict (self ):
2244
2350
# `dataclasses.asdict` deepcopies; we don't want that.
2245
2351
return dict (n_resamples = self .n_resamples , batch = self .batch ,
0 commit comments