Skip to content

Instantly share code, notes, and snippets.

@swarbhanu
Created November 29, 2011 14:48
Show Gist options
  • Save swarbhanu/1405049 to your computer and use it in GitHub Desktop.
Save swarbhanu/1405049 to your computer and use it in GitHub Desktop.
This program creates a EArray using PyTables. One dimension is extendable. One dimensional numpy arrays of the same length are appended as rows.
import tables as tb
import numpy as np
#initialize
filename='earrayEx.h5'
atom=tb.StringAtom(itemsize=8)
shape=(0,)
#open hdf5 file and create an earray
fileh=tb.openFile(filename,'w')
earray=fileh.createEArray(fileh.root,'earray',atom,shape,"Experimental..",expectedrows=4)
#create a one dimensional array for a row
arow=np.array(np.arange(5))
print ""
print "The rows,"
print ""
print arow
print np.array([x**2 for x in range(5)])
print np.array([x**3 for x in range(5)])
print ""
print "are appended to an empty EArray, resulting in the following EArray,"
print ""
#append rows
earray.append(arow)
earray.append(np.array([x**2 for x in range(5)]))
earray.append(np.array([x**3 for x in range(5)]))
#close file
fileh.close()
#open the file again to append
file1=tb.openFile(filename,'a')
root=file1.root
arr=root.earray
#print file1
#append more rows
arr.append(np.array([4*x for x in range(5)]))
#read the strings in the file
for s in arr:
print 'earray[%s] => %r' % (arr.nrow,s)
#close the file
file1.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment