コード例 #1
0
ファイル: knfit.py プロジェクト: shreyasahasram08/ztfrest
def do_knfit(df,
             outputDir='./knfit',
             errorbudget=1.0,
             n_live_points=100,
             evidence_tolerance=0.5):

    if len(df.index) < 2:
        print('Not enough data for candidate %s... continuing.',
              df.candname.values[0])
        return

    if not os.path.isdir(outputDir):
        os.makedirs(outputDir)

    candname = df.name.values[0].decode()
    filters = list(set(df.filtname))

    mint = 0.0
    maxt = 7.0
    dt = 0.05
    tt = np.arange(mint, maxt, dt)
    doWaveformExtrapolate = True

    ZPRange = 5.0
    T0Range = 2.0

    ModelPath = "../gwemlightcurves/output/svdmodels/"

    T0 = np.inf
    mag_min = np.inf

    data_out = {}
    for index, row in df.iterrows():
        filt = row.filtname
        mag = row.magpsf
        dmag = row.sigmapsf
        mjd = Time(row.jd, format='jd').mjd
        magzp = row.magzpsci

        if 99.0 == mag:
            mag = magzp
            dmag = np.inf
        else:
            T0 = np.min([T0, mjd])
            mag_min = np.min([mag_min, mag])

        if not filt in data_out:
            data_out[filt] = np.empty((0, 3), float)
        data_out[filt] = np.append(data_out[filt],
                                   np.array([[mjd, mag, dmag]]),
                                   axis=0)

    distmod = mag_min - -16
    distance = 10**((distmod / 5.0) + 1.0) / 1e6

    for ii, key in enumerate(data_out.keys()):
        if key == "t":
            continue
        else:
            data_out[key][:, 0] = data_out[key][:, 0] - T0
            data_out[key][:, 1] = data_out[key][:, 1] - 5 * (
                np.log10(distance * 1e6) - 1)

    for ii, key in enumerate(data_out.keys()):
        if key == "t":
            continue
        else:
            idxs = np.intersect1d(
                np.where(data_out[key][:, 0] >= mint)[0],
                np.where(data_out[key][:, 0] <= maxt)[0])
            data_out[key] = data_out[key][idxs, :]

    for ii, key in enumerate(data_out.keys()):
        idxs = np.where(~np.isnan(data_out[key][:, 2]))[0]
        if key == "t":
            continue
        else:
            data_out[key] = data_out[key][idxs, :]

    for ii, key in enumerate(data_out.keys()):
        if not key in filters:
            del data_out[key]

    for ii, key in enumerate(data_out.keys()):
        if ii == 0:
            samples = data_out[key].copy()
        else:
            samples = np.vstack((samples, data_out[key].copy()))

    idx = np.argmin(samples[:, 0])
    samples = samples[idx, :]

    Global.data_out = data_out
    Global.errorbudget = errorbudget
    Global.ZPRange = ZPRange
    Global.T0Range = T0Range
    Global.doLightcurves = 1
    Global.filters = filters
    Global.doWaveformExtrapolate = doWaveformExtrapolate

    modelfile = os.path.join(ModelPath, 'Bu2019inc_mag.pkl')
    with open(modelfile, 'rb') as handle:
        svd_mag_model = pickle.load(handle)
    Global.svd_mag_model = svd_mag_model

    modelfile = os.path.join(ModelPath, 'Bu2019inc_lbol.pkl')
    with open(modelfile, 'rb') as handle:
        svd_lbol_model = pickle.load(handle)
    Global.svd_lbol_model = svd_lbol_model

    plotDir = os.path.join(outputDir, candname)
    if not os.path.isdir(plotDir):
        os.makedirs(plotDir)

    max_iter = -1
    best = []

    parameters = ["t0", "mej", "phi", "theta", "zp"]
    labels = [
        r"$T_0$", r"${\rm log}_{10} (M_{\rm ej})$", r"$\Phi$", r"$\Theta$",
        "ZP"
    ]
    n_params = len(parameters)
    pymultinest.run(myloglike_Bu2019inc_ejecta,
                    myprior_Bu2019inc_ejecta,
                    n_params,
                    importance_nested_sampling=False,
                    resume=True,
                    verbose=True,
                    sampling_efficiency='parameter',
                    n_live_points=n_live_points,
                    outputfiles_basename='%s/2-' % plotDir,
                    evidence_tolerance=evidence_tolerance,
                    multimodal=False,
                    max_iter=max_iter)

    multifile = lightcurve_utils.get_post_file(plotDir)
    data = np.loadtxt(multifile)

    t0, mej, phi, theta, zp, loglikelihood = data[:,
                                                  0], 10**data[:,
                                                               1], data[:,
                                                                        2], data[:,
                                                                                 3], data[:,
                                                                                          4], data[:,
                                                                                                   5]
    idx = np.argmax(loglikelihood)
    t0_best, mej_best, phi_best, theta_best, zp_best = data[idx, 0], 10**data[
        idx, 1], data[idx, 2], data[idx, 3], data[idx, 4]
    zp_mu, zp_std = 0.0, Global.ZPRange
    zp_best = scipy.stats.norm(zp_mu, zp_std).ppf(zp_best)
    tmag, lbol, mag = Bu2019inc_model_ejecta(mej_best, phi_best, theta_best)

    pcklFile = os.path.join(plotDir, "data.pkl")
    f = open(pcklFile, 'wb')
    pickle.dump((data_out, data, tmag, lbol, mag, t0_best, zp_best, n_params,
                 labels, best), f)
    f.close()

    title_fontsize = 30
    label_fontsize = 30

    plotName = "%s/corner.pdf" % (plotDir)
    figure = corner.corner(data[:, :-1],
                           labels=labels,
                           quantiles=[0.16, 0.5, 0.84],
                           show_titles=True,
                           title_kwargs={"fontsize": title_fontsize},
                           label_kwargs={"fontsize": label_fontsize},
                           title_fmt=".2f",
                           smooth=3,
                           color="coral")
    figure.set_size_inches(14.0, 14.0)
    plt.savefig(plotName)
    plt.close()

    tmag = tmag + t0_best
    #colors=cm.rainbow(np.linspace(0,1,len(filters)))
    colors = cm.Spectral(np.linspace(0, 1, len(filters)))[::-1]

    color2 = 'coral'
    color1 = 'cornflowerblue'

    plotName = "%s/models_panels.pdf" % (plotDir)
    #plt.figure(figsize=(20,18))
    plt.figure(figsize=(20, 28))

    cnt = 0
    for filt, color in zip(filters, colors):
        cnt = cnt + 1
        if cnt == 1:
            ax1 = plt.subplot(len(filters), 1, cnt)
        else:
            ax2 = plt.subplot(len(filters), 1, cnt, sharex=ax1, sharey=ax1)

        if not filt in data_out: continue
        samples = data_out[filt]
        t, y, sigma_y = samples[:, 0], samples[:, 1], samples[:, 2]
        idx = np.where(~np.isnan(y))[0]
        t, y, sigma_y = t[idx], y[idx], sigma_y[idx]
        if len(t) == 0: continue

        idx = np.where(np.isfinite(sigma_y))[0]
        plt.errorbar(t[idx],
                     y[idx],
                     sigma_y[idx],
                     fmt='o',
                     c=color,
                     markersize=16,
                     label='%s-band' % filt)

        idx = np.where(~np.isfinite(sigma_y))[0]
        plt.errorbar(t[idx],
                     y[idx],
                     sigma_y[idx],
                     fmt='v',
                     c=color,
                     markersize=16)

        magave = lightcurve_utils.get_mag(mag, filt)
        ii = np.where(~np.isnan(magave))[0]
        f = interp.interp1d(tmag[ii], magave[ii], fill_value='extrapolate')
        maginterp = f(tt)
        #plt.plot(tt,maginterp+zp_best,'--',c=color,linewidth=3)
        #plt.fill_between(tt,maginterp+zp_best-errorbudget,maginterp+zp_best+errorbudget,facecolor=color,alpha=0.2)

        plt.plot(tt, maginterp + zp_best, '--', c=color2, linewidth=3)
        plt.fill_between(tt,
                         maginterp + zp_best - errorbudget,
                         maginterp + zp_best + errorbudget,
                         facecolor=color2,
                         alpha=0.2)

        plt.ylabel('%s' % filt, fontsize=48, rotation=0, labelpad=40)

        plt.xlim([0.0, 10.0])
        plt.ylim([-22.0, -8.0])
        plt.gca().invert_yaxis()
        plt.grid()

        if cnt == 1:
            ax1.set_yticks([-22, -18, -14, -10])
            plt.setp(ax1.get_xticklabels(), visible=False)
            #l = plt.legend(loc="upper right",prop={'size':36},numpoints=1,shadow=True, fancybox=True)
        elif not cnt == len(filters):
            plt.setp(ax2.get_xticklabels(), visible=False)
        plt.xticks(fontsize=36)
        plt.yticks(fontsize=36)

    ax1.set_zorder(1)
    plt.xlabel('Time [days]', fontsize=48)
    plt.tight_layout()
    plt.savefig(plotName)
    plt.close()

    return
