Utility

Transformations

graspy.utils.pass_to_ranks(graph, method='simple-nonzero')[source]

Rescales edge weights of an adjacency matrix based on their relative rank in the graph.

Parameters:
graph: Adjacency matrix
method: {'simple-nonzero' (default), 'simple-all', 'zero-boost'} string, optional
  • 'simple-nonzero'
    assigns ranks to all non-zero edges, settling ties using the average. Ranks are then scaled by \(\frac{rank(\text{non-zero edges})}{\text{total non-zero edges} + 1}\)
  • 'simple-all'
    assigns ranks to all non-zero edges, settling ties using the average. Ranks are then scaled by \(\frac{rank(\text{non-zero edges})}{n^2 + 1}\) where n is the number of nodes
  • 'zero-boost'
    preserves the edge weight for all 0s, but ranks the other edges as if the ranks of all 0 edges has been assigned. If there are 10 0-valued edges, the lowest non-zero edge gets weight 11 / (number of possible edges). Ties settled by the average of the weight that those edges would have received. Number of possible edges is determined by the type of graph (loopless or looped, directed or undirected).
Returns:
graph: numpy.ndarray, shape(n_vertices, n_vertices)

Adjacency matrix of graph after being passed to ranks

See also

scipy.stats.rankdata

graspy.utils.to_laplace(graph, form='DAD', regularizer=None)[source]

A function to convert graph adjacency matrix to graph laplacian.

Currently supports I-DAD, DAD, and R-DAD laplacians, where D is the diagonal matrix of degrees of each node raised to the -1/2 power, I is the identity matrix, and A is the adjacency matrix.

R-DAD is regularized laplacian: where \(D_t = D + regularizer*I\).

Parameters:
graph: object

Either array-like, (n_vertices, n_vertices) numpy array, or an object of type networkx.Graph.

form: {'I-DAD' (default), 'DAD', 'R-DAD'}, string, optional
  • 'I-DAD'
    Computes \(L = I - D*A*D\)
  • 'DAD'
    Computes \(L = D*A*D\)
  • 'R-DAD'
    Computes \(L = D_t*A*D_t\) where \(D_t = D + regularizer*I\)
regularizer: int, float or None, optional (default=None)

Constant to be added to the diagonal of degree matrix. If None, average node degree is added. If int or float, must be >= 0. Only used when form == 'R-DAD'.

Returns:
L: numpy.ndarray

2D (n_vertices, n_vertices) array representing graph laplacian of specified form

References

[1]Qin, Tai, and Karl Rohe. "Regularized spectral clustering under the degree-corrected stochastic blockmodel." In Advances in Neural Information Processing Systems, pp. 3120-3128. 2013

Helper Functions

graspy.utils.import_graph(graph)[source]

A function for reading a graph and returning a shared data type.

Parameters:
graph: object

Either array-like, shape (n_vertices, n_vertices) numpy array, or an object of type networkx.Graph.

Returns:
out: array-like, shape (n_vertices, n_vertices)

A graph.

See also

networkx.Graph, numpy.array

graspy.utils.import_edgelist(path, extension='edgelist', delimiter=None, nodetype=<class 'int'>, return_vertices=False)[source]

Function for reading a single or multiple edgelists. When importing multiple edgelists, the union of vertices from all graphs is computed so that each output graph have matched vertex set. The order of nodes are sorted by node values.

Parameters:
path : str, Path object, or iterable

If path is a directory, then the importing order will be sorted in alphabetical order.

extension : str, optional

If path is a directory, then the function will convert all files with matching extension.

delimiter : str or None, default=None, optional

Delimiter of edgelist. If None, the delimiter is whitespace.

nodetype : int (default), float, str, Python type, optional

Convert node data from strings to specified type.

return_vertices : bool, default=False, optional

Returns the union of all ind

Returns:
out : list of array-like, or array-like, shape (n_vertices, n_vertices)

If path is a directory, a list of arrays is returned. If path is a file, an array is returned.

vertices : array-like, shape (n_vertices, )

If return_vertices == True, then returns an array of all vertices that were included in the output graphs.

graspy.utils.symmetrize(graph, method='triu')[source]

A function for forcing symmetry upon a graph.

Parameters:
graph: object

Either array-like, (n_vertices, n_vertices) numpy matrix, or an object of type networkx.Graph.

method: {'triu' (default), 'tril', 'avg'}, optional

An option indicating which half of the edges to retain when symmetrizing.

  • 'triu'
    Retain the upper right triangle.
  • 'tril'
    Retain the lower left triangle.
  • 'avg'
    Retain the average weight between the upper and lower right triangle, of the adjacency matrix.
Returns:
graph: array-like, shape (n_vertices, n_vertices)

the graph with asymmetries removed.

graspy.utils.remove_loops(graph)[source]

A function to remove loops from a graph.

Parameters:
graph: object

Either array-like, (n_vertices, n_vertices) numpy matrix, or an object of type networkx.Graph.

Returns:
graph: array-like, shape(n_vertices, n_vertices)

the graph with self-loops (edges between the same node) removed.