def check_forces_reproducibility(file_name, n_iter=500):
    # minimizer = 'lbfgs'
    minimizer = 'gsl'
    params = optimize.minimize.Parameters(minimizer)
    params['cache_ytilde_transposed'] = True
    params['use_c_functions'] = True
    # params['algorithm'] = "lbfgs"
    params['algorithm'] = "bfgs2"
    params['verbose'] = False

    [forces_init, w0, y, yTilde, YTilde, theta] = fio.load(file_name,
        hdf5_keys=["forces_init", "w0", "y", "yTilde", "YTilde", "theta"])

    fmin_list = []
    forces_sum_list = []
    for i in range(n_iter):
        wopt, yopt, forces, fmin_initial, fmin_final, chiSqr, S = \
            optimize.forces.find_optimum(forces_init, w0, y, yTilde, YTilde, theta, params)
        fmin_list.append(fmin_final)
        forces_sum_list.append(np.sum(forces))

    fmin_diff_list = []
    for i in range(1, n_iter):
        diff = optimize.util.compute_relative_difference_for_values(fmin_list[0], fmin_list[i])
        fmin_diff_list.append(diff)

    forces_diff_list = []
    for i in range(1, n_iter):
        diff = optimize.util.compute_relative_difference_for_values(forces_sum_list[0], forces_sum_list[i])
        forces_diff_list.append(diff)

    assert(np.sum(fmin_diff_list) < tol)
    assert(np.sum(forces_diff_list) < tol)
示例#2
0
def test_fileio_forces():
    if sys.version_info >= (3, ):
        return
    for filename_pkl in filenames_forces:
        hdf5_file = tempfile.NamedTemporaryFile(mode='w',
                                                suffix=".h5",
                                                delete=False)
        hdf5_file.close()
        print(filename_pkl, hdf5_file.name)

        # perform file conversion
        keys = ["forces_init", "w0", "y", "yTilde", "YTilde", "theta"]
        fio.convert_to_hdf5(filename_pkl, hdf5_file.name, keys)

        # load pickle
        with open(filename_pkl, 'rb') as ifile:
            x = pickle.load(ifile)

        # load hdf5
        y = fio.load(hdf5_file.name, hdf5_keys=keys)
        os.unlink(hdf5_file.name)

        # compare
        assert (len(x) == len(y))
        for i, elem_x in enumerate(x):
            elem_y = y[i]
            assert (np.array_equal(elem_x, elem_y))
示例#3
0
def test_dump_load_list_unlabeled(temp_hdf5_file_name):
    filename = temp_hdf5_file_name

    data = [1, 2, 3, 5.0]
    fio.dump(filename, data)
    check = fio.load(filename)
    for i, item in enumerate(check):
        assert(item == data[i])
    os.remove(filename)
示例#4
0
def test_dump_load_list_labeled(temp_hdf5_file_name):
    filename = temp_hdf5_file_name

    data = [1, 2, 3, 5.0]
    keys = ["one", "two", "three", "five.zero"]
    fio.dump(filename, data, hdf5_keys=keys)
    check = fio.load(filename, hdf5_keys=keys)
    for i, item in enumerate(check):
        assert(item == data[i])
    os.remove(filename)
def run_func(use_c=True):
    [GInit, G, y, yTilde, YTilde, w0, theta] = fio.load(
        filename,
        hdf5_keys=["GInit", "G", "y", "yTilde", "YTilde", "w0", "theta"])
    g = GInit.copy()
    gPrime = np.asarray(g[:].T)[0]
    log_posterior = optimize.log_weights.bioen_log_posterior(gPrime,
                                                             g,
                                                             G,
                                                             yTilde,
                                                             YTilde,
                                                             theta,
                                                             use_c=use_c)
    return log_posterior
