本文整理汇总了Python中types.SimpleNamespace.run方法的典型用法代码示例。如果您正苦于以下问题:Python SimpleNamespace.run方法的具体用法?Python SimpleNamespace.run怎么用?Python SimpleNamespace.run使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类types.SimpleNamespace
的用法示例。
在下文中一共展示了SimpleNamespace.run方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_mcmcmc_resume
# 需要导入模块: from types import SimpleNamespace [as 别名]
# 或者: from types.SimpleNamespace import run [as 别名]
def test_mcmcmc_resume(capsys):
mc_obj = SimpleNamespace(dumpfile="does_not_exist", resume=mcmcmc.MCMCMC.resume)
assert mc_obj.resume(mc_obj) is False
tmp_file = br.TempFile(byte_mode=True)
dill.dump(["a", "b", "c"], tmp_file)
mc_obj.dumpfile = tmp_file.path
chain1 = SimpleNamespace(_apply_dump=lambda *_: print("applying chain1"))
chain2 = SimpleNamespace(_apply_dump=lambda *_: print("applying chain2"))
chain3 = SimpleNamespace(_apply_dump=lambda *_: print("applying chain3"))
mc_obj.chains = [chain1, chain2, chain3]
mc_obj.run = lambda *_: print("Running")
assert mc_obj.resume(mc_obj) is True
out, err = capsys.readouterr()
assert out == "applying chain1\napplying chain2\napplying chain3\nRunning\n", print(out)
示例2: test_mcmcmc_run
# 需要导入模块: from types import SimpleNamespace [as 别名]
# 或者: from types.SimpleNamespace import run [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