Example #1
0
def test_roundtrip(xml, benchmark):
    """Ensure we can losslessly round-trip XML through the model and back."""
    xml = str(xml)
    schema = get_schema(xml)

    def canonicalize(xml, strip_empty):
        d = schema.decode(xml, use_defaults=True)
        # Strip extra whitespace in the schemaLocation value.
        d["@xsi:schemaLocation"] = re.sub(r"\s+", " ",
                                          d["@xsi:schemaLocation"])
        root = schema.encode(d, path=NS_OME + "OME", use_defaults=True)
        # These are the tags that appear in the example files with empty
        # content. Since our round-trip will drop empty elements, we'll need to
        # strip them from the "original" documents before comparison.
        if strip_empty:
            for tag in ("Description", "LightPath", "Map"):
                for e in root.findall(f".//{NS_OME}{tag}[.='']..."):
                    e.remove(e.find(f"{NS_OME}{tag}"))
        # ET.canonicalize can't handle an empty namespace so we need to
        # re-register the OME namespace with an actual name before calling
        # tostring.
        ElementTree.register_namespace("ome", URI_OME)
        xml_out = ElementTree.tostring(root, "unicode")
        xml_out = util.canonicalize(xml_out, strip_text=True)
        xml_out = minidom.parseString(xml_out).toprettyxml(indent="  ")
        return xml_out

    original = canonicalize(xml, True)
    ome = from_xml(xml)
    rexml = benchmark(to_xml, ome)
    assert canonicalize(rexml, False) == original
Example #2
0
    def _get_dim_info(self) -> None:
        if self._shape:
            if self.tf.ome_metadata:
                self.ome_metadata = from_xml(self.tf.ome_metadata)
                spp = (self.ome_metadata.images[
                    self.largest_series].pixels.channels[0].samples_per_pixel)
                interleaved = self.ome_metadata.images[
                    self.largest_series].pixels.interleaved

                if spp and spp > 1:
                    self._is_rgb = True
                else:
                    self._is_rgb = False

                if guess_rgb(self._shape) is False:
                    self._channel_axis = 0
                    self._is_interleaved = False
                elif interleaved and guess_rgb(self._shape):
                    self._is_interleaved = True
                    self._channel_axis = len(self._shape) - 1

            else:
                self._is_rgb = guess_rgb(self._shape)
                self._is_interleaved = self._is_rgb
                if self._is_rgb:
                    self._channel_axis = len(self._shape) - 1
                else:
                    self._channel_axis = 0

            self._n_ch = self._shape[self._channel_axis]
Example #3
0
def test_serialization(xml):
    """Test pickle serialization and reserialization."""
    if true_stem(xml) in SHOULD_RAISE_READ:
        pytest.skip("Can't pickle unreadable xml")

    ome = from_xml(xml)
    serialized = pickle.dumps(ome)
    deserialized = pickle.loads(serialized)
    assert ome == deserialized
Example #4
0
def test_to_xml_with_kwargs():
    """Ensure kwargs are passed to ElementTree"""
    ome = from_xml(Path(__file__).parent / "data" / "example.ome.xml")

    with mock.patch("xml.etree.ElementTree.tostring") as mocked_et_tostring:
        element = to_xml_element(ome)
        # Use an ElementTree.tostring kwarg and assert that it was passed through
        to_xml(element, xml_declaration=True)
        assert mocked_et_tostring.call_args.xml_declaration
Example #5
0
def test_wsireg_run_reg_downsampling_m1m2_merge_ds_attach(
        data_out_dir, disk_im_gry):
    wsi_reg = WsiReg2D(gen_project_name_str(), str(data_out_dir))
    img_fp1 = str(disk_im_gry)

    wsi_reg.add_modality(
        "mod1",
        img_fp1,
        0.65,
        channel_names=["test"],
        channel_colors=["red"],
        preprocessing={"downsampling": 2},
    )

    wsi_reg.add_modality(
        "mod2",
        img_fp1,
        0.65,
        channel_names=["test"],
        channel_colors=["red"],
        preprocessing={"downsampling": 2},
    )

    wsi_reg.add_attachment_images(
        "mod2",
        "mod3",
        img_fp1,
        0.65,
        channel_names=["test"],
        channel_colors=["red"],
    )
    wsi_reg.add_reg_path("mod1", "mod2", reg_params=["rigid_test"])

    wsi_reg.add_merge_modalities("mod12-merge", ["mod1", "mod2", "mod3"])
    wsi_reg.register_images()

    im_fps = wsi_reg.transform_images(transform_non_reg=False,
                                      remove_merged=True)
    regim = reg_image_loader(im_fps[0], 0.65)
    ome_data = from_xml(TiffFile(im_fps[0]).ome_metadata)

    assert regim.shape == (3, 2048, 2048)
    assert ome_data.images[0].pixels.physical_size_x == 0.65
    assert ome_data.images[0].pixels.physical_size_y == 0.65
    assert ome_data.images[0].pixels.size_x == 2048
    assert ome_data.images[0].pixels.size_y == 2048
    assert ome_data.images[0].pixels.size_c == 3
Example #6
0
def getMetadata(path):
    with tifffile.TiffFile(path) as tif:
        metadata = ome_types.from_xml(tif.ome_metadata)
        return metadata.images[0].pixels
Example #7
0
def test_with_ome_ns():
    xml = Path(__file__).parent / "data" / "ome_ns.ome.xml"
    ome = from_xml(xml)
    assert ome.experimenters
Example #8
0
def test_refs():
    xml = Path(
        __file__).parent / "data" / "two-screens-two-plates-four-wells.ome.xml"
    ome = from_xml(xml)
    assert ome.screens[0].plate_ref[0].ref is ome.plates[0]