TagMetaCollection

class TagMetaCollection[source]

Bases: supervisely.collection.key_indexed_collection.KeyIndexedCollection, supervisely.io.json.JsonSerializable

Collection with TagMeta instances. TagMetaCollection object is immutable.

Raises

DuplicateKeyError, if instance with given name already exists

Usage example
import supervisely as sly

# Create TagMetas
meta_weather = sly.TagMeta('Weather', sly.TagValueType.ANY_STRING)

season_values = ["Winter", "Spring", "Summer", "Autumn"]
meta_season = sly.TagMeta('Season', sly.TagValueType.ONEOF_STRING, possible_values=season_values)

# Create TagMetaCollection from TagMetas
tag_metas = sly.TagMetaCollection([meta_weather, meta_season])

# Add items to TagMetaCollection
meta_potato = sly.TagMeta('potato', sly.TagValueType.NONE)

# Remember that TagMetaCollection is immutable, and we need to assign new instance of TagMetaCollection to a new variable
tag_metas = tag_metas.add(meta_potato)

# You can also add multiple items to collection
meta_cabbage = sly.TagMeta('cabbage', sly.TagValueType.NONE)
meta_carrot = sly.TagMeta('carrot', sly.TagValueType.NONE)
meta_turnip = sly.TagMeta('turnip', sly.TagValueType.NONE)

tag_metas = tag_metas.add_items([meta_cabbage, meta_carrot, meta_turnip])

# Has key, checks if given key exist in collection
tag_metas.has_key("cabbage")
# Output: True

# Intersection, finds intersection of given list of instances with collection items
meta_dog = sly.TagMeta('dog', sly.TagValueType.NONE)
meta_cat = sly.TagMeta('cat', sly.TagValueType.NONE)
meta_turtle = sly.TagMeta('turtle', sly.TagValueType.NONE)

tag_metas_animals = sly.TagMetaCollection([meta_dog, meta_cat, meta_turtle])

metas_intersections = tag_metas.intersection(tag_metas_animals)
print(metas_intersections.to_json())
# Output: []

# Let's add the potato TagMeta from another collection and compare them again
tag_metas_animals = tag_metas_animals.add(meta_potato)

metas_intersections = tag_metas.intersection(tag_metas_animals)
print(metas_intersections.to_json())
# Output: [
#     {
#         "name":"potato",
#         "value_type":"none",
#         "color":"#8A710F",
#         "hotkey":"",
#         "applicable_type":"all",
#         "classes":[]
#     }
# ]

# Difference, finds difference between collection and given list of TagMetas or TagMetaCollection
meta_car = sly.TagMeta('car', sly.TagValueType.NONE)
meta_bicycle = sly.TagMeta('bicycle', sly.TagValueType.NONE)

tag_metas_vehicles = sly.TagMetaCollection([meta_car, meta_bicycle])

meta_pedestrian = sly.TagMeta('pedestrian', sly.TagValueType.NONE)
meta_road = sly.TagMeta('road', sly.TagValueType.NONE)

difference = tag_metas_vehicles.difference([meta_pedestrian, meta_road])
print(difference.to_json())
# Output: [
#     {
#         "name":"car",
#         "value_type":"none",
#         "color":"#0F138A",
#         "hotkey":"",
#         "applicable_type":"all",
#         "classes":[]
#     },
#     {
#         "name":"bicycle",
#         "value_type":"none",
#         "color":"#0F8A25",
#         "hotkey":"",
#         "applicable_type":"all",
#         "classes":[]
#     }
# ]

# Merge, merges collection and given list of TagMetas
tag_metas_vehicles = sly.TagMetaCollection([meta_car, meta_bicycle])
tag_metas_merge = sly.TagMetaCollection([meta_pedestrian, meta_road])

merged_collections = tag_metas_vehicles.merge(tag_metas_merge)
print(merged_collections.to_json())
# Output: [
#     {
#         "name":"pedestrian",
#         "value_type":"none",
#         "color":"#698A0F",
#         "hotkey":"",
#         "applicable_type":"all",
#         "classes":[]
#     },
#     {
#         "name":"road",
#         "value_type":"none",
#         "color":"#0F8A59",
#         "hotkey":"",
#         "applicable_type":"all",
#         "classes":[]
#     },
#     {
#         "name":"car",
#         "value_type":"none",
#         "color":"#0F138A",
#         "hotkey":"",
#         "applicable_type":"all",
#         "classes":[]
#     },
#     {
#         "name":"bicycle",
#         "value_type":"none",
#         "color":"#0F8A25",
#         "hotkey":"",
#         "applicable_type":"all",
#         "classes":[]
#     }
# ]

# Merge will raise ValueError if item name from given list is in collection but items in both are different
meta_bicycle_1 = sly.TagMeta('bicycle', sly.TagValueType.NONE)
meta_bicycle_2 = sly.TagMeta('bicycle', sly.TagValueType.ANY_STRING)

tag_metas_1 = sly.TagMetaCollection([meta_bicycle_1])
tag_metas_2 = sly.TagMetaCollection([meta_bicycle_2])

