def test5756Wrapped(self): """ Accessing deleted image in share seems to have changed. This tests what happens using BlitzGateway wrappers. """ new_group = self.new_group() new_client, new_user = self.new_client_and_user(new_group) share = new_client.sf.getShareService() query = new_client.sf.getQueryService() update = new_client.sf.getUpdateService() image = self.make_image(client=new_client) objects = [image] share_id = share.createShare("", None, objects, [], [], True) new_context = omero.model.ShareI(share_id, False) old_context = new_client.sf.setSecurityContext(new_context) image = query.get("Image", image.id.val) from omero.gateway import ImageWrapper, BlitzGateway conn = BlitzGateway(client_obj=new_client) wrapper = ImageWrapper(conn=conn, obj=image) new_client.sf.setSecurityContext(old_context) update.deleteObject(image) new_client.sf.setSecurityContext(new_context) with pytest.raises(IndexError): wrapper.__loadedHotSwap__()
def test5756Wrapped(self): """ Accessing deleted image in share seems to have changed. This tests what happens using BlitzGateway wrappers. """ share = self.client.sf.getShareService() query = self.client.sf.getQueryService() update = self.client.sf.getUpdateService() image = self.new_image() image = update.saveAndReturnObject(image) objects = [image] self.share_id = share.createShare("", None, objects, [], [], True) new_context = omero.model.ShareI(self.share_id, False) old_context = self.client.sf.setSecurityContext(new_context) image = query.get("Image", image.id.val) from omero.gateway import ImageWrapper, BlitzGateway conn = BlitzGateway(client_obj = self.client) wrapper = ImageWrapper(conn = conn, obj = image) self.client.sf.setSecurityContext(old_context) update.deleteObject(image) self.client.sf.setSecurityContext(new_context) with pytest.raises(IndexError): wrapper.__loadedHotSwap__()
def load_omero_channel(image: ImageWrapper, channel: ChannelWrapper, c_index: int) -> LayerData: data = get_data_lazy(image, c_index=c_index) color = channel.getColor().getRGB() color = [r / 256 for r in color] cmap = Colormap([[0, 0, 0], color]) scale = None # FIXME: still getting size mismatch sometimes is there a getNDim()? if image.getSizeZ() > 1: size_x = image.getPixelSizeX() size_z = image.getPixelSizeZ() if size_x is not None and size_z is not None: if image.getSizeC() > 1: scale = [1, size_z / size_x, 1, 1] else: scale = [size_z / size_x, 1, 1] meta = { "blending": "additive", "colormap": ("from_omero", cmap), "scale": scale, "name": channel.getLabel(), "visible": channel.isActive(), "contrast_limits": [channel.getWindowStart(), channel.getWindowEnd()], } # contrast limits range ... not accessible from plugin interface # win_min = channel.getWindowMin() # win_max = channel.getWindowMax() return (data, meta)
def get_data_lazy(image: ImageWrapper, c_index: int = 0) -> da.Array: """Get n-dimensional dask array, with delayed reading from OMERO image.""" size_z = image.getSizeZ() size_t = image.getSizeT() size_x = image.getSizeX() size_y = image.getSizeY() pixels = image.getPrimaryPixels() @delayed @timer def get_plane(plane_name): z, c, t = [int(n) for n in plane_name.split(",")] p = pixels.getPlane(z, c, t) return p dtype = PIXEL_TYPES.get(pixels.getPixelsType().value, None) plane_names = [ f"{z},{c_index},{t}" for t in range(size_t) for z in range(size_z) ] lazy_arrays = [get_plane(pn) for pn in plane_names] dask_arrays = [ da.from_delayed(delayed_reader, shape=(size_y, size_x), dtype=dtype) for delayed_reader in lazy_arrays ] # Stack into one large dask.array if size_z == 1 or size_t == 1: return da.stack(dask_arrays, axis=0) z_stacks = [] for t in range(size_t): z_stacks.append( da.stack(dask_arrays[t * size_z:(t + 1) * size_z], axis=0)) stack = da.stack(z_stacks, axis=0) return stack
def listRois(conn, eid=None, limit=10): """ List the most recently created ROIs, optionally filtering by owner. Returns a list of RecentObjects wrapping the linked Images. """ queryService = conn.getQueryService() params = omero.sys.ParametersI() f = omero.sys.Filter() f.limit = rint(limit) params.theFilter = f if eid is not None: params.map["eid"] = rlong(long(eid)) eidSelect = "where roi.details.owner.id=:eid " else: eidSelect = "" query = "select roi from Roi as roi " \ "join fetch roi.image as image " \ "join fetch roi.details.owner " \ "join fetch roi.details.creationEvent as event %s" \ "order by event desc" % eidSelect rois = queryService.findAllByQuery(query, params) return [RecentEvent(ImageWrapper(conn, r.image), r) for r in rois]
def test5756Wrapped(self): """ Accessing deleted image in share seems to have changed. This tests what happens using BlitzGateway wrappers. """ share = self.client.sf.getShareService() query = self.client.sf.getQueryService() update = self.client.sf.getUpdateService() image = self.new_image() image = update.saveAndReturnObject(image) objects = [image] self.share_id = share.createShare("", None, objects, [], [], True) new_context = omero.model.ShareI(self.share_id, False) old_context = self.client.sf.setSecurityContext(new_context) image = query.get("Image", image.id.val) from omero.gateway import ImageWrapper, BlitzGateway conn = BlitzGateway(client_obj=self.client) wrapper = ImageWrapper(conn=conn, obj=image) self.client.sf.setSecurityContext(old_context) update.deleteObject(image) self.client.sf.setSecurityContext(new_context) self.assertRaises(IndexError, wrapper.__loadedHotSwap__)
def get_omero_metadata(image: ImageWrapper) -> Dict: """Get metadata from OMERO as a Dict to pass to napari.""" channels = image.getChannels() colors = [] for ch in channels: # use current rendering settings from OMERO color = ch.getColor().getRGB() color = [r / 256 for r in color] colors.append(Colormap([[0, 0, 0], color])) contrast_limits = [[ch.getWindowStart(), ch.getWindowEnd()] for ch in channels] visibles = [ch.isActive() for ch in channels] names = [ch.getLabel() for ch in channels] scale = None # Setting z-scale causes issues with Z-slider. # See https://github.com/tlambert03/napari-omero/pull/15 # if image.getSizeZ() > 1: # size_x = image.getPixelSizeX() # size_z = image.getPixelSizeZ() # if size_x is not None and size_z is not None: # scale = [1, size_z / size_x, 1, 1] return { 'channel_axis': 1, 'colormap': colors, 'contrast_limits': contrast_limits, 'name': names, 'visible': visibles, 'scale': scale, }
def wrapped_image(): image = ImageI() image.id = rlong(1L) image.description = rstring('description') image.name = rstring('name') image.acquisitionDate = rtime(1000) # In milliseconds image.details.owner = ExperimenterI(1L, False) creation_event = EventI() creation_event.time = rtime(2000) # In milliseconds image.details.creationEvent = creation_event return ImageWrapper(conn=MockConnection(), obj=image)
def get_data_lazy(image: ImageWrapper) -> da.Array: """Get 5D dask array, with delayed reading from OMERO image.""" nt, nc, nz, ny, nx = [getattr(image, f'getSize{x}')() for x in 'TCZYX'] pixels = image.getPrimaryPixels() dtype = PIXEL_TYPES.get(pixels.getPixelsType().value, None) get_plane = delayed(timer(lambda idx: pixels.getPlane(*idx))) def get_lazy_plane(zct): return da.from_delayed(get_plane(zct), shape=(ny, nx), dtype=dtype) # 5D stack: TCZXY t_stacks = [] for t in range(nt): c_stacks = [] for c in range(nc): z_stack = [] for z in range(nz): z_stack.append(get_lazy_plane((z, c, t))) c_stacks.append(da.stack(z_stack)) t_stacks.append(da.stack(c_stacks)) return da.stack(t_stacks)
def testGetROICount(self): """ Test no ROI couting method """ # Create group with two member and one image group = self.new_group(perms="rwrw--") owner = self.new_client(group=group) # Owner of share member = self.new_client(group=group) # Member of group img = self.createTestImage(session=owner.sf) from omero.gateway import ImageWrapper, BlitzGateway conn = BlitzGateway(client_obj=owner) # Test no ROI count wrapper = ImageWrapper(conn, img) assert wrapper.getROICount() == 0 # Test ROI shape roi1 = omero.model.RoiI() roi1.addShape(omero.model.RectI()) roi1.addShape(omero.model.RectI()) roi1.setImage(img) roi1 = owner.sf.getUpdateService().saveAndReturnObject(roi1) roi2 = omero.model.RoiI() roi2.addShape(omero.model.RectI()) roi2.addShape(omero.model.EllipseI()) roi2.setImage(img) roi2 = owner.sf.getUpdateService().saveAndReturnObject(roi2) wrapper = ImageWrapper(conn, img) assert wrapper.getROICount() == 2 assert wrapper.getROICount("Rect") == 2 assert wrapper.getROICount("Ellipse") == 1 assert wrapper.getROICount("Line") == 0 assert wrapper.getROICount(["Rect", "Ellipse"]) == 2 # Test ROI permissions roi3 = omero.model.RoiI() roi3.addShape(omero.model.EllipseI()) roi3.setImage(img) roi3 = member.sf.getUpdateService().saveAndReturnObject(roi3) assert wrapper.getROICount() == 3 assert wrapper.getROICount(filterByCurrentUser=True) == 2 assert wrapper.getROICount("Ellipse") == 2 assert wrapper.getROICount("Ellipse", None) == 2 assert wrapper.getROICount("Ellipse", 1) == 1 assert wrapper.getROICount("Ellipse", True) == 1 assert wrapper.getROICount("Rect") == 2 assert wrapper.getROICount("Rect", None) == 2 assert wrapper.getROICount("Rect", 1) == 2 assert wrapper.getROICount("Rect", True) == 2 # Member gateway conn = BlitzGateway(client_obj=member) wrapper = ImageWrapper(conn, img) assert wrapper.getROICount() == 3 assert wrapper.getROICount(filterByCurrentUser=True) == 1 assert wrapper.getROICount("Ellipse") == 2 assert wrapper.getROICount("Ellipse", None) == 2 assert wrapper.getROICount("Ellipse", 1) == 1 assert wrapper.getROICount("Ellipse", True) == 1 assert wrapper.getROICount("Rect") == 2 assert wrapper.getROICount("Rect", None) == 2 assert wrapper.getROICount("Rect", 1) == 0 assert wrapper.getROICount("Rect", True) == 0
def testGetROICount(self): """ Test no ROI couting method """ # Create group with two member and one image group = self.new_group(perms="rwrw--") owner = self.new_client(group=group) # Owner of share member = self.new_client(group=group) # Member of group img = self.createTestImage(session=owner.sf) from omero.gateway import ImageWrapper, BlitzGateway conn = BlitzGateway(client_obj=owner) # Test no ROI count wrapper = ImageWrapper(conn, img) assert wrapper.getROICount() == 0 # Test ROI shape roi1 = omero.model.RoiI() roi1.addShape(omero.model.RectangleI()) roi1.addShape(omero.model.RectangleI()) roi1.setImage(img) roi1 = owner.sf.getUpdateService().saveAndReturnObject(roi1) roi2 = omero.model.RoiI() roi2.addShape(omero.model.RectangleI()) roi2.addShape(omero.model.EllipseI()) roi2.setImage(img) roi2 = owner.sf.getUpdateService().saveAndReturnObject(roi2) wrapper = ImageWrapper(conn, img) assert wrapper.getROICount() == 2 assert wrapper.getROICount("Rectangle") == 2 assert wrapper.getROICount("Ellipse") == 1 assert wrapper.getROICount("Line") == 0 assert wrapper.getROICount(["Rectangle", "Ellipse"]) == 2 # Test ROI permissions roi3 = omero.model.RoiI() roi3.addShape(omero.model.EllipseI()) roi3.setImage(img) roi3 = member.sf.getUpdateService().saveAndReturnObject(roi3) assert wrapper.getROICount() == 3 assert wrapper.getROICount(filterByCurrentUser=True) == 2 assert wrapper.getROICount("Ellipse") == 2 assert wrapper.getROICount("Ellipse", None) == 2 assert wrapper.getROICount("Ellipse", 1) == 1 assert wrapper.getROICount("Ellipse", True) == 1 assert wrapper.getROICount("Rectangle") == 2 assert wrapper.getROICount("Rectangle", None) == 2 assert wrapper.getROICount("Rectangle", 1) == 2 assert wrapper.getROICount("Rectangle", True) == 2 # Member gateway conn = BlitzGateway(client_obj=member) wrapper = ImageWrapper(conn, img) assert wrapper.getROICount() == 3 assert wrapper.getROICount(filterByCurrentUser=True) == 1 assert wrapper.getROICount("Ellipse") == 2 assert wrapper.getROICount("Ellipse", None) == 2 assert wrapper.getROICount("Ellipse", 1) == 1 assert wrapper.getROICount("Ellipse", True) == 1 assert wrapper.getROICount("Rectangle") == 2 assert wrapper.getROICount("Rectangle", None) == 2 assert wrapper.getROICount("Rectangle", 1) == 0 assert wrapper.getROICount("Rectangle", True) == 0
def load_image_wrapper(image: ImageWrapper) -> List[LayerData]: return [ load_omero_channel(image, channel, c) for c, channel in enumerate(image.getChannels()) ]