-
Notifications
You must be signed in to change notification settings - Fork 0
/
convolve_test.py
65 lines (50 loc) · 2.31 KB
/
convolve_test.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
63
64
65
#!/usr/bin/env python
import numpy as np
import unittest
import cnv
import Image
import tempfile
from os.path import join
from shutil import rmtree
class TestEmbed(unittest.TestCase):
def assertArraysSimilar(self, tolerance, A, B):
self.assertTrue(tolerance >= abs(A - B).max())
def test_embed(self):
'''embed'''
A = np.arange(21).reshape((3,7))
expected = np.array([[ 0., 0., 1., 2., 3., 4., 5., 6., 6., 6.],
[ 7., 7., 8., 9., 10., 11., 12., 13., 13., 13.],
[ 14., 14., 15., 16., 17., 18., 19., 20., 20., 20.]])
self.assertTrue((cnv.embed(A, 3) == expected).all())
def test_convolve_f1d(self):
'''convolve fast axis'''
A = np.arange(21).reshape((3,7))
expected = np.array([[ 2., 5., 8., 11., 14., 17., 18.],
[ 23., 26., 29., 32., 35., 38., 39.],
[ 44., 47., 50., 53., 56., 59., 60.]])
self.assertTrue((cnv.convolve_f1d(A, np.arange(3)) == expected).all())
def test_convolve_png(self):
'''convolve phoenix head, x-axis'''
expected = cnv.png_to_ndarray('data/head-dx.png')
A = cnv.convolve_png('data/head-16.png', cnv.first_d)
self.assertArraysSimilar(1, cnv.igreyscale(A, max=255), expected)
def test_part1a(self):
'''part1a on the head'''
tempdir = tempfile.mkdtemp(prefix='scivis')
saveas=join(tempdir, 'part1a/')
cnv.part1a(filename='data/head-16.png', saveas=saveas, aspect=(1,1))
for op in ['dx', 'dy', 'gm']:
self.assertArraysSimilar(1,
cnv.png_to_ndarray('data/head-%s.png' % op),
cnv.igreyscale(cnv.png_to_ndarray('%s/%s.png' % (saveas, op)), max=255))
rmtree(tempdir)
def test_grid(self):
'''grid'''
expected = [np.array([[0, 1], [3, 4]]), np.array([[1, 2], [4, 5]]),
np.array([[3, 4], [6, 7]]), np.array([[4, 5], [7, 8]])]
for g, e in zip(cnv.grid(np.arange(9).reshape((3,3))), expected):
self.assertArraysSimilar(0, g, e)
if __name__ == '__main__':
tests = [ unittest.TestLoader().loadTestsFromTestCase(v)
for k,v in globals().items() if k.startswith("Test")]
unittest.TextTestRunner(verbosity=2).run(unittest.TestSuite(tests))