if not opts.model in ["SN"]:
        plt.ylim([-20, 10])
    if opts.model in ["Ka2017inc", "Ka2017x2inc"]:
        plt.title('Inclination: %.1f' % opts.iota)
    plt.legend(loc="lower center", ncol=5)
    plt.gca().invert_yaxis()
    plt.savefig(plotName, bbox_inches='tight')
    plotNamePNG = "%s/%s.png" % (plotDir, name)
    plt.savefig(plotNamePNG)
    plt.close()

    color1 = 'coral'
    color2 = 'cornflowerblue'

    if opts.doComparison:
        mag_d_comparison = lightcurve_utils.read_files([opts.comparisonFile])
        key = list(mag_d_comparison[0].keys())[0]
        mag_d_comparison = mag_d_comparison[0][key]

    tini, tmax, dt = 0.0, 21.0, 0.1
    tt = np.arange(tini, tmax, dt)
    zp_best1, zp_best2 = 0, 0
    errorbudget = 1.0

    plotName = "%s/%s_panels.pdf" % (plotDir, name)
    plotNamePNG = "%s/%s_panels.png" % (plotDir, name)
    plt.figure(figsize=(20, 28))

    cnt = 0
    for filt, color, magidx in zip(filts, colors, magidxs):
        cnt = cnt + 1
Beispiel #2
0
if opts.doAB:

    names = opts.name.split(",")
    filenames = []
    legend_names = []
    for name in names:
        for ii, model in enumerate(models):
            filename = '%s/%s/%s.dat' % (outputDir, model, name)
            if not os.path.isfile(filename):
                continue
            filenames.append(filename)
            legend_names.append(models_ref[ii])
            break

    mags, names = lightcurve_utils.read_files(filenames)

    if opts.doModels:

        model_data = {}
        modelfiles = opts.modelfile.split(",")
        for modelfile in modelfiles:
            modelfile = os.path.join(opts.plotDir, modelfile)
            modelfileSplit = modelfile.split("/")
            model_out = np.loadtxt(modelfile)

            errorbudget = float(modelfileSplit[-2])
            modelType = modelfileSplit[-4]
            model = modelfileSplit[-7].split("_")[0]

            if model == "BNS" and modelType == "ejecta":
Beispiel #3
0
def calc_svd_color_model(tini, tmax, dt, n_coeff=100, model="a2.0"):

    print("Calculating SVD model of inclination colors...")

    if model in ["DZ2", "gamA2", "gamB2"]:
        fileDir = "../output/wollaeger/%s" % model
    else:
        fileDir = "../output/kasen_kilonova_2D/%s" % model

    filenames_all = glob.glob('%s/*.dat' % fileDir)
    idxs = []
    for ii, filename in enumerate(filenames_all):
        if "_Lbol.dat" in filename: continue
        if "_spec.dat" in filename: continue
        idxs.append(ii)
    filenames = [filenames_all[idx] for idx in idxs]

    mags, names = lightcurve_utils.read_files(filenames)
    magkeys = mags.keys()

    tt = np.arange(tini, tmax + dt, dt)
    filters = ["u", "g", "r", "i", "z", "y", "J", "H", "K"]

    for key in magkeys:
        keySplit = key.split("_")
        if model in ["DZ2", "gamA2", "gamB2"]:
            incl = float(keySplit[1].split("=")[1]) * 360.0 / (2 * np.pi)
            mags[key]["iota"] = incl
        else:
            mags[key]["iota"] = float(keySplit[-1])

        mags[key]["data"] = np.zeros((len(tt), len(filters)))
        for jj, filt in enumerate(filters):
            ii = np.where(np.isfinite(mags[key][filt]))[0]
            if len(ii) > 1:
                f = interp.interp1d(mags[key]["t"][ii],
                                    mags[key][filt][ii],
                                    fill_value='extrapolate')
                maginterp = f(tt)
                mags[key]["data"][:, jj] = maginterp
            else:
                mags[key]["data"][:, jj] = 0.0

        mags[key]["data_vector"] = np.reshape(mags[key]["data"],
                                              (len(tt) * len(filters), 1))

    magkeys = mags.keys()
    param_array = []
    for key in magkeys:
        param_array.append(mags[key]["iota"])

    param_array_postprocess = np.array(param_array)
    param_mins, param_maxs = np.min(param_array_postprocess,
                                    axis=0), np.max(param_array_postprocess,
                                                    axis=0)
    param_array_postprocess = (param_array_postprocess -
                               param_mins) / (param_maxs - param_mins)
    #for i in range(len(param_mins)):
    #    param_array_postprocess[:,i] = (param_array_postprocess[:,i]-param_mins[i])/(param_maxs[i]-param_mins[i])

    svd_model = {}
    for jj, filt in enumerate(filters):
        mag_array = []
        for key in magkeys:
            mag_array.append(mags[key]["data"][:, jj])

        mag_array_postprocess = np.array(mag_array)
        nmag, ntime = mag_array_postprocess.shape
        mag_array_postprocess_mean = np.median(mag_array_postprocess, axis=0)
        for i in range(nmag):
            mag_array_postprocess[i, :] = mag_array_postprocess[
                i, :] - mag_array_postprocess_mean

        mins, maxs = np.min(mag_array_postprocess,
                            axis=0), np.max(mag_array_postprocess, axis=0)
        for i in range(len(mins)):
            mag_array_postprocess[:, i] = (mag_array_postprocess[:, i] -
                                           mins[i]) / (maxs[i] - mins[i])
        mag_array_postprocess[np.isnan(mag_array_postprocess)] = 0.0
        UA, sA, VA = np.linalg.svd(mag_array_postprocess, full_matrices=True)
        VA = VA.T

        n, n = UA.shape
        m, m = VA.shape

        cAmat = np.zeros((n_coeff, n))
        cAvar = np.zeros((n_coeff, n))
        for i in range(n):
            ErrorLevel = 0.01
            cAmat[:, i] = np.dot(mag_array_postprocess[i, :], VA[:, :n_coeff])
            errors = ErrorLevel * np.ones_like(mag_array_postprocess[i, :])
            cAvar[:, i] = np.diag(
                np.dot(VA[:, :n_coeff].T,
                       np.dot(np.diag(np.power(errors, 2.)), VA[:, :n_coeff])))
        cAstd = np.sqrt(cAvar)

        nsvds, nparams = np.atleast_2d(param_array_postprocess).shape
        kernel = 1.0 * RationalQuadratic(length_scale=1.0, alpha=0.1)
        gps = []
        for i in range(n_coeff):
            gp = GaussianProcessRegressor(kernel=kernel,
                                          n_restarts_optimizer=0)
            gp.fit(np.atleast_2d(param_array_postprocess).T, cAmat[i, :])
            gps.append(gp)

        svd_model[filt] = {}
        svd_model[filt]["n_coeff"] = n_coeff
        svd_model[filt]["param_array"] = param_array
        svd_model[filt]["cAmat"] = cAmat
        svd_model[filt]["cAstd"] = cAstd
        svd_model[filt]["VA"] = VA
        svd_model[filt]["param_mins"] = param_mins
        svd_model[filt]["param_maxs"] = param_maxs
        svd_model[filt]["mins"] = mins
        svd_model[filt]["maxs"] = maxs
        svd_model[filt]["gps"] = gps
        svd_model[filt]["tt"] = tt

    print("Finished calculating SVD model of inclination colors...")

    return svd_model
