Skip to content

weighted_mean_center

Calculate the weighted mean center of a GeoDataFrame containing nuclei.

Parameters:

Name Type Description Default
xy ndarray

A numpy array of shape (n, 2) containing the x and y coordinates. (centroids of the nuclei).

required

Returns:

Type Description
ndarray

np.ndarray: The weighted mean center of the centroids as a numpy array with shape (2,).

Source code in src/histolytics/spatial_clust/centrography.py
def weighted_mean_center(xy: np.ndarray, weights: Sequence) -> np.ndarray:
    """Calculate the weighted mean center of a GeoDataFrame containing nuclei.

    Parameters:
        xy (np.ndarray):
            A numpy array of shape (n, 2) containing the x and y coordinates.
            (centroids of the nuclei).

    Returns:
        np.ndarray:
            The weighted mean center of the centroids as a numpy array with shape (2,).
    """
    points, weights = np.asarray(xy), np.asarray(weights)
    w = weights * 1.0 / weights.sum()
    w.shape = (1, len(points))
    return np.dot(w, points)[0]