Impulse Graph¶
Overview¶
-
class
dynetworkx.
ImpulseGraph
(name='', **attr)¶ Base class for undirected interval graphs.
The ImpulseGraph class allows any hashable object as a node and can associate key/value attribute pairs with each undirected edge.
Each edge must have one integer, timestamp.
Self-loops are allowed. Multiple edges between two nodes are allowed.
Parameters: attr (keyword arguments, optional (default= no attributes)) – Attributes to add to graph as key=value pairs. Examples
Create an empty graph structure (a “null interval graph”) with no nodes and no edges.
>>> G = dnx.ImpulseGraph()
G can be grown in several ways.
Nodes:
Add one node at a time:
>>> G.add_node(1)
Add the nodes from any container (a list, dict, set or even the lines from a file or the nodes from another graph).
Add the nodes from any container (a list, dict, set)
>>> G.add_nodes_from([2, 3]) >>> G.add_nodes_from(range(100, 110))
Edges:
G can also be grown by adding edges. This can be considered the primary way to grow G, since nodes with no edge will not appear in G in most cases. See
G.to_snapshot()
.Add one edge, with timestamp of 10.
>>> G.add_edge(1, 2, 10)
a list of edges,
>>> G.add_edges_from([(1, 2, 10), (1, 3, 11)])
If some edges connect nodes not yet in the graph, the nodes are added automatically. There are no errors when adding nodes or edges that already exist.
Attributes:
Each impulse graph, node, and edge can hold key/value attribute pairs in an associated attribute dictionary (the keys must be hashable). By default these are empty, but can be added or changed using add_edge, add_node.
Keep in mind that the edge timestamp is not an attribute of the edge.
>>> G = dnx.IntervalGraph(day="Friday") >>> G.graph {'day': 'Friday'}
Add node attributes using add_node(), add_nodes_from()
>>> G.add_node(1, time='5pm') >>> G.add_nodes_from([3], time='2pm')
Add edge attributes using add_edge(), add_edges_from().
>>> G.add_edge(1, 2, 10, weight=4.7 ) >>> G.add_edges_from([(3, 4, 11), (4, 5, 33)], color='red')
Shortcuts:
Here are a couple examples of available shortcuts:
>>> 1 in G # check if node in impulse graph during any timestamp True >>> len(G) # number of nodes in the entire impulse graph 5
Subclasses (Advanced): Edges in impulse graphs are represented by tuples kept in a SortedDict (http://www.grantjenks.com/docs/sortedcontainers/) keyed by timestamp.
The Graph class uses a dict-of-dict-of-dict data structure. The outer dict (node_dict) holds adjacency information keyed by nodes. The next dict (adjlist_dict) represents the adjacency information and holds edge data keyed by interval objects. The inner dict (edge_attr_dict) represents the edge data and holds edge attribute values keyed by attribute names.
Methods¶
Adding and removing nodes and edges¶
ImpulseGraph.__init__ ([name]) |
Initialize an interval graph with edges, name, or graph attributes. |
ImpulseGraph.add_node (node_for_adding, **attr) |
Add a single node node_for_adding and update node attributes. |
ImpulseGraph.add_nodes_from (…) |
Add multiple nodes. |
ImpulseGraph.remove_node (n[, begin, end, …]) |
Remove the presence of a node n within the given interval. |
ImpulseGraph.add_edge (u, v, t, **attr) |
Add an edge between u and v, at t. |
ImpulseGraph.add_edges_from (ebunch_to_add, …) |
Add all the edges in ebunch_to_add. |
ImpulseGraph.remove_edge (u, v[, begin, end, …]) |
Remove the edge between u and v in the impulse graph, during the given interval. |
Reporting impulse graph, nodes and edges¶
ImpulseGraph.nodes ([begin, end, inclusive, …]) |
A NodeDataView of the ImpulseGraph nodes. |
ImpulseGraph.has_node (n[, begin, end, inclusive]) |
Return True if the impulse graph contains the node n, during the given interval. |
ImpulseGraph.edges ([u, v, begin, end, …]) |
Returns a list of Interval objects of the IntervalGraph edges. |
ImpulseGraph.has_edge (u, v[, begin, end, …]) |
Return True if there exists an edge between u and v in the impulse graph, during the given interval. |
ImpulseGraph.__contains__ (n) |
Return True if n is a node, False otherwise. |
ImpulseGraph.__str__ () |
Return the interval graph name. |
ImpulseGraph.interval () |
Return a 2-tuple as (begin, end) interval of the entire |
Counting nodes and edges¶
ImpulseGraph.number_of_nodes ([begin, end, …]) |
Return the number of nodes in the impulse graph between the given interval. |
ImpulseGraph.__len__ () |
Return the number of nodes. |
Making copies and subgraphs¶
ImpulseGraph.to_subgraph (begin, end[, …]) |
Return a networkx Graph or MultiGraph which includes all the nodes and edges which have timestamps within the given interval. |
ImpulseGraph.to_snapshots (number_of_snapshots) |
Return a list of networkx Graph or MultiGraph objects as snapshots of the impulse graph in consecutive order. |
Loading an impulse graph¶
ImpulseGraph.load_from_txt (path[, …]) |
Read impulse graph in from path. |
ImpulseGraph.save_to_txt (path[, delimiter]) |
Write impulse graph to path. |
Analyzing impulse graphs¶
ImpulseGraph.degree ([node, begin, end, delta]) |
Return the degree of a specified node between time begin and end. |