Пример #1
1
def showcurve(sn, hml):

    
    source=sncosmo.get_source('s11-2005hm') 
    model=sncosmo.Model(source=source)
    z0 = float(redshifts[sn])
    model.set(z=z0)
    
    numofrows = len(hml[:,1])
    
    #adding flux, fluxerr, mag and magerror columns
    flux = [str(float(hml[i][1])*10**((-27-21.49)/2.5)) for i in range(numofrows)]
    hml=np.column_stack((hml,flux))
    fluxerr = [str(float(hml[i][2])*10**((-27-21.49)/2.5)) for i in range(numofrows)]
    hml=np.column_stack((hml,fluxerr))
    #adding zpsys and filter columns
    ab = ['ab' for i in range(numofrows)]
    hml=np.column_stack((hml,ab))  
    band = ['ptf48r' for i in range(numofrows)]
    hml=np.column_stack((hml,band))
    ''''
    'counts to flux eq',     hml_dat[8][1]*10**((-27-21.49)/2.5)
    'flux to mag eq',     -2.5 *np.log10(flux)-21.49
    'counts to mag eq',     -2.5 *np.log10(hml_dat[8][1]) +27
    '''
    #plt.errorbar([float(i) for i in hml[:,0]], [float(i) for i in hml[:,1]], yerr = [float(i) for i in hml[:,2]], color = 'blue', fmt = 'o')
    #plt.scatter(hml[:,0], hml[:,4], color = 'green')
    
    #plt.show()
    hml_dat=astropy.table.Table(data=hml, names=('mjd', 'counts', 'dcounts', 'zp', 'flux', 'fluxerr', 'zpsys', 'filter'), dtype=('float','float','float','float','float','float','str', 'str'))
    #print hml_dat
    
    mjdlimits = {'09': (55197-365, 55197),'10': (55197, 55562), '11': (55562, 55927), '12': (55927, 56293)}
    res, fitted_model=sncosmo.fit_lc(hml_dat, model, ['t0','amplitude'], nburn=10000, nsamples=50000, bounds={'t0':mjdlimits[sn[:2]]})
    #res, fitted_model=sncosmo.fit_lc(hml_dat, model, ['t0','amplitude'], nburn=10000, nsamples=50000, bounds={'t0':(55830,55960)})
    
   # sncosmo.plot_lc(hml_dat, model=fitted_model, model_label=source.name, errors=res.errors, color='blue', figtext=str(sn+'\n'+'Model name: s11-2005hm'+ '\n'+'Reduced Chi Squared: ')+ str(res.chisq/res.ndof), xfigsize=18)
  #  plt.show()
    
    
    
    hml2 = []
    #print hml
    #print res.parameters[1]
    j = 0; k= 0
    start = float(res.parameters[1])
    peaktime = modellimit(z0)
    for row in hml:
        datestamp = row[0]
        if peaktime + (40.0)*(1+z0) > float(datestamp)-start > peaktime - (20.0)*(1+z0) :
            hml2.append(row)
        if peaktime + (40.0)*(1+z0) > float(datestamp)-start > peaktime:
            j+=1
        if peaktime > float(datestamp)-start> peaktime-(20.0)*(1+z0):
            k+=1  
            
    if j > 0 and k > 0:
        hml2= np.array(hml2)

        ## print 'after', len(hml2)
        hml_dat2=astropy.table.Table(data=hml2, names=('mjd', 'counts', 'dcounts', 'zp', 'flux', 'fluxerr', 'zpsys', 'filter'), dtype=('float','float','float','float','float','float','str', 'str'))
        
        res2, fitted_model2=sncosmo.fit_lc(hml_dat2, model, ['t0','amplitude'], nburn=10000, nsamples=50000, bounds={'t0':mjdlimits[sn[:2]]})

        #plot model
        redchisqaured = res2.chisq/res2.ndof

        #sncosmo.plot_lc(hml_dat2, model=fitted_model2, model_label=source.name, zp = 27.00, errors=res2.errors, color='blue', figtext=str(sn+'\n'+'Model name: s11-2005hm'+ '\n'+'Reduced Chi Squared: ')+ str(res2.chisq/res2.ndof), xfigsize=18)
       # plt.show()
        
        ##print 'Done:', hml2[:,0][0], 'z:',float(res2.parameters[0]), 'Reduced chi^2:', res2.chisq/res2.ndof #'Data points:', len(hml[:,1]),' Type'+types[hml[:,0][0]]+'Model name: '+ source.name,'\n' #'Dof:', res.ndof
        return  redchisqaured, res2.parameters, z0, hml2 #, res.errors['amplitude']]
    else:
        print sn, 'didn\'t make the cut'
Пример #2
0
    def test_wrong_param_names(self):
        """Supplying parameter names that are not part of the model should
        raise an error."""

        # a parameter not in the model
        with pytest.raises(ValueError):
            res, fitmodel = sncosmo.fit_lc(self.data, self.model, ["t0", "not_a_param"])

        # no parameters
        with pytest.raises(ValueError):
            res, fitmodel = sncosmo.fit_lc(self.data, self.model, [])
Пример #3
0
    def test_wrong_param_names(self):
        """Supplying parameter names that are not part of the model should
        raise an error."""

        # a parameter not in the model
        with pytest.raises(ValueError):
            res, fitmodel = sncosmo.fit_lc(self.data, self.model,
                                           ['t0', 'not_a_param'])

        # no parameters
        with pytest.raises(ValueError):
            res, fitmodel = sncosmo.fit_lc(self.data, self.model, [])
Пример #4
0
def get_gaussian_fit(z, t0, x0, x1, c, lc, seed, temp_dir, interped, type="iminuit"):
    model = sncosmo.Model(source='salt2-extended')
    p = {'z': z, 't0': t0, 'x0': x0, 'x1': x1, 'c': c}
    model.set(**p)

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

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

    chain, parameters = add_mu_to_chain(interped, chain, parameters)
    return chain, parameters, res.parameters[1:], res.covariance
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 fitcurve(x, source_name, hml):
    try:
        source=sncosmo.get_source(source_name) 
        model=sncosmo.Model(source=source) 
        
        #adding zpsys and filter columns
        ab=np.zeros(len(hml[:,1]), dtype='|S2')
        for i in range(len(ab)):
            ab[i]='ab'
        hml=np.column_stack((hml,ab))   
        band=np.zeros(len(hml[:,1]), dtype='|S6')
        for i in range(len(band)):
            band[i]='ptf48r'
        hml=np.column_stack((hml,band))
        #fit to model
        z0 = float(spec_z(x[:-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.0001)}, nburn=10000, nsamples=50000)
        
        #The following excludes data points not in the range of the model and data sets with fewer than 4 data points
        limit = modellimit(source_name, x[:-7], res.parameters[1])
        hml2 = []
        for j in range(len(hml[:,1])):
            datapoint = hml [:,1][j]
            if (res.parameters[1]- limit[0])< float(datapoint) < (res.parameters[1]+limit[1]):
                hml2.append(hml[j]) 
        hml2 = np.array(hml2)
        if len(hml2)>3:
            
            return finalfitcurve(x, source_name, hml2) 
               
        
    except ValueError:
        print 'error'  
Пример #7
0
    def fitting(self, sn_name):
        """Fit the light curve for a SN (data as Astropy table) using the model
        SALT2 z and mwebv are setted, it returns the values of the SALT2 parameters
        and their errors. We plot the fit on the notebook.
        """
        self.read(sn_name)
        self.source = sncosmo.get_source('salt2', version='2.4')
        dust = sncosmo.CCM89Dust()
        self.model = sncosmo.Model(source=self.source,
                                   effects=[dust],
                                   effect_names=['mw'],
                                   effect_frames=['obs'])
        self.model.set(mwebv=self.mwebv)
        self.model.set(z=self.zhl)
        #self.model.set(c=0)

        try:
            res, fitted_model = sncosmo.fit_lc(self.data,
                                               self.model,
                                               ['t0', 'x1', 'c', 'x0'],
                                               phase_range=(-15, 45))
            #res, fitted_model = sncosmo.fit_lc(self.data, self.model, ['t0','x1', 'x0'])
            #return res, fitted_model
        except:
            #except RuntimeError:
            print('Fail:', sn_name)
            res, fitted_model = 'fit fail', np.nan

        return res, fitted_model
