示例#1
0
def _collect_test_stats(test_function: str, N: int, noise_std: float, random: bool, gps: Tuple[str, ...], M: int):
    store = store_path(test_function, N, noise_std, random, M)
    for k in range(K):
        fold = data.Fold(store, k)
        for gp in gps:
            gp_path = fold.dir / gp
            frame = _test_stats(k, gp_path)
示例#2
0
def predict_roms(M: int, N: int, random: bool, noisy: bool):
    store_name = 'sin.u1.'
    CDF_scale = 2 * pi
    CDF_loc = pi
    function_with_parameters = function.CallableWithParameters(
        function=function.ishigami, parameters={
            'a': 0,
            'b': 0
        })
    store_name = store_name + '{0:d}.{1:d}'.format(N, M)
    store_name += '.random' if random else '.rom'
    noise_std = 0.0001 if noisy else 0
    store = data.Store(store_dir(store_name, noise_std, CDF_scale),
                       data.Store.InitMode.READ_META_ONLY)
    fold = data.Fold(store, 0)
    rom = model.gpy_.ROM.from_ROM(fold=fold, name='rom', suffix='.test.full')
    model_theta = rom.sobol.parameters_read.Theta
    data_theta = function.linear_matrix_from_meta(store)
    pre_function_with_parameters = (function.CallableWithParameters(
        function=function.linear, parameters={'matrix': data_theta})
                                    if random else None)
    test_store = scalar_function_of_normal(
        store_name=store_name + "\\test",
        N=N,
        M=M,
        X_std=1.0,
        noise_std=noise_std,
        CDF_scale=CDF_scale,
        CDF_loc=CDF_loc,
        pre_function_with_parameters=pre_function_with_parameters,
        function_with_parameters=function_with_parameters)
    fold.set_test_data(df=test_store.data.df)
    rom.sobol.gp.test()
    result = matmul(model_theta, data_theta.T)
    print(result)
示例#3
0
def _collect_std(test_function: str, N: int, noise_std: float, random: bool, M: int):
    store = data.Store(store_path(test_function, N, noise_std, random, M))
    destination = store.dir / "results"
    shutil.rmtree(destination, ignore_errors=True)
    destination.mkdir(mode=0o777, parents=True, exist_ok=False)
    result = 0.0
    for k in range(K):
        fold = data.Fold(store, k)
        result += fold.standard.df.iloc[-1, -1]/K
    savetxt(fname=(destination / "std.csv"), X=atleast_2d(result), delimiter=",")
示例#4
0
def _collect_test_stats(M, N, function_name, random, noisy):
    noisy_str = NORMAL_CDF_DIR if noisy else NOISELESS_DIR
    source_store = store_dir(M, N, function_name, random, noisy)
    for k in range(FOLDS):
        fold = data.Fold(source_store, k)
        gp_dir = fold.dir / "ard"
        frame = data.Frame(
            gp_dir / "test_stats.csv",
            _test_stats(k,
                        data.Frame(gp_dir / "__test__.csv").df.copy()))
示例#5
0
def _collect_test_stats(test_function: str,
                        N: int,
                        noise_std: float,
                        random: bool,
                        gp: str,
                        M: int = 5):
    source_store = store_path(test_function, N, noise_std, random, M)
    for k in range(K):
        fold = data.Fold(source_store, k)
        gp_dir = fold.dir / gp
        frame = data.Frame(
            gp_dir / "test_stats.csv",
            _test_stats(k,
                        data.Frame(gp_dir / "__test__.csv").df.copy()))
