load_graph

Contents

load_graph#

graph_tool.load_graph(file_name, fmt='auto', ignore_vp=None, ignore_ep=None, ignore_gp=None)[git][source]#

Load a graph from a file.

Parameters:
file_namestr or file-like object

Name of the file, or a file-like object (opened in binary mode).

fmtstr (optional, default: "auto")

File format, as given in the table below. If "auto", the format is given by the extension of file_name, or is "gt" for file-like objects.

ignore_vplist of str (optional, default: None)

Names of the vertex property maps that are not read.

ignore_eplist of str (optional, default: None)

Names of the edge property maps that are not read.

ignore_gplist of str (optional, default: None)

Names of the graph property maps that are not read.

Returns:
gGraph

The graph read from the file, with its property maps.

Notes

The format is given by fmt, or, if fmt is "auto", by the extension of the file name, according to the following table.

Note

Only the "gt" and "graphml" formats preserve all the data of a graph in graph-tool: every vertex, edge, and graph property map, with its value type, and the filtering state of the graph (its vertex and edge filters, and whether it is reversed). The other formats store only part of the property maps, and none of the filtering state, as summarized in the second table below, so that a graph saved in them and loaded again is in general not identical to the original.

fmt

Extension

Format

"gt"

.gt

Binary format of graph-tool (see Sec. The gt file format)

"graphml"

.graphml, .xml

GraphML ("xml" is a synonym)

"dot"

.dot

Graphviz DOT

"gml"

.gml

GML

"gexf"

.gexf

GEXF (Gephi)

"pajek"

.net

Pajek

"json"

.json

JSON node-link

"cytoscape_json"

.cyjs

Cytoscape JSON

"adjacency_json"

JSON adjacency

"tree_json"

JSON tree, of directed trees

"mtx"

.mtx

Matrix Market

"graph6"

.g6

graph6 (nauty)

"sparse6"

.s6

sparse6 (nauty)

"digraph6"

.d6

digraph6 (nauty)

"metis"

.graph, .metis

METIS

"paj"

.paj

Pajek project, with a network, partitions, and vectors

"ncol"

.ncol

NCOL (Large Graph Layout)

"lgl"

.lgl

LGL (Large Graph Layout)

"edgelist"

.edgelist, .edges, .edge

Edge list

"adjlist"

.adjlist

Adjacency list

"multiline_adjlist"

Multiline adjacency list

"adjacency"

.adj

Adjacency matrix as text

"dl"

.dl

UCINET DL, in the fullmatrix, edgelist1, and nodelist1 formats

"leda"

.gw, .leda, .lgr

LEDA

"graphdb"

.graphdb

GraphDB, the binary format of the ARG database (DOI: 10.1016/S0167-8655(02)00253-2 [sci-hub, @tor])

"dimacs"

.dimacs, .max, .col

DIMACS maximum flow (p max) and edge (p edge) problems

"network_text"

Network text, a drawing of the graph as text, which can only be saved

Compression

A file name that ends in .gz, .bz2, .xz, or .zst, after the extension of the format, is compressed when the graph is saved and decompressed when it is loaded, with gzip, bzip2, xz, or zstd, respectively, while a file-like object is neither compressed nor decompressed.

Stored data

The "gt" and "graphml" formats also store whether fast edge removal and lookup are enabled (see Sec. Reserved property maps), and the "gt" format is the fastest and most compact one. What each format stores is summarized in the following table, where “all” stands for the vertex, edge, and graph property maps in the second column, and for the vertex and edge filters and the reversal in the third:

fmt

Property maps

Filtering state

"gt"

All, with their value types

All

"graphml"

All, with their value types, except the vertex and edge index maps, which are not stored

All

"dot"

The vertex and edge property maps, as text that is read back as strings, with the vertex property map vertex_name as the names of the vertices

None

"gml"

All, the scalar values as numbers that are read back as double, and the other values as text that is read back as strings, except the graph property map directed, the vertex property map id, and the edge property maps id, source, and target, whose names are keys of the format and which cannot be saved

None

"gexf"

The vertex and edge property maps, with their value types (the property maps pos, color, size, thickness, and shape as visualization data), and the graph property maps description and keywords

None

"pajek"

The vertex property maps label, pos, and shape, the edge property map weight, the other vertex and edge property maps as text that is read back as strings (on vertex lines without a shape, after Pajek’s default shape ellipse, which is read back as well), and the graph property map name

None

"json"

All, as JSON values that are read back as bool, int64_t, double, string, vectors of these, or python::object

None

"cytoscape_json", "adjacency_json"

Those of "json"

None

"tree_json"

The vertex property maps, as in "json"

None

"mtx"

The edge property map weight, with integer, floating point, or complex values (the latter as vectors of two floating point values)

None

"graph6", "sparse6", "digraph6"

None

None

"metis"

The integer vertex property maps size and weight, and the integer edge property map weight

None

"paj"

Those of "pajek", with the integer and floating point vertex property maps as partitions and vectors, which are read back as int64_t and double

None

"ncol", "lgl"

The vertex property map name, as text, and the scalar edge property map weight, which is read back as double

None

"edgelist", "multiline_adjlist"

The vertex property map name, as text, and the edge property maps whose values are Python literals, read back as bool, int64_t, double, string, vectors of these, or python::object

None

"adjlist"

The vertex property map name, as text

None

"adjacency"

The sums of the values of the scalar edge property map weight, or the numbers of edges

None

"dl"

The vertex property map name, as text, and the scalar edge property map weight

None

"leda"

The vertex property map label, and the edge property map weight (or label if there is no weight), with scalar or string values

None

"graphdb"

None

None

"dimacs"

For directed graphs, the scalar edge property map capacity and the graph property maps source and target, for undirected graphs, the scalar vertex property map weight

None

"network_text"

The vertex property maps label and collapse

None

Filtered and reversed graphs

When a filtered graph is saved by save() in the "gt" or "graphml" format with preserve_filter=True (the default), all its vertices and edges are stored, together with the filters, so that the loaded graph is filtered in the same way, and the filtered-out vertices and edges can be recovered by removing the filters; a reversed graph is likewise loaded reversed. With preserve_filter=False, or in any other format, the graph is stored as it is seen instead, i.e. only the vertices and edges that are not filtered out, with the vertices renumbered consecutively, and the edges of a reversed graph in their reversed direction. In neither case is the graph copied.

Restrictions of the formats

The "graph6" format can only store undirected graphs without self-loops or parallel edges, the "sparse6" and "metis" formats only undirected graphs, and the "digraph6" format only directed graphs without parallel edges. Saving other graphs in these formats raises a ValueError, as does saving a graph with more than 65535 vertices, or with a vertex with more than 65535 edges, in the "graphdb" format. Directed graphs are saved in the "dimacs" format as maximum flow problems (with unit capacities if there is no edge property map capacity), and undirected graphs as edge problems. Only the first network of a file in the "paj" format is loaded, with the partitions and vectors of the same number of vertices. Since the "dot" and "gml" formats do not escape backslashes, a string value that ends in a backslash produces a file that cannot be read back.

Formats without the direction of the graph

The formats of other programs that do not state whether the graph is directed ("ncol", "lgl", "edgelist", "adjlist", "multiline_adjlist", "adjacency", "dl", and "graphdb") are handled as follows. The lists of edges and the adjacency lists contain each edge of an undirected graph once, and are read as undirected graphs, whose edges keep the direction in which they appear in the file, so that a directed graph is obtained with set_directed(). The formats that describe an adjacency matrix ("adjacency", "dl", and "graphdb") contain each edge of an undirected graph in both directions (and each self-loop once), and are read as undirected graphs if the matrix is symmetric, and as directed graphs otherwise, so that a directed graph with a symmetric adjacency matrix is read as an undirected graph. In the "adjacency" format, integer entries are numbers of edges, and other values are edge weights, which are read into the edge property map weight, while in the "dl" format the nonzero entries are edge weights, unless they are all equal to one.

Vertex identifiers

The vertices are identified in the "ncol", "lgl", "edgelist", "adjlist", "multiline_adjlist", and "dl" formats by the values of the vertex property map name, which must be distinct and contain no whitespace or # (nor , or " in the "dl" format), or by their indices if there is no such property map. When such a file is loaded, the identifiers are stored in the vertex property map name, with the vertices numbered in the order of their first appearance (the vertices with lines of their own first), except that non-negative integer identifiers are the vertex indices themselves in the "edgelist", "adjlist", and "multiline_adjlist" formats, and that identifiers equal to the vertex indices are not stored. The "ncol" and "edgelist" formats do not contain the vertices without edges. In the files of the "edgelist" and "multiline_adjlist" formats, the values of the edge property maps are written for each edge as a Python dictionary, whose keys are the names of the property maps, and which is read back into one edge property map per key; the values may also be the floating point values nan and inf (which Python writes as such, but which are not Python literals).

Python objects

The values of the property maps of type python::object are stored as their pickle by the "gt" and "graphml" formats, and loaded as the original objects. In the "gt" format, an object that appears in several values is stored only once and loaded as a single object, and references to the graph itself are loaded as references to the loaded graph (see Sec. Python objects). The "dot", "gml", "gexf", and "pajek" formats store the base64 encoding of the pickles as text, loaded as strings. The "json" format stores the objects as JSON values, so that they must be representable in JSON (a ValueError is raised otherwise), and it does not restore a property map whose values are all None.

Warning

Unpickling can execute arbitrary code. Files with property maps of type python::object should therefore only be loaded from trusted sources.

JSON and text formats

The "cytoscape_json", "adjacency_json", and "tree_json" formats store the property maps as the "json" format does, and identify the vertices by the values of the vertex property map value in the "cytoscape_json" format and id in the other two (or by their indices if there is no such property map), which are read back into these property maps unless they are the vertex indices. The "tree_json" format can only store directed trees whose edges point away from the root, and is loaded with the vertices numbered in depth-first order. The edges of a graph with parallel edges are stored by the "cytoscape_json" and "adjacency_json" formats with keys, which are the values of the edge property map key, if there is one, and otherwise the numbers of the previous edges with the same endpoints, so that they are read back into this property map only in the first case. The "network_text" format is a drawing of the graph as a forest of trees in text, drawn with box-drawing characters, where the vertices are labelled by the values of the vertex property map label, or by their indices, and the trees below the vertices with a true value of the vertex property map collapse are omitted; it cannot be loaded.

Examples

>>> g = gt.collection.data["lesmis"]
>>> g.save("lesmis.gt.zst")
>>> h = gt.load_graph("lesmis.gt.zst")
>>> print(h.num_vertices(), h.num_edges(), list(h.vp.keys()))
77 254 ['label', 'pos']