当前位置: 首页>>代码示例>>Python>>正文


Python ChainConsumer.chains[-1]方法代码示例

本文整理汇总了Python中chainconsumer.ChainConsumer.chains[-1]方法的典型用法代码示例。如果您正苦于以下问题:Python ChainConsumer.chains[-1]方法的具体用法?Python ChainConsumer.chains[-1]怎么用?Python ChainConsumer.chains[-1]使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在chainconsumer.ChainConsumer的用法示例。


在下文中一共展示了ChainConsumer.chains[-1]方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: random_obs

# 需要导入模块: from chainconsumer import ChainConsumer [as 别名]
# 或者: from chainconsumer.ChainConsumer import chains[-1] [as 别名]
def random_obs(temp_dir, seed):
    np.random.seed(seed)
    interp = generate_and_return()
    x1 = np.random.normal()
    # colour = np.random.normal(scale=0.1)
    colour = 0
    x0 = 1e-5
    # t0 = np.random.uniform(low=1000, high=2000)
    t0 = 1000
    z = np.random.uniform(low=0.1, high=1.0)

    # deltat = np.random.uniform(low=-20, high=0)
    # num_obs = np.random.randint(low=10, high=40)
    num_obs = 20
    deltat = -35

    filename = temp_dir + "/save_%d.npy" % seed

    if not os.path.exists(filename):
        ts = np.arange(t0 + deltat, (t0 + deltat) + 5 * num_obs, 5)

        times = np.array([[t, t + 0.05, t + 0.1, t + 0.2] for t in ts]).flatten()
        bands = [b for t in ts for b in ["desg", "desr", "desi", "desz"]]
        gains = np.ones(times.shape)
        skynoise = np.random.uniform(low=20, high=800) * 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")
        p = {"z": z, "t0": t0, "x0": x0, "x1": x1, "c": colour}
        model.set(z=z)
        print(seed, " Vals are ", p)
        lc = sncosmo.realize_lcs(obs, model, [p])[0]
        ston = (lc["flux"] / lc["fluxerr"]).max()

        model.set(t0=t0, x1=x1, c=colour, x0=x0)
        try:
            res, fitted_model = sncosmo.fit_lc(
                lc, model, ["t0", "x0", "x1", "c"], guess_amplitude=False, guess_t0=False
            )
        except ValueError:
            return np.nan, np.nan, x1, colour, num_obs, ston, deltat, z, 0

        fig = sncosmo.plot_lc(lc, model=fitted_model, errors=res.errors)
        fig.savefig(temp_dir + os.sep + "lc_%d.png" % seed, bbox_inches="tight", dpi=300)
        my_model = PerfectRedshift([lc], [z], t0, name="posterior%d" % seed)
        sampler = EnsembleSampler(temp_dir=temp_dir, num_burn=400, num_steps=1500)
        c = ChainConsumer()
        my_model.fit(sampler, chain_consumer=c)
        map = {"x0": "$x_0$", "x1": "$x_1$", "c": "$c$", "t0": "$t_0$"}
        parameters = [map[a] for a in res.vparam_names]

        mu1 = get_mu_from_chain(interped, c.chains[-1], c.parameters[-1])
        c.parameteers[-1].append(r"$\mu$")
        c.chains[-1] = np.hstack((c.chains[-1], mu1[:, None]))

        chain2 = np.random.multivariate_normal(res.parameters[1:], res.covariance, size=int(1e5))
        chain2 = np.hstack((chain2, get_mu_from_chain(interp, chain2, parameters)[:, None]))
        c.add_chain(chain2, parameters=parameters, name="Gaussian")
        figfilename = filename.replace(".npy", ".png")
        c.plot(filename=figfilename, truth={"$t_0$": t0, "$x_0$": x0, "$x_1$": x1, "$c$": colour})

        means = []
        stds = []
        isgood = (
            (np.abs(x1 - res.parameters[3]) < 4) & (np.abs(colour - res.parameters[4]) < 2) & (res.parameters[2] > 0.0)
        )
        isgood *= 1.0

        for i in range(len(c.chains)):
            a = c.chains[i][:, -1]
            means.append(a.mean())
            stds.append(np.std(a))
        diffmu = np.diff(means)[0]
        diffstd = np.diff(stds)[0]
        np.save(filename, np.array([diffmu, diffstd, ston, 1.0 * isgood]))

    else:
        vals = np.load(filename)
        diffmu = vals[0]
        diffstd = vals[1]
        ston = vals[2]
        isgood = vals[3]

    return diffmu, diffstd, x1, colour, num_obs, ston, deltat, z, isgood
开发者ID:dessn,项目名称:sn-bhm,代码行数:88,代码来源:general.py


注:本文中的chainconsumer.ChainConsumer.chains[-1]方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。