Exemple #1
0
def calc_model_chisq(data, result, model):
    """Calculate the chi-squared for a given data table and model

    Chi-squared is calculated using parameter values from ``model``. Degrees
    of freedom are calculated using the number of varied parameters specified
    is the ``result`` object.

    Args:
        data    (Table): An sncosmo input table
        model   (Model): An sncosmo Model
        result (Result): sncosmo fitting result

    Returns:
        The un-normalized chi-squared
        The number of data points used in the calculation
    """

    data = deepcopy(data)

    # Drop any data that is not withing the model's range
    min_band_wave = np.array(
        [sncosmo.get_bandpass(b).minwave() for b in data['band']])
    max_band_wave = np.array(
        [sncosmo.get_bandpass(b).maxwave() for b in data['band']])
    data = data[(data['time'] >= model.mintime())
                & (data['time'] <= model.maxtime()) &
                (min_band_wave >= model.minwave()) &
                (max_band_wave <= model.maxwave())]

    if len(data) == 0:
        raise ValueError('No data within model range')

    return sncosmo.chisq(data, model), len(data) - len(result.vparam_names)
Exemple #2
0
    def get_log_likelihood(self, data):
        r""" Uses SNCosmo to move from supernova parameters to a light curve.
        """
        arr = []
        for z, t0, x1, c, lc, mag, mu in zip(data["redshift"], data["t0"],
                                             data["x1"], data["c"],
                                             data["olc"], data["abs_mag"], data["mu"]):
            self.model.set(z=z)
            self.model.source.set_peakmag(mag + mu, "bessellb", "ab")
            self.model.parameters = [z, t0, self.model.get("x0"), x1, c]
            chi2 = sncosmo.chisq(lc, self.model)
            arr.append(-0.5 * chi2)

        return np.array(arr)
Exemple #3
0
    def get_log_likelihood(self, data):
        r""" Uses SNCosmo to move from supernova parameters to a light curve.
        """
        H0 = data["H0"]
        om = data["omega_m"]
        if self.cosmology is None or (self.current_h0 != H0 or self.current_omega_m != om):
            self.cosmology = FlatwCDM(H0, om)
            self.current_h0 = H0
            self.current_omega_m = om
        self.model.set(z=data["redshift"])
        self.model.set_source_peakabsmag(data["luminosity"], 'bessellb', 'ab', cosmo=self.cosmology)
        x0 = self.model.get("x0")
        self.model.parameters = [data["redshift"], data["t0"], x0, data["x1"], data["c"]]

        chi2 = sncosmo.chisq(data["olc"], self.model)
        return -0.5 * chi2
