Package omeroweb :: Package webtest :: Module webtest_utils
[hide private]
[frames] | no frames]

Source Code for Module omeroweb.webtest.webtest_utils

  1   
  2  import omero 
  3  import xml.sax 
  4  from omero.gateway import XmlAnnotationWrapper 
  5   
6 -def getSpimData (conn, image):
7 """ 8 Returns a map of SPIM data according to the specification at http://www.ome-xml.org/wiki/SPIM/InitialSupport 9 where extra Objectives are stored in the Instrument, multiple Images are linked to the same 'spim-set' annotation, one 10 Image for each SPIM angle. 11 Extra Objective attributes, SPIM angles and Stage Positions stored in XML annotations with 3 different namespaces. 12 """ 13 14 instrument = image.getInstrument() 15 16 obs = [] 17 for o in instrument.getObjectives(): 18 ob = [] 19 ob.append( ('Model', o.model ) ) 20 ob.append( ('Manufacturer', o.manufacturer ) ) 21 ob.append( ('Serial Number', o.serialNumber ) ) 22 ob.append( ('Nominal Magnification', o.nominalMagnification ) ) 23 obs.append(ob) 24 25 images = [] 26 objExtras = [] 27 spimAngles = [] 28 stagePositions = {} # iid: [] 29 30 def getLinkedImages(annId): 31 query = "select i from Image as i join i.annotationLinks as a_link join a_link.child as a where a.id='%s'" % annId 32 imgs = conn.getQueryService().findAllByQuery(query, None) 33 return [omero.gateway.ImageWrapper(conn, i) for i in imgs]
34 35 # get the Objective attributes and Spim-set annotations (spim-set also linked to other images) 36 for ann in image.listAnnotations(): 37 if isinstance(ann, XmlAnnotationWrapper): 38 xmlText = ann.textValue 39 if ann.ns == "ome-xml.org:additions:post2010-06:objective": 40 elementNames = ['ObjectiveAdditions'] 41 elif ann.ns == "ome-xml.org:additions:post2010-06:spim:set": 42 elementNames = ['SpimImage'] 43 # also get the other images annotated with this 44 images = getLinkedImages(ann.id) 45 else: 46 continue 47 48 handler = AnnXmlHandler(elementNames) 49 xml.sax.parseString(xmlText, handler) 50 51 if ann.ns == "ome-xml.org:additions:post2010-06:objective": 52 objExtras.extend(handler.attributes) 53 elif ann.ns == "ome-xml.org:additions:post2010-06:spim:set": 54 spimAngles.extend(handler.attributes) 55 56 # for All images, get the spim-position data. 57 for i in images: 58 spos = [] 59 for ann in i.listAnnotations(): 60 if isinstance(ann, XmlAnnotationWrapper) and ann.ns == "ome-xml.org:additions:post2010-06:spim:positions": 61 xmlText = ann.textValue 62 handler = AnnXmlHandler(['StagePosition']) 63 xml.sax.parseString(xmlText, handler) 64 spos.extend(handler.attributes) 65 66 stagePositions[i.id] = spos 67 68 #print "Images" 69 #print images 70 #print "Object Extras" 71 #print objExtras 72 #print "Stage Positions" 73 #print stagePositions 74 #print "Spim Angles" 75 #print spimAngles 76 77 if len(objExtras) == 0 and len(stagePositions) == 0 and len(spimAngles) == 0: 78 return None 79 80 return {'images':images, 'obs': obs, 'objExtras':objExtras, 'stagePositions':stagePositions, 'spimAngles': spimAngles} 81 82 83
84 -class AnnXmlHandler(xml.sax.handler.ContentHandler):
85 """ Parse XML to get Objective attributes """
86 - def __init__(self, elementNames):
87 self.inElement = False 88 self.elementNames = elementNames 89 self.attributes = []
90
91 - def startElement(self, name, attributes):
92 if name in self.elementNames: 93 kv = {} 94 for k, v in attributes.items(): 95 kv[str(k)] = str(v) 96 self.attributes.append(kv) 97 self.inElement = True 98 self.buffer = ""
99
100 - def characters(self, data):
101 if self.inElement: 102 self.buffer += data
103 104 # if we're ending an element that we're interested in, save the text in map
105 - def endElement(self, name):
106 self.inElement = False
107