Description
If a user specifies a zero-padded entity in their filename (e.g., run-01) but provides the standard integer equivalent in their JSON sidecar (e.g., "run": 1), BIDSLayout crashes entirely with a BIDSConflictingValuesError during indexing.
The Bug
When BIDSLayout checks for overlapping entities between the filename and the JSON sidecar, it uses a naive string comparison (src/bids/layout/index.py around line 520):
if str(md_val) != file_val:
raise BIDSConflictingValuesError(...)
Because file_val is parsed as the string "01" directly from the filename, and md_val is parsed as the integer 1 from the JSON sidecar, the expression evaluates "1" != "01" as True. This raises a fatal exception that prevents the layout from initializing, even though the values are numerically identical.
Steps to Reproduce
- Create a minimal dataset with a file named
sub-01_task-rest_run-01_bold.nii.gz.
- Add a JSON sidecar
sub-01_task-rest_run-01_bold.json containing {"run": 1}.
- Attempt to initialize the layout:
layout = BIDSLayout(dset_dir).
Expected behavior: PyBIDS should recognize they are numerically equivalent.
Actual behavior: Crashes with BIDSConflictingValuesError: Conflicting values found for entity 'run'... (value='01') versus its JSON sidecar (value='1').
Suggested Fix
PyBIDS should attempt to coerce both values to a common numeric type (or utilize the internal entity.astype logic) to verify if they are numerically equivalent before raising a genuine user conflict error.
Description
If a user specifies a zero-padded entity in their filename (e.g.,
run-01) but provides the standard integer equivalent in their JSON sidecar (e.g.,"run": 1),BIDSLayoutcrashes entirely with aBIDSConflictingValuesErrorduring indexing.The Bug
When
BIDSLayoutchecks for overlapping entities between the filename and the JSON sidecar, it uses a naive string comparison (src/bids/layout/index.pyaround line 520):Because
file_valis parsed as the string"01"directly from the filename, andmd_valis parsed as the integer1from the JSON sidecar, the expression evaluates"1" != "01"asTrue. This raises a fatal exception that prevents the layout from initializing, even though the values are numerically identical.Steps to Reproduce
sub-01_task-rest_run-01_bold.nii.gz.sub-01_task-rest_run-01_bold.jsoncontaining{"run": 1}.layout = BIDSLayout(dset_dir).Expected behavior: PyBIDS should recognize they are numerically equivalent.
Actual behavior: Crashes with
BIDSConflictingValuesError: Conflicting values found for entity 'run'... (value='01') versus its JSON sidecar (value='1').Suggested Fix
PyBIDS should attempt to coerce both values to a common numeric type (or utilize the internal
entity.astypelogic) to verify if they are numerically equivalent before raising a genuine user conflict error.