Exemple #1
0
 def test_mcmc_invalid_sampler(self):
     """Ensure that mcmc runs."""
     with pytest.raises(ValueError):
         sncosmo.mcmc_lc(self.data,
                         self.model, ['amplitude', 'z', 't0'],
                         bounds={'z': (0., 1.0)},
                         sampler='invalid_sampler')
Exemple #2
0
def get_gaussian_fit(z, t0, x0, x1, c, lc, seed, temp_dir, interped, type="iminuit"):
    model = sncosmo.Model(source='salt2-extended')
    p = {'z': z, 't0': t0, 'x0': x0, 'x1': x1, 'c': c}
    model.set(**p)

    correct_model = sncosmo.Model(source='salt2-extended')
    correct_model.set(**p)
    if type == "iminuit":
        res, fitted_model = sncosmo.fit_lc(lc, model, ['t0', 'x0', 'x1', 'c'],
                                           guess_amplitude=False, guess_t0=False)
        chain = np.random.multivariate_normal(res.parameters[1:], res.covariance, size=int(1e5))
        fig = sncosmo.plot_lc(lc, model=[fitted_model, correct_model], errors=res.errors)
        fig.savefig(temp_dir + os.sep + "lc_%d.png" % seed, bbox_inches="tight", dpi=300)
    elif type == "mcmc":
        res, fitted_model = sncosmo.mcmc_lc(lc, model, ['t0', 'x0', 'x1', 'c'], nburn=500, nwalkers=20,
                                            nsamples=1500, guess_amplitude=False, guess_t0=False)
        chain = np.random.multivariate_normal(res.parameters[1:], res.covariance, size=int(1e5))
    elif type == "nestle":
        bounds = {"t0": [980, 1020], "x0": [0.1e-6, 9e-3], "x1": [-10, 10], "c": [-1, 1]}
        res, fitted_model = sncosmo.nest_lc(lc, model, ['t0', 'x0', 'x1', 'c'], bounds,
                                            guess_amplitude=False, guess_t0=False)
        chain = np.random.multivariate_normal(res.parameters[1:], res.covariance, size=int(1e5))
    else:
        raise ValueError("Type %s not recognised" % type)

    map = {"x0": "$x_0$", "x1": "$x_1$", "c": "$c$", "t0": "$t_0$"}
    parameters = [map[a] for a in res.vparam_names]

    chain, parameters = add_mu_to_chain(interped, chain, parameters)
    return chain, parameters, res.parameters[1:], res.covariance
Exemple #3
0
def get_summary_stats(z, lc, method="emcee", convert_x0_to_mb=True):
    model = sncosmo.Model(source="salt2-extended")
    model.set(z=z)
    if method == "emcee":
        res, fitted_model = sncosmo.mcmc_lc(lc, model, ["t0", "x0", "x1", "c"])
    elif method == "minuit":
        res, fitted_model = sncosmo.fit_lc(lc, model, ["t0", "x0", "x1", "c"])
    else:
        raise ValueError("Method %s not recognised" % method)
    parameters = res.parameters[2:]

    if convert_x0_to_mb:
        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")
        parameters = np.array([mb, parameters[1], parameters[2]])
        x0, x1, c = 1, 2, 3
        sigma_mb2 = 5 * np.sqrt(res.covariance[x0, x0]) / (2 * x0 * np.log(10))
        sigma_mbx1 = -5 * res.covariance[x0, x1] / (2 * x0 * np.log(10))
        sigma_mbc = -5 * res.covariance[x0, c] / (2 * x0 * np.log(10))
        cov = res.covariance
        cov = np.array(
            [
                [sigma_mb2, sigma_mbx1, sigma_mbc],
                [sigma_mbx1, cov[x1, x1], cov[x1, c]],
                [sigma_mbc, cov[x1, c], cov[c, c]],
            ]
        )
    else:
        cov = res.covariance[1:, :][:, 1:]
    return parameters, cov
