This is driving me insane. Here is the code I have:
import pandas as pd
import numpy as np
import re
import torch
import glob
import dgl
from dgl.data import DGLDataset
class InductorDataset(DGLDataset):
def __init__(self):
super().__init__(name='inductor_dataset')
def process(self):
n_node = []
n_edge = []
self.graphs = []
self.labels = []
for i,file in enumerate(glob.iglob(r'./fh_spiral_*.inp')):
self.node_pos = []
self.node_attr = []
src = []
dst = []
n_count = 0
e_count = 0
self.edge_attr = []
self.targets = []
array = []
df = pd.read_csv(file,header =None)
mat_df = pd.read_csv('../mat_files/L0_cell_LNA_SGM_'+ str(i) + '.mat',skiprows = 1,header=None,delim_whitespace=True)
for index, row in df.iterrows():
if row[0][0] == 'N':
n_count = n_count + 1
array = re.findall(r'[\d\.\d]+', row[0])
n = array[0]
x = array[1]
y = array[2]
z = 0.00
self.node_pos.append(np.array((x,y,z),dtype= np.float32))
#node_attr.append(np.array([1,2,3]))
if row[0][0] == 'E':
e_count = e_count + 1
self.edge_attr.append([5.0,3.2]) # w= 5.0, T = 3.2
src.append(e_count)
dst.append(e_count + 1)
src = np.array(src).astype(np.int64)
dst = np.array(dst).astype(np.int64)
n_node.append(n_count)
n_edge.append(e_count)
label = float(mat_df[1].to_list()[0]) #nH
#Create a graph and add it to the list of graphs and labels.
g = dgl.graph((src, dst))
#self.node_pos = np.concatenate(self.node_pos, axis = 0)
#g.ndata['pos'] = torch.tensor(self.node_pos, dtype=torch.float32)
#g.ndata['attr'] = F.tensor(self.node_attr[self.n_cumsum[idx]:self.n_cumsum[idx+1]], dtype=F.data_type_dict['float32'])
#g.edata['edge_attr'] = torch.tensor(self.edge_attr[self.ne_cumsum[idx]:self.ne_cumsum[idx+1]], dtype=torch.float32)
self.graphs.append(g)
self.labels.append(label)
self.node_pos = np.concatenate(self.node_pos, axis = 0)
self.n_cumsum = np.concatenate([[0], np.cumsum(n_node)])
self.ne_cumsum = np.concatenate([[0], np.cumsum(n_edge)])
self.labels = torch.tensor(self.labels)
def __getitem__(self, idx):
pos = self.node_pos[self.n_cumsum[idx]:self.n_cumsum[idx+1]]
self.graphs[idx].ndata['pos'] = torch.tensor(pos, dtype=torch.float32)
return self.graphs[idx], self.labels[idx]
def __len__(self):
return len(self.graphs)
But when create an instance:
inductor_graph = InductorDataset()
and call:
inductor_graph[0]
I get:
DGLError: Expect number of features to match number of nodes (len(u)). Got 14 and 15 instead.