示例#1
0
文件: test_cache.py 项目: GYGit/nengo
def test_warns_out_of_context(tmpdir):
    cache_dir = str(tmpdir)
    cache = DecoderCache(cache_dir=cache_dir)

    with warns(UserWarning):
        cache.shrink()

    solver_mock = SolverMock()
    solver = cache.wrap_solver(solver_mock)
    with warns(UserWarning):
        solver(**get_solver_test_args())
    assert SolverMock.n_calls[solver_mock] == 1
    def test_invalid_interpolation_on_func(self):
        def func(t):
            return t

        with warns(UserWarning):
            process = Piecewise({0.05: 0, 0.1: func}, interpolation='linear')
        assert process.interpolation == 'zero'
    def test_fallback_to_zero(self, Simulator, monkeypatch):
        # Emulate not having scipy in case we have scipy
        monkeypatch.setitem(sys.modules, "scipy.interpolate", None)

        with warns(UserWarning):
            process = Piecewise({0.05: 1, 0.1: 0}, interpolation='linear')
        assert process.interpolation == 'zero'
示例#4
0
def test_writeablecacheindex_warning(monkeypatch, tmpdir):
    def raise_error(*args, **kwargs):
        raise CalledProcessError(-1, "move")

    monkeypatch.setattr(nengo.cache, "replace", raise_error)
    with warns(CacheIOWarning):
        with WriteableCacheIndex(cache_dir=str(tmpdir)):
            pass
示例#5
0
def test_warn_on_opensim_del(Simulator):
    with nengo.Network() as net:
        nengo.Ensemble(10, 1)

    sim = Simulator(net)
    with warns(ResourceWarning):
        sim.__del__()
    sim.close()
示例#6
0
def test_warn_on_opensim_del(Simulator):
    with nengo.Network() as net:
        nengo.Ensemble(10, 1)

    sim = Simulator(net)
    with warns(ResourceWarning):
        sim.__del__()
    sim.close()
示例#7
0
def test_set_output(Simulator):
    counter = []

    def accumulate(t):
        counter.append(t)
        return t

    def noreturn(t):
        pass

    with nengo.Network() as model:
        # if output is None, size_out == size_in
        with warns(UserWarning):
            # warns since size_in != size_out and output is None
            passthrough = nengo.Node(None, size_in=20, size_out=30)
        assert passthrough.output is None
        assert passthrough.size_out == 20

        # if output is an array-like...
        # size_in must be 0
        with pytest.raises(ValidationError):
            nengo.Node(np.ones(1), size_in=1)
        # size_out must match
        with pytest.raises(ValidationError):
            nengo.Node(np.ones(3), size_out=2)
        # must be scalar or vector, not matrix
        with pytest.raises(ValidationError):
            nengo.Node(np.ones((2, 2)))
        # scalar gets promoted to float vector
        scalar = nengo.Node(2)
        assert scalar.output.shape == (1,)
        assert str(scalar.output.dtype) == 'float64'
        # vector stays 1D
        vector = nengo.Node(np.arange(3))
        assert vector.output.shape == (3,)
        assert str(vector.output.dtype) == 'float64'

        # if output is callable...
        # if size_in is 0, should only take in t
        with pytest.raises(ValidationError):
            nengo.Node(lambda t, x: 2.0, size_in=0)
        # if size_in > 0, should take both t and x
        with pytest.raises(ValidationError):
            nengo.Node(lambda t: t ** 2, size_in=1)
        # function must return a scalar or vector, not matrix
        with pytest.raises(ValidationError):
            nengo.Node(lambda t: np.ones((2, 2)))
        # if we pass size_out, function should not be called
        assert len(counter) == 0
        accum_func = nengo.Node(accumulate, size_out=1)
        assert len(counter) == 0
        assert accum_func.size_out == 1
        # if the function returns None, size_out == 0
        noreturn_func = nengo.Node(noreturn)
        assert noreturn_func.size_out == 0

    with Simulator(model):  # Ensure it all builds
        pass
