예제 #1
0
def test_template_match_with_resample():
    """
    Test template_match when both observed and template spectra have different wavelength axis using resampling.
    """
    np.random.seed(42)

    # Create test spectra
    spec_axis1 = np.linspace(0, 50, 50) * u.AA
    spec_axis2 = np.linspace(0, 50, 50) * u.AA
    spec = Spectrum1D(spectral_axis=spec_axis1,
                      flux=np.random.randn(50) * u.Jy,
                      uncertainty=StdDevUncertainty(np.random.sample(50), unit='Jy'))

    spec1 = Spectrum1D(spectral_axis=spec_axis2,
                       flux=np.random.randn(50) * u.Jy,
                       uncertainty=StdDevUncertainty(np.random.sample(50), unit='Jy'))

    # Get result from template_match
    tm_result = template_comparison.template_match(spec, spec1)

    # Create new spectrum for comparison
    spec_result = Spectrum1D(spectral_axis=spec_axis1,
                             flux=spec1.flux * template_comparison._normalize_for_template_matching(spec, spec1))

    assert quantity_allclose(tm_result[0].flux, spec_result.flux, atol=0.01*u.Jy)
    np.testing.assert_almost_equal(tm_result[3], 40093.28353756253)
예제 #2
0
def test_create_collection_from_collections():
    spec = Spectrum1D(spectral_axis=np.linspace(0, 50, 50) * u.AA,
                      flux=np.random.randn(50) * u.Jy,
                      uncertainty=StdDevUncertainty(np.random.sample(50),
                                                    unit='Jy'))
    spec1 = Spectrum1D(spectral_axis=np.linspace(20, 60, 50) * u.AA,
                       flux=np.random.randn(50) * u.Jy,
                       uncertainty=StdDevUncertainty(np.random.sample(50),
                                                     unit='Jy'))

    spec_coll1 = SpectrumCollection.from_spectra([spec, spec1])

    spec2 = Spectrum1D(spectral_axis=np.linspace(40, 80, 50) * u.AA,
                       flux=np.random.randn(50) * u.Jy,
                       uncertainty=StdDevUncertainty(np.random.sample(50),
                                                     unit='Jy'))

    spec_coll2 = SpectrumCollection.from_spectra([spec, spec2])

    spec_coll = SpectrumCollection.from_spectra(
        [spec_coll1, spec_coll2, spec_coll1])

    assert spec_coll.ndim == 2
    assert spec_coll.shape == (3, 2)
    assert spec_coll.nspectral == 50
    assert isinstance(spec_coll.flux, u.Quantity)
    assert isinstance(spec_coll.spectral_axis, u.Quantity)
    assert spec.spectral_axis.unit == spec_coll.spectral_axis.unit
    assert spec.flux.unit == spec_coll.flux.unit
예제 #3
0
def test_template_match_no_overlap():
    """
    Test template_match when both observed and template spectra have no overlap on the wavelength axis.
    """
    # Seed np.random so that results are consistent
    np.random.seed(42)

    # Create test spectra
    spec_axis = np.linspace(0, 50, 50) * u.AA
    spec_axis_no_overlap = np.linspace(51, 102, 50) * u.AA

    spec = Spectrum1D(spectral_axis=spec_axis,
                      flux=np.random.randn(50) * u.Jy,
                      uncertainty=StdDevUncertainty(np.random.sample(50), unit='Jy'))

    spec1 = Spectrum1D(spectral_axis=spec_axis_no_overlap,
                       flux=np.random.randn(50) * u.Jy,
                       uncertainty=StdDevUncertainty(np.random.sample(50), unit='Jy'))

    # Get result from template_match
    tm_result = template_comparison.template_match(spec, spec1)

    # Create new spectrum for comparison
    spec_result = Spectrum1D(spectral_axis=spec_axis,
                             flux=spec1.flux * template_comparison._normalize_for_template_matching(spec, spec1))

    # assert quantity_allclose(tm_result[0].flux, spec_result.flux, atol=0.01*u.Jy)
    assert np.isnan(tm_result[3])
예제 #4
0
def test_template_match_list():
    """
    Test template_match when template spectra are in a list.
    """
    np.random.seed(42)

    # Create test spectra
    spec_axis1 = np.linspace(0, 50, 50) * u.AA
    spec_axis2 = np.linspace(0, 50, 50) * u.AA
    spec = Spectrum1D(spectral_axis=spec_axis1,
                      flux=np.random.randn(50) * u.Jy,
                      uncertainty=StdDevUncertainty(np.random.sample(50),
                                                    unit='Jy'))

    spec1 = Spectrum1D(spectral_axis=spec_axis2,
                       flux=np.random.randn(50) * u.Jy,
                       uncertainty=StdDevUncertainty(np.random.sample(50),
                                                     unit='Jy'))
    spec2 = Spectrum1D(spectral_axis=spec_axis2,
                       flux=np.random.randn(50) * u.Jy,
                       uncertainty=StdDevUncertainty(np.random.sample(50),
                                                     unit='Jy'))

    # Combine spectra into list
    template_list = [spec1, spec2]

    # Get result from template_match
    tm_result = template_comparison.template_match(spec, template_list)

    np.testing.assert_almost_equal(tm_result[3], 40093.28353756253)

    # make sure that multiple template spectra will create a list of
    # chi2 values, one per template.
    assert len(tm_result) == 5
