mzechmeister / serval

calculate radial velocities from stellar spectra
MIT License
36 stars 9 forks source link

LinAlgError: The data may contain too large gaps. #59

Closed mzechmeister closed 9 months ago

mzechmeister commented 9 months ago

If the data contains gaps or poorly sampled egdes, cspline.py can raise a warning in https://github.com/mzechmeister/serval/blob/ebe7190c96de86eae0b8f74017772e17565cd009/src/cspline.py#L611-L613

For gaps longer than about 4 knots (e.g. due to bad column), the cubic spline looses the support and the design matrix contains rows having only zeros. Similar, poorly sampled data (e.g. little barycentric RV spread, single transit nights) can lead to ill posed matrices (overfitting).

You may use the -ofac option to adjust the knot spacing. Or the -pspline option; even a tiny regularisation (e.g. 0.00001) may overcome the problem. Both create smoother templates, which often gives more robust RVs. Also just more data may solve the problem. Please check the template creation with the -lookt option.

Here is an illustration of the problem. On the left side there is support from one only spectrum (with ofac=1). The fit seems to go through each data point, but shows large ringing (overfitting). On the right side a second spectrum helps to mitigate the ringing.

Plot created with

serval test /data/EXPRES/hd3651 -inst EXPRES -targrv 0 -nset ::30 -lookt -bp 1689 -pmax 1200
(Pdb) smod, ymod,cc = spl.ucbspl_fit(wmod[ind], mod[ind], we[ind], K=nk, lam=pspllam, mu=mu, e_mu=e_mu, e_yk=True, retfit=True, cov=True)
(Pdb) gplot(wmod[ind], mod[ind],'t "data",', *smod.osamp(20), 'w l lc 3 t "cubic spline",', smod.xk, smod(), 'lc 3 pt 7 ps 0.7 t "knots"')

The cov option was to used here. It employs np.linalg.inv, which still could give a solution, while cholbnd gave more nonsense.