Networkx - problem while working big data

Viewed 115

I am trying to triangulate a large amount of massive data using Delaunay to scipy.spatial for triangulation and networkx to get the node adjacency relations. My code works very well on small data sets but when I try to introduce volumes of about 2 miollion points I always get the following error:

raise NetworkXError(f"The node {n} is not in the graph.") from e NetworkXError: The node 1 is not in the graph.

It seems like my graph store the first node and nothing more. When I did my research I found that networkx is well adapted to massive data

Here is my code :

import numpy as np
import networkx as nx
import scipy.spatial
points = np.genfromtxt('las1.xyz', delimiter = ';')
xy= points[:,0:2]
z= points[:,2]
delTri = scipy.spatial.Delaunay(xy)
edges = set()
for n in range(delTri.nsimplex):
       edge = sorted([delTri.vertices[n,0], delTri.vertices[n,1]])
       edges.add((edge[0], edge[1]))
       edge = sorted([delTri.vertices[n,0], delTri.vertices[n,2]])
       edges.add((edge[0], edge[1]))
       edge = sorted([delTri.vertices[n,1], delTri.vertices[n,2]])
       edges.add((edge[0], edge[1]))
pts_neigh = {}
graph = nx.Graph(list(edges))
for i in range(len(xy)):
       pts_neigh[i] =  list(graph.neighbors(i))

I still get the edges list from my networkx graph but it seems like it fails at the level of constructing the nodes. I will be so grateful for your help.

1 Answers

Although it's possible to instantiate graph with specific data, the syntax can be a bit complex. An easier option is to explicitly add edges from a list:

graph = nx.Graph()
graph.add_edges_from(list(edges))
Related