@@ -64,7 +64,6 @@ def create_samples(self) -> None:
6464 The Sobol' sequence is a popular quasi-random low-discrepancy sequence used
6565 to generate uniform samples of parameter space.
6666 """
67- console .rule ("Samples" , style = "white" )
6867 # (num_samples x num_outputs)
6968 # total model evaluations are (2d+2) * N for d input factors
7069 num_samples = (2 * self .num_parameters + 2 ) * self .N
@@ -81,7 +80,6 @@ def create_samples(self) -> None:
8180 "parameter" : self .parameter_ids },
8281 name = "samples"
8382 )
84- console .print (self .samples )
8583
8684
8785 def calculate_sensitivity (self , cache_filename : Optional [str ] = None , cache : bool = False ):
@@ -128,40 +126,6 @@ def calculate_sensitivity(self, cache_filename: Optional[str] = None, cache: boo
128126 # write to cache
129127 self .write_cache (data = self .sensitivity , cache_filename = cache_filename , cache = cache )
130128
131- def plot_sobol_indices (
132- self ,
133- fig_path : Path ,
134- ):
135- """Barplots for the Sobol indices."""
136- # parameter_labels: dict[str, str] = {p.uid: f"{p.uid}: {p.name}" for p in self.parameters}
137- parameter_labels : dict [str , str ] = {p .uid : p .uid for p in self .parameters }
138- output_labels : dict [str , str ] = {q .uid : q .name for q in self .outputs }
139-
140- for group in self .groups :
141- gid = group .uid
142- ymax = self .sensitivity [gid ]["ST" ].max (dim = None )
143- ymin = self .sensitivity [gid ]["S1" ].min (dim = None )
144-
145- for ko , output in enumerate (self .outputs ):
146- # f_path = fig_path.parent / f"FigS{ko+22}_{fig_path.stem}_{ko:>03}_{output.uid}{fig_path.suffix}"
147- f_path = fig_path .parent / f"{ fig_path .stem } _{ ko :>03} _{ output .uid } { fig_path .suffix } "
148-
149- S1 = self .sensitivity [gid ]["S1" ][:, ko ]
150- ST = self .sensitivity [gid ]["ST" ][:, ko ]
151- S1_conf = self .sensitivity [gid ]["S1_conf" ][:, ko ]
152- ST_conf = self .sensitivity [gid ]["ST_conf" ][:, ko ]
153- sobol_barplot (
154- S1 = S1 ,
155- ST = ST ,
156- S1_conf = S1_conf ,
157- ST_conf = ST_conf ,
158- title = f"{ output_labels [output .uid ]} ({ group .name } )" ,
159- fig_path = f_path ,
160- parameter_labels = parameter_labels ,
161- ymax = np .max ([1.05 , ymax ]),
162- ymin = np .min ([- 0.05 , ymin ]),
163- )
164-
165129 @staticmethod
166130 def run_sensitivity_analysis (
167131 results_path : Path ,
@@ -192,9 +156,8 @@ def run_sensitivity_analysis(
192156 parameters = parameters ,
193157 groups = groups ,
194158 results_path = results_path ,
195- # N=4096,
196- N = 16 ,
197- seed = 1234 ,
159+ N = N ,
160+ seed = seed ,
198161 )
199162
200163 console .rule ("Samples" , style = "white" )
@@ -232,46 +195,79 @@ def run_sensitivity_analysis(
232195 fig_path = sa .results_path / f"sobol_sensitivity_N{ sa .N } _{ kg :>02} _{ group .uid } .png" ,
233196 )
234197
198+ def plot_sobol_indices (
199+ self ,
200+ fig_path : Path ,
201+ ):
202+ """Barplots for the Sobol indices."""
203+ # parameter_labels: dict[str, str] = {p.uid: f"{p.uid}: {p.name}" for p in self.parameters}
204+ parameter_labels : dict [str , str ] = {p .uid : p .uid for p in self .parameters }
205+ output_labels : dict [str , str ] = {q .uid : q .name for q in self .outputs }
235206
207+ for group in self .groups :
208+ gid = group .uid
209+ ymax = self .sensitivity [gid ]["ST" ].max (dim = None )
210+ ymin = self .sensitivity [gid ]["S1" ].min (dim = None )
236211
237- def sobol_barplot (
238- S1 , ST , S1_conf , ST_conf ,
239- parameter_labels : dict [str , str ],
240- fig_path : Optional [Path ] = None ,
241- title : Optional [str ] = None ,
242- ymax : float = 1.1 ,
243- ymin : float = - 0.1 ,
244- ):
245- # width
246- figsize = (15 , 3 )
247- label_fontsize = 15
248-
249- categories : list [str ] = list (parameter_labels .values ())
250- f , ax = plt .subplots (figsize = figsize )
251-
252- ax .bar (categories , ST , label = 'ST' ,
253- color = "tab:orange" ,
254- alpha = 1.0 ,
255- edgecolor = "black" ,
256- yerr = ST_conf , capsize = 5
257- )
258-
259- ax .bar (categories , S1 , label = 'S1' , color = "tab:blue" ,
260- edgecolor = "black" , yerr = S1_conf , capsize = 5 )
261-
262-
263- # ax.set_xlabel('Parameter', fontsize=label_fontsize, fontweight="bold")
264- ax .set_ylabel ('Sobol Index' , fontsize = label_fontsize , fontweight = "bold" )
265- ax .set_ylim (bottom = ymin , top = ymax )
266- ax .grid (True , axis = "y" )
267- ax .tick_params (axis = 'x' , labelrotation = 90 )
268- # ax.tick_params(axis='x', labelweight='bold')
269- ax .legend ()
212+ for ko , output in enumerate (self .outputs ):
213+ # f_path = fig_path.parent / f"FigS{ko+22}_{fig_path.stem}_{ko:>03}_{output.uid}{fig_path.suffix}"
214+ f_path = fig_path .parent / f"{ fig_path .stem } _{ ko :>03} _{ output .uid } { fig_path .suffix } "
270215
271- if title :
272- plt .suptitle (title , fontsize = 20 , fontweight = "bold" )
216+ S1 = self .sensitivity [gid ]["S1" ][:, ko ]
217+ ST = self .sensitivity [gid ]["ST" ][:, ko ]
218+ S1_conf = self .sensitivity [gid ]["S1_conf" ][:, ko ]
219+ ST_conf = self .sensitivity [gid ]["ST_conf" ][:, ko ]
220+ self .sobol_barplot (
221+ S1 = S1 ,
222+ ST = ST ,
223+ S1_conf = S1_conf ,
224+ ST_conf = ST_conf ,
225+ title = f"{ output_labels [output .uid ]} ({ group .name } )" ,
226+ fig_path = f_path ,
227+ parameter_labels = parameter_labels ,
228+ ymax = np .max ([1.05 , ymax ]),
229+ ymin = np .min ([- 0.05 , ymin ]),
230+ )
273231
274- if fig_path :
275- plt .savefig (fig_path , dpi = 300 , bbox_inches = "tight" )
276- plt .show ()
232+ @staticmethod
233+ def sobol_barplot (
234+ S1 , ST , S1_conf , ST_conf ,
235+ parameter_labels : dict [str , str ],
236+ fig_path : Optional [Path ] = None ,
237+ title : Optional [str ] = None ,
238+ ymax : float = 1.1 ,
239+ ymin : float = - 0.1 ,
240+ ):
241+ # width
242+ figsize = (15 , 3 )
243+ label_fontsize = 15
244+
245+ categories : list [str ] = list (parameter_labels .values ())
246+ f , ax = plt .subplots (figsize = figsize )
247+
248+ ax .bar (categories , ST , label = 'ST' ,
249+ color = "tab:orange" ,
250+ alpha = 1.0 ,
251+ edgecolor = "black" ,
252+ yerr = ST_conf , capsize = 5
253+ )
254+
255+ ax .bar (categories , S1 , label = 'S1' , color = "tab:blue" ,
256+ edgecolor = "black" , yerr = S1_conf , capsize = 5 )
257+
258+
259+ # ax.set_xlabel('Parameter', fontsize=label_fontsize, fontweight="bold")
260+ ax .set_ylabel ('Sobol Index' , fontsize = label_fontsize , fontweight = "bold" )
261+ ax .set_ylim (bottom = ymin , top = ymax )
262+ ax .grid (True , axis = "y" )
263+ ax .tick_params (axis = 'x' , labelrotation = 90 )
264+ # ax.tick_params(axis='x', labelweight='bold')
265+ ax .legend ()
266+
267+ if title :
268+ plt .suptitle (title , fontsize = 20 , fontweight = "bold" )
269+
270+ if fig_path :
271+ plt .savefig (fig_path , dpi = 300 , bbox_inches = "tight" )
272+ plt .show ()
277273
0 commit comments