示例#1
0
def make_input_plot(input_file):
    confhandler = ConfigFileHandler()
    confhandler.load_configuration(input_file)
    models = confhandler.get_sections()

    df = pd.DataFrame()

    for model in models:
        cur_sect = confhandler.get_section(model)

        used_nonperiodic_vars = filter(
            None,
            ConfigFileUtils.parse_list(cur_sect["nonperiodic_columns"],
                                       lambda x: x))
        used_periodic_vars = filter(
            None,
            ConfigFileUtils.parse_list(cur_sect["periodic_columns"],
                                       lambda x: x))

        used_vars = used_nonperiodic_vars + used_periodic_vars
        var_dict = {col: [1.0] for col in used_vars}
        var_dict["model"] = model

        row_df = pd.DataFrame.from_dict(var_dict)

        df = pd.concat([df, row_df], axis=0)

    df = df.fillna(0.0)

    datacols = [col for col in df.columns if col is not "model"]
    plot_data = df[datacols].as_matrix()

    y_label = [convert_variable_name(name) for name in np.array(datacols)]
    x_label = [convert_model_label(label) for label in df["model"].as_matrix()]

    fig = plt.figure(figsize=(12, 10))

    ax = fig.add_subplot(111)

    cax = ax.matshow(plot_data.transpose(), cmap='Blues', vmin=0, vmax=1)
    ax.set_xticklabels(np.concatenate([[''], x_label]),
                       rotation='vertical',
                       fontsize=11)
    ax.set_yticklabels(np.concatenate([[''], y_label]), fontsize=10)
    ax.xaxis.set_label_position("top")
    ax.xaxis.set_major_locator(ticker.MultipleLocator(1))
    ax.yaxis.set_major_locator(ticker.MultipleLocator(1))

    plt.tight_layout()

    return fig
示例#2
0
def load_file(path, keys):
    confhandler = ConfigFileHandler()
    confhandler.load_configuration(path)
    
    retval = {}
    
    for section_name in confhandler.get_sections():
        cur_section = confhandler.get_section(section_name)
        
        for key in keys:
            if not key in retval:
                retval[key] = []
                
            retval[key].append(float(cur_section[key]))
            
    return retval
示例#3
0
def run_bayesian_optimization(name, eval_file, target, var_ranges, init_points, max_iterations, patience, alpha):
    global evalcnt
    evalcnt = 0
    
    print "now optimizing the following variables: " + str(var_ranges)
    print "alpha = " + str(alpha)

    # change the kernel to have a length scale more appropriate to this function
    # alpha ... corresponds to the value added to the diagonal elements of the covariance matrix <-> the approximate noise level in the observations
    gp_params = {'kernel': ConstantKernel(1.0, (1e-8, 1e2)) * Matern(length_scale = 0.01, length_scale_bounds = (1e-5, 1e5), nu = 1.5),
                 'alpha': alpha}

    bo = BayesianOptimization(target, var_ranges)
    
    # check if a file with previous evaluations of this utility function already exists, if so, use it for initialization
    evaluations_path = os.path.join(out_dir, eval_file)
    
    if os.path.exists(evaluations_path):
        confhandler = ConfigFileHandler()
        confhandler.load_configuration(evaluations_path)
        
        init_dict = {}
        
        for section_name in confhandler.get_sections():
            cur_section = confhandler.get_section(section_name)
            
            for key, value in cur_section.iteritems():
                # only take those variables that are actually relevant
                if key in var_ranges or key == "target":
                    if key not in init_dict:
                        init_dict[key] = []
                    
                    init_dict[key].append(float(value))
                
        evalcnt = int(re.sub('evaluation_', '', confhandler.get_sections()[-1])) + 1
        print "resuming " + name + " at evaluation " + str(evalcnt)
        
        init_points_loaded = len(init_dict["target"])
        print "found " + str(init_points_loaded) + " initialization points: " + str(init_dict)
        
        bo.initialize(init_dict)
        bo.maximize(init_points = max(0, init_points - init_points_loaded), n_iter = 0, acq = 'poi', kappa = 3, xi = xi_scheduler(0.0, max_iterations), **gp_params)
        print "initialization done"
    else:
        bo.maximize(init_points = init_points, n_iter = 0, acq = 'poi', kappa = 3, xi = xi_scheduler(0.0, max_iterations), **gp_params)
    
    cur_iteration = 1
    patience_cnt = 0
    best_cost = -7.0
    
    for it in range(max_iterations): 
        cur_xi = xi_scheduler(cur_iteration, max_iterations)
        print "cur_iteration = " + str(cur_iteration) + ", using xi = " + str(cur_xi)

        cur_iteration += 1
        
        bo.maximize(init_points = 0, n_iter = 1, acq = 'poi', kappa = 3, xi = cur_xi, **gp_params)

        # evaluate the current maximum
        curval = bo.res['max']
        cost = curval['max_val']
        curparams = curval['max_params']
    
        confhandler = ConfigFileHandler()
        confhandler.config.optionxform = str
        confhandler.new_section(name)
        confhandler.set_field(name, 'target', str(cost))
        
        for key, val in curparams.iteritems():
            confhandler.set_field(name, key, str(val))
        
        confhandler.save_configuration(os.path.join(out_dir, name + '.txt'))
        
        # check if it is time to stop this optimization
        if(cost > best_cost):
            best_cost = cost
            patience_cnt = 0
            
        patience_cnt += 1
        
        if(patience_cnt > patience):
            break
            
    return curparams
def main():
    
    if len(sys.argv) < 3:
        print "Error: at least 2 arguments are required"

    campaign_dir = sys.argv[1]
    workdir = sys.argv[2]

    if len(sys.argv) >= 4:
        input_config_file = sys.argv[3]
    else:
        input_config_file = None

    # make sure that the given directory ends with a /
    if not campaign_dir.endswith('/'):
        campaign_dir += "/"
        
    confhandler = ConfigFileHandler()
    confhandler.load_configuration(campaign_dir + "campaign.conf")
    
    iterables = {}
    
    for section in confhandler.get_sections():
        if '!' in section:
            sweep_name = re.sub('!', '', section)
            sweep_sections = ConfigFileUtils.parse_list(confhandler.get_field(section, 'variables'), lambda x: x)

            # now look for the sweep variables that belong to this sweep
            for sweep_section in sweep_sections:
                # this is a section that determines a new sweep direction, possibly linked
                sweep_metadata = confhandler.get_field(sweep_section, 'variable').split(':')
                sweep_scope = sweep_metadata[0]
                sweep_parameter = sweep_metadata[1]

                # request more information
                sweep_behaviour = confhandler.get_field(sweep_section, 'behaviour')

                if ConfigFileUtils.is_dict(confhandler.get_field(sweep_section, 'start')):
                    # will need a dictionary iterable
                    start_dict = ConfigFileUtils.parse_dict(confhandler.get_field(sweep_section, 'start'), lambda x: float(x))
                    end_dict = ConfigFileUtils.parse_dict(confhandler.get_field(sweep_section, 'end'), lambda x: float(x))
                    step_dict = ConfigFileUtils.parse_dict(confhandler.get_field(sweep_section, 'step'), lambda x: float(x))

                    if sweep_name not in iterables:
                        it = SweepDimensionDict(sweep_scope, sweep_parameter, start_dict, end_dict, step_dict, sweep_behaviour)
                        iterables[sweep_name] = it
                    else:
                        iterables[sweep_name].add(sweep_scope, sweep_parameter, start_dict, end_dict, step_dict, sweep_behaviour)
                else:
                    # construct a list iterable instead
                    start_list = ConfigFileUtils.parse_list(confhandler.get_field(sweep_section, 'start'), lambda x: x)    
                    end_list = ConfigFileUtils.parse_list(confhandler.get_field(sweep_section, 'end'), lambda x: x)

                    if sweep_name not in iterables:
                        it = SweepDimensionList(sweep_scope, sweep_parameter, start_list, end_list, sweep_behaviour)
                        iterables[sweep_name] = it
                    else:
                        iterables[sweep_name].add(sweep_scope, sweep_parameter, start_list, end_list, sweep_behaviour)

    MC_path = os.path.join(workdir, "trainval/")
    model_type = confhandler.get_field('global', 'model_type')

    # get the mass point from the global config file in a way that ensures backward compatibility
    try:
        mass_point = float(confhandler.get_field('global', 'mass_point'))
    except KeyError:
        mass_point = 125.0

    if model_type == 'SimpleModel':
        # using the full mass range for training, not using the 118/130GeV cut
        mcoll = SimpleModelFactoryDynamic.GenerateSimpleModelCollections(MC_path, input_config_file = input_config_file, hyperparam_config_file = None, mass_point = mass_point)
    elif model_type == 'CombinedModel':
        mcoll = ModelFactoryFullCategorySetOptimizedInputs.GenerateCombinedModelCollections(MC_path)
        
    iterate(iterables, {}, lambda it: augment_config(mcoll, campaign_dir, it))
