Home > front end >  Convert simplices from Delaunay Triangulation to networkx graph
Convert simplices from Delaunay Triangulation to networkx graph

Time:11-05

This is a follow-up to the post here.

I am trying to convert the simplices returned from Scipy's Delaunay Triangulation to a Networkx graph.

Code:

from scipy.spatial import Delaunay as scipy_Delaunay
# tri = scipy_Delaunay(pts[:, 0:2]) #input points
# simplices = tri.simplices
   
simplices = np.array([[ 9, 13, 19],
                     [11,  9,  4],
                     [ 9, 11, 13],
                     [ 0,  7,  2],
                     [ 7,  3, 18]])
G = nx.Graph(simplices)
for path in simplices:
    nx.add_path(G, path)

nx.draw(G, with_labels=True, node_size=500, node_color='lightgreen')

Error:

raise nx.NetworkXError(f"Adjacency matrix not square: nx,ny={A.shape}")
networkx.exception.NetworkXError: Adjacency matrix not square: nx,ny=(5, 3)
networkx.exception.NetworkXError: Input is not a correct numpy matrix or array.

I am not sure how to resolve this error. Suggestions will be really helpful.

CodePudding user response:

I think you can remove the simplices from

G = nx.Graph(simplices)

to:

G = nx.Graph()

to create an empty graph. You are adding the nodes in the loop later on so no needs to add the nodes location during the graph creation. The final code is:

from scipy.spatial import Delaunay as scipy_Delaunay
# tri = scipy_Delaunay(pts[:, 0:2]) #input points
# simplices = tri.simplices
   
simplices = np.array([[ 9, 13, 19],
                     [11,  9,  4],
                     [ 9, 11, 13],
                     [ 0,  7,  2],
                     [ 7,  3, 18]])
G = nx.Graph()
for path in simplices:
    nx.add_path(G, path)

nx.draw(G, with_labels=True, node_size=500, node_color='lightgreen')
  • Related