Exemple #4
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])
		print 'chi2', sncosmo.chisq(hml_dat, fitted_model)
		print 'ndof', len(hml_dat)-4. #len(data)-len(vparam_names)
		print 'red_chi2', sncosmo.chisq(hml_dat, fitted_model)/(len(hml_dat)-4.)
		print 'absolute magnitue', fitted_model.source_peakabsmag('bessellb','ab')
		# print 'chi2', res.chisq
		# print 'res.ndof', res.ndof
		# print 'red_chisq', res.chisq/res.ndof
		cur.execute("INSERT INTO sncosmo_fits (ptfname, redshift, redshift_err, t0, t0_err, x0, x0_err, x1, x1_err, c, c_err, ra, dec, pass_cut, redchi2, abs_mag) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);",(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]), pass_4cut,float(sncosmo.chisq(hml_dat, fitted_model)/(len(hml_dat)-4.)), float(fitted_model.source_peakabsmag('bessellb','ab')),))
		conn.commit()
		print 'Done:', hml[:,0][0]
		
	except ValueError:
	 	print 'Value Error'
	
		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, pdate)
		#print l[i], pass_4cut
		

		
		fig=sncosmo.plot_lc(hml_dat, model=fitted_model, errors=res.errors, color=np.random.choice(flat_cols), figtext=str(l[i])+'\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(l[i])+'.png', dpi=150, bbox_inches='tight')
		plt.close()
		print '### Parameters ###'
		print str(l[i]), 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'])
		print 'chi2', sncosmo.chisq(hml_dat, fitted_model)
		print 'ndof', len(hml_dat)-4. #len(data)-len(vparam_names)
		print 'red_chi2', sncosmo.chisq(hml_dat, fitted_model)/(len(hml_dat)-4.)
		print 'absolute magnitue', fitted_model.source_peakabsmag('bessellb','ab')
		# print 'chi2', res.chisq
		# print 'res.ndof', res.ndof
		# print 'red_chisq', res.chisq/res.ndof
		#cur.execute("INSERT INTO sncosmo_fits (ptfname, redshift, redshift_err, t0, t0_err, x0, x0_err, x1, x1_err, c, c_err, ra, dec, pass_cut, redchi2, abs_mag) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);",(str(l[i]), 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]), pass_4cut,float(sncosmo.chisq(hml_dat, fitted_model)/(len(hml_dat)-4.)), float(fitted_model.source_peakabsmag('bessellb','ab')),))
		#conn.commit()
		print 'Done:', l[i]
		
	except ValueError:
	 	print 'Value Error'
	
Exemple #6
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
    results = pd.DataFrame(columns=columns)
    for i in range(imin, imax):
        sourceId = sourceIds[i]
        print i, sourceId
        sys.stdout.flush()

        # Get the catsim light curve and set the salt2 model with the
        # input truth parameters.
        ref_lc = SN_reflc_server.get_SNLightCurve_object(sourceId)
        model = Salt2Model(ref_lc.params)
        # Compute the chisq stats.
        mask = ((ref_lc.lightcurve['bandpass']!='lsstu')
                & (ref_lc.lightcurve['fluxerr']==ref_lc.lightcurve['fluxerr']))
        data = ref_lc.lightcurve[mask]
        ndof = len(data) - 5
        chisq_catsim = sncosmo.chisq(data, model)
        ndof_catsim = ndof

        # Find the nearest object from the Object table within +/-2
        # arcsec of the catsim source coordinates.
        try:
            obj = get_coadd_object(l2_service, model.ra, model.dec, box_size=2)
        except ValueError:
            # No objects found so skip this one.
            continue
        objectId = obj.objectId.iloc[0]
        # Get the ForcedSource light curves and select the data based
        # on nominal time range of the model (and exclude the u band
        # data).
        lc = lc_factory.create(objectId)
        data_mask = ((lc.data['bandpass'] != 'lsstu')
Exemple #8
0
                       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])
 print 'chi2', sncosmo.chisq(hml_dat, fitted_model)
 print 'ndof', len(hml_dat) - 4.  #len(data)-len(vparam_names)
 print 'red_chi2', sncosmo.chisq(hml_dat,
                                 fitted_model) / (len(hml_dat) - 4.)
 print 'absolute magnitue', fitted_model.source_peakabsmag(
     'bessellb', 'ab')
 # print 'chi2', res.chisq
 # print 'res.ndof', res.ndof
 # print 'red_chisq', res.chisq/res.ndof
 cur.execute(
     "INSERT INTO sncosmo_fits (ptfname, redshift, redshift_err, t0, t0_err, x0, x0_err, x1, x1_err, c, c_err, ra, dec, pass_cut, redchi2, abs_mag) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);",
     (
         str(hml[:, 0][0]),
         float(zed[0]),
         float(0),
         float(res.parameters[1]),
		m=np.array(m)
		
		hml_dat=astropy.table.Table(data=m, names=('time', 'band', 'flux', 'fluxerr', 'zp', 'zpsys'), dtype=('float','str','float','float','float','str'))
		#print 'Doing:', lcs[i]

		
		
		res, fitted_model=sncosmo.mcmc_lc(hml_dat, model, ['z','t0','x0','x1','c'], bounds={'z':(0,0.15),'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)},)
				
		#fig=sncosmo.plot_lc(hml_dat, model=fitted_model, errors=res.errors, color=np.random.choice(flat_cols), xfigsize=10)
		
		#print res.parameters
		#plt.savefig('Fitted_LCs/'+str(lcs[i])+'.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])
		# print 'chi2', sncosmo.chisq(hml_dat, fitted_model)
		# print 'ndof', len(hml_dat)-4. #len(data)-len(vparam_names)
		redchi2=sncosmo.chisq(hml_dat, fitted_model)/(len(hml_dat)-4.)
		#print 'absolute magnitue', fitted_model.source_peakabsmag('bessellb','ab')
		# print 'chi2', res.chisq
		# print 'res.ndof', res.ndof
		#print 'red_chisq', res.chisq/res.ndof
		cur.execute("INSERT INTO inout_fit (snid, redshift, redshift_err, t0, t0_err, x0, x0_err, x1, x1_err, c, c_err, redchi2, abs_mag) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);",(int(float(lcs[i])),float(res.parameters[0]),float(res.errors['z']) ,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(redchi2), float(fitted_model.source_peakabsmag('bessellb','ab')),))
		conn.commit()
		#print 'Done:', hml[:,0][0]
		
	except ValueError:
		print 'Value Error', lcs[i]
Exemple #10
0
                                        vparam_names_used,
                                        bounds=bounds,
                                        **kwargs)

        outarr[vparam_names][ind] = tuple(res.parameters)
        outarr['ztrue'][ind] = z_true
        outarr['zfix'][ind] = zfix
        outarr['t_exp_true'][ind] = t0_true
        outarr['model'][ind] = model_name
        outarr['ID'][ind] = ID
        outarr['nobs'][ind] = len(lcs)

        for j in vparam_names_used:
            outarr[j + '_e'][ind] = res.errors[j]

        tmp_chisq = sncosmo.chisq(lcs, fitted_model) / res.ndof
        outarr['red_chi2'][ind] = tmp_chisq

        t_exp_fitted = get_explosion_time(model_name) * \
                       (1 +
                        res.parameters[np.array(model.param_names) == 'z']) \
                       + res.parameters[np.array(model.param_names) == 't0']
        outarr['t_exp_fit'][ind] = t_exp_fitted
        outarr['t_exp_dif'][ind] = t0_true - t_exp_fitted

    except (ValueError, RuntimeError, KeyError, RuntimeWarning,
            sncosmo.fitting.DataQualityError) as err:
        print(err, ' for model ', model_name, ' and lightcurve ', str(ID),
              ' zfix is ', str(zfix))
        outarr['error'][ind] = True
Exemple #11
0
 def get_log_likelihood(self, data):
     self.model.set(x0=data["x0"], x1=data["x1"], t0=data["t0"], c=data["c"])
     chi2 = sncosmo.chisq(data["lc"][0], self.model)
     if np.isnan(chi2):
         return [-np.inf]
     return [-0.5 * chi2]
Exemple #12
0
 def get_log_likelihood(self, data):
     self.model.set(x0=data["x0"], x1=data["x1"], t0=data["t0"], c=data["c"], z=data["z"][0])
     chi2 = sncosmo.chisq(data["lc"][0], self.model)
     return [-0.5 * chi2]