annotate globalneighbors/neighbors.py @ 25:991bce6b6881 default tip

[knn] placeholder for planning session
author Jeff Hammel <k0scist@gmail.com>
date Sun, 17 Sep 2017 14:35:50 -0700
parents 6891c5523b69
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 """
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2 read neighbors file;
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 this should be in the form of:
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4
22
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents: 20
diff changeset
5 `geoid [(geoid_closest_neighbor, distance), (geoid_2nd_closest_neighbor, distance), ...]`
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents: 20
diff changeset
6
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents: 20
diff changeset
7 *PER LINE* this format was chosen because it is easier to
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents: 20
diff changeset
8 iteratively read and write vs JSON.
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents: 20
diff changeset
9
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents: 20
diff changeset
10 While CSV could be made to fit this model, because
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents: 20
diff changeset
11 there are both distances and geo IDs as pairs, it is not
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents: 20
diff changeset
12 the most natural fit. So we'll settle for our own data model.
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents: 20
diff changeset
13 No, it's not the best, but so be it (for now).
20
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 """
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 import json
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 import os
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 string = (str, basestring) # python2
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 def read_neighbors_file(f):
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 if isinstance(f, string):
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 with open(f) as _f:
23
6891c5523b69 load with neighbors :)
Jeff Hammel <k0scist@gmail.com>
parents: 22
diff changeset
25 return read_neighbors_file(_f)
20
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 retval = {}
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 for line in f:
22
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents: 20
diff changeset
29 key, value = line.split(None, 1)
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents: 20
diff changeset
30 key = int(key)
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents: 20
diff changeset
31 data = json.loads(value)
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents: 20
diff changeset
32 data = [tuple(item) for item in data]
e69cb496324e we have a data dump
Jeff Hammel <k0scist@gmail.com>
parents: 20
diff changeset
33 retval[key] = data
20
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 return retval