Open tenzin3 opened 1 month ago
STAM will already do something similar internally, assigning a new random ID for the annotation data if it is new, and reusing the existing one if not, so you can just pass something like:
ann_store.annotate(target=text_selector, data=[
{
"set": ann_dataset.id(), "key": k, "value": v
},
{
"set": ann_dataset.id(), "key": k2, "value": v2
},
], id=get_uuid())
Note that I omitted the AnnotationData ID here, that means an ID will be assigned automatically. STAM assigns a random 21-char nanoid rather than a uuid, as that takes less space, see https://crates.io/crates/nanoid .
If you really do want to assign the annotationdata ID explicitly, then the method you used is okay, but can be improved slightly for performance inside the try block:
prepared_ann_data.append( next(ann_store.data(set=ann_dataset.id(), key=k, value=v, limit=1)) )
In
ann_data
, we have annotation data that we want to associate with an annotation. We aim to avoid creating a new annotation data entry with a new ID if it already exists. If annotation data with the same key and value is already present, we want to link it to the incoming annotation instead of duplicating it. The current code works, but I wanted to know if there's a better solution using the STAM API.Apparently if the key doesnt exists in the annotation data set, it throws an error.