示例#6
0
def test_random(test_fuction: str, N: int, noise_std: float, M: int = 5, K: int = 2 ):
    random = True
    name = 'rbf'
    gp_optimizer_options = {'optimizer': 'bfgs', 'max_iters': 5000, 'gtol': 1E-16}
    kernel_parameters = model.gpy_.Kernel.ExponentialQuadratic.Parameters(lengthscale=full((1, 1), 2.5**(M/5), dtype=float))
    parameters = model.gpy_.GP.DEFAULT_PARAMETERS._replace(kernel=kernel_parameters, e_floor=1E-6, e=0.003)
    CDF_scale = 2 * pi
    CDF_loc = pi
    if test_fuction == 'sin.1':
        function_with_parameters = function.CallableWithParameters(function.ishigami, parameters={'a': 0.0, 'b': 0.0})
    elif test_fuction == 'sin.2':
        function_with_parameters = function.CallableWithParameters(function.ishigami, parameters={'a': 2.0, 'b': 0.0})
    elif test_fuction == 'ishigami':
        function_with_parameters = function.callable_with_parameters(function.ishigami)
    else:
        CDF_scale = 1.0
        CDF_loc = 0.0
        function_with_parameters = function.CallableWithParameters(function.sobol_g,
                                                                   parameters={'m_very_important': 2, 'm_important': 3, 'm_unimportant': 4})
    store_name = test_fuction + '.{0:d}.{1:.3f}.{2:d}'.format(M, noise_std, N)
    if random:
        lin_trans = ortho_group.rvs(M)
        pre_function_with_parameters = function.CallableWithParameters(function=function.linear, parameters={'matrix': lin_trans})
        store_name += '.random'
    else:
        pre_function_with_parameters = None
        store_name += '.rom'
    store_name = BASE_PATH / store_name
    store = scalar_function_of_normal(store_name=store_name, N=N, M=M, X_std=1.0, noise_std=noise_std, CDF_scale=CDF_scale, CDF_loc=CDF_loc,
                                      pre_function_with_parameters=pre_function_with_parameters,
                                      function_with_parameters=function_with_parameters)
    # lin_trans = transpose(lin_trans)
    savetxt(store.dir / "InverseRotation.csv", lin_trans)
    data.Fold.into_K_folds(parent=store, K=K, shuffled_before_folding=False, standard=data.Store.Standard.mean_and_std,
                           replace_empty_test_with_data_=True)
    model.run.GPs(module=model.run.Module.GPY_, name=name, store=store, M_Used=-1, parameters=parameters, optimize=True, test=True, sobol=True,
                  optimizer_options=gp_optimizer_options)
    model.run.GPs(module=model.run.Module.GPY_, name=name, store=store, M_Used=-1, parameters=None, optimize=True, test=True, sobol=True,
                  optimizer_options=gp_optimizer_options, make_ard=True)
    sobol_options = {'semi_norm': model.base.Sobol.SemiNorm.DEFAULT_META, 'N_exploit': 3, 'N_explore': 4096, 'options': {'gtol': 1.0E-16}}
    for k in range(K):
        sobol = model.gpy_.Sobol.from_GP(data.Fold(store, k), 'rbf.ard', 'rbf.ard.derotated')
        for sobol.m in reversed(range(M)):
            sobol.Theta_old = lin_trans
        sobol.write_parameters(sobol.Parameters(Mu=sobol.Mu, Theta=sobol.Theta, D=sobol.Tensor3AsMatrix(sobol.D), S1=None,
                                                S=sobol.Tensor3AsMatrix(sobol.S)))
示例#7
0
def test_random3(test_fuction: str, N: int, noise_std: float, M: int = 5, K: int = 2 ):
    random = True
    name = 'derotated.rbf'
    gp_optimizer_options = {'optimizer': 'bfgs', 'max_iters': 5000, 'gtol': 1E-16}
    kernel_parameters = model.gpy_.Kernel.ExponentialQuadratic.Parameters(lengthscale=full((1, 1), 2.5**(M/5), dtype=float))
    parameters = model.gpy_.GP.DEFAULT_PARAMETERS._replace(kernel=kernel_parameters, e_floor=1E-6, e=0.003)
    store_name = test_fuction + '.{0:d}.{1:.3f}.{2:d}'.format(M, noise_std, N)
    store_name += '.random'
    store_name = BASE_PATH / store_name
    store = data.Store(store_name)
    lin_trans = loadtxt(store.dir / "InverseRotation.csv")
    for k in range(K):
        fold = data.Fold(store, k)
        replace_X_with_U(fold, transpose(lin_trans))
    model.run.GPs(module=model.run.Module.GPY_, name=name, store=store, M_Used=-1, parameters=parameters, optimize=True, test=True, sobol=True,
                  optimizer_options=gp_optimizer_options)
    model.run.GPs(module=model.run.Module.GPY_, name=name, store=store, M_Used=-1, parameters=None, optimize=True, test=True, sobol=True,
                  optimizer_options=gp_optimizer_options, make_ard=True)
    sobol_options = {'semi_norm': model.base.Sobol.SemiNorm.DEFAULT_META, 'N_exploit': 3, 'N_explore': 4096, 'options': {'gtol': 1.0E-16}}
