Skip to content

Instantly share code, notes, and snippets.

@ScottWales
Created May 29, 2017 02:33
Show Gist options
  • Save ScottWales/b7e724155da2b23e816c1f8c8c116ea3 to your computer and use it in GitHub Desktop.
Save ScottWales/b7e724155da2b23e816c1f8c8c116ea3 to your computer and use it in GitHub Desktop.
import sys
import mule
import numpy.ma as ma
class RemoveMissing(mule.DataOperator):
"""
Remove missing data from a field, setting to average value
"""
def __init__(self):
pass
def new_field(self, source_field):
return source_field.copy()
def transform(self, source_field, result_field):
data = source_field.get_data()
masked = ma.masked_values(data, source_field.bmdi)
return masked.filled(masked.mean())
def validate(filename=None, warn=False):
pass
rem_missing = RemoveMissing()
data = mule.load_umfile(sys.argv[1])
for i,f in enumerate(data.fields):
if f.lbuser4 == 49:
data.fields[i] = rem_missing(f)
data.validate = validate
data.to_file(sys.argv[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment