예제 #1
0
def get_data(config_file="config.yml", init_params="init_params.json"):

    config = ConfigLoader(config_file)
    try:
        config.set_params(init_params)
        print("using {}".format(init_params))
    except Exception as e:
        print("using RANDOM parameters")

    phsp = config.get_data("phsp")

    for i in config.full_decay:
        print(i)
        for j in i:
            print(j.get_ls_list())

    print("\n########### initial parameters")
    print(json.dumps(config.get_params(), indent=2))
    params = config.get_params()

    amp = config.get_amplitude()
    pw = amp.partial_weight(phsp)
    pw_if = amp.partial_weight_interference(phsp)
    weight = amp(phsp)
    print(weight)
    return config, amp, phsp, weight, pw, pw_if
예제 #2
0
def fit(final_params_file):
    config = ConfigLoader(
        "config.yml"
    )  # We use ConfigLoader to read the information in the configuration file
    # config.set_params("gen_params.json") # If not set, we will use random initial parameters
    fit_result = config.fit(method="BFGS")

    errors = config.get_params_error(
        fit_result)  # calculate Hesse errors of the parameters
    print("\n########## fit parameters:")
    for key, value in config.get_params().items():
        print(key, error_print(value, errors.get(key, None)))

    fit_result.save_as(final_params_file)  # save fit_result to a json file
    config.plot_partial_wave(
        fit_result
    )  # Plot distributions of variables indicated in the configuration file

    fit_frac, err_frac = config.cal_fitfractions()
    print("\n########## fit fractions:")
    for i in fit_frac:
        if not isinstance(i, tuple):  # fit fraction
            name = i
        else:
            name = "{}x{}".format(*i)  # interference term
        print(name + ": " + error_print(fit_frac[i], err_frac.get(i, None)))
예제 #3
0
def cal_fitfractions(params_file):
    config = ConfigLoader("config.yml")
    config.set_params(params_file)
    params = config.get_params()
    config.get_params_error(params)

    mcdata = (
        config.get_phsp_noeff()
    )  # use the file of PhaseSpace MC without efficiency indicated in config.yml
    fit_frac, err_frac = fit_fractions(
        config.get_amplitude(), mcdata, config.inv_he, params
    )
    print("########## fit fractions:")
    fit_frac_string = ""
    for i in fit_frac:
        if isinstance(i, tuple):
            name = "{}x{}".format(*i)  # interference term
        else:
            name = i  # fit fraction
        fit_frac_string += "{} {}\n".format(
            name, error_print(fit_frac[i], err_frac.get(i, None))
        )
    print(fit_frac_string)
    print("########## fit fractions table:")
    print_frac_table(
        fit_frac_string
    )  # print the fit-fractions as a 2-D table. The codes below are just to implement the print function.
예제 #4
0
def fit(config="config.yml", init_params="init_params.json", method="BFGS"):
    """
    simple fit script
    """
    # load config.yml
    config = ConfigLoader(config)

    # set initial parameters if have
    try:
        config.set_params(init_params)
        print("using {}".format(init_params))
    except Exception as e:
        if str(e) != "[Errno 2] No such file or directory: 'init_params.json'":
            print(e)
        print("\nusing RANDOM parameters", flush=True)

    # print("\n########### initial parameters")
    # json_print(config.get_params())

    # fit
    data, phsp, bg, inmc = config.get_all_data()
    try:
        fit_result = config.fit(batch=65000, method=method)
    except KeyboardInterrupt:
        config.save_params("break_params.json")
        raise
    except Exception as e:
        print(e)
        config.save_params("break_params.json")
        raise
    json_print(fit_result.params)
    fit_result.save_as("final_params.json")

    # calculate parameters error
    fit_error = config.get_params_error(fit_result, batch=13000)
    fit_result.set_error(fit_error)
    fit_result.save_as("final_params.json")
    pprint(fit_error)

    print("\n########## fit results:")
    for k, v in config.get_params().items():
        print(k, error_print(v, fit_error.get(k, None)))

    # plot partial wave distribution
    config.plot_partial_wave(fit_result, plot_pull=True)

    # calculate fit fractions
    phsp_noeff = config.get_phsp_noeff()
    fit_frac, err_frac = config.cal_fitfractions({}, phsp_noeff)

    print("########## fit fractions")
    fit_frac_string = ""
    for i in fit_frac:
        if isinstance(i, tuple):
            name = "{}x{}".format(*i)
        else:
            name = i
        fit_frac_string += "{} {}\n".format(
            name, error_print(fit_frac[i], err_frac.get(i, None)))
    print(fit_frac_string)
예제 #5
0
def cal_errors(params_file):
    config = ConfigLoader("config.yml")
    config.set_params(params_file)
    fcn = config.get_fcn()
    fcn.model.Amp.vm.rp2xy_all(
    )  # we can use this to transform all complex parameters to xy-coordinates, since the Hesse errors of xy are more statistically reliable
    params = config.get_params()
    errors, config.inv_he = cal_hesse_error(
        fcn, params, check_posi_def=True, save_npy=True
    )  # obtain the Hesse errors and the error matrix (inverse Hessian)
    print("\n########## fit parameters in XY-coordinates:")
    errors = dict(zip(fcn.model.Amp.vm.trainable_vars, errors))
    for key, value in config.get_params().items():
        print(key, error_print(value, errors.get(key, None)))

    print("\n########## correlation matrix:")
    print("Matrix index:\n", fcn.model.Amp.vm.trainable_vars)
    print("Correlation Coefficients:\n", corr_coef_matrix(config.inv_he)
          )  # obtain the correlation matrix using the inverse Hessian
예제 #6
0
def single_fit(config_dict, data, phsp, bg):
    config = ConfigLoader(config_dict)

    print("\n########### initial parameters")
    pprint(config.get_params())
    print(config.full_decay)
    fit_result = config.fit(data, phsp, bg=bg)
    pprint(fit_result.params)
    # fit_result.save_as("final_params.json")
    return fit_result.min_nll, fit_result.ndf