Esempio n. 1
0
def setup_samples(N=500, calc_second_order=True):
    param_file = 'src/SALib/test_functions/params/Ishigami.txt'
    problem = read_param_file(param_file)
    param_values = saltelli.sample(problem,
                                   N=N,
                                   calc_second_order=calc_second_order)
    return problem, param_values
Esempio n. 2
0
def test_optimised_trajectories_with_groups():
    """
    Tests that the optimisation problem gives
    the same answer as the brute force problem
    (for small values of `k_choices` and `N`)
    with groups
    """

    N = 11
    param_file = "SALib/tests/test_param_file_w_groups_prime.txt"
    problem = read_param_file(param_file)
    num_levels = 4
    grid_jump = num_levels / 2
    k_choices = 4

    num_params = problem['num_vars']
    groups = problem['groups']

    input_sample = sample_groups(problem, N, num_levels, grid_jump)

    # From gurobi optimal trajectories
    actual = return_max_combo(input_sample, N, num_params, k_choices, groups)

    desired = find_optimum_combination(input_sample, N, num_params, k_choices,
                                       groups)
    assert_equal(actual, desired)
Esempio n. 3
0
    def test_regression_morris_groups_brute_optim(self, set_seed):

        set_seed
        param_file = 'src/SALib/test_functions/params/Ishigami_groups.txt'
        problem = read_param_file(param_file)

        param_values = sample(problem=problem,
                              N=50,
                              num_levels=4,
                              optimal_trajectories=6,
                              local_optimization=False)

        Y = Ishigami.evaluate(param_values)

        Si = morris.analyze(problem,
                            param_values,
                            Y,
                            conf_level=0.95,
                            print_to_console=False,
                            num_levels=4)

        assert_allclose(Si['mu'], [9.786986, np.NaN], atol=0, rtol=1e-5)

        assert_allclose(Si['sigma'], [6.453729, np.NaN], atol=0, rtol=1e-5)

        assert_allclose(Si['mu_star'], [9.786986, 7.875], atol=0, rtol=1e-5)
Esempio n. 4
0
def plot_morris_by_leaf_by_boot(df_out, variable='normalized_audpc',
                                parameter_range_file='param_range_SA.txt',
                                input_file='morris_input.txt',
                                nboots=5, ylims=None):
    problem = read_param_file(parameter_range_file)
    fig, axs = plt.subplots(6, 2, figsize=(15, 30))
    colors = iter(['b', 'g', 'r', 'k', 'm', 'c', 'y'])
    colors = {b: next(colors) for b in range(nboots)}
    for i, ax in enumerate(axs.flat):
        lf = i + 1
        for boot in np.unique(df_out['i_boot']):
            param_values = np.loadtxt(input_file[:-4] + '_boot' + str(boot) + input_file[-4:])
            df = df_out[(df_out['i_boot'] == boot) & (df_out['num_leaf_top'] == lf)]
            Y = df[variable]
            Si_bt = morris.analyze(problem, param_values, Y, grid_jump=5,
                                   num_levels=10, conf_level=0.95,
                                   print_to_console=False)
            for ip, param in enumerate(Si_bt['names']):
                ax.plot(Si_bt['mu_star'][ip], Si_bt['sigma'][ip],
                        color=colors[boot], marker='*')
                ax.annotate(param, (Si_bt['mu_star'][ip], Si_bt['sigma'][ip]))
        if ylims is not None:
            ax.set_ylim(ylims)
            ax.set_xlim(ylims)
        ax.annotate('Leaf %d' % lf, xy=(0.05, 0.85),
                    xycoords='axes fraction', fontsize=18)
def test_optimised_trajectories_with_groups():
    """
    Tests that the optimisation problem gives
    the same answer as the brute force problem
    (for small values of `k_choices` and `N`)
    with groups
    """
    
    N = 11
    param_file = "SALib/tests/test_param_file_w_groups_prime.txt"
    problem = read_param_file(param_file)
    num_levels = 4
    grid_jump = num_levels / 2
    k_choices = 4
    
    num_params = problem['num_vars']
    groups = problem['groups']

    input_sample = sample_groups(problem, N, num_levels, grid_jump)

    # From gurobi optimal trajectories     
    actual = return_max_combo(input_sample,
                              N,
                              num_params,
                              k_choices,
                              groups)

    desired = find_optimum_combination(input_sample,
                                       N,
                                       num_params,
                                       k_choices,
                                       groups)
    assert_equal(actual, desired)
def test_optimal_sample_with_groups():
    '''
    Tests that the combinatorial optimisation approach matches
    that of the brute force approach
    '''
    param_file = "SALib/tests/test_param_file_w_groups_prime.txt"
    problem = read_param_file(param_file)

    N = 10
    num_levels = 8
    grid_jump = 4
    k_choices = 4    
    num_params = problem['num_vars']
    
    sample = sample_oat(problem,
                        N,
                        num_levels,
                        grid_jump)

    actual = return_max_combo(sample,
                              N,
                              num_params,
                              k_choices)

    desired = find_optimum_combination(sample,
                                        N,
                                        num_params,
                                        k_choices)

    assert_equal(actual, desired)
