Exemplo n.º 1
0
    def test_invalid_plane_option(self, tmpdir):
        f = str(tmpdir.join('invalid_plane_options.companion.ome'))

        i = Image("test", 512, 512, 1, 1, 1)
        options = {
            'EmissionWavelength': '512.0',
            'EmissionWavelengthUnit': 'nm',
        }
        i.add_plane(z=0, c=0, t=0, options=options)
        create_companion(images=[i], out=f)

        root = ElementTree.parse(f).getroot()
        images = root.findall('OME:Image', namespaces=NS)
        assert len(images) == 1
        assert images[0].attrib['Name'] == 'test'
        pixels = images[0].findall('OME:Pixels', namespaces=NS)
        assert len(pixels) == 1
        assert pixels[0].attrib['SizeX'] == '512'
        assert pixels[0].attrib['SizeY'] == '512'
        assert pixels[0].attrib['SizeZ'] == '1'
        assert pixels[0].attrib['SizeC'] == '1'
        assert pixels[0].attrib['SizeT'] == '1'
        assert pixels[0].attrib['DimensionOrder'] == 'XYZTC'
        assert pixels[0].attrib['Type'] == 'uint16'
        planes = pixels[0].findall('OME:Plane', namespaces=NS)
        assert len(planes) == 1
        assert planes[0].attrib['TheZ'] == '0'
        assert planes[0].attrib['TheC'] == '0'
        assert planes[0].attrib['TheT'] == '0'
        assert 'EmissionWavelength' not in planes[0].attrib
        assert 'EmissionWavelengthUnit' not in planes[0].attrib
Exemplo n.º 2
0
    def test_rgb_channel(self, tmpdir):
        f = str(tmpdir.join('rgb.companion.ome'))

        i = Image("test", 256, 512, 1, 3, 1)
        i.add_channel(samplesPerPixel=3)
        create_companion(images=[i], out=f)

        root = ElementTree.parse(f).getroot()
        images = root.findall('OME:Image', namespaces=NS)
        assert len(images) == 1
        assert images[0].attrib['Name'] == 'test'
        pixels = images[0].findall('OME:Pixels', namespaces=NS)
        assert len(pixels) == 1
        assert pixels[0].attrib['SizeX'] == '256'
        assert pixels[0].attrib['SizeY'] == '512'
        assert pixels[0].attrib['SizeZ'] == '1'
        assert pixels[0].attrib['SizeC'] == '3'
        assert pixels[0].attrib['SizeT'] == '1'
        assert pixels[0].attrib['DimensionOrder'] == 'XYZTC'
        assert pixels[0].attrib['Type'] == 'uint16'
        channels = pixels[0].findall('OME:Channel', namespaces=NS)
        assert len(channels) == 1
        assert 'Name' not in channels[0].attrib
        assert 'Color' not in channels[0].attrib
        assert channels[0].attrib['SamplesPerPixel'] == '3'
Exemplo n.º 3
0
    def test_too_many_channel_samples(self, tmpdir):
        f = str(tmpdir.join('invalid.companion.ome'))

        i = Image("test", 512, 512, 1, 2, 1)
        i.add_channel(samplesPerPixel=3)
        with pytest.raises(AssertionError):
            create_companion(images=[i], out=f)
Exemplo n.º 4
0
    def test_pixelzies(self, tmpdir, pixel_size_x, pixel_size_y, pixel_size_z,
                       pixel_size_x_unit, pixel_size_y_unit,
                       pixel_size_z_unit):
        f = str(tmpdir.join('image.companion.ome'))

        i = Image("test",
                  256,
                  512,
                  3,
                  4,
                  5,
                  physSizeX=pixel_size_x,
                  physSizeY=pixel_size_y,
                  physSizeZ=pixel_size_z,
                  physSizeXUnit=pixel_size_x_unit,
                  physSizeYUnit=pixel_size_y_unit,
                  physSizeZUnit=pixel_size_z_unit,
                  type="unit8")
        create_companion(images=[i], out=f)

        root = ElementTree.parse(f).getroot()
        images = root.findall('OME:Image', namespaces=NS)
        pixels = images[0].findall('OME:Pixels', namespaces=NS)[0]
        if pixel_size_x:
            assert pixels.attrib['PhysicalSizeX'] == pixel_size_x
            if pixel_size_x_unit:
                assert pixels.attrib['PhysicalSizeXUnit'] == pixel_size_x_unit
        if pixel_size_y:
            assert pixels.attrib['PhysicalSizeY'] == pixel_size_y
            if pixel_size_y_unit:
                assert pixels.attrib['PhysicalSizeYUnit'] == pixel_size_y_unit
        if pixel_size_z:
            assert pixels.attrib['PhysicalSizeZ'] == pixel_size_z
            if pixel_size_z_unit:
                assert pixels.attrib['PhysicalSizeZUnit'] == pixel_size_z_unit
