Skip to content

Commit 52e6f8c

Browse files
committed
Minor tweaks / fixes
1 parent 80d9c5e commit 52e6f8c

File tree

4 files changed

+22
-16
lines changed

4 files changed

+22
-16
lines changed

bidscoin/bidscoiner.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,11 @@ def addmetadata(bidsses: Path, subid: str, sesid: str) -> None:
324324
for fmap in fmaps:
325325

326326
# Load the existing meta-data
327+
jsondata = {}
327328
jsonfile = bidsses/Path(fmap).with_suffix('').with_suffix('.json')
328-
with jsonfile.open('r') as sidecar:
329-
jsondata = json.load(sidecar)
329+
if jsonfile.is_file():
330+
with jsonfile.open('r') as sidecar:
331+
jsondata = json.load(sidecar)
330332

331333
# Search for the imaging files that match the IntendedFor search criteria
332334
intendedfor = jsondata.get('IntendedFor')
@@ -415,8 +417,9 @@ def addmetadata(bidsses: Path, subid: str, sesid: str) -> None:
415417
LOGGER.verbose(f"Adding EchoTime1: {echotime[0]} and EchoTime2: {echotime[1]} to {jsonfile}")
416418

417419
# Save the collected meta-data to disk
418-
with jsonfile.open('w') as sidecar:
419-
json.dump(jsondata, sidecar, indent=4)
420+
if jsondata:
421+
with jsonfile.open('w') as sidecar:
422+
json.dump(jsondata, sidecar, indent=4)
420423

421424

422425
def main():