예제 #5
0
def test_template_known_redshift():
    """
    Test template match when the redshift is known.
    """
    # Seed np.random so that results are consistent
    np.random.seed(42)

    # Create test spectra
    spec_axis = np.linspace(0, 50, 50) * u.AA
    perm_flux = np.random.randn(50) * u.Jy

    redshift = 3

    # Observed spectrum
    spec = Spectrum1D(spectral_axis=spec_axis * (1 + redshift),
                      flux=perm_flux,
                      uncertainty=StdDevUncertainty(np.random.sample(50),
                                                    unit='Jy'))

    # Template spectrum
    spec1 = Spectrum1D(spectral_axis=spec_axis,
                       flux=perm_flux,
                       uncertainty=StdDevUncertainty(np.random.sample(50),
                                                     unit='Jy'))

    tm_result = template_comparison.template_match(
        observed_spectrum=spec,
        spectral_templates=spec1,
        resample_method="flux_conserving",
        redshift=redshift)
    assert len(tm_result) == 5
    np.testing.assert_almost_equal(tm_result[1], redshift)
    np.testing.assert_almost_equal(tm_result[3], 1.9062409482056814e-31)
예제 #6
0
def test_template_match_spectrum_collection():
    """
    Test template_match when template spectra are in a SpectrumCollection object.
    """
    np.random.seed(42)

    # Create test spectra
    spec_axis1 = np.linspace(0, 50, 50) * u.AA
    spec_axis2 = np.linspace(0, 50, 50) * u.AA
    spec = Spectrum1D(spectral_axis=spec_axis1,
                      flux=np.random.randn(50) * u.Jy,
                      uncertainty=StdDevUncertainty(np.random.sample(50)))
    spec1 = Spectrum1D(spectral_axis=spec_axis2,
                       flux=np.random.randn(50) * u.Jy,
                       uncertainty=StdDevUncertainty(np.random.sample(50)))
    spec2 = Spectrum1D(spectral_axis=spec_axis2,
                       flux=np.random.randn(50) * u.Jy,
                       uncertainty=StdDevUncertainty(np.random.sample(50)))

    # Combine spectra into SpectrumCollection object
    spec_coll = SpectrumCollection.from_spectra([spec1, spec2])

    # Get result from template_match
    tm_result = template_comparison.template_match(spec, spec_coll)

    np.testing.assert_almost_equal(tm_result[3], 40093.28353756253)
예제 #7
0
def test_snr_multiple_flux(simulated_spectra):
    """
    Test the simple version of the spectral SNR, with multiple flux per single dispersion.
    """

    np.random.seed(42)

    #
    #  Set up the data and add the uncertainty and calculate the expected SNR
    #

    uncertainty = StdDevUncertainty(0.1 * np.random.random((5, 10)) * u.mJy)
    spec = Spectrum1D(spectral_axis=np.arange(10) * u.AA,
                      flux=np.random.sample((5, 10)) * u.Jy,
                      uncertainty=uncertainty)
    snr_spec = snr(spec)
    assert np.allclose(
        np.array(snr_spec),
        [18.20863867, 31.89475309, 14.51598119, 22.24603204, 32.01461421])

    uncertainty = StdDevUncertainty(0.1 * np.random.random(10) * u.mJy)
    spec = Spectrum1D(spectral_axis=np.arange(10) * u.AA,
                      flux=np.random.sample(10) * u.Jy,
                      uncertainty=uncertainty)
    snr_spec = snr(spec)

    assert np.allclose(np.array(snr_spec), 31.325265361800415)
예제 #8
0
    def __init__(self,
                 data,
                 wcs=None,
                 parent=None,
                 layer_mask=None,
                 uncertainty=None,
                 unit=None,
                 mask=None,
                 *args,
                 **kwargs):
        # if not issubclass(data.__class__, Spectrum1DRefLayer):
        if uncertainty is None:
            uncertainty = StdDevUncertainty(np.zeros(data.shape))
        elif isinstance(uncertainty, UnknownUncertainty):
            uncertainty = StdDevUncertainty(uncertainty.array)

        if mask is None:
            mask = np.zeros(data.shape).astype(bool)

        super(Spectrum1DRefLayer, self).__init__(data,
                                                 wcs=wcs,
                                                 unit=unit,
                                                 uncertainty=uncertainty,
                                                 mask=mask,
                                                 *args,
                                                 **kwargs)
        self._parent = parent
        self._layer_mask = layer_mask
