def get_zdist(self):
        sn_z = []
        for field in self.survey_fields.values():
            z = list(
                sncosmo.zdist(field[1],
                              field[2],
                              area=field[0],
                              ratefunc=field[3],
                              time=field[4],
                              cosmo=self.cosmo))
            sn_z = np.concatenate((sn_z, z))
        total_z = len(sn_z)
        truncated_z = []
        for field in self.survey_fields.values():
            z = list(
                sncosmo.zdist(field[1],
                              field[2],
                              area=field[0],
                              ratefunc=field[3],
                              time=field[4],
                              cosmo=self.cosmo))
            field_frac = len(z) / float(total_z)
            z_sample = self.draw_fracZ_from_field(z, field_frac)
            truncated_z = np.concatenate((truncated_z, z_sample))

        return truncated_z
Exemple #2
0
    def get_zdist(self):
        sn_z = []
        for field in self.survey_fields.values():
            z = list(
                sncosmo.zdist(field[1],
                              field[2],
                              area=field[0],
                              ratefunc=field[3],
                              time=field[4],
                              cosmo=self.cosmo))
            sn_z = np.concatenate((sn_z, z))
        total_z = len(sn_z)
        truncated_z = []
        for field in self.survey_fields.values():
            z = list(
                sncosmo.zdist(field[1],
                              field[2],
                              area=field[0],
                              ratefunc=field[3],
                              time=field[4],
                              cosmo=self.cosmo))
            field_frac = len(z) / float(total_z)
            z_sample = self.draw_fracZ_from_field(z, field_frac)
            truncated_z = np.concatenate((truncated_z, z_sample))

        if setplot:
            #COMPARING THE SNANA TEST DISTRIBUTION TO THE SNCOSMO DISTRIBUTION
            snana_sim_file = 'SNANA_ALL.DUMP'
            snana = ascii.read(snana_sim_file)
            snana_z = snana['GENZ']
            zbins = np.arange(0, 1.3, 0.1)
            if bench:
                print 'Size of SNANA ALL:', len(snana_z)
                print 'Size of SNCOSMO ALL:', len(sn_z)
            plt.hist(sn_z, normed=True, label='SNCOSMO', bins=zbins)
            plt.hist(truncated_z,
                     normed=True,
                     label='SNCOSMO_downsampled',
                     bins=zbins,
                     color='green',
                     histtype='step',
                     lw=2)
            plt.hist(snana_z,
                     normed=True,
                     label='SNANA',
                     color='red',
                     histtype='step',
                     lw=2,
                     bins=zbins)
            plt.xlabel('Redshift')
            plt.ylabel('Normed PDF')
            plt.legend(loc='upper left', frameon=False)
            plt.title('Redshift Distribution Before Cuts')
            plt.savefig('z_dist.png')
            plt.close()

        return truncated_z
Exemple #3
0
    def _update_simulation_(self):
        """
        """
        # -----------------------
        # - Redshift from Rate
        if "ntransient" not in self.transient_coverage.keys():
            self.simul_parameters["zcmb"] = \
                list(sncosmo.zdist(self.zcmb_range[0], self.zcmb_range[1],
                                   time=self.timescale, area=self.coveredarea,
                                   ratefunc=self.ratefunc))
        else:
            self.simul_parameters["zcmb"] = \
                list(zdist_fixed_nsim(self.transient_coverage["ntransient"],
                                      self.zcmb_range[0], self.zcmb_range[1],
                                      ratefunc=self.ratefunc))

        self.simul_parameters["mjd"] = self._simulate_mjd_()
        self.simul_parameters["ra"], self.simul_parameters["dec"] = \
          random.radec(self.ntransient,
                       ra_range=self.ra_range,
                       dec_range=self.dec_range,
                       mw_exclusion=self._get_event_property_("mw_exclusion"))
        self._derived_properties['mwebmv'] = None

        if "lightcurve_prop" in self.transient_coverage.keys():
            lc = self.transient_coverage["lightcurve_prop"]
            param = lc["param_func"](self.zcmb, self.model,
                                     **lc["param_func_kwargs"])
            self._derived_properties["simul_parameters"]["lightcurve"] = param
    def _update_simulation_(self):
        """
        """
        # -----------------------
        # - Redshift from Rate
        if "ntransient" not in self.transient_coverage.keys():
            self.simul_parameters["zcmb"] = \
                list(sncosmo.zdist(self.zcmb_range[0], self.zcmb_range[1],
                                   time=self.timescale, area=self.coveredarea,
                                   ratefunc=self.ratefunc))
        else:
            self.simul_parameters["zcmb"] = \
                list(zdist_fixed_nsim(self.transient_coverage["ntransient"],
                                      self.zcmb_range[0], self.zcmb_range[1],
                                      ratefunc=self.ratefunc))
            
        self.simul_parameters["mjd"] = self._simulate_mjd_()
        self.simul_parameters["ra"], self.simul_parameters["dec"] = \
          random.radec(self.ntransient,
                       ra_range=self.ra_range,
                       dec_range=self.dec_range,
                       mw_exclusion=self._get_event_property_("mw_exclusion"))
        self._derived_properties['mwebv'] = None

        if "lightcurve_prop" in self.transient_coverage.keys():
            lc = self.transient_coverage["lightcurve_prop"]
            param = lc["param_func"](self.zcmb,self.model,
                                     **lc["param_func_kwargs"])
            self._derived_properties["simul_parameters"]["lightcurve"] = param 