示例#6
0
def run_test_error_opt_logw(file_name=filenames[0],
                            library='scipy/py',
                            caching=False):
    print("=" * 80)

    if (create_reference_values):
        os.environ["OMP_NUM_THREADS"] = "1"

    if "OMP_NUM_THREADS" in os.environ:
        print("OPENMP NUM. THREADS = ", os.environ["OMP_NUM_THREADS"])

    exp = available_tests()

    # load exp. data from file
    [GInit, G, y, yTilde, YTilde, w0, theta] = fio.load(
        file_name,
        hdf5_keys=["GInit", "G", "y", "yTilde", "YTilde", "w0", "theta"])

    # run all available optimizations
    for minimizer in exp:
        for algorithm in exp[minimizer]:

            use_c_functions = True

            params = optimize.minimize.Parameters(minimizer)
            params['cache_ytilde_transposed'] = caching
            params['use_c_functions'] = use_c_functions
            params['algorithm'] = algorithm
            params['verbose'] = verbose

            if params['minimizer'] == "gsl":
                #  Force an error by defining a wrong parameter
                #params['params']['step_size'] = -1.00001
                params['algorithm'] = "TEST_INVALID"
            elif params['minimizer'] == "lbfgs":
                #  Force an error by defining a wrong parameter
                params['params']['delta'] = -1.

            if verbose:
                print("-" * 80)
                print(params)

            with pytest.raises(RuntimeError) as excinfo:
                # run optimization
                wopt, yopt, gopt, fmin_ini, fmin_fin =  \
                    optimize.log_weights.find_optimum(GInit, G, y, yTilde, YTilde, theta, params)

            print(excinfo.value)
            assert ('return code' in str(excinfo.value))
示例#7
0
def test_dump_load_dict(temp_hdf5_file_name):
    filename = temp_hdf5_file_name

    mynested = {"var1": "a_string", "var2": 32, "var3": [2, 3, 4]}
    mydict = {"label": "value", "nested": mynested}
    fio.dump(filename, mydict)

    mydict = fio.load(filename, hdf5_deep_mode=True)
    assert (mydict["label"] == 'value')
    assert (mydict["nested"]["var1"] == "a_string")
    assert (mydict["nested"]["var2"] == 32)
    assert (mydict["nested"]["var3"][0] == 2)
    assert (mydict["nested"]["var3"][1] == 3)
    assert (mydict["nested"]["var3"][2] == 4)
    os.remove(filename)
示例#8
0
def run_test_error_forces(file_name=filenames[0], caching=False):

    print("=" * 80)

    if "OMP_NUM_THREADS" in os.environ:
        print(" OPENMP NUM. THREADS = ", os.environ["OMP_NUM_THREADS"])

    exp = available_tests()

    [forces_init, w0, y, yTilde, YTilde, theta] = fio.load(
        file_name,
        hdf5_keys=["forces_init", "w0", "y", "yTilde", "YTilde", "theta"])

    for minimizer in exp:
        for algorithm in exp[minimizer]:

            params = optimize.minimize.Parameters(minimizer)
            params['cache_ytilde_transposed'] = caching
            params['use_c_functions'] = True
            params['algorithm'] = algorithm
            params['verbose'] = True

            if params['minimizer'] == "gsl":
                #  Force an error by defining a wrong parameter
                params['algorithm'] = "TEST_INVALID"
                #params['params']['step_size'] = -1.00001

                # print (params['params'])
            elif params['minimizer'] == "lbfgs":
                #  Force an error by defining a wrong parameter
                params['params']['delta'] = -1.

            print("-" * 80)
            print("", params)

            with pytest.raises(RuntimeError) as excinfo:
                wopt, yopt, forces, fmin_ini, fmin_fin, chiSqr, S = \
                    optimize.forces.find_optimum(forces_init, w0, y, yTilde, YTilde, theta, params)

            print(excinfo.value)
            assert ('return code' in str(excinfo.value))
def run_grad_forces(use_c=True):
    [forces_init, w0, y, yTilde, YTilde, theta] = fio.load(filename,
        hdf5_keys=["forces_init", "w0", "y", "yTilde", "YTilde", "theta"])
    fprime = optimize.forces.grad_bioen_log_posterior(forces_init, w0, y, yTilde, YTilde, theta, use_c)
    return fprime
