Skip to content

grid_overlay

cellseg_gsontools.grid.grid_overlay(gdf, patch_size=(256, 256), stride=(256, 256), pad=20, predicate='intersects')

Overlay a square grid to the given areas of a geopandas.GeoDataFrame.

Note

Returns None if the gdf is empty.

Parameters:

Name Type Description Default
gdf GeoDataFrame

GeoDataFrame to fit the grid to. Uses the bounding box of the GeoDataFrame to fit the grid.

required
patch_size Tuple[int, int]

Patch size of the grid.

(256, 256)
stride Tuple[int, int]

Stride of the sliding window in the grid.

(256, 256)
pad int

Pad the bounding box with the given number of pixels, by default None.

20
predicate str

Predicate to use for the spatial join, by default "intersects". Allowed values are "intersects" and "within".

'intersects'

Returns:

Type Description
GeoDataFrame

gpd.GeoDataFrame: GeoDataFrame with the grid fitted to the given GeoDataFrame.

Raises:

Type Description
ValueError

If predicate is not one of "intersects" or "within".

Examples:

Fit a square grid to a gdf:

>>> from cellseg_gsontools import read_gdf
>>> from cellseg_gsontools.grid import grid_overlay
>>> # Read in the tissue areas
>>> area_gdf = gpd.read_file("path/to/area.geojson")
>>> # Fit the grid
>>> sq_grid = grid_overlay(area_gdf, patch_size=(256, 256), stride=(256, 256))
>>> sq_grid
gpd.GeoDataFrame
Source code in cellseg_gsontools/grid.py
def grid_overlay(
    gdf: gpd.GeoDataFrame,
    patch_size: Tuple[int, int] = (256, 256),
    stride: Tuple[int, int] = (256, 256),
    pad: int = 20,
    predicate: str = "intersects",
) -> gpd.GeoDataFrame:
    """Overlay a square grid to the given areas of a `geopandas.GeoDataFrame`.

    Note:
        Returns None if the gdf is empty.

    Parameters:
        gdf (gpd.GeoDataFrame):
            GeoDataFrame to fit the grid to. Uses the bounding box of the GeoDataFrame
            to fit the grid.
        patch_size (Tuple[int, int]):
            Patch size of the grid.
        stride (Tuple[int, int]):
            Stride of the sliding window in the grid.
        pad (int):
            Pad the bounding box with the given number of pixels, by default None.
        predicate (str):
            Predicate to use for the spatial join, by default "intersects".
            Allowed values are "intersects" and "within".

    Returns:
        gpd.GeoDataFrame:
            GeoDataFrame with the grid fitted to the given GeoDataFrame.

    Raises:
        ValueError: If predicate is not one of "intersects" or "within".

    Examples:
        Fit a square grid to a gdf:
        >>> from cellseg_gsontools import read_gdf
        >>> from cellseg_gsontools.grid import grid_overlay
        >>> # Read in the tissue areas
        >>> area_gdf = gpd.read_file("path/to/area.geojson")
        >>> # Fit the grid
        >>> sq_grid = grid_overlay(area_gdf, patch_size=(256, 256), stride=(256, 256))
        >>> sq_grid
        gpd.GeoDataFrame
    """
    if gdf.empty or gdf is None:
        return

    allowed = ["intersects", "within"]
    if predicate not in allowed:
        raise ValueError(f"predicate must be one of {allowed}. Got {predicate}")
    grid = get_grid(gdf, patch_size, stride, pad=pad)
    grid.set_crs(epsg=4328, inplace=True, allow_override=True)
    _, grid_inds = grid.sindex.query(gdf.geometry, predicate=predicate)
    grid = grid.iloc[np.unique(grid_inds)]
    # grid = grid.sjoin(gdf, predicate=predicate)

    return grid.drop_duplicates("geometry")