spateo.tools.labels#

Module Contents#

Classes#

Label

Given categorizations for a set of points, wrap into a Label class.

Functions#

row_normalize(→ scipy.sparse.csr_matrix)

Normalize a compressed sparse row (CSR) matrix by row- written for sparse pairwise distance arrays, but can be

_rand_binary_array(array_length, num_onbits)

expand_labels(→ Label)

Spread out label IDs such that they range evenly from 0 to max_label_id, e.g. [0 1 2] -> [0 5 10]

match_labels(→ Label)

Match second set of labels to first, returning a new Label object

match_label_series(→ Tuple[List[Label], int])

Match a list of labels to each other, one after another in order of increasing (if least_labels_first is true)

interlabel_connections(→ numpy.ndarray)

Compute connections strength between labels (based on pairwise distances), normalized by counts of each label

create_label_class(→ Union[Label, List[Label]])

Wraps categorical labels into custom Label class for downstream processing.

spateo.tools.labels.row_normalize(graph: scipy.sparse.csr_matrix, copy: bool = False, verbose: bool = True) scipy.sparse.csr_matrix[source]#

Normalize a compressed sparse row (CSR) matrix by row- written for sparse pairwise distance arrays, but can be applied to any sparse matrix.

Parameters:
graph

Sparse array of shape [n_samples, n_features]. If pairwise distance array, shape [n_samples, n_samples].

copy

If True, create a copy of the graph before computations so that the original is preserved.

verbose

If True, prints number of nonzero entries.

Returns:

Input array (or the copy of the input array) post-normalization.

Return type:

graph

class spateo.tools.labels.Label(labels_dense: numpy.ndarray | list, str_map: None | dict = None, verbose: bool = False)[source]#

Bases: object

Given categorizations for a set of points, wrap into a Label class.

labels_dense: Numerical labels. str_map: Optional mapping of numerical labels (keys) to strings (values). verbose: whether to print running info of row_normalize.

__repr__() str[source]#

Return repr(self).

__str__() str[source]#

Return str(self).

get_onehot() scipy.sparse.csr_matrix[source]#

return one-hot sparse array of labels. If not already computed, generate the sparse array from dense label array

get_normalized_onehot() scipy.sparse.csr_matrix[source]#

Return normalized one-hot sparse array of labels.

generate_normalized_onehot() scipy.sparse.csr_matrix[source]#

Generate a normalized onehot matrix where each row is normalized by the count of that label e.g. a row [0 1 1 0 0] will be converted to [0 0.5 0.5 0 0]

generate_onehot() scipy.sparse.csr_matrix[source]#

Convert an array of labels to a num_labels x num_samples sparse one-hot matrix Labels MUST be integers starting from 0, but can have gaps in between e.g. [0,1,5,9]

spateo.tools.labels._rand_binary_array(array_length, num_onbits)[source]#
spateo.tools.labels.expand_labels(label: Label, max_label_id: int, sort_labels: bool = False) Label[source]#

Spread out label IDs such that they range evenly from 0 to max_label_id, e.g. [0 1 2] -> [0 5 10] Useful if you need to be consistent with other label sets with many more label IDs. This spreads labels out along the color spectrum/map so that the colors are not too similar to each other. Use sort_labels if the list of IDs are not already sorted (although IDs are typically already sorted)

spateo.tools.labels.match_labels(labels_1: Label, labels_2: Label, extra_labels_assignment: str = 'random', verbose: bool = False) Label[source]#

Match second set of labels to first, returning a new Label object Uses scipy’s version of the Hungarian algorithm (linear_sum_assigment)

spateo.tools.labels.match_label_series(label_list: List[Label], least_labels_first: bool = True, extra_labels_assignment: str = 'greedy') Tuple[List[Label], int][source]#

Match a list of labels to each other, one after another in order of increasing (if least_labels_first is true) or decreasing (least_labels_first set to false) number of label ids.

Returns the relabeled list in original order.

spateo.tools.labels.interlabel_connections(label: Label, weights_matrix: scipy.sparse.csr_matrix | numpy.ndarray) numpy.ndarray[source]#

Compute connections strength between labels (based on pairwise distances), normalized by counts of each label

Parameters:
class

Instance of class ‘Label’, with one-hot dense label matrix in “dense”, list of unique labels in “ids”, counts per label in “counts”, etc.

weights_matrix

Pairwise adjacency matrix, weighted by e.g. spatial distance between points.

Returns:

Pairwise connection strength array, shape [n_labels, n_labels].

Return type:

connections

spateo.tools.labels.create_label_class(adata: anndata.AnnData, cat_key: str | List[str]) Label | List[Label][source]#

Wraps categorical labels into custom Label class for downstream processing.

Parameters:
adata

An anndata object.

cat_key

Keys in .obs containing categorical labels. This function and the Label class provide the most utility when this is used in conjunction with the results of multiple different runs of the Louvain algorithm.

Returns:

Either an object of Label class or a list where each element is an object of Label class. Will return a

list if given multiple arguments to ‘cat_key’.

Return type:

label