bioneuralnet.utils.graph

Functions

gen_correlation_graph(X[, k, method, ...])

Build a graph based on pairwise Pearson or Spearman correlations.

gen_gaussian_knn_graph(X[, k, sigma, ...])

Build a normalized knn similarity graph from feature vectors.

gen_lasso_graph(X[, alpha, self_loops])

Infer a sparse network via Graphical Lasso.

gen_mst_graph(X[, self_loops])

Compute the minimum spanning tree (MST) on Euclidean distances.

gen_similarity_graph(X[, k, metric, mutual, ...])

Build a normalized knn similarity graph from feature vectors.

gen_snn_graph(X[, k, mutual, self_loops])

Build a shared nearest neighbor (SNN) graph.

gen_threshold_graph(X[, b, k, mutual, ...])

Generate a soft threshold co-xpression network this is very similar to how WGCNA works

bioneuralnet.utils.graph.gen_correlation_graph(X: DataFrame, k: int = 15, method: str = 'pearson', mutual: bool = False, per_node: bool = True, threshold: float = None, self_loops: bool = True) DataFrame[source]

Build a graph based on pairwise Pearson or Spearman correlations.

Parameters:
  • pd.dataframe (- X)

  • k (-) – Number of neighbors to keep per node if per_node is True.

  • method (-) – ‘pearson’ or ‘spearman’.

  • mutual (-) – If True, only mutual knn edges.

  • per_node (-) – If True, use per node topk selection, else global threshold.

  • threshold (-) – Correlation cutoff when per_node is False.

  • self_loops (-) – If True, adds weight 1 to diagonal.

Returns:

  • pandas.DataFrame. Normalized adjacency matrix (N x N) of the sparse correlation graph.

Note

  • Correlation is very expensive to compute, so this function is not recommended for large datasets.

bioneuralnet.utils.graph.gen_gaussian_knn_graph(X: DataFrame, k: int = 15, sigma: float = None, mutual: bool = False, self_loops: bool = True) DataFrame[source]

Build a normalized knn similarity graph from feature vectors. Computes pairwise cosine or Euclidean similarities, sparsifies via k-nearest neighbors or a global threshold. Optionally prunes to mutual neighbors and/or adds self-loops.

Parameters:
  • X (-) – Feature matrix where rows are nodes and columns are features.

  • k (-) – Number of neighbors to keep per node.

  • metric (-) – ‘cosine’ or ‘euclidean’, uses Gaussian kernel for distances.

  • mutual (-) – If True, only mutual knn edges.

  • per_node (-) – If True, use per-node topk selection; else global threshold.

  • self_loops (-) – If True, adds weight 1 to diagonal.

Returns:

Normalized adjacency matrix (N x N) of the sparse similarity graph.

Return type:

  • pandas.DataFrame

bioneuralnet.utils.graph.gen_lasso_graph(X: DataFrame, alpha: float = 0.01, self_loops: bool = True) DataFrame[source]

Infer a sparse network via Graphical Lasso.

Parameters:
  • X (-) – Data matrix where rows are nodes and columns are features.

  • alpha (-) – Regularization parameter for Graphical Lasso.

  • self_loops (-) – If True, adds weight 1 to diagonal.

Returns:

Normalized adjacency matrix (N x N) of the inferred network.

Return type:

  • pandas.DataFrame

bioneuralnet.utils.graph.gen_mst_graph(X: DataFrame, self_loops: bool = True) DataFrame[source]

Compute the minimum spanning tree (MST) on Euclidean distances.

Parameters:
  • X (-) – Feature matrix where rows are nodes and columns are features.

  • self_loops (-) – If True, adds weight 1 to diagonal.

Returns:

Normalized adjacency matrix (N x N) of the MST graph.

Return type:

  • pandas.DataFrame

bioneuralnet.utils.graph.gen_similarity_graph(X: DataFrame, k: int = 15, metric: str = 'cosine', mutual: bool = False, per_node: bool = True, self_loops: bool = True) DataFrame[source]

Build a normalized knn similarity graph from feature vectors. Computes pairwise cosine or ecledian disntace,then sparsifies via knn or global a threshold. Optionally prunes to mutual neighbors and/or adds self-loops.

Parameters:
  • X (-) – pandas.DataFrame of shape (N, D) (rows = nodes, cols = features)

  • k (-) – Number of neighbors to keep per node.

  • metric (-) – “cosine” or “euclidean” (uses gaussian kernel on distances).

  • mutual (-) – If True, retain only mutual edges (i->j and j->i).

  • per_node (-) – If True, use per-node top_k; else global cutoff.

  • self_loops (-) – If True, add self-loop weight of 1.

Returns:

  • DataFrame of shape (N, N) the normalized adjacency matrix

bioneuralnet.utils.graph.gen_snn_graph(X: DataFrame, k: int = 15, mutual: bool = False, self_loops: bool = True) DataFrame[source]

Build a shared nearest neighbor (SNN) graph.

Parameters:
  • X (-) – Feature matrix where rows are nodes and columns are features.

  • k (-) – Number of neighbors to keep per node.

  • mutual (-) – If True, only mutual knn edges.

  • self_loops (-) – If True, adds weight 1 to diagonal.

Returns:

Normalized adjacency matrix (N x N) of the SNN graph.

Return type:

  • pandas.DataFrame

bioneuralnet.utils.graph.gen_threshold_graph(X: DataFrame, b: float = 6.0, k: int = 15, mutual: bool = False, self_loops: bool = True) DataFrame[source]

Generate a soft threshold co-xpression network this is very similar to how WGCNA works

Parameters:
  • X (-) – Data matrix where rows are nodes and columns are features.

  • b (-) – Thresholding exponent applied to absolute correlations.

  • k (-) – Number of neighbors to keep per node.

  • mutual (-) – If True, only mutual knn edges.

  • self_loops (-) – If True, adds weight 1 to diagonal.

Returns:

Normalized adjacency matrix (N x N) of the soft-thresholded graph.

Return type:

  • pandas.DataFrame