def test_paramset_separator(): ps = ParameterSet() ps.add_parameters(p1=[1, 2], p2=["a", "b"]) ps.add_separator(p3="param") ps.add_parameters(p1=3) assert_equal(len(ps), 6) for i, param_dict in ps: assert_equal(param_dict["p3"], "param") if i < 4: assert_in(param_dict["p1"], [1, 2]) else: assert_equal(param_dict["p1"], 3) ps.add_parameters(p2="c") assert_equal(len(ps), 9) count = 0 for i, param_dict in ps: assert_equal(param_dict["p3"], "param") if i < 4: assert_in(param_dict["p1"], [1, 2]) assert_in(param_dict["p2"], ["a", "b"]) if param_dict["p1"] == 3 and param_dict["p2"] == "c": count += 1 assert_equal(count, 1) assert_raises(ValueError, ps.add_parameters, p4=10)
def test_paramset_getitem(): ps = ParameterSet() ps.add_parameters(p1=[1, 2], p2=["a", "b"]) ps.add_separator(p3="param") ps.add_parameters(p1=3, p2="c") for i, param_dict in ps: assert_equal(param_dict, ps[i])
def test_paramset_get_indices_with(): ps = ParameterSet() ps.add_parameters(p1=[1, 2], p2=["a", "b"]) ps.add_separator(p3="param") ps.add_parameters(p1=3, p2="c") for index in ps.get_indices_with(p1={3}): assert_less(3, index) # 0,1,2,3 --> [1,2] x [a,b] assert_equal(ps[index]["p1"], 3) assert_equal(len(list(ps.get_indices_with(p1={4}))), 0)
def run(self, result, x, z, w, y=2, n=0, **parameters): import time from random import randint, normalvariate result["multiply"] = x * y # Simulate the effect of the new parameter: in this case we add # some gaussian noise to the computation of the sum result["sum"] = z + w + normalvariate(0, n) time.sleep(randint(1, 10)) if __name__ == "__main__": set_stdout_logging() parser = CTParser() environment, _ = parser.parse() param_set = ParameterSet() param_set.add_parameters(x=[1, 2, 3], z=4, w=[5, 6]) # Add the separator, the default value for what was computed previously and # the new parameter values # -- Separator (compare this to `004_adding_parameter_values.py`) param_set.add_separator(n=0) # Notice we pass the default value # -- new parameter values param_set.add_parameters(n=[0.01, 0.001]) experiment = Experiment("BasicUsage", param_set, MyComputation) environment.run(experiment)
""" def run(self, result, x, z, w, y=2, **parameters): import time from random import randint result["multiply"] = x * y result["sum"] = z + w time.sleep(randint(1, 10)) if __name__ == "__main__": set_stdout_logging() parser = CTParser() environment, _ = parser.parse() param_set = ParameterSet() param_set.add_parameters(x=[1, 2, 3], z=4, w=[5, 6]) # Add the separator and the new parameter values # -- Separator param_set.add_separator() # -- new parameter values param_set.add_parameters(x=4) # you could also have added several values at once with: # param_set.add_parameters(x=[4, 5]) for instance experiment = Experiment("BasicUsage", param_set, MyComputation) environment.run(experiment)