Пример #8
0
    def fitting_z(self, sn_name):
        """SALT2 fit using SNCOSMO for the SN without => z free parameter of the
        fit
        """
        self.read(sn_name)
        self.source = sncosmo.get_source('salt2', version='2.4')
        dust = sncosmo.CCM89Dust()
        self.model = sncosmo.Model(source=self.source,
                                   effects=[dust],
                                   effect_names=['mw'],
                                   effect_frames=['obs'])
        self.model.set(mwebv=self.mwebv)
        #self.model.set(c=0)

        try:
            results, fit_model = sncosmo.fit_lc(self.data, self.model,
                                                ['t0', 'x1', 'c', 'x0'])
            #res, fitted_model = sncosmo.fit_lc(self.data, self.model, ['t0','x1', 'x0'])
            #return res, fitted_model
        except:
            #except RuntimeError:
            print('Fail:', sn_name)
            results, fit_model = 'fit fail', np.nan

        return results, fit_model
Пример #9
0
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'
Пример #10
0
	def fit_lc(self, taxis, band):	
		
		data = simlc().reals(taxis, band)[0]
		mod = self.model
		res, fitted_model = sncosmo.fit_lc(data, mod, ['t0'])
		
		return res, fitted_model
Пример #11
0
    def fit_lc(self, taxis, band):

        data = simlc().reals(taxis, band)[0]
        mod = self.model
        res, fitted_model = sncosmo.fit_lc(data, mod, ['t0'])

        return res, fitted_model
Пример #12
0
def get_summary_stats(z, lc, method="emcee", convert_x0_to_mb=True):
    model = sncosmo.Model(source="salt2-extended")
    model.set(z=z)
    if method == "emcee":
        res, fitted_model = sncosmo.mcmc_lc(lc, model, ["t0", "x0", "x1", "c"])
    elif method == "minuit":
        res, fitted_model = sncosmo.fit_lc(lc, model, ["t0", "x0", "x1", "c"])
    else:
        raise ValueError("Method %s not recognised" % method)
    parameters = res.parameters[2:]

    if convert_x0_to_mb:
        determined_parameters = {k: v for k, v in zip(res.param_names, res.parameters)}
        model.set(**determined_parameters)
        mb = fitted_model.source.peakmag("bessellb", "ab")
        parameters = np.array([mb, parameters[1], parameters[2]])
        x0, x1, c = 1, 2, 3
        sigma_mb2 = 5 * np.sqrt(res.covariance[x0, x0]) / (2 * x0 * np.log(10))
        sigma_mbx1 = -5 * res.covariance[x0, x1] / (2 * x0 * np.log(10))
        sigma_mbc = -5 * res.covariance[x0, c] / (2 * x0 * np.log(10))
        cov = res.covariance
        cov = np.array(
            [
                [sigma_mb2, sigma_mbx1, sigma_mbc],
                [sigma_mbx1, cov[x1, x1], cov[x1, c]],
                [sigma_mbc, cov[x1, c], cov[c, c]],
            ]
        )
    else:
        cov = res.covariance[1:, :][:, 1:]
    return parameters, cov
Пример #13
0
    def fit_all(self, modelcov=False):
        """
        """
        if modelcov:
            # TODO: Implement fitting with modelcov
            # from hacked_sncosmo_fitting import fit_lc_hacked
            raise NotImplementedError("Model covariance coming soon.")

        param0 = self._get_current_model_param_()
        self._derived_properties["raw_fit"] = []
        self._derived_properties["idx_good"] = []

        for k, lc in enumerate(self.lightcurves):
            for pname in [name for name in self.model.param_names if name not in self.fit_param and name != "mwr_v"]:
                if pname not in lc.meta.keys():
                    raise KeyError("Parameter '%s' not in lightcurve meta dict" % pname)
                self.model.set(**{pname: lc.meta[pname]})

            try:
                if modelcov:
                    res, _ = fit_lc_hacked(lc, self.model, self.fit_param)
                else:
                    res, _ = sncosmo.fit_lc(lc, self.model, self.fit_param)

                if res["covariance"] is not None:
                    self._derived_properties["raw_fit"].append(res)
                    self._derived_properties["idx_good"].append(k)
                else:
                    print "Light curve fit #%i failed" % k
            except sncosmo.fitting.DataQualityError:
                print "Light curve fit #%i failed because of data quality" % k
            except RuntimeError:
                print "Light curve fit #%i failed to converge" % k

        self.model.set(**param0)
def finalfitcurve(x, source_name, hml):
    try:
        source = sncosmo.get_source(source_name)
        model = sncosmo.Model(source=source)

        #fit to model
        z0 = float(spec_z(x[:-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.0001)},
                                           nburn=10000,
                                           nsamples=50000)
        #The following excludes data with not enough distribution..
        ##Ensuring at least 2 data points on either side of peak.
        limit = modellimit(source_name, x[:-7], res.parameters[1])
        j = 0
        k = 0
        for i in hml[:, 1]:
            if float(i) > float(limit[2]):
                j += 1
            else:
                k += 1
        if j >= 2 and k >= 2:
            return np.array([
                float(res.chisq / res.ndof), hml[:, 0][0], source_name
            ]), hml  #returns reduced chi squared, sn name and model name.

    except ValueError:
        print 'error'
Пример #15
0
    def get_supernova(self, z, num_obs, tmin, tmax, cosmology, alpha, beta,
                      x0_mean=-19.3, x0_sigma=0.1):
        t0 = np.random.uniform(tmin, tmax)
        ts = np.linspace(t0 - 60, t0 + 60, num_obs)

        times = np.array([[t, t + 0.1, t + 0.2] for t in ts]).flatten()
        bands = [b for t in ts for b in ['desg', 'desr', 'desi']]
        gains = np.ones(times.shape)
        skynoise = 20 * 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')

        mabs = np.random.normal(x0_mean, x0_sigma)
        model.set(z=z)
        x1 = np.random.normal(0., 1.)
        c = np.random.normal(0., 0.2)
        mabs = mabs - alpha * x1 + beta * c
        model.set_source_peakabsmag(mabs, 'bessellb', 'ab', cosmo=cosmology)

        x0 = model.get('x0')

        p = {'z': z,
             't0': t0,
             'x0': x0,
             'x1': x1,
             'c': c
             }

        lcs = sncosmo.realize_lcs(obs, model, [p])
        res, fitted_model = sncosmo.fit_lc(lcs[0], model, ['t0', 'x0', 'x1', 'c'])
        determined_parameters = {k: v for k, v in zip(res.param_names, res.parameters)}
        model.set(**determined_parameters)
        mb = fitted_model.source.peakmag("bessellb", "ab")
        x0_ind = res.vparam_names.index('x0')
        x1_ind = res.vparam_names.index('x1')
        c_ind = res.vparam_names.index('c')

        x0 = res.parameters[res.param_names.index('x0')]
        x1 = res.parameters[res.param_names.index('x1')]
        c = res.parameters[res.param_names.index('c')]
        sigma_mb2 = 5 * np.sqrt(res.covariance[x0_ind, x0_ind]) / (2 * x0 * np.log(10))
        sigma_mbx1 = -5 * res.covariance[x0_ind, x1_ind] / (2 * x0 * np.log(10))
        sigma_mbc = -5 * res.covariance[x0_ind, c_ind] / (2 * x0 * np.log(10))
        covariance = np.array([[sigma_mb2, sigma_mbx1, sigma_mbc],
                              [sigma_mbx1, res.covariance[x1_ind, x1_ind],
                               res.covariance[x1_ind, c_ind]],
                              [sigma_mbc, res.covariance[x1_ind, c_ind],
                               res.covariance[c_ind, c_ind]]])
        icov = np.linalg.inv(covariance)
        return [mb, x1, c, covariance, icov]
Пример #16
0
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)
Пример #17
0
 def fits(self):
     """
     `sncosmo.fit_lc` return which is a tuple to fit results, and sncosmo
     model set to best fit model.
     """
     return sncosmo.fit_lc(self.SNCosmoLC(scattered=True,
                                         nightlyCoadd=True, seed=0), model=self.sncosmoModel,
                     vparam_names=['t0', 'x0', 'x1', 'c'], minsnr=0.01)
Пример #18
0
    def test_fit_lc(self):
        res, fitmodel = sncosmo.fit_lc(self.data, self.model,
                                       ['z', 't0', 'amplitude'],
                                       bounds={'z': (0., 1.0)})

        # set model to true parameters
        self.model.set(**self.params)
        assert_allclose(res.parameters, self.model.parameters, rtol=1.e-3)
Пример #19
0
 def fit_lcs(self):
     self.fit_results = []
     self.fitted_model = []
     for jj, lc in enumerate(self.lcs):
         self.model.set(z=self.totz[jj])
         res, fitted_model = sncosmo.fit_lc(lc[0], self.model,['z','x0', 'x1', 'c','t0'],\
         bounds={'x1':(-3.0, 3.0), 'c':(-0.3,0.3),'z':(0.0,1.2)}, minsnr =self.minSNR)
         self.fit_results.append(res)
         self.fitted_model.append(fitted_model)
