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

Trains3 attributes #4

Merged
merged 12 commits into from
Oct 28, 2024
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,8 @@ repos:
rev: v4.0.3
hooks:
- id: reuse

- repo: https://github.com/qoomon/git-conventional-commits
rev: v2.6.7
hooks:
- id: conventional-commits
13 changes: 1 addition & 12 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,7 @@ no_implicit_optional = true
show_error_codes = true
warn_redundant_casts = true
warn_unreachable = true
python_version = "3.8"

[[tool.mypy.overrides]]
module = ["tests.*"]
allow_incomplete_defs = true
allow_untyped_defs = true

[[tool.mypy.overrides]]
# Untyped third party libraries
module = [
# ...
]
python_version = "3.12"
ignore_missing_imports = true

[tool.pydocstyle]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Copyright DB Netz AG and contributors
# SPDX-License-Identifier: Apache-2.0

import json
import typing as t
from pathlib import Path

import jsonschema
import raillabel

from ..._util._warning import _WarningsLogger
Expand All @@ -26,20 +28,17 @@ class LoaderUnderstandAi(LoaderABC):
warnings: t.List[str]

SCHEMA_PATH: Path = (
Path(__file__).parent.parent.parent
/ "validate"
/ "schemas"
/ "understand_ai_t4_schema.json"
Path(__file__).parent.parent.parent / "format" / "understand_ai_t4_schema.json"
)

def load(self, data: dict, validate: bool = False) -> uai_format.Scene:
def load(self, data: dict, validate_schema: bool = False) -> uai_format.Scene:
"""Load the data into a UAIScene and return it.

Parameters
----------
data: dict
A dictionary loaded from a JSON-file.
validate: bool
validate_schema: bool
If True, the annotation data is validated via the respective schema. This is highly
recommended, as not validating the data may lead to Errors during loading or while
handling the scene. However, validating may increase the loading time. Default is False.
Expand All @@ -50,8 +49,8 @@ def load(self, data: dict, validate: bool = False) -> uai_format.Scene:
The loaded scene with the data.
"""

if validate:
self.validate(data)
if validate_schema:
self.validate_schema(data)

with _WarningsLogger() as logger:
data_converted_to_raillabel = uai_format.Scene.fromdict(data).to_raillabel()
Expand Down Expand Up @@ -83,3 +82,16 @@ def supports(self, data: dict) -> bool:
and "coordinateSystems" in data
and "frames" in data
)

def validate_schema(self, data: dict) -> t.List[str]:
"""Check if the schema is correct."""
with self.SCHEMA_PATH.open() as file:
schema = json.load(file)

validator = jsonschema.Draft7Validator(schema=schema)
schema_errors = []

for error in validator.iter_errors(data):
schema_errors.append("$" + error.json_path[1:] + ": " + str(error.message))

return schema_errors
Loading
Loading