Пример #1
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()
Пример #2
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
Пример #3
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()
Пример #4
0
def test_sum_stats_save_load(history: History):
    import scipy as sp
    arr = sp.random.rand(10)
    arr2 = sp.random.rand(10, 2)
    particle_population = [
        ValidParticle(0, Parameter({
            "a": 23,
            "b": 12
        }), .2, [.1], [{
            "ss1": .1,
            "ss2": arr2
        }]),
        ValidParticle(0, Parameter({
            "a": 23,
            "b": 12
        }), .2, [.1], [{
            "ss12": .11,
            "ss22": arr
        }])
    ]
    history.append_population(0, 42, particle_population, 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[1]["ss12"] == .11
    assert (sum_stats[1]["ss22"] == arr).all()
Пример #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)
Пример #6
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)
Пример #7
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
Пример #8
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
Пример #9
0
def test_t_count(history: History):
    particle_population = [
        ValidParticle(0, Parameter({
            "a": 23,
            "b": 12
        }), .2, [.1], [{
            "ss": .1
        }])
    ]
    for t in range(1, 10):
        history.append_population(t, 42, particle_population, 2, ["m1"])
        assert t == history.max_t
Пример #10
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
Пример #11
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()
Пример #12
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
Пример #13
0
def test_total_nr_samples(history: History):
    particle_population = [
        ValidParticle(0, Parameter({
            "a": 23,
            "b": 12
        }), .2, [.1], [{
            "ss": .1
        }])
    ]
    history.append_population(0, 42, particle_population, 4234, ["m1"])
    history.append_population(0, 42, particle_population, 3, ["m1"])

    assert 4237 == history.total_nr_simulations
Пример #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
Пример #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
Пример #16
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)
Пример #17
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
Пример #18
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
Пример #19
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
Пример #20
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()
Пример #21
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_population = [
        ValidParticle(0, Parameter({
            "a": 23,
            "b": 12
        }), .2, [.1], [{
            "ss": .1
        }])
    ]
    history.append_population(0, 42, particle_population, 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
Пример #22
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)