def create_mask( binim, rgba=None, z=None, c=None, t=None, text=None, raise_on_no_mask=False): """ Create a mask shape from a binary image (background=0) :param numpy.array binim: Binary 2D array, must contain values [0, 1] only :param rgba int-4-tuple: Optional (red, green, blue, alpha) colour :param z: Optional Z-index for the mask :param c: Optional C-index for the mask :param t: Optional T-index for the mask :param text: Optional text for the mask :param raise_on_no_mask: If True (default) throw an exception if no mask found, otherwise return an empty Mask :return: An OMERO mask :raises NoMaskFound: If no labels were found :raises InvalidBinaryImage: If the maximum labels is greater than 1 """ # Find bounding box to minimise size of mask xmask = binim.sum(0).nonzero()[0] ymask = binim.sum(1).nonzero()[0] if any(xmask) and any(ymask): x0 = min(xmask) w = max(xmask) - x0 + 1 y0 = min(ymask) h = max(ymask) - y0 + 1 submask = binim[y0:(y0 + h), x0:(x0 + w)] if (not np.array_equal(np.unique(submask), [0, 1]) and not np.array_equal(np.unique(submask), [1])): raise Exception("Invalid binary image") else: if raise_on_no_mask: raise Exception("No mask found") x0 = 0 w = 0 y0 = 0 h = 0 submask = [] mask = MaskI() mask.setBytes(np.packbits(np.asarray(submask, dtype=int))) mask.setWidth(rdouble(w)) mask.setHeight(rdouble(h)) mask.setX(rdouble(x0)) mask.setY(rdouble(y0)) if rgba is not None: ch = ColorHolder.fromRGBA(*rgba) mask.setFillColor(rint(ch.getInt())) if z is not None: mask.setTheZ(rint(z)) if c is not None: mask.setTheC(rint(c)) if t is not None: mask.setTheT(rint(t)) if text is not None: mask.setTextValue(rstring(text)) return mask
def testBasicUsage(self): gateway = self.client.sf.createGateway() gateway.getProjects([0], False) try: # query below does not find image if created with # self.createTestImage() even though it uses 'identical' code to # createTestImage(self.client.sf), which uses script_utils # iid = self.createTestImage().getId().getValue() iid = createTestImage(self.client.sf) print iid, type(iid) query = self.client.sf.getQueryService() params = omero.sys.Parameters() params.map = {} params.map["oid"] = rlong(iid) params.theFilter = omero.sys.Filter() params.theFilter.offset = rint(0) params.theFilter.limit = rint(1) pixel = query.findByQuery( "select p from Pixels as p left outer join\ fetch p.image as i where i.id=:oid", params) print pixel imgid = pixel.image.id.val print imgid gateway.getRenderedImage(pixel.id.val, 0, 0) except omero.ValidationException: print "testBasicUsage - createTestImage has failed.\ This fixture method needs to be fixed." gateway.close()
def missing_pyramid(self, client=None): """ Creates and returns a pixels whose shape changes from 1,1,4000,4000,1 to 4000,4000,1,1,1 making it a pyramid candidate but without the pyramid which is created on initial import in 4.3+. This simulates a big image that was imported in 4.2. """ if client is None: client = self.client pix = self.pix(x=1, y=1, z=4000, t=4000, c=1, client=client) rps = client.sf.createRawPixelsStore() try: rps.setPixelsId(pix.id.val, True) for t in range(4000): rps.setTimepoint([5]*4000, t) # Assuming int8 pix = rps.save() finally: rps.close() pix.sizeX = rint(4000) pix.sizeY = rint(4000) pix.sizeZ = rint(1) pix.sizeT = rint(1) update = client.sf.getUpdateService() return update.saveAndReturnObject(pix)
def add_images_to_plate(conn, images, plate_id, column, row, remove_from=None): """ Add the Images to a Plate, creating a new well at the specified column and row NB - This will fail if there is already a well at that point """ update_service = conn.getUpdateService() well = omero.model.WellI() well.plate = omero.model.PlateI(plate_id, False) well.column = rint(column) well.row = rint(row) try: for image in images: ws = omero.model.WellSampleI() ws.image = omero.model.ImageI(image.id, False) ws.well = well well.addWellSample(ws) update_service.saveObject(well) except Exception: return False # remove from Datast for image in images: if remove_from is not None: links = list(image.getParentLinks(remove_from.id)) link_ids = [l.id for l in links] conn.deleteObjects('DatasetImageLink', link_ids) return True
def well(): """Basic Well for testing Color. Full test of Well below.""" well = WellI() well.id = rlong(1) well.column = rint(0) well.row = rint(0) return well
def addImagesToWell(conn, images, plateId, row, col, removeFrom=None): """ Add Images to a New well, creating a new wellSample for each """ updateService = conn.getUpdateService() well = omero.model.WellI() well.plate = omero.model.PlateI(plateId, False) well.column = rint(col-1) well.row = rint(row-1) try: for image in images: ws = omero.model.WellSampleI() ws.image = omero.model.ImageI(image.id, False) ws.well = well # Optional, add position X and Y ws.posX = omero.model.LengthI(random() * 100, UnitsLength.REFERENCEFRAME) ws.posY = omero.model.LengthI(random() * 100, UnitsLength.REFERENCEFRAME) well.addWellSample(ws) updateService.saveObject(well) except: print "Failed to add image to well sample" return False # remove from Datast if removeFrom is not None: for image in images: for l in image.getParentLinks(removeFrom.id): print " Removing image from Dataset: %s" \ % removeFrom.id conn.deleteObjectDirect(l._obj) return True
def pix(self, x=10, y=10, z=10, c=3, t=50, client=None): """ Creates an int8 pixel of the given size in the database. No data is written. """ image = self.new_image() pixels = PixelsI() pixels.sizeX = rint(x) pixels.sizeY = rint(y) pixels.sizeZ = rint(z) pixels.sizeC = rint(c) pixels.sizeT = rint(t) pixels.sha1 = rstring("") pixels.pixelsType = PixelsTypeI() pixels.pixelsType.value = rstring("int8") pixels.dimensionOrder = DimensionOrderI() pixels.dimensionOrder.value = rstring("XYZCT") image.addPixels(pixels) if client is None: client = self.client update = client.sf.getUpdateService() image = update.saveAndReturnObject(image) pixels = image.getPrimaryPixels() return pixels
def screen_plate_run_well(request, itest, update_service): """ Returns a new OMERO Screen, linked Plate, linked Well, linked WellSample, linked Image populate by an L{test.integration.library.ITest} instance and linked PlateAcquisition with all required fields set. """ screen = ScreenI() screen.name = rstring(itest.uuid()) plate = PlateI() plate.name = rstring(itest.uuid()) # Well A10 (will have two WellSamples) well_a = WellI() well_a.row = rint(0) well_a.column = rint(9) # Well A11 (will not have a WellSample) well_b = WellI() well_b.row = rint(0) well_b.column = rint(10) ws_a = WellSampleI() image_a = itest.new_image(name=itest.uuid()) ws_a.image = image_a ws_b = WellSampleI() image_b = itest.new_image(name=itest.uuid()) ws_b.image = image_b plate_acquisition = PlateAcquisitionI() plate_acquisition.plate = plate ws_a.plateAcquisition = plate_acquisition ws_b.plateAcquisition = plate_acquisition well_a.addWellSample(ws_a) well_a.addWellSample(ws_b) plate.addWell(well_a) plate.addWell(well_b) screen.linkPlate(plate) return update_service.saveAndReturnObject(screen)
def populate_shape(o, set_unit_attributes=True): o.fillColor = rint(0xffffffff) o.fillRule = rstring('solid') o.fontFamily = rstring('cursive') if set_unit_attributes: o.fontSize = LengthI(12, UnitsLength.POINT) o.fontStyle = rstring('italic') o.locked = rbool(False) o.strokeColor = rint(0xffff0000) o.strokeDashArray = rstring('inherit') if SCHEMA_VERSION == '2015-01': o.visibility = rbool(True) o.strokeLineCap = rstring('round') if set_unit_attributes: o.strokeWidth = LengthI(4, UnitsLength.PIXEL) o.textValue = rstring('the_text') o.theC = rint(1) o.theT = rint(2) o.theZ = rint(3) t = identity_transform() if SCHEMA_VERSION == '2015-01': o.transform = t.svg_transform else: o.transform = t return o
def testGetThumbnailSetAfterApplySettings(self): """ Tests that you can getThumbnailSet after applying settings. See https://github.com/openmicroscopy/openmicroscopy/pull/5207 """ group = self.new_group(perms="rwra--") client = self.new_client(group=group) # Create 2 images in group image1 = self.create_test_image(session=client.sf) image2 = self.create_test_image(session=client.sf) pixelsId1 = image1.getPrimaryPixels().id.val pixelsId2 = image2.getPrimaryPixels().id.val tb = client.sf.createThumbnailStore() ss = client.sf.getRenderingSettingsService() # This works to start with: tb.getThumbnailByLongestSideSet(rint(96), [pixelsId1, pixelsId2], {'omero.group': '-1'}) # After applying settings... ss.applySettingsToSet(pixelsId1, "Image", [image2.id.val]) # Should still be able to get thumbnail set tb.getThumbnailByLongestSideSet(rint(96), [pixelsId1, pixelsId2], {'omero.group': '-1'}) # Also should work without any group context tb.getThumbnailByLongestSideSet(rint(96), [pixelsId1, pixelsId2])
def setUp(self): AbstractPlateAnalysisCtx.DEFAULT_ORIGINAL_FILE_PROVIDER = \ FromFileOriginalFileProvider original_files = list() # Create our container images and an original file image map images = list() n_images = 0 for row in range(16): for column in range(24): well = WellI(n_images, True) well.column = rint(column) well.row = rint(row) well_sample = WellSampleI(n_images, True) well_sample.well = well image = ImageI(n_images, True) image.addWellSample(well_sample) images.append(image) original_file_image_map = dict() # Our required original file format format = rstring('Companion/InCell') # Create original file representing the result file o = OriginalFileI(1L, True) o.name = rstring(self.RESULT_FILE) o.path = rstring(os.path.join(self.ROOT, self.RESULT_FILE)) o.mimetype = format original_files.append(o) #[1L] = o original_file_image_map[1L] = image sf = TestingServiceFactory() self.analysis_ctx = InCellPlateAnalysisCtx( images, original_files, original_file_image_map, 1L, sf)
def autocompleteQuery(request): """ Returns json data for autocomplete. Search terms must be provided in the request "GET". E.g. returns a list of ("1003": "Title") results for entries that start with numbers specified. """ conn = getConnection(request) qs = conn.getQueryService() # limit the number of results we return p = omero.sys.Parameters() p.map = {} f = omero.sys.Filter() f.limit = rint(15) f.offset = rint(0) p.theFilter = f projects = [] entryText = request.REQUEST.get("entry") if entryText: query = "select p from Project as p where p.name like '%s%s' order by p.name" % (entryText, "%") # like '123%' projects = qs.findAllByQuery(query, p) results = [] for p in projects: entryId = p.getName().getValue() desc = p.getDescription().getValue() title, sample = desc.split("\n") results.append((entryId, title)) return HttpResponse(simplejson.dumps(results), mimetype="application/javascript")
def addImageToPlate(conn, image, plateId, column, row, removeFrom=None): """ Add the Image to a Plate, creating a new well at the specified column and row NB - This will fail if there is already a well at that point """ updateService = conn.getUpdateService() well = omero.model.WellI() well.plate = omero.model.PlateI(plateId, False) well.column = rint(column) well.row = rint(row) well = updateService.saveAndReturnObject(well) try: ws = omero.model.WellSampleI() ws.image = omero.model.ImageI(image.id, False) ws.well = well well.addWellSample(ws) updateService.saveObject(ws) except: print "Failed to add image to well sample" return False # remove from Datast if removeFrom is not None: links = list(image.getParentLinks(removeFrom.id)) print " Removing image from Dataset: %s" \ % removeFrom.id for l in links: conn.deleteObjectDirect(l._obj) return True
def testConversionMethod(self): assert None == rtype(None) assert rlong(1) == rtype(rlong(1)) # Returns self assert rbool(True) == rtype(True) # Unsupported # assert rdouble(0) == rtype(Double.valueOf(0)) assert rfloat(0) == rtype(float(0)) if sys.version_info < (3, 0, 0): assert rlong(0) == rtype(long(0)) else: assert rint(0) == rtype(long(0)) assert rint(0) == rtype(int(0)) assert rstring("string") == rtype("string") # Unsupported # assert rtime(time) == rtype(new Timestamp(time)) rtype(omero.model.ImageI()) rtype(omero.grid.JobParams()) rtype(set([rlong(1)])) rtype(list([rlong(2)])) rtype({}) # Unsupported # rtype(array) try: rtype(()) assert False, "Shouldn't be able to handle this yet" except omero.ClientError: pass
def rois_by_plane(request, image_id, the_z, the_t, z_end=None, t_end=None, conn=None, **kwargs): """ Get ROIs with all Shapes where any Shapes are on the given Z and T plane. Includes Shapes where Z or T are null. If z_end or t_end are not None, we filter by any shape within the range (inclusive of z/t_end) """ query_service = conn.getQueryService() params = omero.sys.ParametersI() params.addId(image_id) filter = omero.sys.Filter() filter.offset = rint(request.GET.get("offset", 0)) limit = min(MAX_LIMIT, long(request.GET.get("limit", MAX_LIMIT))) filter.limit = rint(limit) params.theFilter = filter where_z = "shapes.theZ = %s or shapes.theZ is null" % the_z where_t = "shapes.theT = %s or shapes.theT is null" % the_t if z_end is not None: where_z = """(shapes.theZ >= %s and shapes.theZ <= %s) or shapes.theZ is null""" % (the_z, z_end) if t_end is not None: where_t = """(shapes.theT >= %s and shapes.theT <= %s) or shapes.theT is null""" % (the_t, t_end) filter_query = """ select distinct(roi) from Roi roi join roi.shapes as shapes where (%s) and (%s) and roi.image.id = :id """ % (where_z, where_t) # We want to load ALL shapes for the ROIs (omero-marshal fails if # any shapes are None) but we want to filter by Shape so we use an inner # query to get the ROI IDs filtered by Shape. query = """ select roi from Roi roi join fetch roi.details.owner join fetch roi.details.creationEvent left outer join fetch roi.shapes where roi.id in (%s) order by roi.id """ % (filter_query) rois = query_service.findAllByQuery(query, params, conn.SERVICE_OPTS) marshalled = [] for r in rois: encoder = omero_marshal.get_encoder(r.__class__) if encoder is not None: marshalled.append(encoder.encode(r)) # Modify query to only select count() and NOT paginate query = filter_query.replace("distinct(roi)", "count(distinct roi)") params = omero.sys.ParametersI() params.addId(image_id) result = query_service.projection(query, params, conn.SERVICE_OPTS) meta = {"totalCount": result[0][0].val} return JsonResponse({'data': marshalled, 'meta': meta})
def addImageToPlate(conn, image, plateId, column, row): """ Add the Image to a Plate, creating a new well at the specified column and row NB - This will fail if there is already a well at that point """ updateService = conn.getUpdateService() well = omero.model.WellI() well.plate = omero.model.PlateI(plateId, False) well.column = rint(column) well.row = rint(row) well = updateService.saveAndReturnObject(well) try: ws = omero.model.WellSampleI() ws.image = omero.model.ImageI(image.id, False) ws.well = well well.addWellSample(ws) updateService.saveObject(ws) except: print "Failed to add image to well sample" return False return True
def screen_plates(self, user1): """Return Screen with Plates and an orphaned Plate.""" # Create and name all the objects screen = ScreenI() screen.name = rstring('screen') for i in range(5): plate1 = PlateI() plate1.name = rstring('Plate%s' % i) screen.linkPlate(plate1) # Create single orphaned Plate plate = PlateI() plate.name = rstring('plate') screen = get_update_service(user1).saveAndReturnObject(screen) plate = get_update_service(user1).saveAndReturnObject(plate) # Add well to first plate plates = screen.linkedPlateList() plates.sort(cmp_name_insensitive) plate_id = plates[0].id.val well = WellI() well.column = rint(0) well.row = rint(0) well.plate = PlateI(plate_id, False) image = self.create_test_image(size_x=5, size_y=5, session=user1[0].getSession()) ws = WellSampleI() ws.image = ImageI(image.id, False) ws.well = well well.addWellSample(ws) well = get_update_service(user1).saveAndReturnObject(well) return screen, plate
def test1179(self): createTestImage(self.root.sf) rdefs = self.root.sf.getQueryService().findAll("RenderingDef", None) if len(rdefs) == 0: raise Exception("Must have at least one rendering def") id = rdefs[0].pixels.id.val # make sure thumbnail is viewed by owner tb = self.root.sf.createThumbnailStore() try: tb.setPixelsId(id) s = tb.getThumbnail(rint(16), rint(16)) assert len(s) > 0 finally: tb.close() share = self.root.sf.getShareService() sid = share.createShare("", None, [], [], [], True) share.activate(sid) # Share is active: we are in the security context of the share tb = self.root.sf.createThumbnailStore() try: tb.setPixelsId(id) s = tb.getThumbnail(rint(16), rint(16)) assert len(s) > 0 except omero.SecurityViolation: assert False, "Pixels was not in share" finally: tb.close() share.deactivate()
def create_shape_point(x_pos, y_pos, z_pos=None, c_pos=None, t_pos=None, name=None, stroke_color=(255, 255, 255, 255), fill_color=(10, 10, 10, 20), stroke_width=1): point = model.PointI() point.x = rtypes.rdouble(x_pos) point.y = rtypes.rdouble(y_pos) if z_pos is not None: point.theZ = rtypes.rint(z_pos) if c_pos is not None: point.theC = rtypes.rint(c_pos) if t_pos is not None: point.theT = rtypes.rint(t_pos) _set_shape_properties(shape=point, name=name, stroke_color=stroke_color, stroke_width=stroke_width, fill_color=fill_color) return point
def create_image_copy(connection, source_image_id, channel_list=None, image_name=None, image_description=None, size_x=None, size_y=None, size_z=None, size_t=None): """Creates a copy of an existing OMERO image using all the metadata but not the pixels values. The parameter values will override the ones of the original image""" pixels_service = connection.getPixelsService() if channel_list is None: source_image = connection.getObject('Image', source_image_id) channel_list = list(range(source_image.getSizeC())) image_id = pixels_service.copyAndResizeImage(imageId=source_image_id, sizeX=rtypes.rint(size_x), sizeY=rtypes.rint(size_y), sizeZ=rtypes.rint(size_z), sizeT=rtypes.rint(size_t), channelList=channel_list, methodology=image_name, copyStats=False) new_image = connection.getObject("Image", image_id) if image_description is not None: # Description is not provided as an override option in the OMERO interface new_image.setDescription(image_description) new_image.save() return new_image
def test_remove_pyramids_check_thumbnails(self, tmpdir, capsys): """Test check that the thumbnail is correctly created""" name = "big&sizeX=3500&sizeY=3500&little=false.fake" img_id = self.import_pyramid(tmpdir, name) query_service = self.client.sf.getQueryService() pix = query_service.findByQuery( "select p from Pixels p where p.image.id = :id", ParametersI().addId(img_id)) tb = self.client.sf.createThumbnailStore() id = pix.id.val thumb_hash = None try: thumbs = tb.getThumbnailByLongestSideSet(rint(64), [id], {'omero.group': '-1'}) assert len(thumbs) == 1 thumb_hash = self.calculate_sha1(thumbs[id]) # remove the pyramid and the thumbnail self.args += ["--endian=big"] self.cli.invoke(self.args, strict=True) out, err = capsys.readouterr() thumbs = tb.getThumbnailByLongestSideSet(rint(64), [id], {'omero.group': '-1'}) assert len(thumbs) == 1 # The clock should be returned during the pyramid generation digest = self.calculate_sha1(thumbs[id]) assert digest != thumb_hash # The pyramid generation has now been triggered. self.wait_for_pyramid(id) thumbs = tb.getThumbnailByLongestSideSet(rint(64), [id], {'omero.group': '-1'}) digest = self.calculate_sha1(thumbs[id]) # The thumbnail should now be back assert digest == thumb_hash finally: tb.close()
def import_pyramid_pre_fs(self, tmpdir): name = "test&sizeX=4000&sizeY=4000.fake" fakefile = tmpdir.join(name) fakefile.write('') pixels = self.import_image(filename=str(fakefile), skip="checksum")[0] id = long(float(pixels)) # wait for the pyramid to be generated self.wait_for_pyramid(id) query_service = self.client.sf.getQueryService() pixels_service = self.client.sf.getPixelsService() orig_pix = query_service.findByQuery( "select p from Pixels p where p.id = :id", ParametersI().addId(id)) orig_fs = query_service.findByQuery( "select f from Image i join i.fileset f where i.id = :id", ParametersI().addId(orig_pix.image.id.val)) try: new_img = pixels_service.copyAndResizeImage( orig_pix.image.id.val, rint(4000), rint(4000), rint(1), rint(1), [0], None, True).val pix_id = unwrap( query_service.projection( "select p.id from Image i join i.pixels p where i.id = :id", ParametersI().addId(new_img)))[0][0] # This won't work but it but we then have a pyramid without fileset self.copyPixels(orig_pix, PixelsI(pix_id, False)) except omero.InternalException: print "Cannot copy pixels for image %s" % orig_pix.image.id.val finally: self.delete([orig_fs]) return pix_id
def missing_pyramid(self, client=None): """ Creates and returns a pixels whose shape changes from 1,1,4000,4000,1 to 4000,4000,1,1,1 making it a pyramid candidate but without the pyramid which is created on initial import in 4.3+. This simulates a big image that was imported in 4.2. """ if client is None: client = self.client pix = self.pix(x=1, y=1, z=4000, t=4000, c=1, client=client) rps = client.sf.createRawPixelsStore() try: rps.setPixelsId(pix.id.val, True) for t in range(4000): rps.setTimepoint([5] * 4000, t) # Assuming int8 pix = rps.save() finally: rps.close() pix.sizeX = rint(4000) pix.sizeY = rint(4000) pix.sizeZ = rint(1) pix.sizeT = rint(1) update = client.sf.getUpdateService() return update.saveAndReturnObject(pix)
def setUp(self): AbstractPlateAnalysisCtx.DEFAULT_ORIGINAL_FILE_PROVIDER = \ FromFileOriginalFileProvider original_files = list() # Create our container images and an original file image map images = list() n_images = 0 for row in range(16): for column in range(24): well = WellI(n_images, True) well.column = rint(column) well.row = rint(row) well_sample = WellSampleI(n_images, True) well_sample.well = well image = ImageI(n_images, True) image.addWellSample(well_sample) images.append(image) original_file_image_map = dict() # Our required original file format format = rstring('Companion/InCell') # Create original file representing the result file o = OriginalFileI(1, True) o.name = rstring(self.RESULT_FILE) o.path = rstring(os.path.join(self.ROOT, self.RESULT_FILE)) o.mimetype = format original_files.append(o) # [1] = o original_file_image_map[1] = image sf = TestingServiceFactory() self.analysis_ctx = InCellPlateAnalysisCtx(images, original_files, original_file_image_map, 1, sf)
def autocompleteQuery(request, conn=None, **kwargs): """ Returns json data for autocomplete. Search terms must be provided in the request "GET". E.g. returns a list of ("1003": "Title") results for entries that start with numbers specified. """ qs = conn.getQueryService() # limit the number of results we return p = omero.sys.Parameters() p.map = {} f = omero.sys.Filter() f.limit = rint(15) f.offset = rint(0) p.theFilter = f projects = [] entryText = request.REQUEST.get('entry') if entryText: query = "select p from Project as p where p.name like '%s%s' order by p.name" % ( entryText, "%") # like '123%' projects = qs.findAllByQuery(query, p) results = [] for p in projects: entryId = p.getName().getValue() desc = p.getDescription().getValue() title, sample = desc.split("\n") results.append((entryId, title)) return HttpResponse(simplejson.dumps(results), mimetype='application/javascript')
def test_well_color_decoder(self, color, well): """Test different combinations of r, g, b, a.""" well.red = rint(color[0]) well.green = rint(color[1]) well.blue = rint(color[2]) well.alpha = rint(color[3]) encoder = get_encoder(well.__class__) decoder = get_decoder(encoder.TYPE) v = encoder.encode(well) # Check we get the expected Integer assert v.get('Color') == color[4] v = decoder.decode(v) # If red is None, all are None if color[0] is None: assert v.red is None assert v.green is None assert v.blue is None assert v.alpha is None elif color[3] is None: # alpha None -> 255 by default assert v.red.val == color[0] assert v.green.val == color[1] assert v.blue.val == color[2] assert v.alpha.val == 255 else: # Otherwise, all preserved assert v.red.val == color[0] assert v.green.val == color[1] assert v.blue.val == color[2] assert v.alpha.val == color[3]
def screen_plate_run_well(self): """ Returns a new OMERO Screen, linked Plate, linked Well, linked WellSample, linked Image populate by an L{test.integration.library.ITest} instance and linked PlateAcquisition with all required fields set. """ screen = ScreenI() screen.name = rstring(self.uuid()) plate = PlateI() plate.name = rstring(self.uuid()) # Well A10 (will have two WellSamples) well_a = WellI() well_a.row = rint(0) well_a.column = rint(9) # Well A11 (will not have a WellSample) well_b = WellI() well_b.row = rint(0) well_b.column = rint(10) ws_a = WellSampleI() image_a = self.new_image(name=self.uuid()) ws_a.image = image_a ws_b = WellSampleI() image_b = self.new_image(name=self.uuid()) ws_b.image = image_b plate_acquisition = PlateAcquisitionI() plate_acquisition.plate = plate ws_a.plateAcquisition = plate_acquisition ws_b.plateAcquisition = plate_acquisition well_a.addWellSample(ws_a) well_a.addWellSample(ws_b) plate.addWell(well_a) plate.addWell(well_b) screen.linkPlate(plate) return self.update.saveAndReturnObject(screen)
def _parse_mnu_roi(self, columns): """Parses out ROI from OmeroTables columns for 'MNU' datasets.""" log.debug("Parsing %s MNU ROIs..." % (len(columns[0].values))) image_ids = columns[self.IMAGE_COL].values rois = list() # Save our file annotation to the database so we can use an unloaded # annotation for the saveAndReturnIds that will be triggered below. self.file_annotation = self.update_service.saveAndReturnObject(self.file_annotation) unloaded_file_annotation = FileAnnotationI(self.file_annotation.id.val, False) batch_no = 1 batches = dict() for i, image_id in enumerate(image_ids): unloaded_image = ImageI(image_id, False) roi = RoiI() shape = PointI() shape.theZ = rint(0) shape.theT = rint(0) values = columns[3].values shape.cx = rdouble(float(values[i])) values = columns[2].values shape.cy = rdouble(float(values[i])) roi.addShape(shape) roi.image = unloaded_image roi.linkAnnotation(unloaded_file_annotation) rois.append(roi) if len(rois) == self.ROI_UPDATE_LIMIT: self.thread_pool.add_task(self.update_rois, rois, batches, batch_no) rois = list() batch_no += 1 self.thread_pool.add_task(self.update_rois, rois, batches, batch_no) self.thread_pool.wait_completion() batch_keys = batches.keys() batch_keys.sort() for k in batch_keys: columns[self.ROI_COL].values += batches[k]
def getTimes(conn, image, theC=0): """ Get a dict of tIndex:time (seconds) for the first plane (Z = 0) at each time-point for the defined image and Channel. @param conn: BlitzGateway connection @param image: ImageWrapper @return: A map of tIndex: timeInSecs """ queryService = conn.getQueryService() pixelsId = image.getPixelsId() params = omero.sys.ParametersI() params.add("theC", rint(theC)) params.add("theZ", rint(0)) params.add("pixelsId", rlong(pixelsId)) query = "from PlaneInfo as Info where Info.theZ=:theZ and Info.theC=:theC and pixels.id=:pixelsId" infoList = queryService.findAllByQuery(query, params, conn.SERVICE_OPTS) timeMap = {} for info in infoList: tIndex = info.theT.getValue() time = info.deltaT.getValue() timeMap[tIndex] = time return timeMap
def data(request, entryId): conn = getConnection(request) entryName = str(entryId) project = conn.getObject("Project", attributes={"name": entryName}) # only want the first few images from each dataset p = omero.sys.Parameters() p.map = {} f = omero.sys.Filter() f.limit = rint(5) f.offset = rint(0) p.theFilter = f datasets = [] entryId = project.getName() for d in project.listChildren(): ds = {} ds["getId"] = str(d.getId()) name = d.getName() if name == entryId: continue # this dataset contains the map, not associated data. ds["getName"] = name ds["getDescription"] = d.getDescription() ds["countChildren"] = d.countChildren() ds["listChildren"] = d.listChildren(params=p) datasets.append(ds) if project == None: # project not found (None) handled by template return render_to_response("webemdb/data/data.html", {"project": project}) return render_to_response("webemdb/data/data.html", {"project": project, "datasets": datasets})
def search(request, conn=None, **kwargs): qs = conn.getQueryService() import omero.model searchTerm = request.REQUEST.get('search') # set up pagination page = int(request.REQUEST.get('page', 1)) # 1-based resultsPerPage = 20 #f.limit = rint(resultsPerPage * page) #f.offset = rint(resultsPerPage * (page-1)) p = omero.sys.Parameters() p.map = {} f = omero.sys.Filter() f.limit = rint(5) f.offset = rint(0) p.theFilter = f results = [] if searchTerm: projects = qs.findAllByFullText('Project', "file.contents:%s" % searchTerm, p) for p in projects: entryId = p.getName().getValue() desc = p.getDescription().getValue() title, sample = desc.split("\n") results.append({"entryId":entryId, "title": title, "sample": sample}) return render_to_response('webemdb/browse/search.html', {'searchString': searchTerm, 'results': results, 'nextPage': page+1 })
def search(request): conn = getConnection(request) qs = conn.getQueryService() import omero.model searchTerm = request.REQUEST.get("search") # set up pagination page = int(request.REQUEST.get("page", 1)) # 1-based resultsPerPage = 20 # f.limit = rint(resultsPerPage * page) # f.offset = rint(resultsPerPage * (page-1)) p = omero.sys.Parameters() p.map = {} f = omero.sys.Filter() f.limit = rint(5) f.offset = rint(0) p.theFilter = f results = [] if searchTerm: projects = qs.findAllByFullText("Project", "file.contents:%s" % searchTerm, p) for p in projects: entryId = p.getName().getValue() desc = p.getDescription().getValue() title, sample = desc.split("\n") results.append({"entryId": entryId, "title": title, "sample": sample}) return render_to_response( "webemdb/browse/search.html", {"searchString": searchTerm, "results": results, "nextPage": page + 1} )
def getTimes(conn, image, theC=0): """ Get a dict of tIndex:time (seconds) for the first plane (Z = 0) at each time-point for the defined image and Channel. @param conn: BlitzGateway connection @param image: ImageWrapper @return: A map of tIndex: timeInSecs """ queryService = conn.getQueryService() pixelsId = image.getPixelsId() params = omero.sys.ParametersI() params.add("theC", rint(theC)) params.add("theZ", rint(0)) params.add("pixelsId", rlong(pixelsId)) query = ("from PlaneInfo as Info where Info.theZ=:theZ" " and Info.theC=:theC and pixels.id=:pixelsId") infoList = queryService.findAllByQuery(query, params, conn.SERVICE_OPTS) timeMap = {} for info in infoList: tIndex = info.theT.getValue() time = info.deltaT.getValue() timeMap[tIndex] = time return timeMap
def populate_shape(o, set_unit_attributes=True): o.fillColor = rint(0xFFFFFFFF) o.fillRule = rstring("solid") o.fontFamily = rstring("cursive") if set_unit_attributes: o.fontSize = LengthI(12, UnitsLength.POINT) o.fontStyle = rstring("italic") o.locked = rbool(False) o.strokeColor = rint(0xFFFF0000) o.strokeDashArray = rstring("inherit") if SCHEMA_VERSION == "2015-01": o.visibility = rbool(True) o.strokeLineCap = rstring("round") if set_unit_attributes: o.strokeWidth = LengthI(4, UnitsLength.PIXEL) o.textValue = rstring("the_text") o.theC = rint(1) o.theT = rint(2) o.theZ = rint(3) t = identity_transform() if SCHEMA_VERSION == "2015-01": o.transform = t.svg_transform else: o.transform = t return o
def create_omero_point(data, image): point = PointI() point.x = rdouble(get_x(data)) point.y = rdouble(get_y(data)) point.theZ = rint(get_z(data, image)) point.theT = rint(get_t(data, image)) point.textValue = rstring("point-from-napari") return point
def dataset_to_well(conn, scriptParams, datasetId, plateId): dataset = conn.getObject("Dataset", datasetId) if dataset is None: print "No dataset found for ID %s" % datasetId return updateService = conn.getUpdateService() row = scriptParams["Well_Column"] col = scriptParams["Well_Row"] plate = conn.getObject("Plate", plateId) if plate is None: print "No Plate found for ID %s" % plateId return print "Moving images from Dataset: %d to Plate: %d; Row: %d, Column: %d" \ % (dataset.id, plate.getId(), row, col) # sort images by name images = list(dataset.listChildren()) datasetImgCount = len(images) if "Filter_Names" in scriptParams: filterBy = scriptParams["Filter_Names"] print "Filtering images for names containing: %s" % filterBy images = [i for i in images if (i.getName().find(filterBy) >= 0)] images.sort(key=lambda x: x.name.lower()) # Do we try to remove images from Dataset and Delete Dataset when/if empty? removeFrom = None removeDataset = "Remove_From_Dataset" in scriptParams and \ scriptParams["Remove_From_Dataset"] if removeDataset: removeFrom = dataset well = omero.model.WellI() well.plate = omero.model.PlateI(plate.getId(), False) well.column = rint(col - 1) well.row = rint(row - 1) well = updateService.saveAndReturnObject(well) for image in images: addedCount = addImageToWell(conn, image, well, removeFrom) # if user wanted to delete dataset, AND it's empty we can delete dataset deleteDataset = False # Turning this functionality off for now. deleteHandle = None if deleteDataset: if datasetImgCount == addedCount: dcs = list() print 'Deleting Dataset %d %s' % (dataset.id, dataset.name) options = None # {'/Image': 'KEEP'} # don't delete the images! dcs.append( omero.api.delete.DeleteCommand("/Dataset", dataset.id, options)) deleteHandle = conn.getDeleteService().queueDelete(dcs) return deleteHandle
def make_well_grid(grid_layout={}): wells = [] for row, column in grid_layout: ws_count = grid_layout[(row, column)] well = well_factory(ws_count) well.row = rint(row) well.column = rint(column) wells.append(well) return wells
def testRMapRMapRInt(self): params = omero.grid.JobParams() param = omero.grid.Param() param.prototype = rmap({"b": rmap({"c": rint(0)})}) params.inputs = {"a": param} input = rmap({"b": rmap({"c": rint(1)})}) inputs = {"a": input} assert "" == validate_inputs(params, inputs)
def testRListRInt(self): params = omero.grid.JobParams() param = omero.grid.Param() param.prototype = rlist(rint(0)) params.inputs = {"a": param} input = rlist(rint(1)) inputs = {"a": input} assert "" == validate_inputs(params, inputs)
def createWell(row, column): well = WellI() well.row = rint(row) well.column = rint(column) ws = WellSampleI() image = self.new_image(name=uuid) ws.image = image well.addWellSample(ws) return well
def test_simple_marshal_tiled(self, wrapped_image): image = wrapped_image._obj pixels = PixelsI() pixels.sizeX = rint(65) pixels.sizeY = rint(65) image.addPixels(pixels) data = wrapped_image.simpleMarshal(xtra={'tiled': True}) self.assert_data(data) assert data['tiled'] is True
def _load_well(self, attributes): """ Loads a Well based on a certain set of attributes from the server. @param attributes Set of attributes to filter on. @type attributes L{dict} """ if 'id' in attributes: return self.conn.getObject('Well', attributes=attributes) if 'name' in attributes: row, column = self.get_well_row_column(attributes['name']) path = self.request.REQUEST.get('path', '') for m in self.PATH_REGEX.finditer(path): object_type = m.group('object_type') # May have 'run' here rather than 'acquisition' because # the path will not have been validated and replaced. if object_type not in ('plate', 'run', 'acquisition'): continue # 'run' is an alternative for 'acquisition' object_type = object_type.replace('run', 'acquisition') # Try and load the potential parent first key = m.group('key') value = m.group('value') if key is None: key = 'id' if key == 'id': value = long(value) parent_attributes = {key: value} parent, = self.conn.getObjects( object_type, attributes=parent_attributes ) # Now use the parent to try and locate the Well query_service = self.conn.getQueryService() params = omero.sys.ParametersI() params.map['row'] = rint(row) params.map['column'] = rint(column) params.addId(parent.id) if object_type == 'plate': db_row, = query_service.projection( 'select w.id from Well as w ' 'where w.row = :row and w.column = :column ' 'and w.plate.id = :id', params, self.conn.SERVICE_OPTS ) if object_type == 'acquisition': db_row, = query_service.projection( 'select distinct w.id from Well as w ' 'join w.wellSamples as ws ' 'where w.row = :row and w.column = :column ' 'and ws.plateAcquisition.id = :id', params, self.conn.SERVICE_OPTS ) well_id, = db_row return self.conn.getObject( 'Well', well_id.val )
def _load_well(self, attributes): """ Loads a Well based on a certain set of attributes from the server. @param attributes Set of attributes to filter on. @type attributes L{dict} """ if "id" in attributes: return self.conn.getObject("Well", attributes=attributes) if "name" in attributes: row, column = self.get_well_row_column(attributes["name"]) path = self.request.GET.get("path", "") for m in self.PATH_REGEX.finditer(path): object_type = m.group("object_type") # May have 'run' here rather than 'acquisition' because # the path will not have been validated and replaced. if object_type not in ("plate", "run", "acquisition"): continue # 'run' is an alternative for 'acquisition' object_type = object_type.replace("run", "acquisition") # Try and load the potential parent first key = m.group("key") value = m.group("value") if key is None: key = "id" if key == "id": value = int(value) parent_attributes = {key: value} (parent, ) = self.conn.getObjects(object_type, attributes=parent_attributes) # Now use the parent to try and locate the Well query_service = self.conn.getQueryService() params = omero.sys.ParametersI() params.map["row"] = rint(row) params.map["column"] = rint(column) params.addId(parent.id) if object_type == "plate": (db_row, ) = query_service.projection( "select w.id from Well as w " "where w.row = :row and w.column = :column " "and w.plate.id = :id", params, self.conn.SERVICE_OPTS, ) if object_type == "acquisition": (db_row, ) = query_service.projection( "select distinct w.id from Well as w " "join w.wellSamples as ws " "where w.row = :row and w.column = :column " "and ws.plateAcquisition.id = :id", params, self.conn.SERVICE_OPTS, ) (well_id, ) = db_row return self.conn.getObject("Well", well_id.val)
def screen_with_plates(screen): for plate_id in range(5, 7): o = PlateI() o.id = rlong(plate_id) o.name = rstring('plate_name_%d' % plate_id) o.description = rstring('plate_description_%d' % plate_id) o.columnNamingConvention = rstring('number') o.rowNamingConvention = rstring('letter') o.columns = rint(12) o.rows = rint(8) o.defaultSample = rint(0) o.externalIdentifier = rstring('external_identifier_%d' % plate_id) o.status = rstring('status_%d' % plate_id) o.wellOriginX = LengthI(0.1, UnitsLength.REFERENCEFRAME) o.wellOriginY = LengthI(1.1, UnitsLength.REFERENCEFRAME) screen.linkPlate(o) for well_id in range(7, 9): well = WellI() well.id = rlong(well_id) well.column = rint(2) well.row = rint(1) well.externalDescription = \ rstring('external_description_%d' % well_id) well.externalIdentifier = \ rstring('external_identifier_%d' % well_id) well.type = rstring('the_type') well.alpha = rint(0) well.red = rint(255) well.green = rint(0) well.blue = rint(0) well.status = rstring('the_status') o.addWell(well) plateacquisition = PlateAcquisitionI() plateacquisition.id = rlong(well_id) plateacquisition.name = rstring( 'plateacquisition_name_%d' % well_id) plateacquisition.description = rstring( 'plateacquisition_description_%d' % well_id) plateacquisition.maximumFieldCount = rint(1) plateacquisition.startTime = rtime(1L) plateacquisition.endTime = rtime(2L) for wellsample_id in range(9, 11): wellsample = WellSampleI() wellsample.setPlateAcquisition(plateacquisition) wellsample.id = rlong(wellsample_id) wellsample.posX = LengthI(1.0, UnitsLength.REFERENCEFRAME) wellsample.posY = LengthI(2.0, UnitsLength.REFERENCEFRAME) wellsample.timepoint = rtime(1L) wellsample.image = create_image(1L) well.addWellSample(wellsample) return screen
def addRoi(self, request, imageid): img = self.getImage0(imageid) if img is None: return None data = json.loads(request.body) shapes = data['shapes'] x = y = l = z = t = -1 for s in shapes: for k in s: val = s[k] if k == "x": x = int(val) elif k == "y": y = int(val) elif k == "width": l = int(val) elif k == "theZ": z = int(val) elif k == "theT": t = int(val) elif k == "fillColorAsInt": fill = int(val) elif k == "strokeColorAsInt": stroke = int(val) elif k == "strokeWidth": strokeWidth = int(val) if (x < 0 or y < 0 or z < 0 or t < 0 or l <= 0): return None updateService = self._connection.getUpdateService() roi = RoiI() roi.setImage(img._obj) rect = RectangleI() rect.x = rdouble(x) rect.y = rdouble(y) rect.width = rdouble(l) rect.height = rdouble(l) rect.theZ = rint(z) rect.theT = rint(t) rect.setFillColor(rint(fill)) strokeLen = LengthI() strokeLen.setValue(strokeWidth) strokeLen.setUnit(UnitsLength.PIXEL) rect.setStrokeWidth(strokeLen) rect.setStrokeColor(rint(stroke)) roi.addShape(rect) if (updateService.saveAndReturnObject(roi) is None): return None return self.get_rois(imageid)
def __init__(self): self.WHITE = 16777215 self.BLACK = 0 self.GREY = 11184810 self.strokeColour = rint(self.GREY) self.strokeWidth = rint(1) self.strokeDashArray = rstring('') self.strokeDashOffset = rint(0) self.strokeLineCap = rstring('') self.strokeLineJoin = rstring('') self.strokeMiterLimit = rint(0) self.fillColour = rint(self.GREY) self.fillRule = rstring('')
def test1184(self): uuid = self.uuid() client = self.new_client(perms="rw----") share = client.sf.getShareService() query = client.sf.getQueryService() update = client.sf.getUpdateService() admin = client.sf.getAdminService() cont = client.sf.getContainerService() ds = DatasetI() ds.setName(rstring('test1184-ds-%s' % (uuid))) for i in range(1,2001): img = ImageI() img.setName(rstring('img1184-%s' % (uuid))) img.setAcquisitionDate(rtime(time.time())) # Saving in one go #dil = DatasetImageLinkI() #dil.setParent(ds) #dil.setChild(img) #update.saveObject(dil) ds.linkImage(img) ds = update.saveAndReturnObject(ds) c = cont.getCollectionCount(ds.__class__.__name__, ("imageLinks"), [ds.id.val], None) assert c[ds.id.val] == 2000 page = 1 p = omero.sys.Parameters() p.map = {} p.map["eid"] = rlong(admin.getEventContext().userId) p.map["oid"] = rlong(ds.id.val) if page is not None: f = omero.sys.Filter() f.limit = rint(24) f.offset = rint((int(page)-1)*24) p.theFilter = f sql = "select im from Image im join fetch im.details.owner join fetch im.details.group " \ "left outer join fetch im.datasetLinks dil left outer join fetch dil.parent d " \ "where d.id = :oid and im.details.owner.id=:eid order by im.id asc" start = time.time() res = query.findAllByQuery(sql,p) assert 24 == len(res) end = time.time() elapsed = end - start assert elapsed < 3.0,\ "Expected the test to complete in < 3 seconds, took: %f" % elapsed
def testFilter(self): p = ParametersI() p.noPage() assert None == p.theFilter p.page(2, 3) assert p.isPagination() assert rint(2) == p.theFilter.offset assert rint(3) == p.theFilter.limit p.noPage() assert not p.isPagination() assert None == p.theFilter.offset assert None == p.theFilter.limit assert None == p.getLimit() assert None == p.getOffset()
def duplicateMIF(self, orig_img): """ Use copyAndResizeImage to create a "synthetic" image (one without a fileset) """ new_img = self.pixels.copyAndResizeImage( orig_img.id.val, rint(16), rint(16), rint(1), rint(1), [0], None, True).val pix_id = unwrap(self.query.projection( "select p.id from Image i join i.pixels p where i.id = :id", ParametersI().addId(new_img)))[0][0] new_img = ImageI(new_img, False) new_pix = PixelsI(pix_id, False) return new_img, new_pix
def testGetAttrWorks(self): rbool(True).val rdouble(0.0).val rfloat(0.0).val rint(0).val rlong(0).val rtime(0).val rinternal(None).val robject(None).val rstring("").val rclass("").val rarray().val rlist().val rset().val rmap().val
def addRect(roi, x=10, y=10, w=100, h=50, theZ=0, theT=0, label=None): """ create and save a rectangle shape, add it to roi """ rect = omero.model.RectI() rect.x = rdouble(x) rect.y = rdouble(y) rect.width = rdouble(w) rect.height = rdouble(h) if theZ is not None: rect.theZ = rint(theZ) if theT is not None: rect.theT = rint(theT) if label is not None: rect.textValue = wrap(label) rect.setRoi(roi) roi.addShape(rect)
def create_image(image_index): image = ImageI() image.name = rstring("%s_%d" % (uuid(), image_index)) image.acquisitionDate = rtime(0) pixels = PixelsI() pixels.sha1 = rstring("") pixels.sizeX = rint(1) pixels.sizeY = rint(1) pixels.sizeZ = rint(1) pixels.sizeC = rint(1) pixels.sizeT = rint(1) pixels.dimensionOrder = DimensionOrderI(1L, False) # XYZCT pixels.pixelsType = PixelsTypeI(1L, False) # bit image.addPixels(pixels) return image