DatasetDict¶
-
class DatasetDict(items=
None)¶ Bases:
KeyIndexedCollectionDatasetcollection ofProject.Base class for
ObjClassCollection,TagMetaCollectionandTagCollectioninstances. It is an analogue of python’s standard Dict. It allows to store objects inherited fromKeyObject.- Parameters:
- items : list, optional¶
List of
ObjClassCollection, TagMetaCollection andTagCollectionobjects.
: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: # 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.
itemsGet 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.
- add(item)¶
Add given item to collection.
- Parameters:
- item¶
ObjClassCollection, TagMetaCollection orTagCollectionobject.
- 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 of
ObjClassCollection, TagMetaCollection orTagCollectionobjects.
- 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=
None¶ List of
ObjClassCollection, TagMetaCollection orTagCollectionobjects.
- items=
- 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:
- other¶
List of items to subtract from the collection.
- Returns:
KeyIndexedCollectionobject- 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)¶ Get item from collection with given key(name).
- Parameters:
- Returns:
ObjClassCollection, TagMetaCollection orTagCollectionobject- 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)¶
Check if given key(item name exist in collection).
- Parameters:
- 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)¶
Find intersection of given list of instances with collection items.
- Parameters:
- other¶
List of items to intersect with the collection.
- 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": [] # } # ]
- 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)¶
Merge collection and other KeyIndexedCollection object.
- Parameters:
- other¶
Other collection to merge with.
- Raises:
ValueError – if item name from given list is in collection but items in both are different
- Returns:
KeyIndexedCollectionobject- 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)¶
Remove items from collection by given list of keys. Creates a new instance of KeyIndexedCollection.
- Parameters:
- 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": [] # } # ]