Skip to content

Commit

Permalink
[ENH] Consolidate stats unit tests and fix linting errors in tests (#421
Browse files Browse the repository at this point in the history
)

* Consolidates stats unit tests
* Changes to simpler assert for stats errors
* Oof linting

Co-Authored-By: Elizabeth DuPre <[email protected]>
  • Loading branch information
jbteves and emdupre authored Nov 11, 2019
1 parent b689504 commit 8e565f6
Show file tree
Hide file tree
Showing 14 changed files with 270 additions and 333 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ parentdir_prefix =

[flake8]
max-line-length = 99
exclude=*build/,tests
exclude=*build/
ignore = E126,E402,W504
per-file-ignores =
*/__init__.py:F401
75 changes: 37 additions & 38 deletions tedana/tests/test_decay.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@pytest.fixture(scope='module')
def testdata1():
tes = np.array([14.5, 38.5, 62.5])
in_files = [op.join(get_test_data_path(), 'echo{0}.nii.gz'.format(i+1))
in_files = [op.join(get_test_data_path(), 'echo{0}.nii.gz'.format(i + 1))
for i in range(3)]
data, _ = io.load_data(in_files, n_echos=len(tes))
mask, mask_sum = utils.make_adaptive_mask(data, getsum=True)
Expand Down Expand Up @@ -64,22 +64,21 @@ def test_fit_decay_ts(testdata1):

def test_smoke_fit_decay():
"""
test_smoke_fit_decay tests that the function fit_decay returns reasonable
objects with random inputs in the correct format
test_smoke_fit_decay tests that the function fit_decay returns reasonable
objects with random inputs in the correct format
"""
n_samples = 100
n_echos = 5
n_times = 20
data = np.random.random((n_samples, n_echos, n_times))
tes = np.random.random((n_echos)).tolist()
mask = np.random.randint(2, size=n_samples) # generate binary mask of random 0s and 1s
mask = np.random.randint(2, size=n_samples) # generate binary mask of random 0s and 1s
masksum = np.random.random((n_samples))
fittype='loglin'
t2s_limited, s0_limited, t2ss, s0vs, t2s_full, s0_full = me.fit_decay(data,
tes,
mask,
masksum,
fittype)
fittype = 'loglin'
t2s_limited, s0_limited, t2ss, s0vs, t2s_full, s0_full = me.fit_decay(
data, tes,
mask, masksum,
fittype)
assert t2s_limited is not None
assert s0_limited is not None
assert t2ss is not None
Expand All @@ -90,23 +89,23 @@ def test_smoke_fit_decay():

def test_smoke_fit_decay_curvefit():
"""
test_smoke_fit_decay tests that the function fit_decay returns reasonable
test_smoke_fit_decay tests that the function fit_decay returns reasonable
objects with random inputs in the correct format when using the direct
monoexponetial approach
monoexponetial approach
"""
n_samples = 100
n_echos = 5
n_times = 20
data = np.random.random((n_samples, n_echos, n_times))
tes = np.random.random((n_echos)).tolist()
mask = np.random.randint(2, size=n_samples) # generate binary mask of random 0s and 1s
mask = np.random.randint(2, size=n_samples) # generate binary mask of random 0s and 1s
masksum = np.random.random((n_samples))
fittype='curvefit'
t2s_limited, s0_limited, t2ss, s0vs, t2s_full, s0_full = me.fit_decay(data,
tes,
mask,
masksum,
fittype)
fittype = 'curvefit'
t2s_limited, s0_limited, t2ss, s0vs, t2s_full, s0_full = me.fit_decay(
data, tes,
mask,
masksum,
fittype)
assert t2s_limited is not None
assert s0_limited is not None
assert t2ss is not None
Expand All @@ -117,22 +116,22 @@ def test_smoke_fit_decay_curvefit():

def test_smoke_fit_decay_ts():
"""
test_smoke_fit_decay_ts tests that the function fit_decay_ts returns reasonable
objects with random inputs in the correct format
test_smoke_fit_decay_ts tests that the function fit_decay_ts returns reasonable
objects with random inputs in the correct format
"""
n_samples = 100
n_echos = 5
n_times = 20
data = np.random.random((n_samples, n_echos, n_times))
tes = np.random.random((n_echos)).tolist()
mask = np.random.randint(2, size=n_samples) # generate binary mask of random 0s and 1s
mask = np.random.randint(2, size=n_samples) # generate binary mask of random 0s and 1s
masksum = np.random.random((n_samples))
fittype='loglin'
t2s_limited_ts, s0_limited_ts, t2s_full_ts, s0_full_ts = me.fit_decay_ts(data,
tes,
mask,
masksum,
fittype)
fittype = 'loglin'
t2s_limited_ts, s0_limited_ts, t2s_full_ts, s0_full_ts = me.fit_decay_ts(
data, tes,
mask,
masksum,
fittype)
assert t2s_limited_ts is not None
assert s0_limited_ts is not None
assert t2s_full_ts is not None
Expand All @@ -141,25 +140,25 @@ def test_smoke_fit_decay_ts():

def test_smoke_fit_decay_curvefit_ts():
"""
test_smoke_fit_decay_ts tests that the function fit_decay_ts returns reasonable
test_smoke_fit_decay_ts tests that the function fit_decay_ts returns reasonable
objects with random inputs in the correct format when using the direct
monoexponetial approach
monoexponetial approach
"""
n_samples = 100
n_echos = 5
n_times = 20
data = np.random.random((n_samples, n_echos, n_times))
tes = np.random.random((n_echos)).tolist()
mask = np.random.randint(2, size=n_samples) # generate binary mask of random 0s and 1s
mask = np.random.randint(2, size=n_samples) # generate binary mask of random 0s and 1s
masksum = np.random.random((n_samples))
fittype='curvefit'
t2s_limited_ts, s0_limited_ts, t2s_full_ts, s0_full_ts = me.fit_decay_ts(data,
tes,
mask,
masksum,
fittype)
fittype = 'curvefit'
t2s_limited_ts, s0_limited_ts, t2s_full_ts, s0_full_ts = me.fit_decay_ts(
data, tes,
mask,
masksum,
fittype)
assert t2s_limited_ts is not None
assert s0_limited_ts is not None
assert t2s_full_ts is not None
assert s0_full_ts is not None
# TODO: BREAK AND UNIT TESTS
# TODO: BREAK AND UNIT TESTS
6 changes: 3 additions & 3 deletions tedana/tests/test_gscontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ def test_break_gscontrol_raw():
optcom = np.empty((n_samples, n_vols))
ref_img = ''

catd = np.empty((n_samples+1, n_echos, n_vols))
catd = np.empty((n_samples + 1, n_echos, n_vols))
with pytest.raises(ValueError) as e_info:
gsc.gscontrol_raw(catd=catd, optcom=optcom, n_echos=n_echos,
ref_img=ref_img, dtrank=4)
assert str(e_info.value) == ('First dimensions of catd ({0}) and optcom ({1}) do not '
'match'.format(catd.shape[0], optcom.shape[0]))

catd = np.empty((n_samples, n_echos+1, n_vols))
catd = np.empty((n_samples, n_echos + 1, n_vols))
with pytest.raises(ValueError) as e_info:
gsc.gscontrol_raw(catd=catd, optcom=optcom, n_echos=n_echos,
ref_img=ref_img, dtrank=4)
assert str(e_info.value) == ('Second dimension of catd ({0}) does not match '
'n_echos ({1})'.format(catd.shape[1], n_echos))

catd = np.empty((n_samples, n_echos, n_vols))
optcom = np.empty((n_samples, n_vols+1))
optcom = np.empty((n_samples, n_vols + 1))
with pytest.raises(ValueError) as e_info:
gsc.gscontrol_raw(catd=catd, optcom=optcom, n_echos=n_echos,
ref_img=ref_img, dtrank=4)
Expand Down
2 changes: 1 addition & 1 deletion tedana/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def test_integration_five_echo(skip_integration):
os.path.dirname(out_dir))
prepend = '/tmp/data/five-echo/p06.SBJ01_S09_Task11_e'
suffix = '.sm.nii.gz'
datalist = [prepend + str(i+1) + suffix for i in range(5)]
datalist = [prepend + str(i + 1) + suffix for i in range(5)]
tedana_workflow(
data=datalist,
tes=[15.4, 29.7, 44.0, 58.3, 72.6],
Expand Down
26 changes: 16 additions & 10 deletions tedana/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

data_dir = get_test_data_path()


def test_new_nii_like():
data, ref = me.load_data(fnames, n_echos=len(tes))
nimg = me.new_nii_like(ref, data)
Expand Down Expand Up @@ -61,15 +62,16 @@ def test_smoke_split_ts():
Ensures that split_ts returns output when fed in with random inputs
Note: classification is ["accepted", "rejected", "ignored"]
"""
np.random.seed(0) # seeded because comptable MUST have accepted components
np.random.seed(0) # seeded because comptable MUST have accepted components
n_samples = 100
n_times = 20
n_components = 6
data = np.random.random((n_samples, n_times))
mmix = np.random.random((n_times, n_components))
mask = np.random.randint(2, size=n_samples)

# creating the component table with component as random floats, a "metric," and random classification
# creating the component table with component as random floats,
# a "metric," and random classification
component = np.random.random((n_components))
metric = np.random.random((n_components))
classification = np.random.choice(["accepted", "rejected", "ignored"], n_components)
Expand All @@ -86,14 +88,15 @@ def test_smoke_write_split_ts():
"""
Ensures that write_split_ts writes out the expected files with random input and tear them down
"""
np.random.seed(0) # at least one accepted and one rejected, thus all files are generated
np.random.seed(0) # at least one accepted and one rejected, thus all files are generated
n_samples, n_times, n_components = 64350, 10, 6
data = np.random.random((n_samples, n_times))
mmix = np.random.random((n_times, n_components))
mask = np.random.randint(2, size=n_samples)
ref_img = os.path.join(data_dir, 'mask.nii.gz') # ref_img has shape of (39, 50, 33) so data is 64350 (39*33*50) x 10

# creating the component table with component as random floats, a "metric," and random classification
ref_img = os.path.join(data_dir, 'mask.nii.gz')
# ref_img has shape of (39, 50, 33) so data is 64350 (39*33*50) x 10
# creating the component table with component as random floats,
# a "metric," and random classification
component = np.random.random((n_components))
metric = np.random.random((n_components))
classification = np.random.choice(["accepted", "rejected", "ignored"], n_components)
Expand All @@ -103,7 +106,8 @@ def test_smoke_write_split_ts():
assert me.write_split_ts(data, mmix, mask, comptable, ref_img) is not None

# TODO: midk_ts.nii is never generated?
for filename in ["hik_ts_.nii.gz", "lowk_ts_.nii.gz", "dn_ts_.nii.gz"]: # remove all files generated
for filename in ["hik_ts_.nii.gz", "lowk_ts_.nii.gz", "dn_ts_.nii.gz"]:
# remove all files generated
try:
os.remove(filename)
except OSError:
Expand All @@ -113,7 +117,8 @@ def test_smoke_write_split_ts():

def test_smoke_writefeats():
"""
Ensures that writefeats writes out the expected feature with random input, since there is no suffix, remove feats_.nii
Ensures that writefeats writes out the expected feature with random
input, since there is no suffix, remove feats_.nii
"""
n_samples, n_times, n_components = 64350, 10, 6
data = np.random.random((n_samples, n_times))
Expand All @@ -133,9 +138,10 @@ def test_smoke_writefeats():

def test_smoke_filewrite():
"""
Ensures that filewrite writes out a neuroimage with random input, since there is no name, remove the image named .nii
Ensures that filewrite writes out a neuroimage with random input,
since there is no name, remove the image named .nii
"""
n_samples, n_times, n_components = 64350, 10, 6
n_samples, n_times, _ = 64350, 10, 6
data_1d = np.random.random((n_samples))
data_2d = np.random.random((n_samples, n_times))
filename = ""
Expand Down
83 changes: 30 additions & 53 deletions tedana/tests/test_model_fit_dependence_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,80 +18,57 @@ def test_break_dependence_metrics():
tsoc = np.empty((n_samples, n_vols))
mmix = np.empty((n_vols, n_comps))
t2s = np.empty((n_samples, n_vols))
t2s_full = np.empty((n_samples, n_vols))
tes = np.empty((n_echos))
combmode = 't2s'
ref_img = ''

# Shape of catd is wrong
catd = np.empty((n_samples+1, n_echos, n_vols))
with pytest.raises(ValueError) as e_info:
catd = np.empty((n_samples + 1, n_echos, n_vols))
with pytest.raises(ValueError):
kundu_fit.dependence_metrics(catd=catd, tsoc=tsoc, mmix=mmix,
t2s=t2s, tes=tes, ref_img=ref_img,
reindex=False, mmixN=None, algorithm='kundu_v3')
assert str(e_info.value) == ('First dimensions (number of samples) of '
'catd ({0}), tsoc ({1}), '
'and t2s ({2}) do not match'.format(
catd.shape[0], tsoc.shape[0],
t2s.shape[0]))
t2s=t2s, tes=tes, ref_img=ref_img,
reindex=False, mmixN=None,
algorithm='kundu_v3')

# Shape of t2s is wrong
catd = np.empty((n_samples, n_echos, n_vols))
t2s = np.empty((n_samples+1, n_vols))
with pytest.raises(ValueError) as e_info:
t2s = np.empty((n_samples + 1, n_vols))
with pytest.raises(ValueError):
kundu_fit.dependence_metrics(catd=catd, tsoc=tsoc, mmix=mmix,
t2s=t2s, tes=tes, ref_img=ref_img,
reindex=False, mmixN=None, algorithm='kundu_v3')
assert str(e_info.value) == ('First dimensions (number of samples) of '
'catd ({0}), tsoc ({1}), '
'and t2s ({2}) do not match'.format(
catd.shape[0], tsoc.shape[0],
t2s.shape[0]))
t2s=t2s, tes=tes, ref_img=ref_img,
reindex=False, mmixN=None,
algorithm='kundu_v3')

# Shape of tsoc is wrong
t2s = np.empty((n_samples, n_vols))
tsoc = np.empty((n_samples+1, n_vols))
with pytest.raises(ValueError) as e_info:
tsoc = np.empty((n_samples + 1, n_vols))
with pytest.raises(ValueError):
kundu_fit.dependence_metrics(catd=catd, tsoc=tsoc, mmix=mmix,
t2s=t2s, tes=tes, ref_img=ref_img,
reindex=False, mmixN=None, algorithm='kundu_v3')
assert str(e_info.value) == ('First dimensions (number of samples) of '
'catd ({0}), tsoc ({1}), '
'and t2s ({2}) do not match'.format(
catd.shape[0], tsoc.shape[0],
t2s.shape[0]))
t2s=t2s, tes=tes, ref_img=ref_img,
reindex=False, mmixN=None,
algorithm='kundu_v3')

# Shape of catd is wrong
catd = np.empty((n_samples, n_echos+1, n_vols))
catd = np.empty((n_samples, n_echos + 1, n_vols))
tsoc = np.empty((n_samples, n_vols))
with pytest.raises(ValueError) as e_info:
with pytest.raises(ValueError):
kundu_fit.dependence_metrics(catd=catd, tsoc=tsoc, mmix=mmix,
t2s=t2s, tes=tes, ref_img=ref_img,
reindex=False, mmixN=None, algorithm='kundu_v3')
assert str(e_info.value) == ('Second dimension of catd ({0}) does not '
'match number '
'of echoes provided (tes; '
'{1})'.format(catd.shape[1], len(tes)))
t2s=t2s, tes=tes, ref_img=ref_img,
reindex=False, mmixN=None,
algorithm='kundu_v3')

# Shape of catd is wrong
catd = np.empty((n_samples, n_echos, n_vols+1))
with pytest.raises(ValueError) as e_info:
catd = np.empty((n_samples, n_echos, n_vols + 1))
with pytest.raises(ValueError):
kundu_fit.dependence_metrics(catd=catd, tsoc=tsoc, mmix=mmix,
t2s=t2s, tes=tes, ref_img=ref_img,
reindex=False, mmixN=None, algorithm='kundu_v3')
assert str(e_info.value) == ('Number of volumes in catd '
'({0}), tsoc ({1}), and '
'mmix ({2}) do not '
'match.'.format(catd.shape[2], tsoc.shape[1],
mmix.shape[0]))
t2s=t2s, tes=tes, ref_img=ref_img,
reindex=False, mmixN=None,
algorithm='kundu_v3')

# Shape of t2s is wrong
catd = np.empty((n_samples, n_echos, n_vols))
t2s = np.empty((n_samples, n_vols+1))
with pytest.raises(ValueError) as e_info:
t2s = np.empty((n_samples, n_vols + 1))
with pytest.raises(ValueError):
kundu_fit.dependence_metrics(catd=catd, tsoc=tsoc, mmix=mmix,
t2s=t2s, tes=tes, ref_img=ref_img,
reindex=False, mmixN=None, algorithm='kundu_v3')
assert str(e_info.value) == ('Number of volumes in catd ({0}) '
'does not match number of volumes in '
't2s ({1})'.format(catd.shape[2], t2s.shape[1]))
t2s=t2s, tes=tes, ref_img=ref_img,
reindex=False, mmixN=None,
algorithm='kundu_v3')
1 change: 0 additions & 1 deletion tedana/tests/test_model_kundu_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Tests for tedana.model.fit
"""

import pytest
import numpy as np
import pandas as pd

Expand Down
3 changes: 0 additions & 3 deletions tedana/tests/test_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
Tests for tedana.selection
"""

import os.path as op

import pytest
import numpy as np
import pandas as pd

Expand Down
Loading

0 comments on commit 8e565f6

Please sign in to comment.