aburgasser / splat

SpeX Prism Spectral Analysis Toolkit

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

LinAlgError in .toWavelengths(wavegrid)

camille-004 opened this issue · comments

With a wavegrid of length 409, running the following gives these errors.

splat.initializeStandards()
standards_spex = splat.STDS_DWARF_SPEX
type(list(standards_spex.values())[0])

for k in list(splat.STDS_DWARF_SPEX.keys()):
    splat.STDS_DWARF_SPEX[k].toWavelengths(wavegrid)  # <-- Error
    splat.STDS_DWARF_SPEX[k].normalize([1, 1.3])

splat.STDS_DWARF_SPEX["L0.0"].plot();

Errors:

---------------------------------------------------------------------------
LinAlgError                               Traceback (most recent call last)
Input In [7], in <cell line: 1>()
      1 for k in list(splat.STDS_DWARF_SPEX.keys()):
----> 2     splat.STDS_DWARF_SPEX[k].toWavelengths(wavegrid)
      3     splat.STDS_DWARF_SPEX[k].normalize([1, 1.3])
      5 splat.STDS_DWARF_SPEX["L0.0"].plot()

File ~/Code/cool_star/splat/splat/core.py:1459, in Spectrum.toWavelengths(self, wave, force, verbose)
   1457 else:
   1458     self.flux = integralResample(self.wave.value,self.flux.value,wave.value)*flux_unit
-> 1459     self.noise = integralResample(self.wave.value,self.noise.value,wave.value)*flux_unit
   1460 self.wave = wave
   1461 self.variance = self.noise**2

File ~/Code/cool_star/splat/splat/utilities.py:2075, in integralResample(xh, yh, xl, nsamp, method)
   2070 #    plt.plot(xh,yh,color='k')
   2071 #    plt.plot(xl,ys,color='r')
   2073     elif method == 'fast':
   2074 #        print(xh,yh)
-> 2075         baseline = numpy.polynomial.Polynomial.fit(xh, yh, 4)
   2076         ip       = InterpolatedUnivariateSpline(xh, yh/baseline(xh), k=3)
   2077         ys       = baseline(xl)*ip(xl)

File ~/opt/anaconda3/envs/coolstar-dev/lib/python3.9/site-packages/numpy/polynomial/_polybase.py:980, in ABCPolyBase.fit(cls, x, y, deg, domain, rcond, full, w, window)
    977     window = cls.window
    979 xnew = pu.mapdomain(x, domain, window)
--> 980 res = cls._fit(xnew, y, deg, w=w, rcond=rcond, full=full)
    981 if full:
    982     [coef, status] = res

File ~/opt/anaconda3/envs/coolstar-dev/lib/python3.9/site-packages/numpy/polynomial/polynomial.py:1362, in polyfit(x, y, deg, rcond, full, w)
   1214 def polyfit(x, y, deg, rcond=None, full=False, w=None):
   1215     """
   1216     Least-squares fit of a polynomial to data.
   1217 
   (...)
   1360 
   1361     """
-> 1362     return pu._fit(polyvander, x, y, deg, rcond, full, w)

File ~/opt/anaconda3/envs/coolstar-dev/lib/python3.9/site-packages/numpy/polynomial/polyutils.py:660, in _fit(vander_f, x, y, deg, rcond, full, w)
    657 scl[scl == 0] = 1
    659 # Solve the least squares problem.
--> 660 c, resids, rank, s = np.linalg.lstsq(lhs.T/scl, rhs.T, rcond)
    661 c = (c.T/scl).T
    663 # Expand c to include non-fitted coefficients which are set to zero

File <__array_function__ internals>:180, in lstsq(*args, **kwargs)

File ~/opt/anaconda3/envs/coolstar-dev/lib/python3.9/site-packages/numpy/linalg/linalg.py:2292, in lstsq(a, b, rcond)
   2289 if n_rhs == 0:
   2290     # lapack can't handle n_rhs = 0 - so allocate the array one larger in that axis
   2291     b = zeros(b.shape[:-2] + (m, n_rhs + 1), dtype=b.dtype)
-> 2292 x, resids, rank, s = gufunc(a, b, rcond, signature=signature, extobj=extobj)
   2293 if m == 0:
   2294     x[...] = 0

File ~/opt/anaconda3/envs/coolstar-dev/lib/python3.9/site-packages/numpy/linalg/linalg.py:100, in _raise_linalgerror_lstsq(err, flag)
     99 def _raise_linalgerror_lstsq(err, flag):
--> 100     raise LinAlgError("SVD did not converge in Linear Least Squares")

LinAlgError: SVD did not converge in Linear Least Squares

When I rerun in the Jupyter notebook, I get a second error from this same line.

IndexError                                Traceback (most recent call last)
Input In [88], in <cell line: 1>()
      1 for k in list(splat.STDS_DWARF_SPEX.keys()):
----> 2     splat.STDS_DWARF_SPEX[k].toWavelengths(wavegrid)
      3     splat.STDS_DWARF_SPEX[k].normalize([1, 1.3])
      5 splat.STDS_DWARF_SPEX["L0.0"].plot()

File ~/Code/cool_star/splat/splat/core.py:1448, in Spectrum.toWavelengths(self, wave, force, verbose)
   1446         if flag==0:
   1447             self.toWaveUnit(wave.unit)
-> 1448             self.trim([numpy.nanmin(wave)-0.05*(numpy.nanmax(wave)-numpy.nanmin(wave)),numpy.nanmax(wave)+0.05*(numpy.nanmax(wave)-numpy.nanmin(wave))])
   1450 # map onto wavelength grid; if spectrum has lower resolution, interpolate; otherwise integrate & resample
   1451             flux_unit = self.flux.unit

File ~/Code/cool_star/splat/splat/core.py:2931, in Spectrum.trim(self, rng, **kwargs)
   2929 #        w = numpy.where(mask == 1)
   2930         self.wave = self.wave[mask == 1]
-> 2931         self.flux = self.flux[mask == 1]
   2932         self.noise = self.noise[mask == 1]
   2933         self.variance = self.variance[mask == 1]

File ~/opt/anaconda3/envs/coolstar-dev/lib/python3.9/site-packages/astropy/units/quantity.py:1197, in Quantity.__getitem__(self, key)
   1194     return self._new_view(self.view(np.ndarray)[key], self.unit[key])
   1196 try:
-> 1197     out = super().__getitem__(key)
   1198 except IndexError:
   1199     # We want zero-dimensional Quantity objects to behave like scalars,
   1200     # so they should raise a TypeError rather than an IndexError.
   1201     if self.isscalar:

IndexError: boolean index did not match indexed array along dimension 0; dimension is 409 but corresponding boolean dimension is 458```