Skip to content

ratiopath.tiling.annotations

annotations_intersection(tree, roi)

Get the intersection of annotations with the region of interest (ROI).

Parameters:

Name Type Description Default
tree STRtree

The spatial index of annotation geometries.

required
roi BaseGeometry

The region of interest geometry.

required

Returns:

Name Type Description
BaseGeometry BaseGeometry

The intersection of annotations with the ROI.

Source code in ratiopath/tiling/annotations.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def annotations_intersection(tree: STRtree, roi: BaseGeometry) -> BaseGeometry:
    """Get the intersection of annotations with the region of interest (ROI).

    Args:
        tree: The spatial index of annotation geometries.
        roi: The region of interest geometry.

    Returns:
        BaseGeometry: The intersection of annotations with the ROI.
    """
    intersection = Polygon()
    for polygon_index in tree.query(roi, predicate="intersects"):
        intersection = intersection.union(
            tree.geometries[int(polygon_index)].intersection(roi)
        )
    return intersection

shift_roi(roi, x, y, downsample)

Shift the region of interest (ROI) for a specific tile.

Parameters:

Name Type Description Default
roi BaseGeometry

The current region of interest geometry.

required
x int

The x-coordinate of the tile to shift the ROI for.

required
y int

The y-coordinate of the tile to shift the ROI for.

required
downsample float

The downsampling factor.

required

Returns:

Name Type Description
BaseGeometry BaseGeometry

The shifted region of interest geometry.

Source code in ratiopath/tiling/annotations.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
def shift_roi(roi: BaseGeometry, x: int, y: int, downsample: float) -> BaseGeometry:
    """Shift the region of interest (ROI) for a specific tile.

    Args:
        roi: The current region of interest geometry.
        x: The x-coordinate of the tile to shift the ROI for.
        y: The y-coordinate of the tile to shift the ROI for.
        downsample: The downsampling factor.

    Returns:
        BaseGeometry: The shifted region of interest geometry.
    """
    return transform(roi, partial(np.add, np.array([x * downsample, y * downsample])))

tile_annotations(annotations, roi, x, y, downsample)

Yield annotated tiles from the annotation tree.

Annotations are assumed to be at level 0. Yielded geometries are transformed to the same level as the ROI and tiles.

Parameters:

Name Type Description Default
annotations list[BaseGeometry]

The list of annotation geometries.

required
roi BaseGeometry

The region of interest geometry.

required
x list[int]

The x-coordinates of the tiles.

required
y list[int]

The y-coordinates of the tiles.

required
downsample float

The downsampling factor.

required

Yields:

Name Type Description
BaseGeometry BaseGeometry

The annotated tile geometry.

Source code in ratiopath/tiling/annotations.py
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
def tile_annotations(
    annotations: list[BaseGeometry],
    roi: BaseGeometry,
    x: list[int],
    y: list[int],
    downsample: float,
) -> Iterator[BaseGeometry]:
    """Yield annotated tiles from the annotation tree.

    Annotations are assumed to be at level 0. Yielded geometries are transformed to the
    same level as the ROI and tiles.

    Args:
        annotations: The list of annotation geometries.
        roi: The region of interest geometry.
        x: The x-coordinates of the tiles.
        y: The y-coordinates of the tiles.
        downsample: The downsampling factor.

    Yields:
        BaseGeometry: The annotated tile geometry.
    """
    tree = STRtree(annotations)
    # transform roi to level 0
    roi = transform(roi, lambda geom: geom * downsample)

    for tile_x, tile_y in zip(x, y, strict=True):
        shifted_roi = shift_roi(roi, tile_x, tile_y, downsample)
        intersection = annotations_intersection(tree, shifted_roi)
        yield transform(intersection, lambda geom: geom / downsample)