def run_test_optimum_logw(file_name=filenames[0], library='scipy/py', caching=False):
    print("=" * 80)

    if (create_reference_values):
        os.environ["OMP_NUM_THREADS"] = "1"

    if "OMP_NUM_THREADS" in os.environ:
        print("OPENMP NUM. THREADS = ", os.environ["OMP_NUM_THREADS"])

    exp = available_tests()

    # Run the optimizer for all the available tests
    for minimizer in exp:
        for algorithm in exp[minimizer]:
            [GInit, G, y, yTilde, YTilde, w0, theta] = fio.load(file_name,
                hdf5_keys=["GInit", "G", "y", "yTilde", "YTilde", "w0", "theta"])

            minimizer_tag = minimizer
            use_c_functions = True
            if (minimizer == 'scipy_py'):
                minimizer_tag = 'scipy'
                use_c_functions = False

            if (minimizer == 'scipy_c'):
                minimizer_tag = 'scipy'

            # get default parameter's configuration for a minimizer
            params = optimize.minimize.Parameters(minimizer_tag)

            params['cache_ytilde_transposed'] = caching
            params['use_c_functions'] = use_c_functions
            params['algorithm'] = algorithm
            params['verbose'] = verbose

            if verbose:
                print("-" * 80)
                print(params)

            # run optimization
            wopt, yopt, gopt, fmin_ini, fmin_fin =  \
                optimize.log_weights.find_optimum(GInit, G, y, yTilde, YTilde, theta, params)

            # store the results in the structure
            exp[minimizer][algorithm]['wopt'] = wopt
            exp[minimizer][algorithm]['yopt'] = yopt
            exp[minimizer][algorithm]['gopt'] = gopt
            exp[minimizer][algorithm]['fmin_ini'] = fmin_ini
            exp[minimizer][algorithm]['fmin_fin'] = fmin_fin



    if (create_reference_values):
        print("-" * 80)

        for minimizer in exp:
            for algorithm in exp[minimizer]:
                fmin_fin = exp[minimizer][algorithm]['fmin_fin']

                print(" === CREATING REFERENCE VALUES ===")
                ref_file_name = os.path.splitext(file_name)[0] + ".ref.h5"
                fio.dump(ref_file_name, fmin_min, "reference")
                print(" [%8s][%4s] -- fmin: %.16f --> %s" % (minimizer, algorithm, fmin_fin, ref_file_name))
                print("=" * 34, " END TEST ", "=" * 34)
                print("%" * 80)
    else:

        ref_file_name = os.path.splitext(file_name)[0] + ".ref.h5"
        available_reference = False
        if (os.path.isfile(ref_file_name)):
            available_reference = True
            x = fio.load(ref_file_name, hdf5_deep_mode=True)
            fmin_reference = x["reference"]

        # print results
        print("=" * 80)
        print(" [LOGW] CHECK RESULTS for file [ %s ] -- caching(%s)" % (file_name, caching))

        if (available_reference):
            print("-" * 80)
            print(" === REFERENCE EVALUATION ===")

            for minimizer in exp:
                for algorithm in exp[minimizer]:
                    fmin_fin = exp[minimizer][algorithm]['fmin_fin']

                    eval_diff = optimize.util.compute_relative_difference_for_values(fmin_fin, fmin_reference)
                    print(" [%8s][%16s] -- fmin: %.16f -- fmin_reference   : %.16f  -- diff(tol=%1.0e) = %.16f" % (
                        minimizer, algorithm, fmin_fin, fmin_reference, tol_min, eval_diff))


            for minimizer in exp:
                for algorithm in exp[minimizer]:
                    fmin_fin = exp[minimizer][algorithm]['fmin_fin']
                    eval_diff = optimize.util.compute_relative_difference_for_values(fmin_fin, fmin_reference)
                    assert(np.all(eval_diff < tol_min))

        else:
            print("-" * 80)
            print(" === REFERENCE EVALUATION === ")
            print(" [Reference not found]  To re-generate reference values enable create_reference_values=True and relaunch")
            assert(False)

        print("-" * 80)
        print(" === RETURNED GRADIENT EVALUATION ===")

        # re-evaluation of minimum for the returned vector
        [GInit, G, y, yTilde, YTilde, w0, theta] = fio.load(file_name,
            hdf5_keys=["GInit", "G", "y", "yTilde", "YTilde", "w0", "theta"])

        for minimizer in exp:
            for algorithm in exp[minimizer]:
                gopt = exp[minimizer][algorithm]['gopt']
                exp[minimizer][algorithm]['re_fmin'] = \
                    optimize.log_weights.bioen_log_posterior(gopt, GInit, G, yTilde, YTilde, theta, use_c=True)


        # print differences
        for minimizer in exp:
            for algorithm in exp[minimizer]:
                re_fmin = exp[minimizer][algorithm]['re_fmin']
                fmin_fin = exp[minimizer][algorithm]['fmin_fin']
                eval_diff = optimize.util.compute_relative_difference_for_values(fmin_fin, re_fmin)
                print(" [%8s][%16s] -- fmin: %.16f -- fmin_for_gradient: %.16f  -- diff(tol=%1.0e) = %.16f" % (
                    minimizer, algorithm, fmin_fin, re_fmin, tol, eval_diff))

        print("=" * 34, " END TEST ", "=" * 34)
        print("%" * 80)

        # validate differences
        for minimizer in exp:
            for algorithm in exp[minimizer]:
                re_fmin = exp[minimizer][algorithm]['re_fmin']
                fmin_fin = exp[minimizer][algorithm]['fmin_fin']

                eval_diff = optimize.util.compute_relative_difference_for_values(fmin_fin, re_fmin)
                assert(np.all(eval_diff < tol))
    return