コード例 #2
0
parameters = ["q", "lambdatilde"]
n_params = len(parameters)
pymultinest.run(myloglike_combined,
                myprior_combined,
                n_params,
                importance_nested_sampling=False,
                resume=True,
                verbose=True,
                sampling_efficiency='parameter',
                n_live_points=n_live_points,
                outputfiles_basename='%s/2-' % plotDir,
                evidence_tolerance=evidence_tolerance,
                multimodal=False)

labels = [r"q", r"$\tilde{\Lambda}$"]
multifile = lightcurve_utils.get_post_file(plotDir)
data_combined = np.loadtxt(multifile)
q_combined = data_combined[:, 0]
lambdatilde_combined = data_combined[:, 1]
data_combined = np.vstack((q_combined, lambdatilde_combined)).T

plotName = "%s/corner_combined.pdf" % (plotDir)
figure = corner.corner(data_combined,
                       labels=labels,
                       quantiles=[0.16, 0.5, 0.84],
                       show_titles=True,
                       title_kwargs={"fontsize": 24},
                       label_kwargs={"fontsize": 28},
                       title_fmt=".2f")
figure.set_size_inches(14.0, 14.0)
plt.savefig(plotName)
コード例 #3
0
def plot_results(plotDir):
    multifile = lightcurve_utils.get_post_file(plotDir)
    data = np.loadtxt(multifile)

    a = pymultinest.Analyzer(n_params=n_params,
                             outputfiles_basename='%s/2-' % plotDir)
    s = a.get_stats()

    if opts.model == "BlackBody":
        T = data[:, 0]
        F = data[:, 1]
        loglikelihood = data[:, 2]
        idx = np.argmax(loglikelihood)

        T_best = data[idx, 0]
        F_best = data[idx, 1]
        truths = [np.nan, np.nan]

        wav, flux = spec_model(T_best, 10**F_best)
        spec_best = {}
        spec_best["lambda"] = wav
        spec_best["data"] = flux
    elif opts.model == "BlackBodyx2":
        T1 = data[:, 0]
        F1 = data[:, 1]
        T2 = data[:, 2]
        F2 = data[:, 3]
        loglikelihood = data[:, 4]
        idx = np.argmax(loglikelihood)

        T1_best = data[idx, 0]
        F1_best = data[idx, 1]
        T2_best = data[idx, 2]
        F2_best = data[idx, 3]
        truths = [np.nan, np.nan, np.nan, np.nan]

        wav1, flux1 = spec_model(T1_best, 10**F1_best)
        wav2, flux2 = spec_model(T2_best, 10**F2_best)
        spec_best = {}
        spec_best["lambda"] = wav1
        spec_best["data"] = flux1 + flux2

    if n_params >= 8:
        title_fontsize = 26
        label_fontsize = 30
    else:
        title_fontsize = 24
        label_fontsize = 28

    plotName = "%s/corner.pdf" % (plotDir)
    figure = corner.corner(data[:, :-1],
                           labels=labels,
                           quantiles=[0.16, 0.5, 0.84],
                           show_titles=True,
                           title_kwargs={"fontsize": title_fontsize},
                           label_kwargs={"fontsize": label_fontsize},
                           title_fmt=".2f",
                           truths=truths)
    if n_params >= 8:
        figure.set_size_inches(18.0, 18.0)
    else:
        figure.set_size_inches(14.0, 14.0)
    plt.savefig(plotName)
    plt.close()

    plotName = "%s/spec.pdf" % (plotDir)
    plt.figure(figsize=(14, 12))
    plt.loglog(spec_best["lambda"], spec_best["data"], 'k--', linewidth=2)
    plt.errorbar(data_out["lambda"],
                 np.abs(data_out["data"]),
                 yerr=data_out["error"],
                 fmt='ro',
                 linewidth=2)
    plt.xlabel(r'$\lambda [\AA]$', fontsize=24)
    plt.ylabel('Fluence [erg/s/cm2/A]', fontsize=24)
    #plt.legend(loc="best",prop={'size':16},numpoints=1)
    ymin = np.min(np.abs(data_out["data"]) - 2 * data_out["error"])
    ymax = np.max(np.abs(data_out["data"]) + 2 * data_out["error"])
    plt.ylim([ymin, ymax])
    plt.grid()
    plt.savefig(plotName)
    plt.close()

    filename = os.path.join(plotDir, 'evidence.dat')
    fid = open(filename, 'w+')
    fid.write("%.15e %.15e" % (s['nested sampling global log-evidence'],
                               s['nested sampling global log-evidence error']))
    fid.close()

    if opts.model == "BlackBody":
        filename = os.path.join(plotDir, 'samples.dat')
        fid = open(filename, 'w+')
        for i, j in zip(T, F):
            fid.write('%.5f %.5f\n' % (i, j))
        fid.close()

        filename = os.path.join(plotDir, 'best.dat')
        fid = open(filename, 'w')
        fid.write('%.5f %.5f\n' % (T_best, F_best))
        fid.close()
    elif opts.model == "BlackBodyx2":
        filename = os.path.join(plotDir, 'samples.dat')
        fid = open(filename, 'w+')
        for i, j, k, l in zip(T1, F1, T2, F2):
            fid.write('%.5f %.5f %.5f %.5f\n' % (i, j, k, l))
        fid.close()

        filename = os.path.join(plotDir, 'best.dat')
        fid = open(filename, 'w')
        fid.write('%.5f %.5f %.5f %.5f\n' %
                  (T1_best, F1_best, T2_best, F2_best))
        fid.close()
