Converting HDF file to Pytorch torch geometric data format

I am trying to convert a hdf5 file to a torch geometric format by wrapping the loaded input with the tg.data.data function but I am getting the following error. Has anyone happened to run into this before?

class HDFDataset(tg.data.Dataset):

  def __init__(self, path, **kwargs):
    print('Path: ', path)
    '''Initialiser for HDFDataset class'''
    self.files = h5py.File(path, 'r')
    self.data = list((self.files).keys())

  def __len__(self):
    return len(self.data)

  def __getitem__(self, idx):
    '''Return training information at provided index'''
    if not 0 <= idx < len(self):
      raise Exception(f'Event number {idx} invalid – must be in range 0 -> {len(self)-1}.')
    # Retrieve datasets from graph at index
    f = self.files
    ret = { dataset: f[str(idx)][dataset][:] for dataset in f[str(idx)].keys() }
    ret['edge_index'] = torch.tensor(ret['edge_index'][:])
    return tg.data.Data(**ret)

Traceback (most recent call last):
File “model_shuffle.py”, line 80, in
print(‘HDF DataLoader ‘, next(iter(h_loader)))
File “/Users/songhyunlee/venv/lib/python3.8/site-packages/torch/utils/data/dataloader.py”, line 517, in next
data = self._next_data()
File “/Users/songhyunlee/venv/lib/python3.8/site-packages/torch/utils/data/dataloader.py”, line 557, in _next_data
data = self._dataset_fetcher.fetch(index) # may raise StopIteration
File “/Users/songhyunlee/venv/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py”, line 47, in fetch
return self.collate_fn(data)
File “/Users/songhyunlee/venv/lib/python3.8/site-packages/torch_geometric/data/dataloader.py”, line 36, in call
return self.collate(batch)
File “/Users/songhyunlee/venv/lib/python3.8/site-packages/torch_geometric/data/dataloader.py”, line 16, in collate
return Batch.from_data_list(batch, self.follow_batch,
File “/Users/songhyunlee/venv/lib/python3.8/site-packages/torch_geometric/data/batch.py”, line 105, in from_data_list
inc = data.inc(key, item)
File “/Users/songhyunlee/venv/lib/python3.8/site-packages/torch_geometric/data/data.py”, line 190, in inc
return self.num_nodes if bool(re.search(’(index|face)’, key)) else 0
File “/Users/songhyunlee/venv/lib/python3.8/site-packages/torch_geometric/data/data.py”, line 215, in num_nodes
return item.size(self.cat_dim(key, item))
TypeError: ‘int’ object is not callable

I guess you are using a numpy array, which returns an int when accessing the size attribute, while PyTorch tensors provide a size() method, which would return the size of the tensor:

a = np.random.randn(3, 2)
print(a.size)
> 6
print(a.size())
> TypeError: 'int' object is not callable

so you could transform the numpy array to a tensor.