Skip to content

Commit

Permalink
Merge pull request #15 from DSD-DBS/include_checks_in_validate
Browse files Browse the repository at this point in the history
Include checks in validate
  • Loading branch information
unexcellent authored Dec 9, 2024
2 parents babdb63 + 87492c3 commit 6bc3cc9
Show file tree
Hide file tree
Showing 42 changed files with 398 additions and 544 deletions.
7 changes: 3 additions & 4 deletions docs/source/howtos/1 Validating Scenes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ Usage
from raillabel_providerkit import validate
with Path("path/to/scene.json").open() as scene_file:
scene_dict = json.load(scene_file)
assert validate(scene_dict) == []
scene_path = Path("path/to/scene.json")
issues_in_scene = validate(scene_path)
assert issues_in_scene == []
If this code does not raise any errors, you are good to go. If it does, read the content of the list `validate` returns carefully. It should tell you where the errors are. If you are unsure, contact your project partner or raise an issue on GitHub.
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ classifiers = [
dependencies = [
"jsonschema>=4.4.0",
"fastjsonschema>=2.16.2",
"raillabel==4.0.0",
"raillabel==4.1.0",
"pyyaml>=6.0.0",
"numpy>=1.24.4",
"pydantic<3.0.0",
Expand Down Expand Up @@ -73,6 +73,7 @@ ignore = [
"D107", # __init__ docstrings are not necessary
"D203", # incompatible with D211
"D213", # incompatible with D212
"D413", # not relevant when using google or numpy docstring style

"FBT001", # flags in functions are not bad practice
"FBT002", # flags in functions are not bad practice
Expand Down
12 changes: 11 additions & 1 deletion raillabel_providerkit/validation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@
"""Package for validating raillabel data regarding the format requirements."""

from .issue import Issue, IssueIdentifiers, IssueType
from .validate_empty_frames.validate_empty_frames import validate_empty_frames
from .validate_onthology.validate_onthology import validate_onthology
from .validate_rail_side.validate_rail_side import validate_rail_side
from .validate_schema import validate_schema

__all__ = ["Issue", "IssueIdentifiers", "IssueType", "validate_onthology", "validate_schema"]
__all__ = [
"Issue",
"IssueIdentifiers",
"IssueType",
"validate_empty_frames",
"validate_onthology",
"validate_rail_side",
"validate_schema",
]
48 changes: 35 additions & 13 deletions raillabel_providerkit/validation/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,50 @@

from __future__ import annotations

import json
from pathlib import Path

from raillabel import Scene
from raillabel.json_format import JSONScene

from raillabel_providerkit.validation import Issue

from . import validate_schema
from . import validate_empty_frames, validate_rail_side, validate_schema


def validate(scene_dict: dict) -> list[Issue]:
def validate(
scene_source: dict | Path,
validate_for_empty_frames: bool = True,
validate_for_rail_side_order: bool = True,
) -> list[Issue]:
"""Validate a scene based on the Deutsche Bahn Requirements.
Parameters
----------
scene_dict : dict
The scene as a dictionary directly from `json.load()` in the raillabel format.
Returns
-------
list[Issue]
list of all requirement errors in the scene. If an empty list is returned, then there are
no errors present and the scene is valid.
Args:
scene_source: The scene either as a dictionary or as a Path to the scene source file.
validate_for_empty_frames (optional): If True, issues are returned if the scene contains
frames without annotations. Default is True.
validate_for_rail_side_order: If True, issues are returned if the scene contains track with
a mismatching rail side order. Default is True.
Returns:
List of all requirement errors in the scene. If an empty list is returned, then there are no
errors present and the scene is valid.
"""
if isinstance(scene_source, Path):
with scene_source.open() as scene_file:
scene_source = json.load(scene_file)

schema_errors = validate_schema(scene_source)
if schema_errors != []:
return schema_errors

scene = Scene.from_json(JSONScene(**scene_source))
errors = []

errors.extend(validate_schema(scene_dict))
if validate_for_empty_frames:
errors.extend(validate_empty_frames(scene))

if validate_for_rail_side_order:
errors.extend(validate_rail_side(scene))

return errors
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import raillabel

json_data_directories = [
Path(__file__).parent / "__test_assets__",
Path(__file__).parent / "__assets__",
Path(__file__).parent.parent / "raillabel_providerkit" / "format",
]

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
22 changes: 0 additions & 22 deletions tests/test_raillabel_providerkit/validation/test_validate.py

This file was deleted.

Loading

0 comments on commit 6bc3cc9

Please sign in to comment.