Skip to content

Group

oqd_dataschema.group

GroupBase

Bases: BaseModel

Schema representation for a group object within an HDF5 file.

Each grouping of data should be defined as a subclass of GroupBase, and specify the datasets that it will contain. This base object only has attributes, attrs, which are associated to the HDF5 group.

Attributes:

Name Type Description
attrs Attrs

A dictionary of attributes to append to the group.

Source code in oqd-dataschema/src/oqd_dataschema/group.py
class GroupBase(BaseModel, extra="forbid"):
    """
    Schema representation for a group object within an HDF5 file.

    Each grouping of data should be defined as a subclass of `GroupBase`, and specify the datasets that it will contain.
    This base object only has attributes, `attrs`, which are associated to the HDF5 group.

    Attributes:
        attrs: A dictionary of attributes to append to the group.

    """

    attrs: Attrs = Field(default_factory=lambda: {})

    @staticmethod
    def _is_basic_groupfield_type(v):
        return reduce(
            lambda x, y: x or y,
            (gf._is_supported_type(v) for gf in GroupField.__subclasses__()),
        )

    @classmethod
    def _is_groupfield_type(cls, v):
        is_datafield = cls._is_basic_groupfield_type(v)

        is_annotated_datafield = typing.get_origin(
            v
        ) is Annotated and cls._is_basic_groupfield_type(v.__origin__)

        is_optional_datafield = typing.get_origin(v) is Union and (
            (v.__args__[0] == NoneType and cls._is_basic_groupfield_type(v.__args__[1]))
            or (
                v.__args__[1] == NoneType
                and cls._is_basic_groupfield_type(v.__args__[0])
            )
        )

        is_dict_datafield = (
            typing.get_origin(v) is dict
            and v.__args__[0] is str
            and cls._is_basic_groupfield_type(v.__args__[1])
        )

        return (
            is_datafield
            or is_annotated_datafield
            or is_optional_datafield
            or is_dict_datafield
        )

    @classmethod
    def _is_classvar(cls, v):
        return v is ClassVar or typing.get_origin(v) is ClassVar

    def __init_subclass__(cls, **kwargs):
        super().__init_subclass__(**kwargs)

        for k, v in cls.__annotations__.items():
            if k == "class_":
                raise AttributeError("`class_` attribute should not be set manually.")

            if k == "attrs" and v is not Attrs:
                raise AttributeError(
                    "`attrs` attribute must have type annotation of Attrs."
                )

            if k == "attrs" or cls._is_classvar(v):
                continue

            if not cls._is_groupfield_type(v):
                raise TypeError(
                    "All fields of `GroupBase` have to be of type `Dataset`, `Table` or `Folder`."
                )

        cls.__annotations__["class_"] = Literal[cls.__name__]
        setattr(cls, "class_", cls.__name__)

        # Auto-register new group types
        GroupRegistry.register(cls)

GroupRegistry

Represents the GroupRegistry

Source code in oqd-dataschema/src/oqd_dataschema/group.py
class GroupRegistry(metaclass=MetaGroupRegistry):
    """
    Represents the GroupRegistry
    """

    pass