Пример #20
0
    def fitmodel(self, modeldir='/Users/rodney/Dropbox/WFIRST/SALT2IR',
                 salt2subdir='salt2ir', fitbands=None):
        """ fit a new model to the data
        :param modeldir:
        :return:
        """
        salt2irmodeldir = os.path.join(modeldir, salt2subdir)
        salt2irsource = sncosmo.models.SALT2Source(
            modeldir=salt2irmodeldir, name=salt2subdir)
        salt2irmodel = sncosmo.Model(source=salt2irsource)

        # limit the fit to specific bands if requested
        if fitbands is None:
            datatofit = self.lcdata
        else:
            if isinstance(fitbands, basestring):
                fitbands = [fitbands]
            itofit = np.array([], dtype=int)
            for fitband in fitbands:
                ithisband = np.where(self.band == fitband)[0]
                itofit = np.append(itofit, ithisband)
            datatofit = self.lcdata[itofit]

        if 'salt2x1' not in self.__dict__:
            print("No SALT2 fitres data available for this SN."
                  " No predefined model")
            self.model_fixedx1c = None
            salt2irmodel.set(z=self.z, x1=0, c=0.1)
        else:
            # evaluate the SALT2 model for the given redshift, x1, c
            salt2irmodel.set(z=self.z, t0=self.TBmax,
                             x1=self.salt2x1, c=self.salt2c)
            salt2irmodel.set_source_peakabsmag(-19.3, 'bessellb','AB')

            # fit the model without allowing x1 and c to vary
            res, model_fixedx1c = sncosmo.fit_lc(
                datatofit, salt2irmodel, ['t0', 'x0'], bounds={})
            self.model_fixedx1c = model_fixedx1c

        # fit the model allowing all parameters to vary
        res, model_fitted = sncosmo.fit_lc(
            datatofit, salt2irmodel, ['t0', 'x0', 'x1', 'c'],
            bounds={'x1':[-3,3], 'c':[-0.5,0.5]})
        self.model_fitted = model_fitted
Пример #21
0
def fitcurve(x, source_name):
    # Get data set needed
    hml = np.load('/home/fcm1g13/Documents/Supernova/CorecollapseLC/ccdata/' +
                  x)
    try:
        # Import model
        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))

        # 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 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 with fewer than 4 data points and not enough distribution..
        j = 0
        k = 0
        for i in hml[:, 1]:
            if float(i) > float(res.parameters[1]):
                j += 1
            else:
                k += 1
        # print hml[:,0][0],k,j
        if j >= 2 and k >= 2:
            if len(hml[:, 1]) > 3:
                #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.close()

                return np.array([
                    float(res.chisq / res.ndof), hml[:, 0][0], source_name
                ])  #returns reduced chi squared, sn name and model name.
            else:
                pass
        else:
            pass

    except ValueError:
        pass
Пример #22
0
    def Fit(self):

        #print self.table_for_fit
        t=self.table_for_fit
        select=t[np.where(np.logical_and(t['flux']/t['fluxerr']>5.,t['flux']>0.))]
        idx=select['band']!='LSST::u'
        select=select[idx]

        if self.z > 0.35:
           idx=select['band']!='LSST::g'
           select=select[idx] 


        for filtre in bands:
            band=sncosmo.Bandpass(self.transmission.atmosphere[filtre].wavelen, self.transmission.atmosphere[filtre].sb, name='LSST::'+filtre,wave_unit=u.nm)
            sncosmo.registry.register(band, force=True)
        
        source=sncosmo.get_source(self.model,version=self.version)
        dust = sncosmo.OD94Dust()
        """
        SN_fit_model=sncosmo.Model(source=source,effects=[dust, dust],
                              effect_names=['host', 'mw'],
                              effect_frames=['rest', 'obs'])
        """
        SN_fit_model=sncosmo.Model(source=source)
        SN_fit_model.set(z=self.z)
        SN_fit_model.set_source_peakabsmag(self.peakAbsMagBesselB, 'bessellB', 'vega',cosmo=self.astropy_cosmo)
        """
        lsstmwebv = EBVbase()
        ebvofMW = lsstmwebv.calculateEbv(
            equatorialCoordinates=np.array([[np.radians(self.radeg)], [np.radians(self.decdeg)]]))[0]
        
        SN_fit_model.set(mwebv=ebvofMW)
        """

        z_sim=self.z
        self.sigma_c=0.
        try:
            res, fitted_model = sncosmo.fit_lc(select, SN_fit_model,['t0', 'x0', 'x1', 'c'],bounds={'z':(z_sim-0.1, z_sim+0.1)})

            self.sigma_c=res['errors']['c']
            """
            print z_sim,self.sigma_c
            if z_sim>=0.409 and z_sim<0.411:
                sncosmo.plot_lc(select, model=fitted_model,color='k',pulls=True,errors=res.errors) 
                
                plt.show()
            """
        except (RuntimeError, TypeError, NameError):
                print 'crashed'
                for sel in select:
                    print sel
                print self.z
                self.Plot_bands(select)
                plt.show()
                print 'crashed'
Пример #23
0
    def test_fit_lc(self):
        """Ensure that fit results match input model parameters (data are
        noise-free)."""
        res, fitmodel = sncosmo.fit_lc(self.data, self.model,
                                       ['z', 't0', 'amplitude'],
                                       bounds={'z': (0., 1.0)})

        # set model to true parameters and compare to fit results.
        self.model.set(**self.params)
        assert_allclose(res.parameters, self.model.parameters, rtol=1.e-3)
Пример #24
0
    def fitmodel(self,
                 modeldir='/Users/rodney/Dropbox/WFIRST/SALT2IR',
                 salt2subdir='salt2ir',
                 useknownx1c=False):
        """ fit a new model to the data
        :param modeldir:
        :return:
        """
        salt2modeldir = os.path.join(modeldir, salt2subdir)
        salt2source = sncosmo.models.SALT2Source(modeldir=salt2modeldir,
                                                 name=salt2subdir)
        salt2model = sncosmo.Model(source=salt2source)

        if useknownx1c:
            if 'salt2x1' not in self.__dict__:
                print(
                    "No SALT2 fitres data available for this SN."
                    " No predefined model")
                self.model_fitted = None
                salt2model.set(z=self.z, x1=0, c=0.1)
            else:
                # evaluate the SALT2 model for the given redshift, x1, c
                salt2model.set(z=self.z,
                               t0=self.TBmax,
                               x1=self.salt2x1,
                               c=self.salt2c)
                salt2model.set_source_peakabsmag(-19.3, 'bessellb', 'AB')

                # fit the model without allowing x1 and c to vary
                res, model_fixedx1c = sncosmo.fit_lc(self.datatofit,
                                                     salt2model, ['t0', 'x0'],
                                                     bounds={})
                self.model_fitted = model_fixedx1c
        else:
            # fit the model allowing all parameters to vary
            res, model_fitted = sncosmo.fit_lc(self.datatofit,
                                               salt2model,
                                               ['t0', 'x0', 'x1', 'c'],
                                               bounds={
                                                   'x1': [-5, 5],
                                                   'c': [-0.5, 1.5]
                                               })
            self.model_fitted = model_fitted
Пример #25
0
def fit_mag(
        model: SNModel,
        light_curves: Collection[Table],
        vparams: List[str],
        bands: Collection[str],
        pwv_arr: Collection[types.Numeric] = None,
        z_arr: Collection[types.Numeric] = None,
        **kwargs
) -> Tuple[Dict[str, np.ndarray], ...]:
    """Determine apparent mag by fitting simulated light-curves

    Returned arrays are shape  (len(pwv_arr), len(z_arr)).

    Args:
        model: The sncosmo model to use when fitting
        light_curves: Array of light-curves to fit
        vparams: Parameters to vary with the fit
        bands: Name of the bands to tabulate magnitudes for
        pwv_arr: Array of PWV values
        z_arr: Array of redshift values
        Any arguments for ``sncosmo.fit_lc``.

    Returns:
        Dictionary with arrays for fitted magnitude at each PWV and redshift
        Dictionary with arrays for fitted parameters at each PWV and redshift
    """

    model = copy(model)

    fitted_mag = {b: [] for b in bands}
    fitted_params = {b: [] for b in bands}
    for lc in light_curves:
        # Use the true light-curve parameters as the initial guess
        lc_parameters = deepcopy(lc.meta)
        lc_parameters.pop('pwv', None)
        lc_parameters.pop('res', None)

        # Fit the model without PWV
        model.update(lc_parameters)
        _, fitted_model = sncosmo.fit_lc(lc, model, vparams, **kwargs)

        for band in bands:
            fitted_mag[band].append(fitted_model.bandmag(band, 'ab', 0))
            fitted_params[band].append(fitted_model.parameters)

    if pwv_arr is not None and z_arr is not None:
        # We could have used a more complicated collection pattern, but reshaping
        # after the fact is simpler.
        shape = (len(pwv_arr), len(z_arr))
        num_params = len(fitted_model.parameters)
        for band in bands:
            fitted_mag[band] = np.reshape(fitted_mag[band], shape)
            fitted_params[band] = np.reshape(fitted_params[band], (*shape, num_params))

    return fitted_mag, fitted_params
