Folks,
I need to store MaskedArrays in a HDF5 file, and retrieve them as such. I
wrote a small subclass of Table (MaskedTable, cf a simplified version below)
that overwrites the __init__ and read methods, so that I can just pass a
masked array, store it as a recarray and read it back to a MaskedArray,
seamlessly.
Well, it doesn't really work as expected: when I write a MaskedTable to a
file, it is recognized as that subclass. When I close a file, reopen it and
access the table, it reverts to a standard Table, and of course my tailored
read method isn't accessed. The behavior is illustrated below.
Obviously, I missing something: is there an attribute that I'm not setting
that would let the file recognize that its tables are in fact MaskedTables?
Thanks a lot in advance...
############################
import numpy as np
import numpy.ma as ma
import tables
from tables import File, Table
from tables.file import _checkfilters
from tables.parameters import EXPECTED_ROWS_TABLE
class MaskedTable(Table):
def __init__(self,parentNode, name, maskedarray,
title="", filters=None,
expectedrows=EXPECTED_ROWS_TABLE,
chunkshape=None, byteorder=None, _log=True):
description = np.array(zip(maskedarray.filled().flat,
ma.getmaskarray(maskedarray).flat),
dtype=[('_data',maskedarray.dtype),
('_mask',bool)])
Table.__init__(self,parentNode, name,
description=description, title=title, filters=filters,
expectedrows=expectedrows,
chunkshape=chunkshape, byteorder=byteorder, _log=_log)
self.attrs.SHAPE = maskedarray.shape
def read(self, start=None, stop=None, step=None, field=None):
data = Table.read(self, start=start, stop=stop, step=step,
field=field)
newshape = self.attrs.SHAPE
return ma.array(data['_data'], mask=data['_mask']).reshape(newshape)
def createMaskedTable(self, where, name, maskedarray, title="",
filters=None, expectedrows=10000,
chunkshape=None, byteorder=None,
createparents=False):
parentNode = self._getOrCreatePath(where, createparents)
_checkfilters(filters)
return MaskedTable(parentNode, name, maskedarray,
title=title, filters=filters,
expectedrows=expectedrows,
chunkshape=chunkshape, byteorder=byteorder)
File.createMaskedTable = createMaskedTable
if __name__ == '__main__':
x = ma.array(np.random.rand(100),mask=(np.random.rand(100) > 0.7))
h5file = tables.openFile('tester.hdf5','w')
mtab = h5file.createMaskedTable('/','random',x)
h5file.flush()
print type(mtab)
print mtab.read()
h5file.close()
h5file = tables.openFile('tester.hdf5','r')
mtab = h5file.root.random
print type(mtab)
print mtab.read()
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Pytables-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pytables-users