Open larnoldgithub opened 1 month ago
hi Luc, No I didn't see any error. I also got QCC_ALL == T for all files produced for 0824AB920Ff* (also for fiber A) Not sure how I can help you find out...
So all these errors are due to the apero_flat error that you posted:
E[40-015-00009]: Error fitting sinc to blaze. Order = 45 Fiber = A Iteration =
-1
Guess: amp=191774.0009571721 period=7034.0 lin=1205.0 slope=0 quad=0 (cube=0)
Lower: amp=0 period=0.0 lin=0.0 slope=-inf quad=-inf (cube=-1e-20)
Upper: amp=287661.00143575814 period=inf lin=4087 slope=inf quad=inf
(cube=1e-20)
Error <class 'RuntimeError'>: Optimal parameters not found: Number of calls to
function has reached maxfev = 1200.
And whats failing is the curve_fit function (which we've all seen fail at some point in different places).... it seems complete non-reproducible:
03:56:49.718-!!|FF[01223]|apero.core.core.drs_exceptions.LogExit: E[40-015-00009]: Error fitting sinc to blaze. Order = 45 Fiber = A Iteration = -1
03:56:49.719-!!|FF[01223]| Guess: amp=191774.0009571721 period=7034.0 lin=1205.0 slope=0 quad=0 (cube=0)
03:56:49.719-!!|FF[01223]| Lower: amp=0 period=0.0 lin=0.0 slope=-inf quad=-inf (cube=-1e-20)
03:56:49.720-!!|FF[01223]| Upper: amp=287661.00143575814 period=inf lin=4087 slope=inf quad=inf (cube=1e-20)
03:56:49.720-!!|FF[01223]| Error <class 'RuntimeError'>: Optimal parameters not found: Number of calls to function has reached maxfev = 1200.
03:56:49.721-!!|FF[01223]| Function = science.extract.extraction.py.calculate_blaze_flat_sinc()
I did add a loop where it simply tries again, but seems in your case this failed too (several times):
03:56:37.855-@$|FF[01223]|W[10-015-00001]: Sinc curve fit failed. Trying again. Tries = 0
03:56:40.809-@$|FF[01223]|W[10-015-00001]: Sinc curve fit failed. Trying again. Tries = 1
03:56:43.739-@$|FF[01223]|W[10-015-00001]: Sinc curve fit failed. Trying again. Tries = 2
03:56:46.724-@$|FF[01223]|W[10-015-00001]: Sinc curve fit failed. Trying again. Tries = 3
03:56:49.663-@$|FF[01223]|W[10-015-00001]: Sinc curve fit failed. Trying again. Tries = 4
03:56:49.669-!!|FF[01223]|E[40-015-00009]: Error fitting sinc to blaze. Order = 45 Fiber = A Iteration = -1
03:56:49.669-!!|FF[01223]| Guess: amp=191774.0009571721 period=7034.0 lin=1205.0 slope=0 quad=0 (cube=0)
03:56:49.670-!!|FF[01223]| Lower: amp=0 period=0.0 lin=0.0 slope=-inf quad=-inf (cube=-1e-20)
03:56:49.670-!!|FF[01223]| Upper: amp=287661.00143575814 period=inf lin=4087 slope=inf quad=inf (cube=1e-20)
03:56:49.671-!!|FF[01223]| Error <class 'RuntimeError'>: Optimal parameters not found: Number of calls to function has reached maxfev = 1200.
03:56:49.671-!!|FF[01223]| Function = science.extract.extraction.py.calculate_blaze_flat_sinc()
There's not much I can do to fix this - maybe someone needs to find a new function to replace curve_fit because I haven't changed anything with this function for v0.7.290 or the inputs (no change to pp data and that is all that comes in!).
The line of code is as follows:
from scipy.optimize import curve_fit
# we optimize over pixels that are not NaN (this time with
# no bounds)
popt, pcov = curve_fit(sinc_func, xpix[keep], e2ds[keep], p0=fit_guess1)
where:
e2ds
is one of the orders (i.e. 4088 pixels long)xpix
is np.arange(4088)
keep
is a np.array of bools (True/False) to mask out NaNsfit_guess1
is given in the error abovepopt
is the fit parameters in the same order as fit_guess1
sinc_func
is reproduced below
We also try first with the lower and upper bounds given above, then if all else fails without the bounds.import numpy as np
def sinc_func(x: np.ndarray, amp: float, period: float, lin_center: float,
slope: float, quad_scale: float = 0.0, cube_scale: float = 0.0,
peak_cut: float = 0.0) -> np.ndarray:
"""
Calculates the sinc function with a slope (and position threshold cut)
y = A * (sin(x)/x)^2 * (1 + C*x)
x = 2*pi*(X - dx + q2*dx^2 + q3*dx^3) / P
where X is a position along the x pixel axis. This assumes
that the blaze follows an airy pattern and that there may be a slope
to the spectral energy distribution.
if peak_cut is non-zero:
y < A * peak_cut = NaN
:param x: numpy array (1D), the x position vector y (X)
:param amp: float, the amplitude of the sinc function (A)
:param period: float, the period of the sinc function (P)
:param lin_center: float, the linear center of the sinc (dx)
:param quad_scale: float, the quad scale of the sinc (q2)
:param cube_scale: float, the cubic scale of the sinc (q3)
:param slope: the slope of the sinc function (C)
:param peak_cut: float, if non-zero if the threshold below the maximum
amplitude to set to NaNs
:type x: np.ndarray
:type amp: float
:type period: float
:type lin_center: float
:type quad_scale: float
:type cube_scale: float
:type slope: float
:type peak_cut: float
:return: the sinc function (with a slope) and if peak_cut non-zero NaN
filled before this fraction of the sinc max amplitude
:rtype: np.ndarray
"""
# set function name
# _ = display_func('sinc', __NAME__)
# Transform the x expressed in pixels into a stretched version
# expressed in phase. The quadratic terms allows for a variation of
# dispersion along the other
deltax = x - lin_center
cent = deltax + quad_scale * deltax ** 2 + cube_scale * deltax ** 3
xp = 2 * np.pi * (cent / period)
# this avoids a division by zero
if np.min(np.abs(xp) / period) < 1e-9:
small_x = (np.abs(xp) / period) < 1e-9
xp[small_x] = 1e-9
# calculate the sinc function
yy = amp * (np.sin(xp) / xp) ** 2
# if we set a peak_cut threshold then values below that fraction of the
# peak sinc value are set to NaN.
if peak_cut != 0:
yy[yy < (peak_cut * amp)] = np.nan
# multiplicative slope in the SED
yy *= (1 + slope * (x - lin_center))
# return the adjusted yy
return yy
So yep I'm pretty stuck on how to "fix" this, short of someone finding another optimized fitting function as if this fails you can't reduce the data (especially for the "reference" night as it did here).
The only thing I can say @larnoldgithub is to try resetting all of the mini data and re-running.
I actually got the error twice over the week end, posted the error after the second trial.
I never saw the fitting error before with the minidata2. For the 2 tests this week-end, the installation reset the folders but the run/ and raw/, so I'm not sure resetting and running a third trial will work. Im still going to try!
I didn't look into the details, but why assinging xp[small_x] = 1e-9 before calculating the sinc rather than just skipping that x value (assinging NaN)? with xp[small_x] forced to 1e-9, we end with 0.01 values for sinc which the fit eventually can't manage?
I don't believe you can assign a NaN in the curve fit - it leads to a crash. @eartigau wrote this function and as it says I believe it is just to stop the vector xp = 2 * np.pi * (cent / period)
having a zero (or negative value) and crashing that way. Note that xp
is a vector so the whole vector doesn't get forced to 1e-9 just very small, zero or negative values.
I never saw the fitting error before with the minidata2.
I have never seen it with the minidata or minidata2 either - nothing has changed in that code for a long time so I don't understand why you are having this problem now and that no one else is having the problem.
I'll try to get @eartigau to look into this.
third test: full reset but asset/ raw/ and run/ error again, exact same.
if I just run the command
''' apero_flat_spirou.py 2020-11-01 2539860f_pp.fits 2539861f_pp.fits 2539862f_pp.fits 2539863f_pp.fits 2539864f_pp.fits 2539865f_pp.fits 2539866f_pp.fits 2539867f_pp.fits 2539868f_pp.fits 2539869f_pp.fits 2539870f_pp.fits 2539871f_pp.fits 2539872f_pp.fits 2539873f_pp.fits 2539874f_pp.fits 2540063f_pp.fits 2540064f_pp.fits 2540065f_pp.fits 2540066f_pp.fits 2540067f_pp.fits 2540068f_pp.fits 2540069f_pp.fits 2540070f_pp.fits 2540071f_pp.fits 2540072f_pp.fits 2540073f_pp.fits 2540074f_pp.fits 2540075f_pp.fits 2540076f_pp.fits 2540077f_pp.fits --crunfile=mini_run2.ini --program=FF[01223] --recipe_kind=calib-night --shortname=FF --parallel=True''' it works, no errors.
I'm going to reprocess the minidata2, skipping the PP and calibs processing up to FF. I'm redoing everything after FF. I have reset the lbl/ only for that new test.
There is no difference to running it in apero_processing or like this so that is very strange.
You can run that one file with plots on (see the --help) to see if the fit is good.
All I can imagine is that it is an edge case that is on your system most the time failing and some times passing but this is bizarre as it should be the same each time.
the machine is a VM running Ubuntu 24.04 LTS (GNU/Linux 6.8.0-36-generic x86_64)
Hi @njcuk9999 Here are the errors at the end of the processing - below there is also the full log of ....apero_flat_spirou.log of 2020-11-01
@clairem789 did your test with the .290 end with no errors ?