bidscoin/plugins/dcm2niix2bids.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,6 @@ def bidscoiner_plugin(session: Path, bidsmap: Bidsmap, bidsses: Path) -> Union[N
245245
# Get a matching run from the bidsmap
246246
datasource = bids.DataSource(sourcefile, {'dcm2niix2bids': options}, dataformat)
247247
run, match = bids.get_matching_run(datasource, bidsmap, runtime=True)
248-
runindex = run['bids'].get('run', '')
249248

250249
# Check if we should ignore this run
251250
if datasource.datatype in bidsmap['Options']['bidscoin']['ignoretypes']:
@@ -271,6 +270,7 @@ def bidscoiner_plugin(session: Path, bidsmap: Bidsmap, bidsses: Path) -> Union[N
271270
ignore = bids.check_ignore(datasource.datatype, bidsignore)
272271
bidsname = bids.get_bidsname(subid, sesid, run, not ignore, runtime=True)
273272
ignore = ignore or bids.check_ignore(bidsname+'.json', bidsignore, 'file')
273+
runindex = bids.get_bidsvalue(bidsname, 'run')
274274
bidsname = bids.increment_runindex(outfolder, bidsname, run)
275275
bidsnames = set() # -> A store for all output targets for this bidsname
276276

@@ -312,7 +312,7 @@ def bidscoiner_plugin(session: Path, bidsmap: Bidsmap, bidsses: Path) -> Union[N
312312
filename = bidsname,
313313
outfolder = outfolder,
314314
source = source)
315-
if bcoin.run_command(command) and not next(outfolder.glob(f"{bidsname}.*"), None):
315+
if bcoin.run_command(command) and not next(outfolder.glob(f"{bidsname}*"), None):
316316
continue
317317

318318
# Collect the bidsname
@@ -352,10 +352,10 @@ def bidscoiner_plugin(session: Path, bidsmap: Bidsmap, bidsses: Path) -> Union[N
352352
# Strip each dcm2niix postfix and assign it to bids entities in a newly constructed bidsname
353353
ext = ''.join(dcm2niixfile.suffixes)
354354
postfixes = dcm2niixfile.name.split(bidsname)[1].rsplit(ext)[0].split('_')[1:]
355-
newbidsname = bids.insert_bidskeyval(dcm2niixfile.name, 'run', bids.sanitize(runindex), ignore) # Restart the run-index. NB: Unlike bidsname, newbidsname has a file extension
355+
newbidsname = bids.insert_bidskeyval(dcm2niixfile.name, 'run', runindex, ignore) # Restart the run-index. NB: Unlike bidsname, newbidsname has a file extension
356356
for postfix in postfixes:
357357

358-
# Patch the echo entity in the newbidsname with the dcm2niix echo info # NB: We can't rely on the bids-entity info here because manufacturers can e.g. put multiple echos in one series / run-folder
358+
# Patch the echo entity in the newbidsname with the dcm2niix echo info # NB: We can't rely on the bids-entity info here because manufacturers can e.g. put multiple echos in one series / run-folder
359359
if 'echo' in run['bids'] and postfix.startswith('e'):
360360
echonr = f"_{postfix}".replace('_e','') # E.g. postfix='e1'
361361
if not echonr:
@@ -445,8 +445,8 @@ def bidscoiner_plugin(session: Path, bidsmap: Bidsmap, bidsses: Path) -> Union[N
445445
outputfiles = [file for file in outfolder.glob(f"{bidsname}.*") if file.suffix in ('.nii','.gz')]
446446
assert len(outputfiles) == 1
447447
outputfile = outputfiles[0]
448-
except AssertionError:
449-
LOGGER.error(f"Unexpected conversion result, no output files: {outfolder/bidsname}.*")
448+
except AssertionError as outputerror:
449+
LOGGER.error(f"Unexpected conversion result(s): {outfolder/bidsname}.*\n{outputerror}")
450450
continue
451451

452452
# Load / copy over the source meta-data
@@ -473,8 +473,9 @@ def bidscoiner_plugin(session: Path, bidsmap: Bidsmap, bidsses: Path) -> Union[N
473473
bfile.unlink()
474474

475475
# Save the meta-data to the json sidecar-file
476-
with jsonfile.open('w') as json_fid:
477-
json.dump(metadata, json_fid, indent=4)
476+
if metadata:
477+
with jsonfile.open('w') as json_fid:
478+
json.dump(metadata, json_fid, indent=4)
478479

479480
# Parse the acquisition time from the source header or else from the json file (NB: assuming the source file represents the first acquisition)
480481
if not ignore and not suffix in bids.get_derivatives(datasource.datatype, exceptions):

bidscoin/plugins/nibabel2bids.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,9 @@ def bidscoiner_plugin(session: Path, bidsmap: Bidsmap, bidsses: Path) -> None:
235235
# Load / copy over the source meta-data
236236
sidecar = bidsfile.with_suffix('').with_suffix('.json')
237237
metadata = bids.updatemetadata(datasource, sidecar, run['meta'], meta)
238-
with sidecar.open('w') as json_fid:
239-
json.dump(metadata, json_fid, indent=4)
238+
if metadata:
239+
with sidecar.open('w') as json_fid:
240+
json.dump(metadata, json_fid, indent=4)
240241

241242
# Add an entry to the scans_table (we typically don't have useful data to put there)
242243
# TODO: Add check for derivative and ignore (as in other plugins)

bidscoin/plugins/spec2nii2bids.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,9 @@ def bidscoiner_plugin(session: Path, bidsmap: Bidsmap, bidsses: Path) -> Union[N
259259

260260
# Load / copy over and adapt the newly produced json sidecar-file (NB: assumes every NIfTI-file comes with a json-file)
261261
metadata = bids.updatemetadata(datasource, sidecar, run['meta'], options['meta'])
262-
with sidecar.open('w') as json_fid:
263-
json.dump(metadata, json_fid, indent=4)
262+
if metadata:
263+
with sidecar.open('w') as json_fid:
264+
json.dump(metadata, json_fid, indent=4)
264265

265266
# Parse the acquisition time from the source header or else from the json file (NB: assuming the source file represents the first acquisition)
266267
suffix = datasource.dynamicvalue(run['bids']['suffix'], True, True)

0 commit comments

Comments
 (0)