2017-01-04 42 views
0

我想使用Python將我收集的數據(從計算機模擬)組織到一個hdf5文件中。 我測量了許多時間步長內某個空間區域內所有原子的位置和速度[x,y,z,vx,vy,vz]。當然,原子的數量隨着時間步長而變化。H5PY - 如何存儲許多不同尺寸的二維數組

一個最小的例子可以如下所示:

[ 
[ [x1,y1,z1,vx1,vy1,vz1], [x2,y2,z2,vx2,vy2,vz2] ], 
[ [x1,y1,z1,vx1,vy1,vz1], [x2,y2,z2,vx2,vy2,vz2], [x3,y3,z3,vx3,vy3,vz3] ] 
] 

(2個時間步, 第一個時間步長:2個原子, 第二時間步驟:3個原子)

我的想法是要建立一個Python中包含所有信息的hdf5數據集。在每個時間步驟中,應該存儲承滴盤位置/所有原子的速度的2D陣列,即

dataset[0] = [ [x1,y1,z1,vx1,vy1,vz1], [x2,y2,z2,vx2,vy2,vz2] ] 
dataset[1] = [ [x1,y1,z1,vx1,vy1,vz1], [x2,y2,z2,vx2,vy2,vz2], [x3,y3,z3,vx3,vy3,vz3] ]. 

的想法是清楚,我想。然而,我努力用數組長度來定義數據集的正確數據類型。

我的代碼如下所示:

import numpy as np 
import h5py 

file = h5py.File ('file.h5','w') 

columnNo = 6  
rowtype = np.dtype("%sfloat32" % columnNo) 
dt = h5py.special_dtype(vlen=np.dtype(rowtype)) 

dataset = file.create_dataset("dset", (2,), dtype=dt) 

print dataset.value 

testarray = np.array([[1.,2.,3.,2.,3.,4.],[1.,2.,3.,2.,3.,4.]]) 
print testarray 

dataset[0] = testarray 
print dataset[0] 

然而,這是行不通的。當我運行腳本時,我收到錯誤消息「AttributeError:'float'object has no attribute'dtype'。」 看來我的定義的dtype是錯誤的。

有沒有人看到它應該如何正確定義?

非常感謝, 斯文

回答

0

在你的情況下,錯誤被埋葬,但很顯然試圖分配testarraydataset發生時:

Traceback (most recent call last): 
    File "stack41465480.py", line 26, in <module> 
    dataset[0] = testarray 
    File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper (/build/h5py-GhwtGD/h5py-2.6.0/h5py/_objects.c:2577) 
... 
    File "h5py/_conv.pyx", line 712, in h5py._conv.ndarray2vlen (/build/h5py-GhwtGD/h5py-2.6.0/h5py/_conv.c:6171) 
AttributeError: 'float' object has no attribute 'dtype' 

我並不熟練與special_dtypevlen,但我能寫numpy結構化陣列h5py

import numpy as np 
import h5py 

file = h5py.File ('file.h5','w') 

columnNo = 6  
# rowtype = np.dtype("%sfloat32" % columnNo) 
rowtype = np.dtype([('f0', '<f4',(6,))]) 
dt = h5py.special_dtype(vlen=np.dtype(rowtype)) 

print('rowtype',rowtype) 
print('dt',dt) 
dataset = file.create_dataset("dset", (2,), dtype=rowtype) 

print('value') 
print(dataset.value[0]) 

arr = np.ones((2,),dtype=rowtype) 
print(repr(arr)) 
dataset[0] = arr[0] 
print(dataset.value) 

testarray = np.array([([1.,2.,3.,2.,3.,4.],),([2.,3.,4.,1.,2.,3.],)], dtype=rowtype) 
print(repr(testarray)) 

dataset[1] = testarray[1] 
print(dataset.value) 
print(dataset.value['f0']) 

生產

1316:~/mypy$ python3 stack41465480.py 
rowtype [('f0', '<f4', (6,))] 
dt object 
value 
([0.0, 0.0, 0.0, 0.0, 0.0, 0.0],) 
array([([1.0, 1.0, 1.0, 1.0, 1.0, 1.0],), ([1.0, 1.0, 1.0, 1.0, 1.0, 1.0],)], 
     dtype=[('f0', '<f4', (6,))]) 
[([1.0, 1.0, 1.0, 1.0, 1.0, 1.0],) ([0.0, 0.0, 0.0, 0.0, 0.0, 0.0],)] 
array([([1.0, 2.0, 3.0, 2.0, 3.0, 4.0],), ([2.0, 3.0, 4.0, 1.0, 2.0, 3.0],)], 
     dtype=[('f0', '<f4', (6,))]) 
[([1.0, 1.0, 1.0, 1.0, 1.0, 1.0],) ([2.0, 3.0, 4.0, 1.0, 2.0, 3.0],)] 
[[ 1. 1. 1. 1. 1. 1.] 
[ 2. 3. 4. 1. 2. 3.]] 
0

感謝您的快速答覆。它幫助了很多。

如果我現在只是把數據集的數據類型

dtype = dt, 

我得到我想有什麼。

這裏,(爲了完整)的Python代碼:

import numpy as np 
import h5py 

file = h5py.File ('file.h5','w') 

columnNo = 6 

rowtype = np.dtype([('f0', '<f4',(6,))]) 
dt = h5py.special_dtype(vlen=np.dtype(rowtype)) 

print('rowtype',rowtype) 
print('dt',dt) 
dataset = file.create_dataset("dset", (2,), dtype=dt) 

# print('value') 
# print(dataset.value[0]) 

arr = np.ones((3,),dtype=rowtype) 
# print(repr(arr)) 
dataset[0] = arr 
# print(dataset.value) 

testarray = np.array([([1.,2.,3.,2.,3.,4.],),([2.,3.,4.,1.,2.,3.],)], dtype=rowtype) 
# print(repr(testarray)) 

dataset[1] = testarray 
print(dataset.value) 
for i in range(2): print dataset[i] 

以及相應的輸出讀取

('rowtype', dtype([('f0', '<f4', (6,))])) 
('dt', dtype('O')) 
[ array([([1.0, 1.0, 1.0, 1.0, 1.0, 1.0],), 
     ([1.0, 1.0, 1.0, 1.0, 1.0, 1.0],), ([1.0, 1.0, 1.0, 1.0, 1.0, 1.0],)], 
     dtype=[('f0', '<f4', (6,))]) 
array([([1.0, 2.0, 3.0, 2.0, 3.0, 4.0],), ([2.0, 3.0, 4.0, 1.0, 2.0, 3.0],)], 
     dtype=[('f0', '<f4', (6,))])] 
[([1.0, 1.0, 1.0, 1.0, 1.0, 1.0],) ([1.0, 1.0, 1.0, 1.0, 1.0, 1.0],) 
([1.0, 1.0, 1.0, 1.0, 1.0, 1.0],)] 
[([1.0, 2.0, 3.0, 2.0, 3.0, 4.0],) ([2.0, 3.0, 4.0, 1.0, 2.0, 3.0],)] 

只是爲了得到它的權利:在我的原代碼的問題是不好的定義我的rowtype數據結構,對吧?

最好, 斯文