Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bthirion committed May 13, 2015
1 parent d252e40 commit 602f5bd
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions nistats/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
from ..utils import multiple_mahalanobis, z_score, multiple_fast_inv
from nose.tools import assert_true
from numpy.testing import assert_almost_equal, assert_array_almost_equal
from ..utils import (matrix_rank, full_rank, pos_recipr)
from nose.tools import (assert_true, assert_equal, assert_false,
assert_raises)


def test_z_score():
p = np.random.rand(10)
Expand Down Expand Up @@ -41,6 +45,43 @@ def test_multiple_fast_inv():
X_inv = multiple_fast_inv(X)
assert_almost_equal(X_inv_ref, X_inv)

def test_full_rank():
rng = np.random.RandomState(20110831)
X = rng.standard_normal((40,5))
# A quick rank check
assert_equal(matrix_rank(X), 5)
X[:,0] = X[:,1] + X[:,2]
assert_equal(matrix_rank(X), 4)
Y1 = full_rank(X)
assert_equal(Y1.shape, (40,4))
Y2 = full_rank(X, r=3)
assert_equal(Y2.shape, (40,3))
Y3 = full_rank(X, r=4)
assert_equal(Y3.shape, (40,4))
# Windows - there seems to be some randomness in the SVD result; standardize
# column signs before comparison
flipper = np.sign(Y1[0]) * np.sign(Y3[0])
assert_almost_equal(Y1, Y3 * flipper)


def test_pos_recipr():
X = np.array([2,1,-1,0], dtype=np.int8)
eX = np.array([0.5,1,0,0])
Y = pos_recipr(X)
yield assert_array_almost_equal, Y, eX
yield assert_equal, Y.dtype.type, np.float64
X2 = X.reshape((2,2))
Y2 = pos_recipr(X2)
yield assert_array_almost_equal, Y2, eX.reshape((2,2))
# check that lists have arrived
XL = [0, 1, -1]
yield assert_array_almost_equal, pos_recipr(XL), [0, 1, 0]
# scalars
yield assert_equal, pos_recipr(-1), 0
yield assert_equal, pos_recipr(0), 0
yield assert_equal, pos_recipr(2), 0.5



if __name__ == "__main__":
import nose
Expand Down

0 comments on commit 602f5bd

Please sign in to comment.