CartoDB / crankshaft

CARTO Spatial Analysis extension for PostgreSQL
BSD 3-Clause "New" or "Revised" License
54 stars 20 forks source link

consolidate 'get_attribute'-type methods into a single submodule #166

Open andy-esch opened 7 years ago

andy-esch commented 7 years ago

There's a decent amount of duplicate code to unpack database responses that should be generalized to make it easier to maintain and debug the functions. These functions should lie within a new file plpy_utils.py.

Given a plpy.execute response, formatted as:

resp = [{'attr1': ..., 'attr2': ..., 'id_col': 'cartodb_id'},
        {'attr1': ..., 'attr2': ..., 'id_col': 'cartodb_id'},
        ...]

Gets unpacked by a function like this:

def get_attributes(resp, attr_max, attr_min=1):
    return np.array([[x['attr{}'.format(str(i))] for x in resp]
                     for i in range(attr_min, attr_max+1)], dtype=float).transpose()

This is done in markov, non-spatial k-means, max-p, moran, GWR, spint...