Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve order of entries in expand method #348

Merged
merged 1 commit into from
Dec 11, 2023
Merged
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
5 changes: 3 additions & 2 deletions snakebids/core/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def expand(
allow_missing=allow_missing
if isinstance(allow_missing, bool)
else list(itx.always_iterable(allow_missing)),
**{self.entity: list(set(self._data))},
**{self.entity: list(dict.fromkeys(self._data))},
**{
wildcard: list(itx.always_iterable(v))
for wildcard, v in wildcards.items()
Expand Down Expand Up @@ -389,7 +389,8 @@ def sequencify(item: bool | str | Iterable[str]) -> bool | list[str]:

allow_missing_seq = sequencify(allow_missing)
inner_expand = list(
set(
# order preserving deduplication
dict.fromkeys(
sn_expand(
list(itx.always_iterable(paths)),
zip,
Expand Down
13 changes: 13 additions & 0 deletions snakebids/tests/test_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,19 @@ def test_expand_deduplicates_paths(self, component: Expandable):
paths = component.expand(path_tpl)
assert len(paths) == len(set(paths))

@given(
component=sb_st.expandables(
restrict_patterns=True,
path_safe=True,
unique=True,
)
)
def test_expand_preserves_entry_order(self, component: Expandable):
path_tpl = bids(**get_wildcard_dict(component.zip_lists))
paths = component.expand(path_tpl)
for path, entity_vals in zip(paths, zip(*component.zip_lists.values())):
assert bids(**dict(zip(component.zip_lists.keys(), entity_vals))) == path


class TestBidsComponentExpand:
"""
Expand Down
9 changes: 6 additions & 3 deletions typings/bids/layout/utils.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
This type stub file was generated by pyright.
"""

"""Miscellaneous layout-related utilities."""
from typing import Sequence
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More of a comment here given this was generated by pyright, but we should look to switch use of abstract base classes in the code base to import from collections.abc. I think a number of these are deprecated from the typing module as of py3.9+.

Related - I came across this a while back, which may be helpful in upgrading syntax, etc: https://github.com/asottile/pyupgrade

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ruff handles these upgrades, so I've been letting it handle it. So may plan was to wait until we drop py38 next year, then ruff should inform us of all these deprecations.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool - I didn't know that. Just took a quick look at the ruff source code and it looks like they're using pyupgrade under the hood too.


class BIDSMetadata(dict):
"""Metadata dictionary that reports the associated file on lookup failures."""
Expand Down Expand Up @@ -66,8 +66,11 @@ class PaddedInt(int):
def __hash__(self) -> int: ...

def parse_file_entities(
filename, entities=..., config=..., include_unmatched=...
): # -> dict[Unknown, Unknown]:
filename: str,
entities: Sequence[str] = ...,
config: str = ...,
include_unmatched: bool = ...,
) -> dict[str, str]:
"""Parse the passed filename for entity/value pairs.

Parameters
Expand Down