Пример #26
0
    def test_fit_lc_spectra(self):
        """Check fit results for a single high-resolution spectrum."""
        self.model.set(**self.start_params)
        res, fitmodel = sncosmo.fit_lc(model=self.model,
                                       spectra=self.bin_spectrum,
                                       vparam_names=['amplitude', 'z', 't0'],
                                       bounds={'z': (0., 0.3)})

        # set model to true parameters and compare to fit results.
        self.model.set(**self.params)
        assert_allclose(res.parameters, self.model.parameters, rtol=1.e-3)
Пример #27
0
    def test_flatten_result(self):
        """Ensure that the flatten_result function works correctly."""
        res, fitmodel = sncosmo.fit_lc(self.data,
                                       self.model, ['amplitude', 'z', 't0'],
                                       bounds={'z': (0., 1.0)})

        flat_result = sncosmo.flatten_result(res)

        # Make sure that we got a dictionary of results back.
        assert isinstance(flat_result, dict)
        assert len(flat_result) > 0
Пример #28
0
    def test_fit_lc_returns_correct_type(self) -> None:
        """Test the fit_lc function returns an ``SNModel`` instance for the fitted model"""

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

        with warnings.catch_warnings():
            warnings.filterwarnings('ignore')
            _, fitted_model = sncosmo.fit_lc(data, model, ['x0'])

        self.assertIsInstance(fitted_model, SNModel)
Пример #29
0
 def fits(self):
     """
     `sncosmo.fit_lc` return which is a tuple to fit results, and sncosmo
     model set to best fit model.
     """
     return sncosmo.fit_lc(self.SNCosmoLC(scattered=True,
                                          nightlyCoadd=True,
                                          seed=0),
                           model=self.sncosmoModel,
                           vparam_names=['t0', 'x0', 'x1', 'c'],
                           minsnr=0.01)
Пример #30
0
    def test_fit_lc(self):
        """Ensure that fit results match input model parameters (data are
        noise-free).

        Pass in parameter names in order different from that stored in
        model; tests parameter re-ordering."""
        res, fitmodel = sncosmo.fit_lc(self.data, self.model, ["amplitude", "z", "t0"], bounds={"z": (0.0, 1.0)})

        # set model to true parameters and compare to fit results.
        self.model.set(**self.params)
        assert_allclose(res.parameters, self.model.parameters, rtol=1.0e-3)
Пример #31
0
    def test_fit_lc_both(self):
        """Check fit results for both spectra and photometry."""
        self.model.set(**self.start_params)
        res, fitmodel = sncosmo.fit_lc(self.photometry,
                                       model=self.model,
                                       spectra=self.bin_spectrum,
                                       vparam_names=['amplitude', 'z', 't0'],
                                       bounds={'z': (0., 0.3)})

        # set model to true parameters and compare to fit results.
        self.model.set(**self.params)
        assert_allclose(res.parameters, self.model.parameters, rtol=1.e-3)
Пример #32
0
    def test_fit_lc(self):
        """Ensure that fit results match input model parameters (data are
        noise-free).

        Pass in parameter names in order different from that stored in
        model; tests parameter re-ordering."""
        res, fitmodel = sncosmo.fit_lc(self.data,
                                       self.model, ['amplitude', 'z', 't0'],
                                       bounds={'z': (0., 1.0)})

        # set model to true parameters and compare to fit results.
        self.model.set(**self.params)
        assert_allclose(res.parameters, self.model.parameters, rtol=1.e-3)
Пример #33
0
 def from_fit(cls, table, values={}, fixed={}, bounds=None):
     """"""
     model = get_saltmodel()
     model.set(**{**values, **fixed})
     parameters = [
         p_ for p_ in ['z', 't0', 'x0', 'x1', 'c'] if p_ not in fixed
     ]
     result, fitted_model = sncosmo.fit_lc(
         table,
         model,
         parameters,  # parameters of model to vary
         bounds=bounds)
     return cls(result=result, fitted_model=fitted_model, data=table)
Пример #34
0
    def __init__(self, data, model, SaltModel):
        self.data = data

        test_data = photdata.photometric_data(deepcopy(data))

        self.model = model
        self.z = self.model.get('z')
        self.bands = np.unique(self.data['band'])
        self.x1_prior = (0, np.sqrt(2))
        self.s_prior = (0, np.sqrt(2))
        self.c_prior = (0, 0.15)
        self.SaltModel = SaltModel
        self.ndim = 5
        self.nwalkers = 100
        self.tmax_guess, self.x0_start = sncosmo.fitting.guess_t0_and_amplitude(
            test_data, self.model, minsnr=3)
        self.tmax_bounds = sncosmo.fitting.t0_bounds(test_data, self.model)

        # try guess tmax ourselves
        t = np.arange(self.tmax_bounds[0], self.tmax_bounds[1] + 1, 1)
        test_chi = 1e20
        test_time = 0
        test_x0 = 0
        x0arr = []

        for time in t:
            params = [self.x0_start, 0, 0, 0, time]
            self.SaltModel.set(t0=time, x0=self.x0_start, x1=0, c=0, z=self.z)
            res, fitted_model = sncosmo.fit_lc(deepcopy(self.data),
                                               self.SaltModel, ['x0'],
                                               guess_amplitude=False,
                                               guess_t0=False,
                                               modelcov=False)
            c = res.chisq

            x0arr.append(res.parameters[2])
            if np.isnan(c) or np.isinf(c):
                continue
            if c < test_chi:
                test_chi = c
                test_time = time
                test_x0 = res.parameters[2]

        self.tmax_guess = test_time
        self.x0_start = test_x0
        self.nest_bounds = {
            't0': self.tmax_bounds,
            'x1': [-4, 4],
            'c': [-1, 1],
            'x0': [np.min(x0arr), np.max(x0arr)]
        }
Пример #35
0
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'
Пример #36
0
def fitcurve(x, source_name):
    # Get data set needed
    hml=np.load('/home/fcm1g13/Documents/Supernova/CorecollapseLC/ccdata/' + x)    
    try:
        # Import model
        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))

        # 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 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 with fewer than 4 data points and not enough distribution..
        j=0; k=0
        for i in hml[:,1]:
            if float(i)>float(res.parameters[1]):
                j+=1
            else:
                k+=1
        # print hml[:,0][0],k,j
        if j>=2 and k>=2:            
            if len(hml[:,1])>3:                                
                #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.close()                
                
                return np.array([float(res.chisq/res.ndof), hml[:,0][0], source_name]) #returns reduced chi squared, sn name and model name. 
            else:
                pass
        else:
            pass
                
    except ValueError:
        pass
Пример #37
0
    def Fit_LC(self,t):

        
        select=t[np.where(np.logical_and(t['flux']/t['fluxerr']>5.,t['flux']>0.))]
        #print 'fit',len(select)
 
        if len(select)>=5:
            #print 'data to be fitted',select
            try:
                
                
                #print 'before fit here'
                #print 'SN parameters',self.SN.SN
                
                #print 'fitting',select
                
                z_sim=self.SN.z
                #print 'hello z',z_sim
                #print 'fit it',val,time.time()-self.thetime
                res, fitted_model = sncosmo.fit_lc(select, self.SN.SN_fit_model,['z', 't0', 'x0', 'x1', 'c'],bounds={'z':(z_sim-0.01, z_sim+0.01)})
                #res, fitted_model = sncosmo.fit_lc(select, self.SN.SN,['t0', 'x0', 'x1', 'c'])
    

                #print 'after fit',res.keys()
                #print res.keys()
                
                """
                print 'after fit'
                print res['parameters'],res['errors']
                """

                mbfit=fitted_model._source.peakmag('bessellb','vega')
                #print 'oooo test',-2.5*np.log10(res['parameters'][2])+10.635,fitted_model.bandmag('bessellb','vega',res['parameters'][1]),mbsim,mbfit,mbsim-mbfit

                """
                sncosmo.plot_lc(t, model=fitted_model,color='k',pulls=False)
                
                plt.show()
                """
                #print 'fitted'
                return res,fitted_model,mbfit,'ok'
            
            except (RuntimeError, TypeError, NameError):
                #print 'crashed'
                return None,None,-1,'crash'

        else:
            return None,None,-1,'Noobs'
