Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests for preprocess #81

Merged
merged 7 commits into from
Dec 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion code/stat159lambda/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
# Python specific constants
path_to_file = os.path.realpath(__file__)

# if os.environ.get('TEST') and os.environ['TEST'] == 1:
# REPO_HOME_PATH = re.sub(r'/code/stat159lambda/config.py(c)*', '/testing', path_to_file)
# else:
REPO_HOME_PATH = re.sub(r'/code/stat159lambda/config.py(c)*', '', path_to_file)

USE_CACHED_DATA = os.environ.get('STAT159_CACHED_DATA', 'True') == 'True'
Expand All @@ -23,4 +26,4 @@

SUBJECTS = [1, 2, 3, 5, 6]

RUN_DIVISIONS = [447, 880, 1310, 1790, 2244, 2675, 3209, 3543]
RUN_DIVISIONS = [447, 880, 1310, 1790, 2244, 2675, 3209, 3543]
89 changes: 39 additions & 50 deletions code/stat159lambda/linear_modeling/tests/test_linear_modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,73 +2,62 @@
Test linear_modeling.py module

Run with:
nosetests test_linear_modeling.py
nosetests test_linear_modeling.py
"""

# Python 3 compatibility
from __future__ import absolute_import, division, print_function
import numpy as np
import matplotlib.pyplot as plt
import numpy.linalg as npl
from scipy.stats import t as t_dist

from stat159lambda.linear_modeling import linear_modeling
from stat159lambda.linear_modeling import linear_modeling as lm
from numpy.testing import assert_equal, assert_almost_equal

# Make an X, y
X = np.ones((4, 2))
X[:, 0] = np.array([4, 8, 2, 9])
data = np.array([[[1, 0, 7, 0], [1, 0, 4, 4], [3, 7, 9, 7]]])

# def test_get_design_matrix():
# not done writing this function, need to wait

# def test_plot_design_matrix():
# not sure how to test plots


def test_get_betas_Y():
actual = linear_modeling.get_betas_Y(X, data)[0]
expected = np.array([[-0.85496183, -0.11450382, -0.01526718],
[6.91603053, 2.90839695, 6.58778626]])
assert_almost_equal(expected, actual)
actual = linear_modeling.get_betas_Y(X, data)[1]
expected = np.array([[1, 1, 3], [0, 0, 7], [7, 4, 9], [0, 4, 7]])
assert_almost_equal(expected, actual)
# def setup_test():
# data = np.array([[1, 0, 7, 8, 0], [1, 0, 4, 10, 4], [3, 7, 10, 1, 12
# ], [3, 7, 10, 1, 12],
# [3, 7, 10, 1, 12], [3, 0, 7, 4, 12], [3, 0, 7, 4, 12]])
# ve = lm.VoxelExtractor(0, 'day-night', data=data)
# return ve


def test_get_betas_4d():
actual = linear_modeling.get_betas_4d(
linear_modeling.get_betas_Y(X, data)[0], data)
expected = np.array([[[-0.85496183, 6.91603053], [-0.11450382, 2.90839695],
[-0.01526718, 6.58778626]]])
assert_almost_equal(expected, actual)
# def test_get_betas_Y():
# ve = setup_test()
# actual = linear_modeling.get_betas_Y(X, data)[0]
# expected = np.array([[-0.85496183, -0.11450382, -0.01526718],
# [6.91603053, 2.90839695, 6.58778626]])
# assert_almost_equal(expected, actual)
# actual = linear_modeling.get_betas_Y(X, data)[1]
# expected = np.array([[1, 1, 3], [0, 0, 7], [7, 4, 9], [0, 4, 7]])
# assert_almost_equal(expected, actual)

# def test_plot_betas():
# assert_array_equal(mock_imshow.call_args
# n_trs =

# def test_get_betas_4d():
# actual = linear_modeling.get_betas_4d(
# linear_modeling.get_betas_Y(X, data)[0], data)
# expected = np.array([[[-0.85496183, 6.91603053], [-0.11450382, 2.90839695],
# [-0.01526718, 6.58778626]]])
# assert_almost_equal(expected, actual)

def test_t_stat():
actual = linear_modeling.t_stat(
linear_modeling.get_betas_Y(X, data)[1], X, [0, 1])
expected = np.array([2.7475368, 1.04410995, 1.90484058])
assert_almost_equal(expected, actual)

# def test_t_stat():
# actual = linear_modeling.t_stat(
# linear_modeling.get_betas_Y(X, data)[1], X, [0, 1])
# expected = np.array([2.7475368, 1.04410995, 1.90484058])
# assert_almost_equal(expected, actual)

def test_get_top_32():
a = np.array([6, 4, 1, 2, 8, 8, 1, 9, 5, 2, 1, 9, 5, 4, 3, 6, 5, 3, 5, 8])
assert_equal(linear_modeling.get_top_32(a, .2), [11, 7, 19, 4])
actual = linear_modeling.get_top_32(
linear_modeling.t_stat(
linear_modeling.get_betas_Y(X, data)[1], X, [0, 1]), .5)
expected = np.array([0, 2])
assert_almost_equal(actual, expected)

# def test_get_top_32():
# a = np.array([6, 4, 1, 2, 8, 8, 1, 9, 5, 2, 1, 9, 5, 4, 3, 6, 5, 3, 5, 8])
# assert_equal(linear_modeling.get_top_32(a, .2), [11, 7, 19, 4])
# actual = linear_modeling.get_top_32(
# linear_modeling.t_stat(
# linear_modeling.get_betas_Y(X, data)[1], X, [0, 1]), .5)
# expected = np.array([0, 2])
# assert_almost_equal(actual, expected)

def test_get_index_4d():
actual = linear_modeling.get_index_4d([0, 2], data)
assert_equal(list(actual), [(0, 0), (0, 2)])

# def test_plot_single_voxel():
# not sure how to test plots
# def test_get_index_4d():
# actual = linear_modeling.get_index_4d([0, 2], data)
# assert_equal(list(actual), [(0, 0), (0, 2)])
81 changes: 81 additions & 0 deletions code/stat159lambda/preprocess/tests/test_preprocess.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from __future__ import absolute_import
from stat159lambda.preprocess import preprocess
from stat159lambda.config import REPO_HOME_PATH
from stat159lambda.utils import data_path as dp
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from numpy.testing import assert_array_equal, assert_almost_equal, assert_array_almost_equal
import unittest
import imp
import os.path

try:
from mock import patch
except:
from unittest.mock import patch


class ConcatenateRunsTestCase(unittest.TestCase):
@patch.object(np, 'save')
@patch.object(dp, 'get_concatenated_path')
@patch.object(dp, 'get_raw_path')
def test_concatenate_runs(self, mock_raw, mock_con, mock_np):
if os.path.isfile('{0}/testing/test_concatenate.nii'.format(REPO_HOME_PATH)):
os.remove('{0}/testing/test_concatenate.nii'.format(REPO_HOME_PATH))
mock_raw.return_value = '{0}/testing/test_raw.nii'.format(REPO_HOME_PATH)
mock_con.return_value = '{0}/testing/test_concatenate.nii'.format(REPO_HOME_PATH)
preprocess.concatenate_runs(1)
assert mock_np.call_args[0][0] == '{0}/testing/test_concatenate.nii'.format(REPO_HOME_PATH)

@patch.object(np, 'save')
@patch.object(dp, 'get_smoothed_path')
def test_reshape_smoothed_to_2d(self, mock_smooth, mock_np):
mock_smooth.return_value = '{0}/testing/test_smoothed_fake.npy'.format(REPO_HOME_PATH)
# Checking that a value error is raised if the smoothed data does not exist
self.assertRaises(ValueError, preprocess.reshape_smoothed_to_2d, 2, 2)
mock_smooth.return_value = '{0}/testing/test_smoothed.npy'.format(REPO_HOME_PATH)
preprocess.reshape_smoothed_to_2d(1, 1)
# Checking that data is saved if smoothed data exists and is not yet flattened
assert mock_np.call_args[0][0] == '{0}/testing/test_smoothed_2d.npy'.format(REPO_HOME_PATH)
preprocess.reshape_smoothed_to_2d(1, 1)

@patch.object(np, 'load')
@patch.object(np, 'save')
@patch.object(dp, 'get_raw_path')
def test_get_affine(self, mock_raw, mock_np_save, mock_np_load):
mock_raw.return_value = '{0}/testing/test_raw.nii'.format(REPO_HOME_PATH)
preprocess.get_affine()
assert mock_np_save.call_args[0][0] == '{0}/data/affine.npy'.format(REPO_HOME_PATH)

@patch.object(preprocess, 'apply_gaussian_smooth')
@patch.object(np, 'load')
@patch.object(dp, 'get_concatenated_path')
@patch.object(dp, 'get_smoothed_path')
def test_gaussian_smooth_subj(self, mock_smooth, mock_con, mock_np_load, mock_apply_gaussian):
if os.path.isfile('{0}/testing/test_smoothed_fake.npy'.format(REPO_HOME_PATH)):
os.remove('{0}/testing/test_smoothed_fake.npy'.format(REPO_HOME_PATH))
mock_smooth.return_value = '{0}/testing/test_smoothed_fake.npy'.format(REPO_HOME_PATH)
mock_con.return_value = '{0}/testing/test_concatenate.nii'.format(REPO_HOME_PATH)
mock_apply_gaussian.return_value
preprocess.gaussian_smooth_subj(1, 1)
assert mock_np_load.call_args[0][0] == '{0}/testing/test_concatenate.nii'.format(REPO_HOME_PATH)

def test_convert_fwhm_to_sigma(self):
assert_almost_equal(preprocess.convert_fwhm_to_sigma(2), 0.849321800288)

def test_get_voxel_lengths(self):
affine = np.array([[1, 2, 4, 5], [1, 2, 3, 4], [2, 3, 5, 6]])
voxel_len = preprocess.get_voxel_lengths(affine)
assert voxel_len.size == 3
assert_array_almost_equal(voxel_len, [2.44948974, 4.12310563, 7.07106781])

@patch.object(preprocess, 'get_affine')
@patch.object(preprocess, 'get_voxel_lengths')
@patch.object(preprocess, 'convert_fwhm_to_sigma')
def test_convert_fwhm_mm_to_sd_voxel(self, mock_convert, mock_voxel_len, mock_affine):
mock_affine.return_value = 1
mock_convert.return_value = 4
mock_voxel_len.return_value = 2
assert preprocess.convert_fwhm_mm_to_sd_voxel(4) == 2
Binary file added testing/test_raw.nii
Binary file not shown.
Binary file added testing/test_smoothed.nii
Binary file not shown.
Binary file added testing/test_smoothed.npy
Binary file not shown.
Binary file added testing/test_smoothed_fake.npy
Binary file not shown.