Compocyte.core.tools

Functions

delete_dict_entries(dictionary[, del_key, ...])

dict_depth(dictionary[, running_count])

flatten_dict(dictionary[, ...])

flatten_labels(pred_h_labels, graph, root_node)

get_last_annotation(obs_names, adata[, ...])

get_leaf_nodes(hierarchy)

hierarchy_names_unique(hierarchy_dict)

infer_dict(graph[, parent])

infer_levels(hierarchy, labels, root_node[, ...])

Infer hierarchical levels for labels based on a hierarchy graph. This function takes a hierarchy (either as a dict or NetworkX DiGraph) and assigns hierarchical levels to labels by finding the shortest path from a root node to each label in the hierarchy graph. :param hierarchy: The hierarchy structure. Can be either a dictionary of edges or a NetworkX directed graph. If a dict is provided, it will be converted to a DiGraph. :type hierarchy: dict or nx.DiGraph :param labels: The labels to infer levels for. Can be: - A string key referring to a column in adata.obs - A list of labels - An array-like object (with tolist() method) of labels :type labels: str, list, or array-like :param root_node: The root node of the hierarchy from which to compute shortest paths. :type root_node: str or int :param adata: An AnnData object. If provided, the obs dataframe from this object will be used as the base dataframe. If None, a new empty DataFrame is created. Default is None. :type adata: anndata.AnnData, optional :param prefix_obs: Prefix for the level column names. Default is 'Level_'. Level columns will be named 'Level_0', 'Level_1', etc. :type prefix_obs: str, optional.

is_counts(matrix[, n_rows_to_try])

Determines whether or not a matrix (such as adata.X, adata.raw.X or an adata layer) contains count data by manually checking a subsample of the supplied matrix.

make_graph_from_edges(d, g[, parent_key])

Add explanation

set_node_to_depth(dictionary[, depth, ...])

z_transform_properties(data_arr[, ...])

Calculates a z transformation to center properties across cells in data_arr around mean zero

Classes

Hierarchical_Metric(true_labels, ...[, ...])

Compocyte.core.tools.set_node_to_depth(dictionary, depth=0, node_to_depth={})[source]
Compocyte.core.tools.is_counts(matrix, n_rows_to_try=100)[source]

Determines whether or not a matrix (such as adata.X, adata.raw.X or an adata layer) contains count data by manually checking a subsample of the supplied matrix.

Compocyte.core.tools.dict_depth(dictionary, running_count=0)[source]
Compocyte.core.tools.infer_levels(hierarchy, labels, root_node, adata=None, prefix_obs='Level_')[source]

Infer hierarchical levels for labels based on a hierarchy graph. This function takes a hierarchy (either as a dict or NetworkX DiGraph) and assigns hierarchical levels to labels by finding the shortest path from a root node to each label in the hierarchy graph. :param hierarchy: The hierarchy structure. Can be either a dictionary of edges or a NetworkX

directed graph. If a dict is provided, it will be converted to a DiGraph.

Parameters:
  • labels (str, list, or array-like) – The labels to infer levels for. Can be: - A string key referring to a column in adata.obs - A list of labels - An array-like object (with tolist() method) of labels

  • root_node (str or int) – The root node of the hierarchy from which to compute shortest paths.

  • adata (anndata.AnnData, optional) – An AnnData object. If provided, the obs dataframe from this object will be used as the base dataframe. If None, a new empty DataFrame is created. Default is None.

  • prefix_obs (str, optional) – Prefix for the level column names. Default is ‘Level_’. Level columns will be named ‘Level_0’, ‘Level_1’, etc.

Returns:

  • pd.DataFrame – A DataFrame with the original labels and new columns for each hierarchical level, containing the nodes at each level of the hierarchy for each label.

  • list – A list of the new level column names.

Notes

  • Paths shorter than the maximum depth are padded with empty strings.

  • The number of levels is determined by the depth of the hierarchy.

Compocyte.core.tools.flatten_dict(dictionary, running_list_of_values=[])[source]
Compocyte.core.tools.hierarchy_names_unique(hierarchy_dict)[source]
Compocyte.core.tools.z_transform_properties(data_arr, discretization=False)[source]

Calculates a z transformation to center properties across cells in data_arr around mean zero

Compocyte.core.tools.make_graph_from_edges(d, g, parent_key='')[source]

Add explanation

Compocyte.core.tools.get_last_annotation(obs_names, adata, barcodes=None, true_only=False)[source]
Compocyte.core.tools.get_leaf_nodes(hierarchy)[source]
Compocyte.core.tools.delete_dict_entries(dictionary, del_key='classifier', first_run=True, deleted_key=False)[source]
Compocyte.core.tools.flatten_labels(pred_h_labels, graph, root_node, verbose=False)[source]
class Compocyte.core.tools.Hierarchical_Metric(true_labels, predicted_labels, hierarchy_structure, root_node='Blood')[source]

Bases: object

__init__(true_labels, predicted_labels, hierarchy_structure, root_node='Blood')[source]

hierarchy_structure: NetworkX graph of hierarchical classifier

augmented_set_of_node_n(node)[source]

Assuming a tree hierarchy structure, ancestors of node n, including node, excluding root

calculate_intersects(t_label, p_label, t_label_augmented, p_label_augmented)[source]
hP()[source]
hR()[source]
hF(beta)[source]
macro_hF(beta)[source]

Macro averaged hF-Score (average of micro hF1’s for each label)

list_micro_metrics(beta)[source]
Compocyte.core.tools.infer_dict(graph, parent=None)[source]