Пример #38
0
def dofitSCP0401(datfile='HST_SCP_0401.sncosmo.dat',
                 z=1.713,
                 t0=53080.0,
                 dt0=50.0):

    # TODO : read in the redshift, etc from the header.

    # read in the obs data
    sn = ascii.read(datfile,
                    format='commented_header',
                    header_start=-1,
                    data_start=0)

    # define SALT2 models and set initial guesses for z and t0
    salt2ex = sncosmo.Model(source='salt2-extended')
    salt2ex.source.set_peakmag(0., 'bessellb', 'ab')
    x0_AB0 = salt2ex.get('x0')
    x0_from_mB = lambda m: x0_AB0 * 10**(-0.4 * (m))
    salt2ex.set(z=1.713, t0=53090.0, x0=x0_from_mB(26.14), x1=0.2, c=-0.1)
    # salt2ex.set( z=1.33, t0=56814.6, hostebv=0.05, hostr_v=3.1 )

    # Do a bounded fit :
    #res, fit = sncosmo.fit_lc( sn, salt2ex, ['z','t0','x0','x1','c'],
    #                           bounds={'z':(1.712,1.714),'t0':(t0-dt0,t0+dt0),
    #                                   'x1':(-5.,5.), 'c':(-0.5,3.0) })
    res, fit = sncosmo.fit_lc(sn,
                              salt2ex, ['z', 't0', 'x0'],
                              bounds={
                                  'z': (1.712, 1.714),
                                  't0': (t0 - dt0, t0 + dt0)
                              })

    x0 = fit.get('x0')
    mB = -2.5 * np.log10(x0 / x0_AB0)
    distmod = mB - -19.19  #  MBmodel from Rubin et al 2013
    deltamuLCDM = distmod - dm(z)
    print("mB = %.2f" % mB)
    print("dist.mod. = %.2f" % distmod)
    print("Delta.mu_LCDM = %.2f" % deltamuLCDM)

    chi2 = res.chisq
    ndof = res.ndof
    pval = chisqprob(chi2, ndof)

    print("chi2/dof= %.3f" % (chi2 / float(ndof)))
    print("p-value = %.3f" % pval)

    return (sn, fit, res)
