Skip to content

Commit bea5939

Browse files
committed
fix(cli): revise flow and crash instead of rewrite if bids_dir == output_dir
1 parent bc62fd5 commit bea5939

File tree

1 file changed

+47
-46
lines changed

1 file changed

+47
-46
lines changed

fmriprep/cli/run.py

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -393,39 +393,28 @@ def before_send(event, hints):
393393
p.start()
394394
p.join()
395395

396-
if p.exitcode != 0:
397-
sys.exit(p.exitcode)
398-
399-
fmriprep_wf = retval['workflow']
400-
plugin_settings = retval['plugin_settings']
401-
bids_dir = retval['bids_dir']
402-
output_dir = retval['output_dir']
403-
work_dir = retval['work_dir']
404-
subject_list = retval['subject_list']
405-
run_uuid = retval['run_uuid']
406-
if not opts.notrack:
407-
with sentry_sdk.configure_scope() as scope:
408-
scope.set_tag('run_uuid', run_uuid)
409-
scope.set_tag('npart', len(subject_list))
396+
retcode = p.exitcode or retval.get('return_code', 0)
410397

411-
retcode = retval['return_code']
412-
413-
if fmriprep_wf is None:
414-
sys.exit(1)
415-
416-
if opts.write_graph:
417-
fmriprep_wf.write_graph(graph2use="colored", format='svg', simple_form=True)
398+
bids_dir = retval.get('bids_dir')
399+
output_dir = retval.get('output_dir')
400+
work_dir = retval.get('work_dir')
401+
plugin_settings = retval.get('plugin_settings', None)
402+
subject_list = retval.get('subject_list', None)
403+
fmriprep_wf = retval.get('workflow', None)
404+
run_uuid = retval.get('run_uuid', None)
418405

419406
if opts.reports_only:
420407
sys.exit(int(retcode > 0))
421408

422409
if opts.boilerplate:
423410
sys.exit(int(retcode > 0))
424411

425-
# Sentry tracking
426-
if not opts.notrack:
427-
sentry_sdk.add_breadcrumb(message='fMRIPrep started', level='info')
428-
sentry_sdk.capture_message('fMRIPrep started', level='info')
412+
if fmriprep_wf and opts.write_graph:
413+
fmriprep_wf.write_graph(graph2use="colored", format='svg', simple_form=True)
414+
415+
retcode = retcode or int(fmriprep_wf is None)
416+
if retcode != 0:
417+
sys.exit(retcode)
429418

430419
# Check workflow for missing commands
431420
missing = check_deps(fmriprep_wf)
@@ -434,9 +423,19 @@ def before_send(event, hints):
434423
for iface, cmd in missing:
435424
print("\t{} (Interface: {})".format(cmd, iface))
436425
sys.exit(2)
437-
438426
# Clean up master process before running workflow, which may create forks
439427
gc.collect()
428+
429+
# Sentry tracking
430+
if not opts.notrack:
431+
with sentry_sdk.configure_scope() as scope:
432+
if run_uuid:
433+
scope.set_tag('run_uuid', run_uuid)
434+
if subject_list:
435+
scope.set_tag('npart', len(subject_list))
436+
sentry_sdk.add_breadcrumb(message='fMRIPrep started', level='info')
437+
sentry_sdk.capture_message('fMRIPrep started', level='info')
438+
440439
try:
441440
fmriprep_wf.run(**plugin_settings)
442441
except RuntimeError as e:
@@ -588,6 +587,24 @@ def build_workflow(opts, retval):
588587
* Run identifier: {uuid}.
589588
""".format
590589

590+
bids_dir = opts.bids_dir.resolve()
591+
output_dir = opts.output_dir.resolve()
592+
work_dir = opts.work_dir
593+
594+
retval['return_code'] = 1
595+
retval['workflow'] = None
596+
retval['bids_dir'] = str(bids_dir)
597+
retval['output_dir'] = str(output_dir)
598+
retval['work_dir'] = str(work_dir)
599+
600+
if output_dir == bids_dir:
601+
logger.error(
602+
'The selected output folder is the same as the input BIDS folder. '
603+
'Please modify the output path (suggestion: %s).',
604+
bids_dir / 'derivatives' / ('fmriprep-%s' % __version__.split('+')[0]))
605+
retval['return_code'] = 1
606+
return retval
607+
591608
# Reduce to unique space identifiers
592609
output_spaces = sorted(set(opts.output_space))
593610

@@ -633,12 +650,13 @@ def build_workflow(opts, retval):
633650

634651
# Set up some instrumental utilities
635652
run_uuid = '%s_%s' % (strftime('%Y%m%d-%H%M%S'), uuid.uuid4())
653+
retval['run_uuid'] = run_uuid
636654

637655
# First check that bids_dir looks like a BIDS folder
638-
bids_dir = opts.bids_dir.resolve()
639656
layout = BIDSLayout(str(bids_dir), validate=False)
640657
subject_list = collect_participants(
641658
layout, participant_label=opts.participant_label)
659+
retval['subject_list'] = subject_list
642660

643661
# Load base plugin_settings from file if --use-plugin
644662
if opts.use_plugin is not None:
@@ -678,19 +696,10 @@ def build_workflow(opts, retval):
678696
logger.warning(
679697
'Per-process threads (--omp-nthreads=%d) exceed total '
680698
'threads (--nthreads/--n_cpus=%d)', omp_nthreads, nthreads)
699+
retval['plugin_settings'] = plugin_settings
681700

682701
# Set up directories
683-
output_dir = opts.output_dir.resolve()
684-
if output_dir == bids_dir:
685-
output_dir = bids_dir / 'derivatives' / ('fmriprep-%s' % __version__.split('+')[0])
686-
logger.warning(
687-
'The selected output folder is the same as the input BIDS folder. '
688-
'Cowardly redirecting outputs to %s', output_dir
689-
)
690-
691702
log_dir = output_dir / 'fmriprep' / 'logs'
692-
work_dir = opts.work_dir
693-
694703
# Check and create output and working directories
695704
output_dir.mkdir(exist_ok=True, parents=True)
696705
log_dir.mkdir(exist_ok=True, parents=True)
@@ -718,20 +727,12 @@ def build_workflow(opts, retval):
718727
if opts.resource_monitor:
719728
ncfg.enable_resource_monitor()
720729

721-
retval['return_code'] = 0
722-
retval['plugin_settings'] = plugin_settings
723-
retval['bids_dir'] = str(bids_dir)
724-
retval['output_dir'] = str(output_dir)
725-
retval['work_dir'] = str(work_dir)
726-
retval['subject_list'] = subject_list
727-
retval['run_uuid'] = run_uuid
728-
retval['workflow'] = None
729-
730730
# Called with reports only
731731
if opts.reports_only:
732732
logger.log(25, 'Running --reports-only on participants %s', ', '.join(subject_list))
733733
if opts.run_uuid is not None:
734734
run_uuid = opts.run_uuid
735+
retval['run_uuid'] = run_uuid
735736
retval['return_code'] = generate_reports(
736737
subject_list, str(output_dir), str(work_dir), run_uuid)
737738
return retval

0 commit comments

Comments
 (0)