view globalneighbors/neighbors.py @ 23:6891c5523b69

load with neighbors :)
author Jeff Hammel <k0scist@gmail.com>
date Sun, 25 Jun 2017 18:13:43 -0700
parents e69cb496324e
children
line wrap: on
line source

"""
read neighbors file;
this should be in the form of:

`geoid [(geoid_closest_neighbor, distance), (geoid_2nd_closest_neighbor, distance), ...]`

*PER LINE*  this format was chosen because it is easier to
iteratively read and write vs JSON.

While CSV could be made to fit this model, because
there are both distances and geo IDs as pairs, it is not
the most natural fit.  So we'll settle for our own data model.
No, it's not the best, but so be it (for now).
"""

import json
import os

string = (str, basestring)  # python2

def read_neighbors_file(f):

    if isinstance(f, string):
        with open(f) as _f:
            return read_neighbors_file(_f)

    retval = {}
    for line in f:
        key, value = line.split(None, 1)
        key = int(key)
        data = json.loads(value)
        data = [tuple(item) for item in data]
        retval[key] = data
    return retval