straph package

Submodules

straph.stream module

straph.stream.DFS_iterative(v, Neighborhood)[source]

Performs a Depth First search from the temporal node v

Parameters
  • v – Source node for the DFS, a temporal node (t0,t1,u)

  • Neighborhood – Neighborhood of the Stream Graph : (t0,t1,u) -> [(t0,t1,w),….]

Returns

The weakly connected component containing v along with visited nodes (other temporal nodes in the wcc)

class straph.stream.StreamGraph(id=None, times=None, nodes=None, node_to_label=None, node_to_id=None, node_presence=None, links=None, link_presence=None, weights=None, trips=None)[source]

Bases: object

A stream graph is a tool tailored to analyse and model sequences of links or, continuous sequences of graphs.

Add a new link to the stream graph. If the extremities are not already present we add them to the stream graph. Their presence times will be the one the link.

Parameters
  • link – (A,B) where ‘A’ and ‘B’ are the nodes’ labels

  • link_presence – Presence times of the added link [b,e,b’,e’,…]

Returns

add_node(node, node_presence)[source]

Add a node to the stream graph, if not already present : this new node will be represented by a an integer, corresponding to the length of self.nodes

Parameters
  • node – Label of the node to add

  • node_presence – Presence times of the added node [b,e,b’,e’,…]

Returns

The id corresponding to the added node

add_nodes(nodes_list, node_presence_list)[source]
aggregated_graph(label=True, to_networkx=False, to_networkit=False)[source]

Return the aggregated induced graph.

Parameters
  • to_networkit

  • to_networkx

  • label – True if we want the node’s label in the adjacency list.

Returns

An adjacency list (networkx compatible)

all_cliques(stable_dag=None, n_jobs=- 1, format='dict')[source]

Return all the cliques of the Stream Graph

Parameters
  • format

  • format

  • n_jobs

  • stable_dag

Returns

animated_plot(repeat=False, cmap='nipy_spectral', fontsize=26, layout=None)[source]

Display an animation of a stream graph from its induced graphs

Parameters
  • repeat

  • cmap

  • fontsize

Returns

Returns

average_clique_size(n_jobs=- 1)[source]
average_clustering(cc=None)[source]

The average clustering coefficient of a stream graph

Returns

average_core_size(n_jobs=- 1)[source]
average_degree(degrees=None, nb_nodes=None)[source]

The average degree of a stream graph

Parameters
  • degrees

  • nb_nodes

Returns

check_integrity()[source]

Check node presence and link presence for overlapping time windows Check that a link presence include both node presence

Returns

True if the structure is coherent or an error with the problematic link/node.

clustering_coefficient()[source]

A dictionary with for each node his clustering coefficient

Returns

condensation_dag(format='object_with_links', free_memory=False, isolated_nodes=True)[source]

Compute the Condensation Graph of the Stream Graph.

Returns

Condensation Graph

core_number(stable_dag=None, n_jobs=- 1)[source]

Compute the core number of each temporal node.

Parameters
  • n_jobs

  • stable_dag – Stable DAG (optional)

Returns

coverage()[source]

Return the coverage of a stream graph object

Returns

degree_over_time(nodes=None, to_series=True, datetime=True, events=None, eps=None)[source]

Compute the degree over time (of the considered nodes) in the Stream Graph

Parameters
  • nodes

  • to_series

  • datetime

  • events

  • eps

Returns

A dictionary event time to counter of nodes degrees

degrees()[source]

Each node degree in the StreamGraph

Returns

A dictionary linking each node to its degree

degrees_partition(E=None)[source]
density()[source]

