Skip to content

Commit

Permalink
Revert "added rules classes, static KIN_LABEL dict, and replaced cpp …
Browse files Browse the repository at this point in the history
…Export method with new Python one"

This reverts commit 3f30ee0.
  • Loading branch information
FrancescoMerlotti committed Jun 18, 2024
1 parent 2ca27d5 commit 57099f8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 110 deletions.
65 changes: 21 additions & 44 deletions validphys2/src/validphys/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@
from reportengine import report
from reportengine.compat import yaml

from validphys.filters import (
Rule,
FilterRule,
AddedFilterRule,
RuleProcessingError,
default_filter_rules_input,
)

from validphys.core import (
DataGroupSpec,
DataSetInput,
Expand Down Expand Up @@ -194,7 +186,7 @@ def parse_load_weights_from_fit(self, name: str):

@element_of("theoryids")
@_id_with_label
def parse_theoryid(self, theoryID: (str, int)): # type: ignore
def parse_theoryid(self, theoryID: (str, int)):
"""A number corresponding to the database theory ID where the
corresponding theory folder is installed in te data directory."""
try:
Expand All @@ -207,7 +199,7 @@ def parse_theoryid(self, theoryID: (str, int)): # type: ignore
display_alternatives="all",
)

def parse_use_cuts(self, use_cuts: (bool, str)): # type: ignore # type: ignore
def parse_use_cuts(self, use_cuts: (bool, str)):
"""Whether to filter the points based on the cuts applied in the fit,
or the whole data in the dataset. The possible options are:
Expand Down Expand Up @@ -245,7 +237,7 @@ def produce_replicas(self, nreplica: int):
return NSList(range(1, nreplica+1), nskey="replica")

def produce_inclusive_use_scalevar_uncertainties(self, use_scalevar_uncertainties: bool = False,
point_prescription: (str, None) = None): # type: ignore
point_prescription: (str, None) = None):
"""Whether to use a scale variation uncertainty theory covmat.
Checks whether a point prescription is included in the runcard and if so
assumes scale uncertainties are to be used."""
Expand Down Expand Up @@ -1212,7 +1204,7 @@ def res(*args, **kwargs):
return res

def produce_fitthcovmat(
self, use_thcovmat_if_present: bool = False, fit: (str, type(None)) = None # type: ignore
self, use_thcovmat_if_present: bool = False, fit: (str, type(None)) = None
):
"""If a `fit` is specified and `use_thcovmat_if_present` is `True` then returns the
corresponding covariance matrix for the given fit if it exists. If the fit doesn't have a
Expand Down Expand Up @@ -1266,7 +1258,7 @@ def produce_fitthcovmat(
fit_theory_covmat = None
return fit_theory_covmat

def parse_speclabel(self, label: (str, type(None))): # type: ignore
def parse_speclabel(self, label: (str, type(None))):
"""A label for a dataspec. To be used in some plots"""
return label

Expand Down Expand Up @@ -1300,7 +1292,7 @@ def parse_groupby(self, grouping: str):
)
return grouping

def parse_norm_threshold(self, val: (numbers.Number, type(None))): # type: ignore
def parse_norm_threshold(self, val: (numbers.Number, type(None))):
"""The threshold to use for covariance matrix normalisation, sets
the maximum l2 norm of the inverse covariance matrix, by clipping
smallest eigenvalues
Expand All @@ -1323,7 +1315,7 @@ def produce_no_covmat_reg(self):
return {"norm_threshold": None}

@configparser.record_from_defaults
def parse_default_filter_rules(self, spec: (str, type(None))): # type: ignore
def parse_default_filter_rules(self, spec: (str, type(None))):
return spec

def load_default_default_filter_rules(self, spec):
Expand All @@ -1347,7 +1339,7 @@ def load_default_default_filter_rules(self, spec):
display_alternatives="all",
)

def parse_filter_rules(self, filter_rules: (list, type(None))): # type: ignore
def parse_filter_rules(self, filter_rules: (list, type(None))):
"""A list of filter rules. See https://docs.nnpdf.science/vp/filters.html
for details on the syntax"""
log.warning("Overwriting filter rules")
Expand All @@ -1359,13 +1351,6 @@ def parse_default_filter_rules_recorded_spec_(self, spec):
it reportengine detects a conflict in the `dataset` key.
"""
return spec

def parse_added_filter_rules(self, rules: (list, type(None)) = None): # type: ignore
"""
Returns a tuple of AddedFilterRule objects. Rules are immutable after parsing.
AddedFilterRule objects inherit from FilterRule objects.
"""
return tuple(AddedFilterRule(**rule) for rule in rules) if rules else None

def produce_rules(
self,
Expand All @@ -1375,9 +1360,15 @@ def produce_rules(
default_filter_rules=None,
filter_rules=None,
default_filter_rules_recorded_spec_=None,
added_filter_rules=None,
):

"""Produce filter rules based on the user defined input and defaults."""
from validphys.filters import (
Rule,
RuleProcessingError,
default_filter_rules_input,
)

theory_parameters = theoryid.get_description()

if filter_rules is None:
Expand All @@ -1401,25 +1392,11 @@ def produce_rules(
]
except RuleProcessingError as e:
raise ConfigError(f"Error Processing filter rules: {e}") from e

if added_filter_rules:
for i, rule in enumerate(added_filter_rules):
try:
rule_list.append(
Rule(
initial_data=rule,
defaults=defaults,
theory_parameters=theory_parameters,
loader=self.loader,
)
)
except RuleProcessingError as e:
raise ConfigError(f"Error processing added rule {i+1}: {e}") from e

return tuple(rule_list)

return rule_list

@configparser.record_from_defaults
def parse_default_filter_settings(self, spec: (str, type(None))): # type: ignore
def parse_default_filter_settings(self, spec: (str, type(None))):
return spec

def load_default_default_filter_settings(self, spec):
Expand All @@ -1443,7 +1420,7 @@ def load_default_default_filter_settings(self, spec):
display_alternatives="all",
)

