Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/bids/layout/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,15 @@ def create_association_pair(src, dst, kind, kind2=None):
# Skip pairs that were already found in the filenames
if tag_string in all_tags:
file_val = all_tags[tag_string]
if str(md_val) != file_val:
entity = all_entities.get(md_key)
values_match = str(md_val) == file_val
if (
entity is not None
and entity._dtype == 'int'
and not isinstance(md_val, bool)
):
values_match = entity._astype(file_val) == md_val
if not values_match:
msg = (
"Conflicting values found for entity '{}' in "
"filename {} (value='{}') versus its JSON sidecar "
Expand Down
32 changes: 32 additions & 0 deletions src/bids/layout/tests/test_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from bids.exceptions import (
BIDSChildDatasetError,
BIDSConflictingValuesError,
BIDSDerivativesValidationError,
BIDSValidationError,
NoMatchError,
Expand Down Expand Up @@ -965,6 +966,37 @@ def test_indexing_tag_conflict(tests_dir):
assert 'run' in str(exc.value)


@pytest.mark.parametrize('metadata_run', [1, 1.0])
def test_indexing_zero_padded_entity_matches_numeric_metadata(tmp_path, metadata_run):
(tmp_path / 'dataset_description.json').write_text(
json.dumps({'Name': 'test', 'BIDSVersion': '1.10.0'})
)
func_dir = tmp_path / 'sub-01' / 'func'
func_dir.mkdir(parents=True)
bold_file = func_dir / 'sub-01_task-rest_run-01_bold.nii.gz'
bold_file.touch()
bold_file.with_suffix('').with_suffix('.json').write_text(json.dumps({'run': metadata_run}))

layout = BIDSLayout(tmp_path)

assert layout.get(run=1, suffix='bold', extension='.nii.gz')


@pytest.mark.parametrize('metadata_run', [2, 1.5, True, '1'])
def test_indexing_zero_padded_entity_rejects_conflicting_metadata(tmp_path, metadata_run):
(tmp_path / 'dataset_description.json').write_text(
json.dumps({'Name': 'test', 'BIDSVersion': '1.10.0'})
)
func_dir = tmp_path / 'sub-01' / 'func'
func_dir.mkdir(parents=True)
bold_file = func_dir / 'sub-01_task-rest_run-01_bold.nii.gz'
bold_file.touch()
bold_file.with_suffix('').with_suffix('.json').write_text(json.dumps({'run': metadata_run}))

with pytest.raises(BIDSConflictingValuesError, match="entity 'run'"):
BIDSLayout(tmp_path)


def test_get_with_wrong_dtypes(layout_7t_trt):
"""Test automatic dtype sanitization."""
l = layout_7t_trt
Expand Down