Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setUp(self):
self.d = Distribution(
**{"a": RV("randint", low=0, high=3+1),
"b": Distribution(**{"b1": RV("randint", low=0, high=3+1),
"b2": RV("randint", low=0, high=3+1)})})
self.d_plus_one = Distribution(
**{"a": RV("randint", low=1, high=1+1),
"b": Distribution(**{"b1": RV("randint", low=1, high=1+1),
"b2": RV("randint", low=1, high=1+1)})})
self.x_one = Parameter({"a": 1,
"b": Parameter({"b1": 1, "b2": 1})})
self.x_zero = Parameter({"a": 0,
"b": Parameter({"b1": 0, "b2": 0})})
self.x_two = Parameter({"a": 2,
"b": Parameter({"b1": 2, "b2": 2})})
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
def setUp(self):
self.d = Distribution(
**{"a": RV("randint", low=0, high=3+1),
"b": Distribution(**{"b1": RV("randint", low=0, high=3+1),
"b2": RV("randint", low=0, high=3+1)})})
self.d_plus_one = Distribution(
**{"a": RV("randint", low=1, high=1+1),
"b": Distribution(**{"b1": RV("randint", low=1, high=1+1),
"b2": RV("randint", low=1, high=1+1)})})
self.x_one = Parameter({"a": 1,
"b": Parameter({"b1": 1, "b2": 1})})
self.x_zero = Parameter({"a": 0,
"b": Parameter({"b1": 0, "b2": 0})})
self.x_two = Parameter({"a": 2,
"b": Parameter({"b1": 2, "b2": 2})})
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
history.append_population(t=0, current_epsilon=42.97,
population=population,
def setUp(self):
self.d = Distribution(
**{"a": RV("randint", low=0, high=3+1),
"b": Distribution(**{"b1": RV("randint", low=0, high=3+1),
"b2": RV("randint", low=0, high=3+1)})})
self.d_plus_one = Distribution(
**{"a": RV("randint", low=1, high=1+1),
"b": Distribution(**{"b1": RV("randint", low=1, high=1+1),
"b2": RV("randint", low=1, high=1+1)})})
self.x_one = Parameter({"a": 1,
"b": Parameter({"b1": 1, "b2": 1})})
self.x_zero = Parameter({"a": 0,
"b": Parameter({"b1": 0, "b2": 0})})
self.x_two = Parameter({"a": 2,
"b": Parameter({"b1": 2, "b2": 2})})
"""
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
return m_ss, theta_ss
# later generation
while True: # find m_s and theta_ss, valid according to prior
if len(m) > 1:
index = fast_random_choice(p)
m_s = m[index]
m_ss = model_perturbation_kernel.rvs(m_s)
# theta_s is None if the population m_ss has died out.
# This can happen since the model_perturbation_kernel
# can return a model nr which has died out.
if m_ss not in m:
continue
else:
m_ss = m[0]
theta_ss = Parameter(**transitions[m_ss].rvs().to_dict())
if (model_prior.pmf(m_ss)
* parameter_priors[m_ss].pdf(theta_ss) > 0):
return m_ss, theta_ss
def copy(self) -> "Parameter":
"""
Copy the parameter.
"""
return Parameter(**self)
def rvs(self) -> Parameter:
"""
Sample from joint distribution
Returns
-------
parameter: Parameter
A parameter which was sampled.
"""
return Parameter(**{key: val.rvs() for key, val in self.items()})