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
data = sncosmo.load_example_data() print(data) ##################################################################### # An important additional note: a table of photometric data has a # ``band`` column and a ``zpsys`` column that use strings to identify # the bandpass (e.g., ``'sdssg'``) and zeropoint system (``'ab'``) of # each observation. If the bandpass and zeropoint systems in your data # are *not* built-ins known to sncosmo, you must register the # corresponding `~sncosmo.Bandpass` or `~sncosmo.MagSystem` to the # right string identifier using the registry. # create a model model = sncosmo.Model(source='salt2') # run the fit result, fitted_model = sncosmo.fit_lc( data, model, ['z', 't0', 'x0', 'x1', 'c'], # parameters of model to vary bounds={'z': (0.3, 0.7)}) # bounds on parameters (if any) ##################################################################### # The first object returned is a dictionary-like object where the keys # can be accessed as attributes in addition to the typical dictionary # lookup like ``result['ncall']``: 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)
def setup_class(self): self.model = sncosmo.Model(source=flatsource(), effects=[sncosmo.CCM89Dust()], effect_frames=['obs'], effect_names=['mw'])
def snia(ax, band='p48g', timescale='efoldDecline', remove_nearby=True): """ Please contact me if you have any questions: [email protected] Sample from SNe Ia young sample observed in the ZTF extragalactic high-cadence partnership experiment. We use 121 ``normal'' SNe Ia from 2018 (including normal, 91T-like, and 91bg-like SNe). Light curves presented in Yao et al. (2019) Parameters: ----------- band: 'p48g': use g band ZTF data 'p48r': use r band ZTF data 'swope2::v', 'f555w', ...: use the SALT2 (Guy et al 2007) tool to estimate light curves in other bands timescale: 'efoldDecline': plot the rest-frame duration for the flux to drop from peak to 1/e*peak [other options TBD] remove_nearby [bool]: If True, remove two SNe with z < 0.02 (ZTF18aasdted and ZTF18abgmcmv), since their absolute peak luminosity are largely affected by the local group peculiar velocity. """ from snia_funcs import add_ZTFfilters, mylinear_fit cwd = os.getcwd() datafile = cwd[:-4] + 'data/snia/' + band + '_' + timescale + '.csv' tbfile = cwd[:-4] + 'data/snia/Yao2019_catalog.csv' try: os.stat(datafile) except: tb = pd.read_csv(tbfile) add_ZTFfilters() zp_temp = 26.325 Mpeaks = np.zeros(len(tb)) Mpeaks_unc = np.zeros(len(tb)) Tdeclines = np.zeros(len(tb)) for i in range(len(tb)): name = tb['name'].values[i] lcfile = cwd[:-4] + 'data/snia/lightcurve/' + name + '.csv' z = tb['z'].values[i] z_unc = tb['z_unc'].values[i] ebv = tb['E_B_V_SandF'].values[i] mytb = pd.read_csv(lcfile) lcband = mytb['band'].values Fratio = mytb['Fratio'].values Fratio_unc = mytb['Fratio_unc'].values zp = np.ones(len(mytb)) * zp_temp zpsys = ['ab'] * len(mytb) time = mytb['jdobs'].values lc = Table( data=[time, lcband, Fratio, Fratio_unc, zp, zpsys], names=['time', 'band', 'flux', 'fluxerr', 'zp', 'zpsys']) dust = sncosmo.CCM89Dust() model = sncosmo.Model(source='salt2', effects=[dust], effect_names=['mw'], effect_frames=['obs']) model.set(z=z, mwebv=ebv, mwr_v=3.1) res, fitted_model = sncosmo.fit_lc(lc, model, ['t0', 'x0', 'x1', 'c'], bounds={ 'x1': (-10., 10.), 'c': (-5., 5.) }) t0jd_B = res.parameters[1] # B band maximum light epoch t0jd_B_unc = res['errors']['t0'] ta = np.round(t0jd_B - 25, 2) + 0.01 tgrid = np.linspace(ta, ta + 75, 100 * 100 + 1) fitted_band = fitted_model.bandflux(band, tgrid, zp=zp_temp, zpsys='ab') # estimate peak epoch and apparent magnitude ixx = np.argsort(fitted_band) t_max = tgrid[ixx[-1]] fratio_max = fitted_band[ixx[-1]] appmag_max = -2.5 * np.log10(fratio_max) appmag_around = fitted_model.bandmag( band, 'ab', [t_max - t0jd_B_unc, t_max + t0jd_B_unc]) - zp_temp appmag_max_unc = (np.sum(appmag_around) - 2 * appmag_max) / 2. # estimate extinction corrected peak epoch apparent magnitude wave_eff = sncosmo.get_bandpass(band).wave_eff Aband = extinction.ccm89(np.array([wave_eff]), 3.1 * ebv, 3.1)[0] appmag0_max = appmag_max - Aband # estimate extinction corrected peak epoch absolute magnitude D = cosmo.luminosity_distance([z])[0].value * 1e+6 # in pc D_unc = ( cosmo.luminosity_distance([z + z_unc])[0].value - cosmo.luminosity_distance([z - z_unc])[0].value) / 2. * 1e+6 dis_mod = 5 * np.log10(D / 10) dis_mod_unc = 5 / np.log(10) / D * D_unc absmag0_max = appmag0_max - dis_mod absmag0_max_unc = np.sqrt(appmag_max_unc**2 + dis_mod_unc**2) Mpeaks[i] = absmag0_max Mpeaks_unc[i] = absmag0_max_unc # estimate the characteristic timescale if timescale == 'efoldDecline': fratio_end = fratio_max / np.e ind = np.argsort(abs(fitted_band - fratio_end)) ix_end = ind[ind > ixx[-1]][0] t_end = tgrid[ix_end] time_duration = t_end - t_max Tdeclines[i] = time_duration tbnew = Table(data=[ tb['name'].values, tb['z'].values, Mpeaks, Mpeaks_unc, Tdeclines ], names=['name', 'z', 'mag', 'mag_unc', 'timescale']) tbnew.write(datafile, overwrite=True) data = pd.read_csv(datafile) data = data[data.z > 0.02] x = data['timescale'].values / (1 + data['z'].values) y = data['mag'].values y_err = data['mag_unc'].values # plt.figure(figsize=(12, 8)) # ax = plt.subplot(111) # plot individual data points ax.errorbar(x, y, y_err, fmt='.k') # plot the grey contour slope, e_slope, intercept = mylinear_fit(x, y, y_err, npar=2) nstd = 5. # to draw 5-sigma intervals slope_up = slope + nstd * e_slope slope_dw = slope - nstd * e_slope x_fit = np.linspace(min(x) - 0.5, max(x) + 0.5, 100) # fit = slope * x_fit+ intercept fit_up = slope_up * x_fit + intercept fit_dw = slope_dw * x_fit + intercept ax.fill_between(x_fit, fit_up, fit_dw, color='grey', alpha=.25, label="5-sigma interval")
def __init__(self, sntype, observations=None, z_range=[1.8,2.2], t0_range=[0,0], nsim=100, perfect=True, Om=0.3, H0=70, filterset='hst' ): """ Run a monte carlo sim using sncosmo to simulate <nsim> SNe of the given <sntype> over the given <z_range>. Simulates Type Ia SNe with the SALT2 model, and CC SNe with the SNANA CC templates. Observations are done at time t=0, unless specified otherwise in a user-defined observations table. Set perfect=True for noiseless "observations" of the simulated SNe. :return: """ from astropy import cosmology import sncosmo from numpy.random import normal, uniform, choice import numpy as np self.sntype = sntype self.z_range = z_range self.nsim = nsim self.perfect = perfect if observations is None : observations = mkobservationsTable( filterset=filterset ) self.observations = observations # Make a list of all the unique sncosmo source models available, # and assign a relative probability that any given simulated SN of this # type (CC or Ia) belongs to that subclass if sntype.lower() in ['cc','ii','ibc'] : subClassDict = SubClassDict_SNANA[sntype.lower()] subClassProbs = ccSubClassProbs[sntype.lower()] self.SourcenameSet = np.array( subClassDict.keys() ) self.SubclassSet = np.array([ subClassDict[source] for source in self.SourcenameSet ]) self.SubclassCount = np.array([ len(np.where(self.SubclassSet==subclass)[0]) for subclass in self.SubclassSet ], dtype=float) self.SourceprobSet = np.array([ subClassProbs[subclass] for subclass in self.SubclassSet ]) / self.SubclassCount self.SourceprobSet /= self.SourceprobSet.sum() elif sntype.lower()=='ia' : # No sub-class divisions for SNIa self.SourcenameSet = np.array(['salt2-extended']) self.SubclassSet = np.array( ['Ia'] ) self.SourceprobSet = np.array( [1] ) self.SubclassCount = np.array( [1] ) # load the O'Donnell 1994 dust model self.dust = sncosmo.OD94Dust() # Define an sncosmo SN model for each available source modelset = np.array([ sncosmo.Model(source=source, effects=[self.dust], effect_names=['host'], effect_frames=['rest']) for source in self.SourcenameSet ]) # Define a cosmology # self.Om = Om # self.H0 = H0 self.cosmo = cosmology.FlatLambdaCDM(Om0=Om, H0=H0) # For each simulated SN, draw random Av from distributions # as defined in Rodney et al 2014a : # For SN Ia : P(Av) = exp(-Av/0.33) # For CC SN : P(Av) = 4 * gauss(0.6) + exp(-Rv/1.7) if sntype=='Ia': tau,sigma,R0 = 0.33, 0, 0 else : tau,sigma,R0 = 1.7, 0.6, 4 self.Av = mcsample( pAv, nsim, tau=tau, sigma=sigma, R0=R0 ) # For each simulated SN, draw a random Rv from a normal # distribution centered on 3.1 with width 0.5 self.Rv = normal( 3.1, 0.5, nsim ) self.Rv = np.where( self.Rv>0, self.Rv, 0.01 ) # Convert Av and Rv to E(B-V) : # Rv = Av/EBV ==> EBV = Av/Rv self.EBV = self.Av / self.Rv # TODO : draw the redshifts with a metropolis-hastings sampler to match # a distribution defined based on the expected SN rate # Disabled : uniform redshift spacing # zlist = np.linspace( z_range[0], z_range[1], nsim ) # Draw a random redshift from a uniform distribution self.z = uniform( low=z_range[0], high=z_range[1], size=nsim ) lightcurvelist = [] peakabsmagRlist = [] modelparamlist = [] subclasslist = [] modelindexlist = [] sourcenamelist = [] t0list = [] if sntype=='Ia': x0list = [] x1list = [] clist = [] else : amplitudelist = [] for isim in range(self.nsim): # Randomly draw an sncosmo model from the available list, according to # the predefined probability list, setting the SN sub-class for this # simulated SN imodel = choice( np.arange(len(modelset)), replace=True, p=self.SourceprobSet ) model = modelset[imodel] subclass = self.SubclassSet[imodel] z = self.z[isim] EBV = self.EBV[isim] Rv = self.Rv[isim] # Set the peak absolute magnitude according to the observed # luminosity functions, as defined in Table 3 of Graur:2014a; # and set the host extinction according to the 'mid' dust model # of Rodney:2014a. if subclass == 'Ia' : MR = normal( -19.37, 0.47 ) elif subclass == 'Ib' : MR = normal( -17.90, 0.90 ) elif subclass == 'Ic' : MR = normal( -18.30, 0.60 ) elif subclass == 'IIP' : MR = normal( -16.56, 0.80 ) elif subclass == 'IIL' : MR = normal( -17.66, 0.42 ) elif subclass == 'IIn' : MR = normal( -18.25, 1.00 ) model.set(z=z) model.set_source_peakabsmag( MR, 'bessellr', 'vega', cosmo=self.cosmo) modelindexlist.append( imodel ) subclasslist.append( subclass ) peakabsmagRlist.append( MR ) sourcenamelist.append( self.SourcenameSet[imodel] ) if subclass =='Ia' : x0 = model.get('x0') # TODO : use bifurcated gaussians for more realistic x1,c dist'ns x1 = normal(0., 1.) c = normal(0., 0.1) t0 = uniform( t0_range[0], t0_range[1] ) modelparams = {'z':z, 't0':t0, 'x0':x0, 'x1':x1, 'c':c, 'hostebv':EBV, 'hostr_v':Rv} t0list.append( t0 ) x0list.append( x0 ) x1list.append( x1 ) clist.append( c ) t0list.append( t0 ) else : amplitude = model.get('amplitude') t0 = uniform( t0_range[0], t0_range[1] ) modelparams = {'z':z, 't0':t0, 'amplitude':amplitude, 'hostebv':EBV, 'hostr_v':Rv } amplitudelist.append( amplitude ) t0list.append( t0 ) modelparamlist.append( modelparams ) # Generate one simulated SN: snlc = sncosmo.realize_lcs(self.observations, model, [ modelparams ], thresh=None)#, perfect=perfect ) lightcurvelist.append( snlc[0] ) self.lightcurves = lightcurvelist self.t0 = np.array( t0list ) self.modelindex = np.array( modelindexlist ) self.sourcename = np.array( sourcenamelist ) self.subclass = np.array( subclasslist ) self.modelparam = np.array( modelparamlist ) self.peakabsmagR = np.array( peakabsmagRlist ) if sntype=='Ia': self.x0 = np.array( x0list ) self.x1 = np.array( x1list ) self.c = np.array( clist ) else : self.amplitude = np.array( amplitudelist ) return
return snid, reschar snana_eg = SNANASims.fromSNANAfileroot( snanafileroot='LSST_Ia', location='/home/zach/Fall2016/CSC_380/MINION_1016_10YR_DDF_v2/', coerce_inds2int=False) if __name__ == '__main__': snana_eg = SNANASims.fromSNANAfileroot( snanafileroot='LSST_Ia', location='/home/zach/Fall2016/CSC_380/MINION_1016_10YR_DDF_v2/', coerce_inds2int=False) dust = sncosmo.CCM89Dust() model = sncosmo.Model(source='salt2-extended', effects=[dust, dust], effect_names=['host', 'mw'], effect_frames=['rest', 'obs']) for i in range(3): try: snid, r = inferParams(snana_eg, model, sncosmo.fit_lc, i, minsnr=3.) with open('results.dat', 'w') as fh: # Should Not be a text file when improved! write_str = snid write_str += ','.join(map(str, r.parameters)) # We should only keep the the independent components # unlike what I am doing here write_str += ','.join(
import pytest import sncosmo import IDRTools import numpy as np from spectral_lines import Gauss np.random.seed(0) DS = IDRTools.Dataset(subset=['training', 'validation']) HSIAO = sncosmo.Model(source='hsiao') HSIAO.set(z=0.05, amplitude=1) class TestGauss: @pytest.fixture(scope='class') def state(self): sn = np.random.choice(DS.sne) idr_spec = sn.spec_nearest(0) self.idr_si = Gauss(idr_spec) self.idr_ca = Gauss(idr_spec, line='CaIIHK') wave, flux, var = idr_spec.rf_spec() self.idr_sim_si = Gauss([wave, flux, var], sim=True) self.idr_sim_ca = Gauss([wave, flux, var], line='CaIIHK', sim=True) wrong_var = np.mean(flux) / 1e6 * np.ones(flux.shape) self.idr_sim_wrong_var = Gauss([wave, flux, wrong_var], sim=True) guess_var = (0.05 * np.quantile(flux, 0.99))**2 * np.ones(flux.shape) self.idr_sim_guess_var = Gauss([wave, flux, guess_var], sim=True) sim_wave = np.arange(3000., 9000., 2.) sim_flux = HSIAO.flux(0, sim_wave) sim_wave /= 1.05 sim_var = (0.05 * np.quantile(sim_flux, 0.99))**2 sim_var *= np.ones(sim_flux.shape)
# 0.00589190110442 snmod = 'hsiao' synlam = numpy.array([[3300.00, 3978.02], [3978.02, 4795.35], [4795.35, 5780.60], [5780.60, 6968.29], [6968.29, 8400.00]]) synname = ['U', 'B', 'V', 'R', 'I'] synbands = [] for name, lams in zip(synname, synlam): synbands.append(sncosmo.Bandpass(lams, [1., 1.], name='tophat' + name)) model_nodust = sncosmo.Model(source=snmod) flux_nodust = model_nodust.bandflux(synbands, 0.) av = numpy.exp( numpy.arange(numpy.log(0.005), numpy.log(1.8) + 0.001, numpy.log(1.8 / 0.005) / 25)) rv = numpy.exp( numpy.arange(numpy.log(2.1), numpy.log(6.9) + 0.001, numpy.log(6.9 / 2.1) / 50)) avs = [] ebvs = [] rvs = [] AX = []
def analyze(): pkl_file = open('ccm.pkl', 'r') amed = pickle.load(pkl_file) pkl_file.close() synlam = numpy.array([[3300.00, 3978.02], [3978.02, 4795.35], [4795.35, 5780.60], [5780.60, 6968.29], [6968.29, 8400.00]]) synname = ['U', 'B', 'V', 'R', 'I'] synbands = [] for name, lams in zip(synname, synlam): synbands.append(sncosmo.Bandpass(lams, [1., 1.], name='tophat' + name)) model_nodust = sncosmo.Model(source='hsiao') flux_nodust = model_nodust.bandflux(synbands, 0.) av = numpy.exp( numpy.arange(numpy.log(0.005), numpy.log(1.8) + 0.001, numpy.log(1.8 / 0.005) / 25)) rv = numpy.exp( numpy.arange(numpy.log(2.1), numpy.log(6.9) + 0.001, numpy.log(6.9 / 2.1) / 50)) avs = [] ebvs = [] rvs = [] AX = [] for a in av: for r in rv: dust = sncosmo.CCM89Dust() dust.set(ebv=a / r, r_v=r) model = sncosmo.Model(source='hsiao', effects=[dust], effect_names=['host'], effect_frames=['rest']) AX.append(-2.5 * numpy.log10(model.bandflux(synbands, 0.) / flux_nodust)) avs.append(a) ebvs.append(a / r) rvs.append(r) avs = numpy.array(avs) ebvs = numpy.array(ebvs) AX = numpy.array(AX) rvs = numpy.array(rvs) diff = AX - (amed[0][None,:]*avs[:,None]+ amed[1][None,:] * avs[:,None]**2 \ +amed[2][None,:]*ebvs[:,None]+ amed[3][None,:] * ebvs[:,None]**2 \ +amed[4][None,:] * (avs*ebvs)[:,None] \ +amed[5][None,:] * (avs**3)[:,None] \ +amed[6][None,:] * (ebvs**3)[:,None] \ +amed[7][None,:] * ((avs**2)*ebvs)[:,None] \ +amed[8][None,:] * (avs*(ebvs**2))[:,None] \ ) print numpy.max(numpy.abs(diff)) arg = numpy.argmax(numpy.abs(diff)) print avs[arg / 5], ebvs[arg / 5] print diff[arg / 5] print avs.max() wav = avs == 1.8 for i in xrange(5): plt.plot(rvs[wav], diff[wav, i], label=synname[i]) plt.ylabel(r'$\Delta A$') plt.xlabel(r'$R$') plt.legend() pp = PdfPages('output18/dfitz.pdf') plt.savefig(pp, format='pdf') pp.close() plt.close()
def __init__(self, PATH_VERSION, OPTMASK, ARGLIST, HOST_PARAM_NAMES): try: # Setup infromation from SNANA ############################## # Process SNANA options bit flags. self.verbose = OPTMASK & (1 << mask_bit_locations["verbose"]) > 0 self.dump = OPTMASK & (1 << mask_bit_locations["dump"]) > 0 try: # split comman separated key value pairs. # search for key "RANSEED" in list # exctract RASEED integer self.SNANA_RANSEED = [ int(arg.split()[1]) for arg in ARGLIST.split(",") if "RANSEED" in arg ][0] except IndexError: # if ranseed is not given if self.verbose: print("No RANSEED found.", flush=True) self.SNANA_RANSEED = 100 if self.verbose: print("Random seed set to ", self.SNANA_RANSEED, flush=True) np.random.seed(self.SNANA_RANSEED) self.host_param_names = [x.upper() for x in HOST_PARAM_NAMES.split(",")] # Load Params file ################## if PATH_VERSION[-1] != "/": if self.verbose: pass print( "PATH_VERSION is expected to be a folder and end with '/'.", flush=True, ) print("PATH_VERSION is {}".format(PATH_VERSION)) # raise RuntimeError("PATH_VERSION is {}. Expected a trailing '/'.".format(PATH_VERSION))) self.PATH_VERSION = os.path.expandvars(os.path.dirname(PATH_VERSION)) if os.path.exists(os.path.join(self.PATH_VERSION, "SNEMO.params")): self.paramfile = os.path.join(self.PATH_VERSION, "SNEMO.params") elif os.path.exists(os.path.join(self.PATH_VERSION, "SNEMO.PARAMS")): self.paramfile = os.path.join(self.PATH_VERSION, "SNEMO.PARAMS") else: raise RuntimeError( "param file %s not found!" % os.path.join(self.PATH_VERSION, "SNEMO.params") ) with open(self.paramfile) as f: print(f"SNEMO paramfile, in {self.PATH_VERSION}") print(f.read(), flush=True) self.params_file_contents = yaml.load(f, Loader=yaml.FullLoader) # Set-up SNEMO options ###################### # Read in from param file or use a default # https://www.tutorialspoint.com/python/dictionary_get.htm # TODO: check user inputs and give helpful excpetions. # minmimum value of As. Default is set from the lower limit of # SNFactory, not the extrapolated distributions. self.As_min = self.params_file_contents.get("AS_MIN", -1.0) # If true, print a summary every time and SED is fetched. self.DEBUG_INFO = self.params_file_contents.get("DEBUG", False) # If true, always set A_s (observed) to zero, but does remove the # A_s_true-M_B relationship. self.NO_COLOR_LAW = self.params_file_contents.get("NO_COLOR_LAW", False) # If true, (observed) c_i and A_s are the same for all SNe. Does # remove the A_s_true-M_B relationship. NO_COLOR_LAW is ignored. self.NO_SCATTER = self.params_file_contents.get("NO_SCATTER", False) # Changes the range of the observed parameter scatter by rescaling # via this parameter self.SCALE_SCATTER = self.params_file_contents.get("SCALE_SCATTER", 1.0) # If true, use the Hsiao template to extend the wavelength range # of the mean SED. Does not effect the variable components. self.EXTENDED = self.params_file_contents.get("EXTENDED", False) # Setup SNEMO & SNCOSMO ####################### if self.params_file_contents["SNEMO_model"] in [2, 7, 15]: # TODO # Allow for Hsiao template wavelength extensions via: # if self.params_file_contents["EXT"] == "True": # snemo_source = sncosmo.models.SNEMOSource('location_to_extended_models/ext_snemo15.dat') # snemo_ext = sncosmo.Model(source=snemo_source) # else: # what is presently here. # This should not be the default model, since it extends the model to 1,000-20,000AA but # all variability is still only from 3500-8400AA. source_name = "snemo{}".format(self.params_file_contents["SNEMO_model"]) self.model = sncosmo.Model(source=source_name) if self.verbose: # Need to flush stdout to work with batch jobs/slurm print( "Running with SNCosmo model {}.".format(source_name), flush=True ) else: if self.verbose: print( "SNEMO_model in params file (", self.params_file_contents["SNEMO_model"], ") is not an SNEMO model (2, 7, 15). Defaulting to SNEMO15.", flush=True, ) self.model = sncosmo.Model(source="snemo15") kde_path = os.path.expandvars( os.path.join( self.params_file_contents["KDE_FOLDER"], "{}_KDE_published.pkl".format(self.model.source.name), ) ) with open(kde_path, "rb") as f: self.KDE, self.rot, self.eigenvals, _ = pickle.load(f) # Setup SNANA/BYOSED Framework variables ######################################## # set a non-positive integer, SNANA uses this as a counting number # this works around the buggy nature of `new_event` in `fetchSED_SNEMO()` self.external_id = -1 # initiate an empty cache. # SNANA requests the full SED per filter it looks at. Don't regenerate the SED for # the same SN at the same phase just because SNANA is looking with a new filter self.cache = {} # SNANA required variables self.wave = self.model.source._wave # https://stackoverflow.com/questions/7271385/how-do-i-combine-two-lists-into-a-dictionary-in-python self.parameter_values = dict( zip(self.model.param_names[2:], self.model.parameters[2:]) ) # Not being used outside of SNANA method. # Currently being used like a c-programmer # Is this faster then recomputing each time the methods are called? self.wavelen = len(self.wave) # don't need SNCosmo's z and t0 self.parameter_names = self.model.param_names[2:] # Be verbose ############ if self.verbose: # Need to flush stdout to work with batch jobs/slurm print( "Initializing SNComso model parameters with", self.parameter_values, flush=True, ) except Exception as e: raise_error(e)
import argparse from collections import OrderedDict import numpy as np import sncosmo delim = 61 * "-" # test data ndata = 100 # make divisible by 4! dates = np.linspace(-15., 40., ndata) bands = np.array((ndata // 4) * ['desg', 'desr', 'desi', 'sdssg']) niter = 100 # models f99dust = sncosmo.F99Dust(3.1) models = OrderedDict([('salt2', sncosmo.Model(source='salt2')), ('hsiao', sncosmo.Model(source='hsiao')), ('salt2+f99dust', sncosmo.Model(source='salt2', effects=[f99dust], effect_names=['mw'], effect_frames=['obs'])), ('hsiao+f99dust', sncosmo.Model(source='hsiao', effects=[f99dust], effect_names=['mw'], effect_frames=['obs']))]) print("\nbandflux(band_array, time_array) [4 des bands]:") print(delim) print("Model n=1 n=10 n=100")
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")
def mksedplot(z=1.8, Av=0, color='k'): """ make a set of plots showing the SN Ia SED (Hsiao template) at various redshifts, with bandpasses overlaid. :return: """ import numpy as np import sncosmo from sncosmohst import hstbandpasses, ccsnmodels from matplotlib import pyplot as pl from matplotlib import ticker from pytools import plotsetup from scipy import interpolate as scint # load the O'Donnell 1994 dust model dust = sncosmo.OD94Dust() snIa = sncosmo.Model(source='hsiao', effects=[dust], effect_names=['host'], effect_frames=['rest']) snIa.set(z=z, t0=0, hostr_v=3.1, hostebv=Av / 3.1) snwave = np.arange(6000., 20000., 10.) snflux = snIa.flux(0, snwave) snwave = snwave / 10000. snflux = 0.5 * snflux / snflux.max() pl.plot(snwave, snflux, color=color, ls='-') f105w = sncosmo.get_bandpass('wfc3f105w') f098m = sncosmo.get_bandpass('wfc3f098m') f127m = sncosmo.get_bandpass('wfc3f127m') f139m = sncosmo.get_bandpass('wfc3f139m') f153m = sncosmo.get_bandpass('wfc3f153m') wf127m = f127m.wave / 10000. wf139m = f139m.wave / 10000. wf153m = f153m.wave / 10000. pl.plot(wf127m, f127m.trans, color='darkmagenta', ls='-') pl.plot(wf139m, f139m.trans, color='teal', ls='-') pl.plot(wf153m, f153m.trans, color='darkorange', ls='-') intf127m = scint.interp1d(wf127m, f127m.trans, bounds_error=False, fill_value=0) overlap = np.min([snflux, intf127m(snwave)], axis=0) pl.fill_between(snwave, np.zeros(len(snwave)), overlap, color='darkmagenta') intf139m = scint.interp1d(wf139m, f139m.trans, bounds_error=False, fill_value=0) overlap = np.min([snflux, intf139m(snwave)], axis=0) pl.fill_between(snwave, np.zeros(len(snwave)), overlap, color='teal') intf153m = scint.interp1d(wf153m, f153m.trans, bounds_error=False, fill_value=0) overlap = np.min([snflux, intf153m(snwave)], axis=0) pl.fill_between(snwave, np.zeros(len(snwave)), overlap, color='darkorange')
def mkExtinctionDemoFigSmall(z=2.0): """ make a set of plots showing the SN Ia SED (Hsiao template) at three extinction values, with bandpasses overlaid. :return: """ import numpy as np import sncosmo # from sncosmost import hstbandpasses, ccsnmodels from matplotlib import rc rc('text', usetex=True) rc('text.latex', preamble='\usepackage[usenames]{xcolor}') from matplotlib import pyplot as pl from matplotlib import ticker from pytools import plotsetup from scipy import interpolate as scint fig = plotsetup.fullpaperfig(1, [8, 3]) # load the O'Donnell 1994 dust model dust = sncosmo.OD94Dust() snIa = sncosmo.Model(source='hsiao', effects=[dust], effect_names=['host'], effect_frames=['rest']) ax1 = pl.gca() f127m = sncosmo.get_bandpass('f127m') f139m = sncosmo.get_bandpass('f139m') f153m = sncosmo.get_bandpass('f153m') f125w = sncosmo.get_bandpass('f125w') f140w = sncosmo.get_bandpass('f140w') f160w = sncosmo.get_bandpass('f160w') wf127m = f127m.wave / 10000. wf139m = f139m.wave / 10000. wf153m = f153m.wave / 10000. wf125w = f125w.wave / 10000. wf140w = f140w.wave / 10000. wf160w = f160w.wave / 10000. # ax2 = ax1.twinx() ax2 = ax1 ax2.plot(wf127m, f127m.trans, color='darkmagenta', ls='-', lw=2) ax2.plot(wf153m, f153m.trans, color='darkorange', ls='-', lw=2) ax2.plot(wf125w, f125w.trans, color='darkmagenta', ls='--', lw=2) ax2.plot(wf160w, f160w.trans, color='darkorange', ls='--', lw=2) intf127m = scint.interp1d(wf127m, f127m.trans, bounds_error=False, fill_value=0) intf153m = scint.interp1d(wf153m, f153m.trans, bounds_error=False, fill_value=0) colorlist1, colorlist2 = [], [] for Av, ls, alpha in zip([2, 1, 0], [':', '--', '-'], [0.1, 0.3, 0.5]): snIa.set(z=z, t0=0, hostr_v=3.1, hostebv=Av / 3.1) colorlist1.append( snIa.bandmag('f127m', 'ab', 0) - snIa.bandmag('f125w', 'ab', 0)) colorlist2.append( snIa.bandmag('f153m', 'ab', 0) - snIa.bandmag('f160w', 'ab', 0)) snwave = np.arange(6000., 20000., 10.) snflux = snIa.flux(0, snwave) snwave = snwave / 10000. snflux = 0.12 * snflux / snflux[400] ax1.plot(snwave, snflux, color='k', ls=ls, lw=1, label='%.1f' % Av) overlap127 = np.min([snflux, intf127m(snwave)], axis=0) ax2.fill_between(snwave, np.zeros(len(snwave)), overlap127, color='darkmagenta', alpha=alpha) overlap153 = np.min([snflux, intf153m(snwave)], axis=0) pl.fill_between(snwave, np.zeros(len(snwave)), overlap153, color='darkorange', alpha=alpha) ax1.legend(loc='upper left', bbox_to_anchor=(0.0, 0.9), frameon=False, fontsize=11) ax1.text(0.08, 0.88, 'A$_V$', transform=ax1.transAxes, ha='left', va='bottom', fontsize=11) ax1.text(0.13, 0.88, '$\Delta$m$_{127}$', color='darkmagenta', transform=ax1.transAxes, ha='left', va='bottom', fontsize=11) ax1.text(0.23, 0.88, '$\Delta$m$_{153}$', color='darkorange', transform=ax1.transAxes, ha='left', va='bottom', fontsize=11) ax1.text(0.14, 0.78, '%.3f' % colorlist1[0], color='darkmagenta', transform=ax1.transAxes, ha='left', va='bottom', fontsize=11) ax1.text(0.23, 0.78, '%.3f' % colorlist2[0], color='darkorange', transform=ax1.transAxes, ha='left', va='bottom', fontsize=11) ax1.text(0.14, 0.68, '%.3f' % colorlist1[1], color='darkmagenta', transform=ax1.transAxes, ha='left', va='bottom', fontsize=11) ax1.text(0.23, 0.68, '%.3f' % colorlist2[1], color='darkorange', transform=ax1.transAxes, ha='left', va='bottom', fontsize=11) ax1.text(0.14, 0.58, '%.3f' % colorlist1[2], color='darkmagenta', transform=ax1.transAxes, ha='left', va='bottom', fontsize=11) ax1.text(0.23, 0.58, '%.3f' % colorlist2[2], color='darkorange', transform=ax1.transAxes, ha='left', va='bottom', fontsize=11) # title=+ # '\\textcolor{DarlMagenta}{W}' + # '\\textcolor{F153M-F160W')#, handlelength=0.5, numpoints=3) # ax1.text( 0.15,0.95,,ha='left',va='bottom') ax1.yaxis.set_major_locator(ticker.MultipleLocator(0.1)) ax1.yaxis.set_minor_locator(ticker.MultipleLocator(0.05)) ax1.xaxis.set_major_locator(ticker.MultipleLocator(0.2)) ax1.xaxis.set_minor_locator(ticker.MultipleLocator(0.1)) ax1.set_xlabel('wavelength ($\mu$m)') ax1.set_ylabel('SN Flux or Filter Transmission\n (arbitrary units)') ax1.set_xlim(0.6, 2.0) ax1.set_ylim(0.0, 0.7) ax1.set_yticklabels([]) ax1.text(1.27, 0.6, 'F127M,F125W', color='darkmagenta', fontsize=9, ha='center', va='center') ax1.text(1.53, 0.6, 'F153M,F160W', color='darkorange', fontsize=9, ha='center', va='center') fig.subplots_adjust(left=0.12, right=0.95, bottom=0.18, top=0.92)
z, lensz, it0 = 0.409, 0.216, 57651.2 # Galactic dust model, assuming that the photometry has not been # corrected for MW extinction. dust = sncosmo.CCM89Dust() ########################################################################### # LIGHTCURVE MODEL # setup Hsiao SN Ia stretch-model p, w, f = sncosmo.read_griddata_fits( '/usr/local/lib/python2.7/dist-packages/snpy/typeIa/Hsiao_SED_V3.fits') mHs = sncosmo.StretchSource(p, w, f, name='hsiao-stretch') mH = sncosmo.Model(source=mHs, effects=[dust, dust, dust], effect_names=['mw', 'host', 'lens'], effect_frames=['obs', 'rest', 'free']) ref = copy.copy(mH) mH.set(mwr_v=3.1) mH.set(z=z, lensz=lensz) mH.set(t0=it0) # Set the initial model, construct a MI_model with it mH.set(amplitude=3.e-8, hostebv=0.1, hostr_v=3.1, lensr_v=2.0) # Setup model based on SNoopy fit for 16geu #geu_source = sn16geu(it0,1.e+9) ############################################################################## if __name__ == '__main__': # The parameters that are varied in the fit
def __init__(self, ra, dec, z, t0, c=0, x1=0, peakAbsMagBesselB=-19.0906, model='salt2-extended', version='1.0', sn_type='Ia', mwdust=True): self.radeg = ra self.decdeg = dec self.z = z self.t0 = t0 self.c = c self.x1 = x1 self.peakAbsMagBesselB = peakAbsMagBesselB self.model = model self.version = version self.sn_type = sn_type self.id_SED = '' self.cosmology = cosmology.WMAP9 self.cosmology = FlatLambdaCDM(H0=70, Om0=0.25) astropy_cosmo = FlatLambdaCDM(H0=self.cosmology.H0, Om0=self.cosmology.Om0) self.lumidist = astropy_cosmo.luminosity_distance(self.z).value * 1.e6 #print 'SN Lumidist',self.lumidist,self.cosmology.H0,self.cosmology.Om0 #print 'sntype',self.sn_type dust = sncosmo.OD94Dust() self.lsstmwebv = EBVbase() self.ebvofMW = self.lsstmwebv.calculateEbv( equatorialCoordinates=np.array([[np.radians(self.radeg)], [np.radians(self.decdeg)]]))[0] if self.sn_type == 'Ia': if version == '': source = sncosmo.get_source(self.model) else: source = sncosmo.get_source(self.model, version=self.version) self.SN = sncosmo.Model(source=source, effects=[dust, dust], effect_names=['host', 'mw'], effect_frames=['rest', 'obs']) #self.SN=sncosmo.Model(source=self.source) #self.SN=sncosmo.Model(source=self.source) #print 'blabla',self.SN.minwave(),self.SN.maxwave() lowrange = -30. highrange = 50. self.SN.set(z=self.z) self.SN.set(t0=self.t0) self.SN.set(c=self.c) self.SN.set(x1=self.x1) self.SN.set_source_peakabsmag(self.peakAbsMagBesselB, 'bessellB', 'vega', cosmo=self.cosmology) #self.SN.set(mwebv=self.ebvofMW) #self.SN.set(mwebv=0.) else: self.Fill_nonIa_ID() resu = self.Get_nonIa_Template_ID() #id_SED='SDSS-018109' #id_SED='SDSS-018457' """ if id_SED != None: self.id_SED=id_SED print 'tagged',self.id_SED #self.SED_all_phases=self.Load_SED('NON1A/'+id_SED+'.SED') phase, wave, values = read_griddata_ascii('NON1A/'+id_SED+'.SED') print 'min max',np.min(wave),np.max(wave) self.SED_template = Spline2d(phase, wave, values, kx=2, ky=2) """ self.model = thename = resu[0] self.version = resu[1] #print 'the choice',resu[0],resu[1] """ thename='snana-2007kw' theversion='1.0' """ if thename != None: #print 'Getting the source' source = sncosmo.get_source(self.model, self.version) #print 'Getting the model' self.SN = sncosmo.Model(source=source, effects=[dust, dust], effect_names=['host', 'mw'], effect_frames=['rest', 'obs']) #print 'Setting z and T0' self.SN.set(z=self.z) self.SN.set(t0=self.t0) #print 'SN here',self.z,self.t0,thename,theversion self.SN.set_source_peakabsmag(-18., 'bessellB', 'vega', cosmo=self.cosmology) #MW extinction if mwdust: self.SN.set(mwebv=self.ebvofMW) self.SN.set(mwebv=0.) self.model_for_fit()
def SNMC(df, name, window=652 / 365, scale=1e6): """ df should be the full data frame, with properties of all the targets it will be indexed with name for single target by 'our survey name' inside the function Using the SN rates with window of time to generate N SNe, [N/yr]*1.8 yr The window ~ final - first observation from read_slate """ avg_NIa = np.mean(df['NIa']) avg_eNIa = np.mean(df['e_NIa']) avg_Ncc = np.mean(df['Ncc']) avg_eNcc = np.mean(df['e_Ncc']) targ = df[df[' Our Survey Name '] == name] zS, mu = targ['zS'], targ['mu'] NIa = targ['NIa'] Ncc = targ['Ncc'] e_NIa = targ['e_NIa'] e_Ncc = targ['e_Ncc'] """ if pd.isna(NIa): # use the avg if rate not available, should only apply to SWELLSJ0841+3824 print('using avg SN rates for',name) NIa,e_NIa = avg_NIa,avg_eNIa """ # scale up rates to large enough numbers to get multiple models to plant NIa *= scale e_NIa *= scale Ncc *= scale e_Ncc *= scale # select a rate NIa = np.random.normal(NIa, e_NIa) if NIa < 0: NIa = 0 # use window to get number from rates if type(window) == dict: window = window['window'] window = window.days / 365 nIa = NIa * window # NIa [/yr], window ~ 652 days, n [expected number of Ia in window] ncc = Ncc * window # dusts """ for a review of dust http://www.astronomy.ohio-state.edu/~pogge/Ast871/Notes/Dust.pdf Observationally, RV ranges between 2 and 6, but most often one finds the interstellar extinction law assumed by people as adopting one of two “typical” values for RV: RV=3.1, typical of the Diffuse ISM. RV=5, typical of dense (molecular) clouds. """ mwdust = sncosmo.CCM89Dust() mwdust.set(ebv=0, r_v=3.1) # these are the defaults of CCM89Dust() hostdust = sncosmo.CCM89Dust() hostdust.set(ebv=0, r_v=3.1) import sfdmap dustmap = sfdmap.SFDMap( '/home/oconnorf/miniconda/lib/python3.7/site-packages/mwdust/dust_maps' ) # using Schlegel, Finkbeiner & Davis (1998) MW dust map to set EBV # get ra and dec of the SN ~ target ra = df['ra'] dec = df['dec'] print(ra, dec) #ra,dec=0.0,0.0 ebv = dustmap.ebv(ra, dec) mwdust.set(ebv=ebv) # Luminosity Functions: Goldstein 2019 Table 1, MB Vega, https://arxiv.org/pdf/1809.10147.pdf band, sys = 'bessellb', 'vega' MIa, sigmaIa = -19.23, 0.1 MIIp, sigmaIIp = -16.9, 1.12 MIIL, sigmaIIL = -17.46, 0.38 MIIn, sigmaIIn = -19.05, 0.5 MIbc, sigmaIbc = -17.51, 0.74 # Luminosity Functions: Converted to AB Mag System, http://www.astronomy.ohio-state.edu/~martini/usefuldata.html, Blanton et al. 2007 # B-Band m_AB - m_Vega ~ -0.09 band, sys = 'bessellb', 'ab' dm = -0.09 MIa += dm MIIp += dm MIIL += dm MIIn += dm MIbc += dm # fractions core collapse: Eldridge 2013, https://arxiv.org/abs/1301.1975 # Volume limited ~ 30 Mpc couple hundred transients in the sample fracIb, fracIc, fracIIp, fracIIL, fracIIn, fracIIb, fracIIpec = 0.09, 0.17, 0.55, 0.03, .024, 0.121, 0.01 # my combinations of eldridges subtypes to goldsteins fracIbc = fracIb + fracIc fracIIL = fracIIL + fracIIpec + fracIIb # if you want the check on CC fractions fracs = [fracIIp, fracIIL, fracIIn, fracIbc] # np.sum(fracs) """ Another possible source for fractions... Fractions core collapse: Smith 2010, https://arxiv.org/pdf/1006.3899.pdf Volume limited ~ 60 Mpc sample from LOSS """ # Stretch and Color Ia parameter distributions c = np.linspace(-0.3, 0.5, 1000) cdist = [f_asymmGauss(ci, -0.054, .043, 0.101) for ci in c] dc = (max(c) - min(c)) / len(c) cps = np.array(cdist) * dc tmp = (1 - np.sum(cps)) / len(c) cps = [i + tmp for i in cps] x1 = np.linspace(-3.0, 2.0, 1000) x1dist = [f_asymmGauss(xi, 0.973, 1.472, 0.222) for xi in x1] dx1 = (max(x1) - min(x1)) / len(x1) x1ps = np.array(x1dist) * dx1 tmp = (1 - np.sum(x1ps)) / len(x1) x1ps = [i + tmp for i in x1ps] # The models Iamodels = [] CCmodels = [] IIpmodels, IILmodels, IInmodels, Ibcmodels = [], [], [], [] # The mags if want to see those (without magnification) magIas = [] magIIps, magIILs, magIIns, magIbcs = [], [], [], [] cs, x1s = [], [] for i in range(int(nIa)): model = sncosmo.Model(source='salt2-extended', effects=[hostdust, mwdust], effect_names=['host', 'mw'], effect_frames=['rest', 'obs']) # Ia magIa = np.random.normal(MIa, sigmaIa) magIas.append(magIa) # lensing magnification mabs = magIa - 2.5 * np.log10(mu) t0 = np.random.uniform(0, int(window * 365)) # stretch list_of_candidates, number_of_items_to_pick, probability_distribution = x1, 1, x1ps x1draw = choice(list_of_candidates, number_of_items_to_pick, p=probability_distribution) x1s.append(x1draw) # color list_of_candidates, number_of_items_to_pick, probability_distribution = c, 1, cps cdraw = choice(list_of_candidates, number_of_items_to_pick, p=probability_distribution) cs.append(cdraw) # set the values model.set(z=zS, t0=t0, c=cdraw, x1=x1draw) model.set_source_peakabsmag(mabs, band, sys) Iamodels.append(model) for i in range(int(fracIIp * ncc)): model = sncosmo.Model(source='s11-2005lc', effects=[hostdust, mwdust], effect_names=['host', 'mw'], effect_frames=['rest', 'obs']) # IIp magIIp = np.random.normal(MIIp, sigmaIIp) magIIps.append(magIIp) mabs = magIIp - 2.5 * np.log10(mu) t0 = np.random.uniform(0, int(window * 365)) model.set(z=zS, t0=t0) model.set_source_peakabsmag(mabs, band, sys) IIpmodels.append(model) CCmodels.append(IIpmodels) for i in range(int(fracIIL * ncc)): model = sncosmo.Model(source='nugent-sn2l', effects=[hostdust, mwdust], effect_names=['host', 'mw'], effect_frames=['rest', 'obs']) # IIL magIIL = np.random.normal(MIIL, sigmaIIL) magIILs.append(magIIL) mabs = magIIL - 2.5 * np.log10(mu) t0 = np.random.uniform(0, int(window * 365)) model.set(z=zS, t0=t0) model.set_source_peakabsmag(mabs, band, sys) IILmodels.append(model) CCmodels.append(IILmodels) for i in range(int(fracIIn * ncc)): model = sncosmo.Model(source='nugent-sn2n', effects=[hostdust, mwdust], effect_names=['host', 'mw'], effect_frames=['rest', 'obs']) # IIn magIIn = np.random.normal(MIIn, sigmaIIn) magIIns.append(magIIn) mabs = magIIn - 2.5 * np.log10(mu) t0 = np.random.uniform(0, int(window * 365)) model.set(z=zS, t0=t0) model.set_source_peakabsmag(mabs, band, sys) IInmodels.append(model) CCmodels.append(IInmodels) for i in range(int(fracIbc * ncc)): model = sncosmo.Model(source='nugent-sn1bc', effects=[hostdust, mwdust], effect_names=['host', 'mw'], effect_frames=['rest', 'obs']) # Ibc magIbc = np.random.normal(MIbc, sigmaIbc) magIbcs.append(magIbc) mabs = magIbc - 2.5 * np.log10(mu) t0 = np.random.uniform(0, int(window * 365)) model.set(z=zS, t0=t0) model.set_source_peakabsmag(mabs, band, sys) Ibcmodels.append(model) CCmodels.append(Ibcmodels) return [Iamodels, CCmodels]
def _extrapolatesed(sedfile, newsedfile, color, table, time, modColor, bands, zpsys, bandsDone, UVoverwrite, IRoverwrite, niter=50): """ (Private) Intermediate sed extrapolation function. Interpolate the given transmission function to the wavestep of the SED, then get the area in the V band for color calculation and run the extrapolation algorithm. """ dlist, wlist, flist = _getsed( sedfile) #first time this is read in, should be in ergs/s/cm^2/AA i = 0 while dlist[i][0] < time[0]: i += 1 dlist = dlist[i:] wlist = wlist[i:] flist = flist[i:] i = -1 while dlist[i][0] > time[-1]: i -= 1 if i != -1: dlist = dlist[:i + 1] wlist = wlist[:i + 1] flist = flist[:i + 1] blue = color[0] red = color[-1] bWave = bands[blue].wave bTrans = bands[blue].trans rWave = bands[red].wave rTrans = bands[red].trans bInterpFunc = scint.interp1d(bWave, bTrans) rInterpFunc = scint.interp1d(rWave, rTrans) cInterpFunc = scint.interp1d(time, modColor) tempTime = [x[0] for x in dlist] colorData = cInterpFunc(tempTime) sed = createSNSED( sedfile, rescale=False) #now the original sed is in ergs/s/cm^2/AA model = sncosmo.Model(sed) #out = open( newsedfile, 'wb' ) log = open('./error.log', 'wb') def _extrap_helper(wave1, interpFunc1, wave2, interpFunc2, known, currentPhase): area = model.bandmag(bands[known], zpsys, currentPhase) val = int(math.ceil(wave2[0] / wavestep)) * wavestep val2 = int(math.floor(wave2[-1] / wavestep)) * wavestep wave = arange(val, val2 + 1, wavestep) trans = interpFunc2(wave) return (wave, trans, area) UV = False IR = False finalF = [] for i in range(len(dlist)): d, w, f = dlist[i], wlist[i], flist[i] wavestep = w[1] - w[0] if bands[blue].wave_eff <= _UVrightBound: bWave, bTrans, rArea = _extrap_helper(rWave, rInterpFunc, bWave, bInterpFunc, red, d[0]) wnew, fnew = _extrapolate_uv(blue, rArea, colorData[i], bTrans, bWave, f, w, niter, log, i, bands, zpsys, UVoverwrite) UV = True elif bands[red].wave_eff >= _IRleftBound: rWave, rTrans, bArea = _extrap_helper(bWave, bInterpFunc, rWave, rInterpFunc, blue, d[0]) wnew, fnew = _extrapolate_ir(red, bArea, colorData[i], rTrans, rWave, d, f, w, niter, log, i, IR, bands, zpsys, model, bandsDone, IRoverwrite) IR = True else: raise RuntimeError( "You supplied a color that does not support extrapolation to the IR or UV!" ) finalF.append(fnew) sncosmo.write_griddata_ascii(array([x[0] for x in dlist]), array(wnew), array(finalF), newsedfile) #for j in range( len( wnew ) ) : # fout.write("%5.1f %10i %12.7e \n"%( d[0], wnew[j], fnew[j] )) #fout.close() #log.close() return (UV, IR)
import sncosmo import matplotlib.pyplot as plt import numpy as np model = sncosmo.Model('hsiao') model.set(z=1.0, t0=1.) model.set_source_peakabsmag(-19,'besselli','ab') timeax=np.linspace(-10,100,200) abg=model.bandflux(['sdssg'], timeax, zp=25, zpsys='ab') abr=model.bandflux(['sdssr'], timeax, zp=25, zpsys='ab') abi=model.bandflux(['sdssi'], timeax, zp=25, zpsys='ab') ax=plt.subplot(2,2,1) ax.plot(timeax,abg) ax.plot(timeax,abr) ax.plot(timeax,abi) plt.xlabel('Time in days') plt.ylabel('Flux in counts') plt.show()
def extendCC(colorTable, colorCurveDict, snType, outFileLoc='.', bandDict=_filters, colorExtreme='median', colors=None, zpsys='AB', sedlist=None, showplots=None, verbose=True, UVoverwrite=False, IRoverwrite=True, medianColor=None, colorVar=None, specList=None, specName=None): """ Function used at top level to extend a core-collapse SED. :param colorTable: Colortable made by colorCalc.curveToColor :type colorTable: astropy.Table :param colorCurveDict: Dictionary of color curves, such as made by fitColorCurve :type colorCurveDict: dict :param snType: SN classification for SED(s) :type snType: str :param outFileLoc: Place you want the new SED to be saved, default current directory :type outFileLoc: str,optional :param bandDict: sncosmo bandpass for each band used in the fitting/table generation :type bandDict: dict,optional :param colorExtreme: 'blue,'median','red' describes which curve extreme to use :type colorExtreme: str,optional :param colors: Colors you would like to use for extrapolation :type colors: list or None,optional :param zpsys: Magnitude system :type zpsys: str,optional :param sedlist: list of SEDs to extrapolate, if None then uses all SEDs in SNDATA_ROOT :type sedlist: list or None,optional :param showplots: If you would like to see plots as they're extrapolated :type showplots: Boolean,optional :param verbose: Printing on? :type verbose: Boolean, optional :param UVoverwrite: Whether you would like to overwrite existing UV data :type UVoverwrite: Boolean,options :param IRoverwrite: Whether you would like to overwrite existing IR data :type IRoverwrite: Boolean,optional :param specList: A list of spectra to use in an average :type specList: list, optional :param specName: The name of a spectrum to use. :type specName: str, optional :returns: Saves extrapolated SED to outFileLoc, and returns an sncosmo.Source SED from the extrapolated timeseries. """ colorTable = _standardize(colorTable) if not isinstance(colorTable, Table): raise RuntimeError('Colors argument must be an astropy table.') for band in bandDict: if not isinstance(bandDict[band], sncosmo.Bandpass): bandDict = _bandCheck(bandDict, band) if not colors: print("No extrapolation colors defined, assuming: ", colorCurveDict.keys()) colors = [x for x in colorCurveDict.keys() if 'U-' in x] + [ x for x in colorCurveDict.keys() if '-J' in x ] + [x for x in colorCurveDict.keys() if '-H' in x ] + [x for x in colorCurveDict.keys() if '-K' in x] else: tempColors = [x for x in colors if 'U-' in x] + [ x for x in colors if '-J' in x ] + [x for x in colors if '-H' in x] + [x for x in colors if '-K' in x] if len(tempColors) > 4: raise RuntimeError("Only can do 4 colors!") bands = append([col[0] for col in colors], [col[-1] for col in colors]) for band in _filters.keys(): if band not in bandDict.keys() and band in bands: bandDict[band] = sncosmo.get_bandpass(_filters[band]) if not sedlist: sedlist = glob.glob(os.path.join(sndataroot, "snsed", "NON1A", "*.SED")) else: sedlist = [sedlist] if not isinstance(sedlist, (tuple, list)) else sedlist sedlist = [ os.path.join(sndataroot, "snsed", "NON1A", sed) for sed in sedlist ] returnList = [] for sedfile in sedlist: origWave = _getWave(sedfile) VRColor = (sncosmo.Model(createSNSED(sedfile)).color( 'bessellv', 'sdss::r', zpsys, 0)) if VRColor >= (medianColor + colorVar): colorExtreme = 'red' elif VRColor <= (medianColor - colorVar): colorExtreme = 'blue' else: colorExtreme = 'median' newsedfile = os.path.join(outFileLoc, os.path.basename(sedfile)) if verbose: print("EXTRAPOLATING %s" % os.path.basename(newsedfile)) once = False boundUV = False boundIR = False bandsDone = [] for color in colors: if verbose: print(' Running %s extrapolation.' % color) if color[0] not in bandDict.keys(): raise RuntimeError( 'Band "%s" defined in color "%s", but band is not defined.' ) if color[-1] not in bandDict.keys(): raise RuntimeError( 'Band "%s" defined in color "%s", but band is not defined.' ) extremeColors = dict([]) extremeColors['blue'], extremeColors['median'], extremeColors[ 'red'] = _getExtremes(colorCurveDict[color]['time'], colorCurveDict[color][color], colorTable, color) tempMask = colorTable[color].mask colorTable[color].mask = tempMask if once: sedfile = newsedfile tempTable = colorTable[~colorTable[color].mask] UV, IR = _extrapolatesed(sedfile, newsedfile, color, tempTable, colorCurveDict[color]['time'], extremeColors[colorExtreme], bandDict, zpsys, bandsDone, UVoverwrite, IRoverwrite, niter=50) if UV: boundUV = True bandsDone.append(color[0]) if IR: boundIR = True bandsDone.append(color[-1]) once = True _addCCSpec(snType, newsedfile, origWave, specList, specName) if showplots: plotSED(newsedfile, day=showplots) plt.show() plt.close() if boundUV: _boundUVsed(newsedfile) if boundIR: _boundIRsed(newsedfile) if verbose: print(" Done with %s.\a\a\a" % os.path.basename(sedfile)) returnList.append(createSNSED(newsedfile)) if len(returnList) > 1: return returnList return returnList[0]
def __init__(self): #define the model self.model = sncosmo.Model(source="Hsiao")
import numpy as np import sncosmo import matplotlib.pyplot as plt # read in spectrum from snfit, created with phase=0 wave, flux1 = np.loadtxt("src/snfit-2.4.2/src/testspec.dat", unpack=True) z = 0.05 # adjust flux by a^2 for some reason. Why? What is the definition of # "rest frame flux" in snfit? flux1 *= 1. / (1. + z)**2 # evalute sncosmo model with same spec model = sncosmo.Model('salt2', effects=[sncosmo.CCM89Dust()], effect_names=['mw'], effect_frames=['obs']) model.set(z=z) flux2 = model.flux(0., wave) #plt.plot(wave, flux1) #plt.plot(wave, flux2) #plt.show() #plt.clf() #plt.plot(wave, flux2/flux1) # overplot grid wgrid = model.source._wave * (1. + z) #plt.plot(wgrid, np.ones_like(wgrid), ls='none', marker ='o')