Beispiel #4
0
def calc_svd_mag(tini, tmax, dt, n_coeff=100, model="BaKa2016"):

    print("Calculating SVD model of lightcurve magnitudes...")

    if model == "BaKa2016":
        fileDir = "../output/barnes_kilonova_spectra"
    elif model == "Ka2017":
        fileDir = "../output/kasen_kilonova_grid"
    elif model == "RoFe2017":
        fileDir = "../output/macronovae-rosswog_wind"
    elif model == "Bu2019":
        fileDir = "../output/bulla_1D"
        fileDir = "../output/bulla_1D_phi0"
        fileDir = "../output/bulla_1D_phi90"
    elif model == "Bu2019inc":
        fileDir = "../output/bulla_2D"
    elif model == "Bu2019lf":
        fileDir = "../output/bulla_2Component_lfree"
    elif model == "Bu2019lr":
        fileDir = "../output/bulla_2Component_lrich"
    elif model == "Bu2019lm":
        fileDir = "../output/bulla_2Component_lmid"
    elif model == "Bu2019lw":
        fileDir = "../output/bulla_2Component_lmid_0p005"
    elif model == "Bu2019bc":
        fileDir = "../output/bulla_blue_cone"
    elif model == "Bu2019re":
        fileDir = "../output/bulla_red_ellipse"
    elif model == "Bu2019op":
        fileDir = "../output/bulla_opacity"
    elif model == "Bu2019ops":
        fileDir = "../output/bulla_opacity_slim"
    elif model == "Bu2019rp":
        fileDir = "../output/bulla_reprocess"
    elif model == "Bu2019rps":
        fileDir = "../output/bulla_reprocess_slim"

    filenames_all = glob.glob('%s/*.dat' % fileDir)
    idxs = []
    for ii, filename in enumerate(filenames_all):
        if "_Lbol.dat" in filename: continue
        if "_spec.dat" in filename: continue
        idxs.append(ii)
    filenames = [filenames_all[idx] for idx in idxs]

    mags, names = lightcurve_utils.read_files(filenames)
    magkeys = mags.keys()

    tt = np.arange(tini, tmax + dt, dt)
    filters = ["u", "g", "r", "i", "z", "y", "J", "H", "K"]

    for jj, key in enumerate(magkeys):
        print('Setup %s: %d/%d' % (key, jj, len(magkeys) + 1))

        keySplit = key.split("_")
        if keySplit[0] == "rpft":
            mej0 = float("0." + keySplit[1].replace("m", ""))
            vej0 = float("0." + keySplit[2].replace("v", ""))
            mags[key]["mej"] = mej0
            mags[key]["vej"] = vej0
        elif keySplit[0] == "knova":
            mej0 = float(keySplit[3].replace("m", ""))
            vej0 = float(keySplit[4].replace("vk", ""))
            if len(keySplit) == 6:
                Xlan0 = 10**float(keySplit[5].replace("Xlan1e", ""))
            elif len(keySplit) == 7:
                #del mags[key]
                #continue
                if "Xlan1e" in keySplit[6]:
                    Xlan0 = 10**float(keySplit[6].replace("Xlan1e", ""))
                elif "Xlan1e" in keySplit[5]:
                    Xlan0 = 10**float(keySplit[5].replace("Xlan1e", ""))

            #if (mej0 == 0.05) and (vej0 == 0.2) and (Xlan0 == 1e-3):
            #    del mags[key]
            #    continue

            mags[key]["mej"] = mej0
            mags[key]["vej"] = vej0
            mags[key]["Xlan"] = Xlan0
        elif keySplit[0] == "SED":
            mags[key]["mej"], mags[key]["vej"], mags[key][
                "Ye"] = lightcurve_utils.get_macronovae_rosswog(key)

        elif "gamma" in key:
            kappaLF = float(keySplit[2].replace("kappaLF", ""))
            gammaLF = float(keySplit[3].replace("gammaLF", ""))
            kappaLR = float(keySplit[4].replace("kappaLR", ""))
            gammaLR = float(keySplit[5].replace("gammaLR", ""))
            theta = float(keySplit[6])

            mags[key]["kappaLF"] = kappaLF
            mags[key]["gammaLF"] = gammaLF
            mags[key]["kappaLR"] = kappaLR
            mags[key]["gammaLR"] = gammaLR
            mags[key]["theta"] = theta

        elif "nsns" in key:

            mejdyn = float(keySplit[2].replace("mejdyn", ""))
            mejwind = float(keySplit[3].replace("mejwind", ""))
            phi0 = float(keySplit[4].replace("phi", ""))
            theta = float(keySplit[5])

            mags[key]["mej_dyn"] = mejdyn
            mags[key]["mej_wind"] = mejwind
            mags[key]["phi"] = phi0
            mags[key]["theta"] = theta

        elif "mejdyn" in key:

            mejdyn = float(keySplit[1].replace("mejdyn", ""))
            mejwind = float(keySplit[2].replace("mejwind", ""))
            phi0 = float(keySplit[4].replace("phi", ""))
            theta = float(keySplit[5])

            mags[key]["mej_dyn"] = mejdyn
            mags[key]["mej_wind"] = mejwind
            mags[key]["phi"] = phi0
            mags[key]["theta"] = theta

        elif "bluecone" in key:

            mej = float(keySplit[2].replace("mej", ""))
            phi0 = float(keySplit[3].replace("th", ""))
            theta = float(keySplit[4])

            mags[key]["mej"] = mej
            mags[key]["phi"] = 90 - phi0
            mags[key]["theta"] = theta

        elif "redellips" in key:

            mej = float(keySplit[2].replace("mej", ""))
            a0 = float(keySplit[3].replace("a", ""))
            theta = float(keySplit[4])

            mags[key]["mej"] = mej
            mags[key]["a"] = a0
            mags[key]["theta"] = theta

        elif keySplit[0] == "nph1.0e+06":
            #if len(keySplit) == 5:
            #    mej0 = float(keySplit[3].replace("mej",""))
            #    phi0 = float(keySplit[2].replace("opang",""))
            #    T0 = float(keySplit[4].replace("T",""))
            #elif len(keySplit) == 6:
            #    mej0 = float(keySplit[2].replace("mej",""))
            #    phi0 = float(keySplit[3].replace("phi",""))
            #    T0 = float(keySplit[4].replace("T",""))
            #    theta = float(keySplit[5])

            mej0 = float(keySplit[1].replace("mej", ""))
            phi0 = float(keySplit[2].replace("phi", ""))
            theta = float(keySplit[3])

            mags[key]["mej"] = mej0
            mags[key]["phi"] = phi0
            mags[key]["theta"] = theta

        elif keySplit[0] == "kasenReprocess":

            mej1 = float(keySplit[2].replace("mejcone", ""))
            mej2 = float(keySplit[3].replace("mejell", ""))
            phi = float(keySplit[4].replace("th", ""))
            a = float(keySplit[5].replace("a", ""))
            theta = float(keySplit[6])

            mags[key]["mej_1"] = mej1
            mags[key]["mej_2"] = mej2
            mags[key]["phi"] = phi
            mags[key]["a"] = a
            mags[key]["theta"] = theta

        mags[key]["data"] = np.zeros((len(tt), len(filters)))

        for jj, filt in enumerate(filters):
            ii = np.where(np.isfinite(mags[key][filt]))[0]
            f = interp.interp1d(mags[key]["t"][ii],
                                mags[key][filt][ii],
                                fill_value='extrapolate')
            maginterp = f(tt)
            mags[key]["data"][:, jj] = maginterp

        mags[key]["data_vector"] = np.reshape(mags[key]["data"],
                                              (len(tt) * len(filters), 1))

    magkeys = mags.keys()
    param_array = []
    for key in magkeys:
        if model == "BaKa2016":
            param_array.append([np.log10(mags[key]["mej"]), mags[key]["vej"]])
        elif model == "Ka2017":
            param_array.append([
                np.log10(mags[key]["mej"]),
                np.log10(mags[key]["vej"]),
                np.log10(mags[key]["Xlan"])
            ])
        elif model == "RoFe2017":
            param_array.append([
                np.log10(mags[key]["mej"]), mags[key]["vej"], mags[key]["Ye"]
            ])
        elif model == "Bu2019":
            param_array.append(
                [np.log10(mags[key]["mej"]),
                 np.log10(mags[key]["T"])])
        elif model == "Bu2019inc":
            param_array.append([
                np.log10(mags[key]["mej"]), mags[key]["phi"],
                mags[key]["theta"]
            ])
        elif model in ["Bu2019lf", "Bu2019lr", "Bu2019lm"]:
            param_array.append([
                np.log10(mags[key]["mej_dyn"]),
                np.log10(mags[key]["mej_wind"]), mags[key]["phi"],
                mags[key]["theta"]
            ])
        elif model in ["Bu2019lw"]:
            param_array.append([
                np.log10(mags[key]["mej_wind"]), mags[key]["phi"],
                mags[key]["theta"]
            ])
        elif model == "Bu2019bc":
            param_array.append([
                np.log10(mags[key]["mej"]), mags[key]["phi"],
                mags[key]["theta"]
            ])
        elif model == "Bu2019re":
            param_array.append([
                np.log10(mags[key]["mej"]), mags[key]["a"], mags[key]["theta"]
            ])
        elif model == "Bu2019op":
            param_array.append([
                np.log10(mags[key]["kappaLF"]), mags[key]["gammaLF"],
                np.log10(mags[key]["kappaLR"]), mags[key]["gammaLR"]
            ])
        elif model == "Bu2019ops":
            param_array.append([
                np.log10(mags[key]["kappaLF"]),
                np.log10(mags[key]["kappaLR"]), mags[key]["gammaLR"]
            ])
        elif model == "Bu2019rp":
            param_array.append([
                np.log10(mags[key]["mej_1"]),
                np.log10(mags[key]["mej_2"]), mags[key]["phi"], mags[key]["a"],
                mags[key]["theta"]
            ])
        elif model == "Bu2019rps":
            param_array.append([
                np.log10(mags[key]["mej_1"]),
                np.log10(mags[key]["mej_2"]), mags[key]["a"]
            ])

    param_array_postprocess = np.array(param_array)
    param_mins, param_maxs = np.min(param_array_postprocess,
                                    axis=0), np.max(param_array_postprocess,
                                                    axis=0)
    for i in range(len(param_mins)):
        param_array_postprocess[:, i] = (param_array_postprocess[:, i] -
                                         param_mins[i]) / (param_maxs[i] -
                                                           param_mins[i])

    svd_model = {}
    for jj, filt in enumerate(filters):
        print('Computing filter %s...' % filt)
        mag_array = []
        for key in magkeys:
            mag_array.append(mags[key]["data"][:, jj])

        mag_array_postprocess = np.array(mag_array)
        mins, maxs = np.min(mag_array_postprocess,
                            axis=0), np.max(mag_array_postprocess, axis=0)
        for i in range(len(mins)):
            mag_array_postprocess[:, i] = (mag_array_postprocess[:, i] -
                                           mins[i]) / (maxs[i] - mins[i])
        mag_array_postprocess[np.isnan(mag_array_postprocess)] = 0.0
        UA, sA, VA = np.linalg.svd(mag_array_postprocess, full_matrices=True)
        VA = VA.T

        n, n = UA.shape
        m, m = VA.shape

        cAmat = np.zeros((n_coeff, n))
        cAvar = np.zeros((n_coeff, n))
        for i in range(n):
            ErrorLevel = 1.0
            cAmat[:, i] = np.dot(mag_array_postprocess[i, :], VA[:, :n_coeff])
            errors = ErrorLevel * np.ones_like(mag_array_postprocess[i, :])
            cAvar[:, i] = np.diag(
                np.dot(VA[:, :n_coeff].T,
                       np.dot(np.diag(np.power(errors, 2.)), VA[:, :n_coeff])))
        cAstd = np.sqrt(cAvar)

        nsvds, nparams = param_array_postprocess.shape
        kernel = 1.0 * RationalQuadratic(length_scale=1.0, alpha=0.1)
        gps = []
        for i in range(n_coeff):
            if np.mod(i, 5) == 0:
                print('Coefficient %d/%d...' % (i, n_coeff))
            gp = GaussianProcessRegressor(kernel=kernel,
                                          n_restarts_optimizer=0)
            gp.fit(param_array_postprocess, cAmat[i, :])
            gps.append(gp)

        svd_model[filt] = {}
        svd_model[filt]["n_coeff"] = n_coeff
        svd_model[filt]["param_array"] = param_array
        svd_model[filt]["cAmat"] = cAmat
        svd_model[filt]["cAstd"] = cAstd
        svd_model[filt]["VA"] = VA
        svd_model[filt]["param_mins"] = param_mins
        svd_model[filt]["param_maxs"] = param_maxs
        svd_model[filt]["mins"] = mins
        svd_model[filt]["maxs"] = maxs
        svd_model[filt]["gps"] = gps
        svd_model[filt]["tt"] = tt

    print("Finished calculating SVD model of lightcurve magnitudes...")

    return svd_model
