|
| 1 | +import copy |
1 | 2 | import logging |
2 | 3 | import os |
3 | 4 | import subprocess # noqa: S404 |
4 | 5 | import tempfile |
5 | 6 | from typing import List |
6 | 7 |
|
7 | 8 | import numpy as np |
| 9 | +import pandas as pd |
8 | 10 |
|
9 | 11 | from ..model import Model |
10 | 12 | from ..parameters import Parameter |
| 13 | +from .utils import timethis |
11 | 14 |
|
12 | 15 | logger = logging.getLogger("ABC.External") |
13 | 16 |
|
@@ -242,6 +245,84 @@ def __call__(self, pars: Parameter): |
242 | 245 | def sample(self, pars): |
243 | 246 | return self(pars) |
244 | 247 |
|
| 248 | + @timethis |
| 249 | + def sample_timing(self, pars): |
| 250 | + return self(pars) |
| 251 | + |
| 252 | + def eval_param_limits(self, limits): |
| 253 | + """ |
| 254 | + evaluate single parameter's boundary value on computation time. |
| 255 | +
|
| 256 | + Parameters |
| 257 | + ---------- |
| 258 | + limits: dict |
| 259 | + the lower and upper boundary values of parameters. The key would |
| 260 | + be the parameter name and the value would be a list of the lower |
| 261 | + and upper limit of parameter value, e.g., [lower, upper]. |
| 262 | +
|
| 263 | + Returns |
| 264 | + ------- |
| 265 | + time_eval_dict: dict |
| 266 | + a dictionary that contains the parameter names as key and a list |
| 267 | + as a value. The list contains the computation time when using |
| 268 | + lower and upper limits, e.g., [lower, upper]. |
| 269 | + """ |
| 270 | + time_eval_dict = {} |
| 271 | + for key, val in limits.items(): |
| 272 | + lower_bound = self.sample_timing({key: val[0]}) |
| 273 | + upper_bound = self.sample_timing({key: val[1]}) |
| 274 | + time_eval_dict[key] = [lower_bound, upper_bound] |
| 275 | + return time_eval_dict |
| 276 | + |
| 277 | + def eval_param_limits_matrix(self, limits): |
| 278 | + """ |
| 279 | + evaluate two paramters' boundary values on computation time. |
| 280 | +
|
| 281 | + Parameters |
| 282 | + ---------- |
| 283 | + limits: dict |
| 284 | + the lower and upper boundary values of parameters. The key would |
| 285 | + be the parameter name and the value would be a list of the lower |
| 286 | + and upper limit of parameter value, e.g., [lower, upper]. |
| 287 | +
|
| 288 | + Returns |
| 289 | + ------- |
| 290 | + time_eval_mat_df_lower: df |
| 291 | + a dataframe for the computation time measured when using the lower |
| 292 | + limit value of parameters. |
| 293 | + time_eval_mat_df_upper: df |
| 294 | + a dataframe for the computation time measured when using the upper |
| 295 | + limit value of parameters. |
| 296 | + """ |
| 297 | + time_eval_mat = np.zeros(shape=(len(limits), len(limits))) |
| 298 | + time_eval_mat_df_lower = pd.DataFrame( |
| 299 | + time_eval_mat, |
| 300 | + columns=[list(limits.keys())], |
| 301 | + index=[list(limits.keys())], |
| 302 | + ) |
| 303 | + time_eval_mat_df_upper = copy.deepcopy(time_eval_mat_df_lower) |
| 304 | + for i, (key_col, val_col) in enumerate(limits.items(), 0): |
| 305 | + for j, (key_row, val_row) in enumerate(limits.items(), 0): |
| 306 | + if i < j: |
| 307 | + time_eval_mat_df_lower.loc[[key_col], [key_row]] = 0 |
| 308 | + time_eval_mat_df_upper.loc[[key_col], [key_row]] = 0 |
| 309 | + |
| 310 | + if key_col == key_row: |
| 311 | + lower_bound = self.sample_timing({key_col: val_col[0]}) |
| 312 | + upper_bound = self.sample_timing({key_col: val_col[1]}) |
| 313 | + |
| 314 | + else: |
| 315 | + lower_bound = self.sample_timing( |
| 316 | + {key_col: val_col[0], key_row: val_row[0]} |
| 317 | + ) |
| 318 | + lower_bound = self.sample_timing( |
| 319 | + {key_col: val_col[1], key_row: val_row[1]} |
| 320 | + ) |
| 321 | + time_eval_mat_df_lower.loc[[key_col], [key_row]] = lower_bound |
| 322 | + time_eval_mat_df_upper.loc[[key_col], [key_row]] = upper_bound |
| 323 | + |
| 324 | + return time_eval_mat_df_lower, time_eval_mat_df_upper |
| 325 | + |
245 | 326 |
|
246 | 327 | class ExternalSumStat: |
247 | 328 | """ |
|
0 commit comments