I'm getting this error trying to analyze data from an HDF5 file directly in python. This code works fine on my linux machine, but I get this error trying to compile the same script on my mac in Spyder3. The reason I'm trying to use my mac is because I'm not sure how to write and run a script on linux terminal.
def dataProcessing(datafile):
import h5py
import numpy as np
import matplotlib.pyplot as plt
import pylab
f = h5py.File(datafile, 'r')
#print(f)
#print("Keys: %s" % f.keys())
groupKeyVal = f.keys()[4]
rawData = list(f[groupKeyVal])
rawDataMat = np.matrix(rawData)
for i in range(0,len(rawDataMat[:,0])):
fig = rawDataMat[i,:]
wav = np.squeeze(np.asarray(fig))
plt.plot(wav)
plt.show()
In Python3, dictionary keys
returns a 'view', not an indexable list.
In [80]: d={'a':1, 'b':2}
In [81]: d.keys()
Out[81]: dict_keys(['a', 'b'])
In [82]: d.keys()[0]
....
TypeError: 'dict_keys' object does not support indexing
Similarly for the dictionary like keys from h5
groups
In [86]: f = h5py.File('data.h5')
In [87]: f.keys()
Out[87]: KeysView(<HDF5 file "data.h5" (mode r+)>)
In [88]: f.keys()[0]
....
TypeError: 'KeysView' object does not support indexing
In [89]: list(f.keys())
Out[89]: ['dset', 'dset1', 'vset']
In [90]: list(f.keys())[1]
Out[90]: 'dset1'
Adding the list
is a bit of a bother, but it makes iteration on the keys more memory efficient.
In [92]: for k in f.keys():print(f[k])
<HDF5 dataset "dset": shape (3, 5), type "<f8">
<HDF5 dataset "dset1": shape (2, 3, 10), type "<f8">
<HDF5 dataset "vset": shape (100,), type "|O">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With