The density of the StreamGraph` object (aka the probability if we randomly chose two nodes that they are linked together)

Returns

the density as a float

describe()[source]

Print a global description of a StreamGraph object in terms of number of nodes and links.

Returns

None

distances(source=None, destination=None, enumerate=False, E=None)[source]

Compute temporal paths in the Stream Graph. If both source and destination are specified: return the distance of the shortest path If only source is specified: return distances to all other nodes (single source) If source and destination aren’t specified: return distances pairwise.

Parameters
  • E

  • source – Can be a source node or a temporal source node(t_0,t_1,u)

  • destination – Can be a destination node or a temporal destination node(t_0,t_1,v)

  • enumerate – if True we enumerate the number of path

Returns

Distances

distances_and_durations(source=None, destination=None, enumerate=False, E=None)[source]

Compute temporal paths in the Stream Graph. If both source and destination are specified: return the distance and duration of the fastest shortest path If only source is specified: return distances and durations to all other nodes (single source) If source and destination aren’t specified: return distances and durations pairwise.

Parameters
  • E

  • source – Can be a source node or a temporal source node(t_0,t_1,u)

  • destination – Can be a destination node or a temporal destination node(t_0,t_1,v)

  • enumerate – if True we enumerate the number of path

Returns

Distances and durations

duration()[source]

Return the total duration of a stream graph object

Returns

event_times(all=False)[source]

Return distinct event times in the Stream Graph

Parameters

all

Returns

expected_node_degrees(d=None)[source]
expected_stream_graph_degree()[source]
extend_on_node_presence()[source]

Return the sub stream induced by links in links_list and nodes implicated in these links.

Parameters

links_list – list of links to filter by

Returns

Return the sub stream induced by links in links_list and nodes implicated in these links.

Parameters

links_list – list of links to filter by

Returns

Return the sub stream induced by links in links_list and nodes implicated in these links.

Parameters

links_list – list of links to filter by

Returns

filter_by_node_id(nodes_target)[source]

Return the sub stream graph induced by nodes in nodes_target. Different from induced stream graph (that include nodes in links containing a node in nodes_list)

Parameters

nodes_target

Returns

filter_by_node_label(nodes_target)[source]

Return the sub stream graph induced by nodes in nodes_target.

Parameters

nodes_target

Returns

filter_by_nodes(nodes_list)[source]

Return the sub stream graph induced by nodes in nodes_target. If an extremity of a link is in nodes_list we add the other node. Different from induced stream graph

Parameters

nodes_list

Returns

filter_by_time_window(a, b)[source]

Return the sub stream induced by T = [a,b].

Parameters
  • a

  • b

Returns

Get both extremities (which are maximal segmented nodes) of the link.

Parameters
  • l

  • t0 – Beginning of the link

  • t1 – Ending of the link

Returns

get_card_E()[source]

Return the sum temporal links presence time ($|E|$)

Returns

get_card_W()[source]

Return the sum of temporal nodes presence time ($|W|$)

Returns

get_interaction_times()[source]

O(M)

Returns

get_node_presence_from_interval(n, b, e)[source]

Return the maximal segmented node corresponding to (b,e,n)

Parameters
  • n – node

  • b – beginning of the interval (time)

  • e – ending of the interval (time)

Returns

Maximal segmented node presence corresponding to (b,e,n) : (t0,t1)

graph_communities(community_function='louvain', node_list=None, n_jobs=- 1, postprocess=True, sdag=None, alpha=0.7)[source]

Compute communities in a Stream Graph according to a community function

Parameters
  • alpha

  • sdag

  • community_function

  • node_list

  • n_jobs

  • postprocess

Returns

graph_property(node_property_function_nx, node_list=None, format='cluster', stable_components=None, postprocess=True, n_jobs=- 1, datetime=True, withnan=True, eps=None)[source]
induced_line_stream()[source]

The induced line stream (which is a stream graph too) corresponding to the stream graph

Returns

Return the sub stream induced by links in links_list and nodes implicated in these links.

Parameters

links_list – list of links ((id1,id2) or (label1,label2))

Returns

A Stream Graph

induced_substream_by_nodes(nodes_list)[source]

Return the sub stream induced by nodes_list. IMPORTANT: Only include links with both extremities in nodes_list. (see filter_links otherwise)

Parameters

nodes_list – A list of nodes (Ids or Labels)

Returns

A Stream Graph

induced_substream_by_time_window(time_window)[source]

Return the sub stream induced by T = [a,b].

Parameters

time_window – The time_window that delimit the substream

Returns

A Stream Graph

instant_graph(t, label=True, to_networkx=False, to_networkit=False)[source]

Return an adjacency list corresponding to the induced graph from the stream graph at a specified time t.

Parameters
  • to_networkit

  • to_networkx

  • t – Time instant

  • label – True if we want the node’s label in the adjacency list.

Returns

An adjacency list (networkx compatible)

isolated_nodes(node_list=None)[source]

Get isolated temporal nodes (node with temporal degree == 0)

Returns

k_clique(k, stable_dag=None, n_jobs=- 1)[source]

Compute the k-cliques of the Stream Graph

Parameters
  • n_jobs

  • k

  • stable_dag

Returns

k_core(k, stable_dag=None, n_jobs=- 1)[source]

Compute the k-core cluster of the Stream Graph.

Parameters
  • n_jobs

  • kk-core

  • stable_dag – CondensationDag: Condensation DAG (optional)

Returns

latencies(source=None, destination=None, enumerate=False, E=None)[source]

Compute temporal paths in the Stream Graph. If both source and destination are specified: return the latency of the fastest path If only source is specified: return latencies to all other nodes (single source) If source and destination aren’t specified: return latencies pairwise.

Parameters
  • E

  • source – Can be a source node or a temporal source node(t_0,t_1,u)

  • destination – Can be a destination node or a temporal destination node(t_0,t_1,v)

  • enumerate – if True we enumerate the number of path

Returns

Latencies

latencies_and_lengths(source=None, destination=None, enumerate=False, E=None)[source]

Compute temporal paths in the Stream Graph. If both source and destination are specified: return the latency_and_length of the shortest fastest path If only source is specified: return latencies and lengths to all other nodes (single source) If source and destination aren’t specified: return latencies and lengths pairwise.

Parameters
  • E

  • source – Can be a source node or a temporal source node(t_0,t_1,u)

  • destination – Can be a destination node or a temporal destination node(t_0,t_1,v)

  • enumerate – if True we enumerate the number of path

Returns

Latencies and lengths

Return the link densities of a StreamGraph object

Returns

The link’s densities, each link is associated to a float

The link duration of a StreamGraph

Returns

a float

sort links by their arrival

Returns

sort links by their duration

Returns

For the tool stream-graph-visualisation.

Returns

link presence in JSON

Return the weight of links at instant t

Parameters

t

Returns

Display the link weigth over a given interval I

Parameters

I

Returns

Compute the number of active links over time in the Stream Graph.

Parameters
  • to_series – return a pandas series

  • datetime – A boolean value to display timestamp as readable data

  • events

  • eps

Returns

A dictionnary event time to the corresponding number of nodes

louvain_communities(stable_dag=None, n_jobs=- 1)[source]
max_clique_number(stable_dag=None, n_jobs=- 1)[source]

Return the size of the maximal clique for each node (in the form of temporal windows : a cluster)

Returns

mean_degree_neighborhood(degrees=None)[source]
mean_degree_over_time(to_series=True, datetime=True, events=None, eps=None)[source]

Compute the mean degree over time in the Stream Graph.

Parameters
  • to_series

  • datetime

  • events

  • eps

Returns

A dictionnary event time to the corresponding number of nodes

nb_cc_from_adjacency_list(a_l)[source]

The number of links according to their presence in the stream graph

Returns

nb_neighbors()[source]

Return the number of nieghbors of each node

Returns

nb_nodes()[source]

The number nodes according to their presence in the stream graph

Returns

neighborhood()[source]

Neighborhood of each node (each node –> their neighbors with the associated interval of time (corresponding to sum of temporal link))

Returns

the neighborhood of each node in a dictionary

neighborhood_from_node(node)[source]

The neighborhood of a specific node (node <-> their neighbor with the associated interval of time)

Parameters

node – a node of the StreamGraph

Returns

The neighborhood of a specific node in a dictionary

neighborhood_with_node_presence()[source]

The neighborhood of each node globally

Returns

a dictionary (each node –> their neighbors with their presences)

newman_communities(stable_dag=None, n_jobs=- 1)[source]
node_activity_to_json(node_to_position=None, sort_type=None)[source]

For the tool stream-graph-visualisation.

Returns

node activity in JSON

node_densities()[source]

Return the node densities of a StreamGraph object

Returns

node_duration()[source]

The node duration of a StreamGraph

Returns

a float

node_position_by_arrival()[source]

Sort nodes by their first time of arrival

Returns

node_position_by_increasing_degree(degrees_partition=None)[source]

Sort nodes by their degree ;

Parameters

degrees_partition

Returns

node_position_by_peak_degree_arrival(degrees_partition=None)[source]

Sort nodes increasingly by their maximal value of degree arrival time

Returns

node_weight_at_t(t)[source]

Return the weight of nodes at instant t

Parameters

t

Returns

node_weight_on_I(I)[source]

Display the node weigth over a given interval I

Parameters

I

Returns

nodes_average_core_number(core_number=None)[source]
nodes_degree(interaction_times=None)[source]
nodes_max_degree()[source]
nodes_over_time(to_series=True, datetime=True, events=None, eps=None)[source]

Compute the number of nodes over time in the Stream Graph.

Parameters
  • to_series – return a pandas series

  • datetime – A boolean value to display timestamp as readable data

  • events

  • eps

Returns

A dictionnary linking event time to the corresponding number of nodes

nodes_partition_on_degrees(interaction_times=None)[source]
number_of_connected_components_in_snapshots()[source]
number_of_event_times()[source]

Return the number of distinct event times in the Stream Graph

Returns

number_of_stable_connected_component(format='cluster')[source]

Return the number of strongly connected components in the stream graph.

Returns

number_of_strongly_connected_component(format='cluster')[source]

Return the number of strongly connected components in the stream graph.

Returns

number_of_weakly_connected_component()[source]

Return the number of weakly connected components in the stream graph.

Returns

ordered_arrivals(free_memory=False)[source]

Return an ordered list of arrival events (node or link arrival) occuring in the StreamGraph.

Returns

ordered_batch_events()[source]

Return an ordered of batch events (event happening at the same time). In each batch there can be (in the same order): node arrival, link arrival, link departure, node departure.

Returns

ordered_events(weights_or_trips=False)[source]

Return an ordered of all the events (nodes and links arrivals or departure) occuring in the stream graph.

Parameters

weights_or_trips

Returns

Returns

plot(clusters=None, nodes_list=None, plot_links=True, title=None, fontsize=26, legend=True, timestamp=False, cmap=None, arrivals_marker=None, lw_nodes=3, lw_links=2.5, ax=None)[source]

Display in an elegant way a (small) stream graph using matplotlib.

Parameters
  • clusters – A list of clusters that can represents temporal nodes (a list of (t0,t1,u)) or a dictionnary linking temporal nodes to a given value.

  • nodes_list – A list of nodes to display, will ignore nodes not in this list.

  • plot_links – A boolean value to plot temporal links or not

  • title – An optional title

  • fontsize

  • legend – A boolean value to display a legend, a colorbar for the current displayed property (only available with the option clusters and clusters must be a dictionary)

  • timestamp – A boolean value to convert the x-axis (the time) into human readable timstamp (dates).

  • cmap – A colormap that will be used to display clusters

  • arrivals_marker – A boolean value to plot or not link arrivals with a marker

  • lw_nodes – linewidth of temporal nodes

  • lw_links – linewidth of temporal links

  • ax – A matplotlib axe object

Returns

A matplotlib axe object

plot_3d(fontsize=26)[source]

Experimental visualisation of a StreamGraph object in a three dimensionnal space.

Parameters

fontsize

Returns

plot_aggregated_graph(fontsize=26, title=None, lw_links=5, node_size=900, layout=None)[source]

Display the aggregated graph of a StreamGraph

Parameters

fontsize

Returns

A matplotlib figure object

plot_instant_graph(time=0, node_size=1000, fontsize=26, title=None, layout=None)[source]

Plot the induced static graph at time t

Parameters
  • time – an instant (a timestamp)

  • node_size – graphic parameters for the size of nodes

  • fontsize

Returns

A matplotlib figure object

Display the link weight over time

Returns

plot_node_weight()[source]

Display the node weight over time

Returns

property_to_cluster(node_property_function_nx, n_jobs, postprocess, stable_components, node_list)[source]
property_to_signal(node_property_function_nx, datetime, eps, n_jobs, postprocess, stable_components, withnan, node_list)[source]

Remove the (link,time_presence) from the Stream Graph

Parameters

l

Returns

Remove links in the set links_to_remove from the Stream Graph

Parameters

links_to_remove

Returns

remove_node(n)[source]

Remove the node n from the Stream Graph

Parameters

n – a node

Returns

remove_nodes(nodes_to_remove)[source]

Remove all nodes in nodes_to_remove from the Stream Graph

Parameters

nodes_to_remove – a set of nodes

Returns

segmented_neighborhood()[source]

On veut des segments de noeuds comme clefs ainsi que dans les voisins (b,e,v) : [(t0,t1,(nt0,nt1,u))] :)

Returns

stable_connected_components(format='object_with_links', node_list=None)[source]
Parameters
  • node_list

  • format – “cluster” to obtain StCC under the form of [(t0,t1,u),…] “object” to obtain StCC under the form of objects of class “connected_components”

Returns

stable_dag(format='object_with_links', free_memory=False, isolated_nodes=True, node_list=None)[source]

Compute the Stable DAG of the Stream Graph.

Parameters
  • format

  • free_memory

  • isolated_nodes

  • node_list

Returns

stream_graph_degree()[source]
strongly_connected_components(format='object', streaming_output=None, method='Direct', isolated_nodes=True, free_memory=False)[source]

Compute the strongly connected components of the Stream Graph

Parameters
  • free_memory

  • isolated_nodes

  • method

  • streaming_output

  • format – “cluster” to obtain SCC under the form of [(t0,t1,u),…] “object” to obtain SCC under the form of objects of class “strongly_connected_components”

Returns

substream(cluster, substream_id=None, return_new_node_label=False)[source]

Return the substream corresponding to the cluster: A Stream Graph containing the nodes of the cluster along with the segmented links that exist between them.

Parameters
  • return_new_node_label

  • cluster – A sequence of node segments: [(t0,t1,u),…,(t0,t1,v)]

  • substream_id – (optional parameter) assign an id to the substream (usefull when dealing with components)

Returns

sum_degree_neighborhood(degrees=None)[source]
surface()[source]

Return the surface of a stream graph object

Returns

times_to_reach(source=None, destination=None, start_time=None, enumerate=False, E=None)[source]

Compute temporal paths in the Stream Graph. If both source and destination are specified: return the time to reach of the foremost path If only source is specified: return time to reach all other nodes (single source) If source and destination aren’t specified: return times to reach pairwise.

Parameters
  • E

  • start_time

  • source – Can be a source node or a temporal source node(t_0,t_1,u)

  • destination – Can be a destination node or a temporal destination node(t_0,t_1,v)

  • enumerate – if True we enumerate the number of path

Returns

Times to reach

times_to_reach_and_lengths(source=None, destination=None, start_time=None, enumerate=False, E=None)[source]

Compute temporal paths in the Stream Graph. If both source and destination are specified: return the ttr and distance of the shortest foremost path If only source is specified: return ttr and distances to all other nodes (single source) If source and destination aren’t specified: return ttr and distances pairwise.

Parameters
  • E

  • start_time

  • source – Can be a source node or a temporal source node(t_0,t_1,u)

  • destination – Can be a destination node or a temporal destination node(t_0,t_1,v)

  • enumerate – if True we enumerate the number of path

Returns

Times to reach and distances

to_snapshots()[source]
uniformity()[source]

Return the uniformity of a StreamGraph object

Returns

the uniformity as a float

wcc_as_substreams(n_jobs=- 1)[source]

Return the weakly connected components of the stream graph in the form of substreams (stream graph induced by the component/cluster)

Parameters

n_jobs – Number of cores to allocate for a parallel computation

Returns

weakly_connected_components(streaming=False, reformat=False, free_memory=False)[source]

Compute the weakly connected components of the Stream Graph. Time Complexity in $O(M+N)$.

Parameters
  • streaming

  • reformat

  • free_memory

Returns

Weakly Connected Components of the Stream Graph (a list of clusters : [[(t0,t1,u),…],[(t0,t1,v)]])

write_to_csv(output_name)[source]

Write the stream graph to CSV format(node1;node2;start_time;duration).

Parameters

output_name

Returns

write_to_json(output_name, index_pos=None)[source]

Write to a JSON format. For the tool stream-graph-visualisation.

Parameters
  • output_name – Name prefix of json files. Will be stored under “output_name_node_activity.json” and “output_name_link_presence.json”

  • index_pos

Returns

write_to_lsf(output_name)[source]
write_to_sg(output_file)[source]

tb : time of arrival (b: begin) te : time of departure (e: end) Output format : node file: id_node1 tb_0 te_0 tb_1 te_1 … tb_n1 te_n1 id_node2 tb_0 te_0 … … link file: id_node1 id_node2 tb_0 te_0 tb_1 te_1 … tb_l1 te_l1 id_node3 id_node4 tb_0 te_0 tb_1 te_1 … tb_l2 te_l2 …

Parameters

output_file – path to store nodes, links and their time of presence

Returns

straph.stream.adjacency_list_to_json(a_l)[source]
straph.stream.algo_kcores_batagelj(a_l, degrees)[source]

Compute k_cores of a static graph from its adjacency list and nodes degrees .. rubric:: References

[1] An O(m) Algorithm for Cores Decomposition of Networks Vladimir Batagelj and Matjaz Zaversnik, 2003. http://arxiv.org/abs/cs.DS/0310049

Parameters
  • a_l

  • degrees

Returns

straph.stream.clusters_to_signals(S, dict_clusters, datetime=False, withnan=True)[source]

Process clusters (a list of temporal nodes [(t0,t1,u),…]) into a signal (a pandas Series)

Parameters
  • withnan

  • datetime

  • S

  • dict_clusters

Returns

straph.stream.condensation_dag_from_wcc(list_WCC)[source]

Get the corresponding condensation DAG from a list of Weakly Connected Components (Stream Graphs Objects)

Parameters

list_WCC

Returns

straph.stream.get_index_node_to_id_wcc(list_WCC)[source]

For source node paths

Parameters

list_WCC

Returns

straph.stream.get_index_segmented_node_to_id_wcc(list_WCC)[source]

for temporal source node paths

Parameters

list_WCC

Returns

straph.stream.merge_clusters_into_communities(sdag, cluster_id_to_community, prev_id, next_id, cnt_com, alpha, done_clusters)[source]

Merge Two Adjacent Clusters into a same community if they share an important number of nodes

Parameters
  • sdag

  • cluster_id_to_community

  • prev_id

  • next_id

  • cnt_com

  • alpha

  • done_clusters

Returns

straph.stream.performance_test_path_functions(S, path_type, random_nodes=False, high_degree_nodes=False)[source]
straph.stream.postprocess_clusters(dict_clusters)[source]

Merge adjacent clusters withe the same value

Parameters

dict_clusters – Dictionnary : prop -> clusters

Returns

straph.stream.postprocess_communities(sdag, alpha=0.7)[source]
straph.stream.postprocess_kcliques_into_clusters(Kcliques)[source]

We only keep the biggest ‘K’ for each segmented temporal nodes

Parameters

Kcliques

Returns

straph.stream.read_stream_graph(path_links, path_nodes=None, node_label=True, path_weights=None, path_trips=None)[source]

tb : time of arrival (b: begin) te : time of departure (e: end) Input format : node file: id_node1 tb_0 te_0 tb_1 te_1 … tb_n1 te_n1 id_node2 tb_0 te_0 … … link file: id_node1 id_node2 tb_0 te_0 tb_1 te_1 … tb_l1 te_l1 id_node3 id_node4 tb_0 te_0 tb_1 te_1 … tb_l2 te_l2 …

Parameters
  • path_trips

  • path_weights

  • node_label

  • path_links – path to store nodes and their time of presence

  • path_nodes – path to store links and their time of presence

Returns

straph.stream.read_stream_graph_from_json(path_nodes, path_links)[source]

Parse a “.json” file and return a stream graph.

Parameters
  • path_nodes

  • path_links

Returns

straph.stream.stream_graph_from_events_list(list_events, stream_id=None)[source]

Construct a StreamGraph from a list of events

Parameters
  • list_events

  • stream_id

Returns

straph.stream.sum_presence(np)[source]
straph.stream.test_all_fastest_paths_dag(S)[source]
straph.stream.write_adjacency_list_to_json(a_l, path_json)[source]

Module contents