-
Notifications
You must be signed in to change notification settings - Fork 9
/
test_awesome_more.py
62 lines (49 loc) · 2.28 KB
/
test_awesome_more.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
""" Some more tests for the awesome module
These tests don't work yet.
Put this file in the same directory as ``awesome.py``
Run the tests with::
nosetests test_awesome_more.py
"""
# Numpy is the array processing package
import numpy as np
# We will need some useful testing routines from numpy
from numpy.testing import assert_array_equal
# Nibabel is a package for loading neuroimaging files
import nibabel as nib
# Load the routine from awesome.py
import awesome
# This loads the module again in case you have edited it since you started
# Python. You don't need this command unless you are working interactively
reload(awesome)
# Declare global variables (variables in the module)
# Global variables should be ALL CAPS so we can see they are global and we don't
# get confused (like I did in class)
FNAME = 'bold.nii.gz'
IMG = nib.load(FNAME)
DATA = IMG.get_data()
def test_beginning_end_vols():
# Test that we can replace the first and last volumes
# This will not work with the current version of awesome - go fix!
# You should consult replace_vol docstring in awesome.py
# Check that we can fix the first volume in the series
# There is nothing before index 0, so let's ensure that when replacing
# index 0 we fill in with the data that is at index 1
start_fixed = awesome.replace_vol(DATA, 0)
assert_array_equal(start_fixed[:, :, :, 0], DATA[:, :, :, 1])
# Check that we can fix the last volume in the series
n_scans = DATA.shape[-1]
last_scan = n_scans - 1
end_fixed = awesome.replace_vol(DATA, last_scan)
# Again there is nothing after the last index , so let's ensure that when
# replacing the last volume, we fill in with the data that just before it
assert_array_equal(end_fixed[:, :, :, last_scan],
DATA[:, :, :, last_scan-1])
# Check that we can use -1 or -2 for the last or second to last scan
end_fixed = awesome.replace_vol(DATA, -1)
assert_array_equal(end_fixed[:, :, :, last_scan],
DATA[:, :, :, last_scan-1])
# If we're using -2, we want the mean of the 3rd to last and the last.
mean_31 = (DATA[:, :, :, -3] + DATA[:, :, :, -1]) / 2.
end_fixed = awesome.replace_vol(DATA, -2)
assert_array_equal(end_fixed[:, :, :, last_scan-1],
mean_31)