Exemple #5
0
 def redshifts(self, area, tmin, tmax, zmax):
     """
     Get the redshifts of the supernovae in the survey
     Inputs: maximum redshift,
     """
     reds = list(sncosmo.zdist(0., zmax, time=(tmax-tmin), area = area))
     return reds
Exemple #6
0
    def zdist(self, n, z=[0.8, 1.2]):
        """
        get a redshift distribution for a number of SNe from JWST
        (aiming for ~ 100 or lower)
        """
        time = 200
        area= 20
        
        reds= list(sncosmo.zdist(z[0], z[1], time = time, area =area))
        
        while len(reds) > n:

            time -= 10
            
            reds= list(sncosmo.zdist(z[0], z[1], time=time, area=area))
        
        return reds
Exemple #7
0
    def zdist(self, n, z=[0.8, 1.2]):
        """
        get a redshift distribution for a number of SNe from JWST
        (aiming for ~ 100 or lower)
        """
        time = 200
        area = 20

        reds = list(sncosmo.zdist(z[0], z[1], time=time, area=area))

        while len(reds) > n:

            time -= 10

            reds = list(sncosmo.zdist(z[0], z[1], time=time, area=area))

        return reds
Exemple #8
0
    def expected_z_dist(self, z=[0., 0.8], t=200, area=20):

        """
        For a 200d 20 square degree survey, the redshift distribution of expected supernovae (no magnitude cuts)

        Zmax  set by the maximum redshift of the rest frame filters

        SN rates are taken from the Perrett et al. 2012 paper for the SNe from SNLS surveyx
        """

                        
        return sorted(list(sncosmo.zdist(z[0], z[1], time=t, area=area, ratefunc=self.snrate_perrett)))
Exemple #9
0
def test_zdist():
    """Test that zdist function works."""

    np.random.seed(0)
    z = list(sncosmo.zdist(0., 0.25))

    # check that zdist returns the same number of SNe as
    # when this test was written (does not test whether the number is
    # correct)
    assert len(z) == 14

    # check that all values are indeed between the input limits.
    zarr = np.array(z)
    assert np.all((zarr > 0.) & (zarr < 0.25))
Exemple #10
0
def test_zdist():
    """Test that zdist function works."""

    np.random.seed(0)
    z = list(sncosmo.zdist(0., 0.25))

    # check that zdist returns the same number of SNe as
    # when this test was written (does not test whether the number is
    # correct)
    assert len(z) == 14

    # check that all values are indeed between the input limits.
    zarr = np.array(z)
    assert np.all((zarr > 0.) & (zarr < 0.25))
