childmindresearch / bids2table

Efficiently index large-scale BIDS neuroimaging datasets and derivatives
https://childmindresearch.github.io/bids2table/
MIT License
13 stars 5 forks source link

`example/example.ipynb` fails due to `KeyError` #28

Closed maouw closed 1 month ago

maouw commented 4 months ago

https://github.com/childmindresearch/bids2table/blob/c3f4c12d97eb7bfb8f18da8ce4150ec3a2348493/example/example.ipynb fails at https://github.com/childmindresearch/bids2table/blob/c3f4c12d97eb7bfb8f18da8ce4150ec3a2348493/example/example.ipynb#L2627-L2637 .

Code:

filtered = (
    tab
    .filter("task", contains="rest")
    .filter("sub", items=["04", "08"])
    .filter("RepetitionTime", 2.5)
)

filtered

Output:


{
    "name": "KeyError",
    "message": "'Invalid key RepetitionTime; expected a valid BIDS entity or metadata field present in the dataset'",
    "stack": "---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File ../bids2table/table.py:184, in BIDSTable.filter(self, key, value, items, contains, regex, func)
    183 if key[:1].isupper():
--> 184     col = self.flat_meta[key]
    185 # Long name entity

File ../.venv/lib/python3.12/site-packages/pandas/core/frame.py:4102, in DataFrame.__getitem__(self, key)
   4101     return self._getitem_multilevel(key)
-> 4102 indexer = self.columns.get_loc(key)
   4103 if is_integer(indexer):

File ../.venv/lib/python3.12/site-packages/pandas/core/indexes/range.py:417, in RangeIndex.get_loc(self, key)
    416 if isinstance(key, Hashable):
--> 417     raise KeyError(key)
    418 self._check_indexing_error(key)

KeyError: 'RepetitionTime'

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
Cell In[14], line 5
      1 filtered = (
      2     tab
      3     .filter(\"task\", contains=\"rest\")
      4     .filter(\"sub\", items=[\"04\", \"08\"])
----> 5     .filter(\"RepetitionTime\", 2.5)
      6 )
      8 filtered

File ../bids2table/bids2table/table.py:192, in BIDSTable.filter(self, key, value, items, contains, regex, func)
    190         col = self.flat[key]
    191 except KeyError as exc:
--> 192     raise KeyError(
    193         f\"Invalid key {key}; expected a valid BIDS entity or metadata field \"
    194         \"present in the dataset\"
    195     ) from exc
    197 if value is not None:
    198     mask = col == value

KeyError: 'Invalid key RepetitionTime; expected a valid BIDS entity or metadata field present in the dataset'"
}
clane9 commented 4 months ago

Hi @maouw, thanks for bringing up this issue! I'm struggling to reproduce it. The files in bids-examples that should be returned by this filter are:

synthetic/derivatives/fmriprep/sub-04/ses-01/func/sub-04_ses-01_task-rest_space-MNI152NLin2009cAsym_desc-preproc_bold.nii
synthetic/derivatives/fmriprep/sub-04/ses-01/func/sub-04_ses-01_task-rest_space-T1w_desc-preproc_bold.nii
synthetic/derivatives/fmriprep/sub-04/ses-02/func/sub-04_ses-02_task-rest_space-MNI152NLin2009cAsym_desc-preproc_bold.nii
synthetic/derivatives/fmriprep/sub-04/ses-02/func/sub-04_ses-02_task-rest_space-T1w_desc-preproc_bold.nii
synthetic/sub-04/ses-01/func/sub-04_ses-01_task-rest_bold.nii
synthetic/sub-04/ses-02/func/sub-04_ses-02_task-rest_bold.nii

Can you confirm that these files are present in your bids-examples directory?

What is the shape of the full table (printed on cell 5)? It should be (10266, 40).

clane9 commented 1 month ago

Closing since not able to reproduce. We can reopen later if it appears again.