示例#8
0
def test_set_output(Simulator):
    counter = []

    def accumulate(t):
        counter.append(t)
        return t

    def noreturn(t):
        pass

    with nengo.Network() as model:
        # if output is None, size_out == size_in
        with warns(UserWarning):
            # warns since size_in != size_out and output is None
            passthrough = nengo.Node(None, size_in=20, size_out=30)
        assert passthrough.output is None
        assert passthrough.size_out == 20

        # if output is an array-like...
        # size_in must be 0
        with pytest.raises(ValidationError):
            nengo.Node(np.ones(1), size_in=1)
        # size_out must match
        with pytest.raises(ValidationError):
            nengo.Node(np.ones(3), size_out=2)
        # must be scalar or vector, not matrix
        with pytest.raises(ValidationError):
            nengo.Node(np.ones((2, 2)))
        # scalar gets promoted to float vector
        scalar = nengo.Node(2)
        assert scalar.output.shape == (1,)
        assert str(scalar.output.dtype) == 'float64'
        # vector stays 1D
        vector = nengo.Node(np.arange(3))
        assert vector.output.shape == (3,)
        assert str(vector.output.dtype) == 'float64'

        # if output is callable...
        # if size_in is 0, should only take in t
        with pytest.raises(ValidationError):
            nengo.Node(lambda t, x: 2.0, size_in=0)
        # if size_in > 0, should take both t and x
        with pytest.raises(ValidationError):
            nengo.Node(lambda t: t ** 2, size_in=1)
        # function must return a scalar or vector, not matrix
        with pytest.raises(ValidationError):
            nengo.Node(lambda t: np.ones((2, 2)))
        # if we pass size_out, function should not be called
        assert len(counter) == 0
        accum_func = nengo.Node(accumulate, size_out=1)
        assert len(counter) == 0
        assert accum_func.size_out == 1
        # if the function returns None, size_out == 0
        noreturn_func = nengo.Node(noreturn)
        assert noreturn_func.size_out == 0

    with Simulator(model):  # Ensure it all builds
        pass
示例#9
0
def test_warns_out_of_context(tmpdir):
    cache_dir = str(tmpdir)
    cache = DecoderCache(cache_dir=cache_dir)

    solver_mock = SolverMock()
    solver = cache.wrap_solver(solver_mock)
    with warns(UserWarning):
        solver(**get_solver_test_args())
    assert SolverMock.n_calls[solver_mock] == 1
示例#10
0
def test_warn_on_future_version(monkeypatch):
    with nengo.Network() as model:
        nengo.Ensemble(10, 1)

    future_version = tuple(v + 1 for v in latest_nengo_version_info)
    monkeypatch.setattr(nengo.version, 'version_info', future_version)
    with warns(UserWarning):
        with nengo_ocl.Simulator(model):
            pass
示例#11
0
def test_invalid_run_time(Simulator):
    net = nengo.Network()
    with Simulator(net) as sim:
        with pytest.raises(ValidationError):
            sim.run(-0.0001)
        with warns(UserWarning):
            sim.run(0)
        sim.run(0.0006)  # Rounds up to 0.001
        assert sim.n_steps == 1
示例#12
0
def test_sobol_invalid_dims():
    with pytest.raises(ValueError):
        Sobol().sample(1, d=0)

    with pytest.raises(ValueError):
        Sobol().sample(1, d=1.5)

    with warns(UserWarning):
        Sobol().sample(2, d=41)
示例#13
0
def test_create_pointer_warning(rng):
    v = Vocabulary(2, rng=rng)

    # five pointers shouldn't fit
    with warns(UserWarning):
        v.parse('A')
        v.parse('B')
        v.parse('C')
        v.parse('D')
        v.parse('E')
示例#14
0
def test_warn_on_opensim_gc(Simulator):
    with nengo.Network() as net:
        nengo.Ensemble(10, 1)

    sim = Simulator(net)
    assert sim

    with warns(ResourceWarning):
        sim = None
        gc.collect()
示例#15
0
def test_warn_on_opensim_gc(Simulator):
    with nengo.Network() as net:
        nengo.Ensemble(10, 1)

    sim = Simulator(net)
    assert sim

    with warns(ResourceWarning):
        sim = None
        gc.collect()
示例#16
0
def test_balred(rng):
    dt = 0.001
    sys = Alpha(0.01) + Lowpass(0.001)

    u = rng.normal(size=2000)
    expected = apply_filter(sys, dt, u)

    def check(order, within, tol, method='del'):
        redsys = balred(sys, order, method=method)
        assert redsys.order_den <= order
        actual = apply_filter(redsys, dt, u)
        assert abs(rmse(expected, actual) - within) < tol

    with warns(UserWarning):
        check(4, 0, 1e-13)
    with warns(UserWarning):
        check(3, 0, 1e-13)
    check(2, 0.03, 0.01)
    check(1, 0.3, 0.1)
示例#17
0
def test_eval_points_number_warning(Simulator, seed):
    model = nengo.Network(seed=seed)
    with model:
        A = nengo.Ensemble(5, 1, n_eval_points=10, eval_points=[[0.1], [0.2]])

    with warns(UserWarning):
        # n_eval_points doesn't match actual passed eval_points, which warns
        sim = Simulator(model)

    assert np.allclose(sim.data[A].eval_points, [[0.1], [0.2]])