def simulate_z_coverage(z_range,npoints=None,snratefunc=(lambda z: 3e-5),time=365.25,area=2e4,
                        z_pdf=None,z_pdf_bins=None):
    """

    """
    if (len(z_range) != 2 or z_range[0] > z_range[1]):
        raise ValueError('Invalid z_range')

        
    if z_pdf is None:
        from sncosmo import zdist
        from astropy.cosmology import FlatLambdaCDM

        if npoints is None:
            z = zdist(z_range[0], z_range[1], time, area, snratefunc,
                      FlatLambdaCDM(H0=_H_0, Om0=_O_M))
            z = np.array(list(z))
        else:
            # Create generator for huge member of redshifts but only draw up to npoints
            # Settings should make a generator for several million redshifts
            gen = zdist(z_range[0], z_range[1], 1e6, 42000, snratefunc,
                      FlatLambdaCDM(H0=_H_0, Om0=_O_M))
            z = []
            for k in xrange(npoints):
                z.append(gen.next())
            z= np.array(z)
    else:
        if z_pdf_bins is None:
            z_pdf_bins = np.linspace(z_range[0],z_range[1],len(z_pdf)+1)
        elif (np.abs(z_pdf_bins[0] - z_range[0]) / z_range[0] > 1e-9 
              or np.abs(z_pdf_bins[-1] - z_range[1]) / z_range[1] > 1e-9 
              or True in [a>b for a,b in zip(z_pdf_bins[:-1],z_pdf_bins[1:])]):
            print np.abs(z_pdf_bins[0] - z_range[0]) / z_range[0] > 1e-9 
            print np.abs(z_pdf_bins[-1] - z_range[1]) / z_range[1] > 1e-9 
            print [a>b for a,b in zip(z_pdf_bins[:-1],z_pdf_bins[1:])]
            print True in [a>b for a,b in zip(z_pdf_bins[:-1],z_pdf_bins[1:])]
            raise ValueError('Invalid z_pdf_bins')
Exemple #12
0
    def get_simulation(self, omega_m=0.3, H0=70, snIa_luminosity=-19.3, snIa_sigma=0.1, snII_luminosity=-18.5,
                       snII_sigma=0.3, sn_rate=0.7, num_days=100, area=1, zmax=0.8, mean_num_obs=20):
        np.random.seed(1)

        # Get cosmology
        w_0 = -1
        cosmology = FlatwCDM(Om0=omega_m, w0=w_0, H0=H0)

        # Get redshift distribution of supernova
        tmin = 56700
        tmax = tmin + num_days
        redshifts = list(sncosmo.zdist(0., zmax, time=num_days, area=area))
        types = ["Ia" if np.random.random() < sn_rate else "II" for z in redshifts]
        lums = [snIa_luminosity if t == "Ia" else snII_luminosity for t in types]
        sigs = [snIa_sigma if t == "Ia" else snII_sigma for t in types]

        self.logger.info('Getting data for %d days of transients, with %d supernova' % (num_days, len(redshifts)))

        num_obs = np.ceil(np.random.normal(loc=mean_num_obs, scale=2.0, size=len(redshifts)))

        observations = [self.get_supernova(z, n, tmin, tmax, cosmology, x0_mean=l, x0_sigma=s) for z, n, l, s in zip(redshifts, num_obs, lums, sigs)]

        theta = {
            "$w$": w_0,
            r"$\Omega_m$": omega_m,
            r"$H_0$": H0,
            r"$L_{\rm SnIa}$": snIa_luminosity,
            r"$L_{\rm SnII}$": snII_luminosity,
            r"$\sigma_{\rm SnIa}$": snIa_sigma,
            r"$\sigma_{\rm SnII}$": snII_sigma,
            "$r$": sn_rate
        }
        for i, obs in enumerate(observations):
            for k, v in obs.meta.items():
                key = "$%s_{%d}$" % (k, i)
                theta[key] = v
        print(theta)
        data = {"z_o": redshifts, "type_o": types, "lcs_o": observations}
        return data, theta
