Skip to content

Layered graphs

Info

Edge layers have been discussed several times, notably in Ingestion and Exploded Edges. Please check these before reading this section.

As previously discussed, an edge object by default will contain information on all layers between its source and destination nodes. However, it is often the case that there are a subset of these relationships that we are interested in. To handle this the Graph, Node and Edge provide the layers() function. This takes a list of layer names and returns a view with only edge updates that occurred on these layers.

Layer views can also be used in combination with any other view function. As an example of this, in the code below we look at the total edge weight over the full graph, then restrict this to the Grooming and Resting layers and then reduce this further by applying a window between the 13th and 20th of June.

total_weight = g.edges.properties.temporal.get("Weight").values().sum().sum()
print(f"Total weight across all edges is {total_weight}.")

total_weight = (
    g.layers(["Grooming", "Resting"])
    .edges.properties.temporal.get("Weight")
    .values()
    .sum()
    .sum()
)
print(f"Total weight across Grooming and Resting is {total_weight}.")

start_day = datetime.strptime("2019-06-13", "%Y-%m-%d")
end_day = datetime.strptime("2019-06-20", "%Y-%m-%d")
total_weight = (
    g.layers(["Grooming", "Resting"])
    .window(start_day, end_day)
    .edges.properties.temporal.get("Weight")
    .values()
    .sum()
    .sum()
)
print(
    f"Total weight across Grooming and Resting between {start_day} and {end_day} is {total_weight}."
)

Output

Total weight across all edges is 2948.
Total weight across Grooming and Resting is 1685.
Total weight across Grooming and Resting between 2019-06-13 00:00:00 and 2019-06-20 00:00:00 is 403.

Traversing the graph with layers

As with the time based filters (discussed here), if a layer view is applied to the graph, all extracted entities will have this view applied to them. If, however, the layer view is applied to a node or edge, it will only last until you have hoped to a new node.

Expanding on the example from the time views, if we wanted to look at LOME's neighbours who he has groomed, followed by who those monkeys have rested with, we could write the following query.

two_hop_neighbours = set(
    g.node("LOME")
    .layer("Grooming")
    .neighbours.layer("Resting")
    .neighbours.name.collect()
)
print(
    f"When the Grooming layer is applied to the node, LOME's two hop neighbours are: {two_hop_neighbours}"
)

Output

When the Grooming layer is applied to the node, LOME's two hop neighbours are: {'LOME', 'FELIPE', 'FEYA', 'KALI', 'ATMOSPHERE', 'MUSE', 'MALI', 'ARIELLE', 'VIOLETTE', 'HARLEM', 'ANGELE', 'LIPS', 'MAKO', 'FANA', 'BOBO', 'NEKKE', 'EWINE', 'PIPO', 'PETOULETTE'}