Key Indexed Collection

class supervisely_lib.collection.key_indexed_collection.KeyObject[source]

Bases: object

Base class fo objects that should implement key method. Child classes then can be stored in KeyIndexedCollection.

key()[source]
class supervisely_lib.collection.key_indexed_collection.KeyIndexedCollection(items: Optional[dict] = None)[source]

Bases: object

Base class for ObjClassCollection, TagMetaCollection and TagCollection instances. It is an analogue of python’s standard Dict. It allows to store objects inherited from KeyObject.

Parameters

items (list, optional) – List of ObjClassCollection, TagMetaCollection and TagCollection objects.

Raises

DuplicateKeyError, when trying to add object with already existing key

Usage example
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])
print(collection.to_json())
# Output: [
#     {
#         "name": "cat",
#         "value_type": "none",
#         "color": "#8A0F12",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     },
#     {
#         "name": "turtle",
#         "value_type": "any_string",
#         "color": "#8A860F",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     }
# ]

# Try to add item with a key that already exists in the collection
dublicate_item = sly.ObjClass('cat', sly.Rectangle)
new_collection = collection.add(dublicate_item)
# Output:
# supervisely_lib.collection.key_indexed_collection.DuplicateKeyError: "Key 'cat' already exists"

# Add item with a key that not exist in the collection
item_dog = sly.ObjClass('dog', sly.Rectangle)
new_collection = collection.add(item_dog)
print(new_collection.to_json())
# Output: [
#     {
#         "name": "cat",
#         "value_type": "none",
#         "color": "#668A0F",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     },
#     {
#         "name": "turtle",
#         "value_type": "any_string",
#         "color": "#4D0F8A",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     },
#     {
#         "title": "dog",
#         "shape": "rectangle",
#         "color": "#0F7F8A",
#         "geometry_config": {},
#         "hotkey": ""
#     }
# ]
item_type

Type of items that can be stored in collection. Default value is KeyObject. Field has to be overridden in child class. Before adding object to collection its type is compared with item_type and TypeError exception is raised if it differs. Collection is immutable.

alias of supervisely_lib.collection.key_indexed_collection.KeyObject

add(item: supervisely_lib.collection.key_indexed_collection.KeyObject)supervisely_lib.collection.key_indexed_collection.KeyIndexedCollection[source]

Add given item to collection.

Parameters

item (KeyObject) – ObjClassCollection, TagMetaCollection or TagCollection object.

Returns

New instance of KeyIndexedCollection

Return type

KeyIndexedCollection

Usage Example
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: List[supervisely_lib.collection.key_indexed_collection.KeyObject])supervisely_lib.collection.key_indexed_collection.KeyIndexedCollection[source]

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
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([item_dog, item_mouse])
get(key: str, default=None)supervisely_lib.collection.key_indexed_collection.KeyObject[source]

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
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}
items() → List[supervisely_lib.collection.key_indexed_collection.KeyObject][source]

Get list of all items in collection.

Returns

List of ObjClassCollection, TagMetaCollection or TagCollection objects

Return type

List[KeyObject]

Usage Example
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_lib.annotation.tag_meta.TagMeta object at 0x7fd08eae4340>,
#  <supervisely_lib.annotation.tag_meta.TagMeta object at 0x7fd08eae4370>]
clone(items: Optional[List[supervisely_lib.collection.key_indexed_collection.KeyObject]] = None)supervisely_lib.collection.key_indexed_collection.KeyIndexedCollection[source]

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
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()
keys() → List[str][source]

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

Returns

List of collection keys

Return type

List[str]

Usage Example
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']
has_key(key: str) → bool[source]

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
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: List[supervisely_lib.collection.key_indexed_collection.KeyObject])supervisely_lib.collection.key_indexed_collection.KeyIndexedCollection[source]

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
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": []
#     }
# ]
difference(other: List[supervisely_lib.collection.key_indexed_collection.KeyObject])supervisely_lib.collection.key_indexed_collection.KeyIndexedCollection[source]

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
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": []
#     }
# ]
merge(other: supervisely_lib.collection.key_indexed_collection.KeyIndexedCollection)supervisely_lib.collection.key_indexed_collection.KeyIndexedCollection[source]

