Skip to content

Commit

Permalink
[FIX,TST] Adds unit tests for ma_pca functions and fixes a bug in ma_…
Browse files Browse the repository at this point in the history
…pca (#549)

* Adds unit tests for ma_pca functions and fixes a bug in ma_pca

* Adds trivwin false check on test_check_order

* Update tedana/tests/test_mapca.py

Co-Authored-By: Taylor Salo <tsalo006@fiu.edu>

Co-authored-by: Taylor Salo <tsalo006@fiu.edu>
  • Loading branch information
eurunuela and tsalo authored Mar 23, 2020
1 parent 4ce1f38 commit 5807ea8
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tedana/decomposition/ma_pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ def _icatb_svd(data, n_comps=None):
"""

if not n_comps:
n_comps = np.min(data.shape[0], data.shape[1])
n_comps = np.min((data.shape[0], data.shape[1]))

_, Lambda, vh = svd(data, full_matrices=False)

Expand Down
98 changes: 98 additions & 0 deletions tedana/tests/test_mapca.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,55 @@
import nibabel as nib
from tedana import decomposition
from pytest import raises
from tedana.decomposition.ma_pca import _autocorr, _check_order, _parzen_win
from tedana.decomposition.ma_pca import _subsampling, _kurtn, _icatb_svd, _eigensp_adj


def test_autocorr():
"""
Unit test on _autocorr function
"""
test_data = np.array([1, 2, 3, 4])
test_result = np.array([30, 20, 11, 4])
autocorr = _autocorr(test_data)
assert np.array_equal(autocorr, test_result)


def test_check_order():
"""
Unit test on _check_order function
"""
test_order = -1
with raises(ValueError) as errorinfo:
ord_out, w, trivwin = _check_order(test_order)
assert 'Order cannot be less than zero' in str(errorinfo.value)

test_order = 0
ord_out, w, trivwin = _check_order(test_order)
assert ord_out == test_order
assert trivwin

test_order = 1
ord_out, w, trivwin = _check_order(test_order)
assert ord_out == test_order
assert w == 1
assert trivwin

test_order = 4
ord_out, w, trivwin = _check_order(test_order)
assert ord_out == test_order
assert not trivwin


def test_parzen_win():
test_npoints = 3
test_result = np.array([0.07407407, 1, 0.07407407])
win = _parzen_win(test_npoints)
assert np.allclose(win, test_result)

test_npoints = 1
win = _parzen_win(test_npoints)
assert win == 1


def test_ent_rate_sp():
Expand All @@ -24,6 +73,55 @@ def test_ent_rate_sp():
ent_rate = decomposition.ent_rate_sp(test_data, 1)
assert 'Divide by zero encountered' in str(errorinfo.value)

# Checks ValueError with incorrect matrix dimensions
test_data = np.ones((200, 10, 10, 200))
with raises(ValueError) as errorinfo:
ent_rate = decomposition.ent_rate_sp(test_data, 1)
assert 'Incorrect matrix dimensions' in str(errorinfo.value)


def test_subsampling():
"""
Unit test for subsampling function
"""
test_data = np.array([1])
with raises(ValueError) as errorinfo:
sub_data = _subsampling(test_data, 1)
assert 'Unrecognized matrix dimension' in str(errorinfo.value)

test_data = np.random.rand(2, 3, 4)
sub_data = _subsampling(test_data, sub_depth=2)
assert sub_data.shape == (1, 2, 2)


def test_kurtn():
"""
Unit test for _kurtn function
"""
test_data = np.random.rand(2, 3, 4)
kurt = _kurtn(test_data)
assert kurt.shape == (3, 1)


def test_icatb_svd():
"""
Unit test for icatb_svd function.
"""
test_data = np.diag(np.random.rand(5))
V, Lambda = _icatb_svd(test_data)
assert np.allclose(np.sum(V, axis=0), np.ones((5,)))


def test_eigensp_adj():
"""
Unit test for eigensp_adj function
"""
test_eigen = np.array([0.9, 0.5, 0.2, 0.1, 0])
n_effective = 2
test_result = np.array([0.13508894, 0.11653465, 0.06727316, 0.05211424, 0.])
lambd_adj = _eigensp_adj(test_eigen, n_effective, p=test_eigen.shape[0])
assert np.allclose(lambd_adj, test_result)


def test_ma_pca():
"""
Expand Down

0 comments on commit 5807ea8

Please sign in to comment.