Skip to content

Commit

Permalink
Move messages of used parameter in SigmaMap.build (#117)
Browse files Browse the repository at this point in the history
* Move messages of used parameter in SigmaMap.build

* refine the docstr

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Formatting

---------

Co-authored-by: Zihao Xu <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 13, 2023
1 parent 9ce880e commit 493fa59
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
46 changes: 36 additions & 10 deletions appletree/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def get_default(self):
if self.default is not OMITTED:
return self.default

raise ValueError(f"Missing option {self.name} " f"required by {self.taken_by}")
raise ValueError(f"Missing option {self.name} required by {self.taken_by}")

def build(self, llh_name: Optional[str] = None):
"""Build configuration, set attributes to Config instance."""
Expand Down Expand Up @@ -280,10 +280,17 @@ def pdf_to_cdf(self, x, pdf):
class SigmaMap(Config):
"""Maps with uncertainty.
Default value is a list whose order is:
[median, lower, upper, (parameter)]
Each map is assigned as attribute of SigmaMap.
If the last element in the list is the required parameter.
The value of a SigmaMap can be:
* a list with four elements,
which are the file names of median, lower, upper maps and the name of the scaler.
* a list with three elements,
which are the file names of median, lower and upper maps. The name of the scaler
is the default one f"{self.name}_sigma".
* a string,
which is the file name of the map for median, lower, upper.
In the first and second case, the name of the scaler will appear
in Component.needed_parameters.
"""

Expand All @@ -294,12 +301,24 @@ def build(self, llh_name: Optional[str] = None):

_configs_default = self.get_default()

if isinstance(_configs, list) and len(_configs) > 4:
raise ValueError(f"You give too much information in {self.name}'s configs.")

if isinstance(_configs_default, list) and len(_configs_default) > 4:
raise ValueError(f"You give too much information in {self.name}'s default configs.")

maps = dict()
sigmas = ["median", "lower", "upper"]
for i, sigma in enumerate(sigmas):
# propagate _configs_default to Map instances
if isinstance(_configs_default, list):
maps[sigma] = Map(name=self.name + f"_{sigma}", default=_configs_default[i])
else:
if not isinstance(_configs_default, str):
raise ValueError(
f"If {self.name}'s default configuration is not a list, "
"then it should be a string."
)
# If only one file is given, then use the same file for all sigmas
maps[sigma] = Map(name=self.name + f"_{sigma}", default=_configs_default)

Expand All @@ -312,6 +331,11 @@ def build(self, llh_name: Optional[str] = None):
if isinstance(_configs, list):
_cached_configs[maps[sigma].name].update({self.llh_name: _configs[i]})
else:
if not isinstance(_configs, str):
raise ValueError(
f"If {self.name}'s configuration is not a list, "
"then it should be a string."
)
# If only one file is given, then use the same file for all sigmas
_cached_configs[maps[sigma].name].update({self.llh_name: _configs})

Expand All @@ -321,8 +345,13 @@ def build(self, llh_name: Optional[str] = None):
self.lower.build(llh_name=self.llh_name) # type: ignore
self.upper.build(llh_name=self.llh_name) # type: ignore

if isinstance(_configs, list) and len(_configs) > 4:
raise ValueError(f"You give too much information in {self.name} configs.")
if self.required_parameter is not None:
print(
f"{self.llh_name}'s map {self.name} is using "
f"the parameter {self.required_parameter}."
)
else:
print(f"{self.llh_name}'s map {self.name} is static and not using any parameter.")

def get_configs(self):
if self.name in _cached_configs:
Expand Down Expand Up @@ -351,9 +380,6 @@ def required_parameter(self):
# Find required parameter
if isinstance(_configs, list):
if len(_configs) == 4:
print(
f"{self.llh_name} is using the parameter " f"{_configs[-1]} in {self.name} map."
)
return _configs[-1]
else:
return self.name + "_sigma"
Expand Down
4 changes: 2 additions & 2 deletions appletree/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ def __init__(self, llh_name: Optional[str] = None):
# llh_name will tell us which map to use
self.llh_name = llh_name
if not self.depends_on:
raise ValueError("depends_on not provided for " f"{self.__class__.__name__}")
raise ValueError(f"depends_on not provided for {self.__class__.__name__}")

if not self.provides:
raise ValueError("provides not provided for " f"{self.__class__.__name__}")
raise ValueError(f"provides not provided for {self.__class__.__name__}")

# configs are loaded when a plugin is initialized
for config in self.takes_config.values():
Expand Down
4 changes: 2 additions & 2 deletions appletree/plugins/efficiency.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def simulate(self, key, parameters, s2_area):
@takes_config(
SigmaMap(
name="s1_eff_3f",
default=["_3fold_recon_eff.json", "_3fold_recon_eff.json", "_3fold_recon_eff.json"],
default="_3fold_recon_eff.json",
help="3fold S1 reconstruction efficiency",
),
)
Expand Down Expand Up @@ -64,7 +64,7 @@ def simulate(self, key, parameters, s1_area):
@takes_config(
SigmaMap(
name="s2_cut_acc",
default=["_s2_cut_acc.json", "_s2_cut_acc.json", "_s2_cut_acc.json"],
default=["_s2_cut_acc.json", "_s2_cut_acc.json", "_s2_cut_acc.json", "s2_cut_acc_sigma"],
help="S2 cut acceptance",
),
)
Expand Down

0 comments on commit 493fa59

Please sign in to comment.