def test_realize_lcs(): # here's some completely made-up data: obs = Table({'time': [10., 60., 110.], 'band': ['desg', 'desr', 'desi'], 'gain': [1., 1., 1.], 'skynoise': [100., 100., 100.], 'zp': [30., 30., 30.], 'zpsys': ['ab', 'ab', 'ab']}) # A model with a flat spectrum between 0 and 100 days. model = sncosmo.Model(source=flatsource()) # parameters to run params = [{'amplitude': 1., 't0': 0., 'z': 0.}, {'amplitude': 1., 't0': 100., 'z': 0.}, {'amplitude': 1., 't0': 200., 'z': 0.}] # By default, realize_lcs should return all observations for all SNe lcs = sncosmo.realize_lcs(obs, model, params) assert len(lcs[0]) == 3 assert len(lcs[1]) == 3 assert len(lcs[2]) == 3 # For trim_obervations=True, only certain observations will be # returned. lcs = sncosmo.realize_lcs(obs, model, params, trim_observations=True) assert len(lcs[0]) == 2 assert len(lcs[1]) == 1 assert len(lcs[2]) == 0
def test_realize_lcs(): # here's some completely made-up data: obs1 = Table({ 'time': [10., 60., 110.], 'band': ['bessellb', 'bessellr', 'besselli'], 'gain': [1., 1., 1.], 'skynoise': [100., 100., 100.], 'zp': [30., 30., 30.], 'zpsys': ['ab', 'ab', 'ab'] }) # same made up data with aliased column names: obs2 = Table({ 'MJD': [10., 60., 110.], 'filter': ['bessellb', 'bessellr', 'besselli'], 'GAIN': [1., 1., 1.], 'skynoise': [100., 100., 100.], 'ZPT': [30., 30., 30.], 'zpmagsys': ['ab', 'ab', 'ab'] }) for obs in (obs1, obs2): # A model with a flat spectrum between 0 and 100 days. model = sncosmo.Model(source=flatsource()) # parameters to run params = [{ 'amplitude': 1., 't0': 0., 'z': 0. }, { 'amplitude': 1., 't0': 100., 'z': 0. }, { 'amplitude': 1., 't0': 200., 'z': 0. }] # By default, realize_lcs should return all observations for all SNe lcs = sncosmo.realize_lcs(obs, model, params) assert len(lcs[0]) == 3 assert len(lcs[1]) == 3 assert len(lcs[2]) == 3 # For trim_obervations=True, only certain observations will be # returned. lcs = sncosmo.realize_lcs(obs, model, params, trim_observations=True) assert len(lcs[0]) == 2 assert len(lcs[1]) == 1 assert len(lcs[2]) == 0
def get_supernova(self, z, num_obs, tmin, tmax, cosmology, x0_mean=-19.3, x0_sigma=0.1): t0 = np.random.uniform(tmin, tmax) ts = np.linspace(t0 - 60, t0 + 60, num_obs) times = np.array([[t, t + 0.1, t + 0.2] for t in ts]).flatten() bands = [b for t in ts for b in ['desg', 'desr', 'desi']] gains = np.ones(times.shape) skynoise = 50 * np.ones(times.shape) zp = 30 * np.ones(times.shape) zpsys = ['ab'] * times.size obs = Table({'time': times, 'band': bands, 'gain': gains, 'skynoise': skynoise, 'zp': zp, 'zpsys': zpsys}) model = sncosmo.Model(source='salt2') mabs = np.random.normal(x0_mean, x0_sigma) model.set(z=z) model.set_source_peakabsmag(mabs, 'bessellb', 'ab', cosmo=cosmology) x0 = model.get('x0') p = {'z': z, 't0': t0, 'x0': x0, 'x1': np.random.normal(0., 1.), 'c': np.random.normal(0., 0.1) } lcs = sncosmo.realize_lcs(obs, model, [p]) return lcs[0]
def data(self, nobs, exclude_bands=[]): model = self.model baset = np.linspace(model.mintime(), model.maxtime(), nobs) zps = [] zpsyss = [] times = [] bands = [] skynoises = [] for band in map(sncosmo.get_bandpass, csp.bands): if band.name in exclude_bands: continue zps += ([csp.zeropoints[band.name]] * nobs) zpsyss += ([csp.name] * nobs) times += baset.tolist() skynoises += (1e-10 * model.bandflux(band, baset)).tolist() bands += [band.name] * nobs gains = np.ones_like(times) * gain obs = Table({'time':times, 'band':bands, 'gain':gains, 'skynoise':skynoises, 'zp':zps, 'zpsys':zpsyss}) params = [dict(zip(model.param_names, model.parameters))] lcs = sncosmo.realize_lcs(obs, model, params) lc = sncosmo.photdata.normalize_data(lcs[0]) lc['fluxerr'] = 0.01 * lc['flux'].max() lc = Table(lc) lc.rename_column('time','mjd') lc.rename_column('band','filter') lc.meta['zcmb'] = self.z return lc
def data(self, nobs, exclude_bands=[]): model = self.model baset = np.linspace(model.mintime(), model.maxtime(), nobs) zps = [] zpsyss = [] times = [] bands = [] skynoises = [] for band in map(sncosmo.get_bandpass, csp.bands): if band.name in exclude_bands: continue zps += ([csp.zeropoints[band.name]] * nobs) zpsyss += ([csp.name] * nobs) times += baset.tolist() skynoises += (1e-10 * model.bandflux(band, baset)).tolist() bands += [band.name] * nobs gains = np.ones_like(times) * gain obs = Table({ 'time': times, 'band': bands, 'gain': gains, 'skynoise': skynoises, 'zp': zps, 'zpsys': zpsyss }) params = [dict(zip(model.param_names, model.parameters))] lcs = sncosmo.realize_lcs(obs, model, params) lc = sncosmo.photdata.normalize_data(lcs[0]) lc['fluxerr'] = 0.01 * lc['flux'].max() lc = Table(lc) lc.rename_column('time', 'mjd') lc.rename_column('band', 'filter') lc.meta['zcmb'] = self.z return lc
def generate_ia_light_curve(z, mabs, x1, c, get_calib=True, **kwargs): if "cosmo" in kwargs: cosmo = kwargs["cosmo"] del kwargs["cosmo"] else: cosmo = WMAP9 model = sncosmo.Model(source="salt2-extended") model.set(z=z) model.set_source_peakabsmag(mabs, "bessellb", "ab", cosmo=cosmo) x0 = model.get("x0") obs, t0, bands, zps = get_obs_times_and_conditions(**kwargs) p = {"z": z, "t0": t0, "x0": x0, "x1": x1, "c": c} lc = sncosmo.realize_lcs(obs, model, [p])[0] if get_calib: filters = ["desg", "desr", "desi", "desz"] delta = 0.01 lcs = [(None, 0, lc)] for f in filters: l = lc.copy() delta_zps = np.array([delta if b == f else 0 for b in bands]) l["flux"] *= np.power(10, delta_zps / 2.5) l["fluxerr"] *= np.power(10, delta_zps / 2.5) lcs.append((f, delta, l)) else: lcs = [(None, 0, lc)] return lcs
def get_supernova(self, z, num_obs, tmin, tmax, cosmology, alpha, beta, x0_mean=-19.3, x0_sigma=0.1): t0 = np.random.uniform(tmin, tmax) ts = np.linspace(t0 - 60, t0 + 60, num_obs) times = np.array([[t, t + 0.1, t + 0.2] for t in ts]).flatten() bands = [b for t in ts for b in ['desg', 'desr', 'desi']] gains = np.ones(times.shape) skynoise = 20 * np.ones(times.shape) zp = 30 * np.ones(times.shape) zpsys = ['ab'] * times.size obs = Table({'time': times, 'band': bands, 'gain': gains, 'skynoise': skynoise, 'zp': zp, 'zpsys': zpsys}) model = sncosmo.Model(source='salt2') mabs = np.random.normal(x0_mean, x0_sigma) model.set(z=z) x1 = np.random.normal(0., 1.) c = np.random.normal(0., 0.2) mabs = mabs - alpha * x1 + beta * c model.set_source_peakabsmag(mabs, 'bessellb', 'ab', cosmo=cosmology) x0 = model.get('x0') p = {'z': z, 't0': t0, 'x0': x0, 'x1': x1, 'c': c } lcs = sncosmo.realize_lcs(obs, model, [p]) res, fitted_model = sncosmo.fit_lc(lcs[0], model, ['t0', 'x0', 'x1', 'c']) determined_parameters = {k: v for k, v in zip(res.param_names, res.parameters)} model.set(**determined_parameters) mb = fitted_model.source.peakmag("bessellb", "ab") x0_ind = res.vparam_names.index('x0') x1_ind = res.vparam_names.index('x1') c_ind = res.vparam_names.index('c') x0 = res.parameters[res.param_names.index('x0')] x1 = res.parameters[res.param_names.index('x1')] c = res.parameters[res.param_names.index('c')] sigma_mb2 = 5 * np.sqrt(res.covariance[x0_ind, x0_ind]) / (2 * x0 * np.log(10)) sigma_mbx1 = -5 * res.covariance[x0_ind, x1_ind] / (2 * x0 * np.log(10)) sigma_mbc = -5 * res.covariance[x0_ind, c_ind] / (2 * x0 * np.log(10)) covariance = np.array([[sigma_mb2, sigma_mbx1, sigma_mbc], [sigma_mbx1, res.covariance[x1_ind, x1_ind], res.covariance[x1_ind, c_ind]], [sigma_mbc, res.covariance[x1_ind, c_ind], res.covariance[c_ind, c_ind]]]) icov = np.linalg.inv(covariance) return [mb, x1, c, covariance, icov]
def create_sne_simulation(sn_type,model,start_phase,params,myfile,snid): #Create Ia simulation lcs=sncosmo.realize_lcs(obs,model,params,trim_observations=True,scatter=False) for k in range(len(lcs)): fluxes=lcs[k]['flux'].quantity time=lcs[k]['time'].quantity band=lcs[k]['band'] redshift=lcs[k].meta['z'] #Find magnitude measrements for each filter idxs_r=np.where(band == 'sdssr')[0] time_r=time[idxs_r] fluxes_r=np.array(fluxes[idxs_r],dtype=float) idxs_g=np.where(band == 'sdssg')[0] time_g=time[idxs_g] fluxes_g=np.array(fluxes[idxs_g],dtype=float) idxs_i=np.where(band=='sdssi')[0] time_i=time[idxs_i] fluxes_i=np.array(fluxes[idxs_i],dtype=float) mask_r=np.where((fluxes_r>0.)&(fluxes_i>0.)&(fluxes_g>0.))[0] obs_flux_r=fluxes_r[mask_r] obs_time_r=time_r[mask_r] obs_flux_g=fluxes_g[mask_r] obs_time_g=time_g[mask_r] obs_flux_i=fluxes_i[mask_r] obs_time_i=time_g[mask_r] full_rband_mag=np.zeros(len(obs_flux_r)) full_iband_mag=np.zeros(len(obs_flux_r)) full_gband_mag=np.zeros(len(obs_flux_r)) for i in range(len(obs_flux_r)): full_rband_mag[i]=ab.band_flux_to_mag(np.array(obs_flux_r[i]), 'sdssr') full_iband_mag[i]=ab.band_flux_to_mag(np.array(obs_flux_i[i]), 'sdssi') full_gband_mag[i]=ab.band_flux_to_mag(np.array(obs_flux_g[i]), 'sdssg') mask=np.where(full_rband_mag<24.) rband_mag=full_rband_mag[mask] rband_time=obs_time_r[mask] iband_mag=full_iband_mag[mask] gband_mag=full_gband_mag[mask] days_after_exp_restframe=((rband_time-lcs[k].meta['t0'])/(1.+lcs[k].meta['z']))-start_phase if len(rband_mag)==0: continue else: #print rband_mag, rband_time start_time=rband_time end_time=rband_time if sn_type=='SNIa': for l in range(0,len(rband_mag)): myfile.write(str(snid)+','+str(rband_mag[l])+','+str(start_time[l])+','+str(end_time[l])+','+str(sn_type)+','+str(redshift)+','+str(days_after_exp_restframe[l])+','+str(lcs[k].meta['x1'])+','+str(lcs[k].meta['c'])+','+str(gband_mag[l])+','+str(iband_mag[l])+'\n') else: for l in range(0,len(rband_mag)): myfile.write(str(snid)+','+str(rband_mag[l])+','+str(start_time[l])+','+str(end_time[l])+','+str(sn_type)+','+str(redshift)+','+str(days_after_exp_restframe[l])+',-99.,-99.,'+str(gband_mag[l])+','+str(iband_mag[l])+'\n') snid+=1 return snid
def realise_light_curve(temp_dir, seed): np.random.seed(seed) t0 = 1000 num_obs = 20 z = np.random.uniform(0.1, 0.9) deltat = -35 newmoon = 1000 - np.random.uniform(0, 29.5) ts = np.arange(t0 + deltat, (t0 + deltat) + 5 * num_obs, 5) bands = [b for t in ts for b in ['desg', 'desr', 'desi', 'desz']] # zps = np.array([b for t in ts for b in [33.80, 34.54, 34.94, 35.52]]) # Deep field OLD # zps = np.array([b for t in ts for b in [34.24, 34.85, 34.94, 35.42]]) # Deep field zps = np.array([b for t in ts for b in [32.46, 32.28, 32.55, 33.12]]) # Shallow field mins = np.array([b for t in ts for b in [22.1, 21.1, 20.1, 18.7]]) maxs = np.array([b for t in ts for b in [19.4, 19.7, 19.4, 18.2]]) seeing = np.array([b for t in ts for b in [1.06, 1.00, 0.96, 0.93]]) times = np.array([[t, t + 0.05, t + 0.1, t + 0.2] for t in ts]).flatten() full = 0.5 + 0.5 * np.sin((times - newmoon) * 2 * np.pi / 29.5) perm = np.random.uniform(-0.1, 0.1, full.shape) seeing2 = np.random.uniform(4, 6, full.shape) sigma_psf = seeing2 / 2.36 sky_noise = np.array([np.sqrt(10.0**(((maxx - minn) * f + minn + p - zp) / -2.5) * 0.263**2) * np.sqrt(4 * np.pi) * s for f, p, s, minn, maxx, zp in zip(full, perm, sigma_psf, mins, maxs, zps)]) # sky_noise = np.array([np.sqrt(10.0**(((maxx - minn) * f + minn + p - zp) / -2.5)) * # np.pi * ((2.04 * se) / 2) ** 2 # for f,p,minn,maxx,zp,se in zip(full, perm, mins, maxs, zps, seeing)]) zpsys = ['ab'] * times.size gains = np.ones(times.shape) obs = Table({'time': times, 'band': bands, 'gain': gains, 'skynoise': sky_noise, 'zp': zps, 'zpsys': zpsys}) model = sncosmo.Model(source='salt2-extended') model.set(z=z) mabs = np.random.normal(-19.3, 0.3) model.set_source_peakabsmag(mabs, 'bessellb', 'ab') x0 = model.get('x0') x1 = np.random.normal() c = np.random.normal(scale=0.1) p = {'z': z, 't0': t0, 'x0': x0, 'x1': x1, 'c': c} lc = sncosmo.realize_lcs(obs, model, [p])[0] fig = sncosmo.plot_lc(lc) fig.savefig(temp_dir + os.sep + "lc_%d.png" % seed, bbox_inches="tight", dpi=300) ston = (lc["flux"] / lc["fluxerr"]).max() print(z, t0, x0, x1, c, ston) return z, t0, x0, x1, c, ston, lc
def _get_lightcurve_(self, p, obs, idx_orig=None): """ """ if obs is not None: ra, dec, mwebv_sfd98 = p.pop('ra'), p.pop('dec'), p.pop( 'mwebv_sfd98') # Get unperturbed lc from sncosmo lc = sncosmo.realize_lcs(obs, self.generator.model, [p], scatter=False)[0] # Replace fluxerrors with covariance matrix that contains # correlated terms for the calibration uncertainty fluxerr = np.sqrt(obs['skynoise']**2 + np.abs(lc['flux']) / obs['gain']) fluxcov = np.diag(fluxerr**2) save_cov = False for band in set(obs['band']): if self.instruments[band]['err_calib'] is not None: save_cov = True idx = np.where(obs['band'] == band)[0] err = self.instruments[band]['err_calib'] for k0 in idx: for k1 in idx: fluxcov[k0, k1] += (lc['flux'][k0] * lc['flux'][k1] * err**2) # Add random (but correlated) noise to the fluxes fluxchol = np.linalg.cholesky(fluxcov) flux = lc['flux'] + fluxchol.dot(np.random.randn(len(lc))) # Apply blinded bias if given if self.blinded_bias is not None: bias_array = np.array([ self.blinded_bias[band] if band in self.blinded_bias.keys() else 0 for band in obs['band'] ]) flux *= 10**(-0.4 * bias_array) lc['flux'] = flux lc['fluxerr'] = np.sqrt(np.diag(fluxcov)) # Additional metadata for the lc fitter lc.meta['ra'] = ra lc.meta['dec'] = dec if save_cov: lc.meta['fluxcov'] = fluxcov lc.meta['mwebv_sfd98'] = mwebv_sfd98 if idx_orig is not None: lc.meta['idx_orig'] = idx_orig else: lc = None return lc
def reals(self, taxis, band): """ Generate simulated light curves for a given parameter setting """ assert len(taxis) == len(band) lcs = sncosmo.realize_lcs(self.obs(taxis, band), self.model, [self.params()]) return lcs
def generate_ii_light_curve(z, mabs, source=None, **kwargs): if source is None: sources = [a[0] for a in sncosmo.builtins.models if a[1] == "SN IIP"] source = sources[np.random.randint(0, len(sources))] obs, t0, bands, zps = get_obs_times_and_conditions(**kwargs) model = sncosmo.Model(source=source) model.set(z=z) model.set_source_peakabsmag(mabs, "bessellb", "ab") p = {"z": z, "amplitude": model.get("amplitude"), "t0": t0} lc = sncosmo.realize_lcs(obs, model, [p])[0] return lc
def generate_lcs(self): if bench: print "Generating lcs...." self.lcs = [] start = time.time() for p in self.params: libid = np.random.choice(self.simlib_obs_sets.keys()) light_curve = sncosmo.realize_lcs(self.simlib_obs_sets[libid], self.model, [p]) flag = self.impose_SNR_cuts(light_curve[0]) if flag: for jj, b in enumerate(light_curve[0]['band']): if b == "desg": light_curve[0]['zp'][jj] += self.zp_off[0] if b == "desr": light_curve[0]['zp'][jj] += self.zp_off[1] if b == "desi": light_curve[0]['zp'][jj] += self.zp_off[2] if b == "desz": light_curve[0]['zp'][jj] += self.zp_off[3] self.lcs.append(light_curve) end = time.time() if bench: print "Simulation took", end - start, "secs" print "Number of simulations passing cuts:", len(self.lcs)
def realise_light_curve(seed): np.random.seed(seed) t0 = 1000 x0 = 1e-5 x1 = 0 c = 0 e = False and np.random.random() > 0.8 if e: u = np.random.random() > 0.3 if u: z = 0.1 else: z = 0.9 else: z = np.random.uniform(0.1, 0.9) sn = np.random.uniform(low=50, high=300) num_obs = 20 deltat = -35 ts = np.arange(t0 + deltat, (t0 + deltat) + 5 * num_obs, 5) times = np.array([[t, t + 0.05, t + 0.1, t + 0.2] for t in ts]).flatten() bands = [b for t in ts for b in ['desg', 'desr', 'desi', 'desz']] gains = np.ones(times.shape) skynoise = sn * np.ones(times.shape) zp = 30 * np.ones(times.shape) zpsys = ['ab'] * times.size obs = Table({'time': times, 'band': bands, 'gain': gains, 'skynoise': skynoise, 'zp': zp, 'zpsys': zpsys}) model = sncosmo.Model(source='salt2-extended') p = {'z': z, 't0': t0, 'x0': x0, 'x1': x1, 'c': c} model.set(z=z) lc = sncosmo.realize_lcs(obs, model, [p])[0] ston = (lc["flux"] / lc["fluxerr"]).max() return z, t0, x0, x1, c, ston, lc
def generate_lcs(self): if bench: print "Generating lcs...." self.lcs = [] start = time.time() for p in self.params: #libid = 2498 #EJ: Nov 7th right now just generate all with same libid. #Need to sort simlib times so they are monotonically increasing libid = np.random.choice(self.simlib_obs_sets.keys()) light_curve = sncosmo.realize_lcs(self.simlib_obs_sets[libid], self.model, [p]) flag = self.impose_SNR_cuts(light_curve[0]) #print type(light_curve[0]),light_curve[0] if flag: for jj, b in enumerate(light_curve[0]['band']): if b == "desg": light_curve[0]['zp'][jj] += self.zp_off[0] if b == "desr": light_curve[0]['zp'][jj] += self.zp_off[1] if b == "desi": light_curve[0]['zp'][jj] += self.zp_off[2] if b == "desz": light_curve[0]['zp'][jj] += self.zp_off[3] self.lcs.append(light_curve) end = time.time() if bench: print "Simulation took", end - start, "secs" print "Number of simulations passing cuts:", len(self.lcs)
def simulate_simlib(simlibfile, snmodelsource, outfile="LC/simulatedlc.dat", restrict=10): """ Simulate SN based on the simlibfile using SNCosmo SALT models Parameters ---------- simlibfile : snmodelsource: outfile: Returns ------- """ from astropy.units import Unit from astropy.coordinates import SkyCoord # read the simlibfile into obstables meta, obstables = sncosmo.read_snana_simlib(simlibfilename) # set the SNCosmo model source dustmaproot = os.getenv("SIMS_DUSTMAPS_DIR") map_dir = os.path.join(dustmaproot, "DustMaps") dust = sncosmo.CCM89Dust() model = Model( source=snmodelsource, effects=[dust, dust], effect_frames=["rest", "obs"], effect_names=["host", "mw"] ) maxSNperField = restrict # Different fields in SIMLIB are indexed by libids libids = obstables.keys() lcs = [] for libid in libids: # Get the obstable corresponding to each field obstable = obstables[libid] manipulateObsTable(obstable) # Need Area from PixSize ra = obstable.meta["RA"] dec = obstable.meta["DECL"] area = obstable.meta["PIXSIZE"] maxmjd = obstable["time"].max() minmjd = obstable["time"].min() rangemjd = maxmjd - minmjd skycoords = SkyCoord(ra, dec, unit="deg") t_mwebv = sncosmo.get_ebv_from_map(skycoords, mapdir=map_dir, interpolate=False) model.set(mwebv=t_mwebv) params = [] # col = Table.Column(obstable['SEARCH'].size*['ab'], name='zpsys') # obstable['FLT'].name = 'band' # obstable['MJD'].name = 'time' # obstable['ZPTAVG'].name = 'zp' # obstable['CCD_GAIN'].name = 'gain' # obstable['SKYSIG'].name = 'skynoise' # obstable.add_column(col) redshifts = list(sncosmo.zdist(0.0, 1.2, ratefunc=cosmoRate, time=rangemjd, area=area)) print "num SN generated ", len(redshifts) for i, z in enumerate(redshifts): mabs = normal(-19.3, 0.3) model.set(z=z) model.set_source_peakabsmag(mabs, "bessellb", "ab") x0 = model.get("x0") # RB: really should not be min, max but done like in catalogs p = {"z": z, "t0": uniform(minmjd, maxmjd), "x0": x0, "x1": normal(0.0, 1.0), "c": normal(0.0, 0.1)} params.append(p) if maxSNperField is not None: if i == maxSNperField: break print "realizing SN" lcslib = sncosmo.realize_lcs(obstable, model, params, trim_observations=True) lcs.append(lcslib) # alllcsintables = vstack(lcslib) # print alllcsintables[0] # print alllcsintables[MJD].size # write light curves to disk for i, field in enumerate(lcs): for snid, lc in enumerate(field): sncosmo.write_lc(lc, fname=outfile + "_" + str(i) + "_" + str(snid) + ".dat", format="ascii") return lcs
def realise_light_curve(temp_dir, zeros, seed, scatter=0.3, cosmology=None, mabs=-19.3): simulation = cosmology is not None if cosmology is None: cosmology = WMAP9 np.random.seed(seed) t0 = 1000 num_obs = 20 if simulation: if np.random.random() < 0.1: z = np.random.uniform(0.03, 0.2) else: zs = list(sncosmo.zdist(0.05, 1.2, area=30, time=10000)) z_ind = np.random.randint(0, len(zs)) z = zs[z_ind] print("Simulation: z=%0.6f" % z) else: z = np.random.uniform(0.1, 0.9) deltat = -35 newmoon = 1000 - np.random.uniform(0, 29.5) ts = np.arange(t0 + deltat, (t0 + deltat) + 5 * num_obs, 5) bands = [b for t in ts for b in ['desg', 'desr', 'desi', 'desz']] zps = np.array([b for t in ts for b in zeros]) mins = np.array([b for t in ts for b in [22.1, 21.1, 20.1, 18.7]]) maxs = np.array([b for t in ts for b in [19.4, 19.7, 19.4, 18.2]]) seeing = np.array([b for t in ts for b in [1.06, 1.00, 0.96, 0.93]]) times = np.array([[t, t + 0.05, t + 0.1, t + 0.2] for t in ts]).flatten() full = 0.5 + 0.5 * np.sin((times - newmoon) * 2 * np.pi / 29.5) perm = np.random.uniform(-0.1, 0.1, full.shape) seeing2 = np.random.uniform(4, 6, full.shape) sigma_psf = seeing2 / 2.36 sky_noise = np.array([np.sqrt(10.0**(((maxx - minn) * f + minn + p - zp) / -2.5) * 0.263**2) * np.sqrt(4 * np.pi) * s for f, p, s, minn, maxx, zp in zip(full, perm, sigma_psf, mins, maxs, zps)]) zpsys = ['ab'] * times.size gains = np.ones(times.shape) obs = Table({'time': times, 'band': bands, 'gain': gains, 'skynoise': sky_noise, 'zp': zps, 'zpsys': zpsys}) model = sncosmo.Model(source='salt2-extended') model.set(z=z) if not simulation: x1 = 0 c = 0 else: x1 = np.random.normal(0, 0.5) c = np.random.normal(0, 0.1) alpha = 0.14 beta = 3.15 mabs = np.random.normal(mabs, scatter) - alpha * x1 + beta * c model.set_source_peakabsmag(mabs, 'bessellb', 'ab', cosmo=cosmology) x0 = model.get('x0') p = {'z': z, 't0': t0, 'x0': x0, 'x1': x1, 'c': c} lc = sncosmo.realize_lcs(obs, model, [p])[0] ston = (lc["flux"] / lc["fluxerr"]).max() return z, t0, x0, x1, c, ston, lc
def random_obs(temp_dir, seed): np.random.seed(seed) interp = generate_and_return() x1 = np.random.normal() # colour = np.random.normal(scale=0.1) colour = 0 x0 = 1e-5 # t0 = np.random.uniform(low=1000, high=2000) t0 = 1000 z = np.random.uniform(low=0.1, high=1.0) # deltat = np.random.uniform(low=-20, high=0) # num_obs = np.random.randint(low=10, high=40) num_obs = 20 deltat = -35 filename = temp_dir + "/save_%d.npy" % seed if not os.path.exists(filename): ts = np.arange(t0 + deltat, (t0 + deltat) + 5 * num_obs, 5) times = np.array([[t, t + 0.05, t + 0.1, t + 0.2] for t in ts]).flatten() bands = [b for t in ts for b in ["desg", "desr", "desi", "desz"]] gains = np.ones(times.shape) skynoise = np.random.uniform(low=20, high=800) * np.ones(times.shape) zp = 30 * np.ones(times.shape) zpsys = ["ab"] * times.size obs = Table({"time": times, "band": bands, "gain": gains, "skynoise": skynoise, "zp": zp, "zpsys": zpsys}) model = sncosmo.Model(source="salt2") p = {"z": z, "t0": t0, "x0": x0, "x1": x1, "c": colour} model.set(z=z) print(seed, " Vals are ", p) lc = sncosmo.realize_lcs(obs, model, [p])[0] ston = (lc["flux"] / lc["fluxerr"]).max() model.set(t0=t0, x1=x1, c=colour, x0=x0) try: res, fitted_model = sncosmo.fit_lc( lc, model, ["t0", "x0", "x1", "c"], guess_amplitude=False, guess_t0=False ) except ValueError: return np.nan, np.nan, x1, colour, num_obs, ston, deltat, z, 0 fig = sncosmo.plot_lc(lc, model=fitted_model, errors=res.errors) fig.savefig(temp_dir + os.sep + "lc_%d.png" % seed, bbox_inches="tight", dpi=300) my_model = PerfectRedshift([lc], [z], t0, name="posterior%d" % seed) sampler = EnsembleSampler(temp_dir=temp_dir, num_burn=400, num_steps=1500) c = ChainConsumer() my_model.fit(sampler, chain_consumer=c) map = {"x0": "$x_0$", "x1": "$x_1$", "c": "$c$", "t0": "$t_0$"} parameters = [map[a] for a in res.vparam_names] mu1 = get_mu_from_chain(interped, c.chains[-1], c.parameters[-1]) c.parameteers[-1].append(r"$\mu$") c.chains[-1] = np.hstack((c.chains[-1], mu1[:, None])) chain2 = np.random.multivariate_normal(res.parameters[1:], res.covariance, size=int(1e5)) chain2 = np.hstack((chain2, get_mu_from_chain(interp, chain2, parameters)[:, None])) c.add_chain(chain2, parameters=parameters, name="Gaussian") figfilename = filename.replace(".npy", ".png") c.plot(filename=figfilename, truth={"$t_0$": t0, "$x_0$": x0, "$x_1$": x1, "$c$": colour}) means = [] stds = [] isgood = ( (np.abs(x1 - res.parameters[3]) < 4) & (np.abs(colour - res.parameters[4]) < 2) & (res.parameters[2] > 0.0) ) isgood *= 1.0 for i in range(len(c.chains)): a = c.chains[i][:, -1] means.append(a.mean()) stds.append(np.std(a)) diffmu = np.diff(means)[0] diffstd = np.diff(stds)[0] np.save(filename, np.array([diffmu, diffstd, ston, 1.0 * isgood])) else: vals = np.load(filename) diffmu = vals[0] diffstd = vals[1] ston = vals[2] isgood = vals[3] return diffmu, diffstd, x1, colour, num_obs, ston, deltat, z, isgood
skynoise = 80 * np.ones(times.shape) zp = 30 * np.ones(times.shape) zpsys = ['ab'] * times.size obs = Table({'time': times, 'band': bands, 'gain': gains, 'skynoise': skynoise, 'zp': zp, 'zpsys': zpsys}) model = sncosmo.Model(source='salt2-extended') p = {'z': z, 't0': t0, 'x0': x0, 'x1': x1, 'c': colour} model.set(z=z) print("Realise LCs") lcs = sncosmo.realize_lcs(obs, model, [p]) print("Fit LCs") res, fitted_model = sncosmo.fit_lc(lcs[0], model, ['t0', 'x0', 'x1', 'c']) dir_name = os.path.dirname(__file__) temp_dir = dir_name + os.sep + "output" surface = temp_dir + os.sep + "surfaces_simple.png" mu_simple = temp_dir + os.sep + "mu_simple.png" mcmc_chain = temp_dir + os.sep + "mcmc_simple.npy" c = ChainConsumer() print("Fit model") my_model = PerfectRedshift(lcs, [z], t0, name="My posterior") sampler = EnsembleSampler(temp_dir=temp_dir, num_steps=20000) my_model.fit(sampler, chain_consumer=c) c.add_chain(np.random.multivariate_normal(res.parameters[1:], res.covariance, size=int(1e7)), name="Summary Stats", parameters=["$t_0$", "$x_0$", "$x_1$", "$c$"])
obstable['MJD'].name = 'time' obstable['ZPTAVG'].name = 'zp' obstable['CCD_GAIN'].name = 'gain' obstable['SKYSIG'].name = 'skynoise' col = Table.Column(prefixbandname("LSST_", obstable), name='band') obstable.add_column(col) return None manipulateObsTable(obstable) # Now display the table of observations print str(obstable) ## Simulate the SN, plot and profile relevantdata = sncosmo.realize_lcs(obstable, model, params, trim_observations=True) from astropy.table import Table print type(relevantdata) x = Table(relevantdata[0]) print str(x) print str(len(relevantdata[0])) # Write out to a file as this is how we will do things in a larger set by looping through, and then read in the file model.set(**params[0]) # fig_relevant = sncosmo.plot_lc(relevantdata[0], model=model)
def get_lightcurves(self, obs, trim_observations=True, **kwargs): """Realize lightcurves based on the randomized lightcurve parameters and a single set of observations""" params = self.get_lightcurve_full_param(full_out=False) return sncosmo.realize_lcs(obs, self.model, params, trim_observations=trim_observations, **kwargs)
def __init__(self, sntype, observations=None, z_range=[1.8,2.2], t0_range=[0,0], nsim=100, perfect=True, Om=0.3, H0=70, filterset='hst' ): """ Run a monte carlo sim using sncosmo to simulate <nsim> SNe of the given <sntype> over the given <z_range>. Simulates Type Ia SNe with the SALT2 model, and CC SNe with the SNANA CC templates. Observations are done at time t=0, unless specified otherwise in a user-defined observations table. Set perfect=True for noiseless "observations" of the simulated SNe. :return: """ from astropy import cosmology import sncosmo from numpy.random import normal, uniform, choice import numpy as np self.sntype = sntype self.z_range = z_range self.nsim = nsim self.perfect = perfect if observations is None : observations = mkobservationsTable( filterset=filterset ) self.observations = observations # Make a list of all the unique sncosmo source models available, # and assign a relative probability that any given simulated SN of this # type (CC or Ia) belongs to that subclass if sntype.lower() in ['cc','ii','ibc'] : subClassDict = SubClassDict_SNANA[sntype.lower()] subClassProbs = ccSubClassProbs[sntype.lower()] self.SourcenameSet = np.array( subClassDict.keys() ) self.SubclassSet = np.array([ subClassDict[source] for source in self.SourcenameSet ]) self.SubclassCount = np.array([ len(np.where(self.SubclassSet==subclass)[0]) for subclass in self.SubclassSet ], dtype=float) self.SourceprobSet = np.array([ subClassProbs[subclass] for subclass in self.SubclassSet ]) / self.SubclassCount self.SourceprobSet /= self.SourceprobSet.sum() elif sntype.lower()=='ia' : # No sub-class divisions for SNIa self.SourcenameSet = np.array(['salt2-extended']) self.SubclassSet = np.array( ['Ia'] ) self.SourceprobSet = np.array( [1] ) self.SubclassCount = np.array( [1] ) # load the O'Donnell 1994 dust model self.dust = sncosmo.OD94Dust() # Define an sncosmo SN model for each available source modelset = np.array([ sncosmo.Model(source=source, effects=[self.dust], effect_names=['host'], effect_frames=['rest']) for source in self.SourcenameSet ]) # Define a cosmology # self.Om = Om # self.H0 = H0 self.cosmo = cosmology.FlatLambdaCDM(Om0=Om, H0=H0) # For each simulated SN, draw random Av from distributions # as defined in Rodney et al 2014a : # For SN Ia : P(Av) = exp(-Av/0.33) # For CC SN : P(Av) = 4 * gauss(0.6) + exp(-Rv/1.7) if sntype=='Ia': tau,sigma,R0 = 0.33, 0, 0 else : tau,sigma,R0 = 1.7, 0.6, 4 self.Av = mcsample( pAv, nsim, tau=tau, sigma=sigma, R0=R0 ) # For each simulated SN, draw a random Rv from a normal # distribution centered on 3.1 with width 0.5 self.Rv = normal( 3.1, 0.5, nsim ) self.Rv = np.where( self.Rv>0, self.Rv, 0.01 ) # Convert Av and Rv to E(B-V) : # Rv = Av/EBV ==> EBV = Av/Rv self.EBV = self.Av / self.Rv # TODO : draw the redshifts with a metropolis-hastings sampler to match # a distribution defined based on the expected SN rate # Disabled : uniform redshift spacing # zlist = np.linspace( z_range[0], z_range[1], nsim ) # Draw a random redshift from a uniform distribution self.z = uniform( low=z_range[0], high=z_range[1], size=nsim ) lightcurvelist = [] peakabsmagRlist = [] modelparamlist = [] subclasslist = [] modelindexlist = [] sourcenamelist = [] t0list = [] if sntype=='Ia': x0list = [] x1list = [] clist = [] else : amplitudelist = [] for isim in range(self.nsim): # Randomly draw an sncosmo model from the available list, according to # the predefined probability list, setting the SN sub-class for this # simulated SN imodel = choice( np.arange(len(modelset)), replace=True, p=self.SourceprobSet ) model = modelset[imodel] subclass = self.SubclassSet[imodel] z = self.z[isim] EBV = self.EBV[isim] Rv = self.Rv[isim] # Set the peak absolute magnitude according to the observed # luminosity functions, as defined in Table 3 of Graur:2014a; # and set the host extinction according to the 'mid' dust model # of Rodney:2014a. if subclass == 'Ia' : MR = normal( -19.37, 0.47 ) elif subclass == 'Ib' : MR = normal( -17.90, 0.90 ) elif subclass == 'Ic' : MR = normal( -18.30, 0.60 ) elif subclass == 'IIP' : MR = normal( -16.56, 0.80 ) elif subclass == 'IIL' : MR = normal( -17.66, 0.42 ) elif subclass == 'IIn' : MR = normal( -18.25, 1.00 ) model.set(z=z) model.set_source_peakabsmag( MR, 'bessellr', 'vega', cosmo=self.cosmo) modelindexlist.append( imodel ) subclasslist.append( subclass ) peakabsmagRlist.append( MR ) sourcenamelist.append( self.SourcenameSet[imodel] ) if subclass =='Ia' : x0 = model.get('x0') # TODO : use bifurcated gaussians for more realistic x1,c dist'ns x1 = normal(0., 1.) c = normal(0., 0.1) t0 = uniform( t0_range[0], t0_range[1] ) modelparams = {'z':z, 't0':t0, 'x0':x0, 'x1':x1, 'c':c, 'hostebv':EBV, 'hostr_v':Rv} t0list.append( t0 ) x0list.append( x0 ) x1list.append( x1 ) clist.append( c ) t0list.append( t0 ) else : amplitude = model.get('amplitude') t0 = uniform( t0_range[0], t0_range[1] ) modelparams = {'z':z, 't0':t0, 'amplitude':amplitude, 'hostebv':EBV, 'hostr_v':Rv } amplitudelist.append( amplitude ) t0list.append( t0 ) modelparamlist.append( modelparams ) # Generate one simulated SN: snlc = sncosmo.realize_lcs(self.observations, model, [ modelparams ], thresh=None)#, perfect=perfect ) lightcurvelist.append( snlc[0] ) self.lightcurves = lightcurvelist self.t0 = np.array( t0list ) self.modelindex = np.array( modelindexlist ) self.sourcename = np.array( sourcenamelist ) self.subclass = np.array( subclasslist ) self.modelparam = np.array( modelparamlist ) self.peakabsmagR = np.array( peakabsmagRlist ) if sntype=='Ia': self.x0 = np.array( x0list ) self.x1 = np.array( x1list ) self.c = np.array( clist ) else : self.amplitude = np.array( amplitudelist ) return