Beispiel #5
0
def calc_svd_mag(tini,tmax,dt, n_coeff = 100, model = "BaKa2016"):

    print("Calculating SVD model of lightcurve magnitudes...")

    if model == "BaKa2016":
        fileDir = "../output/barnes_kilonova_spectra"
    elif model == "Ka2017":
        fileDir = "../output/kasen_kilonova_grid"
    elif model == "RoFe2017":
        fileDir = "../output/macronovae-rosswog_wind"

    filenames_all = glob.glob('%s/*.dat'%fileDir)
    idxs = []
    for ii,filename in enumerate(filenames_all):
        if "_Lbol.dat" in filename: continue
        if "_spec.dat" in filename: continue
        idxs.append(ii)
    filenames = [filenames_all[idx] for idx in idxs]

    mags, names = lightcurve_utils.read_files(filenames)
    magkeys = mags.keys()

    tt = np.arange(tini,tmax+dt,dt)
    filters = ["u","g","r","i","z","y","J","H","K"]

    for key in magkeys:
        keySplit = key.split("_")
        if keySplit[0] == "rpft":
            mej0 = float("0." + keySplit[1].replace("m",""))
            vej0 = float("0." + keySplit[2].replace("v",""))
            mags[key]["mej"] = mej0
            mags[key]["vej"] = vej0
        elif keySplit[0] == "knova":
            mej0 = float(keySplit[3].replace("m",""))
            vej0 = float(keySplit[4].replace("vk",""))
            if len(keySplit) == 6:
                Xlan0 = 10**float(keySplit[5].replace("Xlan1e",""))
            elif len(keySplit) == 7:
                #del mags[key]
                #continue
                if "Xlan1e" in keySplit[6]:
                    Xlan0 = 10**float(keySplit[6].replace("Xlan1e",""))
                elif "Xlan1e" in keySplit[5]:
                    Xlan0 = 10**float(keySplit[5].replace("Xlan1e","")) 

            #if (mej0 == 0.05) and (vej0 == 0.2) and (Xlan0 == 1e-3):
            #    del mags[key]
            #    continue
 
            mags[key]["mej"] = mej0
            mags[key]["vej"] = vej0
            mags[key]["Xlan"] = Xlan0
        elif keySplit[0] == "SED":
            mags[key]["mej"], mags[key]["vej"], mags[key]["Ye"] = lightcurve_utils.get_macronovae_rosswog(key)

        mags[key]["data"] = np.zeros((len(tt),len(filters)))

        for jj,filt in enumerate(filters):
            ii = np.where(np.isfinite(mags[key][filt]))[0]
            f = interp.interp1d(mags[key]["t"][ii], mags[key][filt][ii], fill_value='extrapolate')
            maginterp = f(tt)
            mags[key]["data"][:,jj] = maginterp

        mags[key]["data_vector"] = np.reshape(mags[key]["data"],len(tt)*len(filters),1)

    magkeys = mags.keys()
    param_array = []
    for key in magkeys:
        if model == "BaKa2016":
            param_array.append([np.log10(mags[key]["mej"]),mags[key]["vej"]])
        elif model == "Ka2017":
            param_array.append([np.log10(mags[key]["mej"]),np.log10(mags[key]["vej"]),np.log10(mags[key]["Xlan"])])
        elif model == "RoFe2017":
            param_array.append([np.log10(mags[key]["mej"]),mags[key]["vej"],mags[key]["Ye"]])    

    param_array_postprocess = np.array(param_array)
    param_mins, param_maxs = np.min(param_array_postprocess,axis=0),np.max(param_array_postprocess,axis=0)
    for i in range(len(param_mins)):
        param_array_postprocess[:,i] = (param_array_postprocess[:,i]-param_mins[i])/(param_maxs[i]-param_mins[i])

    svd_model = {}
    for jj,filt in enumerate(filters):
        print('Computing filter %s...' % filt)
        mag_array = []
        for key in magkeys:
            mag_array.append(mags[key]["data"][:,jj])

        mag_array_postprocess = np.array(mag_array)
        mins,maxs = np.min(mag_array_postprocess,axis=0),np.max(mag_array_postprocess,axis=0)
        for i in range(len(mins)):
            mag_array_postprocess[:,i] = (mag_array_postprocess[:,i]-mins[i])/(maxs[i]-mins[i])
        mag_array_postprocess[np.isnan(mag_array_postprocess)]=0.0
        UA, sA, VA = np.linalg.svd(mag_array_postprocess, full_matrices=True)
        VA = VA.T

        n, n = UA.shape
        m, m = VA.shape

        cAmat = np.zeros((n_coeff,n))
        cAvar = np.zeros((n_coeff,n))
        for i in range(n):
            ErrorLevel = 1.0
            cAmat[:,i] = np.dot(mag_array_postprocess[i,:],VA[:,:n_coeff])
            errors = ErrorLevel*np.ones_like(mag_array_postprocess[i,:])
            cAvar[:,i] = np.diag(np.dot(VA[:,:n_coeff].T,np.dot(np.diag(np.power(errors,2.)),VA[:,:n_coeff])))
        cAstd = np.sqrt(cAvar)

        nsvds, nparams = param_array_postprocess.shape
        kernel = 1.0 * RationalQuadratic(length_scale=1.0, alpha=0.1)
        gps = []
        for i in range(n_coeff):
            if np.mod(i,5) == 0:
                print('Coefficient %d/%d...' % (i, n_coeff))

            gp = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=0)
            gp.fit(param_array_postprocess, cAmat[i,:])
            gps.append(gp)

        svd_model[filt] = {}
        svd_model[filt]["n_coeff"] = n_coeff
        svd_model[filt]["param_array"] = param_array
        svd_model[filt]["cAmat"] = cAmat
        svd_model[filt]["cAstd"] = cAstd
        svd_model[filt]["VA"] = VA
        svd_model[filt]["param_mins"] = param_mins
        svd_model[filt]["param_maxs"] = param_maxs
        svd_model[filt]["mins"] = mins
        svd_model[filt]["maxs"] = maxs
        svd_model[filt]["gps"] = gps
        svd_model[filt]["tt"] = tt

    print("Finished calculating SVD model of lightcurve magnitudes...")

    return svd_model
    os.makedirs(plotDir)

