AttributeError: 'Dataset' object has no attribute 'value'

Viewed 12252

I got this error when using a package to read hdf5 files:

dataset.value

Error:

Traceback (most recent call last):
  File "train.py", line 163, in <module>
    train(0, False, args.gpu_list, args.model_path)
  File "train.py", line 76, in train
    dataset = Ani1Dataset(dir_path='/data/ANI-1_release')
  File "/code/ani1dataset.py", line 16, in __init__
    self.parse(dir_path)
  File "/code/ani1dataset.py", line 32, in parse
    for molecule in adl:
  File "/code/pyanitools.py", line 75, in __iter__
    for data in self.h5py_dataset_iterator(self.store):
  File "/code/pyanitools.py", line 71, in h5py_dataset_iterator
    yield from self.h5py_dataset_iterator(item, path)
  File "/code/pyanitools.py", line 60, in h5py_dataset_iterator
    dataset = np.array(item[k].value)
AttributeError: 'Dataset' object has no attribute 'value'
3 Answers

The dataset.value attribute was deprecated. Either use:

dataset[()]

or downgrade h5py to use the old syntax:

pip3 install --upgrade pip && pip3 install h5py=='2.9.0'

Yes, .value has been deprecated for some time. As mentioned in my comments above, I would not downgrade to h5py 2.9.0 without a compelling reason. It was released in 2014. The current h5py release is 3.2 and supports the latest HDF5 formats and has many enhancements and error corrections.

There are 2 primary ways to access HDF5 data with h5py. Briefly, you can:

  1. Return a h5py dataset object. A dataset object behaves "as-if" it is an array, but does not load the data into memory until needed.
  2. Return a NumPy array. This immediately loads the data into memory.

Complete h5py dataset documentation here:
Examples of each below:

with h5py.File('filename.h5','r') as h5f:
    # return a h5py dataset object:
    my_ds_obj = h5f[ds_name]
    # return a h5py dataset object:
    my_ds_arr = h5f[ds_name][:]

In addition, if you only want to read some of the data, h5py supports most NumPy slicing syntax, including a subset of fancy indexing. h5py fancy indexing doc

Related