Skip to content

Commit cdfa1ba

Browse files
barplots for global sensitivity
1 parent c0f1d46 commit cdfa1ba

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

src/sbmlsim/sensitivity/analysis.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
from sbmlsim.sensitivity.parameters import SensitivityParameter
2828
from sbmlsim.sensitivity.outputs import SensitivityOutput
29-
from sbmlsim.sensitivity.plots import heatmap
29+
from sbmlsim.sensitivity.plots import heatmap, sobol_barplot
3030

3131

3232
class SensitivitySimulation:
@@ -487,23 +487,40 @@ def calculate_sensitivity(self) -> None:
487487
def plot_sobol_indices(
488488
self,
489489
fig_path: Path,
490+
**kwargs
490491
):
491492
"""Barplots for the Sobol indices.
492493
493494
"""
494-
parameter_labels: dict[str, str] = {p.uid: f"{p.uid}: {p.name}" for p in self.parameters}
495+
# parameter_labels: dict[str, str] = {p.uid: f"{p.uid}: {p.name}" for p in self.parameters}
496+
parameter_labels: dict[str, str] = {p.uid: p.uid for p in self.parameters}
495497
output_labels: dict[str, str] = {q.uid: q.name for q in self.outputs}
496498

499+
ymax = self.sensitivity["ST"].max(dim=None)
500+
ymin = self.sensitivity["S1"].min(dim=None)
501+
console.print(f"{ymax=}")
497502

498503
for ko, output in enumerate(self.outputs):
504+
f_path = fig_path.parent / f"{fig_path.stem}_{output.uid}{fig_path.suffix}"
505+
499506
S1 = self.sensitivity["S1"][:, ko]
500507
ST = self.sensitivity["ST"][:, ko]
501508
S1_conf = self.sensitivity["S1_conf"][:, ko]
502509
ST_conf = self.sensitivity["ST_conf"][:, ko]
503510
console.print(S1)
504511
console.print(type(S1))
512+
sobol_barplot(
513+
S1=S1,
514+
ST=ST,
515+
S1_conf=S1_conf,
516+
ST_conf=ST_conf,
517+
title=output_labels[output.uid],
518+
fig_path=f_path,
519+
parameter_labels=parameter_labels,
520+
ymax=np.max([1.05, ymax]),
521+
ymin=np.min([-0.05, ymin]),
522+
)
505523

506-
break
507524

508525

509526

src/sbmlsim/sensitivity/plots.py

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,44 @@ def calculate_subset(df, cutoff=0.01) -> pd.DataFrame:
117117
plt.show()
118118

119119

120-
def sobol_barplot():
121-
pass
120+
def sobol_barplot(
121+
S1, ST, S1_conf, ST_conf,
122+
parameter_labels: dict[str, str],
123+
fig_path: Optional[Path] = None,
124+
title: Optional[str] = None,
125+
ymax: float = 1.1,
126+
ymin: float = -0.1,
127+
):
128+
# width
129+
figsize = (15, 3)
130+
label_fontsize = 15
131+
132+
categories: list[str] = list(parameter_labels.values())
133+
f, ax = plt.subplots(figsize=figsize)
134+
135+
ax.bar(categories, ST, label='ST',
136+
color="tab:orange",
137+
alpha=1.0,
138+
edgecolor="black",
139+
yerr=ST_conf, capsize=5
140+
)
141+
142+
ax.bar(categories, S1, label='S1', color="tab:blue",
143+
edgecolor="black", yerr=S1_conf, capsize=5)
144+
145+
146+
ax.set_xlabel('Parameter', fontsize=label_fontsize, fontweight="bold")
147+
ax.set_ylabel('Sobol Index', fontsize=label_fontsize, fontweight="bold")
148+
ax.set_ylim(bottom=ymin, top=ymax)
149+
ax.grid(True, axis="y")
150+
ax.tick_params(axis='x', labelrotation=90)
151+
# ax.tick_params(axis='x', labelweight='bold')
152+
ax.legend()
153+
154+
if title:
155+
plt.suptitle(title, fontsize=20, fontweight="bold")
156+
157+
if fig_path:
158+
plt.savefig(fig_path, dpi=300, bbox_inches="tight")
159+
plt.show()
160+

0 commit comments

Comments
 (0)