test_merge = tag_metas_1.merge(tag_metas_2)
# Output: ValueError: Error during merge for key 'bicycle': values are different

# Let's try to create now a collection where TagMetas have identical names
meta_cow = sly.TagMeta('cow', sly.TagValueType.NONE)
meta_chicken = sly.TagMeta('cow', sly.TagValueType.NONE)

tag_metas = sly.TagMetaCollection([meta_cow, meta_chicken])
# Output: DuplicateKeyError: "Key 'cow' already exists"

Methods

add

Add given item to collection.

add_items

Add items from given list to collection.

clone

Makes a copy of KeyIndexedCollection with new fields, if fields are given, otherwise it will use fields of the original KeyIndexedCollection.

difference

Find difference between collection and given list of instances.

from_json

Convert a list with dicts in json format to TagMetaCollection.

get

Get item from collection with given key(name).

get_by_id

Return TagMeta with given id.

get_id_mapping

Create dict matching TagMetas id to TagMeta.

get_tag_name_by_id

rtype

Optional[str]

has_key

Check if given key(item name exist in collection).

intersection

Find intersection of given list of instances with collection items.

items

Get list of all items in collection.

keys

Get list of all keys(item names) in collection.

merge

Merge two TagMetaCollection objects.

refresh_ids_from

Update ids of TagMetas in collection from given collection.

to_json

Convert the TagMetaCollection to a list of json dicts.

item_type

alias of supervisely.annotation.tag_meta.TagMeta

add(item)

Add given item to collection.

Parameters
item : KeyObject

ObjClassCollection, TagMetaCollection or TagCollection object.

Returns

New instance of KeyIndexedCollection

Return type

KeyIndexedCollection

Usage Example
import supervisely as sly