예제 #9
0
def test_ccd_process_gain_corrected():
    # test the through ccd_process with gain_corrected as False
    ccd_data = CCDData(10.0 * np.ones((100, 100)), unit=u.adu)
    ccd_data.data[:, -10:] = 2
    ccd_data.meta['testkw'] = 100

    mask = np.zeros((100, 90))

    masterbias = CCDData(4.0 * np.ones((100, 90)), unit=u.adu)
    masterbias.uncertainty = StdDevUncertainty(np.zeros((100, 90)))

    dark_frame = CCDData(0.0 * np.ones((100, 90)), unit=u.adu)
    dark_frame.uncertainty = StdDevUncertainty(np.zeros((100, 90)))

    masterflat = CCDData(5.0 * np.ones((100, 90)), unit=u.adu)
    masterflat.uncertainty = StdDevUncertainty(np.zeros((100, 90)))

    occd = ccd_process(ccd_data, oscan=ccd_data[:, -10:], trim='[1:90,1:100]',
                       error=True, master_bias=masterbias,
                       master_flat=masterflat, dark_frame=dark_frame,
                       bad_pixel_mask=mask, gain=0.5 * u.electron/u.adu,
                       readnoise=5**0.5 * u.electron, oscan_median=True,
                       dark_scale=False, dark_exposure=1.*u.s,
                       data_exposure=1.*u.s, gain_corrected=False)

    # final results should be (10 - 2) / 2.0 - 2 = 2
    # error should be (4 + 5)**0.5 / 0.5  = 3.0

    np.testing.assert_array_equal(2.0 * np.ones((100, 90)), occd.data)
    np.testing.assert_almost_equal(3.0 * np.ones((100, 90)),
                                  occd.uncertainty.array)
    np.testing.assert_array_equal(mask, occd.mask)
    assert(occd.unit == u.electron)
    # Make sure the original keyword is still present. Regression test for #401
    assert occd.meta['testkw'] == 100
예제 #10
0
def test_stddevuncertainty_compat_descriptor_no_weakref():
    # TODO: Remove this test if astropy 1.0 isn't supported anymore
    # This test might create a Memoryleak on purpose, so the last lines after
    # the assert are IMPORTANT cleanup.
    ccd = CCDData(np.ones((10, 10)), unit='')
    uncert = StdDevUncertainty(np.ones((10, 10)))
    uncert._parent_nddata = ccd
    assert uncert.parent_nddata is ccd
    uncert._parent_nddata = None
예제 #11
0
def test_correlation():
    """
    Test correlation when both observed and template spectra have the same wavelength axis
    """
    size = 4000

    # Seed np.random so that results are consistent
    np.random.seed(51)

    # Create test spectra
    spec_axis = np.linspace(4001., 8000., num=size) * u.AA

    # Two narrow Gaussians are offset from each other so
    # as to generate a correlation peak at a expected lag.
    f1 = np.random.randn(size) * 0.5 * u.Jy
    f2 = np.random.randn(size) * 0.5 * u.Jy

    expected_lag = 10000. * u.km / u.s
    mean2 = 5000. * u.AA
    stdev2 = 30. * u.AA
    mean1 = (1 + expected_lag / const.c.to('km/s')) * mean2
    stdev1 = (1 + expected_lag / const.c.to('km/s')) * stdev2
    rest_value = mean2

    g1 = models.Gaussian1D(amplitude=30 * u.Jy, mean=mean1, stddev=stdev1)
    g2 = models.Gaussian1D(amplitude=30 * u.Jy, mean=mean2, stddev=stdev2)

    flux1 = f1 + g1(spec_axis)
    flux2 = f2 + g2(spec_axis)

    # Observed spectrum must have a rest wavelength value set in.
    spec1 = Spectrum1D(spectral_axis=spec_axis,
                       flux=flux1,
                       uncertainty=StdDevUncertainty(np.random.sample(size),
                                                     unit='Jy'),
                       velocity_convention='optical',
                       rest_value=rest_value)

    spec2 = Spectrum1D(spectral_axis=spec_axis,
                       flux=flux2,
                       uncertainty=StdDevUncertainty(np.random.sample(size),
                                                     unit='Jy'))

    # Get result from correlation
    corr, lag = correlation.template_correlate(spec1, spec2)

    # Check units
    assert corr.unit == u.dimensionless_unscaled
    assert lag.unit == u.km / u.s

    # Check position of correlation peak.
    maximum = np.argmax(corr)
    v_fit = _fit_peak(corr, lag, maximum)
    # checks against 1.5 * 10**(-decimal)
    np.testing.assert_almost_equal(v_fit.value, expected_lag.value, -1)
