Skip to content

Add try/except around calls to pypeit.core.fittting.iterfit() #2033

@tbowers7

Description

@tbowers7

In the fitting function iterfit(), a ValueError is raised if there are no valid data points for the fitting:

else:
if not maskwork.any():
raise ValueError('No valid data points.')
# return (None,None)

This should be caught by the calling functions and should yield a context-specific error message that will help the user diagnose why there were no valid points in that particular fit. As the code stands, the above lines result in a semi-useless traceback. For consideration, this function also raises a ValueError if various inputs do not match length as they should -- which is a coding error rather than a problem with the input data. I suggest the above lines raise a different type of error that can be caught by the calling functions (perhaps a core-specific custom Exception).


As of the creation of this issue, pypeit.core.fittting.iterfit() is called 5 times from 2 different modules:

b_answer, bmask = fitting.iterfit(wave[indsp], flux_sm[indsp], invvar = fluxivar_sm[indsp],
kwargs_bspline={'everyn': 1.5}, kwargs_reject={'groupbadpix':True,'maxrej':1})
b_answer, bmask2 = fitting.iterfit(wave[indsp], flux_sm[indsp], invvar = fluxivar_sm[indsp]*bmask,
kwargs_bspline={'everyn': 1.5}, kwargs_reject={'groupbadpix':True,'maxrej':1})
c_answer, cmask = fitting.iterfit(wave[indsp], flux_sm[indsp], invvar = fluxivar_sm[indsp]*bmask2,
kwargs_bspline={'everyn': 30}, kwargs_reject={'groupbadpix':True,'maxrej':1})

PypeIt/pypeit/core/extract.py

Lines 1055 to 1056 in 81057b0

bset, bmask = fitting.iterfit(sigma_x.flat[si],norm_obj.flat[si], invvar = norm_ivar.flat[si],
nord = 4, bkpt = bkpt, maxiter = 15, upper = 1, lower = 1)

# init_breakpoints = fullbkpt
msgs.info("Bspline fit on zeropoint. ")
bset1, bmask = fitting.iterfit(wave, zeropoint_clean, invvar=zeropoint_ivar, inmask=zeropoint_fitmask, upper=upper, lower=lower,
fullbkpt=init_breakpoints, maxiter=maxiter, kwargs_bspline=kwargs_bspline,
kwargs_reject=kwargs_reject)
zeropoint_bspl, zeropoint_fit_gpm = bset1.value(wave)
zeropoint_bspl_bkpt, _ = bset1.value(init_breakpoints)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions