def test_smash_spectra(specmr): # Try to stack 2 spectra with different wavelengths with pytest.raises(AssertionError): stack = ltsu.smash_spectra(specmr) # Stack rebinned zarr = np.array([2.1, 2.2]) rest_spec = ltsu.rebin_to_rest(specmr, zarr, 100 * u.km / u.s, debug=False) stack = ltsu.smash_spectra(rest_spec, method='average') # Test assert stack.totpix == 3716 np.testing.assert_allclose(stack.flux[1].value, -3.32135105133, rtol=1e-5)
def test_smash_spectra(specmr): # Try to stack 2 spectra with different wavelengths with pytest.raises(AssertionError): stack = ltsu.smash_spectra(specmr) # Stack rebinned zarr = np.array([2.1,2.2]) rest_spec = ltsu.rebin_to_rest(specmr, zarr, 100*u.km/u.s, debug=False) stack = ltsu.smash_spectra(rest_spec, method='average') # Test assert stack.totpix == 3716 np.testing.assert_allclose(stack.flux[1].value, -3.32135105133, rtol=1e-5)
def stack_spec(spec_file, dv=100 * u.km / u.s, cut_on_rho=4.): # Load xspec, tpe, spec_tbl = load_spec(spec_file) # Cut on separation (should do this much earlier in the process) if cut_on_rho is not None: warnings.warn("Cutting on rho in stack. Should do this earlier") b_coords = SkyCoord(ra=tpe['BG_RA'], dec=tpe['BG_DEC'], unit='deg') f_coords = SkyCoord(ra=tpe['FG_RA'], dec=tpe['FG_DEC'], unit='deg') kpc_amin = cosmo.kpc_comoving_per_arcmin(tpe['FG_Z']) # kpc per arcmin ang_seps = b_coords.separation(f_coords) rho = ang_seps.to('arcmin') * kpc_amin / (1 + tpe['FG_Z']) cut_rho = rho.to('Mpc').value < cut_on_rho print("We have {:d} spectra after the cut.".format(np.sum(cut_rho))) xspec = xspec[cut_rho] tpe = tpe[cut_rho] spec_tbl = spec_tbl[cut_rho] # Remove those without continua has_co = spec_tbl['HAS_CO'].data co_spec = xspec[has_co] co_spec.normed = True # Apply continuum # May also wish to isolate in wavelength to avoid rejected pixels for ii in range(co_spec.nspec): co_spec.select = ii co = co_spec.co.value sig = co_spec.sig.value bad_pix = np.any([(co == 0.), (co == 1.), (sig <= 0.)], axis=0) co_spec.add_to_mask(bad_pix, compressed=True) # Rebin to rest zarr = tpe['FG_Z'][has_co] rebin_spec = lspu.rebin_to_rest(co_spec, zarr, dv) # Stack stack = lspu.smash_spectra(rebin_spec) # Plot tpep.plot_stack(stack, 'all_stack.pdf') tpep.plot_spec_img(rebin_spec, 'spec_img.pdf') pdb.set_trace() # Return return
def tpe_stack_lris(dv=100 * u.km / u.s): """ Testing stacks with LRIS """ # Load sample ipos = this_file.rfind('/') if ipos == -1: path = './' else: path = this_file[0:ipos] tpe = Table.read(path + '/../TPE_DR12_31.2_spec.fits') # Load spectra # Coordiantes b_coords = SkyCoord(ra=tpe['BG_RA'], dec=tpe['BG_DEC'], unit='deg') f_coords = SkyCoord(ra=tpe['FG_RA'], dec=tpe['FG_DEC'], unit='deg') # Cut on impact parameter and BOSS kpc_amin = cosmo.kpc_comoving_per_arcmin(tpe['FG_Z']) # kpc per arcmin ang_seps = b_coords.separation(f_coords) rho = ang_seps.to('arcmin') * kpc_amin / (1 + tpe['FG_Z']) cut_Rlris = (rho.to('Mpc').value < 4) & (tpe['BG_LYA_INSTRUMENT'] == 'LRIS' ) # & ( #tpe['FG_Z'] > 2.) # Some of these have too low z (just barely) # Cut gd_b_coords = b_coords[cut_Rlris] gd_tpe = tpe[cut_Rlris] # Grab these spectra from QPQ # For boss, we are ok taking the first entry of each # The returned set is aligned with the input coords qpq = IgmSpec(db_file=qpq_file, skip_test=True) IDs = qpq.qcat.match_coord(gd_b_coords, group='LRIS') meta = qpq['LRIS'].meta gcut = meta['GRATING'] == '1200/3400' # There is one with B400 B1200 = np.in1d(IDs, meta['PRIV_ID'][gcut]) print("There are {:d} sources without B1200".format(np.sum(~B1200))) # Cut again gd_b_coords = gd_b_coords[B1200] gd_tpe = gd_tpe[B1200] gd_IDs = IDs[B1200] # Find the rows idx = cat_utils.match_ids(gd_IDs, meta['PRIV_ID']) rows = meta['GROUP_ID'][idx] pdb.set_trace() spec, meta = qpq.coords_to_spectra(gd_b_coords, 'LRIS', all_spec=False) # Check for continua has_co = np.array([True] * spec.nspec) for ii in range(spec.nspec): # Select spec.select = ii # Match to lya lya = (1 + gd_tpe['FG_Z'][ii]) * 1215.67 * u.AA iwave = np.argmin(np.abs(spec.wavelength - lya)) # Check for co #coval = spec.co[iwave] #print('spec: {:d} with co={:g}'.format(ii, coval)) if np.isclose(spec.co[iwave], 0.) or np.isclose(spec.co[iwave], 1.): has_co[ii] = False # Slice to good co print("{:d} BOSS spectra with a continuum".format(np.sum(has_co))) co_spec = spec[has_co] co_spec.normed = True # Apply continuum # NEED TO ZERO OUT REGIONS WITHOUT CONTINUUM # May also wish to isolate in wavelength to avoid rejected pixels for ii in range(co_spec.nspec): co_spec.select = ii co = co_spec.co.value bad_pix = np.any([(co == 0.), (co == 1.)], axis=0) co_spec.add_to_mask(bad_pix, compressed=True) # Rebin to rest zarr = gd_tpe['FG_Z'][has_co] rebin_spec = lspu.rebin_to_rest(co_spec, zarr, dv) # Stack stack = lspu.smash_spectra(rebin_spec) # Plot plot_stack(stack, 'LRIS_stack.pdf') return stack
def tpe_stack_boss(dv=100 * u.km / u.s): """ Testing stacks with BOSS """ # Load sample ipos = this_file.rfind('/') if ipos == -1: path = './' else: path = this_file[0:ipos] tpe = Table.read(path + '/../TPE_DR12_31.2_spec.fits') # Load spectra igmsp = IgmSpec() # Coordiantes b_coords = SkyCoord(ra=tpe['BG_RA'], dec=tpe['BG_DEC'], unit='deg') f_coords = SkyCoord(ra=tpe['FG_RA'], dec=tpe['FG_DEC'], unit='deg') # Cut on impact parameter and BOSS kpc_amin = cosmo.kpc_comoving_per_arcmin(tpe['FG_Z']) # kpc per arcmin ang_seps = b_coords.separation(f_coords) rho = ang_seps.to('arcmin') * kpc_amin / (1 + tpe['FG_Z']) cut_Rboss = (rho.to('Mpc').value < 4) & ( tpe['BG_LYA_INSTRUMENT'] == 'BOSS') & ( tpe['FG_Z'] > 2.) # Some of these have too low z (just barely) # Cut gd_b_coords = b_coords[cut_Rboss] gd_f_coords = f_coords[cut_Rboss] gd_tpe = tpe[cut_Rboss] # Grab these spectra from igmsp # For boss, we are ok taking the first entry of each # The returned set is aligned with the input coords spec, meta = igmsp.coords_to_spectra(gd_b_coords, 'BOSS_DR12', all_spec=False) # Check for continua has_co = np.array([True] * spec.nspec) for ii in range(spec.nspec): # Select spec.select = ii # Match to lya lya = (1 + gd_tpe['FG_Z'][ii]) * 1215.67 * u.AA iwave = np.argmin(np.abs(spec.wavelength - lya)) # Check for co #coval = spec.co[iwave] #print('spec: {:d} with co={:g}'.format(ii, coval)) if np.isclose(spec.co[iwave], 0.) or np.isclose(spec.co[iwave], 1.): has_co[ii] = False # Slice to good co print("{:d} BOSS spectra with a continuum".format(np.sum(has_co))) co_spec = spec[has_co] co_spec.normed = True # Apply continuum # NEED TO ZERO OUT REGIONS WITHOUT CONTINUUM # May also wish to isolate in wavelength to avoid rejected pixels for ii in range(co_spec.nspec): co_spec.select = ii co = co_spec.co.value bad_pix = np.any([(co == 0.), (co == 1.)], axis=0) co_spec.add_to_mask(bad_pix, compressed=True) # Rebin to rest zarr = gd_tpe['FG_Z'][has_co] rebin_spec = lspu.rebin_to_rest(co_spec, zarr, dv) # Check 2D check_td = True if check_td: fx = rebin_spec.data['flux'] sig = rebin_spec.data['sig'] gds = sig > 0. fx[~gds] = 0. xdb.set_trace() # xdb.ximshow(fx) # Stack stack = lspu.smash_spectra(rebin_spec) # Plot plot_stack(stack, 'BOSS_stack.pdf') print('Wrote') return stack
def stacks(spec, red, N, zbin, plot=False): """ :param spec: (numpy array) XSpectrum1D objects :param red: (numpy array) their redshift values (z) :param N: (int) number of bootstrap iterations :param zbin: (str) redshift bin "lowz" or "hiz" """ import numpy as np from astropy.io import fits from numpy import random as ran from linetools.spectra import utils as ltsu from linetools.spectra.xspectrum1d import XSpectrum1D print("Number of spectra provided =", spec.nspec) # the actual stack stack = ltsu.smash_spectra(spec) z_med = np.median(red) # the bootstrap R = (spec.nspec) # restraints on the random variable N_stack = [] N_z_med = [] for i in np.arange(N): # this might take a while if i % 10 == 0: print(i) choice = np.asarray(ran.randint(0, R, R)) # the shuffle rand_spec = np.asarray([spec[index] for index in choice]) rand_red = np.asarray([red[index] for index in choice]) rand_collate = ltsu.collate(rand_spec) # collate and stack N_stack.append(ltsu.smash_spectra(rand_collate)) N_z_med.append(np.median(rand_red)) # matrix math to create the error array N_flux = np.array([entry.flux for entry in N_stack]) N_matrix = np.array([N_flux[i] - stack.flux for i in range(N)]) tranpose = np.transpose(N_matrix) # matrix math covariance = np.dot(tranpose, N_matrix) sigma = np.sqrt(np.diagonal(covariance) / (N - 1)) # the final error array composite = XSpectrum1D(stack.wavelength, stack.flux, sig=sigma) # plot with bootstrap generated error if plot == True: composite.plot() # writing out the stack hdr = fits.Header() hdr["REDSHIFT"] = z_med hdr["NSPEC"] = spec.nspec header = fits.PrimaryHDU(header=hdr) c_wave = fits.Column(name='WAVELENGTH', array=stack.wavelength, unit="angstroms", format="E") c_flux = fits.Column(name='FLUX', array=stack.flux, unit="relative flux", format="E") c_noise = fits.Column(name='FLUX_ERR', array=sigma, unit="relative flux", format="E") table = fits.BinTableHDU.from_columns([c_wave, c_flux, c_noise]) full_hdu = fits.HDUList([header, table]) full_hdu.writeto("../../fits/composites/" + zbin + "/composite.fits") # writing out the boostrap hdr = fits.Header() hdr["NSPEC"] = spec.nspec hdr["NBOOT"] = N header = fits.PrimaryHDU(header=hdr) c_wave = fits.Column(name='WAVELENGTH', array=stack.wavelength, unit="angstroms", format="E") c_noise = fits.Column(name='FLUX_ERR', array=sigma, unit="relative flux", format="E") table = fits.BinTableHDU.from_columns([c_wave, c_noise]) flux_data = fits.ImageHDU(N_flux, name="FLUX_ITERATIONS") full_hdu = fits.HDUList([header, table, flux_data]) full_hdu.writeto("../../fits/bootstrap/" + zbin + "/stacks.fits")