示例#8
0
def _run_test(M, N, function_name, random, noisy):
    noisy_str = NORMAL_CDF_DIR if noisy else NOISELESS_DIR
    source_store = store_dir(M, N, function_name, random, noisy)
    Mu = choose_Mu(function_name)
    kernel_parameters = model.gpy_.Kernel.ExponentialQuadratic.Parameters(
        lengthscale=full((1, Mu), 0.2, dtype=float))
    parameters = model.gpy_.GP.DEFAULT_PARAMETERS._replace(
        kernel=kernel_parameters, e_floor=1E-5, e=1E-10)
    for k in range(FOLDS):
        fold = data.Fold(source_store, k, Mu)
        dst = fold.dir / "rom.reduced"
        if dst.exists():
            shutil.rmtree(dst)
        shutil.copytree(src=fold.dir / "rom.optimized", dst=dst)
        gp = model.gpy_.GP(fold, "rom.reduced", parameters)
        gp.optimize(model.gpy_.GP.DEFAULT_OPTIMIZER_OPTIONS)
        frame = data.Frame(gp.dir / "test_stats.csv",
                           _test_stats(k,
                                       gp.test().df.copy()))
示例#9
0
def predict(gb_path: Union[str, Path],
            inputs: NP.Array) -> Tuple[NP.Vector, NP.Vector]:
    """ Prediction using a GaussianBundle.

    Args:
        gb_path: Path to a model.GaussianBundle. The extension of this filename is the number of input dimensions M.
            An extension of 0 or a missing extension means full order, taking M from the training data.
        inputs: An (N,M) numpy array, consisting of N test inputs, each of dimension M.
    Returns: A pair (predictive_mean, predictive_std) of (N,1) numpy arrays.
    """
    gb_path = Path(gb_path)
    gb_name = gb_path.name
    Xs_taken = int(gb_path.suffix[1:])
    fold_dir = gb_path.parent
    k = int(fold_dir.suffix[1:])
    fold = data.Fold(fold_dir.parent, k, Xs_taken)
    if Xs_taken != inputs.shape[1]:
        raise IndexError(
            "The gp you have chosen uses {0:d} dimensional inputs".format(
                fold.M))
    gb = model.gpy_.GaussianBundle(fold, gb_name, reset_log_likelihood=False)
    return gb.predict(inputs)
示例#10
0
def linear_transformation(model_dir: Path) -> NP.Matrix:
    with open(model_dir / "__meta__.json", mode='r') as file:
        meta = load(file)
    function_with_parameters = meta['origin']['functions_with_parameters'][
        0].split("; matrix=")
    if len(function_with_parameters) > 1:
        function_with_parameters = eval(function_with_parameters[-1][:-1])
        return array(function_with_parameters)
    else:
        return eye(meta['data']['M'], dtype=float)


if __name__ == '__main__':
    store = data.Store((BASE_PATH / NOISELESS_DIR) / "sin.u1.5000.5.random",
                       data.Store.InitMode.READ_META_ONLY)
    fold = data.Fold(store, 0)
    rom = model.gpy_.ROM.from_ROM(fold=fold, name='rom', suffix='.test.full')
    model_theta = rom.sobol.parameters_read.Theta
    data_theta = function.linear_matrix_from_meta(store)
    print(model_theta)
    print(data_theta)
"""
def rename(dir_: Path):
    for p in dir_.iterdir():
        if p.is_dir():
            if p.name == "rom..optimized":
                p.replace(p.parent / "rom.optimized")
            else:
                rename(p)
"""
"""