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_name
stror file-like object Name of the file, or a file-like object (opened in binary mode).
- fmt
str(optional, default:"auto") File format, as given in the table below. If
"auto", the format is given by the extension offile_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.
- file_name
- Returns:
- g
Graph The graph read from the file, with its property maps.
- g
Notes
The format is given by
fmt, or, iffmtis"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 ingraph-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.fmtExtension
Format
"gt".gtBinary format of
graph-tool(see Sec. The gt file format)"graphml".graphml,.xmlGraphML (
"xml"is a synonym)"dot".dot"gml".gml"gexf".gexfGEXF (Gephi)
"pajek".net"json".json"cytoscape_json".cyjs"adjacency_json""tree_json"JSON tree, of directed trees
"mtx".mtx"graph6".g6graph6 (nauty)
"sparse6".s6sparse6 (nauty)
"digraph6".d6digraph6 (nauty)
"metis".graph,.metis"paj".pajPajek project, with a network, partitions, and vectors
"ncol".ncolNCOL (Large Graph Layout)
"lgl".lglLGL (Large Graph Layout)
"edgelist".edgelist,.edges,.edge"adjlist".adjlist"multiline_adjlist""adjacency".adjAdjacency matrix as text
"dl".dlUCINET DL, in the
fullmatrix,edgelist1, andnodelist1formats"leda".gw,.leda,.lgr"graphdb".graphdbGraphDB, the binary format of the ARG database (DOI: 10.1016/S0167-8655(02)00253-2 [sci-hub, @tor])
"dimacs".dimacs,.max,.colDIMACS 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:fmtProperty 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_nameas the names of the verticesNone
"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 mapdirected, the vertex property mapid, and the edge property mapsid,source, andtarget, whose names are keys of the format and which cannot be savedNone
"gexf"The vertex and edge property maps, with their value types (the property maps
pos,color,size,thickness, andshapeas visualization data), and the graph property mapsdescriptionandkeywordsNone
"pajek"The vertex property maps
label,pos, andshape, the edge property mapweight, 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 shapeellipse, which is read back as well), and the graph property mapnameNone
"json"All, as JSON values that are read back as
bool,int64_t,double,string, vectors of these, orpython::objectNone
"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
sizeandweight, and the integer edge property mapweightNone
"paj"Those of
"pajek", with the integer and floating point vertex property maps as partitions and vectors, which are read back asint64_tanddoubleNone
"ncol","lgl"The vertex property map
name, as text, and the scalar edge property mapweight, which is read back asdoubleNone
"edgelist","multiline_adjlist"The vertex property map
name, as text, and the edge property maps whose values are Python literals, read back asbool,int64_t,double,string, vectors of these, orpython::objectNone
"adjlist"The vertex property map
name, as textNone
"adjacency"The sums of the values of the scalar edge property map
weight, or the numbers of edgesNone
"dl"The vertex property map
name, as text, and the scalar edge property mapweightNone
"leda"The vertex property map
label, and the edge property mapweight(orlabelif there is noweight), with scalar or string valuesNone
"graphdb"None
None
"dimacs"For directed graphs, the scalar edge property map
capacityand the graph property mapssourceandtarget, for undirected graphs, the scalar vertex property mapweightNone
"network_text"The vertex property maps
labelandcollapseNone
Filtered and reversed graphs
When a filtered graph is saved by
save()in the"gt"or"graphml"format withpreserve_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. Withpreserve_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 aValueError, 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 mapcapacity), 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 withset_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 mapweight, 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 mapname, 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 mapname, 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 valuesnanandinf(which Python writes as such, but which are not Python literals).Python objects
The values of the property maps of type
python::objectare stored as theirpickleby 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 (aValueErroris raised otherwise), and it does not restore a property map whose values are allNone.Warning
Unpickling can execute arbitrary code. Files with property maps of type
python::objectshould 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 mapvaluein the"cytoscape_json"format andidin 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 mapkey, 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 maplabel, or by their indices, and the trees below the vertices with a true value of the vertex property mapcollapseare 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']