Beispiel #1
0
def test_population_retrieval(history: History):
    history.append_population(
        1, .23, Population(rand_pop_list(0)), 234, ["m1"])
    history.append_population(
        2, .123, Population(rand_pop_list(0)), 345, ["m1"])
    history.append_population(
        2, .1235, Population(rand_pop_list(5)), 20345, ["m1"] * 6)
    history.append_population(
        3, .12330, Population(rand_pop_list(30)), 30345, ["m1"] * 31)
    df = history.get_all_populations()

    assert df[df.t == 1].epsilon.iloc[0] == .23
    assert df[df.t == 2].epsilon.iloc[0] == .123
    assert df[df.t == 2].epsilon.iloc[1] == .1235
    assert df[df.t == 3].epsilon.iloc[0] == .12330

    assert df[df.t == 1].samples.iloc[0] == 234
    assert df[df.t == 2].samples.iloc[0] == 345
    assert df[df.t == 2].samples.iloc[1] == 20345
    assert df[df.t == 3].samples.iloc[0] == 30345

    assert history.alive_models(1) == [0]
    assert history.alive_models(2) == [0, 5]
    assert history.alive_models(3) == [30]
    print("ID", history.id)
Beispiel #2
0
def test_save_no_sum_stats(history: History):
    """
    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=1.0 / 6,
            sum_stat={"ss0": np.random.random(), "ss1": np.random.random()},
            distance=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
    weighted_distances_h = history.get_weighted_distances()
    weighted_distances = population.get_weighted_distances()

    assert np.allclose(
        weighted_distances_h[['distance', 'w']],
        weighted_distances[['distance', 'w']],
    )

    weights, sum_stats = history.get_weighted_sum_stats(t=0)
    # all particles should be contained nonetheless
    assert len(weights) == len(particle_list)
    for sum_stat in sum_stats:
        # should be empty
        assert not sum_stat

    history.get_population_extended()
Beispiel #3
0
def test_model_probabilities(history):
    history.append_population(
        1, 0.23, Population(rand_pop_list(3)), 234, ["m0", "m1", "m2", "m3"]
    )
    probs = history.get_model_probabilities(1)
    assert probs.p[3] == 1
    assert probs.index.tolist() == [3]
Beispiel #4
0
def test_sum_stats_save_load(history: History):
    arr = sp.random.rand(10)
    arr2 = sp.random.rand(10, 2)
    particle_list = [
        Particle(0, Parameter({
            "a": 23,
            "b": 12
        }), .2, [.1], [{
            "ss1": .1,
            "ss2": arr2,
            "ss3": example_df(),
            "rdf0": r["iris"]
        }], [], True),
        Particle(0, Parameter({
            "a": 23,
            "b": 12
        }), .2, [.1], [{
            "ss12": .11,
            "ss22": arr,
            "ss33": example_df(),
            "rdf": r["mtcars"]
        }], [], True)
    ]
    history.append_population(0, 42, Population(particle_list), 2,
                              ["m1", "m2"])
    weights, sum_stats = history.get_sum_stats(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()
    assert (sum_stats[0]["rdf0"] == pandas2ri.ri2py(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()
    assert (sum_stats[1]["rdf"] == pandas2ri.ri2py(r["mtcars"])).all().all()
Beispiel #5
0
def test_get_population(history: History):
    population = Population(rand_pop_list(0))
    history.append_population(
        t=0,
        current_epsilon=7.0,
        population=population,
        nr_simulations=200,
        model_names=["m0"],
    )
    population_h = history.get_population(t=0)

    # length
    assert len(population) == len(population_h)

    # distances
    distances = [p.distance for p in population.particles]
    distances_h = [p.distance for p in population_h.particles]
    for d0, d1 in zip(distances, distances_h):
        assert np.isclose(d0, d1)

    # weights
    weights = [p.weight for p in population.particles]
    weights_h = [p.weight for p in population_h.particles]
    for w0, w1 in zip(weights, weights_h):
        assert np.isclose(w0, w1)
Beispiel #6
0
def test_sum_stats_save_load(history: History):
    arr = sp.random.rand(10)
    arr2 = sp.random.rand(10, 2)
    particle_list = [
        Particle(m=0,
                 parameter=Parameter({"a": 23, "b": 12}),
                 weight=.2,
                 accepted_sum_stats=[{"ss1": .1, "ss2": arr2,
                                      "ss3": example_df(),
                                      "rdf0": r["faithful"]}],
                 # TODO: check why iris fails
                 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()
    assert (sum_stats[0]["rdf0"] == pandas2ri.ri2py(r["faithful"])).all().all()
    assert sum_stats[1]["ss12"] == .11
    assert (sum_stats[1]["ss22"] == arr).all()
    assert (sum_stats[1]["ss33"] == example_df()).all().all()
    assert (sum_stats[1]["rdf"] == pandas2ri.ri2py(r["mtcars"])).all().all()
Beispiel #7
0
def test_single_particle_save_load_np_int64(history: History):
    # Test if np.int64 can also be used for indexing
    # This is an important test!!!
    m_list = [0, np.int64(0)]
    t_list = [0, np.int64(0)]
    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, [""])

    for m in m_list:
        for t in t_list:
            df, w = history.get_distribution(m, t)
            assert w[0] == 1
            assert df.a.iloc[0] == 23
            assert df.b.iloc[0] == 12
Beispiel #8
0
    def get_accepted_population(self) -> Population:
        """
        Returns
        -------

        population: Population
            A population of only the accepted particles.
        """
        return Population(self._accepted_particles)
Beispiel #9
0
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(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)
Beispiel #10
0
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
Beispiel #11
0
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
Beispiel #12
0
def test_t_count(history: History):
    particle_list = [
        Particle(0, Parameter({
            "a": 23,
            "b": 12
        }), .2, [.1], [{
            "ss": .1
        }], [], True)
    ]
    for t in range(1, 10):
        history.append_population(t, 42, Population(particle_list), 2, ["m1"])
        assert t == history.max_t
Beispiel #13
0
def test_t_count(history: History):
    particle_list = [
        Particle(
            m=0,
            parameter=Parameter({"a": 23, "b": 12}),
            weight=1.0,
            sum_stat={"ss": 0.1},
            distance=0.1,
        )
    ]
    for t in range(1, 10):
        history.append_population(t, 42, Population(particle_list), 2, ["m1"])
        assert t == history.max_t
Beispiel #14
0
def test_total_nr_samples(history: History):
    particle_list = [
        Particle(0, Parameter({
            "a": 23,
            "b": 12
        }), .2, [.1], [{
            "ss": .1
        }], [], True)
    ]
    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
Beispiel #15
0
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
Beispiel #16
0
def test_single_particle_save_load(history: History):
    particle_list = [
        Particle(0, Parameter({
            "a": 23,
            "b": 12
        }), .2, [.1], [{
            "ss": .1
        }], [], True)
    ]
    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
Beispiel #17
0
def test_total_nr_samples(history: History):
    particle_list = [
        Particle(
            m=0,
            parameter=Parameter({"a": 23, "b": 12}),
            weight=1.0,
            sum_stat={"ss": 0.1},
            distance=0.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
Beispiel #18
0
def test_model_name_load_single_with_pop(history_uninitialized: History):
    h = history_uninitialized
    model_names = ["m1"]
    h.store_initial_data(0, {}, {}, {}, model_names, "", "", "")
    particle_list = [
        Particle(m=0,
                 parameter=Parameter({"a": 23, "b": 12}),
                 weight=.2,
                 accepted_sum_stats=[{"ss": .1}],
                 accepted_distances=[.1])]
    h.append_population(0, 42, Population(particle_list), 2, model_names)

    h2 = History(h.db_identifier)
    model_names_loaded = h2.model_names()
    assert model_names == model_names_loaded
Beispiel #19
0
def test_get_population(history: History):
    population = Population(rand_pop(0))
    history.append_population(t=0, current_epsilon=7.0,
                              population=population,
                              nr_simulations=200,
                              model_names=["m0"])
    population_h = history.get_population(t=0)

    # length
    assert len(population) == len(population_h)

    # distances
    distances = sum((p.accepted_distances
                     for p in population.get_list()), [])
    distances_h = sum((p.accepted_distances
                       for p in population_h.get_list()), [])
    for d0, d1 in zip(distances, distances_h):
        assert np.isclose(d0, d1)

    # weights
    weights = [p.weight for p in population.get_list()]
    weights_h = [p.weight for p in population_h.get_list()]
    for w0, w1 in zip(weights, weights_h):
        assert np.isclose(w0, w1)
Beispiel #20
0
def test_single_particle_save_load(history: History):
    particle_list = [
        Particle(
            m=0,
            parameter=Parameter({"a": 23, "b": 12}),
            weight=1.0,
            sum_stat={"ss": 0.1},
            distance=0.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
Beispiel #21
0
def test_model_name_load_single_with_pop(history_uninitialized: History):
    h = history_uninitialized
    model_names = ["m1"]
    h.store_initial_data(0, {}, {}, {}, model_names, "", "", "")
    particle_list = [
        Particle(0, Parameter({
            "a": 23,
            "b": 12
        }), .2, [.1], [{
            "ss": .1
        }], [], True)
    ]
    h.append_population(0, 42, Population(particle_list), 2, model_names)

    h2 = History(h.db_identifier)
    model_names_loaded = h2.model_names()
    assert model_names == model_names_loaded
Beispiel #22
0
def test_sum_stats_save_load(history: History):
    arr = np.random.rand(10)
    arr2 = np.random.rand(10, 2)
    particle_list = [
        Particle(m=0,
                 parameter=Parameter({
                     "a": 23,
                     "b": 12
                 }),
                 weight=.2,
                 sum_stat={
                     "ss1": .1,
                     "ss2": arr2,
                     "ss3": example_df(),
                     "rdf0": r["iris"]
                 },
                 distance=.1),
        Particle(m=0,
                 parameter=Parameter({
                     "a": 23,
                     "b": 12
                 }),
                 weight=.2,
                 sum_stat={
                     "ss12": .11,
                     "ss22": arr,
                     "ss33": example_df(),
                     "rdf": r["mtcars"]
                 },
                 distance=.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()
Beispiel #23
0
def test_model_name_load_single_with_pop(history_uninitialized: History):
    h = history_uninitialized
    model_names = ["m1"]
    h.store_initial_data(0, {}, {}, {}, model_names, "", "", "")
    particle_list = [
        Particle(
            m=0,
            parameter=Parameter({"a": 23, "b": 12}),
            weight=1.0,
            sum_stat={"ss": 0.1},
            distance=0.1,
        )
    ]
    h.append_population(0, 42, Population(particle_list), 2, model_names)

    h2 = History(h.db)
    model_names_loaded = h2.model_names()
    assert model_names == model_names_loaded
Beispiel #24
0
def test_dataframe_storage_readout():
    path = os.path.join(tempfile.gettempdir(), "history_test.db")
    model_names = ["fake_name"] * 5

    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, normalized=False)
                for particle in pops[(h, m, t)]:
                    particle_list.append(particle)
            total_weight = sum(p.weight for p in particle_list)
            for p in particle_list:
                p.weight /= total_weight
            h.append_population(
                t, 0.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

    try:
        os.remove(path)
    except FileNotFoundError:
        pass
Beispiel #25
0
def test_model_probabilities_all(history):
    history.append_population(1, .23, Population(rand_pop(3)), 234,
                              ["m0", "m1", "m2", "m3"])
    probs = history.get_model_probabilities()
    assert (probs[3].as_matrix() == np.array([1])).all()