|
| 1 | +""" |
| 2 | +Global sensitivity analysis using FAST - Fourier Amplitude Sensitivity Test. |
| 3 | +
|
| 4 | + Cukier, R.I., Fortuin, C.M., Shuler, K.E., Petschek, A.G., Schaibly, |
| 5 | + J.H., 1973. Study of the sensitivity of coupled reaction systems to uncertainties |
| 6 | + in rate coefficients. I theory. Journal of Chemical Physics 59, 3873-3878. https://doi.org/10.1063/1.1680571 |
| 7 | + Saltelli, A., S. Tarantola, and K. P.-S. Chan (1999). A Quantitative |
| 8 | + Model-Independent Method for Global Sensitivity Analysis of Model Output. |
| 9 | + Technometrics, 41(1):39-56, doi:10.1080/00401706.1999.10485594. |
| 10 | +
|
| 11 | +""" |
| 12 | + |
| 13 | +from pathlib import Path |
| 14 | +from typing import Optional |
| 15 | + |
| 16 | +import SALib |
| 17 | +import numpy as np |
| 18 | +import xarray as xr |
| 19 | +from SALib import ProblemSpec |
| 20 | +from SALib.analyze import fast |
| 21 | +from SALib.sample import fast_sampler |
| 22 | +from matplotlib import pyplot as plt |
| 23 | +from pymetadata.console import console |
| 24 | + |
| 25 | +from sbmlsim.sensitivity.analysis import SensitivityAnalysis, SensitivitySimulation, \ |
| 26 | + AnalysisGroup |
| 27 | +from sbmlsim.sensitivity.parameters import SensitivityParameter |
| 28 | +from sbmlsim.sensitivity.plots import plot_S1_ST_indices |
| 29 | + |
| 30 | + |
| 31 | +class FASTSensitivityAnalysis(SensitivityAnalysis): |
| 32 | + """Global sensitivity analysis based Fourier Amplitude Sensitivity Test (FAST) |
| 33 | + (Cukier et al. 1973, Saltelli et al. 1999).""" |
| 34 | + |
| 35 | + sensitivity_keys = ["S1", "ST", "S1_conf", "ST_conf"] |
| 36 | + |
| 37 | + def __init__( |
| 38 | + self, |
| 39 | + sensitivity_simulation: SensitivitySimulation, |
| 40 | + parameters: list[SensitivityParameter], |
| 41 | + groups: list[AnalysisGroup], |
| 42 | + results_path: Path, |
| 43 | + N: int, |
| 44 | + M: int = 4, |
| 45 | + **kwargs, |
| 46 | + ): |
| 47 | + """ |
| 48 | + N (int) – The number of samples to generate |
| 49 | + M (int) – The interference parameter, i.e., the number of harmonics to sum |
| 50 | + in the Fourier series decomposition (default 4) |
| 51 | +
|
| 52 | + The Sobol' sequence is a popular quasi-random low-discrepancy sequence used |
| 53 | + to generate uniform samples of parameter space. |
| 54 | + """ |
| 55 | + |
| 56 | + super().__init__(sensitivity_simulation, parameters, groups, results_path, |
| 57 | + **kwargs) |
| 58 | + self.N: int = N |
| 59 | + self.M: int = M |
| 60 | + |
| 61 | + # define the problem specification |
| 62 | + self.ssa_problems: dict[str, ProblemSpec] = {} |
| 63 | + for group in self.groups: |
| 64 | + self.ssa_problems[group.uid] = ProblemSpec({ |
| 65 | + 'num_vars': self.num_parameters, |
| 66 | + 'names': self.parameter_ids, |
| 67 | + 'bounds': [[p.lower_bound, p.upper_bound] for p in self.parameters], |
| 68 | + "outputs": self.output_ids, |
| 69 | + }) |
| 70 | + |
| 71 | + def create_samples(self) -> None: |
| 72 | + """Create samples for FAST.""" |
| 73 | + # (num_samples x num_outputs) |
| 74 | + # total model evaluations are N * num_parameters |
| 75 | + num_samples = self.N * self.num_parameters |
| 76 | + |
| 77 | + for gid in self.group_ids: |
| 78 | + # libssa samples based on definition |
| 79 | + ssa_samples = fast_sampler.sample( |
| 80 | + self.ssa_problems[gid], N=self.N, M=self.M, |
| 81 | + ) |
| 82 | + self.ssa_problems[gid].set_samples(ssa_samples) |
| 83 | + |
| 84 | + self.samples[gid] = xr.DataArray( |
| 85 | + ssa_samples, |
| 86 | + dims=["sample", "parameter"], |
| 87 | + coords={"sample": range(num_samples), |
| 88 | + "parameter": self.parameter_ids}, |
| 89 | + name="samples" |
| 90 | + ) |
| 91 | + |
| 92 | + def calculate_sensitivity(self, cache_filename: Optional[str] = None, |
| 93 | + cache: bool = False): |
| 94 | + """ Perform extended Fourier Amplitude Sensitivity Test on model outputs. |
| 95 | +
|
| 96 | + Returns a dictionary with keys 'S1' and 'ST', where each entry is a list of |
| 97 | + size D (the number of parameters) containing the indices in the same order |
| 98 | + as the parameter file. |
| 99 | + """ |
| 100 | + |
| 101 | + data = self.read_cache(cache_filename, cache) |
| 102 | + if data: |
| 103 | + self.sensitivity = data |
| 104 | + return |
| 105 | + |
| 106 | + for gid in self.group_ids: |
| 107 | + Y = self.results[gid].values |
| 108 | + self.ssa_problems[gid].set_results(Y) |
| 109 | + |
| 110 | + # num_parameters x num_outputs |
| 111 | + for key in self.sensitivity_keys: |
| 112 | + self.sensitivity[gid][key] = xr.DataArray( |
| 113 | + np.full((self.num_parameters, self.num_outputs), np.nan), |
| 114 | + dims=["parameter", "output"], |
| 115 | + coords={"parameter": self.parameter_ids, |
| 116 | + "output": self.output_ids}, |
| 117 | + name=key |
| 118 | + ) |
| 119 | + |
| 120 | + # Calculate FAST indices |
| 121 | + for ko in range(self.num_outputs): |
| 122 | + Yo = Y[:, ko] |
| 123 | + Si = SALib.analyze.fast.analyze( |
| 124 | + self.ssa_problems[gid], Yo, |
| 125 | + M=self.M, |
| 126 | + num_resamples=100, |
| 127 | + conf_level=0.95, |
| 128 | + print_to_console=False, |
| 129 | + ) |
| 130 | + for key in self.sensitivity_keys: |
| 131 | + self.sensitivity[gid][key][:, ko] = Si[key] |
| 132 | + |
| 133 | + # write to cache |
| 134 | + self.write_cache(data=self.sensitivity, cache_filename=cache_filename, |
| 135 | + cache=cache) |
| 136 | + |
| 137 | + @staticmethod |
| 138 | + def run_sensitivity_analysis( |
| 139 | + results_path: Path, |
| 140 | + sensitivity_simulation: SensitivitySimulation, |
| 141 | + parameters: list[SensitivityParameter], |
| 142 | + groups: list[AnalysisGroup], |
| 143 | + N: int, |
| 144 | + seed: int, |
| 145 | + M: int = 4, |
| 146 | + cache_results: bool = False, |
| 147 | + cache_sensitivity: bool = False, |
| 148 | + ) -> None: |
| 149 | + """FAST sensitivity analysis. |
| 150 | +
|
| 151 | + First-order FAST (main effects only): |
| 152 | + 100 × num_pars samples is usually sufficient |
| 153 | +
|
| 154 | + Extended FAST (eFAST, total effects): |
| 155 | + 200–500 × k samples recommended |
| 156 | + (higher frequencies needed to separate interactions) |
| 157 | +
|
| 158 | + :param sensitivity_simulation: Sensitivity simulation. |
| 159 | + :param parameters: Sensitivity parameters. |
| 160 | + :param groups: Sensitivity groups. |
| 161 | + N (int) – The number of samples to generate |
| 162 | + M (int) – The interference parameter, i.e., the number of harmonics to sum |
| 163 | + :param seed: Random seed. |
| 164 | + """ |
| 165 | + prefix = "fast" |
| 166 | + console.rule(f"{prefix.upper()} SENSITIVITY ANALYSIS", style="blue bold", align="center") |
| 167 | + if cache_sensitivity and not cache_results: |
| 168 | + # sensitivity must be recalculated for new results |
| 169 | + cache_sensitivity = False |
| 170 | + |
| 171 | + sa = FASTSensitivityAnalysis( |
| 172 | + sensitivity_simulation=sensitivity_simulation, |
| 173 | + parameters=parameters, |
| 174 | + groups=groups, |
| 175 | + results_path=results_path, |
| 176 | + N=N, |
| 177 | + M=M, |
| 178 | + seed=seed, |
| 179 | + ) |
| 180 | + |
| 181 | + console.rule("Samples", style="white") |
| 182 | + sa.create_samples() |
| 183 | + console.print(sa.samples_table()) |
| 184 | + |
| 185 | + console.rule("Results", style="white") |
| 186 | + sa.simulate_samples(cache_filename=f"{prefix}_results_N{sa.N}.pkl", |
| 187 | + cache=cache_results) |
| 188 | + console.print(sa.results_table()) |
| 189 | + |
| 190 | + console.rule("Sensitivity", style="white") |
| 191 | + sa.calculate_sensitivity(cache_filename=f"{prefix}_sensitivity_N{sa.N}.pkl", |
| 192 | + cache=cache_sensitivity) |
| 193 | + |
| 194 | + console.rule("Plotting", style="white") |
| 195 | + for kg, group in enumerate(sa.groups): |
| 196 | + # heatmaps |
| 197 | + for key in ["ST", "S1"]: |
| 198 | + sa.plot_sensitivity( |
| 199 | + group_id=group.uid, |
| 200 | + sensitivity_key=key, |
| 201 | + # title=f"{key} {group.name}", |
| 202 | + cutoff=0.05, |
| 203 | + cluster_rows=False, |
| 204 | + cmap="viridis", |
| 205 | + vcenter=0.5, |
| 206 | + vmin=0.0, |
| 207 | + vmax=1.0, |
| 208 | + fig_path=sa.results_path / f"{prefix}_sensitivity_N{sa.N}_{kg:>02}_{group.uid}_{key}.png" |
| 209 | + ) |
| 210 | + |
| 211 | + # barplots |
| 212 | + plot_S1_ST_indices( |
| 213 | + sa=sa, |
| 214 | + fig_path=sa.results_path / f"{prefix}_sensitivity_N{sa.N}_{kg:>02}_{group.uid}.png", |
| 215 | + ) |
0 commit comments