data = {}
legend_names = {}
for mej2 in mej2s:
    name = "Ka2017x2_M%03dV%02dX%d_M%03dV%02dX%d" % (
        mej1 * 1000, vej1 * 100, np.log10(Xlan1), mej2 * 1000, vej2 * 100,
        np.log10(Xlan2))
    filename = os.path.join(outputDir, "%s.dat" % name)

    if not os.path.isfile(filename):
        system_call = "python run_parameterized_models.py --doAB --doEjecta --model Ka2017x2 --mej1 %.5f --vej1 %.5f --Xlan1 %.5f --mej2 %.5f --vej2 %.5f --Xlan2 %.5f" % (
            mej1, vej1, Xlan1, mej2, vej2, Xlan2)
        os.system(system_call)

    mag_d = lightcurve_utils.read_files([filename])
    key = mag_d[0].keys()[0]
    mag_d = mag_d[0][key]

    data_out = np.loadtxt(filename)
    data[name] = mag_d
    legend_names[name] = "%.3f" % mej2

pcklFile = "%s/data.pkl" % (plotDir)
f = open(pcklFile, 'wb')
pickle.dump((data), f)
f.close()

filts = ["u", "g", "r", "i", "z", "y", "J", "H", "K"]
colors = cm.rainbow(np.linspace(0, 1, len(filts)))
magidxs = [0, 1, 2, 3, 4, 5, 6, 7, 8]
Beispiel #7
0
def calc_svd_mag(tini,tmax,dt, n_coeff = 100, model = "BaKa2016",
                 gptype="sklearn"):

    print("Calculating SVD model of lightcurve magnitudes...")

    if model == "BaKa2016":
        fileDir = "../output/barnes_kilonova_spectra"
    elif model == "Ka2017":
        fileDir = "../output/kasen_kilonova_grid"
    elif model == "RoFe2017":
        fileDir = "../output/macronovae-rosswog_wind"
    elif model == "Bu2019":
        fileDir = "../output/bulla_1D"
        fileDir = "../output/bulla_1D_phi0"
        fileDir = "../output/bulla_1D_phi90"
    elif model == "Bu2019inc":
        fileDir = "../output/bulla_2D"
    elif model == "Bu2019lf":
        fileDir = "../output/bulla_2Component_lfree"
    elif model == "Bu2019lr":
        fileDir = "../output/bulla_2Component_lrich"
    elif model == "Bu2019lm":
        fileDir = "../output/bulla_2Component_lmid"
    elif model == "Bu2019lw":
        fileDir = "../output/bulla_2Component_lmid_0p005"
    elif model == "Bu2019bc":
        fileDir = "../output/bulla_blue_cone"
    elif model == "Bu2019re":
        fileDir = "../output/bulla_red_ellipse"
    elif model == "Bu2019op":
        fileDir = "../output/bulla_opacity"
    elif model == "Bu2019ops":
        fileDir = "../output/bulla_opacity_slim"
    elif model == "Bu2019rp":
        fileDir = "../output/bulla_reprocess"
    elif model == "Bu2019rps":
        fileDir = "../output/bulla_reprocess_slim"
    elif model == "Bu2019rpd":
        fileDir = "../output/bulla_reprocess_decimated"
    elif model == "Bu2019nsbh":
        fileDir = "../output/bulla_2Component_lnsbh"
    elif model == "Wo2020dyn":
        fileDir = "../output/bulla_rosswog_dynamical"
    elif model == "Wo2020dw":
        fileDir = "../output/bulla_rosswog_wind"

    filenames_all = glob.glob('%s/*.dat'%fileDir)
    idxs = []
    for ii,filename in enumerate(filenames_all):
        if "_Lbol.dat" in filename: continue
        if "_spec.dat" in filename: continue
        idxs.append(ii)
    filenames = [filenames_all[idx] for idx in idxs]

    mags, names = lightcurve_utils.read_files(filenames)
    magkeys = mags.keys()

    tt = np.arange(tini,tmax+dt,dt)
    filters = ["u","g","r","i","z","y","J","H","K"]

    for jj, key in enumerate(magkeys):
        print('Setup %s: %d/%d' % (key, jj, len(magkeys)+1))
        
        keySplit = key.split("_")
        if keySplit[0] == "rpft":
            mej0 = float("0." + keySplit[1].replace("m",""))
            vej0 = float("0." + keySplit[2].replace("v",""))
            mags[key]["mej"] = mej0
            mags[key]["vej"] = vej0
        elif keySplit[0] == "knova":
            mej0 = float(keySplit[3].replace("m",""))
            vej0 = float(keySplit[4].replace("vk",""))
            if len(keySplit) == 6:
                Xlan0 = 10**float(keySplit[5].replace("Xlan1e",""))
            elif len(keySplit) == 7:
                #del mags[key]
                #continue
                if "Xlan1e" in keySplit[6]:
                    Xlan0 = 10**float(keySplit[6].replace("Xlan1e",""))
                elif "Xlan1e" in keySplit[5]:
                    Xlan0 = 10**float(keySplit[5].replace("Xlan1e","")) 

            #if (mej0 == 0.05) and (vej0 == 0.2) and (Xlan0 == 1e-3):
            #    del mags[key]
            #    continue
 
            mags[key]["mej"] = mej0
            mags[key]["vej"] = vej0
            mags[key]["Xlan"] = Xlan0
        elif keySplit[0] == "SED":
            mags[key]["mej"], mags[key]["vej"], mags[key]["Ye"] = lightcurve_utils.get_macronovae_rosswog(key)

        elif "gamma" in key:
            kappaLF = float(keySplit[2].replace("kappaLF",""))
            gammaLF = float(keySplit[3].replace("gammaLF",""))
            kappaLR = float(keySplit[4].replace("kappaLR",""))
            gammaLR = float(keySplit[5].replace("gammaLR",""))
            theta = float(keySplit[6])

            mags[key]["kappaLF"] = kappaLF
            mags[key]["gammaLF"] = gammaLF
            mags[key]["kappaLR"] = kappaLR
            mags[key]["gammaLR"] = gammaLR
            mags[key]["theta"] = theta

        elif "nsns" in key:

            mejdyn = float(keySplit[2].replace("mejdyn",""))
            mejwind = float(keySplit[3].replace("mejwind",""))
            phi0 = float(keySplit[4].replace("phi",""))
            theta = float(keySplit[5])

            mags[key]["mej_dyn"] = mejdyn
            mags[key]["mej_wind"] = mejwind
            mags[key]["phi"] = phi0
            mags[key]["theta"] = theta

        elif keySplit[0] == "nsbh":

            mej_dyn = float(keySplit[2].replace("mejdyn",""))
            mej_wind = float(keySplit[3].replace("mejwind",""))
            phi = float(keySplit[4].replace("phi",""))
            theta = float(keySplit[5])

            mags[key]["mej_dyn"] = mej_dyn
            mags[key]["mej_wind"] = mej_wind
            #mags[key]["phi"] = phi
            mags[key]["theta"] = theta

        elif "mejdyn" in key:

            mejdyn = float(keySplit[1].replace("mejdyn",""))
            mejwind = float(keySplit[2].replace("mejwind",""))
            phi0 = float(keySplit[4].replace("phi",""))
            theta = float(keySplit[5])

            mags[key]["mej_dyn"] = mejdyn
            mags[key]["mej_wind"] = mejwind
            mags[key]["phi"] = phi0
            mags[key]["theta"] = theta

        elif "bluecone" in key:

            mej = float(keySplit[2].replace("mej",""))
            phi0 = float(keySplit[3].replace("th",""))
            theta = float(keySplit[4])

            mags[key]["mej"] = mej
            mags[key]["phi"] = 90-phi0
            mags[key]["theta"] = theta

        elif "redellips" in key:

            mej = float(keySplit[2].replace("mej",""))
            a0 = float(keySplit[3].replace("a",""))
            theta = float(keySplit[4])

            mags[key]["mej"] = mej
            mags[key]["a"] = a0
            mags[key]["theta"] = theta

        elif keySplit[0] == "nph1.0e+06":
            #if len(keySplit) == 5:
            #    mej0 = float(keySplit[3].replace("mej",""))
            #    phi0 = float(keySplit[2].replace("opang",""))
            #    T0 = float(keySplit[4].replace("T",""))
            #elif len(keySplit) == 6:
            #    mej0 = float(keySplit[2].replace("mej",""))
            #    phi0 = float(keySplit[3].replace("phi",""))
            #    T0 = float(keySplit[4].replace("T",""))
            #    theta = float(keySplit[5])

            
            mej0 = float(keySplit[1].replace("mej",""))
            phi0 = float(keySplit[2].replace("phi",""))
            theta = float(keySplit[3])

            mags[key]["mej"] = mej0
            mags[key]["phi"] = phi0
            mags[key]["theta"] = theta

        elif keySplit[0] == "kasenReprocess":

            mej1 = float(keySplit[2].replace("mejcone",""))
            mej2 = float(keySplit[3].replace("mejell",""))
            phi = float(keySplit[4].replace("th",""))
            a = float(keySplit[5].replace("a",""))
            theta = float(keySplit[6])

            mags[key]["mej_1"] = mej1
            mags[key]["mej_2"] = mej2
            mags[key]["phi"] = phi
            mags[key]["a"] = a
            mags[key]["theta"] = theta

        elif keySplit[0] == "RGAdyn":

            mej = float(keySplit[2].replace("mej",""))
            a = float(keySplit[3].replace("a",""))
            sd = float(keySplit[4].replace("sd",""))
            theta = float(keySplit[5])

            mags[key]["mej"] = mej
            mags[key]["a"] = a
            mags[key]["sd"] = sd
            mags[key]["theta"] = theta

        elif keySplit[0] == "WoWind":

            mej = float(keySplit[2].replace("mej",""))
            rwind = float(keySplit[3].replace("v",""))
            theta = float(keySplit[4])

            mags[key]["mej"] = mej
            mags[key]["rwind"] = rwind
            mags[key]["theta"] = theta

        mags[key]["data"] = np.zeros((len(tt),len(filters)))

        for jj,filt in enumerate(filters):
            ii = np.where(np.isfinite(mags[key][filt]))[0]
            f = interp.interp1d(mags[key]["t"][ii], mags[key][filt][ii], fill_value='extrapolate')
            maginterp = f(tt)
            mags[key]["data"][:,jj] = maginterp

        mags[key]["data_vector"] = np.reshape(mags[key]["data"],(len(tt)*len(filters),1))

    magkeys = mags.keys()
    param_array = []
    for key in magkeys:
        if model == "BaKa2016":
            param_array.append([np.log10(mags[key]["mej"]),mags[key]["vej"]])
        elif model == "Ka2017":
            param_array.append([np.log10(mags[key]["mej"]),np.log10(mags[key]["vej"]),np.log10(mags[key]["Xlan"])])
        elif model == "RoFe2017":
            param_array.append([np.log10(mags[key]["mej"]),mags[key]["vej"],mags[key]["Ye"]])    
        elif model == "Bu2019":
            param_array.append([np.log10(mags[key]["mej"]),np.log10(mags[key]["T"])])
        elif model == "Bu2019inc":
            param_array.append([np.log10(mags[key]["mej"]),mags[key]["phi"],mags[key]["theta"]])
        elif model in ["Bu2019lf","Bu2019lr","Bu2019lm"]:
            param_array.append([np.log10(mags[key]["mej_dyn"]),np.log10(mags[key]["mej_wind"]),mags[key]["phi"],mags[key]["theta"]])
        elif model in ["Bu2019nsbh"]:
            param_array.append([np.log10(mags[key]["mej_dyn"]),np.log10(mags[key]["mej_wind"]),mags[key]["theta"]])
        elif model in ["Bu2019lw"]:
            param_array.append([np.log10(mags[key]["mej_wind"]),mags[key]["phi"],mags[key]["theta"]])
        elif model == "Bu2019bc":
            param_array.append([np.log10(mags[key]["mej"]),mags[key]["phi"],mags[key]["theta"]])
        elif model == "Bu2019re":
            param_array.append([np.log10(mags[key]["mej"]),mags[key]["a"],mags[key]["theta"]])
        elif model == "Bu2019op":
            param_array.append([np.log10(mags[key]["kappaLF"]),mags[key]["gammaLF"],np.log10(mags[key]["kappaLR"]),mags[key]["gammaLR"]])
        elif model == "Bu2019ops":
            param_array.append([np.log10(mags[key]["kappaLF"]),np.log10(mags[key]["kappaLR"]),mags[key]["gammaLR"]])
        elif model in ["Bu2019rp","Bu2019rpd"]:
            param_array.append([np.log10(mags[key]["mej_1"]),np.log10(mags[key]["mej_2"]),mags[key]["phi"],mags[key]["a"],mags[key]["theta"]])
        elif model == "Bu2019rps":
            param_array.append([np.log10(mags[key]["mej_1"]),np.log10(mags[key]["mej_2"]),mags[key]["a"]])
        elif model == "Wo2020dyn":
            param_array.append([np.log10(mags[key]["mej"]),mags[key]["a"],mags[key]["sd"],mags[key]["theta"]])
        elif model == "Wo2020dw":
            param_array.append([np.log10(mags[key]["mej"]),mags[key]["rwind"],mags[key]["theta"]])

    param_array_postprocess = np.array(param_array)
    param_mins, param_maxs = np.min(param_array_postprocess,axis=0),np.max(param_array_postprocess,axis=0)
    for i in range(len(param_mins)):
        param_array_postprocess[:,i] = (param_array_postprocess[:,i]-param_mins[i])/(param_maxs[i]-param_mins[i])

    svd_model = {}
    for jj,filt in enumerate(filters):
        print('Computing filter %s...' % filt)
        mag_array = []
        for key in magkeys:
            mag_array.append(mags[key]["data"][:,jj])

        mag_array_postprocess = np.array(mag_array)
        mins,maxs = np.min(mag_array_postprocess,axis=0),np.max(mag_array_postprocess,axis=0)
        for i in range(len(mins)):
            mag_array_postprocess[:,i] = (mag_array_postprocess[:,i]-mins[i])/(maxs[i]-mins[i])
        mag_array_postprocess[np.isnan(mag_array_postprocess)]=0.0
        UA, sA, VA = np.linalg.svd(mag_array_postprocess, full_matrices=True)
        VA = VA.T

        n, n = UA.shape
        m, m = VA.shape

        cAmat = np.zeros((n_coeff,n))
        cAvar = np.zeros((n_coeff,n))
        for i in range(n):
            ErrorLevel = 1.0
            cAmat[:,i] = np.dot(mag_array_postprocess[i,:],VA[:,:n_coeff])
            errors = ErrorLevel*np.ones_like(mag_array_postprocess[i,:])
            cAvar[:,i] = np.diag(np.dot(VA[:,:n_coeff].T,np.dot(np.diag(np.power(errors,2.)),VA[:,:n_coeff])))
        cAstd = np.sqrt(cAvar)

        nsvds, nparams = param_array_postprocess.shape

        if gptype == "sklearn":
            kernel = 1.0 * RationalQuadratic(length_scale=1.0, alpha=0.1)
            gps = []
            for i in range(n_coeff):
                if np.mod(i,5) == 0:
                    print('Coefficient %d/%d...' % (i, n_coeff))
                gp = GaussianProcessRegressor(kernel=kernel,
                                              n_restarts_optimizer=0)
                gp.fit(param_array_postprocess, cAmat[i,:])
                gps.append(gp)
        elif gptype == "gpytorch":
            # initialize likelihood and model
            likelihood = gpytorch.likelihoods.GaussianLikelihood()
            #training_iter = 10
            training_iter = 11

            gps = []
            for i in range(n_coeff):
                if np.mod(i,5) == 0:
                    print('Coefficient %d/%d...' % (i, n_coeff))

                train_x = torch.from_numpy(param_array_postprocess).float()
                train_y = torch.from_numpy(cAmat[i,:]).float()

                if torch.cuda.is_available():
                    train_x = train_x.cuda()
                    train_y = train_y.cuda()
                    likelihood = likelihood.cuda()

                model = ExactGPModel(train_x, train_y, likelihood)
                if torch.cuda.is_available():
                    model = model.cuda()

                # Find optimal model hyperparameters
                model.train()
                likelihood.train()

                # Use the adam optimizer
                optimizer = torch.optim.Adam([{'params': model.parameters()}],
                                             lr=0.1)

                # "Loss" for GPs - the marginal log likelihood
                mll = gpytorch.mlls.ExactMarginalLogLikelihood(likelihood,
                                                               model)

                for j in range(training_iter):
                    # Zero gradients from previous iteration
                    optimizer.zero_grad()
                    # Output from model
                    output = model(train_x)
                    # Calc loss and backprop gradients
                    loss = -mll(output, train_y)
                    loss.backward()
                    #print('Coeff %d/%d Iter %d/%d - Loss: %.3f   lengthscale: %.3f   noise: %.3f' % (i+1, n_coeff, j + 1, training_iter, loss.item(), model.covar_module.base_kernel.lengthscale.item(), model.likelihood.noise.item()))
                    optimizer.step()

                gps.append(model.state_dict())

        svd_model[filt] = {}
        svd_model[filt]["n_coeff"] = n_coeff
        svd_model[filt]["param_array"] = param_array
        svd_model[filt]["param_array_postprocess"] = param_array_postprocess
        svd_model[filt]["cAmat"] = cAmat
        svd_model[filt]["cAstd"] = cAstd
        svd_model[filt]["VA"] = VA
        svd_model[filt]["param_mins"] = param_mins
        svd_model[filt]["param_maxs"] = param_maxs
        svd_model[filt]["mins"] = mins
        svd_model[filt]["maxs"] = maxs
        svd_model[filt]["gps"] = gps
        svd_model[filt]["tt"] = tt

    print("Finished calculating SVD model of lightcurve magnitudes...")

    return svd_model