1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 import math
32 import numpy
33 import numpy as np
34 from numpy import fft
35 from numpy.testing import assert_, assert_equal, assert_array_equal, \
36 TestCase, run_module_suite, \
37 assert_array_almost_equal, assert_almost_equal
38 import stsci.ndimage as ndimage
39
40 eps = 1e-12
41
43 return math.sqrt(((a - b)**2).sum())
44
46
48
49 self.types = [numpy.int8, numpy.uint8, numpy.int16,
50 numpy.uint16, numpy.int32, numpy.uint32,
51 numpy.int64, numpy.uint64,
52 numpy.float32, numpy.float64]
53
54
55 self.modes = ['nearest', 'wrap', 'reflect', 'mirror', 'constant']
56
58 "correlation 1"
59 array = numpy.array([1, 2])
60 weights = numpy.array([2])
61 expected = [2, 4]
62
63 output = ndimage.correlate(array, weights)
64 assert_array_almost_equal(output, expected)
65
66 output = ndimage.convolve(array, weights)
67 assert_array_almost_equal(output, expected)
68
69 output = ndimage.correlate1d(array, weights)
70 assert_array_almost_equal(output, expected)
71
72 output = ndimage.convolve1d(array, weights)
73 assert_array_almost_equal(output, expected)
74
76 "correlation 2"
77 array = numpy.array([1, 2, 3])
78 kernel = numpy.array([1])
79
80 output = ndimage.correlate(array, kernel)
81 assert_array_almost_equal(array, output)
82
83 output = ndimage.convolve(array, kernel)
84 assert_array_almost_equal(array, output)
85
86 output = ndimage.correlate1d(array, kernel)
87 assert_array_almost_equal(array, output)
88
89 output = ndimage.convolve1d(array, kernel)
90 assert_array_almost_equal(array, output)
91
93 "correlation 3"
94 array = numpy.array([1])
95 weights = numpy.array([1, 1])
96 expected = [2]
97
98 output = ndimage.correlate(array, weights)
99 assert_array_almost_equal(output, expected)
100
101 output = ndimage.convolve(array, weights)
102 assert_array_almost_equal(output, expected)
103
104 output = ndimage.correlate1d(array, weights)
105 assert_array_almost_equal(output, expected)
106
107 output = ndimage.convolve1d(array, weights)
108 assert_array_almost_equal(output, expected)
109
111 "correlation 4"
112 array = numpy.array([1, 2])
113 tcor = [2, 3]
114 tcov = [3, 4]
115 weights = numpy.array([1, 1])
116 output = ndimage.correlate(array, weights)
117 assert_array_almost_equal(output, tcor)
118 output = ndimage.convolve(array, weights)
119 assert_array_almost_equal(output, tcov)
120 output = ndimage.correlate1d(array, weights)
121 assert_array_almost_equal(output, tcor)
122 output = ndimage.convolve1d(array, weights)
123 assert_array_almost_equal(output, tcov)
124
126 "correlation 5"
127 array = numpy.array([1, 2, 3])
128 tcor = [2, 3, 5]
129 tcov = [3, 5, 6]
130 kernel = numpy.array([1, 1])
131 output = ndimage.correlate(array, kernel)
132 assert_array_almost_equal(tcor, output)
133 output = ndimage.convolve(array, kernel)
134 assert_array_almost_equal(tcov, output)
135 output = ndimage.correlate1d(array, kernel)
136 assert_array_almost_equal(tcor, output)
137 output = ndimage.convolve1d(array, kernel)
138 assert_array_almost_equal(tcov, output)
139
141 "correlation 6"
142 array = numpy.array([1, 2, 3])
143 tcor = [9, 14, 17]
144 tcov = [7, 10, 15]
145 weights = numpy.array([1, 2, 3])
146 output = ndimage.correlate(array, weights)
147 assert_array_almost_equal(output, tcor)
148 output = ndimage.convolve(array, weights)
149 assert_array_almost_equal(output, tcov)
150 output = ndimage.correlate1d(array, weights)
151 assert_array_almost_equal(output, tcor)
152 output = ndimage.convolve1d(array, weights)
153 assert_array_almost_equal(output, tcov)
154
156 "correlation 7"
157 array = numpy.array([1, 2, 3])
158 expected = [5, 8, 11]
159 weights = numpy.array([1, 2, 1])
160 output = ndimage.correlate(array, weights)
161 assert_array_almost_equal(output, expected)
162 output = ndimage.convolve(array, weights)
163 assert_array_almost_equal(output, expected)
164 output = ndimage.correlate1d(array, weights)
165 assert_array_almost_equal(output, expected)
166 output = ndimage.convolve1d(array, weights)
167 assert_array_almost_equal(output, expected)
168
170 "correlation 8"
171 array = numpy.array([1, 2, 3])
172 tcor = [1, 2, 5]
173 tcov = [3, 6, 7]
174 weights = numpy.array([1, 2, -1])
175 output = ndimage.correlate(array, weights)
176 assert_array_almost_equal(output, tcor)
177 output = ndimage.convolve(array, weights)
178 assert_array_almost_equal(output, tcov)
179 output = ndimage.correlate1d(array, weights)
180 assert_array_almost_equal(output, tcor)
181 output = ndimage.convolve1d(array, weights)
182 assert_array_almost_equal(output, tcov)
183
185 "correlation 9"
186 array = []
187 kernel = numpy.array([1, 1])
188 output = ndimage.correlate(array, kernel)
189 assert_array_almost_equal(array, output)
190 output = ndimage.convolve(array, kernel)
191 assert_array_almost_equal(array, output)
192 output = ndimage.correlate1d(array, kernel)
193 assert_array_almost_equal(array, output)
194 output = ndimage.convolve1d(array, kernel)
195 assert_array_almost_equal(array, output)
196
198 "correlation 10"
199 array = [[]]
200 kernel = numpy.array([[1, 1]])
201 output = ndimage.correlate(array, kernel)
202 assert_array_almost_equal(array, output)
203 output = ndimage.convolve(array, kernel)
204 assert_array_almost_equal(array, output)
205
207 "correlation 11"
208 array = numpy.array([[1, 2, 3],
209 [4, 5, 6]])
210 kernel = numpy.array([[1, 1],
211 [1, 1]])
212 output = ndimage.correlate(array, kernel)
213 assert_array_almost_equal([[4, 6, 10], [10, 12, 16]], output)
214 output = ndimage.convolve(array, kernel)
215 assert_array_almost_equal([[12, 16, 18], [18, 22, 24]], output)
216
218 "correlation 12"
219 array = numpy.array([[1, 2, 3],
220 [4, 5, 6]])
221 kernel = numpy.array([[1, 0],
222 [0, 1]])
223 output = ndimage.correlate(array, kernel)
224 assert_array_almost_equal([[2, 3, 5], [5, 6, 8]], output)
225 output = ndimage.convolve(array, kernel)
226 assert_array_almost_equal([[6, 8, 9], [9, 11, 12]], output)
227
229 "correlation 13"
230 kernel = numpy.array([[1, 0],
231 [0, 1]])
232 for type1 in self.types:
233 array = numpy.array([[1, 2, 3],
234 [4, 5, 6]], type1)
235 for type2 in self.types:
236 output = ndimage.correlate(array, kernel,
237 output=type2)
238 assert_array_almost_equal([[2, 3, 5], [5, 6, 8]], output)
239 assert_equal(output.dtype.type, type2)
240
241 output = ndimage.convolve(array, kernel,
242 output=type2)
243 assert_array_almost_equal([[6, 8, 9], [9, 11, 12]], output)
244 assert_equal(output.dtype.type, type2)
245
247 "correlation 14"
248 kernel = numpy.array([[1, 0],
249 [0, 1]])
250 for type1 in self.types:
251 array = numpy.array([[1, 2, 3],
252 [4, 5, 6]], type1)
253 for type2 in self.types:
254 output = numpy.zeros(array.shape, type2)
255 ndimage.correlate(array, kernel,
256 output=output)
257 assert_array_almost_equal([[2, 3, 5], [5, 6, 8]], output)
258 assert_equal(output.dtype.type, type2)
259
260 ndimage.convolve(array, kernel, output=output)
261 assert_array_almost_equal([[6, 8, 9], [9, 11, 12]], output)
262 assert_equal(output.dtype.type, type2)
263
265 "correlation 15"
266 kernel = numpy.array([[1, 0],
267 [0, 1]])
268 for type1 in self.types:
269 array = numpy.array([[1, 2, 3],
270 [4, 5, 6]], type1)
271 output = ndimage.correlate(array, kernel,
272 output=numpy.float32)
273 assert_array_almost_equal([[2, 3, 5], [5, 6, 8]], output)
274 assert_equal(output.dtype.type, numpy.float32)
275
276 output = ndimage.convolve(array, kernel,
277 output=numpy.float32)
278 assert_array_almost_equal([[6, 8, 9], [9, 11, 12]], output)
279 assert_equal(output.dtype.type, numpy.float32)
280
282 "correlation 16"
283 kernel = numpy.array([[0.5, 0 ],
284 [0, 0.5]])
285 for type1 in self.types:
286 array = numpy.array([[1, 2, 3],
287 [4, 5, 6]], type1)
288 output = ndimage.correlate(array, kernel,
289 output=numpy.float32)
290 assert_array_almost_equal([[1, 1.5, 2.5], [2.5, 3, 4]], output)
291 assert_equal(output.dtype.type, numpy.float32)
292
293 output = ndimage.convolve(array, kernel,
294 output=numpy.float32)
295 assert_array_almost_equal([[3, 4, 4.5], [4.5, 5.5, 6]], output)
296 assert_equal(output.dtype.type, numpy.float32)
297
299 "correlation 17"
300 array = numpy.array([1, 2, 3])
301 tcor = [3, 5, 6]
302 tcov = [2, 3, 5]
303 kernel = numpy.array([1, 1])
304 output = ndimage.correlate(array, kernel, origin=-1)
305 assert_array_almost_equal(tcor, output)
306 output = ndimage.convolve(array, kernel, origin=-1)
307 assert_array_almost_equal(tcov, output)
308 output = ndimage.correlate1d(array, kernel, origin=-1)
309 assert_array_almost_equal(tcor, output)
310 output = ndimage.convolve1d(array, kernel, origin=-1)
311 assert_array_almost_equal(tcov, output)
312
314 "correlation 18"
315 kernel = numpy.array([[1, 0],
316 [0, 1]])
317 for type1 in self.types:
318 array = numpy.array([[1, 2, 3],
319 [4, 5, 6]], type1)
320 output = ndimage.correlate(array, kernel,
321 output=numpy.float32,
322 mode='nearest', origin=-1)
323 assert_array_almost_equal([[6, 8, 9], [9, 11, 12]], output)
324 assert_equal(output.dtype.type, numpy.float32)
325
326 output = ndimage.convolve(array, kernel,
327 output=numpy.float32,
328 mode='nearest', origin=-1)
329 assert_array_almost_equal([[2, 3, 5], [5, 6, 8]], output)
330 assert_equal(output.dtype.type, numpy.float32)
331
333 "correlation 19"
334 kernel = numpy.array([[1, 0],
335 [0, 1]])
336 for type1 in self.types:
337 array = numpy.array([[1, 2, 3],
338 [4, 5, 6]], type1)
339 output = ndimage.correlate(array, kernel,
340 output=numpy.float32,
341 mode='nearest', origin=[-1, 0])
342 assert_array_almost_equal([[5, 6, 8], [8, 9, 11]], output)
343 assert_equal(output.dtype.type, numpy.float32)
344
345 output = ndimage.convolve(array, kernel,
346 output=numpy.float32,
347 mode='nearest', origin=[-1, 0])
348 assert_array_almost_equal([[3, 5, 6], [6, 8, 9]], output)
349 assert_equal(output.dtype.type, numpy.float32)
350
352 "correlation 20"
353 weights = numpy.array([1, 2, 1])
354 expected = [[5, 10, 15], [7, 14, 21]]
355 for type1 in self.types:
356 array = numpy.array([[1, 2, 3],
357 [2, 4, 6]], type1)
358 for type2 in self.types:
359 output = numpy.zeros((2, 3), type2)
360 ndimage.correlate1d(array, weights, axis=0,
361 output=output)
362 assert_array_almost_equal(output, expected)
363 ndimage.convolve1d(array, weights, axis=0,
364 output=output)
365 assert_array_almost_equal(output, expected)
366
368 "correlation 21"
369 array = numpy.array([[1, 2, 3],
370 [2, 4, 6]])
371 expected = [[5, 10, 15], [7, 14, 21]]
372 weights = numpy.array([1, 2, 1])
373 output = ndimage.correlate1d(array, weights, axis=0)
374 assert_array_almost_equal(output, expected)
375 output = ndimage.convolve1d(array, weights, axis=0)
376 assert_array_almost_equal(output, expected)
377
379 "correlation 22"
380 weights = numpy.array([1, 2, 1])
381 expected = [[6, 12, 18], [6, 12, 18]]
382 for type1 in self.types:
383 array = numpy.array([[1, 2, 3],
384 [2, 4, 6]], type1)
385 for type2 in self.types:
386 output = numpy.zeros((2, 3), type2)
387 ndimage.correlate1d(array, weights, axis=0,
388 mode='wrap', output=output)
389 assert_array_almost_equal(output, expected)
390 ndimage.convolve1d(array, weights, axis=0,
391 mode='wrap', output=output)
392 assert_array_almost_equal(output, expected)
393
395 "correlation 23"
396 weights = numpy.array([1, 2, 1])
397 expected = [[5, 10, 15], [7, 14, 21]]
398 for type1 in self.types:
399 array = numpy.array([[1, 2, 3],
400 [2, 4, 6]], type1)
401 for type2 in self.types:
402 output = numpy.zeros((2, 3), type2)
403 ndimage.correlate1d(array, weights, axis=0,
404 mode='nearest', output=output)
405 assert_array_almost_equal(output, expected)
406 ndimage.convolve1d(array, weights, axis=0,
407 mode='nearest', output=output)
408 assert_array_almost_equal(output, expected)
409
411 "correlation 24"
412 weights = numpy.array([1, 2, 1])
413 tcor = [[7, 14, 21], [8, 16, 24]]
414 tcov = [[4, 8, 12], [5, 10, 15]]
415 for type1 in self.types:
416 array = numpy.array([[1, 2, 3],
417 [2, 4, 6]], type1)
418 for type2 in self.types:
419 output = numpy.zeros((2, 3), type2)
420 ndimage.correlate1d(array, weights, axis=0,
421 mode='nearest', output=output, origin=-1)
422 assert_array_almost_equal(output, tcor)
423 ndimage.convolve1d(array, weights, axis=0,
424 mode='nearest', output=output, origin=-1)
425 assert_array_almost_equal(output, tcov)
426
428 "correlation 25"
429 weights = numpy.array([1, 2, 1])
430 tcor = [[4, 8, 12], [5, 10, 15]]
431 tcov = [[7, 14, 21], [8, 16, 24]]
432 for type1 in self.types:
433 array = numpy.array([[1, 2, 3],
434 [2, 4, 6]], type1)
435 for type2 in self.types:
436 output = numpy.zeros((2, 3), type2)
437 ndimage.correlate1d(array, weights, axis=0,
438 mode='nearest', output=output, origin=1)
439 assert_array_almost_equal(output, tcor)
440 ndimage.convolve1d(array, weights, axis=0,
441 mode='nearest', output=output, origin=1)
442 assert_array_almost_equal(output, tcov)
443
445 "gaussian filter 1"
446 input = numpy.array([[1, 2, 3],
447 [2, 4, 6]], numpy.float32)
448 output = ndimage.gaussian_filter(input, 0)
449 assert_array_almost_equal(output, input)
450
452 "gaussian filter 2"
453 input = numpy.array([[1, 2, 3],
454 [2, 4, 6]], numpy.float32)
455 output = ndimage.gaussian_filter(input, 1.0)
456 assert_equal(input.dtype, output.dtype)
457 assert_equal(input.shape, output.shape)
458
460 "gaussian filter 3 - single precision data"
461 input = numpy.arange(100 * 100).astype(numpy.float32)
462 input.shape = (100, 100)
463 output = ndimage.gaussian_filter(input, [1.0, 1.0])
464
465 assert_equal(input.dtype, output.dtype)
466 assert_equal(input.shape, output.shape)
467
468
469
470 assert_almost_equal(output.sum(dtype='d'), input.sum(dtype='d'), decimal=0)
471 assert_(sumsq(input, output) > 1.0)
472
474 "gaussian filter 4"
475 input = numpy.arange(100 * 100).astype(numpy.float32)
476 input.shape = (100, 100)
477 otype = numpy.float64
478 output = ndimage.gaussian_filter(input, [1.0, 1.0],
479 output=otype)
480 assert_equal(output.dtype.type, numpy.float64)
481 assert_equal(input.shape, output.shape)
482 assert_(sumsq(input, output) > 1.0)
483
485 "gaussian filter 5"
486 input = numpy.arange(100 * 100).astype(numpy.float32)
487 input.shape = (100, 100)
488 otype = numpy.float64
489 output = ndimage.gaussian_filter(input, [1.0, 1.0],
490 order=1, output=otype)
491 assert_equal(output.dtype.type, numpy.float64)
492 assert_equal(input.shape, output.shape)
493 assert_(sumsq(input, output) > 1.0)
494
496 "gaussian filter 6"
497 input = numpy.arange(100 * 100).astype(numpy.float32)
498 input.shape = (100, 100)
499 otype = numpy.float64
500 output1 = ndimage.gaussian_filter(input, [1.0, 1.0],
501 output=otype)
502 output2 = ndimage.gaussian_filter(input, 1.0,
503 output=otype)
504 assert_array_almost_equal(output1, output2)
505
507 "prewitt filter 1"
508 for type in self.types:
509 array = numpy.array([[3, 2, 5, 1, 4],
510 [5, 8, 3, 7, 1],
511 [5, 6, 9, 3, 5]], type)
512 t = ndimage.correlate1d(array, [-1.0, 0.0, 1.0], 0)
513 t = ndimage.correlate1d(t, [1.0, 1.0, 1.0], 1)
514 output = ndimage.prewitt(array, 0)
515 assert_array_almost_equal(t, output)
516
517
519 "prewitt filter 2"
520 for type in self.types:
521 array = numpy.array([[3, 2, 5, 1, 4],
522 [5, 8, 3, 7, 1],
523 [5, 6, 9, 3, 5]], type)
524 t = ndimage.correlate1d(array, [-1.0, 0.0, 1.0], 0)
525 t = ndimage.correlate1d(t, [1.0, 1.0, 1.0], 1)
526 output = numpy.zeros(array.shape, type)
527 ndimage.prewitt(array, 0, output)
528 assert_array_almost_equal(t, output)
529
531 "prewitt filter 3"
532 for type in self.types:
533 array = numpy.array([[3, 2, 5, 1, 4],
534 [5, 8, 3, 7, 1],
535 [5, 6, 9, 3, 5]], type)
536 t = ndimage.correlate1d(array, [-1.0, 0.0, 1.0], 1)
537 t = ndimage.correlate1d(t, [1.0, 1.0, 1.0], 0)
538 output = ndimage.prewitt(array, 1)
539 assert_array_almost_equal(t, output)
540
542 "prewitt filter 4"
543 for type in self.types:
544 array = numpy.array([[3, 2, 5, 1, 4],
545 [5, 8, 3, 7, 1],
546 [5, 6, 9, 3, 5]], type)
547 t = ndimage.prewitt(array, -1)
548 output = ndimage.prewitt(array, 1)
549 assert_array_almost_equal(t, output)
550
552 "sobel filter 1"
553 for type in self.types:
554 array = numpy.array([[3, 2, 5, 1, 4],
555 [5, 8, 3, 7, 1],
556 [5, 6, 9, 3, 5]], type)
557 t = ndimage.correlate1d(array, [-1.0, 0.0, 1.0], 0)
558 t = ndimage.correlate1d(t, [1.0, 2.0, 1.0], 1)
559 output = ndimage.sobel(array, 0)
560 assert_array_almost_equal(t, output)
561
563 "sobel filter 2"
564 for type in self.types:
565 array = numpy.array([[3, 2, 5, 1, 4],
566 [5, 8, 3, 7, 1],
567 [5, 6, 9, 3, 5]], type)
568 t = ndimage.correlate1d(array, [-1.0, 0.0, 1.0], 0)
569 t = ndimage.correlate1d(t, [1.0, 2.0, 1.0], 1)
570 output = numpy.zeros(array.shape, type)
571 ndimage.sobel(array, 0, output)
572 assert_array_almost_equal(t, output)
573
575 "sobel filter 3"
576 for type in self.types:
577 array = numpy.array([[3, 2, 5, 1, 4],
578 [5, 8, 3, 7, 1],
579 [5, 6, 9, 3, 5]], type)
580 t = ndimage.correlate1d(array, [-1.0, 0.0, 1.0], 1)
581 t = ndimage.correlate1d(t, [1.0, 2.0, 1.0], 0)
582 output = numpy.zeros(array.shape, type)
583 output = ndimage.sobel(array, 1)
584 assert_array_almost_equal(t, output)
585
587 "sobel filter 4"
588 for type in self.types:
589 array = numpy.array([[3, 2, 5, 1, 4],
590 [5, 8, 3, 7, 1],
591 [5, 6, 9, 3, 5]], type)
592 t = ndimage.sobel(array, -1)
593 output = ndimage.sobel(array, 1)
594 assert_array_almost_equal(t, output)
595
597 "laplace filter 1"
598 for type in [numpy.int32, numpy.float32, numpy.float64]:
599 array = numpy.array([[3, 2, 5, 1, 4],
600 [5, 8, 3, 7, 1],
601 [5, 6, 9, 3, 5]], type) * 100
602 tmp1 = ndimage.correlate1d(array, [1, -2, 1], 0)
603 tmp2 = ndimage.correlate1d(array, [1, -2, 1], 1)
604 output = ndimage.laplace(array)
605 assert_array_almost_equal(tmp1 + tmp2, output)
606
608 "laplace filter 2"
609 for type in [numpy.int32, numpy.float32, numpy.float64]:
610 array = numpy.array([[3, 2, 5, 1, 4],
611 [5, 8, 3, 7, 1],
612 [5, 6, 9, 3, 5]], type) * 100
613 tmp1 = ndimage.correlate1d(array, [1, -2, 1], 0)
614 tmp2 = ndimage.correlate1d(array, [1, -2, 1], 1)
615 output = numpy.zeros(array.shape, type)
616 ndimage.laplace(array, output=output)
617 assert_array_almost_equal(tmp1 + tmp2, output)
618
620 "gaussian laplace filter 1"
621 for type in [numpy.int32, numpy.float32, numpy.float64]:
622 array = numpy.array([[3, 2, 5, 1, 4],
623 [5, 8, 3, 7, 1],
624 [5, 6, 9, 3, 5]], type) * 100
625 tmp1 = ndimage.gaussian_filter(array, 1.0, [2, 0])
626 tmp2 = ndimage.gaussian_filter(array, 1.0, [0, 2])
627 output = ndimage.gaussian_laplace(array, 1.0)
628 assert_array_almost_equal(tmp1 + tmp2, output)
629
631 "gaussian laplace filter 2"
632 for type in [numpy.int32, numpy.float32, numpy.float64]:
633 array = numpy.array([[3, 2, 5, 1, 4],
634 [5, 8, 3, 7, 1],
635 [5, 6, 9, 3, 5]], type) * 100
636 tmp1 = ndimage.gaussian_filter(array, 1.0, [2, 0])
637 tmp2 = ndimage.gaussian_filter(array, 1.0, [0, 2])
638 output = numpy.zeros(array.shape, type)
639 ndimage.gaussian_laplace(array, 1.0, output)
640 assert_array_almost_equal(tmp1 + tmp2, output)
641
643 "generic laplace filter 1"
644 def derivative2(input, axis, output, mode, cval, a, b):
645 sigma = [a, b / 2.0]
646 input = numpy.asarray(input)
647 order = [0] * input.ndim
648 order[axis] = 2
649 return ndimage.gaussian_filter(input, sigma, order,
650 output, mode, cval)
651 for type in self.types:
652 array = numpy.array([[3, 2, 5, 1, 4],
653 [5, 8, 3, 7, 1],
654 [5, 6, 9, 3, 5]], type)
655 output = numpy.zeros(array.shape, type)
656 tmp = ndimage.generic_laplace(array, derivative2,
657 extra_arguments=(1.0,), extra_keywords={'b': 2.0})
658 ndimage.gaussian_laplace(array, 1.0, output)
659 assert_array_almost_equal(tmp, output)
660
662 "gaussian gradient magnitude filter 1"
663 for type in [numpy.int32, numpy.float32, numpy.float64]:
664 array = numpy.array([[3, 2, 5, 1, 4],
665 [5, 8, 3, 7, 1],
666 [5, 6, 9, 3, 5]], type) * 100
667 tmp1 = ndimage.gaussian_filter(array, 1.0, [1, 0])
668 tmp2 = ndimage.gaussian_filter(array, 1.0, [0, 1])
669 output = ndimage.gaussian_gradient_magnitude(array,
670 1.0)
671 expected = tmp1 * tmp1 + tmp2 * tmp2
672 numpy.sqrt(expected, expected)
673 assert_array_almost_equal(expected, output)
674
676 "gaussian gradient magnitude filter 2"
677 for type in [numpy.int32, numpy.float32, numpy.float64]:
678 array = numpy.array([[3, 2, 5, 1, 4],
679 [5, 8, 3, 7, 1],
680 [5, 6, 9, 3, 5]], type) * 100
681 tmp1 = ndimage.gaussian_filter(array, 1.0, [1, 0])
682 tmp2 = ndimage.gaussian_filter(array, 1.0, [0, 1])
683 output = numpy.zeros(array.shape, type)
684 ndimage.gaussian_gradient_magnitude(array, 1.0,
685 output)
686 expected = tmp1 * tmp1 + tmp2 * tmp2
687 numpy.sqrt(expected, expected)
688 assert_array_almost_equal(expected, output)
689
691 "generic gradient magnitude 1"
692 array = numpy.array([[3, 2, 5, 1, 4],
693 [5, 8, 3, 7, 1],
694 [5, 6, 9, 3, 5]], numpy.float64)
695 def derivative(input, axis, output, mode, cval, a, b):
696 sigma = [a, b / 2.0]
697 input = numpy.asarray(input)
698 order = [0] * input.ndim
699 order[axis] = 1
700 return ndimage.gaussian_filter(input, sigma, order,
701 output, mode, cval)
702 tmp1 = ndimage.gaussian_gradient_magnitude(array, 1.0)
703 tmp2 = ndimage.generic_gradient_magnitude(array,
704 derivative, extra_arguments=(1.0,),
705 extra_keywords={'b': 2.0})
706 assert_array_almost_equal(tmp1, tmp2)
707
715
722
729
736
743
755
757 "minimum filter 1"
758 array = numpy.array([1, 2, 3, 4, 5])
759 filter_shape = numpy.array([2])
760 output = ndimage.minimum_filter(array, filter_shape)
761 assert_array_almost_equal([1, 1, 2, 3, 4], output)
762
764 "minimum filter 2"
765 array = numpy.array([1, 2, 3, 4, 5])
766 filter_shape = numpy.array([3])
767 output = ndimage.minimum_filter(array, filter_shape)
768 assert_array_almost_equal([1, 1, 2, 3, 4], output)
769
771 "minimum filter 3"
772 array = numpy.array([3, 2, 5, 1, 4])
773 filter_shape = numpy.array([2])
774 output = ndimage.minimum_filter(array, filter_shape)
775 assert_array_almost_equal([3, 2, 2, 1, 1], output)
776
778 "minimum filter 4"
779 array = numpy.array([3, 2, 5, 1, 4])
780 filter_shape = numpy.array([3])
781 output = ndimage.minimum_filter(array, filter_shape)
782 assert_array_almost_equal([2, 2, 1, 1, 1], output)
783
785 "minimum filter 5"
786 array = numpy.array([[3, 2, 5, 1, 4],
787 [7, 6, 9, 3, 5],
788 [5, 8, 3, 7, 1]])
789 filter_shape = numpy.array([2, 3])
790 output = ndimage.minimum_filter(array, filter_shape)
791 assert_array_almost_equal([[2, 2, 1, 1, 1],
792 [2, 2, 1, 1, 1],
793 [5, 3, 3, 1, 1]], output)
794
796 "minimum filter 6"
797 array = numpy.array([[3, 2, 5, 1, 4],
798 [7, 6, 9, 3, 5],
799 [5, 8, 3, 7, 1]])
800 footprint = [[1, 1, 1], [1, 1, 1]]
801 output = ndimage.minimum_filter(array,
802 footprint=footprint)
803 assert_array_almost_equal([[2, 2, 1, 1, 1],
804 [2, 2, 1, 1, 1],
805 [5, 3, 3, 1, 1]], output)
806
808 "minimum filter 7"
809 array = numpy.array([[3, 2, 5, 1, 4],
810 [7, 6, 9, 3, 5],
811 [5, 8, 3, 7, 1]])
812 footprint = [[1, 0, 1], [1, 1, 0]]
813 output = ndimage.minimum_filter(array,
814 footprint=footprint)
815 assert_array_almost_equal([[2, 2, 1, 1, 1],
816 [2, 3, 1, 3, 1],
817 [5, 5, 3, 3, 1]], output)
818
820 "minimum filter 8"
821 array = numpy.array([[3, 2, 5, 1, 4],
822 [7, 6, 9, 3, 5],
823 [5, 8, 3, 7, 1]])
824 footprint = [[1, 0, 1], [1, 1, 0]]
825 output = ndimage.minimum_filter(array,
826 footprint=footprint, origin=-1)
827 assert_array_almost_equal([[3, 1, 3, 1, 1],
828 [5, 3, 3, 1, 1],
829 [3, 3, 1, 1, 1]], output)
830
832 "minimum filter 9"
833 array = numpy.array([[3, 2, 5, 1, 4],
834 [7, 6, 9, 3, 5],
835 [5, 8, 3, 7, 1]])
836 footprint = [[1, 0, 1], [1, 1, 0]]
837 output = ndimage.minimum_filter(array,
838 footprint=footprint, origin=[-1, 0])
839 assert_array_almost_equal([[2, 3, 1, 3, 1],
840 [5, 5, 3, 3, 1],
841 [5, 3, 3, 1, 1]], output)
842
844 "maximum filter 1"
845 array = numpy.array([1, 2, 3, 4, 5])
846 filter_shape = numpy.array([2])
847 output = ndimage.maximum_filter(array, filter_shape)
848 assert_array_almost_equal([1, 2, 3, 4, 5], output)
849
851 "maximum filter 2"
852 array = numpy.array([1, 2, 3, 4, 5])
853 filter_shape = numpy.array([3])
854 output = ndimage.maximum_filter(array, filter_shape)
855 assert_array_almost_equal([2, 3, 4, 5, 5], output)
856
858 "maximum filter 3"
859 array = numpy.array([3, 2, 5, 1, 4])
860 filter_shape = numpy.array([2])
861 output = ndimage.maximum_filter(array, filter_shape)
862 assert_array_almost_equal([3, 3, 5, 5, 4], output)
863
865 "maximum filter 4"
866 array = numpy.array([3, 2, 5, 1, 4])
867 filter_shape = numpy.array([3])
868 output = ndimage.maximum_filter(array, filter_shape)
869 assert_array_almost_equal([3, 5, 5, 5, 4], output)
870
872 "maximum filter 5"
873 array = numpy.array([[3, 2, 5, 1, 4],
874 [7, 6, 9, 3, 5],
875 [5, 8, 3, 7, 1]])
876 filter_shape = numpy.array([2, 3])
877 output = ndimage.maximum_filter(array, filter_shape)
878 assert_array_almost_equal([[3, 5, 5, 5, 4],
879 [7, 9, 9, 9, 5],
880 [8, 9, 9, 9, 7]], output)
881
883 "maximum filter 6"
884 array = numpy.array([[3, 2, 5, 1, 4],
885 [7, 6, 9, 3, 5],
886 [5, 8, 3, 7, 1]])
887 footprint = [[1, 1, 1], [1, 1, 1]]
888 output = ndimage.maximum_filter(array,
889 footprint=footprint)
890 assert_array_almost_equal([[3, 5, 5, 5, 4],
891 [7, 9, 9, 9, 5],
892 [8, 9, 9, 9, 7]], output)
893
895 "maximum filter 7"
896 array = numpy.array([[3, 2, 5, 1, 4],
897 [7, 6, 9, 3, 5],
898 [5, 8, 3, 7, 1]])
899 footprint = [[1, 0, 1], [1, 1, 0]]
900 output = ndimage.maximum_filter(array,
901 footprint=footprint)
902 assert_array_almost_equal([[3, 5, 5, 5, 4],
903 [7, 7, 9, 9, 5],
904 [7, 9, 8, 9, 7]], output)
905
907 "maximum filter 8"
908 array = numpy.array([[3, 2, 5, 1, 4],
909 [7, 6, 9, 3, 5],
910 [5, 8, 3, 7, 1]])
911 footprint = [[1, 0, 1], [1, 1, 0]]
912 output = ndimage.maximum_filter(array,
913 footprint=footprint, origin=-1)
914 assert_array_almost_equal([[7, 9, 9, 5, 5],
915 [9, 8, 9, 7, 5],
916 [8, 8, 7, 7, 7]], output)
917
919 "maximum filter 9"
920 array = numpy.array([[3, 2, 5, 1, 4],
921 [7, 6, 9, 3, 5],
922 [5, 8, 3, 7, 1]])
923 footprint = [[1, 0, 1], [1, 1, 0]]
924 output = ndimage.maximum_filter(array,
925 footprint=footprint, origin=[-1, 0])
926 assert_array_almost_equal([[7, 7, 9, 9, 5],
927 [7, 9, 8, 9, 7],
928 [8, 8, 8, 7, 7]], output)
929
931 "rank filter 1"
932 array = numpy.array([1, 2, 3, 4, 5])
933 output = ndimage.rank_filter(array, 1, size=2)
934 assert_array_almost_equal(array, output)
935 output = ndimage.percentile_filter(array, 100, size=2)
936 assert_array_almost_equal(array, output)
937 output = ndimage.median_filter(array, 2)
938 assert_array_almost_equal(array, output)
939
941 "rank filter 2"
942 array = numpy.array([1, 2, 3, 4, 5])
943 output = ndimage.rank_filter(array, 1, size=[3])
944 assert_array_almost_equal(array, output)
945 output = ndimage.percentile_filter(array, 50, size=3)
946 assert_array_almost_equal(array, output)
947 output = ndimage.median_filter(array, (3,))
948 assert_array_almost_equal(array, output)
949
951 "rank filter 3"
952 array = numpy.array([3, 2, 5, 1, 4])
953 output = ndimage.rank_filter(array, 1, size=[2])
954 assert_array_almost_equal([3, 3, 5, 5, 4], output)
955 output = ndimage.percentile_filter(array, 100, size=2)
956 assert_array_almost_equal([3, 3, 5, 5, 4], output)
957
959 "rank filter 4"
960 array = numpy.array([3, 2, 5, 1, 4])
961 expected = [3, 3, 2, 4, 4]
962 output = ndimage.rank_filter(array, 1, size=3)
963 assert_array_almost_equal(expected, output)
964 output = ndimage.percentile_filter(array, 50, size=3)
965 assert_array_almost_equal(expected, output)
966 output = ndimage.median_filter(array, size=3)
967 assert_array_almost_equal(expected, output)
968
970 "rank filter 5"
971 array = numpy.array([3, 2, 5, 1, 4])
972 expected = [3, 3, 2, 4, 4]
973 output = ndimage.rank_filter(array, -2, size=3)
974 assert_array_almost_equal(expected, output)
975
977 "rank filter 6"
978 array = numpy.array([[3, 2, 5, 1, 4],
979 [5, 8, 3, 7, 1],
980 [5, 6, 9, 3, 5]])
981 expected = [[2, 2, 1, 1, 1],
982 [3, 3, 2, 1, 1],
983 [5, 5, 3, 3, 1]]
984 output = ndimage.rank_filter(array, 1, size=[2, 3])
985 assert_array_almost_equal(expected, output)
986 output = ndimage.percentile_filter(array, 17,
987 size=(2, 3))
988 assert_array_almost_equal(expected, output)
989
991 "rank filter 7"
992 array = numpy.array([[3, 2, 5, 1, 4],
993 [5, 8, 3, 7, 1],
994 [5, 6, 9, 3, 5]])
995 expected = [[3, 5, 5, 5, 4],
996 [5, 5, 7, 5, 4],
997 [6, 8, 8, 7, 5]]
998 output = ndimage.rank_filter(array, -2, size=[2, 3])
999 assert_array_almost_equal(expected, output)
1000
1002 "median filter 8"
1003 array = numpy.array([[3, 2, 5, 1, 4],
1004 [5, 8, 3, 7, 1],
1005 [5, 6, 9, 3, 5]])
1006 expected = [[3, 3, 2, 4, 4],
1007 [5, 5, 5, 4, 4],
1008 [5, 6, 7, 5, 5]]
1009 kernel = numpy.array([2, 3])
1010 output = ndimage.percentile_filter(array, 50.0,
1011 size=(2, 3))
1012 assert_array_almost_equal(expected, output)
1013 output = ndimage.rank_filter(array, 3, size=(2, 3))
1014 assert_array_almost_equal(expected, output)
1015 output = ndimage.median_filter(array, size=(2, 3))
1016 assert_array_almost_equal(expected, output)
1017
1019 "rank filter 9"
1020 expected = [[3, 3, 2, 4, 4],
1021 [3, 5, 2, 5, 1],
1022 [5, 5, 8, 3, 5]]
1023 footprint = [[1, 0, 1], [0, 1, 0]]
1024 for type in self.types:
1025 array = numpy.array([[3, 2, 5, 1, 4],
1026 [5, 8, 3, 7, 1],
1027 [5, 6, 9, 3, 5]], type)
1028 output = ndimage.rank_filter(array, 1,
1029 footprint=footprint)
1030 assert_array_almost_equal(expected, output)
1031 output = ndimage.percentile_filter(array, 35,
1032 footprint=footprint)
1033 assert_array_almost_equal(expected, output)
1034
1036 "rank filter 10"
1037 array = numpy.array([[3, 2, 5, 1, 4],
1038 [7, 6, 9, 3, 5],
1039 [5, 8, 3, 7, 1]])
1040 expected = [[2, 2, 1, 1, 1],
1041 [2, 3, 1, 3, 1],
1042 [5, 5, 3, 3, 1]]
1043 footprint = [[1, 0, 1], [1, 1, 0]]
1044 output = ndimage.rank_filter(array, 0,
1045 footprint=footprint)
1046 assert_array_almost_equal(expected, output)
1047 output = ndimage.percentile_filter(array, 0.0,
1048 footprint=footprint)
1049 assert_array_almost_equal(expected, output)
1050
1052 "rank filter 11"
1053 array = numpy.array([[3, 2, 5, 1, 4],
1054 [7, 6, 9, 3, 5],
1055 [5, 8, 3, 7, 1]])
1056 expected = [[3, 5, 5, 5, 4],
1057 [7, 7, 9, 9, 5],
1058 [7, 9, 8, 9, 7]]
1059 footprint = [[1, 0, 1], [1, 1, 0]]
1060 output = ndimage.rank_filter(array, -1,
1061 footprint=footprint)
1062 assert_array_almost_equal(expected, output)
1063 output = ndimage.percentile_filter(array, 100.0,
1064 footprint=footprint)
1065 assert_array_almost_equal(expected, output)
1066
1067
1069 "rank filter 12"
1070 expected = [[3, 3, 2, 4, 4],
1071 [3, 5, 2, 5, 1],
1072 [5, 5, 8, 3, 5]]
1073 footprint = [[1, 0, 1], [0, 1, 0]]
1074 for type in self.types:
1075 array = numpy.array([[3, 2, 5, 1, 4],
1076 [5, 8, 3, 7, 1],
1077 [5, 6, 9, 3, 5]], type)
1078 output = ndimage.rank_filter(array, 1,
1079 footprint=footprint)
1080 assert_array_almost_equal(expected, output)
1081 output = ndimage.percentile_filter(array, 50.0,
1082 footprint=footprint)
1083 assert_array_almost_equal(expected, output)
1084 output = ndimage.median_filter(array,
1085 footprint=footprint)
1086 assert_array_almost_equal(expected, output)
1087
1089 "rank filter 13"
1090 expected = [[5, 2, 5, 1, 1],
1091 [5, 8, 3, 5, 5],
1092 [6, 6, 5, 5, 5]]
1093 footprint = [[1, 0, 1], [0, 1, 0]]
1094 for type in self.types:
1095 array = numpy.array([[3, 2, 5, 1, 4],
1096 [5, 8, 3, 7, 1],
1097 [5, 6, 9, 3, 5]], type)
1098 output = ndimage.rank_filter(array, 1,
1099 footprint=footprint, origin=-1)
1100 assert_array_almost_equal(expected, output)
1101
1103 "rank filter 14"
1104 expected = [[3, 5, 2, 5, 1],
1105 [5, 5, 8, 3, 5],
1106 [5, 6, 6, 5, 5]]
1107 footprint = [[1, 0, 1], [0, 1, 0]]
1108 for type in self.types:
1109 array = numpy.array([[3, 2, 5, 1, 4],
1110 [5, 8, 3, 7, 1],
1111 [5, 6, 9, 3, 5]], type)
1112 output = ndimage.rank_filter(array, 1,
1113 footprint=footprint, origin=[-1, 0])
1114 assert_array_almost_equal(expected, output)
1115
1117 "generic 1d filter 1"
1118 weights = numpy.array([1.1, 2.2, 3.3])
1119 def _filter_func(input, output, fltr, total):
1120 fltr = fltr / total
1121 for ii in range(input.shape[0] - 2):
1122 output[ii] = input[ii] * fltr[0]
1123 output[ii] += input[ii + 1] * fltr[1]
1124 output[ii] += input[ii + 2] * fltr[2]
1125 for type in self.types:
1126 a = numpy.arange(12, dtype=type)
1127 a.shape = (3,4)
1128 r1 = ndimage.correlate1d(a, weights / weights.sum(), 0,
1129 origin=-1)
1130 r2 = ndimage.generic_filter1d(a, _filter_func, 3,
1131 axis=0, origin=-1, extra_arguments=(weights,),
1132 extra_keywords={'total': weights.sum()})
1133 assert_array_almost_equal(r1, r2)
1134
1136 "generic filter 1"
1137 filter_ = numpy.array([[1.0, 2.0], [3.0, 4.0]])
1138 footprint = numpy.array([[1, 0], [0, 1]])
1139 cf = numpy.array([1., 4.])
1140 def _filter_func(buffer, weights, total=1.0):
1141 weights = cf / total
1142 return (buffer * weights).sum()
1143 for type in self.types:
1144 a = numpy.arange(12, dtype=type)
1145 a.shape = (3,4)
1146 r1 = ndimage.correlate(a, filter_ * footprint)
1147 r1 /= 5
1148 r2 = ndimage.generic_filter(a, _filter_func,
1149 footprint=footprint, extra_arguments=(cf,),
1150 extra_keywords={'total': cf.sum()})
1151 assert_array_almost_equal(r1, r2)
1152
1154 "line extension 1"
1155 array = numpy.array([1, 2, 3])
1156 weights = numpy.array([1, 0])
1157 expected_values = [[1, 1, 2],
1158 [3, 1, 2],
1159 [1, 1, 2],
1160 [2, 1, 2],
1161 [0, 1, 2]]
1162 for mode, expected_value in zip(self.modes, expected_values):
1163 output = ndimage.correlate1d(array, weights, 0,
1164 mode=mode, cval=0)
1165 assert_array_equal(output,expected_value)
1166
1168 "line extension 2"
1169 array = numpy.array([1, 2, 3])
1170 weights = numpy.array([1, 0, 0, 0, 0, 0, 0, 0])
1171 expected_values = [[1, 1, 1],
1172 [3, 1, 2],
1173 [3, 3, 2],
1174 [1, 2, 3],
1175 [0, 0, 0]]
1176 for mode, expected_value in zip(self.modes, expected_values):
1177 output = ndimage.correlate1d(array, weights, 0,
1178 mode=mode, cval=0)
1179 assert_array_equal(output, expected_value)
1180
1182 "line extension 3"
1183 array = numpy.array([1, 2, 3])
1184 weights = numpy.array([0, 0, 1])
1185 expected_values = [[2, 3, 3],
1186 [2, 3, 1],
1187 [2, 3, 3],
1188 [2, 3, 2],
1189 [2, 3, 0]]
1190 for mode, expected_value in zip(self.modes, expected_values):
1191 output = ndimage.correlate1d(array, weights, 0,
1192 mode=mode, cval=0)
1193 assert_array_equal(output, expected_value)
1194
1196 "line extension 4"
1197 array = numpy.array([1, 2, 3])
1198 weights = numpy.array([0, 0, 0, 0, 0, 0, 0, 0, 1])
1199 expected_values = [[3, 3, 3],
1200 [2, 3, 1],
1201 [2, 1, 1],
1202 [1, 2, 3],
1203 [0, 0, 0]]
1204 for mode, expected_value in zip(self.modes, expected_values):
1205 output = ndimage.correlate1d(array, weights, 0,
1206 mode=mode, cval=0)
1207 assert_array_equal(output, expected_value)
1208
1209
1211 "line extension 5"
1212 array = numpy.array([[1, 2, 3],
1213 [4, 5, 6],
1214 [7, 8, 9]])
1215 weights = numpy.array([[1, 0], [0, 0]])
1216 expected_values = [[[1, 1, 2], [1, 1, 2], [4, 4, 5]],
1217 [[9, 7, 8], [3, 1, 2], [6, 4, 5]],
1218 [[1, 1, 2], [1, 1, 2], [4, 4, 5]],
1219 [[5, 4, 5], [2, 1, 2], [5, 4, 5]],
1220 [[0, 0, 0], [0, 1, 2], [0, 4, 5]]]
1221 for mode, expected_value in zip(self.modes, expected_values):
1222 output = ndimage.correlate(array, weights,
1223 mode=mode, cval=0)
1224 assert_array_equal(output, expected_value)
1225
1226
1228 "line extension 6"
1229 array = numpy.array([[1, 2, 3],
1230 [4, 5, 6],
1231 [7, 8, 9]])
1232 weights = numpy.array([[0, 0, 0], [0, 0, 0], [0, 0, 1]])
1233 expected_values = [[[5, 6, 6], [8, 9, 9], [8, 9, 9]],
1234 [[5, 6, 4], [8, 9, 7], [2, 3, 1]],
1235 [[5, 6, 6], [8, 9, 9], [8, 9, 9]],
1236 [[5, 6, 5], [8, 9, 8], [5, 6, 5]],
1237 [[5, 6, 0], [8, 9, 0], [0, 0, 0]]]
1238 for mode, expected_value in zip(self.modes, expected_values):
1239 output = ndimage.correlate(array, weights,
1240 mode=mode, cval=0)
1241 assert_array_equal(output, expected_value)
1242
1243
1245 "line extension 7"
1246 array = numpy.array([1, 2, 3])
1247 weights = numpy.array([0, 0, 0, 0, 0, 0, 0, 0, 1])
1248 expected_values = [[3, 3, 3],
1249 [2, 3, 1],
1250 [2, 1, 1],
1251 [1, 2, 3],
1252 [0, 0, 0]]
1253 for mode, expected_value in zip(self.modes, expected_values):
1254 output = ndimage.correlate(array, weights,
1255 mode=mode, cval=0)
1256 assert_array_equal(output, expected_value)
1257
1259 "line extension 8"
1260 array = numpy.array([[1], [2], [3]])
1261 weights = numpy.array([[0], [0], [0], [0], [0], [0], [0],
1262 [0], [1]])
1263 expected_values = [[[3], [3], [3]],
1264 [[2], [3], [1]],
1265 [[2], [1], [1]],
1266 [[1], [2], [3]],
1267 [[0], [0], [0]]]
1268 for mode, expected_value in zip(self.modes, expected_values):
1269 output = ndimage.correlate(array, weights,
1270 mode=mode, cval=0)
1271 assert_array_equal(output, expected_value)
1272
1274 "line extension 9"
1275 array = numpy.array([1, 2, 3])
1276 weights = numpy.array([0, 0, 0, 0, 0, 0, 0, 0, 1])
1277 expected_values = [[3, 3, 3],
1278 [2, 3, 1],
1279 [2, 1, 1],
1280 [1, 2, 3],
1281 [0, 0, 0]]
1282 for mode, expected_value in zip(self.modes, expected_values):
1283 output = ndimage.correlate(array, weights,
1284 mode=mode, cval=0)
1285 assert_array_equal(output, expected_value)
1286
1288 "line extension 10"
1289 array = numpy.array([[1], [2], [3]])
1290 weights = numpy.array([[0], [0], [0], [0], [0], [0], [0],
1291 [0], [1]])
1292 expected_values = [[[3], [3], [3]],
1293 [[2], [3], [1]],
1294 [[2], [1], [1]],
1295 [[1], [2], [3]],
1296 [[0], [0], [0]]]
1297 for mode, expected_value in zip(self.modes, expected_values):
1298 output = ndimage.correlate(array, weights,
1299 mode=mode, cval=0)
1300 assert_array_equal(output, expected_value)
1301
1303 "boundary modes"
1304 def shift(x):
1305 return (x[0] + 0.5,)
1306
1307 data = numpy.array([1,2,3,4.])
1308 expected = {'constant': [1.5,2.5,3.5,-1,-1,-1,-1],
1309 'wrap': [1.5,2.5,3.5,1.5,2.5,3.5,1.5],
1310 'mirror' : [1.5,2.5,3.5,3.5,2.5,1.5,1.5],
1311 'nearest' : [1.5,2.5,3.5,4,4,4,4]}
1312
1313 for mode in expected.keys():
1314 assert_array_equal(expected[mode],
1315 ndimage.geometric_transform(data,shift,
1316 cval=-1,mode=mode,
1317 output_shape=(7,),
1318 order=1))
1319
1321 "boundary modes 2"
1322 def shift(x):
1323 return (x[0] - 0.9,)
1324
1325 data = numpy.array([1,2,3,4])
1326 expected = {'constant': [-1,1,2,3],
1327 'wrap': [3,1,2,3],
1328 'mirror' : [2,1,2,3],
1329 'nearest' : [1,1,2,3]}
1330
1331 for mode in expected.keys():
1332 assert_array_equal(expected[mode],
1333 ndimage.geometric_transform(data,shift,
1334 cval=-1,mode=mode,
1335 output_shape=(4,)))
1336
1338 "gaussian fourier filter for real transforms 1"
1339 for shape in [(32, 16), (31, 15)]:
1340 for type in [numpy.float32, numpy.float64]:
1341 a = numpy.zeros(shape, type)
1342 a[0, 0] = 1.0
1343 a = fft.rfft(a, shape[0], 0)
1344 a = fft.fft(a, shape[1], 1)
1345 a = ndimage.fourier_gaussian(a, [5.0, 2.5],
1346 shape[0], 0)
1347 a = fft.ifft(a, shape[1], 1)
1348 a = fft.irfft(a, shape[0], 0)
1349 assert_almost_equal(ndimage.sum(a), 1)
1350
1352 "gaussian fourier filter for complex transforms 1"
1353 for shape in [(32, 16), (31, 15)]:
1354 for type in [numpy.complex64, numpy.complex128]:
1355 a = numpy.zeros(shape, type)
1356 a[0, 0] = 1.0
1357 a = fft.fft(a, shape[0], 0)
1358 a = fft.fft(a, shape[1], 1)
1359 a = ndimage.fourier_gaussian(a, [5.0, 2.5], -1,
1360 0)
1361 a = fft.ifft(a, shape[1], 1)
1362 a = fft.ifft(a, shape[0], 0)
1363 assert_almost_equal(ndimage.sum(a.real), 1.0)
1364
1378
1391
1393 "shift filter for real transforms 1"
1394 for shape in [(32, 16), (31, 15)]:
1395 for dtype in [numpy.float32, numpy.float64]:
1396 expected = numpy.arange(shape[0] * shape[1], dtype=dtype)
1397 expected.shape = shape
1398 a = fft.rfft(expected, shape[0], 0)
1399 a = fft.fft(a, shape[1], 1)
1400 a = ndimage.fourier_shift(a, [1, 1], shape[0], 0)
1401 a = fft.ifft(a, shape[1], 1)
1402 a = fft.irfft(a, shape[0], 0)
1403 assert_array_almost_equal(a[1:, 1:], expected[:-1, :-1])
1404 assert_array_almost_equal(a.imag, numpy.zeros(shape))
1405
1407 "shift filter for complex transforms 1"
1408 for shape in [(32, 16), (31, 15)]:
1409 for type in [numpy.complex64, numpy.complex128]:
1410 expected = numpy.arange(shape[0] * shape[1],
1411 dtype=type)
1412 expected.shape = shape
1413 a = fft.fft(expected, shape[0], 0)
1414 a = fft.fft(a, shape[1], 1)
1415 a = ndimage.fourier_shift(a, [1, 1], -1, 0)
1416 a = fft.ifft(a, shape[1], 1)
1417 a = fft.ifft(a, shape[0], 0)
1418 assert_array_almost_equal(a.real[1:, 1:], expected[:-1, :-1])
1419 assert_array_almost_equal(a.imag, numpy.zeros(shape))
1420
1422 "ellipsoid fourier filter for real transforms 1"
1423 for shape in [(32, 16), (31, 15)]:
1424 for type in [numpy.float32, numpy.float64]:
1425 a = numpy.zeros(shape, type)
1426 a[0, 0] = 1.0
1427 a = fft.rfft(a, shape[0], 0)
1428 a = fft.fft(a, shape[1], 1)
1429 a = ndimage.fourier_ellipsoid(a, [5.0, 2.5],
1430 shape[0], 0)
1431 a = fft.ifft(a, shape[1], 1)
1432 a = fft.irfft(a, shape[0], 0)
1433 assert_almost_equal(ndimage.sum(a), 1.0)
1434
1436 "ellipsoid fourier filter for complex transforms 1"
1437 for shape in [(32, 16), (31, 15)]:
1438 for type in [numpy.complex64, numpy.complex128]:
1439 a = numpy.zeros(shape, type)
1440 a[0, 0] = 1.0
1441 a = fft.fft(a, shape[0], 0)
1442 a = fft.fft(a, shape[1], 1)
1443 a = ndimage.fourier_ellipsoid(a, [5.0, 2.5], -1,
1444 0)
1445 a = fft.ifft(a, shape[1], 1)
1446 a = fft.ifft(a, shape[0], 0)
1447 assert_almost_equal(ndimage.sum(a.real), 1.0)
1448
1450 "spline filter 1"
1451 for type in self.types:
1452 data = numpy.ones([], type)
1453 for order in range(2, 6):
1454 out = ndimage.spline_filter(data, order=order)
1455 assert_array_almost_equal(out, 1)
1456
1458 "spline filter 2"
1459 for type in self.types:
1460 data = numpy.array([1])
1461 for order in range(2, 6):
1462 out = ndimage.spline_filter(data, order=order)
1463 assert_array_almost_equal(out, [1])
1464
1466 "spline filter 3"
1467 for type in self.types:
1468 data = numpy.ones([], type)
1469 for order in range(2, 6):
1470 out = ndimage.spline_filter(data, order,
1471 output=type)
1472 assert_array_almost_equal(out, 1)
1473
1475 "spline filter 4"
1476 for type in self.types:
1477 data = numpy.ones([4], type)
1478 for order in range(2, 6):
1479 out = ndimage.spline_filter(data, order)
1480 assert_array_almost_equal(out, [1, 1, 1, 1])
1481
1483 "spline filter 5"
1484 for type in self.types:
1485 data = numpy.ones([4, 4], type)
1486 for order in range(2, 6):
1487 out = ndimage.spline_filter(data, order=order)
1488 assert_array_almost_equal(out, [[1, 1, 1, 1],
1489 [1, 1, 1, 1],
1490 [1, 1, 1, 1],
1491 [1, 1, 1, 1]])
1492
1498 for order in range(0, 6):
1499 out = ndimage.geometric_transform(data, mapping,
1500 data.shape,
1501 order=order)
1502 assert_array_almost_equal(out, [1])
1503
1509 for order in range(0, 6):
1510 out = ndimage.geometric_transform(data, mapping,
1511 data.shape, order=order)
1512 assert_array_almost_equal(out, [1, 1, 1, 1])
1513
1519 for order in range(0, 6):
1520 out = ndimage.geometric_transform(data, mapping,
1521 data.shape, order=order)
1522 assert_array_almost_equal(out, [0, 1, 1, 1])
1523
1529 for order in range(0, 6):
1530 out = ndimage.geometric_transform(data, mapping,
1531 data.shape, order=order)
1532 assert_array_almost_equal(out, [0, 4, 1, 3])
1533
1541 for order in range(0, 6):
1542 out = ndimage.geometric_transform(data, mapping,
1543 data.shape, order=order)
1544 assert_array_almost_equal(out, [[0, 1, 1, 1],
1545 [0, 1, 1, 1],
1546 [0, 1, 1, 1]])
1547
1555 for order in range(0, 6):
1556 out = ndimage.geometric_transform(data, mapping,
1557 data.shape, order=order)
1558 assert_array_almost_equal(out, [[0, 4, 1, 3],
1559 [0, 7, 6, 8],
1560 [0, 3, 5, 3]])
1561
1569 for order in range(0, 6):
1570 out = ndimage.geometric_transform(data, mapping,
1571 data.shape, order=order)
1572 assert_array_almost_equal(out, [[0, 0, 0, 0],
1573 [4, 1, 3, 2],
1574 [7, 6, 8, 5]])
1575
1583 for order in range(0, 6):
1584 out = ndimage.geometric_transform(data, mapping,
1585 data.shape, order=order)
1586 assert_array_almost_equal(out, [[0, 0, 0, 0],
1587 [0, 4, 1, 3],
1588 [0, 7, 6, 8]])
1589
1597 for order in range(0, 6):
1598 if (order > 1):
1599 filtered = ndimage.spline_filter(data,
1600 order=order)
1601 else:
1602 filtered = data
1603 out = ndimage.geometric_transform(filtered, mapping,
1604 data.shape, order=order, prefilter=False)
1605 assert_array_almost_equal(out, [[0, 0, 0, 0],
1606 [0, 4, 1, 3],
1607 [0, 7, 6, 8]])
1608
1614 for order in range(0, 6):
1615 out = ndimage.geometric_transform(data, mapping,
1616 [4], order=order)
1617 assert_array_almost_equal(out, [1, 1, 1, 1])
1618
1624 for order in range(0, 6):
1625 out = ndimage.geometric_transform(data, mapping,
1626 [4], order=order)
1627 assert_array_almost_equal(out, [1, 2, 3, 4])
1628
1634 for order in range(0, 6):
1635 out = ndimage.geometric_transform(data, mapping,
1636 [8], order=order)
1637 assert_array_almost_equal(out[::2], [1, 2, 3, 4])
1638
1646 for order in range(0, 6):
1647 out = ndimage.geometric_transform(data, mapping,
1648 (3, 2), order=order)
1649 assert_array_almost_equal(out, [[1, 3], [5, 7], [9, 11]])
1650
1658 for order in range(0, 6):
1659 out = ndimage.geometric_transform(data, mapping,
1660 (1, 4), order=order)
1661 assert_array_almost_equal(out, [[1, 2, 3, 4]])
1662
1670 for order in range(0, 6):
1671 out = ndimage.geometric_transform(data, mapping,
1672 (1, 2), order=order)
1673 assert_array_almost_equal(out, [[1, 3]])
1674
1682 for order in range(0, 6):
1683 out = ndimage.geometric_transform(data, mapping,
1684 (3, 8), order=order)
1685 assert_array_almost_equal(out[..., ::2], data)
1686
1694 for order in range(0, 6):
1695 out = ndimage.geometric_transform(data, mapping,
1696 (6, 4), order=order)
1697 assert_array_almost_equal(out[::2, ...], data)
1698
1706 for order in range(0, 6):
1707 out = ndimage.geometric_transform(data, mapping,
1708 (6, 8), order=order)
1709 assert_array_almost_equal(out[::2, ::2], data)
1710
1711
1719 def mapping2(x):
1720 return (x[0] * 2, x[1] * 2)
1721 for order in range(0, 6):
1722 out = ndimage.geometric_transform(data, mapping1,
1723 (6, 8), order=order)
1724 out = ndimage.geometric_transform(out, mapping2,
1725 (3, 4), order=order)
1726 assert_array_almost_equal(out, data)
1727
1735 for order in range(0, 6):
1736 out = ndimage.geometric_transform(data, mapping,
1737 (2,), order=order)
1738 out = out.astype(numpy.int32)
1739 assert_array_almost_equal(out, [5, 7])
1740
1748 for order in range(0, 6):
1749 out = ndimage.geometric_transform(data, mapping,
1750 (2,), order=order, extra_arguments=(1,),
1751 extra_keywords={'b': 2})
1752 assert_array_almost_equal(out, [5, 7])
1753
1755 "map coordinates 1"
1756 data = numpy.array([[4, 1, 3, 2],
1757 [7, 6, 8, 5],
1758 [3, 5, 3, 6]])
1759 idx = numpy.indices(data.shape)
1760 idx -= 1
1761 for order in range(0, 6):
1762 out = ndimage.map_coordinates(data, idx, order=order)
1763 assert_array_almost_equal(out, [[0, 0, 0, 0],
1764 [0, 4, 1, 3],
1765 [0, 7, 6, 8]])
1766
1768 "map coordinates 2"
1769 data = numpy.array([[4, 1, 3, 2],
1770 [7, 6, 8, 5],
1771 [3, 5, 3, 6]])
1772 idx = numpy.indices(data.shape, numpy.float64)
1773 idx -= 0.5
1774 for order in range(0, 6):
1775 out1 = ndimage.shift(data, 0.5, order=order)
1776 out2 = ndimage.map_coordinates(data, idx,
1777 order=order)
1778 assert_array_almost_equal(out1, out2)
1779
1787
1795
1803
1811
1824
1837
1850
1863
1881
1889
1897
1905
1916
1927
1938
1949
1960
1972
1988
1998
2008
2010 "shift 1"
2011 data = numpy.array([1])
2012 for order in range(0, 6):
2013 out = ndimage.shift(data, [1], order=order)
2014 assert_array_almost_equal(out, [0])
2015
2017 "shift 2"
2018 data = numpy.ones([4])
2019 for order in range(0, 6):
2020 out = ndimage.shift(data, [1], order=order)
2021 assert_array_almost_equal(out, [0, 1, 1, 1])
2022
2024 "shift 3"
2025 data = numpy.ones([4])
2026 for order in range(0, 6):
2027 out = ndimage.shift(data, -1, order=order)
2028 assert_array_almost_equal(out, [1, 1, 1, 0])
2029
2031 "shift 4"
2032 data = numpy.array([4, 1, 3, 2])
2033 for order in range(0, 6):
2034 out = ndimage.shift(data, 1, order=order)
2035 assert_array_almost_equal(out, [0, 4, 1, 3])
2036
2038 "shift 5"
2039 data = numpy.array([[1, 1, 1, 1],
2040 [1, 1, 1, 1],
2041 [1, 1, 1, 1]])
2042 for order in range(0, 6):
2043 out = ndimage.shift(data, [0, 1], order=order)
2044 assert_array_almost_equal(out, [[0, 1, 1, 1],
2045 [0, 1, 1, 1],
2046 [0, 1, 1, 1]])
2047
2049 "shift 6"
2050 data = numpy.array([[4, 1, 3, 2],
2051 [7, 6, 8, 5],
2052 [3, 5, 3, 6]])
2053 for order in range(0, 6):
2054 out = ndimage.shift(data, [0, 1], order=order)
2055 assert_array_almost_equal(out, [[0, 4, 1, 3],
2056 [0, 7, 6, 8],
2057 [0, 3, 5, 3]])
2058
2060 "shift 7"
2061 data = numpy.array([[4, 1, 3, 2],
2062 [7, 6, 8, 5],
2063 [3, 5, 3, 6]])
2064 for order in range(0, 6):
2065 out = ndimage.shift(data, [1, 0], order=order)
2066 assert_array_almost_equal(out, [[0, 0, 0, 0],
2067 [4, 1, 3, 2],
2068 [7, 6, 8, 5]])
2069
2070
2072 "shift 8"
2073 data = numpy.array([[4, 1, 3, 2],
2074 [7, 6, 8, 5],
2075 [3, 5, 3, 6]])
2076 for order in range(0, 6):
2077 out = ndimage.shift(data, [1, 1], order=order)
2078 assert_array_almost_equal(out, [[0, 0, 0, 0],
2079 [0, 4, 1, 3],
2080 [0, 7, 6, 8]])
2081
2083 "shift 9"
2084 data = numpy.array([[4, 1, 3, 2],
2085 [7, 6, 8, 5],
2086 [3, 5, 3, 6]])
2087 for order in range(0, 6):
2088 if (order > 1):
2089 filtered = ndimage.spline_filter(data,
2090 order=order)
2091 else:
2092 filtered = data
2093 out = ndimage.shift(filtered, [1, 1], order=order,
2094 prefilter=False)
2095 assert_array_almost_equal(out, [[0, 0, 0, 0],
2096 [0, 4, 1, 3],
2097 [0, 7, 6, 8]])
2098
2100 "zoom 1"
2101 for order in range(0,6):
2102 for z in [2,[2,2]]:
2103 arr = numpy.array(range(25)).reshape((5,5)).astype(float)
2104 arr = ndimage.zoom(arr, z, order=order)
2105 assert_equal(arr.shape,(10,10))
2106 assert_(numpy.all(arr[-1,:] != 0))
2107 assert_(numpy.all(arr[-1,:] >= (20 - eps)))
2108 assert_(numpy.all(arr[0,:] <= (5 + eps)))
2109 assert_(numpy.all(arr >= (0 - eps)))
2110 assert_(numpy.all(arr <= (24 + eps)))
2111
2113 "zoom 2"
2114 arr = numpy.arange(12).reshape((3,4))
2115 out = ndimage.zoom(ndimage.zoom(arr,2),0.5)
2116 assert_array_equal(out,arr)
2117
2119 "zoom by affine transformation 1"
2120 data = [[1, 2, 3, 4],
2121 [5, 6, 7, 8],
2122 [9, 10, 11, 12]]
2123 for order in range(0, 6):
2124 out = ndimage.affine_transform(data, [0.5, 0.5], 0,
2125 (6, 8), order=order)
2126 assert_array_almost_equal(out[::2, ::2], data)
2127
2129 "rotate 1"
2130 data = numpy.array([[0, 0, 0, 0],
2131 [0, 1, 1, 0],
2132 [0, 0, 0, 0]], dtype=numpy.float64)
2133 for order in range(0, 6):
2134 out = ndimage.rotate(data, 0)
2135 assert_array_almost_equal(out, data)
2136
2138 "rotate 2"
2139 data = numpy.array([[0, 0, 0, 0],
2140 [0, 1, 0, 0],
2141 [0, 0, 0, 0]], dtype=numpy.float64)
2142 expected = numpy.array([[0, 0, 0],
2143 [0, 0, 0],
2144 [0, 1, 0],
2145 [0, 0, 0]], dtype=numpy.float64)
2146 for order in range(0, 6):
2147 out = ndimage.rotate(data, 90)
2148 assert_array_almost_equal(out, expected)
2149
2151 "rotate 3"
2152 data = numpy.array([[0, 0, 0, 0, 0],
2153 [0, 1, 1, 0, 0],
2154 [0, 0, 0, 0, 0]], dtype=numpy.float64)
2155 expected = numpy.array([[0, 0, 0],
2156 [0, 0, 0],
2157 [0, 1, 0],
2158 [0, 1, 0],
2159 [0, 0, 0]], dtype=numpy.float64)
2160 for order in range(0, 6):
2161 out = ndimage.rotate(data, 90)
2162 assert_array_almost_equal(out, expected)
2163
2165 "rotate 4"
2166 data = numpy.array([[0, 0, 0, 0, 0],
2167 [0, 1, 1, 0, 0],
2168 [0, 0, 0, 0, 0]], dtype=numpy.float64)
2169 expected = numpy.array([[0, 0, 0, 0, 0],
2170 [0, 0, 1, 0, 0],
2171 [0, 0, 1, 0, 0]], dtype=numpy.float64)
2172 for order in range(0, 6):
2173 out = ndimage.rotate(data, 90, reshape=False)
2174 assert_array_almost_equal(out, expected)
2175
2177 "rotate 5"
2178 data = numpy.empty((4,3,3))
2179 for i in range(3):
2180 data[:,:,i] = numpy.array([[0,0,0],
2181 [0,1,0],
2182 [0,1,0],
2183 [0,0,0]], dtype=numpy.float64)
2184
2185 expected = numpy.array([[0,0,0,0],
2186 [0,1,1,0],
2187 [0,0,0,0]], dtype=numpy.float64)
2188
2189 for order in range(0, 6):
2190 out = ndimage.rotate(data, 90)
2191 for i in range(3):
2192 assert_array_almost_equal(out[:,:,i], expected)
2193
2195 "rotate 6"
2196 data = numpy.empty((3,4,3))
2197 for i in range(3):
2198 data[:,:,i] = numpy.array([[0,0,0,0],
2199 [0,1,1,0],
2200 [0,0,0,0]], dtype=numpy.float64)
2201
2202 expected = numpy.array([[0,0,0],
2203 [0,1,0],
2204 [0,1,0],
2205 [0,0,0]], dtype=numpy.float64)
2206
2207 for order in range(0, 6):
2208 out = ndimage.rotate(data, 90)
2209 for i in range(3):
2210 assert_array_almost_equal(out[:,:,i], expected)
2211
2213 "rotate 7"
2214 data = numpy.array([[[0, 0, 0, 0, 0],
2215 [0, 1, 1, 0, 0],
2216 [0, 0, 0, 0, 0]]] * 2,
2217 dtype=numpy.float64)
2218 data = data.transpose()
2219 expected = numpy.array([[[0, 0, 0],
2220 [0, 1, 0],
2221 [0, 1, 0],
2222 [0, 0, 0],
2223 [0, 0, 0]]] * 2, dtype=numpy.float64)
2224 expected = expected.transpose([2,1,0])
2225
2226 for order in range(0, 6):
2227 out = ndimage.rotate(data, 90, axes=(0, 1))
2228 assert_array_almost_equal(out, expected)
2229
2231 "rotate 8"
2232 data = numpy.array([[[0, 0, 0, 0, 0],
2233 [0, 1, 1, 0, 0],
2234 [0, 0, 0, 0, 0]]] * 2,
2235 dtype=numpy.float64)
2236 data = data.transpose()
2237 expected = numpy.array([[[0, 0, 1, 0, 0],
2238 [0, 0, 1, 0, 0],
2239 [0, 0, 0, 0, 0]]] * 2,
2240 dtype=numpy.float64)
2241 expected = expected.transpose()
2242 for order in range(0, 6):
2243 out = ndimage.rotate(data, 90, axes=(0, 1),
2244 reshape=False)
2245 assert_array_almost_equal(out, expected)
2246
2248 "watershed_ift 1"
2249 data = numpy.array([[0, 0, 0, 0, 0, 0, 0],
2250 [0, 1, 1, 1, 1, 1, 0],
2251 [0, 1, 0, 0, 0, 1, 0],
2252 [0, 1, 0, 0, 0, 1, 0],
2253 [0, 1, 0, 0, 0, 1, 0],
2254 [0, 1, 1, 1, 1, 1, 0],
2255 [0, 0, 0, 0, 0, 0, 0],
2256 [0, 0, 0, 0, 0, 0, 0]], numpy.uint8)
2257 markers = numpy.array([[ -1, 0, 0, 0, 0, 0, 0],
2258 [ 0, 0, 0, 0, 0, 0, 0],
2259 [ 0, 0, 0, 0, 0, 0, 0],
2260 [ 0, 0, 0, 1, 0, 0, 0],
2261 [ 0, 0, 0, 0, 0, 0, 0],
2262 [ 0, 0, 0, 0, 0, 0, 0],
2263 [ 0, 0, 0, 0, 0, 0, 0],
2264 [ 0, 0, 0, 0, 0, 0, 0]],
2265 numpy.int8)
2266 out = ndimage.watershed_ift(data, markers,
2267 structure=[[1,1,1],
2268 [1,1,1],
2269 [1,1,1]])
2270 expected = [[-1, -1, -1, -1, -1, -1, -1],
2271 [-1, 1, 1, 1, 1, 1, -1],
2272 [-1, 1, 1, 1, 1, 1, -1],
2273 [-1, 1, 1, 1, 1, 1, -1],
2274 [-1, 1, 1, 1, 1, 1, -1],
2275 [-1, 1, 1, 1, 1, 1, -1],
2276 [-1, -1, -1, -1, -1, -1, -1],
2277 [-1, -1, -1, -1, -1, -1, -1]]
2278 assert_array_almost_equal(out, expected)
2279
2281 "watershed_ift 2"
2282 data = numpy.array([[0, 0, 0, 0, 0, 0, 0],
2283 [0, 1, 1, 1, 1, 1, 0],
2284 [0, 1, 0, 0, 0, 1, 0],
2285 [0, 1, 0, 0, 0, 1, 0],
2286 [0, 1, 0, 0, 0, 1, 0],
2287 [0, 1, 1, 1, 1, 1, 0],
2288 [0, 0, 0, 0, 0, 0, 0],
2289 [0, 0, 0, 0, 0, 0, 0]], numpy.uint8)
2290 markers = numpy.array([[ -1, 0, 0, 0, 0, 0, 0],
2291 [ 0, 0, 0, 0, 0, 0, 0],
2292 [ 0, 0, 0, 0, 0, 0, 0],
2293 [ 0, 0, 0, 1, 0, 0, 0],
2294 [ 0, 0, 0, 0, 0, 0, 0],
2295 [ 0, 0, 0, 0, 0, 0, 0],
2296 [ 0, 0, 0, 0, 0, 0, 0],
2297 [ 0, 0, 0, 0, 0, 0, 0]],
2298 numpy.int8)
2299 out = ndimage.watershed_ift(data, markers)
2300 expected = [[-1, -1, -1, -1, -1, -1, -1],
2301 [-1, -1, 1, 1, 1, -1, -1],
2302 [-1, 1, 1, 1, 1, 1, -1],
2303 [-1, 1, 1, 1, 1, 1, -1],
2304 [-1, 1, 1, 1, 1, 1, -1],
2305 [-1, -1, 1, 1, 1, -1, -1],
2306 [-1, -1, -1, -1, -1, -1, -1],
2307 [-1, -1, -1, -1, -1, -1, -1]]
2308 assert_array_almost_equal(out, expected)
2309
2311 "watershed_ift 3"
2312 data = numpy.array([[0, 0, 0, 0, 0, 0, 0],
2313 [0, 1, 1, 1, 1, 1, 0],
2314 [0, 1, 0, 1, 0, 1, 0],
2315 [0, 1, 0, 1, 0, 1, 0],
2316 [0, 1, 0, 1, 0, 1, 0],
2317 [0, 1, 1, 1, 1, 1, 0],
2318 [0, 0, 0, 0, 0, 0, 0]], numpy.uint8)
2319 markers = numpy.array([[ 0, 0, 0, 0, 0, 0, 0],
2320 [ 0, 0, 0, 0, 0, 0, 0],
2321 [ 0, 0, 0, 0, 0, 0, 0],
2322 [ 0, 0, 2, 0, 3, 0, 0],
2323 [ 0, 0, 0, 0, 0, 0, 0],
2324 [ 0, 0, 0, 0, 0, 0, 0],
2325 [ 0, 0, 0, 0, 0, 0, -1]],
2326 numpy.int8)
2327 out = ndimage.watershed_ift(data, markers)
2328 expected = [[-1, -1, -1, -1, -1, -1, -1],
2329 [-1, -1, 2, -1, 3, -1, -1],
2330 [-1, 2, 2, 3, 3, 3, -1],
2331 [-1, 2, 2, 3, 3, 3, -1],
2332 [-1, 2, 2, 3, 3, 3, -1],
2333 [-1, -1, 2, -1, 3, -1, -1],
2334 [-1, -1, -1, -1, -1, -1, -1]]
2335 assert_array_almost_equal(out, expected)
2336
2338 "watershed_ift 4"
2339 data = numpy.array([[0, 0, 0, 0, 0, 0, 0],
2340 [0, 1, 1, 1, 1, 1, 0],
2341 [0, 1, 0, 1, 0, 1, 0],
2342 [0, 1, 0, 1, 0, 1, 0],
2343 [0, 1, 0, 1, 0, 1, 0],
2344 [0, 1, 1, 1, 1, 1, 0],
2345 [0, 0, 0, 0, 0, 0, 0]], numpy.uint8)
2346 markers = numpy.array([[ 0, 0, 0, 0, 0, 0, 0],
2347 [ 0, 0, 0, 0, 0, 0, 0],
2348 [ 0, 0, 0, 0, 0, 0, 0],
2349 [ 0, 0, 2, 0, 3, 0, 0],
2350 [ 0, 0, 0, 0, 0, 0, 0],
2351 [ 0, 0, 0, 0, 0, 0, 0],
2352 [ 0, 0, 0, 0, 0, 0, -1]],
2353 numpy.int8)
2354 out = ndimage.watershed_ift(data, markers,
2355 structure=[[1,1,1],
2356 [1,1,1],
2357 [1,1,1]])
2358 expected = [[-1, -1, -1, -1, -1, -1, -1],
2359 [-1, 2, 2, 3, 3, 3, -1],
2360 [-1, 2, 2, 3, 3, 3, -1],
2361 [-1, 2, 2, 3, 3, 3, -1],
2362 [-1, 2, 2, 3, 3, 3, -1],
2363 [-1, 2, 2, 3, 3, 3, -1],
2364 [-1, -1, -1, -1, -1, -1, -1]]
2365 assert_array_almost_equal(out, expected)
2366
2368 "watershed_ift 5"
2369 data = numpy.array([[0, 0, 0, 0, 0, 0, 0],
2370 [0, 1, 1, 1, 1, 1, 0],
2371 [0, 1, 0, 1, 0, 1, 0],
2372 [0, 1, 0, 1, 0, 1, 0],
2373 [0, 1, 0, 1, 0, 1, 0],
2374 [0, 1, 1, 1, 1, 1, 0],
2375 [0, 0, 0, 0, 0, 0, 0]], numpy.uint8)
2376 markers = numpy.array([[ 0, 0, 0, 0, 0, 0, 0],
2377 [ 0, 0, 0, 0, 0, 0, 0],
2378 [ 0, 0, 0, 0, 0, 0, 0],
2379 [ 0, 0, 3, 0, 2, 0, 0],
2380 [ 0, 0, 0, 0, 0, 0, 0],
2381 [ 0, 0, 0, 0, 0, 0, 0],
2382 [ 0, 0, 0, 0, 0, 0, -1]],
2383 numpy.int8)
2384 out = ndimage.watershed_ift(data, markers,
2385 structure=[[1,1,1],
2386 [1,1,1],
2387 [1,1,1]])
2388 expected = [[-1, -1, -1, -1, -1, -1, -1],
2389 [-1, 3, 3, 2, 2, 2, -1],
2390 [-1, 3, 3, 2, 2, 2, -1],
2391 [-1, 3, 3, 2, 2, 2, -1],
2392 [-1, 3, 3, 2, 2, 2, -1],
2393 [-1, 3, 3, 2, 2, 2, -1],
2394 [-1, -1, -1, -1, -1, -1, -1]]
2395 assert_array_almost_equal(out, expected)
2396
2398 "watershed_ift 6"
2399 data = numpy.array([[0, 1, 0, 0, 0, 1, 0],
2400 [0, 1, 0, 0, 0, 1, 0],
2401 [0, 1, 0, 0, 0, 1, 0],
2402 [0, 1, 1, 1, 1, 1, 0],
2403 [0, 0, 0, 0, 0, 0, 0],
2404 [0, 0, 0, 0, 0, 0, 0]], numpy.uint8)
2405 markers = numpy.array([[ -1, 0, 0, 0, 0, 0, 0],
2406 [ 0, 0, 0, 1, 0, 0, 0],
2407 [ 0, 0, 0, 0, 0, 0, 0],
2408 [ 0, 0, 0, 0, 0, 0, 0],
2409 [ 0, 0, 0, 0, 0, 0, 0],
2410 [ 0, 0, 0, 0, 0, 0, 0]],
2411 numpy.int8)
2412 out = ndimage.watershed_ift(data, markers,
2413 structure=[[1,1,1],
2414 [1,1,1],
2415 [1,1,1]])
2416 expected = [[-1, 1, 1, 1, 1, 1, -1],
2417 [-1, 1, 1, 1, 1, 1, -1],
2418 [-1, 1, 1, 1, 1, 1, -1],
2419 [-1, 1, 1, 1, 1, 1, -1],
2420 [-1, -1, -1, -1, -1, -1, -1],
2421 [-1, -1, -1, -1, -1, -1, -1]]
2422 assert_array_almost_equal(out, expected)
2423
2425 "watershed_ift 7"
2426 shape = (7, 6)
2427 data = numpy.