From 0d0c37db2aae2268860452e2adf35c1018ee0915 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20N=C3=B6the?= Date: Mon, 27 May 2019 16:19:38 +0200 Subject: [PATCH] Fix writing of booleans --- ctapipe/io/hdf5tableio.py | 2 +- ctapipe/io/tests/test_hdf5.py | 28 ++++++++++++++++++++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/ctapipe/io/hdf5tableio.py b/ctapipe/io/hdf5tableio.py index a963e141b54..3d5a2d6f1ad 100644 --- a/ctapipe/io/hdf5tableio.py +++ b/ctapipe/io/hdf5tableio.py @@ -27,7 +27,7 @@ "uint16": tables.UInt16Col, "uint32": tables.UInt32Col, "uint64": tables.UInt64Col, - "bool": tables.UInt8Col, + "bool": tables.BoolCol, } diff --git a/ctapipe/io/tests/test_hdf5.py b/ctapipe/io/tests/test_hdf5.py index 61dc2284c08..c70486120f9 100644 --- a/ctapipe/io/tests/test_hdf5.py +++ b/ctapipe/io/tests/test_hdf5.py @@ -72,12 +72,12 @@ def test_prefix(tmp_path): def test_write_containers(temp_h5_file): class C1(Container): - a = Field("a", None) - b = Field("b", None) + a = Field(None, "a") + b = Field(None, "b") class C2(Container): - c = Field("c", None) - d = Field("d", None) + c = Field(None, "c") + d = Field(None, "d") with tempfile.NamedTemporaryFile() as f: with HDF5TableWriter(f.name, "test") as writer: @@ -90,6 +90,26 @@ class C2(Container): writer.write("tel_001", [c1, c2]) +def test_write_bool(): + class C(Container): + boolean = Field(True, 'Boolean value') + + with tempfile.NamedTemporaryFile() as f: + with HDF5TableWriter(f.name, "test") as writer: + for i in range(2): + c = C(boolean=(i % 2 == 0)) + writer.write("c", c) + + c = C() + with HDF5TableReader(f.name) as reader: + c_reader = reader.read('/test/c', c) + for i in range(2): + cur = next(c_reader) + expected = (i % 2) == 0 + assert isinstance(cur.boolean, np.bool_) + assert cur.boolean == expected + + def test_read_container(temp_h5_file): r0tel1 = R0CameraContainer() r0tel2 = R0CameraContainer()