annotate tests/test_datasets.py @ 89:800f3938ebaa

test our pet datasets
author Jeff Hammel <k0scist@gmail.com>
date Sun, 17 Dec 2017 14:10:53 -0800
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
89
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 """
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 test dataset generation
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 """
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 import math
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 import unittest
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 from tvii.dataset import circle
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 from tvii.dataset import line
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 class DatasetGenerationTest(unittest.TestCase):
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 """test our dataset generation"""
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 def test_random_circle(self):
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 """test generating points in a circle"""
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 # test variables
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 center = (2.5, 3.5)
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 radius = 10.
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 number = 100000
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 # generate points
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 c = circle.CircularRandom(center, radius)
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 points = c(number)
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 # validate
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 assert len(points) == number
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 offsets = [(point[0] - center[0], point[1] - center[1])
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 for point in points]
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 distances = [math.hypot(*offset) for offset in offsets]
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 assert max(distances) <= radius
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33 # TODO: ensure the distribution is fair (it's not)
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 def test_x_range(self):
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37 x = line.x_range(0., 10., 11)
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 for expected, actual in enumerate(x):
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 self.assertAlmostEqual(actual, float(expected))
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41 def test_line(self):
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43 # make a line
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44 l = line.Line(1., 1.)
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 self.assertAlmostEquals(l.y(1.), 2.)
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47 self.assertAlmostEquals(l(2.)[0], 3.)
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
50 if __name__ == '__main__':
800f3938ebaa test our pet datasets
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
51 unittest.main()