Skip to content

Commit

Permalink
Update bids.py
Browse files Browse the repository at this point in the history
  • Loading branch information
tsalo committed Nov 1, 2024
1 parent 1004366 commit 84109c4
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions aslprep/interfaces/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,54 @@ class _BIDSDataGrabberInputSpec(BaseInterfaceInputSpec):
class _BIDSDataGrabberOutputSpec(TraitedSpec):
out_dict = traits.Dict(desc='output data structure')
fmap = OutputMultiObject(desc='output fieldmaps')
asl = OutputMultiObject(desc='output ASL images')
bold = OutputMultiObject(desc='output functional images')
sbref = OutputMultiObject(desc='output sbrefs')
t1w = OutputMultiObject(desc='output T1w images')
roi = OutputMultiObject(desc='output ROI images')
t2w = OutputMultiObject(desc='output T2w images')
flair = OutputMultiObject(desc='output FLAIR images')
pet = OutputMultiObject(desc='output PET images')
dwi = OutputMultiObject(desc='output DWI images')
asl = OutputMultiObject(desc='output ASL images')


class BIDSDataGrabber(SimpleInterface):
"""Collect files from a BIDS directory structure."""
"""Collect files from a BIDS directory structure.
.. testsetup::
>>> data_dir_canary()
>>> bids_src = BIDSDataGrabber(anat_only=False)
>>> bids_src.inputs.subject_data = bids_collect_data(
... str(datadir / 'ds114'), '01', bids_validate=False)[0]
>>> bids_src.inputs.subject_id = '01'
>>> res = bids_src.run()
>>> res.outputs.t1w # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
['.../ds114/sub-01/ses-retest/anat/sub-01_ses-retest_T1w.nii.gz',
'.../ds114/sub-01/ses-test/anat/sub-01_ses-test_T1w.nii.gz']
"""

input_spec = _BIDSDataGrabberInputSpec
output_spec = _BIDSDataGrabberOutputSpec
_require_funcs = True

def __init__(self, *args, **kwargs):
anat_only = kwargs.pop('anat_only')
anat_derivatives = kwargs.pop('anat_derivatives', None)
super().__init__(*args, **kwargs)
if anat_only is not None:
self._require_funcs = not anat_only
self._require_t1w = anat_derivatives is None

def _run_interface(self, runtime):
bids_dict = self.inputs.subject_data

self._results['out_dict'] = bids_dict
self._results.update(bids_dict)

if not bids_dict['t1w']:
if self._require_t1w and not bids_dict['t1w']:
raise FileNotFoundError(
f'No T1w images found for subject sub-{self.inputs.subject_id}'
)
Expand All @@ -74,12 +94,10 @@ def _run_interface(self, runtime):
f'No ASL images found for subject sub-{self.inputs.subject_id}'
)

for imtype in ['t2w', 'flair', 'fmap', 'sbref', 'roi', 'asl']:
for imtype in ['bold', 't2w', 'flair', 'fmap', 'sbref', 'roi', 'pet', 'asl']:
if not bids_dict[imtype]:
config.loggers.interface.info(
'No "%s" images found for sub-%s',
imtype,
self.inputs.subject_id,
'No "%s" images found for sub-%s', imtype, self.inputs.subject_id
)

return runtime
Expand Down

0 comments on commit 84109c4

Please sign in to comment.