Exemplo n.º 5
0
    def test_multiple_rows_columns_wellsamples(self, tmpdir):
        f = str(tmpdir.join('plate.companion.ome'))

        p = Plate("test", 4, 5)
        for row in range(4):
            for column in range(5):
                well = p.add_well(row, column)
                for field in range(6):
                    i = Image("test", 256, 512, 3, 4, 5)
                    well.add_wellsample(field, i)
        create_companion(plates=[p], out=f)

        root = ElementTree.parse(f).getroot()
        plates = root.findall('OME:Plate', namespaces=NS)
        assert len(plates) == 1
        assert plates[0].attrib['Name'] == 'test'
        wells = plates[0].findall('OME:Well', namespaces=NS)
        assert len(wells) == 20
        imageids = []
        for i in range(20):
            wellsamples = wells[i].findall('OME:WellSample', namespaces=NS)
            assert len(wellsamples) == 6
            for ws in wellsamples:
                imagerefs = ws.findall('OME:ImageRef', namespaces=NS)
                assert len(imagerefs) == 1
                imageids.append(imagerefs[0].attrib['ID'])
        assert len(imageids) == 120
        images = root.findall('OME:Image', namespaces=NS)
        assert len(images) == 120
        assert [x.attrib['ID'] for x in images] == imageids
Exemplo n.º 6
0
    def test_ome(self, tmpdir):
        f = str(tmpdir.join('root.companion.ome'))

        create_companion(out=f)

        root = ElementTree.parse(f).getroot()
        assert root.tag == '{%s}OME' % NS['OME']
        assert root.attrib['Creator'] == 'ome_model %s' % __version__
Exemplo n.º 7
0
def write_companion(images, file_path):
    # Generate companion OME-XML file
    if not os.path.exists(dirname(file_path)):
        os.makedirs(dirname(file_path))
        logging.info("Created %s" % dirname(file_path))
    create_companion(images=images, out=file_path)

    # Indent XML for readability
    proc = subprocess.Popen(
        ['xmllint', '--format', '-o', file_path, file_path],
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE)
    (output, error_output) = proc.communicate()
    logging.info("Created %s" % file_path)
Exemplo n.º 8
0
    def test_plane_options(self, tmpdir):
        f = str(tmpdir.join('plane_options.companion.ome'))

        i = Image("test", 512, 512, 1, 1, 1)
        options = {
            'DeltaT': '0.0',
            'DeltaTUnit': 's',
            'ExposureTime': '100',
            'ExposureTimeUnit': 'ms',
            'PositionX': '0.0',
            'PositionXUnit': 'reference frame',
            'PositionY': '0.0',
            'PositionYUnit': 'reference frame',
            'PositionZ': '0.0',
            'PositionZUnit': 'reference frame',
        }
        i.add_plane(z=0, c=0, t=0, options=options)
        create_companion(images=[i], out=f)

        root = ElementTree.parse(f).getroot()
        images = root.findall('OME:Image', namespaces=NS)
        assert len(images) == 1
        assert images[0].attrib['Name'] == 'test'
        pixels = images[0].findall('OME:Pixels', namespaces=NS)
        assert len(pixels) == 1
        assert pixels[0].attrib['SizeX'] == '512'
        assert pixels[0].attrib['SizeY'] == '512'
        assert pixels[0].attrib['SizeZ'] == '1'
        assert pixels[0].attrib['SizeC'] == '1'
        assert pixels[0].attrib['SizeT'] == '1'
        assert pixels[0].attrib['DimensionOrder'] == 'XYZTC'
        assert pixels[0].attrib['Type'] == 'uint16'
        planes = pixels[0].findall('OME:Plane', namespaces=NS)
        assert len(planes) == 1
        assert planes[0].attrib['TheZ'] == '0'
        assert planes[0].attrib['TheC'] == '0'
        assert planes[0].attrib['TheT'] == '0'
        assert planes[0].attrib['DeltaT'] == '0.0'
        assert planes[0].attrib['DeltaTUnit'] == 's'
        assert planes[0].attrib['ExposureTime'] == '100'
        assert planes[0].attrib['ExposureTimeUnit'] == 'ms'
        assert planes[0].attrib['PositionX'] == '0.0'
        assert planes[0].attrib['PositionXUnit'] == 'reference frame'
        assert planes[0].attrib['PositionY'] == '0.0'
        assert planes[0].attrib['PositionYUnit'] == 'reference frame'
        assert planes[0].attrib['PositionZ'] == '0.0'
        assert planes[0].attrib['PositionZUnit'] == 'reference frame'
