From 8c2a235726703ee3f7ed9b9c2c3e37262484b305 Mon Sep 17 00:00:00 2001 From: Amedeo Chiefa <103528316+achiefa@users.noreply.github.com> Date: Wed, 18 Dec 2024 11:53:21 +0100 Subject: [PATCH] Clean code + remove unused code --- .../commondata/CMS_WCHARM_13TEV/filter.py | 5 +- .../CMS_WCHARM_13TEV/filter_utils.py | 92 +++++++------------ 2 files changed, 35 insertions(+), 62 deletions(-) diff --git a/nnpdf_data/nnpdf_data/commondata/CMS_WCHARM_13TEV/filter.py b/nnpdf_data/nnpdf_data/commondata/CMS_WCHARM_13TEV/filter.py index 4e7e98cfbd..e91f0a0fa3 100644 --- a/nnpdf_data/nnpdf_data/commondata/CMS_WCHARM_13TEV/filter.py +++ b/nnpdf_data/nnpdf_data/commondata/CMS_WCHARM_13TEV/filter.py @@ -5,12 +5,9 @@ import logging from filter_utils import Extractor -import numpy as np -import yaml logging.basicConfig(level=logging.INFO, format='[%(levelname)s] %(message)s') - if __name__ == "__main__": CMS_WCHARM = Extractor("./metadata.yaml", "WPWM-TOT-UNNORM", mult_factor=1000) - _, _, _ = CMS_WCHARM.generate_data(variant='default', save_to_yaml=True) + CMS_WCHARM.generate_data() diff --git a/nnpdf_data/nnpdf_data/commondata/CMS_WCHARM_13TEV/filter_utils.py b/nnpdf_data/nnpdf_data/commondata/CMS_WCHARM_13TEV/filter_utils.py index 2bf20feda4..a96cca22e8 100644 --- a/nnpdf_data/nnpdf_data/commondata/CMS_WCHARM_13TEV/filter_utils.py +++ b/nnpdf_data/nnpdf_data/commondata/CMS_WCHARM_13TEV/filter_utils.py @@ -1,8 +1,6 @@ import logging -import os import numpy as np -import pandas as pd from sys_uncertainties import SYS_DEFINITIONS, SYS_UNC_BY_BIN import yaml @@ -10,21 +8,12 @@ yaml.add_representer(float, prettify_float) -SQRTS = 8000 MW2 = 80.385**2 CMSLUMI13 = 2.5 # % -# List of systematic uncertainties that shuold -# be considered uncorrelated -UNCORR_SYS_UNC = ['UnfoldMCstat', 'UnfoldOtherGen', 'UnfoldReweight'] -ART_LABEL = 'art_corr_unc' STAT_LABEL = 'stat_uncorr_unc' TABLE = '' -# From Table 1 of the paper -SYS_UNC_by_bin = [{}] - - class Extractor: """ Extracts kinematics, central data, and uncertainties for a given dataset @@ -61,11 +50,6 @@ def __init__(self, metadata_file, observable, mult_factor=1): self.kin_labels = self.metadata['kinematic_coverage'] self.ndata = self.metadata['ndata'] - # Collect diagonal absoulute uncertainties - # self.diag_unc = self.__collect_diag_unc() - # self.unc_labels = list(self.diag_unc[0].keys()) - # self.unc_labels.pop(0) - def __retrieve_table(self, table_id): """ Implementation of the lazy loading for the tables. If the table @@ -109,8 +93,6 @@ def __extract_kinematics(self, table: dict): ---------- table: dict Dictionary containing the bins in the transverse momentum - tab_number: int - Index to select the range of the second kinematic variable Return ------ @@ -209,7 +191,7 @@ def symmetrized_sys_unc(self): symmetrized_uncs.append(unc_dict) return symmetrized_uncs - def __build_unc_definitions(self, variant='default'): + def __build_unc_definitions(self): unc_definitions = {} # Statistical uncertainty @@ -229,18 +211,19 @@ def __build_unc_definitions(self, variant='default'): # Add systematic uncertainty unc_definitions = unc_definitions | SYS_DEFINITIONS - if variant != 'default': - raise ValueError(f'The variant {variant} is not implemented yet.') - return unc_definitions - def generate_data(self, variant='default', save_to_yaml=False, path='./'): + def generate_data(self): + ''' + Collect central data, kinematics, and uncertainties ans save them + into yaml files. + ''' # Get central data and kinematics central_data, stat_unc, _ = self.generate_data_and_unc(self.mult_factor) kinematics = self.generate_kinematics() # Uncertainty definitions - unc_definitions = self.__build_unc_definitions(variant=variant) + unc_definitions = self.__build_unc_definitions() sys_artificial = [] # Initialize vector of artificial uncertainties @@ -268,37 +251,30 @@ def generate_data(self, variant='default', save_to_yaml=False, path='./'): unc_dict = unc_dict | tmp sys_artificial.append(unc_dict) - - if save_to_yaml: - # Save kinematics into file - logging.info("Dumping kinematics to file...") - kinematics_yaml = {'bins': kinematics} - with open(path + self.metadata['kinematics']['file'], 'w') as kin_out_file: - yaml.dump(kinematics_yaml, kin_out_file, sort_keys=False) - logging.info("Done!") - - # Save central data into file - logging.info("Dumping kinematics to file...") - dat_central_yaml = {'data_central': central_data} - file_name = self.metadata['data_central'] - with open(path + file_name, 'w') as dat_out_file: - yaml.dump(dat_central_yaml, dat_out_file, sort_keys=False) - logging.info("Done!") - - # Save unertainties - logging.info("Dumping kinematics to file...") - uncertainties_yaml = {'definitions': unc_definitions, 'bins': sys_artificial} - file_name = ( - self.metadata['data_uncertainties'][0] - if variant == 'default' - else self.metadata['variants'][variant]['data_uncertainties'][0] - ) - with open(path + file_name, 'w') as dat_out_file: - yaml.dump(uncertainties_yaml, dat_out_file, sort_keys=False) - logging.info("Done!") - return kinematics, central_data, sys_artificial - else: - return kinematics, central_data, sys_artificial - - def get_table(self, table_id): - return self.__retrieve_table(table_id) + + # Local path for yaml files + path = './' + + # Save kinematics into file + logging.info("Dumping kinematics to file...") + kinematics_yaml = {'bins': kinematics} + with open(path + self.metadata['kinematics']['file'], 'w') as kin_out_file: + yaml.dump(kinematics_yaml, kin_out_file, sort_keys=False) + logging.info("Done!") + + # Save central data into file + logging.info("Dumping kinematics to file...") + dat_central_yaml = {'data_central': central_data} + file_name = self.metadata['data_central'] + with open(path + file_name, 'w') as dat_out_file: + yaml.dump(dat_central_yaml, dat_out_file, sort_keys=False) + logging.info("Done!") + + # Save unertainties + logging.info("Dumping kinematics to file...") + uncertainties_yaml = {'definitions': unc_definitions, 'bins': sys_artificial} + file_name = self.metadata['data_uncertainties'][0] + with open(path + file_name, 'w') as dat_out_file: + yaml.dump(uncertainties_yaml, dat_out_file, sort_keys=False) + logging.info("Done!") + return kinematics, central_data, sys_artificial