2017-01-04 2 views
0

Ich möchte meine gesammelten Daten (aus Computersimulationen) in eine hdf5-Datei mit Python organisieren. Ich habe Positionen und Geschwindigkeiten [x, y, z, vx, vy, vz] aller Atome innerhalb einer bestimmten Raumregion über viele Zeitschritte gemessen. Die Anzahl der Atome variiert natürlich von Zeitschritt zu Zeitschritt.H5PY - So speichern Sie viele 2D-Arrays unterschiedlicher Dimensionen

Ein minimales Beispiel könnte wie folgt aussehen:

[ 
[ [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 Zeitschritte, ersten Zeitschritt: 2 Atome, zweiter Zeitschritt: 3 Atome)

Meine Idee ein zu schaffen, war hdf5-Dataset in Python, das alle Informationen speichert. Bei jedem Zeitschritt sollte es speichert eine 2D-Anordnung von all Positionen/Geschwindigkeiten aller Atom, d.h.

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] ]. 

Die Idee ist klar, glaube ich. Ich kämpfe jedoch mit der Definition des korrekten Datentyps des Datensatzes mit variierender Array-Länge.

Mein Code sieht wie folgt aus:

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] 

Dies ist jedoch nicht funktioniert. Wenn ich das Skript ausführe, bekomme ich die Fehlermeldung "AttributeError: 'float' Objekt hat kein Attribut 'dtype'." Es scheint, dass mein definierter dtype falsch ist.

Kann jemand sehen, wie es richtig definiert werden sollte?

Vielen Dank, Sven

Antwort

0

Der Fehler in Ihrem Fall begraben liegt, obwohl es klar ist, kommt es, wenn die testarray auf die dataset zuweisen versuchen:

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' 

Ich bin nicht Mann mit der special_dtype und vlen, aber ich konnte eine numpy strukturierte Arrays zu h5py schreiben.

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']) 

Herstellung

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

Danke für die schnelle Antwort. Es hat sehr geholfen.

Wenn ich jetzt einfach die Art des Datensatzes Daten ändern

dtype = dt, 

ich bekommen, was ich haben möchte.

Hier ist der Python-Code (auf Vollständigkeit):

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] 

Und zu entsprechendem Ausgang liest

('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],)] 

einfach es richtig: Das Problem in meinem ursprünglichen Code war eine schlechte Definition meine Rowtype Datenstruktur, richtig?

Beste, Sven

Verwandte Themen