示例#18
0
def test_create_pointer_warning(rng):
    v = Vocabulary(2, rng=rng)

    # five pointers shouldn't fit
    with warns(UserWarning):
        v.parse('A')
        v.parse('B')
        v.parse('C')
        v.parse('D')
        v.parse('E')
示例#19
0
def test_eval_points_number_warning(Simulator, seed):
    model = nengo.Network(seed=seed)
    with model:
        A = nengo.Ensemble(5, 1, n_eval_points=10, eval_points=[[0.1], [0.2]])

    with warns(UserWarning):
        # n_eval_points doesn't match actual passed eval_points, which warns
        sim = Simulator(model)

    assert np.allclose(sim.data[A].eval_points, [[0.1], [0.2]])
示例#20
0
def test_delay_invalid():
    with pytest.raises(ValidationError):
        PureDelay(1, order=0)

    with pytest.raises(ValidationError):
        PureDelay(1, order=1)

    with pytest.raises(ValidationError):
        PureDelay(1, order=2.5)

    with pytest.raises(ValidationError):
        PureDelay(1, order=2, p=0)

    with pytest.raises(ValidationError):
        PureDelay(1, order=2, p=1.5)

    with warns(UserWarning):
        PureDelay(1, order=10, p=8)
示例#21
0
def test_spa_vocab():
    # create a model without a vocab and check that it is empty
    model = spa.SPA()
    assert model._default_vocabs == {}

    # create a model with a vocab and check that it's filled
    va = spa.Vocabulary(16)
    va.parse("PANTS")
    vb = spa.Vocabulary(32)
    vb.parse("SHOES")
    model = spa.SPA(vocabs=[va, vb])
    assert model._default_vocabs[16].keys == ["PANTS"]
    assert model._default_vocabs[32].keys == ["SHOES"]

    # warning on vocabs with duplicate dimensions
    vc = spa.Vocabulary(16)
    vc.parse("SOCKS")
    with warns(UserWarning):
        model = spa.SPA(vocabs=[va, vb, vc])
    assert model._default_vocabs[16].keys == ["SOCKS"]
    assert model._default_vocabs[32].keys == ["SHOES"]
示例#22
0
 def test_unpickling_warning_in_network(self, make_f):
     original = make_f()
     pkl = pickle.dumps(original)
     with nengo.Network():
         with warns(NotAddedToNetworkWarning):
             pickle.loads(pkl)
示例#23
0
def test_missing_attribute():
    with nengo.Network():
        a = nengo.Ensemble(10, 1)

        with warns(SyntaxWarning):
            a.dne = 9
示例#24
0
def test_seed_warning():
    with Network():
        ens = nengo.Ensemble(100, 1)
        res = Reservoir(ens, ens)
        with warns(UserWarning):
            res.train(lambda x: x, 1.0, 0.001, WhiteSignal(1.0, high=10))
示例#25
0
 def test_python_copy_warns_abt_adding_to_network(self, make_f, assert_f):
     original = make_f()
     copy(original)  # Fine because not in a network
     with nengo.Network():
         with warns(NotAddedToNetworkWarning):
             copy(original)
示例#26
0
def test_missing_attribute():
    with nengo.Network():
        a = nengo.Ensemble(10, 1)

        with warns(SyntaxWarning):
            a.dne = 9
示例#27
0
 def test_unpickling_warning_in_network(self, make_f):
     original = make_f()
     pkl = pickle.dumps(original)
     with nengo.Network():
         with warns(NotAddedToNetworkWarning):
             pickle.loads(pkl)
示例#28
0
def test_model_attribute_is_deprecated(RefSimulator):
    with warns(DeprecationWarning):
        with RefSimulator(nengo.Network()) as sim:
            pass
        assert sim.model
示例#29
0
def test_unstable_warning():
    with warns(UserWarning):
        with Network():
            LinearNetwork(
                ~s, 1, synapse=0.02, dt=0.001, normalizer=Controllable())
示例#30
0
def test_output_warning():
    with warns(UserWarning):
        LinearNetwork(([1, 1], [1, 1]), 1, synapse=1, dt=1)
示例#31
0
 def test_python_copy_warns_abt_adding_to_network(self, make_f, assert_f):
     original = make_f()
     copy(original)  # Fine because not in a network
     with nengo.Network():
         with warns(NotAddedToNetworkWarning):
             copy(original)
示例#32
0
def test_hypersphere_warns(rng):
    with warns(UserWarning):
        dists.UniformHypersphere(surface=True, min_magnitude=0.1)