How to use the pyabc.population.Population function in pyabc

To help you get started, we’ve selected a few pyabc examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ICB-DCM / pyABC / test / test_storage.py View on Github external
"""
    Test that what has been stored can be retrieved correctly
    also when no sum stats are saved.
    """
    particle_list = []
    for _ in range(0, 6):
        particle = Particle(
            m=0,
            parameter=Parameter({"th0": np.random.random()}),
            weight=.2,
            accepted_sum_stats=[{"ss0": np.random.random(),
                                 "ss1": np.random.random()}],
            accepted_distances=[np.random.random()])
        particle_list.append(particle)

    population = Population(particle_list)

    # do not save sum stats
    # use the attribute first to make sure we have no typo
    print(history.stores_sum_stats)
    history.stores_sum_stats = False

    # test some basic routines
    history.append_population(t=0, current_epsilon=42.97,
                              population=population,
                              nr_simulations=10,
                              model_names=[""])

    # just call
    history.get_distribution(0, 0)

    # test whether weights and distances returned correctly
github ICB-DCM / pyABC / test / test_storage.py View on Github external
def test_model_probabilities_all(history):
    history.append_population(1, .23, Population(rand_pop_list(3)), 234,
                              ["m0", "m1", "m2", "m3"])
    probs = history.get_model_probabilities()
    assert (probs[3].values == np.array([1])).all()
github ICB-DCM / pyABC / test / test_storage.py View on Github external
def test_t_count(history: History):
    particle_list = [
        Particle(m=0,
                 parameter=Parameter({"a": 23, "b": 12}),
                 weight=.2,
                 accepted_sum_stats=[{"ss": .1}],
                 accepted_distances=[.1])]
    for t in range(1, 10):
        history.append_population(t, 42, Population(particle_list), 2, ["m1"])
        assert t == history.max_t
github ICB-DCM / pyABC / test / test_storage.py View on Github external
def test_total_nr_samples(history: History):
    particle_list = [
        Particle(m=0,
                 parameter=Parameter({"a": 23, "b": 12}),
                 weight=.2,
                 accepted_sum_stats=[{"ss": .1}],
                 accepted_distances=[.1])]
    population = Population(particle_list)
    history.append_population(0, 42, population, 4234, ["m1"])
    history.append_population(0, 42, population, 3, ["m1"])

    assert 4237 == history.total_nr_simulations
github ICB-DCM / pyABC / test / test_storage.py View on Github external
def make_hist():
        h = History("sqlite:///" + path)
        h.store_initial_data(0, {}, {}, {}, model_names, "", "", "")
        return h

    pops = {}
    histories = [make_hist() for _ in range(4)]
    for h in histories:
        for t in range(4):
            particle_list = []
            for m in range(5):
                pops[(h, m, t)] = rand_pop_list(m)
                for particle in pops[(h, m, t)]:
                    particle_list.append(particle)
            h.append_population(t, .1, Population(particle_list), 2,
                                model_names)

    for h in histories:
        for t in range(4):
            for m in range(5):
                pop = pops[(h, m, t)]
                expected_particles_list = [p.parameter for p in pop]
                pars_df, w = h.get_distribution(m, t)
                # use range(len and not zip on dataframe to not stop early
                # in case of population not completely stored
                assert np.isclose(w.sum(), 1)
                for par_ix, expected_par in \
                        enumerate(expected_particles_list):
                    actual_par = pars_df.iloc[par_ix]
                    assert expected_par.a == actual_par.a
                    assert expected_par.b == actual_par.b
github ICB-DCM / pyABC / test / test_storage.py View on Github external
def test_single_particle_save_load(history: History):
    particle_list = [
        Particle(m=0,
                 parameter=Parameter({"a": 23, "b": 12}),
                 weight=.2,
                 accepted_sum_stats=[{"ss": .1}],
                 accepted_distances=[.1]),
    ]
    history.append_population(0, 42, Population(particle_list), 2, [""])

    df, w = history.get_distribution(0, 0)
    assert w[0] == 1
    assert df.a.iloc[0] == 23
    assert df.b.iloc[0] == 12
github ICB-DCM / pyABC / test / test_storage.py View on Github external
def test_population_to_df(history: History):
    # TODO this test is not very good yet
    for t in range(3):
        for m in range(4):
            history.append_population(t, .23, Population(rand_pop_list(m)),
                                      234,
                                      ["m0", "m1", "m2", "m3"])
    df = history.get_population_extended(m=0)
    df_js = sumstat_to_json(df)
    assert len(df) == len(df_js)
github ICB-DCM / pyABC / test / test_storage.py View on Github external
parameter=Parameter({"a": 23, "b": 12}),
                 weight=.2,
                 accepted_sum_stats=[{"ss1": .1, "ss2": arr2,
                                      "ss3": example_df(),
                                      "rdf0": r["iris"]}],
                 accepted_distances=[.1]),
        Particle(m=0,
                 parameter=Parameter({"a": 23, "b": 12}),
                 weight=.2,
                 accepted_sum_stats=[{"ss12": .11, "ss22": arr,
                                      "ss33": example_df(),
                                      "rdf": r["mtcars"]}],
                 accepted_distances=[.1])]

    history.append_population(0, 42,
                              Population(particle_list), 2, ["m1", "m2"])
    weights, sum_stats = history.get_weighted_sum_stats_for_model(0, 0)
    assert (weights == 0.5).all()
    assert sum_stats[0]["ss1"] == .1
    assert (sum_stats[0]["ss2"] == arr2).all()
    assert (sum_stats[0]["ss3"] == example_df()).all().all()
    with localconverter(pandas2ri.converter):
        assert (sum_stats[0]["rdf0"] == r["iris"]).all().all()
    assert sum_stats[1]["ss12"] == .11
    assert (sum_stats[1]["ss22"] == arr).all()
    assert (sum_stats[1]["ss33"] == example_df()).all().all()
    with localconverter(pandas2ri.converter):
        assert (sum_stats[1]["rdf"] == r["mtcars"]).all().all()
github ICB-DCM / pyABC / pyabc / storage / history.py View on Github external
py_accepted_distances.append(py_distance)

                # create particle
                py_particle = PyParticle(
                    m=py_m,
                    parameter=py_parameter,
                    weight=py_weight,
                    accepted_sum_stats=py_accepted_sum_stats,
                    accepted_distances=py_accepted_distances,
                    rejected_sum_stats=[],
                    rejected_distances=[],
                    accepted=True)
                py_particles.append(py_particle)

        # create population
        py_population = PyPopulation(py_particles)

        return py_population
github ICB-DCM / pyABC / pyabc / sampler / base.py View on Github external
def get_accepted_population(self) -> Population:
        """
        Returns
        -------

        population: Population
            A population of only the accepted particles.
        """
        return Population(self._accepted_particles)