tdm.dataset.NeighborsDataset#
- class tdm.dataset.NeighborsDataset(tissue: Tissue, neighborhood_size: float = 7.999999999999999e-05, exclude_cells_near_edge: bool = True)[source]#
Counts the number of neighbors of each type for each cell in the tissue.
- __init__(tissue: Tissue, neighborhood_size: float = 7.999999999999999e-05, exclude_cells_near_edge: bool = True) None [source]#
Counts the number of neighbors of each type for each cell in the tissue.
- Parameters:
tissue (Tissue) – instance of Tissue.
neighborhood_size (float, optional) – neighborhood radius in microns. Default: 80 microns.
exclude_cells_near_edge (bool, optional) – remove cells whose neighborhood exceeds tissue limits. Defaults to True.
Warning
NeighborsDataset
excludes cells whose neighborhood exceeds tissue limits!Note
If
Tissue
is a small sections of a large tissue, then cells near the edges have neighbors we do not see.NeighborsDataset
excludes these cells.ExtrapolateNeighborsDataset
includes these cells but corrects for the bias due to the partially observed neighborhood. The latter increases sample size.It makes sense to keep cells near the edge without correction if the edge of the image is indeed the edge of the tissue. This could be the case for complete tissue sections.
- construct_dataset(tissue: Tissue, cell_type: str) tuple[DataFrame, DataFrame] [source]#
- Parameters:
tissue – instance of type Tissue
cell_type – str from the list CELL_TYPES_ARRAY, as defined in tdm.tissue.cell_types
- Returns:
features: dataframe with shape (n_cells, n_features) observations: dataframe with shape (n_cells, 2) holding observations. columns: division, death
- Return type:
features,observations (tuple)
- construct_features(tissue: Tissue, cell_type: str) DataFrame [source]#
Computes n_neighbors of each type for each cell in the dataset.
- construct_observations(tissue: Tissue, cell_type: str) DataFrame [source]#
Fetches division & death columns for this cell type.
- get_n_neighbors_by_xy(xy: tuple[float, float])[source]#
Computes the number of neighbors of each cell type, for a hypothetical cell placed in position xy within the tissue.
xy: position in microns tissue: Tissue object
- get_xys(cell_type: str | None) ndarray [source]#
Get the xy coords for a cell type (str) or all cells (None).
Note
Caches previous calls in a dictionary. Avoiding the lru_cache decorator so class can be pickled.
- Returns:
numpy array of shape (n_cells,2) with x,y locations of all cells of type cell_type.
- n_neighbors(cell_xy: ndarray, cell_type_xys: ndarray, neighborhood_size: float = 7.999999999999999e-05) int [source]#
- Parameters:
cell_xy – np.array of shape (2,)
cell_type_xys – np.array of shape (n_cells, 2) containing all xys of a specific cell type (e.g all macrophage xys).
neighborhood_size – cells within this distance are considered neighors. default: 100 microns.
- Returns:
(int) number of neighbors
- valid_cells(tissue: Tissue, cell_type: str)[source]#
Returns a subset of xys with cells that have a distance of at least NEIGHBORHOOD_SIZE to each edge of the slide.
Cells with less than neighborhood_size to each edge of the slide are excluded from the dataset so we don’t get an under-estimate of their n_neighbors.
Cells near the edge are of-course taken into account as neighbors of other cells.