Beispiel #1
0
 def test_using_list(self):
     data = self.data.tolist()
     c = ChainConsumer()
     c.add_chain(data)
     summary = c.get_summary()
     actual = np.array(list(summary.values())[0])
     assert np.abs(actual[1] - 5.0) < 0.1
Beispiel #2
0
 def test_using_dict(self):
     data = {"x": self.data, "y": self.data2}
     c = ChainConsumer()
     c.add_chain(data)
     summary = c.get_summary()
     print(c.chains[0].shape)
     deviations = np.abs([summary["x"][1] - 5, summary["y"][1] - 3])
     assert np.all(deviations < 0.1)
Beispiel #3
0
def is_unconstrained(chain, param):
    c = ChainConsumer()
    c.add_chain(chain, parameters=param)
    constraints = c.get_summary()[0]
    for key in constraints:
        val = constraints[key]
        if val[0] is None or val[2] is None:
            return True
    return False
Beispiel #4
0
 def test_summary1(self):
     tolerance = 5e-2
     consumer = ChainConsumer()
     consumer.add_chain(self.data)
     consumer.configure_general(bins=1.6)
     summary = consumer.get_summary()
     actual = np.array(list(summary.values())[0])
     expected = np.array([3.5, 5.0, 6.5])
     diff = np.abs(expected - actual)
     assert np.all(diff < tolerance)
Beispiel #5
0
 def test_convergence_failure(self):
     data = np.concatenate(
         (np.random.normal(loc=0.0,
                           size=10000), np.random.normal(loc=4.0,
                                                         size=10000)))
     consumer = ChainConsumer()
     consumer.add_chain(data)
     summary = consumer.get_summary()
     actual = np.array(list(summary.values())[0])
     print(actual)
     assert actual[0] is None and actual[2] is None
Beispiel #6
0
 def test_file_loading2(self):
     data = self.data[:1000]
     directory = tempfile._get_default_tempdir()
     filename = next(tempfile._get_candidate_names())
     filename = directory + os.sep + filename + ".npy"
     np.save(filename, data)
     consumer = ChainConsumer()
     consumer.add_chain(filename)
     summary = consumer.get_summary()
     actual = np.array(list(summary.values())[0])
     assert np.abs(actual[1] - 5.0) < 0.5
Beispiel #7
0
 def test_summary2(self):
     tolerance = 5e-2
     consumer = ChainConsumer()
     consumer.add_chain(self.data_combined,
                        parameters=["a", "b"],
                        name="chain1")
     consumer.add_chain(self.data_combined, name="chain2")
     summary = consumer.get_summary()
     k1 = list(summary[0].keys())
     k2 = list(summary[1].keys())
     assert len(k1) == 2
     assert "a" in k1
     assert "b" in k1
     assert len(k2) == 2
     assert "a" in k2
     assert "b" in k2
     expected1 = np.array([3.5, 5.0, 6.5])
     expected2 = np.array([2.0, 3.0, 4.0])
     diff1 = np.abs(expected1 - np.array(list(summary[0]["a"])))
     diff2 = np.abs(expected2 - np.array(list(summary[0]["b"])))
     assert np.all(diff1 < tolerance)
     assert np.all(diff2 < tolerance)
Beispiel #8
0
                                    lnprob,
                                    args=(zhel, zcmb, mb, x1, color, thirdvar,
                                          Cmu, blind_values),
                                    threads=4)
    # run the sampler
    # how many steps (will have nSteps*nwalkers of samples)
    sampler.run_mcmc(pos, nSteps)

    if options.plot:
        burnin = 0  # change
        data = np.genfromtxt(
            options.chains +
            '/my_params_JLA_FlatwCDM_CPL_uncorrected_PV_blind_%s.txt' % date,
            delimiter=',')
        data = data[burnin:]
        c = ChainConsumer()
        c.add_chain(data,
                    parameters=[
                        r'$\Omega_m$', r'$w_0$', r'$\\alpha$', r'$\beta$',
                        r'$M_B$', r'$\Delta_M$'
                    ])
        params = c.get_summary()
        print params.keys()

        figw = c.plot_walks()
        figw.show()
        fig = c.plot()
        figw.savefig(options.chains + '/walks.png')
        fig.savefig(options.chains + '/marginals.png')
        fig.show()
        plt.plot(out_absc[20:], out_lines[20:], '-', color='k')
        plt.axhline(1.01)
        plt.savefig(dir_output + 'GRtrace_pam_' + repr(nd) + '.png', bbox_inches='tight')
        plt.close()

    print
    print '*************************************************************'
    print

if args.cc != 'False':
    cc = ChainConsumer()
    for nd in xrange(0, mc.ndim):  # (0,ndim):
        cc.add_chain(chain[:, :, nd].flatten(), walkers=mc.nwalkers)

    #print(cc.get_latex_table())
    print cc.get_summary()

    print cc.diagnostic_gelman_rubin(threshold=0.05)
    print cc.diagnostic_geweke()
    print
    print '*************************************************************'
    print

x0 = 1. / 150

M_star1_rand = np.random.normal(M_star1, M_star1_err, n_kept)

