예제 #1
0
    def test_FitMCMC_best_curves_dt_SN1999em(self):
        from pystella.rf import light_curve_func as lcf
        from pystella.rf import light_curve_plot as lcp
        # Get observations
        D = 11.5e6  # pc
        dm = -5. * np.log10(D) + 5
        # dm = -30.4  # D = 12.e6 pc
        curves_obs = sn1999em.read_curves()
        curves_obs.set_mshift(dm)

        # Get model
        name = 'cat_R500_M15_Ni006_E12'
        path = join(dirname(dirname(abspath(__file__))), 'data', 'stella')

        curves_mdl = lcf.curves_compute(name,
                                        path,
                                        distance=10,
                                        bands=curves_obs.BandNames)

        # fit
        is_debug = True  # True
        # fitter = FitLcMcmc()
        fitter = ps.FitMCMC()
        fitter.is_info = True
        if is_debug:
            fitter.is_debug = is_debug
            fitter.nwalkers = 100  # number of MCMC walkers
            fitter.nburn = 20  # "burn-in" period to let chains stabilize
            fitter.nsteps = 200  # number of MCMC steps to take
        threads = 1

        # fitter.is_debug = False
        fit_result, res, (th, e1,
                          e2), samples = fitter.best_curves(curves_mdl,
                                                            curves_obs,
                                                            dt0=0.,
                                                            threads=threads,
                                                            is_sampler=True)
        fig = fitter.plot_corner(samples,
                                 labels=('dt', ),
                                 bnames=curves_obs.BandNames)

        # print
        # '{:10s} {:.4f} ^{:.4f}_{:.4f}\n'.format('lnf:', res['lnf'], res['lnfsig2'], res['lnfsig1']) + \
        txt = '{:10s} {:.4f} ^{:.4f}_{:.4f} \n'.format('tshift:', th.dt, e1.dt, e2.dt) + \
              '{:10s} chi2= {:.1f}  BIC= {:.1f} AIC= {:.1f} dof= {} accept= {:.3f}\n'. \
                  format('stat:', res['chi2'], res['bic'], res['aic'], res['dof'], res['acceptance_fraction'])
        print(txt)
        # plot model
        curves_obs.set_tshift(-th.dt)
        ax = lcp.curves_plot(curves_mdl)

        lt = {lc.Band.Name: 'o' for lc in curves_obs}
        lcp.curves_plot(curves_obs, ax, lt=lt, xlim=(-10, 300), is_line=False)
        ax.text(0.1, 0.1, txt, transform=ax.transAxes)
        plt.show()
예제 #2
0
    def test_fit_mpfit_curves_Stella_SN1999em(self):
        from pystella.rf import light_curve_func as lcf
        from pystella.rf import light_curve_plot as lcp
        # matplotlib.rcParams['backend'] = "TkAgg"
        # matplotlib.rcParams['backend'] = "Qt4Agg"
        # from matplotlib import pyplot as plt
        # Get observations
        D = 11.5e6  # pc
        dm = -5. * np.log10(D) + 5
        # dm = -30.4  # D = 12.e6 pc
        curves_obs = sn1999em.read_curves()
        curves_obs.set_mshift(dm)

        # Get model
        name = 'cat_R500_M15_Ni006_E12'
        path = join(dirname(dirname(abspath(__file__))), 'data', 'stella')

        curves_mdl = lcf.curves_compute(name, path, curves_obs.BandNames)

        # fit
        # fitter = FitLcMcmc()
        fitter = ps.FitMPFit()
        fitter.is_debug = True
        res = fitter.fit_curves(curves_obs, curves_mdl)

        # print
        # txt = '{0:10} {1:.4e} \n'.format('tshift:', res.tshift) + \
        #       '{0:10} {1:.4e} \n'.format('tsigma:', res.tsigma) + \
        #       '{0}\n'.format(res.comm)
        txt = '{:10s} {:.4f}+-{:.4f} \n'.format('tshift:', res.tshift, res.tsigma) + \
              '{:10s} {:.4f}+-{:.4f}\n'.format('msigma:', res.mshift, res.msigma) + \
              '{0}\n'.format(res.comm)

        print(txt)
        # plot model
        curves_obs.set_tshift(res.tshift)
        # curves_mdl.set_tshift(0.)
        ax = lcp.curves_plot(curves_mdl)

        lt = {lc.Band.Name: 'o' for lc in curves_obs}
        lcp.curves_plot(curves_obs, ax, lt=lt, xlim=(-10, 300), is_line=False)
        ax.text(0.1, 0.1, txt, transform=ax.transAxes)
        plt.show()
