def fit_curve(hml_dat, source, hml): # Retrieving model from source, (requires internet access) model = sncosmo.Model(source=source) # Fitting model: model parameter z bound. res, fitted_model = sncosmo.fit_lc(hml_dat, model, ['z', 't0', 'amplitude'], bounds={'z': (0.005, 0.35)}) # The following excludes data sets with fewer than 4 data points and not enough distribution.. # print 'peak', float(res.parameters[1]) j = 0 k = 0 for i in hml[:, 1]: if float(i) > float(res.parameters[1]): j += 1 else: k += 1 if j >= 2 and k >= 2: print len(hml[:, 1]) if len(hml[:, 1]) > 3: # print res.errors # print model sncosmo.plot_lc(hml_dat, model=fitted_model, errors=res.errors, color='blue', figtext=str( hml[:, 0][0] + ' Type' + types[hml[:, 0][0]] + '\n' + 'Model name: ' + source.name), xfigsize=10) plt.show() print 'Done:', hml[:, 0][0], 'z:', float( res.parameters[0]), 'Reduced chi^2:', res.chisq / res.ndof, 'Data points:', len( hml[:, 1]), '\n' # 'Dof:', res.ndof else: pass
def showcurve(sn, source_name): try: hml=np.load('/home/fcm1g13/Documents/Supernova/CorecollapseLC/ccdata/' + sn) source=sncosmo.get_source(source_name) model=sncosmo.Model(source=source) # print len(hml[:,1]), 'data points' # Adding zpsys and filter columns. zp system used is ab and filter used is ptf48r ab = np.zeros(len(hml[:, 1]), dtype='|S2') band = np.zeros(len(hml[:, 1]), dtype='|S6') for i in range(len(ab)): ab[i] = 'ab' band[i] = 'ptf48r' hml = np.column_stack((hml, ab, 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')) ###fitting model res, fitted_model=sncosmo.fit_lc(hml_dat, model, ['z','t0','amplitude'], bounds={'z':(0.005,0.35)}, nburn=10000, nsamples=50000) #print 'peak', float(res.parameters[1]) print res sncosmo.plot_lc(hml_dat, model=fitted_model, errors=res.errors, color='blue', figtext=str(hml[:,0][0]+' Type'+types[hml[:,0][0]]+'\n'+'Model name: '+ source.name), xfigsize=10) #plt.clfig() plt.show() #print #'### Parameters ###' #print 'SN',str(hml[:,0][0]), 'z:',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(hml[:,8][0]), float(hml[:,9][0]) print 'Done:', hml[:,0][0], 'z:',float(res.parameters[0]), 'Reduced chi^2:', res.chisq/res.ndof,#'Data points:', len(hml[:,1]),' Type'+types[hml[:,0][0]]+'Model name: '+ source.name,'\n' #'Dof:', res.ndof except ValueError: sn, source_name, 'cannot be plotted'
def Plot_LC(lc,sn): dust = sncosmo.OD94Dust() fitted_model=sncosmo.Model(source='salt2-extended', effects=[dust, dust], effect_names=['host', 'mw'], effect_frames=['rest', 'obs']) fitted_model.set(z=sn['z']) fitted_model.set(t0=sn['salt2.T0']) fitted_model.set(x0=sn['salt2.X0']) fitted_model.set(x1=sn['salt2.X1']) fitted_model.set(c=sn['salt2.Color']) errors={} errors['t0']=np.sqrt(sn['salt2.CovT0T0']) errors['x0']=np.sqrt(sn['salt2.CovX0X0']) errors['x1']=np.sqrt(sn['salt2.CovX1X1']) errors['c']=np.sqrt(sn['salt2.CovColorColor']) print 'Phases : first',(lc['time'][0]-sn['salt2.T0'])/(1.+sn['z']),'last',(lc['time'][-1]-sn['salt2.T0'])/(1.+sn['z']) """ res, fitted_modelb = sncosmo.fit_lc(lc, fitted_model,['t0', 'x0', 'x1', 'c'],bounds={'z':(sn['z']-0.001, sn['z']+0.001)}) print 'ooo',res.errors print 'bbb',errors """ sncosmo.plot_lc(lc, model=fitted_model,pulls=True)
def test_classify(templateset='PSNID', verbose=3): """ run a test classification :return: """ os.chdir(os.path.expanduser("~/sandbox/wfirst")) sn = get_test_sn(verbose=verbose) sn = standardize_sn_data(sn) sn = strip_post_detection_epochs(sn, 4) sncosmo.plot_lc(sn) pl.draw() z = sn.meta['SIM_REDSHIFT_CMB'] tpk = sn.meta['PEAKMJD'] excludetemplates = get_sncosmo_excludelist(sn) start = time.time() snclassdict = sncosmo.classify.classify(sn, zhost=z, zhosterr=0.0001, zminmax=[z - 0.01, z + 0.01], t0_range=[tpk - 10, tpk + 10], templateset=templateset, excludetemplates=excludetemplates, nobj=20, maxiter=1000, nsteps_pdf=51, verbose=verbose) end = time.time() print "P(Ia)=%.2f ; Type = %s ; best model = %s ; %.3f seconds" % ( snclassdict['pIa'], sn.meta['SIM_TYPE_NAME'].strip(), snclassdict['bestmodel'], (end - start)) return sn, snclassdict
def sncosmoFit(datacat_name='sncosmo_SN2018kp.txt'): """ z = 0.007166 ; NED """ import sncosmo data = ascii.read(datacat_name) magsys = sncosmo.CompositeMagSystem( bands={ 'standard::b': ('ab', 9.851778333549941), 'standard::v': ('ab', 10.165691850734973), 'standard::r': ('ab', 9.780891653643735), 'standard::i': ('ab', 10.00617773098994) }) dust1 = sncosmo.F99Dust(r_v=3.1) dust = sncosmo.F99Dust(r_v=1.766) model = sncosmo.Model(source='salt2', effects=[dust, dust1], effect_names=['host', 'mw'], effect_frames=['rest', 'obs']) model.set(z=0.010142) #model.set_source_peakabsmag(-19,'standard::b', 'ab') model.param_names model.parameters print('Set the model like this;', model) #source = sncosmo.SALT2Source(modeldir='/home/lim9/.astropy/cache/sncosmo/models/salt2/salt2-4') ### Applying cuts minsnr = 3 tl = data['mjd'][0] tu = data['mjd'][0] + 50. #mask = (data['mjd'] >= tl) & (data['mjd'] < tu) #mask = (data['mjd'] >= tl) & (data['mjd'] < tu) & (data['flux'] / data['fluxerr'] > minsnr) #data = data[mask] params_to_fit = ['t0', 'x0', 'x1', 'c', 'hostebv', 'mwebv'] bounds = {'hostebv': (0, 1.0), 't0': (58158, 58160), 'mwebv': (0, 1.0)} #bounds = { 't0':(58618, 58620), 'x0':(0.01,0.05), 'x1':(-2,-0.5), 'c':(0.0, 0.15), 'hostebv':(0, 0.1)} result, fitted_model = sncosmo.fit_lc(data, model, params_to_fit, guess_amplitude=False, guess_t0=True, guess_z=False, bounds=bounds, method='minuit', modelcov=True, phase_range=(-20, 75), verbose=True) print("Number of chi^2 function calls :", result.ncall) print("Number of degrees of freedom in fit :", result.ndof) print("Chi^2 value at minimum :", result.chisq) print("Reduced Chi^2 :", result.chisq / result.ndof) print("Model parameters :", result.param_names) print("Best-fit values :", result.parameters) print("The result contains the following attributes:\n", result.keys()) sncosmo.plot_lc(data, model=fitted_model, errors=result.errors, pulls=True, figtext='SN 2018kp; SALT2', show_model_params=True)
def plot_testdata(nsne, under_model): if not os.path.exists("lcplots_%s"%(under_model.name(nsne))): os.mkdir("lcplots_%s"%(under_model.name(nsne))) fnames = glob("testdata_%s/*"%(under_model.name(nsne))) for fname in fnames: plotname = fname.replace("testdata_%s"%(under_model.name(nsne)), "lcplots_%s"%(under_model.name(nsne))).replace("dat", "png") data = sncosmo.read_lc(fname) sncosmo.plot_lc(data, fname=plotname)
def simple(): # choose a built-in source model = sncosmo.Model(source='hsiao') # set model parameters model.set(z=0.2) # plot sncosmo.plot_lc(model=model, bands=['sdssr', 'sdssg']) plt.savefig("z02_gr.png") plt.show()
def plot_lightcurve(self, fixedx1c=False, **kwargs): """ Make a figure showing the observed light curve of the given low-z Type Ia SN, compared to the light curve from the SALT2ir model for the (predefined) x1, c values appropriate to that SN. """ if self.model_fitted is None: self.fitmodel() if fixedx1c: model = self.model_fixedx1c else: model = self.model_fitted sncosmo.plot_lc(self.lcdata, model=model, **kwargs)
def showcurve(sn, source_name): try: hml = np.load( '/home/fcm1g13/Documents/Supernova/CorecollapseLC/ccdata/' + sn) source = sncosmo.get_source(source_name) model = sncosmo.Model(source=source) # print len(hml[:,1]), 'data points' # Adding zpsys and filter columns. zp system used is ab and filter used is ptf48r ab = np.zeros(len(hml[:, 1]), dtype='|S2') band = np.zeros(len(hml[:, 1]), dtype='|S6') for i in range(len(ab)): ab[i] = 'ab' band[i] = 'ptf48r' hml = np.column_stack((hml, ab, 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')) ###fitting model res, fitted_model = sncosmo.fit_lc(hml_dat, model, ['z', 't0', 'amplitude'], bounds={'z': (0.005, 0.35)}, nburn=10000, nsamples=50000) #print 'peak', float(res.parameters[1]) print res sncosmo.plot_lc( hml_dat, model=fitted_model, errors=res.errors, color='blue', figtext=str(hml[:, 0][0] + ' Type' + types[hml[:, 0][0]] + '\n' + 'Model name: ' + source.name), xfigsize=10) #plt.clfig() plt.show() #print #'### Parameters ###' #print 'SN',str(hml[:,0][0]), 'z:',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(hml[:,8][0]), float(hml[:,9][0]) print 'Done:', hml[:, 0][0], 'z:', float( res.parameters[0] ), 'Reduced chi^2:', res.chisq / res.ndof, #'Data points:', len(hml[:,1]),' Type'+types[hml[:,0][0]]+'Model name: '+ source.name,'\n' #'Dof:', res.ndof except ValueError: sn, source_name, 'cannot be plotted'
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
def multiple_z(): # choose a built-in source models = [] npts = 3 zs = np.linspace(0, 0.5, npts) for ii in np.arange(npts): model = sncosmo.Model(source='hsiao') model.set(z=zs[ii]) models.append(model) # plot sncosmo.plot_lc(model=models, bands=['sdssr'] * npts, model_label=zs, color='k') plt.show()
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 visualize_lightcurve(self): """ Make a simple light curve plot. """ if self.lightcurve is None: raise ValueError('No lightcurve yet. Use build_lightcurve first.') fig = sncosmo.plot_lc(self.lightcurve) return fig
def showcurve(sn, source_name, hml): try: source=sncosmo.get_source(source_name) model=sncosmo.Model(source=source) #fit to model z0 = float(spec_z(sn[:-7])) 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')) res, fitted_model=sncosmo.fit_lc(hml_dat, model, ['z','t0','amplitude'], bounds={'z':(z0, z0+0.001)}, nburn=10000, nsamples=50000) #plot model sncosmo.plot_lc(hml_dat, model=fitted_model, errors=res.errors, color='blue', figtext=str(hml[:,0][0]+' Type'+types[hml[:,0][0]]+'\n'+'Model name: '+ source.name + '\n'+'Reduced Chi Squared: ')+ str(res.chisq/res.ndof), xfigsize=10) plt.show() #print 'Parameters:''z:',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(hml[:,8][0]), float(hml[:,9][0])' print 'Done:', hml[:,0][0], 'z:',float(res.parameters[0]), 'Reduced chi^2:', res.chisq/res.ndof,#'Data points:', len(hml[:,1]),' Type'+types[hml[:,0][0]]+'Model name: '+ source.name,'\n' #'Dof:', res.ndof except ValueError: print sn, source_name, 'cannot be plotted'
def fit_curve(hml_dat, source, hml): # Retrieving model from source, (requires internet access) model = sncosmo.Model(source=source) # Fitting model: model parameter z bound. res, fitted_model = sncosmo.fit_lc(hml_dat, model, ['z', 't0', 'amplitude'], bounds={'z': (0.005, 0.35)}) # The following excludes data sets with fewer than 4 data points and not enough distribution.. # print 'peak', float(res.parameters[1]) j = 0 k = 0 for i in hml[:, 1]: if float(i) > float(res.parameters[1]): j += 1 else: k += 1 if j >= 2 and k >= 2: print len(hml[:, 1]) if len(hml[:, 1]) > 3: # print res.errors # print model sncosmo.plot_lc( hml_dat, model=fitted_model, errors=res.errors, color='blue', figtext=str(hml[:, 0][0] + ' Type' + types[hml[:, 0][0]] + '\n' + 'Model name: ' + source.name), xfigsize=10) plt.show() print 'Done:', hml[:, 0][0], 'z:', float( res.parameters[0] ), 'Reduced chi^2:', res.chisq / res.ndof, 'Data points:', len( hml[:, 1]), '\n' # 'Dof:', res.ndof else: pass
def _plot_lc(data, result, fitted_model): """Plot fit results Args: data (Table): The data used in the fit result (Result): The fit results fitted_model (Model): Model with params set to fitted values """ fig = sncosmo.plot_lc(data, fitted_model, errors=result.errors) xs, d = utils.calc_model_chisq(data, result, fitted_model) print(f'chisq / ndof = {xs} / {d} = {xs / d}', flush=True) return fig
def showcurve(sn, source_name, hml): try: source = sncosmo.get_source(source_name) model = sncosmo.Model(source=source) #fit to model z0 = float(spec_z(sn[:-7])) 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')) res, fitted_model = sncosmo.fit_lc(hml_dat, model, ['z', 't0', 'amplitude'], bounds={'z': (z0, z0 + 0.001)}, nburn=10000, nsamples=50000) #plot model sncosmo.plot_lc( hml_dat, model=fitted_model, errors=res.errors, color='blue', figtext=str(hml[:, 0][0] + ' Type' + types[hml[:, 0][0]] + '\n' + 'Model name: ' + source.name + '\n' + 'Reduced Chi Squared: ') + str(res.chisq / res.ndof), xfigsize=10) plt.show() #print 'Parameters:''z:',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(hml[:,8][0]), float(hml[:,9][0])' print 'Done:', hml[:, 0][0], 'z:', float( res.parameters[0] ), 'Reduced chi^2:', res.chisq / res.ndof, #'Data points:', len(hml[:,1]),' Type'+types[hml[:,0][0]]+'Model name: '+ source.name,'\n' #'Dof:', res.ndof except ValueError: print sn, source_name, 'cannot be plotted'
def Plot_LC_sncosmo(table, telescope): print('What will be plotted', table) prefix = 'LSST::' print(table.dtype) for band in 'ugrizy': name_filter = prefix + band if telescope.airmass > 0: bandpass = sncosmo.Bandpass(telescope.atmosphere[band].wavelen, telescope.atmosphere[band].sb, name=name_filter, wave_unit=u.nm) else: bandpass = sncosmo.Bandpass(telescope.system[band].wavelen, telescope.system[band].sb, name=name_filter, wave_unit=u.nm) # print('registering',name_filter) sncosmo.registry.register(bandpass, force=True) source = sncosmo.get_source('salt2-extended', version='1.0') dust = sncosmo.OD94Dust() model = sncosmo.Model(source=source) print('metadata', table.meta) model.set(z=table.meta['z'], c=table.meta['Color'], t0=table.meta['DayMax'], x1=table.meta['X1']) z = table.meta['z'] tab = table[['flux', 'fluxerr', 'band', 'zp', 'zpsys', 'time']] res, fitted_model = sncosmo.fit_lc(tab, model, ['t0', 'x0', 'x1', 'c'], bounds={'z': (z - 0.001, z + 0.001)}) print(res) print('jjj', fitted_model) sncosmo.plot_lc(data=tab, model=fitted_model, errors=res['errors']) plt.draw() plt.pause(20.) plt.close()
def plot_photmag(models, lc): plt.rcParams["font.family"] = "monospace" fig = sncosmo.plot_lc(model=models, data=lc, fill_percentiles=(2.5,50.,97.5), zpsys='csp', mag=True) for axis in fig.axes: yhi = axis.get_ylim()[1] axis.set_ylim(23, yhi) axis.tick_params(labelsize=8) if axis.get_xlabel() != "": axis.set_xlabel('mjd') fig.set_size_inches(8., 6.) fig.suptitle(lc.meta['name']) fig.tight_layout() fig.subplots_adjust(top=0.92, right=0.95) return fig
def lcplot(self, lowrange=-30., highrange=50., nightlyCoadd=False, scattered=False, seed=0): # if nightlyCoadd: # lc = self.coaddedLightCurve # else: # lc = self.lightcurve # aggregations = {'time': np.mean, # 'flux': np.mean, # 'fluxerr': lambda x: np.sqrt(np.sum(x**2))/len(x), # 'zp': np.mean, # 'zpsys': 'first'} # groupedbynightlyfilters = lc.groupby(['night','band']) # lc = groupedbynightlyfilters.agg(aggregations) data = self.SNCosmoLC(nightlyCoadd=nightlyCoadd, scattered=scattered, seed=seed) return sncosmo.plot_lc(data, model=self.sncosmoModel, color='k', pulls=False)
def plot_photmag(models, lc): plt.rcParams["font.family"] = "monospace" fig = sncosmo.plot_lc(model=models, data=lc, fill_percentiles=(2.5, 50., 97.5), zpsys='csp', mag=True) for axis in fig.axes: yhi = axis.get_ylim()[1] axis.set_ylim(23, yhi) axis.tick_params(labelsize=8) if axis.get_xlabel() != "": axis.set_xlabel('mjd') fig.set_size_inches(8., 6.) fig.suptitle(lc.meta['name']) fig.tight_layout() fig.subplots_adjust(top=0.92, right=0.95) return fig
def plotLC(self, fits=True): data = self.data model = self.model fit_model = self.fitModel mcmc_model = self.mcmcModel nest_model = self.nestModel models = [model] model_names = ['model'] if fits: if fit_model: models.append(fit_model) model_names.append('MLE') if mcmc_model: models.append(mcmc_model) model_names.append('MCMC') if nest_model: models.append(nest_model) model_names.append('nest') fig = sncosmo.plot_lc(data, model=models, model_label=model_names) return fig
def compare_model_sncosmo(data=None, models=[], bands=None, **kwargs): if data is None and bands is None: raise ValueError("Must provide bands if no data is given") fig = sncosmo.plot_lc(data=data, model=models, bands=bands, **kwargs) return fig
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]) 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()
def test_plotmodel(self): fig = sncosmo.plot_lc(model=self.model, bands=['bessellb', 'bessellr']) assert isinstance(fig, Figure)
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))
def plotLC(self, table, time_display): """ Light curve plot using sncosmo methods Parameters --------------- table: astropy table table with LS informations (flux, ...) time_display: float duration of the window display """ import pylab as plt import sncosmo prefix = 'LSST::' """ _photdata_aliases = odict([ ('time', set(['time', 'date', 'jd', 'mjd', 'mjdobs', 'mjd_obs'])), ('band', set(['band', 'bandpass', 'filter', 'flt'])), ('flux', set(['flux', 'f'])), ('fluxerr', set( ['fluxerr', 'fe', 'fluxerror', 'flux_error', 'flux_err'])), ('zp', set(['zp', 'zpt', 'zeropoint', 'zero_point'])), ('zpsys', set(['zpsys', 'zpmagsys', 'magsys'])) ]) """ for band in 'grizy': name_filter = prefix + band if self.telescope.airmass > 0: bandpass = sncosmo.Bandpass( self.telescope.atmosphere[band].wavelen, self.telescope.atmosphere[band].sb, name=name_filter, wave_unit=u.nm) else: bandpass = sncosmo.Bandpass( self.telescope.system[band].wavelen, self.telescope.system[band].sb, name=name_filter, wave_unit=u.nm) # print('registering',name_filter) sncosmo.registry.register(bandpass, force=True) z = table.meta['z'] x1 = table.meta['x1'] color = table.meta['color'] daymax = table.meta['daymax'] model = sncosmo.Model('salt2') model.set( z=z, c=color, t0=daymax, # x0=self.X0, x1=x1) """ print('tests',isinstance(table, np.ndarray),isinstance(table,Table),isinstance(table,dict)) array_tab = np.asarray(table) print(array_tab.dtype) colnames = array_tab.dtype.names # Create mapping from lowercased column names to originals lower_to_orig = dict([(colname.lower(), colname) for colname in colnames]) # Set of lowercase column names lower_colnames = set(lower_to_orig.keys()) orig_colnames_to_use = [] for aliases in _photdata_aliases.values(): i = lower_colnames & aliases if len(i) != 1: raise ValueError('Data must include exactly one column from {0} ' '(case independent)'.format(', '.join(aliases))) orig_colnames_to_use.append(lower_to_orig[i.pop()]) new_data = table[orig_colnames_to_use].copy() print('bbbb',orig_colnames_to_use,_photdata_aliases.keys(),new_data.dtype.names) new_data.dtype.names = _photdata_aliases.keys() """ sncosmo.plot_lc(data=table, model=model) plt.draw() plt.pause(time_display) plt.close()
photParams=photParams, FWHMeff=FWHMeff) snr_m5_through, gamma_through = SignalToNoise.calcSNR_m5( mag_SNb, transmission.lsst_atmos_aerosol[band], m5_calc, photParams) snr_m5_opsim, gamma_opsim = SignalToNoise.calcSNR_m5( mag_SNb, transmission.lsst_atmos_aerosol[band], m5_opsim, photParams) err_flux_SNb = flux_SNb / snr_SNb table_for_fit_b['error_calc'].add_row( (time, flux_SNb, err_flux_SNb, 'LSST::' + band, 25, 'ab')) print 'resultat', table_for_fit['error_calc'] """ #now do the fit... z_sim=SN.z res, fitted_model = sncosmo.fit_lc(table_for_fit['error_calc'], SN.SN_model,['z', 't0', 'x0', 'x1', 'c'],bounds={'z':(z_sim-0.01, z_sim+0.01)}) mbfit=fitted_model._source.peakmag('bessellb','vega') sncosmo.plot_lc(table_for_fit['error_calc'], model=fitted_model,color='k',pulls=False) print res,fitted_model """ figa, axa = plt.subplots(ncols=2, nrows=3, figsize=(10, 9)) for j, band in enumerate(['u', 'g', 'r', 'i', 'z', 'y']):
err[k] = 0.5 * (e[1] - e[0]) # using the maximum likelihood sample just for plotting purposes maxlike = np.argmax(chains['lnprob']) model.set(t0=chains['t0'][maxlike], x1=chains['x1'][maxlike], s=chains['s'][maxlike], c=chains['c'][maxlike], x0=chains['x0'][maxlike], z=z, mwebv=float(mwebv)) # the errors passed in here are errors in the measured parameters. Best-fit taken from maximum likelihood sample sncosmo.plot_lc(emfit.data, model=model, errors=err, fname='./plots/emcee/jla/%s.pdf' % (nickname), color='black') if args.noskew: triangle_keys = ['mB', 'c', 't0', 'x1'] else: triangle_keys = ['mB', 'c', 's', 't0', 'x1'] helpers.save(chains, './chains/%s.chains' % (nickname)) # triangle plots emfit.plots(chains, nickname, triangle_keys, outdir='./plots/emcee/jla/triangle') except Exception as e:
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
def normal_salt_fit(self, nickname): bounds = {'t0': self.tmax_bounds} exception_bounds = { 't0': self.tmax_bounds, 'x1': [-4, 4], 'c': [-1, 1] } self.SaltModel.set(t0=self.tmax_guess, x0=self.x0_start) res, fitted_model = sncosmo.fit_lc(deepcopy(self.data), self.SaltModel, ['t0', 'x0', 'x1', 'c'], bounds=exception_bounds, guess_amplitude=False, guess_t0=False, modelcov=True) sncosmo.plot_lc(self.data, model=fitted_model, fname='%s/plots/emcee/cadencesim/salt/%s.pdf' % (scratch, nickname), color='black') x1 = fitted_model.get('x1') c = fitted_model.get('c') if (x1 < exception_bounds['x1'][0] ) | (x1 > exception_bounds['x1'][1]) | ( c < exception_bounds['c'][0]) | (c > exception_bounds['c'][1]): print 'in exception bounds if for first data phase cut iteration' self.SaltModel.set(t0=self.tmax_guess, x0=self.x0_start) res, fitted_model = sncosmo.fit_lc(deepcopy(self.data), self.SaltModel, ['t0', 'x0', 'x1', 'c'], bounds=exception_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) self.SaltModel.set(t0=self.tmax_guess, x0=self.x0_start) res, fitted_model = sncosmo.fit_lc(deepcopy(self.data), self.SaltModel, ['t0', 'x0', 'x1', 'c'], bounds=exception_bounds, guess_amplitude=False, guess_t0=False, modelcov=True) x1 = fitted_model.get('x1') c = fitted_model.get('c') if (x1 < exception_bounds['x1'][0] ) | (x1 > exception_bounds['x1'][1]) | ( c < exception_bounds['c'][0]) | (c > exception_bounds['c'][1]): raise tmax = fitted_model.get('t0') # tmax = tmax 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']) sncosmo.plot_lc(self.data, model=fitted_model, fname='%s/plots/emcee/cadencesim/salt/%s.pdf' % (scratch, nickname), color='black') self.invcov = np.linalg.inv(self.SaltCov + self.data['cov']) self.x0_salt = fitted_model.get('x0') self.tmax_salt = fitted_model.get('t0') self.c_salt = fitted_model.get('c') return self.SaltCov, res
# Make plot for min chisq/dof plotname = "{0}/{1}.png".format(plotdir, meta['snid']) if not os.path.exists(plotname): model = models[name]['model'] res = candres[name] model.set(**res.param_dict) idx = np.argmax(res.logl) parameters = res.samples[idx] model.set(**dict(zip(res.param_names, parameters))) figtext = ("SNID = {}\n" "model = {}\n" "type={}\n" "$\\chi^2$/dof = {:.1f}/{:d}" .format(meta['snid'], name, res.type, res.chisq, res.ndof)) sncosmo.plot_lc(data, model, figtext=figtext, fname=plotname) # Plot chisq distribution minchisq = np.array(minchisq) minchisqtype = np.array(minchisqtype) minchisqsets = [] minchisqlabels = [] for t in np.unique(minchisqtype): mask = minchisqtype == t minchisqsets.append(minchisq[mask]) minchisqlabels.append(t) plt.hist(minchisqsets, bins=40, range=(0, 20), histtype='barstacked', rwidth=1.0, label=minchisqlabels) plt.legend() plt.xlabel("lowest $\chi^2$ / dof")
def print_and_save(sn,isn,nSN,data=None,fit=None,par1=None,par2=None,save=None,message=None,filename=None): if save == 1: out = open(filename+".txt","a+") # SN included in the analysis, good fit if message == 0: print("%s (%d out of %d)"%(sn,isn+1,nSN)) if save == 1: out.write("%s "%sn) for i in range(0,len(fit[1].parameters)): out.write("%f "%fit[1].parameters[i]) out.write("%f %f "%(par1,par2)) lc_datag = data[np.where(data['band']=="p48g")] lc_datar = data[np.where(data['band']=="p48r")] if len(lc_datag)!=0 and len(lc_datar)!=0: out.write("%f "%(lc_datag['mjd'][0] - fit[1].parameters[1])) out.write("%f "%(lc_datar['mjd'][0] - fit[1].parameters[1])) elif len(lc_datag)==0: out.write("1e6 ") out.write("%f "%(lc_datar['mjd'][0] - fit[1].parameters[1])) elif len(lc_datar)==0: out.write("%f "%(lc_datag['mjd'][0] - fit[1].parameters[1])) out.write("1e6 ") else: out.write("1e6 ") out.write("1e6 ") out.write("%d "%len(lc_datag) ) out.write("%d "%len(lc_datar) ) out.write("%f %d \n"%(fit[0].chisq,fit[0].ndof)) fig = sncosmo.plot_lc(data, model=fit[1],errors=fit[0].errors,xfigsize=15,tighten_ylim=True) fig.savefig("fits/"+sn+".pdf",bbox_inches='tight') elif message == -1: print("%s not considered, duplicate "%sn) if save == 1: out.write("#%s not considered, duplicate \n"%sn) # Not a normal Ia elif message == 1: print("%s not considered, not a SN Ia norm (%s)"%(sn,par1)) if save == 1: out.write("#%s not considered, not a SN Ia norm (%s) \n"%(sn,par1)) # Missing redshift elif message == 2: if par2 == 1: print("%s not considered, missing redshift (spectra available) "%sn) if save == 1: out.write("#%s not considered, missing redshift (spectra available) \n"%sn) else: print("%s not considered, missing redshift "%sn) if save == 1: out.write("#%s not considered, missing redshift \n"%sn) # Bad fit elif message == 3: print("%s not considered, bad fit"%sn) if save == 1: out.write("#%s not considered, bad fit \n"%sn) # No data points elif message == 4: print("%s not considered, no data points \n"%sn) if save == 1: out.write("#%s not considered, no data points \n"%sn) if save == 1: out.close()
def sncosmoplot( sn, fit, res ): from pytools import plotsetup plotsetup.presfig() sncosmo.plot_lc( sn, model=fit, errors=res.errors )
wave = np.linspace(2000.0, 10000.0, 500) for w in (0.0, 0.2, 0.4, 0.6, 0.8, 1.0): source.set(w=w) plt.plot(wave, source.flux(10., wave), label='w={:3.1f}'.format(w)) plt.legend() plt.show() ########################################################################## # The w=0 spectrum is that of the Ia model, the w=1 spectrum is that of # the IIp model, while intermediate spectra are weighted combinations. # # We can even fit the model to some data! model = sncosmo.Model(source=source) data = sncosmo.load_example_data() result, fitted_model = sncosmo.fit_lc(data, model, ['z', 't0', 'amplitude', 'w'], bounds={ 'z': (0.2, 1.0), 'w': (0.0, 1.0) }) sncosmo.plot_lc(data, model=fitted_model, errors=result.errors) ########################################################################## # The fact that the fitted value of w is closer to 0 than 1 indicates that # the light curve looks more like the Ia template than the IIp template. # This is generally what we expected since the example data here was # generated from a Ia template (although not the Nugent template!).
f = h5py.File('sine.h52') cs = f['samples'] if f['current_stage'][()] else f['burn'] i = cs['last_index_filled'][()] models = [] for k in range(5): params = cs['params'][i, k] fc = pickle.load(open('sinefc.pkl2','rb')) vec = bolomc.ParamVec(params, fc.nph, fc.nl) model = fc._create_model(vec) models.append(model) fig = sncosmo.plot_lc(data=fc.lc, model=models) fig.savefig('sine3.pdf') #sncosmo.animate_source(model.source, fname="degraded.mp4") #mod2 = fc._create_model(bolomc.ParamVec(cs['params'][i-200,0], fc.nph, fc.nl)) #mod3 = fc._create_model(bolomc.ParamVec(cs['params'][i-150,20], fc.nph, fc.nl)) """control = params.copy() control[4:] = 1. vec_control = bolomc.ParamVec(control, fc.nph, fc.nl) gp_control = bolomc.reconstruct_gp(fc, vec_control) control_mod = fc._create_model(vec_control, gp_control) fig = sncosmo.plot_lc(model=models+[control_mod], data=fc.lc)
f = h5py.File(name + '.h5_lores', 'r') cs = f['samples'] if f['current_stage'][()] else f['burn'] i = cs['last_index_filled'][()] models = [] for k in range(50): params = cs['params'][i, k] fc = pickle.load(open(name + '.fc.pkl_lores','rb')) vec = bolomc.ParamVec(params, fc.nph, fc.nl) model = fc._create_model(vec) models.append(model) fig = sncosmo.plot_lc(data=fc.lc, model=models, figtext=name.split('/')[-1]) fig.savefig(name + '.lores.pdf') #sncosmo.animate_source(model.source, fname="degraded.mp4") #mod2 = fc._create_model(bolomc.ParamVec(cs['params'][i-200,0], fc.nph, fc.nl)) #mod3 = fc._create_model(bolomc.ParamVec(cs['params'][i-150,20], fc.nph, fc.nl)) """control = params.copy() control[4:] = 1. vec_control = bolomc.ParamVec(control, fc.nph, fc.nl) gp_control = bolomc.reconstruct_gp(fc, vec_control) control_mod = fc._create_model(vec_control, gp_control) fig = sncosmo.plot_lc(model=models+[control_mod], data=fc.lc)
meta, lc = lc('2007nc', 'SDSS_allCandidates+BOSS', lcmerge) bandnames = np.zeros(len(lc), dtype='a20') for i, f in enumerate(lc['FLT']): b = 'sdss'+f.strip() bandnames[i] = b.strip() bands = Column(bandnames, name='band') lc.add_column(bands) lc.remove_column('FLT') print meta print lc.meta print lc model = sncosmo.Model('s11-2006jl') snIamodel = sncosmo.Model('salt2-extended') model.set(z=meta) snIamodel.set(z=meta) res, fitted_model = sncosmo.fit_lc(lc, model, ['t0', 'amplitude']) sniares, sniafitted_model = sncosmo.fit_lc(lc, snIamodel, ['t0', 'x0', 'x1', 'c']) print fitted_model print sniafitted_model print sniares sncosmo.plot_lc(lc, fitted_model) import matplotlib.pyplot as plt print res plt.show()
k1_bump_amp=-0.2, k2_bump_amp=0.2) bump_map = ['blue', 'blue', 'blue', 'blue', 'blue', 'i', 'y', 'j', 'h', 'k'] fig = sncosmo.plot_lc(model=[model, model2], bands=np.unique(lc['filter'])) for i,ax in enumerate(fig.axes): try: line1, line2 = ax.lines[:2] x = line1.get_xdata() y2 = line2.get_ydata() y = line1.get_ydata() ax.plot(x, y2-y, color=line1.get_color(), ls=':') name = bump_map[i] bumps = filter(lambda bump: name in bump.name, model.source.bumps) for color, bump in zip(['k','g','r'], bumps): mu = bump._gaussian.mu sigma = bump._gaussian.sigma for x in [mu - sigma, mu, mu + sigma]:
#objectId = 57576 #mjd_range = (60560, 60595) #t0 = 60578. objectId = 1026 mjd_range = (62400, 62600) t0 = 62440. z = 0.27 lc = lc_factory.create(objectId) date_mask = np.where((mjd_range[0] < lc.data['mjd']) & (lc.data['mjd'] < mjd_range[1])) sn1_all = lc.data[date_mask] sncosmo.plot_lc(sn1_all) band_mask = np.where((mjd_range[0] < lc.data['mjd']) & (lc.data['mjd'] < mjd_range[1]) # & (lc.data['bandpass'] != 'lsstu') # & (lc.data['bandpass'] != 'lsstg') # & (lc.data['bandpass'] != 'lsstr') # & (lc.data['bandpass'] != 'lssti') # & (lc.data['bandpass'] != 'lsstz') # & (lc.data['bandpass'] != 'lssty') ) sn1 = lc.data[band_mask] model = sncosmo.Model(source='salt2-extended') model.set(t0=t0, z=z) res, fitted_model = sncosmo.fit_lc(sn1, model,
def plot(self, **kwds): "Plot the light curve data." kwds['data'] = self.data fig = sncosmo.plot_lc(**kwds) return fig
# monte carlo estimate covariance matrix # on bolometric light # curve due to uncertainty in distance Dl_samples = Dl_prior.rvs(1e4) lcs = lc[None, :] * (4 * np.pi * (cm(Dl_samples)**2)[:, None]) sig = np.cov(lcs, rowvar=0) fig,ax = plt.subplots(figsize=(10,5)) ax.errorbar(phase, lcs.mean(axis=0), yerr=lcs.std(axis=0), color='k', marker='.', capsize=0, ls='None', label='2011fe (Amanullah+2015)') ax.set_xlim(-20, 80) ax.set_xlabel("Time since bolometric peak (days)") ax.set_ylabel(r"$L$ (erg)") ax.grid(True) from bolomc import bolo import pickle import samples lc, _, models = samples.models('scripts/2011fe.out') stack = bolo.LCStack.from_models(models, dl=6.4) ax = stack.plot(ax=ax) fig.savefig('scripts/2011fe_bolo_comparison.pdf') import sncosmo fig = sncosmo.plot_lc(data=lc, model=models, fill_percentiles=(0.,50.,100.)) fig.savefig('2011fe.broadband.pdf')
from matplotlib import pyplot as plt wave = np.linspace(2000.0, 10000.0, 500) for w in (0.0, 0.2, 0.4, 0.6, 0.8, 1.0): source.set(w=w) plt.plot(wave, source.flux(10., wave), label='w={:3.1f}'.format(w)) plt.legend() plt.show() ########################################################################## # The w=0 spectrum is that of the Ia model, the w=1 spectrum is that of # the IIp model, while intermediate spectra are weighted combinations. # # We can even fit the model to some data! model = sncosmo.Model(source=source) data = sncosmo.load_example_data() result, fitted_model = sncosmo.fit_lc(data, model, ['z', 't0', 'amplitude', 'w'], bounds={'z': (0.2, 1.0), 'w': (0.0, 1.0)}) sncosmo.plot_lc(data, model=fitted_model, errors=result.errors) ########################################################################## # The fact that the fitted value of w is closer to 0 than 1 indicates that # the light curve looks more like the Ia template than the IIp template. # This is generally what we expected since the example data here was # generated from a Ia template (although not the Nugent template!).
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
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 c2 = ChainConsumer() means = [] stds = [] print("Add chains") for i in range(len(c.chains)): chain = c.chains[i] apparent_interp = generate_and_return() x0s = chain[:, c.parameters[i].index("$x_0$")] x1s = chain[:, c.parameters[i].index("$x_1$")] cs = chain[:, c.parameters[i].index("$c$")]
def run_sn_model(data_dict): """ Use `sncosmo` to fit data to a model with name `source_name`. For this analysis, we expect the `inputs` dictionary to have the following keys: - source: the name of the model to fit to the data - fix_z: whether to fix the redshift - photometry: the photometry to fit to the model (in csv format) - redshift: the known redshift of the object Other analysis services may require additional keys in the `inputs` dictionary. """ analysis_parameters = data_dict["inputs"].get("analysis_parameters", {}) analysis_parameters = { **default_analysis_parameters, **analysis_parameters } source = analysis_parameters.get("source") fix_z = analysis_parameters.get("fix_z") in [True, "True", "t", "true"] # this example analysis service expects the photometry to be in # a csv file (at data_dict["inputs"]["photometry"]) with the following columns # - filter: the name of the bandpass # - mjd: the modified Julian date of the observation # - magsys: the mag system (e.g. ab) of the observations # - flux: the flux of the observation # # the following code transforms these inputs from SkyPortal # to the format expected by sncosmo. # rez = {"status": "failure", "message": "", "analysis": {}} try: data = Table.read(data_dict["inputs"]["photometry"], format='ascii.csv') data.rename_column('mjd', 'time') data.rename_column('filter', 'band') data.rename_column('magsys', 'zpsys') data['flux'].fill_value = 1e-6 data = data.filled() data.sort("time") redshift = Table.read(data_dict["inputs"]["redshift"], format='ascii.csv') z = redshift['redshift'][0] except Exception as e: rez.update({ "status": "failure", "message": f"input data is not in the expected format {e}", }) return rez # we will need to write to temp files # locally and then write their contents # to the results dictionary for uploading local_temp_files = [] try: model = sncosmo.Model(source=source) if fix_z: if z is not None: model.set(z=z) bounds = {'z': (z, z)} else: raise ValueError("No redshift provided but `fix_z` requested.") else: bounds = {'z': (0.01, 1.0)} # run the fit result, fitted_model = sncosmo.fit_lc( data, model, model.param_names, bounds=bounds, ) if result.success: f = tempfile.NamedTemporaryFile(suffix=".png", prefix="snplot_", delete=False) f.close() _ = sncosmo.plot_lc( data, model=fitted_model, errors=result.errors, model_label=source, figtext=data_dict["resource_id"], fname=f.name, ) plot_data = base64.b64encode(open(f.name, "rb").read()) local_temp_files.append(f.name) # make some draws from the posterior (simulating what we'd expect # with an MCMC analysis) post = rng.multivariate_normal(result.parameters, result.covariance, 10000) post = post[np.newaxis, :] # create an inference dataset inference = az.convert_to_inference_data( {x: post[:, :, i] for i, x in enumerate(result.param_names)}) f = tempfile.NamedTemporaryFile(suffix=".nc", prefix="inferencedata_", delete=False) f.close() inference.to_netcdf(f.name) inference_data = base64.b64encode(open(f.name, 'rb').read()) local_temp_files.append(f.name) result.update({"source": source, "fix_z": fix_z}) f = tempfile.NamedTemporaryFile(suffix=".joblib", prefix="results_", delete=False) f.close() joblib.dump(result, f.name, compress=3) result_data = base64.b64encode(open(f.name, "rb").read()) local_temp_files.append(f.name) analysis_results = { "inference_data": { "format": "netcdf4", "data": inference_data }, "plots": [{ "format": "png", "data": plot_data }], "results": { "format": "joblib", "data": result_data }, } rez.update({ "analysis": analysis_results, "status": "success", "message": f"Good results with chi^2/dof={result.chisq/result.ndof}", }) else: log("Fit failed.") rez.update({ "status": "failure", "message": "model failed to converge" }) except Exception as e: log(f"Exception while running the model: {e}") log(f"{traceback.format_exc()}") log(f"Data: {data}") rez.update({ "status": "failure", "message": f"problem running the model {e}" }) finally: # clean up local files for f in local_temp_files: try: os.remove(f) except: # noqa E722 pass return rez
def sncosmoplot(sn, fit, res): from pytools import plotsetup from matplotlib import rcParams plotsetup.fullpaperfig() rcParams['text.usetex'] = False sncosmo.plot_lc(sn, model=fit, errors=res.errors)
band = np.zeros(len(hml[:, 1]), dtype='|S6') for i in range(len(ab)): ab[i] = 'ab' band[i] = 'ptf48r' hml = np.column_stack((hml, ab, band)) # Converting into a table using astropy with titles: ptfname, time, # magnitude, mag_err, flux, flux_err, zp_new, zp, ra, dec, zpsys and filter 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')) # Fitting model: model parameters of z, x1 and c are bound. res, fitted_model = sncosmo.fit_lc(hml_dat, model, ['z', 't0', 'x0', 'x1', 'c'], bounds={'z': (0.005, 0.35), 'x1': (-3.5, 3.5), 'c': (-0.35, 0.35)}) # Use sncosmo to plot data and error sncosmo.plot_lc(hml_dat, model=fitted_model, errors=res.errors, color='blue', figtext=str(hml[:, 0][0]), xfigsize=10) # The following information can be shown if wished : # print 'chi^2 value at minimum:', res.chisq, ';', 'dof:', res.ndof print 'Number of chi^2 function calls made:', res.ncall print 'reduced chi^2:', res.chisq / res.ndof print # '### Parameters ###' print 'SN', str(hml[:, 0][0]), '; z:', float(res.parameters[0]) print 'Completed fit for supernova:', hml[:, 0][0] # Displays plot plt.show()
def sncosmoplot_minchi2fit( sn, fit, res ): import sncosmo sncosmo.plot_lc( sn, model=fit, errors=res.errors )
def __call__(self, meas): #time_begin=time.time() t = meas select = t[np.where( np.logical_and(t['flux'] / t['fluxerr'] > 5., t['flux'] > 0.))] #print 'hello',select.meta #idx=select['band']!='LSST::u' #select=select[idx] #print 'I will fit',meas """ if self.z > 0.35: idx=select['band']!='LSST::g' select=select[idx] if self.z > 0.85: idx=select['band']!='LSST::r' select=select[idx] if self.z > 1.27: idx=select['band']!='LSST::i' select=select[idx] """ #print 'what I have to fit',len(select) try: #print 'trying to fit',len(select) fit_status = 'notok' if len(select) > 0: res, fitted_model = sncosmo.fit_lc( select, self.SN_fit_model, ['t0', 'x0', 'x1', 'c'], bounds={'z': (self.z - 0.001, self.z + 0.001)}) #print 'total elapse time fit',time.time()-time_begin #self.sigma_c=res['errors']['c'] mbfit = fitted_model._source.peakmag('bessellb', 'vega') #mbfit=0. params = {} #print res for i, par in enumerate(fitted_model.param_names): params[par] = fitted_model.parameters[i] #print(i,par) #snutils=SN_Utils() #mbfit_calc=snutils.mB(params) #print 'total elapse time mbfit',time.time()-time_begin,mbfit fit_status = 'fitok' else: res = None fitted_model = None mbfit = -1 fit_status = 'nodat' """ covar_mb=snutils.Covar(params,res['covariance'],res['vparam_names']) print covar_mb #print 'fitted',res['vparam_names'] covar_mb={} what=(params['z'],params['x0'],params['x1'],params['c']) print what Der=np.zeros(shape=(len(res['vparam_names']),1)) ider=-1 for i,key in enumerate(res['vparam_names']): ider+=1 if key == 't0': Der[ider]=0. else: Der[ider]=griddata((deriv_mb['z'],deriv_mb['X0'],deriv_mb['X1'],deriv_mb['Color']), deriv_mb['dmb_d'+key],what , method='nearest') print 'hhh',res['covariance'],Der Prod=np.dot(res['covariance'],Der) #print 'prod',Prod for i,key in enumerate(res['vparam_names']): if key != 'c': covar_mb['salt2.Cov'+key.upper()+'mb']=Prod[i,0] else: covar_mb['salt2.CovColormb']=Prod[i,0] covar_mb['salt2.Covmbmb']=np.asscalar(np.dot(Der.T,Prod)) print covar_mb #print 'total elapse time covar',time.time()-time_begin #print 'after fit',mbfit,mbfit_calc,mbfit-mbfit_calc #snutils.Test() """ if self.Plot: sncosmo.plot_lc(select, model=fitted_model, color='r', pulls=False, errors=res.errors) plt.show() #print 'total elapse time',time.time()-time_begin #return res,fitted_model,mbfit,covar_mb,'ok' return res, fitted_model, mbfit, fit_status except (RuntimeError, TypeError, NameError): """ print select self.Plot_bands(select) plt.show() self.sigma_c=0. print 'crashed' """ #print 'crashed' #self.Plot_bands(select) #plt.show() #return None,None,-1,None,'crash' return None, None, -1, 'crash'
f = h5py.File(name + '.h5_lores', 'r') cs = f['samples'] if f['current_stage'][()] else f['burn'] i = cs['last_index_filled'][()] models = [] for k in range(50): params = cs['params'][i, k] fc = pickle.load(open(name + '.fc.pkl_lores', 'rb')) vec = bolomc.ParamVec(params, fc.nph, fc.nl) model = fc._create_model(vec) models.append(model) fig = sncosmo.plot_lc(data=fc.lc, model=models, figtext=name.split('/')[-1]) fig.savefig(name + '.lores.pdf') #sncosmo.animate_source(model.source, fname="degraded.mp4") #mod2 = fc._create_model(bolomc.ParamVec(cs['params'][i-200,0], fc.nph, fc.nl)) #mod3 = fc._create_model(bolomc.ParamVec(cs['params'][i-150,20], fc.nph, fc.nl)) """control = params.copy() control[4:] = 1. vec_control = bolomc.ParamVec(control, fc.nph, fc.nl) gp_control = bolomc.reconstruct_gp(fc, vec_control) control_mod = fc._create_model(vec_control, gp_control) fig = sncosmo.plot_lc(model=models+[control_mod], data=fc.lc) fig.savefig('%s_%d_%d.pdf' % (fc.lc.meta['name'], i, 0))
''' for mod in mods: #print(mod) fits.append( _snFit((mod, lc, method, params, bounds, ignore, constants, dust, effect_names, effect_frames))) ''' for model in models[typ[f[:-4]]]: mod=sncosmo.Model(source=model) mod.set(z=redshift[f[:-4]]) res,fit=sncosmo.fit_lc(lc,mod,[x for x in mod.param_names if x != 'z'],bounds={'t0':(np.min(lc['MJD'])-20,np.min(lc['MJD'])+20)}) ''' bestChisq = np.inf for fit in fits: if fit: res, mod = fit if res.chisq < bestChisq: bestChisq = res.chisq bestfit = mod bestres = res t0 = bestres.parameters[bestres.param_names.index('t0')] print(bestfit._source.name) sncosmo.plot_lc(lc, model=bestfit, errors=bestres.errors) plt.show() sne.append(f[:-4]) times.append(t0) np.savetxt('sne.dat', sne, fmt='%s') np.savetxt('peaks.dat', times)
def __init__(self,survey_fields=None, simlib_file=None,simlib_obs_sets=None, c_pdf='Normal',\ c_params=[-0.05,0.2],x1_pdf='Normal',x1_params=[0.5,1.0],\ t0min = 56525.0,t0max=57070.0,NumSN = 500,minNumEpochs = 5,minNumFilters = 3, minSNR = 4.0,\ cosmo=None,alpha=0.14,beta=3.2,deltaM=0.0,zp_off=[0.0,0.0,0.0,0.0]): '''Input: survey_fields:dict, survey fields to generate z dist, values should be [area,zmin,zmax,dndz func,time ] simlib_file: str, snana simlib simlib_obs_sets= observation library c_pdf and x1_pdf: str, either 'Normal' or 'SkewNormal' c_params and x1_params: list, hyperparams for 'Normal' [mean,var] or 'SkewNormal' [mean,var,skew] t0min: float t0max: float NumSN: int, number of SN to simulate and fit minNumEpochs: int, minNumFilters: int, minSNR: int, selection cut: require minNumEpochs in at least minNumFilters with SNR>minSNR ''' self.t0min = t0min self.t0max = t0max self.NumSN = NumSN self.minNumEpochs = minNumEpochs self.minNumFilters = minNumFilters self.minSNR = minSNR self.alpha = alpha self.beta = beta self.deltaM = deltaM self.zp_off = zp_off if survey_fields == None: self.survey_fields = self.DES_specific_zdist() else: self.survey_fields = survey_fields if cosmo == None: self.cosmo = FlatLambdaCDM(H0=70.0, Om0=0.3) else: self.cosmo = cosmo self.totz = self.get_zdist() print "Total number of sn to be simulated:", len(self.totz) start = time.time() if simlib_file: self.simlib_meta, self.simlib_obs_sets = self.read_simlib( simlib_file) else: self.simlib_obs_sets = simlib_obs_sets end = time.time() if bench: print "Read simlib file in ", end - start, "secs" self.c_pdf = c_pdf self.c_params = c_params self.x1_pdf = x1_pdf self.x1_params = x1_params start = time.time() self.generate_random_c_x1() end = time.time() if bench: print "c-x1 generated in ", end - start, "secs" self.generate_t0() #EJ: Note the model needs to be downloaded at this point - is there #anyway we can avoid this for users who are not online? dust = sncosmo.CCM89Dust() self.model = sncosmo.Model(source='salt2-extended',effects=[dust],effect_names=['mw'],\ effect_frames=['obs']) self.get_parameter_list() self.generate_lcs() start = time.time() self.fit_lcs() end = time.time() if bench: print "Fitting took", end - start, "secs" if setplot: for ii in range(len(self.fit_results)): plt.plot(self.simx0[ii],self.fit_results[ii]['parameters'][2],\ linestyle='None', marker="o") plt.savefig('x0.png') plt.close() for ii in range(len(self.fit_results)): plt.plot(self.simx1[ii],self.fit_results[ii]['parameters'][3],\ linestyle='None', marker="o") plt.savefig('x1.png') plt.close() for ii in range(len(self.fit_results)): plt.plot(self.simc[ii],self.fit_results[ii]['parameters'][4],\ linestyle='None', marker="o") plt.savefig('c.png') plt.close() #print self.fit_results[0].keys(), self.fit_results[0]['param_names'], self.fit_results[0]['parameters'] #print self.fit_results[0]['parameters'] #print self.fit_results[0]['covariance'], self.fit_results[0]['covariance'].shape #print self.totz[0],self.simt0[0], self.simx0[0], self.simx1[0], self.simc[0] #print self.lcs[0][0] #print self.lcs[0][0] #print self.fitted_model[0] sncosmo.plot_lc(self.lcs[0][0], model=self.fitted_model[0], errors=self.fit_results[0].errors) plt.savefig("lc.png")