2 windowed_filter(array, winfunc, s)
7 Command line bindings for filtering 2 column ACSII data.
12 def windowed_mean_point(array, i, width=50) :
14 Filter data with a windowed average (mean).
15 The ith point is replaced by the average of the points with indices in
16 the range i +/- width (inclusive).
18 This function returns the ith point in the filtered array,
19 given the raw array and window half-width.
21 The weights on the average are uniform at the moment.
25 for j in range(i-width, i+width) :
26 if j >= 0 and j < len(array) :
32 def windowed_median_point(array, i, width=9) :
34 Filter data with a windowed median.
35 The ith point is replaced by the median of the points with indices in
36 the range i +/- width (inclusive).
38 This function returns the ith point in the filtered array,
39 given the raw array and window half-width.
42 if imin < 0 : imin = 0
44 if imax >= len(array) : imax = len(array-1)
45 slice = array[imin:imax+1].copy()
47 imid = numpy.floor((imax-imin)/2)
50 def windowed_filter(array, winfunc, width=None) :
52 Filter data with a windowing function winfunc.
53 The ith point is replaced by the winfunc(array, i, s).
55 See the windowed_* functions for possible winfunc options.
59 win = lambda i : winfunc(array, i) # user winfunc's default s
61 win = lambda i : winfunc(array, i, width)
62 for i in range(len(out)) :
67 # commandline interface functions
72 ifile can be a filename string or open (seekable) file object.
73 returns (column 1 array, column 2 array)
75 if ifile == None : ifile = sys.stdin
76 data=scipy.io.read_array(ifile)
77 return (data[:,0], data[:,1])
79 def write_data(ofile, x, y):
81 ofile can be a filename string or open (seekable) file object.
83 data = numpy.zeros((len(x),2))
86 if ofile == None : ofile = sys.stdout
87 scipy.io.write_array(ofile, data, separator='\t')
89 if __name__ == '__main__' :
90 # command line interface
91 from optparse import OptionParser
93 usage_string = ('%prog <input-file>\n'
94 '2008, W. Trevor King.\n'
96 'Apply various filters to data y(x).'
97 '<input-file> should be whitespace-delimited, 2 column ASCII\n'
98 'without a header line.\n'
99 'e.g: "<x>\\t<y>\\n"')
100 parser = OptionParser(usage=usage_string, version='%prog 0.1')
101 parser.add_option('-o', '--output-file', dest='ofilename',
102 help='write output to FILE (default stdout)',
103 type='string', metavar='FILE')
104 parser.add_option('-w', '--width', dest='width', default=None,
105 help='window width (i +/- width, inclusive)',
106 type='int', metavar='WIDTH')
107 parser.add_option('-t', '--type', dest='type', default='mean',
108 help='filter type (default %default)',
109 type='string', metavar='TYPE')
110 parser.add_option('-v', '--verbose', dest='verbose', action='store_true',
111 help='Print lots of debugging information',
114 options,args = parser.parse_args()
116 assert len(args) >= 1, "Need an input file"
120 x,y = read_data(ifilename)
122 if options.type == 'mean' :
123 winfunc = windowed_mean_point
124 elif options.type == 'median' :
125 winfunc = windowed_median_point
127 raise Exception, "unrecognized window type '%s'" % options.type
129 y = windowed_filter(y, winfunc, width=options.width)
131 write_data(options.ofilename, x, y)