Beispiel #1
0
    def test_write_tileset(self):
        image = slicedimage.TileSet(
            [DimensionNames.X, DimensionNames.Y, "ch", "hyb"],
            {
                'ch': 2,
                'hyb': 2
            },
            {
                DimensionNames.Y: 120,
                DimensionNames.X: 80
            },
        )

        for hyb in range(2):
            for ch in range(2):
                tile = slicedimage.Tile(
                    {
                        DimensionNames.X: (0.0, 0.01),
                        DimensionNames.Y: (0.0, 0.01),
                    },
                    {
                        'hyb': hyb,
                        'ch': ch,
                    },
                )
                tile.numpy_array = np.zeros((120, 80))
                tile.numpy_array[hyb, ch] = 1
                image.add_tile(tile)

        with tempfile.TemporaryDirectory() as tempdir:
            with tempfile.NamedTemporaryFile(suffix=".json",
                                             dir=tempdir,
                                             delete=False) as partition_file:
                partition_file_path = Path(partition_file.name)
                partition_doc = slicedimage.v0_0_0.Writer(
                ).generate_partition_document(image,
                                              partition_file_path.as_uri())
                writer = codecs.getwriter("utf-8")
                json.dump(partition_doc, writer(partition_file))

            loaded = slicedimage.Reader.parse_doc(
                partition_file_path.name, partition_file_path.parent.as_uri())

            for hyb in range(2):
                for ch in range(2):
                    tiles = [
                        _tile
                        for _tile in loaded.tiles(lambda tile: (tile.indices[
                            'hyb'] == hyb and tile.indices['ch'] == ch))
                    ]

                    self.assertEqual(len(tiles), 1)

                    expected = np.zeros((100, 100))
                    expected[hyb, ch] = 1

                    self.assertEqual(tiles[0].numpy_array.all(),
                                     expected.all())
                    self.assertIsNotNone(tiles[0].sha256)
    def test_tileset_without_shapes(self):
        image = slicedimage.TileSet(
            [DimensionNames.X, DimensionNames.Y, "ch", "hyb"],
            {'ch': 2, 'hyb': 2},
        )

        for hyb in range(2):
            for ch in range(2):
                tile = slicedimage.Tile(
                    {
                        DimensionNames.X: (0.0, 0.01),
                        DimensionNames.Y: (0.0, 0.01),
                    },
                    {
                        'hyb': hyb,
                        'ch': ch,
                    },
                )
                tile.numpy_array = np.zeros((120, 80))
                tile.numpy_array[hyb, ch] = 1
                image.add_tile(tile)

        with tempfile.TemporaryDirectory() as tempdir:
            with tempfile.NamedTemporaryFile(
                    suffix=".json", dir=tempdir, delete=False) as partition_file:
                partition_file_path = Path(partition_file.name)
                partition_doc = slicedimage.v0_0_0.Writer().generate_partition_document(
                    image, partition_file_path.as_uri())

                # remove the shape information from the tiles.
                for tile in partition_doc[TileSetKeys.TILES]:
                    del tile[TileKeys.TILE_SHAPE]

                writer = codecs.getwriter("utf-8")
                json.dump(partition_doc, writer(partition_file))

            loaded = slicedimage.Reader.parse_doc(
                partition_file_path.name, partition_file_path.parent.as_uri())

            for hyb in range(2):
                for ch in range(2):
                    tiles = [_tile
                             for _tile in loaded.tiles(
                                 lambda tile: (
                                     tile.indices['hyb'] == hyb
                                     and tile.indices['ch'] == ch))]

                    self.assertEqual(len(tiles), 1)
                    with warnings.catch_warnings(record=True) as w:
                        # Cause all warnings to always be triggered.  Duplicate warnings are
                        # normally suppressed.
                        warnings.simplefilter("always")

                        tile_shape = tiles[0].tile_shape

                        self.assertEqual(tile_shape, {DimensionNames.Y: 120, DimensionNames.X: 80})
                        self.assertEqual(len(w), 1)
                        self.assertIn("Decoding tile just to obtain shape", str(w[0].message))
