Skip to content

Commit

Permalink
regression tests for all methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jdherman committed Jan 29, 2015
1 parent 5e4c5e0 commit 8c5b442
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
64 changes: 64 additions & 0 deletions SALib/tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@

from ..sample.morris import sample
from ..analyze import morris
from SALib.sample import saltelli
from SALib.analyze import sobol
from SALib.sample import fast_sampler
from SALib.analyze import fast
from SALib.sample import finite_diff
from SALib.analyze import dgsm
from SALib.sample import latin
from SALib.analyze import delta
from ..test_functions import Ishigami
from ..util import read_param_file

Expand Down Expand Up @@ -76,3 +84,59 @@ def test_regression_morris_optimal():
num_levels=4, grid_jump=2)

assert_allclose(Si['mu_star'], [8.1, 2.2, 5.4], rtol=10)


def test_regression_sobol():
param_file = 'SALib/test_functions/params/Ishigami.txt'
param_values = saltelli.sample(10000, param_file, calc_second_order=True)

Y = Ishigami.evaluate(param_values)
np.savetxt('model_output.txt', Y, delimiter=' ')

Si = sobol.analyze(param_file, 'model_output.txt', column=0,
calc_second_order=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_fast():
param_file = 'SALib/test_functions/params/Ishigami.txt'
param_values = fast_sampler.sample(1000, param_file)

Y = Ishigami.evaluate(param_values)
np.savetxt("model_output.txt", Y, delimiter=' ')

Si = fast.analyze(param_file, 'model_output.txt', column=0, print_to_console=True)
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_regression_dgsm():
param_file = 'SALib/test_functions/params/Ishigami.txt'
param_values = finite_diff.sample(10000, param_file, delta=0.001)

np.savetxt('model_input.txt', param_values, delimiter=' ')
Y = Ishigami.evaluate(param_values)
np.savetxt('model_output.txt', Y, delimiter=' ')

Si = dgsm.analyze(param_file, 'model_input.txt', 'model_output.txt',
column=0, 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 = 'SALib/test_functions/params/Ishigami.txt'
param_values = latin.sample(10000, param_file)

np.savetxt('model_input.txt', param_values, delimiter=' ')
Y = Ishigami.evaluate(param_values)
np.savetxt('model_output.txt', Y, delimiter=' ')

Si = delta.analyze(param_file, 'model_input.txt', 'model_output.txt',
column=0, 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)
2 changes: 1 addition & 1 deletion examples/delta/delta.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
# Perform the sensitivity analysis using the model output
# Specify which column of the output file to analyze (zero-indexed)
Si = delta.analyze(param_file, 'model_input.txt', 'model_output.txt',
column=0, num_resamples=10, conf_level=0.95, print_to_console=False)
column=0, num_resamples=10, conf_level=0.95, print_to_console=True)
# Returns a dictionary with keys 'delta', 'delta_conf', 'S1', 'S1_conf'
print(str(Si['delta']))
2 changes: 1 addition & 1 deletion examples/dgsm/dgsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# Perform the sensitivity analysis using the model output
# Specify which column of the output file to analyze (zero-indexed)
Si = dgsm.analyze(param_file, 'model_input.txt', 'model_output.txt',
column=0, conf_level=0.95, print_to_console=False)
column=0, conf_level=0.95, print_to_console=True)
# Returns a dictionary with keys 'vi', 'vi_std', 'dgsm', and 'dgsm_conf'
# e.g. Si['vi'] contains the sensitivity measure for each parameter, in
# the same order as the parameter file
Expand Down
2 changes: 1 addition & 1 deletion examples/fast/fast.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# Perform the sensitivity analysis using the model output
# Specify which column of the output file to analyze (zero-indexed)
Si = fast.analyze(
param_file, 'model_output.txt', column=0, print_to_console=False)
param_file, 'model_output.txt', column=0, print_to_console=True)
# Returns a dictionary with keys 'S1' and 'ST'
# e.g. Si['S1'] contains the first-order index for each parameter, in the
# same order as the parameter file

0 comments on commit 8c5b442

Please sign in to comment.