예제 #3
0
    def test_FitMPFit_best_curves_gp_SN1999em(self):
        from pystella.rf import light_curve_func as lcf
        from pystella.rf import light_curve_plot as lcp
        # matplotlib.rcParams['backend'] = "TkAgg"
        # matplotlib.rcParams['backend'] = "Qt4Agg"
        # from matplotlib import pyplot as plt
        # Get observations
        D = 11.5e6  # pc
        ebv_sn = 0.1
        dm = -5. * np.log10(D) + 5
        # dm = -30.4  # D = 12.e6 pc
        curves_obs = sn1999em.read_curves()
        curves_obs.set_mshift(dm)

        # Get model
        name = 'cat_R500_M15_Ni006_E12'
        path = join(dirname(dirname(abspath(__file__))), 'data', 'stella')

        curves_mdl = lcf.curves_compute(name, path, curves_obs.BandNames)

        # fit
        # fitter = FitLcMcmc()
        fitter = ps.FitMPFit()
        fitter.is_info = True
        fitter.is_debug = True
        res = fitter.best_curves_gp(curves_mdl, curves_obs, dt0=0., dm0=0.)
        # print
        txt = '{0:10} {1:.4e} \n'.format('tshift:', res['dt']) + \
              '{0:10} {1:.4e} \n'.format('tsigma:', res['dtsig'])
        print(txt)
        # plot model
        curves_obs.set_tshift(res['dt'])
        # curves_mdl.set_tshift(0.)
        ax = lcp.curves_plot(curves_mdl)

        lt = {lc.Band.Name: 'o' for lc in curves_obs}
        lcp.curves_plot(curves_obs, ax, lt=lt, xlim=(-10, 300), is_line=False)
        plt.show()
예제 #4
0
파일: scm.py 프로젝트: baklanovp/pystella
def run_scm(bands, distance, names, path, t50, t_beg, t_end, z):
    res = np.array(np.zeros(len(names)),
                   dtype=np.dtype({'names': ['v'] + bands,
                                   'formats': [np.float] * (1 + len(bands))}))
    im = 0
    for name in names:
        vels = velocity.compute_vel(name, path, z=z, t_beg=t_beg, t_end=t_end)
        if vels is None:
            print "No enough data for %s " % name
            continue

        curves = light_curve_func.curves_compute(name, path, bands, z=z, distance=distance,
                                                 t_beg=t_beg, t_end=t_end)
        v = extract_time(t50, vels['time'], vels['vel'])
        res[im]['v'] = v
        # dic_results[name]['v'] = v
        for bname in bands:
            m = extract_time(t50, curves.get(bname).Time, curves.get(bname).Mag)
            res[im][bname] = m
        print "Run: %s [%d/%d]" % (name, im + 1, len(names))
        im += 1
    res = res[res[:]['v'] > 0]
    return res
