Skip to content

Commit

Permalink
Move common metadata to top of JSON (#875)
Browse files Browse the repository at this point in the history
* Move common metadata to top of JSON.

* Update bids.py

* Update bids.py

* Update bids.py

* Apply suggestions from code review

Co-authored-by: Matt Cieslak <[email protected]>

* Update bids.py

---------

Co-authored-by: Matt Cieslak <[email protected]>
  • Loading branch information
tsalo and mattcieslak authored Nov 15, 2024
1 parent 74f7058 commit dcb0f30
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions qsiprep/utils/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,18 +435,40 @@ def scan_groups_to_sidecar(scan_groups):
"""Create a sidecar that reflects how the preprocessed image was created."""

# Add the information about how the images were grouped and which fieldmaps were used
derivatives_metadata = {"scan_grouping": scan_groups}
derivatives_metadata = {"ScanGrouping": scan_groups}

# Get metadata from the individual scans that were combined to make this preprocessed image
concatenated_dwi_files = scan_groups.get("dwi_series")
fieldmap_info = scan_groups.get("fieldmap_info")
if fieldmap_info.get("suffix") == "rpe_series":
concatenated_dwi_files.extend(fieldmap_info.get("rpe_series", []))

scan_metadata = {}
for dwi_file in concatenated_dwi_files:
for i_file, dwi_file in enumerate(concatenated_dwi_files):
dwi_file_name = Path(dwi_file).name
scan_metadata[dwi_file_name] = config.execution.layout.get_metadata(dwi_file)
derivatives_metadata["source_metadata"] = scan_metadata
if i_file == 0:
common_metadata = scan_metadata[dwi_file_name]
else:
keys_to_remove = []
for key in common_metadata:
if key not in scan_metadata[dwi_file_name]:
keys_to_remove.append(key)
else:
val1 = common_metadata[key]
val2 = scan_metadata[dwi_file_name][key]
if isinstance(val1, float) and isinstance(val2, float):
if not np.isclose(val1, val2):
keys_to_remove.append(key)
elif val1 != val2:
keys_to_remove.append(key)

for key in keys_to_remove:
common_metadata.pop(key)

derivatives_metadata["SourceMetadata"] = scan_metadata
derivatives_metadata = {**common_metadata, **derivatives_metadata}
derivatives_metadata["Sources"] = sorted(scan_metadata.keys())
return derivatives_metadata


Expand Down

0 comments on commit dcb0f30

Please sign in to comment.