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
    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 test_invalid_plane_index(self, tmpdir, invalidindex):

        i = Image("test", 512, 512, 1, 1, 1)
        with pytest.raises(AssertionError):
            i.add_plane(z=invalidindex, c=0, t=0)
        with pytest.raises(AssertionError):
            i.add_plane(z=0, c=invalidindex, t=0)
        with pytest.raises(AssertionError):
            i.add_plane(z=0, c=0, t=invalidindex)
    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'
Exemple #5
0
def create_phenotypic_companions():
    """Phenotypic metadata files"""

    logging.info("Generating phenotypic metadata files")
    time_indexes = read_phenotypic_time_indexes()
    timestamps = read_phenotypic_timestamps()
    slides = parse_phenotypic_filenames(timestamps)
    filePaths = []

    for slide in slides:
        images = []
        logging.debug("Processing phenotypic %s" % slide)
        # Genotypic metadata generation
        for position in sorted(slides[slide]):
            logging.debug("  Creating image for %s" % position)
            image = Image(position,
                          2048,
                          879,
                          1,
                          2,
                          481,
                          order="XYZCT",
                          type="uint16")
            image.add_channel("Phase", -1)
            image.add_channel("Fluorescence", 16711935)

            # Phase images
            for t in range(481):
                relative_path = get_phenotypic_filename(position, "phase", t)
                full_path = get_phenotypic_filename(position,
                                                    "phase",
                                                    t,
                                                    slide=slide)
                image.add_tiff(relative_path, c=0, z=0, t=t, planeCount=1)
                plane_options = {
                    "DeltaT": timestamps[full_path],
                    "DeltaTUnit": "s",
                    "ExposureTime": "20",
                    "ExposureTimeUnit": "ms",
                }
                image.add_plane(c=0, z=0, t=t, options=plane_options)

            # Fluorescence images
            for i in range(241):
                relative_path = get_phenotypic_filename(position, "fluor", i)
                full_path = get_phenotypic_filename(position,
                                                    "fluor",
                                                    i,
                                                    slide=slide)
                t = time_indexes[full_path]
                plane_options = {
                    "DeltaT": timestamps[full_path],
                    "DeltaTUnit": "s",
                    "ExposureTime": "300",
                    "ExposureTimeUnit": "ms",
                }
                if t == 2 * i:
                    image.add_tiff(relative_path, c=1, z=0, t=t, planeCount=1)
                    if i != 240:
                        image.add_tiff(None, c=1, z=0, t=(2 * i) + 1)
                elif t == (2 * i) + 1:
                    image.add_tiff(None, c=1, z=0, t=2 * i)
                    if t != 481:
                        image.add_tiff(relative_path,
                                       c=1,
                                       z=0,
                                       t=t,
                                       planeCount=1)
                else:
                    raise Exception("Invalid mapping")
                image.add_plane(c=1, z=0, t=t, options=plane_options)
            images.append(image)

            position_dir = join(EXPERIMENTA_DIRECTORY, 'companions', slide,
                                "pheno", position)
            if not os.path.lexists(position_dir):
                os.symlink(join(DATA_DIRECTORY, slide, "pheno", position),
                           position_dir)

        companion_file = join(EXPERIMENTA_DIRECTORY, 'companions', slide,
                              "pheno", slide + '.companion.ome')
        write_companion(images, companion_file)

        filePaths.append("Project:name:idr0065-camsund-crispri/experimentA/"
                         "Dataset:name:%s\t"
                         "%s\t%s\n" % (slide, companion_file, slide))

    tsv = join(EXPERIMENTA_DIRECTORY, 'idr0065-experimentA-filePaths.tsv')
    with open(tsv, 'w') as f:
        for p in filePaths:
            f.write(p)
    return slides
                          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.")