def test_find_or_create(self): ps = ParameterSet.find_or_create(0, 1, 2, 3) self.assertEqual(ps.id, 0) self.assertEqual(ps.params, (0, 1, 2, 3)) self.assertEqual(len(ParameterSet.all()), 1) ps2 = ParameterSet.find_or_create(3, 4, 5, 6) self.assertEqual(len(ParameterSet.all()), 2)
def test_all(self): ps = ParameterSet.find_or_create((0, 1, 2, 3)) runs = ps.create_runs_upto(3) self.assertEqual(Run.all(), runs) ps2 = ParameterSet.find_or_create((0, 1, 2, 4)) runs2 = ps2.create_runs_upto(3) self.assertEqual(len(Run.all()), 6)
def test_ps(self): param = {"p1": 1, "p2": 2} ps = self.sim.find_or_create_parameter_set(param) self.assertEqual(ps.id(), 0) self.assertEqual(ps.v(), param) self.assertEqual(ps.runs(), []) self.assertEqual(ps.simulator(), self.sim) self.assertEqual(ParameterSet.all(), [ps]) self.assertEqual(ParameterSet.find(0), ps) self.assertEqual(self.sim.find_parameter_set(param), ps) self.assertEqual(ps.to_dict(), { "id": 0, "sim_id": 0, "params": param, "run_ids": [] }) # second PS param2 = {"p1": 2, "p2": 3} ps2 = self.sim.find_or_create_parameter_set(param2) self.assertEqual(ps2.id(), 1) self.assertEqual(ps2.v(), param2) self.assertEqual(ParameterSet.all(), [ps, ps2]) # duplicate PS self.assertEqual(self.sim.find_or_create_parameter_set(param), ps) self.assertEqual(len(ParameterSet.all()), 2)
def test_create_runs(self): ps = ParameterSet.find_or_create(0, 1, 2, 3) runs = ps.create_runs_upto(3) self.assertEqual([r.id for r in runs], [0, 1, 2]) self.assertEqual([r.seed for r in runs], [0, 1, 2]) ps2 = ParameterSet.find_or_create(0, 1, 3, 4) runs = ps2.create_runs_upto(3) self.assertEqual([r.id for r in runs], [3, 4, 5]) self.assertEqual([r.seed for r in runs], [0, 1, 2])
def test_find(self): sim2 = Simulator.create("echo") self.assertEqual(sim2.id(), 1) ps1 = self.sim.find_or_create_parameter_set({"foo": "bar"}) self.assertEqual(self.sim.parameter_sets(), [ps1]) ps1.create_runs_upto(2) ps2 = sim2.find_or_create_parameter_set({"foo": "bar"}) self.assertEqual(sim2.parameter_sets(), [ps2]) ps2.create_runs_upto(2) self.assertEqual([r.id() for r in ps1.runs()], [0, 1]) self.assertEqual([r.id() for r in ps2.runs()], [2, 3]) self.assertEqual([r.id() for r in ParameterSet.all()], [0, 1]) self.assertEqual(ParameterSet.find(1), ps2)
def test_average_results(self): ps = ParameterSet.find_or_create(0, 1, 2, 3) runs = ps.create_runs_upto(3) self.assertEqual(ps.average_results(), ()) for (i, r) in enumerate(runs): r.store_result([1.0 + i, 2.0 + i, 3.0 + 1], 0, 3, 111, 222) self.assertEqual(ps.average_results(), (2.0, 3.0, 4.0))
def test_is_finished(self): ps = ParameterSet.find_or_create(0, 1, 2, 3) self.assertEqual(ps.is_finished(), True) runs = ps.create_runs_upto(1) self.assertFalse(ps.is_finished()) self.assertEqual(len(ps.finished_runs()), 0) runs[0].store_result([1.0, 2.0, 3.0], 0, 3, 111, 222) self.assertTrue(ps.is_finished()) self.assertEqual(len(ps.finished_runs()), 1)
def test_dump(self): ps = ParameterSet.find_or_create((0, 1, 2, 3)) runs = ps.create_runs_upto(3) runs[0].store_result([1.0, 2.0, 3.0], 0, 3, 111, 222) ps = ParameterSet.find_or_create((4, 5, 6, 7)) self.assertEqual(len(self.t.ps_table), 2) runs = ps.create_runs_upto(3) runs[2].store_result([1.0, 2.0, 3.0], 0, 3, 111, 222) self.assertEqual(len(self.t.tasks_table), 6) path = self.dump_path Tables.dump(path) self.assertTrue(os.path.exists(path)) self.t.clear() Tables.load(path) self.t = Tables.get() self.assertEqual(len(self.t.ps_table), 2) self.assertEqual(len(self.t.tasks_table), 6) self.assertTrue(self.t.tasks_table[0].is_finished()) self.assertTrue(self.t.tasks_table[5].is_finished())
def test_dump(self): sim = Simulator.create("~/my_simulator") ps = sim.find_or_create_parameter_set({"p1": 0, "p2": 1}) runs = ps.create_runs_upto(3) runs[0]._store_result([1.0, 2.0, 3.0], 0, 3, 111, 222) ps2 = sim.find_or_create_parameter_set({"p1": 2, "p2": 3}) self.assertEqual(len(ParameterSet.all()), 2) runs = ps2.create_runs_upto(3) runs[2]._store_result([1.0, 2.0, 3.0], 0, 3, 111, 222) self.assertEqual(len(self.t.tasks_table), 6) path = self.dump_path Tables.dump(path) self.assertTrue(os.path.exists(path)) self.t.clear() Tables.load(path) self.t = Tables.get() self.assertEqual(len(ParameterSet.all()), 2) self.assertEqual(len(Task.all()), 6) self.assertTrue(Task.find(0).is_finished()) self.assertTrue(Task.find(5).is_finished())
def test_find(self): ps = ParameterSet.find_or_create((0, 1, 2, 3)) runs = ps.create_runs_upto(3) rid = runs[1].id self.assertEqual(rid, 1) self.assertEqual(Run.find(rid), runs[1])
def test_find(self): ps = ParameterSet.find_or_create(0, 1, 2, 3) ps2 = ParameterSet.find_or_create(0, 1, 2, 4) pid = ps2.id self.assertEqual(pid, 1) self.assertEqual(ParameterSet.find(1), ps2)
def test_all(self): ps = ParameterSet.find_or_create(0, 1, 2, 3) self.assertEqual(ParameterSet.all(), [ps]) ps2 = ParameterSet.find_or_create(0, 1, 2, 4) self.assertEqual(ParameterSet.all(), [ps, ps2]) self.assertEqual(len(ParameterSet.all()), 2)
def test_find_or_create(self): ps1 = ParameterSet.find_or_create(0, 1, 2, 3) ps2 = ParameterSet.find_or_create(0, 1, 2, 3) self.assertEqual(ps1, ps2) self.assertEqual(len(ParameterSet.all()), 1)
def test_ps(self): ps = ParameterSet(500, (2, 3, 4, 5)) self.assertEqual(ps.id, 500) self.assertEqual(ps.params, (2, 3, 4, 5)) self.assertEqual(ps.run_ids, [])