comparison tests/test_distance.py @ 0:5dba84370182

initial commit; half-working prototype
author Jeff Hammel <k0scist@gmail.com>
date Sat, 24 Jun 2017 12:03:39 -0700
parents
children 1b94f3bf97e5
comparison
equal deleted inserted replaced
-1:000000000000 0:5dba84370182
1 #!/usr/bin/env python
2
3 """
4 test distance calculation
5 """
6
7 import math
8 import os
9 import unittest
10 from globalneighbors import distance
11 from globalneighbors.constants import Rearth
12 from globalneighbors.locations import locations
13 from globalneighbors.read import read_city_list
14 from globalneighbors.schema import primary_key
15
16 here = os.path.dirname(os.path.abspath(__file__))
17 data = os.path.join(here, 'data')
18 full_tsv_lines = 149092
19
20 class DistanceTests(unittest.TestCase):
21
22 # created with
23 # head -n 10 cities1000.txt > GlobalNeighbors/tests/data/sample.tsv
24 test_tsv = os.path.join(data, 'sample.tsv')
25 test_tsv_lines = 10
26
27 # full dataset: test with caution
28 full_tsv = os.path.join(data, 'cities1000.txt')
29 full_tsv_lines = 149092
30
31 def test_haversine(self):
32
33 # a simple canned case
34 # equator to pole
35 lat1 = 0.
36 lat2 = 90.
37 lon2 = 70. # undefined, technically
38 expected_distance = 0.5*math.pi
39
40 for lon1 in range(-135, 135, 15):
41 radians = [distance.deg_to_rad(degrees)
42 for degrees in (lat1, lon2, lat2, lon2)]
43 error = (distance.haversine(*radians) == expected_distance)
44 assert error < 1e-4
45
46 def test_distance(self):
47 """test distance between two known cities"""
48
49 # Source:https://en.wikipedia.org/wiki/List_of_cities_by_latitude
50 # http://www.distancefromto.net/distance-from-new-york-to-chicago-us
51 chicago = (40.71278,
52 -74.00594)
53
54 new_york = (41.85003,
55 -87.65005)
56 ref_distance = 1149.
57
58 args = [distance.deg_to_rad(i) for i in
59 list(chicago) + list(new_york)]
60 calculated = distance.haversine(*args, r=Rearth)
61
62 # Allow some error for circular projection approximation
63 error = abs(calculated - ref_distance)/ref_distance
64
65 assert error < 0.01
66
67 def test_distances(self):
68 """"ensure disances monotonically decay"""
69
70 # parse the data
71 assert os.path.exists(self.test_tsv)
72 cities = read_city_list(self.test_tsv)
73 assert len(cities) == self.test_tsv_lines
74 city_locations = locations(cities)
75 assert len(city_locations) == self.test_tsv_lines
76
77 # calculate all the neighbors
78 # WARNING: n*2 algorithm Too computationally intensive
79 # for full data set
80 for key, value in distance.calculate_distances(city_locations,
81 r=Rearth):
82 # for now, just make sure we can iterate over them
83 pass
84
85 def test_neighbors(self):
86
87 # parse the data
88 tsv = os.path.join(data, 'sample.tsv')
89 assert os.path.exists(tsv)
90 cities = read_city_list(tsv)
91 city_locations = locations(cities)
92
93 # calculate the neighbors
94 neighbors = distance.calculate_neighbors(city_locations,
95 k=self.test_tsv_lines)
96 assert len(neighbors) == self.test_tsv_lines
97
98 # ensure distance increases for each thing
99 for src, value in neighbors.items():
100 distances = [i[-1] for i in value]
101 assert len(distances) == self.test_tsv_lines - 1
102 for i in range(1, len(distances)):
103 assert distances[i] >= distances[i-1]
104
105 if __name__ == '__main__':
106 unittest.main()