KeyIndexedCollection¶
- class KeyIndexedCollection[source]¶
Bases:
object
Base class for
ObjClassCollection
,TagMetaCollection
andTagCollection
instances. It is an analogue of python’s standard Dict. It allows to store objects inherited fromKeyObject
.- Parameters
- items : list, optional
List of
ObjClassCollection
,TagMetaCollection
andTagCollection
objects.
- Raises
DuplicateKeyError
, when trying to add object with already existing key- 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]) 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.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": "" # } # ]
Methods
Add given item to collection.
Add items from given list to collection.
Makes a copy of KeyIndexedCollection with new fields, if fields are given, otherwise it will use fields of the original KeyIndexedCollection.
Find difference between collection and given list of instances.
Get item from collection with given key(name).
Check if given key(item name exist in collection).
Find intersection of given list of instances with collection items.
Get list of all items in collection.
Get list of all keys(item names) in collection.
Merge collection and other KeyIndexedCollection object.
Remove items from collection by given list of keys.
Convert the KeyIndexedCollection to a json serializable list.
- item_type¶
The type of items that can be storred in collection. Defaul value is
KeyObject
. Field has to be overridden in child class. Before adding object to collection its type is compared withitem_type
andTypeError
exception is raised if it differs. Collection is immutable.alias of
supervisely.collection.key_indexed_collection.KeyObject
- add(item)[source]¶
Add given item to collection.
- Parameters
- item : KeyObject
ObjClassCollection
,TagMetaCollection
orTagCollection
object.
- Returns
New instance of KeyIndexedCollection
- Return type
- 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)[source]¶
Add items from given list to collection.
- Parameters
- items : List[KeyObject]
List of
ObjClassCollection
,TagMetaCollection
orTagCollection
objects.
- Returns
New instance of KeyIndexedCollection
- Return type
- 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
)[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
orTagCollection
objects.
- Returns
New instance of KeyIndexedCollection
- Return type
- 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)[source]¶
Find difference between collection and given list of instances.
- Parameters
- key : List[KeyObject]
List of
ObjClassCollection
,TagMetaCollection
orTagCollection
objects.
- Returns
KeyIndexedCollection object
- Return type
- 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": [] # } # ]
-
get(key, default=
None
)[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
orTagCollection
object- Return type
- 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}
- has_key(key)[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
- 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)[source]¶
Find intersection of given list of instances with collection items.
- Parameters
- key : List[KeyObject]
List of
ObjClassCollection
,TagMetaCollection
orTagCollection
objects.
- Raises
ValueError
if find items with same keys(item names)- Returns
KeyIndexedCollection object
- Return type
- 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()[source]¶
Get list of all items in collection.
- Returns
List of
ObjClassCollection
,TagMetaCollection
orTagCollection
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()[source]¶
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 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
- 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) 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": [] # } # ]
- remove_items(keys)[source]¶
Remove items from collection by given list of keys. Creates a new instance of KeyIndexedCollection.
- Parameters
- keys : List[str]
List of keys(item names) in collection.
- Returns
New instance of KeyIndexedCollection
- Return type
- to_json()[source]¶
Convert the KeyIndexedCollection to a json serializable list.
- Returns
List of json serializable dicts
- Return type
List[dict]
- 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_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": [] # } # ]