Exemple #4
0
    def test_mcmc_lc_returns_correct_type(self) -> None:
        """Test the fit_lc function returns an ``SNModel`` instance for the fitted model"""

        data = sncosmo.load_example_data()
        model = SNModel('salt2-extended')

        # Some sncosmo versions use deprecated emcee kwargs
        with warnings.catch_warnings():
            warnings.simplefilter('ignore')
            _, fitted_model = sncosmo.mcmc_lc(data, model, ['x0'])

        self.assertIsInstance(fitted_model, SNModel)
def shift_loop_mcmc(delta_t0, lsst_obs):
    model = sncosmo.Model(source='salt2-extended')
    var = []

    for i in delta_t0:
        try:
            sn = PerSNMetric(summarydf=lsst_obs.summary,t0=i, raCol='ra', decCol='dec', lsst_bp=lsst_bp)
            data = sn.SNCosmoLC() 
            mcmc_out = sncosmo.mcmc_lc(data,model,['z', 't0', 'x0', 'x1', 'c'],bounds={'z':(0.3, 0.7)})
            t = anf.ResChar.fromSNCosmoRes(mcmc_out)
            print(i,t.salt_samples().mu.std())
            var.append(t.salt_samples().mu.std()*t.salt_samples().mu.std())
        except:
            print('I failed!')
    
    return np.mean(np.array(var))
def mcmc_fit(data, model, vparam_names, **kwargs):
    """Fit light curves Monte Carlo sampling

    Args:
        data            (Table): Table of photometric data
        model           (Model): The model to fit
        vparam_names (iterable): Model parameters to vary
        Any other parameters for ``sncosmo.mcmc_lc``

    Returns:
         - A dictionary like object with fit results
         - A fitted ``Model`` instance
    """

    data, model, vparam_names, kwargs = \
        _copy_data(data, model, vparam_names, kwargs)

    return sncosmo.mcmc_lc(data, model, vparam_names, **kwargs)
Exemple #7
0
		for i in range(len(band)):
			band[i]='ptf48r'
		hml=np.column_stack((hml,band))
		hml_dat=astropy.table.Table(data=hml, names=('ptfname', 'time', 'magnitude', 'mag_err', 'flux', 'flux_err', 'zp_new', 'zp', 'ra', 'dec', 'zpsys', 'filter'), dtype=('str','float','float','float','float','float','float','float','float','float','str','str'))
		print 'Doing:', hml[:,0][0]

		cur.execute("SELECT redshift, obsdate, phase from specinfo where ptfname=%s;",(str(hml[:,0][0]),))
		zed=cur.fetchone()

		if len(zed)==0:
			print 'Bad Query'
			break
		#print zed[0]
		
		model.set(z=zed[0])
		res, fitted_model=sncosmo.mcmc_lc(hml_dat, model, ['t0','x0','x1','c'], bounds={'x1':(-3.5,3.5), 'c':(-0.35,0.45)}, nburn=100, nsamples=5000)
		#res, fitted_model=sncosmo.fit_lc(hml_dat, model, ['t0','x0','x1','c'], bounds={'x1':(-3.5,3.5), 'c':(-0.35,0.45)}, verbose=True)
		#res, fitted_model=sncosmo.nest_lc(hml_dat, model, ['t0','x0','x1','c'], bounds={'x1':(-3.5,3.5), 'c':(-0.35,0.45)},)
		pdate=res.parameters[1]
		pass_4cut=Check_Dates(hml[:,1].astype(float), pdate)
		print hml[:,0][0], pass_4cut
		

		
		fig=sncosmo.plot_lc(hml_dat, model=fitted_model, errors=res.errors, color=np.random.choice(flat_cols), figtext=str(hml[:,0][0])+'\n'+str(pass_4cut), xfigsize=10, pulls=False)
		plt.axvline(-20., color='black', linestyle='--')
		plt.axvline(+50., color='black', linestyle='--')
		plt.savefig('LC_Fixed/'+str(hml[:,0][0])+'.png', dpi=150, bbox_inches='tight')
		plt.close()
		print '### Parameters ###'
		print str(hml[:,0][0]), float(zed[0]), float(0), float(res.parameters[1]), float(res.errors['t0']),float(res.parameters[2]), float(res.errors['x0']),  float(res.parameters[3]), float(res.errors['x1']), float(res.parameters[4]), float(res.errors['c']), float(hml[:,8][0]), float(hml[:,9][0])
