API Reference

List of classes and functions available in the networkx_gdf module.

class GDF

Implements read_gdf() and write_gdf() methods for GDF (Graph Data Format) files.

GDF is a compact file format originally implemented by GUESS. Although the software itself is not anymore maintained, the format is still supported by active open-source projects such as Gephi. It is based on a tabular text format, and is defined by the simple following rules:

  • A first line starting with nodedef>name VARCHAR defines the node table.

  • Each subsequent line contains a node name and its attributes separated by commas.

  • A second line starting with edgedef>node1 VARCHAR,node2 VARCHAR defines the edge table.

  • Each subsequent line contains an edge and its attributes separated by commas.

The following object types are supported by GDF: VARCHAR, INT, LONG, FLOAT, DOUBLE, and BOOLEAN. It also supports single and double quotes as text delimiters.

File example

The example below displays a simple GDF with two nodes (\(A\), \(B\)) and one edge. Note that nodes do not have attributes, so the nodes table may be left empty after the header:

nodedef>name VARCHAR
edgedef>node1 VARCHAR,node2 VARCHAR
A,B

A special property directed can be added to the edge table to specify directed (True) or undirected (False) edges. If not found, edges are considered as undirected by default.

See also

  • The GUESS Wiki (archived) for the official GDF format documentation.

  • The Gephi Wiki for more information and examples using the format.

Code example

The following code creates the graph above, writes it to a GDF file, and reads it afterwards:

>>> import networkx as nx
>>> from networkx_gdf import read_gdf, write_gdf
>>>
>>> G = nx.Graph()
>>> G.add_edge("A", "B")
>>>
>>> write_gdf(G, "graph.gdf")
>>> read_gdf("graph.gdf")

<networkx.classes.graph.Graph object at 0x7f3b9c7b2a60>

Both methods are static and do not require instantiation if the class is inherited:

>>> from networkx_gdf import GDF
>>>
>>> class MyClass(GDF):
>>>     ...
>>>
>>> G = MyClass.read_gdf("graph.gdf")
>>> MyClass.write_gdf(G, "graph.gdf")

Additional parameters are available for both methods and described in their documentation.

read_gdf(file: str | BufferedReader | BytesIO | StringIO | TextIOWrapper, directed: bool | None = None, multigraph: bool | None = None, weighted: bool | None = True, node_attr: list | bool | None = True, edge_attr: list | bool | None = True, encoding: str = 'utf-8', errors: str = 'strict') Graph

Returns a NetworkX graph object from file path or object.

Parameters:
  • file (object) – File object or string containing path to GDF file.

  • directed (bool | None) –

    Consider edges as directed or undirected. Optional. Default is None.

    • If None, decides based on 'directed' edge attribute in file, if it exists. In case it does not exist, the graph will be considered as undirected.

    • If True, returns a DiGraph or MultiDiGraph object.

    • If False, returns a Graph or MultiGraph object.

  • multigraph (bool | None) –

    Consider multiple edges among pairs of nodes. Optional. Default is None.

    • If None, decides based on number of edges among node pairs. In case of multiple edges among the same node pairs, the graph will be considered as a multigraph, preserving dynamic edge-level attributes.

    • If True, returns a MultiGraph or MultiDiGraph object.

    • If False, sums edge weights and returns a Graph or DiGraph object.

  • weighted (bool | None) – Consider edge weights. Optional. Default is True. Only applicable if multigraph is manually set as False.

  • node_attr (list | bool | None) –

    Accepts a list or bool. Optional. Default is True.

    • If a list, only the specified attributes will be considered.

    • If True, all node attributes will be considered.

    • If False, no node attributes will be considered.

  • edge_attr (list | bool | None) –

    Accepts a list or bool. Optional. Default is True.

    • If a list, only the specified attributes will be considered.

    • If True, all edge attributes will be considered.

    • If False, no edge attributes will be considered.

  • encoding (str) – The encoding of the file. Default is 'utf-8'. For a list of possible values, see Python documentation: Standard Encodings.

  • errors (str) – The error handling scheme. Default is 'strict'. For a list of possible values, see Python documentation: Error Handlers.

Return type:

Graph

write_gdf(G: Graph, file: str | BufferedWriter | BytesIO | StringIO | TextIOWrapper | None = None, node_attr: list | bool | None = True, edge_attr: list | bool | None = True, encoding: str = 'utf-8', errors: str = 'strict') str | None

Writes a NetworkX graph object to file path or object.

Parameters:
  • G (Graph) – NetworkX graph object.

  • file (object) – File object or string containing path to GDF file. Optional. If None (default), returns content as string.

  • node_attr (list | bool | None) –

    Accepts a list or bool. Optional. Default is True.

    • If a list, only the specified attributes will be considered.

    • If True, all node attributes will be considered.

    • If False, no node attributes will be considered.

  • edge_attr (list | bool | None) –

    Accepts a list or bool. Optional. Default is True.

    • If a list, only the specified attributes will be considered.

    • If True, all edge attributes will be considered.

    • If False, no edge attributes will be considered.

  • encoding (str) – The encoding of the file. Default is 'utf-8'. For a list of possible values, see Python documentation: Standard Encodings.

  • errors (str) – The error handling scheme. Default is 'strict'. For a list of possible values, see Python documentation: Error Handlers.

Return type:

str | None