bundles / scipy 1.17.1 / scipy / _lib / _util / _transition_to_rng
function
scipy._lib._util:_transition_to_rng
source: /scipy/_lib/_util.py :201
Signature
def _transition_to_rng ( old_name , * , position_num = None , end_version = None , replace_doc = True ) Summary
Example decorator to transition from old PRNG usage to new rng behavior
Extended Summary
Suppose the decorator is applied to a function that used to accept parameter old_name='random_state' either by keyword or as a positional argument at position_num=1. At the time of application, the name of the argument in the function signature is manually changed to the new name, rng. If positional use was allowed before, this is not changed.*
If the function is called with both
random_stateandrng, the decorator raises an error.If
random_stateis provided as a keyword argument, the decorator passesrandom_stateto the function'srngargument as a keyword. Ifend_versionis specified, the decorator will emit aDeprecationWarningabout the deprecation of keywordrandom_state.If
random_stateis provided as a positional argument, the decorator passesrandom_stateto the function'srngargument by position. Ifend_versionis specified, the decorator will emit aFutureWarningabout the changing interpretation of the argument.If
rngis provided as a keyword argument, the decorator validatesrngusing numpy.random.default_rng before passing it to the function.If
end_versionis specified and neitherrandom_statenorrngis provided by the user, the decorator checks whethernp.random.seedhas been used to set the global seed. If so, it emits aFutureWarning, noting that usage of numpy.random.seed will eventually have no effect. Either way, the decorator calls the function without explicitly passing therngargument.
If end_version is specified, a user must pass rng as a keyword to avoid warnings.
After the deprecation period, the decorator can be removed, and the function can simply validate the rng argument by calling np.random.default_rng(rng).
A
FutureWarningis emitted when the PRNG argument is used by position. It indicates that the "Hinsen principle" (same code yielding different results in two versions of the software) will be violated, unless positional use is deprecated. Specifically:If
Noneis passed by position andnp.random.seedhas been used, the function will change from being seeded to being unseeded.If an integer is passed by position, the random stream will change.
If
np.randomor an instance ofRandomStateis passed by position, an error will be raised.
We suggest that projects consider deprecating positional use of
random_state/rng(i.e., change their function signatures todef my_func(..., *, rng=None)); that might not make sense for all projects, so this SPEC does not make that recommendation, neither does this decorator enforce it.
Parameters
old_name: strThe old name of the PRNG argument (e.g.
seedorrandom_state).position_num: int, optionalThe (0-indexed) position of the old PRNG argument (if accepted by position). Maintainers are welcome to eliminate this argument and use, for example, inspect, if preferred.
end_version: str, optionalThe full version number of the library when the behavior described in `DeprecationWarning`s and `FutureWarning`s will take effect. If left unspecified, no warnings will be emitted by the decorator.
replace_doc: bool, default: TrueWhether the decorator should replace the documentation for parameter
rngwith_rng_desc(defined above), which documents both newrngkeyword behavior and typical legacyrandom_state/seedbehavior. If True, manually replace the first paragraph of the function's oldrandom_state/seeddocumentation with the desired finalrngdocumentation; this way, no changes to documentation are needed when the decorator is removed. Documentation ofrngafter the first blank line is preserved. Use False if the function's oldrandom_state/seedbehavior does not match that described by_rng_desc.
Aliases
-
scipy.cluster.vq._transition_to_rng