create graph from file (other than CSV) #4
Description
Module description
Creates a graph from a file that contains all information on the graph structure.
This can be a graphml file, a gefx file (Gephi file), or a json file (could also add csv here).
Inputs
- the type of the file from which the graph is created (graphml, gefx..)
- path to file
- column names for source and target
- optional: any other columns to be considered
- graph type (often: multigraph or not multigraph)
- for some file formats: data type of columns (str, int ..)
- for gefx: version of Gephi
- for gefx: use Gephi "id" or "label" columns as labels in networkX
Graph types
Affects all graph types.
Outupts
The output is a graph.
Example code
- graphml
1.1. igraph:
Gix = ig.read('graph.graphml', format='graphml') #this creates an igraph graph (not a networkX graph) from a graphml file, indicate file name and type.
1.2. networkX:
G = nx.read_graphml(path, node_type=<class 'str'>, edge_key_type=<class 'int'>, force_multigraph=False)
- csv with pandas
edges = pd.read_csv('JournalEdges1902.csv') #read csv with pandas and create df (should be an edge list representation of a graph)
G = nx.from_pandas_edgelist(edges, "Source", "Target", edge_attr=True, create_using=nx.DiGraph()) #then create graph indicating the pandas df, mapping the columns, and indicating graph type
- gefx
G = nx.read_gexf(path, node_type=None, relabel=False, version='1.2draft')
Activity