Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Crawl dataset's metadata only once and before Nipype's workflow #1317

Merged
merged 9 commits into from
Aug 16, 2024
Prev Previous commit
sty: run ruff
oesteban committed Aug 16, 2024
commit cf1ea8f7d8382d7b73b98aec5f8f4d1b74512d78
6 changes: 2 additions & 4 deletions mriqc/cli/parser.py
Original file line number Diff line number Diff line change
@@ -479,7 +479,6 @@ def _bids_filter(value):

def parse_args(args=None, namespace=None):
"""Parse args and run further checks on the command line."""
from contextlib import suppress
from json import loads
from logging import DEBUG, FileHandler
from pathlib import Path
@@ -555,10 +554,9 @@ def parse_args(args=None, namespace=None):
if output_dir == bids_dir:
parser.error(
'The selected output folder is the same as the input BIDS folder. '
'Please modify the output path (suggestion: %s).'
% bids_dir
f'Please modify the output path (suggestion: {bids_dir}).'
/ 'derivatives'
/ ('mriqc-%s' % version.split('+')[0])
/ ('mriqc-{}'.format(version.split('+')[0]))
)

if bids_dir in work_dir.parents:
30 changes: 13 additions & 17 deletions mriqc/interfaces/webapi.py
Original file line number Diff line number Diff line change
@@ -169,15 +169,17 @@ def _run_interface(self, runtime):
config.loggers.interface.info(messages.QC_UPLOAD_COMPLETE)
return runtime

errmsg = '\n'.join([
'Unsuccessful upload.',
f'Server response status {response.status_code}:',
response.text,
'',
'',
'Payload:',
json.dumps(payload, indent=2),
])
errmsg = '\n'.join(
[
'Unsuccessful upload.',
f'Server response status {response.status_code}:',
response.text,
'',
'',
'Payload:',
json.dumps(payload, indent=2),
]
)
config.loggers.interface.warning(errmsg)
if self.inputs.strict:
raise RuntimeError(errmsg)
@@ -227,11 +229,7 @@ def upload_qc_metrics(
data = deepcopy(in_data)

# Check modality
modality = (
meta.get('modality', None)
or meta.get('suffix', None)
or modality
)
modality = meta.get('modality', None) or meta.get('suffix', None) or modality
if modality not in ('T1w', 'bold', 'T2w'):
errmsg = (
'Submitting to MRIQCWebAPI: image modality should be "bold", "T1w", or "T2w", '
@@ -244,9 +242,7 @@ def upload_qc_metrics(

# Check for fields with appended _id
bids_meta_names = {k: k.replace('_id', '') for k in META_WHITELIST if k.endswith('_id')}
data['bids_meta'].update({
k: meta[v] for k, v in bids_meta_names.items() if v in meta
})
data['bids_meta'].update({k: meta[v] for k, v in bids_meta_names.items() if v in meta})

# For compatibility with WebAPI. Should be rolled back to int
if (run_id := data['bids_meta'].get('run_id', None)) is not None:
40 changes: 25 additions & 15 deletions mriqc/workflows/anatomical/base.py
Original file line number Diff line number Diff line change
@@ -53,6 +53,7 @@
"""

from itertools import chain

from nipype.interfaces import utility as niu
from nipype.pipeline import engine as pe
from niworkflows.interfaces.fixes import FixHeaderApplyTransforms as ApplyTransforms
@@ -94,18 +95,24 @@ def anat_qc_workflow(name='anatMRIQC'):
# config.workflow.biggest_file_gb['t1w'],
# config.workflow.biggest_file_gb['t2w'],
# )
dataset = list(chain(
config.workflow.inputs.get('t1w', []),
config.workflow.inputs.get('t2w', []),
))
metadata = list(chain(
config.workflow.inputs_metadata.get('t1w', []),
config.workflow.inputs_metadata.get('t2w', []),
))
entities = list(chain(
config.workflow.inputs_entities.get('t1w', []),
config.workflow.inputs_entities.get('t2w', []),
))
dataset = list(
chain(
config.workflow.inputs.get('t1w', []),
config.workflow.inputs.get('t2w', []),
)
)
metadata = list(
chain(
config.workflow.inputs_metadata.get('t1w', []),
config.workflow.inputs_metadata.get('t2w', []),
)
)
entities = list(
chain(
config.workflow.inputs_entities.get('t1w', []),
config.workflow.inputs_entities.get('t2w', []),
)
)
message = BUILDING_WORKFLOW.format(
modality='anatomical',
detail=f'for {len(dataset)} NIfTI files.',
@@ -117,9 +124,12 @@ def anat_qc_workflow(name='anatMRIQC'):

# Define workflow, inputs and outputs
# 0. Get data
inputnode = pe.Node(niu.IdentityInterface(
fields=['in_file', 'metadata', 'entities'],
), name='inputnode')
inputnode = pe.Node(
niu.IdentityInterface(
fields=['in_file', 'metadata', 'entities'],
),
name='inputnode',
)
inputnode.synchronize = True # Do not test combinations of iterables
inputnode.iterables = [
('in_file', dataset),
9 changes: 6 additions & 3 deletions mriqc/workflows/diffusion/base.py
Original file line number Diff line number Diff line change
@@ -98,9 +98,12 @@ def dmri_qc_workflow(name='dwiMRIQC'):
# Define workflow, inputs and outputs
# 0. Get data, put it in RAS orientation
workflow = pe.Workflow(name=name)
inputnode = pe.Node(niu.IdentityInterface(
fields=['in_file', 'metadata', 'entities'],
), name='inputnode')
inputnode = pe.Node(
niu.IdentityInterface(
fields=['in_file', 'metadata', 'entities'],
),
name='inputnode',
)
inputnode.synchronize = True # Do not test combinations of iterables
inputnode.iterables = [
('in_file', dataset),
10 changes: 6 additions & 4 deletions mriqc/workflows/functional/base.py
Original file line number Diff line number Diff line change
@@ -43,7 +43,6 @@
This workflow is orchestrated by :py:func:`fmri_qc_workflow`.
"""


from nipype.interfaces import utility as niu
from nipype.pipeline import engine as pe
from niworkflows.utils.connections import pop_file as _pop
@@ -86,9 +85,12 @@ def fmri_qc_workflow(name='funcMRIQC'):
# Define workflow, inputs and outputs
# 0. Get data, put it in RAS orientation
workflow = pe.Workflow(name=name)
inputnode = pe.Node(niu.IdentityInterface(
fields=['in_file', 'metadata', 'entities'],
), name='inputnode')
inputnode = pe.Node(
niu.IdentityInterface(
fields=['in_file', 'metadata', 'entities'],
),
name='inputnode',
)
inputnode.synchronize = True # Do not test combinations of iterables
inputnode.iterables = [
('in_file', dataset),
2 changes: 1 addition & 1 deletion mriqc/workflows/functional/output.py
Original file line number Diff line number Diff line change
@@ -49,7 +49,7 @@ def init_func_report_wf(name='func_report_wf'):
# from mriqc.interfaces.reports import IndividualReport

verbose = config.execution.verbose_reports
mem_gb = config.workflow.biggest_file_gb["bold"]
mem_gb = config.workflow.biggest_file_gb['bold']
reportlets_dir = config.execution.work_dir / 'reportlets'

workflow = pe.Workflow(name=name)