MultiKeyIndexedCollection¶
- class MultiKeyIndexedCollection[source]¶
Bases:
supervisely.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
andTagCollection
objects.
- Usage example
import supervisely as sly 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": [] # } # ]
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).
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 with other MultiKeyIndexedCollection object.
Merge collection with other MultiKeyIndexedCollection object.
Remove items from collection by given list of keys.
Convert the KeyIndexedCollection to a json serializable list.
- item_type¶
alias of
supervisely.collection.key_indexed_collection.KeyObject
- add(item)¶
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)¶
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
)¶ 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)¶
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). 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
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) 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, default=
[]
)[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 : List, optional
The value that is returned if there is no key in the collection.
- Returns
List of
ObjClassCollection
,TagMetaCollection
orTagCollection
objects or empty list- Return type
List[KeyObject]
orlist
- Usage Example
import supervisely as sly 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.annotation.tag_meta.TagMeta object at 0x7f0278662340>, <supervisely.annotation.obj_class.ObjClass object at 0x7f02786623a0>]
- 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
- 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
MultiKeyIndexedCollection 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) 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": [] # } # ]
- 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) 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.annotation.tag_meta.TagMeta object at 0x7fdbd28ce340>, # <supervisely.annotation.obj_class.ObjClass object at 0x7fdbd28ce3a0>, # <supervisely.annotation.tag_meta.TagMeta object at 0x7fdbd28ce370>]
- 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 collection with other MultiKeyIndexedCollection object.
- Parameters
- key : MultiKeyIndexedCollection
MultiKeyIndexedCollection object.
- Returns
MultiKeyIndexedCollection 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) 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)[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
- Usage Example
import supervisely as sly 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": [] # } # ]
- remove_items(keys)¶
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()¶
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": [] # } # ]