From 9442047556d786b431af4931b6e9222246501caa Mon Sep 17 00:00:00 2001 From: MarsMellow Date: Tue, 24 Sep 2024 14:50:42 +0200 Subject: [PATCH] Bugfix for random sorting of the unknowntypes (set() -> list()) --- bidscoin/bids.py | 12 ++++++------ bidscoin/utilities/dicomsort.py | 2 +- tests/conftest.py | 1 + tests/test_bidscoiner.py | 13 +++++++++---- tests/test_bidsmapper.py | 1 - 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/bidscoin/bids.py b/bidscoin/bids.py index bae75379..f60d8673 100644 --- a/bidscoin/bids.py +++ b/bidscoin/bids.py @@ -936,10 +936,10 @@ def __init__(self, yamlfile: Path, folder: Path=templatefolder, plugins: Iterabl bidsignorefile = folder.parents[1]/'.bidsignore' if bidsignorefile.is_file(): self.options['bidsignore'] = list(set(list(self.options['bidsignore']) + bidsignorefile.read_text().splitlines())) - self.options['bidsignore'] = list(set(self.options.get('bidsignore') or [])) - self.options['unknowntypes'] = list(set(self.options.get('unknowntypes') or [])) - self.options['ignoretypes'] = list(set(self.options.get('ignoretypes') or [])) - self.options['notderivative'] = list(set(self.options.get('notderivative') or [])) + self.options['bidsignore'] = sorted(set(self.options.get('bidsignore'))) or [] + self.options['unknowntypes'] = self.options.get('unknowntypes') or [] + self.options['ignoretypes'] = self.options.get('ignoretypes') or [] + self.options['notderivative'] = self.options.get('notderivative') or [] # Make sure we get a proper plugin options and dataformat sections (use plugin default bidsmappings when a template bidsmap is loaded) if plugins: @@ -1536,7 +1536,7 @@ def unpack(sesfolder: Path, wildcard: str='', workfolder: Path='', _subprefix: U :param wildcard: A glob search pattern to select the tarball/zipped files (leave empty to skip unzipping) :param workfolder: A root folder for temporary data :param _subprefix: A pytest helper variable that is passed to dicomsort.sortsessions(args, subprefix=_subprefix) - :return: Either ([unpacked and sorted session folders], True), or ([sourcefolder], False) + :return: Either ({a set of unpacked session folders}, True), or ({sourcefolder}, False) """ # Search for zipped/tarball files @@ -2560,7 +2560,7 @@ def updatemetadata(datasource: DataSource, targetmeta: Path, usermeta: Meta, ext # Add the source metadata to the metadict or copy it over if sourcemeta.name: - for ext in set(extensions): + for ext in extensions: for sourcefile in sourcemeta.parent.glob(sourcemeta.with_suffix('').with_suffix(ext).name): LOGGER.verbose(f"Copying source data from: '{sourcefile}''") diff --git a/bidscoin/utilities/dicomsort.py b/bidscoin/utilities/dicomsort.py index dafcb829..faa0b4e5 100755 --- a/bidscoin/utilities/dicomsort.py +++ b/bidscoin/utilities/dicomsort.py @@ -149,7 +149,7 @@ def sortsessions(sourcefolder: Path, subprefix: Union[str,None]='', sesprefix: s :param recursive: Boolean to search for DICOM files recursively in a session folder :param force: Sort the DICOM data even the DICOM fields of the folder/name scheme are not in the data :param dryrun: Boolean to just display the action - :return: List of sorted sessions + :return: A set of sessions """ # Input checking diff --git a/tests/conftest.py b/tests/conftest.py index f6c49afb..12529071 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -21,6 +21,7 @@ def raw_dicomdir(tmp_path_factory): dicomsort.sortsessions(raw/'DICOMDIR', None) # The bidsmapper/coiner are NOT picking up the multi-subject DICOMDIR data properly :-( dicomfile = sorted((raw/'Doe^Peter'/'03-Brain'/'002-TSC RF FAST PILOT/').iterdir())[0] # Make sure this is the first file with dicomfile.with_suffix('.json').open('w') as sidecar: + print(f"Saving extended metadata file: {dicomfile.with_suffix('.json')}") # = raw_dicomdir0/Doe^Peter/03-Brain/002-TSC RF FAST PILOT/4950.json json.dump({'SeriesDescription': 'TestExtAtrributes', 'Comment': 'TestExtComment'}, sidecar) return raw diff --git a/tests/test_bidscoiner.py b/tests/test_bidscoiner.py index 496f0380..c8b7e1eb 100644 --- a/tests/test_bidscoiner.py +++ b/tests/test_bidscoiner.py @@ -1,6 +1,7 @@ import os import json from bidscoin import bcoin, bidsmapper, bidscoiner, bidsmap_template, __version__ +from bidscoin.bids import BidsMap from duecredit.io import load_due, DUECREDIT_FILE bcoin.setup_logging() @@ -14,8 +15,11 @@ def test_bidscoiner(raw_dicomdir, bids_dicomdir, bidsmap_dicomdir): except Exception: pass bidscoiner.bidscoiner(raw_dicomdir, bids_dicomdir) - logs = (bidsmap_dicomdir.parent/'bidscoiner.errors').read_text() - sidecars = sorted((bids_dicomdir/'sub-Peter'/'ses-03Brain'/'extra_data').glob('*TestExtAtrributes*.json')) + logs = (bidsmap_dicomdir.parent/'bidscoiner.errors').read_text() + sidecar = sorted((bids_dicomdir/'sub-Peter'/'ses-03Brain').rglob('*TestExtAtrributes*.json'))[0] + bidsmap = BidsMap(bidsmap_dicomdir) + assert bidsmap.options['unknowntypes'][-1] == 'extra_data' + assert sidecar.relative_to(bids_dicomdir).as_posix() == 'sub-Peter/ses-03Brain/extra_data/sub-Peter_ses-03Brain_acq-TSCRFFASTPILOTi00001_mod-TestExtAtrributes_GR.json' try: (bidsmap_dicomdir.parent/'bidscoiner.errors').unlink(missing_ok=True) except Exception: @@ -27,7 +31,7 @@ def test_bidscoiner(raw_dicomdir, bids_dicomdir, bidsmap_dicomdir): assert (bids_dicomdir/'sub-Peter'/'ses-01').is_dir() assert (bids_dicomdir/'sub-Peter'/'ses-04BrainMRA').is_dir() assert len(list(bids_dicomdir.rglob('*.nii*'))) > 3 # Exact number (10) is a bit arbitrary (depends on what dcm2niix can convert) - with sidecars[0].open('r') as json_fid: + with sidecar.open('r') as json_fid: metadict = json.load(json_fid) assert metadict.get('ProtocolName') == 'T/S/C RF FAST PILOT' assert metadict.get('SeriesDescription') == 'TestExtAtrributes' @@ -45,5 +49,6 @@ def test_bidscoiner(raw_dicomdir, bids_dicomdir, bidsmap_dicomdir): # def test_addmetadata(bids_dicomdir, bidsmap_dicomdir): -# bidsmap, _ = bids.load_bidsmap(bidsmap_dicomdir) +# """WIP""" +# bidsmap = BidsMap(bidsmap_dicomdir) # bidscoiner.addmetadata(bids_dicomdir/'sub-something'/'ses-else', '*', '*') diff --git a/tests/test_bidsmapper.py b/tests/test_bidsmapper.py index 94ce75c8..103fa67b 100644 --- a/tests/test_bidsmapper.py +++ b/tests/test_bidsmapper.py @@ -1,7 +1,6 @@ import pytest import re from bidscoin import bcoin, bidsmapper, bidsmap_template -from pathlib import Path bcoin.setup_logging()