本文整理汇总了Python中types.SimpleNamespace.lava方法的典型用法代码示例。如果您正苦于以下问题:Python SimpleNamespace.lava方法的具体用法?Python SimpleNamespace.lava怎么用?Python SimpleNamespace.lava使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类types.SimpleNamespace
的用法示例。
在下文中一共展示了SimpleNamespace.lava方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_mcmcmc_step_parse
# 需要导入模块: from types import SimpleNamespace [as 别名]
# 或者: from types.SimpleNamespace import lava [as 别名]
def test_mcmcmc_step_parse(capsys):
rand_gen = random.Random(4)
tmp_file = br.TempFile()
walker = SimpleNamespace(name="qwerty", proposed_score=None, score_history=[1.12, 3.42], current_score=3.42,
accept=lambda *_: print("Calling accept() method"), rand_gen=rand_gen, heat=0.25,
ice=False, lava=False, proposed_score_file=tmp_file)
# Accept higher score
tmp_file.write("7.90", mode="w")
mcmcmc.MCMCMC.step_parse(walker=walker, std=1.5)
assert walker.score_history == [1.12, 3.42, 7.9]
assert walker.proposed_score == 7.9
out, err = capsys.readouterr()
assert out == "Calling accept() method\n"
# Reject lower score
tmp_file.write("0.91", mode="w")
mcmcmc.MCMCMC.step_parse(walker=walker, std=3.1)
assert walker.score_history == [1.12, 3.42, 7.9, 0.91]
assert walker.proposed_score == 0.91
out, err = capsys.readouterr()
assert out == ""
# Accept lower score
tmp_file.write("3.3", mode="w")
mcmcmc.MCMCMC.step_parse(walker=walker, std=3.1)
assert walker.score_history == [1.12, 3.42, 7.9, 0.91, 3.3]
assert walker.proposed_score == 3.3
out, err = capsys.readouterr()
assert out == "Calling accept() method\n", print(out)
# Lava walker accepts any score
tmp_file.write("0.1", mode="w")
walker.lava = True
mcmcmc.MCMCMC.step_parse(walker=walker, std=3.1)
assert walker.score_history == [1.12, 3.42, 7.9, 0.91, 3.3, 0.1]
out, err = capsys.readouterr()
assert out == "Calling accept() method\n"
# Ice walker rejects any lower scores
tmp_file.write("3.4", mode="w")
walker.lava = False
walker.ice = True
mcmcmc.MCMCMC.step_parse(walker=walker, std=3.1)
assert walker.score_history == [1.12, 3.42, 7.9, 0.91, 3.3, 0.1, 3.4]
out, err = capsys.readouterr()
assert out == ""
# Do not allow history to grow over 1000 items long
walker.score_history = [1 for _ in range(1000)]
assert len(walker.score_history) == 1000
mcmcmc.MCMCMC.step_parse(walker, 3.1)
assert len(walker.score_history) == 1000
assert walker.score_history[-1] == 3.4
示例2: test_mcmcmc_run
# 需要导入模块: from types import SimpleNamespace [as 别名]
# 或者: from types.SimpleNamespace import lava [as 别名]
def test_mcmcmc_run(capsys):
rand_gen = random.Random(1)
foo_var = SimpleNamespace(name="foo", draw_random=lambda: print("foo_var draw_raindom()"), current_value=0.98,
draw_new_value=lambda heat: print("foo_var draw_new_value()"), draw_value=0.12)
bar_var = SimpleNamespace(name="bar", draw_random=lambda: print("bar_var draw_raindom()"), current_value=0.87,
draw_new_value=lambda heat: print("bar_var draw_new_value()"), draw_value=0.23)
walker1_1 = SimpleNamespace(name="1_1", variables=[foo_var, bar_var], lava=False, current_score=3, heat=0.25,
score_history=[1.12, 3.42, 7.9, 0.91, 3.3, 0.1, 3.4])
walker1_2 = SimpleNamespace(name="1_2", variables=[foo_var, bar_var], lava=False, current_score=3, heat=0.25,
score_history=[1.12, 3.42, 7.9, 0.91, 3.3, 0.1, 3.4])
walker1_3 = SimpleNamespace(name="1_3", variables=[foo_var, bar_var], lava=False, current_score=3, heat=0.25,
score_history=[1.12, 3.42, 7.9, 0.91, 3.3, 0.1, 3.4])
walker2_1 = SimpleNamespace(name="2_1", variables=[foo_var, bar_var], lava=False, current_score=3, heat=0.25,
score_history=[1.12, 3.42, 7.9, 0.91, 3.3, 0.1, 3.4])
walker2_2 = SimpleNamespace(name="2_2", variables=[foo_var, bar_var], lava=False, current_score=3, heat=0.25,
score_history=[1.12, 3.42, 7.9, 0.91, 3.3, 0.1, 3.4])
walker2_3 = SimpleNamespace(name="2_3", variables=[foo_var, bar_var], lava=False, current_score=3, heat=0.25,
score_history=[1.12, 3.42, 7.9, 0.91, 3.3, 0.1, 3.4])
walker3_1 = SimpleNamespace(name="3_1", variables=[foo_var, bar_var], lava=False, current_score=3, heat=0.25,
score_history=[1.12, 3.42, 7.9, 0.91, 3.3, 0.1, 3.4])
walker3_2 = SimpleNamespace(name="3_2", variables=[foo_var, bar_var], lava=False, current_score=3, heat=0.25,
score_history=[1.12, 3.42, 7.9, 0.91, 3.3, 0.1, 3.4])
walker3_3 = SimpleNamespace(name="3_3", variables=[foo_var, bar_var], lava=False, current_score=3, heat=0.25,
score_history=[1.12, 3.42, 7.9, 0.91, 3.3, 0.1, 3.4])
chain1 = SimpleNamespace(walkers=[walker1_1, walker1_2, walker1_3], step_counter=99,
_dump_obj=lambda: b"chain1_obj\n", swap_hot_cold=lambda: print("Chain1 swap_hot_cold()"),
write_sample=lambda: print("Chain1 write_sample()"))
chain2 = SimpleNamespace(walkers=[walker2_1, walker2_2, walker2_3], step_counter=99,
_dump_obj=lambda: b"chain2_obj\n", swap_hot_cold=lambda: print("Chain2 swap_hot_cold()"),
write_sample=lambda: print("Chain2 write_sample()"))
chain3 = SimpleNamespace(walkers=[walker3_1, walker3_2, walker3_3], step_counter=99,
_dump_obj=lambda: b"chain3_obj\n", swap_hot_cold=lambda: print("Chain3 swap_hot_cold()"),
write_sample=lambda: print("Chain3 write_sample()"))
global convergence_counter
convergence_counter = 0
def mock_check_convergence():
global convergence_counter
convergence_counter += 1
return convergence_counter > 5
tmp_file = br.TempFile()
mc_obj = SimpleNamespace(run=mcmcmc.MCMCMC.run, _check_convergence=mock_check_convergence, steps=1,
dumpfile=tmp_file.path, chains=[chain1, chain2, chain3], rand_gen=rand_gen,
mc_step_run=lambda *args: print("mc_step_run", args),
step_parse=lambda *args: print("step_parse:", args), best={"score": None, "variables": {}},
sample_rate=1)
# Break out when counter > steps
mc_obj.run(mc_obj)
out, err = capsys.readouterr()
out = out.split("\n")
assert out.count("foo_var draw_raindom()") == 0
assert out.count("foo_var draw_new_value()") == 18
assert out.count("bar_var draw_raindom()") == 0
assert out.count("bar_var draw_new_value()") == 18
assert out.count("Chain1 swap_hot_cold()") == 2
assert out.count("Chain2 swap_hot_cold()") == 2
assert out.count("Chain3 swap_hot_cold()") == 2
assert out.count("Chain1 write_sample()") == 2
assert out.count("Chain2 write_sample()") == 2
assert out.count("Chain3 write_sample()") == 2
# assert len([None for x in out if "mc_step_run:" in x]) == 18, print(out)
assert len([None for x in out if "step_parse:" in x]) == 18
with open(tmp_file.path, "br") as ifile:
dump_file = dill.load(ifile)
assert dump_file == [b'chain1_obj\n', b'chain2_obj\n', b'chain3_obj\n']
# Break when _check_convergence() pops, and include a lava walker
mc_obj.steps = 0
walker1_1.lava = True
mc_obj.run(mc_obj)
out, err = capsys.readouterr()
out = out.split("\n")
assert out.count("foo_var draw_raindom()") == 2
assert out.count("foo_var draw_new_value()") == 16
assert out.count("bar_var draw_raindom()") == 2
assert out.count("bar_var draw_new_value()") == 16