예제 #12
0
def test_is_continuum_below_threshold():

    # No mask, no uncertainty
    wavelengths = [300, 500, 1000] * u.nm
    data = [0.001, -0.003, 0.003] * u.Jy
    spectrum = Spectrum1D(spectral_axis=wavelengths, flux=data)

    assert is_continuum_below_threshold(spectrum,
                                        threshold=0.1 * u.Jy) == True  # noqa

    #    # No mask, no uncertainty, threshold is float
    #    wavelengths = [300, 500, 1000] * u.nm
    #    data = [0.0081, 0.0043, 0.0072] * u.Jy
    #    spectrum = Spectrum1D(spectral_axis=wavelengths, flux=data)
    #    assert True == is_continuum_below_threshold(spectrum, threshold=0.1)

    # No mask, with uncertainty
    wavelengths = [300, 500, 1000] * u.nm
    data = [0.03, 0.029, 0.031] * u.Jy
    uncertainty = StdDevUncertainty([1.01, 1.03, 1.01] * u.Jy)
    spectrum = Spectrum1D(spectral_axis=wavelengths,
                          flux=data,
                          uncertainty=uncertainty)

    assert is_continuum_below_threshold(spectrum,
                                        threshold=0.1 * u.Jy) == True  # noqa

    # With mask, with uncertainty
    wavelengths = [300, 500, 1000] * u.nm
    data = [0.01, 1.029, 0.013] * u.Jy
    mask = np.array([False, True, False])
    uncertainty = StdDevUncertainty([1.01, 1.13, 1.1] * u.Jy)
    spectrum = Spectrum1D(spectral_axis=wavelengths,
                          flux=data,
                          uncertainty=uncertainty,
                          mask=mask)

    assert is_continuum_below_threshold(spectrum,
                                        threshold=0.1 * u.Jy) == True  # noqa

    # With mask, with uncertainty -- should throw an exception
    wavelengths = [300, 500, 1000] * u.nm
    data = [10.03, 10.029, 10.033] * u.Jy
    mask = np.array([False, False, False])
    uncertainty = StdDevUncertainty([1.01, 1.13, 1.1] * u.Jy)
    spectrum = Spectrum1D(spectral_axis=wavelengths,
                          flux=data,
                          uncertainty=uncertainty,
                          mask=mask)

    with pytest.warns(
            AstropyUserWarning,
            match=r'.*If you want to suppress this warning.*') as e_info:
        find_lines_threshold(spectrum, noise_factor=1)
    assert len(e_info) == 1
예제 #13
0
def test_template_redshift_with_multiple_template_spectra_in_match():
    # Seed np.random so that results are consistent
    np.random.seed(42)

    # Create test spectra
    spec_axis = np.linspace(0, 50, 50) * u.AA
    perm_flux = np.random.randn(50) * u.Jy

    # Test redshift
    redshift = 3

    # Observed spectrum
    spec = Spectrum1D(spectral_axis=spec_axis * (1 + redshift),
                      flux=perm_flux,
                      uncertainty=StdDevUncertainty(np.random.sample(50),
                                                    unit='Jy'))

    # Template spectrum
    spec1 = Spectrum1D(spectral_axis=spec_axis,
                       flux=np.random.randn(50) * u.Jy,
                       uncertainty=StdDevUncertainty(np.random.sample(50)))
    spec2 = Spectrum1D(spectral_axis=spec_axis,
                       flux=np.random.randn(50) * u.Jy,
                       uncertainty=StdDevUncertainty(np.random.sample(50)))

    # Combine spectra into SpectrumCollection object
    spec_coll = SpectrumCollection.from_spectra([spec1, spec2])

    # Test redshift parameters
    min_redshift = .5
    max_redshift = 5.5
    delta_redshift = .25
    redshift_trial_values = np.arange(min_redshift,
                                      max_redshift + delta_redshift,
                                      delta_redshift)

    tm_result = template_comparison.template_match(
        observed_spectrum=spec,
        spectral_templates=spec_coll,
        resample_method="flux_conserving",
        redshift=redshift_trial_values)
    assert len(tm_result) == 5
    # TODO: Determine cause of and fix failing assert
    # np.testing.assert_almost_equal(tm_result[1], redshift)

    np.testing.assert_almost_equal(tm_result[3], 6803.922741644725)

    # When a spectrum collection is matched with a redshift
    # grid, a list-of-lists is returned with the trial chi2
    # values computed for every combination redshift-template.
    # The external list spans the templates in the collection,
    # while each internal list contains all chi2 values
    # for a given template.
    assert len(tm_result[4]) == 2
예제 #14
0
def test_create_with_uncertainty():
    spec = Spectrum1D(spectral_axis=np.arange(1, 50) * u.nm,
                      flux=np.random.sample(49) * u.Jy,
                      uncertainty=StdDevUncertainty(np.random.sample(49) * 0.1))

    assert isinstance(spec.uncertainty, StdDevUncertainty)

    spec = Spectrum1D(spectral_axis=np.arange(1, 50) * u.nm,
                      flux=np.random.sample(49) * u.Jy,
                      uncertainty=StdDevUncertainty(np.random.sample(49) * 0.1))

    assert spec.flux.unit == spec.uncertainty.unit
