Utility¶
Transformations¶

graspy.utils.
pass_to_ranks
(graph, method='zeroboost')[source]¶ Rescales edge weights of an adjacency matrix based on their relative rank in the graph.
Parameters:  graph: Adjacency matrix
 method: {'zeroboost' (default), 'simpleall', 'simplenonzero'} string, optional
 'zeroboost'
 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 0valued edges, the lowest nonzero 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).
 'simpleall'
 assigns ranks to all nonzero edges, settling ties using the average. Ranks are then scaled by \(\frac{2 rank(\text{nonzero edges})}{n^2 + 1}\) where n is the number of nodes
 'simplenonzero'
 same as simpleall, but ranks are scaled by \(\frac{2 rank(\text{nonzero edges})}{\text{total nonzero edges} + 1}\)
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')[source]¶ A function to convert graph adjacency matrix to graph laplacian.
Currently supports IDAD and 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
Parameters:  graph: object
Either arraylike, (n_vertices, n_vertices) numpy array, or an object of type networkx.Graph.
 form: {'IDAD' (default), 'DAD'}, string, optional
 'IDAD'
 Computes \(L = I  D*A*D\)
 'DAD'
 Computes \(L = D*A*D\)
Returns:  L: numpy.ndarray
2D (n_vertices, n_vertices) array representing graph laplacian of specified form
Helper Functions¶

graspy.utils.
import_graph
(graph)[source]¶  A function for reading a graph and returning a shared data type. Makes IO cleaner and easier.
Parameters:  graph: object
Either arraylike, shape (n_vertices, n_vertices) numpy array, or an object of type networkx.Graph.
Returns:  out: arraylike, shape (n_vertices, n_vertices)
A graph.
See also
networkx.Graph
,numpy.array

graspy.utils.
symmetrize
(graph, method='triu')[source]¶ A function for forcing symmetry upon a graph.
Parameters:  graph: object
Either arraylike, (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: arraylike, 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 arraylike, (n_vertices, n_vertices) numpy matrix, or an object of type networkx.Graph.
Returns:  graph: arraylike, shape(n_vertices, n_vertices)
the graph with selfloops (edges between the same node) removed.