def test_affine_transform(self): X = dot(randn(5, 5), randn(5, 1000)) + randn(5, 1) Y = dot(randn(2, 2), randn(2, 1000)) + dot(randn(2, 5), X) meanIn = randn(5, 1) preIn = randn(5, 5) pre = AffineTransform(meanIn, preIn, Y.shape[0]) self.assertLess(max(abs(pre(X) - dot(preIn, X - meanIn))), 1e-10) # test inverse Xp, Yp = pre(X, Y) Xr, Yr = pre.inverse(Xp, Yp) self.assertLess(max(abs(Xr - X)), 1e-10) self.assertLess(max(abs(Yr - Y)), 1e-10) # reference counts should not change Xrc = sys.getrefcount(X) Yrc = sys.getrefcount(Y) for i in range(10): pre(X, Y) self.assertEqual(sys.getrefcount(X), Xrc) self.assertEqual(sys.getrefcount(Y), Yrc)
def test_affine_transform_pickle(self): meanIn = randn(5, 1) preIn = randn(5, 5) dim_out = 3 pre0 = AffineTransform(meanIn, preIn, dim_out) tmp_file = mkstemp()[1] # store transformation with open(tmp_file, 'w') as handle: dump({'pre': pre0}, handle) # load transformation with open(tmp_file) as handle: pre1 = load(handle)['pre'] X, Y = randn(5, 100), randn(3, 100) X0, Y0 = pre0(X, Y) X1, Y1 = pre1(X, Y) # make sure linear transformation hasn't changed self.assertLess(max(abs(X0 - X1)), 1e-20) self.assertLess(max(abs(Y0 - Y1)), 1e-20)
def test_sample_spike_train(self): inputs = array([[0, 0, 0, 0, 1, 1, 1, 1], [0, 0, 1, 1, 0, 0, 1, 1], [0, 1, 0, 1, 0, 1, 0, 1]]) outputs = array([[1, 0, 0, 0, 1, 0, 0, 0]]) glm = GLM(3, LogisticFunction, Bernoulli) glm.train(inputs, outputs) # generate a spike train without any stimulus input spike_train = sample_spike_train(empty([0, 100]), glm, 3) # test difference to expected spike train diff = spike_train.ravel()[:10] - [0, 0, 0, 1, 0, 0, 1, 0, 0, 1] self.assertLess(max(abs(diff)), 1e-8) # preconditioner which removes first (uninformative) dimension from input m = zeros([3, 1]) A = array([[0, 1, 0], [0, 0, 1]]) pre = AffineTransform(m, A) glm = GLM(2, LogisticFunction, Bernoulli) glm.train(pre(inputs), outputs) # generate a spike train with preconditioned spike history spike_train = sample_spike_train(empty([0, 100]), glm, 3, pre) # test difference to expected spike train diff = spike_train.ravel()[:10] - [0, 0, 0, 1, 0, 0, 1, 0, 0, 1] self.assertLess(max(abs(diff)), 1e-8)
def __new__(cls, *args, **kwargs): return AffineTransform( mean_in=vstack([transform.mean_in for transform in args]), pre_in=block_diag(*[transform.pre_in for transform in args]), **kwargs)