Skip to content

Commit

Permalink
RF: Initial steps to start RFing validate-bids to use ValidationResul…
Browse files Browse the repository at this point in the history
…t records
  • Loading branch information
yarikoptic committed Aug 26, 2022
1 parent 84b6035 commit 72a51fb
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 47 deletions.
46 changes: 7 additions & 39 deletions dandi/bids_utils.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,14 @@
from .utils import pluralize
from .validate import ValidationResult


def is_valid(
validation_result: dict,
allow_invalid_filenames: bool = False,
allow_missing_files: bool = False,
) -> bool:
"""Determine whether a dataset validation result marks it as valid.
Parameters
----------
validation_result: dict
Dictionary as returned by `dandi.support.bids.validator.validate_bids()`.
allow_missing_files: bool, optional
Whether to consider the dataset invalid if any mandatory files are not present.
allow_invalid_filenames: bool, optional
Whether to consider the dataset invalid if any filenames inside are invalid.
Returns
-------
bool: whether the dataset validation result marks it as valid.
"""

if allow_invalid_filenames and allow_missing_files:
return True
missing_files = [
i["regex"] for i in validation_result["schema_tracking"] if i["mandatory"]
]
invalid_filenames = validation_result["path_tracking"]

if missing_files and not allow_missing_files:
return False
if invalid_filenames and not allow_invalid_filenames:
return False
else:
return True


def report_errors(
validation_result: dict,
def print_validation_results(
validation_result: list[ValidationResult],
# TODO: options for control
# - either report warnings, hints, ...
# - either report groupped by severity, record.id
) -> None:
raise NotImplementedError("TODO: RF to use ValidationResult records")
import click

missing_files = [
Expand Down
14 changes: 9 additions & 5 deletions dandi/cli/cmd_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,23 @@ def validate_bids(
dandi validate-bids /my/path
"""

from ..bids_utils import is_valid, report_errors
from ..bids_utils import print_validation_results
from ..validate import Severity
from ..validate import validate_bids as validate_bids_

validator_result = validate_bids_(
validator_result = validate_bids_( # Controller
*paths,
report=report,
report_path=report_path,
schema_version=schema,
)
valid = is_valid(validator_result)
report_errors(validator_result)

if not valid:
if validator_result:
print_validation_results(validator_result) # View

validation_errors = [e for e in validator_result if e.severity == Severity.ERROR]

if validation_errors:
raise SystemExit(1)


Expand Down
1 change: 1 addition & 0 deletions dandi/files/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def _validate(self) -> None:
bids_paths = [str(self.filepath)] + [
str(asset.filepath) for asset in self.dataset_files
]
# TODO: use RFed data structures, avoid duplicating logic
results = validate_bids(*bids_paths)
self._dataset_errors: list[str] = []
if len(results["path_listing"]) == len(results["path_tracking"]):
Expand Down
43 changes: 41 additions & 2 deletions dandi/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def validate_bids(
schema_version: Optional[str] = None,
report: bool = False,
report_path: str = "",
) -> dict:
) -> list[ValidationResult]:
"""Validate BIDS paths.
Parameters
Expand All @@ -67,6 +67,7 @@ def validate_bids(
patterns.
"""

import bidsschematools
from bidsschematools.validator import validate_bids as validate_bids_

if report and not report_path:
Expand All @@ -80,7 +81,45 @@ def validate_bids(
schema_version=schema_version,
report_path=report_path,
)
return dict(validation_result)
our_validation_result = []
origin = ValidationOrigin(
name="bidsschematools",
version=bidsschematools.__version__,
)
for path in validation_result["path_tracking"]:
our_validation_result.append(
ValidationResult(
origin=origin,
severity=Severity.ERROR,
id="BIDS.WRONG_PATH_TODO",
scope=Scope.FILE,
path=path,
message="TODO",
# TODO - discover dandiset or actually BIDS dataset
# might want separate the two
# dandiset_path="TODO", # might contain multiple datasets
# dataset_path="TODO", # BIDS dataset in this case
# asset_paths: Optional[list[str]] = None
)
)

for pattern in validation_result["schema_tracking"]:
if pattern["mandatory"]: # TODO: future proof if gets renamed to required
our_validation_result.append(
ValidationResult(
origin=origin,
severity=Severity.ERROR,
id="BIDS.MANDATORY_FILE_MISSING", # we decided to generalize, and not have
scope=Scope.FILE,
message="TODO",
# TODO - discover dandiset or actually BIDS dataset
# might want separate the two
# dandiset_path="TODO", # might contain multiple datasets
# dataset_path="TODO", # BIDS dataset in this case
# asset_paths: Optional[list[str]] = None
)
)
return our_validation_result


def validate(
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ project_urls =
python_requires = >=3.7
install_requires =
appdirs
bidsschematools
bidsschematools ~= 0.4.0
click
click-didyoumean
dandischema ~= 0.7.0
Expand Down

0 comments on commit 72a51fb

Please sign in to comment.