item_cat = sly.TagMeta('cat', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
collection = sly.collection.key_indexed_collection.KeyIndexedCollection([item_cat, item_turtle])
# Remember that KeyIndexedCollection object is immutable, and we need to assign new instance of KeyIndexedCollection to a new variable
item_dog = sly.ObjClass('dog', sly.Rectangle)
new_collection = collection.add(item_dog)
add_items(items)

Add items from given list to collection.

Parameters
items : List[KeyObject]

List of ObjClassCollection, TagMetaCollection or TagCollection objects.

Returns

New instance of KeyIndexedCollection

Return type

KeyIndexedCollection

Usage Example
import supervisely as sly

item_cat = sly.TagMeta('cat', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
collection = sly.collection.key_indexed_collection.KeyIndexedCollection([item_cat, item_turtle])
# Remember that KeyIndexedCollection object is immutable, and we need to assign new instance of KeyIndexedCollection to a new variable
item_dog = sly.ObjClass('dog', sly.Rectangle)
item_mouse = sly.ObjClass('mouse', sly.Bitmap)
new_collection = collection.add_items([item_dog, item_mouse])
clone(items=None)

Makes a copy of KeyIndexedCollection with new fields, if fields are given, otherwise it will use fields of the original KeyIndexedCollection.

Parameters
items : List[KeyObject], optional

List of ObjClassCollection, TagMetaCollection or TagCollection objects.

Returns

New instance of KeyIndexedCollection

Return type

KeyIndexedCollection

Usage Example
import supervisely as sly

item_cat = sly.TagMeta('cat', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
collection = sly.collection.key_indexed_collection.KeyIndexedCollection([item_cat, item_turtle])
# Remember that KeyIndexedCollection object is immutable, and we need to assign new instance of KeyIndexedCollection to a new variable
new_collection = collection.clone()
difference(other)

Find difference between collection and given list of instances.

Parameters
key : List[KeyObject]

List of ObjClassCollection, TagMetaCollection or TagCollection objects.

Returns

KeyIndexedCollection object

Return type

KeyIndexedCollection

Usage Example
import supervisely as sly

item_cat = sly.TagMeta('cat', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
collection = sly.collection.key_indexed_collection.KeyIndexedCollection([item_cat, item_turtle])

item_dog = sly.TagMeta('dog', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
items = [item_dog, item_turtle]

diff = collection.difference(items)
print(diff.to_json())
# Output: [
#     {
#         "name": "cat",
#         "value_type": "none",
#         "color": "#8A150F",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     }
# ]
classmethod from_json(data)[source]

Convert a list with dicts in json format to TagMetaCollection. Read more about Supervisely format.

Parameters
data : List[dict]

List with dicts in json format.

Returns

TagMetaCollection object

Return type

TagMetaCollection

Usage example
import supervisely as sly

data = [
    {
        "name":"Weather",
        "value_type":"any_string",
        "color":"#8A620F",
        "hotkey":"",
        "applicable_type":"all",
        "classes":[]
    },
    {
        "name":"Season",
        "value_type": "oneof_string",
                      "color": "#700F8A",
                      "values": ["Winter", "Spring", "Summer", "Autumn"],
        "hotkey":"",
        "applicable_type":"all",
        "classes":[]
}
]

tag_metas = sly.TagMetaCollection.from_json(data)
get(key, default=None)

Get item from collection with given key(name).

Parameters
items : str

Name of KeyObject in collection.

default : optional

The value that is returned if there is no key in the collection.

Returns

ObjClassCollection, TagMetaCollection or TagCollection object

Return type

KeyObject

Usage Example
import supervisely as sly

item_cat = sly.TagMeta('cat', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
collection = sly.collection.key_indexed_collection.KeyIndexedCollection([item_cat, item_turtle])

item_cat = collection.get('cat')
print(item_cat)
# Output:
# Name:  cat                      Value type:none          Possible values:None       Hotkey                  Applicable toall        Applicable classes[]

item_not_exist = collection.get('no_item', {1: 2})
print(item_not_exist)
# Output:
# {1: 2}
get_by_id(tag_meta_id)[source]

Return TagMeta with given id.

Parameters
tag_meta_id : int

TagMeta id to search for.

Returns

TagMeta with given id.

Return type

TagMeta

get_id_mapping(raise_if_no_id=False)[source]

Create dict matching TagMetas id to TagMeta.

Parameters
raise_if_no_id : bool, optional

Raise ValueError if where is TagMetas without id.

Returns

Json format as a dict

Return type

dict

Raises

KeyError, if where is duplication of TagMetas id

has_key(key)

Check if given key(item name exist in collection).

Parameters
key : str

The key to look for in the collection.

Returns

Is the key in the collection or not

Return type

bool

Usage Example
import supervisely as sly

item_cat = sly.TagMeta('cat', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
collection = sly.collection.key_indexed_collection.KeyIndexedCollection([item_cat, item_turtle])

collection.has_key('cat') # True
collection.has_key('hamster') # False
intersection(other)

Find intersection of given list of instances with collection items.

Parameters
key : List[KeyObject]

List of ObjClassCollection, TagMetaCollection or TagCollection objects.

Raises

ValueError if find items with same keys(item names)

Returns

KeyIndexedCollection object

Return type

KeyIndexedCollection

Usage Example
import supervisely as sly

item_cat = sly.TagMeta('cat', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
collection = sly.collection.key_indexed_collection.KeyIndexedCollection([item_cat, item_turtle])

item_dog = sly.TagMeta('dog', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
items = [item_dog, item_turtle]

intersection = collection.intersection(items)
print(intersection.to_json())
# Output: [
#     {
#         "name": "turtle",
#         "value_type": "any_string",
#         "color": "#760F8A",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     }
# ]
items()

Get list of all items in collection.

Returns

List of ObjClassCollection, TagMetaCollection or TagCollection objects

Return type

List[KeyObject]

Usage Example
import supervisely as sly

item_cat = sly.TagMeta('cat', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
collection = sly.collection.key_indexed_collection.KeyIndexedCollection([item_cat, item_turtle])
items = collection.items()
print(items)
# Output:
# [<supervisely.annotation.tag_meta.TagMeta object at 0x7fd08eae4340>,
#  <supervisely.annotation.tag_meta.TagMeta object at 0x7fd08eae4370>]
keys()

Get list of all keys(item names) in collection.

Returns

List of collection keys

Return type

List[str]

Usage Example
import supervisely as sly

item_cat = sly.TagMeta('cat', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
collection = sly.collection.key_indexed_collection.KeyIndexedCollection([item_cat, item_turtle])
keys = collection.keys() # ['cat', 'turtle']
merge(other)[source]

Merge two TagMetaCollection objects.

Parameters
other : TagMetaCollection

TagMetaCollection object.

Raises

ValueError if item name from given collection is in TagMetaCollection but TagMetas in both are different

Returns

TagMetaCollection object

Return type

TagMetaCollection

Usage Example
import supervisely as sly

item_cat = sly.TagMeta('cat', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
collection = sly.TagMetaCollection([item_cat, item_turtle])

item_dog = sly.TagMeta('dog', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
other_collection = sly.TagMetaCollection([item_dog, item_turtle])

merge = collection.merge(other_collection)
print(merge.to_json())
# Output: [
#     {
#         "name": "dog",
#         "value_type": "none",
#         "color": "#8A6C0F",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     },
#     {
#         "name": "cat",
#         "value_type": "none",
#         "color": "#0F4A8A",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     },
#     {
#         "name": "turtle",
#         "value_type": "any_string",
#         "color": "#4F0F8A",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     }
# ]
refresh_ids_from(tags)[source]

Update ids of TagMetas in collection from given collection.

Parameters
tags : TagMetaCollection

TagMetaCollection to update ids from.

Return type

None

to_json()[source]

Convert the TagMetaCollection to a list of json dicts. Read more about Supervisely format.

Returns

List of dicts in json format

Return type

List[dict]

Usage example
import supervisely as sly

tag_metas = tag_metas.to_json()
# Output:
# [
#   {
#    "name": "Weather",
#    "value_type": "any_string",
#    "color": "#8A620F",
#    "hotkey": "",
#    "applicable_type": "all",
#    "classes": []
#   },
#   {
#    "name": "Season",
#    "value_type": "oneof_string",
#    "color": "#700F8A",
#    "values": ["Winter", 'Spring", "Summer", "Autumn"],
#    "hotkey": "",
#    "applicable_type": "all",
#    "classes": []
#   }
# ]