test_measurements.py 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  1. import os.path
  2. import numpy as np
  3. from numpy.testing import (assert_, assert_array_almost_equal, assert_equal,
  4. assert_almost_equal, assert_array_equal,
  5. suppress_warnings)
  6. from pytest import raises as assert_raises
  7. import scipy.ndimage as ndimage
  8. from . import types
  9. class Test_measurements_stats:
  10. """ndimage._measurements._stats() is a utility used by other functions."""
  11. def test_a(self):
  12. x = [0, 1, 2, 6]
  13. labels = [0, 0, 1, 1]
  14. index = [0, 1]
  15. for shp in [(4,), (2, 2)]:
  16. x = np.array(x).reshape(shp)
  17. labels = np.array(labels).reshape(shp)
  18. counts, sums = ndimage._measurements._stats(
  19. x, labels=labels, index=index)
  20. assert_array_equal(counts, [2, 2])
  21. assert_array_equal(sums, [1.0, 8.0])
  22. def test_b(self):
  23. # Same data as test_a, but different labels. The label 9 exceeds the
  24. # length of 'labels', so this test will follow a different code path.
  25. x = [0, 1, 2, 6]
  26. labels = [0, 0, 9, 9]
  27. index = [0, 9]
  28. for shp in [(4,), (2, 2)]:
  29. x = np.array(x).reshape(shp)
  30. labels = np.array(labels).reshape(shp)
  31. counts, sums = ndimage._measurements._stats(
  32. x, labels=labels, index=index)
  33. assert_array_equal(counts, [2, 2])
  34. assert_array_equal(sums, [1.0, 8.0])
  35. def test_a_centered(self):
  36. x = [0, 1, 2, 6]
  37. labels = [0, 0, 1, 1]
  38. index = [0, 1]
  39. for shp in [(4,), (2, 2)]:
  40. x = np.array(x).reshape(shp)
  41. labels = np.array(labels).reshape(shp)
  42. counts, sums, centers = ndimage._measurements._stats(
  43. x, labels=labels, index=index, centered=True)
  44. assert_array_equal(counts, [2, 2])
  45. assert_array_equal(sums, [1.0, 8.0])
  46. assert_array_equal(centers, [0.5, 8.0])
  47. def test_b_centered(self):
  48. x = [0, 1, 2, 6]
  49. labels = [0, 0, 9, 9]
  50. index = [0, 9]
  51. for shp in [(4,), (2, 2)]:
  52. x = np.array(x).reshape(shp)
  53. labels = np.array(labels).reshape(shp)
  54. counts, sums, centers = ndimage._measurements._stats(
  55. x, labels=labels, index=index, centered=True)
  56. assert_array_equal(counts, [2, 2])
  57. assert_array_equal(sums, [1.0, 8.0])
  58. assert_array_equal(centers, [0.5, 8.0])
  59. def test_nonint_labels(self):
  60. x = [0, 1, 2, 6]
  61. labels = [0.0, 0.0, 9.0, 9.0]
  62. index = [0.0, 9.0]
  63. for shp in [(4,), (2, 2)]:
  64. x = np.array(x).reshape(shp)
  65. labels = np.array(labels).reshape(shp)
  66. counts, sums, centers = ndimage._measurements._stats(
  67. x, labels=labels, index=index, centered=True)
  68. assert_array_equal(counts, [2, 2])
  69. assert_array_equal(sums, [1.0, 8.0])
  70. assert_array_equal(centers, [0.5, 8.0])
  71. class Test_measurements_select:
  72. """ndimage._measurements._select() is a utility used by other functions."""
  73. def test_basic(self):
  74. x = [0, 1, 6, 2]
  75. cases = [
  76. ([0, 0, 1, 1], [0, 1]), # "Small" integer labels
  77. ([0, 0, 9, 9], [0, 9]), # A label larger than len(labels)
  78. ([0.0, 0.0, 7.0, 7.0], [0.0, 7.0]), # Non-integer labels
  79. ]
  80. for labels, index in cases:
  81. result = ndimage._measurements._select(
  82. x, labels=labels, index=index)
  83. assert_(len(result) == 0)
  84. result = ndimage._measurements._select(
  85. x, labels=labels, index=index, find_max=True)
  86. assert_(len(result) == 1)
  87. assert_array_equal(result[0], [1, 6])
  88. result = ndimage._measurements._select(
  89. x, labels=labels, index=index, find_min=True)
  90. assert_(len(result) == 1)
  91. assert_array_equal(result[0], [0, 2])
  92. result = ndimage._measurements._select(
  93. x, labels=labels, index=index, find_min=True,
  94. find_min_positions=True)
  95. assert_(len(result) == 2)
  96. assert_array_equal(result[0], [0, 2])
  97. assert_array_equal(result[1], [0, 3])
  98. assert_equal(result[1].dtype.kind, 'i')
  99. result = ndimage._measurements._select(
  100. x, labels=labels, index=index, find_max=True,
  101. find_max_positions=True)
  102. assert_(len(result) == 2)
  103. assert_array_equal(result[0], [1, 6])
  104. assert_array_equal(result[1], [1, 2])
  105. assert_equal(result[1].dtype.kind, 'i')
  106. def test_label01():
  107. data = np.ones([])
  108. out, n = ndimage.label(data)
  109. assert_array_almost_equal(out, 1)
  110. assert_equal(n, 1)
  111. def test_label02():
  112. data = np.zeros([])
  113. out, n = ndimage.label(data)
  114. assert_array_almost_equal(out, 0)
  115. assert_equal(n, 0)
  116. def test_label03():
  117. data = np.ones([1])
  118. out, n = ndimage.label(data)
  119. assert_array_almost_equal(out, [1])
  120. assert_equal(n, 1)
  121. def test_label04():
  122. data = np.zeros([1])
  123. out, n = ndimage.label(data)
  124. assert_array_almost_equal(out, [0])
  125. assert_equal(n, 0)
  126. def test_label05():
  127. data = np.ones([5])
  128. out, n = ndimage.label(data)
  129. assert_array_almost_equal(out, [1, 1, 1, 1, 1])
  130. assert_equal(n, 1)
  131. def test_label06():
  132. data = np.array([1, 0, 1, 1, 0, 1])
  133. out, n = ndimage.label(data)
  134. assert_array_almost_equal(out, [1, 0, 2, 2, 0, 3])
  135. assert_equal(n, 3)
  136. def test_label07():
  137. data = np.array([[0, 0, 0, 0, 0, 0],
  138. [0, 0, 0, 0, 0, 0],
  139. [0, 0, 0, 0, 0, 0],
  140. [0, 0, 0, 0, 0, 0],
  141. [0, 0, 0, 0, 0, 0],
  142. [0, 0, 0, 0, 0, 0]])
  143. out, n = ndimage.label(data)
  144. assert_array_almost_equal(out, [[0, 0, 0, 0, 0, 0],
  145. [0, 0, 0, 0, 0, 0],
  146. [0, 0, 0, 0, 0, 0],
  147. [0, 0, 0, 0, 0, 0],
  148. [0, 0, 0, 0, 0, 0],
  149. [0, 0, 0, 0, 0, 0]])
  150. assert_equal(n, 0)
  151. def test_label08():
  152. data = np.array([[1, 0, 0, 0, 0, 0],
  153. [0, 0, 1, 1, 0, 0],
  154. [0, 0, 1, 1, 1, 0],
  155. [1, 1, 0, 0, 0, 0],
  156. [1, 1, 0, 0, 0, 0],
  157. [0, 0, 0, 1, 1, 0]])
  158. out, n = ndimage.label(data)
  159. assert_array_almost_equal(out, [[1, 0, 0, 0, 0, 0],
  160. [0, 0, 2, 2, 0, 0],
  161. [0, 0, 2, 2, 2, 0],
  162. [3, 3, 0, 0, 0, 0],
  163. [3, 3, 0, 0, 0, 0],
  164. [0, 0, 0, 4, 4, 0]])
  165. assert_equal(n, 4)
  166. def test_label09():
  167. data = np.array([[1, 0, 0, 0, 0, 0],
  168. [0, 0, 1, 1, 0, 0],
  169. [0, 0, 1, 1, 1, 0],
  170. [1, 1, 0, 0, 0, 0],
  171. [1, 1, 0, 0, 0, 0],
  172. [0, 0, 0, 1, 1, 0]])
  173. struct = ndimage.generate_binary_structure(2, 2)
  174. out, n = ndimage.label(data, struct)
  175. assert_array_almost_equal(out, [[1, 0, 0, 0, 0, 0],
  176. [0, 0, 2, 2, 0, 0],
  177. [0, 0, 2, 2, 2, 0],
  178. [2, 2, 0, 0, 0, 0],
  179. [2, 2, 0, 0, 0, 0],
  180. [0, 0, 0, 3, 3, 0]])
  181. assert_equal(n, 3)
  182. def test_label10():
  183. data = np.array([[0, 0, 0, 0, 0, 0],
  184. [0, 1, 1, 0, 1, 0],
  185. [0, 1, 1, 1, 1, 0],
  186. [0, 0, 0, 0, 0, 0]])
  187. struct = ndimage.generate_binary_structure(2, 2)
  188. out, n = ndimage.label(data, struct)
  189. assert_array_almost_equal(out, [[0, 0, 0, 0, 0, 0],
  190. [0, 1, 1, 0, 1, 0],
  191. [0, 1, 1, 1, 1, 0],
  192. [0, 0, 0, 0, 0, 0]])
  193. assert_equal(n, 1)
  194. def test_label11():
  195. for type in types:
  196. data = np.array([[1, 0, 0, 0, 0, 0],
  197. [0, 0, 1, 1, 0, 0],
  198. [0, 0, 1, 1, 1, 0],
  199. [1, 1, 0, 0, 0, 0],
  200. [1, 1, 0, 0, 0, 0],
  201. [0, 0, 0, 1, 1, 0]], type)
  202. out, n = ndimage.label(data)
  203. expected = [[1, 0, 0, 0, 0, 0],
  204. [0, 0, 2, 2, 0, 0],
  205. [0, 0, 2, 2, 2, 0],
  206. [3, 3, 0, 0, 0, 0],
  207. [3, 3, 0, 0, 0, 0],
  208. [0, 0, 0, 4, 4, 0]]
  209. assert_array_almost_equal(out, expected)
  210. assert_equal(n, 4)
  211. def test_label11_inplace():
  212. for type in types:
  213. data = np.array([[1, 0, 0, 0, 0, 0],
  214. [0, 0, 1, 1, 0, 0],
  215. [0, 0, 1, 1, 1, 0],
  216. [1, 1, 0, 0, 0, 0],
  217. [1, 1, 0, 0, 0, 0],
  218. [0, 0, 0, 1, 1, 0]], type)
  219. n = ndimage.label(data, output=data)
  220. expected = [[1, 0, 0, 0, 0, 0],
  221. [0, 0, 2, 2, 0, 0],
  222. [0, 0, 2, 2, 2, 0],
  223. [3, 3, 0, 0, 0, 0],
  224. [3, 3, 0, 0, 0, 0],
  225. [0, 0, 0, 4, 4, 0]]
  226. assert_array_almost_equal(data, expected)
  227. assert_equal(n, 4)
  228. def test_label12():
  229. for type in types:
  230. data = np.array([[0, 0, 0, 0, 1, 1],
  231. [0, 0, 0, 0, 0, 1],
  232. [0, 0, 1, 0, 1, 1],
  233. [0, 0, 1, 1, 1, 1],
  234. [0, 0, 0, 1, 1, 0]], type)
  235. out, n = ndimage.label(data)
  236. expected = [[0, 0, 0, 0, 1, 1],
  237. [0, 0, 0, 0, 0, 1],
  238. [0, 0, 1, 0, 1, 1],
  239. [0, 0, 1, 1, 1, 1],
  240. [0, 0, 0, 1, 1, 0]]
  241. assert_array_almost_equal(out, expected)
  242. assert_equal(n, 1)
  243. def test_label13():
  244. for type in types:
  245. data = np.array([[1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1],
  246. [1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1],
  247. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  248. [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]],
  249. type)
  250. out, n = ndimage.label(data)
  251. expected = [[1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1],
  252. [1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1],
  253. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  254. [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
  255. assert_array_almost_equal(out, expected)
  256. assert_equal(n, 1)
  257. def test_label_output_typed():
  258. data = np.ones([5])
  259. for t in types:
  260. output = np.zeros([5], dtype=t)
  261. n = ndimage.label(data, output=output)
  262. assert_array_almost_equal(output, 1)
  263. assert_equal(n, 1)
  264. def test_label_output_dtype():
  265. data = np.ones([5])
  266. for t in types:
  267. output, n = ndimage.label(data, output=t)
  268. assert_array_almost_equal(output, 1)
  269. assert output.dtype == t
  270. def test_label_output_wrong_size():
  271. data = np.ones([5])
  272. for t in types:
  273. output = np.zeros([10], t)
  274. assert_raises((RuntimeError, ValueError),
  275. ndimage.label, data, output=output)
  276. def test_label_structuring_elements():
  277. data = np.loadtxt(os.path.join(os.path.dirname(
  278. __file__), "data", "label_inputs.txt"))
  279. strels = np.loadtxt(os.path.join(
  280. os.path.dirname(__file__), "data", "label_strels.txt"))
  281. results = np.loadtxt(os.path.join(
  282. os.path.dirname(__file__), "data", "label_results.txt"))
  283. data = data.reshape((-1, 7, 7))
  284. strels = strels.reshape((-1, 3, 3))
  285. results = results.reshape((-1, 7, 7))
  286. r = 0
  287. for i in range(data.shape[0]):
  288. d = data[i, :, :]
  289. for j in range(strels.shape[0]):
  290. s = strels[j, :, :]
  291. assert_equal(ndimage.label(d, s)[0], results[r, :, :])
  292. r += 1
  293. def test_ticket_742():
  294. def SE(img, thresh=.7, size=4):
  295. mask = img > thresh
  296. rank = len(mask.shape)
  297. la, co = ndimage.label(mask,
  298. ndimage.generate_binary_structure(rank, rank))
  299. _ = ndimage.find_objects(la)
  300. if np.dtype(np.intp) != np.dtype('i'):
  301. shape = (3, 1240, 1240)
  302. a = np.random.rand(np.prod(shape)).reshape(shape)
  303. # shouldn't crash
  304. SE(a)
  305. def test_gh_issue_3025():
  306. """Github issue #3025 - improper merging of labels"""
  307. d = np.zeros((60, 320))
  308. d[:, :257] = 1
  309. d[:, 260:] = 1
  310. d[36, 257] = 1
  311. d[35, 258] = 1
  312. d[35, 259] = 1
  313. assert ndimage.label(d, np.ones((3, 3)))[1] == 1
  314. def test_label_default_dtype():
  315. test_array = np.random.rand(10, 10)
  316. label, no_features = ndimage.label(test_array > 0.5)
  317. assert_(label.dtype in (np.int32, np.int64))
  318. # Shouldn't raise an exception
  319. ndimage.find_objects(label)
  320. def test_find_objects01():
  321. data = np.ones([], dtype=int)
  322. out = ndimage.find_objects(data)
  323. assert_(out == [()])
  324. def test_find_objects02():
  325. data = np.zeros([], dtype=int)
  326. out = ndimage.find_objects(data)
  327. assert_(out == [])
  328. def test_find_objects03():
  329. data = np.ones([1], dtype=int)
  330. out = ndimage.find_objects(data)
  331. assert_equal(out, [(slice(0, 1, None),)])
  332. def test_find_objects04():
  333. data = np.zeros([1], dtype=int)
  334. out = ndimage.find_objects(data)
  335. assert_equal(out, [])
  336. def test_find_objects05():
  337. data = np.ones([5], dtype=int)
  338. out = ndimage.find_objects(data)
  339. assert_equal(out, [(slice(0, 5, None),)])
  340. def test_find_objects06():
  341. data = np.array([1, 0, 2, 2, 0, 3])
  342. out = ndimage.find_objects(data)
  343. assert_equal(out, [(slice(0, 1, None),),
  344. (slice(2, 4, None),),
  345. (slice(5, 6, None),)])
  346. def test_find_objects07():
  347. data = np.array([[0, 0, 0, 0, 0, 0],
  348. [0, 0, 0, 0, 0, 0],
  349. [0, 0, 0, 0, 0, 0],
  350. [0, 0, 0, 0, 0, 0],
  351. [0, 0, 0, 0, 0, 0],
  352. [0, 0, 0, 0, 0, 0]])
  353. out = ndimage.find_objects(data)
  354. assert_equal(out, [])
  355. def test_find_objects08():
  356. data = np.array([[1, 0, 0, 0, 0, 0],
  357. [0, 0, 2, 2, 0, 0],
  358. [0, 0, 2, 2, 2, 0],
  359. [3, 3, 0, 0, 0, 0],
  360. [3, 3, 0, 0, 0, 0],
  361. [0, 0, 0, 4, 4, 0]])
  362. out = ndimage.find_objects(data)
  363. assert_equal(out, [(slice(0, 1, None), slice(0, 1, None)),
  364. (slice(1, 3, None), slice(2, 5, None)),
  365. (slice(3, 5, None), slice(0, 2, None)),
  366. (slice(5, 6, None), slice(3, 5, None))])
  367. def test_find_objects09():
  368. data = np.array([[1, 0, 0, 0, 0, 0],
  369. [0, 0, 2, 2, 0, 0],
  370. [0, 0, 2, 2, 2, 0],
  371. [0, 0, 0, 0, 0, 0],
  372. [0, 0, 0, 0, 0, 0],
  373. [0, 0, 0, 4, 4, 0]])
  374. out = ndimage.find_objects(data)
  375. assert_equal(out, [(slice(0, 1, None), slice(0, 1, None)),
  376. (slice(1, 3, None), slice(2, 5, None)),
  377. None,
  378. (slice(5, 6, None), slice(3, 5, None))])
  379. def test_value_indices01():
  380. "Test dictionary keys and entries"
  381. data = np.array([[1, 0, 0, 0, 0, 0],
  382. [0, 0, 2, 2, 0, 0],
  383. [0, 0, 2, 2, 2, 0],
  384. [0, 0, 0, 0, 0, 0],
  385. [0, 0, 0, 0, 0, 0],
  386. [0, 0, 0, 4, 4, 0]])
  387. vi = ndimage.value_indices(data, ignore_value=0)
  388. true_keys = [1, 2, 4]
  389. assert_equal(list(vi.keys()), true_keys)
  390. truevi = {}
  391. for k in true_keys:
  392. truevi[k] = np.where(data == k)
  393. vi = ndimage.value_indices(data, ignore_value=0)
  394. assert_equal(vi, truevi)
  395. def test_value_indices02():
  396. "Test input checking"
  397. data = np.zeros((5, 4), dtype=np.float32)
  398. msg = "Parameter 'arr' must be an integer array"
  399. with assert_raises(ValueError, match=msg):
  400. ndimage.value_indices(data)
  401. def test_value_indices03():
  402. "Test different input array shapes, from 1-D to 4-D"
  403. for shape in [(36,), (18, 2), (3, 3, 4), (3, 3, 2, 2)]:
  404. a = np.array((12*[1]+12*[2]+12*[3]), dtype=np.int32).reshape(shape)
  405. trueKeys = np.unique(a)
  406. vi = ndimage.value_indices(a)
  407. assert_equal(list(vi.keys()), list(trueKeys))
  408. for k in trueKeys:
  409. trueNdx = np.where(a == k)
  410. assert_equal(vi[k], trueNdx)
  411. def test_sum01():
  412. for type in types:
  413. input = np.array([], type)
  414. output = ndimage.sum(input)
  415. assert_equal(output, 0.0)
  416. def test_sum02():
  417. for type in types:
  418. input = np.zeros([0, 4], type)
  419. output = ndimage.sum(input)
  420. assert_equal(output, 0.0)
  421. def test_sum03():
  422. for type in types:
  423. input = np.ones([], type)
  424. output = ndimage.sum(input)
  425. assert_almost_equal(output, 1.0)
  426. def test_sum04():
  427. for type in types:
  428. input = np.array([1, 2], type)
  429. output = ndimage.sum(input)
  430. assert_almost_equal(output, 3.0)
  431. def test_sum05():
  432. for type in types:
  433. input = np.array([[1, 2], [3, 4]], type)
  434. output = ndimage.sum(input)
  435. assert_almost_equal(output, 10.0)
  436. def test_sum06():
  437. labels = np.array([], bool)
  438. for type in types:
  439. input = np.array([], type)
  440. output = ndimage.sum(input, labels=labels)
  441. assert_equal(output, 0.0)
  442. def test_sum07():
  443. labels = np.ones([0, 4], bool)
  444. for type in types:
  445. input = np.zeros([0, 4], type)
  446. output = ndimage.sum(input, labels=labels)
  447. assert_equal(output, 0.0)
  448. def test_sum08():
  449. labels = np.array([1, 0], bool)
  450. for type in types:
  451. input = np.array([1, 2], type)
  452. output = ndimage.sum(input, labels=labels)
  453. assert_equal(output, 1.0)
  454. def test_sum09():
  455. labels = np.array([1, 0], bool)
  456. for type in types:
  457. input = np.array([[1, 2], [3, 4]], type)
  458. output = ndimage.sum(input, labels=labels)
  459. assert_almost_equal(output, 4.0)
  460. def test_sum10():
  461. labels = np.array([1, 0], bool)
  462. input = np.array([[1, 2], [3, 4]], bool)
  463. output = ndimage.sum(input, labels=labels)
  464. assert_almost_equal(output, 2.0)
  465. def test_sum11():
  466. labels = np.array([1, 2], np.int8)
  467. for type in types:
  468. input = np.array([[1, 2], [3, 4]], type)
  469. output = ndimage.sum(input, labels=labels,
  470. index=2)
  471. assert_almost_equal(output, 6.0)
  472. def test_sum12():
  473. labels = np.array([[1, 2], [2, 4]], np.int8)
  474. for type in types:
  475. input = np.array([[1, 2], [3, 4]], type)
  476. output = ndimage.sum(input, labels=labels, index=[4, 8, 2])
  477. assert_array_almost_equal(output, [4.0, 0.0, 5.0])
  478. def test_sum_labels():
  479. labels = np.array([[1, 2], [2, 4]], np.int8)
  480. for type in types:
  481. input = np.array([[1, 2], [3, 4]], type)
  482. output_sum = ndimage.sum(input, labels=labels, index=[4, 8, 2])
  483. output_labels = ndimage.sum_labels(
  484. input, labels=labels, index=[4, 8, 2])
  485. assert (output_sum == output_labels).all()
  486. assert_array_almost_equal(output_labels, [4.0, 0.0, 5.0])
  487. def test_mean01():
  488. labels = np.array([1, 0], bool)
  489. for type in types:
  490. input = np.array([[1, 2], [3, 4]], type)
  491. output = ndimage.mean(input, labels=labels)
  492. assert_almost_equal(output, 2.0)
  493. def test_mean02():
  494. labels = np.array([1, 0], bool)
  495. input = np.array([[1, 2], [3, 4]], bool)
  496. output = ndimage.mean(input, labels=labels)
  497. assert_almost_equal(output, 1.0)
  498. def test_mean03():
  499. labels = np.array([1, 2])
  500. for type in types:
  501. input = np.array([[1, 2], [3, 4]], type)
  502. output = ndimage.mean(input, labels=labels,
  503. index=2)
  504. assert_almost_equal(output, 3.0)
  505. def test_mean04():
  506. labels = np.array([[1, 2], [2, 4]], np.int8)
  507. with np.errstate(all='ignore'):
  508. for type in types:
  509. input = np.array([[1, 2], [3, 4]], type)
  510. output = ndimage.mean(input, labels=labels,
  511. index=[4, 8, 2])
  512. assert_array_almost_equal(output[[0, 2]], [4.0, 2.5])
  513. assert_(np.isnan(output[1]))
  514. def test_minimum01():
  515. labels = np.array([1, 0], bool)
  516. for type in types:
  517. input = np.array([[1, 2], [3, 4]], type)
  518. output = ndimage.minimum(input, labels=labels)
  519. assert_almost_equal(output, 1.0)
  520. def test_minimum02():
  521. labels = np.array([1, 0], bool)
  522. input = np.array([[2, 2], [2, 4]], bool)
  523. output = ndimage.minimum(input, labels=labels)
  524. assert_almost_equal(output, 1.0)
  525. def test_minimum03():
  526. labels = np.array([1, 2])
  527. for type in types:
  528. input = np.array([[1, 2], [3, 4]], type)
  529. output = ndimage.minimum(input, labels=labels,
  530. index=2)
  531. assert_almost_equal(output, 2.0)
  532. def test_minimum04():
  533. labels = np.array([[1, 2], [2, 3]])
  534. for type in types:
  535. input = np.array([[1, 2], [3, 4]], type)
  536. output = ndimage.minimum(input, labels=labels,
  537. index=[2, 3, 8])
  538. assert_array_almost_equal(output, [2.0, 4.0, 0.0])
  539. def test_maximum01():
  540. labels = np.array([1, 0], bool)
  541. for type in types:
  542. input = np.array([[1, 2], [3, 4]], type)
  543. output = ndimage.maximum(input, labels=labels)
  544. assert_almost_equal(output, 3.0)
  545. def test_maximum02():
  546. labels = np.array([1, 0], bool)
  547. input = np.array([[2, 2], [2, 4]], bool)
  548. output = ndimage.maximum(input, labels=labels)
  549. assert_almost_equal(output, 1.0)
  550. def test_maximum03():
  551. labels = np.array([1, 2])
  552. for type in types:
  553. input = np.array([[1, 2], [3, 4]], type)
  554. output = ndimage.maximum(input, labels=labels,
  555. index=2)
  556. assert_almost_equal(output, 4.0)
  557. def test_maximum04():
  558. labels = np.array([[1, 2], [2, 3]])
  559. for type in types:
  560. input = np.array([[1, 2], [3, 4]], type)
  561. output = ndimage.maximum(input, labels=labels,
  562. index=[2, 3, 8])
  563. assert_array_almost_equal(output, [3.0, 4.0, 0.0])
  564. def test_maximum05():
  565. # Regression test for ticket #501 (Trac)
  566. x = np.array([-3, -2, -1])
  567. assert_equal(ndimage.maximum(x), -1)
  568. def test_median01():
  569. a = np.array([[1, 2, 0, 1],
  570. [5, 3, 0, 4],
  571. [0, 0, 0, 7],
  572. [9, 3, 0, 0]])
  573. labels = np.array([[1, 1, 0, 2],
  574. [1, 1, 0, 2],
  575. [0, 0, 0, 2],
  576. [3, 3, 0, 0]])
  577. output = ndimage.median(a, labels=labels, index=[1, 2, 3])
  578. assert_array_almost_equal(output, [2.5, 4.0, 6.0])
  579. def test_median02():
  580. a = np.array([[1, 2, 0, 1],
  581. [5, 3, 0, 4],
  582. [0, 0, 0, 7],
  583. [9, 3, 0, 0]])
  584. output = ndimage.median(a)
  585. assert_almost_equal(output, 1.0)
  586. def test_median03():
  587. a = np.array([[1, 2, 0, 1],
  588. [5, 3, 0, 4],
  589. [0, 0, 0, 7],
  590. [9, 3, 0, 0]])
  591. labels = np.array([[1, 1, 0, 2],
  592. [1, 1, 0, 2],
  593. [0, 0, 0, 2],
  594. [3, 3, 0, 0]])
  595. output = ndimage.median(a, labels=labels)
  596. assert_almost_equal(output, 3.0)
  597. def test_median_gh12836_bool():
  598. # test boolean addition fix on example from gh-12836
  599. a = np.asarray([1, 1], dtype=bool)
  600. output = ndimage.median(a, labels=np.ones((2,)), index=[1])
  601. assert_array_almost_equal(output, [1.0])
  602. def test_median_no_int_overflow():
  603. # test integer overflow fix on example from gh-12836
  604. a = np.asarray([65, 70], dtype=np.int8)
  605. output = ndimage.median(a, labels=np.ones((2,)), index=[1])
  606. assert_array_almost_equal(output, [67.5])
  607. def test_variance01():
  608. with np.errstate(all='ignore'):
  609. for type in types:
  610. input = np.array([], type)
  611. with suppress_warnings() as sup:
  612. sup.filter(RuntimeWarning, "Mean of empty slice")
  613. output = ndimage.variance(input)
  614. assert_(np.isnan(output))
  615. def test_variance02():
  616. for type in types:
  617. input = np.array([1], type)
  618. output = ndimage.variance(input)
  619. assert_almost_equal(output, 0.0)
  620. def test_variance03():
  621. for type in types:
  622. input = np.array([1, 3], type)
  623. output = ndimage.variance(input)
  624. assert_almost_equal(output, 1.0)
  625. def test_variance04():
  626. input = np.array([1, 0], bool)
  627. output = ndimage.variance(input)
  628. assert_almost_equal(output, 0.25)
  629. def test_variance05():
  630. labels = [2, 2, 3]
  631. for type in types:
  632. input = np.array([1, 3, 8], type)
  633. output = ndimage.variance(input, labels, 2)
  634. assert_almost_equal(output, 1.0)
  635. def test_variance06():
  636. labels = [2, 2, 3, 3, 4]
  637. with np.errstate(all='ignore'):
  638. for type in types:
  639. input = np.array([1, 3, 8, 10, 8], type)
  640. output = ndimage.variance(input, labels, [2, 3, 4])
  641. assert_array_almost_equal(output, [1.0, 1.0, 0.0])
  642. def test_standard_deviation01():
  643. with np.errstate(all='ignore'):
  644. for type in types:
  645. input = np.array([], type)
  646. with suppress_warnings() as sup:
  647. sup.filter(RuntimeWarning, "Mean of empty slice")
  648. output = ndimage.standard_deviation(input)
  649. assert_(np.isnan(output))
  650. def test_standard_deviation02():
  651. for type in types:
  652. input = np.array([1], type)
  653. output = ndimage.standard_deviation(input)
  654. assert_almost_equal(output, 0.0)
  655. def test_standard_deviation03():
  656. for type in types:
  657. input = np.array([1, 3], type)
  658. output = ndimage.standard_deviation(input)
  659. assert_almost_equal(output, np.sqrt(1.0))
  660. def test_standard_deviation04():
  661. input = np.array([1, 0], bool)
  662. output = ndimage.standard_deviation(input)
  663. assert_almost_equal(output, 0.5)
  664. def test_standard_deviation05():
  665. labels = [2, 2, 3]
  666. for type in types:
  667. input = np.array([1, 3, 8], type)
  668. output = ndimage.standard_deviation(input, labels, 2)
  669. assert_almost_equal(output, 1.0)
  670. def test_standard_deviation06():
  671. labels = [2, 2, 3, 3, 4]
  672. with np.errstate(all='ignore'):
  673. for type in types:
  674. input = np.array([1, 3, 8, 10, 8], type)
  675. output = ndimage.standard_deviation(input, labels, [2, 3, 4])
  676. assert_array_almost_equal(output, [1.0, 1.0, 0.0])
  677. def test_standard_deviation07():
  678. labels = [1]
  679. with np.errstate(all='ignore'):
  680. for type in types:
  681. input = np.array([-0.00619519], type)
  682. output = ndimage.standard_deviation(input, labels, [1])
  683. assert_array_almost_equal(output, [0])
  684. def test_minimum_position01():
  685. labels = np.array([1, 0], bool)
  686. for type in types:
  687. input = np.array([[1, 2], [3, 4]], type)
  688. output = ndimage.minimum_position(input, labels=labels)
  689. assert_equal(output, (0, 0))
  690. def test_minimum_position02():
  691. for type in types:
  692. input = np.array([[5, 4, 2, 5],
  693. [3, 7, 0, 2],
  694. [1, 5, 1, 1]], type)
  695. output = ndimage.minimum_position(input)
  696. assert_equal(output, (1, 2))
  697. def test_minimum_position03():
  698. input = np.array([[5, 4, 2, 5],
  699. [3, 7, 0, 2],
  700. [1, 5, 1, 1]], bool)
  701. output = ndimage.minimum_position(input)
  702. assert_equal(output, (1, 2))
  703. def test_minimum_position04():
  704. input = np.array([[5, 4, 2, 5],
  705. [3, 7, 1, 2],
  706. [1, 5, 1, 1]], bool)
  707. output = ndimage.minimum_position(input)
  708. assert_equal(output, (0, 0))
  709. def test_minimum_position05():
  710. labels = [1, 2, 0, 4]
  711. for type in types:
  712. input = np.array([[5, 4, 2, 5],
  713. [3, 7, 0, 2],
  714. [1, 5, 2, 3]], type)
  715. output = ndimage.minimum_position(input, labels)
  716. assert_equal(output, (2, 0))
  717. def test_minimum_position06():
  718. labels = [1, 2, 3, 4]
  719. for type in types:
  720. input = np.array([[5, 4, 2, 5],
  721. [3, 7, 0, 2],
  722. [1, 5, 1, 1]], type)
  723. output = ndimage.minimum_position(input, labels, 2)
  724. assert_equal(output, (0, 1))
  725. def test_minimum_position07():
  726. labels = [1, 2, 3, 4]
  727. for type in types:
  728. input = np.array([[5, 4, 2, 5],
  729. [3, 7, 0, 2],
  730. [1, 5, 1, 1]], type)
  731. output = ndimage.minimum_position(input, labels,
  732. [2, 3])
  733. assert_equal(output[0], (0, 1))
  734. assert_equal(output[1], (1, 2))
  735. def test_maximum_position01():
  736. labels = np.array([1, 0], bool)
  737. for type in types:
  738. input = np.array([[1, 2], [3, 4]], type)
  739. output = ndimage.maximum_position(input,
  740. labels=labels)
  741. assert_equal(output, (1, 0))
  742. def test_maximum_position02():
  743. for type in types:
  744. input = np.array([[5, 4, 2, 5],
  745. [3, 7, 8, 2],
  746. [1, 5, 1, 1]], type)
  747. output = ndimage.maximum_position(input)
  748. assert_equal(output, (1, 2))
  749. def test_maximum_position03():
  750. input = np.array([[5, 4, 2, 5],
  751. [3, 7, 8, 2],
  752. [1, 5, 1, 1]], bool)
  753. output = ndimage.maximum_position(input)
  754. assert_equal(output, (0, 0))
  755. def test_maximum_position04():
  756. labels = [1, 2, 0, 4]
  757. for type in types:
  758. input = np.array([[5, 4, 2, 5],
  759. [3, 7, 8, 2],
  760. [1, 5, 1, 1]], type)
  761. output = ndimage.maximum_position(input, labels)
  762. assert_equal(output, (1, 1))
  763. def test_maximum_position05():
  764. labels = [1, 2, 0, 4]
  765. for type in types:
  766. input = np.array([[5, 4, 2, 5],
  767. [3, 7, 8, 2],
  768. [1, 5, 1, 1]], type)
  769. output = ndimage.maximum_position(input, labels, 1)
  770. assert_equal(output, (0, 0))
  771. def test_maximum_position06():
  772. labels = [1, 2, 0, 4]
  773. for type in types:
  774. input = np.array([[5, 4, 2, 5],
  775. [3, 7, 8, 2],
  776. [1, 5, 1, 1]], type)
  777. output = ndimage.maximum_position(input, labels,
  778. [1, 2])
  779. assert_equal(output[0], (0, 0))
  780. assert_equal(output[1], (1, 1))
  781. def test_maximum_position07():
  782. # Test float labels
  783. labels = np.array([1.0, 2.5, 0.0, 4.5])
  784. for type in types:
  785. input = np.array([[5, 4, 2, 5],
  786. [3, 7, 8, 2],
  787. [1, 5, 1, 1]], type)
  788. output = ndimage.maximum_position(input, labels,
  789. [1.0, 4.5])
  790. assert_equal(output[0], (0, 0))
  791. assert_equal(output[1], (0, 3))
  792. def test_extrema01():
  793. labels = np.array([1, 0], bool)
  794. for type in types:
  795. input = np.array([[1, 2], [3, 4]], type)
  796. output1 = ndimage.extrema(input, labels=labels)
  797. output2 = ndimage.minimum(input, labels=labels)
  798. output3 = ndimage.maximum(input, labels=labels)
  799. output4 = ndimage.minimum_position(input,
  800. labels=labels)
  801. output5 = ndimage.maximum_position(input,
  802. labels=labels)
  803. assert_equal(output1, (output2, output3, output4, output5))
  804. def test_extrema02():
  805. labels = np.array([1, 2])
  806. for type in types:
  807. input = np.array([[1, 2], [3, 4]], type)
  808. output1 = ndimage.extrema(input, labels=labels,
  809. index=2)
  810. output2 = ndimage.minimum(input, labels=labels,
  811. index=2)
  812. output3 = ndimage.maximum(input, labels=labels,
  813. index=2)
  814. output4 = ndimage.minimum_position(input,
  815. labels=labels, index=2)
  816. output5 = ndimage.maximum_position(input,
  817. labels=labels, index=2)
  818. assert_equal(output1, (output2, output3, output4, output5))
  819. def test_extrema03():
  820. labels = np.array([[1, 2], [2, 3]])
  821. for type in types:
  822. input = np.array([[1, 2], [3, 4]], type)
  823. output1 = ndimage.extrema(input, labels=labels,
  824. index=[2, 3, 8])
  825. output2 = ndimage.minimum(input, labels=labels,
  826. index=[2, 3, 8])
  827. output3 = ndimage.maximum(input, labels=labels,
  828. index=[2, 3, 8])
  829. output4 = ndimage.minimum_position(input,
  830. labels=labels, index=[2, 3, 8])
  831. output5 = ndimage.maximum_position(input,
  832. labels=labels, index=[2, 3, 8])
  833. assert_array_almost_equal(output1[0], output2)
  834. assert_array_almost_equal(output1[1], output3)
  835. assert_array_almost_equal(output1[2], output4)
  836. assert_array_almost_equal(output1[3], output5)
  837. def test_extrema04():
  838. labels = [1, 2, 0, 4]
  839. for type in types:
  840. input = np.array([[5, 4, 2, 5],
  841. [3, 7, 8, 2],
  842. [1, 5, 1, 1]], type)
  843. output1 = ndimage.extrema(input, labels, [1, 2])
  844. output2 = ndimage.minimum(input, labels, [1, 2])
  845. output3 = ndimage.maximum(input, labels, [1, 2])
  846. output4 = ndimage.minimum_position(input, labels,
  847. [1, 2])
  848. output5 = ndimage.maximum_position(input, labels,
  849. [1, 2])
  850. assert_array_almost_equal(output1[0], output2)
  851. assert_array_almost_equal(output1[1], output3)
  852. assert_array_almost_equal(output1[2], output4)
  853. assert_array_almost_equal(output1[3], output5)
  854. def test_center_of_mass01():
  855. expected = [0.0, 0.0]
  856. for type in types:
  857. input = np.array([[1, 0], [0, 0]], type)
  858. output = ndimage.center_of_mass(input)
  859. assert_array_almost_equal(output, expected)
  860. def test_center_of_mass02():
  861. expected = [1, 0]
  862. for type in types:
  863. input = np.array([[0, 0], [1, 0]], type)
  864. output = ndimage.center_of_mass(input)
  865. assert_array_almost_equal(output, expected)
  866. def test_center_of_mass03():
  867. expected = [0, 1]
  868. for type in types:
  869. input = np.array([[0, 1], [0, 0]], type)
  870. output = ndimage.center_of_mass(input)
  871. assert_array_almost_equal(output, expected)
  872. def test_center_of_mass04():
  873. expected = [1, 1]
  874. for type in types:
  875. input = np.array([[0, 0], [0, 1]], type)
  876. output = ndimage.center_of_mass(input)
  877. assert_array_almost_equal(output, expected)
  878. def test_center_of_mass05():
  879. expected = [0.5, 0.5]
  880. for type in types:
  881. input = np.array([[1, 1], [1, 1]], type)
  882. output = ndimage.center_of_mass(input)
  883. assert_array_almost_equal(output, expected)
  884. def test_center_of_mass06():
  885. expected = [0.5, 0.5]
  886. input = np.array([[1, 2], [3, 1]], bool)
  887. output = ndimage.center_of_mass(input)
  888. assert_array_almost_equal(output, expected)
  889. def test_center_of_mass07():
  890. labels = [1, 0]
  891. expected = [0.5, 0.0]
  892. input = np.array([[1, 2], [3, 1]], bool)
  893. output = ndimage.center_of_mass(input, labels)
  894. assert_array_almost_equal(output, expected)
  895. def test_center_of_mass08():
  896. labels = [1, 2]
  897. expected = [0.5, 1.0]
  898. input = np.array([[5, 2], [3, 1]], bool)
  899. output = ndimage.center_of_mass(input, labels, 2)
  900. assert_array_almost_equal(output, expected)
  901. def test_center_of_mass09():
  902. labels = [1, 2]
  903. expected = [(0.5, 0.0), (0.5, 1.0)]
  904. input = np.array([[1, 2], [1, 1]], bool)
  905. output = ndimage.center_of_mass(input, labels, [1, 2])
  906. assert_array_almost_equal(output, expected)
  907. def test_histogram01():
  908. expected = np.ones(10)
  909. input = np.arange(10)
  910. output = ndimage.histogram(input, 0, 10, 10)
  911. assert_array_almost_equal(output, expected)
  912. def test_histogram02():
  913. labels = [1, 1, 1, 1, 2, 2, 2, 2]
  914. expected = [0, 2, 0, 1, 1]
  915. input = np.array([1, 1, 3, 4, 3, 3, 3, 3])
  916. output = ndimage.histogram(input, 0, 4, 5, labels, 1)
  917. assert_array_almost_equal(output, expected)
  918. def test_histogram03():
  919. labels = [1, 0, 1, 1, 2, 2, 2, 2]
  920. expected1 = [0, 1, 0, 1, 1]
  921. expected2 = [0, 0, 0, 3, 0]
  922. input = np.array([1, 1, 3, 4, 3, 5, 3, 3])
  923. output = ndimage.histogram(input, 0, 4, 5, labels, (1, 2))
  924. assert_array_almost_equal(output[0], expected1)
  925. assert_array_almost_equal(output[1], expected2)
  926. def test_stat_funcs_2d():
  927. a = np.array([[5, 6, 0, 0, 0], [8, 9, 0, 0, 0], [0, 0, 0, 3, 5]])
  928. lbl = np.array([[1, 1, 0, 0, 0], [1, 1, 0, 0, 0], [0, 0, 0, 2, 2]])
  929. mean = ndimage.mean(a, labels=lbl, index=[1, 2])
  930. assert_array_equal(mean, [7.0, 4.0])
  931. var = ndimage.variance(a, labels=lbl, index=[1, 2])
  932. assert_array_equal(var, [2.5, 1.0])
  933. std = ndimage.standard_deviation(a, labels=lbl, index=[1, 2])
  934. assert_array_almost_equal(std, np.sqrt([2.5, 1.0]))
  935. med = ndimage.median(a, labels=lbl, index=[1, 2])
  936. assert_array_equal(med, [7.0, 4.0])
  937. min = ndimage.minimum(a, labels=lbl, index=[1, 2])
  938. assert_array_equal(min, [5, 3])
  939. max = ndimage.maximum(a, labels=lbl, index=[1, 2])
  940. assert_array_equal(max, [9, 5])
  941. class TestWatershedIft:
  942. def test_watershed_ift01(self):
  943. data = np.array([[0, 0, 0, 0, 0, 0, 0],
  944. [0, 1, 1, 1, 1, 1, 0],
  945. [0, 1, 0, 0, 0, 1, 0],
  946. [0, 1, 0, 0, 0, 1, 0],
  947. [0, 1, 0, 0, 0, 1, 0],
  948. [0, 1, 1, 1, 1, 1, 0],
  949. [0, 0, 0, 0, 0, 0, 0],
  950. [0, 0, 0, 0, 0, 0, 0]], np.uint8)
  951. markers = np.array([[-1, 0, 0, 0, 0, 0, 0],
  952. [0, 0, 0, 0, 0, 0, 0],
  953. [0, 0, 0, 0, 0, 0, 0],
  954. [0, 0, 0, 1, 0, 0, 0],
  955. [0, 0, 0, 0, 0, 0, 0],
  956. [0, 0, 0, 0, 0, 0, 0],
  957. [0, 0, 0, 0, 0, 0, 0],
  958. [0, 0, 0, 0, 0, 0, 0]], np.int8)
  959. out = ndimage.watershed_ift(data, markers, structure=[[1, 1, 1],
  960. [1, 1, 1],
  961. [1, 1, 1]])
  962. expected = [[-1, -1, -1, -1, -1, -1, -1],
  963. [-1, 1, 1, 1, 1, 1, -1],
  964. [-1, 1, 1, 1, 1, 1, -1],
  965. [-1, 1, 1, 1, 1, 1, -1],
  966. [-1, 1, 1, 1, 1, 1, -1],
  967. [-1, 1, 1, 1, 1, 1, -1],
  968. [-1, -1, -1, -1, -1, -1, -1],
  969. [-1, -1, -1, -1, -1, -1, -1]]
  970. assert_array_almost_equal(out, expected)
  971. def test_watershed_ift02(self):
  972. data = np.array([[0, 0, 0, 0, 0, 0, 0],
  973. [0, 1, 1, 1, 1, 1, 0],
  974. [0, 1, 0, 0, 0, 1, 0],
  975. [0, 1, 0, 0, 0, 1, 0],
  976. [0, 1, 0, 0, 0, 1, 0],
  977. [0, 1, 1, 1, 1, 1, 0],
  978. [0, 0, 0, 0, 0, 0, 0],
  979. [0, 0, 0, 0, 0, 0, 0]], np.uint8)
  980. markers = np.array([[-1, 0, 0, 0, 0, 0, 0],
  981. [0, 0, 0, 0, 0, 0, 0],
  982. [0, 0, 0, 0, 0, 0, 0],
  983. [0, 0, 0, 1, 0, 0, 0],
  984. [0, 0, 0, 0, 0, 0, 0],
  985. [0, 0, 0, 0, 0, 0, 0],
  986. [0, 0, 0, 0, 0, 0, 0],
  987. [0, 0, 0, 0, 0, 0, 0]], np.int8)
  988. out = ndimage.watershed_ift(data, markers)
  989. expected = [[-1, -1, -1, -1, -1, -1, -1],
  990. [-1, -1, 1, 1, 1, -1, -1],
  991. [-1, 1, 1, 1, 1, 1, -1],
  992. [-1, 1, 1, 1, 1, 1, -1],
  993. [-1, 1, 1, 1, 1, 1, -1],
  994. [-1, -1, 1, 1, 1, -1, -1],
  995. [-1, -1, -1, -1, -1, -1, -1],
  996. [-1, -1, -1, -1, -1, -1, -1]]
  997. assert_array_almost_equal(out, expected)
  998. def test_watershed_ift03(self):
  999. data = np.array([[0, 0, 0, 0, 0, 0, 0],
  1000. [0, 1, 1, 1, 1, 1, 0],
  1001. [0, 1, 0, 1, 0, 1, 0],
  1002. [0, 1, 0, 1, 0, 1, 0],
  1003. [0, 1, 0, 1, 0, 1, 0],
  1004. [0, 1, 1, 1, 1, 1, 0],
  1005. [0, 0, 0, 0, 0, 0, 0]], np.uint8)
  1006. markers = np.array([[0, 0, 0, 0, 0, 0, 0],
  1007. [0, 0, 0, 0, 0, 0, 0],
  1008. [0, 0, 0, 0, 0, 0, 0],
  1009. [0, 0, 2, 0, 3, 0, 0],
  1010. [0, 0, 0, 0, 0, 0, 0],
  1011. [0, 0, 0, 0, 0, 0, 0],
  1012. [0, 0, 0, 0, 0, 0, -1]], np.int8)
  1013. out = ndimage.watershed_ift(data, markers)
  1014. expected = [[-1, -1, -1, -1, -1, -1, -1],
  1015. [-1, -1, 2, -1, 3, -1, -1],
  1016. [-1, 2, 2, 3, 3, 3, -1],
  1017. [-1, 2, 2, 3, 3, 3, -1],
  1018. [-1, 2, 2, 3, 3, 3, -1],
  1019. [-1, -1, 2, -1, 3, -1, -1],
  1020. [-1, -1, -1, -1, -1, -1, -1]]
  1021. assert_array_almost_equal(out, expected)
  1022. def test_watershed_ift04(self):
  1023. data = np.array([[0, 0, 0, 0, 0, 0, 0],
  1024. [0, 1, 1, 1, 1, 1, 0],
  1025. [0, 1, 0, 1, 0, 1, 0],
  1026. [0, 1, 0, 1, 0, 1, 0],
  1027. [0, 1, 0, 1, 0, 1, 0],
  1028. [0, 1, 1, 1, 1, 1, 0],
  1029. [0, 0, 0, 0, 0, 0, 0]], np.uint8)
  1030. markers = np.array([[0, 0, 0, 0, 0, 0, 0],
  1031. [0, 0, 0, 0, 0, 0, 0],
  1032. [0, 0, 0, 0, 0, 0, 0],
  1033. [0, 0, 2, 0, 3, 0, 0],
  1034. [0, 0, 0, 0, 0, 0, 0],
  1035. [0, 0, 0, 0, 0, 0, 0],
  1036. [0, 0, 0, 0, 0, 0, -1]],
  1037. np.int8)
  1038. out = ndimage.watershed_ift(data, markers,
  1039. structure=[[1, 1, 1],
  1040. [1, 1, 1],
  1041. [1, 1, 1]])
  1042. expected = [[-1, -1, -1, -1, -1, -1, -1],
  1043. [-1, 2, 2, 3, 3, 3, -1],
  1044. [-1, 2, 2, 3, 3, 3, -1],
  1045. [-1, 2, 2, 3, 3, 3, -1],
  1046. [-1, 2, 2, 3, 3, 3, -1],
  1047. [-1, 2, 2, 3, 3, 3, -1],
  1048. [-1, -1, -1, -1, -1, -1, -1]]
  1049. assert_array_almost_equal(out, expected)
  1050. def test_watershed_ift05(self):
  1051. data = np.array([[0, 0, 0, 0, 0, 0, 0],
  1052. [0, 1, 1, 1, 1, 1, 0],
  1053. [0, 1, 0, 1, 0, 1, 0],
  1054. [0, 1, 0, 1, 0, 1, 0],
  1055. [0, 1, 0, 1, 0, 1, 0],
  1056. [0, 1, 1, 1, 1, 1, 0],
  1057. [0, 0, 0, 0, 0, 0, 0]], np.uint8)
  1058. markers = np.array([[0, 0, 0, 0, 0, 0, 0],
  1059. [0, 0, 0, 0, 0, 0, 0],
  1060. [0, 0, 0, 0, 0, 0, 0],
  1061. [0, 0, 3, 0, 2, 0, 0],
  1062. [0, 0, 0, 0, 0, 0, 0],
  1063. [0, 0, 0, 0, 0, 0, 0],
  1064. [0, 0, 0, 0, 0, 0, -1]],
  1065. np.int8)
  1066. out = ndimage.watershed_ift(data, markers,
  1067. structure=[[1, 1, 1],
  1068. [1, 1, 1],
  1069. [1, 1, 1]])
  1070. expected = [[-1, -1, -1, -1, -1, -1, -1],
  1071. [-1, 3, 3, 2, 2, 2, -1],
  1072. [-1, 3, 3, 2, 2, 2, -1],
  1073. [-1, 3, 3, 2, 2, 2, -1],
  1074. [-1, 3, 3, 2, 2, 2, -1],
  1075. [-1, 3, 3, 2, 2, 2, -1],
  1076. [-1, -1, -1, -1, -1, -1, -1]]
  1077. assert_array_almost_equal(out, expected)
  1078. def test_watershed_ift06(self):
  1079. data = np.array([[0, 1, 0, 0, 0, 1, 0],
  1080. [0, 1, 0, 0, 0, 1, 0],
  1081. [0, 1, 0, 0, 0, 1, 0],
  1082. [0, 1, 1, 1, 1, 1, 0],
  1083. [0, 0, 0, 0, 0, 0, 0],
  1084. [0, 0, 0, 0, 0, 0, 0]], np.uint8)
  1085. markers = np.array([[-1, 0, 0, 0, 0, 0, 0],
  1086. [0, 0, 0, 1, 0, 0, 0],
  1087. [0, 0, 0, 0, 0, 0, 0],
  1088. [0, 0, 0, 0, 0, 0, 0],
  1089. [0, 0, 0, 0, 0, 0, 0],
  1090. [0, 0, 0, 0, 0, 0, 0]], np.int8)
  1091. out = ndimage.watershed_ift(data, markers,
  1092. structure=[[1, 1, 1],
  1093. [1, 1, 1],
  1094. [1, 1, 1]])
  1095. expected = [[-1, 1, 1, 1, 1, 1, -1],
  1096. [-1, 1, 1, 1, 1, 1, -1],
  1097. [-1, 1, 1, 1, 1, 1, -1],
  1098. [-1, 1, 1, 1, 1, 1, -1],
  1099. [-1, -1, -1, -1, -1, -1, -1],
  1100. [-1, -1, -1, -1, -1, -1, -1]]
  1101. assert_array_almost_equal(out, expected)
  1102. def test_watershed_ift07(self):
  1103. shape = (7, 6)
  1104. data = np.zeros(shape, dtype=np.uint8)
  1105. data = data.transpose()
  1106. data[...] = np.array([[0, 1, 0, 0, 0, 1, 0],
  1107. [0, 1, 0, 0, 0, 1, 0],
  1108. [0, 1, 0, 0, 0, 1, 0],
  1109. [0, 1, 1, 1, 1, 1, 0],
  1110. [0, 0, 0, 0, 0, 0, 0],
  1111. [0, 0, 0, 0, 0, 0, 0]], np.uint8)
  1112. markers = np.array([[-1, 0, 0, 0, 0, 0, 0],
  1113. [0, 0, 0, 1, 0, 0, 0],
  1114. [0, 0, 0, 0, 0, 0, 0],
  1115. [0, 0, 0, 0, 0, 0, 0],
  1116. [0, 0, 0, 0, 0, 0, 0],
  1117. [0, 0, 0, 0, 0, 0, 0]], np.int8)
  1118. out = np.zeros(shape, dtype=np.int16)
  1119. out = out.transpose()
  1120. ndimage.watershed_ift(data, markers,
  1121. structure=[[1, 1, 1],
  1122. [1, 1, 1],
  1123. [1, 1, 1]],
  1124. output=out)
  1125. expected = [[-1, 1, 1, 1, 1, 1, -1],
  1126. [-1, 1, 1, 1, 1, 1, -1],
  1127. [-1, 1, 1, 1, 1, 1, -1],
  1128. [-1, 1, 1, 1, 1, 1, -1],
  1129. [-1, -1, -1, -1, -1, -1, -1],
  1130. [-1, -1, -1, -1, -1, -1, -1]]
  1131. assert_array_almost_equal(out, expected)
  1132. def test_watershed_ift08(self):
  1133. # Test cost larger than uint8. See gh-10069.
  1134. shape = (2, 2)
  1135. data = np.array([[256, 0],
  1136. [0, 0]], np.uint16)
  1137. markers = np.array([[1, 0],
  1138. [0, 0]], np.int8)
  1139. out = ndimage.watershed_ift(data, markers)
  1140. expected = [[1, 1],
  1141. [1, 1]]
  1142. assert_array_almost_equal(out, expected)