1 from __future__ import division
2
3 import numpy as num
4 from _combine import combine as _comb
5 import operator as _operator
6
7
8 -def _combine_f(funcstr, arrays, output=None, outtype=None, nlow=0, nhigh=0, badmasks=None):
9 arrays = [ num.asarray(a) for a in arrays ]
10 shape = arrays[0].shape
11 if output is None:
12 if outtype is not None:
13 out = arrays[0].astype(outtype)
14 else:
15 out = arrays[0].copy()
16 else:
17 out = output
18 for a in tuple(arrays[1:])+(out,):
19 if a.shape != shape:
20 raise ValueError("all arrays must have identical shapes")
21 _comb(arrays, out, nlow, nhigh, badmasks, funcstr)
22 if output is None:
23 return out
24
74
75 -def average( arrays, output=None, outtype=None, nlow=0, nhigh=0, badmasks=None):
76 """average() nominally computes the average pixel value for a stack of
77 identically shaped images.
78
79 arrays specifies a sequence of inputs arrays, which are nominally a
80 stack of identically shaped images.
81
82 output may be used to specify the output array. If none is specified,
83 either arrays[0] is copied or a new array of type 'outtype'
84 is created.
85
86 outtype specifies the type of the output array when no 'output' is
87 specified.
88
89 nlow specifies the number of pixels to be excluded from average
90 on the low end of the pixel stack.
91
92 nhigh specifies the number of pixels to be excluded from average
93 on the high end of the pixel stack.
94
95 badmasks specifies boolean arrays corresponding to 'arrays', where true
96 indicates that a particular pixel is not to be included in the
97 average calculation.
98
99 >>> a = num.arange(4)
100 >>> a = a.reshape((2,2))
101 >>> arrays = [a*16, a*4, a*2, a*8]
102 >>> average(arrays)
103 array([[ 0, 7],
104 [15, 22]])
105 >>> average(arrays, nhigh=1)
106 array([[ 0, 4],
107 [ 9, 14]])
108 >>> average(arrays, nlow=1)
109 array([[ 0, 9],
110 [18, 28]])
111 >>> average(arrays, outtype=num.float32)
112 array([[ 0. , 7.5],
113 [ 15. , 22.5]], dtype=float32)
114 >>> bm = num.zeros((4,2,2), dtype=num.bool8)
115 >>> bm[2,...] = 1
116 >>> average(arrays, badmasks=bm)
117 array([[ 0, 9],
118 [18, 28]])
119 >>> average(arrays, badmasks=threshhold(arrays, high=25))
120 array([[ 0, 7],
121 [ 9, 14]])
122
123 """
124 return _combine_f("average", arrays, output, outtype, nlow, nhigh, badmasks)
125
126 -def minimum( arrays, output=None, outtype=None, nlow=0, nhigh=0, badmasks=None):
127 """minimum() nominally computes the minimum pixel value for a stack of
128 identically shaped images.
129
130 arrays specifies a sequence of inputs arrays, which are nominally a
131 stack of identically shaped images.
132
133 output may be used to specify the output array. If none is specified,
134 either arrays[0] is copied or a new array of type 'outtype'
135 is created.
136
137 outtype specifies the type of the output array when no 'output' is
138 specified.
139
140 nlow specifies the number of pixels to be excluded from minimum
141 on the low end of the pixel stack.
142
143 nhigh specifies the number of pixels to be excluded from minimum
144 on the high end of the pixel stack.
145
146 badmasks specifies boolean arrays corresponding to 'arrays', where true
147 indicates that a particular pixel is not to be included in the
148 minimum calculation.
149
150 >>> a = num.arange(4)
151 >>> a = a.reshape((2,2))
152 >>> arrays = [a*16, a*4, a*2, a*8]
153 >>> minimum(arrays)
154 array([[0, 2],
155 [4, 6]])
156 >>> minimum(arrays, nhigh=1)
157 array([[0, 2],
158 [4, 6]])
159 >>> minimum(arrays, nlow=1)
160 array([[ 0, 4],
161 [ 8, 12]])
162 >>> minimum(arrays, outtype=num.float32)
163 array([[ 0., 2.],
164 [ 4., 6.]], dtype=float32)
165 >>> bm = num.zeros((4,2,2), dtype=num.bool8)
166 >>> bm[2,...] = 1
167 >>> minimum(arrays, badmasks=bm)
168 array([[ 0, 4],
169 [ 8, 12]])
170 >>> minimum(arrays, badmasks=threshhold(arrays, low=10))
171 array([[ 0, 16],
172 [16, 12]])
173
174 """
175 return _combine_f("minimum", arrays, output, outtype, nlow, nhigh, badmasks)
176
177 -def threshhold(arrays, low=None, high=None, outputs=None):
178 """threshhold() computes a boolean array 'outputs' with
179 corresponding elements for each element of arrays. The
180 boolean value is true where each of the arrays values
181 is < the low or >= the high threshholds.
182
183 >>> a=num.arange(100)
184 >>> a=a.reshape((10,10))
185 >>> (threshhold(a, 1, 50)).astype(num.int8)
186 array([[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
187 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
188 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
189 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
190 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
191 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
192 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
193 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
194 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
195 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], dtype=int8)
196 >>> (threshhold([ range(10)]*10, 3, 7)).astype(num.int8)
197 array([[1, 1, 1, 0, 0, 0, 0, 1, 1, 1],
198 [1, 1, 1, 0, 0, 0, 0, 1, 1, 1],
199 [1, 1, 1, 0, 0, 0, 0, 1, 1, 1],
200 [1, 1, 1, 0, 0, 0, 0, 1, 1, 1],
201 [1, 1, 1, 0, 0, 0, 0, 1, 1, 1],
202 [1, 1, 1, 0, 0, 0, 0, 1, 1, 1],
203 [1, 1, 1, 0, 0, 0, 0, 1, 1, 1],
204 [1, 1, 1, 0, 0, 0, 0, 1, 1, 1],
205 [1, 1, 1, 0, 0, 0, 0, 1, 1, 1],
206 [1, 1, 1, 0, 0, 0, 0, 1, 1, 1]], dtype=int8)
207 >>> (threshhold(a, high=50)).astype(num.int8)
208 array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
209 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
210 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
211 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
212 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
213 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
214 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
215 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
216 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
217 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], dtype=int8)
218 >>> (threshhold(a, low=50)).astype(num.int8)
219 array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
220 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
221 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
222 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
223 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
224 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
225 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
226 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
227 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
228 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int8)
229
230 """
231
232 if not isinstance(arrays[0], num.ndarray):
233 return threshhold( num.asarray(arrays), low, high, outputs)
234
235 if outputs is None:
236 outs = num.zeros(shape=(len(arrays),)+arrays[0].shape,
237 dtype=num.bool8)
238 else:
239 outs = outputs
240
241 for i in range(len(arrays)):
242 a, out = arrays[i], outs[i]
243 out[:] = 0
244
245 if high is not None:
246 num.greater_equal(a, high, out)
247 if low is not None:
248 num.logical_or(out, a < low, out)
249 else:
250 if low is not None:
251 num.less(a, low, out)
252
253 if outputs is None:
254 return outs
255
257 """time a 10**6 element median"""
258 import time
259 a = num.arange(10**6)
260 a = a.reshape((1000, 1000))
261 arrays = [a*2, a*64, a*16, a*8]
262 t0 = time.clock()
263 median(arrays)
264 print "maskless:", time.clock()-t0
265
266 a = num.arange(10**6)
267 a = a.reshape((1000, 1000))
268 arrays = [a*2, a*64, a*16, a*8]
269 t0 = time.clock()
270 median(arrays, badmasks=num.zeros((1000,1000), dtype=num.bool8))
271 print "masked:", time.clock()-t0
272