コード例 #4
0
    grb = grbdir.split("/")[-1]
    grbdir = os.path.join(grbdir, '1.00')

    dataDirs.append(grbdir)
    grbs.append(grb)

#grbs_skip = ["GW170817","GRB060614","GRB050709","GRB130603B"]
grbs_include = ["GW170817", "GRB060614", "GRB050709", "GRB130603B"]

nsamples = 100
data_out = {}
for grb, dataDir in zip(grbs, dataDirs):
    if not grb in grbs_include: continue
    if not grb in data_out:
        data_out[grb] = {}
    multifile = lightcurve_utils.get_post_file(dataDir)
    data = np.loadtxt(multifile)
    #if (data.size > 0) and (not grb in grbs_skip):
    if data.size > 0:
        data_out[grb]["KN"] = data
        data_out[grb]["KN_samples"] = KNTable.read_multinest_samples(
            multifile, 'Ka2017_A')
        data_out[grb]["KN_samples"] = data_out[grb]["KN_samples"].downsample(
            Nsamples=nsamples)

ModelPath = '%s/svdmodels' % ('../output')
kwargs = {'SaveModel': False, 'LoadModel': True, 'ModelPath': ModelPath}
kwargs["doAB"] = True
kwargs["doSpec"] = False

filts = ["u", "g", "r", "i", "z", "y", "J", "H", "K"]
コード例 #5
0
basedataDir = dataDir

post = {}
for name in names:
    post[name] = {}
    for errorbudget in errorbudgets:
        if opts.doModels:
            plotDir = os.path.join(baseplotDir,"%.2f"%float(errorbudget))
            dataDir = plotDir.replace("fitting_models","models").replace("_EOSFit","")
        elif opts.doSimulation:
            plotDir = os.path.join(baseplotDir,"%.3f"%(float(errorbudget)*100.0))
        elif opts.doGoingTheDistance or opts.doMassGap:
            dataDir = os.path.join(basedataDir,"%.2f"%float(errorbudget))
            plotDir = os.path.join(baseplotDir,"%.2f"%float(errorbudget))

            multifile = lightcurve_utils.get_post_file(dataDir)
            data = np.loadtxt(multifile)

            filename = os.path.join(dataDir,"truth_mej_vej.dat")
            truths_mej_vej = np.loadtxt(filename)
            truths_mej_vej[0] = np.log10(truths_mej_vej[0])

            filename = os.path.join(dataDir,"truth.dat")
            truths = np.loadtxt(filename)

            if opts.doEjecta:
                mej_em = data[:,1]
                vej_em = data[:,2]

                mej_true = truths_mej_vej[0]
                vej_true = truths_mej_vej[1]
コード例 #6
0
plt.close()

H0Dir = os.path.join(plotDir,'H0')
if not os.path.isdir(H0Dir):
    os.makedirs(H0Dir)

rm_command = "rm %s/*" % H0Dir
#os.system(rm_command)

parameters = ["H0","Om0","0de"]
labels = [r'$H_0$',r'$\Omega_m$',r'$\Omega_\Lambda$']
n_params = len(parameters)

pymultinest.run(myloglike_H0, myprior_H0, n_params, importance_nested_sampling = False, resume = True, verbose = True, sampling_efficiency = 'parameter', n_live_points = n_live_points, outputfiles_basename='%s/2-'%H0Dir, evidence_tolerance = evidence_tolerance, multimodal = False, max_iter = max_iter)

multifile = lightcurve_utils.get_post_file(H0Dir)
data = np.loadtxt(multifile)

H0_EM, Om0, Ode0, loglikelihood = data[:,0], data[:,1], data[:,2], data[:,3]
idx = np.argmax(loglikelihood)
H0_best, Om0_best, Ode0_best = data[idx,0:-1]

plotName = "%s/corner.pdf"%(H0Dir)
figure = corner.corner(data[:,:-1], labels=labels,
                   quantiles=[0.16, 0.5, 0.84],
                   show_titles=True, title_kwargs={"fontsize": title_fontsize},
                   label_kwargs={"fontsize": label_fontsize}, title_fmt=".3f",
                   smooth=3)
figure.set_size_inches(18.0,18.0)
plt.savefig(plotName)
plt.close()