def main():
    if len(sys.argv) != 3:
        print "Error: exactly 2 arguments are required!"

    source_path = sys.argv[1]
    #source_path = "/data_CMS/cms/wind/CJLST_NTuples_prepared_systematics/"
    dest_path = sys.argv[2]

    # global settings:
    zzroot = os.environ["CMSSW_BASE"]
    bin_dir = os.path.join(zzroot, "bin/slc6_amd64_gcc630/")

    scrambler = os.path.join(bin_dir, "run_scrambler")
    chunk_extractor = os.path.join(bin_dir, "run_chunk_extractor")

    settings_path = os.path.join(dest_path, "settings.conf")

    confhandler = ConfigFileHandler()
    confhandler.load_configuration(settings_path)

    # load global settings from the configuration file
    root_file_name = confhandler.get_field("Global", "root_file_name")
    source_dir = confhandler.get_field("Global", "source_dir")
    chunk_size = int(confhandler.get_field("Global", "chunk_size"))

    def submit_job(cmd_dir, command):
        job_submitter = os.environ["JOB_SUBMITTER"]

        filename = str(uuid.uuid4()) + ".sh"
        file_path = os.path.join(cmd_dir, filename)
        with open(file_path, "w") as cmd_file:
            cmd_file.write("#!/bin/bash\n")
            cmd_file.write(command)

        while True:
            try:
                output = sp.check_output([job_submitter, "-short", file_path])
                break
            except sp.CalledProcessError:
                print "-------------------------------------------------"
                print " error submitting job, retrying ... "
                print "-------------------------------------------------"

        print output

    def chunk_file(in_dir, out_root, base_name, number_chunks, cmd_dir):
        splits = np.linspace(0, 1, number_chunks)
        in_file = os.path.join(in_dir, root_file_name)

        if number_chunks == 1:
            out_folder = os.path.join(out_root, base_name + "_chunk_0/")

            if not os.path.exists(out_folder):
                os.makedirs(out_folder)

            out_file = os.path.join(out_folder, root_file_name)

            command = " ".join([chunk_extractor, in_file, out_file, str(0.0), str(1.0), str(0)])
            submit_job(cmd_dir, command)
            print command

        else:
            for i in range(len(splits) - 1):
                start_split = splits[i]
                end_split = splits[i + 1]
            
                out_folder = os.path.join(out_root, base_name + "_chunk_" + str(i) + "/")
                if not os.path.exists(out_folder):
                    os.makedirs(out_folder)

                out_file = os.path.join(out_folder, root_file_name)
                
                command = " ".join([chunk_extractor, in_file, out_file, str(start_split), str(end_split), str(0)])
                submit_job(cmd_dir, command)
                print command

    # create the needed folders:
    train_dir = os.path.join(dest_path, "training/")
    validation_dir = os.path.join(dest_path, "validation/")
    test_dir = os.path.join(dest_path, "test/")
    trainval_dir = os.path.join(dest_path, "trainval/")
    temp_dir = os.path.join(dest_path, "temp/")

    # create these directories
    if not os.path.exists(train_dir):
        os.makedirs(train_dir)
    
    if not os.path.exists(validation_dir):
        os.makedirs(validation_dir)
    
    if not os.path.exists(test_dir):
        os.makedirs(test_dir)
    
    if not os.path.exists(trainval_dir):
        os.makedirs(trainval_dir)
    
    if not os.path.exists(temp_dir):
        os.makedirs(temp_dir)

    training_files = [cur_file for cur_file in confhandler.get_sections() if "Global" not in cur_file]
    available_files = next(os.walk(source_path))[1]
    used_files = []
    
    for training_file in training_files:
        sect = confhandler.get_section(training_file)
    
        print "--------------------------------------------------"
        print "currently splitting: " + training_file
    
        source_files = ConfigFileUtils.parse_list(sect["source"], lambda x: x)
        train_val_splits = ConfigFileUtils.parse_list(sect["train_val_split"], lambda x: float(x))
        val_test_splits = ConfigFileUtils.parse_list(sect["val_test_split"], lambda x: float(x))
    
        # first split the needed files into 3 pieces, as dictated by the splits read from the config file
        for source_file, train_val_split, val_test_split in zip(source_files, train_val_splits, val_test_splits):
        
            print "extracting 0.0 - " + str(train_val_split) + " from " + source_file
        
            dest_dir = os.path.join(train_dir, source_file)
            if not os.path.exists(dest_dir):
                os.makedirs(dest_dir)
    
            output = sp.check_output([chunk_extractor, os.path.join(source_path, source_file, root_file_name),
                                      os.path.join(dest_dir, root_file_name), str(0.0), str(train_val_split)])      
            print output
        
            print "-- -- -- -- -- -- -- -- -- -- -- --"
        
            print "extracting " + str(train_val_split) + " - " + str(val_test_split) + " from " + source_file
        
            dest_dir = os.path.join(validation_dir, source_file)
            if not os.path.exists(dest_dir):
                os.makedirs(dest_dir)
    
            output = sp.check_output([chunk_extractor, os.path.join(source_path, source_file, root_file_name),
                                      os.path.join(dest_dir, root_file_name), str(train_val_split), str(val_test_split)])      
            print output
        
            print "-- -- -- -- -- -- -- -- -- -- -- --"
        
            print "extracting " + str(val_test_split) + " - 1.0 from " + source_file
        
            dest_dir = os.path.join(test_dir, source_file)
            if not os.path.exists(dest_dir):
                os.makedirs(dest_dir)
    
            output = sp.check_output([chunk_extractor, os.path.join(source_path, source_file, root_file_name),
                                      os.path.join(dest_dir, root_file_name), str(val_test_split), str(1.0)])      
            print output
        
            used_files.append(source_file)
    
        print "--------------------------------------------------"

    unused_files = [cur_file for cur_file in available_files if cur_file not in used_files]

    # for all files that are not used for training, split them 50:50 into validation and test ...
    for unused_file in unused_files:
        source_dir = os.path.join(source_path, unused_file)

        # ... unless they are only needed to assess systematics, i.e. are not going to be used at all during the validation step
        if "ext" in unused_file or "tuneup" in unused_file or "tunedown" in unused_file:
            print "extracting 0.0 - 1.0 from " + unused_file

            dest_dir = os.path.join(test_dir, unused_file)
            if not os.path.exists(dest_dir):
                os.makedirs(dest_dir)
                
            output = sp.check_output([chunk_extractor, os.path.join(source_dir, root_file_name),
                                      os.path.join(dest_dir, root_file_name), str(0.0), str(1.0)])      
            print output

        else:
            print "extracting 0.0 - 0.5 from " + unused_file
            
            dest_dir = os.path.join(validation_dir, unused_file)
            if not os.path.exists(dest_dir):
                os.makedirs(dest_dir)
    
            output = sp.check_output([chunk_extractor, os.path.join(source_dir, root_file_name),
                                      os.path.join(dest_dir, root_file_name), str(0.0), str(0.5)])      
            print output

            print "-- -- -- -- -- -- -- -- -- -- -- --"

            print "extracting 0.5 - 1.0 from " + unused_file
            
            dest_dir = os.path.join(test_dir, unused_file)
            if not os.path.exists(dest_dir):
                os.makedirs(dest_dir)
    
            output = sp.check_output([chunk_extractor, os.path.join(source_dir, root_file_name),
                                      os.path.join(dest_dir, root_file_name), str(0.5), str(1.0)])      
            print output
    
    # now have all the needed files split apart, can now proceed to combine them into the training 
    # datasets that will end up in trainval
    for training_file in training_files:
        print "now building training dataset: " + training_file
        sect = confhandler.get_section(training_file)
        source_folders = ConfigFileUtils.parse_list(sect["source"], lambda x: x)
    
        for mode in ["training", "validation"]:

            temp_dest_folder = os.path.join(dest_path, temp_dir, training_file, mode)
            temp_dest_file = os.path.join(temp_dest_folder, root_file_name)

            if not os.path.exists(temp_dest_folder):
                os.makedirs(temp_dest_folder)

            source_files = [os.path.join(dest_path, mode, cur_file, root_file_name) for cur_file in source_folders]

            print "hadd " + temp_dest_file + " " + " ".join(source_files)
            output = sp.check_output(["hadd", temp_dest_file] + source_files)      
            print output
    
            temp_scrambled_folder = os.path.join(dest_path, temp_dir, "scrambled", training_file, mode)
            if not os.path.exists(temp_scrambled_folder):
                os.makedirs(temp_scrambled_folder)
            
            temp_scrambled_file = os.path.join(temp_scrambled_folder, root_file_name)
        
            print scrambler + " " + temp_dest_file + " " + temp_scrambled_file
            output = sp.check_output([scrambler, temp_dest_file, temp_scrambled_file])      
            print output
        
        trainval_dest_folder = os.path.join(trainval_dir, training_file)
        if not os.path.exists(trainval_dest_folder):
            os.makedirs(trainval_dest_folder)
        
        print "hadd " + os.path.join(trainval_dest_folder, root_file_name) + " " + os.path.join(dest_path, temp_dir, "scrambled", training_file, "training", root_file_name) + " " + os.path.join(dest_path, temp_dir, "scrambled", training_file, "validation", root_file_name)
        
        output = sp.check_output(["hadd", os.path.join(trainval_dest_folder, root_file_name),
                                 os.path.join(dest_path, temp_dir, "scrambled", training_file, "training", root_file_name),
                                 os.path.join(dest_path, temp_dir, "scrambled", training_file, "validation", root_file_name)])
        print output

    # at the end, chunk the ROOT files into many smaller ones, to keep the augmentation time short
    train_chunks_dir = os.path.join(dest_path, "training_chunks/")
    validation_chunks_dir = os.path.join(dest_path, "validation_chunks/")
    test_chunks_dir = os.path.join(dest_path, "test_chunks/")

    # create these directories
    if not os.path.exists(train_chunks_dir):
        os.makedirs(train_chunks_dir)
    
    if not os.path.exists(validation_chunks_dir):
        os.makedirs(validation_chunks_dir)
    
    if not os.path.exists(test_chunks_dir):
        os.makedirs(test_chunks_dir)

    for mode in ["training", "validation", "test"]:
        # look at each file individually and put it into chunks
        cur_dir = os.path.join(dest_path, mode)
        available_folders = next(os.walk(cur_dir))[1]

        for available_folder in available_folders:
            available_file = os.path.join(cur_dir, available_folder, root_file_name)

            number_chunks = max(1, os.path.getsize(available_file) / chunk_size)

            print "now splitting file " + available_file + " into " + str(number_chunks) + " chunks"

            out_root = os.path.join(dest_path, mode + "_chunks")
            
            chunk_file(os.path.join(dest_path, mode, available_folder), out_root, available_folder, number_chunks, temp_dir)
        
    print "done."        
