Ejemplo n.º 1
0
def test_read_multiple_containers():
    hillas_parameter_container = HillasParametersContainer(x=1 * u.m,
                                                           y=1 * u.m,
                                                           length=1 * u.m,
                                                           width=1 * u.m)

    leakage_container = LeakageContainer(
        pixels_width_1=0.1,
        pixels_width_2=0.1,
        intensity_width_1=0.1,
        intensity_width_2=0.1,
    )
    with tempfile.NamedTemporaryFile() as f:
        with HDF5TableWriter(f.name, group_name="dl1",
                             add_prefix=True) as writer:
            writer.write("params",
                         [hillas_parameter_container, leakage_container])

        df = pd.read_hdf(f.name, key="/dl1/params")
        assert "hillas_x" in df.columns
        assert "leakage_pixels_width_1" in df.columns

        # test reading both containers separately
        with HDF5TableReader(f.name) as reader:
            generator = reader.read("/dl1/params",
                                    HillasParametersContainer(),
                                    prefixes=True)
            hillas = next(generator)
        for value, read_value in zip(
                hillas_parameter_container.as_dict().values(),
                hillas.as_dict().values()):
            np.testing.assert_equal(value, read_value)

        with HDF5TableReader(f.name) as reader:
            generator = reader.read("/dl1/params",
                                    LeakageContainer(),
                                    prefixes=True)
            leakage = next(generator)
        for value, read_value in zip(leakage_container.as_dict().values(),
                                     leakage.as_dict().values()):
            np.testing.assert_equal(value, read_value)

        # test reading both containers simultaneously
        with HDF5TableReader(f.name) as reader:
            generator = reader.read(
                "/dl1/params",
                [HillasParametersContainer(),
                 LeakageContainer()],
                prefixes=True,
            )
            hillas_, leakage_ = next(generator)

        for value, read_value in zip(leakage_container.as_dict().values(),
                                     leakage_.as_dict().values()):
            np.testing.assert_equal(value, read_value)

        for value, read_value in zip(
                hillas_parameter_container.as_dict().values(),
                hillas_.as_dict().values()):
            np.testing.assert_equal(value, read_value)
Ejemplo n.º 2
0
def test_read_without_prefixes(tmp_path):
    path = tmp_path / "test.h5"

    hillas_parameter_container = HillasParametersContainer(fov_lon=1 * u.deg,
                                                           fov_lat=1 * u.deg,
                                                           length=1 * u.deg,
                                                           width=1 * u.deg)

    leakage_container = LeakageContainer(
        pixels_width_1=0.1,
        pixels_width_2=0.1,
        intensity_width_1=0.1,
        intensity_width_2=0.1,
    )

    with HDF5TableWriter(path, group_name="dl1", add_prefix=False) as writer:
        writer.write("params", [hillas_parameter_container, leakage_container])

    df = pd.read_hdf(path, key="/dl1/params")
    assert "fov_lon" in df.columns
    assert "pixels_width_1" in df.columns

    # call with prefixes=False
    with HDF5TableReader(path) as reader:
        generator = reader.read(
            "/dl1/params",
            [HillasParametersContainer(),
             LeakageContainer()],
            prefixes=False,
        )
        hillas_, leakage_ = next(generator)

    for value, read_value in zip(leakage_container.as_dict().values(),
                                 leakage_.as_dict().values()):
        np.testing.assert_equal(value, read_value)

    for value, read_value in zip(hillas_parameter_container.as_dict().values(),
                                 hillas_.as_dict().values()):
        np.testing.assert_equal(value, read_value)

    # call with manually removed prefixes
    with HDF5TableReader(path) as reader:
        generator = reader.read(
            "/dl1/params",
            [
                HillasParametersContainer(prefix=""),
                LeakageContainer(prefix="")
            ],
            prefixes=True,
        )
        hillas_, leakage_ = next(generator)

    for value, read_value in zip(leakage_container.as_dict().values(),
                                 leakage_.as_dict().values()):
        np.testing.assert_equal(value, read_value)

    for value, read_value in zip(hillas_parameter_container.as_dict().values(),
                                 hillas_.as_dict().values()):
        np.testing.assert_equal(value, read_value)
