# algorithms.graph.bipartite_graph

## Module: `algorithms.graph.bipartite_graph`

Inheritance diagram for `nipy.algorithms.graph.bipartite_graph`:

This module implements the BipartiteGraph class, used to represent weighted bipartite graph: it contains two types of vertices, say ‘left’ and ‘right’; then edges can only exist between ‘left’ and ‘right’ vertices. For simplicity the vertices of either side are labeled [1..V] and [1..W] respectively.

Author: Bertrand Thirion, 2006–2011

## Class

## `BipartiteGraph`

class nipy.algorithms.graph.bipartite_graph.BipartiteGraph(V, W, edges=None, weights=None)

  Bases: `object`

  Bipartite graph class

  A graph for which there are two types of nodes, such that edges can exist only between nodes of type 1 and type 2 (not within) fields of this class: V (int, > 0) the number of type 1 vertices W (int, > 0) the number of type 2 vertices E: (int) the number of edges edges: array of shape (self.E, 2) reprensenting pairwise neighbors weights, array of shape (self.E), +1/-1 for scending/descending links

  __init__(V, W, edges=None, weights=None)

    Constructor

    Parameters:
      V (int), the number of vertices of subset 1
      W (int), the number of vertices of subset 2
      edges=None: array of shape (self.E, 2)

        the edge array of the graph

      weights=None: array of shape (self.E)

        the associated weights array

  copy()

    returns a copy of self

  set_edges(edges)

    Set edges to graph

    sets self.edges=edges if

      1. edges has a correct size

      2. edges take values in [0..V-1]*[0..W-1]

    Parameters:
      edges: array of shape(self.E, 2): set of candidate edges
  set_weights(weights)

    Set weights weights to edges

    Parameters:
      weights, array of shape(self.V): edges weights
  subgraph_left(valid, renumb=True)

    Extraction of a subgraph

    Parameters:
      valid, boolean array of shape self.V
      renumb, boolean: renumbering of the (left) edges
    Returns:
      G None or `BipartiteGraph` instance

        A new BipartiteGraph instance with only the left vertices that are True. If sum(valid)==0, None is returned

  subgraph_right(valid, renumb=True)

    Extraction of a subgraph

    Parameters:
      valid bool array of shape self.V
      renumb bool, optional

        renumbering of the (right) edges

    Returns:
      G None or `BipartiteGraph` instance.

        A new BipartiteGraph instance with only the right vertices that are True. If sum(valid)==0, None is returned

## Functions

nipy.algorithms.graph.bipartite_graph.bipartite_graph_from_adjacency(x)

  Instantiates a weighted graph from a square 2D array

  Parameters:
    x: 2D array instance, the input array
  Returns:
    wg: BipartiteGraph instance

nipy.algorithms.graph.bipartite_graph.bipartite_graph_from_coo_matrix(x)

  Instantiates a weighted graph from a (sparse) coo_matrix

  Parameters:
    x: scipy.sparse.coo_matrix instance, the input matrix
  Returns:
    bg: BipartiteGraph instance

nipy.algorithms.graph.bipartite_graph.check_feature_matrices(X, Y)

  checks whether the dimensions of X and Y are consistent

  Parameters:
    X, Y arrays of shape (n1, p) and (n2, p)
    where p = common dimension of the features

nipy.algorithms.graph.bipartite_graph.cross_eps(X, Y, eps=1.0)

  Return the eps-neighbours graph of from X to Y

  Parameters:
    X, Y arrays of shape (n1, p) and (n2, p)
    where p = common dimension of the features
    eps=1, float: the neighbourhood size considered
  Returns:
    the resulting bipartite graph instance

  Notes

  for the sake of speed it is advisable to give PCA-preprocessed matrices X and Y.

nipy.algorithms.graph.bipartite_graph.cross_knn(X, Y, k=1)

  return the k-nearest-neighbours graph of from X to Y

  Parameters:
    X, Y arrays of shape (n1, p) and (n2, p)
    where p = common dimension of the features
    eps=1, float: the neighbourhood size considered
  Returns:
    BipartiteGraph instance

  Notes

  For the sake of speed it is advised to give PCA-transformed matrices X and Y.