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, vertices and edges, these are discussed together in Property queries.
Info
In the below code segment you will see the functions num_edges()
and num_temporal_edges()
being called and returning different results. This is because num_edges()
returns the number of unique edges and num_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_vertices = g.num_vertices()
number_of_edges = g.num_edges()
total_interactions = g.num_temporal_edges()
unique_layers = g.get_unique_layers()
print("Number of vertices (Baboons):", number_of_vertices)
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 vertices (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 vertices and edges
Three types of functions are provided for accessing the vertices and edges within the graph:
- Existance check: Via
has_vertex()
andhas_edge()
you can check if an entity is present within the graph. - Direct access:
vertex()
andedge()
will return a vertex/edge object if the entity is present andNone
if it is not. - Iterable access:
vertices()
andedges()
will return iterables for all vertices/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 vertices and edges are in the graph:")
if g.has_vertex(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 vertices and edges:")
print(g.vertex("LOME"))
print(g.edge("LOME", "NEKKE"), "\n")
print("Getting iterators over all vertices and edges:")
print(g.vertices())
print(g.edges())
Output
Checking if specific vertices and edges are in the graph:
Lomme is in the graph
Lomme has played with Nekke
Getting individual vertices and edges:
Vertex(name=LOME, earliest_time="1560419520000", latest_time="1562756100000", properties={_id: LOME})
Edge(source=LOME, target=NEKKE, earliest_time=1560421080000, latest_time=1562755980000, properties={Weight: 1})
Getting iterators over all vertices and edges:
Vertices(Vertex(name=ANGELE, earliest_time="1560419400000", latest_time="1562754600000", properties={_id: ANGELE}), Vertex(name=FELIPE, earliest_time="1560419400000", latest_time="1562756700000", properties={_id: FELIPE}), Vertex(name=LIPS, earliest_time="1560419460000", latest_time="1562756700000", properties={_id: LIPS}), Vertex(name=NEKKE, earliest_time="1560419520000", latest_time="1562756700000", properties={_id: NEKKE}), Vertex(name=LOME, earliest_time="1560419520000", latest_time="1562756100000", properties={_id: LOME}), Vertex(name=BOBO, earliest_time="1560419520000", latest_time="1562755500000", properties={_id: BOBO}), Vertex(name=ATMOSPHERE, earliest_time="1560419640000", latest_time="1562683260000", properties={_id: ATMOSPHERE}), Vertex(name=FEYA, earliest_time="1560420000000", latest_time="1562756040000", properties={_id: FEYA}), Vertex(name=FANA, earliest_time="1560420000000", latest_time="1562754600000", properties={_id: FANA}), Vertex(name=PIPO, earliest_time="1560420660000", latest_time="1562752560000", properties={_id: PIPO}), ...)
Edges(Edge(source=ANGELE, target=FELIPE, earliest_time=1560419400000, latest_time=1562753640000, properties={Weight: 1}), Edge(source=ANGELE, target=LIPS, earliest_time=1560523860000, latest_time=1562670060000, properties={Weight: 1}), Edge(source=ANGELE, target=NEKKE, earliest_time=1560441780000, latest_time=1562670060000, properties={Weight: 1}), Edge(source=ANGELE, target=LOME, earliest_time=1560441780000, latest_time=1562669940000, properties={Weight: 1}), Edge(source=ANGELE, target=BOBO, earliest_time=1560441960000, latest_time=1560441960000, properties={Weight: 1}), Edge(source=ANGELE, target=ATMOSPHERE, earliest_time=1560441960000, latest_time=1561988880000, properties={Weight: 1}), Edge(source=ANGELE, target=FEYA, earliest_time=1560766980000, latest_time=1562249160000, properties={Weight: 1}), Edge(source=ANGELE, target=FANA, earliest_time=1561122000000, latest_time=1562753640000, properties={Weight: 1}), Edge(source=ANGELE, target=MUSE, earliest_time=1560855540000, latest_time=1562683020000, properties={Weight: 1}), Edge(source=ANGELE, target=MAKO, earliest_time=1560441720000, latest_time=1562669940000, properties={Weight: 1}), ...)