예제 #15
0
def test_tabular_fits_writer(tmpdir, spectral_axis):
    wlu = {
        'wavelength': u.AA,
        'frequency': u.GHz,
        'energy': u.eV,
        'wavenumber': u.cm**-1
    }
    # Create a small data set
    disp = np.arange(1, 1.1, 0.01) * wlu[spectral_axis]
    flux = np.ones(len(disp)) * 1.e-14 * u.Jy
    unc = StdDevUncertainty(0.01 * flux)
    if spectral_axis not in ('wavelength', ):
        disp = np.flip(disp)

    spectrum = Spectrum1D(flux=flux, spectral_axis=disp, uncertainty=unc)
    tmpfile = str(tmpdir.join('_tst.fits'))
    spectrum.write(tmpfile, format='tabular-fits')

    # Read it in and check against the original
    spec = Spectrum1D.read(tmpfile)
    assert spec.flux.unit == spectrum.flux.unit
    assert spec.spectral_axis.unit == spectrum.spectral_axis.unit
    assert quantity_allclose(spec.spectral_axis, spectrum.spectral_axis)
    assert quantity_allclose(spec.flux, spectrum.flux)
    assert quantity_allclose(spec.uncertainty.quantity,
                             spectrum.uncertainty.quantity)

    # Test spectrum with different flux unit
    flux = np.random.normal(0., 1.e-9,
                            disp.shape[0]) * u.W * u.m**-2 * u.AA**-1
    unc = StdDevUncertainty(0.1 * np.sqrt(np.abs(flux.value)) * flux.unit)
    spectrum = Spectrum1D(flux=flux, spectral_axis=disp, uncertainty=unc)

    # Try to overwrite the file
    with pytest.raises(OSError, match=r'File exists:'):
        spectrum.write(tmpfile, format='tabular-fits')
    spectrum.write(tmpfile, format='tabular-fits', overwrite=True)

    cmap = {
        spectral_axis: ('spectral_axis', wlu[spectral_axis]),
        'flux': ('flux', 'erg / (s cm**2 AA)'),
        'uncertainty': ('uncertainty', None)
    }

    # Read it back again and check against the original
    spec = Spectrum1D.read(tmpfile, format='tabular-fits', column_mapping=cmap)
    assert spec.flux.unit == u.Unit('erg / (s cm**2 AA)')
    assert spec.spectral_axis.unit == spectrum.spectral_axis.unit
    assert quantity_allclose(spec.spectral_axis, spectrum.spectral_axis)
    assert quantity_allclose(spec.flux, spectrum.flux)
    assert quantity_allclose(spec.uncertainty.quantity,
                             spectrum.uncertainty.quantity)
예제 #16
0
def fromMarzJSON(json_spectrum):
    from astropy.nddata import (
        VarianceUncertainty,
        StdDevUncertainty,
        InverseVariance,
    )
    wavelength = Quantity(json_spectrum["wavelength"], u.Angstrom)

    spectrum = SpectrumList()
    uncertainty = None
    if "variance" in json_spectrum.keys():
        uncertainty = StdDevUncertainty(
            Quantity(np.array(json_spectrum["variance"], dtype=np.float)))

    for key in json_spectrum.keys():
        if key != 'wavelength' and key != 'variance':
            flux = Quantity(np.array(json_spectrum[key], dtype=np.float))
            if key == 'intensity' and uncertainty:
                aspectrum = Spectrum1D(flux=flux,
                                       spectral_axis=wavelength,
                                       uncertainty=uncertainty,
                                       mask=np.isnan(flux),
                                       meta={'purpose': 'reduced'})
            else:
                aspectrum = Spectrum1D(flux=flux,
                                       spectral_axis=wavelength,
                                       mask=np.isnan(flux),
                                       meta={'purpose': key})
            spectrum.append(aspectrum)
    return spectrum
예제 #17
0
def test_line_flux_masked():

    np.random.seed(42)

    N = 100

    wavelengths = np.linspace(0.4, 1.05, N) * u.um

    g = models.Gaussian1D(amplitude=2000 * u.mJy,
                          mean=0.56 * u.um,
                          stddev=0.01 * u.um)
    flux = g(wavelengths) + 1000 * u.mJy
    noise = 400 * np.random.random(flux.shape) * u.mJy
    flux += noise

    spectrum = Spectrum1D(spectral_axis=wavelengths, flux=flux)
    spectrum.uncertainty = StdDevUncertainty(noise)

    spectrum_masked = snr_threshold(spectrum, 10.)

    # Ensure we have at least 50% of the data being masked.
    assert len(np.where(spectrum_masked.mask)[0]) > N / 2

    result = line_flux(spectrum_masked)

    assert result.unit.is_equivalent(u.Jy * u.um)

    assert quantity_allclose(result.value, 720.52992, atol=0.001)

    # With flux conserving resampler
    result = line_flux(spectrum_masked,
                       mask_interpolation=FluxConservingResampler)
    assert quantity_allclose(result.value, 720.61116, atol=0.001)
