def post_map_annotation(conn, object_type, object_ids, kv_dict, ns): """Create new MapAnnotation and link to images. Parameters ---------- conn : ``omero.gateway.BlitzGateway`` object OMERO connection. object_type : str OMERO object type, passed to ``BlitzGateway.getObjects`` object_ids : int or list of ints IDs of objects to which the new MapAnnotation will be linked. kv_dict : dict key-value pairs that will be included in the MapAnnotation ns : str Namespace for the MapAnnotation Returns ------- map_ann_id : int IDs of newly created MapAnnotation Examples -------- >>> ns = 'jax.org/jax/example/namespace' >>> d = {'species': 'human', 'occupation': 'time traveler' 'first name': 'Kyle', 'surname': 'Reese'} >>> post_map_annotation(conn, "Image", [23,56,78], d, ns) 234 """ if type(object_ids) not in [list, int]: raise TypeError('object_ids must be list or integer') if type(object_ids) is not list: object_ids = [object_ids] if len(object_ids) == 0: raise ValueError('object_ids must contain one or more items') if type(kv_dict) is not dict: raise TypeError('kv_dict must be of type `dict`') kv_pairs = [] for k, v in kv_dict.items(): k = str(k) v = str(v) kv_pairs.append([k, v]) map_ann = MapAnnotationWrapper(conn) map_ann.setNs(str(ns)) map_ann.setValue(kv_pairs) map_ann.save() for o in conn.getObjects(object_type, object_ids): o.linkAnnotation(map_ann) return map_ann.getId()
def check_annotation(self, obj, value, namespace, update=False): from omero.gateway import MapAnnotationWrapper from omero.rtypes import rstring status = True anns = list(obj.listAnnotations(ns=namespace)) if len(anns) > 1: self.log.error("Found multiple annotations with the %s namespace" % STUDY_NS) status = False elif len(anns) == 0: if update: self.log.info("Creating map annotation") m = MapAnnotationWrapper(conn=obj._conn) m.setNs(rstring(namespace)) m.setValue(value) m.save() obj.linkAnnotation(m) else: self.log.error("No map annotation found") elif anns[0].getValue() != value: status = False if update: self.log.info("Updating map annotation") self.log.debug("previous:%s" % anns[0].getValue()) self.log.debug("new:%s" % value) anns[0].setValue(value) anns[0].save() else: self.log.error("Mismatching annotation") self.log.debug("current:%s" % anns[0].getValue()) self.log.debug("expected:%s" % value) return status
def save_results(conn, image, values): # Add values as a Map Annotation on the image namespace = "demo.simple_frap_data" delete_old_annotations(conn, image, namespace) key_value_data = [[str(t), str(value)] for t, value in enumerate(values)] map_ann = MapAnnotationWrapper(conn) map_ann.setNs(namespace) map_ann.setValue(key_value_data) map_ann.save() image.linkAnnotation(map_ann)
def save_map_annotations(conn, images, image_data, script_params): """Summarise ROIs as Key-Value pairs for each Image.""" for i, image in enumerate(images): key_value_data = [] for col_name in SUMMARY_COL_NAMES: col_data = image_data.get(col_name) key_value_data.append([col_name, str(col_data[i])]) map_ann = MapAnnotationWrapper(conn) # Use custom namespace to allow finding/deleting map_anns we create map_ann.setNs(BATCH_ROI_EXPORT_NS) map_ann.setValue(key_value_data) map_ann.save() image.linkAnnotation(map_ann)
def add_map_annotation(conn, image, params): """Add key-value pairs from params onto image.""" window_size = params.get("Kernel_Window_Size") sigma = params.get("Sigma") key_value_data = [["Kernel Window Size", str(window_size)], ["Sigma", str(sigma)]] print("Adding MAP", key_value_data, image.getId()) map_ann = MapAnnotationWrapper(conn) # Use 'client' namespace to allow editing in Insight & web map_ann.setNs(omero.constants.metadata.NSCLIENTMAPANNOTATION) map_ann.setValue(key_value_data) map_ann.save() # NB: only link a client map annotation to a single object image.linkAnnotation(map_ann)
def save_map_annotations(conn, images, export_data, script_params): """Summarise ROIs as Key-Value pairs for each Image.""" for image in images: data = get_summary_data_for_image(conn, image, export_data, script_params) if data is None: continue key_value_data = [[k, str(data[k])] for k in SUMMARY_COL_NAMES] map_ann = MapAnnotationWrapper(conn) # Use custom namespace to allow finding/deleting map_anns we create map_ann.setNs(BATCH_ROI_EXPORT_NS) map_ann.setValue(key_value_data) map_ann.save() image.linkAnnotation(map_ann)
def save_values(conn, image, values): """ Attach the values as map annotation to the image :param conn: The BlitzGateway :param image: The image :param values: The values :return: Nothing """ namespace = "demo.simple_frap_data" key_value_data = [[str(t), str(value)] for t, value in enumerate(values)] map_ann = MapAnnotationWrapper(conn) map_ann.setNs(namespace) map_ann.setValue(key_value_data) map_ann.save() image.linkAnnotation(map_ann)
def update_mapannotation(conn, annotated, kv_pairs): """Search for MapAnnotations associated to the annotated object and updates **the first map it finds** (in the dictionnary update sense) or creates a new one if no map annotation was present. """ log.info("Map annotations: ") log.info("\n".join([f"{k}: {v}" for k, v in kv_pairs.items()])) for map_ann in annotated.listAnnotations(): if isinstance(map_ann, MapAnnotationWrapper): vals = dict(map_ann.getValue()) vals.update(kv_pairs) map_ann.setValue(list(vals.items())) break else: map_ann = MapAnnotationWrapper(conn) namespace = omero.constants.metadata.NSCLIENTMAPANNOTATION map_ann.setNs(namespace) map_ann.setValue(list(kv_pairs.items())) map_ann.save() annotated.linkAnnotation(map_ann)
def create_map_ann(conn, obj, key_value_data): map_ann = MapAnnotationWrapper(conn) map_ann.setValue(key_value_data) map_ann.setNs('from.channels.keyvaluepair') map_ann.save() obj.linkAnnotation(map_ann)