-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add event type filter to ctapipe-process (#1841)
- Loading branch information
Showing
4 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from ..core import Component | ||
from ..core.traits import Enum, Set | ||
from ..containers import EventType | ||
|
||
|
||
__all__ = ["EventTypeFilter"] | ||
|
||
|
||
class EventTypeFilter(Component): | ||
"""Check that an event has one of the allowed types""" | ||
|
||
allowed_types = Set( | ||
# add both the enum instance and the integer values to support | ||
# giving the integers in config files. | ||
trait=Enum(list(EventType) + [t.value for t in EventType]), | ||
default_value=None, | ||
allow_none=True, | ||
help="The allowed types. Set to None to allow all types.", | ||
).tag(config=True) | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
# convert ints to enum type | ||
if self.allowed_types is not None: | ||
self.allowed_types = {EventType(e) for e in self.allowed_types} | ||
|
||
def __call__(self, event): | ||
"""Returns True if the event should be kept""" | ||
if self.allowed_types is None: | ||
return True | ||
|
||
return event.trigger.event_type in self.allowed_types |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from traitlets.config import Config | ||
from ctapipe.containers import EventType, ArrayEventContainer | ||
|
||
|
||
def test_event_filter(): | ||
from ctapipe.utils import EventTypeFilter | ||
|
||
event_filter = EventTypeFilter( | ||
allowed_types={EventType.SUBARRAY, EventType.FLATFIELD} | ||
) | ||
|
||
e = ArrayEventContainer() | ||
e.trigger.event_type = EventType.SUBARRAY | ||
assert event_filter(e) | ||
e.trigger.event_type = EventType.FLATFIELD | ||
assert event_filter(e) | ||
e.trigger.event_type = EventType.DARK_PEDESTAL | ||
assert not event_filter(e) | ||
|
||
|
||
def test_event_filter_config(): | ||
from ctapipe.utils import EventTypeFilter | ||
|
||
config = Config({"EventTypeFilter": {"allowed_types": [EventType.SUBARRAY.value]}}) | ||
event_filter = EventTypeFilter(config=config) | ||
|
||
e = ArrayEventContainer() | ||
e.trigger.event_type = EventType.DARK_PEDESTAL | ||
assert not event_filter(e) | ||
|
||
e.trigger.event_type = EventType.SUBARRAY | ||
assert event_filter(e) |