Esempio n. 7
0
def generate_spectra(sample_number=10000,
                     bounds='../assets/prosail_param_bounds.txt',
                     save_to_npy=False,
                     spectra_save='../data/spectra.npy',
                     params_save='../data/params.npy',
                     params_norm_save='../data/params_norm.npy'):
    param_dimension = 15
    wavelength_start = 400
    wavelength_end = 2500
    wavelength_num = wavelength_end - wavelength_start + 1

    problem = read_param_file(bounds)
    params = saltelli.sample(problem, sample_number)
    params_norm = params.copy()

    num = sample_number * (param_dimension + 1) * 2
    cores = multiprocessing.cpu_count()
    pool = multiprocessing.Pool(processes=cores)
    spec = pool.map(generate_spectrum, params)
    spec = np.array(spec).reshape(num, 2101)

    for i in range(num):
        p = params[i]
        for j in range(param_dimension):
            params_norm[i][j] = (p[j] - problem['bounds'][j][0]) / (
                problem['bounds'][j][1] - problem['bounds'][j][0])

    if save_to_npy:
        np.save(spectra_save, spec)
        np.save(params_save, params)

    return spec, params, params_norm
Esempio n. 8
0
def test_optimised_trajectories_groups(setup_param_groups_prime):
    """
    Tests that the optimisation problem gives
    the same answer as the brute force problem
    (for small values of `k_choices` and `N`)
    with groups
    """

    N = 11
    param_file = setup_param_groups_prime
    problem = read_param_file(param_file)
    num_levels = 4
    k_choices = 4

    num_params = problem['num_vars']
    groups = compute_groups_matrix(problem['groups'], num_params)
    input_sample = _sample_groups(problem, N, num_levels)

    # From gurobi optimal trajectories
    strategy = GlobalOptimisation()
    actual = strategy.sample(input_sample,
                             N,
                             num_params,
                             k_choices,
                             groups)

    brute_strategy = BruteForce()
    desired = brute_strategy.sample(input_sample,
                                    N,
                                    num_params,
                                    k_choices,
                                    groups)
    assert_equal(actual, desired)
Esempio n. 9
0
    def test_local_optimised_groups(self,
                                    setup_param_groups_prime):
        """
        Tests that the local optimisation problem gives
        the same answer as the brute force problem
        (for small values of `k_choices` and `N`)
        with groups
        """
        N = 8
        param_file = setup_param_groups_prime
        problem = read_param_file(param_file)
        num_levels = 4
        grid_jump = num_levels / 2
        k_choices = 4

        num_params = problem['num_vars']

        num_groups = len(set(problem['groups']))

        input_sample = _sample_groups(problem, N, num_levels, grid_jump)

        strategy = LocalOptimisation()

        # From local optimal trajectories
        actual = strategy.find_local_maximum(input_sample, N, num_params,
                                             k_choices, num_groups)

        brute = BruteForce()
        desired = brute.brute_force_most_distant(input_sample,
                                                 N,
                                                 num_params,
                                                 k_choices,
                                                 num_groups)
        assert_equal(actual, desired)
Esempio n. 10
0
def test_optimal_sample_with_groups(setup_param_groups_prime):
    '''
    Tests that the combinatorial optimisation approach matches
    that of the brute force approach
    '''
    param_file = setup_param_groups_prime
    problem = read_param_file(param_file)

    N = 10
    num_levels = 8
    k_choices = 4
    num_params = problem['num_vars']

    sample = _sample_oat(problem,
                         N,
                         num_levels)

    strategy = GlobalOptimisation()
    actual = strategy.return_max_combo(sample,
                                       N,
                                       num_params,
                                       k_choices)

    brute_strategy = BruteForce()
    desired = brute_strategy.brute_force_most_distant(sample,
                                                      N,
                                                      num_params,
                                                      k_choices)

    assert_equal(actual, desired)
Esempio n. 11
0
    def test_regression_morris_optimal(self, set_seed):
        '''
        Tests the use of optimal trajectories with Morris.

        Uses brute force approach

        Note that the relative tolerance is set to a very high value
        (default is 1e-05) due to the coarse nature of the num_levels.
        '''
        set_seed
        param_file = 'src/SALib/test_functions/params/Ishigami.txt'
        problem = read_param_file(param_file)
        param_values = sample(problem=problem, N=20,
                              num_levels=4,
                              optimal_trajectories=9,
                              local_optimization=True)

        Y = Ishigami.evaluate(param_values)

        Si = morris.analyze(problem, param_values, Y,
                            conf_level=0.95, print_to_console=False,
                            num_levels=4)

        assert_allclose(Si['mu_star'],
                        [9.786986e+00, 7.875000e+00, 1.388621],
                        atol=0,
                        rtol=1e-5)
Esempio n. 12
0
def plot_morris_3_leaves(df_out, leaves=[10, 5, 1],
                         variable='normalized_audpc',
                         parameter_range_file='param_range_SA.txt',
                         input_file='morris_input.txt',
                         nboots=5, ylims=None,
                         force_rename={}, axs=None,
                         annotation_suffix='',
                         folder='septoria', markers_SA={},
                         markersize=8, title=None):
    plt.rcParams['text.usetex'] = True
    problem = read_param_file('./' + folder + '/' + parameter_range_file)
    if axs is None:
        fig, axs = plt.subplots(1, 3, figsize=(18, 6))
    leaves = iter(leaves)
    for i, ax in enumerate(axs.flat):
        lf = leaves.next()
        if ax == axs[-1]:
            add_legend = True
        else:
            add_legend = False
        plot_morris_one_leaf(df_out, problem,
                             leaf=lf, ax=ax,
                             variable=variable,
                             input_file=input_file,
                             nboots=nboots, ylims=ylims,
                             force_rename=force_rename,
                             markers_SA=markers_SA,
                             add_legend=add_legend,
                             annotation_suffix=annotation_suffix,
                             folder=folder, markersize=markersize)
    # plt.tight_layout()
    plt.rcParams['text.usetex'] = True
    if title is not None:
        fig.savefig(title, bbox_inches='tight')