if 'kepler' in mc.model_list:

    if args.p != 'False' or args.v != 'False':
        plt.axhline(1.01)
        plt.savefig(dir_output + 'GRtrace_pam_' + repr(nd) + '.png',
                    bbox_inches='tight')
        plt.close()

    print
    print '*************************************************************'
    print

if args.cc != 'False':
    cc = ChainConsumer()
    for nd in xrange(0, mc.ndim):  # (0,ndim):
        cc.add_chain(chain[:, :, nd].flatten(), walkers=mc.nwalkers)

    #print(cc.get_latex_table())
    print cc.get_summary()

    print cc.diagnostic_gelman_rubin(threshold=0.05)
    print cc.diagnostic_geweke()
    print
    print '*************************************************************'
    print

x0 = 1. / 150

M_star1_rand = np.random.normal(M_star1, M_star1_err, n_kept)

if 'kepler' in mc.model_list:

    if args.p != 'False' or args.v != 'False':
Beispiel #11
0
def load_stan_from_folder(folder, replace=True, merge=True, cut=False, num=None):
    vals = get_truths_labels_significance()
    full_params = [[k[2]] if not isinstance(k[2], list) else k[2] for k in vals if k[2] is not None]
    params = [[k[2]] if not isinstance(k[2], list) else k[2] for k in vals if
              k[3] and k[2] is not None]
    full_params = list(itertools.chain.from_iterable(full_params))
    full_params.remove("$\\rho$")
    params = list(itertools.chain.from_iterable(params))
    name_map = {k[0]: k[2] for k in vals}
    truths = {k[2]: k[1] for k in vals if not isinstance(k[2], list)}
    is_array = [k[0] for k in vals if not isinstance(k[1], float) and not isinstance(k[1], int)]
    cs = {}
    fs = sorted([f for f in os.listdir(folder) if f.startswith("stan") and f.endswith(".pkl")])
    if num is not None:
        filter = "_%d_" % num
        fs = [f for f in fs if filter in f]

    for f in fs:
        splits = f.split("_")
        c = splits[1]
        t = os.path.abspath(folder + os.sep + f)
        if cs.get(c) is None:
            cs[c] = []
        cs[c].append(get_chain(t, name_map, replace=replace))
    assert len(cs.keys()) > 0, "No results found"
    result = []
    good_ks = []
    for k in sorted(list(cs.keys())):
        chains = cs[k]
        chain = chains[0]
        for c in chains[1:]:
            for key in chain.keys():
                chain[key] = np.concatenate((chain[key], c[key]))
        posterior = chain["Posterior"]
        del chain["Posterior"]
        if "weight" in chain.keys():
            weights = chain["weight"]
            del chain["weight"]
        else:
            weights = np.ones(posterior.shape)
        if "old\\_weight" in chain.keys():
            ow = chain["old\\_weight"]
            # ow -= ow.min()
            # ow = np.exp(ow)
            del chain["old\\_weight"]
        elif "old_weight" in chain.keys():
            ow = chain["old_weight"]
            del chain["old_weight"]
        else:
            ow = np.ones(posterior.shape)
        print(chain.keys())
        for param in is_array:
            latex = name_map[param]
            truth_val = truths[latex]
            shape = truth_val.shape
            if not replace:
                del chain[param]
            if len(shape) > 1 or latex not in chain: continue  # Dont do 2D parameters
            for i in range(shape[0]):
                column = chain[latex][:, i]
                chain[latex % i] = column
                truths[latex % i] = truth_val[i]
            del chain[latex]

        c = ChainConsumer()
        c.add_chain(chain, weights=weights)
        summary = c.get_summary()
        num_failed = sum([1 if summary[k][0] is None else 0 for k in summary.keys()])
        num_param = len(list(summary.keys()))
        if not cut or num_failed < 4:
            print("Chain %s good" % k)
            good_ks.append(k)
            result.append((chain, posterior, truths, params, full_params, len(chains), weights, ow))
        else:
            print("Chain %s is bad" % k)
    if merge:
        rr = list(result[0])
        for r in result[1:]:
            for key in rr[0].keys():
                rr[0][key] = np.concatenate((rr[0][key], r[0][key]))
            rr[1] = np.concatenate((rr[1], r[1]))
            rr[6] = np.concatenate((rr[6], r[6]))
            rr[7] = np.concatenate((rr[7], r[7]))
            rr[5] += r[5]
        return tuple(rr)
    else:
        return result
Beispiel #12
0
 def test_output_text(self):
     consumer = ChainConsumer()
     consumer.add_chain(self.data, parameters=["a"])
     vals = consumer.get_summary()["a"]
     text = consumer.get_parameter_text(*vals)
     assert text == r"5.0\pm 1.5"
Beispiel #13
0
 def test_squeeze_doesnt_squeeze_multi(self):
     c = ChainConsumer()
     c.add_chain(self.data).add_chain(self.data)
     sum = c.get_summary()
     assert isinstance(sum, list)
     assert len(sum) == 2
Beispiel #14
0
 def test_summary_when_no_parameter_names(self):
     c = ChainConsumer()
     c.add_chain(self.data)
     summary = c.get_summary()
     assert list(summary.keys()) == [0]