Skip to content

Graph metrics and functions

Basic metrics

Now that we have our graph let's start probing it for some basic metrics, such as how many nodes and edges it contains and the time range over which it exists.

Note, as the property APIs are the same for the graph, nodes and edges, these are discussed together in Property queries.

Info

In the below code segment you will see the functions count_edges() and count_temporal_edges() being called and returning different results. This is because count_edges() returns the number of unique edges and count_temporal_edges() returns the total edge updates which have occurred.

The second is useful if you want to imagine each edge update as a separate connection between the two nodes. The edges can be accessed in this manner via edge.explode(), as is discussed in edge metrics and functions.

Graph

print("Stats on the graph structure:")

number_of_nodes = g.count_nodes()
number_of_edges = g.count_edges()
total_interactions = g.count_temporal_edges()
unique_layers = g.unique_layers

print("Number of nodes (Baboons):", number_of_nodes)
print("Number of unique edges (src,dst,layer):", number_of_edges)
print("Total interactions (edge updates):", total_interactions)
print("Unique layers:", unique_layers, "\n")


print("Stats on the graphs time range:")

earliest_datetime = g.earliest_date_time
latest_datetime = g.latest_date_time
earliest_epoch = g.earliest_time
latest_epoch = g.latest_time

print("Earliest datetime:", earliest_datetime)
print("Latest datetime:", latest_datetime)
print("Earliest time (Unix Epoch):", earliest_epoch)
print("Latest time (Unix Epoch):", latest_epoch)

Output

Stats on the graph structure:
Number of nodes (Baboons): 22
Number of unique edges (src,dst,layer): 290
Total interactions (edge updates): 3196
Unique layers: ['_default', 'Grooming', 'Resting', 'Presenting', 'Playing with', 'Grunting-Lipsmacking', 'Supplanting', 'Threatening', 'Submission', 'Touching', 'Avoiding', 'Attacking', 'Carrying', 'Embracing', 'Mounting', 'Copulating', 'Chasing'] 

Stats on the graphs time range:
Earliest datetime: 2019-06-13 09:50:00
Latest datetime: 2019-07-10 11:05:00
Earliest time (Unix Epoch): 1560419400000
Latest time (Unix Epoch): 1562756700000

Accessing nodes and edges

Three types of functions are provided for accessing the nodes and edges within the graph:

  • Existance check: Via has_node() and has_edge() you can check if an entity is present within the graph.
  • Direct access: node() and edge() will return a node/edge object if the entity is present and None if it is not.
  • Iterable access: nodes and edges will return iterables for all nodes/edges which can be used within a for loop or as part of a function chain.

All of these functions are shown in the code below and will appear in several other examples throughout this tutorial.

Graph

print("Checking if specific nodes and edges are in the graph:")
if g.has_node(id="LOME"):
    print("Lomme is in the graph")
if g.has_edge(src="LOME", dst="NEKKE", layer="Playing with"):
    print("Lomme has played with Nekke \n")

print("Getting individual nodes and edges:")
print(g.node("LOME"))
print(g.edge("LOME", "NEKKE"), "\n")

print("Getting iterators over all nodes and edges:")
print(g.nodes)
print(g.edges)

Output

Checking if specific nodes and edges are in the graph:
Lomme is in the graph
Lomme has played with Nekke 

Getting individual nodes and edges:
Node(name=LOME, earliest_time=1560419520000, latest_time=1562756100000)
Edge(source=LOME, target=NEKKE, earliest_time=1560421080000, latest_time=1562755980000, properties={Weight: 1}) 

Getting iterators over all nodes and edges:
Nodes(Node(name=ANGELE, earliest_time=1560419400000, latest_time=1562754600000), Node(name=FELIPE, earliest_time=1560419400000, latest_time=1562756700000), Node(name=LIPS, earliest_time=1560419460000, latest_time=1562756700000), Node(name=NEKKE, earliest_time=1560419520000, latest_time=1562756700000), Node(name=LOME, earliest_time=1560419520000, latest_time=1562756100000), Node(name=BOBO, earliest_time=1560419520000, latest_time=1562755500000), Node(name=ATMOSPHERE, earliest_time=1560419640000, latest_time=1562683260000), Node(name=FEYA, earliest_time=1560420000000, latest_time=1562756040000), Node(name=FANA, earliest_time=1560420000000, latest_time=1562754600000), Node(name=PIPO, earliest_time=1560420660000, latest_time=1562752560000), ...)
Edges(Edge(source=ANGELE, target=FELIPE, earliest_time=1560419400000, latest_time=1562753640000, properties={Weight: 1}), Edge(source=LOME, target=FEYA, earliest_time=1560421260000, latest_time=1562328420000, properties={Weight: 1}), Edge(source=VIOLETTE, target=LIPS, earliest_time=1560423600000, latest_time=1560423600000, properties={Weight: -1}), Edge(source=PETOULETTE, target=VIOLETTE, earliest_time=1560438960000, latest_time=1562143080000, properties={Weight: 1}), Edge(source=MALI, target=VIOLETTE, earliest_time=1560441420000, latest_time=1562331780000, properties={Weight: 1}), Edge(source=FELIPE, target=MUSE, earliest_time=1560504540000, latest_time=1562332380000, properties={Weight: 1}), Edge(source=EWINE, target=PETOULETTE, earliest_time=1560507720000, latest_time=1562754420000, properties={Weight: 1}), Edge(source=EWINE, target=FANA, earliest_time=1560523200000, latest_time=1562681760000, properties={Weight: 1}), Edge(source=ANGELE, target=PETOULETTE, earliest_time=1560764760000, latest_time=1562249100000, properties={Weight: 1}), Edge(source=FELIPE, target=FANA, earliest_time=1560769440000, latest_time=1562754600000, properties={Weight: 1}), ...)