File size: 2,871 Bytes
12d2e9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""
Copyright 2021, Dana-Farber Cancer Institute and Weill Cornell Medicine
License: GNU GPL 2.0
"""

import reprlib
from collections import OrderedDict

import pathml.core.h5managers
import pathml.core.masks
import pathml.core.tile


class Tiles:
    """
    Object wrapping a dict of tiles.

    Args:
        tiles (Union[dict[tuple[int], `~pathml.core.tiles.Tile`], list[`~pathml.core.tiles.Tile`]]): tile objects
    """

    def __init__(self, h5manager, tiles=None):
        assert isinstance(h5manager, pathml.core.h5managers.h5pathManager)
        self.h5manager = h5manager
        # if tiles are supplied, add them to the h5manager
        if tiles:
            assert isinstance(tiles, list) and all(
                [isinstance(tile, pathml.core.Tile) for tile in tiles]
            ), f"tiles are of type {reprlib.repr([type(t) for t in tiles])} but must all be pathml.core.Tile"

            tiledictionary = {}
            for tile in tiles:
                if not isinstance(tile, pathml.core.Tile):
                    raise ValueError(
                        f"Tiles expects a list of type Tile but was given {type(tile)}"
                    )
                if tile.coords is None:
                    raise ValueError("tiles must contain valid coords")
                coords = tile.coords
                tiledictionary[coords] = tile
            self._tiles = OrderedDict(tiledictionary)

            # add tiles in _tiles to h5manager
            for key, val in self._tiles.items():
                self.h5manager.add_tile(val)
            del self._tiles

    @property
    def tile_shape(self):
        return self.h5manager.h5["tiles"].attrs["tile_shape"]

    @property
    def keys(self):
        return list(self.h5manager.h5["tiles"].keys())

    def __repr__(self):
        rep = f"{len(self.h5manager.h5['tiles'])} tiles: {reprlib.repr(list(self.h5manager.h5['tiles'].keys()))}"
        return rep

    def __len__(self):
        return len(self.h5manager.h5["tiles"].keys())

    def __getitem__(self, item):
        return self.h5manager.get_tile(item)

    def add(self, tile):
        """
        Add tile indexed by tile.coords to tiles.

        Args:
            tile(Tile): tile object
        """
        if not isinstance(tile, pathml.core.tile.Tile):
            raise ValueError(
                f"can not add {type(tile)}, tile must be of type pathml.core.tiles.Tile"
            )
        self.h5manager.add_tile(tile)

    def update(self, tile):
        """
        Update a tile.

        Args:
            tile(pathml.core.tile.Tiles): key of tile to be updated
        """
        self.h5manager.add_tile(tile)

    def remove(self, key):
        """
        Remove tile from tiles.

        Args:
            key(str): key (coords) indicating tile to be removed
        """
        self.h5manager.remove_tile(key)