예제 #18
0
파일: plotCUBES.py 프로젝트: drheitor/CUBES
def snr_spec(flux, wl, n):

    sample = len(wl)
    noise = n * np.asarray(random.sample(range(0, len(wl)), sample)) / len(wl)
    unc = StdDevUncertainty(noise)

    fluxn = [[] for i in range(len(wl))]
    i = 0
    for inc in unc:
        fluxn[i] = flux[i] + noise[i]
        i = i + 1

    spec1d = Spectrum1D(spectral_axis=wl * u.AA,
                        flux=fluxn * u.Jy,
                        uncertainty=unc)

    #ax = plt.subplots()[1]
    #ax.plot(spec1d.spectral_axis, spec1d.flux)
    #ax.set_xlim([3520,3550])

    sn1 = snr(spec1d, SpectralRegion(3070 * u.AA, 3090 * u.AA))
    sn = snr_derived(spec1d, SpectralRegion(3070 * u.AA, 3090 * u.AA))

    #print('SNR1: '+ str(snr(spec1d)), SpectralRegion(3500*u.AA, 3550*u.AA))
    print('SNR: ' + str(sn1))
    #print('SNR: '+ str(sn))
    #print('FWHM:'+str(fwhm(spec1d)))

    #0.042 = snr 50
    #

    try:
        return fluxn
    except:
        raise Exception('Check S/N function')
예제 #19
0
def test_centroid_masked(simulated_spectra):
    """
    Test centroid with masked spectrum.
    """

    np.random.seed(42)

    # Same as in test for unmasked spectrum, but using
    # masked version of same spectrum.
    spectrum = simulated_spectra.s1_um_mJy_e1_masked
    uncertainty = StdDevUncertainty(
        0.1 * np.random.random(len(spectrum.flux)) * u.mJy)
    spectrum.uncertainty = uncertainty

    # Use masked flux and dispersion arrays to compute
    # the expected value for centroid.
    wavelengths = spectrum.spectral_axis[~spectrum.mask]
    flux = spectrum.flux[~spectrum.mask]

    spec_centroid_expected = np.sum(flux * wavelengths) / np.sum(flux)

    spec_centroid = centroid(spectrum, None)

    assert isinstance(spec_centroid, u.Quantity)
    assert np.allclose(spec_centroid.value, spec_centroid_expected.value)
예제 #20
0
def test_block_replicate():
    ccd = CCDData(np.ones((4, 4)),
                  unit='adu',
                  meta={'testkw': 1},
                  mask=np.zeros((4, 4), dtype=bool),
                  uncertainty=StdDevUncertainty(np.ones((4, 4))),
                  wcs=np.zeros((4, 4)))
    with catch_warnings(AstropyUserWarning) as w:
        ccd_repl = block_replicate(ccd, (2, 2))
    assert len(w) == 1
    assert 'following attributes were set' in str(w[0].message)

    assert isinstance(ccd_repl, CCDData)
    assert np.all(ccd_repl.data == 0.25)
    assert ccd_repl.data.shape == (8, 8)
    assert ccd_repl.unit == u.adu
    # Other attributes are set to None. In case the function is modified to
    # work on these attributes correctly those tests need to be updated!
    assert ccd_repl.meta == {'testkw': 1}
    assert ccd_repl.mask is None
    assert ccd_repl.wcs is None
    assert ccd_repl.uncertainty is None

    # Make sure meta is copied
    ccd_repl.meta['testkw2'] = 10
    assert 'testkw2' not in ccd.meta
예제 #21
0
def muscles_sed(file_obj, **kwargs):
    """
    Load spectrum from a MUSCLES Treasury Survey panchromatic SED FITS file.

    Parameters
    ----------
    file_obj: str, file-like, or HDUList
          FITS file name, object (provided from name by Astropy I/O Registry),
          or HDUList (as resulting from astropy.io.fits.open()).

    Returns
    -------
    data: Spectrum1D
        The spectrum that is represented by the data in this table.
    """
    # name is not used; what was it for?
    # name = os.path.basename(file_name.rstrip(os.sep)).rsplit('.', 1)[0]

    with read_fileobj_or_hdulist(file_obj, **kwargs) as hdulist:
        header = hdulist[0].header

        tab = Table.read(hdulist[1])

    meta = {'header': header}
    uncertainty = StdDevUncertainty(tab["ERROR"])
    data = Quantity(tab["FLUX"])
    wavelength = Quantity(tab["WAVELENGTH"])

    return Spectrum1D(flux=data, spectral_axis=wavelength,
                      uncertainty=uncertainty, meta=meta)
예제 #22
0
def cos_spectrum_loader(file_name, **kwargs):
    """
    Load COS spectral data from the MAST archive into a spectrum object.

    Parameters
    ----------
    file_name: str
        The path to the FITS file

    Returns
    -------
    data: Spectrum1D
        The spectrum that is represented by the data in this table.
    """

    with fits.open(file_name, **kwargs) as hdu:
        header = hdu[0].header
        name = header.get('FILENAME')
        meta = {'header': header}

        unit = Unit("erg/cm**2 Angstrom s")
        disp_unit = Unit('Angstrom')
        data = hdu[1].data['FLUX'].flatten() * unit
        dispersion = hdu[1].data['wavelength'].flatten() * disp_unit
        uncertainty = StdDevUncertainty(hdu[1].data["ERROR"].flatten() * unit)

        sort_idx = dispersion.argsort()
        dispersion = dispersion[sort_idx]
        data = data[sort_idx]
        uncertainty = uncertainty[sort_idx]

    return Spectrum1D(flux=data,
                      spectral_axis=dispersion,
                      uncertainty=uncertainty,
                      meta=meta)
