Skip to content

Commit

Permalink
fix docstrings and update validate logic
Browse files Browse the repository at this point in the history
- remove mention of uncommenting lines
- rephrased "Enable by default..." to "Boilerplate starts with the
validator plugin enabled..." in the overview doc
- focused docstring of validation using pybids in `generate_inputs()`
- associte pybids validate logic to plugins.validator.skip
(--skip-bids-validation), which will make it simpler for downstream devs
  - left plugins.validator.success in as it can still be used for more
complex logic if desired
- instantiate validator plugin in boilerplate
- moved docstring under `__call__`
- replaced `__init__` with attr
- remove logic comments in Snakefile
- replace individual #noqa: PLR0913 calls to ignore under ruff
configuration
  • Loading branch information
kaitj committed Jun 22, 2023
1 parent 8c3d707 commit d733c9f
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 41 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,4 @@ reportImportCycles = false

[tool.ruff]
select = ["E", "F", "PL", "RUF"]
ignore = ["PLR0913"]
2 changes: 1 addition & 1 deletion snakebids/core/construct_bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Optional


def bids( # noqa: PLR0913
def bids(
root: Optional[str | Path] = None,
datatype: Optional[str] = None,
prefix: Optional[str] = None,
Expand Down
10 changes: 5 additions & 5 deletions snakebids/core/input_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@


@overload
def generate_inputs( # noqa: PLR0913
def generate_inputs(
bids_dir: Path | str,
pybids_inputs: InputsConfig,
pybidsdb_dir: Path | str | None = ...,
Expand All @@ -50,7 +50,7 @@ def generate_inputs( # noqa: PLR0913


@overload
def generate_inputs( # noqa: PLR0913
def generate_inputs(
bids_dir: Path | str,
pybids_inputs: InputsConfig,
pybidsdb_dir: Path | str | None = ...,
Expand All @@ -68,7 +68,7 @@ def generate_inputs( # noqa: PLR0913
...


def generate_inputs( # noqa: PLR0913
def generate_inputs(
bids_dir: Path | str,
pybids_inputs: InputsConfig,
pybidsdb_dir: Path | str | None = None,
Expand Down Expand Up @@ -150,8 +150,8 @@ def generate_inputs( # noqa: PLR0913
the default behaviour
validate
If False, skips validation of BIDS directory. Otherwise use node.js
implementation of bids-validator, falling back on pybids validation.
If True performs validation of BIDS directory using pybids, otherwise
skips validation.
Returns
-------
Expand Down
2 changes: 1 addition & 1 deletion snakebids/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ def __init__(self, misspecified_filter: str):
"{entity}={filter} or {entity}:{REQUIRED|OPTIONAL|NONE} (case-insensitive)."
)


class SnakebidsPluginError(Exception):
"""Exception raised when a Snakebids plugin encounters a problem"""
26 changes: 14 additions & 12 deletions snakebids/plugins/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import subprocess
import tempfile

import attr

from snakebids.app import SnakeBidsApp
from snakebids.exceptions import SnakebidsPluginError

Expand All @@ -13,21 +15,21 @@ class InvalidBidsError(SnakebidsPluginError):
"""Error raised if an input BIDS dataset is invalid."""


@attr.define
class BidsValidator:
"""Perform BIDS validation of dataset, falling back to the pybids
version of validation if the node-version of bids-validator is
not found.
Parameters
----------
app
Snakebids application to be run
"""

def __init__(self, raise_invalid_bids: bool = True) -> None:
self.raise_invalid_bids = raise_invalid_bids
raise_invalid_bids: bool = attr.field(default=True)

def __call__(self, app: SnakeBidsApp) -> None:
"""Perform BIDS validation of dataset, falling back to the pybids
version of validation if the node-version of bids-validator is
not found. If the validation of the dataset fails, an InvalidBidsError
is raised.
Parameters
----------
app
Snakebids application to be run
"""
# Skip bids validation
if app.config["plugins.validator.skip"]:
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,9 @@ parse_args:
# custom command-line parameters can then be added, these will get added to the config and also accessible to plugins
# below are examples for plugin and custom parameters (e.g. config['smoothing_fwhm'])
--skip_bids_validation:
help: 'Skip validation of BIDS dataset. BIDS validation is performed by
default using the bids-validator (if installed) or with the pybids
validator implementation (if bids-validator is not installed).
(default: %(default)s)'
help: 'Skip validation of BIDS dataset. BIDS validation is performed by
default using the bids-validator plugin (if installed/enabled) or with the pybids
validator implementation (if bids-validator is not installed/enabled).'
dest: "plugins.validator.skip"
action: "store_true"
default: False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ inputs = generate_inputs(
derivatives=config.get("derivatives", None),
participant_label=config.get("participant_label", None),
exclude_participant_label=config.get("exclude_participant_label", None),
validate=config.get("plugins.validator.success", False),
validate=not config.get("plugins.validator.skip", False)
)



#this adds constraints to the bids naming
wildcard_constraints: **get_wildcard_constraints(config['pybids_inputs'])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def get_parser():
def main():
app = SnakeBidsApp(
Path(__file__).resolve().parent.parent, # to get repository root
plugins=[BidsValidator],
plugins=[BidsValidator()],
)
app.run_snakemake()

Expand Down
4 changes: 2 additions & 2 deletions snakebids/tests/mock/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
},
"--skip_bids_validation": {
"help": "Skip validation of BIDS dataset. BIDS validation is performed by "
"default using the bids-validator (if installed) or with the pybids "
"validator implementation (if bids-validator is not installed). "
"default using the bids-validator plugin (if installed/enabled) or with the pybids "
"validator implementation (if bids-validator is not installed/enabled). "
"(default: %(default)s)",
"dest": "plugins.validator.skip",
"action": "store_true",
Expand Down
7 changes: 3 additions & 4 deletions snakebids/tests/mock/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,9 @@ parse_args:
nargs: '+'

--skip_bids_validation:
help: 'Skip validation of BIDS dataset. BIDS validation is performed by
default using the bids-validator (if installed) or with the pybids
validator implementation (if bids-validator is not installed).
(default: %(default)s)'
help: 'Skip validation of BIDS dataset. BIDS validation is performed by
default using the bids-validator plugin (if installed/enabled) or with the pybids
validator implementation (if bids-validator is not installed/enabled).'
dest: "plugins.validator.skip"
action: "store_true"
default: False
Expand Down
14 changes: 7 additions & 7 deletions snakebids/tests/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def _filter_invalid_entity_lists(entities: Container[BidsEntity | str]):


@st.composite
def bids_path( # noqa: PLR0913
def bids_path(
draw: st.DrawFn,
*,
root: PathLike[str] | str | None = None,
Expand Down Expand Up @@ -177,7 +177,7 @@ def inputs_configs() -> st.SearchStrategy[InputsConfig]:


@st.composite
def zip_lists( # noqa: PLR0913
def zip_lists(
draw: st.DrawFn,
*,
min_entities: int = 1,
Expand Down Expand Up @@ -232,7 +232,7 @@ def zip_lists( # noqa: PLR0913


@st.composite
def bids_component_row( # noqa: PLR0913
def bids_component_row(
draw: st.DrawFn,
*,
min_values: int = 1,
Expand Down Expand Up @@ -267,7 +267,7 @@ def bids_component_row( # noqa: PLR0913


@st.composite
def bids_partial_components( # noqa: PLR0913
def bids_partial_components(
draw: st.DrawFn,
*,
min_entities: int = 1,
Expand Down Expand Up @@ -331,7 +331,7 @@ def bids_partial_components( # noqa: PLR0913


@st.composite
def bids_components( # noqa: PLR0913
def bids_components(
draw: st.DrawFn,
*,
min_entities: int = 1,
Expand Down Expand Up @@ -382,7 +382,7 @@ def bids_components( # noqa: PLR0913


@st.composite
def expandables( # noqa: PLR0913
def expandables(
draw: st.DrawFn,
*,
max_entities: int = 5,
Expand Down Expand Up @@ -481,7 +481,7 @@ def datasets(


@st.composite
def datasets_one_comp( # noqa: PLR0913
def datasets_one_comp(
draw: st.DrawFn,
*,
root: Optional[Path] = None,
Expand Down
2 changes: 1 addition & 1 deletion snakebids/tests/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_template_dry_runs_successfully(tmp_path: Path):
"path_bold": None,
"participant_label": None,
"exclude_participant_label": None,
"skip_bids_validation": True,
"plugins.validator.skip": True,
},
),
)
Expand Down

0 comments on commit d733c9f

Please sign in to comment.