Skip to content

Conversation

@tsalo
Copy link
Member

@tsalo tsalo commented Feb 27, 2025

Closes #403.

In order to make this work without relying entirely on string replacement to find metadata, we need to index metadata in the BIDSLayout. Not sure how much that slows things down on large datasets with current versions of pybids. I worked around this by loading sidecars directly, but it means that it won't work with inheritance (which I think is true of CuBIDS in general, unfortunately).

Changes proposed in this pull request

  • Create new CLI subcommand, cubids add-file-collections, to add file collection metadata to files in dataset.
  • Create functions to add file collection metadata. These functions loop over NIfTI files in the dataset and look for sets of files that share everything except file collection-defining entities, such as echo, flip, etc. The functions then look up the corresponding metadata (EchoTime for echo) and build a list of values across the collection (e.g., EchoTimes = [0.01, 0.02, 0.03]), which get added to the metadata for all of the files in the collection.

@tsalo tsalo added the enhancement New feature or request label Feb 27, 2025
@tsalo tsalo marked this pull request as ready for review February 27, 2025 20:45
out_metadata["FileCollection"] = [get_bidsuri(f.path, layout.root) for f in files]

files_metadata = [f.get_metadata() for f in files]
assert all(bool(meta) for meta in files_metadata), files
Copy link
Member Author

Choose a reason for hiding this comment

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

Raises an error if no metadata are available for any of the files.

Comment on lines +1058 to +1064
file_collection_entities = {
"echo": "EchoTime",
"part": None,
"mt": "MTState",
"inv": "InversionTime",
"flip": "FlipAngle",
}
Copy link
Member Author

@tsalo tsalo Feb 27, 2025

Choose a reason for hiding this comment

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

This relies on a hardcoded list of entities that indicate file collections and their corresponding metadata fields. It also does not work for file collections that are encoded with the acq entity or different suffixes, like TB1AFI (which differentiates files with acq-tr1/acq-tr2), MP2RAGE (which has both _MP2RAGE and _UNIT1 images from the same scan), or phase-difference field maps (which have suffixes like magnitude1, magnitude2, phasediff, phase1, and phase2).

Copy link
Contributor

Choose a reason for hiding this comment

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

should dir be included?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, you need to have separate acquisitions to get different phase encoding directions

Copy link
Contributor

@singlesp singlesp left a comment

Choose a reason for hiding this comment

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

I reviewed the changes and they look really good to me.

Copy link
Contributor

@tien-tong tien-tong left a comment

Choose a reason for hiding this comment

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

Looks good to me.

I don't see add_file_collections in the API section in RTD https://cubids--445.org.readthedocs.build/en/445/api.html -- does it have to be, because it's already in the CLI? But it's weird that the API seemed to not be updated automatically.

Also, the issue describes this function well #403. I think this should be added to the RTD.

@tsalo tsalo changed the title Collect file collection metadata Add file collection metadata as array-type fields to JSONs Apr 4, 2025
@tsalo
Copy link
Member Author

tsalo commented Apr 4, 2025

One thing I just noticed- I make a point that index_metadata needs to be changed to True for this to work, but I reverted that change in the final version, so I think I need to update the documentation a bit.

We don't need to index metadata in the BIDSLayout, but the drawback is that inheritance is ignored.
@tsalo
Copy link
Member Author

tsalo commented Apr 4, 2025

I've updated the relevant documentation (docstrings and this PR summary), so I'll merge once CI passes.

@tsalo
Copy link
Member Author

tsalo commented Apr 4, 2025

Oh wait sorry I missed the thing about the API docs. I'll fix that before I merge.

@tsalo
Copy link
Member Author

tsalo commented Apr 4, 2025

Okay added.

@tsalo tsalo merged commit edf7a94 into main Apr 4, 2025
10 checks passed
@tsalo tsalo deleted the file-collection-metadata branch April 4, 2025 13:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add file collection metadata as array-type fields to JSONs

4 participants