-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #187 from berkeley-stat159/Tests
Merge tests into master branch
- Loading branch information
Showing
6 changed files
with
282 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
""" Tests for glm function in glm module | ||
This checks the glm function. | ||
Run at the tests directory with: | ||
nosetests code/utils/tests/test_glm.py | ||
""" | ||
# Loading modules. | ||
import numpy as np | ||
import numpy.linalg as npl | ||
import nibabel as nib | ||
import os | ||
import sys | ||
from numpy.testing import assert_almost_equal, assert_array_equal | ||
|
||
|
||
# Add path to functions to the system path. | ||
sys.path.append(os.path.join(os.path.dirname(__file__), "../functions/")) | ||
|
||
|
||
# Load our GLM functions. | ||
from glm import glm_beta, glm_mrss | ||
|
||
def test_glm_beta(): | ||
# Read in the image data. | ||
img = nib.load('data/ds114/sub009/BOLD/task002_run001/ds114_sub009_t2r1.nii') | ||
data = img.get_data() | ||
# Read in the convolutions. | ||
p = 2 | ||
convolved1 = np.loadtxt('data/ds114/sub009/behav/task002_run001/ds114_sub009_t2r1_conv.txt') | ||
# Create design matrix. | ||
X_matrix = np.ones((len(convolved1), p)) | ||
X_matrix[:, 1] = convolved1 | ||
|
||
# Calculate betas, copied from the exercise. | ||
data_2d = np.reshape(data, (-1, data.shape[-1])) | ||
B = npl.pinv(X_matrix).dot(data_2d.T) | ||
B_4d = np.reshape(B.T, img.shape[:-1] + (-1,)) | ||
|
||
# Run function. | ||
test_B_4d = glm_beta(data, X_matrix) | ||
assert_almost_equal(B_4d, test_B_4d) | ||
|
||
|
||
def test_glm_mrss(): | ||
img = nib.load('data/ds114/sub009/BOLD/task002_run001/ds114_sub009_t2r1.nii') | ||
data = img.get_data() | ||
convolved1 = np.loadtxt('data/ds114/sub009/behav/task002_run001/ds114_sub009_t2r1_conv.txt') | ||
X_matrix = np.ones((len(convolved1), 2)) | ||
X_matrix[:, 1] = convolved1 | ||
data_2d = np.reshape(data, (-1, data.shape[-1])) | ||
B = npl.pinv(X_matrix).dot(data_2d.T) | ||
B_4d = np.reshape(B.T, img.shape[:-1] + (-1,)) | ||
test_B_4d = glm_beta(data, X_matrix) | ||
|
||
# Pick a single voxel to check mrss functiom. | ||
# Calculate actual fitted values, residuals, and MRSS of voxel. | ||
fitted = X_matrix.dot(B_4d[12, 22, 10]) | ||
residuals = data[12, 22, 10] - fitted | ||
MRSS = np.sum(residuals**2)/(X_matrix.shape[0] - npl.matrix_rank(X_matrix)) | ||
|
||
# Calculate using glm_diagnostics function. | ||
test_MRSS, test_fitted, test_residuals = glm_mrss(test_B_4d, X_matrix, data) | ||
assert_almost_equal(MRSS, test_MRSS[12, 22, 10]) | ||
assert_almost_equal(fitted, test_fitted[12, 22, 10]) | ||
assert_almost_equal(residuals, test_residuals[12, 22, 10]) | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
""" Tests for smoothvoxels in smooth module | ||
Run at the tests directory with: | ||
nosetests code/utils/tests/test_smoothing.py | ||
""" | ||
|
||
import os | ||
import sys | ||
import numpy as np | ||
import itertools | ||
import scipy.ndimage | ||
from scipy.ndimage.filters import gaussian_filter | ||
import matplotlib.pyplot as plt | ||
import nibabel as nib | ||
from numpy.testing import assert_almost_equal | ||
from nose.tools import assert_not_equals | ||
|
||
|
||
# Add path to functions to the system path. | ||
sys.path.append(os.path.join(os.path.dirname(__file__), "../functions/")) | ||
|
||
# Load smoothing function. | ||
from smoothing import smoothing | ||
|
||
def test_smooth(): | ||
# Read in the image data. | ||
img = nib.load('data/ds114/sub009/BOLD/task002_run001/ds114_sub009_t2r1.nii') | ||
data = img.get_data() | ||
|
||
# Run the smoothing function with sigma 0 at time 12 | ||
non_smoothed_data = smoothing(data, 0, 12) | ||
|
||
# assert that data at time 12 and non_smoothed_data are equal since sigma = 0 | ||
assert_almost_equal(data[..., 12], non_smoothed_data) | ||
|
||
# Run the smoothvoxels function with sigma 5 at time 100 | ||
smoothed_data = smoothing(data, 5, 100) | ||
# assert that data at time 16 and smoothed_data are not equal | ||
assert_not_equals(data[..., 100].all(), smoothed_data.all()) |
Binary file not shown.
173 changes: 173 additions & 0 deletions
173
data/ds114/sub009/behav/task002_run001/ds114_sub009_t2r1_conv.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
0.000000000000000000e+00 | ||
0.000000000000000000e+00 | ||
0.000000000000000000e+00 | ||
0.000000000000000000e+00 | ||
0.000000000000000000e+00 | ||
2.321802309091016703e-01 | ||
8.321802309091016481e-01 | ||
1.141223007348432184e+00 | ||
1.134358594322657066e+00 | ||
1.035057581311021879e+00 | ||
9.611284719183640357e-01 | ||
9.262370763114995409e-01 | ||
9.135602883776547944e-01 | ||
9.097297884100494780e-01 | ||
9.087243611417219480e-01 | ||
9.084884912227670917e-01 | ||
9.084884912227670917e-01 | ||
6.763082603136654214e-01 | ||
7.630826031366537421e-02 | ||
-2.327345161256650918e-01 | ||
-2.258701030998900294e-01 | ||
-1.265690900882549264e-01 | ||
-5.263998069559708975e-02 | ||
-1.774858508873256718e-02 | ||
-5.071797154887739204e-03 | ||
-1.241297187282385493e-03 | ||
-2.358699189549879390e-04 | ||
0.000000000000000000e+00 | ||
0.000000000000000000e+00 | ||
2.321802309091016703e-01 | ||
8.321802309091016481e-01 | ||
1.141223007348432184e+00 | ||
1.134358594322657066e+00 | ||
1.035057581311021879e+00 | ||
9.611284719183640357e-01 | ||
9.262370763114995409e-01 | ||
9.135602883776547944e-01 | ||
9.097297884100494780e-01 | ||
9.087243611417219480e-01 | ||
9.084884912227670917e-01 | ||
9.084884912227670917e-01 | ||
6.763082603136654214e-01 | ||
7.630826031366537421e-02 | ||
-2.327345161256650918e-01 | ||
-2.258701030998900294e-01 | ||
-1.265690900882549264e-01 | ||
-5.263998069559708975e-02 | ||
-1.774858508873256718e-02 | ||
-5.071797154887739204e-03 | ||
-1.241297187282385493e-03 | ||
-2.358699189549879390e-04 | ||
0.000000000000000000e+00 | ||
0.000000000000000000e+00 | ||
2.321802309091016703e-01 | ||
8.321802309091016481e-01 | ||
1.141223007348432184e+00 | ||
1.134358594322657066e+00 | ||
1.035057581311021879e+00 | ||
9.611284719183640357e-01 | ||
9.262370763114995409e-01 | ||
9.135602883776547944e-01 | ||
9.097297884100494780e-01 | ||
9.087243611417219480e-01 | ||
9.084884912227670917e-01 | ||
9.084884912227670917e-01 | ||
6.763082603136654214e-01 | ||
7.630826031366537421e-02 | ||
-2.327345161256650918e-01 | ||
-2.258701030998900294e-01 | ||
-1.265690900882549264e-01 | ||
-5.263998069559708975e-02 | ||
-1.774858508873256718e-02 | ||
-5.071797154887739204e-03 | ||
-1.241297187282385493e-03 | ||
-2.358699189549879390e-04 | ||
0.000000000000000000e+00 | ||
0.000000000000000000e+00 | ||
2.321802309091016703e-01 | ||
8.321802309091016481e-01 | ||
1.141223007348432184e+00 | ||
1.134358594322657066e+00 | ||
1.035057581311021879e+00 | ||
9.611284719183640357e-01 | ||
9.262370763114995409e-01 | ||
9.135602883776547944e-01 | ||
9.097297884100494780e-01 | ||
9.087243611417219480e-01 | ||
9.084884912227670917e-01 | ||
9.084884912227670917e-01 | ||
6.763082603136654214e-01 | ||
7.630826031366537421e-02 | ||
-2.327345161256650918e-01 | ||
-2.258701030998900294e-01 | ||
-1.265690900882549264e-01 | ||
-5.263998069559708975e-02 | ||
-1.774858508873256718e-02 | ||
-5.071797154887739204e-03 | ||
-1.241297187282385493e-03 | ||
-2.358699189549879390e-04 | ||
0.000000000000000000e+00 | ||
0.000000000000000000e+00 | ||
2.321802309091016703e-01 | ||
8.321802309091016481e-01 | ||
1.141223007348432184e+00 | ||
1.134358594322657066e+00 | ||
1.035057581311021879e+00 | ||
9.611284719183640357e-01 | ||
9.262370763114995409e-01 | ||
9.135602883776547944e-01 | ||
9.097297884100494780e-01 | ||
9.087243611417219480e-01 | ||
9.084884912227670917e-01 | ||
9.084884912227670917e-01 | ||
6.763082603136654214e-01 | ||
7.630826031366537421e-02 | ||
-2.327345161256650918e-01 | ||
-2.258701030998900294e-01 | ||
-1.265690900882549264e-01 | ||
-5.263998069559708975e-02 | ||
-1.774858508873256718e-02 | ||
-5.071797154887739204e-03 | ||
-1.241297187282385493e-03 | ||
-2.358699189549879390e-04 | ||
0.000000000000000000e+00 | ||
0.000000000000000000e+00 | ||
2.321802309091016703e-01 | ||
8.321802309091016481e-01 | ||
1.141223007348432184e+00 | ||
1.134358594322657066e+00 | ||
1.035057581311021879e+00 | ||
9.611284719183640357e-01 | ||
9.262370763114995409e-01 | ||
9.135602883776547944e-01 | ||
9.097297884100494780e-01 | ||
9.087243611417219480e-01 | ||
9.084884912227670917e-01 | ||
9.084884912227670917e-01 | ||
6.763082603136654214e-01 | ||
7.630826031366537421e-02 | ||
-2.327345161256650918e-01 | ||
-2.258701030998900294e-01 | ||
-1.265690900882549264e-01 | ||
-5.263998069559708975e-02 | ||
-1.774858508873256718e-02 | ||
-5.071797154887739204e-03 | ||
-1.241297187282385493e-03 | ||
-2.358699189549879390e-04 | ||
0.000000000000000000e+00 | ||
0.000000000000000000e+00 | ||
2.321802309091016703e-01 | ||
8.321802309091016481e-01 | ||
1.141223007348432184e+00 | ||
1.134358594322657066e+00 | ||
1.035057581311021879e+00 | ||
9.611284719183640357e-01 | ||
9.262370763114995409e-01 | ||
9.135602883776547944e-01 | ||
9.097297884100494780e-01 | ||
9.087243611417219480e-01 | ||
9.084884912227670917e-01 | ||
9.084884912227670917e-01 | ||
6.763082603136654214e-01 | ||
7.630826031366537421e-02 | ||
-2.327345161256650918e-01 | ||
-2.258701030998900294e-01 | ||
-1.265690900882549264e-01 | ||
-5.263998069559708975e-02 | ||
-1.774858508873256718e-02 | ||
-5.071797154887739204e-03 | ||
-1.241297187282385493e-03 | ||
-2.358699189549879390e-04 | ||
0.000000000000000000e+00 | ||
0.000000000000000000e+00 |