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', 'Resting', 'Grooming', 'Touching', 'Presenting', 'Playing with', 'Grunting-Lipsmacking', 'Attacking', 'Chasing', '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}, layer(s)=[Resting, Grooming, Touching, Playing with, Carrying])
Getting iterators over all nodes and edges:
Nodes(Node(name=ATMOSPHERE, earliest_time=1560419640000, latest_time=1562683260000), 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=EWINE, earliest_time=1560442020000, latest_time=1562754600000), Node(name=MALI, earliest_time=1560422040000, latest_time=1562755320000), Node(name=LIPS, earliest_time=1560419460000, latest_time=1562756700000), Node(name=MAKO, earliest_time=1560421620000, latest_time=1562756100000), Node(name=HARLEM, earliest_time=1560423540000, latest_time=1562754900000), Node(name=FANA, earliest_time=1560420000000, latest_time=1562754600000), ...)
Edges(Edge(source=ATMOSPHERE, target=LIPS, earliest_time=1560420000000, latest_time=1560420000000, properties={Weight: 1}, layer(s)=[Playing with]), Edge(source=ANGELE, target=FELIPE, earliest_time=1560419400000, latest_time=1562753640000, properties={Weight: 1}, layer(s)=[Resting, Grooming, Presenting, Grunting-Lipsmacking, Copulating, Submission]), Edge(source=FELIPE, target=ANGELE, earliest_time=1560419460000, latest_time=1562754600000, properties={Weight: 1}, layer(s)=[Resting, Touching, Presenting, Grunting-Lipsmacking, Chasing, Mounting, Embracing, Supplanting, Submission]), Edge(source=VIOLETTE, target=LIPS, earliest_time=1560423600000, latest_time=1560423600000, properties={Weight: -1}, layer(s)=[Threatening]), Edge(source=FELIPE, target=LIPS, earliest_time=1560419460000, latest_time=1562251080000, properties={Weight: 1}, layer(s)=[Resting, Touching]), Edge(source=ARIELLE, target=PETOULETTE, earliest_time=1560437700000, latest_time=1561626660000, properties={Weight: 1}, layer(s)=[Resting, Grooming]), Edge(source=FEYA, target=FANA, earliest_time=1560420000000, latest_time=1562248680000, properties={Weight: 1}, layer(s)=[Resting, Touching, Presenting]), Edge(source=PIPO, target=KALI, earliest_time=1560420660000, latest_time=1562752560000, properties={Weight: 1}, layer(s)=[Resting, Grooming, Touching, Grunting-Lipsmacking, Chasing, Mounting, Copulating]), Edge(source=VIOLETTE, target=PETOULETTE, earliest_time=1560438720000, latest_time=1562253000000, properties={Weight: 1}, layer(s)=[Presenting]), Edge(source=FELIPE, target=NEKKE, earliest_time=1560419520000, latest_time=1562596320000, properties={Weight: 0}, layer(s)=[Resting, Touching, Playing with, Grunting-Lipsmacking, Embracing, Carrying]), ...)