RaGraph basics tutorial¶
Create a Graph¶
Let's start by building a graph! A graph consists of nodes and edges, sometimes respectively called
vertices and arcs but we use the former. We can start with an empty
You can slowly populate the empty
Graph object or load nodes and edges in
bulk, both of which we'll see later this tutorial and how-to guides.
Add a Node¶
When creating a
Node, all we need is a name. Let's create a node called
What you see here, is that we create a
Node object and add it to the
Graph. We can fetch the node from the graph via its
name, which has to be unique within the
Also, there are quite some attributes attached to the
Node by default. These
are mostly metadata which you can safely ignore for now. The important thing is that it got our name
Add an Edge¶
An edge runs from a source node to a target node, which means that it is directed. Those two nodes
are the only required parameters to create one! Lets create a second node called
"B" and an edge
So that concludes our first
Edge! You can query all edges between two nodes
(you can add any amount!) by supplying both a source and target
Node name as
a tuple. Again, the same metadata properties have been added as we've seen before, which you can
Create a hierarchical Graph¶
Which means the children have been added to
A's children property. The parent relationship is
updated automatically, too, though!
It's perfectly possible to add edges to hierarchical graphs. There are no restrictions as to which source nodes can target which target nodes, as long as both exist in the graph.
Some algorithms leverage parent-child relationships and the edges between descendant when calculating weights between nodes, so make sure you understand how the weights or relations between nodes are calculated in an algorithm so you provide it with the correct input.
Using the metadata fields¶
kind: The main category or domain of a node or edge.
labels: A list of labels you can to attach to any node or edge.
weights: A dictionary of keys to (numeric) values. For instance a
costproperty for a node or the
strengthof an edge.
annotations: A rather flexible
ragraph.generic.Annotationsobject you can store pretty much any additional information in. You can initialize it using a dictionary as you will see in the following example.
Where most properties are fairly explanatory, the
object might need a little explaining. It's essentially a class you
can supply any (keyword) arguments or a dictionary to. The keys are used to form property names.
Keep in mind that it is recommended to only add serializable objects to this class, so you can
export and import your data with ease.
As a dictionary¶
All of the
Graph classes feature a
json_dict property which is a regular Python dictionary containing only serializable data that's
readily exportable using Python's default
json module or most other Python I/O packages.
Let's review the it for some of the previously introduced variables:
This works for
Graph's themselves, too, but you get the point.
A short summary of most of the available properties:
width: The number of children this node has.
depth: The number of consecutive ancestor (parent) nodes up until the root node. If this is a root node, the depth is 0.
height: The maximum number of consecutive descendant (child) nodes until a leaf node is reached. If this is a leaf node, the height is 0.
is_leaf: Whether this node has no children and thus is a leaf node.
is_root: Whether this node is a root node and thus has no parent.
is_bus: Whether this node is a highly integrative node within its network of siblings. See bus detection.
Kinds, labels, weights¶
To check what kinds, labels, or weights have been used, you can use any of the following properties
So far, we've discussed getting nodes by name using
graph["node name"]. However, you can use any
of the following methods to retrieve specific nodes as well:
graph.roots: Get all nodes in the graph that have no parent.
graph.leafs: Get all nodes in the graph that have no children.
graph.targets_of: Yield all nodes that have an incoming edge from your given node.
graph.sources_of: Yield all nodes that have an edge targeting your given node.
Previously, we have retrieved edges using
graph["source name", "target name"] or via their edge ID
graph.id_to_edge[id]. Similarly to the nodes, we have the following methods to retrieve
graph.edges_from: Yield all edges originating from your given node.
graph.edges_to: Yield all edges that target your given node.
graph.edges_between: Yield all edges between two nodes.
graph.edges_between_all: Yield all edges between a set of sources and targets.
Calculate an adjacency matrix¶
An adjacency matrix represents the sum of edge weights between sets of nodes. The nodes are
identically ordered on both the matrix' axes. A cell value on
[i, j] then denotes the sum of edge
weights of edges going from the
j node (column) to the
i node (row). This follows the IR/FAD
(inputs in rows, feedback above diagonal) convention. Here is a little example of the
Or, if you want to omit self loops and only look at the
Please take a look at the method's documentation for more information:
Similarly, you can calculate a mapping matrix using
where the rows and columns do not need to be symmetrical. This is commonly used to calculate
a mapping from nodes of one domain (node kind) to another.