Skip to content

Commit

Permalink
Simplify updatemetadata()
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelzwiers committed Jan 18, 2024
1 parent 2b92fd2 commit e3eb818
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
9 changes: 6 additions & 3 deletions bidscoin/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -1989,23 +1989,26 @@ def rename_runless_to_run1(runs: List[dict], scans_table: pd.DataFrame) -> None:
run['datasource'].targets.remove(target)
run['datasource'].targets.append((outfolder/run1_bidsname).with_suffix(suffixes))

def updatemetadata(sourcemeta: Path, targetmeta: Path, usermeta: dict, extensions: list, datasource: DataSource) -> dict:

def updatemetadata(datasource: DataSource, targetmeta: Path, usermeta: dict, extensions: list, sourcemeta: Path = Path()) -> dict:
"""
Load the metadata from the target (json sidecar), then add metadata from the source (json sidecar) and finally add
the user metadata (meta table). Source metadata other than json sidecars are copied over to the target folder. Special
dynamic <<session>> values are replaced with the session label, and unused B0-field tags are removed
NB: In future versions this function could also support more source metadata formats, e.g. yaml, csv- or Excel-files
:param sourcemeta: The filepath of the source data file with associated/equally named meta-data files (name may include wildcards)
:param datasource: The data source from which dynamic values are read
:param targetmeta: The filepath of the target data file with meta-data
:param usermeta: A user metadata dict, e.g. the meta table from a run-item
:param extensions: A list of file extensions of the source metadata files, e.g. as specified in bidsmap['Options']['plugins']['plugin']['meta']
:param datasource: The data source from which dynamic values are read
:param sourcemeta: The filepath of the source data file with associated/equally named meta-data files (name may include wildcards). Leave empty to use datasource.path
:return: The combined target + source + user metadata
"""

metapool = {}
if not sourcemeta.name:
sourcemeta = datasource.path

# Add the target metadata to the metadict
if targetmeta.is_file():
Expand Down
2 changes: 1 addition & 1 deletion bidscoin/plugins/dcm2niix2bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ def bidscoiner_plugin(session: Path, bidsmap: dict, bidsses: Path) -> Union[None
for jsonfile in sorted(jsonfiles):

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

# Remove the bval/bvec files of sbref- and inv-images (produced by dcm2niix but not allowed by the BIDS specifications)
if (datasource.datatype=='dwi' and suffix=='sbref') or (datasource.datatype=='fmap' and suffix=='epi'):
Expand Down
2 changes: 1 addition & 1 deletion bidscoin/plugins/nibabel2bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def bidscoiner_plugin(session: Path, bidsmap: dict, bidsses: Path) -> None:

# Load / copy over the source meta-data
sidecar = bidsfile.with_suffix('').with_suffix('.json')
metadata = bids.updatemetadata(sourcefile, sidecar, run['meta'], meta, datasource)
metadata = bids.updatemetadata(datasource, sidecar, run['meta'], meta)
with sidecar.open('w') as json_fid:
json.dump(metadata, json_fid, indent=4)

Expand Down
2 changes: 1 addition & 1 deletion bidscoin/plugins/spec2nii2bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def bidscoiner_plugin(session: Path, bidsmap: dict, bidsses: Path) -> Union[None
datasource.targets += outfolder.glob(f"{bidsname}.*[!json]")

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

Expand Down

0 comments on commit e3eb818

Please sign in to comment.