Repository navigation
Add TemporaryBidsDir for runnable BIDS examples - #987
Conversation
|
Hey @OBagnell, thanks a lot for contributing! What do you think of creating a The current example would look like this: from tempfile import TemporaryDirectory
description = {
"Name": "Toy image-loading example",
"BIDSVersion": "1.10.0",
"DatasetType": "raw",
}
metadata = pd.DataFrame(
{
"participant_id": ["sub-001", "sub-002"],
"session_id": ["ses-M000", "ses-M000"],
"age": [55.0, 62.0],
"diagnosis": ["control", "patient"],
}
)
with TemporaryBidsDir(description, metadata) as bids_directory:
for participant in metadata["participant_id"]:
anat = bids_directory / participant / "ses-M000" / "anat"
anat.mkdir(parents=True)
image = (in (np.zeros((8, 8, 8), dtype=np.float32), np.eye(4))
nib.save(image, anat / f"{participant}_ses-M000_T1w.nii.gz")
dataset = BidsDataset(
bids=bids_directory,
file_type=BidsFileType(data_type="anat", suffix="T1w"),
data=bids_directory / "metadata.tsv",
)The example in #985, would look like this: description = {
"Name": "Toy directory navigation example",
"BIDSVersion": "1.10.0",
"DatasetType": "raw",
}
with TemporaryBidsDir(description, metadata=None) as bids_directory:
(bids_directory / "sub-01" / "ses-M000").mkdir(parents=True)
bids = Bids(bids_directory)
print(sorted(bids.get_all_participants_sessions()))We could go even further and implement the image creation inside this context manager: with TemporaryBidsDir(description, metadata, with_images={"anat": "T1w"}) as bids_directory:
dataset = BidsDataset(
bids=bids_directory,
file_type=BidsFileType(data_type="anat", suffix="T1w"),
data=bids_directory / "metadata.tsv",
)As you can see, this makes the example cleaner, with all the setup hidden in the context manager. The argument For example, in order to save T1w and PET images, I would do: with TemporaryBidsDir(description, metadata, with_images={"anat": "res-1x1x1_T1w", "pet": "trc-18FFDG_pet"}) as bids_directory:
...Which will create the following BIDS: Finally, a last interesting feature could be to let the user define the size of the images generated. This would lead to something like this: with TemporaryBidsDir(description, metadata, with_images={"anat": "T1w"}, image_shape=(1, 8, 8, 8)) as bids_directory:
...What do you think? |
|
|
Thanks! Alzheimer’s research is important to me, and I’m glad to help improve the tools supporting it. |
thibaultdvx
left a comment
There was a problem hiding this comment.
Hey @OBagnell, thanks for the great contribution! Here are some suggestions of improvement.
FIY, you can easily commit my suggestions by clicking on the button "Apply suggestion".
And by the way, could you please allow maintainers to edit your PR next time? It facilitates collaboration ;)
Apply thibaultdvx's requested guide and docstring improvements. Prepared and validated with OpenAI Codex assistance.
|
Failing test (unittest Python 3.14 on Ubuntu) is unrelated. I merge. |
Related issues
Partially addresses #953. Implements the TemporaryBidsDir follow-up suggested in #987 (comment).
Key changes made
clinicadl.io.bids.TemporaryBidsDir, inheritingTemporaryDirectoryand returning aPathfrom its context.dataset_description.jsonand optionalmetadata.tsv; optionally generate zero-valued NIfTI images for every participant/session pair and requested data type.res-1x1x1_T1wandtrc-18FFDG_pet, and configurable(channels, x, y, z)image shapes. Use TorchIO to preserve the channel/spatial convention when saving.Type of changes
Checklist
make unit-tests)cd docs && make html)Validation
6f5f5e5(threeTestBidsFileType.test_3cases andTestClinicaPipelines.test_dwi). No unrelated fixes included.git diff --checkpass.The full application test suite and Sphinx build were not run locally; upstream checks remain required. No new dependencies, patient data, downloads in the examples, training or scientific algorithm changes.
AI usage disclosure
I used OpenAI Codex assistance for:
Introduce yourself
I'm a returning contributor helping make the research tooling easier to try.