Skip to content

Commit

Permalink
Prevent _run-{values} with leading 0s
Browse files Browse the repository at this point in the history
When used with `run`, the leading 0 gets stripped because run is
converted into an int. This breaks tests that instantiate a datset on
the filesystem and re-parse it using generate_inputs.

Adds a `type` field to run in `bids_tags.json` and uses this to filter
integers with leading 0s in the zip_lists strategy
  • Loading branch information
pvandyken committed May 9, 2024
1 parent 156f58f commit 9f818bb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
3 changes: 2 additions & 1 deletion snakebids/resources/bids_tags.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"run": {
"tag": "run",
"before": "[_/\\\\]+run-",
"match": "[0-9]{1,5}"
"match": "[0-9]{1,5}",
"type": "int"
},
"proc": {
"tag": "proc",
Expand Down
12 changes: 11 additions & 1 deletion snakebids/tests/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,20 @@ def zip_lists(
)
)

def filter_ints(type_: str | None):
def inner(s: str):
if type_ == "int":
return int(s) > 0 and not s.startswith("0")
return True

return inner

values = {
entity: draw(
st.lists(
bids_value(entity.match if restrict_patterns else ".*"),
bids_value(entity.match if restrict_patterns else ".*").filter(
filter_ints(entity.type if restrict_patterns else None)
),
min_size=min_values,
max_size=max_values,
unique=(cull or unique),
Expand Down
11 changes: 11 additions & 0 deletions snakebids/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,17 @@ def wildcard(self) -> str:
return self.entity
return self.tag

@property
def type(self) -> str | None:
"""Get the type of the entity.
Returns None if type unspecified.
"""
tags = read_bids_tags()
# See note in .before
_def: dict[Any, Any] = {}
return tags.get(self.entity, _def).get("type")

@classmethod
def from_tag(cls, tag: str) -> BidsEntity:
"""Return the entity associated with the given tag, if found.
Expand Down

0 comments on commit 9f818bb

Please sign in to comment.