Skip to content

Instantly share code, notes, and snippets.

@swarbhanu
Created November 29, 2011 15:43
Show Gist options
  • Save swarbhanu/1405233 to your computer and use it in GitHub Desktop.
Save swarbhanu/1405233 to your computer and use it in GitHub Desktop.
This program creates a VLArray using PyTables. One dimensional Numpy arrays of varying length are appended as rows.
##################################################################################
# #
# This program creates a VLArray using PyTables. One dimensional numpy arrays of #
# varying length are appended as rows # # #
# #
###################################################################################
import tables as tb
import numpy as np
#initialize
filename='vlarrayEx.h5'
atom=tb.Int32Atom(shape=())
#open hdf5 file and create an vlarray
fileh=tb.openFile(filename,'w')
vlarray=fileh.createVLArray(fileh.root,'vlarray',atom,"Experimental VL Array", \
filters=tb.Filters(1))
#append variable length rows
vlarray.append(np.array([x for x in range(10)]))
vlarray.append(np.array([x**2 for x in range(2)]))
vlarray.append(np.array([x**3 for x in range(3)]))
#close file
fileh.close()
#open the file again to append
file1=tb.openFile(filename,'a')
root=file1.root
print "root --> ", root
arr=root.vlarray
#print file1
#append more rows
arr.append(np.array([4*x for x in range(4)]))
#read the strings in the file
for s in arr:
print '%s [%d] --> %s' % (arr.name,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