def create_companion(file_template, name, outdir, maxindex):
    os.makedirs(outdir, exist_ok=True)
    outpath = f'{outdir}/{name}.companion.ome'
    if os.path.isfile(outpath):
        print(f'{outpath} already exists, not overwriting')
        return

    for index in range(maxindex + 1):
        tif = file_template.format(index=index)
        if os.path.exists(tif):
            with tifffile.TiffFile(tif) as t:
                assert len(t.series) == 1
                s = t.series[0]
                assert s.axes == 'ZYX'
                t = index
                if index == 0:
                    z, y, x = s.shape
                    dt = str(s.dtype)
                    companion = ome.Image(
                        os.path.basename(name), x, y, z, 1, sizeT=(maxindex + 1),
                        order='XYZCT', type=dt)
                    companion.add_channel(samplesPerPixel=1)
                else:
                    assert s.shape == (z, y, x)
                    #assert str(s.dtype) == dt, (tif, index, dt)
                companion.add_tiff(os.path.basename(tif), t=t, planeCount=z)
        else:
            no_tif = tif
            tif = file_template.format(index=(index-1))
            if not os.path.exists(tif):
                tif = file_template.format(index=(index+2))
            if not os.path.exists(tif):
                print("{} doesn't exit, not substitute found.".format(no_tif))
                exit(1)
            print("{} doesn't exit, using {}".format(no_tif, tif))
            companion.add_tiff(os.path.basename(tif), t=t, planeCount=z)

        tifbase = os.path.basename(tif)
        tiflink = f'{outdir}/{tifbase}'
        if not os.path.islink(tiflink):
            os.symlink(tif, tiflink)

    with open(outpath, 'wb') as o:
        ome.create_companion(images=[companion], out=o)
Exemplo n.º 10
0
    def test_minimal_plate(self, tmpdir, rc):
        f = str(tmpdir.join('plate.companion.ome'))

        if rc:
            p = Plate("test", 1, 2)
        else:
            p = Plate("test")
        well = p.add_well(0, 0)
        i = Image("test", 256, 512, 3, 4, 5)
        well.add_wellsample(0, i)
        create_companion(plates=[p], out=f)

        root = ElementTree.parse(f).getroot()
        plates = root.findall('OME:Plate', namespaces=NS)
        assert len(plates) == 1
        assert plates[0].attrib['Name'] == 'test'
        if rc:
            assert plates[0].attrib['Rows'] == '1'
            assert plates[0].attrib['Columns'] == '2'
        wells = plates[0].findall('OME:Well', namespaces=NS)
        assert len(wells) == 1
        assert wells[0].attrib['Row'] == '0'
        assert wells[0].attrib['Column'] == '0'
        wellsamples = wells[0].findall('OME:WellSample', namespaces=NS)
        assert len(wellsamples) == 1
        imagerefs = wellsamples[0].findall('OME:ImageRef', namespaces=NS)
        assert len(imagerefs) == 1
        imageid = imagerefs[0].attrib['ID']
        images = root.findall('OME:Image', namespaces=NS)
        assert len(images) == 1
        assert images[0].attrib['Name'] == 'test'
        assert images[0].attrib['ID'] == imageid
        pixels = images[0].findall('OME:Pixels', namespaces=NS)
        assert len(pixels) == 1
        assert pixels[0].attrib['SizeX'] == '256'
        assert pixels[0].attrib['SizeY'] == '512'
        assert pixels[0].attrib['SizeZ'] == '3'
        assert pixels[0].attrib['SizeC'] == '4'
        assert pixels[0].attrib['SizeT'] == '5'
        assert pixels[0].attrib['DimensionOrder'] == 'XYZTC'
        assert pixels[0].attrib['Type'] == 'uint16'
        channels = pixels[0].findall('OME:Channel', namespaces=NS)
        assert len(channels) == 0
Exemplo n.º 11
0
    def test_pixeltypes(self, tmpdir, pixel_type):
        f = str(tmpdir.join('image.companion.ome'))

        i = Image("test", 256, 512, 3, 4, 5, type=pixel_type)
        create_companion(images=[i], out=f)

        root = ElementTree.parse(f).getroot()
        images = root.findall('OME:Image', namespaces=NS)
        assert len(images) == 1
        assert images[0].attrib['Name'] == 'test'
        pixels = images[0].findall('OME:Pixels', namespaces=NS)
        assert len(pixels) == 1
        assert pixels[0].attrib['SizeX'] == '256'
        assert pixels[0].attrib['SizeY'] == '512'
        assert pixels[0].attrib['SizeZ'] == '3'
        assert pixels[0].attrib['SizeC'] == '4'
        assert pixels[0].attrib['SizeT'] == '5'
        assert pixels[0].attrib['DimensionOrder'] == 'XYZTC'
        assert pixels[0].attrib['Type'] == pixel_type
        channels = pixels[0].findall('OME:Channel', namespaces=NS)
        assert len(channels) == 0
Exemplo n.º 12
0
    def test_channel(self, tmpdir, name, color):
        f = str(tmpdir.join('3channels.companion.ome'))

        i = Image("test", 256, 512, 3, 4, 5)
        i.add_channel()
        i.add_channel(name="Blue", color="65535", samplesPerPixel=1)
        i.add_channel(name=name, color=color)
        create_companion(images=[i], out=f)

        root = ElementTree.parse(f).getroot()
        images = root.findall('OME:Image', namespaces=NS)
        assert len(images) == 1
        assert images[0].attrib['Name'] == 'test'
        pixels = images[0].findall('OME:Pixels', namespaces=NS)
        assert len(pixels) == 1
        assert pixels[0].attrib['SizeX'] == '256'
        assert pixels[0].attrib['SizeY'] == '512'
        assert pixels[0].attrib['SizeZ'] == '3'
        assert pixels[0].attrib['SizeC'] == '4'
        assert pixels[0].attrib['SizeT'] == '5'
        assert pixels[0].attrib['DimensionOrder'] == 'XYZTC'
        assert pixels[0].attrib['Type'] == 'uint16'
        channels = pixels[0].findall('OME:Channel', namespaces=NS)
        assert len(channels) == 3
        assert 'Name' not in channels[0].attrib
        assert 'Color' not in channels[0].attrib
        assert channels[0].attrib['SamplesPerPixel'] == '1'
        assert channels[1].attrib['Name'] == 'Blue'
        assert channels[1].attrib['Color'] == '65535'
        assert channels[1].attrib['SamplesPerPixel'] == '1'
        if name:
            assert channels[2].attrib['Name'] == name
        else:
            assert 'Name' not in channels[2].attrib
        if color:
            assert channels[2].attrib['Color'] == color
        else:
            assert 'Color' not in channels[2].attrib
        assert channels[2].attrib['SamplesPerPixel'] == '1'
Exemplo n.º 13
0
    def test_planes(self, tmpdir):
        f = str(tmpdir.join('planes.companion.ome'))

        i = Image("test", 256, 512, 1, 2, 2)
        i.add_plane(c=0, z=0, t=0)
        i.add_plane(c=0, z=0, t=1)
        i.add_plane(c=1, z=0, t=0)
        i.add_plane(c=1, z=0, t=1)
        create_companion(images=[i], out=f)

        root = ElementTree.parse(f).getroot()
        images = root.findall('OME:Image', namespaces=NS)
        assert len(images) == 1
        assert images[0].attrib['Name'] == 'test'
        pixels = images[0].findall('OME:Pixels', namespaces=NS)
        assert len(pixels) == 1
        assert pixels[0].attrib['SizeX'] == '256'
        assert pixels[0].attrib['SizeY'] == '512'
        assert pixels[0].attrib['SizeZ'] == '1'
        assert pixels[0].attrib['SizeC'] == '2'
        assert pixels[0].attrib['SizeT'] == '2'
        assert pixels[0].attrib['DimensionOrder'] == 'XYZTC'
        assert pixels[0].attrib['Type'] == 'uint16'
        planes = pixels[0].findall('OME:Plane', namespaces=NS)
        assert len(planes) == 4
        assert planes[0].attrib['TheZ'] == '0'
        assert planes[0].attrib['TheC'] == '0'
        assert planes[0].attrib['TheT'] == '0'
        assert planes[1].attrib['TheZ'] == '0'
        assert planes[1].attrib['TheC'] == '0'
        assert planes[1].attrib['TheT'] == '1'
        assert planes[2].attrib['TheZ'] == '0'
        assert planes[2].attrib['TheC'] == '1'
        assert planes[2].attrib['TheT'] == '0'
        assert planes[3].attrib['TheZ'] == '0'
        assert planes[3].attrib['TheC'] == '1'
        assert planes[3].attrib['TheT'] == '1'
