class MaskCategoriesMixin(AttrsMixin): """A mixin class supporting category information of a MaskSubcatalog. Attributes: categories: All the possible categories in the corresponding dataset stored in a :class:`~tensorbay.utility.name.NameList` with the category names as keys and the :class:`~tensorbay.label.supports.MaskCategoryInfo` as values. category_delimiter: The delimiter in category values indicating parent-child relationship. """ category_delimiter: str = attr(is_dynamic=True, key=camel) categories: NameList[MaskCategoryInfo] = attr(is_dynamic=True) def get_category_to_index(self) -> Dict[str, int]: """Return the dict containing the conversion from category name to category id. Returns: A dict containing the conversion from category name to category id. """ if not hasattr(self, "categories"): return {} return {item.name: item.category_id for item in self.categories} def get_index_to_category(self) -> Dict[int, str]: """Return the dict containing the conversion from category id to category name. Returns: A dict containing the conversion from category id to category name. """ if not hasattr(self, "categories"): return {} return {item.category_id: item.name for item in self.categories} def add_category(self, name: str, category_id: int, description: str = "") -> None: """Add a category to the Subcatalog. Arguments: name: The name of the category. category_id: The id of the category. description: The description of the category. """ if not hasattr(self, "categories"): self.categories = NameList() self.categories.append(MaskCategoryInfo(name, category_id, description))
def add_category(self, name: str, description: str = "") -> None: """Add a category to the Subcatalog. Arguments: name: The name of the category. description: The description of the category. """ if not hasattr(self, "categories"): self.categories = NameList() self.categories.append(CategoryInfo(name, description))
def attributes(attributes_catalog_data): """Load AttributeInfo into a NameList. Arguments: attributes_catalog_data: A list containing attributes info. Returns: A NameList containing multiple AttributeInfo. """ attribute_dict = NameList() for attribute in attributes_catalog_data: attribute_dict.append(AttributeInfo.loads(attribute)) return attribute_dict
def categories(categories_catalog_data): """Load CategoryInfo into a NameList. Arguments: categories_catalog_data: A list containing categories info. Returns: A NameList containing multiple CategoryInfo. """ category_dict = NameList() for category in categories_catalog_data: category_dict.append(CategoryInfo.loads(category)) return category_dict
def add_attribute( self, name: str, *, type_: _ArgType = "", enum: Optional[Iterable[_EnumElementType]] = None, minimum: Optional[float] = None, maximum: Optional[float] = None, items: Optional[Items] = None, parent_categories: Union[None, str, Iterable[str]] = None, description: str = "", ) -> None: """Add an attribute to the Subcatalog. Arguments: name: The name of the attribute. type_: The type of the attribute value, could be a single type or multi-types. The type must be within the followings: - array - boolean - integer - number - string - null - instance enum: All the possible values of an enumeration attribute. minimum: The minimum value of number type attribute. maximum: The maximum value of number type attribute. items: The items inside array type attributes. parent_categories: The parent categories of the attribute. description: The description of the attributes. """ attribute_info = AttributeInfo( name, type_=type_, enum=enum, minimum=minimum, maximum=maximum, items=items, parent_categories=parent_categories, description=description, ) if not hasattr(self, "attributes"): self.attributes = NameList() self.attributes.append(attribute_info)
def _get_subcatalog() -> ClassificationSubcatalog: categories: NameList[CategoryInfo] = NameList() for i in range(1, 2001): categories.append(CategoryInfo(str(i).zfill(4))) classification_subcatalog = ClassificationSubcatalog.loads(_ATTRIBUTES) classification_subcatalog.categories = categories return classification_subcatalog
class AttributesMixin(AttrsMixin): """A mixin class supporting attribute information of a subcatalog. Attributes: attributes: All the possible attributes in the corresponding dataset stored in a :class:`~tensorbay.utility.name.NameList` with the attribute names as keys and the :class:`~tensorbay.label.attribute.AttributeInfo` as values. """ attributes: NameList[AttributeInfo] = attr(is_dynamic=True) def add_attribute( self, name: str, *, type_: _ArgType = "", enum: Optional[Iterable[_EnumElementType]] = None, minimum: Optional[float] = None, maximum: Optional[float] = None, items: Optional[Items] = None, parent_categories: Union[None, str, Iterable[str]] = None, description: str = "", ) -> None: """Add an attribute to the Subcatalog. Arguments: name: The name of the attribute. type_: The type of the attribute value, could be a single type or multi-types. The type must be within the followings: - array - boolean - integer - number - string - null - instance enum: All the possible values of an enumeration attribute. minimum: The minimum value of number type attribute. maximum: The maximum value of number type attribute. items: The items inside array type attributes. parent_categories: The parent categories of the attribute. description: The description of the attributes. """ attribute_info = AttributeInfo( name, type_=type_, enum=enum, minimum=minimum, maximum=maximum, items=items, parent_categories=parent_categories, description=description, ) if not hasattr(self, "attributes"): self.attributes = NameList() self.attributes.append(attribute_info)
def mask_categories(mask_categories_catalog_data): category_dict = NameList() for category in mask_categories_catalog_data: category_dict.append(MaskCategoryInfo.loads(category)) return category_dict