Exemple #8
0
                  model.source.bumps}
        bounds['s'] = (0, 3.)

        # Get an idea of where the mode of the posterior is by doing
        # an MLE fit.
        res, model = sncosmo.fit_lc(lc, model, vparams, bounds=bounds)

        # Add bounds for MCMC fit.
        bounds['t0'] = (model.get('t0') - 2, model.get('t0') + 2)
        bounds['amplitude'] = (0.5 * model.get('amplitude'), 
                               2 * model.get('amplitude'))

        # Do MCMC.
        fres, fitmod = sncosmo.mcmc_lc(lc, model, vparams,
                                       bounds=bounds,
                                       nwalkers=200,
                                       nburn=1000,
                                       nsamples=20,
                                       guess_t0=False, guess_amplitude=False)
        samples = fres.samples

        # Represent results as a list of dictionaries mapping
        # parameter names to parameter values. Ultimately, we want all
        # of the model parameters, including the ones that do not
        # vary, as a big numpy array. In order to do that we will
        # access the `parameters` attribute of the
        
        pdicts = [dict(zip(fres.vparam_names, sample)) for sample in samples]

        # Aggregate all of the parameters, including the ones that do
        # not vary.
        
Exemple #9
0
    def mcmc_salt_fit(self, nickname):
        # do 2 SALT fits to iterate on the data to use for the fit
        bounds = {'t0': self.tmax_bounds}
        try:
            res, fitted_model = sncosmo.mcmc_lc(self.data,
                                                self.SaltModel,
                                                ['t0', 'x0', 'x1', 'c'],
                                                minsnr=3)
        except:
            # embed()
            print 'mcmc_salt_fit 1st iteration failed'
            self.SaltModel.set(t0=self.tmax_guess, x0=self.x0_start)
            res, fitted_model = sncosmo.mcmc_lc(self.data,
                                                self.SaltModel,
                                                ['t0', 'x0', 'x1', 'c'],
                                                bounds=bounds,
                                                guess_amplitude=False,
                                                guess_t0=False,
                                                modelcov=True)
        tmax = fitted_model.get('t0')
        phase = (self.data['time'] - tmax) / (1 + self.z)
        phase_mask = np.array(((phase > -15) & (phase < 45)))
        self.data = sncosmo.select_data(self.data, phase_mask)

        # fit 2
        try:
            res, fitted_model = sncosmo.mcmc_lc(self.data,
                                                self.SaltModel,
                                                ['t0', 'x0', 'x1', 'c'],
                                                minsnr=3)
        except:
            print 'mcmc_salt_fit 2nd iteration failed'
            raise  #nothing succeeded!
            self.SaltModel.set(t0=self.tmax_guess, x0=self.x0_start)
            res, fitted_model = sncosmo.mcmc_lc(self.data,
                                                self.SaltModel,
                                                ['t0', 'x0', 'x1', 'c'],
                                                bounds=bounds,
                                                guess_amplitude=False,
                                                guess_t0=False,
                                                modelcov=True)
        tmax = fitted_model.get('t0')
        phase = (self.data['time'] - tmax) / (1 + self.z)
        phase_mask = np.array(((phase > -15) & (phase < 45)))
        self.data = sncosmo.select_data(self.data, phase_mask)

        # pull out the cov
        self.whocares, self.SaltCov = fitted_model.bandfluxcov(
            self.data['band'], self.data['time'], self.data['zp'],
            self.data['zpsys'])
        # plot
        sncosmo.plot_lc(self.data,
                        model=fitted_model,
                        fname='%s/plots/emcee/cadencesim/salt/%s.pdf' %
                        (scratch, nickname),
                        color='black')

        # pull out the cov and some other parameters
        self.invcov = np.linalg.inv(self.SaltCov + self.data['fluxcov'])
        self.x0_salt = fitted_model.get('x0')
        self.tmax_salt = fitted_model.get('t0')
        self.c_salt = fitted_model.get('c')
        chisq = sncosmo.chisq(self.data, fitted_model)
        res.chisq = chisq
        res.ndof = len(self.data) - 4

        return self.SaltCov, res
