Skip to content

Commit 2c4de8b

Browse files
committed
fix: sort out loggging, run boilerplate's md -> html isolated in process
1 parent b5ed669 commit 2c4de8b

File tree

4 files changed

+65
-55
lines changed

4 files changed

+65
-55
lines changed

fmriprep/cli/run.py

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ def main():
9393
sentry_sdk.capture_message('fMRIPrep started', level='info')
9494

9595
config.loggers.workflow.log(25, 'fMRIPrep started!')
96+
config.loggers.workflow.log(15, '\n'.join(
97+
['fMRIPrep config:'] + ['\t\t%s' % s for s in config.dumps().splitlines()])
98+
)
9699
errno = 1 # Default is error exit unless otherwise set
97100
try:
98101
fmriprep_wf.run(**config.nipype.get_plugin())
@@ -113,13 +116,20 @@ def main():
113116
config.loggers.workflow.critical('fMRIPrep failed: %s', e)
114117
raise
115118
else:
116-
117119
config.loggers.workflow.log(25, 'fMRIPrep finished successfully!')
118120
if not config.execution.notrack:
119121
success_message = 'fMRIPrep finished without errors'
120122
sentry_sdk.add_breadcrumb(message=success_message, level='info')
121123
sentry_sdk.capture_message(success_message, level='info')
122124

125+
# Bother users with the boilerplate only iff the workflow went okay.
126+
if (config.execution.output_dir / 'fmriprep' / 'logs' / 'CITATION.md').exists():
127+
config.loggers.workflow.log(
128+
25, 'Works derived from this fMRIPrep execution should '
129+
'include the following boilerplate:\n\n%s',
130+
(config.execution.output_dir / 'fmriprep' / 'logs' / 'CITATION.md').read_text()
131+
)
132+
123133
if config.workflow.run_reconall:
124134
from templateflow import api
125135
from niworkflows.utils.misc import _copy_any
@@ -131,52 +141,7 @@ def main():
131141
errno = 0
132142
finally:
133143
from niworkflows.reports import generate_reports
134-
from subprocess import check_call, CalledProcessError, TimeoutExpired
135144
from pkg_resources import resource_filename as pkgrf
136-
from shutil import copyfile
137-
138-
citation_files = {
139-
ext: config.execution.output_dir / 'fmriprep' / 'logs'
140-
/ ('CITATION.%s' % ext) for ext in ('bib', 'tex', 'md', 'html')
141-
}
142-
143-
if not config.execution.md_only_boilerplate and citation_files['md'].exists():
144-
# Generate HTML file resolving citations
145-
cmd = ['pandoc', '-s', '--bibliography',
146-
pkgrf('fmriprep', 'data/boilerplate.bib'),
147-
'--filter', 'pandoc-citeproc',
148-
'--metadata', 'pagetitle="fMRIPrep citation boilerplate"',
149-
str(citation_files['md']),
150-
'-o', str(citation_files['html'])]
151-
152-
config.loggers.cli.info(
153-
'Generating an HTML version of the citation boilerplate...')
154-
try:
155-
check_call(cmd, timeout=10)
156-
except (FileNotFoundError, CalledProcessError, TimeoutExpired):
157-
config.loggers.cli.warning(
158-
'Could not generate CITATION.html file:\n%s', ' '.join(cmd))
159-
160-
# Generate LaTex file resolving citations
161-
cmd = ['pandoc', '-s', '--bibliography',
162-
pkgrf('fmriprep', 'data/boilerplate.bib'),
163-
'--natbib', str(citation_files['md']),
164-
'-o', str(citation_files['tex'])]
165-
config.loggers.cli.info(
166-
'Generating a LaTeX version of the citation boilerplate...')
167-
try:
168-
check_call(cmd, timeout=10)
169-
except (FileNotFoundError, CalledProcessError, TimeoutExpired):
170-
config.loggers.cli.warning(
171-
'Could not generate CITATION.tex file:\n%s', ' '.join(cmd))
172-
else:
173-
copyfile(pkgrf('fmriprep', 'data/boilerplate.bib'),
174-
citation_files['bib'])
175-
else:
176-
config.loggers.cli.warning(
177-
'fMRIPrep could not find the markdown version of '
178-
'the citation boilerplate (%s). HTML and LaTeX versions'
179-
' of it will not be available', citation_files['md'])
180145

