Calculate a mean value from a field
I saw a few nice posts over on another blog that I thought would be fun to re-implement using pygp, in this particular case, “Calculate a mean value from a field”. Modification are 1) checking the field type to ensure it is numeric (re-use of some validation and enumerations) 2) checking that the field exists, and 3) filtering out any None values (but keeping 0′s). This code runs on ArcGIS 9.2 to 10.1 beta.
# -*- coding: ascii -*-
"""
Calculate a mean value from a field
"""
__author__ = 'Jason Humber - Integrated Informatics Inc.'
__maintainer__ = '$LastChangedBy$'
__vcs_id__ = '$HeadURL$'
from numpy.ma.core import mean
from pygp.core.field import NUMBERS
__copyright__ = 'Copyright (c) 2012, Integrated Informatics Inc.'
__license__ = 'LGPLv3'
__version__ = '0.1.0'
__email__ = 'gis@integrated-informatics.com'
def calculate_mean_value(table, field):
"""
Calculate the Mean Value from a field
:param table: Table or Feature Class
:type table: pygp.dataelement.extended.Table
:param field: Field Object
:type field: pygp.core.field.Field
:return: Mean value
:rtype: float
"""
if field.type_ in NUMBERS and field in table.fields:
return mean([v for v, in table.as_list(field) if v is not None])
# End calculate_mean_value function
if __name__ == '__main__':
pass