def test_10_07_iter_wells(self): o = O.OMEXML(self.GROUPFILES_XML) for i, name in enumerate(o.plates[0].Well): row = int(i / 12) column = i % 12 self.assertEqual(name, "ABCDEFGH"[row] + "%02d" % (column + 1)) self.assertEqual(name, "H12")
def test_08_03_plate_iter(self): o = O.OMEXML(self.GROUPFILES_XML) nplates = 5 for i in range(1, nplates): o.plates.newPlate("MyPlate%d" % i, "Plate:%d" % i) for i, plate in enumerate(o.plates): self.assertEqual(plate.ID, "Plate:%d" % i)
def test_05_18_pixels_set_channel_count(self): o = O.OMEXML(TIFF_XML) o.image(0).Pixels.channel_count = 2 self.assertEqual( len( o.image(0).Pixels.node.findall(O.qn(o.get_ns("ome"), "Channel"))), 2)
def test_14_14_set_delta_t(self): o = O.OMEXML(TIFF_XML) pixels = o.image(0).Pixels self.assertTrue(isinstance(pixels, O.OMEXML.Pixels)) plane = pixels.Plane(0) plane.DeltaT = 1.25 self.assertEqual(float(plane.node.get("DeltaT")), 1.25)
def test_14_17_set_position_z(self): o = O.OMEXML(TIFF_XML) pixels = o.image(0).Pixels self.assertTrue(isinstance(pixels, O.OMEXML.Pixels)) plane = pixels.Plane(0) plane.PositionZ = 7.5 self.assertEqual(float(plane.node.get("PositionZ")), 7.5)
def test_14_02_set_plane_count(self): o = O.OMEXML() pixels = o.image(0).Pixels self.assertTrue(isinstance(pixels, O.OMEXML.Pixels)) pixels.plane_count = 2 self.assertEqual( len(pixels.node.findall(O.qn(o.get_ns('ome'), "Plane"))), 2)
def test_13_11_get_image_ref(self): o = O.OMEXML(self.GROUPFILES_XML) ws = o.plates[0].Well['A02'].Sample[3] self.assertEqual(ws.ImageRef, "Image:9") ref = ws.node.find(O.qn(O.NS_SPW, "ImageRef")) ws.node.remove(ref) self.assertTrue(ws.ImageRef is None)
def test_14_08_set_the_t(self): o = O.OMEXML(TIFF_XML) pixels = o.image(0).Pixels self.assertTrue(isinstance(pixels, O.OMEXML.Pixels)) plane = pixels.Plane(0) plane.TheC = 7 self.assertEqual(int(plane.node.get("TheC")), 7)
def test_14_05_get_the_t(self): o = O.OMEXML(TIFF_XML) pixels = o.image(0).Pixels self.assertTrue(isinstance(pixels, O.OMEXML.Pixels)) plane = pixels.Plane(0) self.assertTrue(isinstance(plane, O.OMEXML.Plane)) plane.node.set("TheT", "9") self.assertEqual(plane.TheT, 9)
def test_10_08_new_well(self): o = O.OMEXML() plate = o.plates.newPlate("Foo", "Bar") plate.Well.new(4, 5, "xyz") w = plate.Well[0] self.assertEqual(w.node.get("Row"), "4") self.assertEqual(w.node.get("Column"), "5") self.assertEqual(w.node.get("ID"), "xyz")
def test_02_04_set_text(self): o = O.OMEXML(TIFF_XML) ad = o.root_node.find("/".join( [O.qnome(x) for x in ("Image", "AcquiredDate")])) im = o.root_node.find(O.qnome("Image")) O.set_text(im, "Foo") self.assertEqual(O.get_text(im), "Foo") O.set_text(ad, "Bar") self.assertEqual(O.get_text(ad), "Bar")
def test_13_08_set_timepoint(self): o = O.OMEXML(self.GROUPFILES_XML) ws = o.plates[0].Well['A02'].Sample[3] now = datetime.datetime.now() now_string = now.isoformat() ws.Timepoint = now self.assertEqual(ws.node.get("Timepoint"), now_string) ws = o.plates[0].Well['A03'].Sample[4] ws.Timepoint = now_string self.assertEqual(ws.node.get("Timepoint"), now_string)
def test_04_07_image_1_acquisition_date(self): # regression test of #38 o = O.OMEXML() o.set_image_count(2) date_1 = "2011-12-21T11:04:14.903000" date_2 = "2015-10-13T09:57:00.000000" o.image(0).AcquisitionDate = date_1 o.image(1).AcquisitionDate = date_2 self.assertEqual(o.image(0).AcquisitionDate, date_1) self.assertEqual(o.image(1).AcquisitionDate, date_2)
def test_12_05_new_sample_item(self): o = O.OMEXML() plate = o.plates.newPlate("foo", "bar") w = plate.Well.new(4, 5, "xyz") w.Sample.new("ooo") w.Sample.new("ppp") sample_nodes = w.node.findall(O.qn(O.NS_SPW, "WellSample")) self.assertEqual(len(sample_nodes), 2) self.assertEqual(sample_nodes[0].get("ID"), "ooo") self.assertEqual(sample_nodes[1].get("ID"), "ppp") self.assertEqual(sample_nodes[0].get("Index"), "0") self.assertEqual(sample_nodes[1].get("Index"), "1")
def test_07_01_sa_get_item(self): o = O.OMEXML(TIFF_XML) a = o.structured_annotations["Annotation:4"] self.assertEqual(a.tag, O.qn(O.NS_SA, "XMLAnnotation")) values = a.findall(O.qn(O.NS_SA, "Value")) self.assertEqual(len(values), 1) oms = values[0].findall( O.qn(O.NS_ORIGINAL_METADATA, "OriginalMetadata")) self.assertEqual(len(oms), 1) keys = oms[0].findall(O.qn(O.NS_ORIGINAL_METADATA, "Key")) self.assertEqual(len(keys), 1) self.assertEqual(O.get_text(keys[0]), "XResolution") values = oms[0].findall(O.qn(O.NS_ORIGINAL_METADATA, "Value")) self.assertEqual(len(values), 1) self.assertEqual(O.get_text(values[0]), "72")
def test_07_05_om_setitem(self): o = O.OMEXML() o.structured_annotations.OriginalMetadata["Foo"] = "Bar" sa = o.structured_annotations.node a = sa.findall(O.qn(O.NS_SA, "XMLAnnotation")) self.assertEqual(len(a), 1) vs = a[0].findall(O.qn(O.NS_SA, "Value")) self.assertEqual(len(vs), 1) om = vs[0].findall(O.qn(O.NS_ORIGINAL_METADATA, "OriginalMetadata")) self.assertEqual(len(om), 1) k = om[0].findall(O.qn(O.NS_ORIGINAL_METADATA, "Key")) self.assertEqual(len(k), 1) self.assertEqual(O.get_text(k[0]), "Foo") v = om[0].findall(O.qn(O.NS_ORIGINAL_METADATA, "Value")) self.assertEqual(len(v), 1) self.assertEqual(O.get_text(v[0]), "Bar")
def test_08_04_plate_slice(self): o = O.OMEXML(self.GROUPFILES_XML) for i in range(1, 5): o.plates.newPlate("MyPlate%d" % i, "Plate:%d" % i) plates = o.plates[2:-1] self.assertEqual(len(plates), 2) self.assertTrue( all([ plate.ID == "Plate:%d" % (i + 2) for i, plate in enumerate(plates) ])) plates = o.plates[-4:4] self.assertEqual(len(plates), 3) self.assertTrue( all([ plate.ID == "Plate:%d" % (i + 1) for i, plate in enumerate(plates) ]))
def test_04_03_image_get_name(self): o = O.OMEXML(TIFF_XML) self.assertEquals(o.image(0).Name, "Channel1-01-A-01.tif")
def test_04_02_image_set_id(self): o = O.OMEXML(TIFF_XML) o.image(0).ID = "Foo" self.assertEquals(o.image(0).node.get("ID"), "Foo")
def test_04_01_image_get_id(self): o = O.OMEXML(TIFF_XML) self.assertEquals(o.image(0).ID, "Image:0")
def test_03_04_structured_annotations(self): o = O.OMEXML(TIFF_XML) self.assertEqual(o.structured_annotations.node.tag, O.qn(O.NS_SA, "StructuredAnnotations"))
def test_03_03_image(self): o = O.OMEXML(self.GROUPFILES_XML) self.assertEquals(o.image_count, 576) for i in range(576): im = o.image(i) self.assertEqual(im.node.get("ID"), "Image:%d" % i)
def test_14_13_get_position_z(self): o = O.OMEXML(TIFF_XML) pixels = o.image(0).Pixels self.assertTrue(isinstance(pixels, O.OMEXML.Pixels)) plane = pixels.Plane(0) self.assertEqual(plane.PositionZ, 2.25)
def test_14_10_get_exposure_time(self): o = O.OMEXML(TIFF_XML) pixels = o.image(0).Pixels self.assertTrue(isinstance(pixels, O.OMEXML.Pixels)) plane = pixels.Plane(0) self.assertEqual(plane.ExposureTime, 0.25)
def test_14_09_get_delta_t(self): o = O.OMEXML(TIFF_XML) pixels = o.image(0).Pixels self.assertTrue(isinstance(pixels, O.OMEXML.Pixels)) plane = pixels.Plane(0) self.assertEqual(plane.DeltaT, 1.25)
def test_03_01_get_image_count(self): for xml, count in ((self.GROUPFILES_XML, 576), (TIFF_XML, 1)): o = O.OMEXML(xml) self.assertEqual(o.image_count, count)
def write(img, path, dtype='uint16', verbose=True): """ Write img as TIF on disk :param img: Numpy ZYX stack :param path: Full path (with filename) to save :param type: byte type to save :param verbose: Print process :return: Nothing, saves file to disk """ import bioformats.omexml as ome import javabridge as jutil import bioformats import numpy as np import os import sys # Memotrack works on TZYX, swaps to XYZT, for the standard. # memotrack numpy sequence T[0]Z[1]Y[2]X[3] img_swap = np.swapaxes(img, 0, 3) # Here swapping T and X: TZYX -> XZYT img_swap = np.swapaxes(img_swap, 1, 2) # Now on Z and Y: XZYT -> XYZT # Inserting the C dimension (to get the XYCZT) img_XYCZT = np.expand_dims(img_swap, 2) if verbose: print('Dimensions (XYCZT): ' + str(np.shape(img_XYCZT))) sys.stdout.flush() # Get the new dimensions SizeX = np.shape(img_XYCZT)[0] SizeY = np.shape(img_XYCZT)[1] SizeC = np.shape(img_XYCZT)[2] SizeZ = np.shape(img_XYCZT)[3] SizeT = np.shape(img_XYCZT)[4] # Start JVM for bioformats jutil.start_vm(class_path=bioformats.JARS) # Getting metadata info omexml = ome.OMEXML() omexml.image(0).Name = os.path.split(path)[1] p = omexml.image(0).Pixels assert isinstance(p, ome.OMEXML.Pixels) p.SizeX = SizeX p.SizeY = SizeY p.SizeC = SizeC p.SizeT = SizeT p.SizeZ = SizeZ p.DimensionOrder = ome.DO_XYCZT p.PixelType = dtype p.channel_count = SizeC p.plane_count = SizeZ p.Channel(0).SamplesPerPixel = SizeC omexml.structured_annotations.add_original_metadata( ome.OM_SAMPLES_PER_PIXEL, str(SizeC)) # Converting to omexml xml = omexml.to_xml() # Write file using Bioformats if verbose: print('Writing frames:'), sys.stdout.flush() for frame in range(SizeT): if verbose: print('[' + str(frame + 1) + ']'), sys.stdout.flush() index = frame pixel_buffer = bioformats.formatwriter.convert_pixels_to_buffer( img_XYCZT[:, :, :, :, frame], dtype) script = """ importClass(Packages.loci.formats.services.OMEXMLService, Packages.loci.common.services.ServiceFactory, Packages.loci.formats.out.TiffWriter); var service = new ServiceFactory().getInstance(OMEXMLService); var metadata = service.createOMEXMLMetadata(xml); var writer = new TiffWriter(); writer.setBigTiff(true); writer.setMetadataRetrieve(metadata); writer.setId(path); writer.setInterleaved(true); writer.saveBytes(index, buffer); writer.close(); """ jutil.run_script( script, dict(path=path, xml=xml, index=index, buffer=pixel_buffer)) if verbose: print('[Done]') sys.stdout.flush() if verbose: print('File saved on ' + str(path)) sys.stdout.flush()
def test_03_02_set_image_count(self): o = O.OMEXML(TIFF_XML) o.image_count = 2 self.assertEqual(len(o.root_node.findall(O.qnome("Image"))), 2)
def write_image(pathname, pixels, pixel_type, c=0, z=0, t=0, size_c=1, size_z=1, size_t=1, channel_names=None): """Write the image using bioformats. :param filename: save to this filename :param pixels: the image to save :param pixel_type: save using this pixel type :param c: the image's channel index :param z: the image's `z` index :param t: the image's `t` index :param size_c: # of channels in the stack :param size_z: # of z stacks :param size_t: # of timepoints in the stack :param channel_names: names of the channels (make up names if not present). """ omexml = ome.OMEXML() omexml.image(0).Name = os.path.split(pathname)[1] p = omexml.image(0).Pixels assert isinstance(p, ome.OMEXML.Pixels) p.SizeX = pixels.shape[1] p.SizeY = pixels.shape[0] p.SizeC = size_c p.SizeT = size_t p.SizeZ = size_z p.DimensionOrder = ome.DO_XYCZT p.PixelType = pixel_type index = c + size_c * z + size_c * size_z * t if pixels.ndim == 3: p.SizeC = pixels.shape[2] p.Channel(0).SamplesPerPixel = pixels.shape[2] omexml.structured_annotations.add_original_metadata( ome.OM_SAMPLES_PER_PIXEL, str(pixels.shape[2])) elif size_c > 1: p.channel_count = size_c pixel_buffer = convert_pixels_to_buffer(pixels, pixel_type) xml = omexml.to_xml() script = """ importClass(Packages.loci.formats.services.OMEXMLService, Packages.loci.common.services.ServiceFactory, Packages.loci.formats.ImageWriter); var service = new ServiceFactory().getInstance(OMEXMLService); var metadata = service.createOMEXMLMetadata(xml); var writer = new ImageWriter(); writer.setMetadataRetrieve(metadata); writer.setId(path); writer.setInterleaved(true); writer.saveBytes(index, buffer); writer.close(); """ jutil.run_script( script, dict(path=pathname, xml=xml, index=index, buffer=pixel_buffer))
def test_14_01_get_plane_count(self): o = O.OMEXML(TIFF_XML) self.assertEqual(o.image(0).Pixels.plane_count, 1)