Creates a temporal undirected graph. Does not allow parallel edges among node pairs.
It inherits a static NetworkX Graph
and includes all methods available from it, such as add_node(), add_edge(),
neighbors(), subgraph(), to_directed(), and to_undirected(),
as well as additional methods implemented for handling temporal graphs and snapshots.
This is equivalent to calling temporal_graph() with directed=False
and multigraph=False.
Hint
Setting t as greater than 1 initializes a list of NetworkX graph objects, each
representing a snapshot in time. Unless dynamic node attributes are required, it is
recommended to use the slice() method instead,
allowing to create less resource-demanding graph views
on the fly.
Example
The following example demonstrates how to create a temporal graph with two snapshots:
>>> importnetworkx_temporalastx>>>>>> TG=tx.TemporalGraph(t=2)>>>>>> TG[0].add_edge("a","b")>>> TG[1].add_edge("c","b")>>>>>> print(TG)TemporalGraph (t=2) with 4 nodes and 2 edges
If G is None, adds an empty graph with the same properties as the temporal graph.
Returns the added snapshot (i.e., the parameter G if not None, or an empty graph).
Parameters:
G (Graph) – NetworkX graph object to add as snapshot.
High-level conversion function to other libraries. Calls the appropriate function based on the
received to parameter. The following libraries are currently supported for conversion:
To reduce package dependencies and avoid unnecessary imports, the required library is
imported on function call based on the to parameter and must be separately installed.
Example
Convert the Karate Club
dataset from NetworkX into a PyTorch Geometric Data
object:
>>> importnetworkxasnx>>> importnetworkx_temporalastx>>>>>> G=nx.karate_club_graph()>>> data=tx.convert(G,"torch_geometric")>>>>>> print(G)>>> print(data)Graph named "Zachary's Karate Club" with 34 nodes and 78 edgesData(edge_index=[2, 156], club=[34], weight=[156], name='Zachary's Karate Club', num_nodes=34)
Parameters:
G (object) – Graph object. Accepts a TemporalGraph, a
single static NetworkX graph, or a list of static NetworkX graphs as input.
to (str) – Package name or alias to convert the graph object.
args – Additional positional arguments for the conversion function.
kwargs – Additional keyword arguments for the conversion function.
Return type:
Any
Note:
Available both as a function and as a method from
TemporalGraph objects.
Returns ‘’flattened’’ version of temporal graph.
Equivalent to slice() with bins=1.
This method differs from to_static() only in
the sense that it returns a TemporalGraph object with a
single snapshot, rather than a static NetworkX graph object (i.e., the snapshot).
Attention
As each node in a flattened graph is unique, dynamic node attributes are not preserved.
Parallel edges from different snapshots are also not preserved, except for multigraphs.
Parallel (multiple) edges among nodes are converted to single edges, with a weight
attribute storing their total occurrences. If the attribute exists, their total
sum is stored instead.
Attention
Converting a multigraph to a graph object may result in data loss: multiple pairwise
edges are merged, with later attributes other than weight taking
precedence over earlier ones,
Example
Converting a static multigraph to a graph, summing the weights of parallel edges:
Slices temporal graph into snapshots, returning a new temporal graph object.
All node interactions are preserved when using this method. Note that the returned object may
contain temporal node copies, if they interact with other nodes in different intervals.
If attr is unset, the method will consider the order of appearance of edges or nodes, i.e.,
rank_first=True. If attr is set and bins is unset, the method will create as many
snapshots as unique values found in the specified node- or edge-level attr attribute data.
Alternatively, a list of 2-tuple intervals may be provided with custom time windows.
Hint
By default, views
of the original graph are returned to avoid memory overhead. If the resulting
snapshots need to be modified, set as_view=False to return copies instead.
Example
Slicing a temporal graph into two snapshots based on the edge-level time attribute:
>>> importnetworkx_temporalastx>>>>>> TG=tx.TemporalGraph()>>>>>> TG.add_edge("a","b",time=0)>>> TG.add_edge("c","b",time=1)>>>>>> TG=TG.slice(attr="time")>>> print(TG)TemporalGraph (t=2) with 3 nodes and 2 edges
Calling this method from the returned object, now with bins=1, will
flatten() the graph:
>>> TG=TG.slice(bins=1)>>> print(TG)TemporalGraph (t=1) with 3 nodes and 2 edges
Note that a TemporalMultiGraph or
TemporalMultiDiGraph object is required to store multiple
edges among pairs and allow many interactions between the same nodes.
bins (int | None) – Number of snapshots (slices) to return.
If unset, corresponds to the number of unique attribute values defined by attr.
Required only if attr is unset, otherwise optional.
attr (str | list | dict | DataFrame | Series | None) – Node- or edge-level attribute to
consider as temporal data. If unset, the method will consider the order of appearance of
edges or nodes (rank_first=True).
level (str) –
Whether to consider node- or edge-level data for slicing. Required if attr
is a string. Defaults to 'edge' if unset. Choices:
'edge': Edge-level temporal slice. This is the default.
'node': Node-level temporal slice. Alias for 'source'.
'source': Node-level slice with temporality defined by the source node
(pairwise interaction time defined by the source node attr).
'target': Node-level slice with temporality defined by the target node
(pairwise interaction time defined by the target node attr).
axis (int) – Whether bins should be created by time intervals (0) or by number of
nodes/edges (1). Default is 0.
qcut (bool) – If True, applies quantile-based discretization
for snapshots. Only has an effect if attr is set. Default is False.
duplicates (str) – Control whether slices containing duplicates raise an error.
Accepts 'drop' or 'raise'. Default is 'raise'.
rank_first (bool | None) – If True, applies rank-based sort
by order of appearance of edges, nodes, or an attribute. Automatically set as
True if both attr and rank_first are unset, otherwise False.
sort (bool) – If True, sort unique temporal values after slicing.
Only applies to categorical temporal data. Default is True.
as_view (bool) – If False, returns copies instead of views
of the original graph. Default is True.
fillna (Any | None) – Value to fill null values in attribute data.
apply_func (Callable) – Function to apply to temporal attribute values.
where \(i\) is a node,
\(j\) is a node in the neighborhood \(\mathcal{N}(i)\),
\(G\) is a snapshot in the temporal graph \(\mathcal{G}\),
and \(\mathbf{A}\) is the adjacency matrix, optionally weighted by an edge-level
weight attribute. For multigraphs, adjacencies are weighted by the number of
parallel edges, unless weight=False.
Parameters:
TG (object) – A TemporalGraph or static NetworkX graph
object.
nbunch (Any | None) – One or more nodes to return. Optional.
where \(i\) is a node,
\(j\) is a node in the neighborhood \(\mathcal{N}(i)\),
\(G\) is a snapshot in the temporal graph \(\mathcal{G}\),
and \(\mathbf{A}\) is the adjacency matrix, optionally weighted by an edge-level
weight attribute. For multigraphs, adjacencies are weighted by the number of
parallel edges, unless weight=False.
Parameters:
TG (object) – A TemporalGraph or static NetworkX graph
object.
nbunch (Any | None) – One or more nodes to return. Optional.
where \(i\) is a node,
\(j\) is a node in the neighborhood \(\mathcal{N}(i)\),
\(G\) is a snapshot in the temporal graph \(\mathcal{G}\),
and \(\mathbf{A}\) is the adjacency matrix, optionally weighted by an edge-level
weight attribute. For multigraphs, adjacencies are weighted by the number of
parallel edges, unless weight=False.
Parameters:
TG (object) – A TemporalGraph or static NetworkX graph
object.
nbunch (Any | None) – One or more nodes to return. Optional.
3-tuples (\(u, v, t\)), where elements are the source node, target node, and time
attribute;
4-tuples (\(u, v, t, \delta\)), where \(\delta\) is either an
integer for edge addition (1) or deletion (-1) event, or a float defining the
duration of the interaction (zero for a single snapshot).
Attention
As events are edge-based, node isolates without self-loops are not preserved.
Defines which additional parameter \(\delta\) should be returned.
If None, returns events as 3-tuples. Default.
If 'int', returns events as 4-tuples with an additional parameter representing
edge addition (1) or deletion (-1) events.
If 'float', returns events as 4-tuples with an additional parameter representing
the duration of the pairwise interaction.
attr (str | None) – Edge attribute to consider when delta is 'float'. If provided,
the attribute value is used for delta instead of the time difference between
the start and end of the interaction.
Note:
Available both as a function and as a method from
TemporalGraph objects.
Returns a multigraph from a graph object. SImilar to
The from_multigraph().
A multigraph is a graph that allows multiple (parallel) edges between pairwise nodes.
Attention
This function does not duplicate edges with a weight attribute larger than one, but
simply converts the graph to a multigraph format, allowing for parallel edges to be added.
Parameters:
G (object) – TemporalGraph
or static NetworkX graph object.
Returns a list of snapshots. Each snapshot is a view of the original graph, which can be
converted to a different format using the to argument, if desired.
Note
Internally, TemporalGraph already stores data as a
list of graph views on slice(). This method
simply returns the underlying data, unless convert()
is called by setting to.
A static graph is a single object that contains all the nodes and edges of
the temporal graph. If directed and multigraph are unset, the
returned graph type will match that of the temporal graph. Specifying attr
allows to store the time of interaction as an edge attribute.
Attention
As each node in a static graph is unique, dynamic node attributes are not preserved.
See also
The to_unrolled() method for a static
representation allowing dynamic node attributes.
An unrolled temporal graph is a single graph containing all nodes and edges in all
snapshots, plus time-adjacent node copies and edge couplings which connect these copies.
If delta is set, edges are added between temporal nodes in sequential time steps,
\((u_{t}, v_{t+\delta})\), resulting in time series representations [0] of temporal
networks.
Creates a temporal directed graph. Does not allow parallel edges among node pairs.
It inherits a static NetworkX DiGraph
and includes all methods available from it, such as add_node(), add_edge(),
neighbors(), subgraph(), to_directed(), and to_undirected(),
as well as additional methods implemented for handling temporal graphs and snapshots.
This is equivalent to calling temporal_graph() with directed=True
and multigraph=False.
See also
The TemporalGraph class documentation
for details on its implemented methods.
Parameters:
t (int) – Number of temporal graphs to initialize. Optional. Default is 1.
Note:
Documentation on inherited methods is available only if networkx>=2.8.1.
Creates a temporal undirected multigraph. Allows parallel edges among the same pair of nodes.
It inherits a static NetworkX MultiGraph
and includes all methods available from it, such as add_node(), add_edge(),
neighbors(), subgraph(), to_directed(), and to_undirected(),
as well as additional methods implemented for handling temporal graphs and snapshots.
This is equivalent to calling temporal_graph() with directed=False
and multigraph=True.
See also
The TemporalGraph class documentation
for details on its implemented methods.
Parameters:
t (int) – Number of temporal graphs to initialize. Optional. Default is 1.
Note:
Documentation on inherited methods is available only if networkx>=2.8.1.
Creates a temporal directed multigraph. Allows parallel edges among the same pair of nodes.
It inherits a static NetworkX MultiDiGraph
and includes all methods available from it, such as add_node(), add_edge(),
neighbors(), subgraph(), to_directed(), and to_undirected(),
as well as additional methods implemented for handling temporal graphs and snapshots.
This is equivalent to calling temporal_graph() with directed=True
and multigraph=True.
See also
The TemporalGraph class documentation
for details on its implemented methods.
Parameters:
t (int) – Number of temporal graphs to initialize. Optional. Default is 1.
Note:
Documentation on inherited methods is available only if networkx>=2.8.1.
Returns the union of two graphs.
For temporal graphs, the snapshots of each graph are concatenated in order,
so that the resulting object contains all input graph snapshots.
Parameters:
G1 (object) – TemporalGraph
or static NetworkX graph object.
G2 (object) – TemporalGraph
or static NetworkX graph object.
Returns the union of multiple graphs.
For temporal graphs, the snapshots of each graph are concatenated in order,
so that the resulting object contains all input graph snapshots.
Parameters:
graphs (object) – A list of TemporalGraph
or static NetworkX graph objects.
Parallel (multiple) edges among nodes are converted to single edges, with a weight
attribute storing their total occurrences. If the attribute exists, their total
sum is stored instead.
Attention
Converting a multigraph to a graph object may result in data loss: multiple pairwise
edges are merged, with later attributes other than weight taking
precedence over earlier ones,
Example
Converting a static multigraph to a graph, summing the weights of parallel edges:
A frozen graph is immutable, meaning that nodes and edges cannot be added or removed.
Calling copy on a frozen graph returns a (mutable) deep copy of the graph object.
Parameters:
TG (object) – A TemporalGraph or static NetworkX graph
object.
Returns True if static graph is an unrolled temporal graph.
Unrolled graphs are a static representation of temporal networks, where each node is suffixed
with its temporal index (e.g., 'a_0') and inter-slice edges are added to connect copies of
the same node at different time steps (e.g., 'a_0' and 'a_1').
Setting t as greater than 1 initializes a list of NetworkX graph objects, each
representing a snapshot in time. Unless dynamic node attributes are required, it is
recommended to use the slice() method instead,
allowing to create less resource-demanding graph views
on the fly.
Example
Creating a temporal directed multigraph with a single snapshot:
>>> importnetworkx_temporalastx>>>>>> TG=tx.temporal_graph(directed=True,multigraph=True)>>> print(TG)TemporalMultiDiGraph (t=1) with 0 nodes and 0 edges
Parameters:
t (int) – Number of snapshots to initialize. Optional. Default is 1.
directed (bool) – If True, inherits a
DiGraph.
Defaults to False.
multigraph (bool) – If True, inherits a
MultiGraph.
Defaults to True.
create_using (object) – NetworkX or TemporalGraph to use
as template. Optional. Does not allow setting directed and multigraph if passed.
Returns a multigraph from a graph object. SImilar to
The from_multigraph().
A multigraph is a graph that allows multiple (parallel) edges between pairwise nodes.
Attention
This function does not duplicate edges with a weight attribute larger than one, but
simply converts the graph to a multigraph format, allowing for parallel edges to be added.
Parameters:
G (object) – TemporalGraph
or static NetworkX graph object.