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

Load observation info with TableLoader; standardize id fields in containers #2096

Merged
merged 4 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
19 changes: 12 additions & 7 deletions ctapipe/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
UNKNOWN_ID = np.uint64(np.iinfo(np.uint64).max)


obs_id_field = partial(Field, UNKNOWN_ID, description="Observation Block ID")
event_id_field = partial(Field, UNKNOWN_ID, description="Array Event ID")
tel_id_field = partial(Field, UNKNOWN_ID, description="Telescope ID")
LukasBeiske marked this conversation as resolved.
Show resolved Hide resolved


class SchedulingBlockType(enum.Enum):
"""
Types of Scheduling Block
Expand Down Expand Up @@ -161,8 +166,8 @@ class EventIndexContainer(Container):
"""index columns to include in event lists, common to all data levels"""

default_prefix = "" # don't want to prefix these
obs_id = Field(0, "observation identifier")
event_id = Field(0, "event identifier")
obs_id = obs_id_field()
event_id = event_id_field()


class TelEventIndexContainer(Container):
Expand All @@ -172,9 +177,9 @@ class TelEventIndexContainer(Container):
"""

default_prefix = "" # don't want to prefix these
obs_id = Field(0, "observation identifier")
event_id = Field(0, "event identifier")
tel_id = Field(0, "telescope identifier")
obs_id = obs_id_field()
event_id = event_id_field()
tel_id = tel_id_field()


class BaseHillasParametersContainer(Container):
Expand Down Expand Up @@ -1158,7 +1163,7 @@ class SimulatedShowerDistribution(Container):

default_prefix = ""

obs_id = Field(-1, description="links to which events this corresponds to")
obs_id = obs_id_field()
hist_id = Field(-1, description="Histogram ID")
LukasBeiske marked this conversation as resolved.
Show resolved Hide resolved
n_entries = Field(-1, description="Number of entries in the histogram")
bins_energy = Field(
Expand Down Expand Up @@ -1243,7 +1248,7 @@ class ObservationBlockContainer(Container):
"""Stores information about the observation"""

default_prefix = ""
obs_id = Field(UNKNOWN_ID, "Observation Block ID", type=np.uint64)
obs_id = obs_id_field()
sb_id = Field(UNKNOWN_ID, "ID of the parent SchedulingBlock", type=np.uint64)
producer_id = Field(
"unknown",
Expand Down
21 changes: 21 additions & 0 deletions ctapipe/io/tableloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
TRUE_IMPACT_GROUP = "/simulation/event/telescope/impact"
SIMULATION_CONFIG_TABLE = "/configuration/simulation/run"
SHOWER_DISTRIBUTION_TABLE = "/simulation/service/shower_distribution"
OBSERVATION_TABLE = "/configuration/observation/observation_block"

DL2_SUBARRAY_GROUP = "/dl2/event/subarray"
DL2_TELESCOPE_GROUP = "/dl2/event/telescope"
Expand Down Expand Up @@ -162,6 +163,10 @@ class TableLoader(Component):
False, help="join subarray instrument information to each event"
).tag(config=True)

load_observation_info = traits.Bool(
False, help="join observation information to each event"
).tag(config=True)

focal_length_choice = traits.UseEnum(
FocalLengthKind,
default_value=FocalLengthKind.EFFECTIVE,
Expand Down Expand Up @@ -278,6 +283,19 @@ def read_shower_distribution(self):
"""
return read_table(self.h5file, SHOWER_DISTRIBUTION_TABLE)

def read_observation_information(self, start=None, stop=None):
"""
Read the observation information
"""
return read_table(self.h5file, OBSERVATION_TABLE, start=start, stop=stop)

def _join_observation_info(self, table, start=None, stop=None):
observation_table = self.read_observation_information(start=start, stop=stop)
table = join_allow_empty(
table, observation_table, keys="obs_id", join_type="left"
)
return table

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

Expand Down Expand Up @@ -309,6 +327,9 @@ def read_subarray_events(self, start=None, stop=None, keep_order=True):
)
table = _join_subarray_events(table, dl2)

if self.load_observation_info:
table = self._join_observation_info(table, start=start, stop=stop)

if keep_order:
self._sort_to_original_order(table)
return table
Expand Down
9 changes: 9 additions & 0 deletions ctapipe/io/tests/test_table_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ def test_true_parameters(dl1_file):
assert "true_hillas_intensity" in table.colnames


def test_observation_info(dl1_file):
"""Test joining observation info onto telescope events"""
from ctapipe.io.tableloader import TableLoader

with TableLoader(dl1_file, load_observation_info=True) as table_loader:
table = table_loader.read_telescope_events()
assert "subarray_pointing_lat" in table.colnames


def test_read_subarray_events(dl2_shower_geometry_file):
"""Test reading subarray events"""
from ctapipe.io.tableloader import TableLoader
Expand Down