Digraph#
For the parent class, see networkx.DiGraph.
Digraph creates a NetworkX Digraph and includes methods for generating a distance matrix, changing the edge weights and retrieving the edge weights.
Usage:
directed_graph = DiGraph(adjacency_martix, vertices, counts, loops: bool)
adjancency_matrix is either a binary or weighted matrix encoding the adjancency of the directed graph. vertices is the list of vertices, which in our use case is the list of nonempty bin numbers. counts is a list that is the weights of each vertex, and loops is a Boolean determing whether or not to keep the self-loops when building the graph. This would be associated with the situation when the dynamical system stays in the same bin for conseccutive time steps.
Methods:
D = Digraph.distance_matrix(method)
This method returns the distance matrix of the graph, as needed for homology computations. There are currently two tested methods, unweighted_shortest_path and weighted_shortest_path. A third method, probabilistic, is implemented but mostly untested, and could be an avenue for future research, where the bins become the states of a discretized view of the dynamical system as a Markov chain. Currently, if the graph is not strongly path connected, unreachable pairs are assigned a value of 1000.
Digraph.weight_graph(weights, smallest, biggest)
The weight_graph method allows for custom or random weighting of a directed graph. weights should be a callable function, that takes smallest and biggest as inputs, and assigns each edge in the directed graph a weight based on that function. For example, several of our experiments use numpy.random.randint.
Digraph.get_weights()
get_weights returns a dictionary whose keys are the edges of the directed graph, with associated values the weights of the edges.
- class cactis.Digraph(*args, backend=None, **kwargs)[source]#
A wrapper for networkx.DiGraph with custom edge weighting and distance matrix methods.
Each node stores two attributes:
bin: the corresponding bin/vertex label
count: an integer count associated with that bin
Methods
- __init__(adjacency_matrix, vertices, counts, loops: bool)[source]#
Initialize a directed graph from an adjacency matrix.
- Parameters:
- adjacency_matrixndarray of shape (n, n)
Binary or weighted adjacency matrix describing directed edges.
- verticeslist of length n
Labels for the vertices (e.g., bin indices).
- countslist of length n
Associated counts for each vertex.
- loopsbool
Whether to keep self-loops (True) or remove them (False).
Notes
If ‘loops’ is False, the diagonal of the adjacency matrix is zeroed out before graph construction.
Node attributes ‘bin’ and ‘count’ are set from ‘vertices’ and ‘counts’ respectively.
- distance_matrix(method='unweighted_shortest_path')[source]#
Compute a pairwise distance matrix between all nodes.
- Parameters:
- method{unweighted_shortest_path, weighted_shortest_path, probabilistic}
- Returns:
- Dndarray of shape (n, n)
Distance matrix, where D[i, j] is the distance from node i to node j. Unreachable pairs are assigned a large value (1000).
Notes
The distance matrix is also stored as self.D.
- get_weights()[source]#
Retrieve all edge weights.
- Returns:
- weightsdict
Dictionary mapping edges ‘(u, v)’ to their ‘weight’ attribute.
- weight_graph(weights, smallest, biggest)[source]#
Assign edge weights to the graph.
- Parameters:
- weightscallable
Function of the form weights(smallest, biggest) that returns a weight for an edge. This is applied to every edge. For example, numpy.random.randint
- smallestnumber
Smallest number in the weight range
- biggestnumber
Biggest number in the weight range