Skip to content

Instantly share code, notes, and snippets.

@swarbhanu
Created November 23, 2011 20:57
Show Gist options
  • Save swarbhanu/1389889 to your computer and use it in GitHub Desktop.
Save swarbhanu/1389889 to your computer and use it in GitHub Desktop.
An example of creating two differently shaped hyperslabs in a CArray and a VLArray using the same data contained in a numpy array
#################################################################################################
# #
# This program takes a numpy array and feeds it into a hyperslab in a CArray. The numpy array #
# is then changed to a different shape and fed into a hyperslab in a VLArray. The end result #
# is that one has two differently shaped hyperslabs of the same data. #
# #
#################################################################################################
import sys
import tables as tb
import numpy as np
#initialize
filename='vlarrayEx.h5'
filenameC= 'carrayEx1.h5'
atomVL=tb.Int32Atom(shape=())
atomC=tb.UInt8Atom()
#opening a file for CArray and creating a Carray
fileh=tb.openFile(filenameC, 'w')
carray = fileh.createCArray(fileh.root, 'carray', atomC, shape=(200,300))
#writing to a hyperslab in the CArray
smallArray=np.array(np.arange(5*3)).reshape((5,3))
#copy the data into a hyperslab in the CArray. Using step size '2', the data is written in
# alternating columns....
carray[10:15,10:15:2] =smallArray
print "We copy the (5,3) array,"
print smallArray
print ""
print "as a hyperslab into an empty large CArray, writing data in alternate columns."
print ""
print "The portion of the CArray, carray[5:15,5:15] ="
print carray[5:15,5:15]
#opening a file for VL array and creating a VL array
fileh=tb.openFile(filename,'w')
#create a filter that uses the zlib library, shuffle and fletcher32 for faster compression
filters=tb.Filters(complevel=1,complib='zlib',shuffle=True,fletcher32=True)
#create the vlarray using the defined filter
vlarray=fileh.createVLArray(fileh.root,'vlarray',atomVL,"Experimental VL Array", \
filters,expectedsizeinMB=1.0,chunkshape=None)
#changing the shape of the hyperslab
anotherArray=np.array(smallArray).reshape((3,5))
print ""
print "Reshaping the hyperslab to (3,5)"
print anotherArray
#Copy hyperslab to VLArray
for x in anotherArray:
vlarray.append(x)
print ""
print "and copying into the following VLArray:"
print vlarray[:]
print ""
print "Printing each rows of the array..."
for s in vlarray:
print '%s [%d] --> %s' % (vlarray.name,vlarray.nrow,s)
#
#close file
fileh.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment