def test_dgsm_to_df(): params = ['x1', 'x2', 'x3'] problem = { 'num_vars': 3, 'names': params, 'groups': None, 'bounds': [[-3.14159265359, 3.14159265359], [-3.14159265359, 3.14159265359], [-3.14159265359, 3.14159265359]] } param_values = finite_diff.sample(problem, 1000, delta=0.001) Y = Ishigami.evaluate(param_values) Si = dgsm.analyze(problem, param_values, Y, print_to_console=False) Si_df = Si.to_df() assert isinstance(Si_df, pd.DataFrame), \ "DGSM Si: Expected DataFrame, got {}".format(type(Si_df)) assert set(Si_df.index) == set(params), "Incorrect index in DataFrame" col_names = ['vi', 'vi_std', 'dgsm', 'dgsm_conf'] assert set(Si_df.columns) == set(col_names), \ "Unexpected column names in DataFrame"
def SALib(result_title, input_title, bounds): num_vars = len(bounds[1]) + 1 for i in range(len(result_title)): input_title.insert(0, result_title[i]) print(input_title) new_bounds = bounds[1] new_bounds.insert(0, bounds[0][i]) problem = { 'num_vars': num_vars, 'names': input_title, 'bounds': new_bounds } # Generate samples param_values = saltelli.sample(problem, 1000) # Run model (example) Y = Ishigami.evaluate(param_values) # Perform analysis _ = sobol.analyze(problem, Y, print_to_console=True) del(input_title[0]) del(new_bounds[0])
def test_sobol_to_df(): params = ['x1', 'x2', 'x3'] problem = {'num_vars': 3, 'names': params, 'bounds': [[-np.pi, np.pi]] * 3} X = saltelli.sample(problem, 1000) Y = Ishigami.evaluate(X) Si = sobol.analyze(problem, Y, print_to_console=False) total, first, second = Si.to_df() assert isinstance(total, pd.DataFrame), \ "Total Si: Expected DataFrame, got {}".format(type(total)) assert isinstance(first, pd.DataFrame), \ "First Si: Expected DataFrame, got {}".format(type(first)) assert isinstance(second, pd.DataFrame), \ "Second Si: Expected DataFrame, got {}".format(type(second)) expected_index = set(params) assert set(total.index) == expected_index, \ "Index for Total Si are incorrect" assert set(first.index) == expected_index, \ "Index for first order Si are incorrect" assert set(second.index) == set([('x1', 'x2'), ('x1', 'x3'), ('x2', 'x3')]), \ "Index for second order Si are incorrect"
def test_morris(): # Generate inputs cmd = "python {cli} sample morris -p {fn} -o model_input.txt -n 100\ --precision=8 --levels=10 --seed=100 -lo False"\ .format(cli=salib_cli, fn=ishigami_fp).split() subprocess.run(cmd) # Run model and save output np.savetxt('model_output.txt', Ishigami.evaluate( np.loadtxt('model_input.txt'))) # run analysis analyze_cmd = "python {cli} analyze morris -p {fn} -X model_input.txt\ -Y model_output.txt -c 0 -r 1000 -l 10 --seed=100"\ .format(cli=salib_cli, fn=ishigami_fp).split() result = subprocess.check_output(analyze_cmd, universal_newlines=True) result = re.sub(r'[\n\t\s]*', '', result) expected_output = """ParameterMu_StarMuMu_Star_ConfSigmax13.3753.3750.5903.003x21.4740.1180.0001.477x32.6980.4200.5954.020""" assert len(result) > 0 and result == expected_output, \ "Results did not match expected values:\n\n Expected: \n{} \n\n Got: \n{}".format( expected_output, result)
def test_fast_to_df(): params = ['x1', 'x2', 'x3'] problem = { 'num_vars': 3, 'names': params, 'groups': None, 'bounds': [[-3.14159265359, 3.14159265359], [-3.14159265359, 3.14159265359], [-3.14159265359, 3.14159265359]] } param_values = fast_sampler.sample(problem, 1000) Y = Ishigami.evaluate(param_values) Si = fast.analyze(problem, Y, print_to_console=False) Si_df = Si.to_df() expected_index = set(params) assert isinstance(Si_df, pd.DataFrame), \ "FAST Si: Expected DataFrame, got {}".format(type(Si_df)) assert set(Si_df.index) == expected_index, "Incorrect index in DataFrame" col_names = set(['S1', 'ST']) assert set(Si_df.columns) == col_names, \ "Unexpected column names in DataFrame. Expected {}, got {}".format( col_names, Si_df.columns)
def test_delta(): cmd = f"python {salib_cli} sample saltelli -p {ishigami_fp} -o {input_path} -n 1000 \ --precision 8 --max-order 2 --seed=100" subprocess.run(cmd.split()) # Run model and save output np.savetxt(output_path, Ishigami.evaluate(np.loadtxt(input_path))) analyze_cmd = f"python {salib_cli} analyze delta -p {ishigami_fp} -X {input_path} \ -Y {output_path} -c 0 -r 10 --seed=100".split() result = subprocess.check_output(analyze_cmd, universal_newlines=True) delta_expected = [0.210478, 0.354023, 0.160986] sobol_expected = [0.311362, 0.428365, 0.001111] test = pd.read_csv(StringIO(result), index_col=0, sep=r'\s+') test['expected'] = delta_expected lower = (test['delta'] - test['delta_conf']) upper = (test['delta'] + test['delta_conf']) comparison = test['expected'].between(lower, upper) assert comparison.all(), \ "Expected Delta results not within confidence bounds" test['expected'] = sobol_expected lower = (test['S1'] - test['S1_conf']) upper = (test['S1'] + test['S1_conf']) comparison = test['expected'].between(lower, upper) assert comparison.all(), \ "Expected Sobol results not within confidence bounds"
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)
def test_dgsm(): # Generate inputs cmd = f"python {salib_cli} sample finite_diff -p {ishigami_fp} -o {input_path} -d 0.001\ --precision=8 -n 1000 --seed=100".split() subprocess.run(cmd) # Run model and save output np.savetxt(output_path, Ishigami.evaluate(np.loadtxt(input_path))) analyze_cmd = f"python {salib_cli} analyze dgsm -p {ishigami_fp} -X {input_path}\ -Y {output_path} -c 0 -r 1000 --seed=100".split() # run analysis and use regex to strip all whitespace from result result = subprocess.check_output(analyze_cmd, universal_newlines=True) dgsm_expected = [2.207554, 7.092019, 3.238259] test = pd.read_csv(StringIO(result), index_col=0, sep=r'\s+') test['expected'] = dgsm_expected lower = (test['dgsm'] - test['dgsm_conf']) upper = (test['dgsm'] + test['dgsm_conf']) comparison = test['expected'].between(lower, upper) assert comparison.all(), \ "Expected DGSM results not within confidence bounds"
def test_rbd_fast(): # Generate inputs cmd = f"python {salib_cli} sample latin -p {ishigami_fp} -o {input_path} \ --precision=8 -n 1000 --seed=100".split() subprocess.run(cmd) # Run model and save output np.savetxt(output_path, Ishigami.evaluate(np.loadtxt(input_path))) analyze_cmd = f"python {salib_cli} analyze rbd_fast -p {ishigami_fp} -X {input_path}\ -Y {output_path} --seed=100".split() # run analysis and use regex to strip all whitespace from result result = subprocess.check_output(analyze_cmd, universal_newlines=True) expected = [0.351277, 0.468170, -0.005864] test = pd.read_csv(StringIO(result), index_col=0, sep=r'\s+') test['expected'] = expected lower = (test['S1'] - test['S1_conf']) upper = (test['S1'] + test['S1_conf']) comparison = test['expected'].between(lower, upper) assert comparison.all(), \ "RBD-FAST results +/- CI not in line with expected results."
def test_morris(): # Generate inputs cmd = "python {cli} sample morris -p {fn} -o model_input.txt -n 100\ --precision=8 --levels=10 --seed=100 -lo False"\ .format(cli=salib_cli, fn=ishigami_fp).split() subprocess.run(cmd) # Run model and save output np.savetxt('model_output.txt', Ishigami.evaluate( np.loadtxt('model_input.txt'))) # run analysis analyze_cmd = "python {cli} analyze morris -p {fn} -X model_input.txt\ -Y model_output.txt -c 0 -r 1000 -l 10 --seed=100"\ .format(cli=salib_cli, fn=ishigami_fp).split() result = subprocess.check_output(analyze_cmd, universal_newlines=True) result = re.sub(r'[\n\t\s]*', '', result) expected_output = """ParameterMu_StarMuMu_Star_ConfSigmax17.4997.4991.8019.330x22.215-0.4700.3482.776x35.4240.8641.1487.862""" assert len(result) > 0 and result == expected_output, \ "Results did not match expected values:\n\n Expected: \n{} \n\n Got: \n{}".format( expected_output, result)
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)
def test_sobol_to_df(): params = ['x1', 'x2', 'x3'] problem = { 'num_vars': 3, 'names': params, 'bounds': [[-np.pi, np.pi]]*3 } X = saltelli.sample(problem, 1000) Y = Ishigami.evaluate(X) Si = sobol.analyze(problem, Y, print_to_console=False) total, first, second = Si.to_df() assert isinstance(total, pd.DataFrame), \ "Total Si: Expected DataFrame, got {}".format(type(total)) assert isinstance(first, pd.DataFrame), \ "First Si: Expected DataFrame, got {}".format(type(first)) assert isinstance(second, pd.DataFrame), \ "Second Si: Expected DataFrame, got {}".format(type(second)) expected_index = set(params) assert set(total.index) == expected_index, \ "Index for Total Si are incorrect" assert set(first.index) == expected_index, \ "Index for first order Si are incorrect" assert set(second.index) == set([('x1', 'x2'), ('x1', 'x3'), ('x2', 'x3')]), \ "Index for second order Si are incorrect"
def evaluate(self, parameter_values: np.ndarray) -> np.ndarray: """ """ results = Ishigami.evaluate(parameter_values) return results
def test_parallel_second_order(): c2o = True N = 10000 problem, param_values = setup_samples(N=N, calc_second_order=c2o) Y = Ishigami.evaluate(param_values) A, B, AB, BA = sobol.separate_output_values(Y, D=3, N=N, calc_second_order=c2o) r = np.random.randint(N, size=(N, 100)) Z = norm.ppf(0.5 + 0.95 / 2) tasks, n_processors = sobol.create_task_list(D=3, calc_second_order=c2o, n_processors=None) Si_list = [] for t in tasks: Si_list.append(sobol.sobol_parallel(Z, A, AB, BA, B, r, t)) Si = sobol.Si_list_to_dict(Si_list, D=3, calc_second_order=c2o) 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)
def test_rbd_to_df(): params = ['x1', 'x2', 'x3'] problem = { 'num_vars': 3, 'names': params, 'groups': None, 'bounds': [[-3.14159265359, 3.14159265359], [-3.14159265359, 3.14159265359], [-3.14159265359, 3.14159265359]] } param_values = latin.sample(problem, 1000) Y = Ishigami.evaluate(param_values) Si = rbd_fast.analyze(problem, Y, param_values, print_to_console=False) Si_df = Si.to_df() assert isinstance(Si_df, pd.DataFrame), \ "RBD Si: Expected DataFrame, got {}".format(type(Si_df)) assert set(Si_df.index) == set(params), "Incorrect index in DataFrame" col_names = set(['S1']) assert set(Si_df.columns) == col_names, \ "Unexpected column names in DataFrame. Expected {}, got {}".format( col_names, Si_df.columns)
def test_include_print(): problem, param_values = setup_samples() Y = Ishigami.evaluate(param_values) sobol.analyze(problem, Y, calc_second_order=True, conf_level=0.95, print_to_console=True)
def test_bad_conf_level(): problem, param_values = setup_samples() Y = Ishigami.evaluate(param_values) with raises(RuntimeError): sobol.analyze(problem, Y, calc_second_order=True, conf_level=1.01, print_to_console=False)
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)
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)
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)
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)
def test_regression_morris_vanilla(): param_file = 'SALib/test_functions/params/Ishigami.txt' problem = read_param_file(param_file) param_values = sample(problem=problem, N=5000, \ num_levels=10, grid_jump=5, \ 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=10, grid_jump=5) assert_allclose(Si['mu_star'], [8.1, 2.2, 5.4], atol=0, rtol=5e-1)
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)
def test_regression_sobol_groups(): problem = { 'num_vars': 3, 'names': ['x1', 'x2', 'x3'], 'bounds': [[-np.pi, np.pi]]*3, 'groups': ['G1', 'G2', 'G1'] } 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.55, 0.44], atol=5e-2, rtol=1e-1) assert_allclose(Si['ST'], [0.55, 0.44], atol=5e-2, rtol=1e-1) assert_allclose(Si['S2'][0][1], [0.00], atol=5e-2, rtol=1e-1)
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)
def test_regression_sobol_groups(): problem = { 'num_vars': 3, 'names': ['x1', 'x2', 'x3'], 'bounds': [[-np.pi, np.pi]] * 3, 'groups': ['G1', 'G2', 'G1'] } 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.55, 0.44], atol=5e-2, rtol=1e-1) assert_allclose(Si['ST'], [0.55, 0.44], atol=5e-2, rtol=1e-1) assert_allclose(Si['S2'][0][1], [0.00], atol=5e-2, rtol=1e-1)
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_regression_morris_groups_local_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=500, num_levels=4, optimal_trajectories=20, 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'], [13.95285, 7.875], rtol=1e-5)
def test_set_model_factors(): np.random.seed(1) fac_1 = ModelInputFactor('1', -1, 1) fac_2 = ModelInputFactor('2', -1, 1) fac_3 = ModelInputFactor('3', -1, 1) container = list((fac_1, fac_2, fac_3)) input_factors_container = Container(container) # sample test_input_sample = lhc_sample(100, input_factors_container, seed=1) test_output_sample = Ishigami.evaluate(test_input_sample) num_levels = 4 delta = num_levels / (2.0 * (num_levels - 1)) params_number = test_input_sample.shape[1] trajectories_num = int(test_output_sample.size / (params_number + 1)) return input_factors_container, test_input_sample, test_output_sample, delta, trajectories_num
def test_parallel_first_order(): c2o = False N = 10000 problem,param_values = setup_samples(N=N, calc_second_order=c2o) Y = Ishigami.evaluate(param_values) A,B,AB,BA = sobol.separate_output_values(Y, D=3, N=N, calc_second_order=c2o) r = np.random.randint(N, size=(N, 100)) Z = norm.ppf(0.5 + 0.95 / 2) tasks, n_processors = sobol.create_task_list(D=3, calc_second_order=c2o, n_processors=None) Si_list = [] for t in tasks: Si_list.append(sobol.sobol_parallel(Z, A, AB, BA, B, r, t)) Si = sobol.Si_list_to_dict(Si_list, D=3, calc_second_order=c2o) 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)
def test_ff(): # Generate inputs cmd = f"python {salib_cli} sample ff -p {ishigami_fp} -o {input_path} \ --precision=8 -n 1000 --seed=100".split() subprocess.run(cmd) # Run model and save output np.savetxt(output_path, Ishigami.evaluate(np.loadtxt(input_path))) analyze_cmd = f"python {salib_cli} analyze ff -p {ishigami_fp} -X {input_path} \ -Y {output_path} -c 0 --seed=100".split() # run analysis and use regex to strip all whitespace from result result = subprocess.check_output(analyze_cmd, universal_newlines=True) result = re.sub(r'[\n\t\s]*', '', result) expected = "MEx13.855764e-08x20.000000e+00x30.000000e+00dummy_00.000000e+00IE(x1,x2)0.0(x1,x3)0.0(x2,x3)0.0(x1,dummy_0)0.0(x2,dummy_0)0.0(x3,dummy_0)0.0" assert len(result) > 0 and result == expected, \ f"Unexpected FF results.\n\nExpected:\n{expected}\n\nGot:{result}"
def test_regression_morris_optimal(): ''' Tests the use of optimal trajectories with Morris. 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 and grid_jump. ''' param_file = 'SALib/test_functions/params/Ishigami.txt' problem = read_param_file(param_file) param_values = sample(problem=problem, N=20, \ num_levels=4, grid_jump=2, \ optimal_trajectories=9) Y = Ishigami.evaluate(param_values) Si = morris.analyze(problem, param_values, Y, conf_level=0.95, print_to_console=False, num_levels=4, grid_jump=2) assert_allclose(Si['mu_star'], [8.1, 2.2, 5.4], rtol=10)
def test_delta(): cmd = "python {cli} sample saltelli -p {fn} -o model_input.txt -n 1000"\ .format(cli=salib_cli, fn=ishigami_fp) +\ " --precision 8 --max-order 2 --seed=100" subprocess.run(cmd.split()) # Run model and save output np.savetxt('model_output.txt', Ishigami.evaluate(np.loadtxt('model_input.txt'))) analyze_cmd = "python {cli} analyze delta -p {fn} -X model_input.txt \ -Y model_output.txt -c 0 -r 10 --seed=100".format(cli=salib_cli, fn=ishigami_fp).split() result = subprocess.check_output(analyze_cmd, universal_newlines=True) result = re.sub(r'[\n\t\s]*', '', result) expected_output = 'Parameterdeltadelta_confS1S1_confx10.2104780.0060910.3113620.012291x20.3540230.0062380.4283650.017972x30.1609860.0047180.0011110.002995' assert len(result) > 0 and result in expected_output, \ "Results did not match expected values:\n\n Expected: \n{} \n\n Got: \n{}".format( expected_output, result)
def test_delta(): cmd = "python {cli} sample saltelli -p {fn} -o model_input.txt -n 1000"\ .format(cli=salib_cli, fn=ishigami_fp) +\ " --precision 8 --max-order 2 --seed=100" subprocess.run(cmd.split()) # Run model and save output np.savetxt('model_output.txt', Ishigami.evaluate( np.loadtxt('model_input.txt'))) analyze_cmd = "python {cli} analyze delta -p {fn} -X model_input.txt \ -Y model_output.txt -c 0 -r 10 --seed=100".format(cli=salib_cli, fn=ishigami_fp).split() result = subprocess.check_output(analyze_cmd, universal_newlines=True) result = re.sub(r'[\n\t\s]*', '', result) expected_output = 'Parameterdeltadelta_confS1S1_confx10.2104780.0060910.3113620.012291x20.3540230.0062380.4283650.017972x30.1609860.0047180.0011110.002995' assert len(result) > 0 and result in expected_output, \ "Results did not match expected values:\n\n Expected: \n{} \n\n Got: \n{}".format( expected_output, result)
def test_delta(): cmd = "python {cli} sample saltelli -p {fn} -o model_input.txt -n 1024"\ .format(cli=salib_cli, fn=ishigami_fp) +\ " --precision 8 --max-order 2 --seed=100" subprocess.run(cmd.split()) # Run model and save output np.savetxt('model_output.txt', Ishigami.evaluate( np.loadtxt('model_input.txt'))) analyze_cmd = "python {cli} analyze delta -p {fn} -X model_input.txt \ -Y model_output.txt -c 0 -r 10 --seed=100".format(cli=salib_cli, fn=ishigami_fp).split() result = subprocess.check_output(analyze_cmd, universal_newlines=True) result = re.sub(r'[\n\t\s]*', '', result) expected_output = 'Parameterdeltadelta_confS1S1_confx10.2122850.0074810.3123190.011463x20.3530150.0061840.4306860.013135x30.1613440.0057540.0013880.001545' assert len(result) > 0 and result in expected_output, \ "Results did not match expected values:\n\n Expected: \n{} \n\n Got: \n{}".format( expected_output, result)
def test_sobol(): # Generate inputs cmd = f"python {salib_cli} sample saltelli -p {ishigami_fp} -o {input_path} -n 1000\ --precision 8 --max-order 2 --seed=100" cmd = cmd.split() result = subprocess.check_output(cmd, universal_newlines=True) np.savetxt(output_path, Ishigami.evaluate(np.loadtxt(input_path))) analyze_cmd = f"python {salib_cli} analyze sobol -p {ishigami_fp}\ -Y {output_path} -c 0 --max-order 2\ -r 1000 --seed=100".split() result = subprocess.check_output(analyze_cmd, universal_newlines=True) result = re.sub(r'[\n\t\s]*', '', result) expected_output = 'STST_confx10.5601370.091908x20.4387220.040634x30.2428450.026578S1S1_confx10.3079750.063047x20.4477670.053323x3-0.0042550.059667S2S2_conf(x1,x2)0.0122050.086177(x1,x3)0.2515260.108147(x2,x3)-0.0099540.065569' assert len(result) > 0 and result == expected_output, \ "Results did not match expected values:\n\n Expected: \n{} \n\n Got: \n{}".format( expected_output, result)
def test_sobol(): # Generate inputs cmd = "python {cli} sample saltelli -p {fn} -o model_input.txt -n 1000\ --precision 8 --max-order 2 --seed=100".format(cli=salib_cli, fn=ishigami_fp) cmd = cmd.split() result = subprocess.check_output(cmd, universal_newlines=True) np.savetxt('model_output.txt', Ishigami.evaluate( np.loadtxt('model_input.txt'))) analyze_cmd = "python {cli} analyze sobol -p {fn}\ -Y model_output.txt -c 0 --max-order 2\ -r 1000 --seed=100".format(cli=salib_cli, fn=ishigami_fp).split() result = subprocess.check_output(analyze_cmd, universal_newlines=True) result = re.sub(r'[\n\t\s]*', '', result) expected_output = 'ParameterS1S1_confSTST_confx10.3079750.0630470.5601370.091908x20.4477670.0533230.4387220.040634x3-0.0042550.0596670.2428450.026578Parameter_1Parameter_2S2S2_confx1x20.0122050.086177x1x30.2515260.108147x2x3-0.0099540.065569' assert len(result) > 0 and result == expected_output, \ "Results did not match expected values:\n\n Expected: \n{} \n\n Got: \n{}".format( expected_output, result)
def test_sobol(): # Generate inputs cmd = "python {cli} sample saltelli -p {fn} -o model_input.txt -n 1024\ --precision 8 --max-order 2 --seed=100".format(cli=salib_cli, fn=ishigami_fp) cmd = cmd.split() result = subprocess.check_output(cmd, universal_newlines=True) np.savetxt('model_output.txt', Ishigami.evaluate( np.loadtxt('model_input.txt'))) analyze_cmd = "python {cli} analyze sobol -p {fn}\ -Y model_output.txt -c 0 --max-order 2\ -r 1000 --seed=100".format(cli=salib_cli, fn=ishigami_fp).split() result = subprocess.check_output(analyze_cmd, universal_newlines=True) result = re.sub(r'[\n\t\s]*', '', result) expected_output = 'ParameterS1S1_confSTST_confx10.3168320.0622410.5558600.085972x20.4437630.0560470.4418980.041596x30.0122030.0559540.2446750.025332Parameter_1Parameter_2S2S2_confx1x20.0092540.083829x1x30.2381720.101764x2x3-0.0048880.067819' assert len(result) > 0 and result == expected_output, \ "Results did not match expected values:\n\n Expected: \n{} \n\n Got: \n{}".format( expected_output, result)
def test_ff(): # Generate inputs cmd = "python {cli} sample ff -p {fn} -o model_input.txt \ --precision=8 -n 1000 --seed=100".format(cli=salib_cli, fn=ishigami_fp).split() subprocess.run(cmd) # Run model and save output np.savetxt('model_output.txt', Ishigami.evaluate( np.loadtxt('model_input.txt'))) analyze_cmd = "python {cli} analyze ff -p {fn} -X model_input.txt\ -Y model_output.txt -c 0 --seed=100"\ .format(cli=salib_cli, fn=ishigami_fp).split() # run analysis and use regex to strip all whitespace from result result = subprocess.check_output(analyze_cmd, universal_newlines=True) result = re.sub(r'[\n\t\s]*', '', result) expected = "ParameterMEx10.000000x20.000000x30.000000dummy_00.000000('x1','x2')0.000000('x1','x3')0.000000('x2','x3')0.000000('x1','dummy_0')0.000000('x2','dummy_0')0.000000('x3','dummy_0')0.000000" assert len(result) > 0 and result == expected, \ "Unexpected FF results.\n\nExpected:\n{}\n\nGot:{}"\ .format(expected, result)
def test_fast(): # Generate inputs cmd = "python {cli} sample fast_sampler -p {fn} -o model_input.txt \ --precision=8 -n 1000 -M 4 --seed=100".format(cli=salib_cli, fn=ishigami_fp).split() subprocess.run(cmd) # Run model and save output np.savetxt('model_output.txt', Ishigami.evaluate( np.loadtxt('model_input.txt'))) analyze_cmd = "python {cli} analyze fast -p {fn} \ -Y model_output.txt -c 0 --seed=100"\ .format(cli=salib_cli, fn=ishigami_fp).split() # run analysis and use regex to strip all whitespace from result result = subprocess.check_output(analyze_cmd, universal_newlines=True) result = re.sub(r'[\n\t\s]*', '', result) expected = "ParameterFirstTotalx10.3104030.555603x20.4425530.469546x30.0000000.239155" assert len(result) > 0 and result == expected, \ "Unexpected FAST results.\n\nExpected:\n{}\n\nGot:{}"\ .format(expected, result)
def test_dgsm(): # Generate inputs cmd = "python {cli} sample finite_diff -p {fn} -o model_input.txt -d 0.001\ --precision=8 -n 1000 --seed=100".format(cli=salib_cli, fn=ishigami_fp).split() subprocess.run(cmd) # Run model and save output np.savetxt('model_output.txt', Ishigami.evaluate( np.loadtxt('model_input.txt'))) analyze_cmd = "python {cli} analyze dgsm -p {fn} -X model_input.txt\ -Y model_output.txt -c 0 -r 1000 --seed=100"\ .format(cli=salib_cli, fn=ishigami_fp).split() # run analysis and use regex to strip all whitespace from result result = subprocess.check_output(analyze_cmd, universal_newlines=True) result = re.sub(r'[\n\t\s]*', '', result) expected = "Parametervivi_stddgsmdgsm_confx17.62237816.1981232.2075541.034173x224.48775717.3385567.0920191.090835x311.18125824.0621273.2382591.477114" assert len(result) > 0 and result == expected, \ "Unexpected DGSM results.\n\nExpected:\n{}\n\nGot:{}"\ .format(expected, result)
def test_rbd_fast(): # Generate inputs cmd = "python {cli} sample ff -p {fn} -o model_input.txt \ --precision=8 --seed=100".format(cli=salib_cli, fn=ishigami_fp).split() subprocess.run(cmd) # Run model and save output np.savetxt('model_output.txt', Ishigami.evaluate( np.loadtxt('model_input.txt'))) analyze_cmd = "python {cli} analyze rbd_fast -p {fn} -X model_input.txt\ -Y model_output.txt --seed=100"\ .format(cli=salib_cli, fn=ishigami_fp).split() # run analysis and use regex to strip all whitespace from result result = subprocess.check_output(analyze_cmd, universal_newlines=True) result = re.sub(r'[\n\t\s]*', '', result) expected = "ParameterFirstx10.437313x20.129825x30.000573789" assert len(result) > 0 and result == expected, \ "Unexpected RBD-FAST results.\n\nExpected:\n{}\n\nGot:{}"\ .format(expected, result)
import sys sys.path.append('../..') from SALib.analyze import sobol from SALib.sample import saltelli from SALib.test_functions import Ishigami from SALib.util import read_param_file # Read the parameter range file and generate samples problem = read_param_file('../../SALib/test_functions/params/Ishigami.txt') # Generate samples param_values = saltelli.sample(problem, 1000, calc_second_order=True) # Run the "model" and save the output in a text file # This will happen offline for external models Y = Ishigami.evaluate(param_values) # Perform the sensitivity analysis using the model output # Specify which column of the output file to analyze (zero-indexed) Si = sobol.analyze(problem, Y, calc_second_order=True, conf_level=0.95, print_to_console=True) # Returns a dictionary with keys 'S1', 'S1_conf', 'ST', and 'ST_conf' # e.g. Si['S1'] contains the first-order index for each parameter, in the same order as the parameter file # The optional second-order indices are now returned in keys 'S2', 'S2_conf' # These are both upper triangular DxD matrices with nan's in the duplicate # entries.
def test_incorrect_second_order_setting(): # note this will still be a problem if N(2D+2) also divides by (D+2) problem,param_values = setup_samples(N=501, calc_second_order=False) Y = Ishigami.evaluate(param_values) sobol.analyze(problem, Y, calc_second_order=True)
def test_incorrect_sample_size(): problem,param_values = setup_samples() Y = Ishigami.evaluate(param_values) sobol.analyze(problem, Y[:-10], calc_second_order=True)
from SALib.analyze import morris from SALib.test_functions import Ishigami import numpy as np # Read the parameter range file and generate samples param_file = '../../SALib/test_functions/params/Ishigami.txt' # Generate samples param_values = Morris(param_file, samples=50, num_levels=4, grid_jump=2, \ group_file=None, \ optimal_trajectories=7) # Save the parameter values in a file (they are needed in the analysis) param_values.save_data('model_input.txt') # Run the "model" and save the output in a text file # This will happen offline for external models online_model_values = param_values.get_input_sample_scaled() Y = Ishigami.evaluate(online_model_values) np.savetxt("model_output.txt", Y, delimiter=' ') # Perform the sensitivity analysis using the model output # Specify which column of the output file to analyze (zero-indexed) Si = morris.analyze(param_file, 'model_input.txt', 'model_output.txt', column=0, conf_level=0.95, print_to_console=False) # Returns a dictionary with keys 'mu', 'mu_star', 'sigma', and 'mu_star_conf' # e.g. Si['mu_star'] contains the mu* value for each parameter, in the # same order as the parameter file print(Si['mu_star'])