示例#1
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)))
示例#2
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)
示例#3
0
def write_some_results_combine(config, fit_result, save_root=False):

    from tf_pwa.applications import fit_fractions

    for i, c in enumerate(config.configs):
        c.plot_partial_wave(fit_result,
                            prefix="figure/s{}_".format(i),
                            save_root=save_root)

    for it, config_i in enumerate(config.configs):
        print("########## fit fractions {}:".format(it))
        print(f"nll{it}", config_i.get_fcn()({}).numpy())
        mcdata = config_i.get_phsp_noeff()
        fit_frac, err_frac = fit_fractions(
            config_i.get_amplitude(),
            mcdata,
            config.inv_he,
            fit_result.params,
        )
        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)
        save_frac_csv(f"fit_frac{it}.csv", fit_frac)
        save_frac_csv(f"fit_frac{it}_err.csv", err_frac)
示例#4
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.
示例#5
0
文件: fit.py 项目: zhixing1996/tf-pwa
def fit(config, init_params="", method="BFGS", loop=1):
    """
    simple fit script
    """
    # load config.yml
    # config = ConfigLoader(config_file)

    # load data
    all_data = config.get_all_data()

    fit_results = []
    for i in range(loop):
        # set initial parameters if have
        if config.set_params(init_params):
            print("using {}".format(init_params))
        else:
            print("\nusing RANDOM parameters", flush=True)
        # try to fit
        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
        fit_results.append(fit_result)
        # reset parameters
        try:
            config.reinit_params()
        except Exception as e:
            print(e)

    fit_result = fit_results.pop()
    for i in fit_results:
        if i.success:
            if not fit_result.success or fit_result.min_nll > i.min_nll:
                fit_result = i

    config.set_params(fit_result.params)
    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:")
    print("Fit status: ", fit_result.success)
    print("Minimal -lnL = ", fit_result.min_nll)
    for k, v in config.get_params().items():
        print(k, error_print(v, fit_error.get(k, None)))

    return fit_result
示例#6
0
文件: fit.py 项目: zhixing1996/tf-pwa
def write_some_results(config, fit_result):
    # 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)
示例#7
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
示例#8
0
def fit():
    dtype = "float64"

    config_list = load_config_file("Resonances")
    decs, final_particles, decay = get_decay_chains(config_list)
    amp = get_amplitude(decs, config_list, decay)
    # data/pure1w.dat  data/flat_mc30w.dat
    data = prepare_data(
        "data/data4600_new.dat", decs, particles=final_particles, dtype=dtype
    )
    mcdata = prepare_data(
        "data/PHSP4600_new.dat", decs, particles=final_particles, dtype=dtype
    )
    bg = prepare_data(
        "data/bg4600_new.dat", decs, particles=final_particles, dtype=dtype
    )
    w_bkg = 0.768331

    model = Model(amp, w_bkg=w_bkg)
    load_params(model.Amp, "glb_params_rp.json")
    model.Amp.vm.trans_params(True)
    args_name = model.Amp.vm.trainable_vars

    fcn = FCN(model, data, mcdata, bg, batch=65000)
    SCIPY = False
    hesse = True
    if SCIPY:

        def callback(x):
            print(fcn.cached_nll)

        f_g = fcn.model.Amp.vm.trans_fcn_grad(fcn.nll_grad)
        now = time.time()
        s = minimize(
            f_g,
            np.array(fcn.model.Amp.vm.get_all_val(True)),
            method="BFGS",
            jac=True,
            callback=callback,
            options={"disp": 1, "gtol": 1e-4, "maxiter": 1000},
        )
        model.Amp.vm.trans_params(True)
        xn = fcn.model.Amp.vm.get_all_val()
        print("########## fit state:")
        print(s)
        print("\nTime for fitting:", time.time() - now)
        val = {k: v.numpy() for k, v in model.Amp.variables.items()}
        err = {}
        if hesse:
            inv_he = cal_hesse_error(
                model.Amp, val, w_bkg, data, mcdata, bg, args_name, batch=20000
            )
            diag_he = inv_he.diagonal()
            hesse_error = np.sqrt(np.fabs(diag_he)).tolist()
            print(hesse_error)
            err = dict(zip(model.Amp.vm.trainable_vars, hesse_error))

    else:
        from tf_pwa.fit import fit_minuit

        m = fit_minuit(fcn, hesse=False)
        model.Amp.vm.trans_params(True)
        err_mtrx = m.np_covariance()
        np.save("error_matrix.npy", err_mtrx)
        val = dict(m.values)
        err = dict(m.errors)

    print("\n########## fit results:")
    for i in val:
        print("  ", i, ":", error_print(val[i], err.get(i, None)))

    frac = True
    err_frac = {}
    if frac:
        err_frac = {}
        if hesse:
            frac, grad = cal_fitfractions(
                model.Amp, list(split_generator(mcdata, 25000))
            )
        else:
            frac = cal_fitfractions_no_grad(
                model.Amp, list(split_generator(mcdata, 45000))
            )
        for i in frac:
            if hesse:
                err_frac[i] = np.sqrt(np.dot(np.dot(inv_he, grad[i]), grad[i]))
        print("########## fit fractions")
        for i in frac:
            print(i, ":", error_print(frac[i], err_frac.get(i, None)))

    outdic = {
        "value": val,
        "error": err,
        "config": config_list,
        "frac": frac,
        "err_frac": err_frac,
        "NLL": fcn.cached_nll.numpy(),
    }
    with open("glb_params_rp.json", "w") as f:
        json.dump(outdic, f, indent=2)
