def updateStats(self): if self.current_layer is not None: current_attribute = self.dlg.getCurrentAttribute() if current_attribute >= 0: attribute = self.layer_attributes[current_attribute] # check if stats have been calculated before idx = self.checkValuesAvailable(attribute) if idx == -1: self.retrieveAttributeValues(attribute) idx = len(self.attribute_statistics) - 1 stats = self.attribute_statistics[idx] # calculate stats of selected objects only select_stats = dict() if self.current_layer.selectedFeatureCount() > 0: self.selection_values, self.selection_ids = lfh.getFieldValues(self.current_layer, attribute['name'], null=False, selection=True) sel_values = [val for val in self.selection_values if val != NULL] select_stats['Number'] = len(sel_values) select_stats['Mean'] = uf.truncateNumber(np.mean(sel_values)) select_stats['Std Dev'] = uf.truncateNumber(np.std(sel_values)) select_stats['Variance'] = uf.truncateNumber(np.var(sel_values)) select_stats['Median'] = uf.truncateNumber(np.median(sel_values)) select_stats['Minimum'] = np.min(sel_values) select_stats['Maximum'] = np.max(sel_values) select_stats['Range'] = uf.truncateNumber(select_stats['Maximum'] - select_stats['Minimum']) select_stats['1st Quart'] = uf.truncateNumber(np.percentile(sel_values, 25)) select_stats['3rd Quart'] = uf.truncateNumber(np.percentile(sel_values, 75)) select_stats['IQR'] = uf.truncateNumber(select_stats['3rd Quart'] - select_stats['1st Quart']) select_stats['Gini'] = uf.roundNumber(uf.calcGini(sel_values)) else: self.selection_values = [] self.selection_ids = [] # update the dialog self.dlg.setStats(stats, select_stats)
def test_roundNumber_over_100(self): self.assertEqual(uf.roundNumber(200.0), 200.0) self.assertEqual(uf.roundNumber(200.045), 200.0) self.assertEqual(uf.roundNumber(200.055), 200.1) self.assertEqual(uf.roundNumber(-200.0), -200.0) self.assertEqual(uf.roundNumber(-200.045), -200.0) self.assertEqual(uf.roundNumber(-200.055), -200.1)
def test_roundNumber_1_100(self): self.assertEqual(uf.roundNumber(2.00), 2.00) self.assertEqual(uf.roundNumber(2.0045), 2.00) self.assertEqual(uf.roundNumber(2.0055), 2.01) self.assertEqual(uf.roundNumber(-2.00), -2.00) self.assertEqual(uf.roundNumber(-2.0045), -2.00) self.assertEqual(uf.roundNumber(-2.0055), -2.01)
def test_roundNumber_001_1(self): self.assertEqual(uf.roundNumber(0.2000), 0.2000) self.assertEqual(uf.roundNumber(0.200045), 0.2000) self.assertEqual(uf.roundNumber(0.200055), 0.2001) self.assertEqual(uf.roundNumber(-0.2000), -0.2000) self.assertEqual(uf.roundNumber(-0.200045), -0.2000) self.assertEqual(uf.roundNumber(-0.200055), -0.2001)
def retrieveAttributeValues(self, attribute): storage = self.current_layer.storageType() if 'spatialite' in storage.lower(): # todo: retrieve values and ids using SQL query values, ids = lfh.getFieldValues(self.current_layer, attribute["name"], null=True) clean_values = [val for val in values if val != NULL] elif 'postgresql' in storage.lower(): # todo: retrieve values and ids using SQL query values, ids = lfh.getFieldValues(self.current_layer, attribute["name"], null=True) clean_values = [val for val in values if val != NULL] else: values, ids = lfh.getFieldValues(self.current_layer, attribute["name"], null=True) # we need to keep the complete values set for the scatterplot, must get rid of NULL values for other stats clean_values = [val for val in values if val != NULL] if values and ids: stats = dict() stats['Layer'] = self.current_layer.name() stats['Attribute'] = attribute['name'] stats['Number'] = len(clean_values) stats['Mean'] = uf.truncateNumber(np.mean(clean_values)) stats['Std Dev'] = uf.truncateNumber(np.std(clean_values)) stats['Variance'] = uf.truncateNumber(np.var(clean_values)) stats['Median'] = uf.truncateNumber(np.median(clean_values)) stats['Minimum'] = np.min(clean_values) stats['Maximum'] = np.max(clean_values) stats['Range'] = uf.truncateNumber(stats['Maximum'] - stats['Minimum']) stats['1st Quart'] = uf.truncateNumber(np.percentile(clean_values, 25)) stats['3rd Quart'] = uf.truncateNumber(np.percentile(clean_values, 75)) stats['IQR'] = uf.truncateNumber(stats['3rd Quart'] - stats['1st Quart']) stats['Gini'] = uf.roundNumber(uf.calcGini(clean_values)) # store the results self.attribute_statistics.append(stats) # store retrieved values for selection stats and charts attr = dict() attr['Layer'] = self.current_layer.name() attr['Attribute'] = attribute['name'] attr['values'] = values attr['ids'] = ids attr['nulls'] = (len(values) != len(clean_values)) attr['bins'] = uf.calcBins(clean_values) self.attribute_values.append(attr)
def calculateBivariateStats(self, xname, xvalues, yname, yvalues): bistats = dict() bistats['Layer'] = self.current_layer.name() bistats['x'] = xname bistats['y'] = yname bistats['r'] = uf.roundNumber(np.corrcoef(xvalues, yvalues)[1][0]) fit, residuals, rank, singular_values, rcond = np.polyfit(xvalues, yvalues, 1, None, True, None, False) bistats['slope'] = fit[0] bistats['intercept'] = fit[1] bistats['r2'] = uf.roundNumber((1 - residuals[0] / (len(yvalues) * np.var(yvalues)))) # fixme: pvalue calc not correct bistats['p'] = 0 if bistats['slope'] > 0: bistats['line'] = "%s + %s * X" % (uf.roundNumber(bistats['intercept']), uf.roundNumber(bistats['slope'])) else: bistats['line'] = "%s - %s * X" % ( uf.roundNumber(abs(bistats['intercept'])), uf.roundNumber(bistats['slope'])) self.bivariate_statistics.append(bistats)
def test_roundNumber_wrong_type(self): self.assertRaises(TypeError, lambda: uf.roundNumber("0.002000"))