def test_bgs_target_types(self): """Test that incorrect BGS target types are caught """ with self.assertRaises(ValueError): dum = cuts.isBGS_colors(targtype='blatfoo') with self.assertRaises(ValueError): dum = cuts.notinBGS_mask(targtype='blatfoo')
def main(args=None): log = get_logger() if isinstance(args, (list, tuple, type(None))): args = parse(args) thedate = datetime.now().strftime('%Y-%m-%d') # Generate transient model if one is specified. trans_model = None if args.transient is not None: trans_model = transients.get_model(args.transient) # Generate list of HEALPix pixels to randomly sample the mocks. rng = np.random.RandomState(args.seed) nside = args.nside healpixels = _get_healpixels_in_footprint(nside=args.nside) npix = np.minimum(10*args.nsim, len(healpixels)) pixels = rng.choice(healpixels, size=npix, replace=False) ipix = iter(pixels) # Set up the template generator. fluxratio_range = 10**(-np.sort(args.magrange)[::-1] / 2.5) epoch_range = np.sort(args.epochrange) if args.host == 'bgs': maker = BGSMaker(seed=args.seed) tmpl_maker = BGS elif args.host == 'elg': maker = ELGMaker(seed=args.seed) tmpl_maker = ELG elif args.host == 'lrg': maker = LRGMaker(seed=args.seed) tmpl_maker = LRG else: raise ValueError('Unusable host type {}'.format(args.host)) maker.template_maker = tmpl_maker(transient=trans_model, tr_fluxratio=fluxratio_range, tr_epoch=epoch_range) for j in range(args.nsim): # Loop until finding a non-empty healpixel with mock galaxies. tdata = [] while len(tdata) == 0: pixel = next(ipix) tdata = maker.read(healpixels=pixel, nside=args.nside) # Generate spectral templates and write them to truth files. # Keep producing templates until we have enough to pass brightness cuts. wave = None flux, targ, truth, objtr = [], [], [], [] ntosim = np.min([args.nspec, len(tdata['RA'])]) ngood = 0 while ngood < args.nspec: idx = rng.choice(len(tdata['RA']), ntosim) tflux, twave, ttarg, ttruth, tobj = maker.make_spectra(tdata, indx=idx) g, r, z, w1, w2 = [ttruth['FLUX_{}'.format(_)] for _ in ['G','R','Z','W1','W2']] rfib = ttarg['FIBERFLUX_R'] # print(g, r, z, w1, w2, rfib) # Apply color cuts. is_bright = isBGS_colors(rfib, g, r, z, w1, w2, targtype='bright') is_faint = isBGS_colors(rfib, g, r, z, w1, w2, targtype='faint') is_wise = isBGS_colors(rfib, g, r, z, w1, w2, targtype='wise') keep = np.logical_or.reduce([is_bright, is_faint, is_wise]) _ngood = np.count_nonzero(keep) if _ngood > 0: ngood += _ngood flux.append(tflux[keep, :]) targ.append(ttarg[keep]) truth.append(ttruth[keep]) objtr.append(tobj[keep]) wave = maker.wave flux = np.vstack(flux)[:args.nspec, :] targ = vstack(targ)[:args.nspec] truth = vstack(truth)[:args.nspec] objtr = vstack(objtr)[:args.nspec] # Set up and verify the TARGETID across all truth tables. n = len(truth) new_id = 10000*pixel + 100*j + np.arange(1, n+1) targ['OBJID'][:] = new_id truth['TARGETID'][:] = new_id objtr['TARGETID'][:] = new_id assert(len(truth) == args.nspec) assert(np.all(targ['OBJID'] == truth['TARGETID'])) assert(len(targ) == len(np.unique(targ['OBJID']))) assert(len(truth) == len(np.unique(truth['TARGETID']))) assert(len(objtr) == len(np.unique(objtr['TARGETID']))) truthfile = os.path.join(args.outdir, '{}_{}_{:04d}s_{:03d}_truth.fits'.format(args.host, thedate, int(args.exptime), j+1)) write_templates(truthfile, flux, wave, targ, truth, objtr) # Get observing conditions and generate spectra. obs = dict(AIRMASS=args.airmass, EXPTIME=args.exptime, MOONALT=args.moonalt, MOONFRAC=args.moonfrac, MOONSEP=args.moonsep, SEEING=args.seeing) fcols = dict(BRICKID=targ['BRICKID'], BRICK_OBJID=targ['OBJID'], FLUX_G=targ['FLUX_G'], FLUX_R=targ['FLUX_R'], FLUX_Z=targ['FLUX_Z'], FLUX_W1=targ['FLUX_W1'], FLUX_W2=targ['FLUX_W2'], FLUX_IVAR_G=targ['FLUX_IVAR_G'], FLUX_IVAR_R=targ['FLUX_IVAR_R'], FLUX_IVAR_Z=targ['FLUX_IVAR_Z'], FLUX_IVAR_W1=targ['FLUX_IVAR_W1'], FLUX_IVAR_W2=targ['FLUX_IVAR_W2'], FIBERFLUX_G=targ['FIBERFLUX_G'], FIBERFLUX_R=targ['FIBERFLUX_R'], FIBERFLUX_Z=targ['FIBERFLUX_Z'], FIBERTOTFLUX_G=targ['FIBERTOTFLUX_G'], FIBERTOTFLUX_R=targ['FIBERTOTFLUX_R'], FIBERTOTFLUX_Z=targ['FIBERTOTFLUX_Z'], MW_TRANSMISSION_G=targ['MW_TRANSMISSION_G'], MW_TRANSMISSION_R=targ['MW_TRANSMISSION_R'], MW_TRANSMISSION_Z=targ['MW_TRANSMISSION_Z'], EBV=targ['EBV']) specfile = os.path.join(args.outdir, '{}_{}_{:04d}s_{:03d}_spect.fits'.format(args.host, thedate, int(args.exptime), j+1)) redshifts = truth['TRUEZ'] if args.host=='bgs' else None sim_spectra(wave, flux, args.host, specfile, sourcetype=args.host, obsconditions=obs, meta=obs, fibermap_columns=fcols, targetid=truth['TARGETID'], redshift=redshifts, ra=targ['RA'], dec=targ['DEC'], seed=args.seed, expid=j)
def main(args=None): log = get_logger() if isinstance(args, (list, tuple, type(None))): args = parse(args) # Save simulation output. rng = np.random.RandomState(args.seed) simdata = bgs_write_simdata(args) obs = simdata2obsconditions(args) # Generate list of HEALPix pixels to randomly sample from the mocks. healpixels = _get_healpixels_in_footprint(nside=args.nside) npix = np.minimum(10 * args.nsim, len(healpixels)) pixels = rng.choice(healpixels, size=npix, replace=False) ipix = iter(pixels) # Set up the template generator. maker = BGSMaker(seed=args.seed) maker.template_maker = BGS(add_SNeIa=args.addsnia, add_SNeIIp=args.addsniip, wave=_default_wave()) for j in range(args.nsim): # Loop until finding a non-empty healpixel (one with mock galaxies). tdata = [] while len(tdata) == 0: pixel = next(ipix) tdata = maker.read(healpixels=pixel, nside=args.nside) # Add SN generation options. if args.addsnia or args.addsniip: tdata['SNE_FLUXRATIORANGE'] = (args.snrmin, args.snrmax) tdata['SNE_FILTER'] = 'decam2014-r' # Generate nspec spectral templates and write them to "truth" files. wave = None flux, targ, truth, obj = [], [], [], [] # Generate templates until we have enough to pass brightness cuts. ntosim = np.min((args.nspec, len(tdata['RA']))) ngood = 0 while ngood < args.nspec: idx = rng.choice(len(tdata['RA']), ntosim) tflux, twave, ttarg, ttruth, tobj = \ maker.make_spectra(tdata, indx=idx) # Apply color cuts. is_bright = isBGS_colors(gflux=ttruth['FLUX_G'], rflux=ttruth['FLUX_R'], zflux=ttruth['FLUX_Z'], w1flux=ttruth['FLUX_W1'], w2flux=ttruth['FLUX_W2'], targtype='bright') is_faint = isBGS_colors(gflux=ttruth['FLUX_G'], rflux=ttruth['FLUX_R'], zflux=ttruth['FLUX_Z'], w1flux=ttruth['FLUX_W1'], w2flux=ttruth['FLUX_W2'], targtype='faint') is_wise = isBGS_colors(gflux=ttruth['FLUX_G'], rflux=ttruth['FLUX_R'], zflux=ttruth['FLUX_Z'], w1flux=ttruth['FLUX_W1'], w2flux=ttruth['FLUX_W2'], targtype='wise') keep = np.logical_or(np.logical_or(is_bright, is_faint), is_wise) _ngood = np.count_nonzero(keep) if _ngood > 0: ngood += _ngood flux.append(tflux[keep, :]) targ.append(ttarg[keep]) truth.append(ttruth[keep]) obj.append(tobj[keep]) wave = maker.wave flux = np.vstack(flux)[:args.nspec, :] targ = vstack(targ)[:args.nspec] truth = vstack(truth)[:args.nspec] obj = vstack(obj)[:args.nspec] if args.addsnia or args.addsniip: # TARGETID in truth table is split in two; deal with it here. truth['TARGETID'] = truth['TARGETID_1'] # Set up and verify the TARGETID across all truth tables. n = len(truth) new_id = 10000000 * pixel + 100000 * j + np.arange(1, n + 1) truth['TARGETID'][:] = new_id targ['TARGETID'][:] = new_id obj['TARGETID'][:] = new_id assert (len(truth) == args.nspec) assert (np.all(targ['TARGETID'] == truth['TARGETID'])) assert (len(truth) == len(np.unique(truth['TARGETID']))) assert (len(targ) == len(np.unique(targ['TARGETID']))) assert (len(obj) == len(np.unique(obj['TARGETID']))) truthfile = os.path.join( args.simdir, 'bgs_{}_{:03}_truth.fits'.format(args.simid, j)) write_templates(truthfile, flux, wave, targ, truth, obj) # Generate simulated spectra, given observing conditions. specfile = os.path.join( args.simdir, 'bgs_{}_{:03}_spectra.fits'.format(args.simid, j)) sim_spectra(wave, flux, 'bgs', specfile, obsconditions=obs, sourcetype='bgs', targetid=truth['TARGETID'], redshift=truth['TRUEZ'], seed=args.seed, expid=j)