Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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=.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
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
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
def simulate_one():
accepted = np.random.randint(2)
return Particle(0, {}, 0.1, [], [], accepted)
def rand_pop_list(m: int):
"""
Create a population for model m, of random size >= 3.
Parameters
----------
m: int
the model number
Returns
-------
"""
pop = [
Particle(m=m,
parameter=Parameter({"a": np.random.randint(10),
"b": np.random.randn()}),
weight=np.random.rand() * 42,
accepted_sum_stats=[{"ss_float": 0.1,
"ss_int": 42,
"ss_str": "foo bar string",
"ss_np": np.random.rand(13, 42),
"ss_df": example_df()}],
accepted_distances=[np.random.rand()])
for _ in range(np.random.randint(10) + 3)]
return pop
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)
model_names_loaded = h2.model_names()
assert model_names == model_names_loaded
def simulate_one():
accepted = np.random.randint(2)
return Particle(0, {}, 0.1, [], [], accepted)
def simulate_one():
return Particle(m=0, parameter={}, weight=0,
accepted_sum_stats=[], accepted_distances=[],
accepted=True)
with pytest.raises(AssertionError):
accepted_sum_stats.append(model_result.sum_stats)
accepted_distances.append(model_result.distance)
accepted_weights.append(model_result.weight)
else:
rejected_sum_stats.append(model_result.sum_stats)
rejected_distances.append(model_result.distance)
accepted = len(accepted_sum_stats) > 0
if accepted:
weight = weight_function(
accepted_distances, m_ss, theta_ss, accepted_weights)
else:
weight = 0
return Particle(
m=m_ss,
parameter=theta_ss,
weight=weight,
accepted_sum_stats=accepted_sum_stats,
accepted_distances=accepted_distances,
rejected_sum_stats=rejected_sum_stats,
rejected_distances=rejected_distances,
accepted=accepted)