njcuk9999 / lbl

Line by line code for radial velocity
MIT License
15 stars 3 forks source link

CurveFit exception #50

Open clairem789 opened 3 months ago

clairem789 commented 3 months ago

Hello, I am running into CurveFit exceptions with stars of broad CCF. The exception also occurs when I'm using another star as template, which looks weird. Also, it happens now while it was not an issue before on the same stars. Also, sometimes the exception does not kill the LBL and I get the table at the end, but in one occurrence (example below) it does crash the job and produces no new file, and no rdb table. Any clue? Anyone having this too? Thanks!

This is an example with FUORI:

Traceback (most recent call last): File "/Users/moutou/miniconda3/envs/ap288/lib/python3.9/site-packages/lbl/recipes/lbl_compute.py", line 81, in main namespace = main(inst) File "/Users/moutou/miniconda3/envs/ap288/lib/python3.9/site-packages/lbl/recipes/lbl_compute.py", line 159, in main systemic_vel_props = general.get_systemic_vel_props(inst, template_file, File "/Users/moutou/miniconda3/envs/ap288/lib/python3.9/site-packages/lbl/science/general.py", line 550, in get_systemic_vel_props raise LblException(emsg) lbl.core.base_classes.LblException: CurveFit exception in rough CCF Iteration = 1 P0 = [-1.92436321e+04 4.21759601e+04 2.08085222e+01 -1.84596027e+00 3.42332460e+00] Function = science.general.py.get_systemic_vel_props() Error: <class 'RuntimeError'>: Optimal parameters not found: Number of calls to function has reached maxfev = 1200.

njcuk9999 commented 3 months ago

So this is for a specific file (as its with lbl_compute) the crash means that scipy curve fit can not do a fit which normally means the data is bad

The fit here is a gaussian fit to the CCF between the mask "depth" and the spectrum - which means its hard from this to see from the input data what the CCF looks like. You could try to calculate a ccf externally and see if it looks like it could be fit from a single gaussian. (the P0 are the gaussian parameters to the following function, except x which is passed in by the curve_fit function, so `P0 = [x0, fwhm, amp, ears, expo]

def gauss_fit_e(x: Union[float, np.ndarray], x0: float, fwhm: float,
                amp: float, ears: float, expo: float) -> Union[float, np.ndarray]:
    """
    Gaussian fit with a shape factor and ears

    :param x: numpy array (1D), the x values for the gauss fit
    :param x0: float, the mean position
    :param fwhm: float, the FWHM
    :param amp: float, the amplitude
    :param ears: float, the ears (wings of the gaussian)
    :param expo: float, the shape factor

    :return: np.ndarray - the gaussian value
    """
    # gaussian FWHM = 2*sqrt(2*ln(2))*sigma
    ew = fwhm / (2 * (2 * np.log(2)) ** (1 / expo))

    g1 = amp * np.exp(-np.abs(x - x0) ** expo / ew ** expo)
    g2 = (amp / ears ** 2) * np.exp(-np.abs(x - x0) ** expo / (ew * 2) ** expo)

    return 1 - g1 + g2

I will note that line references are different to mine so you might have an old version - so my first recommendation would be to update lbl (we are on v0.63.007). You curve fit seems to happen on line 550 whereas ours would be around line 664.

Does this file always cause an exception? If so you could pass the file to me and I could try running it to the point in the code to plot the CCF.

I might also be able to add a way to get a debug plot when it crashes (and assuming you have plot_xxxx = True) so we could actually see the CCF as it is under Etienne's implementation - that might at least allow us to see if the data is bad / give a reason for why we can't fit a gaussian to it.

clairem789 commented 3 months ago

OK, I will update and rerun and let you know if it solves the problem

clairem789 commented 3 months ago

After a pull, this is what I got - same error, the line number has changed (81 to 83 in lbl_ompute)

Traceback (most recent call last): File "/Users/moutou/lbl/wrap/OBJ/wrap_uni.py", line 80, in lbl_wrap(rparams) File "/Users/moutou/miniconda3/envs/ap288/lib/python3.9/site-packages/lbl/recipes/lbl_wrap.py", line 179, in main lbl_compute.main(instrument=instrument, data_dir=data_dir, File "/Users/moutou/miniconda3/envs/ap288/lib/python3.9/site-packages/lbl/recipes/lbl_compute.py", line 83, in main raise LblException(e.message, verbose=False) lbl.core.base_classes.LblException: CurveFit exception in rough CCF Iteration = 1 P0 = [-1.92436321e+04 4.21759601e+04 2.08085222e+01 -1.84596027e+00 3.42332460e+00] Function = science.general.py.get_systemic_vel_props() Error: <class 'RuntimeError'>: Optimal parameters not found: Number of calls to function has reached maxfev = 1200.

njcuk9999 commented 3 months ago

Could you send me (via email) the one file you used for this, I'll see if I can get the same error as well.