From 7fc301b18a5e9df8bf689251e8a3fbfe4608b244 Mon Sep 17 00:00:00 2001 From: William Moore Date: Wed, 27 Nov 2019 13:30:05 +0000 Subject: [PATCH 1/4] Add saeri examples from docs PR https://github.com/ome/ome-documentation/pull/2010 --- examples/Training/python/Read_Data.py | 56 +++++++++++++++++++++++++- examples/Training/python/Write_Data.py | 24 ++++++++++- 2 files changed, 76 insertions(+), 4 deletions(-) diff --git a/examples/Training/python/Read_Data.py b/examples/Training/python/Read_Data.py index c5c30b8bead..dbfc12de3ee 100755 --- a/examples/Training/python/Read_Data.py +++ b/examples/Training/python/Read_Data.py @@ -14,7 +14,7 @@ from __future__ import print_function from builtins import range -from past.utils import old_div +import omero from omero.gateway import BlitzGateway from Parse_OMERO_Properties import USERNAME, PASSWORD, HOST, PORT from Parse_OMERO_Properties import datasetId, imageId, plateId @@ -66,6 +66,32 @@ def print_obj(obj, indent=0): for image in dataset.listChildren(): print_obj(image, 4) +# Find objects by ID. NB: getObjects() returns a generator, not a list +datasets = conn.getObjects("Dataset", [datasetId, datasetId+1]) + +# Get a single object by ID. +img = conn.getObject("Image", imageId) +img_name = img.getName() +# Can use "Annotation" for all types of annotations by ID +annotation = conn.getObject("Annotation", 1) + +# Find an Object by attribute. E.g. 'name' +images = conn.getObjects("Image", attributes={"name": img_name}) + + +# Get different types of Annotations +# =========================== + +# Supported types are: ``tagannotation``, ``longannotation``, +# ``booleanannotation``, ``fileannotation``, ``doubleannotation``, +# ``termannotation``, ``timestampannotation``, ``mapannotation`` + +# List All Tags that you have permission to access +conn.getObjects("TagAnnotation") + +# Find Tags with a known text value +tags = conn.getObjects("TagAnnotation", attributes={"textValue": "OK"}) + # Retrieve 'orphaned' objects # =========================== @@ -108,7 +134,7 @@ def print_obj(obj, indent=0): print('Is reverse intensity?', channel.isReverseIntensity()) # render the first timepoint, mid Z section -z = old_div(image.getSizeZ(), 2) +z = image.getSizeZ()// 2 t = 0 rendered_image = image.renderImage(z, t) # renderedImage.show() # popup (use for debug only) @@ -155,6 +181,32 @@ def print_obj(obj, indent=0): well.getImage(index).getName(), well.getImage(index).getId()) + +# List all annotations on an object. Filter for Tags and get textValue** + +for ann in image.listAnnotations(): + print(ann.getId(), ann.OMERO_TYPE) + print(" added by ", ann.link.getDetails().getOwner().getOmeName()) + if ann.OMERO_TYPE == omero.model.TagAnnotationI: + print("Tag value:", ann.getTextValue()) + +# Get Links between Objects and Annotations +# Find Images linked to Annotation(s), unlink Images from these annotations +# and link them to another Tag Annotation +annotation_ids = [1, 2, 3] +tag_id = 4 +for link in conn.getAnnotationLinks('Image', ann_ids=annotation_ids): + print("Image ID:", link.getParent().id) + print("Annotation ID:", link.getChild().id) + # Update the child of the underlying omero.model.ImageAnnotationLinkI + link._obj.child = omero.model.TagAnnotationI(tag_id, False) + link.save() + +# Find Annotations linked to Object(s), filter by namespace (optional) +for link in conn.getAnnotationLinks('Image', parent_ids=[1,2], ns="test.namespace"): + print("Annotation ID:", link.getChild().id) + + # Close connection # ================ # When you are done, close the session to free up server resources. diff --git a/examples/Training/python/Write_Data.py b/examples/Training/python/Write_Data.py index f556c420844..ab69f0919ea 100755 --- a/examples/Training/python/Write_Data.py +++ b/examples/Training/python/Write_Data.py @@ -47,8 +47,11 @@ sys.stderr.write("Error: Object does not exist.\n") sys.exit(1) link = omero.model.ProjectDatasetLinkI() -link.setParent(omero.model.ProjectI(project.getId(), False)) -link.setChild(dataset_obj) +# We can use a 'loaded' object, but we might get an Exception +# link.setChild(dataset_obj) +# Better to use an 'unloaded' object (loaded = False) +link.setChild(omero.model.DatasetI(dataset_obj.id.val, False)) +link.setParent(omero.model.ProjectI(projectId, False)) conn.getUpdateService().saveObject(link) @@ -56,6 +59,7 @@ # ================================= tag_ann = omero.gateway.TagAnnotationWrapper(conn) tag_ann.setValue("New Tag") +tag_ann.setDescription("Add optional description") tag_ann.save() project = conn.getObject("Project", projectId) project.linkAnnotation(tag_ann) @@ -89,6 +93,22 @@ print("Tag value:", ann.getTextValue()) +# Remove Annotations from an Object (delete link) +project = conn.getObject("Project", projectId) +to_delete = [] +for ann in project.listAnnotations(): + if ann.ns != namespace: + to_delete.append(ann.link.id) +conn.deleteObjects("ProjectAnnotationLink", to_delete, wait=True) + +# Delete Annotations from an Object +to_delete = [] +# Optionally to filter by namespace +for ann in project.listAnnotations(ns=namespace): + to_delete.append(ann.id) +conn.deleteObjects('Annotation', to_delete, wait=True) + + # How to create a file annotation and link to a Dataset # ===================================================== dataset = conn.getObject("Dataset", dataset_id) From 740068b572b8e7d83968053b501b325d5cf92f0a Mon Sep 17 00:00:00 2001 From: William Moore Date: Thu, 28 Nov 2019 23:49:47 +0000 Subject: [PATCH 2/4] ensure datasetId is a number --- examples/Training/python/Read_Data.py | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/Training/python/Read_Data.py b/examples/Training/python/Read_Data.py index dbfc12de3ee..52361bf0b00 100755 --- a/examples/Training/python/Read_Data.py +++ b/examples/Training/python/Read_Data.py @@ -27,6 +27,7 @@ # =================== conn = BlitzGateway(USERNAME, PASSWORD, host=HOST, port=PORT) conn.connect() +datasetId = int(datasetId) def print_obj(obj, indent=0): From d5b47d6721191bbd7a87a5e1b83f2ebbe965cd2a Mon Sep 17 00:00:00 2001 From: William Moore Date: Wed, 4 Dec 2019 14:45:30 +0000 Subject: [PATCH 3/4] Fix Validation Error. Use genuine image/tag IDs --- examples/Training/python/Read_Data.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/examples/Training/python/Read_Data.py b/examples/Training/python/Read_Data.py index 52361bf0b00..e2b1e1f5527 100755 --- a/examples/Training/python/Read_Data.py +++ b/examples/Training/python/Read_Data.py @@ -184,27 +184,30 @@ def print_obj(obj, indent=0): # List all annotations on an object. Filter for Tags and get textValue** - +ann_ids = [] for ann in image.listAnnotations(): print(ann.getId(), ann.OMERO_TYPE) + ann_ids.append(ann.id) print(" added by ", ann.link.getDetails().getOwner().getOmeName()) if ann.OMERO_TYPE == omero.model.TagAnnotationI: print("Tag value:", ann.getTextValue()) # Get Links between Objects and Annotations -# Find Images linked to Annotation(s), unlink Images from these annotations -# and link them to another Tag Annotation -annotation_ids = [1, 2, 3] -tag_id = 4 -for link in conn.getAnnotationLinks('Image', ann_ids=annotation_ids): +# Find links to Images, unlink Images from these annotations +# and link them to a new Tag Annotation +tag_ann = omero.gateway.TagAnnotationWrapper(conn) +tag_ann.setValue("Replacement Tag") +tag_ann.save() +ann_ids = ann_ids[:1] # Just use first annotation +for link in conn.getAnnotationLinks('Image', ann_ids=ann_ids): print("Image ID:", link.getParent().id) print("Annotation ID:", link.getChild().id) # Update the child of the underlying omero.model.ImageAnnotationLinkI - link._obj.child = omero.model.TagAnnotationI(tag_id, False) + link._obj.child = omero.model.TagAnnotationI(tag_ann.id, False) link.save() # Find Annotations linked to Object(s), filter by namespace (optional) -for link in conn.getAnnotationLinks('Image', parent_ids=[1,2], ns="test.namespace"): +for link in conn.getAnnotationLinks('Image', parent_ids=[imageId], ns="test.namespace"): print("Annotation ID:", link.getChild().id) From b70494ba204adfa1005008ebd3c6a28bdaea2f99 Mon Sep 17 00:00:00 2001 From: Will Moore Date: Thu, 5 Dec 2019 09:28:19 +0000 Subject: [PATCH 4/4] Update examples/Training/python/Read_Data.py Co-Authored-By: Mark Carroll --- examples/Training/python/Read_Data.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/Training/python/Read_Data.py b/examples/Training/python/Read_Data.py index e2b1e1f5527..8509ca4ada7 100755 --- a/examples/Training/python/Read_Data.py +++ b/examples/Training/python/Read_Data.py @@ -81,7 +81,7 @@ def print_obj(obj, indent=0): # Get different types of Annotations -# =========================== +# ================================== # Supported types are: ``tagannotation``, ``longannotation``, # ``booleanannotation``, ``fileannotation``, ``doubleannotation``,