Skip to content

Commit

Permalink
fix: trains 2 attribute parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
tklockau committed Oct 28, 2024
1 parent 8543a08 commit a0402d0
Show file tree
Hide file tree
Showing 6 changed files with 32,516 additions and 84 deletions.
16 changes: 8 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ default_install_hook_types: [commit-msg, pre-commit]
default_stages: [commit, merge-commit]
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.2.0
rev: v5.0.0
hooks:
- id: check-added-large-files
- id: check-ast
Expand All @@ -26,28 +26,28 @@ repos:
- id: fix-byte-order-marker
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 22.3.0
rev: 24.10.0
hooks:
- id: black
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
rev: 5.13.2
hooks:
- id: isort
- repo: https://github.com/PyCQA/docformatter
rev: v1.5.0
rev: v1.7.5
hooks:
- id: docformatter
additional_dependencies:
- docformatter[tomli]
- repo: https://github.com/PyCQA/pydocstyle
rev: 6.1.1
rev: 6.3.0
hooks:
- id: pydocstyle
exclude: '^tests/'
additional_dependencies:
- pydocstyle[toml]
- repo: https://github.com/Lucas-C/pre-commit-hooks
rev: v1.1.13
rev: v1.5.5
hooks:
- id: insert-license
name: Insert license headers (shell-style comments)
Expand Down Expand Up @@ -90,10 +90,10 @@ repos:
- --comment-style
- '..| |'
- repo: https://github.com/fsfe/reuse-tool
rev: v1.0.0
rev: v4.0.3
hooks:
- id: reuse
- repo: https://github.com/qoomon/git-conventional-commits
rev: v2.1.1
rev: v2.6.7
hooks:
- id: conventional-commits
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

0 comments on commit a0402d0

Please sign in to comment.