def test_new_table(self, parent): tcols, meta, ftnames = TableStoreHelper.get_columns([2], "multi", False) if parent: iid = unwrap(TableStoreHelper.create_image(self.sess).getId()) store = OmeroTablesFeatureStore.new_table( self.sess, self.name, self.ft_space, self.ann_space, meta, ftnames, "Image:%d" % iid ) else: store = OmeroTablesFeatureStore.new_table( self.sess, self.name, self.ft_space, self.ann_space, meta, ftnames ) assert store.table TableStoreHelper.assert_coltypes_equal(store.cols, tcols) if parent: tid = unwrap(store.get_table().getOriginalFile().getId()) q = "SELECT link.child FROM ImageAnnotationLink link " "WHERE link.parent.id=:id" p = omero.sys.ParametersI() p.addId(iid) r = self.sess.getQueryService().findAllByQuery(q, p) assert len(r) == 1 assert isinstance(r[0], omero.model.FileAnnotation) assert unwrap(r[0].getFile().getId()) == tid store.close()
def rgba_to_int(cls, red, green, blue, alpha): """ Encodes the color as an Integer in RGBA encoding Returns None if any of red, green or blue are None. If alpha is None we use 255 by default. :return: Integer :rtype: int """ red = unwrap(red) green = unwrap(green) blue = unwrap(blue) alpha = unwrap(alpha) if red is None or green is None or blue is None: return None if alpha is None: alpha = 255 r = red << 24 g = green << 16 b = blue << 8 a = alpha << 0 rgba_int = r+g+b+a if (rgba_int > (2**31-1)): # convert to signed 32-bit int rgba_int = rgba_int - 2**32 return rgba_int
def removeTagAnnotations(conn, obj): """ Unlink tag annotations, but do not delete the tags """ linkType = obj.OMERO_CLASS + 'AnnotationLink' q = 'select oal from %s as oal join ' \ 'fetch oal.child as ann where oal.parent.id = :parentid and ' \ 'oal.child.ns like :ns' % linkType params = omero.sys.Parameters() params.map = { 'parentid': wrap(obj.getId()), 'ns': wrap(WndcharmStorage.CLASSIFIER_WNDCHARM_NAMESPACE + '/%') } anns = conn.getQueryService().findAllByQuery(q, params) rmIds = [] rmTags = [] for ann in anns: if isinstance(ann.child, omero.model.TagAnnotation): rmIds.append(unwrap(ann.getId())) rmTags.append(unwrap(ann.child.getTextValue())) deleteObjects(conn, linkType, rmIds) message = 'Removed tags: %s from %s id:%d\n' % ( rmTags, obj.OMERO_CLASS, obj.getId()) return message
def delete(self): """ Delete the entire featureset including annotations """ # There's a bug (?) which means multiple FileAnnotations with the same # OriginalFile child can't be deleted using the graph spec methods. # For now just delete everything individually qs = self.session.getQueryService() tof = self.table.getOriginalFile() fid = unwrap(tof.getId()) params = omero.sys.ParametersI() params.addId(fid) ds = [] linktypes = self._get_annotation_link_types() for link in linktypes: r = qs.findAllByQuery( 'SELECT al FROM %s al WHERE al.child.file.id=:id' % link, params) ds.extend(r) r = qs.findAllByQuery( 'SELECT ann FROM FileAnnotation ann WHERE ann.file.id=:id', params) ds.extend(r) ds.append(tof) log.info('Deleting: %s', [(d.__class__.__name__, unwrap(d.getId())) for d in ds]) us = self.session.getUpdateService() self.close() for d in ds: us.deleteObject(d)
def testGroupConfigA(self, data): save_value, expect_value = data LOAD = ("select g from ExperimenterGroup g " "left outer join fetch g.config where g.id = :id") queryService = self.root.sf.getQueryService() updateService = self.root.sf.getUpdateService() params = omero.sys.ParametersI() def load_group(id): params.addId(id) return queryService.findByQuery(LOAD, params) group = self.new_group() gid = group.id.val group.config = save_value updateService.saveObject(group) group = load_group(gid) config = unwrap(group.config) self.assertNVs(expect_value, config) name, value = unwrap(queryService.projection( """ select m.name, m.value from ExperimenterGroup g left outer join g.config m where m.name = 'a' and g.id = :id """, omero.sys.ParametersI().addId(gid))[0]) self.assertNV(expect_value[0], name, value)
def test_create_file_annotation(self): tid, tcols, ftnames = TableStoreHelper.create_table( self.sess, self.ft_space, self.name, 1) imageid = unwrap(TableStoreHelper.create_image(self.sess).getId()) ofile = self.sess.getQueryService().get( 'omero.model.OriginalFile', tid) store = FeatureTableProxy( self.sess, self.name, self.ft_space, self.ann_space) assert store._file_annotation_exists( 'Image', imageid, self.ann_space, tid) == [] link = store.create_file_annotation( 'Image', imageid, self.ann_space, ofile) p = link.getParent() c = link.getChild() assert isinstance(p, omero.model.Image) assert isinstance(c, omero.model.FileAnnotation) assert unwrap(p.getId()) == imageid assert unwrap(c.getFile().getId()) == tid links = store._file_annotation_exists( 'Image', imageid, self.ann_space, tid) assert len(links) == 1 assert links[0].__class__ == link.__class__ and links[0].id == link.id store.close()
def test_store1(self): tid, tcols = TableStoreHelper.create_table( self.sess, self.nsc, self.fsmeta) store = OmeroTablesFeatureStore.FeatureSetTableStore( self.sess, self.nsc, self.nsr, self.fsmeta) TableStoreHelper.assert_coltypes_equal(store.cols, tcols) assert store.table.getNumberOfRows() == 0 rowmeta = {'objectid': 4} values = [[1, 2], [-1.0]] store.store1(rowmeta, values) assert store.table.getNumberOfRows() == 1 data = store.table.readCoordinates([0]) assert data.columns[0].values == [values[0]] assert data.columns[1].values == [values[1]] params = omero.sys.ParametersI() params.addString('ns', self.nsr) ann = self.sess.getQueryService().findAllByQuery( 'from MapAnnotation ann join fetch ann.mapValue where ann.ns=:ns', params) assert len(ann) == 1 ann = ann[0] assert unwrap(ann.getNs()) == self.nsr m = unwrap(ann.getMapValue()) assert len(m) == 3 assert m['objectid'] == '4' assert m['_tableid'] == str(tid) assert m['_offset'] == '0' store.close()
def addFileAnnotationTo(tc, obj): """ Attach a table as an annotation to an object (dataset/project) if not already attached """ # The OriginalFile may be out-of-date, which can lead to data loss, so # reload it #tfile = tc.table.getOriginalFile() tfile = tc.conn.getObject( 'OriginalFile', unwrap(tc.table.getOriginalFile().getId())) oclass = obj.OMERO_CLASS obj = tc.conn.getObject(oclass, obj.getId()) for a in obj.listAnnotations(WNDCHARM_NAMESPACE): if isinstance(a, omero.gateway.FileAnnotationWrapper): if tfile.getId() == unwrap(a._obj.getFile().getId()): return 'Already attached' fa = omero.model.FileAnnotationI() fa.setFile(tfile._obj) fa.setNs(wrap(WNDCHARM_NAMESPACE)) fa.setDescription(wrap(WNDCHARM_NAMESPACE + ':' + tfile.getName())) objClass = getattr(omero.model, oclass + 'I') linkClass = getattr(omero.model, oclass + 'AnnotationLinkI') annLink = linkClass() annLink.link(objClass(obj.getId(), False), fa) annLink = tc.conn.getUpdateService().saveAndReturnObject(annLink) return 'Attached file id:%d to %s id:%d\n' % \ (tfile.getId(), oclass, obj.getId())
def registerNamespace(iQuery, iUpdate, namespace, keywords): """ Register a workflow with the server, if the workflow does not exist create it and returns it, otherwise it returns the already created workflow. @param iQuery The query service. @param iUpdate The update service. @param namespace The namespace of the workflow. @param keywords The keywords associated with the workflow. @return see above. """ from omero.util.OmeroPopo import WorkflowData as WorkflowData # Support rstring and str namespaces namespace = unwrap(namespace) keywords = unwrap(keywords) workflow = iQuery.findByQuery("from Namespace as n where n.name = '" + namespace+"'", None); workflowData = WorkflowData(); if(workflow!=None): workflowData = WorkflowData(workflow); else: workflowData.setNamespace(namespace); splitKeywords = keywords.split(','); SU_LOG.debug(workflowData.asIObject()) for keyword in splitKeywords: workflowData.addKeyword(keyword); SU_LOG.debug(workflowData.asIObject()) workflow = iUpdate.saveAndReturnObject(workflowData.asIObject()); return WorkflowData(workflow);
def testProjectionPermissions(self, fixture): writer = self.writer(fixture) reader = self.reader(fixture) project = self.make_project(name="testProjectPermissions", client=writer) update = writer.sf.getUpdateService() project = update.saveAndReturnObject(project) try: perms1 = unwrap(reader.projection(( "select new ome.util.PermDetails(p) " "from Project p where p.id = :id"), ParametersI().addId(project.id.val)))[0][0] perms2 = unwrap(reader.projection(( "select new map(p.id as id, p as p_details_permissions) " "from Project p where p.id = :id"), ParametersI().addId(project.id.val)))[0][0] perms2 = perms2["p_details_permissions"] assert perms1 == perms2 assert fixture.canRead except IndexError: # No permissions were returned. assert not fixture.canRead else: self.assertPerms(perms1, fixture) self.assertPerms(perms2, fixture)
def test_unlinkAnnotations(self): us = self.conn.getUpdateService() f = omero.model.OriginalFileI() f.setName(wrap(str(uuid.uuid1()))) f.setPath(wrap(str(uuid.uuid1()))) f = self.sess.getUpdateService().saveAndReturnObject(f) tag = omero.model.TagAnnotationI() tag.setTextValue(wrap(str(uuid.uuid1()))); tag = us.saveAndReturnObject(tag); link = omero.model.OriginalFileAnnotationLinkI() link.setChild(tag) link.setParent(f) link = us.saveAndReturnObject(link) self.assertIsNotNone(self.getObject('TagAnnotation', tag.id)) self.assertIsNotNone(self.getObject('OriginalFile', f.id)) self.assertIsNotNone(self.getObject( 'OriginalFileAnnotationLink', link.id)) file = self.conn.getObject('OriginalFile', unwrap(f.id)) WndcharmStorage.unlinkAnnotations(self.conn, file) self.assertIsNotNone(self.getObject('TagAnnotation', tag.id)) self.assertIsNotNone(self.getObject('OriginalFile', file.id)) self.assertIsNone(self.getObject('OriginalFileAnnotationLink', link.id)) self.delete('/OriginalFile', unwrap(f.id)) self.delete('/Annotation', unwrap(tag.id))
def test_store_pending_flush(self): width = 2 tid, tcols, meta, ftnames = TableStoreHelper.create_table( self.sess, self.ft_space, self.name, [width], "multi", False ) imageid = unwrap(TableStoreHelper.create_image(self.sess).getId()) roiid = unwrap(TableStoreHelper.create_roi(self.sess).getId()) store = FeatureTable(self.sess, self.name, self.ft_space, self.ann_space) store.open_table(tid) store.store_pending([imageid, -1, "aa"], [10, 20]) assert store.table.getNumberOfRows() == 0 store.store_pending([-1, roiid, "bb"], [90, 80]) assert store.table.getNumberOfRows() == 0 store.store_flush() assert store.table.getNumberOfRows() == 2 d = store.table.readCoordinates(range(0, 2)).columns assert len(d) == 4 assert d[0].values == [imageid, -1] assert d[1].values == [-1, roiid] assert d[2].values == ["aa", "bb"] assert d[3].values == [[10, 20], [90, 80]] store.store_flush() assert store.table.getNumberOfRows() == 2 store.close()
def testUpdateSessions(self, who): who, idlediff, livediff = who if who.startswith("root"): client = self.root else: client = self.client uuid = client.getSessionId() service = client.sf.getSessionService() obj_before = service.getSession(uuid) live = unwrap(obj_before.timeToLive) idle = unwrap(obj_before.timeToIdle) req = UpdateSessionTimeoutRequest() req.session = uuid if livediff is not None: req.timeToLive = rlong(live+livediff) if idlediff is not None: req.timeToIdle = rlong(idle+idlediff) try: cb = client.submit(req) cb.getResponse() cb.close(True) assert not who.startswith("bad") # must throw except omero.CmdError, ce: if who.startswith("bad"): assert ce.err.name == "non-admin-increase" return else: print ce.err.parameters.get("stacktrace") raise Exception(ce.err.category, ce.err.name)
def query_by_map_ann(self, kvs, projection=None): params = omero.sys.ParametersI() qs = self.session.getQueryService() conditions = [] if self.namespace is not None: conditions.append('ann.ns = :ns') params.addString('ns', self.namespace) for k, v in kvs.iteritems(): paramk = 'k%d' % len(conditions) paramv = 'v%d' % len(conditions) params.addString(paramk, k) if isinstance(v, list): conditions.append( 'ann.mapValue[:%s] in (:%s)' % (paramk, paramv)) else: conditions.append( 'ann.mapValue[:%s] = :%s' % (paramk, paramv)) params.add(paramv, wrap(v)) # join fetch mapValue is only needed if we need to return the map, the # query should run without it if projection: q = ('select ann.id, index(map), map from MapAnnotation ann ' 'join ann.mapValue map') try: iter(projection) conditions.append('index(map) in (:fields)') params.add('fields', wrap(projection)) except TypeError: pass else: q = 'from MapAnnotation ann join fetch ann.mapValue map' if conditions: q += ' where ' + ' and '.join(conditions) if projection: # Each [id, key, value] is returned separately, use order by to # ensure all keys/values for an annotation are consecutive q += ' order by ann.id' anns = self.paged_query(qs.projection, q, params) # iikvs: A map of ids:((id, key, value), ...) results = dict( (unwrap(iikvs[0]), dict((unwrap(ikv[1]), unwrap(ikv[2])) for ikv in iikvs[1])) for iikvs in itertools.groupby(anns, lambda x: x[0])) else: results = self.paged_query(qs.findAllByQuery, q, params) return results
def test_updateNameTag(self): tag = omero.model.TagAnnotationI() tag.setNs(wrap('foo')) tag.setTextValue(wrap('bar')) tag = self.conn.getUpdateService().saveAndReturnObject(tag) pysliddb.updateNameTag(self.conn, tag, 'baz') tag2 = self.conn.getObject('TagAnnotation', unwrap(tag.getId()))._obj self.assertEqual(tag2.getNs(), tag.getNs()) self.assertEqual(unwrap(tag2.getTextValue()), 'baz')
def getRectangle(roiService, imageId, roiLabel): """ Returns (x, y, width, height, zMin, zMax, tMin, tMax) of the first rectange in the image that has @roiLabel as text """ result = roiService.findByImage(imageId, None) roiText = roiLabel.lower() roiCount = 0 rectCount = 0 foundLabelledRoi = False for roi in result.rois: roiCount += 1 # go through all the shapes of the ROI for shape in roi.copyShapes(): if type(shape) == omero.model.RectangleI: t = unwrap(shape.getTheT()) z = unwrap(shape.getTheZ()) x = shape.getX().getValue() y = shape.getY().getValue() tv = shape.getTextValue() if tv is not None: text = tv.getValue() else: text = "" # get ranges for whole ROI if rectCount == 0: zMin = z zMax = zMin tMin = t tMax = tMin width = shape.getWidth().getValue() height = shape.getHeight().getValue() else: zMin = min(zMin, z) zMax = max(zMax, z) tMin = min(tMin, t) tMax = max(tMax, t) rectCount += 1 if text is not None and text.lower() == roiText: foundLabelledRoi = True if foundLabelledRoi: return (int(x), int(y), int(width), int(height), int(zMin), int(zMax), int(tMin), int(tMax)) else: rectCount = 0 # try another ROI # if we got here without finding an ROI that matched, simply return any # ROI we have (last one) if roiCount > 0: return (int(x), int(y), int(width), int(height), int(zMin), int(zMax), int(tMin), int(tMax))
def test_get_parents(self, parents): if parents: dataset1, dataset2, project1 = self.create_hierarchy(self.image) expected = set((unwrap(dataset1.getName()), unwrap(dataset2.getName()))) assert self.md.get_parent().getName() in expected assert set(p.getName() for p in self.md.get_parents()) == expected else: assert self.md.get_parent() is None assert self.md.get_parents() == []
def assertTb(self, buf, x=64, y=64): try: from PIL import Image, ImageDraw # see ticket:2597 except ImportError: try: import Image, ImageDraw # see ticket:2597 except ImportError: print "PIL not installed" thumb = self.open_jpeg_buffer(buf) assert unwrap(x) == thumb.size[0] assert unwrap(y) == thumb.size[1]
def assign(f, method, *args): name = "test%s" % method[0].upper() name += method[1:] for i in args: try: for i2 in i: name += "x%s" % unwrap(i2) except: name += "x%s" % unwrap(i) f.func_name = name setattr(TestThumbs, name, f)
def from_omero(self, tcode, v): if isinstance(tcode, type): o = self.proxy.factory.wrap(v) if not isinstance(o, tcode): raise ValueError("inconsistent type result type(%s) for %s" % (type(o), tcode)) return o elif tcode is TIMESTAMP: return vluo.rtime2time(ort.unwrap(v)) elif tcode in WRAPPING: return ort.unwrap(v) else: raise ValueError("illegal tcode value: %s" % tcode)
def filterImageUserChannels(conn, iids, uids=None, chnames=None): """ Queries the database to see which images fit the requested criteria TODO: Check whether this query is correct or not... it might not be """ if not iids: return {} qs = conn.getQueryService() query = ('select p from Pixels p join ' 'fetch p.channels as c join ' 'fetch c.logicalChannel as lc join ' 'fetch p.image as im ' 'where im.id in (:iids)') params = omero.sys.ParametersI() params.add('iids', wrap([long(u) for u in iids])) logger.debug('iids:%s uids:%s chnames:%s', iids, uids, chnames) if uids: query += 'and p.details.owner.id in (:uids) ' params.add('uids', wrap([long(u) for u in uids])) if chnames: if UNNAMED_CHANNEL in chnames: query += 'and (lc.name in (:chns) or lc.name is NULL) ' else: query += 'and lc.name in (:chns) ' params.add('chns', wrap([str(chn) for chn in chnames])) ps = qs.findAllByQuery(query, params, conn.SERVICE_OPTS) def getChName(pixels, c): try: ch = p.getChannel(c) except IndexError: return None if not ch: return None name = ch.getLogicalChannel(c).name if name is None: return UNNAMED_CHANNEL return unwrap(name) imChMap = {} for p in ps: iid = unwrap(p.image.id) # The HQL query restricted the channel search, so some channels won't # be loaded. Unloaded trailing channels won't be created either. cs = [getChName(p, c) for c in xrange(unwrap(p.getSizeC()))] imChMap[iid] = cs return imChMap
def test_delete(self, owned): if owned: tablesess = self.sess else: user2 = self.create_user_same_group() tablesess = self.create_client_session(user2) iid1 = unwrap(TableStoreHelper.create_image(self.sess).getId()) iid2 = unwrap(TableStoreHelper.create_image(self.sess).getId()) store = FeatureTable(tablesess, self.name, self.ft_space, self.ann_space) store.new_table([("Long", "id")], ["x"]) ofile = store.get_table().getOriginalFile() link1 = store.create_file_annotation("Image", iid1, self.ann_space, ofile) link2 = store.create_file_annotation("Image", iid2, self.ann_space, ofile) if not owned: store.close() # Reopen the store with a different session store = FeatureTable(self.sess, self.name, self.ft_space, self.ann_space) store.open_table(unwrap(ofile.getId())) def get(obj): # Fetch the latest copy of an object return self.sess.getQueryService().find("omero.model.%s" % obj.__class__.__name__, unwrap(obj.getId())) assert get(link1) is not None assert get(link1.getParent()) is not None assert get(link1.getChild()) assert get(link1.getChild().getFile()) assert get(link2) assert get(link2.getParent()) assert get(link2.getChild()) assert get(link2.getChild().getFile()) if owned: store.delete() assert get(link1) is None assert get(link1.getParent()) assert get(link1.getChild()) is None assert get(link1.getChild().getFile()) is None assert get(link2) is None assert get(link2.getParent()) assert get(link2.getChild()) is None assert get(link2.getChild().getFile()) is None else: with pytest.raises(OmeroTablesFeatureStore.FeaturePermissionException): store.delete() store.close()
def test_get_objects(self): ims = [ TableStoreHelper.create_image(self.sess, name="image-test"), TableStoreHelper.create_image(self.sess, name="other-test"), TableStoreHelper.create_image(self.sess, name="image-test"), ] store = FeatureTable(self.sess, self.name, self.ft_space, self.ann_space) rs = store.get_objects("Image", {"name": "image-test"}) assert sorted(unwrap(r.getId()) for r in rs) == unwrap([ims[0].getId(), ims[2].getId()]) store.close()
def script_form(request, scriptId): """ Generates an html form for the parameters of a defined script. """ conn = getConnection(request) scriptService = conn.getScriptService() params = scriptService.getParams(long(scriptId)) if params == None: return HttpResponse() paramData = {} paramData["id"] = long(scriptId) paramData["name"] = params.name.replace("_", " ") paramData["description"] = params.description paramData["authors"] = ", ".join([a for a in params.authors]) paramData["contact"] = params.contact paramData["version"] = params.version paramData["institutions"] = ", ".join([i for i in params.institutions]) inputs = [] # use a list so we can sort by 'grouping' for key, param in params.inputs.items(): i = {} i["name"] = key.replace("_", " ") i["key"] = key if not param.optional: i["required"] = True i["description"] = param.description if param.min: i["min"] = param.min.getValue() if param.max: i["max"] = param.max.getValue() if param.values: i["options"] = [v.getValue() for v in param.values.getValue()] pt = unwrap(param.prototype) #print key, pt.__class__ if pt.__class__ == type(True): i["boolean"] = True elif pt.__class__ == type(0) or pt.__class__ == type(long(0)): print "Number!" i["number"] = "number" # will stop the user entering anything other than numbers. elif pt.__class__ == type(float(0.0)): #print "Float!" i["number"] = "float" i["prototype"] = unwrap(param.prototype) # E.g "" (string) or [0] (int list) or 0.0 (float) i["grouping"] = param.grouping inputs.append(i) inputs.sort(key=lambda i: i["grouping"]) paramData["inputs"] = inputs return render_to_response('webemdb/scripts/script_form.html', {'paramData': paramData})
def test_addTagTo(self): pid = self.create_project('addTagTo') txt = 'This is a tag' tid = self.create_tag(txt) tag = omero.model.TagAnnotationI(tid, False) WndcharmStorage.addTagTo(self.conn, tag, 'Project', pid) proj = self.conn.getObject('Project', pid) a = proj.getAnnotation() self.assertIsInstance(a._obj, omero.model.TagAnnotation) self.assertEqual(unwrap(a.getTextValue()), txt) self.assertEqual(unwrap(a.getId()), tid) self.delete('/Project', pid)
def test_get_objects(self): ims = [ TableStoreHelper.create_image(self.sess, name='image-test'), TableStoreHelper.create_image(self.sess, name='other-test'), TableStoreHelper.create_image(self.sess, name='image-test') ] store = FeatureTableProxy( self.sess, self.name, self.ft_space, self.ann_space) rs = store.get_objects('Image', {'name': 'image-test'}) assert sorted(unwrap(r.getId()) for r in rs) == unwrap( [ims[0].getId(), ims[2].getId()]) store.close()
def test_create_map_ann(self): ns = UserAccount.uuid() ma = OmeroMetadata.MapAnnotations(self.sess, namespace=ns) d = {'a': '1', 'bb': 'cc'} aid = ma.create_map_ann({'a': '1', 'bb': 'cc'}) params = omero.sys.ParametersI() params.addLong('id', aid) mr = self.sess.getQueryService().findByQuery( 'from MapAnnotation ann join fetch ann.mapValue where id=:id', params) assert unwrap(mr.getNs()) == ns assert unwrap(mr.getMapValue()) == d
def test_addFileAnnotationTo(self): pid = self.create_project('addFileAnnotationTo') p = self.conn.getObject('Project', pid) tc = self.Tc(self.conn) fid = unwrap(tc.table.getOriginalFile().getId()) WndcharmStorage.addFileAnnotationTo(tc, p) proj = self.conn.getObject('Project', pid) a = proj.getAnnotation() self.assertIsInstance(a._obj, omero.model.FileAnnotation) self.assertEqual(unwrap(a.getFile().getId()), fid) tc.close() self.delete('/Project', pid)
def close(self, current=None): if self._closed: self.logger.warn( "File object %d already closed", unwrap(self.file_obj.id) if self.file_obj else None) return modified = self.storage.modified() try: size = self.cleanup() self.logger.info("Closed %s", self) except: self.logger.warn("Closed %s with errors", self) self._closed = True fid = unwrap(self.file_obj.id) if self.file_obj is not None and self.can_write and modified: gid = unwrap(self.file_obj.details.group.id) client_uuid = self.factory.ice_getIdentity().category[8:] ctx = { "omero.group": str(gid), omero.constants.CLIENTUUID: client_uuid} try: # Size to reset the server object to (must be checked after # the underlying HDF file has been closed) rfs = self.factory.createRawFileStore(ctx) try: rfs.setFileId(fid, ctx) if size: rfs.truncate(size, ctx) # May do nothing rfs.write([], size, 0, ctx) # Force an update else: rfs.write([], 0, 0, ctx) # No-op file_obj = rfs.save(ctx) finally: rfs.close(ctx) self.logger.info( "Updated file object %s to hash=%s (%s bytes)", fid, unwrap(file_obj.hash), unwrap(file_obj.size)) except: self.logger.warn("Failed to update file object %s", fid, exc_info=1) else: self.logger.info("File object %s not updated", fid)
def new_client( self, group=None, user=None, perms=None, owner=False, system=False, session=None, password=None, email=None ): """ Like new_user() but returns an active client. Passing user= or session= will prevent self.new_user from being called, and instead the given user (by name or ExperimenterI) or session will be logged in. """ props = self.root.getPropertyMap() if session is not None: if user is not None: self.log.warning("user= argument will be ignored: %s", user) session = unwrap(session) props["omero.user"] = session props["omero.pass"] = session else: if user is not None: user, name = self.user_and_name(user) else: user = self.new_user(group, perms, owner=owner, system=system, email=email) props["omero.user"] = user.omeName.val if password is not None: props["omero.pass"] = password else: props["omero.pass"] = user.omeName.val client = omero.client(props) self.__clients.add(client) client.setAgent("OMERO.py.new_client_test") client.createSession() return client
def plane_shape_counts(request, image_id, conn=None, **kwargs): """ Get the number of shapes that will be visible on each plane. Shapes that have theZ or theT unset will show up on all planes. """ image = conn.getObject("Image", image_id) if image is None: return JsonResponse({"error": "Image not found"}, status=404) query_service = conn.getQueryService() params = omero.sys.ParametersI() params.addId(image_id) counts = [] # Attempt to load counts for each Z/T index. # Unfortunately this is too slow. Use alternative approach (see below) # query = """ # select count(distinct shape) from Shape shape # join shape.roi as roi # where (shape.theZ = %s or shape.theZ is null) # and (shape.theT = %s or shape.theT is null) # and roi.image.id = :id # """ # for z in range(image.getSizeZ()): # t_counts = [] # for t in range(image.getSizeT()): # result = query_service.projection(query % (z, t), params, # conn.SERVICE_OPTS) # t_counts.append(result[0][0].val) # counts.append(t_counts) size_t = image.getSizeT() size_z = image.getSizeZ() counts = [[0] * size_t for z in range(size_z)] query = """ select shape.theZ, shape.theT from Shape shape join shape.roi as roi where roi.image.id = :id """ # key is 'z:t' for unattached shapes, e.g. {'none:5':3} unattached_counts = defaultdict(int) result = query_service.projection(query, params, conn.SERVICE_OPTS) for shape in result: z = unwrap(shape[0]) t = unwrap(shape[1]) if z is not None and t is not None: counts[z][t] += 1 else: key = "%s:%s" % (z, t) unattached_counts[key] += 1 for key, count in unattached_counts.items(): z = key.split(':')[0] t = key.split(':')[1] z_range = range(size_z) if z == 'None' else [int(z)] t_range = range(size_t) if t == 'None' else [int(t)] for the_z in z_range: for the_t in t_range: counts[the_z][the_t] += count return JsonResponse({'data': counts})
def test2855MetadataMethods(self): """ Tests the various metadata methods for a table """ grid = self.client.sf.sharedResources() table = grid.newTable(1, "/test") assert table def clean(m): """ Unwraps the RTypes for easier processing and gets rid of auto-generated values for easier testing. """ m = unwrap(m) assert "__initialized" in m assert "__version" in m del m["__initialized"] del m["__version"] return m try: print table.getOriginalFile().id.val lc = columns.LongColumnI('lc', 'desc', [1]) table.initialize([lc]) assert len(clean(table.getAllMetadata())) == 0 # Set a string table.setMetadata("s", rstring("b")) assert "b" == unwrap(table.getMetadata("s")) assert {"s": "b"} == clean(table.getAllMetadata()) # Set an int table.setMetadata("i", rint(1)) assert 1 == unwrap(table.getMetadata("i")) assert {"s": "b", "i": 1} == clean(table.getAllMetadata()) # Set a float table.setMetadata("f", rfloat(1)) assert 1 == unwrap(table.getMetadata("f")) assert {"s": "b", "i": 1, "f": 1} == clean(table.getAllMetadata()) # Replace all user-metadata table.setAllMetadata({"s2": rstring("b2"), "l2": rlong(3)}) assert {"s2": "b2", "l2": 3} == clean(table.getAllMetadata()) assert table.getMetadata("s") is None table.setAllMetadata({}) assert {} == clean(table.getAllMetadata()) table.setMetadata("z", rint(1)) with pytest.raises(omero.ApiUsageException): table.setMetadata("__z", rint(2)) assert {"z": 1} == clean(table.getAllMetadata()) with pytest.raises(omero.ValidationException): table.setMetadata("z", rint(None)) finally: table.delete() table.close()
def testUnwrap(self): # NUMS plain assert 0 == unwrap(0) assert 1 == unwrap(1) assert 0.0 == unwrap(0.0) assert 1.0 == unwrap(1.0) # NUMS rtyped assert 0 == unwrap(rint(0)) assert 0 == unwrap(rlong(0)) assert 1 == unwrap(rint(1)) assert 1 == unwrap(rlong(1)) assert 0.0 == unwrap(rfloat(0.0)) assert 0.0 == unwrap(rdouble(0.0)) assert 1.0 == unwrap(rfloat(1.0)) assert 1.0 == unwrap(rdouble(1.0)) # STRINGS assert "" == unwrap("") assert "str" == unwrap("str") # BOOL assert True == unwrap(True) assert False == unwrap(False) assert True == unwrap(rbool(True)) assert False == unwrap(rbool(False)) # TIME # Bit odd, do we want the long for time, or transformed? assert 0 == unwrap(rtime(0)) assert 1 == unwrap(rtime(1)) # CLASS # same for class, should we map it? assert "k" == unwrap(rclass("k")) # INTERNAL color = omero.Color() assert color == unwrap(rinternal(color)) # OBJECT image = omero.model.ImageI() assert image == unwrap(robject(image)) # COLLECTIONS # empty assert [] == unwrap([]) assert {} == unwrap({}) assert set() == unwrap(set()) # plain in collection assert [1] == unwrap([1]) # rtype in collection assert [1] == unwrap([rint(1)]) assert {"a": 1} == unwrap({"a": 1}) # plain in rcollection ILLEGAL # assert [1] == unwrap(rlist([1])) # assert {"a":1} == unwrap(rmap({"a":1})) # rtype in rcollection assert [1] == unwrap(rlist([rint(1)])) assert {"a": 1} == unwrap(rmap({"a": rint(1)})) # rtype keys ILLEGAL # assert {"a":1} == unwrap(rmap({rstring("a"):rint(1)})) # recursion, ticket:1977 m1 = rmap({"a": rint(1)}) m1.val["m1"] = m1 m2 = {"a": 1} m2["m1"] = m2 unwrap(m1) assert m2["a"] == unwrap(m1)["a"] # Can't compare directly "maximum recursion depth exceeded in cmp" assert type(m2["m1"]) == type(unwrap(m1)["m1"])
def cmp_column_row(x, y): """Sort wells by row, then column.""" sort_by_column = cmp(unwrap(x.column), unwrap(y.column)) if sort_by_column == 0: return cmp(unwrap(x.row), unwrap(y.row)) return sort_by_column
def _marshal_image(conn, row, tags_on_images): image = tree._marshal_image(conn, row[0:5]) image['clientPath'] = unwrap(row[5]) image['tags'] = tags_on_images.get(image['id']) or [] return image
def marshal_mapannotations(conn, mapann_value, query=False, case_sensitive=False, mapann_ns=[], mapann_names=[], group_id=-1, experimenter_id=-1, page=1, limit=settings.PAGE): ''' Marshals mapannotation values @param conn OMERO gateway. @type conn L{omero.gateway.BlitzGateway} @param mapann_ns The Map annotation namespace to filter by. @type mapann_ns L{string} @param mapann_names The Map annotation names to filter by. @type mapann_names L{string} @param mapann_value The Map annotation value to filter by. @type mapann_value L{string} @param query Flag allowing to search for value patters. @type query L{boolean} @param group_id The Group ID to filter by or -1 for all groups, defaults to -1 @type group_id L{long} @param experimenter_id The Experimenter (user) ID to filter by or -1 for all experimenters @type experimenter_id L{long} @param page Page number of results to get. `None` or 0 for no paging defaults to 1 @type page L{long} @param limit The limit of results per page to get defaults to the value set in settings.PAGE @type page L{long} ''' mapannotations = [] params, where_clause = _set_parameters(mapann_ns=mapann_ns, mapann_names=mapann_names, mapann_value=mapann_value, query=query, case_sensitive=case_sensitive, params=None, experimenter_id=experimenter_id, page=page, limit=limit) service_opts = deepcopy(conn.SERVICE_OPTS) # Set the desired group context if group_id is None: group_id = -1 service_opts.setOmeroGroup(group_id) qs = conn.getQueryService() q = """ select mv.value as value, count(distinct i.id) as imgCount, count(distinct sl.parent.id) as childCount1, count(distinct pdl.parent.id) as childCount2 from ImageAnnotationLink ial join ial.child a join a.mapValue mv join ial.parent i left outer join i.wellSamples ws left outer join ws.well w left outer join w.plate pl left outer join pl.screenLinks sl left outer join i.datasetLinks dil left outer join dil.parent ds left outer join ds.projectLinks pdl where %s AND ( (dil is null and ds is null and pdl is null and ws is not null and w is not null and pl is not null and sl is not null) OR (ws is null and w is null and pl is null and sl is null and dil is not null and ds is not null and pdl is not null) ) group by mv.value order by count(distinct i.id) DESC """ % (" and ".join(where_clause)) logger.debug("HQL QUERY: %s\nPARAMS: %r" % (q, params)) for e in qs.projection(q, params, service_opts): e = unwrap(e) if e[1] > 0: c = e[1] e = [ e[0], "%s (%d)" % (e[0], e[1]), None, experimenter_id, # e[0]["ownerId"], {}, # e[0]["map_details_permissions"], None, # e[0]["ns"], e[2] + e[3] ] mt = _marshal_map(conn, e[0:7]) mt.update({'extra': {'counter': c}}) mapannotations.append(mt) return mapannotations
def stat_plates(query, screen, images=False): params = ParametersI() params.addString("screen", screen) obj = query.findByQuery(("select s from Screen s " "where s.name = :screen"), params) if not obj: raise Exception("unknown screen: %s" % screen) if images: q = ("select %s from Image i " "join i.wellSamples ws join ws.well w " "join w.plate p join p.screenLinks sl " "join sl.parent s where s.name = :screen") limit = 1000 found = 0 count = unwrap(query.projection(q % "count(distinct i.id)", params))[0][0] print >> stderr, count params.page(0, limit) q = q % "distinct i.id" q = "%s order by i.id" % q while count > 0: rv = unwrap(query.projection(q, params)) count -= len(rv) found += len(rv) params.page(found, limit) for x in rv: yield x[0] return plates = glob(join(screen, "plates", "*")) plates = map(basename, plates) tb = TableBuilder("Plate") tb.cols(["PID", "Wells", "Images"]) well_count = 0 image_count = 0 for plate in plates: params.addString("plate", plate) rv = unwrap( query.projection( ("select p.id, count(distinct w.id), count(distinct i.id)" " from Screen s " "left outer join s.plateLinks spl join spl.child as p " "left outer join p.wells as w " "left outer join w.wellSamples as ws " "left outer join ws.image as i " "where s.name = :screen and p.name = :plate " "group by p.id"), params)) if not rv: tb.row(plate, "MISSING", "", "") else: for x in rv: plate_id, wells, images = x well_count += wells image_count += images tb.row(plate, plate_id, wells, images) tb.row("Total", "", well_count, image_count) print str(tb.build())
def runScript(session, scriptService, scriptPath): """ This will attempt to run the script (scriptPath can be path or ID), asking the user for inputs for all the script parameters, and printing out the results when the script completes. """ try: scriptId = long(scriptPath) except: scriptId = scriptService.getScriptID(scriptPath) # Identify the script we want to run: Get all 'official' scripts and filter by path. print "Running script: %s with ID: %s" % (scriptPath, scriptId) # make a map of all the parameters we want to pass to the script # keys are strings. Values must be omero.rtypes such as rlong, rbool, rlist. map = {} params = scriptService.getParams(scriptId) for key, param in params.inputs.items(): print "" print key if param.description: print param.description if not param.optional: print " * Required" if param.values: print "Options:", ", ".join(unwrap(param.values)) if param.min: print "Min:", param.min.getValue() if param.max: print "Max:", param.max.getValue() prototype = param.prototype prompt = ": " default = None if param.useDefault: default = param.prototype.val prompt = "[%s]: " % default pclass = prototype.__class__ if pclass == omero.rtypes.RListI: valueList = [] listClass = omero.rtypes.rstring l = prototype.val # list if len( l ) > 0: # check if a value type has been set (first item of prototype list) listClass = l[0].getValue().__class__ if listClass == int(1).__class__: listClass = omero.rtypes.rint if listClass == long(1).__class__: listClass = omero.rtypes.rlong print "List:" while (True): value = raw_input(prompt) if value == "": break try: obj = listClass(value) except: print "Invalid entry" continue if isinstance(obj, omero.model.IObject): valueList.append(omero.rtypes.robject(obj)) else: valueList.append(obj) if len(valueList) > 0: map[key] = omero.rtypes.rlist(valueList) elif pclass == omero.rtypes.RMapI: print "MAP!" valueMap = {} m = prototype.val # check if a value type has been set for the map print m else: value = raw_input(prompt) while (True): if value == "": if default: map[key] = param.prototype break try: map[key] = pclass(value) break except: print "Invalid entry" print map # The last parameter is how long to wait as an RInt proc = scriptService.runScript(scriptId, map, None) try: cb = omero.scripts.ProcessCallbackI(client, proc) while not cb.block(1000): # ms. pass cb.close() results = proc.getResults(0) # ms finally: proc.close(False) # handle any results from the script #print results.keys() if 'Message' in results: print "\nRESULTS:", results['Message'].getValue() for result in results.keys(): if result not in ["Message", "stdout", "stderr"]: print "\n", result, results[result].getValue().__class__ printOutErr = True if printOutErr: rawFileService = session.createRawFileStore() queryService = session.getQueryService() if 'stdout' in results: origFile = results['stdout'].getValue() fileId = origFile.getId().getValue() print "\n******** Script generated StdOut in file:%s *******" % fileId print scriptUtil.readFromOriginalFile(rawFileService, queryService, fileId) if 'stderr' in results: origFile = results['stderr'].getValue() fileId = origFile.getId().getValue() print "\n******** Script generated StdErr in file:%s *******" % fileId print scriptUtil.readFromOriginalFile(rawFileService, queryService, fileId) rawFileService.close()
def marshal_autocomplete(conn, mapann_ns=[], query=None, mapann_names=None, group_id=-1, experimenter_id=-1, page=1, limit=settings.PAGE): ''' Marshals mapannotation values for autocomplete @param conn OMERO gateway. @type conn L{omero.gateway.BlitzGateway} @param query The Map annotation value to filter by using like. @type query L{string} @param mapann_names The Map annotation name to filter by. @type mapann_names L{string} @param group_id The Group ID to filter by or -1 for all groups, defaults to -1 @type group_id L{long} @param experimenter_id The Experimenter (user) ID to filter by or -1 for all experimenters @type experimenter_id L{long} @param page Page number of results to get. `None` or 0 for no paging defaults to 1 @type page L{long} @param limit The limit of results per page to get defaults to the value set in settings.PAGE @type page L{long} ''' if not query: return ['Pattern not found'] params, where_clause = _set_parameters(mapann_ns=mapann_ns, mapann_names=mapann_names, mapann_query=query, mapann_value=None, params=None, experimenter_id=experimenter_id, page=page, limit=limit) service_opts = deepcopy(conn.SERVICE_OPTS) # Set the desired group context if group_id is None: group_id = -1 service_opts.setOmeroGroup(group_id) qs = conn.getQueryService() q = """ select new map(mv.value as value) from ImageAnnotationLink ial join ial.child a join a.mapValue mv where %s group by mv.value order by lower(mv.value) """ % (" and ".join(where_clause)) logger.debug("HQL QUERY: %s\nPARAMS: %r" % (q, params)) autocomplete = [] for e in qs.projection(q, params, service_opts): e = unwrap(e) autocomplete.append({'value': e[0]["value"]}) return autocomplete
def who(self, args): show_uuid = args.show_uuid client = self.ctx.conn(args) uuid = self.ctx.get_event_context().sessionUuid req = omero.cmd.CurrentSessionsRequest() try: cb = client.submit(req) try: rsp = cb.getResponse() finally: cb.close(True) headers = ["name", "group", "logged in", "agent", "timeout"] extra = set() results = { "name": [], "group": [], "logged in": [], "agent": [], "timeout": [], "uuid": [] } # Preparse data to find extra columns for idx, s in enumerate(rsp.sessions): for k in list(rsp.data[idx].keys()): extra.add(k) if show_uuid: headers.append("uuid") for add in sorted(extra): headers.append(add) results[add] = [] for idx, s in enumerate(rsp.sessions): ec = rsp.contexts[idx] data = unwrap(rsp.data[idx]) # Handle missing keys for k in extra: if k not in list(data.keys()): results[k].append("---") for k, v in sorted(data.items()): try: if k.endswith("Time"): t = old_div(v, 1000.0) t = time.localtime(t) v = time.strftime('%Y-%m-%d %H:%M:%S', t) except: pass results[k].append(v) results["name"].append(ec.userName) results["group"].append(ec.groupName) if s is not None: t = old_div(s.started.val, 1000.0) t = time.localtime(t) t = time.strftime("%Y-%m-%d %H:%M:%S", t) if uuid == ec.sessionUuid: t = t + " (*)" results["logged in"].append(t) results["agent"].append(unwrap(s.userAgent)) results["timeout"].append(self._parse_timeout( s.timeToIdle)) results["uuid"].append(ec.sessionUuid) else: # Insufficient privileges. The EventContext # will be missing fields as well. msg = "---" results["logged in"].append(msg) results["agent"].append(msg) results["timeout"].append(msg) results["uuid"].append(msg) from omero.util.text import Table, Column columns = tuple([Column(x, results[x]) for x in headers]) self.ctx.out(str(Table(*columns))) except omero.CmdError as ce: self.ctx.dbg(str(ce.err)) self.ctx.die(560, "CmdError: %s" % ce.err.name) except omero.ClientError as ce: if ce.message == "Null handle": v = client.sf.getConfigService().getVersion() self.ctx.die(561, "Operation unsupported. Server version: %s" % v) else: exc = traceback.format_exc() self.ctx.dbg(exc) self.ctx.die(562, "ClientError: %s" % ce.err.name) except omero.LockTimeout: exc = traceback.format_exc() self.ctx.dbg(exc) self.ctx.die(563, "LockTimeout: operation took too long")
def __init__(self, name, optional=True, out=False, description=None, default=None, **kwargs): # Non-Param attributes omero.grid.Param.__init__(self) # Non-Param attributes self._name = name self._in = True self._out = out # Other values will be filled in by the kwargs # Mostly leaving these for backwards compatibility self.description = description self.optional = optional # Assign all the kwargs for k, v in list(kwargs.items()): if not hasattr(self, k): TYPE_LOG.warn("Unknown property: %s", k) setattr(self, k, v) _DEF = self.__get(self.PROTOTYPE_DEFAULT, False) _FUN = self.__get(self.PROTOTYPE_FUNCTION) _MAX = self.__get(self.PROTOTYPE_MAX) _MIN = self.__get(self.PROTOTYPE_MIN) _VAL = self.__get(self.PROTOTYPE_VALUES) # Someone specifically set the prototype, then # we assume that useDefault should be True # For whatever reason, inheritance isn't working. if default is not None: newfunc = _FUN newdefault = default if isinstance(self, List): if isinstance(default, (list, tuple)): newdefault = wrap(default).val elif isinstance(default, omero.RCollection): newfunc = lambda x: x elif isinstance(default, omero.RType): default = [default] else: newfunc = lambda x: x newdefault = rlist([rtype(default)]) self.useDefault = True self.prototype = newfunc(newdefault) else: if not callable(_FUN): raise ValueError("Bad prototype function: %s" % _FUN) # To prevent weirdness, if the class default is # callable, we'll assume its a constructor and # create a new one to prevent modification. try: _def = _DEF() except TypeError: _def = _DEF self.prototype = _FUN(_def) # The following use wrap to guarantee that an rtype is present if self.min is not None: if _MIN is None: self.min = _FUN(self.min) else: self.min = _MIN(self.min) if self.max is not None: if _MAX is None: self.max = _FUN(self.max) else: self.max = _MAX(self.max) if self.values is not None: if _VAL is None: self.values = wrap(self.values) else: self.values = _VAL(self.values) # Now if useDefault has been set, either manually, or # via setting default="..." we check that the default # value matches a value if present if self.values is not None and self.values and self.useDefault: if isinstance(self.prototype, omero.RCollection): test = unwrap(self.prototype.val[0]) else: test = unwrap(self.prototype) values = unwrap(self.values) if test not in values: raise ValueError("%s is not in %s" % (test, values))
def get_rectangle(roi_service, image_id, roi_label): """ Returns (x, y, width, height, zMin, zMax, tMin, tMax) of the first rectange in the image that has @roi_label as text """ result = roi_service.findByImage(image_id, None) roi_text = roi_label.lower() roi_count = 0 rect_count = 0 found_labelled_roi = False for roi in result.rois: roi_count += 1 # go through all the shapes of the ROI for shape in roi.copyShapes(): if type(shape) == omero.model.RectangleI: the_t = unwrap(shape.getTheT()) the_z = unwrap(shape.getTheZ()) t = 0 z = 0 if the_t is not None: t = the_t if the_z is not None: z = the_z x = shape.getX().getValue() y = shape.getY().getValue() tv = shape.getTextValue() if tv is not None: text = tv.getValue() else: text = "" # get ranges for whole ROI if rect_count == 0: z_min = z z_max = z_min t_min = t t_max = t_min width = shape.getWidth().getValue() height = shape.getHeight().getValue() else: z_min = min(z_min, z) z_max = max(z_max, z) t_min = min(t_min, t) t_max = max(t_max, t) rect_count += 1 if text is not None and text.lower() == roi_text: found_labelled_roi = True if found_labelled_roi: return (int(x), int(y), int(width), int(height), int(z_min), int(z_max), int(t_min), int(t_max)) else: rect_count = 0 # try another ROI # if we got here without finding an ROI that matched, simply return any # ROI we have (last one) if roi_count > 0: return (int(x), int(y), int(width), int(height), int(z_min), int(z_max), int(t_min), int(t_max))
def contains_uuids(u1, u2): params = svc.getParams(script) datasets = unwrap(params.inputs["Object"].values) assert u1 == (uuid1 in datasets) assert u2 == (uuid2 in datasets)
def save(self, masks: List[omero.model.Shape], name: str) -> None: # Figure out whether we can flatten some dimensions unique_dims: Dict[str, Set[int]] = { "T": {unwrap(mask.theT) for shapes in masks for mask in shapes}, "C": {unwrap(mask.theC) for shapes in masks for mask in shapes}, "Z": {unwrap(mask.theZ) for shapes in masks for mask in shapes}, } ignored_dimensions: Set[str] = set() print(f"Unique dimensions: {unique_dims}") for d in "TCZ": if unique_dims[d] == {None}: ignored_dimensions.add(d) filename = f"{self.image.id}.zarr" # Verify that we are linking this mask to a real ome-zarr source_image = self.source_image source_image_link = self.source_image if source_image is None: # Assume that we're using the output directory source_image = filename source_image_link = "../.." # Drop "labels/0" src = parse_url(source_image) assert src, "Source image does not exist" input_pyramid = Node(src, []) assert input_pyramid.load(Multiscales), "No multiscales metadata found" input_pyramid_levels = len(input_pyramid.data) root = zarr_open(filename) if self.path in root.group_keys(): out_labels = getattr(root, self.path) else: out_labels = root.create_group(self.path) _mask_shape: List[int] = list(self.image_shape) for d in ignored_dimensions: _mask_shape[DIMENSION_ORDER[d]] = 1 mask_shape: Tuple[int, ...] = tuple(_mask_shape) del _mask_shape print(f"Ignoring dimensions {ignored_dimensions}") if self.style not in ("labeled", "split"): assert False, "6d has been removed" # Create and store binary data labels, fill_colors = self.masks_to_labels( masks, mask_shape, ignored_dimensions, check_overlaps=True, ) scaler = Scaler(max_layer=input_pyramid_levels) label_pyramid = scaler.nearest(labels) pyramid_grp = out_labels.create_group(name) write_multiscale(label_pyramid, pyramid_grp) # TODO: dtype, chunks, overwite # Specify and store metadata image_label_colors: List[JSONDict] = [] image_label = { "version": "0.1", "colors": image_label_colors, "source": {"image": source_image_link}, } if fill_colors: for label_value, rgba_int in sorted(fill_colors.items()): image_label_colors.append( {"label-value": label_value, "rgba": int_to_rgba_255(rgba_int)} ) # TODO: move to write method pyramid_grp.attrs["image-label"] = image_label # Register with labels metadata print(f"Created {filename}/{self.path}/{name}") attrs = out_labels.attrs.asdict() # TODO: could temporarily support "masks" here as well if "labels" in attrs: if name not in attrs["labels"]: attrs["labels"].append(name) else: attrs["labels"] = [name] out_labels.attrs.update(attrs)
def marshal_plates(conn, screen_id, mapann_value, query=False, mapann_ns=[], mapann_names=[], group_id=-1, experimenter_id=-1, page=1, limit=settings.PAGE): ''' Marshals plates @param conn OMERO gateway. @type conn L{omero.gateway.BlitzGateway} @param screen_id The Screen ID to filter by. @type screen_id L{long} @param mapann_value The Map annotation value to filter by. @type mapann_value L{string} @param query Flag allowing to search for value patters. @type query L{boolean} @param mapann_ns The Map annotation namespace to filter by. @type mapann_ns L{string} @param mapann_names The Map annotation names to filter by. @type mapann_names L{string} @param group_id The Group ID to filter by or -1 for all groups, defaults to -1 @type group_id L{long} @param experimenter_id The Experimenter (user) ID to filter by or -1 for all experimenters @type experimenter_id L{long} @param page Page number of results to get. `None` or 0 for no paging defaults to 1 @type page L{long} @param limit The limit of results per page to get defaults to the value set in settings.PAGE @type page L{long} ''' plates = [] # early exit if screen_id is None or not isinstance(screen_id, long): return plates params, where_clause = _set_parameters(mapann_ns=mapann_ns, mapann_names=mapann_names, query=query, mapann_value=mapann_value, params=None, experimenter_id=experimenter_id, page=page, limit=limit) params.addLong("sid", screen_id) where_clause.append('screen.id = :sid') service_opts = deepcopy(conn.SERVICE_OPTS) # Set the desired group context if group_id is None: group_id = -1 service_opts.setOmeroGroup(group_id) qs = conn.getQueryService() # TODO: Joining wellsample should be enough since wells are annotated # with the same annotaitons as images. In the future if that change, # query has to be restored to: # - count(distinct i.id) as childCount) # - from ImageAnnotationLink ial join ial.child a join a.mapValue mv # - join ial.parent i join i.wellSamples ws join ws.well w # - join w.plate plate join plate.screenLinks sl join sl.parent screen q = """ select new map(mv.value as value, plate.id as id, plate.name as name, plate.details.owner.id as ownerId, plate as plate_details_permissions, count(distinct ws.id) as childCount) from WellAnnotationLink wal join wal.child a join a.mapValue mv join wal.parent w join w.wellSamples ws join w.plate plate join plate.screenLinks sl join sl.parent screen where %s group by plate.id, plate.name, mv.value order by lower(plate.name), plate.id, mv.value """ % (" and ".join(where_clause)) logger.debug("HQL QUERY: %s\nPARAMS: %r" % (q, params)) for e in qs.projection(q, params, service_opts): e = unwrap(e) v = e[0]['value'] e = [ e[0]['id'], e[0]['name'], e[0]['ownerId'], e[0]['plate_details_permissions'], e[0]['childCount'] ] mp = _marshal_plate(conn, e[0:5]) extra = {'extra': {'node': 'plate'}} if mapann_value is not None: extra['extra']['value'] = v mp.update(extra) plates.append(mp) return plates
def marshal_images(conn, parent, parent_id, mapann_value, mapann_ns=[], mapann_names=[], load_pixels=False, group_id=-1, experimenter_id=-1, page=1, date=False, thumb_version=False, limit=settings.PAGE): ''' Marshals images @param conn OMERO gateway. @type conn L{omero.gateway.BlitzGateway} @param plate_id The Plate ID to filter by. @type plate_id L{long} @param mapann_names The Map annotation names to filter by. @type mapann_names L{string} @param mapann_value The Map annotation value to filter by. @type mapann_value L{string} @param load_pixels Whether to load the X,Y,Z dimensions @type load_pixels Boolean @param group_id The Group ID to filter by or -1 for all groups, defaults to -1 @type group_id L{long} @param experimenter_id The Experimenter (user) ID to filter by or -1 for all experimenters @type experimenter_id L{long} @param page Page number of results to get. `None` or 0 for no paging defaults to 1 @type page L{long} @param limit The limit of results per page to get defaults to the value set in settings.PAGE @type page L{long} ''' images = [] params, where_clause = _set_parameters(mapann_ns=mapann_ns, mapann_names=mapann_names, mapann_query=None, mapann_value=mapann_value, params=None, experimenter_id=experimenter_id, page=page, limit=limit) service_opts = deepcopy(conn.SERVICE_OPTS) # Set the desired group context if group_id is None: group_id = -1 service_opts.setOmeroGroup(group_id) from_join_clauses = [] qs = conn.getQueryService() extraValues = "" if load_pixels: extraValues = """ , pix.sizeX as sizeX, pix.sizeY as sizeY, pix.sizeZ as sizeZ """ if date: extraValues += """, image.details.creationEvent.time as date, image.acquisitionDate as acqDate """ q = """ select new map(image.id as id, image.name as name, image.details.owner.id as ownerId, image as image_details_permissions, image.fileset.id as filesetId %s) from Image image """ % extraValues if load_pixels: # We use 'left outer join', since we still want images if no pixels q += ' left outer join image.pixels pix ' q += """ where image.id in ( """ if parent == 'plate': from_join_clauses.append(""" ImageAnnotationLink ial join ial.child a join a.mapValue mv join ial.parent image join image.wellSamples ws join ws.well well join well.plate plate """) params.addLong("pid", parent_id) where_clause.append('plate.id = :pid') if parent == 'dataset': from_join_clauses.append(""" ImageAnnotationLink ial join ial.child a join a.mapValue mv join ial.parent image join image.datasetLinks dil join dil.parent dataset """) params.addLong("did", parent_id) where_clause.append('dataset.id = :did') q += """ %s %s order by lower(image.name)) """ % (' select image.id from ' + ' '.join(from_join_clauses), build_clause(where_clause, 'where', 'and')) logger.debug("HQL QUERY: %s\nPARAMS: %r" % (q, params)) for e in qs.projection(q, params, service_opts): e = unwrap(e)[0] d = [ e["id"], e["name"], e["ownerId"], e["image_details_permissions"], e["filesetId"] ] kwargs = {'conn': conn, 'row': d[0:5]} if load_pixels: d = [e["sizeX"], e["sizeY"], e["sizeZ"]] kwargs['row_pixels'] = d if date: kwargs['acqDate'] = e['acqDate'] kwargs['date'] = e['date'] images.append(_marshal_image(**kwargs)) # Load thumbnails separately # We want version of most recent thumbnail (max thumbId) owned by user if thumb_version and len(images) > 0: userId = conn.getUserId() iids = [i['id'] for i in images] params = omero.sys.ParametersI() params.addIds(iids) params.add('thumbOwner', wrap(userId)) q = """select image.id, thumbs.version from Image image join image.pixels pix join pix.thumbnails thumbs where image.id in (:ids) and thumbs.id = ( select max(t.id) from Thumbnail t where t.pixels = pix.id and t.details.owner.id = :thumbOwner ) """ thumbVersions = {} logger.debug("HQL QUERY: %s\nPARAMS: %r" % (q, params)) for t in qs.projection(q, params, service_opts): iid, tv = unwrap(t) thumbVersions[iid] = tv # For all images, set thumb version if we have it... for i in images: if i['id'] in thumbVersions: i['thumbVersion'] = thumbVersions[i['id']] return images
def marshal_datasets(conn, project_id, mapann_value, query=False, mapann_ns=[], mapann_names=[], group_id=-1, experimenter_id=-1, page=1, limit=settings.PAGE): ''' Marshals plates @param conn OMERO gateway. @type conn L{omero.gateway.BlitzGateway} @param project_id The Project ID to filter by. @type project_id L{long} @param mapann_value The Map annotation value to filter by. @type mapann_value L{string} @param query Flag allowing to search for value patters. @type query L{boolean} @param mapann_ns The Map annotation namespace to filter by. @type mapann_ns L{string} @param mapann_names The Map annotation names to filter by. @type mapann_names L{string} @param group_id The Group ID to filter by or -1 for all groups, defaults to -1 @type group_id L{long} @param experimenter_id The Experimenter (user) ID to filter by or -1 for all experimenters @type experimenter_id L{long} @param page Page number of results to get. `None` or 0 for no paging defaults to 1 @type page L{long} @param limit The limit of results per page to get defaults to the value set in settings.PAGE @type page L{long} ''' datasets = [] # early exit if project_id is None or not isinstance(project_id, long): return datasets params, where_clause = _set_parameters(mapann_ns=mapann_ns, mapann_names=mapann_names, query=query, mapann_value=mapann_value, params=None, experimenter_id=experimenter_id, page=page, limit=limit) params.addLong("pid", project_id) where_clause.append('project.id = :pid') service_opts = deepcopy(conn.SERVICE_OPTS) # Set the desired group context if group_id is None: group_id = -1 service_opts.setOmeroGroup(group_id) qs = conn.getQueryService() q = """ select new map(mv.value as value, dataset.id as id, dataset.name as name, dataset.details.owner.id as ownerId, dataset as dataset_details_permissions, count(distinct i.id) as childCount) from ImageAnnotationLink ial join ial.child a join a.mapValue mv join ial.parent i join i.datasetLinks dil join dil.parent dataset join dataset.projectLinks pl join pl.parent project where %s group by dataset.id, dataset.name, mv.value order by lower(dataset.name), dataset.id, mv.value """ % (" and ".join(where_clause)) logger.debug("HQL QUERY: %s\nPARAMS: %r" % (q, params)) for e in qs.projection(q, params, service_opts): e = unwrap(e) v = e[0]['value'] e = [ e[0]['id'], e[0]['name'], e[0]['ownerId'], e[0]['dataset_details_permissions'], e[0]['childCount'] ] mp = _marshal_plate(conn, e[0:5]) extra = {'extra': {'node': 'dataset'}} if mapann_value is not None: extra['extra']['value'] = v mp.update(extra) datasets.append(mp) return datasets
def marshal_plates(conn, screen_id, mapann_value, mapann_ns=[], mapann_names=[], group_id=-1, experimenter_id=-1, page=1, limit=settings.PAGE): ''' Marshals plates @param conn OMERO gateway. @type conn L{omero.gateway.BlitzGateway} @param screen_id The Screen ID to filter by. @type screen_id L{long} @param mapann_value The Map annotation value to filter by. @type mapann_value L{string} @param mapann_names The Map annotation names to filter by. @type mapann_names L{string} @param group_id The Group ID to filter by or -1 for all groups, defaults to -1 @type group_id L{long} @param experimenter_id The Experimenter (user) ID to filter by or -1 for all experimenters @type experimenter_id L{long} @param page Page number of results to get. `None` or 0 for no paging defaults to 1 @type page L{long} @param limit The limit of results per page to get defaults to the value set in settings.PAGE @type page L{long} ''' plates = [] params, where_clause = _set_parameters(mapann_ns=mapann_ns, mapann_names=mapann_names, mapann_query=None, mapann_value=mapann_value, params=None, experimenter_id=experimenter_id, page=page, limit=limit) params.addLong("sid", screen_id) where_clause.append('screen.id = :sid') service_opts = deepcopy(conn.SERVICE_OPTS) # Set the desired group context if group_id is None: group_id = -1 service_opts.setOmeroGroup(group_id) qs = conn.getQueryService() q = """ select new map(plate.id as id, plate.name as name, plate.details.owner.id as ownerId, plate as plate_details_permissions, count(distinct i.id) as childCount) from ImageAnnotationLink ial join ial.child a join a.mapValue mv join ial.parent i join i.wellSamples ws join ws.well w join w.plate plate join plate.screenLinks sl join sl.parent screen where %s group by plate.id, plate.name order by lower(plate.name), plate.id """ % (" and ".join(where_clause)) logger.debug("HQL QUERY: %s\nPARAMS: %r" % (q, params)) for e in qs.projection(q, params, service_opts): e = unwrap(e) e = [ e[0]["id"], e[0]["name"], e[0]["ownerId"], e[0]["plate_details_permissions"], e[0]["childCount"] ] mp = _marshal_plate(conn, e[0:5]) mp.update({'extra': {'value': mapann_value, 'node': 'plate'}}) plates.append(mp) return plates
def count_mapannotations(conn, mapann_value, query=False, case_sensitive=False, mapann_ns=[], mapann_names=[], group_id=-1, experimenter_id=-1): ''' Count mapannotiation values @param conn OMERO gateway. @type conn L{omero.gateway.BlitzGateway} @param mapann_ns The Map annotation namespace to filter by. @type mapann_ns L{string} @param mapann_names The Map annotation names to filter by. @type mapann_names L{string} @param mapann_value The Map annotation value to filter by. @type mapann_value L{string} @param query Flag allowing to search for value patters. @type query L{boolean} @param group_id The Group ID to filter by or -1 for all groups, defaults to -1 @type group_id L{long} @param experimenter_id The Experimenter (user) ID to filter by or -1 for all experimenters @type experimenter_id L{long} ''' params, where_clause = _set_parameters(mapann_ns=mapann_ns, mapann_names=mapann_names, query=query, mapann_value=mapann_value, case_sensitive=case_sensitive, params=None, experimenter_id=experimenter_id, page=None, limit=None) service_opts = deepcopy(conn.SERVICE_OPTS) # Set the desired group context if group_id is None: group_id = -1 service_opts.setOmeroGroup(group_id) qs = conn.getQueryService() q = """ select count(distinct mv.value) as childCount from ImageAnnotationLink ial join ial.child a join a.mapValue mv join ial.parent i left outer join i.wellSamples ws left outer join i.datasetLinks dil where %s AND ( (ws is not null) OR (dil is not null) ) """ % (" and ".join(where_clause)) logger.debug("HQL QUERY: %s\nPARAMS: %r" % (q, params)) counter = unwrap(qs.projection(q, params, service_opts))[0][0] return counter
def marshal_projects(conn, mapann_ns=[], mapann_names=[], mapann_value=None, mapann_query=None, group_id=-1, experimenter_id=-1, page=1, limit=settings.PAGE): ''' Marshals projects @param conn OMERO gateway. @type conn L{omero.gateway.BlitzGateway} @param mapann_names The Map annotation names to filter by. @type mapann_names L{string} @param mapann_value The Map annotation value to filter by. @type mapann_value L{string} @param group_id The Group ID to filter by or -1 for all groups, defaults to -1 @type group_id L{long} @param experimenter_id The Experimenter (user) ID to filter by or -1 for all experimenters @type experimenter_id L{long} @param page Page number of results to get. `None` or 0 for no paging defaults to 1 @type page L{long} @param limit The limit of results per page to get defaults to the value set in settings.PAGE @type page L{long} ''' projects = [] params, where_clause = _set_parameters(mapann_ns=mapann_ns, mapann_names=mapann_names, mapann_query=mapann_query, mapann_value=mapann_value, params=None, experimenter_id=experimenter_id, page=page, limit=limit) service_opts = deepcopy(conn.SERVICE_OPTS) # Set the desired group context if group_id is None: group_id = -1 service_opts.setOmeroGroup(group_id) qs = conn.getQueryService() q = """ select new map(project.id as id, project.name as name, project.details.owner.id as ownerId, project as project_details_permissions, count(distinct dataset.id) as childCount, count(distinct i.id) as imgCount) from ImageAnnotationLink ial join ial.child a join a.mapValue mv join ial.parent i join i.datasetLinks dil join dil.parent dataset join dataset.projectLinks pl join pl.parent project where %s group by project.id, project.name order by lower(project.name), project.id """ % (" and ".join(where_clause)) logger.debug("HQL QUERY: %s\nPARAMS: %r" % (q, params)) for e in qs.projection(q, params, service_opts): e = unwrap(e) e = [ e[0]["id"], "%s (%d)" % (e[0]["name"], e[0]["imgCount"]), e[0]["ownerId"], e[0]["project_details_permissions"], e[0]["childCount"] ] ms = _marshal_screen(conn, e[0:5]) ms.update({'extra': {'value': mapann_value}}) projects.append(ms) return projects
def get_rectangle(roi_service, image_id, roi_label): """ Returns (x, y, width, height, timeShapeMap) of the all rectanges in the first ROI of the image where timeShapeMap is a map of tIndex: (x,y,zMin,zMax) x, y, Width and Height are from the first rectangle (assumed that all are same size!) """ result = roi_service.findByImage(image_id, None) roi_text = roi_label.lower() roi_count = 0 rect_count = 0 found_labelled_roi = False for roi in result.rois: rectangles = [shape for shape in roi.copyShapes() if isinstance(shape, omero.model.RectangleI)] if len(rectangles) == 0: continue time_shape_map = {} # map of tIndex: (x,y,zMin,zMax) for a single roi for shape in rectangles: the_t = unwrap(shape.getTheT()) the_z = unwrap(shape.getTheZ()) t = 0 z = 0 if the_t is not None: t = the_t if the_z is not None: z = the_z x = int(shape.getX().getValue()) y = int(shape.getY().getValue()) text = shape.getTextValue() and shape.getTextValue().getValue() \ or None # build a map of tIndex: (x,y,zMin,zMax) if t in time_shape_map: xx, yy, min_z, max_z = time_shape_map[t] tz_min = min(min_z, z) tz_max = max(max_z, z) time_shape_map[t] = (x, y, tz_min, tz_max) else: time_shape_map[t] = (x, y, z, z) # get ranges for whole ROI if rect_count == 0: width = shape.getWidth().getValue() height = shape.getHeight().getValue() x1 = x y1 = y rect_count += 1 if text is not None and text.lower() == roi_text: found_labelled_roi = True # will return after the first ROI that matches text if found_labelled_roi: return (int(x1), int(y1), int(width), int(height), time_shape_map) else: if rect_count > 0: roi_count += 1 rect_count = 0 # try another ROI # if we got here without finding an ROI that matched, simply return any # ROI we have (last one) if roi_count > 0: return (int(x1), int(y1), int(width), int(height), time_shape_map)
def marshal_mapannotations(conn, mapann_ns=[], mapann_names=[], mapann_query=None, mapann_value=None, group_id=-1, experimenter_id=-1, page=1, limit=settings.PAGE): ''' Marshals mapannotation values @param conn OMERO gateway. @type conn L{omero.gateway.BlitzGateway} @param mapann_names The Map annotation names to filter by. @type mapann_names L{string} @param mapann_query The Map annotation value to filter by using like. @type mapann_query L{string} @param group_id The Group ID to filter by or -1 for all groups, defaults to -1 @type group_id L{long} @param experimenter_id The Experimenter (user) ID to filter by or -1 for all experimenters @type experimenter_id L{long} @param page Page number of results to get. `None` or 0 for no paging defaults to 1 @type page L{long} @param limit The limit of results per page to get defaults to the value set in settings.PAGE @type page L{long} ''' mapannotations = [] params, where_clause = _set_parameters(mapann_ns=mapann_ns, mapann_names=mapann_names, mapann_query=mapann_query, mapann_value=mapann_value, params=None, experimenter_id=experimenter_id, page=page, limit=limit) service_opts = deepcopy(conn.SERVICE_OPTS) # Set the desired group context if group_id is None: group_id = -1 service_opts.setOmeroGroup(group_id) qs = conn.getQueryService() # TODO: # a.details.owner.id as ownerId, # a as map_details_permissions, q = """ select new map( mv.value as value, count(distinct s.id) as childCount, count(distinct i.id) as imgCount) from ImageAnnotationLink ial join ial.child a join a.mapValue mv join ial.parent i join i.wellSamples ws join ws.well w join w.plate p join p.screenLinks sl join sl.parent s where %s group by mv.value order by count(i.id) DESC """ % (" and ".join(where_clause)) logger.debug("HQL QUERY: %s\nPARAMS: %r" % (q, params)) for e in qs.projection(q, params, service_opts): e = unwrap(e) c = e[0]["imgCount"] e = [ e[0]["value"], "%s (%d)" % (e[0]["value"], e[0]["imgCount"]), None, experimenter_id, # e[0]["ownerId"], {}, # e[0]["map_details_permissions"], None, # e[0]["ns"], e[0]["childCount"] ] mt = _marshal_map(conn, e[0:7]) mt.update({'extra': {'counter': c}}) mapannotations.append(mt) q = """ select new map( mv.value as value, a.ns as ns, count(distinct p.id) as childCount, count(distinct i.id) as imgCount) from ImageAnnotationLink ial join ial.child a join a.mapValue mv join ial.parent i join i.datasetLinks dsl join dsl.parent ds join ds.projectLinks pl join pl.parent p where %s group by mv.value, a.ns order by count(i.id) DESC """ % (" and ".join(where_clause)) logger.debug("HQL QUERY: %s\nPARAMS: %r" % (q, params)) for e in qs.projection(q, params, service_opts): e = unwrap(e) c = e[0]["imgCount"] e = [ e[0]["value"], "%s (%d)" % (e[0]["value"], e[0]["imgCount"]), None, experimenter_id, # e[0]["ownerId"], {}, # e[0]["map_details_permissions"], e[0]["ns"], e[0]["childCount"] ] mt = _marshal_map(conn, e[0:7]) mt.update({'extra': {'counter': c}}) mapannotations.append(mt) return mapannotations
def get_image_detail_and_tags(request, conn=None, **kwargs): # According to REST, this should be a GET, but because of the amount of # data being submitted, this is problematic if not request.POST: return HttpResponseNotAllowed('Methods allowed: POST') image_ids = request.POST.getlist("imageIds[]") if not image_ids: return HttpResponseBadRequest('Image IDs required') image_ids = map(long, image_ids) group_id = request.session.get('active_group') if group_id is None: group_id = conn.getEventContext().groupId # All the tags available to the user tags = tree.marshal_tags(conn, group_id=group_id) # Details about the images specified params = omero.sys.ParametersI() service_opts = deepcopy(conn.SERVICE_OPTS) # Set the desired group context service_opts.setOmeroGroup(group_id) params.add('iids', wrap(image_ids)) qs = conn.getQueryService() # Get the tags that are applied to individual images q = """ SELECT DISTINCT itlink.parent.id, itlink.child.id FROM ImageAnnotationLink itlink WHERE itlink.child.class=TagAnnotation AND itlink.parent.id IN (:iids) """ tags_on_images = {} for e in qs.projection(q, params, service_opts): tags_on_images.setdefault(unwrap(e[0]), []).append(unwrap(e[1])) # Get the images' details q = """ SELECT new map(image.id AS id, image.name AS name, image.details.owner.id AS ownerId, image AS image_details_permissions, image.fileset.id AS filesetId, filesetentry.clientPath AS clientPath) FROM Image image JOIN image.fileset fileset JOIN fileset.usedFiles filesetentry WHERE index(filesetentry) = 0 AND image.id IN (:iids) ORDER BY lower(image.name), image.id """ images = [] for e in qs.projection(q, params, service_opts): e = unwrap(e)[0] d = [ e["id"], e["name"], e["ownerId"], e["image_details_permissions"], e["filesetId"], e["clientPath"] ] images.append(_marshal_image(conn, d, tags_on_images)) # Get the users from this group for reference users = tree.marshal_experimenters(conn, group_id=group_id, page=None) return JsonResponse({'tags': tags, 'images': images, 'users': users})
def count_mapannotations(conn, mapann_ns=[], mapann_names=[], mapann_value=None, mapann_query=None, group_id=-1, experimenter_id=-1, page=1, limit=settings.PAGE): ''' Count mapannotiation values @param conn OMERO gateway. @type conn L{omero.gateway.BlitzGateway} @param mapann_names The Map annotation names to filter by. @type mapann_names L{string} @param mapann_query The Map annotation value to filter by using like. @type mapann_query L{string} @param group_id The Group ID to filter by or -1 for all groups, defaults to -1 @type group_id L{long} @param experimenter_id The Experimenter (user) ID to filter by or -1 for all experimenters @type experimenter_id L{long} ''' params, where_clause = _set_parameters(mapann_ns=mapann_ns, mapann_names=mapann_names, mapann_query=mapann_query, mapann_value=mapann_value, params=None, experimenter_id=experimenter_id, page=page, limit=limit) service_opts = deepcopy(conn.SERVICE_OPTS) # Set the desired group context if group_id is None: group_id = -1 service_opts.setOmeroGroup(group_id) qs = conn.getQueryService() q = """ select count( distinct mv.value) as childCount from ImageAnnotationLink ial join ial.child a join a.mapValue mv join ial.parent i join i.wellSamples ws join ws.well w join w.plate p join p.screenLinks sl join sl.parent s where %s """ % (" and ".join(where_clause)) counter = unwrap(qs.projection(q, params, service_opts))[0][0] q = """ select count( distinct mv.value) as childCount from ImageAnnotationLink ial join ial.child a join a.mapValue mv join ial.parent i join i.datasetLinks dsl join dsl.parent ds join ds.projectLinks pl join pl.parent p where %s """ % (" and ".join(where_clause)) logger.debug("HQL QUERY: %s\nPARAMS: %r" % (q, params)) counter += unwrap(qs.projection(q, params, service_opts))[0][0] return counter
def remove(self, parents, tag_owner_id=None): """ Removes the current object (file, tag, comment, dataset, plate, image) from its parents by manually deleting the link. Orphaned comments will be deleted server side. If self.tag and owner_id is specified, only remove the tag if it is owned by that owner @param parents: List of parent IDs, E.g. ['image-123'] """ toDelete = [] notFound = [] for p in parents: parent = p.split("-") dtype = str(parent[0]) parentId = int(parent[1]) if dtype == "acquisition": dtype = "PlateAcquisition" if self.tag: for al in self.tag.getParentLinks(dtype, [parentId]): if (al is not None and al.canDelete() and (tag_owner_id is None or unwrap(al.details.owner.id) == tag_owner_id)): toDelete.append(al._obj) elif self.file: for al in self.file.getParentLinks(dtype, [parentId]): if al is not None and al.canDelete(): toDelete.append(al._obj) elif self.comment: # remove the comment from specified parent # the comment is automatically deleted when orphaned for al in self.comment.getParentLinks(dtype, [parentId]): if al is not None and al.canDelete(): toDelete.append(al._obj) elif self.dataset is not None: if dtype == "project": for pdl in self.dataset.getParentLinks([parentId]): if pdl is not None: toDelete.append(pdl._obj) elif self.plate is not None: if dtype == "screen": for spl in self.plate.getParentLinks([parentId]): if spl is not None: toDelete.append(spl._obj) elif self.image is not None: if dtype == "dataset": for dil in self.image.getParentLinks([parentId]): if dil is not None: toDelete.append(dil._obj) else: notFound.append(p) # Need to group objects by class then batch delete linksByType = {} for obj in toDelete: objType = obj.__class__.__name__.rstrip("I") if objType not in linksByType: linksByType[objType] = [] linksByType[objType].append(obj.id.val) for linkType, ids in linksByType.items(): self.conn.deleteObjects(linkType, ids, wait=True) if len(notFound) > 0: raise AttributeError("Attribute not specified. Cannot be removed.")
# second parameter scripts.List("IDs", grouping="2", optional=False).ofType(rlong(0)), scripts.String("First_key", grouping="3", default=""), scripts.String("First_value", grouping="4", default=""), scripts.String("Second_key", grouping="5", default=""), scripts.String("Second_value", grouping="6", default=""), ) # we can now create our Blitz Gateway by wrapping the client object conn = BlitzGateway(client_obj=client) script_params = client.getInputs(unwrap=True) print script_params namespace = omero.constants.metadata.NSCLIENTMAPANNOTATION # get the 'IDs' parameter (which we have restricted to 'Image' IDs) ids = unwrap(client.getInput("IDs")) images = conn.getObjects("Image", ids) first_k = script_params["First_key"] first_v = script_params["First_value"] second_k = script_params["Second_key"] second_v = script_params["Second_value"] for i in images: print i.name key_value_data = [] to_delete = [] for ann in i.listAnnotations(ns=namespace): kv = ann.getValue() key_value_data.extend(kv)
def run(password, target, host, port): for i in range(1, 51): username = "******" % i print(username) conn = BlitzGateway(username, password, host=host, port=port) try: conn.connect() params = omero.sys.ParametersI() params.addString('username', username) query = "from Dataset where name='%s' \ AND details.owner.omeName=:username" % target query_service = conn.getQueryService() datasets = query_service.findAllByQuery(query, params, conn.SERVICE_OPTS) if len(datasets) == 0: print("No datasets with name %s found" % target) continue dataset_id = datasets[0].getId().getValue() print('dataset', dataset_id) params2 = omero.sys.ParametersI() params2.addId(dataset_id) query = "select l.child.id from DatasetImageLink l \ where l.parent.id = :id" images = query_service.projection(query, params2, conn.SERVICE_OPTS) for k in range(0, len(images)): image_id = images[k][0].getValue() delta_t = 300 image = conn.getObject("Image", image_id) params = omero.sys.ParametersI() params.addLong('pid', image.getPixelsId()) query = "from PlaneInfo as Info where \ Info.theZ=0 and Info.theC=0 and pixels.id=:pid" info_list = query_service.findAllByQuery( query, params, conn.SERVICE_OPTS) print('info_list', len(info_list)) if len(info_list) == 0: print("Creating info...", image.getSizeT()) info_list = [] for t_index in range(image.getSizeT()): print(' t', t_index) info = PlaneInfoI() info.theT = rint(t_index) info.theZ = rint(0) info.theC = rint(0) info.pixels = PixelsI(image.getPixelsId(), False) dt = t_index * delta_t info.deltaT = TimeI(dt, UnitsTime.SECOND) info_list.append(info) else: for info in info_list: unwrap_t = unwrap(info.theT) unwrap_z = unwrap(info.theZ) print('theT %s, theZ %s' % (unwrap_t, unwrap_z)) t_index = info.theT.getValue() dt = t_index * delta_t info.deltaT = TimeI(dt, UnitsTime.SECOND) print("Saving info_list", len(info_list)) conn.getUpdateService().saveArray(info_list) except Exception as exc: print("Error when setting the timestamps: %s" % str(exc)) finally: conn.close()
def marshal_autocomplete(conn, mapann_value, query=True, case_sensitive=False, mapann_ns=[], mapann_names=None, group_id=-1, experimenter_id=-1, page=1, limit=settings.PAGE): ''' Marshals mapannotation values for autocomplete @param conn OMERO gateway. @type conn L{omero.gateway.BlitzGateway} @param mapann_value The Map annotation value to filter by. @type mapann_value L{string} @param query Flag allowing to search for value patters. @type query L{boolean} @param mapann_ns The Map annotation namespace to filter by. @type mapann_ns L{string} @param mapann_names The Map annotation name to filter by. @type mapann_names L{string} @param group_id The Group ID to filter by or -1 for all groups, defaults to -1 @type group_id L{long} @param experimenter_id The Experimenter (user) ID to filter by or -1 for all experimenters @type experimenter_id L{long} @param page Page number of results to get. `None` or 0 for no paging defaults to 1 @type page L{long} @param limit The limit of results per page to get defaults to the value set in settings.PAGE @type page L{long} ''' autocomplete = [] if not mapann_value: return autocomplete mapann_value = mapann_value if case_sensitive else mapann_value.lower() # mapann_value is customized due to multiple queries params, where_clause = _set_parameters(mapann_ns=mapann_ns, mapann_names=mapann_names, query=query, mapann_value=None, case_sensitive=case_sensitive, params=None, experimenter_id=experimenter_id, page=page, limit=limit) params2 = copy.deepcopy(params) where_clause2 = copy.deepcopy(where_clause) _cwc = 'mv.value' if case_sensitive else 'lower(mv.value)' params.addString("query", rstring("%s%%" % _escape_chars_like(mapann_value))) where_clause.append(' %s like :query ' % _cwc) order_by = "length(mv.value) ASC, lower(mv.value) ASC" params2.addString("query", rstring("%%%s%%" % _escape_chars_like(mapann_value))) params2.addString("query2", rstring("%s%%" % _escape_chars_like(mapann_value))) where_clause2.append('%s like :query' % _cwc) where_clause2.append('%s not like :query2' % _cwc) order_by2 = "lower(mv.value)" service_opts = deepcopy(conn.SERVICE_OPTS) # Set the desired group context if group_id is None: group_id = -1 service_opts.setOmeroGroup(group_id) qs = conn.getQueryService() _q = """ select new map(mv.value as value) from ImageAnnotationLink ial join ial.child a join a.mapValue mv where {where_clause} group by mv.value order by {order_by} """ # query by value% q = _q.format(where_clause=(" and ".join(where_clause)), order_by=order_by) logger.debug("HQL QUERY: %s\nPARAMS: %r" % (q, params)) for e in qs.projection(q, params, service_opts): e = unwrap(e) autocomplete.append({'value': e[0]["value"]}) # query by %value% and exclude value% q = _q.format(where_clause=(" and ".join(where_clause2)), order_by=order_by2) logger.debug("HQL QUERY: %s\nPARAMS: %r" % (q, params)) for e in qs.projection(q, params2, service_opts): e = unwrap(e) autocomplete.append({'value': e[0]["value"]}) return autocomplete
def merge(self, args): """Uses PIL to read multiple planes from a local folder. Planes are combined and uploaded to OMERO as new images with additional T, C, Z dimensions. It should be run as a local script (not via scripting service) in order that it has access to the local users file system. Therefore need EMAN2 or PIL installed locally. Example usage: will$ bin/omero cecog merge /Applications/CecogPackage/Data/Demo_data/0037/ Since this dir does not contain folders, this will upload images in '0037' into a Dataset called Demo_data in a Project called 'Data'. will$ bin/omero cecog merge /Applications/CecogPackage/Data/Demo_data/ Since this dir does contain folders, this will look for images in all subdirectories of 'Demo_data' and upload images into a Dataset called Demo_data in a Project called 'Data'. Images will be combined in Z, C and T according to the MetaMorph_PlateScanPackage naming convention. E.g. tubulin_P0037_T00005_Cgfp_Z1_S1.tiff is Point 37, Timepoint 5, Channel gfp, Z 1. S? see /Applications/CecogPackage/CecogAnalyzer.app/Contents/Resources/resources/naming_schemes.conf """ """ Processes the command args, makes project and dataset then calls uploadDirAsImages() to process and upload the images to OMERO. """ from omero.rtypes import unwrap from omero.util.script_utils import uploadDirAsImages path = args.path client = self.ctx.conn(args) queryService = client.sf.getQueryService() updateService = client.sf.getUpdateService() pixelsService = client.sf.getPixelsService() # if we don't have any folders in the 'dir' E.g. CecogPackage/Data/Demo_data/0037/ # then 'Demo_data' becomes a dataset subDirs = [] for f in os.listdir(path): fullpath = path + f # process folders in root dir: if os.path.isdir(fullpath): subDirs.append(fullpath) # get the dataset name and project name from path if len(subDirs) == 0: p = path[:-1] # will remove the last folder p = os.path.dirname(p) else: if os.path.basename(path) == "": p = path[:-1] # remove slash datasetName = os.path.basename(p) # e.g. Demo_data p = p[:-1] p = os.path.dirname(p) projectName = os.path.basename(p) # e.g. Data self.ctx.err("Putting images in Project: %s Dataset: %s" % (projectName, datasetName)) # create dataset dataset = omero.model.DatasetI() dataset.name = rstring(datasetName) dataset = updateService.saveAndReturnObject(dataset) # create project project = omero.model.ProjectI() project.name = rstring(projectName) project = updateService.saveAndReturnObject(project) # put dataset in project link = omero.model.ProjectDatasetLinkI() link.parent = omero.model.ProjectI(project.id.val, False) link.child = omero.model.DatasetI(dataset.id.val, False) updateService.saveAndReturnObject(link) if len(subDirs) > 0: for subDir in subDirs: self.ctx.err("Processing images in %s" % subDir) rv = uploadDirAsImages(client.sf, queryService, updateService, pixelsService, subDir, dataset) self.ctx.out("%s" % unwrap(rv)) # if there are no sub-directories, just put all the images in the dir else: self.ctx.err("Processing images in %s" % path) rv = uploadDirAsImages(client.sf, queryService, updateService, pixelsService, path, dataset) self.ctx.out("%s" % unwrap(rv))
def masks_to_labels( self, masks: List[omero.model.Mask], mask_shape: Tuple[int, ...], ignored_dimensions: Set[str] = None, check_overlaps: bool = True, ) -> Tuple[np.ndarray, Dict[int, str]]: """ :param masks [MaskI]: Iterable container of OMERO masks :param mask_shape 5-tuple: the image dimensions (T, C, Z, Y, X), taking into account `ignored_dimensions` :param ignored_dimensions set(char): Ignore these dimensions and set size to 1 :param check_overlaps bool: Whether to check for overlapping masks or not :return: Label image with size `mask_shape` as well as color metadata. TODO: Move to https://github.com/ome/omero-rois/ """ # FIXME: hard-coded dimensions assert len(mask_shape) > 3 size_t: int = mask_shape[0] size_c: int = mask_shape[1] size_z: int = mask_shape[2] ignored_dimensions = ignored_dimensions or set() labels = np.zeros(mask_shape, np.int64) for d in "TCZYX": if d in ignored_dimensions: assert ( labels.shape[DIMENSION_ORDER[d]] == 1 ), f"Ignored dimension {d} should be size 1" assert ( labels.shape == mask_shape ), f"Invalid label shape: {labels.shape}, expected {mask_shape}" fillColors: Dict[int, str] = {} for count, shapes in enumerate(masks): # All shapes same color for each ROI print(count) for mask in shapes: # Unused metadata: the{ZTC}, x, y, width, height, textValue if mask.fillColor: fillColors[count + 1] = unwrap(mask.fillColor) binim_yx, (t, c, z, y, x, h, w) = self._mask_to_binim_yx(mask) for i_t in self._get_indices(ignored_dimensions, "T", t, size_t): for i_c in self._get_indices(ignored_dimensions, "C", c, size_c): for i_z in self._get_indices( ignored_dimensions, "Z", z, size_z ): if check_overlaps and np.any( np.logical_and( labels[ i_t, i_c, i_z, y : (y + h), x : (x + w) ].astype(np.bool), binim_yx, ) ): raise Exception( f"Mask {count} overlaps with existing labels" ) # ADD to the array, so zeros in our binarray don't # wipe out previous masks labels[i_t, i_c, i_z, y : (y + h), x : (x + w)] += ( binim_yx * (count + 1) # Prevent zeroing ) return labels, fillColors