Exemplo n.º 14
0
    def test_multiple_plates_one_per_file(self, tmpdir):

        files = [str(tmpdir.join('%s.companion.ome' % i)) for i in range(4)]
        for i in range(4):
            p = Plate("test %s" % i, 1, 1)
            well = p.add_well(0, 0)
            img = Image("test %s" % i, 256, 512, 3, 4, 5)
            well.add_wellsample(0, img)
            create_companion(plates=[p], out=files[i])

        for i in range(4):
            root = ElementTree.parse(files[i]).getroot()
            plates = root.findall('OME:Plate', namespaces=NS)
            assert len(plates) == 1
            assert plates[0].attrib['Name'] == 'test %s' % i
            wells = plates[0].findall('OME:Well', namespaces=NS)
            assert len(wells) == 1
            wellsamples = wells[0].findall('OME:WellSample', namespaces=NS)
            assert len(wellsamples) == 1
            imagerefs = wellsamples[0].findall('OME:ImageRef', namespaces=NS)
            assert len(imagerefs) == 1
            images = root.findall('OME:Image', namespaces=NS)
            assert len(images) == 1
            assert images[0].attrib['Name'] == 'test %s' % i
                      size_z,
                      2,
                      1,
                      order="XYZCT",
                      type="uint16")
        image.data['Pixels']['PhysicalSizeX'] = '20'
        image.data['Pixels']['PhysicalSizeXUnit'] = 'nm'
        image.data['Pixels']['PhysicalSizeY'] = '20'
        image.data['Pixels']['PhysicalSizeYUnit'] = 'nm'
        (n1, c1, n2, c2) = CHANNEL_MAPPING[os.path.basename(folder)]
        image.add_channel(n1, c1)
        image.add_channel(n2, c2)

        for i in range(len(rawtiffs)):
            image.add_tiff("%s/%s" % (os.path.basename(cell), rawtiffs[i]),
                           c=i,
                           z=0,
                           t=0,
                           ifd=0,
                           planeCount=size_z)
        create_companion(images=[image], out=cell + '.companion.ome')

        # Generate indented XML for readability
        proc = subprocess.Popen([
            'xmllint', '--format', '-o', cell + '.companion.ome',
            cell + '.companion.ome'
        ],
                                stdin=subprocess.PIPE,
                                stdout=subprocess.PIPE)
        (output, error_output) = proc.communicate()
                          1552,
                          25,
                          3,
                          len(timepoints),
                          order="XYZTC",
                          type="uint8")
            image.add_channel(samplesPerPixel=3)
            for i, timepoint in enumerate(timepoints):
                filename = "{}_{}{}_{}.tiff".format(plate_name, row, column,
                                                    timepoint)
                image.add_tiff(filename, c=0, z=0, t=i, planeCount=25)
                options = {
                    'DeltaT': timepoint[:-1],
                    'DeltaTUnit': 'h',
                }
                for z in range(25):
                    image.add_plane(c=0, z=z, t=i, options=options)
            well.add_wellsample(well_index, image)
            well_index += 1
companion_file = "../companions/{}.companion.ome".format(plate_name)
create_companion(plates=[plate], out=companion_file)

# Indent XML for readability
proc = subprocess.Popen(
    ['xmllint', '--format', '-o', companion_file, companion_file],
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE)
(output, error_output) = proc.communicate()

print("Done.")