def parse_filter_defaults(self, filter_defaults: (dict, type(None))): # type: ignore
def parse_filter_defaults(self, filter_defaults: (dict, type(None))):
"""A mapping containing the default kinematic limits to be used when
filtering data (when using internal cuts).
Currently these limits are ``q2min`` and ``w2min``.
Expand Down Expand Up @@ -1523,8 +1500,8 @@ def produce_data(

def _parse_data_input_from_(
self,
parse_from_value: (str, type(None)), # type: ignore
additional_context: (dict, type(None)) = None, # type: ignore
parse_from_value: (str, type(None)),
additional_context: (dict, type(None)) = None,
):
"""Function which parses the ``data_input`` from a namespace. Usage
is similar to :py:meth:`self.parse_from_` except this function bridges
Expand Down
71 changes: 5 additions & 66 deletions validphys2/src/validphys/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
"""

import logging
import dataclasses
import re
from collections.abc import Mapping
from importlib.resources import read_text
from typing import Union

import numpy as np

Expand All @@ -18,32 +16,6 @@

log = logging.getLogger(__name__)

KIN_LABEL = {
"DIS": ("x", "Q2", "y"),
"DYP": ("y", "M2", "sqrts"),
"JET": ("eta", "p_T2", "sqrts"),
"DIJET": ("eta", "m_12", "sqrts"),
"PHT": ("eta_gamma", "E_Tgamma2", "sqrts"),
"INC": ("0", "mu2", "sqrts"),
"EWK_RAP": ("etay", "M2", "sqrts"),
"EWK_PT": ("p_T", "M2", "sqrts"),
"EWK_PTRAP": ("etay", "p_T2", "sqrts"),
"EWK_MLL": ("M_ll", "M_ll2", "sqrts"),
"EWJ_RAP": ("etay", "M2", "sqrts"),
"EWJ_PT": ("p_T", "M2", "sqrt(s)"),
"EWJ_PTRAP": ("etay", "p_T2", "sqrts"),
"EWJ_JRAP": ("etay", "M2", "sqrts"),
"EWJ_JPT": ("p_T", "M2", "sqrts"),
"EWJ_MLL": ("M_ll", "M_ll2", "sqrts"),
"HQP_YQQ": ("yQQ", "mu2", "sqrts"),
"HQP_MQQ": ("MQQ", "mu2", "sqrts"),
"HQP_PTQQ": ("p_TQQ", "mu2", "sqrts"),
"HQP_YQ": ("yQ", "mu2", "sqrts"),
"HQP_PTQ": ("p_TQ", "mu2", "sqrts"),
"HIG_RAP": ("y", "M_H2", "sqrts"),
"SIA": ("z", "Q2", "y"),
}

class RuleProcessingError(Exception):
"""Exception raised when we couldn't process a rule."""

Expand All @@ -59,34 +31,6 @@ class MissingRuleAttribute(RuleProcessingError, AttributeError):
class FatalRuleError(Exception):
"""Exception raised when a rule application failed at runtime."""

@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):
"""
Dataclass which carries extra filter rule that is added to the
default rule.
"""

pass

def default_filter_settings_input():
"""Return a dictionary with the default hardcoded filter settings.
Expand Down Expand Up @@ -220,9 +164,7 @@ def _filter_real_data(filter_path, data):
nfull, ncut = _write_ds_cut_data(path, dataset)
total_data_points += nfull
total_cut_data_points += ncut
# dataset.load().Export(str(path))
cuts = dataset.cuts.load()
dataset.commondata.load_commondata(cuts=cuts).export(path)
dataset.load().Export(str(path))
return total_data_points, total_cut_data_points


Expand Down Expand Up @@ -378,9 +320,6 @@ def __init__(
self.dataset = None
self.process_type = None
self._local_variables_code = {}

if isinstance(initial_data, FilterRule):
initial_data = initial_data.to_dict()
for key in initial_data:
setattr(self, key, initial_data[key])

Expand All @@ -404,14 +343,14 @@ def __init__(
f"Could not find dataset {self.dataset}"
) from e
if cd.process_type[:3] == "DIS":
self.variables = KIN_LABEL["DIS"]
self.variables = CommonData.kinLabel["DIS"]
else:
self.variables = KIN_LABEL[cd.process_type]
self.variables = CommonData.kinLabel[cd.process_type]
else:
if self.process_type[:3] == "DIS":
self.variables = KIN_LABEL["DIS"]
self.variables = CommonData.kinLabel["DIS"]
else:
self.variables = KIN_LABEL[self.process_type]
self.variables = CommonData.kinLabel[self.process_type]

if hasattr(self, "local_variables"):
if not isinstance(self.local_variables, Mapping):
Expand Down

0 comments on commit 57099f8

Please sign in to comment.