refactored from pylint

This commit is contained in:
Andy Eschbacher 2016-03-29 22:49:31 -07:00
parent 06f5cf9951
commit 8dd8ab37a5
2 changed files with 112 additions and 116 deletions

View File

@ -11,10 +11,12 @@ import plpy
# High level interface --------------------------------------- # High level interface ---------------------------------------
def moran(subquery, attr_name, permutations, geom_col, id_col, w_type, num_ngbrs): def moran(subquery, attr_name,
permutations, geom_col, id_col, w_type, num_ngbrs):
""" """
Moran's I (global) Moran's I (global)
Implementation building neighors with a PostGIS database and Moran's I core clusters with PySAL. Implementation building neighbors with a PostGIS database and Moran's I
core clusters with PySAL.
Andy Eschbacher Andy Eschbacher
""" """
qvals = {"id_col": id_col, qvals = {"id_col": id_col,
@ -23,48 +25,39 @@ def moran(subquery, attr_name, permutations, geom_col, id_col, w_type, num_ngbrs
"subquery": subquery, "subquery": subquery,
"num_ngbrs": num_ngbrs} "num_ngbrs": num_ngbrs}
q = get_query(w_type, qvals) query = construct_neighbor_query(w_type, qvals)
plpy.notice('** Query: %s' % q) plpy.notice('** Query: %s' % query)
try: try:
r = plpy.execute(q) result = plpy.execute(query)
if (len(r) == 0) & (w_type != 'knn'): ## if there are no neighbors, exit
plpy.notice('** Query returned with 0 rows, trying kNN weights') if len(result) == 0:
q = get_query('knn', qvals) return zip([None], [None])
r = plpy.execute(q) plpy.notice('** Query returned with %d rows' % len(result))
plpy.notice('** Query returned with %d rows' % len(r))
except plpy.SPIError: except plpy.SPIError:
plpy.error('Error: areas of interest query failed, check input parameters') plpy.error('Error: areas of interest query failed, check input parameters')
plpy.notice('** Query failed: "%s"' % q) plpy.notice('** Query failed: "%s"' % query)
plpy.notice('** Error: %s' % plpy.SPIError) plpy.notice('** Error: %s' % plpy.SPIError)
plpy.notice('** Exiting function')
return zip([None], [None])
## if there are no neighbors, exit
if len(r) == 0:
return zip([None], [None]) return zip([None], [None])
## collect attributes ## collect attributes
attr_vals = get_attributes(r, 1) attr_vals = get_attributes(result)
## calculate weights ## calculate weights
weight = get_weight(r, w_type, num_ngbrs) weight = get_weight(result, w_type, num_ngbrs)
## calculate moran global ## calculate moran global
moran_global = ps.esda.moran.Moran(attr_vals, weight, permutations=permutations) moran_global = ps.esda.moran.Moran(attr_vals, weight, permutations=permutations)
return zip([moran_global.I],[moran_global.EI]) return zip([moran_global.I], [moran_global.EI])
def moran_local(subquery, attr, permutations, geom_col, id_col, w_type, num_ngbrs): def moran_local(subquery, attr,
permutations, geom_col, id_col, w_type, num_ngbrs):
""" """
Moran's I implementation for PL/Python Moran's I implementation for PL/Python
Andy Eschbacher Andy Eschbacher
""" """
# TODO: ensure that the significance output can be smaller that 1e-3 (0.001)
# TODO: make a wishlist of output features (zscores, pvalues, raw local lisa, what else?)
plpy.notice('** Constructing query')
# geometries with attributes that are null are ignored # geometries with attributes that are null are ignored
# resulting in a collection of not as near neighbors # resulting in a collection of not as near neighbors
@ -75,30 +68,32 @@ def moran_local(subquery, attr, permutations, geom_col, id_col, w_type, num_ngbr
"subquery": subquery, "subquery": subquery,
"num_ngbrs": num_ngbrs} "num_ngbrs": num_ngbrs}
q = get_query(w_type, qvals) query = construct_neighbor_query(w_type, qvals)
try: try:
r = plpy.execute(q) result = plpy.execute(query)
plpy.notice('** Query returned with %d rows' % len(r)) if len(result) == 0:
return zip([None], [None], [None], [None], [None])
except plpy.SPIError: except plpy.SPIError:
plpy.error('Error: areas of interest query failed, check input parameters') plpy.error('Error: areas of interest query failed, check input parameters')
plpy.notice('** Query failed: "%s"' % q) plpy.notice('** Query failed: "%s"' % query)
plpy.notice('** Exiting function')
return zip([None], [None], [None], [None], [None]) return zip([None], [None], [None], [None], [None])
y = get_attributes(r, 1) attr_vals = get_attributes(result)
w = get_weight(r, w_type) weight = get_weight(result, w_type)
# calculate LISA values # calculate LISA values
lisa = ps.esda.moran.Moran_Local(y, w) lisa = ps.esda.moran.Moran_Local(attr_vals, weight,
permutations=permutations)
# find quadrants for each geometry # find quadrants for each geometry
quads = quad_position(lisa.q) quads = quad_position(lisa.q)
plpy.notice('** Finished calculations') plpy.notice('** Finished calculations')
return zip(lisa.Is, quads, lisa.p_sim, w.id_order, lisa.y) return zip(lisa.Is, quads, lisa.p_sim, weight.id_order, lisa.y)
def moran_rate(subquery, numerator, denominator, permutations, geom_col, id_col, w_type, num_ngbrs): def moran_rate(subquery, numerator, denominator,
permutations, geom_col, id_col, w_type, num_ngbrs):
""" """
Moran's I Rate (global) Moran's I Rate (global)
Andy Eschbacher Andy Eschbacher
@ -110,88 +105,82 @@ def moran_rate(subquery, numerator, denominator, permutations, geom_col, id_col,
"subquery": subquery, "subquery": subquery,
"num_ngbrs": num_ngbrs} "num_ngbrs": num_ngbrs}
q = get_query(w_type, qvals) query = construct_neighbor_query(w_type, qvals)
plpy.notice('** Query: %s' % q) plpy.notice('** Query: %s' % query)
try: try:
r = plpy.execute(q) result = plpy.execute(query)
if len(r) == 0: if len(result) == 0:
plpy.notice('** Query returned with 0 rows, trying kNN weights') ## if there are no values returned, exit
q = get_query('knn', qvals) return zip([None], [None])
r = plpy.execute(q) plpy.notice('** Query returned with %d rows' % len(result))
plpy.notice('** Query returned with %d rows' % len(r))
except plpy.SPIError: except plpy.SPIError:
plpy.error('Error: areas of interest query failed, check input parameters') plpy.error('Error: areas of interest query failed, check input parameters')
plpy.notice('** Query failed: "%s"' % q) plpy.notice('** Query failed: "%s"' % query)
plpy.notice('** Error: %s' % plpy.SPIError) plpy.notice('** Error: %s' % plpy.SPIError)
plpy.notice('** Exiting function')
return zip([None], [None])
## if there are no values returned, exit
if len(r) == 0:
return zip([None], [None]) return zip([None], [None])
## collect attributes ## collect attributes
numer = get_attributes(r, 1) numer = get_attributes(result, 1)
denom = get_attributes(r, 2) denom = get_attributes(result, 2)
w = get_weight(r, w_type, num_ngbrs) weight = get_weight(result, w_type, num_ngbrs)
## calculate moran global rate ## calculate moran global rate
mr = ps.esda.moran.Moran_Rate(numer, denom, w, permutations=permutations) lisa_rate = ps.esda.moran.Moran_Rate(numer, denom, weight,
permutations=permutations)
plpy.notice('** Finished calculations') return zip([lisa_rate.I], [lisa_rate.EI])
return zip([mr.I],[mr.EI]) def moran_local_rate(subquery, numerator, denominator,
permutations, geom_col, id_col, w_type, num_ngbrs):
def moran_local_rate(subquery, numerator, denominator, permutations, geom_col, id_col, w_type, num_ngbrs):
""" """
Moran's I Local Rate Moran's I Local Rate
Andy Eschbacher Andy Eschbacher
""" """
# geometries with values that are null are ignored
plpy.notice('** Constructing query')
# geometries with attributes that are null are ignored
# resulting in a collection of not as near neighbors # resulting in a collection of not as near neighbors
qvals = {"id_col": id_col, query = construct_neighbor_query(w_type,
{"id_col": id_col,
"numerator": numerator, "numerator": numerator,
"denominator": denominator, "denominator": denominator,
"geom_col": geom_col, "geom_col": geom_col,
"subquery": subquery, "subquery": subquery,
"num_ngbrs": num_ngbrs} "num_ngbrs": num_ngbrs})
q = get_query(w_type, qvals)
try: try:
r = plpy.execute(q) result = plpy.execute(query)
plpy.notice('** Query returned with %d rows' % len(r)) plpy.notice('** Query returned with %d rows' % len(result))
if len(result) == 0:
return zip([None], [None], [None], [None], [None])
except plpy.SPIError: except plpy.SPIError:
plpy.error('Error: areas of interest query failed, check input parameters') plpy.error('Error: areas of interest query failed, check input parameters')
plpy.notice('** Query failed: "%s"' % q) plpy.notice('** Query failed: "%s"' % query)
plpy.notice('** Error: %s' % plpy.SPIError) plpy.notice('** Error: %s' % plpy.SPIError)
plpy.notice('** Exiting function')
return zip([None], [None], [None], [None], [None]) return zip([None], [None], [None], [None], [None])
## collect attributes ## collect attributes
numer = get_attributes(r, 1) numer = get_attributes(result, 1)
denom = get_attributes(r, 2) denom = get_attributes(result, 2)
w = get_weight(r, w_type, num_ngbrs) weight = get_weight(result, w_type, num_ngbrs)
# calculate LISA values # calculate LISA values
lisa = ps.esda.moran.Moran_Local_Rate(numer, denom, w, permutations=permutations) lisa = ps.esda.moran.Moran_Local_Rate(numer, denom, weight,
permutations=permutations)
# find units of significance # find units of significance
quads = quad_position(lisa.q) quads = quad_position(lisa.q)
plpy.notice('** Finished calculations') return zip(lisa.Is, quads, lisa.p_sim, weight.id_order, lisa.y)
return zip(lisa.Is, quads, lisa.p_sim, w.id_order, lisa.y) def moran_local_bv(subquery, attr1, attr2,
permutations, geom_col, id_col, w_type, num_ngbrs):
def moran_local_bv(subquery, attr1, attr2, permutations, geom_col, id_col, w_type, num_ngbrs): """
Moran's I (local) Bivariate (untested)
"""
plpy.notice('** Constructing query') plpy.notice('** Constructing query')
qvals = {"num_ngbrs": num_ngbrs, qvals = {"num_ngbrs": num_ngbrs,
@ -201,27 +190,28 @@ def moran_local_bv(subquery, attr1, attr2, permutations, geom_col, id_col, w_typ
"geom_col": geom_col, "geom_col": geom_col,
"id_col": id_col} "id_col": id_col}
q = get_query(w_type, qvals) query = construct_neighbor_query(w_type, qvals)
try: try:
r = plpy.execute(q) result = plpy.execute(query)
plpy.notice('** Query returned with %d rows' % len(r)) plpy.notice('** Query returned with %d rows' % len(result))
if len(result) == 0:
return zip([None], [None], [None], [None])
except plpy.SPIError: except plpy.SPIError:
plpy.error('Error: areas of interest query failed, check input parameters') plpy.error('Error: areas of interest query failed, check input parameters')
plpy.notice('** Query failed: "%s"' % q) plpy.notice('** Query failed: "%s"' % query)
plpy.notice('** Error: %s' % plpy.SPIError)
plpy.notice('** Exiting function')
return zip([None], [None], [None], [None]) return zip([None], [None], [None], [None])
## collect attributes ## collect attributes
attr1_vals = get_attributes(r, 1) attr1_vals = get_attributes(result, 1)
attr2_vals = get_attributes(r, 2) attr2_vals = get_attributes(result, 2)
# create weights # create weights
w = get_weight(r, w_type, num_ngbrs) weight = get_weight(result, w_type, num_ngbrs)
# calculate LISA values # calculate LISA values
lisa = ps.esda.moran.Moran_Local_BV(attr1_vals, attr2_vals, w) lisa = ps.esda.moran.Moran_Local_BV(attr1_vals, attr2_vals, weight,
permutations=permutations)
plpy.notice("len of Is: %d" % len(lisa.Is)) plpy.notice("len of Is: %d" % len(lisa.Is))
@ -230,7 +220,7 @@ def moran_local_bv(subquery, attr1, attr2, permutations, geom_col, id_col, w_typ
plpy.notice('** Finished calculations') plpy.notice('** Finished calculations')
return zip(lisa.Is, lisa_sig, lisa.p_sim, w.id_order) return zip(lisa.Is, lisa_sig, lisa.p_sim, weight.id_order)
# Low level functions ---------------------------------------- # Low level functions ----------------------------------------
@ -240,7 +230,7 @@ def map_quads(coord):
Map a quadrant number to Moran's I designation Map a quadrant number to Moran's I designation
HH=1, LH=2, LL=3, HL=4 HH=1, LH=2, LL=3, HL=4
Input: Input:
:param coord (int): quadrant of a specific measurement @param coord (int): quadrant of a specific measurement
""" """
if coord == 1: if coord == 1:
return 'HH' return 'HH'
@ -256,7 +246,7 @@ def map_quads(coord):
def query_attr_select(params): def query_attr_select(params):
""" """
Create portion of SELECT statement for attributes inolved in query. Create portion of SELECT statement for attributes inolved in query.
:param params: dict of information used in query (column names, @param params: dict of information used in query (column names,
table name, etc.) table name, etc.)
""" """
@ -293,7 +283,7 @@ def query_attr_where(params):
def knn(params): def knn(params):
"""SQL query for k-nearest neighbors. """SQL query for k-nearest neighbors.
:param vars: dict of values to fill template @param vars: dict of values to fill template
""" """
attr_select = query_attr_select(params) attr_select = query_attr_select(params)
@ -322,7 +312,7 @@ def knn(params):
## SQL query for finding queens neighbors (all contiguous polygons) ## SQL query for finding queens neighbors (all contiguous polygons)
def queen(params): def queen(params):
"""SQL query for queen neighbors. """SQL query for queen neighbors.
:param params: dict of information to fill query @param params dict: information to fill query
""" """
attr_select = query_attr_select(params) attr_select = query_attr_select(params)
attr_where = query_attr_where(params) attr_where = query_attr_where(params)
@ -348,10 +338,10 @@ def queen(params):
## to add more weight methods open a ticket or pull request ## to add more weight methods open a ticket or pull request
def get_query(w_type, query_vals): def construct_neighbor_query(w_type, query_vals):
"""Return requested query. """Return requested query.
:param w_type: type of neighbors to calculate (knn or queen) @param w_type text: type of neighbors to calculate ('knn' or 'queen')
:param query_vals: values used to construct the query @param query_vals dict: values used to construct the query
""" """
if w_type == 'knn': if w_type == 'knn':
@ -359,10 +349,10 @@ def get_query(w_type, query_vals):
else: else:
return queen(query_vals) return queen(query_vals)
def get_attributes(query_res, attr_num): def get_attributes(query_res, attr_num=1):
""" """
:param query_res: query results with attributes and neighbors @param query_res: query results with attributes and neighbors
:param attr_num: attribute number (1, 2, ...) @param attr_num: attribute number (1, 2, ...)
""" """
return np.array([x['attr' + str(attr_num)] for x in query_res], dtype=np.float) return np.array([x['attr' + str(attr_num)] for x in query_res], dtype=np.float)
@ -370,7 +360,7 @@ def get_attributes(query_res, attr_num):
def get_weight(query_res, w_type='queen', num_ngbrs=5): def get_weight(query_res, w_type='queen', num_ngbrs=5):
""" """
Construct PySAL weight from return value of query Construct PySAL weight from return value of query
:param query_res: query results with attributes and neighbors @param query_res: query results with attributes and neighbors
""" """
if w_type == 'knn': if w_type == 'knn':
row_normed_weights = [1.0 / float(num_ngbrs)] * num_ngbrs row_normed_weights = [1.0 / float(num_ngbrs)] * num_ngbrs
@ -387,6 +377,11 @@ def get_weight(query_res, w_type='queen', num_ngbrs=5):
def quad_position(quads): def quad_position(quads):
""" """
Produce Moran's I classification based of n Produce Moran's I classification based of n
Input:
@param quads ndarray: an array of quads classified by
1-4 (PySAL default)
Output:
@param ndarray: an array of quads classied by 'HH', 'LL', etc.
""" """
lisa_sig = np.array([map_quads(q) for q in quads]) lisa_sig = np.array([map_quads(q) for q in quads])

View File

@ -56,7 +56,7 @@ class MoranTest(unittest.TestCase):
self.assertEqual(cc.query_attr_where(self.params), ans) self.assertEqual(cc.query_attr_where(self.params), ans)
def test_knn(self): def test_knn(self):
"""Test knn function.""" """Test knn neighbors constructor"""
ans = "SELECT i.\"cartodb_id\" As id, i.\"andy\"::numeric As attr1, " \ ans = "SELECT i.\"cartodb_id\" As id, i.\"andy\"::numeric As attr1, " \
"i.\"jay_z\"::numeric As attr2, (SELECT ARRAY(SELECT j.\"cartodb_id\" " \ "i.\"jay_z\"::numeric As attr2, (SELECT ARRAY(SELECT j.\"cartodb_id\" " \
@ -70,7 +70,7 @@ class MoranTest(unittest.TestCase):
self.assertEqual(cc.knn(self.params), ans) self.assertEqual(cc.knn(self.params), ans)
def test_queen(self): def test_queen(self):
"""Test queen neighbors function.""" """Test queen neighbors constructor"""
ans = "SELECT i.\"cartodb_id\" As id, i.\"andy\"::numeric As attr1, " \ ans = "SELECT i.\"cartodb_id\" As id, i.\"andy\"::numeric As attr1, " \
"i.\"jay_z\"::numeric As attr2, (SELECT ARRAY(SELECT " \ "i.\"jay_z\"::numeric As attr2, (SELECT ARRAY(SELECT " \
@ -83,19 +83,20 @@ class MoranTest(unittest.TestCase):
self.assertEqual(cc.queen(self.params), ans) self.assertEqual(cc.queen(self.params), ans)
def test_get_query(self): def test_construct_neighbor_query(self):
"""Test get_query.""" """Test construct_neighbor_query"""
ans = "SELECT i.\"cartodb_id\" As id, i.\"andy\"::numeric As attr1, " \ ans = "SELECT i.\"cartodb_id\" As id, i.\"andy\"::numeric As attr1, " \
"i.\"jay_z\"::numeric As attr2, (SELECT ARRAY(SELECT " \ "i.\"jay_z\"::numeric As attr2, (SELECT ARRAY(SELECT " \
"j.\"cartodb_id\" FROM (SELECT * FROM a_list) As j WHERE j.\"andy\" IS " \ "j.\"cartodb_id\" FROM (SELECT * FROM a_list) As j WHERE j.\"andy\" IS " \
"NOT NULL AND j.\"jay_z\" IS NOT NULL AND j.\"jay_z\" <> 0 " \ "NOT NULL AND j.\"jay_z\" IS NOT NULL AND j.\"jay_z\" <> 0 " \
"ORDER BY j.\"the_geom\" <-> i.\"the_geom\" ASC LIMIT 321 " \ "ORDER BY j.\"the_geom\" <-> i.\"the_geom\" ASC LIMIT 321 " \
"OFFSET 1 ) ) As neighbors FROM (SELECT * FROM a_list) As i WHERE " \ "OFFSET 1 ) ) As neighbors FROM (SELECT * FROM a_list) As i " \
"i.\"andy\" IS NOT NULL AND i.\"jay_z\" IS NOT NULL AND " \ "WHERE i.\"andy\" IS NOT NULL AND i.\"jay_z\" IS NOT NULL AND " \
"i.\"jay_z\" <> 0 ORDER BY i.\"cartodb_id\" ASC;" "i.\"jay_z\" <> 0 " \
"ORDER BY i.\"cartodb_id\" ASC;"
self.assertEqual(cc.get_query('knn', self.params), ans) self.assertEqual(cc.construct_neighbor_query('knn', self.params), ans)
def test_get_attributes(self): def test_get_attributes(self):
"""Test get_attributes.""" """Test get_attributes."""