Skip to content

Commit

Permalink
Expand ruamel.yaml aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelzwiers committed Feb 5, 2024
1 parent 5a5af50 commit 4128878
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
1 change: 1 addition & 0 deletions bidscoin/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
15 changes: 6 additions & 9 deletions bidscoin/plugins/dcm2niix2bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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]:
Expand Down

0 comments on commit 4128878

Please sign in to comment.