Install

Raphtory can be easily installed via pip. This will pull all of the background dependencies for Raphtory, automatically setting up any system paths to point at the correct location. Our only requirement here is you running python version 3.10.

Install

pip install raphtory

Running Raphtory

Once installed, let’s set up the most bare bones Raphtory graph, test that we can add some data to it and run our first query. Once this is all working we can move on to some much more exciting examples in the next section!

Before we start, however, you may have noticed that this page looks oddly like a iPython notebook. That is because it is! If you click the open on github link in the top right of the page you can follow along on your own machine. Right, Back to the code!

First we need to import Raphtory. You may see some references to Rust in the logs here, this is because under the hood Raphtory is written in Rust. You don’t have to worry about any of that though as its all hidden away!

[1]:
from raphtory import Graph

Creating your first graph

Once Raphtory is installed we can create our first graph! To do this we create a Graph object.

This takes one parameter, which is the number of shards to split the data into. This is useful when working with large graphs that are distributed across across multiple machines. But we do not need to worry about this when running it locally.

Once we have our graph we can add data into and run queries on.

[2]:
graph = Graph(1)

Adding data to your Graph

Once a graph is created, we need to add some data to it if we want run anything interesting. There are loads of ways of doing this in Raphtory, which we will cover in the next section, but for simplicity lets just add some vertices and edges without any properties.

As Raphtory is focused on dynamic and temporal analysis, all events in the graph’s history (adding, updating or deleting nodes/edges) must happen at a given time. This can all be at the same time (if, for example, you are working with snapshots) but we still need a time.

As such, when we add a vertex we have two arguments: the timestamp and the vertex ID. Similarly, when adding an edge, we have three arguments: the timestamp, the source vertex and the destination vertex. The vertex ID can either be an int or a str

In the following code block we have five updates for our graph, adding three vertices (1,2,3) at time 1 and two edges (1->2, 1->3) at time 2 .

[3]:
graph.add_vertex(1, 1)
graph.add_vertex(1, 2)
graph.add_vertex(1, 3)
graph.add_edge(2, 1, 2)
graph.add_edge(2, 1, 3)

Running your first Query

Now that our data is loaded we can start interrogating it!

While we can write some very complicated algorithms in Raphtory, lets start off with something simple, getting the in_degree and out_degree of our nodes.

For this we call the in_degree and out_degree functions on each node, and also use an algorithm for the entire graph.

Per node we can do:

[4]:
print("Vertex 1 - In degree: %i" % graph.vertex(1).in_degree())
print("Vertex 2 - In degree: %i" % graph.vertex(2).in_degree())
print("Vertex 1 - Out degree: %i" % graph.vertex(1).out_degree())
print("Vertex 2 - Out degree: %i" % graph.vertex(2).out_degree())
Vertex 1 - In degree: 0
Vertex 2 - In degree: 1
Vertex 1 - Out degree: 2
Vertex 2 - Out degree: 0

For the entire graph we can run a graph algorithm. We have created many built in algorithms, and they can be found on the documentation.

For the graph we can run:

[5]:
from raphtory import algorithms

print("Graph - Max out degree: %i" %  algorithms.max_out_degree(graph))
Graph - Max out degree: 2

As with every other cool feature I have hinted at, you will soon be an expert in queries, time-analysis and much more. All you have to do is continue on to the next page!