Merge collection and other KeyIndexedCollection object.

Parameters

key (KeyIndexedCollection) – KeyIndexedCollection object.

Raises

ValueError if item name from given list is in collection but items in both are different

Returns

KeyIndexedCollection object

Return type

KeyIndexedCollection

Usage Example
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)
other_collection = sly.collection.key_indexed_collection.KeyIndexedCollection([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": []
#     }
# ]
to_json() → List[dict][source]

Convert the KeyIndexedCollection to a json serializable list.

Returns

List of json serializable dicts

Return type

List[dict]

Usage Example
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_json = collection.to_json()
# Output: [
#     {
#         "name": "cat",
#         "value_type": "none",
#         "color": "#8A0F12",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     },
#     {
#         "name": "turtle",
#         "value_type": "any_string",
#         "color": "#8A860F",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     }
# ]
class supervisely_lib.collection.key_indexed_collection.MultiKeyIndexedCollection(items=None)[source]

Bases: supervisely_lib.collection.key_indexed_collection.KeyIndexedCollection

Base class for TagCollection instances. MultiKeyIndexedCollection makes it possible to add an object with an already existing key.

Parameters

items (list, optional) – List of ObjClassCollection, TagMetaCollection and TagCollection objects.

Usage example
item_cat = sly.TagMeta('cat', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
# Create item with same key 'cat'
other_cat = sly.ObjClass('cat', sly.Rectangle)
collection = sly.collection.key_indexed_collection.MultiKeyIndexedCollection([item_cat, item_turtle, other_cat])
print(collection.to_json())
# Output: [
#     {
#         "name": "cat",
#         "value_type": "none",
#         "color": "#0F198A",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     },
#     {
#         "title": "cat",
#         "shape": "rectangle",
#         "color": "#0F8A6B",
#         "geometry_config": {},
#         "hotkey": ""
#     },
#     {
#         "name": "turtle",
#         "value_type": "any_string",
#         "color": "#0F658A",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     }
# ]
get(key: str, default=None)supervisely_lib.collection.key_indexed_collection.KeyObject[source]

Get item from collection with given key(name). If there are many values for the same key, the first value will be returned.

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
item_cat = sly.TagMeta('cat', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
other_cat = sly.ObjClass('cat', sly.Rectangle)
collection = sly.collection.key_indexed_collection.MultiKeyIndexedCollection([item_cat, item_turtle, other_cat])
item = collection.get('cat')
print(item)
# Output:
# Name:  cat                      Value type:none          Possible values:None       Hotkey                  Applicable toall        Applicable classes[]
get_all(key: str, default=[]) → List[supervisely_lib.collection.key_indexed_collection.KeyObject][source]

Get item from collection with given key(name). If there are many values for the same key,all values will be returned by list.

Parameters
  • items (str) – Name of KeyObject in collection.

  • default (optional) – The value that is returned if there is no key in the collection.

Returns

List of ObjClassCollection, TagMetaCollection or TagCollection objects or empty list

Return type

List[KeyObject] or list

Usage Example
item_cat = sly.TagMeta('cat', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
other_cat = sly.ObjClass('cat', sly.Rectangle)
collection = sly.collection.key_indexed_collection.MultiKeyIndexedCollection([item_cat, item_turtle, other_cat])
items = collection.get('cat')
print(items)
# Output:
# [<supervisely_lib.annotation.tag_meta.TagMeta object at 0x7f0278662340>, <supervisely_lib.annotation.obj_class.ObjClass object at 0x7f02786623a0>]
items() → List[supervisely_lib.collection.key_indexed_collection.KeyObject][source]

Get list of all items in collection.

Returns

List of ObjClassCollection, TagMetaCollection or TagCollection objects

Return type

List[KeyObject]

Usage Example
item_cat = sly.TagMeta('cat', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
other_cat = sly.ObjClass('cat', sly.Rectangle)
collection = sly.collection.key_indexed_collection.MultiKeyIndexedCollection([item_cat, item_turtle, other_cat])
print(collection.items())
# Output:
# [<supervisely_lib.annotation.tag_meta.TagMeta object at 0x7fdbd28ce340>,
#  <supervisely_lib.annotation.obj_class.ObjClass object at 0x7fdbd28ce3a0>,
#  <supervisely_lib.annotation.tag_meta.TagMeta object at 0x7fdbd28ce370>]
intersection(other: List[supervisely_lib.collection.key_indexed_collection.KeyObject])supervisely_lib.collection.key_indexed_collection.MultiKeyIndexedCollection[source]

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

MultiKeyIndexedCollection object

Return type

MultiKeyIndexedCollection

Usage Example
item_cat = sly.TagMeta('cat', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
other_cat = sly.ObjClass('cat', sly.Rectangle)
collection = sly.collection.key_indexed_collection.MultiKeyIndexedCollection([item_cat, item_turtle, other_cat])

# Note, item_cat_2 have same key as item_cat, but another value
item_cat_2 = sly.TagMeta('cat', sly.TagValueType.ANY_STRING)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
items = [item_cat_2, item_turtle]

intersect = collection.intersection(items)
print(intersect.to_json())
# Output: [
#     {
#         "name": "turtle",
#         "value_type": "any_string",
#         "color": "#5B8A0F",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     }
# ]
merge(other: List[supervisely_lib.collection.key_indexed_collection.KeyObject])supervisely_lib.collection.key_indexed_collection.MultiKeyIndexedCollection[source]

Merge collection with other MultiKeyIndexedCollection object.

Parameters

key (MultiKeyIndexedCollection) – MultiKeyIndexedCollection object.

Returns

MultiKeyIndexedCollection object

Return type

MultiKeyIndexedCollection

Usage Example
item_cat = sly.TagMeta('cat', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
other_cat = sly.ObjClass('cat', sly.Rectangle)
collection = sly.collection.key_indexed_collection.MultiKeyIndexedCollection([item_cat, item_turtle, other_cat])

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

merge = collection.merge(other_collection)
print(merge.to_json())
# Output: [
#     {
#         "name": "cat",
#         "value_type": "none",
#         "color": "#198A0F",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     },
#     {
#         "title": "cat",
#         "shape": "rectangle",
#         "color": "#898A0F",
#         "geometry_config": {},
#         "hotkey": ""
#     },
#     {
#         "name": "turtle",
#         "value_type": "any_string",
#         "color": "#650F8A",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     },
#     {
#         "name": "turtle",
#         "value_type": "any_string",
#         "color": "#0F8A83",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     },
#     {
#         "name": "dog",
#         "value_type": "none",
#         "color": "#1A8A0F",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     }
# ]
merge_without_duplicates(other: supervisely_lib.collection.key_indexed_collection.MultiKeyIndexedCollection)supervisely_lib.collection.key_indexed_collection.MultiKeyIndexedCollection[source]

Merge collection with other MultiKeyIndexedCollection object. Duplicates will be ignored.

Parameters

key (MultiKeyIndexedCollection) – MultiKeyIndexedCollection object.

Raises

ValueError if item name from given MultiKeyIndexedCollection is in collection but items in both are different

Returns

MultiKeyIndexedCollection object

Return type

MultiKeyIndexedCollection

Usage Example
item_cat = sly.TagMeta('cat', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
other_cat = sly.ObjClass('cat', sly.Rectangle)
collection = sly.collection.key_indexed_collection.MultiKeyIndexedCollection([item_cat, item_turtle, other_cat])

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

merge = collection.merge_without_duplicates(other_collection)
print(merge.to_json())
# Output: [
#     {
#         "name": "dog",
#         "value_type": "none",
#         "color": "#8A0F37",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     },
#     {
#         "name": "cat",
#         "value_type": "none",
#         "color": "#778A0F",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     },
#     {
#         "title": "cat",
#         "shape": "rectangle",
#         "color": "#8A0F76",
#         "geometry_config": {},
#         "hotkey": ""
#     },
#     {
#         "name": "turtle",
#         "value_type": "any_string",
#         "color": "#850F8A",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     }
# ]