desihub / desispec

DESI spectral pipeline
BSD 3-Clause "New" or "Revised" License
33 stars 24 forks source link

Spectra object iteration doesn't work correctly #2191

Open sbailey opened 4 months ago

sbailey commented 4 months ago

We never explicitly added support for iterating over Spectra objects, but it is a reasonable thing to want to do and confusingly it "works" in a buggy way instead of just crashing or actually working. e.g.

for i, sp in enumerate(spectra[0:3]):
    print(i, len(sp), np.array(sp.fibermap['TARGETID']))

Result

0 1 [39628312921771046]
1 1 [39628312921772862]
2 1 [39628312925964626]
3 0 []
4 0 []
...

i.e. it iterates over the objects as requested, returning length=1 Spectra objects, but then it continues iterating returning length=0 objects instead of exiting the iteration.

I think it is reasonable for the iterator to return length=1 Spectra objects instead of inventing another Spectrum class for containing a single spectrum, but the iterator should stop on its own.

Thanks to Charlie Tolley for noticing and reporting the problem.