def local_bivariate_stat(self, subquery, attr1, attr2, permutations, geom_col, id_col, w_type, num_ngbrs): """ Moran's I (local) Bivariate (untested) """ params = OrderedDict([("id_col", id_col), ("attr1", attr1), ("attr2", attr2), ("geom_col", geom_col), ("subquery", subquery), ("num_ngbrs", num_ngbrs)]) result = self.data_provider.get_moran(w_type, params) # collect attributes attr1_vals = pu.get_attributes(result, 1) attr2_vals = pu.get_attributes(result, 2) # create weights weight = pu.get_weight(result, w_type, num_ngbrs) # calculate LISA values lisa = ps.esda.moran.Moran_Local_BV(attr1_vals, attr2_vals, weight, permutations=permutations) # find clustering of significance lisa_sig = quad_position(lisa.q) return zip(lisa.Is, lisa_sig, lisa.p_sim, weight.id_order)
def local_rate_stat(self, subquery, numerator, denominator, w_type, num_ngbrs, permutations, geom_col, id_col): """ Moran's I Local Rate Andy Eschbacher """ # geometries with values that are null are ignored # resulting in a collection of not as near neighbors params = OrderedDict([("id_col", id_col), ("numerator", numerator), ("denominator", denominator), ("geom_col", geom_col), ("subquery", subquery), ("num_ngbrs", num_ngbrs)]) result = self.data_provider.get_moran(w_type, params) # collect attributes numer = pu.get_attributes(result, 1) denom = pu.get_attributes(result, 2) weight = pu.get_weight(result, w_type, num_ngbrs) # calculate LISA values lisa = ps.esda.moran.Moran_Local_Rate(numer, denom, weight, permutations=permutations) # find quadrants for each geometry quads = quad_position(lisa.q) return zip(lisa.Is, quads, lisa.p_sim, weight.id_order, lisa.y)
def local_stat(self, subquery, attr, w_type, num_ngbrs, permutations, geom_col, id_col): """ Moran's I implementation for PL/Python Andy Eschbacher """ # geometries with attributes that are null are ignored # resulting in a collection of not as near neighbors params = OrderedDict([("id_col", id_col), ("attr1", attr), ("geom_col", geom_col), ("subquery", subquery), ("num_ngbrs", num_ngbrs)]) result = self.data_provider.get_moran(w_type, params) attr_vals = pu.get_attributes(result) weight = pu.get_weight(result, w_type, num_ngbrs) # calculate LISA values lisa = ps.esda.moran.Moran_Local(attr_vals, weight, permutations=permutations) # find quadrants for each geometry quads = quad_position(lisa.q) return zip(lisa.Is, quads, lisa.p_sim, weight.id_order, lisa.y)
def global_stat(self, subquery, attr_name, w_type, num_ngbrs, permutations, geom_col, id_col): """ Moran's I (global) Implementation building neighbors with a PostGIS database and Moran's I core clusters with PySAL. Andy Eschbacher """ params = OrderedDict([("id_col", id_col), ("attr1", attr_name), ("geom_col", geom_col), ("subquery", subquery), ("num_ngbrs", num_ngbrs)]) result = self.data_provider.get_moran(w_type, params) # collect attributes attr_vals = pu.get_attributes(result) # calculate weights weight = pu.get_weight(result, w_type, num_ngbrs) # calculate moran global moran_global = ps.esda.moran.Moran(attr_vals, weight, permutations=permutations) return zip([moran_global.I], [moran_global.EI])
def global_rate_stat(self, subquery, numerator, denominator, w_type, num_ngbrs, permutations, geom_col, id_col): """ Moran's I Rate (global) Andy Eschbacher """ params = OrderedDict([("id_col", id_col), ("attr1", numerator), ("attr2", denominator) ("geom_col", geom_col), ("subquery", subquery), ("num_ngbrs", num_ngbrs)]) result = self.data_provider.get_moran(w_type, params) # collect attributes numer = pu.get_attributes(result, 1) denom = pu.get_attributes(result, 2) weight = pu.get_weight(result, w_type, num_ngbrs) # calculate moran global rate lisa_rate = ps.esda.moran.Moran_Rate(numer, denom, weight, permutations=permutations) return zip([lisa_rate.I], [lisa_rate.EI])
def getis_ord(self, subquery, attr, w_type, num_ngbrs, permutations, geom_col, id_col): """ Getis-Ord's G* Implementation building neighbors with a PostGIS database and PySAL's Getis-Ord's G* hotspot/coldspot module. Andy Eschbacher """ # geometries with attributes that are null are ignored # resulting in a collection of not as near neighbors if kNN is chosen qvals = OrderedDict([("id_col", id_col), ("attr1", attr), ("geom_col", geom_col), ("subquery", subquery), ("num_ngbrs", num_ngbrs)]) result = self.data_provider.get_getis(w_type, qvals) attr_vals = pu.get_attributes(result) # build PySAL weight object weight = pu.get_weight(result, w_type, num_ngbrs) # calculate Getis-Ord's G* z- and p-values getis = ps.esda.getisord.G_Local(attr_vals, weight, star=True, permutations=permutations) return zip(getis.z_sim, getis.p_sim, getis.p_z_sim, weight.id_order)
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') qvals = { "num_ngbrs": num_ngbrs, "attr1": attr1, "attr2": attr2, "subquery": subquery, "geom_col": geom_col, "id_col": id_col } query = pu.construct_neighbor_query(w_type, qvals) try: result = plpy.execute(query) # if there are no neighbors, exit if len(result) == 0: return pu.empty_zipped_array(4) except plpy.SPIError: plpy.error("Error: areas of interest query failed, " \ "check input parameters") plpy.notice('** Query failed: "%s"' % query) return pu.empty_zipped_array(4) ## collect attributes attr1_vals = pu.get_attributes(result, 1) attr2_vals = pu.get_attributes(result, 2) # create weights weight = pu.get_weight(result, w_type, num_ngbrs) # calculate LISA values lisa = ps.esda.moran.Moran_Local_BV(attr1_vals, attr2_vals, weight, permutations=permutations) plpy.notice("len of Is: %d" % len(lisa.Is)) # find clustering of significance lisa_sig = quad_position(lisa.q) plpy.notice('** Finished calculations') return zip(lisa.Is, lisa_sig, lisa.p_sim, weight.id_order)
def moran_local_rate(subquery, numerator, denominator, w_type, num_ngbrs, permutations, geom_col, id_col): """ Moran's I Local Rate Andy Eschbacher """ # geometries with values that are null are ignored # resulting in a collection of not as near neighbors query = pu.construct_neighbor_query( w_type, { "id_col": id_col, "numerator": numerator, "denominator": denominator, "geom_col": geom_col, "subquery": subquery, "num_ngbrs": num_ngbrs }) try: result = plpy.execute(query) # if there are no neighbors, exit if len(result) == 0: return pu.empty_zipped_array(5) except plpy.SPIError: plpy.error( 'Error: areas of interest query failed, check input parameters') plpy.notice('** Query failed: "%s"' % query) plpy.notice('** Error: %s' % plpy.SPIError) return pu.empty_zipped_array(5) ## collect attributes numer = pu.get_attributes(result, 1) denom = pu.get_attributes(result, 2) weight = pu.get_weight(result, w_type, num_ngbrs) # calculate LISA values lisa = ps.esda.moran.Moran_Local_Rate(numer, denom, weight, permutations=permutations) # find units of significance quads = quad_position(lisa.q) return zip(lisa.Is, quads, lisa.p_sim, weight.id_order, lisa.y)
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') qvals = OrderedDict([("id_col", id_col), ("attr1", attr1), ("attr2", attr2), ("geom_col", geom_col), ("subquery", subquery), ("num_ngbrs", num_ngbrs)]) query = pu.construct_neighbor_query(w_type, qvals) try: result = plpy.execute(query) # if there are no neighbors, exit if len(result) == 0: return pu.empty_zipped_array(4) except plpy.SPIError: plpy.error("Error: areas of interest query failed, " \ "check input parameters") plpy.notice('** Query failed: "%s"' % query) return pu.empty_zipped_array(4) ## collect attributes attr1_vals = pu.get_attributes(result, 1) attr2_vals = pu.get_attributes(result, 2) # create weights weight = pu.get_weight(result, w_type, num_ngbrs) # calculate LISA values lisa = ps.esda.moran.Moran_Local_BV(attr1_vals, attr2_vals, weight, permutations=permutations) plpy.notice("len of Is: %d" % len(lisa.Is)) # find clustering of significance lisa_sig = quad_position(lisa.q) plpy.notice('** Finished calculations') return zip(lisa.Is, lisa_sig, lisa.p_sim, weight.id_order)
def global_stat(self, subquery, attr_name, w_type, num_ngbrs, permutations, geom_col, id_col): """ Moran's I (global) Implementation building neighbors with a PostGIS database and Moran's I core clusters with PySAL. Args: subquery (str): Query to give access to the data needed. This query must give access to ``attr_name``, ``geom_col``, and ``id_col``. attr_name (str): Column name of data to analyze w_type (str): Type of spatial weight. Must be one of `knn` or `queen`. See `PySAL documentation <http://pysal.readthedocs.io/en/latest/users/tutorials/weights.html>`__ for more information. num_ngbrs (int): If using `knn` for ``w_type``, this specifies the number of neighbors to be used to define the spatial neighborhoods. permutations (int): Number of permutations for performing conditional randomization to find the p-value. Higher numbers takes a longer time for getting results. geom_col (str): Name of the geometry column in the dataset for finding the spatial neighborhoods. id_col (str): Row index for each value. Usually the database index. """ params = OrderedDict([("id_col", id_col), ("attr1", attr_name), ("geom_col", geom_col), ("subquery", subquery), ("num_ngbrs", num_ngbrs)]) result = self.data_provider.get_moran(w_type, params) # collect attributes attr_vals = pu.get_attributes(result) # calculate weights weight = pu.get_weight(result, w_type, num_ngbrs) # calculate moran global moran_global = ps.esda.moran.Moran(attr_vals, weight, permutations=permutations) return zip([moran_global.I], [moran_global.EI])
def moran(subquery, attr_name, w_type, num_ngbrs, permutations, geom_col, id_col): """ Moran's I (global) Implementation building neighbors with a PostGIS database and Moran's I core clusters with PySAL. Andy Eschbacher """ qvals = { "id_col": id_col, "attr1": attr_name, "geom_col": geom_col, "subquery": subquery, "num_ngbrs": num_ngbrs } query = pu.construct_neighbor_query(w_type, qvals) plpy.notice('** Query: %s' % query) try: result = plpy.execute(query) # if there are no neighbors, exit if len(result) == 0: return pu.empty_zipped_array(2) plpy.notice('** Query returned with %d rows' % len(result)) except plpy.SPIError: plpy.error( 'Error: areas of interest query failed, check input parameters') plpy.notice('** Query failed: "%s"' % query) plpy.notice('** Error: %s' % plpy.SPIError) return pu.empty_zipped_array(2) ## collect attributes attr_vals = pu.get_attributes(result) ## calculate weights weight = pu.get_weight(result, w_type, num_ngbrs) ## calculate moran global moran_global = ps.esda.moran.Moran(attr_vals, weight, permutations=permutations) return zip([moran_global.I], [moran_global.EI])
def moran_rate(subquery, numerator, denominator, w_type, num_ngbrs, permutations, geom_col, id_col): """ Moran's I Rate (global) Andy Eschbacher """ qvals = { "id_col": id_col, "attr1": numerator, "attr2": denominator, "geom_col": geom_col, "subquery": subquery, "num_ngbrs": num_ngbrs } query = pu.construct_neighbor_query(w_type, qvals) plpy.notice('** Query: %s' % query) try: result = plpy.execute(query) # if there are no neighbors, exit if len(result) == 0: return pu.empty_zipped_array(2) plpy.notice('** Query returned with %d rows' % len(result)) except plpy.SPIError: plpy.error( 'Error: areas of interest query failed, check input parameters') plpy.notice('** Query failed: "%s"' % query) plpy.notice('** Error: %s' % plpy.SPIError) return pu.empty_zipped_array(2) ## collect attributes numer = pu.get_attributes(result, 1) denom = pu.get_attributes(result, 2) weight = pu.get_weight(result, w_type, num_ngbrs) ## calculate moran global rate lisa_rate = ps.esda.moran.Moran_Rate(numer, denom, weight, permutations=permutations) return zip([lisa_rate.I], [lisa_rate.EI])
def global_rate_stat(self, subquery, numerator, denominator, w_type, num_ngbrs, permutations, geom_col, id_col): """ Moran's I Rate (global) Args: subquery (str): Query to give access to the data needed. This query must give access to ``attr_name``, ``geom_col``, and ``id_col``. numerator (str): Column name of numerator to analyze denominator (str): Column name of the denominator w_type (str): Type of spatial weight. Must be one of `knn` or `queen`. See `PySAL documentation <http://pysal.readthedocs.io/en/latest/users/tutorials/weights.html>`__ for more information. num_ngbrs (int): If using `knn` for ``w_type``, this specifies the number of neighbors to be used to define the spatial neighborhoods. permutations (int): Number of permutations for performing conditional randomization to find the p-value. Higher numbers takes a longer time for getting results. geom_col (str): Name of the geometry column in the dataset for finding the spatial neighborhoods. id_col (str): Row index for each value. Usually the database index. """ params = OrderedDict([("id_col", id_col), ("attr1", numerator), ("attr2", denominator), ("geom_col", geom_col), ("subquery", subquery), ("num_ngbrs", num_ngbrs)]) result = self.data_provider.get_moran(w_type, params) # collect attributes numer = pu.get_attributes(result, 1) denom = pu.get_attributes(result, 2) weight = pu.get_weight(result, w_type, num_ngbrs) # calculate moran global rate lisa_rate = ps.esda.moran.Moran_Rate(numer, denom, weight, permutations=permutations) return list(zip([lisa_rate.I], [lisa_rate.EI]))
def moran_local(subquery, attr, w_type, num_ngbrs, permutations, geom_col, id_col): """ Moran's I implementation for PL/Python Andy Eschbacher """ # geometries with attributes that are null are ignored # resulting in a collection of not as near neighbors qvals = { "id_col": id_col, "attr1": attr, "geom_col": geom_col, "subquery": subquery, "num_ngbrs": num_ngbrs } query = pu.construct_neighbor_query(w_type, qvals) try: result = plpy.execute(query) # if there are no neighbors, exit if len(result) == 0: return pu.empty_zipped_array(5) except plpy.SPIError: plpy.error( 'Error: areas of interest query failed, check input parameters') plpy.notice('** Query failed: "%s"' % query) return pu.empty_zipped_array(5) attr_vals = pu.get_attributes(result) weight = pu.get_weight(result, w_type, num_ngbrs) # calculate LISA values lisa = ps.esda.moran.Moran_Local(attr_vals, weight, permutations=permutations) # find quadrants for each geometry quads = quad_position(lisa.q) return zip(lisa.Is, quads, lisa.p_sim, weight.id_order, lisa.y)
def moran_local_rate(subquery, numerator, denominator, w_type, num_ngbrs, permutations, geom_col, id_col): """ Moran's I Local Rate Andy Eschbacher """ # geometries with values that are null are ignored # resulting in a collection of not as near neighbors qvals = OrderedDict([("id_col", id_col), ("numerator", numerator), ("denominator", denominator), ("geom_col", geom_col), ("subquery", subquery), ("num_ngbrs", num_ngbrs)]) query = pu.construct_neighbor_query(w_type, qvals) try: result = plpy.execute(query) # if there are no neighbors, exit if len(result) == 0: return pu.empty_zipped_array(5) except plpy.SPIError: plpy.error('Error: areas of interest query failed, check input parameters') plpy.notice('** Query failed: "%s"' % query) plpy.notice('** Error: %s' % plpy.SPIError) return pu.empty_zipped_array(5) ## collect attributes numer = pu.get_attributes(result, 1) denom = pu.get_attributes(result, 2) weight = pu.get_weight(result, w_type, num_ngbrs) # calculate LISA values lisa = ps.esda.moran.Moran_Local_Rate(numer, denom, weight, permutations=permutations) # find quadrants for each geometry quads = quad_position(lisa.q) return zip(lisa.Is, quads, lisa.p_sim, weight.id_order, lisa.y)
def moran(subquery, attr_name, w_type, num_ngbrs, permutations, geom_col, id_col): """ Moran's I (global) Implementation building neighbors with a PostGIS database and Moran's I core clusters with PySAL. Andy Eschbacher """ qvals = OrderedDict([("id_col", id_col), ("attr1", attr_name), ("geom_col", geom_col), ("subquery", subquery), ("num_ngbrs", num_ngbrs)]) query = pu.construct_neighbor_query(w_type, qvals) plpy.notice('** Query: %s' % query) try: result = plpy.execute(query) # if there are no neighbors, exit if len(result) == 0: return pu.empty_zipped_array(2) plpy.notice('** Query returned with %d rows' % len(result)) except plpy.SPIError: plpy.error('Error: areas of interest query failed, check input parameters') plpy.notice('** Query failed: "%s"' % query) plpy.notice('** Error: %s' % plpy.SPIError) return pu.empty_zipped_array(2) ## collect attributes attr_vals = pu.get_attributes(result) ## calculate weights weight = pu.get_weight(result, w_type, num_ngbrs) ## calculate moran global moran_global = ps.esda.moran.Moran(attr_vals, weight, permutations=permutations) return zip([moran_global.I], [moran_global.EI])
def moran_rate(subquery, numerator, denominator, w_type, num_ngbrs, permutations, geom_col, id_col): """ Moran's I Rate (global) Andy Eschbacher """ qvals = OrderedDict([("id_col", id_col), ("attr1", numerator), ("attr2", denominator) ("geom_col", geom_col), ("subquery", subquery), ("num_ngbrs", num_ngbrs)]) query = pu.construct_neighbor_query(w_type, qvals) plpy.notice('** Query: %s' % query) try: result = plpy.execute(query) # if there are no neighbors, exit if len(result) == 0: return pu.empty_zipped_array(2) plpy.notice('** Query returned with %d rows' % len(result)) except plpy.SPIError: plpy.error('Error: areas of interest query failed, check input parameters') plpy.notice('** Query failed: "%s"' % query) plpy.notice('** Error: %s' % plpy.SPIError) return pu.empty_zipped_array(2) ## collect attributes numer = pu.get_attributes(result, 1) denom = pu.get_attributes(result, 2) weight = pu.get_weight(result, w_type, num_ngbrs) ## calculate moran global rate lisa_rate = ps.esda.moran.Moran_Rate(numer, denom, weight, permutations=permutations) return zip([lisa_rate.I], [lisa_rate.EI])
def moran_local_bv(subquery, attr1, attr2, permutations, geom_col, id_col, w_type, num_ngbrs): """ Moran's I (local) Bivariate (untested) """ qvals = OrderedDict([("id_col", id_col), ("attr1", attr1), ("attr2", attr2), ("geom_col", geom_col), ("subquery", subquery), ("num_ngbrs", num_ngbrs)]) query = pu.construct_neighbor_query(w_type, qvals) try: result = plpy.execute(query) # if there are no neighbors, exit if len(result) == 0: return pu.empty_zipped_array(4) except plpy.SPIError: plpy.error("Error: areas of interest query failed, " "check input parameters") return pu.empty_zipped_array(4) # collect attributes attr1_vals = pu.get_attributes(result, 1) attr2_vals = pu.get_attributes(result, 2) # create weights weight = pu.get_weight(result, w_type, num_ngbrs) # calculate LISA values lisa = ps.esda.moran.Moran_Local_BV(attr1_vals, attr2_vals, weight, permutations=permutations) # find clustering of significance lisa_sig = quad_position(lisa.q) return zip(lisa.Is, lisa_sig, lisa.p_sim, weight.id_order)
"subquery": subquery, "num_ngbrs": num_ngbrs} try: query_result = plpy.execute( pu.construct_neighbor_query(w_type, qvals) ) if len(query_result) == 0: return zip([None], [None], [None], [None], [None]) except plpy.SPIError, err: plpy.debug('Query failed with exception %s: %s' % (err, pu.construct_neighbor_query(w_type, qvals))) plpy.error('Query failed, check the input parameters') return zip([None], [None], [None], [None], [None]) ## build weight weights = pu.get_weight(query_result, w_type) weights.transform = 'r' ## prep time data t_data = get_time_data(query_result, time_cols) plpy.debug('shape of t_data %d, %d' % t_data.shape) plpy.debug('number of weight objects: %d, %d' % (weights.sparse).shape) plpy.debug('first num elements: %f' % t_data[0, 0]) sp_markov_result = ps.Spatial_Markov(t_data, weights, k=num_classes, fixed=False, permutations=permutations)
query = pu.construct_neighbor_query(w_type, qvals) try: result = plpy.execute(query) # if there are no neighbors, exit if len(result) == 0: return pu.empty_zipped_array(2) except plpy.SPIError, e: plpy.error('Analysis failed: %s' % e) return pu.empty_zipped_array(2) ## collect attributes attr_vals = pu.get_attributes(result) ## calculate weights weight = pu.get_weight(result, w_type, num_ngbrs) ## calculate moran global moran_global = ps.esda.moran.Moran(attr_vals, weight, permutations=permutations) return zip([moran_global.I], [moran_global.EI]) def moran_local(subquery, attr, w_type, num_ngbrs, permutations, geom_col, id_col): """ Moran's I implementation for PL/Python Andy Eschbacher """
query = pu.construct_neighbor_query(w_type, qvals) try: result = plpy.execute(query) # if there are no neighbors, exit if len(result) == 0: return pu.empty_zipped_array(2) except plpy.SPIError, e: plpy.error('Analysis failed: %s' % e) return pu.empty_zipped_array(2) ## collect attributes attr_vals = pu.get_attributes(result) ## calculate weights weight = pu.get_weight(result, w_type, num_ngbrs) ## calculate moran global moran_global = ps.esda.moran.Moran(attr_vals, weight, permutations=permutations) return zip([moran_global.I], [moran_global.EI]) def moran_local(subquery, attr, w_type, num_ngbrs, permutations, geom_col, id_col): """ Moran's I implementation for PL/Python Andy Eschbacher """ # geometries with attributes that are null are ignored
def spatial_trend(self, subquery, time_cols, num_classes=7, w_type='knn', num_ngbrs=5, permutations=0, geom_col='the_geom', id_col='cartodb_id'): """ Predict the trends of a unit based on: 1. history of its transitions to different classes (e.g., 1st quantile -> 2nd quantile) 2. average class of its neighbors Inputs: @param subquery string: e.g., SELECT the_geom, cartodb_id, interesting_time_column FROM table_name @param time_cols list of strings: list of strings of column names @param num_classes (optional): number of classes to break distribution of values into. Currently uses quantile bins. @param w_type string (optional): weight type ('knn' or 'queen') @param num_ngbrs int (optional): number of neighbors (if knn type) @param permutations int (optional): number of permutations for test stats @param geom_col string (optional): name of column which contains the geometries @param id_col string (optional): name of column which has the ids of the table Outputs: @param trend_up float: probablity that a geom will move to a higher class @param trend_down float: probablity that a geom will move to a lower class @param trend float: (trend_up - trend_down) / trend_static @param volatility float: a measure of the volatility based on probability stddev(prob array) """ if len(time_cols) < 2: plpy.error('More than one time column needs to be passed') params = { "id_col": id_col, "time_cols": time_cols, "geom_col": geom_col, "subquery": subquery, "num_ngbrs": num_ngbrs } result = self.data_provider.get_markov(w_type, params) # build weight weights = pu.get_weight(result, w_type) weights.transform = 'r' # prep time data t_data = get_time_data(result, time_cols) sp_markov_result = ps.Spatial_Markov(t_data, weights, k=num_classes, fixed=False, permutations=permutations) # get lag classes lag_classes = ps.Quantiles(ps.lag_spatial(weights, t_data[:, -1]), k=num_classes).yb # look up probablity distribution for each unit according to class and # lag class prob_dist = get_prob_dist(sp_markov_result.P, lag_classes, sp_markov_result.classes[:, -1]) # find the ups and down and overall distribution of each cell trend_up, trend_down, trend, volatility = get_prob_stats( prob_dist, sp_markov_result.classes[:, -1]) # output the results return zip(trend, trend_up, trend_down, volatility, weights.id_order)
"subquery": subquery, "num_ngbrs": num_ngbrs } try: query_result = plpy.execute(pu.construct_neighbor_query(w_type, qvals)) if len(query_result) == 0: return zip([None], [None], [None], [None], [None]) except plpy.SPIError, e: plpy.debug('Query failed with exception %s: %s' % (err, pu.construct_neighbor_query(w_type, qvals))) plpy.error('Analysis failed: %s' % e) return zip([None], [None], [None], [None], [None]) ## build weight weights = pu.get_weight(query_result, w_type) weights.transform = 'r' ## prep time data t_data = get_time_data(query_result, time_cols) plpy.debug('shape of t_data %d, %d' % t_data.shape) plpy.debug('number of weight objects: %d, %d' % (weights.sparse).shape) plpy.debug('first num elements: %f' % t_data[0, 0]) sp_markov_result = ps.Spatial_Markov(t_data, weights, k=num_classes, fixed=False, permutations=permutations)
def local_stat(self, subquery, attr, w_type, num_ngbrs, permutations, geom_col, id_col): """ Moran's I (local) Args: subquery (str): Query to give access to the data needed. This query must give access to ``attr_name``, ``geom_col``, and ``id_col``. attr (str): Column name of data to analyze w_type (str): Type of spatial weight. Must be one of `knn` or `queen`. See `PySAL documentation <http://pysal.readthedocs.io/en/latest/users/tutorials/weights.html>`__ for more information. num_ngbrs (int): If using `knn` for ``w_type``, this specifies the number of neighbors to be used to define the spatial neighborhoods. permutations (int): Number of permutations for performing conditional randomization to find the p-value. Higher numbers takes a longer time for getting results. geom_col (str): Name of the geometry column in the dataset for finding the spatial neighborhoods. id_col (str): Row index for each value. Usually the database index. Returns: list of tuples: Where each tuple consists of the following values: - quadrants classification (one of `HH`, `HL`, `LL`, or `LH`) - p-value - spatial lag - standardized spatial lag (centered on the mean, normalized by the standard deviation) - original value - standardized value - Moran's I statistic - original row index """ # geometries with attributes that are null are ignored # resulting in a collection of not as near neighbors params = OrderedDict([("id_col", id_col), ("attr1", attr), ("geom_col", geom_col), ("subquery", subquery), ("num_ngbrs", num_ngbrs)]) result = self.data_provider.get_moran(w_type, params) attr_vals = pu.get_attributes(result) weight = pu.get_weight(result, w_type, num_ngbrs) # calculate LISA values lisa = ps.esda.moran.Moran_Local(attr_vals, weight, permutations=permutations) # find quadrants for each geometry quads = quad_position(lisa.q) # calculate spatial lag lag = ps.weights.spatial_lag.lag_spatial(weight, lisa.y) lag_std = ps.weights.spatial_lag.lag_spatial(weight, lisa.z) return zip(quads, lisa.p_sim, lag, lag_std, lisa.y, lisa.z, lisa.Is, weight.id_order)