Skip to content

Commit 4372e3c

Browse files
committed
remove code duplication
1 parent 58350c8 commit 4372e3c

1 file changed

Lines changed: 14 additions & 18 deletions

File tree

openml/runs/functions.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,18 @@ def _get_seeded_model(model, seed=None):
258258
a seed
259259
'''
260260

261+
def _seed_current_object(current_value):
262+
if isinstance(current_value, int): # acceptable behaviour
263+
return False
264+
elif isinstance(current_value, np.random.RandomState):
265+
raise ValueError(
266+
'Models initialized with a RandomState object are not supported. Please seed with an integer. ')
267+
elif current_value is not None:
268+
raise ValueError(
269+
'Models should be seeded with int or None (this should never happen). ')
270+
else:
271+
return True
272+
261273
rs = np.random.RandomState(seed)
262274
model_params = model.get_params()
263275
random_states = {}
@@ -268,15 +280,8 @@ def _get_seeded_model(model, seed=None):
268280
# this way we guarantee that if a different set of subflows is seeded,
269281
# the same number of the random generator is used
270282
newValue = rs.randint(0, 2**16)
271-
if currentValue is None:
283+
if _seed_current_object(currentValue):
272284
random_states[param_name] = newValue
273-
elif isinstance(currentValue, int):
274-
# acceptable behaviour
275-
pass
276-
elif isinstance(currentValue, np.random.RandomState):
277-
raise ValueError('Models initialized with a RandomState object are not supported. Please seed with an integer. ')
278-
else:
279-
raise ValueError('Models should be seeded with int or None (this should never happen). ')
280285

281286
# Also seed CV objects!
282287
elif isinstance(model_params[param_name],
@@ -286,17 +291,8 @@ def _get_seeded_model(model, seed=None):
286291

287292
currentValue = model_params[param_name].random_state
288293
newValue = rs.randint(0, 2 ** 16)
289-
if currentValue is None:
294+
if _seed_current_object(currentValue):
290295
model_params[param_name].random_state = newValue
291-
elif isinstance(currentValue, int):
292-
# acceptable behaviour
293-
pass
294-
elif isinstance(currentValue, np.random.RandomState):
295-
raise ValueError(
296-
'Models initialized with a RandomState object are not supported. Please seed with an integer. ')
297-
else:
298-
raise ValueError(
299-
'Models should be seeded with int or None (this should never happen). ')
300296

301297
model.set_params(**random_states)
302298
return model

0 commit comments

Comments
 (0)