예제 #5
0
파일: ubv.py 프로젝트: baklanovp/pystella
def main(name='', model_ext='.ph'):
    is_quiet = True
    is_save_mags = False
    is_save_plot = False
    is_plot_time_points = False
    is_extinction = False
    is_vel = False

    label = None
    fsave = None
    t_diff = 1.00001
    # path = ''
    path = os.getcwd()
    z = 0
    e = 0.
    magnification = 1.
    distance = 10.  # pc
    callback = None
    xlim = None
    ylim = None
    # bshift = None
    bshift = {}

    try:
        opts, args = getopt.getopt(sys.argv[1:], "hqwtc:d:p:e:i:b:l:m:vs:x:y:z:")
    except getopt.GetoptError as err:
        print str(err)  # will print something like "option -a not recognized"
        usage()
        sys.exit(2)

    if len(args) > 0:
        path, name = os.path.split(str(args[0]))
        path = os.path.expanduser(path)
        name = name.replace('.ph', '')
    elif len(opts) == 0:
        usage()
        sys.exit(2)

    if not name:
        for opt, arg in opts:
            if opt == '-i':
                name = os.path.splitext(os.path.basename(str(arg)))[0]
                break

    bands = ['U', 'B', 'V', 'R', "I"]
    # bands = ['U', 'B', 'V', 'R', "I", 'UVM2', "UVW1", "UVW2", 'g', "r", "i"]

    for opt, arg in opts:
        if opt == '-e':
            e = float(arg)
            is_extinction = True
            continue
        if opt == '-b':
            bands = []
            band.Band.load_settings()
            for b in str(arg).split('-'):
                # extract band shift
                if ':' in b:
                    bname, shift = b.split(':')
                    if '_' in shift:
                        bshift[bname] = -float(shift.replace('_', ''))
                    else:
                        bshift[bname] = float(shift)
                else:
                    bname = b
                if not band.band_is_exist(bname):
                    print 'No such band: ' + bname
                    sys.exit(2)
                bands.append(bname)
            continue
        if opt == '-c':
            c = lc_wrapper(str(arg))
            if callback is not None:
                c = cb.CallBackArray((callback, c))
            callback = c
            continue
        if opt == '-q':
            is_quiet = False
            continue
        if opt == '-s':
            is_save_plot = True
            fsave = str.strip(arg)
            continue
        if opt == '-w':
            is_save_mags = True
            continue
        if opt == '-t':
            is_plot_time_points = True
            continue
        if opt == '-v':
            is_vel = True
            continue
        if opt == '-m':
            magnification = float(arg)
            continue
        if opt == '-z':
            z = float(arg)
            continue
        if opt == '-d':
            distance = float(arg)
            continue
        if opt == '-l':
            label = str.strip(arg)
            continue
        if opt == '-x':
            xlim = map(float, str(arg).split(':'))
            continue
        if opt == '-y':
            ylim = map(float, str(arg).split(':'))
            continue
        if opt == '-p':
            path = os.path.expanduser(str(arg))
            if not (os.path.isdir(path) and os.path.exists(path)):
                print "No such directory: " + path
                sys.exit(2)
            continue
        elif opt == '-h':
            usage()
            sys.exit(2)

    print "Plot magnitudes on z=%f at distance=%e [cosmology D(z)=%s Mpc]" % (z, distance, cosmology_D_by_z(z))

    names = []
    if name != '':
        names.append(name)
    else:  # run for all files in the path
        names = get_model_names(path, model_ext)

    if len(names) > 0:
        models_mags = {}  # dict((k, None) for k in names)
        models_vels = {}  # dict((k, None) for k in names)
        i = 0
        for name in names:
            i += 1
            # mags = lcf.compute_mag(name, path, bands, ext=ext, z=z, distance=distance, magnification=magnification,
            #                        t_diff=t_diff, is_show_info=not is_quiet, is_save=is_save_mags)
            curves = lcf.curves_compute(name, path, bands, z=z, distance=distance, magnification=magnification,
                                        is_save=is_save_mags)
            if is_extinction:
                lcf.curves_reddening(curves, ebv=e, z=z)
            # lcf.plot_curves(curves)
            # exit()
            # models_mags[name] = mags
            models_mags[name] = curves

            if not is_quiet:
                # z, distance = 0.145, 687.7e6  # pc for comparison with Maria
                # lcf.plot_bands(mags, bands, title=name, fname='', is_time_points=is_plot_time_points)
                lcf.plot_bands(curves, bands, title=name, fname='', is_time_points=is_plot_time_points)

            if is_vel:
                vels = vel.compute_vel(name, path, z=z)
                if vels is None:
                    sys.exit("No data for: %s in %s" % (name, path))
                models_vels[name] = vels
                print "Finish velocity: %s [%d/%d]" % (name, i, len(names))
            else:
                models_vels = None
                print "Finish mags: %s [%d/%d] in %s" % (name, i, len(names), path)

        if label is None:
            if callback is not None:
                label = "ts=%s z=%4.2f D=%6.2e mu=%3.1f ebv=%4.2f" % (
                    callback.arg_totext(0), z, distance, magnification, e)
            else:
                label = "z=%4.2f D=%6.2e mu=%3.1f ebv=%4.2f" % (z, distance, magnification, e)

        if is_save_plot:
            if len(fsave) == 0:
                if is_vel:
                    fsave = "ubv_vel_%s" % name
                else:
                    fsave = "ubv_%s" % name

            if is_extinction and e > 0:
                fsave = "%s_e0%2d" % (fsave, int(e * 100))  # bad formula for name

            d = os.path.expanduser('~/')
            # d = '/home/bakl/Sn/my/conf/2016/snrefsdal/img'
            fsave = os.path.join(d, fsave) + '.pdf'

        plot_all(models_vels, models_mags, bands, call=callback, xlim=xlim, ylim=ylim,
                 is_time_points=is_plot_time_points, title=label, fsave=fsave, bshift=bshift)
        # plot_all(dic_results, bands,  xlim=(-10, 410), is_time_points=is_plot_time_points)
        # plot_all(dic_results, bands, xlim=(-10, 410), callback=callback, is_time_points=is_plot_time_points)
        # plot_all(dic_results, bands,  ylim=(40, 23),  is_time_points=is_plot_time_points)
    else:
        print "There are no models in the directory: %s with extension: %s " % (path, model_ext)
