Skip to content

Commit 507f1ad

Browse files
committed
Adding parameter estimate data class
1 parent e6950f8 commit 507f1ad

21 files changed

+344
-146
lines changed

.github/pull_request_template.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Example how to mark a checkbox :-
2323

2424
# How Has This Been Tested?
2525

26-
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce.
26+
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce.
2727

2828
# Describe if there is any unusual behaviour of your code(Write `NA` if there isn't)
2929

calisim/abc/pyabc_wrapper.py

+18-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from matplotlib import pyplot as plt
1515

1616
from ..base import CalibrationWorkflowBase
17-
from ..data_model import ParameterDataType
17+
from ..data_model import ParameterDataType, ParameterEstimateModel
1818

1919

2020
class PyABCApproximateBayesianComputation(CalibrationWorkflowBase):
@@ -183,22 +183,22 @@ def analyze(self) -> None:
183183

184184
distribution_dfs = pd.concat(distribution_dfs)
185185
outfile = self.join(
186-
outdir, f"{time_now}-{task}-{experiment_name}-parameters.csv"
186+
outdir, f"{time_now}-{task}-{experiment_name}_parameters.csv"
187187
)
188188
self.append_artifact(outfile)
189189
distribution_dfs.to_csv(outfile, index=False)
190190

191191
populations_df = self.history.get_all_populations()
192192
outfile = self.join(
193-
outdir, f"{time_now}-{task}-{experiment_name}-populations.csv"
193+
outdir, f"{time_now}-{task}-{experiment_name}_populations.csv"
194194
)
195195
self.append_artifact(outfile)
196196
populations_df.to_csv(outfile, index=False)
197197

198198
population_particles_df = self.history.get_nr_particles_per_population()
199199
outfile = self.join(
200200
outdir,
201-
f"{time_now}-{task}-{experiment_name}-nr_particles_per_population.csv",
201+
f"{time_now}-{task}-{experiment_name}_nr_particles_per_population.csv",
202202
)
203203
self.append_artifact(outfile)
204204
population_particles_df.to_csv(outfile, index=False)
@@ -211,7 +211,20 @@ def analyze(self) -> None:
211211
distances_df = pd.concat(distances_df)
212212

213213
outfile = self.join(
214-
outdir, f"{time_now}-{task}-{experiment_name}-distances.csv"
214+
outdir, f"{time_now}-{task}-{experiment_name}_distances.csv"
215215
)
216216
self.append_artifact(outfile)
217217
distances_df.to_csv(outfile, index=False)
218+
219+
max_t = distribution_dfs["t"].max()
220+
trace_df = distribution_dfs.query(f"t == {max_t}")
221+
trace_df = trace_df.drop(columns=["t", "w"])
222+
223+
for name in trace_df:
224+
estimate = trace_df[name].mean()
225+
uncertainty = trace_df[name].std()
226+
227+
parameter_estimate = ParameterEstimateModel(
228+
name=name, estimate=estimate, uncertainty=uncertainty
229+
)
230+
self.add_parameter_estimate(parameter_estimate)

calisim/abc/pymc_wrapper.py

+16-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from matplotlib import pyplot as plt
1515

1616
from ..base import CalibrationWorkflowBase
17+
from ..data_model import ParameterEstimateModel
1718

1819

1920
class PyMCApproximateBayesianComputation(CalibrationWorkflowBase):
@@ -111,7 +112,7 @@ def analyze(self) -> None:
111112

112113
if outdir is not None:
113114
outfile = self.join(
114-
outdir, f"{time_now}-{task}-{experiment_name}-{plot}.png"
115+
outdir, f"{time_now}-{task}-{experiment_name}_{plot}.png"
115116
)
116117
self.append_artifact(outfile)
117118
plt.tight_layout()
@@ -125,7 +126,7 @@ def _create_plot(plot_func: Callable, plot_kwargs: dict) -> None:
125126
if outdir is not None:
126127
outfile = self.join(
127128
outdir,
128-
f"{time_now}-{task}-{experiment_name}-{plot_func.__name__}.png",
129+
f"{time_now}-{task}-{experiment_name}_{plot_func.__name__}.png",
129130
)
130131
self.append_artifact(outfile)
131132
plt.tight_layout()
@@ -156,13 +157,23 @@ def _create_plot(plot_func: Callable, plot_kwargs: dict) -> None:
156157
return
157158

158159
trace_df = self.trace.to_dataframe(include_coords=False, groups="posterior")
159-
outfile = self.join(outdir, f"{time_now}-{task}-{experiment_name}-trace.csv")
160+
outfile = self.join(outdir, f"{time_now}-{task}-{experiment_name}_trace.csv")
160161
self.append_artifact(outfile)
161162
trace_df.to_csv(outfile, index=False)
162163

163-
trace_summary_df = az.summary(self.trace)
164+
trace_summary_df = az.summary(self.trace).reset_index()
165+
trace_summary_df = trace_summary_df.rename(columns={"index": "name"})
164166
outfile = self.join(
165-
outdir, f"{time_now}-{task}-{experiment_name}-trace_summary.csv"
167+
outdir, f"{time_now}-{task}-{experiment_name}_trace_summary.csv"
166168
)
167169
self.append_artifact(outfile)
168170
trace_summary_df.to_csv(outfile, index=False)
171+
172+
for row in trace_summary_df.to_dict("records"):
173+
name = row["name"]
174+
estimate = row["mean"]
175+
uncertainty = row["sd"]
176+
parameter_estimate = ParameterEstimateModel(
177+
name=name, estimate=estimate, uncertainty=uncertainty
178+
)
179+
self.add_parameter_estimate(parameter_estimate)

0 commit comments

Comments
 (0)