diff --git a/src/bids/layout/index.py b/src/bids/layout/index.py index 09c9f415..125ce550 100644 --- a/src/bids/layout/index.py +++ b/src/bids/layout/index.py @@ -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 " diff --git a/src/bids/layout/tests/test_layout.py b/src/bids/layout/tests/test_layout.py index b51c04e2..3119114b 100644 --- a/src/bids/layout/tests/test_layout.py +++ b/src/bids/layout/tests/test_layout.py @@ -13,6 +13,7 @@ from bids.exceptions import ( BIDSChildDatasetError, + BIDSConflictingValuesError, BIDSDerivativesValidationError, BIDSValidationError, NoMatchError, @@ -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