|
| 1 | +import warnings |
| 2 | + |
1 | 3 | import numpy as np |
2 | 4 | import pandas as pd |
| 5 | +import pytest |
3 | 6 |
|
4 | 7 | from corrai.base.parameter import Parameter |
5 | 8 | from corrai.base.model import IshigamiDynamic, Ishigami |
|
29 | 32 |
|
30 | 33 |
|
31 | 34 | class TestSensitivity: |
32 | | - def test_sanalysis_sobol_with_sobol_sampler(self): |
33 | | - # sobol_analysis = SobolSanalysis( |
34 | | - # parameters=PARAMETER_LIST, |
35 | | - # model=IshigamiDynamic(), |
36 | | - # simulation_options=SIMULATION_OPTIONS, |
37 | | - # ) |
38 | | - # |
39 | | - # sobol_analysis.add_sample(N=1000, n_cpu=1, calc_second_order=True, seed=42) |
40 | | - # res = sobol_analysis.analyze("res", calc_second_order=True, seed=42) |
41 | | - # |
42 | | - # np.testing.assert_almost_equal( |
43 | | - # res["mean_res"]["S1"], |
44 | | - # np.array([0.33080399, 0.44206835, 0.00946747]), |
45 | | - # ) |
46 | | - # |
47 | | - # res = sobol_analysis.analyze("res", freq="h", calc_second_order=True, seed=42) |
48 | | - # assert res.index.tolist() == [ |
49 | | - # pd.Timestamp("2009-01-01 00:00:00"), |
50 | | - # pd.Timestamp("2009-01-01 01:00:00"), |
51 | | - # pd.Timestamp("2009-01-01 02:00:00"), |
52 | | - # pd.Timestamp("2009-01-01 03:00:00"), |
53 | | - # pd.Timestamp("2009-01-01 04:00:00"), |
54 | | - # pd.Timestamp("2009-01-01 05:00:00"), |
55 | | - # ] |
56 | | - # |
57 | | - # sobol_analysis.plot_sample_hist( |
58 | | - # "res", bins=10, reference_value=10, reference_label="ref" |
59 | | - # ) |
60 | | - # |
61 | | - # np.testing.assert_almost_equal( |
62 | | - # res["2009-01-01 00:00:00"]["S1"], |
63 | | - # np.array([0.33080399, 0.44206835, 0.00946747]), |
64 | | - # decimal=3, |
65 | | - # ) |
66 | | - # |
| 35 | + def test_sanalysis_sobol_static(self): |
67 | 36 | sobol_analysis = SobolSanalysis( |
68 | 37 | parameters=PARAMETER_LIST, |
69 | 38 | model=Ishigami(), |
| 39 | + calc_second_order=True, |
| 40 | + ) |
| 41 | + |
| 42 | + sobol_analysis.add_sample(N=1000, n_cpu=1, seed=42) |
| 43 | + res = sobol_analysis.analyze("res", seed=42) |
| 44 | + |
| 45 | + np.testing.assert_almost_equal( |
| 46 | + res["mean_res"]["S1"], |
| 47 | + np.array([0.33080399, 0.44206835, 0.00946747]), |
| 48 | + ) |
| 49 | + |
| 50 | + def test_sanalysis_sobol_dynamic(self): |
| 51 | + sobol_analysis = SobolSanalysis( |
| 52 | + parameters=PARAMETER_LIST, |
| 53 | + model=IshigamiDynamic(), |
| 54 | + simulation_options=SIMULATION_OPTIONS, |
| 55 | + calc_second_order=True, |
70 | 56 | ) |
71 | 57 |
|
72 | | - sobol_analysis.add_sample(N=1000, n_cpu=1, calc_second_order=True, seed=42) |
73 | | - res = sobol_analysis.analyze("res", calc_second_order=True, seed=42) |
| 58 | + sobol_analysis.add_sample(N=1000, n_cpu=1, seed=42) |
| 59 | + res = sobol_analysis.analyze("res", seed=42) |
74 | 60 |
|
75 | 61 | np.testing.assert_almost_equal( |
76 | 62 | res["mean_res"]["S1"], |
77 | 63 | np.array([0.33080399, 0.44206835, 0.00946747]), |
78 | 64 | ) |
79 | 65 |
|
80 | | - assert True |
| 66 | + res_freq = sobol_analysis.analyze("res", freq="h", seed=42) |
| 67 | + assert res_freq.index.tolist() == [ |
| 68 | + pd.Timestamp("2009-01-01 00:00:00"), |
| 69 | + pd.Timestamp("2009-01-01 01:00:00"), |
| 70 | + pd.Timestamp("2009-01-01 02:00:00"), |
| 71 | + pd.Timestamp("2009-01-01 03:00:00"), |
| 72 | + pd.Timestamp("2009-01-01 04:00:00"), |
| 73 | + pd.Timestamp("2009-01-01 05:00:00"), |
| 74 | + ] |
| 75 | + np.testing.assert_almost_equal( |
| 76 | + res_freq["2009-01-01 00:00:00"]["S1"], |
| 77 | + np.array([0.33080399, 0.44206835, 0.00946747]), |
| 78 | + decimal=3, |
| 79 | + ) |
| 80 | + |
| 81 | + def test_sobol_incremental_sampling_raises(self): |
| 82 | + sobol_analysis = SobolSanalysis( |
| 83 | + parameters=PARAMETER_LIST, |
| 84 | + model=Ishigami(), |
| 85 | + ) |
| 86 | + sobol_analysis.add_sample(N=64, n_cpu=1, seed=42) |
| 87 | + with pytest.raises(ValueError, match="does not support incremental sampling"): |
| 88 | + sobol_analysis.add_sample(N=64, n_cpu=1, seed=0) |
| 89 | + |
| 90 | + def test_sobol_plot_s2_matrix_raises_when_no_second_order(self): |
| 91 | + sobol_analysis = SobolSanalysis( |
| 92 | + parameters=PARAMETER_LIST, |
| 93 | + model=Ishigami(), |
| 94 | + calc_second_order=False, |
| 95 | + ) |
| 96 | + sobol_analysis.add_sample(N=64, n_cpu=1, seed=42) |
| 97 | + with pytest.raises(ValueError, match="requires second-order indices"): |
| 98 | + sobol_analysis.plot_s2_matrix() |
| 99 | + |
| 100 | + def test_sobol_static_model_no_spurious_warning(self): |
| 101 | + sobol_analysis = SobolSanalysis( |
| 102 | + parameters=PARAMETER_LIST, |
| 103 | + model=Ishigami(), |
| 104 | + ) |
| 105 | + sobol_analysis.add_sample(N=64, n_cpu=1, seed=42) |
| 106 | + with warnings.catch_warnings(record=True) as caught: |
| 107 | + warnings.simplefilter("always") |
| 108 | + sobol_analysis.analyze("res", seed=42) |
| 109 | + our_warnings = [w for w in caught if "sensitivity" in str(w.filename)] |
| 110 | + assert len(our_warnings) == 0, f"Unexpected warnings from sensitivity.py: {our_warnings}" |
81 | 111 |
|
82 | 112 | def test_sanalysis_morris(self): |
83 | 113 | morris_analysis = MorrisSanalysis( |
@@ -202,8 +232,9 @@ def test_sobol_s2_matrix(self): |
202 | 232 | parameters=PARAMETER_LIST, |
203 | 233 | model=IshigamiDynamic(), |
204 | 234 | simulation_options=SIMULATION_OPTIONS, |
| 235 | + calc_second_order=True, |
205 | 236 | ) |
206 | | - sobol_analysis.add_sample(N=2**2, n_cpu=1, calc_second_order=True) |
| 237 | + sobol_analysis.add_sample(N=2**2, n_cpu=1) |
207 | 238 | fig_matrix = sobol_analysis.plot_s2_matrix() |
208 | 239 | assert fig_matrix["layout"]["title"]["text"] == ( |
209 | 240 | "Sobol mean res " "- 2nd order interactions" |
@@ -321,7 +352,7 @@ def test_sobol_plot_bar_plot_kwargs(self): |
321 | 352 | parameters=PARAMETER_LIST, |
322 | 353 | model=Ishigami(), |
323 | 354 | ) |
324 | | - sobol_analysis.add_sample(N=2**4, n_cpu=1, calc_second_order=True, seed=42) |
| 355 | + sobol_analysis.add_sample(N=2**4, n_cpu=1, seed=42) |
325 | 356 | fig = sobol_analysis.plot_bar( |
326 | 357 | plot_kwargs={ |
327 | 358 | "title": "My Custom Title", |
|
0 commit comments