Skip to content

Commit

Permalink
Deduplicate wildcards when parsing components
Browse files Browse the repository at this point in the history
Specifying a wildcard multiple times leads to an unintuitive KeyError.
This includes re-specifying a wildcard defined in the config by the
developer.

Fix by using a set to deduplicate wildcards before parsing.

Resolves #422
  • Loading branch information
pvandyken committed Jul 26, 2024
1 parent 96fb5e1 commit 00e5629
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion snakebids/core/input_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ def _get_component(
for img in matching_files:
wildcards: list[str] = [
wildcard
for wildcard in component.get("wildcards", [])
for wildcard in set(component.get("wildcards", []))
if wildcard in img.entities
]
_logger.debug("Wildcards %s found entities for %s", wildcards, img.path)
Expand Down
29 changes: 29 additions & 0 deletions snakebids/tests/test_generate_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,35 @@ def test_filter_with_invalid_method_raises_error(self, tmpdir: Path, method: str
generate_inputs(tmpdir, pybids_inputs)


@settings(
deadline=800,
suppress_health_check=[
HealthCheck.function_scoped_fixture,
HealthCheck.too_slow,
],
max_examples=1,
)
@given(dataset=sb_st.datasets_one_comp(unique=True))
def test_duplicate_wildcards_does_not_create_error(
dataset: BidsDataset, bids_fs: Path, fakefs_tmpdir: Path
):
root = tempfile.mkdtemp(dir=fakefs_tmpdir)
rooted = BidsDataset.from_iterable(
attrs.evolve(comp, path=os.path.join(root, comp.path))
for comp in dataset.values()
)
create_dataset(Path("/"), rooted)
config = create_snakebids_config(dataset)
wildcards = itx.first(config.values()).get("wildcards", [])
wildcards.append(wildcards[0])
reindexed = generate_inputs(
root,
config,
)
assert reindexed == rooted
assert reindexed.layout is not None


class TestAbsentConfigEntries:
def get_entities(self, root: Path):
# Generate directory
Expand Down

0 comments on commit 00e5629

Please sign in to comment.