Skip to content

Commit

Permalink
Bugfix for random sorting of the unknowntypes (set() -> list())
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelzwiers committed Sep 24, 2024
1 parent 9134e82 commit 9442047
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
12 changes: 6 additions & 6 deletions bidscoin/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}''")

Expand Down
2 changes: 1 addition & 1 deletion bidscoin/utilities/dicomsort.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 9 additions & 4 deletions tests/test_bidscoiner.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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:
Expand All @@ -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'
Expand All @@ -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', '*', '*')
1 change: 0 additions & 1 deletion tests/test_bidsmapper.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pytest
import re
from bidscoin import bcoin, bidsmapper, bidsmap_template
from pathlib import Path

bcoin.setup_logging()

Expand Down

0 comments on commit 9442047

Please sign in to comment.