This page covers importing and exporting
Graph objects from and to
different (file) formats using the
ragraph.io module. Among the available formats
are JSON, YAML, XML, 2D matrices, and more. Feel free to head over to the module's reference
documentation to see them all!
The importing and exporting of JSON is probably the most extensive implementation, closely followed
by CSV. Some formats are only partly supported, where in most cases it features either importing to
Graph object or merely exporting one.
Graph can be translated both from and to a JSON file or encoded JSON
string. These are all based on the JSON dictionary representations of the objects in the graph and
handled by the
Importing it goes like this:
Which loads a
g with six nodes (
"f") with a
couple of edges between them.
If you already have a JSON encoded string loaded into a variable, you can also supply this by using:
Exporting the graph is rather similar:
Which by setting
path=None will give you a JSON string representation of the graph. When actually
setting the path to a filepath, the string will not be returned and written to that filepath
The CSV format is probably one of the most compact formats we support. To import from CSV you need
both a nodes and an edges CSV file. The functionality is included in the
The minimum requirement to a nodes file is that each node has a
name. A basic nodes file thus
looks like simple_nodes.csv. This file will generate six nodes named
"f" when imported, with all other
Node arguments left to
The minimum edges file needs a
source and a
target column. These should refer to the source and
target node of each edge, such as the simple_edges.csv.
Importing these can be done using the following snippet:
You can tweak some additional settings in the
from_csv method like the
CSV delimiter and some parameters to indicate which column includes which metadata.
Graph and its adjacency matrix are closely related. To facilitate quick
transitions between these representations, we included the
module. This allows you to transition back and forth from a list of lists or
nested numpy array and a
Graph object. A small example:
We usually default to leaving out edges that are self-loops (e.g. the diagonal is 0), but if you
would like to include them, just set
loops=True. There are some tweaks for hierarchical graphs,
too. Please refer to the
ragraph.io.matrix module's documentation for that.
For other formats, including the Elephant Specification Language, please refer to the
module reference documentation.