Esempio n. 13
0
    def test_regression_morris_optimal(self, set_seed):
        '''
        Tests the use of optimal trajectories with Morris.

        Uses brute force approach

        Note that the relative tolerance is set to a very high value
        (default is 1e-05) due to the coarse nature of the num_levels.
        '''
        set_seed
        param_file = 'src/SALib/test_functions/params/Ishigami.txt'
        problem = read_param_file(param_file)
        param_values = sample(problem=problem,
                              N=20,
                              num_levels=4,
                              optimal_trajectories=9,
                              local_optimization=True)

        Y = Ishigami.evaluate(param_values)

        Si = morris.analyze(problem,
                            param_values,
                            Y,
                            conf_level=0.95,
                            print_to_console=False,
                            num_levels=4)

        assert_allclose(Si['mu_star'], [9.786986e+00, 7.875000e+00, 1.388621],
                        atol=0,
                        rtol=1e-5)
Esempio n. 14
0
    def test_regression_morris_groups_brute_optim(self, set_seed):

        set_seed
        param_file = 'src/SALib/test_functions/params/Ishigami_groups.txt'
        problem = read_param_file(param_file)

        param_values = sample(problem=problem, N=50,
                              num_levels=4,
                              optimal_trajectories=6,
                              local_optimization=False)

        Y = Ishigami.evaluate(param_values)

        Si = morris.analyze(problem, param_values, Y,
                            conf_level=0.95, print_to_console=False,
                            num_levels=4)

        assert_allclose(Si['mu'], [9.786986, np.NaN],
                        atol=0, rtol=1e-5)

        assert_allclose(Si['sigma'], [6.453729, np.NaN],
                        atol=0, rtol=1e-5)

        assert_allclose(Si['mu_star'], [9.786986, 7.875],
                        atol=0, rtol=1e-5)
Esempio n. 15
0
def plot_morris_by_leaf(df_out, variable='normalized_audpc',
                        parameter_range_file='param_range_SA.txt',
                        input_file='morris_input.txt',
                        nboots=5, ylims=None, force_rename={},
                        markers_SA={}, annotation_suffix='',
                        folder='septoria'):
    plt.rcParams['text.usetex'] = True
    problem = read_param_file(parameter_range_file)
    fig, axs = plt.subplots(6, 2, figsize=(15, 30))
    for i, ax in enumerate(axs.flat):
        lf = i + 1
        if ax == axs[0][-1]:
            add_legend = True
        else:
            add_legend = False
        plot_morris_one_leaf(df_out, problem,
                             leaf=lf, ax=ax,
                             variable=variable,
                             input_file=input_file,
                             nboots=nboots, ylims=ylims,
                             force_rename=force_rename,
                             markers_SA=markers_SA,
                             add_legend=add_legend,
                             annotation_suffix=annotation_suffix,
                             folder=folder)
    plt.rcParams['text.usetex'] = False
Esempio n. 16
0
def plot_morris_by_leaf(df_out,
                        variable='normalized_audpc',
                        parameter_range_file='param_range_SA.txt',
                        input_file='morris_input.txt',
                        nboots=5,
                        ylims=None,
                        force_rename={},
                        markers_SA={},
                        annotation_suffix='',
                        folder='septoria'):
    plt.rcParams['text.usetex'] = True
    problem = read_param_file(parameter_range_file)
    fig, axs = plt.subplots(6, 2, figsize=(15, 30))
    for i, ax in enumerate(axs.flat):
        lf = i + 1
        if ax == axs[0][-1]:
            add_legend = True
        else:
            add_legend = False
        plot_morris_one_leaf(df_out,
                             problem,
                             leaf=lf,
                             ax=ax,
                             variable=variable,
                             input_file=input_file,
                             nboots=nboots,
                             ylims=ylims,
                             force_rename=force_rename,
                             markers_SA=markers_SA,
                             add_legend=add_legend,
                             annotation_suffix=annotation_suffix,
                             folder=folder)
    plt.rcParams['text.usetex'] = False
Esempio n. 17
0
def generate_parameter_set(parameters,
                           scenarios,
                           parameter_range_file='param_range_SA.txt',
                           sample_file='morris_input.txt',
                           num_trajectories=10,
                           num_levels=10,
                           grid_jump=5,
                           optimal_trajectories=None,
                           nboots=5):
    """ Generate the file with samples for the analysis of Morris.

    Parameters
    ----------
    parameters: OrderedDict([('name', [min, max])])
        Names and variation range of quantitative parameters
    scenarios: list[tuple(year, variety)]
        Years and varieties of wheat on which SA is repeated
    """
    # Reset parameter range file
    open(parameter_range_file, 'w').close()
    generate_parameter_range_file(parameters=parameters,
                                  filename=parameter_range_file)

    # Generate samples
    problem = read_param_file(parameter_range_file)
    for boot in range(nboots):
        param_values = sample(problem,
                              N=num_trajectories,
                              num_levels=num_levels,
                              grid_jump=grid_jump,
                              optimal_trajectories=optimal_trajectories)

        # For Method of Morris, save the parameter values in a file (they are needed in the analysis)
        s_file = sample_file[:-4] + '_boot' + str(boot) + sample_file[-4:]
        np.savetxt(s_file, param_values, delimiter=' ')

        # Repeat samples for scenario
        full_params = []
        for scen in scenarios:
            for i_set, param_set in enumerate(param_values):
                full_params += [np.insert(param_set, 0, scen).tolist()]

                # Add boot number
        full_params = [
            np.insert(param_set, 0, boot).tolist()
            for i_sample, param_set in enumerate(full_params)
        ]

        # Add indices of sample
        full_params = [
            np.insert(param_set, 0, i_sample).tolist()
            for i_sample, param_set in enumerate(full_params)
        ]

        # Save full parameter values
        np.savetxt(s_file[:-4] + '_full' + s_file[-4:],
                   full_params,
                   delimiter=' ')
Esempio n. 18
0
def test_regression_rbd_fast():
    param_file = 'src/SALib/test_functions/params/Ishigami.txt'
    problem = read_param_file(param_file)
    param_values = latin.sample(problem, 10000)

    Y = Ishigami.evaluate(param_values)

    Si = rbd_fast.analyze(problem, param_values, Y, print_to_console=False)
    assert_allclose(Si['S1'], [0.31, 0.44, 0.00], atol=5e-2, rtol=1e-1)
Esempio n. 19
0
def test_optimal_trajectories_lt_samples(setup_param_file):
    parameter_file = setup_param_file
    problem = read_param_file(parameter_file)

    samples = 10
    num_levels = 4

    with raises(ValueError):
        sample(problem, samples, num_levels, optimal_trajectories=samples)
Esempio n. 20
0
def test_regression_rbd_fast():
    param_file = 'src/SALib/test_functions/params/Ishigami.txt'
    problem = read_param_file(param_file)
    param_values = latin.sample(problem, 10000)

    Y = Ishigami.evaluate(param_values)

    Si = rbd_fast.analyze(problem, param_values, Y, print_to_console=False)
    assert_allclose(Si['S1'], [0.31, 0.44, 0.00], atol=5e-2, rtol=1e-1)
Esempio n. 21
0
def cli_action(args):
    rd.seed(args.seed)

    problem = read_param_file(args.paramfile)
    param_values = sample(problem, args.samples, args.levels,
                          args.k_optimal, args.local)

    np.savetxt(args.output, param_values, delimiter=args.delimiter,
               fmt='%.' + str(args.precision) + 'e')
Esempio n. 22
0
def cli_action(args):
    problem = read_param_file(args.paramfile)
    Y = np.loadtxt(args.model_output_file,
                   delimiter=args.delimiter, usecols=(args.column,))
    X = np.loadtxt(args.model_input_file, delimiter=args.delimiter, ndmin=2)
    if len(X.shape) == 1:
        X = X.reshape((len(X), 1))
    analyze(problem, X, Y, (args.max_order == 2), print_to_console=True,
            seed=args.seed)
Esempio n. 23
0
def sobol_index(input_filename, N, B):
    import sys
    import numpy as np
    import time

    sys.path.append('./SALib-master')

    from SALib.analyze import sobol
    from SALib.util import read_param_file
    start = time.time()
    Y_names = ['NPV_Abat.txt', 'NPV_Dam.txt']

    #Load in data
    filename = "SALib/%s" % input_filename
    problem = read_param_file(filename, ' ')

    S1 = np.zeros((29, 2))
    S1_conf = np.zeros((29, 2))
    S2 = np.zeros((29, 29, 2))
    S2_conf = np.zeros((29, 29, 2))
    ST = np.zeros((29, 2))
    ST_conf = np.zeros((29, 2))

    cnt = 0

    for i in range(2):
        filename = Y_names[i]
        Y = np.loadtxt(filename)
        Y = Y[:, 17]  #2200
        Si = sobol.analyze(problem,
                           Y,
                           calc_second_order=True,
                           conf_level=0.95,
                           num_resamples=B,
                           print_to_console=False,
                           parallel=True,
                           n_processors=15)

        S1[:, cnt] = Si.get("S1")
        S1_conf[:, cnt] = Si.get("S1_conf")
        S2[:, :, cnt] = Si.get("S2")
        S2_conf[:, :, cnt] = Si.get("S2_conf")
        ST[:, cnt] = Si.get("ST")
        ST_conf[:, cnt] = Si.get("ST_conf")
        cnt = cnt + 1

    filename = "Dam_%i.txt" % N
    np.savez(filename,
             S1=S1,
             S1_conf=S1_conf,
             S2=S2,
             S2_conf=S2_conf,
             ST=ST,
             ST_conf=ST_conf)
    end = time.time()
    print(end - start)
Esempio n. 24
0
def test_even_num_levels_no_warning(setup_param_file_with_groups):
    parameter_file = setup_param_file_with_groups
    problem = read_param_file(parameter_file)
    with warnings.catch_warnings(record=True) as w:
        # Cause all warnings to always be triggered.
        warnings.simplefilter("always")
        # Trigger a warning.
        sample(problem, 10, num_levels=4)
        # Verify some things
        assert len(w) == 0
Esempio n. 25
0
def test_optimal_trajectories_lt_samples():

    parameter_file = setup_param_file()
    problem = read_param_file(parameter_file)

    samples = 10
    num_levels = 4
    grid_jump = 2

    sample(problem, samples, num_levels, grid_jump, \
           optimal_trajectories=samples)
Esempio n. 26
0
def test_tab_readfile_whitespace(setup_tab_param_file_espace_names):
    '''
    A tab delimited parameter file with whitespace in the names
    '''

    filename = setup_tab_param_file_espace_names
    pf = read_param_file(filename)

    assert_equal(pf['bounds'], [[0, 100], [5, 51]])
    assert_equal(pf['num_vars'], 2)
    assert_equal(pf['names'], ['Test 1', 'Test 2'])
Esempio n. 27
0
def test_optimal_trajectories_lt_samples(setup_param_file):

    parameter_file = setup_param_file
    problem = read_param_file(parameter_file)

    samples = 10
    num_levels = 4

    with raises(ValueError):
        sample(problem, samples, num_levels,
               optimal_trajectories=samples)
Esempio n. 28
0
def test_readfile_group_dist(setup_param_file_group_dist):
    '''
    Tests a parameter file with groups and distributions is read correctly
    '''
    filename = setup_param_file_group_dist
    pf = read_param_file(filename)
    assert_equal(pf['bounds'], [[0, 100], [5, 51], [10, 1]])
    assert_equal(pf['num_vars'], 3)
    assert_equal(pf['names'], ['Test1', 'Test2', 'Test3'])
    assert_equal(pf['groups'], ['Group1', 'Group1', 'Group2'])
    assert_equal(pf['dists'], ['unif', 'triang', 'norm'])
Esempio n. 29
0
def test_readfile(setup_function):
    '''
    Tests a standard parameter file is read correctly
    '''

    filename = setup_function
    pf = read_param_file(filename)

    assert_equal(pf['bounds'], [[0, 100], [5, 51]])
    assert_equal(pf['num_vars'], 2)
    assert_equal(pf['names'], ['Test1', 'Test2'])
def test_optimised_trajectories_without_groups():
    """
    Tests that the optimisation problem gives
    the same answer as the brute force problem
    (for small values of `k_choices` and `N`),
    particularly when there are two or more identical
    trajectories
    """
    
    N = 6
    param_file = "SALib/tests/test_params.txt"
    problem = read_param_file(param_file)
    num_levels = 4
    k_choices = 4

    num_params = problem['num_vars']
    groups = problem['groups']

    # 6 trajectories, with 5th and 6th identical
    input_sample = np.array([[ 0.33333333,  0.66666667],
                             [ 1.          ,0.66666667],
                             [ 1.          ,0.        ],
                             [ 0.          ,0.33333333],
                             [ 0.          ,1.        ],
                             [ 0.66666667  ,1.        ],
                             [ 0.66666667  ,0.33333333],
                             [ 0.66666667  ,1.        ],
                             [ 0.          ,1.        ],
                             [ 0.66666667  ,1.        ],
                             [ 0.66666667  ,0.33333333],
                             [ 0.          ,0.33333333],
                             [ 1.          ,1.        ],
                             [ 1.          ,0.33333333],
                             [ 0.33333333  ,0.33333333],
                             [ 1.          ,1.        ],
                             [ 1.          ,0.33333333],
                             [ 0.33333333  ,0.33333333]], dtype=np.float32)

    print(input_sample)

    # From gurobi optimal trajectories     
    actual = return_max_combo(input_sample,
                              N,
                              num_params,
                              k_choices,
                              groups)

    desired = find_optimum_combination(input_sample,
                                       N,
                                       num_params,
                                       k_choices,
                                       groups)
    
    assert_equal(actual, desired)
Esempio n. 31
0
def test_group_in_param_file_read(setup_param_file_with_groups):
    '''
    Tests that groups in a parameter file are read correctly
    '''
    parameter_file = setup_param_file_with_groups
    problem = read_param_file(parameter_file)
    groups, group_names = compute_groups_matrix(problem['groups'])

    assert_equal(problem['names'], ["Test 1", "Test 2", "Test 3"])
    assert_equal(groups, np.array([[1, 0], [1, 0], [0, 1]], dtype=np.int))
    assert_equal(group_names, ['Group 1', 'Group 2'])
Esempio n. 32
0
def test_readfile(setup_function):
    '''
    Tests a standard parameter file is read correctly
    '''

    filename = setup_function
    pf = read_param_file(filename)

    assert_equal(pf['bounds'], [[0, 100], [5, 51]])
    assert_equal(pf['num_vars'], 2)
    assert_equal(pf['names'], ['Test1', 'Test2'])
Esempio n. 33
0
def test_readfile_group_dist(setup_param_file_group_dist):
    '''
    Tests a parameter file with groups and distributions is read correctly
    '''
    filename = setup_param_file_group_dist
    pf = read_param_file(filename)
    assert_equal(pf['bounds'], [[0, 100], [5, 51], [10, 1]])
    assert_equal(pf['num_vars'], 3)
    assert_equal(pf['names'], ['Test1', 'Test2', 'Test3'])
    assert_equal(pf['groups'], ['Group1', 'Group1', 'Group2'])
    assert_equal(pf['dists'], ['unif', 'triang', 'norm'])
