niworkflows.utils.misc module

Miscellaneous utilities.

niworkflows.utils.misc.add_suffix(in_files, suffix)[source]

Wrap nipype’s fname_presuffix to conveniently just add a prefix

>>> add_suffix([
...     '/path/to/sub-045_ses-test_T1w.nii.gz',
...     '/path/to/sub-045_ses-retest_T1w.nii.gz'], '_test')
'sub-045_ses-test_T1w_test.nii.gz'
niworkflows.utils.misc.clean_directory(path)[source]

Clears a directory of all contents.

Returns True if no content remains. If any content cannot be removed, returns False.

Notes

This function is not guaranteed to work across multiple threads or processes.

niworkflows.utils.misc.fix_multi_T1w_source_name(in_files)[source]

Make up a generic source name when there are multiple T1s

>>> fix_multi_T1w_source_name([
...     '/path/to/sub-045_ses-test_T1w.nii.gz',
...     '/path/to/sub-045_ses-retest_T1w.nii.gz'])
'/path/to/sub-045_T1w.nii.gz'
>>> fix_multi_T1w_source_name([
...    ('/path/to/sub-045-echo-1_T1w.nii.gz', 'path/to/sub-045-echo-2_T1w.nii.gz')])
'/path/to/sub-045_T1w.nii.gz'
niworkflows.utils.misc.get_template_specs(in_template: str, template_spec: dict | None = None, default_resolution: int = 1, fallback: bool = False)[source]

Parse template specifications

>>> get_template_specs('MNI152NLin2009cAsym', {'suffix': 'T1w'})[1]
{'resolution': 1}
>>> get_template_specs('MNI152NLin2009cAsym', {'res': '2', 'suffix': 'T1w'})[1]
{'resolution': '2'}
>>> specs = get_template_specs('MNIInfant', {'res': '2', 'cohort': '10', 'suffix': 'T1w'})[1]
>>> sorted(specs.items())
[('cohort', '10'), ('resolution', '2')]
>>> get_template_specs('MNI152NLin2009cAsym',
...                    {'suffix': 'T1w', 'cohort': 1})[1] 
Traceback (most recent call last):
RuntimeError:
...
>>> get_template_specs('MNI152NLin2009cAsym',
...                    {'suffix': 'T1w', 'res': '1|2'})[1] 
Traceback (most recent call last):
RuntimeError:
...
>>> get_template_specs('UNCInfant',
...                    {'suffix': 'T1w', 'res': 1})[1] 
Traceback (most recent call last):
RuntimeError:
...
>>> get_template_specs('UNCInfant',
...                    {'cohort': 1, 'suffix': 'T1w', 'res': 1}, fallback=True)[1]
{'resolution': None, 'cohort': 1}
niworkflows.utils.misc.read_crashfile(path)[source]
niworkflows.utils.misc.splitext(fname)[source]

Split filename in name and extension (.gz safe).

Examples

>>> splitext('some/file.nii.gz')
('file', '.nii.gz')
>>> splitext('some/other/file.nii')
('file', '.nii')
>>> splitext('otherext.tar.gz')
('otherext', '.tar.gz')
>>> splitext('text.txt')
('text', '.txt')
>>> splitext('some/figure.svg')
('figure', '.svg')
>>> splitext('some/figure.svg.gz')
('figure', '.svg.gz')
>>> splitext('some/sub-01_bold.func.gii')
('sub-01_bold.func', '.gii')