Skip to content

Commit

Permalink
Merge pull request #6187 from will-moore/saeri_training_examples
Browse files Browse the repository at this point in the history
saeri training examples
  • Loading branch information
joshmoore authored Dec 5, 2019
2 parents 06e3331 + b70494b commit 54082ce
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
60 changes: 58 additions & 2 deletions examples/Training/python/Read_Data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,6 +27,7 @@
# ===================
conn = BlitzGateway(USERNAME, PASSWORD, host=HOST, port=PORT)
conn.connect()
datasetId = int(datasetId)


def print_obj(obj, indent=0):
Expand Down Expand Up @@ -66,6 +67,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
# ===========================
Expand Down Expand Up @@ -108,7 +135,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)
Expand Down Expand Up @@ -155,6 +182,35 @@ 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**
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 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_ann.id, False)
link.save()

# Find Annotations linked to Object(s), filter by namespace (optional)
for link in conn.getAnnotationLinks('Image', parent_ids=[imageId], ns="test.namespace"):
print("Annotation ID:", link.getChild().id)


# Close connection
# ================
# When you are done, close the session to free up server resources.
Expand Down
24 changes: 22 additions & 2 deletions examples/Training/python/Write_Data.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,19 @@
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)


# Annotate Project with a new 'tag'
# =================================
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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 54082ce

Please sign in to comment.