示例#6
0
def main():
    global evalcnt

    if len(sys.argv) != 4:
        print "Error: exactly 3 arguments are required"

    run_dir = sys.argv[1]
    out_dir = sys.argv[2]
    engine = sys.argv[3]

    print run_dir
    print out_dir
    print engine

    # punzi_target_2d = lambda WHlept_prior, ZHlept_prior: punzi_target(ggH_prior_default, WHhadr_prior_default, ZHhadr_prior_default,
    #                                                                       WHlept_prior, ZHlept_prior, ZHMET_prior_default,
    #                                                                       ttHhadr_prior_default, ttHlept_prior_default)

    def punzi_target(ggH_prior, WHhadr_prior, ZHhadr_prior, WHlept_prior,
                     ZHlept_prior, ZHMET_prior, ttHhadr_prior, ttHlept_prior):
        global evalcnt

        bin_dir = "/home/llr/cms/wind/cmssw/CMSSW_9_4_2/bin/slc6_amd64_gcc630/"
        cost_function_evaluator = "run_prior_evaluator"

        output = check_output([
            bin_dir + cost_function_evaluator, run_dir, out_dir, engine,
            str(ggH_prior),
            str(WHhadr_prior),
            str(ZHhadr_prior),
            str(WHlept_prior),
            str(ZHlept_prior),
            str(ZHMET_prior),
            str(ttHhadr_prior),
            str(ttHlept_prior)
        ])

        costval = 0.0

        for line in output.split('\n'):
            if "cost = " in line:
                costval = float(line.replace("cost = ", ""))
                break

        if math.isnan(costval):
            costval = -8.75

        # add a regularization term that prefers default priors (i.e. close to 1.0)
        reg_term = 1.0 / 8.0 * (
            (ggH_prior - 1.0)**2.0 + (WHhadr_prior - 1.0)**2.0 +
            (ZHhadr_prior - 1.0)**2.0 + (WHlept_prior - 1.0)**2.0 +
            (ZHlept_prior - 1.0)**2.0 + (ZHMET_prior - 1.0)**2.0 +
            (ttHhadr_prior - 1.0)**2.0 + (ttHlept_prior - 1.0)**2.0)
        costval -= reg_term * lambda_reg

        # save the sampled point such that later they can be used as exploration points (if the need occurs)
        confhandler = ConfigFileHandler()
        evaluations_path = out_dir + 'evaluations.txt'

        if os.path.exists(evaluations_path):
            confhandler.load_configuration(evaluations_path)

        print "saving evaluation for iteration " + str(evalcnt)

        section_name = 'evaluation_' + str(evalcnt)
        confhandler.new_section(section_name)
        confhandler.set_field(section_name, 'cost', str(costval))
        confhandler.set_field(section_name, 'ggH_prior', str(ggH_prior))
        confhandler.set_field(section_name, 'WHhadr_prior', str(WHhadr_prior))
        confhandler.set_field(section_name, 'ZHhadr_prior', str(ZHhadr_prior))
        confhandler.set_field(section_name, 'WHlept_prior', str(WHlept_prior))
        confhandler.set_field(section_name, 'ZHlept_prior', str(ZHlept_prior))
        confhandler.set_field(section_name, 'ZHMET_prior', str(ZHMET_prior))
        confhandler.set_field(section_name, 'ttHhadr_prior',
                              str(ttHhadr_prior))
        confhandler.set_field(section_name, 'ttHlept_prior',
                              str(ttHlept_prior))

        confhandler.save_configuration(evaluations_path)

        evalcnt += 1

        return costval

    eps = 1e-1
    delta = 0.2
    bo = BayesianOptimization(
        punzi_target, {
            'ggH_prior': (1.0 - delta, 1.0 + delta),
            'WHhadr_prior': (eps, 1.0),
            'ZHhadr_prior': (eps, 1.0),
            'WHlept_prior': (eps, 1.0),
            'ZHlept_prior': (eps, 1.0),
            'ZHMET_prior': (eps, 1.0),
            'ttHhadr_prior': (eps, 1.0),
            'ttHlept_prior': (eps, 1.0)
        })

    # bo = BayesianOptimization(punzi_target_2d, {'WHlept_prior': (eps, WHlept_prior_default + delta),
    #                                                  'ZHlept_prior': (eps, ZHlept_prior_default + delta)})

    # check if a file with previously evaluated points exists, if so, use them for initialization
    confhandler = ConfigFileHandler()
    evaluations_path = out_dir + 'evaluations.txt'

    if os.path.exists(evaluations_path):
        confhandler.load_configuration(evaluations_path)

        ggH_priors_init = []
        WHhadr_priors_init = []
        ZHhadr_priors_init = []
        WHlept_priors_init = []
        ZHlept_priors_init = []
        ZHMET_priors_init = []
        ttHhadr_priors_init = []
        ttHlept_priors_init = []
        targets_init = []

        for section_name in confhandler.get_sections():
            cur_section = confhandler.get_section(section_name)

            targets_init.append(float(cur_section['cost']))
            ggH_priors_init.append(float(cur_section['ggH_prior']))
            WHhadr_priors_init.append(float(cur_section['WHhadr_prior']))
            ZHhadr_priors_init.append(float(cur_section['ZHhadr_prior']))
            WHlept_priors_init.append(float(cur_section['WHlept_prior']))
            ZHlept_priors_init.append(float(cur_section['ZHlept_prior']))
            ZHMET_priors_init.append(float(cur_section['ZHMET_prior']))
            ttHhadr_priors_init.append(float(cur_section['ttHhadr_prior']))
            ttHlept_priors_init.append(float(cur_section['ttHlept_prior']))

        init_dict = {
            'target': targets_init,
            'ggH_prior': ggH_priors_init,
            'WHhadr_prior': WHhadr_priors_init,
            'ZHhadr_prior': ZHhadr_priors_init,
            'WHlept_prior': WHlept_priors_init,
            'ZHlept_prior': ZHlept_priors_init,
            'ZHMET_prior': ZHMET_priors_init,
            'ttHhadr_prior': ttHhadr_priors_init,
            'ttHlept_prior': ttHlept_priors_init
        }

        evalcnt = int(re.sub('evaluation_', '',
                             confhandler.get_sections()[-1])) + 1

        print "resuming at evaluation " + str(evalcnt)

        bo.initialize(init_dict)
        initialized = True
    else:
        initialized = False

    # change the kernel to have a length scale more appropriate to this function
    # alpha ... corresponds to the value added to the diagonal elements of the covariance matrix <-> the approximate noise level in the observations
    gp_params = {
        'kernel':
        1.0 *
        Matern(length_scale=0.05, length_scale_bounds=(1e-5, 1e5), nu=1.5),
        'alpha':
        1e-1
    }

    # perform the standard initialization and setup
    if initialized:
        bo.maximize(init_points=0,
                    n_iter=0,
                    acq='poi',
                    kappa=3,
                    xi=xi_scheduler(0.0),
                    **gp_params)
    else:
        bo.maximize(init_points=6,
                    n_iter=0,
                    acq='poi',
                    kappa=3,
                    xi=xi_scheduler(0.0),
                    **gp_params)

    cur_iteration = 1
    for it in range(1000):
        cur_iteration += 1

        cur_xi = xi_scheduler(cur_iteration)
        print "using xi = " + str(cur_xi)

        bo.maximize(init_points=6,
                    n_iter=1,
                    acq='poi',
                    kappa=3,
                    xi=cur_xi,
                    **gp_params)

        # evaluate the current maximum
        curval = bo.res['max']
        cost = curval['max_val']
        priors = curval['max_params']

        confhandler = ConfigFileHandler()
        confhandler.config.optionxform = str
        confhandler.new_section('Priors')
        confhandler.set_field('Priors', 'cost', str(cost))
        confhandler.set_field('Priors', 'VBF_prior', str(1.0))

        for key, val in priors.iteritems():
            confhandler.set_field('Priors', key, str(val))

        confhandler.save_configuration(out_dir + 'priors.txt')
