Skip to content

Commit de345f4

Browse files
committed
🎨 Increment seed for ANTs retry
1 parent 29b7490 commit de345f4

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

CPAC/pipeline/random_state/seed.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@
3232
_seed = {'seed': None}
3333

3434

35+
def increment_seed(node: 'Node') -> 'Node':
36+
"""Increment the random seed for a given node
37+
38+
Parameters
39+
----------
40+
node : Node
41+
42+
Returns
43+
-------
44+
node : Node
45+
"""
46+
if isinstance(node.seed, int):
47+
node.seed = seed_plus_1()
48+
return node
49+
50+
3551
def random_random_seed():
3652
'''Returns a random postive integer up to 2147483647
3753

CPAC/registration/guardrails.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,12 @@ def registration_guardrail(registered: str, reference: str,
117117
bad_registration = BadRegistrationError(
118118
metric=metric, value=value, threshold=threshold)
119119
logger.error(str(bad_registration))
120-
if retry_num: # if we've already retried, raise the error
120+
if retry_num:
121+
# if we've already retried, raise the error and
122+
# stop execution
123+
from nipype import config
124+
config.update_config({
125+
'execution': {'stop_on_first_crash': True}})
121126
raise bad_registration
122127
return registered, failed_qc
123128

@@ -247,12 +252,10 @@ def retry_registration_node(registered, registration_node):
247252
-------
248253
Node
249254
"""
250-
from CPAC.pipeline.random_state.seed import seed_plus_1
255+
from CPAC.pipeline.random_state.seed import increment_seed
251256
if registered.endswith('-failed'):
252-
retry_node = registration_node.clone(
253-
name=f'{registration_node.name}-retry')
254-
if isinstance(retry_node.seed, int):
255-
retry_node.seed = seed_plus_1()
257+
retry_node = increment_seed(registration_node.clone(
258+
name=f'{registration_node.name}-retry'))
256259
return retry_node
257260
return registration_node
258261

CPAC/registration/registration.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from nipype.interfaces.afni import utils as afni_utils
2424
from CPAC.anat_preproc.lesion_preproc import create_lesion_preproc
2525
from CPAC.func_preproc.utils import chunk_ts, split_ts_chunks
26+
from CPAC.pipeline.random_state.seed import increment_seed
2627
from CPAC.registration.guardrails import guardrail_selection, \
2728
registration_guardrail_node
2829
from CPAC.registration.utils import seperate_warps_list, \
@@ -1212,7 +1213,7 @@ def create_wf_calculate_ants_warp(name='create_wf_calculate_ants_warp',
12121213
imports=reg_imports),
12131214
name='calc_ants_warp', mem_gb=2.8,
12141215
mem_x=(2e-7, 'moving_brain', 'xyz'))
1215-
retry_calculate_ants_warp = pe.Node(
1216+
retry_calculate_ants_warp = increment_seed(pe.Node(
12161217
interface=util.Function(input_names=[*warp_inputs, 'previous_failure'],
12171218
output_names=warp_outputs,
12181219
function=retry_hardcoded_reg,
@@ -1222,7 +1223,7 @@ def create_wf_calculate_ants_warp(name='create_wf_calculate_ants_warp',
12221223
'from CPAC.utils.docs import '
12231224
'retry_docstring']),
12241225
name='retry_calc_ants_warp', mem_gb=2.8,
1225-
mem_x=(2e-7, 'moving_brain', 'xyz'))
1226+
mem_x=(2e-7, 'moving_brain', 'xyz')))
12261227
guardrails = tuple(registration_guardrail_node(
12271228
f'{_try}{name}_guardrail', i) for i, _try in enumerate(('', 'retry_')))
12281229

0 commit comments

Comments
 (0)