Esempio n. 34
0
def test_tab_readfile_whitespace(setup_tab_param_file_espace_names):
    '''
    A tab delimited parameter file with whitespace in the names
    '''

    filename = setup_tab_param_file_espace_names
    pf = read_param_file(filename)

    assert_equal(pf['bounds'], [[0, 100], [5, 51]])
    assert_equal(pf['num_vars'], 2)
    assert_equal(pf['names'], ['Test 1', 'Test 2'])
Esempio n. 35
0
def test_regression_dgsm():
    param_file = 'src/SALib/test_functions/params/Ishigami.txt'
    problem = read_param_file(param_file)
    param_values = finite_diff.sample(problem, 10000, delta=0.001)

    Y = Ishigami.evaluate(param_values)

    Si = dgsm.analyze(problem, param_values, Y,
                      conf_level=0.95, print_to_console=False)

    assert_allclose(Si['dgsm'], [2.229, 7.066, 3.180], atol=5e-2, rtol=1e-1)
Esempio n. 36
0
def test_optimised_trajectories_without_groups(setup_function):
    """
    Tests that the optimisation problem gives
    the same answer as the brute force problem
    (for small values of `k_choices` and `N`),
    particularly when there are two or more identical
    trajectories
    """

    N = 6
    param_file = setup_function
    problem = read_param_file(param_file)
    k_choices = 4

    num_params = problem['num_vars']
    groups = problem['groups']

    # 6 trajectories, with 5th and 6th identical
    input_sample = np.array([[0.33333333,  0.66666667],
                             [1., 0.66666667],
                             [1., 0.],
                             [0., 0.33333333],
                             [0., 1.],
                             [0.66666667, 1.],
                             [0.66666667, 0.33333333],
                             [0.66666667, 1.],
                             [0., 1.],
                             [0.66666667, 1.],
                             [0.66666667, 0.33333333],
                             [0., 0.33333333],
                             [1., 1.],
                             [1., 0.33333333],
                             [0.33333333, 0.33333333],
                             [1., 1.],
                             [1., 0.33333333],
                             [0.33333333, 0.33333333]], dtype=np.float32)

    # print(input_sample)

    # From gurobi optimal trajectories
    strategy = GlobalOptimisation()
    actual = strategy.return_max_combo(input_sample,
                                       N,
                                       num_params,
                                       k_choices)

    local_strategy = BruteForce()
    desired = local_strategy.brute_force_most_distant(input_sample,
                                                      N,
                                                      num_params,
                                                      k_choices,
                                                      groups)

    assert_equal(actual, desired)
Esempio n. 37
0
def test_regression_dgsm():
    param_file = 'src/SALib/test_functions/params/Ishigami.txt'
    problem = read_param_file(param_file)
    param_values = finite_diff.sample(problem, 10000, delta=0.001)

    Y = Ishigami.evaluate(param_values)

    Si = dgsm.analyze(problem, param_values, Y,
                      conf_level=0.95, print_to_console=False)

    assert_allclose(Si['dgsm'], [2.229, 7.066, 3.180], atol=5e-2, rtol=1e-1)
Esempio n. 38
0
def test_csv_readfile_with_whitespace():
    '''
    A comma delimited parameter file with whitespace in the names
    '''

    filename = setup_csv_param_file_with_whitespace_in_names()
    pf = read_param_file(filename)

    assert_equal(pf['bounds'], [[0, 100], [5, 51]])
    assert_equal(pf['num_vars'], 2)
    assert_equal(pf['names'], ['Test 1', 'Test 2'])
Esempio n. 39
0
def test_optimal_trajectories_lt_10(setup_param_file):

    parameter_file = setup_param_file
    problem = read_param_file(parameter_file)

    samples = 10
    num_levels = 4
    grid_jump = 2
    optimal_trajectories = 11
    with raises(ValueError):
        sample(problem, samples, num_levels, grid_jump,
               optimal_trajectories=optimal_trajectories)
Esempio n. 40
0
def test_group_in_param_file_read(setup_param_file_with_groups):
    '''
    Tests that groups in a parameter file are read correctly
    '''
    parameter_file = setup_param_file_with_groups
    problem = read_param_file(parameter_file)
    groups, group_names = compute_groups_matrix(
        problem['groups'])

    assert_equal(problem['names'], ["Test 1", "Test 2", "Test 3"])
    assert_equal(groups, np.array([[1, 0], [1, 0], [0, 1]], dtype=np.int))
    assert_equal(group_names, ['Group 1', 'Group 2'])
Esempio n. 41
0
def test_regression_delta():
    param_file = 'src/SALib/test_functions/params/Ishigami.txt'
    problem = read_param_file(param_file)
    param_values = latin.sample(problem, 10000)

    Y = Ishigami.evaluate(param_values)

    Si = delta.analyze(problem, param_values, Y, num_resamples=10,
                       conf_level=0.95, print_to_console=True)

    assert_allclose(Si['delta'], [0.210, 0.358, 0.155], atol=5e-2, rtol=1e-1)
    assert_allclose(Si['S1'], [0.31, 0.44, 0.00], atol=5e-2, rtol=1e-1)
