Package stsci :: Package ndimage :: Package tests :: Module test_ndimage
[hide private]
[frames] | no frames]

Source Code for Module stsci.ndimage.tests.test_ndimage

   1  # Copyright (C) 2003-2005 Peter J. Verveer 
   2  # 
   3  # Redistribution and use in source and binary forms, with or without 
   4  # modification, are permitted provided that the following conditions 
   5  # are met: 
   6  # 
   7  # 1. Redistributions of source code must retain the above copyright 
   8  #    notice, this list of conditions and the following disclaimer. 
   9  # 
  10  # 2. Redistributions in binary form must reproduce the above 
  11  #    copyright notice, this list of conditions and the following 
  12  #    disclaimer in the documentation and/or other materials provided 
  13  #    with the distribution. 
  14  # 
  15  # 3. The name of the author may not be used to endorse or promote 
  16  #    products derived from this software without specific prior 
  17  #    written permission. 
  18  # 
  19  # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 
  20  # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
  21  # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
  22  # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 
  23  # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
  24  # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
  25  # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
  26  # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
  27  # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
  28  # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
  29  # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
  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   
42 -def sumsq(a, b):
43 return math.sqrt(((a - b)**2).sum())
44
45 -class TestNdimage:
46
47 - def setUp(self):
48 # list of numarray data types 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 # list of boundary modes: 55 self.modes = ['nearest', 'wrap', 'reflect', 'mirror', 'constant']
56
57 - def test_correlate01(self):
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
75 - def test_correlate02(self):
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
92 - def test_correlate03(self):
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
110 - def test_correlate04(self):
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
125 - def test_correlate05(self):
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
140 - def test_correlate06(self):
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
155 - def test_correlate07(self):
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
169 - def test_correlate08(self):
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
184 - def test_correlate09(self):
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
197 - def test_correlate10(self):
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
206 - def test_correlate11(self):
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
217 - def test_correlate12(self):
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
228 - def test_correlate13(self):
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
246 - def test_correlate14(self):
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
264 - def test_correlate15(self):
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
281 - def test_correlate16(self):
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
298 - def test_correlate17(self):
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
313 - def test_correlate18(self):
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
332 - def test_correlate19(self):
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
351 - def test_correlate20(self):
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
367 - def test_correlate21(self):
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
378 - def test_correlate22(self):
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
394 - def test_correlate23(self):
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
410 - def test_correlate24(self):
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
427 - def test_correlate25(self):
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
444 - def test_gauss01(self):
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
451 - def test_gauss02(self):
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
459 - def test_gauss03(self):
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 # input.sum() is 49995000.0. With single precision floats, we can't 469 # expect more than 8 digits of accuracy, so use decimal=0 in this test. 470 assert_almost_equal(output.sum(dtype='d'), input.sum(dtype='d'), decimal=0) 471 assert_(sumsq(input, output) > 1.0)
472
473 - def test_gauss04(self):
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
484 - def test_gauss05(self):
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
495 - def test_gauss06(self):
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
506 - def test_prewitt01(self):
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
518 - def test_prewitt02(self):
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
530 - def test_prewitt03(self):
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
541 - def test_prewitt04(self):
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
551 - def test_sobel01(self):
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
562 - def test_sobel02(self):
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
574 - def test_sobel03(self):
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
586 - def test_sobel04(self):
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
596 - def test_laplace01(self):
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
607 - def test_laplace02(self):
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
619 - def test_gaussian_laplace01(self):
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
630 - def test_gaussian_laplace02(self):
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
642 - def test_generic_laplace01(self):
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
661 - def test_gaussian_gradient_magnitude01(self):
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
675 - def test_gaussian_gradient_magnitude02(self):
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
690 - def test_generic_gradient_magnitude01(self):
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
708 - def test_uniform01(self):
709 "uniform filter 1" 710 array = numpy.array([2, 4, 6]) 711 size = 2 712 output = ndimage.uniform_filter1d(array, size, 713 origin=-1) 714 assert_array_almost_equal([3, 5, 6], output)
715
716 - def test_uniform02(self):
717 "uniform filter 2" 718 array = numpy.array([1, 2, 3]) 719 filter_shape = [0] 720 output = ndimage.uniform_filter(array, filter_shape) 721 assert_array_almost_equal(array, output)
722
723 - def test_uniform03(self):
724 "uniform filter 3" 725 array = numpy.array([1, 2, 3]) 726 filter_shape = [1] 727 output = ndimage.uniform_filter(array, filter_shape) 728 assert_array_almost_equal(array, output)
729
730 - def test_uniform04(self):
731 "uniform filter 4" 732 array = numpy.array([2, 4, 6]) 733 filter_shape = [2] 734 output = ndimage.uniform_filter(array, filter_shape) 735 assert_array_almost_equal([2, 3, 5], output)
736
737 - def test_uniform05(self):
738 "uniform filter 5" 739 array = [] 740 filter_shape = [1] 741 output = ndimage.uniform_filter(array, filter_shape) 742 assert_array_almost_equal([], output)
743
744 - def test_uniform06(self):
745 "uniform filter 6" 746 filter_shape = [2, 2] 747 for type1 in self.types: 748 array = numpy.array([[4, 8, 12], 749 [16, 20, 24]], type1) 750 for type2 in self.types: 751 output = ndimage.uniform_filter(array, 752 filter_shape, output=type2) 753 assert_array_almost_equal([[4, 6, 10], [10, 12, 16]], output) 754 assert_equal(output.dtype.type, type2)
755
756 - def test_minimum_filter01(self):
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
763 - def test_minimum_filter02(self):
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
770 - def test_minimum_filter03(self):
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
777 - def test_minimum_filter04(self):
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
784 - def test_minimum_filter05(self):
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
795 - def test_minimum_filter06(self):
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
807 - def test_minimum_filter07(self):
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
819 - def test_minimum_filter08(self):
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
831 - def test_minimum_filter09(self):
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
843 - def test_maximum_filter01(self):
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
850 - def test_maximum_filter02(self):
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
857 - def test_maximum_filter03(self):
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
864 - def test_maximum_filter04(self):
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
871 - def test_maximum_filter05(self):
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
882 - def test_maximum_filter06(self):
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
894 - def test_maximum_filter07(self):
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
906 - def test_maximum_filter08(self):
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
918 - def test_maximum_filter09(self):
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
930 - def test_rank01(self):
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
940 - def test_rank02(self):
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
950 - def test_rank03(self):
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
958 - def test_rank04(self):
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
969 - def test_rank05(self):
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
976 - def test_rank06(self):
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
990 - def test_rank07(self):
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
1001 - def test_rank08(self):
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
1018 - def test_rank09(self):
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
1035 - def test_rank10(self):
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
1051 - def test_rank11(self):
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
1068 - def test_rank12(self):
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
1088 - def test_rank13(self):
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
1102 - def test_rank14(self):
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
1116 - def test_generic_filter1d01(self):
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
1135 - def test_generic_filter01(self):
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
1153 - def test_extend01(self):
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
1167 - def test_extend02(self):
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
1181 - def test_extend03(self):
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
1195 - def test_extend04(self):
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
1210 - def test_extend05(self):
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
1227 - def test_extend06(self):
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
1244 - def test_extend07(self):
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
1258 - def test_extend08(self):
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
1273 - def test_extend09(self):
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
1287 - def test_extend10(self):
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
1302 - def test_boundaries(self):
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
1320 - def test_boundaries2(self):
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
1337 - def test_fourier_gaussian_real01(self):
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
1351 - def test_fourier_gaussian_complex01(self):
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
1365 - def test_fourier_uniform_real01(self):
1366 "uniform fourier filter for real transforms 1" 1367 for shape in [(32, 16), (31, 15)]: 1368 for type in [numpy.float32, numpy.float64]: 1369 a = numpy.zeros(shape, type) 1370 a[0, 0] = 1.0 1371 a = fft.rfft(a, shape[0], 0) 1372 a = fft.fft(a, shape[1], 1) 1373 a = ndimage.fourier_uniform(a, [5.0, 2.5], 1374 shape[0], 0) 1375 a = fft.ifft(a, shape[1], 1) 1376 a = fft.irfft(a, shape[0], 0) 1377 assert_almost_equal(ndimage.sum(a), 1.0)
1378
1379 - def test_fourier_uniform_complex01(self):
1380 "uniform fourier filter for complex transforms 1" 1381 for shape in [(32, 16), (31, 15)]: 1382 for type in [numpy.complex64, numpy.complex128]: 1383 a = numpy.zeros(shape, type) 1384 a[0, 0] = 1.0 1385 a = fft.fft(a, shape[0], 0) 1386 a = fft.fft(a, shape[1], 1) 1387 a = ndimage.fourier_uniform(a, [5.0, 2.5], -1, 0) 1388 a = fft.ifft(a, shape[1], 1) 1389 a = fft.ifft(a, shape[0], 0) 1390 assert_almost_equal(ndimage.sum(a.real), 1.0)
1391
1392 - def test_fourier_shift_real01(self):
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
1406 - def test_fourier_shift_complex01(self):
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
1421 - def test_fourier_ellipsoid_real01(self):
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
1435 - def test_fourier_ellipsoid_complex01(self):
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
1449 - def test_spline01(self):
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
1457 - def test_spline02(self):
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
1465 - def test_spline03(self):
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
1474 - def test_spline04(self):
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
1482 - def test_spline05(self):
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
1493 - def test_geometric_transform01(self):
1494 "geometric transform 1" 1495 data = numpy.array([1]) 1496 def mapping(x): 1497 return x
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
1504 - def test_geometric_transform02(self):
1505 "geometric transform 2" 1506 data = numpy.ones([4]) 1507 def mapping(x): 1508 return x
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
1514 - def test_geometric_transform03(self):
1515 "geometric transform 3" 1516 data = numpy.ones([4]) 1517 def mapping(x): 1518 return (x[0] - 1,)
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
1524 - def test_geometric_transform04(self):
1525 "geometric transform 4" 1526 data = numpy.array([4, 1, 3, 2]) 1527 def mapping(x): 1528 return (x[0] - 1,)
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
1534 - def test_geometric_transform05(self):
1535 "geometric transform 5" 1536 data = numpy.array([[1, 1, 1, 1], 1537 [1, 1, 1, 1], 1538 [1, 1, 1, 1]]) 1539 def mapping(x): 1540 return (x[0], x[1] - 1)
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
1548 - def test_geometric_transform06(self):
1549 "geometric transform 6" 1550 data = numpy.array([[4, 1, 3, 2], 1551 [7, 6, 8, 5], 1552 [3, 5, 3, 6]]) 1553 def mapping(x): 1554 return (x[0], x[1] - 1)
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
1562 - def test_geometric_transform07(self):
1563 "geometric transform 7" 1564 data = numpy.array([[4, 1, 3, 2], 1565 [7, 6, 8, 5], 1566 [3, 5, 3, 6]]) 1567 def mapping(x): 1568 return (x[0] - 1, x[1])
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
1576 - def test_geometric_transform08(self):
1577 "geometric transform 8" 1578 data = numpy.array([[4, 1, 3, 2], 1579 [7, 6, 8, 5], 1580 [3, 5, 3, 6]]) 1581 def mapping(x): 1582 return (x[0] - 1, x[1] - 1)
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
1590 - def test_geometric_transform10(self):
1591 "geometric transform 10" 1592 data = numpy.array([[4, 1, 3, 2], 1593 [7, 6, 8, 5], 1594 [3, 5, 3, 6]]) 1595 def mapping(x): 1596 return (x[0] - 1, x[1] - 1)
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
1609 - def test_geometric_transform13(self):
1610 "geometric transform 13" 1611 data = numpy.ones([2], numpy.float64) 1612 def mapping(x): 1613 return (x[0] // 2,)
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
1619 - def test_geometric_transform14(self):
1620 "geometric transform 14" 1621 data = [1, 5, 2, 6, 3, 7, 4, 4] 1622 def mapping(x): 1623 return (2 * x[0],)
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
1629 - def test_geometric_transform15(self):
1630 "geometric transform 15" 1631 data = [1, 2, 3, 4] 1632 def mapping(x): 1633 return (x[0] / 2,)
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
1639 - def test_geometric_transform16(self):
1640 "geometric transform 16" 1641 data = [[1, 2, 3, 4], 1642 [5, 6, 7, 8], 1643 [9.0, 10, 11, 12]] 1644 def mapping(x): 1645 return (x[0], x[1] * 2)
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
1651 - def test_geometric_transform17(self):
1652 "geometric transform 17" 1653 data = [[1, 2, 3, 4], 1654 [5, 6, 7, 8], 1655 [9, 10, 11, 12]] 1656 def mapping(x): 1657 return (x[0] * 2, x[1])
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
1663 - def test_geometric_transform18(self):
1664 "geometric transform 18" 1665 data = [[1, 2, 3, 4], 1666 [5, 6, 7, 8], 1667 [9, 10, 11, 12]] 1668 def mapping(x): 1669 return (x[0] * 2, x[1] * 2)
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
1675 - def test_geometric_transform19(self):
1676 "geometric transform 19" 1677 data = [[1, 2, 3, 4], 1678 [5, 6, 7, 8], 1679 [9, 10, 11, 12]] 1680 def mapping(x): 1681 return (x[0], x[1] / 2)
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
1687 - def test_geometric_transform20(self):
1688 "geometric transform 20" 1689 data = [[1, 2, 3, 4], 1690 [5, 6, 7, 8], 1691 [9, 10, 11, 12]] 1692 def mapping(x): 1693 return (x[0] / 2, x[1])
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
1699 - def test_geometric_transform21(self):
1700 "geometric transform 21" 1701 data = [[1, 2, 3, 4], 1702 [5, 6, 7, 8], 1703 [9, 10, 11, 12]] 1704 def mapping(x): 1705 return (x[0] / 2, x[1] / 2)
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
1712 - def test_geometric_transform22(self):
1713 "geometric transform 22" 1714 data = numpy.array([[1, 2, 3, 4], 1715 [5, 6, 7, 8], 1716 [9, 10, 11, 12]], numpy.float64) 1717 def mapping1(x): 1718 return (x[0] / 2, x[1] / 2)
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
1728 - def test_geometric_transform23(self):
1729 "geometric transform 23" 1730 data = [[1, 2, 3, 4], 1731 [5, 6, 7, 8], 1732 [9, 10, 11, 12]] 1733 def mapping(x): 1734 return (1, x[0] * 2)
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
1741 - def test_geometric_transform24(self):
1742 "geometric transform 24" 1743 data = [[1, 2, 3, 4], 1744 [5, 6, 7, 8], 1745 [9, 10, 11, 12]] 1746 def mapping(x, a, b): 1747 return (a, x[0] * b)
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
1754 - def test_map_coordinates01(self):
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
1767 - def test_map_coordinates02(self):
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
1780 - def test_affine_transform01(self):
1781 "affine_transform 1" 1782 data = numpy.array([1]) 1783 for order in range(0, 6): 1784 out = ndimage.affine_transform(data, [[1]], 1785 order=order) 1786 assert_array_almost_equal(out, [1])
1787
1788 - def test_affine_transform02(self):
1789 "affine transform 2" 1790 data = numpy.ones([4]) 1791 for order in range(0, 6): 1792 out = ndimage.affine_transform(data, [[1]], 1793 order=order) 1794 assert_array_almost_equal(out, [1, 1, 1, 1])
1795
1796 - def test_affine_transform03(self):
1797 "affine transform 3" 1798 data = numpy.ones([4]) 1799 for order in range(0, 6): 1800 out = ndimage.affine_transform(data, [[1]], -1, 1801 order=order) 1802 assert_array_almost_equal(out, [0, 1, 1, 1])
1803
1804 - def test_affine_transform04(self):
1805 "affine transform 4" 1806 data = numpy.array([4, 1, 3, 2]) 1807 for order in range(0, 6): 1808 out = ndimage.affine_transform(data, [[1]], -1, 1809 order=order) 1810 assert_array_almost_equal(out, [0, 4, 1, 3])
1811
1812 - def test_affine_transform05(self):
1813 "affine transform 5" 1814 data = numpy.array([[1, 1, 1, 1], 1815 [1, 1, 1, 1], 1816 [1, 1, 1, 1]]) 1817 for order in range(0, 6): 1818 out = ndimage.affine_transform(data, [[1, 0], 1819 [0, 1]], 1820 [0, -1], order=order) 1821 assert_array_almost_equal(out, [[0, 1, 1, 1], 1822 [0, 1, 1, 1], 1823 [0, 1, 1, 1]])
1824
1825 - def test_affine_transform06(self):
1826 "affine transform 6" 1827 data = numpy.array([[4, 1, 3, 2], 1828 [7, 6, 8, 5], 1829 [3, 5, 3, 6]]) 1830 for order in range(0, 6): 1831 out = ndimage.affine_transform(data, [[1, 0], 1832 [0, 1]], 1833 [0, -1], order=order) 1834 assert_array_almost_equal(out, [[0, 4, 1, 3], 1835 [0, 7, 6, 8], 1836 [0, 3, 5, 3]])
1837
1838 - def test_affine_transform07(self):
1839 "affine transform 7" 1840 data = numpy.array([[4, 1, 3, 2], 1841 [7, 6, 8, 5], 1842 [3, 5, 3, 6]]) 1843 for order in range(0, 6): 1844 out = ndimage.affine_transform(data, [[1, 0], 1845 [0, 1]], 1846 [-1, 0], order=order) 1847 assert_array_almost_equal(out, [[0, 0, 0, 0], 1848 [4, 1, 3, 2], 1849 [7, 6, 8, 5]])
1850
1851 - def test_affine_transform08(self):
1852 "affine transform 8" 1853 data = numpy.array([[4, 1, 3, 2], 1854 [7, 6, 8, 5], 1855 [3, 5, 3, 6]]) 1856 for order in range(0, 6): 1857 out = ndimage.affine_transform(data, [[1, 0], 1858 [0, 1]], 1859 [-1, -1], order=order) 1860 assert_array_almost_equal(out, [[0, 0, 0, 0], 1861 [0, 4, 1, 3], 1862 [0, 7, 6, 8]])
1863
1864 - def test_affine_transform09(self):
1865 "affine transform 9" 1866 data = numpy.array([[4, 1, 3, 2], 1867 [7, 6, 8, 5], 1868 [3, 5, 3, 6]]) 1869 for order in range(0, 6): 1870 if (order > 1): 1871 filtered = ndimage.spline_filter(data, 1872 order=order) 1873 else: 1874 filtered = data 1875 out = ndimage.affine_transform(filtered,[[1, 0], 1876 [0, 1]], 1877 [-1, -1], order=order, prefilter=False) 1878 assert_array_almost_equal(out, [[0, 0, 0, 0], 1879 [0, 4, 1, 3], 1880 [0, 7, 6, 8]])
1881
1882 - def test_affine_transform10(self):
1883 "affine transform 10" 1884 data = numpy.ones([2], numpy.float64) 1885 for order in range(0, 6): 1886 out = ndimage.affine_transform(data, [[0.5]], 1887 output_shape=(4,), order=order) 1888 assert_array_almost_equal(out, [1, 1, 1, 0])
1889
1890 - def test_affine_transform11(self):
1891 "affine transform 11" 1892 data = [1, 5, 2, 6, 3, 7, 4, 4] 1893 for order in range(0, 6): 1894 out = ndimage.affine_transform(data, [[2]], 0, (4,), 1895 order=order) 1896 assert_array_almost_equal(out, [1, 2, 3, 4])
1897
1898 - def test_affine_transform12(self):
1899 "affine transform 12" 1900 data = [1, 2, 3, 4] 1901 for order in range(0, 6): 1902 out = ndimage.affine_transform(data, [[0.5]], 0, 1903 (8,), order=order) 1904 assert_array_almost_equal(out[::2], [1, 2, 3, 4])
1905
1906 - def test_affine_transform13(self):
1907 "affine transform 13" 1908 data = [[1, 2, 3, 4], 1909 [5, 6, 7, 8], 1910 [9.0, 10, 11, 12]] 1911 for order in range(0, 6): 1912 out = ndimage.affine_transform(data, [[1, 0], 1913 [0, 2]], 0, 1914 (3, 2), order=order) 1915 assert_array_almost_equal(out, [[1, 3], [5, 7], [9, 11]])
1916
1917 - def test_affine_transform14(self):
1918 "affine transform 14" 1919 data = [[1, 2, 3, 4], 1920 [5, 6, 7, 8], 1921 [9, 10, 11, 12]] 1922 for order in range(0, 6): 1923 out = ndimage.affine_transform(data, [[2, 0], 1924 [0, 1]], 0, 1925 (1, 4), order=order) 1926 assert_array_almost_equal(out, [[1, 2, 3, 4]])
1927
1928 - def test_affine_transform15(self):
1929 "affine transform 15" 1930 data = [[1, 2, 3, 4], 1931 [5, 6, 7, 8], 1932 [9, 10, 11, 12]] 1933 for order in range(0, 6): 1934 out = ndimage.affine_transform(data, [[2, 0], 1935 [0, 2]], 0, 1936 (1, 2), order=order) 1937 assert_array_almost_equal(out, [[1, 3]])
1938
1939 - def test_affine_transform16(self):
1940 "affine transform 16" 1941 data = [[1, 2, 3, 4], 1942 [5, 6, 7, 8], 1943 [9, 10, 11, 12]] 1944 for order in range(0, 6): 1945 out = ndimage.affine_transform(data, [[1, 0.0], 1946 [0, 0.5]], 0, 1947 (3, 8), order=order) 1948 assert_array_almost_equal(out[..., ::2], data)
1949
1950 - def test_affine_transform17(self):
1951 "affine transform 17" 1952 data = [[1, 2, 3, 4], 1953 [5, 6, 7, 8], 1954 [9, 10, 11, 12]] 1955 for order in range(0, 6): 1956 out = ndimage.affine_transform(data, [[0.5, 0], 1957 [0, 1]], 0, 1958 (6, 4), order=order) 1959 assert_array_almost_equal(out[::2, ...], data)
1960
1961 - def test_affine_transform18(self):
1962 "affine transform 18" 1963 data = [[1, 2, 3, 4], 1964 [5, 6, 7, 8], 1965 [9, 10, 11, 12]] 1966 for order in range(0, 6): 1967 out = ndimage.affine_transform(data, 1968 [[0.5, 0], 1969 [0, 0.5]], 0, 1970 (6, 8), order=order) 1971 assert_array_almost_equal(out[::2, ::2], data)
1972
1973 - def test_affine_transform19(self):
1974 "affine transform 19" 1975 data = numpy.array([[1, 2, 3, 4], 1976 [5, 6, 7, 8], 1977 [9, 10, 11, 12]], numpy.float64) 1978 for order in range(0, 6): 1979 out = ndimage.affine_transform(data, 1980 [[0.5, 0], 1981 [0, 0.5]], 0, 1982 (6, 8), order=order) 1983 out = ndimage.affine_transform(out, 1984 [[2.0, 0], 1985 [0, 2.0]], 0, 1986 (3, 4), order=order) 1987 assert_array_almost_equal(out, data)
1988
1989 - def test_affine_transform20(self):
1990 "affine transform 20" 1991 data = [[1, 2, 3, 4], 1992 [5, 6, 7, 8], 1993 [9, 10, 11, 12]] 1994 for order in range(0, 6): 1995 out = ndimage.affine_transform(data, [[0], [2]], 0, 1996 (2,), order=order) 1997 assert_array_almost_equal(out, [1, 3])
1998
1999 - def test_affine_transform21(self):
2000 "affine transform 21" 2001 data = [[1, 2, 3, 4], 2002 [5, 6, 7, 8], 2003 [9, 10, 11, 12]] 2004 for order in range(0, 6): 2005 out = ndimage.affine_transform(data, [[2], [0]], 0, 2006 (2,), order=order) 2007 assert_array_almost_equal(out, [1, 9])
2008
2009 - def test_shift01(self):
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
2016 - def test_shift02(self):
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
2023 - def test_shift03(self):
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
2030 - def test_shift04(self):
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
2037 - def test_shift05(self):
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
2048 - def test_shift06(self):
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
2059 - def test_shift07(self):
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
2071 - def test_shift08(self):
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
2082 - def test_shift09(self):
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
2099 - def test_zoom1(self):
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
2112 - def test_zoom2(self):
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
2118 - def test_zoom_affine01(self):
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
2128 - def test_rotate01(self):
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
2137 - def test_rotate02(self):
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
2150 - def test_rotate03(self):
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
2164 - def test_rotate04(self):
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
2176 - def test_rotate05(self):
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
2194 - def test_rotate06(self):
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
2212 - def test_rotate07(self):
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
2230 - def test_rotate08(self):
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
2247 - def test_watershed_ift01(self):
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
2280 - def test_watershed_ift02(self):
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
2310 - def test_watershed_ift03(self):
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
2337 - def test_watershed_ift04(self):
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
2367 - def test_watershed_ift05(self):
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
2397 - def test_watershed_ift06(self):
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
2424 - def test_watershed_ift07(self):
2425 "watershed_ift 7" 2426 shape = (7, 6) 2427 data = numpy.