Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add only existing jsonfiles to jsonfiles and make it set #215

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions bidscoin/plugins/dcm2niix2bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def bidscoiner_plugin(session: Path, bidsmap: dict, bidsses: Path) -> Union[None
bidsname = bids.get_bidsname(subid, sesid, run, not bidsignore, runtime=True)
bidsignore = bidsignore or bids.check_ignore(bidsname+'.json', bidsmap['Options']['bidscoin']['bidsignore'], 'file')
bidsname = bids.increment_runindex(outfolder, bidsname, run, scans_table)
jsonfiles = [(outfolder/bidsname).with_suffix('.json')] # List -> Collect the associated json-files (for updating them later) -- possibly > 1
jsonfiles = set() # Set -> Collect the associated json-files (for updating them later) -- possibly > 1

# Check if the bidsname is valid
bidstest = (Path('/')/subid/sesid/datasource.datatype/bidsname).with_suffix('.json').as_posix()
Expand Down Expand Up @@ -308,6 +308,8 @@ def bidscoiner_plugin(session: Path, bidsmap: dict, bidsses: Path) -> Union[None
if bcoin.run_command(command):
if not list(outfolder.glob(f"{bidsname}.*nii*")): continue

jsonfiles.update(outfolder.glob(f"{bidsname}.json")) # add existing created json files: bidsname.json
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line should also be added right after the physio2tsv conversion above (i.e. after line 295)


# Handle the ABCD GE pepolar sequence
extrafile = list(outfolder.glob(f"{bidsname}a.nii*"))
if extrafile:
Expand All @@ -320,7 +322,7 @@ def bidscoiner_plugin(session: Path, bidsmap: dict, bidsses: Path) -> Union[None
LOGGER.verbose(f"Renaming GE reversed polarity image: {extrafile[0]} -> {invfile}")
extrafile[0].replace(invfile)
extrafile[0].with_suffix('').with_suffix('.json').replace(invfile.with_suffix('').with_suffix('.json'))
jsonfiles.append(invfile.with_suffix('').with_suffix('.json'))
jsonfiles.add(invfile.with_suffix('').with_suffix('.json'))
else:
LOGGER.warning(f"Unexpected variants of {outfolder/bidsname}* were produced by dcm2niix. Possibly this can be remedied by using the dcm2niix -i option (to ignore derived, localizer and 2D images) or by clearing the BIDS folder before running bidscoiner")

Expand All @@ -335,8 +337,7 @@ def bidscoiner_plugin(session: Path, bidsmap: dict, bidsses: Path) -> Union[None
# Rename all files that got additional postfixes from dcm2niix. See: https://github.com/rordenlab/dcm2niix/blob/master/FILENAMING.md
dcm2niixpostfixes = ('_c', '_i', '_Eq', '_real', '_imaginary', '_MoCo', '_t', '_Tilt', '_e', '_ph', '_ADC', '_fieldmaphz') #_c%d, _e%d and _ph (and any combination of these in that order) are for multi-coil data, multi-echo data and phase data
dcm2niixfiles = sorted(set([dcm2niixfile for dcm2niixpostfix in dcm2niixpostfixes for dcm2niixfile in outfolder.glob(f"{bidsname}*{dcm2niixpostfix}*.nii*")]))
if not jsonfiles[0].is_file() and dcm2niixfiles: # Possibly renamed by dcm2niix, e.g. with multi-echo data (but not always for the first echo)
jsonfiles.pop(0)

for dcm2niixfile in dcm2niixfiles:

# Strip each dcm2niix postfix and assign it to bids entities in a newly constructed bidsname
Expand Down Expand Up @@ -431,12 +432,12 @@ def bidscoiner_plugin(session: Path, bidsmap: dict, bidsses: Path) -> Union[None
if oldjsonfile in jsonfiles:
jsonfiles.remove(oldjsonfile)
if newjsonfile not in jsonfiles:
jsonfiles.append(newjsonfile)
jsonfiles.add(newjsonfile)
for oldfile in outfolder.glob(dcm2niixfile.with_suffix('').stem + '.*'):
oldfile.replace(newjsonfile.with_suffix(''.join(oldfile.suffixes)))

# Loop over all the newly produced json sidecar-files and adapt the data (NB: assumes every NIfTI-file comes with a json-file)
for jsonfile in sorted(set(jsonfiles)):
for jsonfile in sorted(jsonfiles):

# Load / copy over the source meta-data
metadata = bids.updatemetadata(sourcefile, jsonfile, run['meta'], options['meta'], datasource)
Expand Down