Skip to content

Commit 07a91cd

Browse files
committed
🎨 Keep random seed in bounds
1 parent fe139cc commit 07a91cd

File tree

5 files changed

+29
-24
lines changed

5 files changed

+29
-24
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'''Random state for C-PAC'''
2-
from .seed import random_seed, random_seed_flags, set_up_random_state, \
3-
set_up_random_state_logger
2+
from .seed import MAX_SEED, random_seed, random_seed_flags, \
3+
set_up_random_state, set_up_random_state_logger
44

5-
__all__ = ['random_seed', 'random_seed_flags', 'set_up_random_state',
6-
'set_up_random_state_logger']
5+
__all__ = ['MAX_SEED', 'random_seed', 'random_seed_flags',
6+
'set_up_random_state', 'set_up_random_state_logger']

CPAC/pipeline/random_state/seed.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from CPAC.utils.interfaces.ants import AI
3030
from CPAC.utils.monitoring.custom_logging import set_up_logger
3131

32+
MAX_SEED = np.iinfo(np.int32).max
3233
_seed = {'seed': None}
3334

3435

@@ -45,10 +46,10 @@ def random_random_seed():
4546
4647
Examples
4748
--------
48-
>>> 0 < random_random_seed() <= np.iinfo(np.int32).max
49+
>>> 0 < random_random_seed() <= MAX_SEED
4950
True
5051
'''
51-
return random.randint(1, np.iinfo(np.int32).max)
52+
return random.randint(1, MAX_SEED)
5253

5354

5455
def random_seed():
@@ -183,7 +184,7 @@ def set_up_random_state(seed):
183184
seed = random_random_seed()
184185
if (seed != 'random' and not (
185186
isinstance(seed, int) and
186-
(0 < int(seed) <= np.iinfo(np.int32).max)
187+
(0 < int(seed) <= MAX_SEED)
187188
)):
188189
raise ValueError('Valid random seeds are positive integers up to '
189190
f'2147483647, "random", or None, not {seed}')

CPAC/pipeline/schema.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
"""Validation schema for C-PAC pipeline configurations"""
1818
# pylint: disable=too-many-lines
1919
from itertools import chain, permutations
20-
import numpy as np
2120
from voluptuous import All, ALLOW_EXTRA, Any, Capitalize, Coerce, \
2221
ExactSequence, ExclusiveInvalid, In, Length, Lower, \
2322
Match, Maybe, Optional, Range, Required, Schema
2423
from CPAC import docs_prefix
24+
from CPAC.pipeline.random_state.seed import MAX_SEED
2525
from CPAC.utils.datatypes import ListFromItem
2626
from CPAC.utils.utils import delete_nested_value, lookup_nested_value, \
2727
set_nested_value
@@ -406,7 +406,7 @@ def _changes_1_8_0_to_1_8_1(config_dict):
406406
'num_participants_at_once': int,
407407
'random_seed': Maybe(Any(
408408
'random',
409-
All(int, Range(min=1, max=np.iinfo(np.int32).max)))),
409+
All(int, Range(min=1, max=MAX_SEED)))),
410410
'observed_usage': {
411411
'callback_log': Maybe(str),
412412
'buffer': Number,

CPAC/registration/__init__.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
from .registration import create_fsl_flirt_linear_reg, \
2-
create_fsl_fnirt_nonlinear_reg, \
3-
create_fsl_fnirt_nonlinear_reg_nhp, \
4-
create_register_func_to_anat, \
5-
create_register_func_to_anat_use_T2, \
6-
create_bbregister_func_to_anat, \
7-
create_wf_calculate_ants_warp
2+
create_fsl_fnirt_nonlinear_reg, \
3+
create_fsl_fnirt_nonlinear_reg_nhp, \
4+
create_register_func_to_anat, \
5+
create_register_func_to_anat_use_T2, \
6+
create_bbregister_func_to_anat, \
7+
create_wf_calculate_ants_warp
88

99
from .output_func_to_standard import output_func_to_standard
1010

11-
__all__ = ['create_fsl_flirt_linear_reg', \
12-
'create_fsl_fnirt_nonlinear_reg', \
13-
'create_fsl_fnirt_nonlinear_reg_nhp', \
14-
'create_register_func_to_anat', \
15-
'create_register_func_to_anat_use_T2', \
16-
'create_bbregister_func_to_anat', \
17-
'create_wf_calculate_ants_warp']
11+
__all__ = ['create_fsl_flirt_linear_reg',
12+
'create_fsl_fnirt_nonlinear_reg',
13+
'create_fsl_fnirt_nonlinear_reg_nhp',
14+
'create_register_func_to_anat',
15+
'create_register_func_to_anat_use_T2',
16+
'create_bbregister_func_to_anat',
17+
'create_wf_calculate_ants_warp',
18+
'output_func_to_standard']

CPAC/registration/guardrails.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,15 @@ def retry_registration_node(registered, registration_node):
194194
-------
195195
Node
196196
"""
197-
from CPAC.pipeline.random_state.seed import random_seed
197+
from CPAC.pipeline.random_state.seed import MAX_SEED, random_seed
198198
seed = random_seed()
199199
if registered.endswith('-failed') and isinstance(seed, int):
200200
retry_node = registration_node.clone(
201201
name=f'{registration_node.name}-retry')
202-
retry_node.seed = seed + 1
202+
if seed < MAX_SEED: # increment random seed
203+
retry_node.seed = seed + 1
204+
else: # loop back to minumum seed
205+
retry_node.seed = 1
203206
return retry_node
204207
return registration_node
205208

0 commit comments

Comments
 (0)