Skip to content

nhood_vals

cellseg_gsontools.neighbors.nhood_vals(nhood, values, **kwargs)

Get the values of objects in the neighboring nodes.

Note

This function is designed to be used with the gdf_apply function. See the example.

Parameters:

Name Type Description Default
nhood Sequence[int]

A list or array of neighboring node uids.

required
values Series

A value column-vector of shape (N, ).

required
**kwargs Dict[str, Any]

Additional keyword arguments. Not used.

{}

Returns:

Type Description
ndarray

np.ndarray: The counts vector of the given values vector. Shape (n_classes, )

Examples:

Use gdf_apply to get the neighborhood values for each area of a cell

>>> from functools import partial
>>> from cellseg_gsontools.data import gland_cells
>>> from cellseg_gsontools.graphs import fit_graph
>>> from cellseg_gsontools.utils import set_uid
>>> from cellseg_gsontools.apply import gdf_apply
>>> from cellseg_gsontools.neighbors import neighborhood, nhood_vals
>>> gc = gland_cells()
>>> # To fit the delaunay graph, we need to set a unique id for each cell first
>>> gc = set_uid(gc, id_col="uid")
>>> w = fit_graph(gc, type="delaunay", thresh=100, id_col="uid")
>>> # Get the neihgboring nodes of the graph
>>> func = partial(neighborhood, spatial_weights=w)
>>> gc["nhood"] = gdf_apply(gc, func, columns=["uid"])
>>> # get the area values of the neighbors
>>> func = partial(nhood_vals, values=gc.area.round(2))
>>> gc["neighbor_areas"] = gdf_apply(
...     gc,
...     func=func,
...     parallel=True,
...     columns=["nhood"],
... )
>>> gc["neighbor_areas"].head(5)
    uid
0     [520.24, 565.58, 435.91, 302.26, 241.85, 418.02]
1     [565.58, 520.24, 302.26, 318.15, 241.85, 485.71]
2      [721.5, 435.91, 556.05, 466.96, 418.02, 678.35]
3    [435.91, 520.24, 721.5, 302.26, 556.05, 655.42...
4    [302.26, 520.24, 565.58, 435.91, 655.42, 485.7...
Name: neighbor_areas, dtype: object
Source code in cellseg_gsontools/neighbors.py
def nhood_vals(nhood: Sequence[int], values: pd.Series, **kwargs) -> np.ndarray:
    """Get the values of objects in the neighboring nodes.

    Note:
        This function is designed to be used with the `gdf_apply` function.
        See the example.

    Parameters:
        nhood (Sequence[int]):
            A list or array of neighboring node uids.
        values (pd.Series):
            A value column-vector of shape (N, ).
        **kwargs (Dict[str, Any]):
            Additional keyword arguments. Not used.

    Returns:
        np.ndarray:
            The counts vector of the given values vector. Shape (n_classes, )

    Examples:
        Use `gdf_apply` to get the neighborhood values for each area of a cell
        >>> from functools import partial
        >>> from cellseg_gsontools.data import gland_cells
        >>> from cellseg_gsontools.graphs import fit_graph
        >>> from cellseg_gsontools.utils import set_uid
        >>> from cellseg_gsontools.apply import gdf_apply
        >>> from cellseg_gsontools.neighbors import neighborhood, nhood_vals
        >>> gc = gland_cells()
        >>> # To fit the delaunay graph, we need to set a unique id for each cell first
        >>> gc = set_uid(gc, id_col="uid")
        >>> w = fit_graph(gc, type="delaunay", thresh=100, id_col="uid")
        >>> # Get the neihgboring nodes of the graph
        >>> func = partial(neighborhood, spatial_weights=w)
        >>> gc["nhood"] = gdf_apply(gc, func, columns=["uid"])
        >>> # get the area values of the neighbors
        >>> func = partial(nhood_vals, values=gc.area.round(2))
        >>> gc["neighbor_areas"] = gdf_apply(
        ...     gc,
        ...     func=func,
        ...     parallel=True,
        ...     columns=["nhood"],
        ... )
        >>> gc["neighbor_areas"].head(5)
            uid
        0     [520.24, 565.58, 435.91, 302.26, 241.85, 418.02]
        1     [565.58, 520.24, 302.26, 318.15, 241.85, 485.71]
        2      [721.5, 435.91, 556.05, 466.96, 418.02, 678.35]
        3    [435.91, 520.24, 721.5, 302.26, 556.05, 655.42...
        4    [302.26, 520.24, 565.58, 435.91, 655.42, 485.7...
        Name: neighbor_areas, dtype: object
    """
    if isinstance(nhood, pd.Series):
        nhood = nhood.iloc[0]  # assume that the series is a row

    nhood_vals = np.array([0])
    if nhood not in (None, np.nan) and isinstance(nhood, (Sequence, np.ndarray)):
        nhood_vals = values.loc[nhood].to_numpy()

    return nhood_vals