2015-11-19 66 views
0

我想將尺寸標度附加到我想用python存儲在hdf5文件中的數據集,但在設置它們後嘗試打印屬性時出現錯誤。相關的代碼段的內容如下:在python中正確設置和讀取hdf5文件中的dimscale

import h5py 
import numpy as np 

# create data and x-axis 
my_data = np.random.randint(10, size=(100, 200)) 
x_axis = np.linspace(0, 1, 100) 

h5f = h5.File('my_file.h5','w') 
h5f.create_dataset('data_1', data=my_data) 
h5f['data_1'].dims[0].label = 'm' 
h5f['data_1'].dims.create_scale(h5f['x_axis'], 'x') 

# the following line is creating the problems 
h5f['data_1'].dims[0].attach_scale(h5f['x_axis']) 

# this is where the crash happens but only if the above line is included 
for ii in h5f['data_1'].attrs.items(): 
    print ii 

h5f.close() 

的命令print(h5.version.info)打印以下輸出:

Summary of the h5py configuration 
--------------------------------- 

h5py 2.2.1 
HDF5 1.8.11 
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] 
sys.platform linux2 
sys.maxsize  9223372036854775807 
numpy 1.8.2 

錯誤消息是以下各項:

Traceback (most recent call last): 
    File "HDF_write_dimScales.py", line 16 
    for ii in h5f['data_1'].attrs.items(): 
    File "/usr/lib/python2.7/dist-packages/h5py/_hl/base.py", line 347, in items 
    return [(x, self.get(x)) for x in self] 
    File "/usr/lib/python2.7/dist-packages/h5py/_hl/base.py", line 310, in get 
    return self[name] 
    File "/usr/lib/python2.7/dist-packages/h5py/_hl/attrs.py", line 55, in __getitem__ 
    rtdt = readtime_dtype(attr.dtype, []) 
    File "h5a.pyx", line 318, in h5py.h5a.AttrID.dtype.__get__ (h5py/h5a.c:4285) 
    File "h5t.pyx", line 337, in h5py.h5t.TypeID.py_dtype (h5py/h5t.c:3892) 
TypeError: No NumPy equivalent for TypeVlenID exists 

任何意見或提示是理解。

回答

1

它與一些輕微的調整,我在h5py 2.5.0。問題可能與您撥打create_scale時有關。用h5py 2.5.0,我在create_scale()調用中得到KeyErrorh5f['x_axis']。爲了讓你的例子發揮作用,我必須首先明確創建x_axis數據集。

import h5py 
import numpy as np 

# create data and x-axis 
my_data = np.random.randint(10, size=(100, 200)) 

# Use a context manager to ensure h5f is closed 
with h5py.File('my_file.h5','w') as h5f: 
    h5f.create_dataset('data_1', data=my_data) 

    # Create the x_axis dataset directly in the HDF5 file 
    h5f['x_axis'] = np.linspace(0, 1, 100) 

    h5f['data_1'].dims[0].label = 'm' 

    # Now we can create and attach the scale without problems 
    h5f['data_1'].dims.create_scale(h5f['x_axis'], 'x') 
    h5f['data_1'].dims[0].attach_scale(h5f['x_axis']) 

    for ii in h5f['data_1'].attrs.items(): 
     print(ii) 

# Output 
#(u'DIMENSION_LABELS', array(['m', ''], dtype=object)) 
#(u'DIMENSION_LIST', array([array([<HDF5 object reference>], dtype=object), 
#  array([], dtype=object)], dtype=object)) 

如果你仍然有問題,您可能需要升級到h5py 2.5.0,它具有更好的操控性(雖然還不夠完善)VLEN個類型。

+0

感謝您的答案,但它不能解決我的問題。我想我會檢查我正在工作的服務器的更新可能性(我沒有超級用戶/ root訪問權限)。 – Alf

+1

@Alf如果您有權訪問pip,您可以使用'pip install --user h5py'在主目錄中安裝東西。 – Yossarian

+0

我只是做了這幾分鐘才發佈它;)它現在工作(與我的代碼上面)和輸出讀取: – Alf