コード例 #1
0
    plt.close()

elif opts.doSpec:

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

    if opts.doEvent:
        events = opts.event.split(",")
        eventdata = {}
        for event in events:
            filename = "%s/%s.dat" % (spectraDir, event)
            data_out = lightcurve_utils.loadEventSpec(filename)
            eventdata[event] = data_out

    maxhist = -1e10
    colors = ["g", "r", "c", "y", "m"]
    plotName = "%s/models_spec.pdf" % (plotDir)
    plt.figure(figsize=(12, 10))
    for ii, name in enumerate(names):
        spec_d = specs[name]
コード例 #2
0
ファイル: svd_utils.py プロジェクト: landryp/gwemlightcurves
def calc_svd_spectra(tini,
                     tmax,
                     dt,
                     lambdaini,
                     lambdamax,
                     dlambda,
                     n_coeff=100,
                     model="BaKa2016"):

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

    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 = glob.glob('%s/*_spec.dat' % fileDir)

    specs, names = lightcurve_utils.read_files_spec(filenames)
    speckeys = specs.keys()

    tt = np.arange(tini, tmax + dt, dt)
    lambdas = np.arange(lambdaini, lambdamax + dlambda, dlambda)

    for key in speckeys:
        keySplit = key.split("_")
        if keySplit[0] == "rpft":
            mej0 = float("0." + keySplit[1].replace("m", ""))
            vej0 = float("0." + keySplit[2].replace("v", ""))
            specs[key]["mej"] = mej0
            specs[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 specs[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 specs[key]
            #    continue

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

        data = specs[key]["data"].T
        data[data == 0.0] = 1e-20
        f = interp.interp2d(specs[key]["t"],
                            specs[key]["lambda"],
                            np.log10(data),
                            kind='cubic')
        #specs[key]["data"] = (10**(f(tt,lambdas))).T
        specs[key]["data"] = f(tt, lambdas).T

    speckeys = specs.keys()
    param_array = []
    for key in speckeys:
        if model == "BaKa2016":
            param_array.append(
                [np.log10(specs[key]["mej"]), specs[key]["vej"]])
        elif model == "Ka2017":
            param_array.append([
                np.log10(specs[key]["mej"]), specs[key]["vej"],
                np.log10(specs[key]["Xlan"])
            ])
        elif model == "RoFe2017":
            param_array.append([
                np.log10(specs[key]["mej"]), specs[key]["vej"],
                specs[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, lambda_d in enumerate(lambdas):
        if np.mod(jj, 1) == 0:
            print("%d / %d" % (jj, len(lambdas)))

        spec_array = []
        for key in speckeys:
            spec_array.append(specs[key]["data"][:, jj])

        spec_array_postprocess = np.array(spec_array)
        mins, maxs = np.min(spec_array_postprocess,
                            axis=0), np.max(spec_array_postprocess, axis=0)
        for i in range(len(mins)):
            spec_array_postprocess[:, i] = (spec_array_postprocess[:, i] -
                                            mins[i]) / (maxs[i] - mins[i])
        spec_array_postprocess[np.isnan(spec_array_postprocess)] = 0.0
        UA, sA, VA = np.linalg.svd(spec_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))
        ErrorLevel = 2
        for i in range(n):
            cAmat[:, i] = np.dot(spec_array_postprocess[i, :], VA[:, :n_coeff])
            errors = ErrorLevel * spec_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):
            gp = GaussianProcessRegressor(kernel=kernel,
                                          n_restarts_optimizer=0)
            gp.fit(param_array_postprocess, cAmat[i, :])
            gps.append(gp)

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

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

    return svd_model