annotate tests/test_write.py @ 22:e69cb496324e

we have a data dump
author Jeff Hammel <k0scist@gmail.com>
date Sun, 25 Jun 2017 17:45:19 -0700
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
22
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 """
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 test writing + reading distances
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 """
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 import os
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 import shutil
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 import tempfile
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 import unittest
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 from common import datafile
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 from globalneighbors.distance import calculate_neighbors
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 from globalneighbors.distance import write_neighbors
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 from globalneighbors.locations import locations
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 from globalneighbors.neighbors import read_neighbors_file
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 from globalneighbors.read import read_cities
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 class TestDistanceReadWrite(unittest.TestCase):
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 def test_10000(self):
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 """test 10000 cities"""
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 # read locations
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 citiesfile = datafile('10000cities.tsv')
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 assert os.path.exists(citiesfile)
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 with open(citiesfile) as f:
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 city_locations = locations(read_cities(f))
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 # calculate neighbors
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 neighbors = calculate_neighbors(city_locations,
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 k=50,
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33 lat_tol=2.,
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 lon_tol=2.)
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 # make a staging area
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37 tmpdir = tempfile.mkdtemp()
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 try:
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 # write the neighbors
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40 outfile = os.path.join(tmpdir, 'neighbors.dat')
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41 assert not os.path.exists(outfile)
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42 with open(outfile, 'w') as f:
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43 write_neighbors(f, neighbors)
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44 assert os.path.exists(outfile)
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 # read the neighbors
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47 with open(outfile) as f:
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48 new_neighbors = read_neighbors_file(f)
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49 finally:
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
50 shutil.rmtree(tmpdir, ignore_errors=True)
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
51
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
52 # they should be equal
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
53 assert len(neighbors) == len(new_neighbors)
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
54 assert sorted(neighbors.keys()) == sorted(new_neighbors.keys())
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
55 for key in neighbors.keys():
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
56 valueA = neighbors[key]
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
57 valueB = new_neighbors[key]
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
58 assert valueA == valueB
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
59
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
60
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
61 if __name__ == '__main__':
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
62 unittest.main()