Exemple #10
0
        # Get an idea of where the mode of the posterior is by doing
        # an MLE fit.
        res, model = sncosmo.fit_lc(lc, model, vparams, bounds=bounds, 
                                    mag=True)

        # Add bounds for MCMC fit.
        bounds['t0'] = (model.get('t0') - 2, model.get('t0') + 2)
        bounds['amplitude'] = (0.5 * model.get('amplitude'), 
                               2 * model.get('amplitude'))

        # Do MCMC.
        fres, fitmod = sncosmo.mcmc_lc(lc, model, vparams,
                                       bounds=bounds,
                                       nwalkers=config['nwalkers'],
                                       nburn=config['nburn'],
                                       nsamples=config['nsamples'],
                                       guess_t0=False, guess_amplitude=False,
                                       mag=True)
        samples = fres.samples

        # Represent results as a list of dictionaries mapping
        # parameter names to parameter values. Ultimately, we want all
        # of the model parameters, including the ones that do not
        # vary, as a big numpy array. In order to do that we will
        # access the `parameters` attribute of the
        
        pdicts = [dict(zip(fres.vparam_names, sample)) for sample in samples]

        # Aggregate all of the parameters, including the ones that do
        # not vary.
Exemple #11
0
        cur.execute(
            "SELECT redshift, obsdate, phase from specinfo where ptfname=%s;",
            (str(hml[:, 0][0]), ))
        zed = cur.fetchone()

        if len(zed) == 0:
            print 'Bad Query'
            break
        #print zed[0]

        model.set(z=zed[0])
        res, fitted_model = sncosmo.mcmc_lc(hml_dat,
                                            model, ['t0', 'x0', 'x1', 'c'],
                                            bounds={
                                                'x1': (-3.5, 3.5),
                                                'c': (-0.35, 0.45)
                                            },
                                            nburn=100,
                                            nsamples=5000)
        #res, fitted_model=sncosmo.fit_lc(hml_dat, model, ['t0','x0','x1','c'], bounds={'x1':(-3.5,3.5), 'c':(-0.35,0.45)}, verbose=True)
        #res, fitted_model=sncosmo.nest_lc(hml_dat, model, ['t0','x0','x1','c'], bounds={'x1':(-3.5,3.5), 'c':(-0.35,0.45)},)
        pdate = res.parameters[1]
        pass_4cut = Check_Dates(hml[:, 1].astype(float), pdate)
        print hml[:, 0][0], pass_4cut

        fig = sncosmo.plot_lc(hml_dat,
                              model=fitted_model,
                              errors=res.errors,
                              color=np.random.choice(flat_cols),
                              figtext=str(hml[:, 0][0]) + '\n' +
                              str(pass_4cut),
Exemple #12
0
def task(filename, i, j, nrv, nebv, kind='mcmc'):

    lc = sncosmo.read_lc(filename, format='csp')

    model = sncosmo.Model(bump.BumpSource(),
                          effect_names=['host', 'mw'],
                          effect_frames=['rest', 'obs'],
                          effects=[sncosmo.OD94Dust(),
                                   sncosmo.F99Dust()])

    rv_prior = burns.get_hostrv_prior(lc.meta['name'], 'gmm', sncosmo.OD94Dust)

    host_ebv, err = burns.get_hostebv(lc.meta['name'])
    ebv_prior = TruncNorm(-np.inf, np.inf, host_ebv, err)

    rv_prior, low, high = burns.get_hostrv_prior(lc.meta['name'],
                                                 'gmm',
                                                 sncosmo.OD94Dust,
                                                 retlims=True)
    host_ebv, err = burns.get_hostebv(lc.meta['name'])

    rv = np.linspace(low if low >= 0 else 0, high, nrv)[i]
    ebvlo = host_ebv - err
    ebvhi = host_ebv + err
    ebv = np.linspace(ebvlo if ebvlo >= 0 else 0, ebvhi, nebv)[j]

    model.set(z=lc.meta['zcmb'])
    model.set(mwebv=burns.get_mwebv(lc.meta['name'])[0])
    model.set(hostebv=ebv)
    model.set(hostr_v=rv)
    model.set(t0=burns.get_t0(lc.meta['name']))

    vparams = filter(lambda x: 'bump' in x, model._param_names)
    vparams += ['t0', 's']
    bounds = {b.name + "_bump_amp": (-1, 2) for b in model.source.bumps}
    #bounds['hostr_v'] = (rv_prior.mean - 0.5, rv_prior.mean + 0.5)
    #bounds['hostebv'] = (0, 0.2)
    bounds['s'] = (0, 3.)

    res, model = sncosmo.fit_lc(lc,
                                model, ['amplitude'] + vparams,
                                bounds=bounds)

    bounds['t0'] = (model.get('t0') - 2, model.get('t0') + 2)

    vparams.append('amplitude')
    bounds['amplitude'] = (0.5 * model.get('amplitude'),
                           2 * model.get('amplitude'))

    qualifier = '_ebv_%.2f_rv_%.2f' % (ebv, rv)

    if kind != 'fit':
        if kind == 'mcmc':
            result = sncosmo.mcmc_lc(lc,
                                     model,
                                     vparams,
                                     bounds=bounds,
                                     nwalkers=500,
                                     nburn=1000,
                                     nsamples=20)
        elif kind == 'nest':
            result = sncosmo.nest_lc(lc,
                                     model,
                                     vparams,
                                     bounds=bounds,
                                     method='multi',
                                     npoints=800)

        samples = result[0].samples.reshape(500, 20, -1)
        vparams = result[0].vparam_names
        plot_arg = np.rollaxis(samples, 2)

        plotting.plot_chains(plot_arg,
                             param_names=vparams,
                             filename='fits/%s_samples%s.pdf' %
                             (lc.meta['name'], qualifier))

        dicts = [
            dict(zip(vparams, samp)) for samp in samples.reshape(500 * 20, -1)
        ]
        thinned = samples.reshape(500, 20, -1)[:, [0, -1]].reshape(1000, -1)

        pickle.dump(
            samples,
            open('fits/%s_samples%s.pkl' % (lc.meta['name'], qualifier), 'wb'))

        models = [copy(result[1]) for i in range(len(thinned))]
        for d, m in zip(dicts, models):
            m.set(**d)

        fig = sncosmo.plot_lc(data=lc,
                              model=models,
                              ci=(50 - 68 / 2., 50., 50 + 68 / 2.),
                              model_label=lc.meta['name'])
        fig.savefig('fits/%s%s.pdf' % (lc.meta['name'], qualifier))

    else:

        fitres, model = sncosmo.fit_lc(lc, model, vparams, bounds=bounds)
        fig = sncosmo.plot_lc(data=lc, model=model)
        fig.savefig('fits/%s_fit%s.pdf' % (lc.meta['name'], qualifier))
Exemple #13
0
    def runMCMC(self):
        MCMCout = sncosmo.mcmc_lc(self.data, self.model, vparam_names=self.vparams,
                                    bounds=self.bounds, minsnr=3.0)

        return MCMCout
Exemple #14
0
def task(filename, i, j, nrv, nebv, kind='mcmc'):
    
    lc = sncosmo.read_lc(filename, format='csp')

    model = sncosmo.Model(bump.BumpSource(),
                          effect_names=['host','mw'],
                          effect_frames=['rest','obs'],
                          effects=[sncosmo.OD94Dust(), sncosmo.F99Dust()])

    rv_prior = burns.get_hostrv_prior(lc.meta['name'], 
                                      'gmm', sncosmo.OD94Dust)

    host_ebv, err = burns.get_hostebv(lc.meta['name'])
    ebv_prior = TruncNorm(-np.inf, np.inf, host_ebv, err)

    rv_prior, low, high = burns.get_hostrv_prior(lc.meta['name'], 
                                                 'gmm', sncosmo.OD94Dust,
                                                 retlims=True)
    host_ebv, err = burns.get_hostebv(lc.meta['name'])
    
    rv = np.linspace(low if low >= 0 else 0, high, nrv)[i]
    ebvlo = host_ebv - err
    ebvhi = host_ebv + err
    ebv = np.linspace(ebvlo if ebvlo >= 0 else 0, ebvhi, nebv)[j]

    model.set(z=lc.meta['zcmb'])
    model.set(mwebv=burns.get_mwebv(lc.meta['name'])[0])
    model.set(hostebv=ebv)
    model.set(hostr_v=rv)
    model.set(t0=burns.get_t0(lc.meta['name']))

    vparams = filter(lambda x: 'bump' in x, model._param_names)
    vparams += ['t0', 's']
    bounds = {b.name + "_bump_amp":(-1,2) for b in 
                                     model.source.bumps}
    #bounds['hostr_v'] = (rv_prior.mean - 0.5, rv_prior.mean + 0.5)
    #bounds['hostebv'] = (0, 0.2)
    bounds['s'] = (0, 3.)

    res, model = sncosmo.fit_lc(lc,model,['amplitude']+vparams,
                                bounds=bounds)

    bounds['t0'] = (model.get('t0')-2, model.get('t0')+2)
    
    vparams.append('amplitude')
    bounds['amplitude'] = (0.5 * model.get('amplitude'),
                           2 * model.get('amplitude'))

    qualifier = '_ebv_%.2f_rv_%.2f' % (ebv, rv)

    if kind != 'fit':
        if kind == 'mcmc':
            result = sncosmo.mcmc_lc(lc, model, vparams,
                                     bounds=bounds,
                                     nwalkers=500,
                                     nburn=1000,
                                     nsamples=20)
        elif kind == 'nest':
            result = sncosmo.nest_lc(lc, model, vparams, bounds=bounds,
                                     method='multi', npoints=800)

        samples = result[0].samples.reshape(500, 20, -1)
        vparams = result[0].vparam_names
        plot_arg = np.rollaxis(samples, 2)

        plotting.plot_chains(plot_arg, param_names=vparams, 
                             filename='fits/%s_samples%s.pdf' % (lc.meta['name'], qualifier))

        dicts = [dict(zip(vparams, samp)) for samp in samples.reshape(500 * 20, -1)]
        thinned = samples.reshape(500, 20, -1)[:, [0, -1]].reshape(1000, -1)

        pickle.dump(samples, open('fits/%s_samples%s.pkl' % (lc.meta['name'], qualifier), 'wb'))

        models = [copy(result[1]) for i in range(len(thinned))]
        for d, m in zip(dicts, models):
            m.set(**d)

        fig = sncosmo.plot_lc(data=lc, model=models, ci=(50-68/2., 50., 50+68/2.),
                              model_label=lc.meta['name'])
        fig.savefig('fits/%s%s.pdf' % (lc.meta['name'], qualifier))
        
    else:
        
        fitres, model = sncosmo.fit_lc(lc, model, vparams, bounds=bounds)
        fig = sncosmo.plot_lc(data=lc, model=model)
        fig.savefig('fits/%s_fit%s.pdf' % (lc.meta['name'], qualifier))
Exemple #15
0
    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$"])

    if False:
        if not os.path.exists(mcmc_chain):
            res2, fitted_model2 = sncosmo.mcmc_lc(lcs[0], model, ['t0', 'x0', 'x1', 'c'], nwalkers=20,
                                                  nburn=500, nsamples=4000)
            mcchain = res2.samples
            np.save(mcmc_chain, mcchain)
        else:
            mcchain = np.load(mcmc_chain)
        c.add_chain(mcchain, name="sncosmo mcmc", parameters=["$t_0$", "$x_0$", "$x_1$", "$c$"])
    print("Plot surfaces")
    c.configure_contour(shade=True, shade_alpha=0.2, sigmas=[0.0, 1.0, 2.0, 3.0])
    c.configure_bar(shade=True)
    c.plot(filename=surface, figsize=(7, 7))
    if False:
        fig = sncosmo.plot_lc(lcs[0], model=fitted_model, errors=res.errors)
        fig.savefig(temp_dir + os.sep + "lc_simple.png", bbox_inches="tight", dpi=300)

    alpha = 0.14
    beta = 3.15