Coverage for aixcalibuha/sensitivity_analysis/fast.py: 96%
24 statements
« prev ^ index » next coverage.py v7.4.1, created at 2024-01-27 10:48 +0000
« prev ^ index » next coverage.py v7.4.1, created at 2024-01-27 10:48 +0000
1"""
2Adds the FASTAnalyzer to the available
3classes of sensitivity analysis.
4"""
5import numpy as np
6from SALib.sample import fast_sampler as fast
7from SALib.analyze import fast as analyze_fast
8from aixcalibuha.sensitivity_analysis import SenAnalyzer
9from aixcalibuha import CalibrationClass
12class FASTAnalyzer(SenAnalyzer):
13 """
14 FAST method from SALib
15 https://salib.readthedocs.io/en/latest/api.html#fast-fourier-amplitude-sensitivity-test
16 A variance-based method which can compute the sensitivity measures
17 'S1' and 'ST'.
19 Additional arguments:
21 :keyword int M:
22 Default 4, used for the fast-method
23 :keyword seed:
24 Used for the fast-method
25 """
27 def __init__(self, sim_api, **kwargs):
28 super().__init__(
29 sim_api=sim_api,
30 **kwargs)
31 # Set additional kwargs
32 self.M = kwargs.pop("M", 4)
33 self.seed = kwargs.pop("seed", None)
35 @property
36 def analysis_variables(self):
37 """The analysis variables of the FAST method"""
38 return ['S1', 'ST']
40 def analysis_function(self, x, y):
41 """
42 Use the SALib.analyze.fast method to analyze the simulation results.
44 :param np.array x:
45 placeholder for the `X` parameter of the morris method not used for sobol
46 :param np.array y:
47 The NumPy array containing the model outputs
48 :return:
49 returns the result of the SALib.analyze.fast method (from the documentation:
50 Returns a dictionary with keys 'S1' and 'ST', where each entry is a list of
51 size D (the number of parameters) containing the indices in the same order
52 as the parameter file.)
53 """
54 return analyze_fast.analyze(self.problem, y,
55 M=self.M)
57 def create_sampler_demand(self):
58 """
59 Function to create the sampler parameters for the fast method
60 """
61 return {'M': self.M}
63 def generate_samples(self):
64 """
65 Run the sampler for fast and return the results.
67 :return:
68 The list of samples generated as a NumPy array with one row per sample
69 and each row containing one value for each variable name in `problem['names']`.
70 :rtype: np.ndarray
71 """
72 return fast.sample(self.problem,
73 N=self.num_samples,
74 **self.create_sampler_demand())
76 def _get_res_dict(self, result: dict, cal_class: CalibrationClass, analysis_variable: str):
77 """
78 Convert the result object to a dict with the key
79 being the variable name and the value being the result
80 associated to self.analysis_variable.
81 """
82 names = self.create_problem(cal_class.tuner_paras)['names']
83 if result is None:
84 return {var_name: np.abs(res_val)
85 for var_name, res_val in zip(names,
86 np.zeros(len(names)))}
87 return {var_name: np.abs(res_val)
88 for var_name, res_val in zip(names,
89 result[analysis_variable])}