File: test_table_node.py

package info (click to toggle)
python-apptools 5.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,552 kB
  • sloc: python: 9,868; makefile: 80
file content (80 lines) | stat: -rw-r--r-- 2,782 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# (C) Copyright 2005-2025 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
import unittest

from apptools._testing.optional_dependencies import (
    numpy as np,
    pandas,
    tables,
    requires_numpy,
    requires_pandas,
    requires_tables,
)

if np is not None and tables is not None:
    from ..table_node import H5TableNode
    from .utils import temp_h5_file


NODE = "/table_node"


@requires_numpy
@requires_tables
class TableNodeTestCase(unittest.TestCase):
    def test_basics(self):
        description = [("a", np.float64), ("b", np.float64)]
        with temp_h5_file() as h5:
            h5table = H5TableNode.add_to_h5file(h5, NODE, description)
            h5table.append({"a": [1, 2], "b": [3, 4]})

            np.testing.assert_allclose(h5table["a"], [1, 2])
            np.testing.assert_allclose(h5table["b"], [3, 4])

        dtype_description = np.dtype([("c", "f4"), ("d", "f4")])
        with temp_h5_file() as h5:
            h5table = H5TableNode.add_to_h5file(h5, NODE, dtype_description)
            h5table.append({"c": [1.2, 3.4], "d": [5.6, 7.8]})

            np.testing.assert_allclose(h5table["c"], [1.2, 3.4])
            np.testing.assert_allclose(h5table["d"], [5.6, 7.8])

            assert len(repr(h5table)) > 0

    def test_getitem(self):
        description = [("a", np.float64), ("b", np.float64)]
        with temp_h5_file() as h5:
            h5table = H5TableNode.add_to_h5file(h5, NODE, description)
            h5table.append({"a": [1, 2], "b": [3, 4]})
            np.testing.assert_allclose(h5table["a"], (1, 2))
            np.testing.assert_allclose(h5table[["b", "a"]], [(3, 1), (4, 2)])

    def test_keys(self):
        description = [("hello", "int"), ("world", "int"), ("Qux1", "bool")]
        with temp_h5_file() as h5:
            keys = set(list(zip(*description))[0])
            h5table = H5TableNode.add_to_h5file(h5, NODE, description)
            assert set(h5table.keys()) == keys

    @requires_pandas
    def test_to_dataframe(self):
        description = [("a", np.float64)]
        with temp_h5_file() as h5:
            h5table = H5TableNode.add_to_h5file(h5, NODE, description)
            h5table.append({"a": [1, 2, 3]})
            df = h5table.to_dataframe()
            assert isinstance(df, pandas.DataFrame)
            np.testing.assert_allclose(df["a"], h5table["a"])


if __name__ == "__main__":
    from numpy import testing

    testing.run_module_suite()