From 4ff20b83aa5f17c7e5f58a0c9e2f02d0bddcb2b1 Mon Sep 17 00:00:00 2001 From: Kevin Keraudren Date: Sun, 8 May 2016 08:13:01 +0100 Subject: [PATCH] moving check_random_state() to skimage._shared.util --- skimage/_shared/utils.py | 18 ++++++++++++++++++ skimage/measure/fit.py | 20 +------------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/skimage/_shared/utils.py b/skimage/_shared/utils.py index 96bc0fd7..db741814 100644 --- a/skimage/_shared/utils.py +++ b/skimage/_shared/utils.py @@ -3,6 +3,7 @@ import functools import sys import numpy as np import types +import numbers import six @@ -191,3 +192,20 @@ def copy_func(f, name=None): return types.FunctionType(six.get_function_code(f), six.get_function_globals(f), name or f.__name__, six.get_function_defaults(f), six.get_function_closure(f)) + + +def check_random_state(seed): + """Turn seed into a np.random.RandomState instance + If seed is None, return the RandomState singleton used by np.random. + If seed is an int, return a new RandomState instance seeded with seed. + If seed is already a RandomState instance, return it. + Otherwise raise ValueError. + """ + if seed is None or seed is np.random: + return np.random.mtrand._rand + if isinstance(seed, (numbers.Integral, np.integer)): + return np.random.RandomState(seed) + if isinstance(seed, np.random.RandomState): + return seed + raise ValueError('%r cannot be used to seed a numpy.random.RandomState' + ' instance' % seed) diff --git a/skimage/measure/fit.py b/skimage/measure/fit.py index 2b186aa1..7ec96a15 100644 --- a/skimage/measure/fit.py +++ b/skimage/measure/fit.py @@ -1,8 +1,7 @@ import math -import numbers import numpy as np from scipy import optimize -from .._shared.utils import skimage_deprecation, warn +from .._shared.utils import check_random_state, skimage_deprecation, warn def _check_data_dim(data, dim): @@ -688,23 +687,6 @@ def _dynamic_max_trials(n_inliers, n_samples, min_samples, probability): return 0 return int(np.ceil(nom / denom)) - - -def check_random_state(seed): - """Turn seed into a np.random.RandomState instance - If seed is None, return the RandomState singleton used by np.random. - If seed is an int, return a new RandomState instance seeded with seed. - If seed is already a RandomState instance, return it. - Otherwise raise ValueError. - """ - if seed is None or seed is np.random: - return np.random.mtrand._rand - if isinstance(seed, (numbers.Integral, np.integer)): - return np.random.RandomState(seed) - if isinstance(seed, np.random.RandomState): - return seed - raise ValueError('%r cannot be used to seed a numpy.random.RandomState' - ' instance' % seed) def ransac(data, model_class, min_samples, residual_threshold,