예제 #6
0
    def test_fit_stella_model(self):
        """Example: Fitting a Stella model to Sn1999em"""

        # obs data
        dm = -29.38
        curves = sn1999em.read_curves()
        lc_obs = curves.get('V')
        lc_obs.mshift = -dm
        lc_obs.tshift = -lc_obs.Tmin

        # model light curves
        name = 'cat_R1000_M15_Ni007_E15'
        path = join(dirname(abspath(__file__)), 'data', 'stella')
        curves_model = lcf.curves_compute(name, path, bands='V')
        lc_mdl = curves_model.get('V')

        todo
        # Choose the "true" parameters.
        dt_init = 0.  # initial time shift
        dm_init = 0.  # initial time shift
        f_true = 0.534

        # Generate some synthetic data from the model.
        # N = 50
        t = lc_obs.Time
        merr = lc_obs.Err
        m = lc_obs.Mag

        def lnlike(theta, t, m, yerr):
            dt, dm, lnf = theta
            model = lc_mdl.Mag
            inv_sigma2 = 1.0 / (yerr**2 + model**2 * np.exp(2 * lnf))
            return -0.5 * (np.sum((lc_obs - model)**2 * inv_sigma2 -
                                  np.log(inv_sigma2)))

        nll = lambda *args: -lnlike(*args)
        result = op.minimize(
            nll, [dt_init, dm_init, np.log(f_true)], args=(t, m, merr))
        m_ml, b_ml, lnf_ml = result["x"]

        def lnprior(theta):
            m, b, lnf = theta
            if -5.0 < m < 0.5 and 0.0 < b < 10.0 and -10.0 < lnf < 1.0:
                return 0.0
            return -np.inf

        def lnprob(theta, x, y, yerr):
            lp = lnprior(theta)
            if not np.isfinite(lp):
                return -np.inf
            return lp + lnlike(theta, x, y, yerr)

        ndim, nwalkers = 3, 100
        pos = [
            result["x"] + 1e-4 * np.random.randn(ndim) for i in range(nwalkers)
        ]

        sampler = emcee.EnsembleSampler(nwalkers,
                                        ndim,
                                        lnprob,
                                        args=(t, m, merr))
        sampler.run_mcmc(pos, 500)

        samples = sampler.chain[:, 50:, :].reshape((-1, ndim))

        xl = np.array([0, 10])
        for m, b, lnf in samples[np.random.randint(len(samples), size=100)]:
            plt.plot(xl, m * xl + b, color="k", alpha=0.1)
        plt.plot(xl, dt_init * xl + dm_init, color="r", lw=2, alpha=0.8)
        plt.errorbar(t, m, yerr=merr, fmt=".k")
        plt.show()

        samples[:, 2] = np.exp(samples[:, 2])
        m_mcmc, b_mcmc, f_mcmc = map(
            lambda v: (v[1], v[2] - v[1], v[1] - v[0]),
            zip(*np.percentile(samples, [16, 50, 84], axis=0)))

        def print_v3(v3):
            print("v = %f + %f - %f" % v3)

        map(print_v3, (m_mcmc, b_mcmc, f_mcmc))