Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions t4_devkit/schema/tables/registry.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import inspect
from enum import Enum
from typing import TYPE_CHECKING, Callable, KeysView

from ..name import SchemaName
Expand All @@ -14,9 +15,14 @@


class SchemaRegistry:
"""A manager class to register schema tables."""
"""A manager class to register schema tables.

def __init__(self) -> None:
Args:
name_type (type[Enum], optional): Enum of the table names this registry accepts.
"""

def __init__(self, name_type: type[Enum] = SchemaName) -> None:
self.__name_type = name_type
self.__schemas: dict[str, SchemaTable] = {}

@property
Expand All @@ -27,13 +33,13 @@ def __len__(self) -> int:
return len(self.__schemas)

def __contains__(self, key: str | SchemaName) -> bool:
if isinstance(key, SchemaName):
if isinstance(key, self.__name_type):
key = key.value

return key in self.keys()

def get(self, key: str | SchemaName) -> SchemaTable:
if isinstance(key, SchemaName):
if isinstance(key, self.__name_type):
key = key.value

if key not in self.__schemas:
Expand All @@ -44,8 +50,8 @@ def keys(self) -> KeysView[str]:
return self.__schemas.keys()

def register(self, name: SchemaName, *, force: bool = False) -> Callable:
if not isinstance(name, SchemaName):
raise TypeError(f"name must be SchemaName, but got {type(name)}.")
if not isinstance(name, self.__name_type):
raise TypeError(f"name must be {self.__name_type.__name__}, but got {type(name)}.")

