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.
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', 'Touching', 'Presenting', 'Attacking', 'Playing with', 'Chasing', 'Grunting-Lipsmacking', 'Mounting', 'Copulating', 'Avoiding', 'Embracing', 'Threatening', 'Supplanting', 'Submission', 'Carrying']
Stats on the graphs time range:
Earliest datetime: 2019-06-13 09:50:00+00:00
Latest datetime: 2019-07-10 11:05:00+00: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()
andhas_edge()
you can check if an entity is present within the graph. - Direct access:
node()
andedge()
will return a node/edge object if the entity is present andNone
if it is not. - Iterable access:
nodes
andedges
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.
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.layer("Playing with").has_edge(src="LOME", dst="NEKKE"):
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, Weight: 1, Weight: 1, Weight: 1, Weight: 1}, layer(s)=[Grooming, Resting, Touching, Playing with, Carrying])
Getting iterators over all nodes and edges:
Nodes(Node(name=MAKO, earliest_time=1560421620000, latest_time=1562756100000), Node(name=EWINE, earliest_time=1560442020000, latest_time=1562754600000), Node(name=FEYA, earliest_time=1560420000000, latest_time=1562756040000), Node(name=ANGELE, earliest_time=1560419400000, latest_time=1562754600000), Node(name=MUSE, earliest_time=1560421080000, latest_time=1562755500000), Node(name=PETOULETTE, earliest_time=1560422520000, latest_time=1562754420000), Node(name=FELIPE, earliest_time=1560419400000, latest_time=1562756700000), Node(name=NEKKE, earliest_time=1560419520000, latest_time=1562756700000), Node(name=LOME, earliest_time=1560419520000, latest_time=1562756100000), Node(name=MALI, earliest_time=1560422040000, latest_time=1562755320000), ...)
Edges(Edge(source=PIPO, target=KALI, earliest_time=1560420660000, latest_time=1562752560000, properties={Weight: 1, Weight: 1, Weight: 1, Weight: 1, Weight: 1, Weight: 1, Weight: 1}, layer(s)=[Grooming, Resting, Touching, Chasing, Grunting-Lipsmacking, Mounting, Copulating]), Edge(source=FELIPE, target=ANGELE, earliest_time=1560419460000, latest_time=1562754600000, properties={Weight: 1, Weight: 1, Weight: 1, Weight: 1, Weight: 1, Weight: 1, Weight: 1, Weight: 1, Weight: 1}, layer(s)=[Resting, Touching, Presenting, Chasing, Grunting-Lipsmacking, Mounting, Embracing, Supplanting, Submission]), Edge(source=FELIPE, target=LIPS, earliest_time=1560419460000, latest_time=1562251080000, properties={Weight: 1, Weight: 1}, layer(s)=[Resting, Touching]), Edge(source=ANGELE, target=FELIPE, earliest_time=1560419400000, latest_time=1562753640000, properties={Weight: 1, Weight: 1, Weight: 1, Weight: 1, Weight: 1, Weight: 1}, layer(s)=[Grooming, Resting, Presenting, Grunting-Lipsmacking, Copulating, Submission]), Edge(source=PETOULETTE, target=ARIELLE, earliest_time=1560422520000, latest_time=1562076000000, properties={Weight: 1, Weight: 1}, layer(s)=[Resting, Grunting-Lipsmacking]), Edge(source=PIPO, target=FELIPE, earliest_time=1560420720000, latest_time=1562151240000, properties={Weight: 1, Weight: 1, Weight: 1, Weight: 1}, layer(s)=[Resting, Presenting, Grunting-Lipsmacking, Avoiding]), Edge(source=FELIPE, target=NEKKE, earliest_time=1560419520000, latest_time=1562596320000, properties={Weight: 0, Weight: 0, Weight: 0, Weight: 0, Weight: 0, Weight: 0}, layer(s)=[Resting, Touching, Playing with, Grunting-Lipsmacking, Embracing, Carrying]), Edge(source=KALI, target=NEKKE, earliest_time=1560423840000, latest_time=1560423840000, properties={Weight: 1}, layer(s)=[Resting]), Edge(source=FELIPE, target=LOME, earliest_time=1560419520000, latest_time=1562671020000, properties={Weight: 1, Weight: 1, Weight: 1, Weight: 1, Weight: 1}, layer(s)=[Resting, Touching, Playing with, Threatening, Submission]), Edge(source=FELIPE, target=BOBO, earliest_time=1560419520000, latest_time=1560423420000, properties={Weight: -1, Weight: -1, Weight: -1}, layer(s)=[Resting, Presenting, Supplanting]), ...)