Beispiel #3
0
    def test_write_png(self):
        image = slicedimage.TileSet(
            dimensions=[DimensionNames.X, DimensionNames.Y, "ch", "hyb"],
            shape={'ch': 2, 'hyb': 2},
            default_tile_shape={DimensionNames.Y: 120, DimensionNames.X: 80},
        )

        for hyb in range(2):
            for ch in range(2):
                tile = slicedimage.Tile(
                    coordinates={
                        DimensionNames.X: (0.0, 0.01),
                        DimensionNames.Y: (0.0, 0.01),
                    },
                    indices={
                        'hyb': hyb,
                        'ch': ch,
                    },
                )
                tile.numpy_array = np.zeros((120, 80), dtype=np.uint32)
                tile.numpy_array[hyb, ch] = 1
                image.add_tile(tile)

        with tempfile.TemporaryDirectory() as tempdir:
            with tempfile.NamedTemporaryFile(
                    suffix=".json", dir=tempdir, delete=False) as partition_file:
                # create the tileset and save it.
                partition_file_path = Path(partition_file.name)
                partition_doc = slicedimage.v0_0_0.Writer().generate_partition_document(
                    image, partition_file_path.as_uri(), tile_format=ImageFormat.PNG)
                writer = codecs.getwriter("utf-8")
                json.dump(partition_doc, writer(partition_file))

            # construct a URL to the tileset we wrote, and load the tileset.
            loaded = slicedimage.Reader.parse_doc(
                partition_file_path.name, partition_file_path.parent.as_uri())

            # compare the tiles we loaded to the tiles we set up.
            for hyb in range(2):
                for ch in range(2):
                    tiles = [_tile
                             for _tile in loaded.tiles(
                                 lambda tile: (
                                     tile.indices['hyb'] == hyb
                                     and tile.indices['ch'] == ch))]

                    self.assertEqual(len(tiles), 1)

                    expected = np.zeros((120, 80), dtype=np.uint32)
                    expected[hyb, ch] = 1

                    self.assertEqual(tiles[0].numpy_array.all(), expected.all())
                    self.assertIsNotNone(tiles[0].sha256)
Beispiel #4
0
    def test_write_collection(self):
        image = slicedimage.TileSet(
            ["x", "y", "ch", "hyb"],
            {'ch': 2, 'hyb': 2},
            (100, 100),
        )

        for hyb in range(2):
            for ch in range(2):
                tile = slicedimage.Tile(
                    {
                        'x': (0.0, 0.01),
                        'y': (0.0, 0.01),
                    },
                    {
                        'hyb': hyb,
                        'ch': ch,
                    },
                )
                tile.numpy_array = numpy.zeros((100, 100))
                tile.numpy_array[hyb, ch] = 1
                image.add_tile(tile)
        collection = slicedimage.Collection()
        collection.add_partition("fov002", image)

        with TemporaryDirectory() as tempdir, \
                tempfile.NamedTemporaryFile(suffix=".json", dir=tempdir) as partition_file:
            partition_doc = slicedimage.v0_0_0.Writer().generate_partition_document(
                collection, partition_file.name)
            writer = codecs.getwriter("utf-8")
            json.dump(partition_doc, writer(partition_file))
            partition_file.flush()

            basename = os.path.basename(partition_file.name)
            baseurl = "file://{}".format(os.path.dirname(partition_file.name))

            loaded = slicedimage.Reader.parse_doc(basename, baseurl)

            for hyb in range(2):
                for ch in range(2):
                    tiles = [_tile
                             for _tile in loaded.tiles(
                                 lambda tile: (tile.indices['hyb'] == hyb and
                                               tile.indices['ch'] == ch))]

                    self.assertEqual(len(tiles), 1)

                    expected = numpy.zeros((100, 100))
                    expected[hyb, ch] = 1

                    self.assertEqual(tiles[0].numpy_array.all(), expected.all())
                    self.assertIsNotNone(tiles[0].sha256)
Beispiel #5
0
    def test_numpy(self):
        """
        Generate a tileset consisting of a single TIFF tile, and then read it.
        """
        image = slicedimage.TileSet(
            [DimensionNames.X, DimensionNames.Y, "ch", "hyb"],
            {
                'ch': 1,
                'hyb': 1
            },
            {
                DimensionNames.Y: 120,
                DimensionNames.X: 80
            },
        )

        tile = slicedimage.Tile(
            {
                DimensionNames.X: (0.0, 0.01),
                DimensionNames.Y: (0.0, 0.01),
            },
            {
                'hyb': 0,
                'ch': 0,
            },
        )
        tile.numpy_array = np.random.randint(0,
                                             65535,
                                             size=(120, 80),
                                             dtype=np.uint16)
        image.add_tile(tile)

        with tempfile.TemporaryDirectory() as tempdir:
            tempdir_path = Path(tempdir)
            tileset_path = tempdir_path / "tileset.json"
            partition_doc = slicedimage.v0_0_0.Writer(
            ).generate_partition_document(image, (tempdir_path /
                                                  "tileset.json").as_uri())
            with open(fspath(tileset_path), "w") as fh:
                json.dump(partition_doc, fh)

            result = slicedimage.Reader.parse_doc("tileset.json",
                                                  tempdir_path.as_uri())

            self.assertTrue(
                np.array_equal(
                    list(result.tiles())[0].numpy_array, tile.numpy_array))