示例#7
0
def main():
    global evalcnt

    if len(sys.argv) != 4:
        print "Error: exactly 3 arguments are required"

    ref_dir = sys.argv[1]
    out_dir = sys.argv[2]
    lumi = float(sys.argv[3])

    print ref_dir
    print out_dir
    print lumi

    def punzi_target(WP_VBF2j, WP_VBF1j, WP_WHh, WP_ZHh):
        global evalcnt

        bin_dir = "/home/llr/cms/wind/cmssw/CMSSW_9_4_2/bin/slc6_amd64_gcc630/"
        cost_function_evaluator = "run_WP_evaluator"

        output = check_output([
            bin_dir + cost_function_evaluator, ref_dir, out_dir,
            str(lumi),
            str(WP_VBF2j),
            str(WP_VBF1j),
            str(WP_WHh),
            str(WP_ZHh)
        ])

        costval = 0.0

        for line in output.split('\n'):
            if "cost = " in line:
                costval = float(line.replace("cost = ", ""))
                break

        if math.isnan(costval):
            costval = -8.75

        # save the sampled point such that later they can be used as exploration points (if the need occurs)
        confhandler = ConfigFileHandler()
        evaluations_path = out_dir + 'evaluations.txt'

        if os.path.exists(evaluations_path):
            confhandler.load_configuration(evaluations_path)

        print "saving evaluation for iteration " + str(evalcnt)

        section_name = 'evaluation_' + str(evalcnt)
        confhandler.new_section(section_name)
        confhandler.set_field(section_name, 'cost', str(costval))
        confhandler.set_field(section_name, 'WP_VBF2j', str(WP_VBF2j))
        confhandler.set_field(section_name, 'WP_VBF1j', str(WP_VBF1j))
        confhandler.set_field(section_name, 'WP_WHh', str(WP_WHh))
        confhandler.set_field(section_name, 'WP_ZHh', str(WP_ZHh))

        confhandler.save_configuration(evaluations_path)

        evalcnt += 1

        return costval

    eps = 1e-3
    delta = 0.2
    bo = BayesianOptimization(
        punzi_target, {
            'WP_VBF2j': (eps, 1.0 - eps),
            'WP_VBF1j': (eps, 1.0 - eps),
            'WP_WHh': (eps, 1.0 - eps),
            'WP_ZHh': (eps, 1.0 - eps)
        })

    # check if a file with previously evaluated points exists, if so, use them for initialization
    confhandler = ConfigFileHandler()
    evaluations_path = out_dir + 'evaluations.txt'

    if os.path.exists(evaluations_path):
        confhandler.load_configuration(evaluations_path)

        targets_init = []
        WP_VBF2j_init = []
        WP_VBF1j_init = []
        WP_WHh_init = []
        WP_ZHh_init = []

        for section_name in confhandler.get_sections():
            cur_section = confhandler.get_section(section_name)

            targets_init.append(float(cur_section['cost']))
            WP_VBF2j_init.append(float(cur_section['WP_VBF2j']))
            WP_VBF1j_init.append(float(cur_section['WP_VBF1j']))
            WP_WHh_init.append(float(cur_section['WP_WHh']))
            WP_ZHh_init.append(float(cur_section['WP_ZHh']))

        init_dict = {
            'target': targets_init,
            'WP_VBF2j': WP_VBF2j_init,
            'WP_VBF1j': WP_VBF1j_init,
            'WP_WHh': WP_WHh_init,
            'WP_ZHh': WP_ZHh_init
        }

        evalcnt = int(re.sub('evaluation_', '',
                             confhandler.get_sections()[-1])) + 1

        print "resuming at evaluation " + str(evalcnt)

        bo.initialize(init_dict)
        initialized = True
    else:
        initialized = False

    # change the kernel to have a length scale more appropriate to this function
    gp_params = {
        'kernel':
        1.0 *
        Matern(length_scale=0.05, length_scale_bounds=(1e-5, 1e5), nu=1.5),
        'alpha':
        1e-5
    }

    # perform the standard initialization and setup
    if initialized:
        bo.maximize(init_points=0,
                    n_iter=0,
                    acq='poi',
                    kappa=3,
                    xi=xi_scheduler(0.0),
                    **gp_params)
    else:
        bo.maximize(init_points=6,
                    n_iter=0,
                    acq='poi',
                    kappa=3,
                    xi=xi_scheduler(0.0),
                    **gp_params)

    cur_iteration = 1
    for it in range(1000):
        cur_xi = xi_scheduler(cur_iteration)
        cur_iteration += 1
        print "using xi = " + str(cur_xi)

        bo.maximize(init_points=6,
                    n_iter=1,
                    acq='poi',
                    kappa=3,
                    xi=cur_xi,
                    **gp_params)

        # evaluate the current maximum
        curval = bo.res['max']
        cost = curval['max_val']
        WPs = curval['max_params']

        confhandler = ConfigFileHandler()
        confhandler.config.optionxform = str
        confhandler.new_section('WPs')
        confhandler.set_field('WPs', 'cost', str(cost))

        for key, val in WPs.iteritems():
            confhandler.set_field('WPs', key, str(val))

        confhandler.save_configuration(out_dir + 'WPs.txt')