Skip to content

Commit

Permalink
exception handling when optimization crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
hagne committed Sep 12, 2023
1 parent f2cc083 commit f8c089b
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions atmPy/aerosols/physics/optical_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -1638,8 +1638,8 @@ def replwl(b):

self.tp_arguments = arguments
self.tp_bounds = bounds

fit_res = scipy.optimize.least_squares(self.cost_fun,
try:
fit_res = scipy.optimize.least_squares(self.cost_fun,
# self.start_conditions.args,
arguments,
# sigma=None,
Expand All @@ -1662,9 +1662,16 @@ def replwl(b):
# Removed it none the less. I think the initial adjustment of amplitudes makes this unnecessary and I see better results witout it.
# col_deriv=True
)
except ValueError as e:
print(str(e))
fit_res = type('adhoc_fitres', (), {'success': False,
'x': arguments,
'cost': _np.nan})

if not fit_res.success:
if self.fit_method == 'dogbox' and self.rerun_if_fail:
fit_res = scipy.optimize.least_squares(self.cost_fun,
try:
fit_res = scipy.optimize.least_squares(self.cost_fun,
fit_res.x,
bounds=bounds,
jac = self.fit_jac,
Expand All @@ -1675,6 +1682,11 @@ def replwl(b):
x_scale = self.fit_scale_vars,
# max_nfev = self.max_nfev,
)
except ValueError as e:
print(str(e))
fit_res = type('adhoc_fitres', (), {'success': False,
'x': arguments,
'cost': _np.nan})
self.tp_fit_res = fit_res
#### computations cost finallize
cpu_percent = p.cpu_percent()
Expand Down

0 comments on commit f8c089b

Please sign in to comment.