示例#9
0
def fit(method="BFGS", init_params="init_params.json", hesse=True, frac=True):
    POLAR = True  # fit in polar coordinates. should be consistent with init_params.json if any
    GEN_TOY = False  # use toy data (mcdata and bg stay the same). REMEMBER to update gen_params.json

    dtype = "float64"
    w_bkg = 0.768331
    # set_gpu_mem_growth()
    tf.keras.backend.set_floatx(dtype)
    # open Resonances list as dict
    config_list = load_config_file("Resonances")
    config_list2 = load_config_file("Resonances")

    vm = VarsManager()

    decs, final_particles, decay = get_decay_chains(config_list)
    data, bg, mcdata = prepare_data(
        data_file_name, decs, particles=final_particles, dtype=dtype
    )
    decs2, final_particles2, decay2 = get_decay_chains(config_list)
    data2, bg2, mcdata2 = prepare_data(
        data_file_name2, decs2, particles=final_particles2, dtype=dtype
    )

    amp = get_amplitude(decs, config_list, decay, polar=POLAR, vm=vm)
    amp2 = get_amplitude(decs2, config_list2, decay2, polar=POLAR, vm=vm)

    load_params(amp, init_params)
    load_params(amp2, init_params)

    model = Model(amp, w_bkg=w_bkg)
    model2 = Model(amp2, w_bkg=w_bkg)
    pprint(model.get_params())

    fcn = CombineFCN(
        [model, model2],
        [data, data2],
        [mcdata, mcdata2],
        bg=[bg, bg2],
        batch=65000,
    )

    # fit configure
    args = {}
    args_name = vm.trainable_vars
    x0 = []
    bnds = []
    bounds_dict = {
        # "Zc_4160_m:0":(4.1,4.22),
        # "Zc_4160_g:0":(0,None)
    }

    for i in model.Amp.trainable_variables:
        args[i.name] = i.numpy()
        x0.append(i.numpy())
        if i.name in bounds_dict:
            bnds.append(bounds_dict[i.name])
        else:
            bnds.append((None, None))
        args["error_" + i.name] = 0.1

    points = []
    nlls = []
    now = time.time()
    maxiter = 1000

    method = "BFGS"

    def callback(x):
        if np.fabs(x).sum() > 1e7:
            x_p = dict(zip(args_name, x))
            raise Exception("x too large: {}".format(x_p))
        points.append(vm.get_all_val())
        nlls.append(float(fcn.cached_nll))
        print(fcn.cached_nll)

    vm.set_bound(bounds_dict)
    f_g = vm.trans_fcn_grad(fcn.nll_grad)
    s = minimize(
        f_g,
        np.array(vm.get_all_val(True)),
        method=method,
        jac=True,
        callback=callback,
        options={"disp": 1, "gtol": 1e-4, "maxiter": maxiter},
    )
    xn = vm.get_all_val()
    print("########## fit state:")
    print(s)
    print("\nTime for fitting:", time.time() - now)
    model.Amp.vm.trans_params(POLAR)
    val = {k: v.numpy() for k, v in model.Amp.variables.items()}
    with open("final_params.json", "w") as f:
        json.dump({"value": val}, f, indent=2)

    err = {}
    inv_he = cal_hesse_error(
        model.Amp, val, w_bkg, data, mcdata, bg, args_name, batch=20000
    )
    diag_he = inv_he.diagonal()
    hesse_error = np.sqrt(np.fabs(diag_he)).tolist()
    print(hesse_error)
    err = dict(zip(model.Amp.vm.trainable_vars, hesse_error))
    print("\n########## fit results:")
    for i in val:
        print("  ", i, ":", error_print(val[i], err.get(i, None)))

    outdic = {"value": val, "error": err, "config": config_list}
    with open("final_params.json", "w") as f:
        json.dump(outdic, f, indent=2)

    err_frac = {}

    frac, grad = cal_fitfractions(
        model.Amp, list(split_generator(mcdata, 25000))
    )

    for i in frac:
        err_frac[i] = np.sqrt(np.dot(np.dot(inv_he, grad[i]), grad[i]))

    print("########## fit fractions")
    for i in frac:
        print(i, ":", error_print(frac[i], err_frac.get(i, None)))
    print("\nEND\n")
    """outdic = {"value": val, "error": err, "frac": frac, "err_frac": err_frac}