Esempio n. 42
0
def test_group_in_param_file_read():
    '''
    Tests that groups in a parameter file are read correctly
    '''
    parameter_file = setup_param_file_with_groups()
    problem = read_param_file(parameter_file)
    groups, group_names = compute_groups_matrix(problem['groups'],
                                                problem['num_vars'])

    assert_equal(problem['names'], ["Test 1", "Test 2", "Test 3"])
    assert_equal(groups, np.matrix('1,0;1,0;0,1', dtype=np.int))
    assert_equal(group_names, ['Group 1', 'Group 2'])
Esempio n. 43
0
def test_csv_readfile_with_whitespace(setup_csv_param_file_space):
    '''
    A comma delimited parameter file with whitespace in the names
    '''

    filename = setup_csv_param_file_space

    pf = read_param_file(filename)

    assert_equal(pf['bounds'], [[0, 100], [5, 51]])
    assert_equal(pf['num_vars'], 2)
    assert_equal(pf['names'], ['Test 1', 'Test 2'])
Esempio n. 44
0
def test_regression_delta():
    param_file = 'src/SALib/test_functions/params/Ishigami.txt'
    problem = read_param_file(param_file)
    param_values = latin.sample(problem, 10000)

    Y = Ishigami.evaluate(param_values)

    Si = delta.analyze(problem, param_values, Y, num_resamples=10,
                       conf_level=0.95, print_to_console=True)

    assert_allclose(Si['delta'], [0.210, 0.358, 0.155], atol=5e-2, rtol=1e-1)
    assert_allclose(Si['S1'], [0.31, 0.44, 0.00], atol=5e-2, rtol=1e-1)
Esempio n. 45
0
def test_regression_sobol_parallel():
    param_file = 'SALib/test_functions/params/Ishigami.txt'
    problem = read_param_file(param_file)
    param_values = saltelli.sample(problem, 10000, calc_second_order=True)

    Y = Ishigami.evaluate(param_values)

    Si = sobol.analyze(problem, Y,
                       calc_second_order=True, parallel=True, conf_level=0.95, print_to_console=False)

    assert_allclose(Si['S1'], [0.31, 0.44, 0.00], atol=5e-2, rtol=1e-1)
    assert_allclose(Si['ST'], [0.55, 0.44, 0.24], atol=5e-2, rtol=1e-1)
    assert_allclose([Si['S2'][0][1], Si['S2'][0][2], Si['S2'][1][2]], [0.00, 0.25, 0.00], atol=5e-2, rtol=1e-1)
Esempio n. 46
0
def test_csv_readfile_comments(setup_csv_param_file_space_comments):
    '''
    '''

    filename = setup_csv_param_file_space_comments

    pf = read_param_file(filename)

    print(pf['bounds'], pf['num_vars'], pf['names'])

    assert_equal(pf['bounds'], [[0, 100], [5, 51]])
    assert_equal(pf['num_vars'], 2)
    assert_equal(pf['names'], ['Test 1', 'Test 2'])
Esempio n. 47
0
def test_csv_readfile_comments(setup_csv_param_file_space_comments):
    '''
    '''

    filename = setup_csv_param_file_space_comments

    pf = read_param_file(filename)

    print(pf['bounds'], pf['num_vars'], pf['names'])

    assert_equal(pf['bounds'], [[0, 100], [5, 51]])
    assert_equal(pf['num_vars'], 2)
    assert_equal(pf['names'], ['Test 1', 'Test 2'])
Esempio n. 48
0
def test_odd_num_levels_raises_warning(setup_param_file_with_groups):
    parameter_file = setup_param_file_with_groups
    problem = read_param_file(parameter_file)
    with warnings.catch_warnings(record=True) as w:
        # Cause all warnings to always be triggered.
        warnings.simplefilter("always")
        # Trigger a warning.
        sample(problem, 10, num_levels=3)
        # Verify some things
        assert len(w) == 1
        assert issubclass(w[-1].category, UserWarning)
        assert "num_levels should be an even number, sample may be biased" in str(
            w[-1].message)
Esempio n. 49
0
def generate_parameter_set(parameters,
                           scenarios,
                           parameter_range_file='param_range_SA.txt',
                           sample_file='morris_input.txt',
                           num_trajectories=10,
                           num_levels=10,
                           grid_jump=5,
                           optimal_trajectories=None,
                           nboots=5):
    """ Generate the file with samples for the analysis of Morris.

    Parameters
    ----------
    parameters: OrderedDict([('name', [min, max])])
        Names and variation range of quantitative parameters
    scenarios: list[tuple(year, variety)]
        Years and varieties of wheat on which SA is repeated
    """
    # Reset parameter range file
    open(parameter_range_file, 'w').close()
    generate_parameter_range_file(parameters=parameters,
                                  filename=parameter_range_file)

    # Generate samples
    problem = read_param_file(parameter_range_file)
    for boot in range(nboots):
        param_values = sample(problem, N=num_trajectories,
                              num_levels=num_levels, grid_jump=grid_jump,
                              optimal_trajectories=optimal_trajectories)

        # For Method of Morris, save the parameter values in a file (they are needed in the analysis)
        s_file = sample_file[:-4] + '_boot' + str(boot) + sample_file[-4:]
        np.savetxt(s_file, param_values, delimiter=' ')

        # Repeat samples for scenario
        full_params = []
        for scen in scenarios:
            for i_set, param_set in enumerate(param_values):
                full_params += [np.insert(param_set, 0, scen).tolist()]

                # Add boot number
        full_params = [np.insert(param_set, 0, boot).tolist()
                       for i_sample, param_set in enumerate(full_params)]

        # Add indices of sample
        full_params = [np.insert(param_set, 0, i_sample).tolist()
                       for i_sample, param_set in enumerate(full_params)]

        # Save full parameter values
        np.savetxt(s_file[:-4] + '_full' + s_file[-4:],
                   full_params, delimiter=' ')