181146
# Generate reports phase
182147
failed_reports = generate_reports(

fmriprep/cli/workflow.py

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,45 @@ def build_boilerplate(config_file, workflow):
121121
except FileNotFoundError:
122122
pass
123123

124-
citation_files['md'].write_text(boilerplate)
125-
config.loggers.workflow.log(
126-
25, 'Works derived from this fMRIPrep execution should '
127-
'include the following boilerplate:\n\n%s', boilerplate
128-
)
124+
citation_files['md'].write_text(boilerplate)
125+
126+
citation_files = {
127+
ext: config.execution.output_dir / 'fmriprep' / 'logs'
128+
/ ('CITATION.%s' % ext) for ext in ('bib', 'tex', 'md', 'html')
129+
}
130+
131+
if not config.execution.md_only_boilerplate and citation_files['md'].exists():
132+
from subprocess import check_call, CalledProcessError, TimeoutExpired
133+
from pkg_resources import resource_filename as pkgrf
134+
from shutil import copyfile
135+
# Generate HTML file resolving citations
136+
cmd = ['pandoc', '-s', '--bibliography',
137+
pkgrf('fmriprep', 'data/boilerplate.bib'),
138+
'--filter', 'pandoc-citeproc',
139+
'--metadata', 'pagetitle="fMRIPrep citation boilerplate"',
140+
str(citation_files['md']),
141+
'-o', str(citation_files['html'])]
142+
143+
config.loggers.cli.info(
144+
'Generating an HTML version of the citation boilerplate...')
145+
try:
146+
check_call(cmd, timeout=10)
147+
except (FileNotFoundError, CalledProcessError, TimeoutExpired):
148+
config.loggers.cli.warning(
149+
'Could not generate CITATION.html file:\n%s', ' '.join(cmd))
150+
151+
# Generate LaTex file resolving citations
152+
cmd = ['pandoc', '-s', '--bibliography',
153+
pkgrf('fmriprep', 'data/boilerplate.bib'),
154+
'--natbib', str(citation_files['md']),
155+
'-o', str(citation_files['tex'])]
156+
config.loggers.cli.info(
157+
'Generating a LaTeX version of the citation boilerplate...')
158+
try:
159+
check_call(cmd, timeout=10)
160+
except (FileNotFoundError, CalledProcessError, TimeoutExpired):
161+
config.loggers.cli.warning(
162+
'Could not generate CITATION.tex file:\n%s', ' '.join(cmd))
163+
else:
164+
copyfile(pkgrf('fmriprep', 'data/boilerplate.bib'),
165+
citation_files['bib'])

fmriprep/config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,9 @@ class workflow(_Config):
412412
class loggers(_Config):
413413
"""Configure loggers."""
414414

415+
_fmt = "%(asctime)s,%(msecs)d %(name)-2s " "%(levelname)-2s:\n\t %(message)s"
416+
_datefmt = "%y%m%d-%H:%M:%S"
417+
415418
default = logging.getLogger()
416419
cli = logging.getLogger('cli')
417420
workflow = nlogging.getLogger('nipype.workflow')
@@ -485,6 +488,11 @@ def init_layout():
485488

486489
def set_logger_level():
487490
"""Set the current log level to all nipype loggers."""
491+
_handler = logging.StreamHandler(stream=sys.stdout)
492+
_handler.setFormatter(
493+
logging.Formatter(fmt=loggers._fmt, datefmt=loggers._datefmt)
494+
)
495+
loggers.cli.addHandler(_handler)
488496
loggers.default.setLevel(execution.log_level)
489497
loggers.cli.setLevel(execution.log_level)
490498
loggers.interface.setLevel(execution.log_level)

fmriprep/workflows/bold/base.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,11 @@ def init_func_preproc_wf(bold_file):
192192
"Multiple single-band reference files found for {}; using "
193193
"{}".format(refbase, sbbase))
194194
else:
195-
config.loggers.workflow.log(
196-
25, "Using single-band reference file {}".format(sbbase))
195+
config.loggers.workflow.info("Using single-band reference file %s.",
196+
sbbase)
197197
else:
198-
config.loggers.workflow.log(
199-
25, "No single-band-reference found for {}".format(refbase))
198+
config.loggers.workflow.info("No single-band-reference found for %s.",
199+
refbase)
200200

201201
metadata = layout.get_metadata(ref_file)
202202

0 commit comments

Comments
 (0)