def simple_fit(data, model, vparam_names, **kwargs):
    """Fit light curves using the basic ``fit_lc`` functionality in ``sncosmo``

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

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

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

    return sncosmo.fit_lc(data, model, vparam_names, **kwargs)
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 fitcurve(x, source_name, hml):
    try:
        source = sncosmo.get_source(source_name)
        model = sncosmo.Model(source=source)

        #adding zpsys and filter columns
        ab = np.zeros(len(hml[:, 1]), dtype='|S2')
        for i in range(len(ab)):
            ab[i] = 'ab'
        hml = np.column_stack((hml, ab))
        band = np.zeros(len(hml[:, 1]), dtype='|S6')
        for i in range(len(band)):
            band[i] = 'ptf48r'
        hml = np.column_stack((hml, band))
        #fit to model
        z0 = float(spec_z(x[:-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.0001)},
                                           nburn=10000,
                                           nsamples=50000)

        #The following excludes data points not in the range of the model and data sets with fewer than 4 data points
        limit = modellimit(source_name, x[:-7], res.parameters[1])
        hml2 = []
        for j in range(len(hml[:, 1])):
            datapoint = hml[:, 1][j]
            if (res.parameters[1] - limit[0]) < float(datapoint) < (
                    res.parameters[1] + limit[1]):
                hml2.append(hml[j])
        hml2 = np.array(hml2)
        if len(hml2) > 3:

            return finalfitcurve(x, source_name, hml2)

    except ValueError:
        print 'error'
Пример #42
0
def lcFit(df_main):
    df_main = df_main.rename({'mjd': 'time', 'passband': 'band'}, axis=1)
    df_main['band'] = df_main['band'].map(pb_mapping)
    df_main['zp'] = 25
    df_main['zpsys'] = 'ab'
    data = Table.from_pandas(df_main)

    try:
        res, fitted_model = sncosmo.fit_lc(data,
                                           model, ['t0', 'x0', 'x1', 'c'],
                                           minsnr=3)
    except (RuntimeError, sncosmo.fitting.DataQualityError):
        res = {}
        res['parameters'] = [0, 0, 0, 0, 0]
        res['param_names'] = ['z', 't0', 'x0', 'x1', 'c']
    ret = pd.Series(res['parameters'], index=res['param_names'])
    ret.drop('z', inplace=True)
    ret.drop('t0', inplace=True)

    return ret
Пример #43
0
def SALT2(x):
    lsst_band = {
        0: "lsstu",
        1: "lsstg",
        2: "lsstr",
        3: "lssti",
        4: "lsstz",
        5: "lssty",
    }

    x['passband'].replace(lsst_band, inplace=True)
    x['zp'] = 0
    x['zpsys'] = 'ab'

    x = x.rename(columns={'passband': 'band'})

    x.loc[x['band'] == 'lsstu', 'zp'] = 9.17
    x.loc[x['band'] == 'lsstg', 'zp'] = 50.7
    x.loc[x['band'] == 'lsstr', 'zp'] = 43.7
    x.loc[x['band'] == 'lssti', 'zp'] = 32.36
    x.loc[x['band'] == 'lsstz', 'zp'] = 22.68
    x.loc[x['band'] == 'lssty', 'zp'] = 10.58

    # print(x)
    x = Table.from_pandas(x)

    # magsys = sncosmo.CompositeMagSystem(bands={'lsstu': ('ab', 9.16), 'lsstg': ('ab', 50.7), "lsstr": ('ab', 43.7), "lssti": ('ab', 32.36), "lsstz": ('ab', 22.68), "lssty": ("ab", 10.58)})
    # sncosmo.registry.register(magsys, name="my")

    # print(x)
    try:
        result, fitted_model = sncosmo.fit_lc(
            x,
            model,
            ['z', 't0', 'x0', 'x1', 'c'],  # parameters of model to vary
            bounds={'z': (0.3, 0.7)})  # bounds on parameters (if any)

        print(result)
    except:
        return 0
    return 0
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
Пример #45
0
def test_fit_lc_vs_snfit():
    """Test fit_lc versus snfit result for one SN."""

    # purposefully use CCM dust to match snfit
    model = sncosmo.Model(source='salt2',
                          effects=[sncosmo.CCM89Dust()],
                          effect_names=['mw'],
                          effect_frames=['obs'])

    fname = join(dirname(__file__), "data", "lc-03D4ag.list")

    data = sncosmo.read_lc(fname,
                           format='salt2',
                           read_covmat=True,
                           expand_bands=True)
    model.set(mwebv=data.meta['MWEBV'], z=data.meta['Z_HELIO'])
    result, fitted_model = sncosmo.fit_lc(data,
                                          model, ['t0', 'x0', 'x1', 'c'],
                                          bounds={
                                              'x1': (-3., 3.),
                                              'c': (-0.4, 0.4)
                                          },
                                          modelcov=True,
                                          phase_range=(-15., 45.),
                                          wave_range=(3000., 7000.),
                                          warn=False,
                                          verbose=False)

    print(result)
    assert result.ndof == 25
    assert result.nfit == 3
    assert_allclose(fitted_model['t0'], 52830.9313, atol=0.01, rtol=0.)
    assert_allclose(fitted_model['x0'], 5.6578663e-05, atol=0., rtol=0.005)
    assert_allclose(fitted_model['x1'], 0.937399344, atol=0.005, rtol=0.)
    assert_allclose(fitted_model['c'], -0.0851965244, atol=0.001, rtol=0.)

    # errors
    assert_allclose(result.errors['t0'], 0.0955792638, atol=0., rtol=0.01)
    assert_allclose(result.errors['x0'], 1.52745001e-06, atol=0., rtol=0.01)
    assert_allclose(result.errors['x1'], 0.104657847, atol=0., rtol=0.01)
    assert_allclose(result.errors['c'], 0.0234763446, atol=0., rtol=0.01)
Пример #46
0
def fitcurve(x, source_name, hml):
    try:
        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))

        # 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 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 points not in the range of the model and data sets with fewer than 4 data points
        limit = modellimit(source_name, res.parameters[0], res.parameters[1])
        hml2 = []
        for j in range(len(hml[:, 1])):
            datapoint = hml[:, 1][j]
            if (res.parameters[1] - limit[0]) < float(datapoint) < (
                    res.parameters[1] + limit[1]):
                hml2.append(hml[j])
        hml2 = np.array(hml2)
        if len(hml2) > 3:
            return finalfitcurve(x, source_name, hml2)

    except ValueError:
        pass
Пример #47
0
def dofitSCP0401( datfile='HST_SCP_0401.sncosmo.dat', z=1.713,
                  t0=53080.0, dt0=50.0 ) :

    # TODO : read in the redshift, etc from the header.

    # read in the obs data
    sn = ascii.read( datfile, format='commented_header', header_start=-1, data_start=0 )

    # define SALT2 models and set initial guesses for z and t0
    salt2ex = sncosmo.Model( source='salt2-extended')
    salt2ex.source.set_peakmag( 0., 'bessellb', 'ab' )
    x0_AB0 = salt2ex.get('x0')
    x0_from_mB = lambda m : x0_AB0 * 10**(-0.4*(m) )
    salt2ex.set( z=1.713, t0=53090.0, x0=x0_from_mB(26.14), x1=0.2, c=-0.1 )
    # salt2ex.set( z=1.33, t0=56814.6, hostebv=0.05, hostr_v=3.1 )

    # Do a bounded fit :
    #res, fit = sncosmo.fit_lc( sn, salt2ex, ['z','t0','x0','x1','c'],
    #                           bounds={'z':(1.712,1.714),'t0':(t0-dt0,t0+dt0),
    #                                   'x1':(-5.,5.), 'c':(-0.5,3.0) })
    res, fit = sncosmo.fit_lc( sn, salt2ex, ['z','t0','x0'],
                               bounds={'z':(1.712,1.714),'t0':(t0-dt0,t0+dt0)})

    x0 = fit.get( 'x0' )
    mB = -2.5*np.log10(  x0 / x0_AB0 )
    distmod = mB - -19.19 #  MBmodel from Rubin et al 2013
    deltamuLCDM = distmod - dm(z)
    print( "mB = %.2f"%mB )
    print( "dist.mod. = %.2f"%distmod)
    print( "Delta.mu_LCDM = %.2f"%deltamuLCDM)

    chi2 = res.chisq
    ndof = res.ndof
    pval = chisqprob( chi2, ndof )

    print( "chi2/dof= %.3f"% (chi2/float(ndof) ) )
    print( "p-value = %.3f"% pval )

    return( sn, fit, res )
def finalfitcurve(x, source_name, hml):
    try:
        source=sncosmo.get_source(source_name) 
        model=sncosmo.Model(source=source) 
        
        #fit to model
        z0 = float(spec_z(x[:-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.0001)}, nburn=10000, nsamples=50000)
        #The following excludes data with not enough distribution..
        ##Ensuring at least 2 data points on either side of peak.
        limit = modellimit(source_name, x[:-7], res.parameters[1])
        j=0; k=0
        for i in hml[:,1]:
            if float(i)>float(limit[2]):
                j+=1
            else:
                k+=1
        if j>=2 and k>=2:          
            return np.array([float(res.chisq/res.ndof), hml[:,0][0], source_name]), hml #returns reduced chi squared, sn name and model name. 
           
    except ValueError:
        print 'error'
def fitcurve(x, source_name, hml):
    try:
        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))

        # 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 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 points not in the range of the model and data sets with fewer than 4 data points
        limit = modellimit(source_name, res.parameters[0], res.parameters[1])
        hml2 = []
        for j in range(len(hml[:,1])):
            datapoint = hml [:,1][j]
            if (res.parameters[1]- limit[0])< float(datapoint) < (res.parameters[1]+limit[1]):
                hml2.append(hml[j]) 
        hml2 = np.array(hml2)
        if len(hml2)>3:
            return finalfitcurve(x, source_name, hml2)    
    
    except ValueError:
        pass   
Пример #50
0
def test_fit_lc_vs_snfit():
    """Test fit_lc versus snfit result for one SN."""

    # purposefully use CCM dust to match snfit
    model = sncosmo.Model(source='salt2',
                          effects=[sncosmo.CCM89Dust()],
                          effect_names=['mw'],
                          effect_frames=['obs'])

    fname = join(dirname(__file__), "data", "lc-03D4ag.list")

    data = sncosmo.read_lc(fname, format='salt2', read_covmat=True,
                           expand_bands=True)
    model.set(mwebv=data.meta['MWEBV'], z=data.meta['Z_HELIO'])
    result, fitted_model = sncosmo.fit_lc(
        data, model, ['t0', 'x0', 'x1', 'c'],
        bounds={'x1': (-3., 3.), 'c': (-0.4, 0.4)},
        modelcov=True,
        phase_range=(-15., 45.),
        wave_range=(3000., 7000.),
        warn=False,
        verbose=False)

    print(result)
    assert result.ndof == 25
    assert result.nfit == 3
    assert_allclose(fitted_model['t0'], 52830.9313, atol=0.01, rtol=0.)
    assert_allclose(fitted_model['x0'], 5.6578663e-05, atol=0., rtol=0.005)
    assert_allclose(fitted_model['x1'], 0.937399344, atol=0.005, rtol=0.)
    assert_allclose(fitted_model['c'], -0.0851965244, atol=0.001, rtol=0.)

    # errors
    assert_allclose(result.errors['t0'], 0.0955792638, atol=0., rtol=0.01)
    assert_allclose(result.errors['x0'], 1.52745001e-06, atol=0., rtol=0.01)
    assert_allclose(result.errors['x1'], 0.104657847, atol=0., rtol=0.01)
    assert_allclose(result.errors['c'], 0.0234763446, atol=0., rtol=0.01)
Пример #51
0
        model.set(hostebv=ebv)
        model.set(hostr_v=r_v)
        model.set(t0=lc.meta['t0'])

        # Identify parameters to vary in the fit.
        vparams = filter(lambda x: 'bump' in x or 'slope' in x, model._param_names)
        vparams += ['t0', 's', 'amplitude']

        # Set boundaries on the parameters to vary.
        bounds = {b.name + "_bump_amp":(-1,2) for b in 
                  model.source.bumps}
        bounds['s'] = (0, 3.)

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

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

        # Do MCMC.
        fres, fitmod = sncosmo.mcmc_lc(lc, model, vparams,
                                       bounds=bounds,
                                       nwalkers=200,
                                       nburn=1000,
                                       nsamples=20,
                                       guess_t0=False, guess_amplitude=False)
        samples = fres.samples
Пример #52
0
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()
Пример #53
0
def showcurve(sn, hml, source_name):
    try:
        source = sncosmo.get_source(source_name)
        model = sncosmo.Model(source=source)
        z0 = specs[sn]
        if z0 == "no data":
            # pass
            print sn[:-7], "cannot be plotted"
        else:
            z0 = float(z0)
            model.set(z=z0)
            # print z0

            # adding zpsys and filter columns
            ab = np.zeros(len(hml[:, 1]), dtype="|S2")
            for i in range(len(ab)):
                ab[i] = "ab"
            hml = np.column_stack((hml, ab))
            band = np.zeros(len(hml[:, 1]), dtype="|S6")
            for i in range(len(band)):
                band[i] = "ptf48r"
            hml = np.column_stack((hml, band))

            hml_dat = astropy.table.Table(
                data=hml,
                names=(
                    "ptfname",
                    "time",
                    "magnitude",
                    "mag_err",
                    "flux",
                    "flux_err",
                    "zp_new",
                    "zp",
                    "ra",
                    "dec",
                    "zpsys",
                    "filter",
                ),
                dtype=(
                    "str",
                    "float",
                    "float",
                    "float",
                    "float",
                    "float",
                    "float",
                    "float",
                    "float",
                    "float",
                    "str",
                    "str",
                ),
            )
            res, fitted_model = sncosmo.fit_lc(hml_dat, model, ["t0", "amplitude"], nburn=10000, nsamples=50000)
            ##sncosmo.plot_lc(hml_dat, model=fitted_model, model_label=source.name, errors=res.errors, color='blue', figtext=str(hml[:,0][0]+'\n'+'Model name: nugent-sn1bc'+ '\n'+'Reduced Chi Squared: ')+ str(res.chisq/res.ndof), xfigsize=18)
            ##plt.show()
            zp_new = [hml_dat[i][7] for i in range(len(hml))]

            hml2 = []
            # print hml
            # print res.parameters[1]
            j = 0
            k = 0
            for row in hml:
                datestamp = row[1]
                peaktime = float(res.parameters[1])
                limits = modellimit(z0)
                if peaktime + (40.0) * (1 + z0) > float(datestamp) > peaktime - (15.0) * (1 + z0):
                    hml2.append(row)
                if peaktime + (40.0) * (1 + z0) > float(datestamp) > peaktime:
                    j += 1
                if peaktime > float(datestamp) > peaktime - (15.0) * (1 + z0):
                    k += 1
            if j > 1 and k > 1:
                hml2 = np.array(hml2)

                ##print 'after', len(hml2)
                hml_dat2 = astropy.table.Table(
                    data=hml2,
                    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",
                    ),
                )
                res2, fitted_model2 = sncosmo.fit_lc(hml_dat2, model, ["t0", "amplitude"], nburn=10000, nsamples=50000)

                # plot model
                redchisqaured = res2.chisq / res2.ndof
                if redchisqaured < 1:
                    pass
                # sncosmo.plot_lc(hml_dat2, model=fitted_model2, model_label=source.name, zp = np.mean(zp_new), errors=res2.errors, color='blue', figtext=str(hml[:,0][0]+'\n'+'Model name: nugent-sn1bc'+ '\n'+'Reduced Chi Squared: ')+ str(res2.chisq/res2.ndof), xfigsize=18)
                # plt.show()

                ##print 'Done:', hml2[:,0][0], 'z:',float(res2.parameters[0]), 'Reduced chi^2:', res2.chisq/res2.ndof #'Data points:', len(hml[:,1]),' Type'+types[hml[:,0][0]]+'Model name: '+ source.name,'\n' #'Dof:', res.ndof
                return res2.parameters, np.mean(zp_new), redchisqaured, z0, hml2  # , res.errors['amplitude']]
            else:
                pass
                # return res.parameters, np.mean(zp_new), res.chisq/res.ndof, z0, hml
    except ValueError:
        # pass
        print sn, "cannot be plotted"
Пример #54
0
def do_sncosmo(catalog):
    import warnings
    warnings.filterwarnings("ignore", message="fcn returns Nan")
    warnings.filterwarnings("ignore", message="overflow encountered in power")
    warnings.filterwarnings(
        "ignore", message="Dropping following bands from data:*")
    warnings.filterwarnings("ignore", message="overflow encountered in square")
    warnings.filterwarnings(
        "ignore", message="invalid value encountered in multiply")
    warnings.filterwarnings(
        "ignore", message="overflow encountered in multiply")

    task_str = catalog.get_current_task_str()

    for event in pbar(catalog.entries, task_str):
        catalog.add_entry(event, delete=False)
        if (SUPERNOVA.PHOTOMETRY not in catalog.entries[event]  # or
                # SUPERNOVA.REDSHIFT in catalog.entries[event] or
                # SUPERNOVA.LUM_DIST in catalog.entries[event]
            ):
            catalog.entries[event] = catalog.entries[event].get_stub()
            continue
        photodat = []
        for photo in catalog.entries[event][SUPERNOVA.PHOTOMETRY]:
            if (photo.get(PHOTOMETRY.BAND_SET, '') == 'SDSS' and
                    PHOTOMETRY.TIME in photo and PHOTOMETRY.BAND in photo and
                    PHOTOMETRY.FLUX_DENSITY in photo and
                    PHOTOMETRY.E_FLUX_DENSITY in photo and
                    PHOTOMETRY.UPPER_LIMIT not in photo):
                photodat.append(
                    (float(photo[PHOTOMETRY.TIME]),
                     'sdss' + photo[PHOTOMETRY.BAND].replace("'", ''),
                     float(photo[PHOTOMETRY.FLUX_DENSITY]),
                     float(photo[PHOTOMETRY.E_FLUX_DENSITY]),
                     float(photo[PHOTOMETRY.ZERO_POINT]), 'ab'))
            elif (photo.get(PHOTOMETRY.BAND_SET, '') == 'MegaCam' and
                  PHOTOMETRY.TIME in photo and PHOTOMETRY.BAND in photo and
                  PHOTOMETRY.COUNT_RATE in photo and
                  PHOTOMETRY.E_COUNT_RATE in photo and
                  PHOTOMETRY.UPPER_LIMIT not in photo):
                photodat.append(
                    (float(photo[PHOTOMETRY.TIME]),
                     'sdss' + photo[PHOTOMETRY.BAND].replace("'", ''),
                     float(photo[PHOTOMETRY.COUNT_RATE]),
                     float(photo[PHOTOMETRY.E_COUNT_RATE]),
                     float(photo[PHOTOMETRY.ZERO_POINT]), 'bd17'))
        if len(photodat) < 20:
            catalog.entries[event] = catalog.entries[event].get_stub()
            continue
        table = Table(
            rows=photodat,
            names=('time', 'band', 'flux', 'fluxerr', 'zp', 'zpsys'))
        if (catalog.entries[event].get(SUPERNOVA.CLAIMED_TYPE, [{
                QUANTITY.VALUE: ''
        }])[0][QUANTITY.VALUE] == 'Ia'):
            source = sncosmo.get_source('salt2', version='2.4')
            model = sncosmo.Model(source=source)
            mredchisq = np.inf
            fm = None
            for zmin in np.linspace(0.0, 1.0, 19):
                zmax = zmin + 0.1  # Overlapping intervals
                try:
                    resl, fml = sncosmo.fit_lc(
                        table,
                        model, ['z', 't0', 'x0', 'x1', 'c'],
                        bounds={'z': (zmin, zmax)})
                except RuntimeError:
                    continue
                except sncosmo.fitting.DataQualityError:
                    break
                if resl.ndof < 15:
                    continue
                redchiq = resl.chisq / resl.ndof
                if (redchiq < mredchisq and redchiq < 2.0 and not np.isclose(
                        zmin, fml.get('z'), rtol=1.0e-3) and not np.isclose(
                            zmax, fml.get('z'), rtol=1.e-3)):
                    mredchisq = resl.chisq
                    res, fm = resl, fml

            if fm:
                print(event, res.chisq / res.ndof,
                      fm.get('z'),
                      catalog.entries[event][SUPERNOVA.REDSHIFT][0]['value']
                      if SUPERNOVA.REDSHIFT in catalog.entries[event] else
                      'no redshift')
                # source = catalog.entries[event].add_source(
                #     bibcode='2014A&A...568A..22B')
                # catalog.entries[event].add_quantity(SUPERNOVA.REDSHIFT,
                #                                     str(fm.get('z')), source)
                catalog.journal_entries()
            else:
                catalog.entries[event] = catalog.entries[event].get_stub()

    return
Пример #55
0
            # No objects found so skip this one.
            continue
        objectId = obj.objectId.iloc[0]
        # Get the ForcedSource light curves and select the data based
        # on nominal time range of the model (and exclude the u band
        # data).
        lc = lc_factory.create(objectId)
        data_mask = ((lc.data['bandpass'] != 'lsstu')
                     & (model.mintime() < lc.data['mjd'])
                     & (lc.data['mjd'] < model.maxtime()))
        data = lc.data[data_mask]
        # Fit the data using a redshift bounds of +/- 0.03 centered on
        # the input model value.
        try:
            res, fitted_model = sncosmo.fit_lc(data, model,
                                               'z t0 x0 x1 c'.split(),
                                               bounds=dict(z=(model.z-0.03,
                                                              model.z+0.03)))
            # Stuff everything into the results DataFrame.
            row = [objectId, obj.ra.iloc[0], obj.dec.iloc[0]]
            foo = sncosmo.flatten_result(res)
            for key in 'z t0 x0 x1 c chisq ndof'.split():
                row.append(foo[key])
            row.extend([sourceId, model.ra, model.dec, model.z, model.t0,
                        model.x0, model.x1, model.c, chisq_catsim, ndof_catsim])
            results = results.append(pd.DataFrame(data=[row], columns=columns),
                                     ignore_index=True)

#            sncosmo.plot_lc(data=ref_lc.lightcurve[mask], model=model)
#            sncosmo.plot_lc(data=data, model=fitted_model, errors=res.errors)
            results.to_pickle(outfile)
        except (RuntimeError, sncosmo.fitting.DataQualityError, ValueError):
Пример #56
0
#####################################################################
# 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)
print("model parameters:", result.param_names)
print("best-fit values:", result.parameters)
print("The result contains the following attributes:\n", result.keys())

##################################################################
# The second object returned is a shallow copy of the input model with
Пример #57
0
    print irow
    sys.stdout.flush()
    row = results.iloc[irow]
    objectId = int(row.objectId)
    lc = desc.monitor.LightCurve()
    lc.build_lightcurve_from_db(objectId, **db_info)
    # t0.row may be previous iterations but could also be truth values
    # if thse were fixed, we should not add them to the fit_lc call
    model.set(t0=row.t0, z=row.z)
    dustmap = sncosmo.SFD98Map()
    # ra, dec in degrees from truth/DM evaluation of ra, dec
    ebv = dustmap.get_ebv((row.ra, row.dec))
    model.set(mwebv=ebv)
    date_mask = np.where((model.mintime() < lc.lightcurve['mjd']) &
                         (lc.lightcurve['mjd'] < model.maxtime()) &
                         (lc.lightcurve['bandpass'] != 'lsstu'))
    data = lc.lightcurve[date_mask]
    try:
        res, fitted_model = sncosmo.fit_lc(data, model,
                                           ['z', 't0', 'x0', 'x1', 'c'],
                                           bounds=dict(z=(0.1, 0.4)))

        for par, value in zip(res['param_names'], res['parameters']):
            results[par].set_value(irow, value)
        results['chisq'].set_value(irow, res.chisq)
        results['ndof'].set_value(irow, res.ndof)
#        sncosmo.plot_lc(data=data, model=fitted_model, errors=res.errors)
    except Exception:
        continue
    results.iloc[imin:imax].to_pickle(outfile)
Пример #58
0
print str(x)

print str(len(relevantdata[0]))

# Write out to a file as this is how we will do things in a larger set by looping through, and then read in the file

model.set(**params[0])

# fig_relevant = sncosmo.plot_lc(relevantdata[0], model=model)

#print "Close Window to continute"
#pl.show()
sncosmo.write_lc(Table(relevantdata[0]), fname='lc.dat', format='ascii')

# sncosmo.write_lc(Table(relevantdata[0]), fname='lc.dat.json', format='json')

# sncosmo.write_lc(Table(relevantdata[0]), fname='lc.dat.fits', format='snana') # fits

lc = sncosmo.read_lc('lc.dat', format='ascii')

fmodel = sncosmo.Model(source='salt2-extended')
for z in zvals:
    fmodel.set(z=z)
    res, fitmodel = sncosmo.fit_lc(relevantdata[0], fmodel, ['t0', 'x0', 'x1', 'c']) 

print res

print params[0]


Пример #59
0
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!).
Пример #60
0
def lcfit(lightcurves,params,fitparams,truearray,sources):
    models = []
    for source in sources:
        try:
            models.append(sncosmo.Model(source=source))
        except:
            models.append('custom_bazin_model')
    iatot = 0.
    noniatot = 0.
    iafailed = 0.
    noniafailed= 0.
    badrows = []
    for i,lc in enumerate(lightcurves):
        if truearray[i] == 0:
            iatot += 1.
        else:
            noniatot += 1.

        z = params[i]['hostz']
        ze = params[i]['hostz_std']
        fitparams[i,-2] = z
        fitparams[i,-1] = ze
        t0 = lc['time'][np.argmax(lc['flux'])]
        print('Fitting LC '+str(i)+'/'+str(len(lightcurves)),'t0',t0,'z',z)
        input_param_index = 3

        saltworked = True
        bazinworked = True
        for j, model, source in zip(range(len(sources)), models, sources):

            if source == 'salt2':
                try:
                    #model.set(z=z)
                    res, fitted_model = sncosmo.fit_lc(lc, model,['z','t0', 'x0', 'x1', 'c'],
                                               bounds={'z':(z-3.*ze,z+3.*ze ),'t0':(t0-50.,t0+50.)})
                    #print(res.keys())
                    print('Type',truearray[i],'Source', source,'Fit Chisq', res.chisq / res.ndof,
                          'Fit c',res.parameters[4],'Fit x0',res.parameters[2],'Fit x1',res.parameters[3])
                    #print('chisq/ndof',res.chisq/res.ndof)
                    #print('param names',res.param_names[:])
                    #print('params',res.parameters[:])
                    #print('-'*100)

                    fitparams[i,:4] = np.array([res.parameters[2], res.parameters[3], res.parameters[4], res.chisq/res.ndof])
                    t0 = res.parameters[1]
                    #fitparams[i, :] = np.array([res.parameters[4], res.chisq / res.ndof])
                    #sys.exit()
                except sncosmo.fitting.DataQualityError:
                    #fitparams[i, :] = np.array([99, 99, 99, 99])
                    # if truearray[i] == 0:
                    #     print('could not fit Type Ia')
                    #     sys.exit()
                    print('fitting failed: SN Type ',truearray[i])
                    fitparams[i, :4] = np.array([0., 0., 0., 500.])
                    print('-'*100)
                    if truearray[i] == 0:
                        iafailed += 1.
                    else:
                        noniafailed += 1.
                    #badrows.append(i)
                    #saltworked = False

                except RuntimeError:
                    # if truearray[i] == 0:
                    #     print('could not fit Type Ia')
                    #     sys.exit()
                    #fitparams[i, :] = np.array([99, 99, 99, 99])
                    fitparams[i, :4] = np.array([0., 0., 0., 500.])
                    print('fitting failed: SN Type ',truearray[i])
                    print('-'*100)
                    #badrows.append(i)
                    #saltworked = False
                    if truearray[i] == 0:
                        iafailed += 1.
                    else:
                        noniafailed += 1.
            elif saltworked:
                if source == 'bazin':
                    if True:
                        flux = lc['flux']
                        fluxerr = lc['fluxerr']
                        bands = lc['band']
                        mjds = lc['time']
                        input_param_index = 3
                        for band in np.sort(np.unique(bands)):
                            bflux = flux[bands==band]
                            bfluxerr = fluxerr[bands==band]
                            bmjds = mjds[bands==band]
                            try:
                                chisq,popt = bazin.lmfit(bmjds,bflux,bfluxerr)
                                if chisq > 500.:
                                    chisq = 500.
                                print('Source Bazin',band,'Fit Chisq',chisq,'t0',popt[0],'tau_fall',popt[1],
                                      'tau_rise',popt[2],'A',popt[3])
                            except:
                                print('BASIN FAILED ' * 10)
                                chisq, popt = 500.,[0,0,0,0,0]
                            fitparams[i,j+input_param_index] = chisq
                            input_param_index += 1
                            fitparams[i, j+input_param_index] = popt[1]
                            input_param_index += 1
                            fitparams[i, j+input_param_index] = popt[2]
                            input_param_index += 1
                            fitparams[i, j+input_param_index] = popt[3]
                            input_param_index += 1
                            fitparams[i, j+input_param_index] = popt[4]
                            input_param_index += 1
                        input_param_index -= 1

                else:
                    if bazinworked:
                        if not source == 'mlcs2k2':
                            #print(model.param_names)
                            try:
                                res, fitted_model = sncosmo.fit_lc(lc, model, ['z', 't0', 'amplitude'],
                                                                   bounds={'z':(z-3.*ze,z+3.*ze ),
                                                                           't0': (t0 - 20., t0 + 20.)})
                                                                   #bounds={'z': (z - .05 * z,
                                                                   #              z + .05 * z)})  # assume we know hostgal z to within 5%
                                print('Source',source,'Fit Chisq',res.chisq/res.ndof)
                                fitparams[i, j+input_param_index] = res.chisq/res.ndof
                            except:
                                print('Source', source, 'Could not Fit... Chisq', 300.)
                                fitparams[i, j+input_param_index] = 300.
                        else:
                            try:
                                res, fitted_model = sncosmo.fit_lc(lc, model, ['z', 't0', 'amplitude', 'delta',],
                                                                   bounds={'z':(z-3.*ze,z+3.*ze ),
                                                                           't0': (t0 - 20., t0 + 20.)})
                                                                   #bounds={'z': (z - .05 * z,
                                                                   #              z + .05 * z)})  # assume we know hostgal z to within 5%
                                print('Source', source, 'Fit Chisq', res.chisq / res.ndof)
                                fitparams[i, j + input_param_index] = res.chisq / res.ndof
                            except:
                                print('Source', source, 'Could not Fit... Chisq', 300.)
                                fitparams[i, j + input_param_index] = 300.

        print(fitparams[i,:])
        print('-' * 100)

    print('Percentage of Ia Failed',iafailed/iatot)
    print('Percentage of NONIa Failed',noniafailed/noniatot)
    fitparams = np.delete(fitparams,np.array(badrows),axis=0)
    truetypes = np.delete(truearray,np.array(badrows))


    return fitparams, truetypes