Skip to content

Commit

Permalink
Clean code + remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
achiefa authored Dec 18, 2024
1 parent 342e1f5 commit 8c2a235
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 62 deletions.
5 changes: 1 addition & 4 deletions nnpdf_data/nnpdf_data/commondata/CMS_WCHARM_13TEV/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
92 changes: 34 additions & 58 deletions nnpdf_data/nnpdf_data/commondata/CMS_WCHARM_13TEV/filter_utils.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
import logging
import os

import numpy as np
import pandas as pd
from sys_uncertainties import SYS_DEFINITIONS, SYS_UNC_BY_BIN
import yaml

from nnpdf_data.filter_utils.utils import prettify_float, symmetrize_errors

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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
------
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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

0 comments on commit 8c2a235

Please sign in to comment.