def example_sweeps(): empty_sweep = ParameterSweep() empty_product = ParameterSweep() empty_zip = ParameterSweep() empty_zip.sweep.factors.add() empty_zip.sweep.factors.add() full_sweep = ParameterSweep() f1 = full_sweep.sweep.factors.add() s11 = f1.sweeps.add() s11.parameter_key = '11' s11.linspace.first_point = 0 s11.linspace.last_point = 10 s11.linspace.num_points = 5 s12 = f1.sweeps.add() s12.parameter_key = '12' s12.points.points.extend(range(7)) f2 = full_sweep.sweep.factors.add() s21 = f2.sweeps.add() s21.parameter_key = '21' s21.linspace.first_point = 0 s21.linspace.last_point = 10 s21.linspace.num_points = 11 s22 = f2.sweeps.add() s22.parameter_key = '22' s22.points.points.extend(range(13)) return [empty_sweep, empty_product, empty_zip, full_sweep]
def test_gen_param_sweep(): ps = ParameterSweep() f1 = ps.sweep.factors.add() s1 = f1.sweeps.add() s1.parameter_key = 'foo' s1.points.points.extend([1, 2, 3]) f2 = ps.sweep.factors.add() s2 = f2.sweeps.add() s2.parameter_key = 'bar' s2.points.points.extend([4, 5]) out = params.sweep_from_proto(ps) assert out == Product(Zip(Points('foo', [1, 2, 3])), Zip(Points('bar', [4, 5])))
def test_param_sweep_size(): ps = ParameterSweep() f1 = ps.sweep.factors.add() s11 = f1.sweeps.add() s11.parameter_key = '11' s11.linspace.num_points = 5 s12 = f1.sweeps.add() s12.parameter_key = '12' s12.points.points.extend(range(7)) f2 = ps.sweep.factors.add() s21 = f2.sweeps.add() s21.parameter_key = '21' s21.linspace.num_points = 11 s22 = f2.sweeps.add() s22.parameter_key = '22' s22.points.points.extend(range(13)) assert len(params.sweep_from_proto(ps)) == 5 * 11
def test_param_sweep_keys(): ps = ParameterSweep() f1 = ps.sweep.factors.add() s11 = f1.sweeps.add() s11.parameter_key = 'foo' s11.points.points.extend(range(5)) s12 = f1.sweeps.add() s12.parameter_key = 'bar' s12.points.points.extend(range(7)) f2 = ps.sweep.factors.add() s21 = f2.sweeps.add() s21.parameter_key = 'baz' s21.points.points.extend(range(11)) s22 = f2.sweeps.add() s22.parameter_key = 'qux' s22.points.points.extend(range(13)) out = params.sweep_from_proto(ps) assert out.keys == ['foo', 'bar', 'baz', 'qux']
def test_empty_param_sweep_keys(): ps = ParameterSweep() assert params.sweep_from_proto(ps).keys == []
def test_gen_empty_param_sweep(): ps = ParameterSweep() out = params.sweep_from_proto(ps) assert out == Unit
def test_param_sweep_size_no_sweeps(): ps = ParameterSweep() ps.sweep.factors.add() ps.sweep.factors.add() assert len(params.sweep_from_proto(ps)) == 0
def test_empty_param_sweep_size(): ps = ParameterSweep() assert len(params.sweep_from_proto(ps)) == 1
def sweep_from_proto(param_sweep: params_pb2.ParameterSweep) -> Sweep: if not param_sweep.HasField('sweep'): return UnitSweep return Product( *[_sweep_from_param_sweep_zip(f) for f in param_sweep.sweep.factors])