예제 #23
0
def test_snr(simulated_spectra):
    """
    Test the simple version of the spectral SNR.
    """

    np.random.seed(42)

    #
    #  Set up the data and add the uncertainty and calculate the expected SNR
    #

    spectrum = simulated_spectra.s1_um_mJy_e1
    uncertainty = StdDevUncertainty(
        0.1 * np.random.random(len(spectrum.flux)) * u.mJy)
    spectrum.uncertainty = uncertainty

    wavelengths = spectrum.spectral_axis
    flux = spectrum.flux

    spec_snr_expected = np.mean(flux / (uncertainty.array * uncertainty.unit))

    #
    # SNR of the whole spectrum
    #

    spec_snr = snr(spectrum)

    assert isinstance(spec_snr, u.Quantity)
    assert np.allclose(spec_snr.value, spec_snr_expected.value)
예제 #24
0
def test_centroid(simulated_spectra):
    """
    Test the simple version of the spectral centroid.
    """

    np.random.seed(42)

    #
    #  Set up the data and add the uncertainty and calculate the expected SNR
    #

    spectrum = simulated_spectra.s1_um_mJy_e1
    uncertainty = StdDevUncertainty(
        0.1 * np.random.random(len(spectrum.flux)) * u.mJy)
    spectrum.uncertainty = uncertainty

    wavelengths = spectrum.spectral_axis
    flux = spectrum.flux

    spec_centroid_expected = np.sum(flux * wavelengths) / np.sum(flux)

    #
    # SNR of the whole spectrum
    #

    spec_centroid = centroid(spectrum, None)

    assert isinstance(spec_centroid, u.Quantity)
    assert np.allclose(spec_centroid.value, spec_centroid_expected.value)
예제 #25
0
def cos_spectrum_loader(file_name, **kwargs):
    """ Load file from COS spectral data into a spectrum object

    Parameters
    ----------
    file_name: str
        The path to the FITS file

    Returns
    -------
    data: Spectrum1DRef
        The data.
    """

    name = os.path.basename(file_name)

    with fits.open(file_name, **kwargs) as hdu:
        header = hdu[0].header
        meta = {'header': header}

        uncertainty = StdDevUncertainty(hdu[1].data["ERROR"].flatten())
        data = hdu[1].data['FLUX'].flatten()
        dispersion = hdu[1].data['wavelength'].flatten()
        unit = Unit("erg/cm**2 Angstrom s")

    return Spectrum1DRef.from_array(data=data,
                                    dispersion=dispersion,
                                    dispersion_unit=Unit('Angstrom'),
                                    uncertainty=uncertainty,
                                    unit=unit,
                                    meta=meta)
예제 #26
0
def test_region_simple(simulated_spectra):
    np.random.seed(42)

    spectrum = simulated_spectra.s1_um_mJy_e1
    uncertainty = StdDevUncertainty(
        0.1 * np.random.random(len(spectrum.flux)) * u.mJy)
    spectrum.uncertainty = uncertainty

    region = SpectralRegion(0.6 * u.um, 0.8 * u.um)

    sub_spectrum = extract_region(spectrum, region)

    sub_spectrum_flux_expected = np.array([
        1605.71612173, 1651.41650744, 2057.65798618, 2066.73502361,
        1955.75832537, 1670.52711471, 1491.10034446, 1637.08084112,
        1471.28982259, 1299.19484483, 1423.11195734, 1226.74494917,
        1572.31888312, 1311.50503403, 1474.05051673, 1335.39944397,
        1420.61880528, 1433.18623759, 1290.26966668, 1605.67341284,
        1528.52281708, 1592.74392861, 1568.74162534, 1435.29407808,
        1536.68040935, 1157.33825995, 1136.12679394, 999.92394692,
        1038.61546167, 1011.60297294
    ])

    assert quantity_allclose(sub_spectrum.flux.value,
                             sub_spectrum_flux_expected)
예제 #27
0
def test_snr_single_region(simulated_spectra):
    """
    Test the simple version of the spectral SNR over a region of the spectrum.
    """

    np.random.seed(42)

    region = SpectralRegion(0.52 * u.um, 0.59 * u.um)

    #
    #  Set up the data
    #

    spectrum = simulated_spectra.s1_um_mJy_e1
    uncertainty = StdDevUncertainty(
        0.1 * np.random.random(len(spectrum.flux)) * u.mJy)
    spectrum.uncertainty = uncertainty

    wavelengths = spectrum.spectral_axis
    flux = spectrum.flux

    # +1 because we want to include it in the calculation
    l = np.nonzero(wavelengths > region.lower)[0][0]
    r = np.nonzero(wavelengths < region.upper)[0][-1] + 1

    spec_snr_expected = np.mean(flux[l:r] /
                                (uncertainty.array[l:r] * uncertainty.unit))

    #
    # SNR of the whole spectrum
    #

    spec_snr = snr(spectrum, region)

    assert np.allclose(spec_snr.value, spec_snr_expected.value)