Beispiel #6
0
    def test_numpy(self):
        """
        Generate a tileset consisting of a single NUMPY tile.  Deposit it where the HTTP server can
        find the tileset, and fetch it.
        """
        image = slicedimage.TileSet(
            ["x", "y", "ch", "hyb"],
            {'ch': 1, 'hyb': 1},
            (100, 100),
        )

        tile = slicedimage.Tile(
            {
                'x': (0.0, 0.01),
                'y': (0.0, 0.01),
            },
            {
                'hyb': 0,
                'ch': 0,
            },
        )
        tile.numpy_array = np.random.randint(0, 65535, size=(100, 100), dtype=np.uint16)
        image.add_tile(tile)

        partition_path = os.path.join(self.tempdir.name, "tileset.json")
        partition_doc = slicedimage.v0_0_0.Writer().generate_partition_document(
            image, partition_path)
        with open(partition_path, "w") as fh:
            json.dump(partition_doc, fh)

        result = slicedimage.Reader.parse_doc(
            "tileset.json",
            "http://localhost:{port}/".format(port=self.port),
            allow_caching=False,
        )

        self.assertTrue(np.array_equal(list(result.tiles())[0].numpy_array, tile.numpy_array))
Beispiel #7
0
    def test_multi_directory_write_collection(self):
        """Test that we can write collections with a directory hierarchy."""
        image = slicedimage.TileSet(
            ["x", "y", "ch", "hyb"],
            {'ch': 2, 'hyb': 2},
            {'y': 120, 'x': 80},
        )

        for hyb in range(2):
            for ch in range(2):
                tile = slicedimage.Tile(
                    {
                        'x': (0.0, 0.01),
                        'y': (0.0, 0.01),
                    },
                    {
                        'hyb': hyb,
                        'ch': ch,
                    },
                )
                tile.numpy_array = np.zeros((120, 80))
                tile.numpy_array[hyb, ch] = 1
                image.add_tile(tile)
        collection = slicedimage.Collection()
        collection.add_partition("fov002", image)

        def partition_path_generator(parent_toc_path, toc_name):
            directory = parent_toc_path.parent / toc_name
            directory.mkdir()
            return directory / "{}.json".format(parent_toc_path.stem)

        def tile_opener(tileset_path, tile, ext):
            directory_path = tempfile.mkdtemp(dir=str(tileset_path.parent))
            return tempfile.NamedTemporaryFile(
                suffix=".{}".format(ext),
                prefix="{}-".format(tileset_path.stem),
                dir=directory_path,
                delete=False,
            )

        with tempfile.TemporaryDirectory() as tempdir:
            with tempfile.NamedTemporaryFile(
                    suffix=".json", dir=tempdir, delete=False) as partition_file:
                partition_file_path = Path(partition_file.name)
                partition_doc = slicedimage.v0_0_0.Writer().generate_partition_document(
                    collection, partition_file_path.as_uri(),
                    partition_path_generator=partition_path_generator,
                    tile_opener=tile_opener,
                )
                writer = codecs.getwriter("utf-8")
                json.dump(partition_doc, writer(partition_file))

            loaded = slicedimage.Reader.parse_doc(
                partition_file_path.name, partition_file_path.parent.as_uri())

            for hyb in range(2):
                for ch in range(2):
                    tiles = [
                        _tile
                        for _tile in loaded.tiles(
                            lambda tile: (
                                tile.indices['hyb'] == hyb
                                and tile.indices['ch'] == ch))]

                    self.assertEqual(len(tiles), 1)

                    expected = np.zeros((100, 100))
                    expected[hyb, ch] = 1

                    self.assertEqual(tiles[0].numpy_array.all(), expected.all())
                    self.assertIsNotNone(tiles[0].sha256)