Ejemplo n.º 3
0
def test_read_multiple_containers(tmp_path):
    path = tmp_path / "test_append.h5"
    hillas_parameter_container = HillasParametersContainer(fov_lon=1 * u.deg,
                                                           fov_lat=1 * u.deg,
                                                           length=1 * u.deg,
                                                           width=1 * u.deg)

    leakage_container = LeakageContainer(
        pixels_width_1=0.1,
        pixels_width_2=0.1,
        intensity_width_1=0.1,
        intensity_width_2=0.1,
    )
    with HDF5TableWriter(path, group_name="dl1", add_prefix=True) as writer:
        writer.write("params", [hillas_parameter_container, leakage_container])

    df = pd.read_hdf(path, key="/dl1/params")
    assert "hillas_fov_lon" in df.columns
    assert "leakage_pixels_width_1" in df.columns

    # test reading both containers separately
    with HDF5TableReader(path) as reader:
        generator = reader.read("/dl1/params",
                                HillasParametersContainer(),
                                prefixes=True)
        hillas = next(generator)
    for value, read_value in zip(hillas_parameter_container.as_dict().values(),
                                 hillas.as_dict().values()):
        np.testing.assert_equal(value, read_value)

    with HDF5TableReader(path) as reader:
        generator = reader.read("/dl1/params",
                                LeakageContainer(),
                                prefixes=True)
        leakage = next(generator)
    for value, read_value in zip(leakage_container.as_dict().values(),
                                 leakage.as_dict().values()):
        np.testing.assert_equal(value, read_value)

    # test reading both containers simultaneously
    with HDF5TableReader(path) as reader:
        generator = reader.read(
            "/dl1/params",
            [HillasParametersContainer(),
             LeakageContainer()],
            prefixes=True,
        )
        hillas_, leakage_ = next(generator)

    for value, read_value in zip(leakage_container.as_dict().values(),
                                 leakage_.as_dict().values()):
        np.testing.assert_equal(value, read_value)

    for value, read_value in zip(hillas_parameter_container.as_dict().values(),
                                 hillas_.as_dict().values()):
        np.testing.assert_equal(value, read_value)
Ejemplo n.º 4
0
def test_read_without_prefixes():
    hillas_parameter_container = HillasParametersContainer(
        x=1 * u.m, y=1 * u.m, length=1 * u.m, width=1 * u.m
    )

    leakage_container = LeakageContainer(
        pixels_width_1=0.1,
        pixels_width_2=0.1,
        intensity_width_1=0.1,
        intensity_width_2=0.1,
    )
    with tempfile.NamedTemporaryFile() as f:
        with HDF5TableWriter(f.name, group_name="dl1", add_prefix=False) as writer:
            writer.write("params", [hillas_parameter_container, leakage_container])

        df = pd.read_hdf(f.name, key="/dl1/params")
        assert "x" in df.columns
        assert "pixels_width_1" in df.columns

        # call with prefixes=False
        with HDF5TableReader(f.name) as reader:
            generator = reader.read(
                "/dl1/params",
                [HillasParametersContainer(), LeakageContainer()],
                prefixes=False,
            )
            hillas_, leakage_ = next(generator)

        for value, read_value in zip(
            leakage_container.as_dict().values(), leakage_.as_dict().values()
        ):
            np.testing.assert_equal(value, read_value)

        for value, read_value in zip(
            hillas_parameter_container.as_dict().values(), hillas_.as_dict().values()
        ):
            np.testing.assert_equal(value, read_value)

        # call with manually removed prefixes
        with HDF5TableReader(f.name) as reader:
            generator = reader.read(
                "/dl1/params",
                [HillasParametersContainer(prefix=""), LeakageContainer(prefix="")],
                prefixes=True,
            )
            hillas_, leakage_ = next(generator)

        for value, read_value in zip(
            leakage_container.as_dict().values(), leakage_.as_dict().values()
        ):
            np.testing.assert_equal(value, read_value)

        for value, read_value in zip(
            hillas_parameter_container.as_dict().values(), hillas_.as_dict().values()
        ):
            np.testing.assert_equal(value, read_value)
Ejemplo n.º 5
0
def test_prefix(tmp_path):
    tmp_file = tmp_path / "test_prefix.hdf5"
    hillas_parameter_container = HillasParametersContainer(x=1 * u.m,
                                                           y=1 * u.m,
                                                           length=1 * u.m,
                                                           width=1 * u.m)

    leakage_container = LeakageContainer(
        pixels_width_1=0.1,
        pixels_width_2=0.1,
        intensity_width_1=0.1,
        intensity_width_2=0.1,
    )

    with HDF5TableWriter(tmp_file.name, group_name="blabla",
                         add_prefix=True) as writer:
        writer.write("events", [hillas_parameter_container, leakage_container])

    df = pd.read_hdf(tmp_file.name, key="/blabla/events")
    assert "hillas_x" in df.columns
    assert "leakage_pixels_width_1" in df.columns

    with HDF5TableReader(tmp_file.name) as reader:
        generator = reader.read("/blabla/events",
                                HillasParametersContainer(),
                                prefix=True)
        hillas = next(generator)
    for value, read_value in zip(hillas_parameter_container.as_dict().values(),
                                 hillas.as_dict().values()):
        np.testing.assert_equal(value, read_value)

    with HDF5TableReader(tmp_file.name) as reader:
        generator = reader.read("/blabla/events",
                                LeakageContainer(),
                                prefix=True)
        leakage = next(generator)
    for value, read_value in zip(leakage_container.as_dict().values(),
                                 leakage.as_dict().values()):
        np.testing.assert_equal(value, read_value)