|
32 | 32 | from math import sqrt
|
33 | 33 | import numpy as np
|
34 | 34 | import os
|
| 35 | +import pandas as pd |
35 | 36 | import scipy.linalg as LA
|
36 | 37 | import scipy.stats as STAT
|
37 | 38 | import warnings
|
|
41 | 42 | from .params import *
|
42 | 43 | from .util import *
|
43 | 44 |
|
44 |
| -__all__ = ['solve'] |
| 45 | +__all__ = ['solve', 'OptimResults'] |
45 | 46 |
|
46 | 47 | module_logger = logging.getLogger(__name__)
|
47 | 48 |
|
@@ -102,6 +103,50 @@ def __str__(self):
|
102 | 103 | output += "%s\n" % self.msg
|
103 | 104 | output += "****************************\n"
|
104 | 105 | return output
|
| 106 | + |
| 107 | + def to_dict(self, replace_nan=True): |
| 108 | + # Convert to a serializable dict object suitable for saving in a json file |
| 109 | + # If replace_nan=True, convert all NaN entries to None |
| 110 | + soln_dict = {} |
| 111 | + soln_dict['x'] = self.x.tolist() if self.x is not None else None |
| 112 | + soln_dict['resid'] = self.resid.tolist() if self.resid is not None else None |
| 113 | + soln_dict['obj'] = float(self.obj) |
| 114 | + soln_dict['jacobian'] = self.jacobian.tolist() if self.jacobian is not None else None |
| 115 | + soln_dict['nf'] = int(self.nf) |
| 116 | + soln_dict['nx'] = int(self.nx) |
| 117 | + soln_dict['nruns'] = int(self.nruns) |
| 118 | + soln_dict['flag'] = int(self.flag) |
| 119 | + soln_dict['msg'] = str(self.msg) |
| 120 | + soln_dict['diagnostic_info'] = self.diagnostic_info.to_dict() if self.diagnostic_info is not None else None |
| 121 | + soln_dict['xmin_eval_num'] = int(self.xmin_eval_num) |
| 122 | + soln_dict['jacmin_eval_nums'] = self.jacmin_eval_nums.tolist() if self.jacmin_eval_nums is not None else None |
| 123 | + if replace_nan: |
| 124 | + return replace_nan_with_none(soln_dict) |
| 125 | + else: |
| 126 | + return soln_dict |
| 127 | + |
| 128 | + @staticmethod |
| 129 | + def from_dict(soln_dict): |
| 130 | + # Take a dict object containing OptimResults information, and return the relevant OptimResults object |
| 131 | + # Input soln_dict should come from soln.to_dict() |
| 132 | + # Note: np.array(mylist, dtype=float) automatically converts None to NaN |
| 133 | + x = np.array(soln_dict['x'], dtype=float) if soln_dict['x'] is not None else None |
| 134 | + resid = np.array(soln_dict['resid'], dtype=float) if soln_dict['resid'] is not None else None |
| 135 | + obj = soln_dict['obj'] |
| 136 | + jacobian = np.array(soln_dict['jacobian'], dtype=float) if soln_dict['jacobian'] is not None else None |
| 137 | + nf = soln_dict['nf'] |
| 138 | + nx = soln_dict['nx'] |
| 139 | + nruns = soln_dict['nruns'] |
| 140 | + flag = soln_dict['flag'] |
| 141 | + msg = soln_dict['msg'] |
| 142 | + xmin_eval_num = soln_dict['xmin_eval_num'] |
| 143 | + jacmin_eval_nums = np.array(soln_dict['jacmin_eval_nums'], dtype=int) if soln_dict['jacmin_eval_nums'] is not None else None |
| 144 | + |
| 145 | + soln = OptimResults(x, resid, obj, jacobian, nf, nx, nruns, flag, msg, xmin_eval_num, jacmin_eval_nums) |
| 146 | + |
| 147 | + if soln_dict['diagnostic_info'] is not None: |
| 148 | + soln.diagnostic_info = pd.DataFrame.from_dict(soln_dict['diagnostic_info']) |
| 149 | + return soln |
105 | 150 |
|
106 | 151 |
|
107 | 152 | def solve_main(objfun, x0, argsf, xl, xu, projections, npt, rhobeg, rhoend, maxfun, nruns_so_far, nf_so_far, nx_so_far, nsamples, params,
|
|
0 commit comments