Esempio n. 50
0
def test_regression_sobol_parallel():
    param_file = 'src/SALib/test_functions/params/Ishigami.txt'
    problem = read_param_file(param_file)
    param_values = saltelli.sample(problem, 10000, calc_second_order=True)

    Y = Ishigami.evaluate(param_values)

    Si = sobol.analyze(problem, Y,
                       calc_second_order=True, parallel=True,
                       conf_level=0.95, print_to_console=False)

    assert_allclose(Si['S1'], [0.31, 0.44, 0.00], atol=5e-2, rtol=1e-1)
    assert_allclose(Si['ST'], [0.55, 0.44, 0.24], atol=5e-2, rtol=1e-1)
    assert_allclose([Si['S2'][0][1], Si['S2'][0][2], Si['S2'][1][2]], [
                    0.00, 0.25, 0.00], atol=5e-2, rtol=1e-1)
Esempio n. 51
0
    def test_regression_morris_groups(self, set_seed):
        set_seed
        param_file = 'src/SALib/test_functions/params/Ishigami_groups.txt'
        problem = read_param_file(param_file)

        param_values = sample(problem=problem, N=10000,
                              num_levels=4,
                              optimal_trajectories=None)

        Y = Ishigami.evaluate(param_values)

        Si = morris.analyze(problem, param_values, Y,
                            conf_level=0.95, print_to_console=False,
                            num_levels=4)

        assert_allclose(Si['mu_star'], [7.610322, 10.197014],
                        atol=0, rtol=1e-5)
Esempio n. 52
0
def test_raise_error_if_k_gt_N(setup_function):
    """Check that an error is raised if `k_choices` is greater than
    (or equal to) `N`
    """
    N = 4
    param_file = setup_function
    problem = read_param_file(param_file)
    num_levels = 4
    k_choices = 6

    morris_sample = _sample_oat(problem, N, num_levels)

    with raises(ValueError):
        _compute_optimised_trajectories(problem,
                                        morris_sample,
                                        N,
                                        k_choices,
                                        local_optimization=False)
Esempio n. 53
0
    def test_regression_morris_vanilla(self, set_seed):
        """Note that this is a poor estimate of the Ishigami
        function.
        """
        set_seed
        param_file = 'src/SALib/test_functions/params/Ishigami.txt'
        problem = read_param_file(param_file)
        param_values = sample(problem, 10000, 4,
                              optimal_trajectories=None)

        Y = Ishigami.evaluate(param_values)

        Si = morris.analyze(problem, param_values, Y,
                            conf_level=0.95, print_to_console=False,
                            num_levels=4)

        assert_allclose(Si['mu_star'], [7.536586, 7.875, 6.308785],
                        atol=0, rtol=1e-5)
def test_raise_error_if_k_gt_N():
    """
    Check that an error is raised if `k_choices` is greater than (or equal to) `N`
    """
    N = 4
    param_file = "SALib/tests/test_params.txt"
    problem = read_param_file(param_file)
    num_levels = 4
    grid_jump = num_levels / 2
    k_choices = 6

    morris_sample = sample_oat(problem, N, num_levels, grid_jump)


    compute_optimised_trajectories(problem,
                                   morris_sample,
                                   N,
                                   k_choices)
Esempio n. 55
0
def Create_LHS_parameter_set(nsamples):

 from SALib.sample import latin_hypercube
 from SALib.util import scale_samples, read_param_file
 import random as rd

 # Set random seed (does not affect quasi-random Sobol sampling)
 seed = 1
 np.random.seed(seed)
 rd.seed(seed)

 #Define parameters and ranges
 parameters = ordereddict.OrderedDict()
 parameters['log10m'] = [np.log10(0.001),np.log10(0.1)]
 parameters['lnTe'] = [np.log(np.exp(-8.0)/3600.0),np.log(np.exp(8.0)/3600.0)]
 parameters['log10soil'] = [np.log10(1.0),np.log10(2.00)]
 parameters['sdmax'] = [0.1,2.0] #dtopmodel

 #Make directory
 if os.path.exists('LHS') == False:
  os.mkdir('LHS')

 #Prepare file with parameter range
 fp = open('LHS/parameters.txt','w')
 vars = []
 for var in parameters:
  vars.append(var)
  fp.write('%s %f %f\n' % (var,parameters[var][0],parameters[var][1]))
 fp.close()

 #Read the parameter range file and generate samples
 param_file = 'LHS/parameters.txt'
 pf = read_param_file(param_file)

 #Generate samples (choose method here)
 param_values = latin_hypercube.sample(nsamples, pf['num_vars'])

 #Samples are given in range [0, 1] by default. Rescale them to your parameter bounds.
 scale_samples(param_values, pf['bounds'])

 #Save parameters to file         
 np.savetxt('LHS/LHS_sampling.txt', param_values, delimiter=' ',header=" ".join(vars))

 return