Exemplo n.º 1
0
def _to_zip_product(sweep: Sweep) -> Product:
    """Converts sweep to a product of zips of single sweeps, if possible."""
    if not isinstance(sweep, Product):
        sweep = Product(sweep)
    if not all(isinstance(f, Zip) for f in sweep.factors):
        factors = [f if isinstance(f, Zip) else Zip(f) for f in sweep.factors]
        sweep = Product(*factors)
    for factor in sweep.factors:
        for term in cast(Zip, factor).sweeps:
            if not isinstance(term, SingleSweep):
                raise ValueError(
                    'cannot convert to zip-product form: {}'.format(sweep))
    return sweep
Exemplo n.º 2
0
def sweep_from_proto_dict(param_sweep: Dict) -> Sweep:
    if 'sweep' in param_sweep and 'factors' in param_sweep['sweep']:
        return Product(*[
            _sweep_from_param_sweep_zip_proto_dict(f)
            for f in param_sweep['sweep']['factors']
        ])
    return UnitSweep
Exemplo n.º 3
0
def test_gen_param_sweep():
    s1 = {
        'parameter_key': 'foo',
        'points': {
            'points': [1, 2, 3]
        }
    }
    s2 = {
        'parameter_key': 'bar',
        'points': {
            'points': [4, 5]
        }
    }
    ps = {
        'sweep': {
            'factors': [
                {
                    'sweeps': [s1]
                },
                {
                    'sweeps': [s2]
                }

            ]
        }
    }
    out = params.sweep_from_proto_dict(ps)
    assert out == Product(Zip(Points('foo', [1, 2, 3])),
                          Zip(Points('bar', [4, 5])))
Exemplo n.º 4
0
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])))
Exemplo n.º 5
0
@pytest.mark.parametrize('param_sweep', example_sweeps())
def test_param_sweep_size_versus_gen(param_sweep):
    sweep = params.sweep_from_proto(param_sweep)
    predicted_size = len(sweep)
    out = list(sweep)
    assert len(out) == predicted_size


@pytest.mark.parametrize('sweep,expected', [
    (
        Unit,
        Unit
    ),
    (
        Linspace('a', 0, 10, 25),
        Product(Zip(Linspace('a', 0, 10, 25)))
    ),
    (
        Points('a', [1, 2, 3]),
        Product(Zip(Points('a', [1, 2, 3])))
    ),
    (
        Zip(Linspace('a', 0, 1, 5), Points('b', [1, 2, 3])),
        Product(Zip(Linspace('a', 0, 1, 5), Points('b', [1, 2, 3]))),
    ),
    (
        Product(Linspace('a', 0, 1, 5), Points('b', [1, 2, 3])),
        Product(Zip(Linspace('a', 0, 1, 5)), Zip(Points('b', [1, 2, 3]))),
    ),
    (
        Product(
Exemplo n.º 6
0
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])