Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods to read run config and shower distribution to TableLoader #2010

Merged
merged 2 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions ctapipe/io/tableloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
TRUE_IMAGES_GROUP = "/simulation/event/telescope/images"
TRUE_PARAMETERS_GROUP = "/simulation/event/telescope/parameters"
TRUE_IMPACT_GROUP = "/simulation/event/telescope/impact"
SIMULATION_CONFIG_TABLE = "/configuration/simulation/run"
SHOWER_DISTRIBUTION_TABLE = "/simulation/service/shower_distribution"

DL2_SUBARRAY_GROUP = "/dl2/event/subarray"
DL2_TELESCOPE_GROUP = "/dl2/event/telescope"
Expand Down Expand Up @@ -299,6 +301,18 @@ def _add_index_if_needed(table):
if "__index__" not in table.colnames:
table["__index__"] = np.arange(len(table))

def read_simulation_configuration(self):
"""
Read the simulation configuration table
"""
return read_table(self.h5file, SIMULATION_CONFIG_TABLE)

def read_shower_distribution(self):
"""
Read the simulated shower distribution histograms
"""
return read_table(self.h5file, SHOWER_DISTRIBUTION_TABLE)

def read_subarray_events(self, start=None, stop=None, keep_order=True):
"""Read subarray-based event information.

Expand Down
28 changes: 24 additions & 4 deletions ctapipe/io/tests/test_table_loader.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import astropy.units as u
import numpy as np
import pytest
import tables
import numpy as np
from astropy.table import Table

from ctapipe.instrument.subarray import SubarrayDescription
from ctapipe.io.astropy_helpers import read_table


Expand Down Expand Up @@ -63,9 +65,6 @@ def test_check_order():
)


from ctapipe.instrument.subarray import SubarrayDescription


@pytest.fixture(params=["by_type", "by_id"])
def test_file(request, dl1_file, dl1_by_type_file):
"""Test dl1 files in both structures"""
Expand Down Expand Up @@ -343,3 +342,24 @@ def test_chunked(dl2_shower_geometry_file):
assert n_events_by_type == len(tel_events)

assert n_read == n_events


def test_read_simulation_config(dl2_merged_file):
from ctapipe.io import TableLoader

with TableLoader(dl2_merged_file) as table_loader:
runs = table_loader.read_simulation_configuration()
assert len(runs) == 2
assert np.all(runs["obs_id"] == [4, 1])
assert u.allclose(runs["energy_range_min"].quantity, [0.004, 0.003] * u.TeV)


def test_read_shower_distributions(dl2_merged_file):
from ctapipe.io import TableLoader

with TableLoader(dl2_merged_file) as table_loader:
histograms = table_loader.read_shower_distribution()
assert len(histograms) == 2
assert np.all(histograms["obs_id"] == [4, 1])
assert np.all(histograms["num_entries"] == [2000, 1000])
assert np.all(histograms["histogram"].sum(axis=(1, 2)) == [2000, 1000])