def test_commonsig_readonly(): """Test that the common signals cannot be modified.""" net = nengo.Network(label="test_commonsig") model = Model() model.build(net) signals = SignalDict() for sig in itervalues(model.sig['common']): signals.init(sig) with pytest.raises((ValueError, RuntimeError)): signals[sig] = np.array([-1]) with pytest.raises((ValueError, RuntimeError)): signals[sig][...] = np.array([-1])
def test_signaldict(allclose): """Tests SignalDict's dict overrides.""" signaldict = SignalDict() scalar = Signal(1.0) # Both __getitem__ and __setitem__ raise KeyError with pytest.raises(KeyError): signaldict[scalar] with pytest.raises(KeyError): signaldict[scalar] = np.array(1.0) signaldict.init(scalar) # tests repeat init with pytest.raises(SignalError, match="Cannot add signal twice"): signaldict.init(scalar) assert allclose(signaldict[scalar], np.array(1.0)) # __getitem__ handles scalars assert signaldict[scalar].shape == () one_d = Signal([1.0]) signaldict.init(one_d) assert allclose(signaldict[one_d], np.array([1.0])) assert signaldict[one_d].shape == (1, ) two_d = Signal([[1.0], [1.0]]) signaldict.init(two_d) assert allclose(signaldict[two_d], np.array([[1.0], [1.0]])) assert signaldict[two_d].shape == (2, 1) # __getitem__ handles views implicitly (note no .init) two_d_view = two_d[0, :] assert allclose(signaldict[two_d_view], np.array([1.0])) assert signaldict[two_d_view].shape == (1, ) # __setitem__ ensures memory location stays the same memloc = signaldict[scalar].__array_interface__["data"][0] signaldict[scalar] = np.array(0.0) assert allclose(signaldict[scalar], np.array(0.0)) assert signaldict[scalar].__array_interface__["data"][0] == memloc memloc = signaldict[one_d].__array_interface__["data"][0] signaldict[one_d] = np.array([0.0]) assert allclose(signaldict[one_d], np.array([0.0])) assert signaldict[one_d].__array_interface__["data"][0] == memloc memloc = signaldict[two_d].__array_interface__["data"][0] signaldict[two_d] = np.array([[0.0], [0.0]]) assert allclose(signaldict[two_d], np.array([[0.0], [0.0]])) assert signaldict[two_d].__array_interface__["data"][0] == memloc # __str__ pretty-prints signals and current values # Order not guaranteed for dicts, so we have to loop for k in signaldict: assert "%s %s" % (repr(k), repr(signaldict[k])) in str(signaldict)
def test_signal_reshape(): """Tests Signal.reshape""" # check proper shape after reshape three_d = Signal(np.ones((2, 2, 2))) assert three_d.reshape((8, )).shape == (8, ) assert three_d.reshape((4, 2)).shape == (4, 2) assert three_d.reshape((2, 4)).shape == (2, 4) assert three_d.reshape(-1).shape == (8, ) assert three_d.reshape((4, -1)).shape == (4, 2) assert three_d.reshape((-1, 4)).shape == (2, 4) assert three_d.reshape((2, -1, 2)).shape == (2, 2, 2) assert three_d.reshape((1, 2, 1, 2, 2, 1)).shape == (1, 2, 1, 2, 2, 1) # check with non-contiguous arrays (and with offset) value = np.arange(20).reshape(5, 4) s = Signal(np.array(value), name='s') s0slice = slice(0, 3), slice(None, None, 2) s0shape = 2, 3 s0 = s[s0slice].reshape(*s0shape) assert s.offset == 0 assert np.array_equal(s0.initial_value, value[s0slice].reshape(*s0shape)) s1slice = slice(1, None), slice(None, None, 2) s1shape = 2, 4 s1 = s[s1slice].reshape(s1shape) assert s1.offset == 4 * s1.dtype.itemsize assert np.array_equal(s1.initial_value, value[s1slice].reshape(s1shape)) # check error if non-contiguous array cannot be reshaped without copy s2slice = slice(None, None, 2), slice(None, None, 2) s2shape = 2, 3 s2 = s[s2slice] with pytest.raises(SignalError): s2.reshape(s2shape) # check that views are working properly (incrementing `s` effects views) values = SignalDict() values.init(s) values.init(s0) values.init(s1) values[s] += 1 assert np.array_equal(values[s0], value[s0slice].reshape(s0shape) + 1) assert np.array_equal(values[s1], value[s1slice].reshape(s1shape) + 1)
def test_signal_reshape(): """Tests Signal.reshape""" # check proper shape after reshape three_d = Signal(np.ones((2, 2, 2))) assert three_d.reshape((8,)).shape == (8,) assert three_d.reshape((4, 2)).shape == (4, 2) assert three_d.reshape((2, 4)).shape == (2, 4) assert three_d.reshape(-1).shape == (8,) assert three_d.reshape((4, -1)).shape == (4, 2) assert three_d.reshape((-1, 4)).shape == (2, 4) assert three_d.reshape((2, -1, 2)).shape == (2, 2, 2) assert three_d.reshape((1, 2, 1, 2, 2, 1)).shape == (1, 2, 1, 2, 2, 1) # check with non-contiguous arrays (and with offset) value = np.arange(20).reshape(5, 4) s = Signal(np.array(value), name='s') s0slice = slice(0, 3), slice(None, None, 2) s0shape = 2, 3 s0 = s[s0slice].reshape(*s0shape) assert s.offset == 0 assert np.array_equal(s0.initial_value, value[s0slice].reshape(*s0shape)) s1slice = slice(1, None), slice(None, None, 2) s1shape = 2, 4 s1 = s[s1slice].reshape(s1shape) assert s1.offset == 4 * s1.dtype.itemsize assert np.array_equal(s1.initial_value, value[s1slice].reshape(s1shape)) # check error if non-contiguous array cannot be reshaped without copy s2slice = slice(None, None, 2), slice(None, None, 2) s2shape = 2, 3 s2 = s[s2slice] with pytest.raises(SignalError): s2.reshape(s2shape) # check that views are working properly (incrementing `s` effects views) values = SignalDict() values.init(s) values.init(s0) values.init(s1) values[s] += 1 assert np.array_equal(values[s0], value[s0slice].reshape(s0shape) + 1) assert np.array_equal(values[s1], value[s1slice].reshape(s1shape) + 1)
def test_signal_init(sig_type): if sig_type == "sparse_scipy": pytest.importorskip("scipy.sparse") sig, dense = make_signal( sig_type, shape=(3, 3), indices=np.asarray([[0, 0], [0, 2], [1, 1], [2, 2]]), data=[1.0, 2.0, 1.0, 1.5], ) signals = SignalDict() signals.init(sig) assert np.all(signals[sig] == dense) sig.readonly = True signals = SignalDict() signals.init(sig) with pytest.raises((ValueError, RuntimeError, TypeError)): signals[sig].data[0] = -1
def test_signaldict(): """Tests SignalDict's dict overrides.""" signaldict = SignalDict() scalar = Signal(1.) # Both __getitem__ and __setitem__ raise KeyError with pytest.raises(KeyError): signaldict[scalar] with pytest.raises(KeyError): signaldict[scalar] = np.array(1.) signaldict.init(scalar) assert np.allclose(signaldict[scalar], np.array(1.)) # __getitem__ handles scalars assert signaldict[scalar].shape == () one_d = Signal([1.]) signaldict.init(one_d) assert np.allclose(signaldict[one_d], np.array([1.])) assert signaldict[one_d].shape == (1,) two_d = Signal([[1.], [1.]]) signaldict.init(two_d) assert np.allclose(signaldict[two_d], np.array([[1.], [1.]])) assert signaldict[two_d].shape == (2, 1) # __getitem__ handles views two_d_view = two_d[0, :] signaldict.init(two_d_view) assert np.allclose(signaldict[two_d_view], np.array([1.])) assert signaldict[two_d_view].shape == (1,) # __setitem__ ensures memory location stays the same memloc = signaldict[scalar].__array_interface__['data'][0] signaldict[scalar] = np.array(0.) assert np.allclose(signaldict[scalar], np.array(0.)) assert signaldict[scalar].__array_interface__['data'][0] == memloc memloc = signaldict[one_d].__array_interface__['data'][0] signaldict[one_d] = np.array([0.]) assert np.allclose(signaldict[one_d], np.array([0.])) assert signaldict[one_d].__array_interface__['data'][0] == memloc memloc = signaldict[two_d].__array_interface__['data'][0] signaldict[two_d] = np.array([[0.], [0.]]) assert np.allclose(signaldict[two_d], np.array([[0.], [0.]])) assert signaldict[two_d].__array_interface__['data'][0] == memloc # __str__ pretty-prints signals and current values # Order not guaranteed for dicts, so we have to loop for k in signaldict: assert "%s %s" % (repr(k), repr(signaldict[k])) in str(signaldict)
def test_signaldict(): """Tests SignalDict's dict overrides.""" signaldict = SignalDict() scalar = Signal(1.) # Both __getitem__ and __setitem__ raise KeyError with pytest.raises(KeyError): signaldict[scalar] with pytest.raises(KeyError): signaldict[scalar] = np.array(1.) signaldict.init(scalar) assert np.allclose(signaldict[scalar], np.array(1.)) # __getitem__ handles scalars assert signaldict[scalar].shape == () one_d = Signal([1.]) signaldict.init(one_d) assert np.allclose(signaldict[one_d], np.array([1.])) assert signaldict[one_d].shape == (1, ) two_d = Signal([[1.], [1.]]) signaldict.init(two_d) assert np.allclose(signaldict[two_d], np.array([[1.], [1.]])) assert signaldict[two_d].shape == (2, 1) # __getitem__ handles views two_d_view = two_d[0, :] signaldict.init(two_d_view) assert np.allclose(signaldict[two_d_view], np.array([1.])) assert signaldict[two_d_view].shape == (1, ) # __setitem__ ensures memory location stays the same memloc = signaldict[scalar].__array_interface__['data'][0] signaldict[scalar] = np.array(0.) assert np.allclose(signaldict[scalar], np.array(0.)) assert signaldict[scalar].__array_interface__['data'][0] == memloc memloc = signaldict[one_d].__array_interface__['data'][0] signaldict[one_d] = np.array([0.]) assert np.allclose(signaldict[one_d], np.array([0.])) assert signaldict[one_d].__array_interface__['data'][0] == memloc memloc = signaldict[two_d].__array_interface__['data'][0] signaldict[two_d] = np.array([[0.], [0.]]) assert np.allclose(signaldict[two_d], np.array([[0.], [0.]])) assert signaldict[two_d].__array_interface__['data'][0] == memloc # __str__ pretty-prints signals and current values # Order not guaranteed for dicts, so we have to loop for k in signaldict: assert "%s %s" % (repr(k), repr(signaldict[k])) in str(signaldict)
def test_signaldict_reset(): """Tests SignalDict's reset function.""" signaldict = SignalDict() two_d = Signal([[1], [1]]) signaldict.init(two_d) two_d_view = two_d[0, :] signaldict[two_d_view] = -0.5 assert np.allclose(signaldict[two_d], np.array([[-0.5], [1]])) signaldict[two_d] = np.array([[-1], [-1]]) assert np.allclose(signaldict[two_d], np.array([[-1], [-1]])) assert np.allclose(signaldict[two_d_view], np.array([-1])) signaldict.reset(two_d_view) assert np.allclose(signaldict[two_d_view], np.array([1])) assert np.allclose(signaldict[two_d], np.array([[1], [-1]])) signaldict.reset(two_d) assert np.allclose(signaldict[two_d], np.array([[1], [1]]))