From 4128878086bc3e2d69a4fb3c205a6e76bede30ab Mon Sep 17 00:00:00 2001 From: Marcel Zwiers Date: Mon, 5 Feb 2024 16:45:28 +0100 Subject: [PATCH] Expand ruamel.yaml aliases --- bidscoin/bids.py | 1 + bidscoin/plugins/dcm2niix2bids.py | 15 ++++++--------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/bidscoin/bids.py b/bidscoin/bids.py index ef4604e8..3cfd7952 100644 --- a/bidscoin/bids.py +++ b/bidscoin/bids.py @@ -32,6 +32,7 @@ from bidscoin.utilities import dicomsort from ruamel.yaml import YAML yaml = YAML() +yaml.composer.return_alias = lambda s: copy.deepcopy(s) # Expand aliases (https://stackoverflow.com/questions/66977002/yaml-anchor-for-sequence/66983530#66983530) # Define custom data types (replace with TypeAlias when Python >= 3.10) Plugin = NewType('Plugin', Dict[str, Any]) diff --git a/bidscoin/plugins/dcm2niix2bids.py b/bidscoin/plugins/dcm2niix2bids.py index 30c69976..50aa40d4 100644 --- a/bidscoin/plugins/dcm2niix2bids.py +++ b/bidscoin/plugins/dcm2niix2bids.py @@ -174,15 +174,9 @@ def bidsmapper_plugin(session: Path, bidsmap_new: Bidsmap, bidsmap_old: Bidsmap, run['provenance'] = str(shutil.copyfile(sourcefile, targetfile)) run['datasource'].path = targetfile - # Copy the filled-in run over to the new bidsmap - bids.append_run(bidsmap_new, run) - - # Try to automagically set the {part: phase/imag/real} (should work for Siemens data) - for datatype in bidsmap_new['DICOM']: - if not isinstance(bidsmap_new['DICOM'][datatype], list): continue # E.g. 'subject', 'session' and empty datatypes - for run in bidsmap_new['DICOM'][datatype]: - if 'part' in run['bids'] and not run['bids']['part'][-1] and 'ImageType' in run['attributes']: - imagetype = ast.literal_eval(run['attributes']['ImageType']) + # Try to automagically set the {part: phase/imag/real} (should work for Siemens data) + if 'part' in run['bids'] and not run['bids']['part'][-1] and 'ImageType' in run['attributes']: # part[-1]==0 -> part is not specified + imagetype = ast.literal_eval(run['attributes']['ImageType']) # E.g. ImageType: "['ORIGINAL', 'PRIMARY', 'M', 'ND']" if 'P' in imagetype: run['bids']['part'][-1] = run['bids']['part'].index('phase') # elif 'M' in imagetype: @@ -192,6 +186,9 @@ def bidsmapper_plugin(session: Path, bidsmap_new: Bidsmap, bidsmap_old: Bidsmap, elif 'R' in imagetype: run['bids']['part'][-1] = run['bids']['part'].index('real') + # Copy the filled-in run over to the new bidsmap + bids.append_run(bidsmap_new, run) + @due.dcite(Doi('10.1016/j.jneumeth.2016.03.001'), description='dcm2niix: DICOM to NIfTI converter', tags=['reference-implementation']) def bidscoiner_plugin(session: Path, bidsmap: Bidsmap, bidsses: Path) -> Union[None, dict]: