我正在使用PyTables 2.2.1 w / Python 2.6,我想创建一个包含可变长度嵌套数组的表。
我搜索了PyTables文档和教程示例(PyTables教程3.8)展示了如何创建长度为1的嵌套数组。但是对于这个例子,我如何向数据'info2 / info3 / x'和'info2 / info3 / y'添加可变数量的行?
对于可能更容易理解的表结构,这是我的本土示例:
"""Desired Pytable output:
DIEM TEMPUS Temperature Data
5 0 100 Category1 <--||--> Category2
x <--| |--> y z <--|
0 0 0
2 1 1
4 1.33 2.67
6 1.5 4.5
8 1.6 6.4
5 1 99
2 2 0
4 2 2
6 2 4
8 2 6
5 2 96
4 4 0
6 3 3
8 2.67 5.33
Note that nested arrays have variable length.
"""
import tables as ts
tableDef = {'DIEM': ts.Int32Col(pos=0),
'TEMPUS': ts.Int32Col(pos=1),
'Temperature' : ts.Float32Col(pos=2),
'Data':
{'Category1':
{
'x': ts.Float32Col(),
'y': ts.Float32Col()
},
'Category2':
{
'z': ts.Float32Col(),
}
}
}
# create output file
fpath = 'TestDb.h5'
fh = ts.openFile(fpath, 'w')
# define my table
tableName = 'MyData'
fh.createTable('/', tableName, tableDef)
tablePath = '/'+tableName
table = fh.getNode(tablePath)
# get row iterator
row = table.row
for i in xrange(3):
print '\ni=', i
# calc some fake data
row['DIEM'] = 5
row['TEMPUS'] = i
row['Temperature'] = 100-i**2
for j in xrange(5-i):
# Note that nested array has variable number of rows
print 'j=', j,
# calc some fake nested data
val1 = 2.0*(i+j)
val2 = val1/(j+1.0)
val3 = val1 - val2
''' Magic happens here...
How do I write 'j' rows of data to the elements of
Category1 and/or Category2?
In bastardized pseudo-code, I want to do:
row['Data/Category1/x'][j] = val1
row['Data/Category1/y'][j] = val2
row['Data/Category2/z'][j] = val3
'''
row.append()
table.flush()
fh.close()
我没有在PyTables文档中发现任何迹象表明这样的结构是不可能的......但是如果这样的结构实际上是不可能的,那么我对可变长度嵌套列的替代方法是什么?
- EArray? VLArray?如果是这样,如何将这些数据类型集成到上述结构中?
- 其他一些想法?
非常感谢任何帮助!
编辑附加信息: 似乎PyTables专家已经解决了“这样一个可能的结构”的问题:
那么有没有人想出一种方法来创建一个类似的PyTable数据结构?
再次感谢!