Skip to content

BIDSLayout crashes with BIDSConflictingValuesError when zero-padded entities in filenames match integer entities in JSON sidecars #1255

Description

@anushkagupta200615-jpg

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

  1. Create a minimal dataset with a file named sub-01_task-rest_run-01_bold.nii.gz.
  2. Add a JSON sidecar sub-01_task-rest_run-01_bold.json containing {"run": 1}.
  3. 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.

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