def planeGen(): """set up a generator of 2D numpy arrays.""" for z in range(sizeZ): # all Z sections for c in range(sizeC): for t in range(sizeT): # all time-points print "Plane: ", z, c, t if c == replaceChannel: yield pixels2.getPlane(z, c, t) else: yield pixels.getPlane(z, c, t) desc = "Image created from Image ID: %s, replacing Channel %s from Image ID: %s" % (imageId, replaceChannel, imageId2) newImg = conn.createImageFromNumpySeq(planeGen(), "ImageFromTwo", \ sizeZ, sizeC, sizeT, description=desc, dataset=dataset) # Get original channel names and colors to apply to new image # ================================================================= cNames = [] colors = [] for ch in image.getChannels(): cNames.append(ch.getLabel()) colors.append(ch.getColor().getRGB()) # Save channel names and colors # ================================================================= print "Applying channel Names:", cNames, " Colors:", colors for i, c in enumerate(newImg.getChannels()):
for z in range(sizeZ): # all Z sections # print(z) for c in range(sizeC): # print(c) for t in range(sizeT): # all time-points print("Plane: ", z, c, t) # print(pixels[t].getPlane(0, c, z)) yield pixels[t].getPlane(0, c, z) desc = ("Image created from concatenating multiple tiffs") print(sizeZ, sizeC, sizeT) new_img = conn.createImageFromNumpySeq(planeGen(), "SingleTimelapse", sizeZ, sizeC, sizeT, description=desc, dataset=dataset) save_as_ome_tiff(conn, new_img) folder_name = "" exp_dir = "" ometiff_ids = [t.id for t in dataset.listAnnotations(ns=NSOMETIFF)] #conn.deleteObjects("Annotation", ometiff_ids) extension = "ome.tif" name = os.path.basename(new_img.getName()) iid = new_img.getId() img_name = "%s-%d.%s" % (name, iid, extension) export_file = img_name
# hard-coded array of data. from numpy import array sizeX, sizeY, sizeZ, sizeC, sizeT = 5, 4, 1, 2, 1 plane1 = array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]) plane2 = array([[5, 6, 7, 8, 9], [0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [0, 1, 2, 3, 4]]) planes = [plane1, plane2] def planeGen(): """generator will yield planes""" for p in planes: yield p desc = "Image created from a hard-coded arrays" i = conn.createImageFromNumpySeq(planeGen(), "numpy image",\ sizeZ, sizeC, sizeT, description=desc, dataset=None) # Create an Image from an existing image # ================================================================= # We are going to create a new image by passing the method a 'generator' of 2D # planes This will come from an existing image, by taking the average of 2 channels. zctList = [] image = conn.getObject('Image', imageId) sizeZ, sizeC, sizeT = image.getSizeZ(), image.getSizeC(), image.getSizeT() dataset = image.getParent() pixels = image.getPrimaryPixels() newSizeC = 1 def planeGen():
# hard-coded array of data. from numpy import array, int8 sizeX, sizeY, sizeZ, sizeC, sizeT = 5, 4, 1, 2, 1 plane1 = array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [0, 1, 2, 3, 4], [5, 6, 7, 8, 9]], dtype=int8) plane2 = array([[5, 6, 7, 8, 9], [0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [0, 1, 2, 3, 4]], dtype=int8) planes = [plane1, plane2] def planeGen(): """generator will yield planes""" for p in planes: yield p desc = "Image created from a hard-coded arrays" i = conn.createImageFromNumpySeq(planeGen(), "numpy image",\ sizeZ, sizeC, sizeT, description=desc, dataset=None) # Create an Image from an existing image # ================================================================= # We are going to create a new image by passing the method a 'generator' of 2D # planes This will come from an existing image, by taking the average of 2 channels. zctList = [] image = conn.getObject('Image', imageId) sizeZ, sizeC, sizeT = image.getSizeZ(), image.getSizeC(), image.getSizeT() dataset = image.getParent() pixels = image.getPrimaryPixels() newSizeC = 1 def planeGen():
dtype=int8) plane2 = array( [[5, 6, 7, 8, 9], [0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [0, 1, 2, 3, 4]], dtype=int8) planes = [plane1, plane2] def plane_gen(): """generator will yield planes""" for p in planes: yield p desc = "Image created from a hard-coded arrays" i = conn.createImageFromNumpySeq( plane_gen(), "numpy image", size_z, size_c, size_t, description=desc, dataset=None) print('Created new Image:%s Name:"%s"' % (i.getId(), i.getName())) # Set the pixel size using units # ============================== # Lengths are specified by value and a unit enumeration # Here we set the pixel size X and Y to be 9.8 Angstroms # Re-load the image to avoid update conflicts i = conn.getObject("Image", i.getId()) u = omero.model.LengthI(9.8, UnitsLength.ANGSTROM) p = i.getPrimaryPixels()._obj
class TestSavedMasks(ITest): def setup_method(self): self.conn = BlitzGateway(client_obj=self.client) def save_and_return_masks(self, im, masks): roi = omero.model.RoiI() for mask in masks: roi.addShape(mask) us = self.conn.getUpdateService() roi.setImage(im._obj) roi = us.saveAndReturnObject(roi) assert roi rois = self.conn.getRoiService().findByImage(im.id, None) assert len(rois.rois) == 1 shapes = rois.rois[0].copyShapes() assert len(shapes) == len(masks) assert all((type(mask) == omero.model.MaskI) for mask in shapes) return shapes @pytest.mark.parametrize('args', [{}, { 'rgba': (255, 128, 64, 128), 'z': 1, 'c': 2, 't': 3, 'text': 'test' }]) def test_mask_from_binary_image(self, binary_image, args): im = self.conn.createImageFromNumpySeq(iter([binary_image]), 'omero-rois-integration') # Reload im = self.conn.getObject('Image', im.id) px = im.getPrimaryPixels().getPlane() mask = mask_from_binary_image(px, **args) mask = self.save_and_return_masks(im, [mask])[0] # The rest of this is more or less the same as the unit test assert unwrap(mask.getWidth()) == 2 assert unwrap(mask.getHeight()) == 2 assert unwrap(mask.getX()) == 1 assert unwrap(mask.getY()) == 1 assert np.array_equal(np.frombuffer(mask.getBytes(), np.uint8), np.array([224], dtype=np.uint8)) if args: assert unwrap(mask.getTheZ()) == 1 assert unwrap(mask.getTheC()) == 2 assert unwrap(mask.getTheT()) == 3 assert unwrap(mask.getTextValue()) == 'test' else: assert unwrap(mask.getTheZ()) is None assert unwrap(mask.getTheC()) is None assert unwrap(mask.getTheT()) is None assert unwrap(mask.getTextValue()) is None def test_mask_from_binary_full_image(self): binim = np.ones((4, 4), dtype=np.uint8) im = self.conn.createImageFromNumpySeq(iter([binim]), 'omero-rois-integration') # Reload im = self.conn.getObject('Image', im.id) px = im.getPrimaryPixels().getPlane() mask = mask_from_binary_image(px) mask = self.save_and_return_masks(im, [mask])[0] # The rest of this is more or less the same as the unit test assert unwrap(mask.getWidth()) == 4 assert unwrap(mask.getHeight()) == 4 assert np.array_equal(np.frombuffer(mask.getBytes(), np.uint8), np.array([255, 255], dtype=np.uint8)) @pytest.mark.parametrize('args', [{}, { 'rgba': (255, 128, 64, 128), 'z': 1, 'c': 2, 't': 3, 'text': 'test' }]) def test_masks_from_label_image(self, label_image, args): im = self.conn.createImageFromNumpySeq(iter([label_image]), 'omero-rois-integration') # Reload im = self.conn.getObject('Image', im.id) px = im.getPrimaryPixels().getPlane() masks = masks_from_label_image(px, **args) masks = self.save_and_return_masks(im, masks) # The rest of this is more or less the same as the unit test expected = ( # w, h, x, y, bytes (2, 2, 1, 1, np.array([224], dtype=np.uint8)), (2, 3, 2, 0, np.array([72], dtype=np.uint8)), ) assert len(masks) == 2 for i, mask in enumerate(masks): assert unwrap(mask.getWidth()) == expected[i][0] assert unwrap(mask.getHeight()) == expected[i][1] assert unwrap(mask.getX()) == expected[i][2] assert unwrap(mask.getY()) == expected[i][3] assert np.array_equal(np.frombuffer(mask.getBytes(), np.uint8), expected[i][4]) if args: assert unwrap(mask.getTheZ()) == 1 assert unwrap(mask.getTheC()) == 2 assert unwrap(mask.getTheT()) == 3 assert unwrap(mask.getTextValue()) == 'test' else: assert unwrap(mask.getTheZ()) is None assert unwrap(mask.getTheC()) is None assert unwrap(mask.getTheT()) is None assert unwrap(mask.getTextValue()) is None @pytest.mark.parametrize('args', [{}, { 'rgba': (255, 128, 64, 128), 'z': 1, 'c': 2, 't': 3, 'text': 'test' }]) def test_empty_mask_from_binary_image(self, args): empty_binary_image = np.array([[0]], dtype=np.uint8) raise_on_no_mask = False im = self.conn.createImageFromNumpySeq(iter([empty_binary_image]), 'omero-rois-integration') # Reload im = self.conn.getObject('Image', im.id) px = im.getPrimaryPixels().getPlane() mask = mask_from_binary_image(px, raise_on_no_mask=raise_on_no_mask, **args) mask = self.save_and_return_masks(im, [mask])[0] # The rest of this is more or less the same as the unit test assert unwrap(mask.getWidth()) == 0 assert unwrap(mask.getHeight()) == 0 assert unwrap(mask.getX()) == 0 assert unwrap(mask.getY()) == 0 assert np.array_equal(np.frombuffer(mask.getBytes(), np.uint8), []) if args: assert unwrap(mask.getTheZ()) == 1 assert unwrap(mask.getTheC()) == 2 assert unwrap(mask.getTheT()) == 3 assert unwrap(mask.getTextValue()) == 'test' else: assert unwrap(mask.getTheZ()) is None assert unwrap(mask.getTheC()) is None assert unwrap(mask.getTheT()) is None assert unwrap(mask.getTextValue()) is None
for z in range(size_z): # all Z sections for c in range(size_c): for t in range(size_t): # all time-points print("Plane: ", z, c, t) if c == replace_channel: yield pixels.getPlane(z, c, t) else: yield pixels2.getPlane(z, c, t) desc = ("Image created from Image ID: %s, replacing Channel %s from Image ID:" " %s" % (image_id2, replace_channel, imageId)) new_img = conn.createImageFromNumpySeq(plane_gen(), "ImageFromTwo", size_z, size_c, size_t, description=desc, dataset=dataset) # Get original channel names and colors to apply to new image # ================================================================= c_names = [] colors = [] for ch in img.getChannels(): c_names.append(ch.getLabel()) colors.append(ch.getColor().getRGB()) # Save channel names and colors # ================================================================= print("Applying channel Names:", c_names, " Colors:", colors)