예제 #28
0
def test_collection_without_optional_arguments():
    # Without uncertainties
    flux = u.Quantity(np.random.sample((5, 10)), unit='Jy')
    spectral_axis = u.Quantity(np.arange(50).reshape((5, 10)) + 1, unit='AA')
    uncertainty = StdDevUncertainty(np.random.sample((5, 10)), unit='Jy')
    wcs = np.array([gwcs_from_array(x) for x in spectral_axis])
    mask = np.ones((5, 10)).astype(bool)
    meta = [{'test': 5, 'info': [1, 2, 3]} for i in range(5)]

    spec_coll = SpectrumCollection(flux=flux,
                                   spectral_axis=spectral_axis,
                                   wcs=wcs,
                                   mask=mask,
                                   meta=meta)

    # Without mask
    spec_coll = SpectrumCollection(flux=flux,
                                   spectral_axis=spectral_axis,
                                   uncertainty=uncertainty,
                                   wcs=wcs,
                                   meta=meta)

    # Without meta
    spec_coll = SpectrumCollection(  # noqa
        flux=flux,
        spectral_axis=spectral_axis,
        uncertainty=uncertainty,
        wcs=wcs,
        mask=mask)
예제 #29
0
def test_tabular_fits_2d(tmpdir, spectral_axis):
    wlu = {
        'wavelength': u.AA,
        'frequency': u.GHz,
        'energy': u.eV,
        'wavenumber': u.cm**-1
    }
    # Create a small data set with 2D flux + uncertainty
    disp = np.arange(1, 1.1, 0.01) * wlu[spectral_axis]
    flux = np.ones(
        (3, len(disp))) * np.arange(1,
                                    len(disp) + 1)**2 * 1.e-14 * u.Jy
    unc = StdDevUncertainty(0.01 * np.random.rand(3, len(disp)))
    if spectral_axis not in ('wavelength', ):
        disp = np.flip(disp)

    spectrum = Spectrum1D(flux=flux, spectral_axis=disp, uncertainty=unc)
    tmpfile = str(tmpdir.join('_tst.fits'))
    spectrum.write(tmpfile, format='tabular-fits')

    # Read it in and check against the original
    spec = Spectrum1D.read(tmpfile)
    assert spec.flux.unit == spectrum.flux.unit
    assert spec.spectral_axis.unit == spectrum.spectral_axis.unit
    assert spec.flux.shape == flux.shape
    assert spec.uncertainty.array.shape == flux.shape
    assert quantity_allclose(spec.spectral_axis, spectrum.spectral_axis)
    assert quantity_allclose(spec.flux, spectrum.flux)
    assert quantity_allclose(spec.uncertainty.quantity,
                             spectrum.uncertainty.quantity)
예제 #30
0
def test_arithmetic_overload_ccddata_operand(ccd_data):
    ccd_data.uncertainty = StdDevUncertainty(np.ones_like(ccd_data))
    operand = ccd_data.copy()
    result = ccd_data.add(operand)
    assert len(result.meta) == 0
    np.testing.assert_array_equal(result.data, 2 * ccd_data.data)
    np.testing.assert_array_equal(result.uncertainty.array,
                                  np.sqrt(2) * ccd_data.uncertainty.array)

    result = ccd_data.subtract(operand)
    assert len(result.meta) == 0
    np.testing.assert_array_equal(result.data, 0 * ccd_data.data)
    np.testing.assert_array_equal(result.uncertainty.array,
                                  np.sqrt(2) * ccd_data.uncertainty.array)

    result = ccd_data.multiply(operand)
    assert len(result.meta) == 0
    np.testing.assert_array_equal(result.data, ccd_data.data**2)
    expected_uncertainty = (np.sqrt(2) * np.abs(ccd_data.data) *
                            ccd_data.uncertainty.array)
    np.testing.assert_allclose(result.uncertainty.array, expected_uncertainty)

    result = ccd_data.divide(operand)
    assert len(result.meta) == 0
    np.testing.assert_array_equal(result.data, np.ones_like(ccd_data.data))
    expected_uncertainty = (np.sqrt(2) / np.abs(ccd_data.data) *
                            ccd_data.uncertainty.array)
    np.testing.assert_allclose(result.uncertainty.array, expected_uncertainty)
예제 #31
0
파일: nddata.py 프로젝트: mmorage/DRAGONS
def new_variance_uncertainty_instance(array):

    if array is None:
        return

    with warnings.catch_warnings():

        if (array < 0.).any():
            warnings.warn("Negative variance values found. Setting to zero.",
                          RuntimeWarning)
            array = np.where(array >= 0., array, 0.)

        warnings.simplefilter("ignore", RuntimeWarning)

        obj = StdDevUncertainty(np.sqrt(array))

        warnings.simplefilter("default", RuntimeWarning)

    cls = obj.__class__
    obj.__class__ = cls.__class__(cls.__name__ + "WithAsVariance", (cls, StdDevAsVariance), {})
    return obj