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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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, coordinate, 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
coordinate NDArray[int64]

The coordinates 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
22
def shift_roi(
    roi: BaseGeometry, coordinate: NDArray[np.int64], downsample: float
) -> BaseGeometry:
    """Shift the region of interest (ROI) for a specific tile.

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

    Returns:
        BaseGeometry: The shifted region of interest geometry.
    """
    return transform(roi, lambda r: r + coordinate * downsample)

tile_annotations(annotations, roi, coordinates, 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 Iterable[BaseGeometry]

The list of annotation geometries.

required
roi BaseGeometry

The region of interest geometry.

required
coordinates Iterable[NDArray[int64]]

The iterable of 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
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
def tile_annotations(
    annotations: Iterable[BaseGeometry],
    roi: BaseGeometry,
    coordinates: Iterable[NDArray[np.int64]],
    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.
        coordinates: The iterable of 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 coordinate in coordinates:
        shifted_roi = shift_roi(roi, coordinate, downsample)
        intersection = annotations_intersection(tree, shifted_roi)
        yield transform(intersection, lambda geom: geom / downsample)