Exemple #13
0
    def simulate(self, bandpasses, norm=None, seed=13, Ncpus=None):

        self.obs = np.array([])

        np.random.seed(seed)

        tstep = 1
        tmin = self.model.mintime()
        tmax = self.model.maxtime()
        time = np.arange(tmin, tmax + tstep, tstep)

        wstep = 10
        wmin = self.model.minwave()
        wmax = self.model.maxwave()
        wavelen = np.arange(wmin, wmax + wstep, wstep)

        fluxes = self.model.flux(time, wavelen)
        norm = np.max(fluxes) if norm is None else norm
        fluxes = fluxes.T * norm / np.max(fluxes)

        lc = LightCurve(time, wavelen, fluxes)

        redshifts = list(
            sncosmo.zdist(self.zmin,
                          self.zmax,
                          time=self.duration,
                          area=self.area))

        tasks = list(
            zip(redshifts, [self] * len(redshifts),
                [bandpasses] * len(redshifts), [self.bias] * len(redshifts),
                [lc] * len(redshifts),
                np.random.randint(2**32 - 1, size=len(redshifts))))
        with MultiPool(processes=Ncpus) as pool:
            observations = np.array(list(pool.map(survey_worker, tasks)))
        self.obs = observations
Exemple #14
0
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
dm = 24 - -19.1		#Work out limiting magnitude for a Ia

r0 = 2.27e-5		#Rate parameters
a = 1.7			#Rate parameters

#Set up rate function for sncosmo
def snrate(z):
	return r0*(1+z)**a

#Maximum z to which we can see SNe Ia with LSST
zmax=z_at_value(cosmo.distmod, dm*u.mag)

#-------------------------------------------------------------------------------------------------
#Generate SNIa redshifts

redshifts = list(sncosmo.zdist(0., zmax, time=(mjd_end-mjd_start), area=area, ratefunc=snrate))
redshifts=np.array(redshifts,dtype=float)
num_Ia=len(redshifts)

#-------------------------------------------------------------------------------------------------
#Make 0.8 Ia, 0.15 Ia-91T, 0.05 Ia-91bg, and split in different types of Ia using numbers from Li et al 2011c.

redshifts_91bg=random.sample(redshifts,int(num_Ia*0.05))
bg_idx=np.isin(redshifts,redshifts_91bg,invert=True)
redshifts_91bg=random.sample(redshifts_91bg,int(len(redshifts_91bg)*0.4545))

redshifts=redshifts[bg_idx]

redshifts_91T=random.sample(redshifts,int(num_Ia*0.15))
T_idx=np.isin(redshifts,redshifts_91T,invert=True)
redshifts_91T=random.sample(redshifts_91T,int(len(redshifts_91T)*0.882))
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
Exemple #17
0
##PART 1: THE REDSHIFT DISTRIBUTION
##---------------------------------
##FROM THE SIMLIB, WE CAN GET THE AREAS OF THE DES FIELDS
##THIS SHOULD PROBABLY BE USER INPUT, BUT I'M JUST GOING TO
##HARD CODE THIS FOR NOW

print 'DRAWING THE REDSHIFT DISTRIBUTION'

xarea, carea, earea, sarea = 17.1173, 16.2981, 11.6045, 12.7980  #in deg^2
surveytime = 540  #getting this from SNANA_MAIN_FILE.input GENRANGE_PEAKMJD
zmin, zmax, zcut = 0.05, 1.2, 1.0  #from SNANA_MAIN_FILE.input GENRANGE_REDSHIFT & DNDZ

#WE NEED A LOW AND HIGH Z DISTRIBUTION IN EACH OF THE FOUR FIELD GROUPS
x_loz = sncosmo.zdist(zmin,
                      zcut,
                      area=xarea,
                      ratefunc=snana_snrate_loz,
                      time=surveytime)
x_hiz = sncosmo.zdist(zcut,
                      zmax,
                      area=xarea,
                      ratefunc=snana_snrate_hiz,
                      time=surveytime)
c_loz = sncosmo.zdist(zmin,
                      zcut,
                      area=carea,
                      ratefunc=snana_snrate_loz,
                      time=surveytime)
c_hiz = sncosmo.zdist(zcut,
                      zmax,
                      area=carea,
Exemple #18
0
 def z_dist_lsst(self):
     time = 1000
     area= 10
     return list(sncosmo.zdist(0, 1.2, time=time, area=area))
Exemple #19
0
 def z_dist_lsst(self):
     time = 1000
     area = 10
     return list(sncosmo.zdist(0, 1.2, time=time, area=area))