def _register_decorator(obj: object) -> object:
self._add_module(obj, name=name, force=force)
Expand All @@ -72,7 +78,7 @@ def build_from_json(self, key: str | SchemaName, filepath: PathLike) -> list[Sch
Returns:
Instantiated dataclass.
"""
if isinstance(key, SchemaName):
if isinstance(key, self.__name_type):
key = key.value

schema: SchemaTable = self.get(key)
Expand Down
2 changes: 2 additions & 0 deletions t4_devkit/schema/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
from .log import * # noqa
from .manifest import * # noqa
from .map import * # noqa
from .name import * # noqa
from .object_ann import * # noqa
from .registry import * # noqa
from .route import * # noqa
from .sample import * # noqa
from .sample_annotation import * # noqa
Expand Down
4 changes: 4 additions & 0 deletions t4_devkit/schema/v1/attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

from t4_devkit.schema.tables.base import SchemaBase

from .name import SchemaName
from .registry import SCHEMAS

__all__ = ["Attribute"]


@define(slots=False)
@SCHEMAS.register(SchemaName.ATTRIBUTE)
class Attribute(SchemaBase):
"""`attribute.json` dataclass.

Expand Down
3 changes: 3 additions & 0 deletions t4_devkit/schema/v1/caption.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from t4_devkit.schema.tables.base import SchemaBase, impossible_empty

from .annotation_type import AnnotationType
from .name import SchemaName
from .registry import SCHEMAS

__all__ = ["CaptionType", "Caption"]

Expand All @@ -29,6 +31,7 @@ class CaptionType(str, Enum):


@define(slots=False)
@SCHEMAS.register(SchemaName.CAPTION)
class Caption(SchemaBase):
"""`caption.json` dataclass.

Expand Down
4 changes: 4 additions & 0 deletions t4_devkit/schema/v1/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

from t4_devkit.schema.tables.base import SchemaBase, impossible_empty

from .name import SchemaName
from .registry import SCHEMAS

__all__ = ["Category"]


@define(slots=False)
@SCHEMAS.register(SchemaName.CATEGORY)
class Category(SchemaBase):
"""`category.json` dataclass.

Expand Down
4 changes: 4 additions & 0 deletions t4_devkit/schema/v1/ego_pose.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
from t4_devkit.schema.tables.base import SchemaBase, impossible_empty
from t4_devkit.typing import Quaternion, Vector3, Vector6

from .name import SchemaName
from .registry import SCHEMAS

__all__ = ["EgoPoseSource", "EgoPose"]


Expand All @@ -21,6 +24,7 @@ class EgoPoseSource(str, Enum):


@define(slots=False)
@SCHEMAS.register(SchemaName.EGO_POSE)
class EgoPose(SchemaBase):
"""`ego_pose.json` dataclass.

Expand Down
3 changes: 3 additions & 0 deletions t4_devkit/schema/v1/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
from t4_devkit.schema.tables.base import SchemaBase, impossible_empty

from .map import MapElement
from .name import SchemaName
from .registry import SCHEMAS

__all__ = ["Instance"]


@define(slots=False)
@SCHEMAS.register(SchemaName.INSTANCE)
class Instance(SchemaBase):
"""`instance.json` dataclass.

Expand Down
3 changes: 3 additions & 0 deletions t4_devkit/schema/v1/keypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
from t4_devkit.schema.tables.base import SchemaBase, impossible_empty

from .annotation_type import AnnotationType
from .name import SchemaName
from .registry import SCHEMAS

__all__ = ["Keypoint"]


@define(slots=False)
@SCHEMAS.register(SchemaName.KEYPOINT)
class Keypoint(SchemaBase):
"""`keypoint.json` dataclass.

Expand Down
4 changes: 4 additions & 0 deletions t4_devkit/schema/v1/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

from t4_devkit.schema.tables.base import SchemaBase, impossible_empty

from .name import SchemaName
from .registry import SCHEMAS

__all__ = ["Log"]


@define(slots=False)
@SCHEMAS.register(SchemaName.LOG)
class Log(SchemaBase):
"""`log.json` dataclass.

Expand Down
4 changes: 4 additions & 0 deletions t4_devkit/schema/v1/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

from t4_devkit.schema.tables.base import SchemaBase, impossible_empty

from .name import SchemaName
from .registry import SCHEMAS

__all__ = [
"MapFormat",
"ProjectorType",
Expand Down Expand Up @@ -98,6 +101,7 @@ class MapElement:


@define(slots=False)
@SCHEMAS.register(SchemaName.MAP)
class Map(SchemaBase):
"""`map.json` dataclass.

Expand Down
60 changes: 60 additions & 0 deletions t4_devkit/schema/v1/name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from __future__ import annotations

from enum import Enum, unique

__all__ = ["SchemaName"]


@unique
class SchemaName(str, Enum):
"""Names of the T4Dataset 1.0 tables, one JSON file each in `annotation/`.

Required tables: sample, sample_data, ego_pose, sensor, scene, log, vehicle.
All other tables are optional; `manifest.json` declares which ones a dataset contains.
"""

SAMPLE = "sample"
SAMPLE_DATA = "sample_data"
EGO_POSE = "ego_pose"
SENSOR = "sensor"
SCENE = "scene"
LOG = "log"
VEHICLE = "vehicle"
MAP = "map" # optional
VEHICLE_STATE = "vehicle_state" # optional
CATEGORY = "category" # optional
ATTRIBUTE = "attribute" # optional
VISIBILITY = "visibility" # optional
INSTANCE = "instance" # optional
SAMPLE_ANNOTATION = "sample_annotation" # optional
OBJECT_ANN = "object_ann" # optional
KEYPOINT = "keypoint" # optional
TRAFFIC_LIGHT = "traffic_light" # optional
ROUTE = "route" # optional
SCENARIO = "scenario" # optional
CAPTION = "caption" # optional

@property
def filename(self) -> str:
"""Return the annotation json filename.

Returns:
Annotation json filename.
"""
return f"{self.value}.json"

def is_optional(self) -> bool:
"""Indicates if this table is optional.

Returns:
Return True if this table is optional.
"""
return self not in (
SchemaName.SAMPLE,
SchemaName.SAMPLE_DATA,
SchemaName.EGO_POSE,
SchemaName.SENSOR,
SchemaName.SCENE,
SchemaName.LOG,
SchemaName.VEHICLE,
)
3 changes: 3 additions & 0 deletions t4_devkit/schema/v1/object_ann.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
from t4_devkit.schema.tables.object_ann import RLEMask

from .annotation_type import AnnotationType
from .name import SchemaName
from .registry import SCHEMAS

__all__ = ["ObjectAnn"]


@define(slots=False)
@SCHEMAS.register(SchemaName.OBJECT_ANN)
class ObjectAnn(SchemaBase):
"""`object_ann.json` dataclass.

Expand Down
9 changes: 9 additions & 0 deletions t4_devkit/schema/v1/registry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from __future__ import annotations

from t4_devkit.schema.tables.registry import SchemaRegistry

from .name import SchemaName

__all__ = ["SCHEMAS"]

SCHEMAS = SchemaRegistry(SchemaName)
4 changes: 4 additions & 0 deletions t4_devkit/schema/v1/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
from t4_devkit.schema.tables.base import SchemaBase, impossible_empty
from t4_devkit.typing import Quaternion, Vector3

from .name import SchemaName
from .registry import SCHEMAS

__all__ = ["RouteSource", "RoutePose", "RouteSegment", "Route"]


Expand Down Expand Up @@ -51,6 +54,7 @@ class RouteSegment:


@define(slots=False)
@SCHEMAS.register(SchemaName.ROUTE)
class Route(SchemaBase):
"""`route.json` dataclass.

Expand Down
4 changes: 4 additions & 0 deletions t4_devkit/schema/v1/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

from t4_devkit.schema.tables.base import SchemaBase, impossible_empty

from .name import SchemaName
from .registry import SCHEMAS

__all__ = ["Sample"]


@define(slots=False)
@SCHEMAS.register(SchemaName.SAMPLE)
class Sample(SchemaBase):
"""`sample.json` dataclass.

Expand Down
3 changes: 3 additions & 0 deletions t4_devkit/schema/v1/sample_annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
from t4_devkit.typing import Quaternion, Vector3

from .annotation_type import AnnotationType
from .name import SchemaName
from .registry import SCHEMAS

__all__ = ["SampleAnnotation"]


@define(slots=False)
@SCHEMAS.register(SchemaName.SAMPLE_ANNOTATION)
class SampleAnnotation(SchemaBase):
"""`sample_annotation.json` dataclass.

Expand Down
4 changes: 4 additions & 0 deletions t4_devkit/schema/v1/sample_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

from t4_devkit.schema.tables.base import SchemaBase, impossible_empty

from .name import SchemaName
from .registry import SCHEMAS

__all__ = ["FileFormat", "SampleData"]


Expand Down Expand Up @@ -33,6 +36,7 @@ class FileFormat(str, Enum):


@define(slots=False)
@SCHEMAS.register(SchemaName.SAMPLE_DATA)
class SampleData(SchemaBase):
"""`sample_data.json` dataclass.

Expand Down
3 changes: 3 additions & 0 deletions t4_devkit/schema/v1/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from t4_devkit.schema.tables.base import SchemaBase, impossible_empty

from .annotation_type import AnnotationType
from .name import SchemaName
from .registry import SCHEMAS

__all__ = ["ScenarioStatus", "Taxonomy", "Scenario"]

Expand All @@ -34,6 +36,7 @@ class Taxonomy:


@define(slots=False)
@SCHEMAS.register(SchemaName.SCENARIO)
class Scenario(SchemaBase):
"""`scenario.json` dataclass.

Expand Down
4 changes: 4 additions & 0 deletions t4_devkit/schema/v1/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

from t4_devkit.schema.tables.base import SchemaBase, impossible_empty

from .name import SchemaName
from .registry import SCHEMAS

__all__ = ["Scene"]


@define(slots=False)
@SCHEMAS.register(SchemaName.SCENE)
class Scene(SchemaBase):
"""`scene.json` dataclass.

Expand Down
Loading
Loading