| 1 |
# -*- coding: utf-8 -*- |
| 2 |
from __future__ import division, print_function, absolute_import |
| 3 |
|
| 4 |
|
| 5 |
from itertools import chain |
| 6 |
from tempfile import NamedTemporaryFile |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
from petl.test.helpers import ieq |
| 11 |
from petl.transform.sorts import sort |
| 12 |
import petl as etl |
| 13 |
from petl.io.pytables import fromhdf5, fromhdf5sorted, tohdf5, appendhdf5 |
| 14 |
|
| 15 |
|
| 16 |
try: |
| 17 |
import tables # noqa: F401 |
| 18 |
except ImportError as e: |
| 19 |
pytest.skip('SKIP pytables tests: %s' % e, allow_module_level=True) |
| 20 |
else: |
| 21 |
|
| 22 |
class FooBar(tables.IsDescription): |
| 23 |
foo = tables.Int32Col(pos=0) |
| 24 |
bar = tables.StringCol(6, pos=2) |
| 25 |
|
| 26 |
def test_fromhdf5(): |
| 27 |
|
| 28 |
f = NamedTemporaryFile() |
| 29 |
|
| 30 |
# set up a new hdf5 table to work with |
| 31 |
h5file = tables.open_file(f.name, mode='w', title='Test file') |
| 32 |
h5file.create_group('/', 'testgroup', 'Test Group') |
| 33 |
h5table = h5file.create_table('/testgroup', 'testtable', FooBar, 'Test Table') |
| 34 |
|
| 35 |
# load some data into the table |
| 36 |
table1 = (('foo', 'bar'), |
| 37 |
(1, b'asdfgh'), |
| 38 |
(2, b'qwerty'), |
| 39 |
(3, b'zxcvbn')) |
| 40 |
for row in table1[1:]: |
| 41 |
for i, fld in enumerate(table1[0]): |
| 42 |
h5table.row[fld] = row[i] |
| 43 |
h5table.row.append() |
| 44 |
h5file.flush() |
| 45 |
h5file.close() |
| 46 |
|
| 47 |
# verify we can get the data back out |
| 48 |
table2a = fromhdf5(f.name, '/testgroup', 'testtable') |
| 49 |
ieq(table1, table2a) |
| 50 |
ieq(table1, table2a) |
| 51 |
|
| 52 |
# verify we can get the data back out |
| 53 |
table2b = fromhdf5(f.name, '/testgroup/testtable') |
| 54 |
ieq(table1, table2b) |
| 55 |
ieq(table1, table2b) |
| 56 |
|
| 57 |
# verify using an existing tables.File object |
| 58 |
h5file = tables.open_file(f.name) |
| 59 |
table3 = fromhdf5(h5file, '/testgroup/testtable') |
| 60 |
ieq(table1, table3) |
| 61 |
|
| 62 |
# verify using an existing tables.Table object |
| 63 |
h5tbl = h5file.get_node('/testgroup/testtable') |
| 64 |
table4 = fromhdf5(h5tbl) |
| 65 |
ieq(table1, table4) |
| 66 |
|
| 67 |
# verify using a condition to filter data |
| 68 |
table5 = fromhdf5(h5tbl, condition="(foo < 3)") |
| 69 |
ieq(table1[:3], table5) |
| 70 |
|
| 71 |
# clean up |
| 72 |
h5file.close() |
| 73 |
|
| 74 |
def test_fromhdf5sorted(): |
| 75 |
|
| 76 |
f = NamedTemporaryFile() |
| 77 |
|
| 78 |
# set up a new hdf5 table to work with |
| 79 |
h5file = tables.open_file(f.name, mode='w', title='Test file') |
| 80 |
h5file.create_group('/', 'testgroup', 'Test Group') |
| 81 |
h5table = h5file.create_table('/testgroup', 'testtable', FooBar, |
| 82 |
'Test Table') |
| 83 |
|
| 84 |
# load some data into the table |
| 85 |
table1 = (('foo', 'bar'), |
| 86 |
(3, b'asdfgh'), |
| 87 |
(2, b'qwerty'), |
| 88 |
(1, b'zxcvbn')) |
| 89 |
for row in table1[1:]: |
| 90 |
for i, f in enumerate(table1[0]): |
| 91 |
h5table.row[f] = row[i] |
| 92 |
h5table.row.append() |
| 93 |
h5table.cols.foo.create_csindex() |
| 94 |
h5file.flush() |
| 95 |
|
| 96 |
# verify we can get the data back out |
| 97 |
table2 = fromhdf5sorted(h5table, sortby='foo') |
| 98 |
ieq(sort(table1, 'foo'), table2) |
| 99 |
ieq(sort(table1, 'foo'), table2) |
| 100 |
|
| 101 |
# clean up |
| 102 |
h5file.close() |
| 103 |
|
| 104 |
def test_tohdf5(): |
| 105 |
|
| 106 |
f = NamedTemporaryFile() |
| 107 |
|
| 108 |
# set up a new hdf5 table to work with |
| 109 |
h5file = tables.open_file(f.name, mode="w", title="Test file") |
| 110 |
h5file.create_group('/', 'testgroup', 'Test Group') |
| 111 |
h5file.create_table('/testgroup', 'testtable', FooBar, 'Test Table') |
| 112 |
h5file.flush() |
| 113 |
h5file.close() |
| 114 |
|
| 115 |
# load some data via tohdf5 |
| 116 |
table1 = (('foo', 'bar'), |
| 117 |
(1, b'asdfgh'), |
| 118 |
(2, b'qwerty'), |
| 119 |
(3, b'zxcvbn')) |
| 120 |
|
| 121 |
tohdf5(table1, f.name, '/testgroup', 'testtable') |
| 122 |
ieq(table1, fromhdf5(f.name, '/testgroup', 'testtable')) |
| 123 |
|
| 124 |
tohdf5(table1, f.name, '/testgroup/testtable') |
| 125 |
ieq(table1, fromhdf5(f.name, '/testgroup/testtable')) |
| 126 |
|
| 127 |
h5file = tables.open_file(f.name, mode="a") |
| 128 |
tohdf5(table1, h5file, '/testgroup/testtable') |
| 129 |
ieq(table1, fromhdf5(h5file, '/testgroup/testtable')) |
| 130 |
|
| 131 |
h5table = h5file.get_node('/testgroup/testtable') |
| 132 |
tohdf5(table1, h5table) |
| 133 |
ieq(table1, fromhdf5(h5table)) |
| 134 |
|
| 135 |
# clean up |
| 136 |
h5file.close() |
| 137 |
|
| 138 |
def test_tohdf5_create(): |
| 139 |
|
| 140 |
table1 = (('foo', 'bar'), |
| 141 |
(1, b'asdfgh'), |
| 142 |
(2, b'qwerty'), |
| 143 |
(3, b'zxcvbn')) |
| 144 |
|
| 145 |
f = NamedTemporaryFile() |
| 146 |
|
| 147 |
# test creation with defined datatype |
| 148 |
tohdf5(table1, f.name, '/testgroup', 'testtable', create=True, |
| 149 |
drop=True, description=FooBar, createparents=True) |
| 150 |
ieq(table1, fromhdf5(f.name, '/testgroup', 'testtable')) |
| 151 |
|
| 152 |
# test dynamically determined datatype |
| 153 |
tohdf5(table1, f.name, '/testgroup', 'testtable2', create=True, |
| 154 |
drop=True, createparents=True) |
| 155 |
ieq(table1, fromhdf5(f.name, '/testgroup', 'testtable2')) |
| 156 |
|
| 157 |
def test_appendhdf5(): |
| 158 |
|
| 159 |
f = NamedTemporaryFile() |
| 160 |
|
| 161 |
# set up a new hdf5 table to work with |
| 162 |
h5file = tables.open_file(f.name, mode="w", title="Test file") |
| 163 |
h5file.create_group('/', 'testgroup', 'Test Group') |
| 164 |
h5file.create_table('/testgroup', 'testtable', FooBar, 'Test Table') |
| 165 |
h5file.flush() |
| 166 |
h5file.close() |
| 167 |
|
| 168 |
# load some initial data via tohdf5() |
| 169 |
table1 = (('foo', 'bar'), |
| 170 |
(1, b'asdfgh'), |
| 171 |
(2, b'qwerty'), |
| 172 |
(3, b'zxcvbn')) |
| 173 |
tohdf5(table1, f.name, '/testgroup', 'testtable') |
| 174 |
ieq(table1, fromhdf5(f.name, '/testgroup', 'testtable')) |
| 175 |
|
| 176 |
# append some more data |
| 177 |
appendhdf5(table1, f.name, '/testgroup', 'testtable') |
| 178 |
ieq(chain(table1, table1[1:]), fromhdf5(f.name, '/testgroup', |
| 179 |
'testtable')) |
| 180 |
|
| 181 |
def test_integration(): |
| 182 |
|
| 183 |
f = NamedTemporaryFile() |
| 184 |
|
| 185 |
# set up a new hdf5 table to work with |
| 186 |
h5file = tables.open_file(f.name, mode="w", title="Test file") |
| 187 |
h5file.create_group('/', 'testgroup', 'Test Group') |
| 188 |
h5file.create_table('/testgroup', 'testtable', FooBar, 'Test Table') |
| 189 |
h5file.flush() |
| 190 |
h5file.close() |
| 191 |
|
| 192 |
# load some initial data via tohdf5() |
| 193 |
table1 = etl.wrap((('foo', 'bar'), |
| 194 |
(1, b'asdfgh'), |
| 195 |
(2, b'qwerty'), |
| 196 |
(3, b'zxcvbn'))) |
| 197 |
table1.tohdf5(f.name, '/testgroup', 'testtable') |
| 198 |
ieq(table1, etl.fromhdf5(f.name, '/testgroup', 'testtable')) |
| 199 |
|
| 200 |
# append some more data |
| 201 |
table1.appendhdf5(f.name, '/testgroup', 'testtable') |
| 202 |
ieq(chain(table1, table1[1:]), etl.fromhdf5(f.name, '/testgroup', |
| 203 |
'testtable')) |