Home > Mobile >  Import graph from file to Networkx or NNGT
Import graph from file to Networkx or NNGT

Time:09-17

I need to build a large weighted digraph from data that's being exported from MatLab. The ultimate goal is to analyse it using NNGT, but it looks like building it using Networkx and then converting to NNGT might be easier.

'sample.txt' looks like this:

1,37
1,76
2,4
2,41
3,2
3,4
4,2
4,3

etc etc etc. So I tried:

import networkx as nx
G=nx.DiGraph()
G=nx.read_edgelist('sample.txt')

But this returns an empty graph object (0 nodes, 0 edges).

Tried the answer that was previously suggested by Networkx: how can i import a graph from a file? :

with open('sample.txt') as f:
    lines = f.readlines()
myList = [line.strip().split() for line in lines]
G.add_edges_from(myList)

but got error:

NetworkXError: Edge tuple ['1,37'] must be a 2-tuple or 3-tuple.

So I tried:

tup = tuple(myList)
G.add_edges_from(tup)

But still got the same error:

NetworkXError: Edge tuple ['1,76'] must be a 2-tuple or 3-tuple.

This is just trying to get edges in, I'm not even dealing with weights yet.

Ultimately I need to be able to:

  1. Create a digraph in NNGT with a set number of nodes - some nodes will have no edges but must still exist)
  2. Import edges (ie adjacency matrix) and weights from a file - any file type that can be exported from MatLab, doesn't need to be a .txt

Any ideas?

CodePudding user response:

You only need to slightly adjust the read_edgelist and use the two parameters create_using to get your DiGraph instance and use as delimiter , instead of the default space:

import networkx as nx

G = nx.read_edgelist('sample.txt', create_using=nx.DiGraph, delimiter=",", nodetype=int)

print(G.edges())
# [('1', '37'), ('1', '76'), ('2', '4'), ('2', '41'), ('4', '2'), ('4', '3'), ('3', '2'), ('3', '4')]

As mentioned below by Frodnar, you can include nodetype=int if you anyways only add integer nodes and you need to keep in mind that nodes without edges are not added to the graph.

CodePudding user response:

you can read the data directly from matlab if you want. Save the workspace as a .mat file, then you scipy.io

from scipy.io import loadmat

path = 'file.mat'

raw_data = loadmat(path, squeeze_me=True)

data = raw_data['data_struct']

Then import the data into a dataframe then to networkx. No need for text file.

  • Related