mirror of
https://github.com/CartoDB/crankshaft.git
synced 2024-11-01 10:20:48 +08:00
add query for gwr
This commit is contained in:
parent
01c9195ea5
commit
6e50e43e1c
@ -42,7 +42,7 @@ def get_weight(query_res, w_type='knn', num_ngbrs=5):
|
||||
return built_weight
|
||||
|
||||
|
||||
def query_attr_select(params):
|
||||
def query_attr_select(params, table_ref="i"):
|
||||
"""
|
||||
Create portion of SELECT statement for attributes inolved in query.
|
||||
@param params: dict of information used in query (column names,
|
||||
@ -50,11 +50,15 @@ def query_attr_select(params):
|
||||
"""
|
||||
|
||||
attr_string = ""
|
||||
template = "i.\"%(col)s\"::numeric As attr%(alias_num)s, "
|
||||
template = "\"%(col)s\"::numeric As attr%(alias_num)s, "
|
||||
|
||||
if 'time_cols' in params:
|
||||
# if markov analysis
|
||||
attrs = params['time_cols']
|
||||
if table_ref is not None:
|
||||
template = "{table_ref}.".format(table_ref=table_ref) + template
|
||||
|
||||
if ('time_cols' in params) or ('ind_vars' in params):
|
||||
# if markov or gwr analysis
|
||||
attrs = (params['time_cols'] if 'time_cols' in params
|
||||
else params['ind_vars'])
|
||||
|
||||
for idx, val in enumerate(attrs):
|
||||
attr_string += template % {"col": val, "alias_num": idx + 1}
|
||||
@ -71,7 +75,7 @@ def query_attr_select(params):
|
||||
return attr_string
|
||||
|
||||
|
||||
def query_attr_where(params):
|
||||
def query_attr_where(params, table_ref=None):
|
||||
"""
|
||||
Construct where conditions when building neighbors query
|
||||
Create portion of WHERE clauses for weeding out NULL-valued geometries
|
||||
@ -90,11 +94,14 @@ def query_attr_where(params):
|
||||
NULL AND idx_replace."time3" IS NOT NULL'
|
||||
"""
|
||||
attr_string = []
|
||||
template = "idx_replace.\"%s\" IS NOT NULL"
|
||||
template = "\"%s\" IS NOT NULL"
|
||||
if table_ref is not None:
|
||||
template = "idx_replace." + template
|
||||
|
||||
if 'time_cols' in params:
|
||||
# markov where clauses
|
||||
attrs = params['time_cols']
|
||||
if ('time_cols' in params) or ('ind_vars' in params):
|
||||
# markov or gwr where clauses
|
||||
attrs = (params['time_cols'] if 'time_cols' in params
|
||||
else params['ind_vars'])
|
||||
# add values to template
|
||||
for attr in attrs:
|
||||
attr_string.append(template % attr)
|
||||
@ -104,13 +111,16 @@ def query_attr_where(params):
|
||||
# get keys
|
||||
attrs = sorted([k for k in params
|
||||
if k not in ('id_col', 'geom_col', 'subquery',
|
||||
'num_ngbrs', 'subquery')])
|
||||
'num_ngbrs', 'ind_vars')])
|
||||
# add values to template
|
||||
for attr in attrs:
|
||||
attr_string.append(template % params[attr])
|
||||
|
||||
if len(attrs) == 2:
|
||||
attr_string.append("idx_replace.\"%s\" <> 0" % params[attrs[1]])
|
||||
check_zero = "\"%s\" <> 0" % params[attrs[1]]
|
||||
if table_ref is not None:
|
||||
check_zero = "idx_replace." + check_zero
|
||||
attr_string.append(check_zero)
|
||||
|
||||
out = " AND ".join(attr_string)
|
||||
|
||||
@ -177,6 +187,31 @@ def queen(params):
|
||||
|
||||
return query.format(**params)
|
||||
|
||||
def gwr_query(params):
|
||||
"""
|
||||
GWR query
|
||||
"""
|
||||
|
||||
replacements = {"ind_vars_select": query_attr_select(params,
|
||||
table_ref=None),
|
||||
"ind_vars_where": query_attr_where(params,
|
||||
table_ref=None)}
|
||||
|
||||
query = '''
|
||||
SELECT
|
||||
array_agg(ST_X({geom_col})) As x,
|
||||
array_agg(ST_Y({geom_col})) As y,
|
||||
array_agg({dep_var}) As dep_var,
|
||||
%(ind_vars_select)s,
|
||||
array_agg({id_col}) As rowid
|
||||
FROM ({subquery}) As q
|
||||
WHERE
|
||||
{dep_var} IS NOT NULL AND
|
||||
%(ind_vars_where)s
|
||||
''' % replacements
|
||||
|
||||
return query.format(**params).strip()
|
||||
|
||||
# to add more weight methods open a ticket or pull request
|
||||
|
||||
|
||||
|
@ -16,14 +16,14 @@ def gwr(subquery, dep_var, ind_vars, fixed=False, kernel='bisquare'):
|
||||
|
||||
x = np.array(query_result[0]['x'])
|
||||
y = np.array(query_result[0]['y'])
|
||||
coords = zip(x,y)
|
||||
coords = zip(x, y)
|
||||
|
||||
Y = query_result[0]['dep'].reshape((-1,1))
|
||||
Y = query_result[0]['dep'].reshape((-1, 1))
|
||||
n = Y.shape[0]
|
||||
k = len(ind_vars)
|
||||
X = np.zeros((n, k))
|
||||
|
||||
for attr in range(0,k):
|
||||
for attr in range(0, k):
|
||||
attr_name = 'attr' + str(attr+1)
|
||||
X[:, attr] = np.array(query_result[0][attr_name]).flatten()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user