Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronmartino committed Jun 18, 2019
1 parent 72a3e98 commit e8b5f4d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 46 deletions.
17 changes: 17 additions & 0 deletions deicode/scripts/tests/test_standalone_rpca.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@ def test_standalone_rpca(self):
# Lastly, check that DEICODE's exit code was 0 (indicating success)
self.assertEqual(result.exit_code, 0)

def test_standalone_rpca_n_components(self):
"""Tests the standalone script when n_components is 2
"""
in_ = get_data_path('test.biom')
out_ = os_path_sep.join(in_.split(os_path_sep)[:-1])
runner = CliRunner()
# run the same command but with rank==2
result = runner.invoke(standalone_rpca, ['--in-biom', in_,
'--output-dir', out_,
'--n_components', 2,
'--max_iterations', 5])
self.assertEqual(result.exit_code, 0)
ord_res = OrdinationResults.read(get_data_path('ordination.txt'))
# check it contains three axis
if len(ord_res.proportion_explained) == 3:
pass


if __name__ == "__main__":
unittest.main()
9 changes: 2 additions & 7 deletions deicode/tests/test_base.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import unittest
from deicode.base import _BaseTransform, _BaseImpute


class Test_BaseTransform(unittest.TestCase):
def test_no_instantiation(self):
class Foo(_BaseTransform):
pass
from deicode.base import _BaseImpute


