From 55902b77413ca72d5869228888ef3bd544073824 Mon Sep 17 00:00:00 2001 From: Roy Stegeman Date: Thu, 31 Oct 2024 19:48:06 +0000 Subject: [PATCH] loop over point_prescriptions --- .../examples/Fit_with_theory_covmat.yml | 2 +- validphys2/src/validphys/config.py | 90 ++++++++++--------- .../theorycovariance/construction.py | 5 +- .../theorycovariance/theorycovarianceutils.py | 1 - 4 files changed, 54 insertions(+), 44 deletions(-) diff --git a/n3fit/runcards/examples/Fit_with_theory_covmat.yml b/n3fit/runcards/examples/Fit_with_theory_covmat.yml index f7fa8ebc85..da4f8b9739 100644 --- a/n3fit/runcards/examples/Fit_with_theory_covmat.yml +++ b/n3fit/runcards/examples/Fit_with_theory_covmat.yml @@ -27,7 +27,7 @@ datacuts: theory: theoryid: 708 # database id theorycovmatconfig: - point_prescription: "9 point" + point_prescriptions: ["9 point", "3 point"] # theoryids: # from_: scale_variation_theories pdf: NNPDF31_nlo_as_0118 diff --git a/validphys2/src/validphys/config.py b/validphys2/src/validphys/config.py index eeb4c6166a..93696ab742 100644 --- a/validphys2/src/validphys/config.py +++ b/validphys2/src/validphys/config.py @@ -210,13 +210,16 @@ def produce_replicas(self, nreplica: int): """Produce a replicas array""" return NSList(range(1, nreplica + 1), nskey="replica") + def parse_point_prescriptions(self, point_prescriptions): + return NSList(point_prescriptions, nskey="point_prescription") + def produce_inclusive_use_scalevar_uncertainties( - self, use_scalevar_uncertainties: bool = False, point_prescription: (str, None) = None + self, use_scalevar_uncertainties: bool = False, point_prescriptions: (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.""" - if (not use_scalevar_uncertainties) and (point_prescription is not None): + if (not use_scalevar_uncertainties) and (point_prescriptions is not None): use_scalevar_uncertainties = True return use_scalevar_uncertainties @@ -1142,9 +1145,9 @@ def produce_nnfit_theory_covmat( f = total_theory_covmat_fitting else: # Only scalevar uncertainties - from validphys.theorycovariance.construction import theory_covmat_custom_fitting + from validphys.theorycovariance.construction import theory_covmats_fitting - f = theory_covmat_custom_fitting + f = theory_covmats_fitting elif use_user_uncertainties: # Only user uncertainties from validphys.theorycovariance.construction import user_covmat_fitting @@ -1628,12 +1631,11 @@ def produce_group_dataset_inputs_by_experiment(self, data_input): def produce_group_dataset_inputs_by_process(self, data_input): return self.produce_group_dataset_inputs_by_metadata(data_input, "nnpdf31_process") - def produce_theoryids(self, theoryid, point_prescription): + def produce_point_prescriptions_theoryids(self, theoryid, point_prescriptions): """Produces a list of theoryids given a theoryid at central scales and a point prescription. The options for the latter are '3 point', '5 point', '5bar point', '7 point' and '9 point'. Note that these are defined in arXiv:1906.10698. This hard codes the theories needed for each prescription to avoid user error.""" - pp = point_prescription th = theoryid.id lsv = yaml.safe_load(read_text(validphys.scalevariations, "scalevariationtheoryids.yaml")) @@ -1654,44 +1656,50 @@ def produce_theoryids(self, theoryid, point_prescription): read_text(validphys.scalevariations, "pointprescriptions.yaml") ) - try: - scales = pp_scales_dict[pp] - except KeyError: - valid_pps = ", ".join(pp_scales_dict.keys()) - raise ConfigError( - "Scale variations are not currently defined for this point prescription. This " - + "configuration only works when 'point_prescription' is equal to one of the " - + f"following: {valid_pps}. Please use one of these instead if you wish to " - + "include theory uncertainties here." - ) + pp_thids = {} + for pp in point_prescriptions: + try: + scales = pp_scales_dict[pp] + except KeyError: + valid_pps = ", ".join(pp_scales_dict.keys()) + raise ConfigError( + "Scale variations are not currently defined for this point prescription. This " + + "configuration only works when 'point_prescription' is equal to one of the " + + f"following: {valid_pps}. Please use one of these instead if you wish to " + + "include theory uncertainties here." + ) - # Get dictionary containing theoryid and variations for central theory from runcard - for scalevarsfor_dict in scalevarsfor_list: - if scalevarsfor_dict["theoryid"] == int(th): - theoryid_variations = scalevarsfor_dict + # Get dictionary containing theoryid and variations for central theory from runcard + for scalevarsfor_dict in scalevarsfor_list: + if scalevarsfor_dict["theoryid"] == int(th): + theoryid_variations = scalevarsfor_dict - # Find theoryids for given point prescription for given central theoryid - try: - thids = [theoryid_variations["variations"][scale] for scale in scales] - except KeyError: - available_scales = list(theoryid_variations["variations"]) - missing_scales = [] - for scale in scales: - if scale not in available_scales: - missing_scales.append(scale) - missing_scales_string = ", ".join(missing_scales) - raise ConfigError( - "For this central theoryid, the requested point prescription is not currently " - + "available. To use this point prescription for this central theoryid, theoryids " - + "that correspond to the following scale choices must be created and added to " - + "validphys2/src/validphys/scalevariations/scalevariationtheoryids.yaml: " - + f"(k_F, k_R) = {missing_scales_string}." - ) + # Find theoryids for given point prescription for given central theoryid + try: + thids = [theoryid_variations["variations"][scale] for scale in scales] + except KeyError: + available_scales = list(theoryid_variations["variations"]) + missing_scales = [] + for scale in scales: + if scale not in available_scales: + missing_scales.append(scale) + missing_scales_string = ", ".join(missing_scales) + raise ConfigError( + "For this central theoryid, the requested point prescription is not currently " + + "available. To use this point prescription for this central theoryid, theoryids " + + "that correspond to the following scale choices must be created and added to " + + "validphys2/src/validphys/scalevariations/scalevariationtheoryids.yaml: " + + f"(k_F, k_R) = {missing_scales_string}." + ) + + # Check each theory is loaded + theoryids = [self.loader.check_theoryID(thid) for thid in thids] + pp_thids[pp] = NSList(theoryids, nskey="theoryid") + + return pp_thids - # Check each theory is loaded - theoryids = [self.loader.check_theoryID(thid) for thid in thids] - # NSList needs to be used for theoryids to be recognised as a namespace - return NSList(theoryids, nskey="theoryid") + def produce_theoryids(self, point_prescriptions_theoryids, point_prescription): + return point_prescriptions_theoryids[point_prescription] @configparser.explicit_node def produce_filter_data(self, fakedata: bool = False, theorycovmatconfig=None): diff --git a/validphys2/src/validphys/theorycovariance/construction.py b/validphys2/src/validphys/theorycovariance/construction.py index ea8aab30ef..f1202aab7f 100644 --- a/validphys2/src/validphys/theorycovariance/construction.py +++ b/validphys2/src/validphys/theorycovariance/construction.py @@ -53,7 +53,7 @@ def theory_covmat_dataset( ProcessInfo = namedtuple("ProcessInfo", ("preds", "namelist", "sizes")) -def combine_by_type(each_dataset_results_central_bytheory): +def combine_by_type(each_dataset_results_central_bytheory, theoryids): """Groups the datasets bu process and returns an instance of the ProcessInfo class Parameters @@ -523,6 +523,9 @@ def theory_covmat_custom_fitting(theory_covmat_custom, procs_index_matched): return df +theory_covmats_fitting = collect(theory_covmat_custom_fitting, ("point_prescriptions",)) + + def total_theory_covmat_fitting(total_theory_covmat, procs_index_matched): """total_theory_covmat but reindexed so the order of the datasets matches those in the experiment covmat so they are aligned when fitting.""" diff --git a/validphys2/src/validphys/theorycovariance/theorycovarianceutils.py b/validphys2/src/validphys/theorycovariance/theorycovarianceutils.py index 11ec428f9a..f6c22f001e 100644 --- a/validphys2/src/validphys/theorycovariance/theorycovarianceutils.py +++ b/validphys2/src/validphys/theorycovariance/theorycovarianceutils.py @@ -23,7 +23,6 @@ def check_correct_theory_combination_internal( l in {3, 5, 7, 9, 62, 64, 66, 70, 19, 23}, f"Expecting exactly 3, 5, 7, 9, 62, 64, 66, 23, 19 or 70 theories, but got {l}.", ) - opts = {"bar", "nobar"} xifs = [theoryid.get_description()["XIF"] for theoryid in theoryids] xirs = [theoryid.get_description()["XIR"] for theoryid in theoryids] if l == 3: