|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +from astropy import units as u |
| 4 | +from astropy.io import fits |
| 5 | +from astropy.nddata import CCDData, NDData, VarianceUncertainty |
| 6 | +from astropy.utils.data import download_file |
| 7 | + |
| 8 | +from specreduce.extract import HorneExtract |
| 9 | +from specreduce.tracing import FlatTrace |
| 10 | +from specutils import Spectrum1D, SpectralAxis |
| 11 | + |
| 12 | +# fetch test image |
| 13 | +fn = download_file('https://stsci.box.com/shared/static/exnkul627fcuhy5akf2gswytud5tazmw.fits', |
| 14 | + cache=True) |
| 15 | + |
| 16 | +# duplicate image in all accepted formats |
| 17 | +# (one Spectrum1D variant has a physical spectral axis; the other is in pixels) |
| 18 | +img = fits.getdata(fn).T |
| 19 | +flux = img * u.MJy / u.sr |
| 20 | +sax = SpectralAxis(np.linspace(14.377, 3.677, flux.shape[-1]) * u.um) |
| 21 | +unc = VarianceUncertainty(np.random.rand(*flux.shape)) |
| 22 | + |
| 23 | +all_images = {} |
| 24 | +all_images['arr'] = img |
| 25 | +all_images['s1d'] = Spectrum1D(flux, spectral_axis=sax, uncertainty=unc) |
| 26 | +all_images['s1d_pix'] = Spectrum1D(flux, uncertainty=unc) |
| 27 | +all_images['ccd'] = CCDData(img, uncertainty=unc, unit=flux.unit) |
| 28 | +all_images['ndd'] = NDData(img, uncertainty=unc, unit=flux.unit) |
| 29 | +all_images['qnt'] = img * flux.unit |
| 30 | + |
| 31 | +# save default values used for spectral axis and uncertainty when they are not |
| 32 | +# available from the image object or provided by the user |
| 33 | +sax_def = np.arange(img.shape[1]) * u.pix |
| 34 | +unc_def = np.ones_like(img) |
| 35 | + |
| 36 | + |
| 37 | +# (for use inside tests) |
| 38 | +def compare_images(key, collection, compare='s1d'): |
| 39 | + # was input converted to Spectrum1D? |
| 40 | + assert isinstance(collection[key], Spectrum1D), (f"image '{key}' not " |
| 41 | + "of type Spectrum1D") |
| 42 | + |
| 43 | + # do key's fluxes match its comparison's fluxes? |
| 44 | + assert np.allclose(collection[key].data, |
| 45 | + collection[compare].data), (f"images '{key}' and " |
| 46 | + f"'{compare}' have unequal " |
| 47 | + "flux values") |
| 48 | + |
| 49 | + # if the image came with a spectral axis, was it kept? if not, was the |
| 50 | + # default spectral axis in pixels applied? |
| 51 | + sax_provided = hasattr(all_images[key], 'spectral_axis') |
| 52 | + assert np.allclose(collection[key].spectral_axis, |
| 53 | + (all_images[key].spectral_axis if sax_provided |
| 54 | + else sax_def)), (f"spectral axis of image '{key}' does " |
| 55 | + f"not match {'input' if sax_provided else 'default'}") |
| 56 | + |
| 57 | + # if the image came with an uncertainty, was it kept? if not, was the |
| 58 | + # default uncertainty created? |
| 59 | + unc_provided = hasattr(all_images[key], 'uncertainty') |
| 60 | + assert np.allclose(collection[key].uncertainty.array, |
| 61 | + (all_images[key].uncertainty.array if unc_provided |
| 62 | + else unc_def)), (f"uncertainty of image '{key}' does " |
| 63 | + f"not match {'input' if unc_provided else 'default'}") |
| 64 | + |
| 65 | + # were masks created despite none being given? (all indices should be False) |
| 66 | + assert (getattr(collection[key], 'mask', None) |
| 67 | + is not None), f"no mask was created for image '{key}'" |
| 68 | + assert np.all(collection[key].mask == 0), ("mask not all False " |
| 69 | + f"for image '{key}'") |
| 70 | + |
| 71 | + |
| 72 | +# test consistency of general image parser results |
| 73 | +def test_parse_general(): |
| 74 | + all_images_parsed = {k: FlatTrace._parse_image(object, im) |
| 75 | + for k, im in all_images.items()} |
| 76 | + |
| 77 | + for key in all_images_parsed.keys(): |
| 78 | + compare_images(key, all_images_parsed) |
| 79 | + |
| 80 | + |
| 81 | +# use verified general image parser results to check HorneExtract's image parser |
| 82 | +def test_parse_horne(): |
| 83 | + # HorneExtract's parser is more stringent than the general one, hence the |
| 84 | + # separate test. Given proper inputs, both should produce the same results. |
| 85 | + images_collection = {k: {} for k in all_images.keys()} |
| 86 | + |
| 87 | + for key, col in images_collection.items(): |
| 88 | + img = all_images[key] |
| 89 | + col['general'] = FlatTrace._parse_image(object, img) |
| 90 | + |
| 91 | + if hasattr(all_images[key], 'uncertainty'): |
| 92 | + defaults = {} |
| 93 | + else: |
| 94 | + # save default values of attributes used in general parser when |
| 95 | + # they are not available from the image object. HorneExtract always |
| 96 | + # requires a variance, so it's chosen here to be on equal footing |
| 97 | + # with the general case |
| 98 | + defaults = {'variance': unc_def, |
| 99 | + 'mask': np.ma.masked_invalid(img).mask, |
| 100 | + 'unit': getattr(img, 'unit', u.DN)} |
| 101 | + |
| 102 | + col[key] = HorneExtract._parse_image(object, img, **defaults) |
| 103 | + |
| 104 | + compare_images(key, col, compare='general') |
0 commit comments