class Test_BaseImpute(unittest.TestCase):
"""Tests base class imports."""
def test_no_instantiation(self):
class Foo_boo(_BaseImpute):
pass
66 changes: 37 additions & 29 deletions deicode/tests/test_optspace.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from deicode._optspace import (
G,
F_t,
gradF_t,
Gp,
getoptT,
getoptS,
optspace,
from deicode.optspace import (
grassmann_manifold_one,
cost_function,
gradient_decent,
grassmann_manifold_two,
line_search,
singular_values,
OptSpace,
svd_sort)
import numpy as np
from numpy.linalg import norm
Expand All @@ -21,22 +21,23 @@ def setUp(self):
pass

def test_G(self):
"""Test first grassmann manifold runs."""
X = np.ones((10, 10))
m0 = 2
r = 2
exp = G(X, m0, r)
exp = grassmann_manifold_one(X, m0, r)
self.assertAlmostEqual(exp, 0.644944589179)

def test_G_z_0(self):

"""Test first grassmann manifold converges."""
X = np.array([[1, 3], [4, 1], [2, 1]])
m0 = 2
r = 2
exp = G(X, m0, r)
exp = grassmann_manifold_one(X, m0, r)
self.assertAlmostEqual(exp, 2.60980232)

def test_F_t(self):

"""Test cost function coverages."""
X = np.ones((5, 5))
Y = np.ones((5, 5))
E = np.zeros((5, 5))
Expand All @@ -47,12 +48,12 @@ def test_F_t(self):
M_E[0, 0] = 1
m0 = 2
rho = 0.5
res = F_t(X, Y, S, M_E, E, m0, rho)
res = cost_function(X, Y, S, M_E, E, m0, rho)
exp = 1
assert_array_almost_equal(res, exp, decimal=3)

def test_F_t_random(self):

"""Test cost function on random values."""
# random ones and zeros
np.random.seed(0)
X = np.ones((5, 5))
Expand All @@ -63,10 +64,11 @@ def test_F_t_random(self):
M_E[0, 0] = 1
m0 = 2
rho = 0.5
res = F_t(X, Y, S, M_E, E, m0, rho)
res = cost_function(X, Y, S, M_E, E, m0, rho)
self.assertAlmostEqual(res, 6.5)

def test_gradF_t(self):
"""Test gradient decent converges."""
X = np.ones((5, 5))
Y = np.ones((5, 5))
E = np.zeros((5, 5))
Expand All @@ -77,7 +79,7 @@ def test_gradF_t(self):
M_E[0, 0] = 1
m0 = 2
rho = 0.5
res = gradF_t(X, Y, S, M_E, E, m0, rho)
res = gradient_decent(X, Y, S, M_E, E, m0, rho)
exp = np.array([[[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[2., 2., 2., 2., 2.],
Expand All @@ -91,11 +93,12 @@ def test_gradF_t(self):
npt.assert_allclose(exp, res)

def test_Gp(self):
"""Test second grassmann manifold converges."""
X = np.ones((5, 5)) * 3
X[0, 0] = 2
m0 = 2
r = 5
res = Gp(X, m0, r)
res = grassmann_manifold_two(X, m0, r)
exp = np.array(
[[1.08731273, 1.6309691, 1.6309691, 1.6309691, 1.6309691],
[3.57804989, 3.57804989, 3.57804989, 3.57804989, 3.57804989],
Expand All @@ -107,6 +110,7 @@ def test_Gp(self):
npt.assert_allclose(exp, res)

def test_getoptT(self):
"""Test gradient decent line search."""
X = np.ones((5, 5))
Y = np.ones((5, 5))
E = np.zeros((5, 5))
Expand All @@ -117,52 +121,56 @@ def test_getoptT(self):
M_E[0, 0] = 1
m0 = 2
rho = 0.5
W, Z = gradF_t(X, Y, S, M_E, E, m0, rho)
res = getoptT(X, W, Y, Z, S, M_E, E, m0, rho)
W, Z = gradient_decent(X, Y, S, M_E, E, m0, rho)
res = line_search(X, W, Y, Z, S, M_E, E, m0, rho)
exp = -9.5367431640625e-08
npt.assert_allclose(exp, res)

def test_getoptS_small(self):
# warning : this test must ALWAYS pass
"""Test singular values from U and V."""
data = loadmat(get_data_path('small_test.mat'))

M_E = np.array(data['M_E'].todense())
E = data['E']

x = data['x']
y = data['y']
res = getoptS(x, y, M_E, E)
res = singular_values(x, y, M_E, E)
exp = np.array([[0.93639499, 0.07644197, -0.02828782],
[-0.03960841, 0.60787383, 0.00521257],
[0.00729038, 0.00785834, 0.67853083]])
npt.assert_allclose(res, exp, atol=1e-5)

def test_optspace_original(self):
"""Test OptSpace converges on test dataset."""
M0 = loadmat(get_data_path('large_test.mat'))['M0']
M_E = loadmat(get_data_path('large_test.mat'))['M_E']

M0 = M0.astype(np.float)
M_E = np.array(M_E.todense()).astype(np.float)
X, S, Y, dist = optspace(M_E, r=3, niter=11, tol=1e-8)
err = X.dot(S).dot(Y.T) - M0
X, S, Y = OptSpace(n_components=3,
max_iterations=11,
tol=1e-8).solve(M_E)
err = X[:, ::-1].dot(S).dot(Y[:, ::-1].T) - M0
n, m = M0.shape

res = norm(err, 'fro') / np.sqrt(m * n)
exp = 0.0010701845536
assert_array_almost_equal(res, exp, decimal=3)

def test_optspace_ordering(self):
"""Test OptSpace produces reproducible loadings."""
# the expected sorting
# for U, S, V.
s_exp = np.array([[5, 4, 1],
[8, 3, 0],
[7, 9, 2]])
U_exp = np.array([[0, 3, 6],
[1, 4, 7],
[2, 5, 8]])
V_exp = np.array([[0, 3, 6],
[1, 4, 7],
[2, 5, 8]])
U_exp = np.array([[6, 3, 0],
[7, 4, 1],
[8, 5, 2]])
V_exp = np.array([[6, 3, 0],
[7, 4, 1],
[8, 5, 2]])
# un-sorted U,s,v from SVD.
s_test = np.array([[5, 1, 4],
[7, 2, 9],
Expand Down
36 changes: 26 additions & 10 deletions deicode/tests/test_preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,43 @@

class Testpreprocessing(unittest.TestCase):
def setUp(self):

self.cdata1 = np.array([[2, 2, 6],
[4, 4, 2]])
self.cdata2 = [[3, 3, 0], [0, 4, 2]]
self.cdata2 = np.array([[3, 3, 0],
[0, 4, 2]])
self.true2 = np.array([[0.0, 0.0, np.nan],
[np.nan, 0.34657359, -0.34657359]])

self.bad1 = np.array([1, 2, -1])
self.bad1
self._rclr = rclr()
self.bad2 = np.array([1, 2, np.inf])
self.bad3 = np.array([1, 2, np.nan])
pass

def test_rclr(self):

def test_rclr_dense(self):
"""Test rclr and clr are the same on dense datasets."""
# test clr works the same if there are no zeros
cmat = self._rclr.fit_transform(self.cdata1)
cmat = rclr(self.cdata1)
npt.assert_allclose(cmat, clr(self.cdata1.copy()))

def test_rclr_sparse(self):
"""Test rclr on sparse data."""
# test a case with zeros :)
cmat = self._rclr.fit_transform(self.cdata2)
cmat = rclr(self.cdata2)
npt.assert_allclose(cmat, self.true2)

def test_rclr_negative_raises(self):
"""Test rclr ValueError on negative."""
# test negatives throw value error
with self.assertRaises(ValueError):
rclr(self.bad1)

def test_rclr_inf_raises(self):
"""Test rclr ValueError on undefined."""
# test undefined throw value error
with self.assertRaises(ValueError):
rclr(self.bad2)

def test_rclr_nan_raises(self):
"""Test rclr ValueError on missing (as nan)."""
# test nan throw value error
with self.assertRaises(ValueError):
self._rclr.fit_transform(self.bad1)
rclr(self.bad3)

0 comments on commit e8b5f4d

Please sign in to comment.