-
Notifications
You must be signed in to change notification settings - Fork 14
Hashable Rules #2076
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
Merged
Merged
Hashable Rules #2076
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
fb3bd05
rules are tuple instead of list -> makes them hashable
comane df6a42b
return empty tuple instead of None -> None is not iterable
comane 82cd476
added FilterDefaults frozen dataclass
comane 90f04da
filter defaults returns FilterDefaults dataclass instead of unhashabl…
comane a9b7e1c
make inputs to produce_rules hashable types. no need for freeze args
comane a600a3f
import validphys.filters at top of module
comane a256b45
parse_filter_rules returns tuple of frozendicts
comane ad75add
check_default_filter_rules returns tuple
comane 9d08c61
applied review changes
comane 0c085e0
Introduced FilterRule and AddedFilterRule objects
comane 2d022aa
removed frozendict import in config
comane fd9024f
applied comments of review
comane 4bf928d
Update validphys2/src/validphys/config.py
comane d9e3c18
Update validphys2/src/validphys/config.py
comane d542c7d
Rule accepts both FilterRule objects as well as Mappings
comane 7da9282
Update validphys2/src/validphys/filters.py
comane 610a142
to_dict method of DefaultFilters also takes default None values
comane 214a75a
restored previous behaviour of produce_defaults function
comane bbfe2c9
removed freeze args from get_cuts_for_dataset as args are now hashable
comane 8858d30
removed freeze_args and make_hashable functions as they are not used …
comane feae109
removed frozendict dependency from pyproject and conda-recipe/meta.ya…
comane 3d7f319
pass rules as tuple in test
comane c2b5659
Update validphys2/src/validphys/config.py
comane fac5218
Update validphys2/src/validphys/config.py
comane 3fd9ec5
Merge branch 'master' into hashable_rules
comane File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -7,14 +7,16 @@ | |||||
| from importlib.resources import read_text | ||||||
| import logging | ||||||
| import re | ||||||
| import dataclasses | ||||||
| from typing import Union | ||||||
|
|
||||||
| import numpy as np | ||||||
|
|
||||||
| from reportengine.checks import check, make_check | ||||||
| from reportengine.compat import yaml | ||||||
| import validphys.cuts | ||||||
| from validphys.process_options import PROCESSES | ||||||
| from validphys.utils import freeze_args, generate_path_filtered_data | ||||||
| from validphys.utils import generate_path_filtered_data | ||||||
|
|
||||||
| log = logging.getLogger(__name__) | ||||||
|
|
||||||
|
|
@@ -99,18 +101,63 @@ class FatalRuleError(Exception): | |||||
| """Exception raised when a rule application failed at runtime.""" | ||||||
|
|
||||||
|
|
||||||
| @dataclasses.dataclass(frozen=True) | ||||||
| class FilterDefaults: | ||||||
| """ | ||||||
| Dataclass carrying default values for filters (cuts) taking into | ||||||
| account the values of ``q2min``, ``w2min`` and ``maxTau``. | ||||||
| """ | ||||||
| q2min: float = None | ||||||
| w2min: float = None | ||||||
| maxTau: float = None | ||||||
|
|
||||||
| def to_dict(self): | ||||||
| return dataclasses.asdict(self) | ||||||
|
|
||||||
|
|
||||||
| @dataclasses.dataclass(frozen=True) | ||||||
| class FilterRule: | ||||||
| """ | ||||||
| Dataclass which carries the filter rule information. | ||||||
| """ | ||||||
| dataset: str = None | ||||||
| process_type: str = None | ||||||
| rule: str = None | ||||||
| reason: str = None | ||||||
| local_variables: Mapping[str, Union[str, float]] = None | ||||||
| PTO: str = None | ||||||
| FNS: str = None | ||||||
| IC: str = None | ||||||
|
|
||||||
| def to_dict(self): | ||||||
| rule_dict = dataclasses.asdict(self) | ||||||
| filtered_dict = {k: v for k, v in rule_dict.items() if v is not None} | ||||||
| return filtered_dict | ||||||
|
|
||||||
|
|
||||||
| @dataclasses.dataclass(frozen=True) | ||||||
| class AddedFilterRule(FilterRule): | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the purpose of this class? |
||||||
| """ | ||||||
| Dataclass which carries extra filter rule that is added to the | ||||||
| default rule. | ||||||
| """ | ||||||
| pass | ||||||
|
scarlehoff marked this conversation as resolved.
|
||||||
|
|
||||||
|
|
||||||
| def default_filter_settings_input(): | ||||||
| """Return a dictionary with the default hardcoded filter settings. | ||||||
| """Return a FilterDefaults dataclass with the default hardcoded filter settings. | ||||||
| These are defined in ``defaults.yaml`` in the ``validphys.cuts`` module. | ||||||
| """ | ||||||
| return yaml.safe_load(read_text(validphys.cuts, "defaults.yaml")) | ||||||
| return FilterDefaults(**yaml.safe_load(read_text(validphys.cuts, "defaults.yaml"))) | ||||||
|
|
||||||
|
|
||||||
| def default_filter_rules_input(): | ||||||
| """Return a dictionary with the input settings. | ||||||
| """ | ||||||
| Return a tuple of FilterRule objects. | ||||||
| These are defined in ``filters.yaml`` in the ``validphys.cuts`` module. | ||||||
| """ | ||||||
| return yaml.safe_load(read_text(validphys.cuts, "filters.yaml")) | ||||||
| list_rules = yaml.safe_load(read_text(validphys.cuts, "filters.yaml")) | ||||||
| return tuple(FilterRule(**rule) for rule in list_rules) | ||||||
|
|
||||||
|
|
||||||
| def check_nonnegative(var: str): | ||||||
|
|
@@ -457,10 +504,19 @@ class Rule: | |||||
|
|
||||||
| numpy_functions = {"sqrt": np.sqrt, "log": np.log, "fabs": np.fabs} | ||||||
|
|
||||||
| def __init__(self, initial_data: dict, *, defaults: dict, theory_parameters: dict, loader=None): | ||||||
| def __init__(self, initial_data: FilterRule, *, defaults: dict, theory_parameters: dict, loader=None): | ||||||
| self.dataset = None | ||||||
| self.process_type = None | ||||||
| self._local_variables_code = {} | ||||||
|
|
||||||
| # For compatibility with legacy code that passed a dictionary | ||||||
| if isinstance(initial_data, FilterRule): | ||||||
| initial_data = initial_data.to_dict() | ||||||
| elif isinstance(initial_data, Mapping): | ||||||
| initial_data = dict(initial_data) | ||||||
| else: | ||||||
| raise RuleProcessingError("Expecting initial_data to be an instance of a FilterRule dataclass.") | ||||||
|
|
||||||
| for key in initial_data: | ||||||
| setattr(self, key, initial_data[key]) | ||||||
|
|
||||||
|
|
@@ -511,7 +567,7 @@ def __init__(self, initial_data: dict, *, defaults: dict, theory_parameters: dic | |||||
| self.rule_string = self.rule | ||||||
| self.defaults = defaults | ||||||
| self.theory_params = theory_parameters | ||||||
| ns = {*self.numpy_functions, *self.defaults, *self.variables, "idat", "central_value"} | ||||||
| ns = {*self.numpy_functions, *self.defaults.to_dict().keys(), *self.variables, "idat", "central_value"} | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Not that it makes a difference though. |
||||||
| for k, v in self.local_variables.items(): | ||||||
| try: | ||||||
| self._local_variables_code[k] = lcode = compile( | ||||||
|
|
@@ -592,7 +648,7 @@ def __call__(self, dataset, idat): | |||||
| return eval( | ||||||
| self.rule, | ||||||
| self.numpy_functions, | ||||||
| {**{"idat": idat, "central_value": central_value}, **self.defaults, **ns}, | ||||||
| {**{"idat": idat, "central_value": central_value}, **self.defaults.to_dict(), **ns}, | ||||||
| ) | ||||||
| except Exception as e: # pragma: no cover | ||||||
| raise FatalRuleError(f"Error when applying rule {self.rule_string!r}: {e}") from e | ||||||
|
|
@@ -628,7 +684,6 @@ def _make_point_namespace(self, dataset, idat) -> dict: | |||||
| return ns | ||||||
|
|
||||||
|
|
||||||
| @freeze_args | ||||||
| @functools.lru_cache | ||||||
| def get_cuts_for_dataset(commondata, rules) -> list: | ||||||
| """Function to generate a list containing the index | ||||||
|
|
||||||
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.