swnn.graph2tree module

swnn.graph2tree.adaptive_tree(adj_max, group_lbs, stage_lbs=None, stage_ord=None, ignore_pa=(), ext_sep='_')

Adaptatively build the developmental tree from the stagewise-KNN graph.

Parameters
  • adj_max (spmatrix) – sparse.csc_matrix, shape = (n_points, n_points) adjacent matrix of single points (cells)

  • group_lbs (Sequence) – np.array, shape = (n_points,) group labels specifying each cluster in each stage. e.g. ‘stage1_1’ specifies the cluster 1 in stage1.

  • stage_lbs (Optional[Sequence]) – np.array, shape = (n_points,) stage labels, better be explicitly assigned. If None, this will be extracted from group_lbs, and may cause unexpected results.

  • stage_ord (Optional[Sequence]) – np.array, shape = (n_stages,) order of stages, better provided by user; if None, it will be decided automatically.

  • ignore_pa (list or set) – parent nodes to be ignored; empty tuple by default.

  • ext_sep (str) – parse string for automatically extract the stage-labels from group_lbs

Returns

  • edgedf (pd.DataFrame) – a DataFrame with each row representing an edge, columns are [‘node’, ‘parent’, ‘prop’], where ‘prop’ is the proportion of nodes that have voted for the current parent.

  • group_lbs – refined group-labels for each sample (e.g. single-cell)

Examples

>>> edgedf, group_lbs = adaptive_tree(adj_max, group_lbs, stage_ord=stages)
swnn.graph2tree.connect_near_stages(adj_max, group_lbs0, group_lbs1, groups0=None, groups1=None, ignore_pa=(), df2edges=True, sep='_')
Parameters
  • adj_max – csc_sparse matrix, adjacent matrix of samples (cells), shape (n_samples, n_samples)

  • group_lbs0 – group labels of the parent stage

  • group_lbs1 – group labels of the descendent stage

Returns

  • edgedf – a DataFrame with each row representing an edge, columns are [‘node’, ‘parent’, ‘prop’], where ‘prop’ is the proportion of nodes that vote for the current parent.

  • new_group_lbs1 – np.array, refined group labels

  • new_groups1 – unique group names from new_group_lbs1

swnn.graph2tree.agg_group_edge_props(adj, group_lbs0, group_lbs1=None, groups0=None, groups1=None, axis=0, verbose=True)
swnn.graph2tree.agg_group_edges(adj, group_lbs0, group_lbs1=None, groups0=None, groups1=None, asdf=True, verbose=True)
Parameters
  • adj – adjacent matrix of shape (N0, N1), if group_lbs1 is None, then set N0=N1.

  • group_lbs0 – a list or a np.array of shape (N0,)

  • group_lbs1 – a list or a np.array of shape (N1,)

Returns

group_conn

Return type

summation of connected edges between given groups

swnn.graph2tree.max_connection(adj, axis=0, df2edges=False)

keep only max element (connection) for each column (axis=0), and remove the other elements (connections)

swnn.graph2tree.make_stage_group_dict(group_lbs, stage_lbs=None)
swnn.graph2tree.find_children(nodes, children_dict, n=100)
Parameters
  • nodes (Sequence) – better a list of node(s) to be looked up

  • children_dict (dict) – parent (key) -> children (value)

  • n (int) – max number of iterations

swnn.graph2tree.make_children_dict(df_tree, column_pa='parent', column_ch='node')

making a dict for looking up descendants