Skip to content

Commit

Permalink
Simplify logic for no global parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
maxnoe committed Nov 25, 2022
1 parent 0306384 commit dc087c3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
7 changes: 7 additions & 0 deletions ctapipe/core/tests/test_traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,13 @@ def test_telescope_parameter_lookup_by_type(mock_subarray):
assert lookup["LST_LST_LSTCam"] == 100
assert lookup["MST_MST_MSTCam"] == 10

# no global default
lookup = TelescopeParameterLookup([("type", "LST*", 100)])
lookup.attach_subarray(mock_subarray)
assert lookup["LST_LST_LSTCam"] == 100
with pytest.raises(KeyError):
assert lookup["MST_MST_MSTCam"] == 10


def test_telescope_parameter_patterns(mock_subarray):
"""Test validation of TelescopeParameters"""
Expand Down
8 changes: 3 additions & 5 deletions ctapipe/core/traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,12 +393,10 @@ def __init__(self, telescope_parameter_list):
self._value_for_tel_id = None
self._value_for_type = None
self._subarray = None
self._subarray_global_value = None
self._subarray_global_value_set = False
self._subarray_global_value = Undefined
for param in telescope_parameter_list:
if param[1] == "*":
self._subarray_global_value = param[2]
self._subarray_global_value_set = True

def attach_subarray(self, subarray):
"""
Expand Down Expand Up @@ -440,7 +438,7 @@ def __getitem__(self, tel: Optional[Union[int, str]]):
Returns the resolved parameter for the given telescope id
"""
if tel is None:
if self._subarray_global_value_set:
if self._subarray_global_value is not Undefined:
return self._subarray_global_value

raise KeyError("No subarray global value set for TelescopeParameter")
Expand All @@ -464,7 +462,7 @@ def __getitem__(self, tel: Optional[Union[int, str]]):

if isinstance(tel, str):
try:
if self._subarray_global_value_set:
if self._subarray_global_value is not Undefined:
return self._value_for_type.get(tel, self._subarray_global_value)
return self._value_for_type[tel]
except KeyError:
Expand Down

0 comments on commit dc087c3

Please sign in to comment.