Skip to content

Commit 9515f6b

Browse files
committed
Added error handling for the "ValueError: Residuals are not finite in the initial point." that has started popping up. I am also investigating how to avoid it happening. For issue #1162
1 parent 832fdc4 commit 9515f6b

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

xga/products/base.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This code is a part of X-ray: Generate and Analyse (XGA), a module designed for the XMM Cluster Survey (XCS).
2-
# Last modified by David J Turner (turne540@msu.edu) 24/07/2024, 16:16. Copyright (c) The Contributors
2+
# Last modified by David J Turner (turne540@msu.edu) 25/07/2024, 11:04. Copyright (c) The Contributors
33

44
import inspect
55
import os
@@ -1015,6 +1015,8 @@ def nlls_fit(self, model: BaseModel1D, num_samples: int, show_warn: bool) -> Tup
10151015

10161016
# Curve fit is a simple non-linear least squares implementation, its alright but fragile
10171017
try:
1018+
print(y_errs)
1019+
print(lower_bounds, upper_bounds)
10181020
fit_par, fit_cov = curve_fit(model.model, rads, y_data, p0=model.unitless_start_pars, sigma=y_errs,
10191021
absolute_sigma=True, bounds=(lower_bounds, upper_bounds))
10201022

@@ -1031,11 +1033,17 @@ def nlls_fit(self, model: BaseModel1D, num_samples: int, show_warn: bool) -> Tup
10311033
warning_str = "Very large parameter uncertainties"
10321034
success = False
10331035
except RuntimeError as r_err:
1034-
warn("{}, curve_fit has failed.".format(str(r_err)))
1036+
warn("{}, curve_fit has failed.".format(str(r_err)), stacklevel=2)
10351037
warning_str = str(r_err)
10361038
success = False
10371039
fit_par = np.full(len(model.model_pars), np.nan)
10381040
fit_par_err = np.full(len(model.model_pars), np.nan)
1041+
except ValueError as v_err:
1042+
warn("{}, curve_fit has failed.".format(str(v_err)), stacklevel=2)
1043+
warning_str = str(v_err)
1044+
success = False
1045+
fit_par = np.full(len(model.model_pars), np.nan)
1046+
fit_par_err = np.full(len(model.model_pars), np.nan)
10391047

10401048
# Using the parameter values and the covariance matrix I generate parameter distributions to store in the
10411049
# model instance.
@@ -2852,8 +2860,8 @@ def view(self, figsize: Tuple = (10, 7), xscale: str = "log", yscale: str = "log
28522860
else:
28532861
main_leg = main_ax.legend(loc="upper left", bbox_to_anchor=(1.01, 1), ncol=1, borderaxespad=0)
28542862
# This makes sure legend keys are shown, even if the data is hidden
2855-
for leg_key in main_leg.legendHandles:
2856-
leg_key.set_visible(True)
2863+
# for leg_key in main_leg.legendHandles:
2864+
# leg_key.set_visible(True)
28572865

28582866
# We specify which axes object needs formatters applied, depends on whether the residual ax is being
28592867
# shown or not - slightly dodgy way of checking for a local declaration of the residual axes
@@ -2892,17 +2900,3 @@ def __add__(self, other):
28922900
raise TypeError("You may only add 1D Profiles, 1D Aggregate Profiles, or a list of 1D profiles"
28932901
" to this object.")
28942902
return BaseAggregateProfile1D(to_combine)
2895-
2896-
2897-
2898-
2899-
2900-
2901-
2902-
2903-
2904-
2905-
2906-
2907-
2908-

0 commit comments

Comments
 (0)