Skip to content

get_interfaces

Get the interfaces b/w the polygons defined in a areas and buffer_area.

Note

Identifies the interface regions between polygons in areas and in buffer_area by buffering the buffer_area polygons and finding their intersections with areas. The width of the interface is controlled by the buffer_dist parameter.

Parameters:

Name Type Description Default
buffer_area GeoDataFrame

The area or region of interest that is buffered on top of polygons in gdf.

required
areas GeoDataFrame

A geodataframe containing polygons (tissue areas) that might intersect with the buffer_area.

required
buffer_dist int

The radius (in pixels) of the buffer.

200

Returns:

Type Description
GeoDataFrame

gpd.GeoDataFrame: A geodataframe containing the intersecting polygons including the buffer.

Examples:

>>> from histolytics.spatial_ops import get_interfaces
>>> from histolytics.data import cervix_tissue
>>> # load the tissue data
>>> tis = cervix_tissue()
>>>
>>> # get the stromal and CIN tissue
>>> stroma = tis[tis["class_name"] == "stroma"]
>>> cin_tissue = tis[tis["class_name"] == "cin"]
>>>
>>> # get the interface between cin lesion and stroma
>>> interface = get_interfaces(cin_tissue, stroma, buffer_dist=300)
>>> print(interface.head(3))
    class_name                                           geometry
0     stroma  POLYGON ((1588.71 4829.03, 1591.18 4828.83, 15...
1     stroma  POLYGON ((743.07 5587.48, 743.63 5589, 744.07 ...
2     stroma  POLYGON ((1151 7566, 1150 7567, 1148.2 7568.2,...
>>> # plot the tissue and the interface
>>> ax = tis.plot(column="class_name", figsize=(5, 5), aspect=1, alpha=0.5)
>>> interface.plot(ax=ax, color="blue", lw=1, alpha=0.3)
>>> ax.set_axis_off()

out

Source code in src/histolytics/spatial_ops/ops.py
def get_interfaces(
    buffer_area: gpd.GeoDataFrame, areas: gpd.GeoDataFrame, buffer_dist: int = 200
) -> gpd.GeoDataFrame:
    """Get the interfaces b/w the polygons defined in a `areas` and `buffer_area`.

    Note:
        Identifies the interface regions between polygons in `areas` and in `buffer_area`
        by buffering the `buffer_area` polygons and finding their intersections with `areas`.
        The width of the interface is controlled by the `buffer_dist` parameter.

    Parameters:
        buffer_area (gpd.GeoDataFrame):
            The area or region of interest that is buffered on top of polygons in gdf.
        areas (gpd.GeoDataFrame):
            A geodataframe containing polygons (tissue areas) that might intersect
            with the `buffer_area`.
        buffer_dist (int):
            The radius (in pixels) of the buffer.

    Returns:
        gpd.GeoDataFrame:
            A geodataframe containing the intersecting polygons including the buffer.

    Examples:
        >>> from histolytics.spatial_ops import get_interfaces
        >>> from histolytics.data import cervix_tissue
        >>> # load the tissue data
        >>> tis = cervix_tissue()
        >>>
        >>> # get the stromal and CIN tissue
        >>> stroma = tis[tis["class_name"] == "stroma"]
        >>> cin_tissue = tis[tis["class_name"] == "cin"]
        >>>
        >>> # get the interface between cin lesion and stroma
        >>> interface = get_interfaces(cin_tissue, stroma, buffer_dist=300)
        >>> print(interface.head(3))
            class_name                                           geometry
        0     stroma  POLYGON ((1588.71 4829.03, 1591.18 4828.83, 15...
        1     stroma  POLYGON ((743.07 5587.48, 743.63 5589, 744.07 ...
        2     stroma  POLYGON ((1151 7566, 1150 7567, 1148.2 7568.2,...
        >>> # plot the tissue and the interface
        >>> ax = tis.plot(column="class_name", figsize=(5, 5), aspect=1, alpha=0.5)
        >>> interface.plot(ax=ax, color="blue", lw=1, alpha=0.3)
        >>> ax.set_axis_off()
    ![out](../../img/interfaces.png)
    """
    buffer_area = set_crs(buffer_area)
    areas = set_crs(areas)

    buffer_zone = gpd.GeoDataFrame(
        {"geometry": list(buffer_area.buffer(buffer_dist))},
        crs=buffer_area.crs,
    )
    inter = areas.overlay(buffer_zone, how="intersection")

    # if the intersecting area is covered totally by any polygon in the `areas` gdf
    # take the difference of the intresecting area and the orig roi to discard
    # the roi from the interface 'sheet'
    if not inter.empty:
        if areas.covers(inter.geometry.loc[0]).any():  # len(inter) == 1
            inter = inter.overlay(buffer_area, how="difference", keep_geom_type=True)

    return inter.dissolve().explode().reset_index(drop=True)