Skip to content

[bug] BIDSFile.get_associations() returns wrong files for some entities #1253

Description

@MaximeBICMTL

Greetings,

I think I found a bug with the BIDSFile.get_associations() method, see the reproduction method below.

Version

Python 3.11
PyBIDS 0.22.0 (also reproduces on PyBIDS 0.17.0)

Reproduction

I have the following BIDS dataset:

/bids/
└── sub-SUB/
    └── ses-SES/
        ├── func/
        │   ├── sub-SUB_ses-SES_task-rest_echo-1_bold.nii.gz
        │   ├── sub-SUB_ses-SES_task-rest_echo-1_bold.json
        │   ├── sub-SUB_ses-SES_task-rest_echo-1_part-phase_bold.nii.gz
        │   └── sub-SUB_ses-SES_task-rest_echo-1_part-phase_bold.json
        │
        └── anat/
            ├── sub-SUB_ses-SES_acq-05mm_rec-deface_inv-1_MP2RAGE.nii.gz
            ├── sub-SUB_ses-SES_acq-05mm_rec-deface_inv-1_MP2RAGE.json
            ├── sub-SUB_ses-SES_acq-05mm_rec-deface_run-2_inv-1_MP2RAGE.nii.gz
            ├── sub-SUB_ses-SES_acq-05mm_rec-deface_run-2_inv-1_MP2RAGE.json
            ├── sub-SUB_ses-SES_acq-05mm_rec-deface_inv-2_MP2RAGE.nii.gz
            ├── sub-SUB_ses-SES_acq-05mm_rec-deface_inv-2_MP2RAGE.json
            ├── sub-SUB_ses-SES_acq-05mm_rec-deface_run-2_inv-2_MP2RAGE.nii.gz
            └── sub-SUB_ses-SES_acq-05mm_rec-deface_run-2_inv-2_MP2RAGE.json

And the following short script to get the associated files of each NIfTI file:

from bids import BIDSLayout
import sys

layout = BIDSLayout(sys.argv[1])
for f in layout.get(extension=["nii", "nii.gz"]):
    print(f.path)
    for assoc in f.get_associations():
        print("  ", assoc.path)

This outputs the following:

/bids/sub-SUB/ses-SES/func/sub-SUB_ses-SES_task-rest_echo-1_bold.nii.gz
   /bids/sub-SUB/ses-SES/func/sub-SUB_ses-SES_task-rest_echo-1_bold.json
   /bids/sub-SUB/ses-SES/func/sub-SUB_ses-SES_task-rest_echo-1_part-phase_bold.nii.gz
/bids/sub-SUB/ses-SES/func/sub-SUB_ses-SES_task-rest_echo-1_part-phase_bold.nii.gz
   /bids/sub-SUB/ses-SES/func/sub-SUB_ses-SES_task-rest_echo-1_bold.json
   /bids/sub-SUB/ses-SES/func/sub-SUB_ses-SES_task-rest_echo-1_bold.nii.gz
/bids/sub-SUB/ses-SES/anat/sub-SUB_ses-SES_acq-05mm_rec-deface_inv-1_MP2RAGE.nii.gz
   /bids/sub-SUB/ses-SES/anat/sub-SUB_ses-SES_acq-05mm_rec-deface_inv-1_MP2RAGE.json
   /bids/sub-SUB/ses-SES/anat/sub-SUB_ses-SES_acq-05mm_rec-deface_run-2_inv-1_MP2RAGE.nii.gz
/bids/sub-SUB/ses-SES/anat/sub-SUB_ses-SES_acq-05mm_rec-deface_inv-2_MP2RAGE.nii.gz
   /bids/sub-SUB/ses-SES/anat/sub-SUB_ses-SES_acq-05mm_rec-deface_inv-2_MP2RAGE.json
   /bids/sub-SUB/ses-SES/anat/sub-SUB_ses-SES_acq-05mm_rec-deface_run-2_inv-2_MP2RAGE.nii.gz

As we can see, the JSON sidecars returned for acquisitions with the part and run entities are not the correct ones.

EDIT: Actually this issue might be a duplicate of #530, though I guess this issue may be a more direct description?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions