Home>

Download the data from the following CSV file and
Atoms (nodes) within a certain distance are connected by edges,
An error occurred in the process of converting it from networkx to dgl.

Read data

I loaded the following data using pandas.read_csv.

           Xnm Ynm Znm Mass element Cluster
0 23.481321 -7.134153 -39.491711 11.996704 0 1
1 23.853514 -7.915858 -39.470348 11.994069 0 1
2 24.003975 -7.988654 -39.413750 13.089952 0 1
3 22.822966 -7.829986 -39.594307 13.992764 1 1
4 21.998539 -7.488298 -39.792698 14.035108 1 1
... ... ... ... ... ... ...
91324 -6.768085 -11.560308 33.360088 12.003025 0 1000
91325 -9.146031 -10.729319 33.862568 12.499396 0 1000
91326 -11.821389 -11.540415 34.308655 12.985589 0 1000
91327 -13.612114 -9.707627 35.039318 14.013540 1 1000
91328 -7.447951 -10.075975 33.741989 12.998513 0 1000
Implementation details
dgl_graph_data = []
nx_graph_data = []
cutoff = 1.30
for l in range (1000):
  df1 = df [df ["Cluster"] == l + 1] .reset_index (drop = True) # Extract cluster number
  G = nx.Graph ()
  for i in range (len (df1) -2):
    x = df1.loc [i, "Xnm"]
    y = df1.loc [i, "Ynm"]
    z = df1.loc [i, "Znm"]
    element = df1.loc [i, "element"]
    G.add_node (i, Xnm = x, Ynm = y, Znm = z, Element = element)
    nx.draw (G)
    df2 = df1.loc [i + 1, "Xnm": "Znm"]-df1.loc [i, "Xnm": "Znm"]
    df3 = df2 * df2
    df4 = np.sqrt (df3.sum (axis = 0))
    df5 = df4 [df4<cutoff] # Judge the distance between atoms
    for j in df5.index:
      G.add_edge (i, j)
  H = dgl.from_networkx (G, node_attrs = ["Xnm", "Ynm", "Znm", "Element"])
  dgl_graph_data.append (H)
  nx_graph_data.append (G)
error contents
KeyError Traceback (most recent call last)
<ipython-input-34-f103bb7d2140>in<module>()
     21 for j in df5.index:
     22 G.add_edge (i, j)
--->23 H = dgl.from_networkx (G, node_attrs = ["Xnm", "Ynm", "Znm", "Element"])
     24 dgl_graph_data.append (H)
     25 nx_graph_data.append (G)
/usr/local/lib/python3.6/dist-packages/dgl/convert.py in from_networkx (nx_graph, node_attrs, edge_attrs, edge_id_attr_name, idtype, device)
   1003 for nid in range (g.number_of_nodes ()):
   1004 for attr in node_attrs:
->1005 attr_dict [attr] .append (nx_graph.nodes [nid] [attr])
   1006 for attr in node_attrs:
   1007 g.ndata [attr] = F.copy_to (_batcher (attr_dict [attr]), g.device)
KeyError:'Xnm'

How on earth can I convert from networkx to dgl format?

  • Answer # 1

    for l in range (1000):
      df1 = df [df ["Cluster"] == l + 1] .reset_index (drop = True) # Extract the cluster number here
      G = nx.Graph ()
      for i in range (len (df1)):
        G.add_node (i, Xnm = df1.loc [i, "Xnm"], Ynm = df1.loc [i, "Ynm"], Znm = df1.loc [i, "Znm"], Element = df1.loc [i, "element"]) Add #node
        df2 = df1.loc [i + 1 :, "Xnm": "Znm"]-df1.loc [i, "Xnm": "Znm"]
        df3 = df2 * df2
        df4 = np.sqrt (df3.sum (axis = 1))
        df5 = df4 [df4<cutoff]
        for j in df5.index:
          G.add_edge (i, j)
      H = dgl.from_networkx (G, node_attrs = ["Xnm", "Ynm", "Znm", "Element"]) Convert #netwokx to dgl
      dgl_graph_data.append (H)
      nx_graph_data.append (G)


    It worked by inserting df2 = df1.loc [i + 1 :, "Xnm": "Znm"] and a colon.