test_measurements.py 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696
  1. import os
  2. import os.path
  3. import warnings
  4. import numpy as np
  5. from scipy._lib._array_api import (
  6. is_torch,
  7. xp_assert_equal,
  8. xp_assert_close,
  9. assert_array_almost_equal,
  10. assert_almost_equal,
  11. make_xp_test_case,
  12. )
  13. import pytest
  14. from pytest import raises as assert_raises
  15. import scipy.ndimage as ndimage
  16. from . import types
  17. skip_xp_backends = pytest.mark.skip_xp_backends
  18. IS_WINDOWS_AND_NP1 = os.name == 'nt' and np.__version__ < '2'
  19. @skip_xp_backends(np_only=True, reason='test internal numpy-only helpers')
  20. class Test_measurements_stats:
  21. """ndimage._measurements._stats() is a utility used by other functions.
  22. Since internal ndimage/_measurements.py code is NumPy-only,
  23. so is this this test class.
  24. """
  25. def test_a(self, xp):
  26. x = [0, 1, 2, 6]
  27. labels = [0, 0, 1, 1]
  28. index = [0, 1]
  29. for shp in [(4,), (2, 2)]:
  30. x = np.array(x).reshape(shp)
  31. labels = np.array(labels).reshape(shp)
  32. counts, sums = ndimage._measurements._stats(
  33. x, labels=labels, index=index)
  34. dtype_arg = {'dtype': np.int64} if IS_WINDOWS_AND_NP1 else {}
  35. xp_assert_equal(counts, np.asarray([2, 2], **dtype_arg))
  36. xp_assert_equal(sums, np.asarray([1.0, 8.0]))
  37. def test_b(self, xp):
  38. # Same data as test_a, but different labels. The label 9 exceeds the
  39. # length of 'labels', so this test will follow a different code path.
  40. x = [0, 1, 2, 6]
  41. labels = [0, 0, 9, 9]
  42. index = [0, 9]
  43. for shp in [(4,), (2, 2)]:
  44. x = np.array(x).reshape(shp)
  45. labels = np.array(labels).reshape(shp)
  46. counts, sums = ndimage._measurements._stats(
  47. x, labels=labels, index=index)
  48. dtype_arg = {'dtype': np.int64} if IS_WINDOWS_AND_NP1 else {}
  49. xp_assert_equal(counts, np.asarray([2, 2], **dtype_arg))
  50. xp_assert_equal(sums, np.asarray([1.0, 8.0]))
  51. def test_a_centered(self, xp):
  52. x = [0, 1, 2, 6]
  53. labels = [0, 0, 1, 1]
  54. index = [0, 1]
  55. for shp in [(4,), (2, 2)]:
  56. x = np.array(x).reshape(shp)
  57. labels = np.array(labels).reshape(shp)
  58. counts, sums, centers = ndimage._measurements._stats(
  59. x, labels=labels, index=index, centered=True)
  60. dtype_arg = {'dtype': np.int64} if IS_WINDOWS_AND_NP1 else {}
  61. xp_assert_equal(counts, np.asarray([2, 2], **dtype_arg))
  62. xp_assert_equal(sums, np.asarray([1.0, 8.0]))
  63. xp_assert_equal(centers, np.asarray([0.5, 8.0]))
  64. def test_b_centered(self, xp):
  65. x = [0, 1, 2, 6]
  66. labels = [0, 0, 9, 9]
  67. index = [0, 9]
  68. for shp in [(4,), (2, 2)]:
  69. x = np.array(x).reshape(shp)
  70. labels = np.array(labels).reshape(shp)
  71. counts, sums, centers = ndimage._measurements._stats(
  72. x, labels=labels, index=index, centered=True)
  73. dtype_arg = {'dtype': np.int64} if IS_WINDOWS_AND_NP1 else {}
  74. xp_assert_equal(counts, np.asarray([2, 2], **dtype_arg))
  75. xp_assert_equal(sums, np.asarray([1.0, 8.0]))
  76. xp_assert_equal(centers, np.asarray([0.5, 8.0]))
  77. def test_nonint_labels(self, xp):
  78. x = [0, 1, 2, 6]
  79. labels = [0.0, 0.0, 9.0, 9.0]
  80. index = [0.0, 9.0]
  81. for shp in [(4,), (2, 2)]:
  82. x = np.array(x).reshape(shp)
  83. labels = np.array(labels).reshape(shp)
  84. counts, sums, centers = ndimage._measurements._stats(
  85. x, labels=labels, index=index, centered=True)
  86. dtype_arg = {'dtype': np.int64} if IS_WINDOWS_AND_NP1 else {}
  87. xp_assert_equal(counts, np.asarray([2, 2], **dtype_arg))
  88. xp_assert_equal(sums, np.asarray([1.0, 8.0]))
  89. xp_assert_equal(centers, np.asarray([0.5, 8.0]))
  90. @skip_xp_backends(np_only=True, reason='test internal numpy-only helpers')
  91. class Test_measurements_select:
  92. """ndimage._measurements._select() is a utility used by other functions."""
  93. def test_basic(self, xp):
  94. x = [0, 1, 6, 2]
  95. cases = [
  96. ([0, 0, 1, 1], [0, 1]), # "Small" integer labels
  97. ([0, 0, 9, 9], [0, 9]), # A label larger than len(labels)
  98. ([0.0, 0.0, 7.0, 7.0], [0.0, 7.0]), # Non-integer labels
  99. ]
  100. for labels, index in cases:
  101. result = ndimage._measurements._select(
  102. x, labels=labels, index=index)
  103. assert len(result) == 0
  104. result = ndimage._measurements._select(
  105. x, labels=labels, index=index, find_max=True)
  106. assert len(result) == 1
  107. xp_assert_equal(result[0], [1, 6])
  108. result = ndimage._measurements._select(
  109. x, labels=labels, index=index, find_min=True)
  110. assert len(result) == 1
  111. xp_assert_equal(result[0], [0, 2])
  112. result = ndimage._measurements._select(
  113. x, labels=labels, index=index, find_min=True,
  114. find_min_positions=True)
  115. assert len(result) == 2
  116. xp_assert_equal(result[0], [0, 2])
  117. xp_assert_equal(result[1], [0, 3])
  118. assert result[1].dtype.kind == 'i'
  119. result = ndimage._measurements._select(
  120. x, labels=labels, index=index, find_max=True,
  121. find_max_positions=True)
  122. assert len(result) == 2
  123. xp_assert_equal(result[0], [1, 6])
  124. xp_assert_equal(result[1], [1, 2])
  125. assert result[1].dtype.kind == 'i'
  126. @make_xp_test_case(ndimage.label)
  127. def test_label01(xp):
  128. data = xp.ones(())
  129. out, n = ndimage.label(data)
  130. assert out == 1
  131. assert n == 1
  132. @make_xp_test_case(ndimage.label)
  133. def test_label02(xp):
  134. data = xp.zeros(())
  135. out, n = ndimage.label(data)
  136. assert out == 0
  137. assert n == 0
  138. @make_xp_test_case(ndimage.label)
  139. def test_label03(xp):
  140. data = xp.ones([1])
  141. out, n = ndimage.label(data)
  142. assert_array_almost_equal(out, xp.asarray([1]))
  143. assert n == 1
  144. @make_xp_test_case(ndimage.label)
  145. def test_label04(xp):
  146. data = xp.zeros([1])
  147. out, n = ndimage.label(data)
  148. assert_array_almost_equal(out, xp.asarray([0]))
  149. assert n == 0
  150. @make_xp_test_case(ndimage.label)
  151. def test_label05(xp):
  152. data = xp.ones([5])
  153. out, n = ndimage.label(data)
  154. assert_array_almost_equal(out, xp.asarray([1, 1, 1, 1, 1]))
  155. assert n == 1
  156. @make_xp_test_case(ndimage.label)
  157. def test_label06(xp):
  158. data = xp.asarray([1, 0, 1, 1, 0, 1])
  159. out, n = ndimage.label(data)
  160. assert_array_almost_equal(out, xp.asarray([1, 0, 2, 2, 0, 3]))
  161. assert n == 3
  162. @make_xp_test_case(ndimage.label)
  163. def test_label07(xp):
  164. data = xp.asarray([[0, 0, 0, 0, 0, 0],
  165. [0, 0, 0, 0, 0, 0],
  166. [0, 0, 0, 0, 0, 0],
  167. [0, 0, 0, 0, 0, 0],
  168. [0, 0, 0, 0, 0, 0],
  169. [0, 0, 0, 0, 0, 0]])
  170. out, n = ndimage.label(data)
  171. assert_array_almost_equal(out, xp.asarray(
  172. [[0, 0, 0, 0, 0, 0],
  173. [0, 0, 0, 0, 0, 0],
  174. [0, 0, 0, 0, 0, 0],
  175. [0, 0, 0, 0, 0, 0],
  176. [0, 0, 0, 0, 0, 0],
  177. [0, 0, 0, 0, 0, 0]]))
  178. assert n == 0
  179. @make_xp_test_case(ndimage.label)
  180. def test_label08(xp):
  181. data = xp.asarray([[1, 0, 0, 0, 0, 0],
  182. [0, 0, 1, 1, 0, 0],
  183. [0, 0, 1, 1, 1, 0],
  184. [1, 1, 0, 0, 0, 0],
  185. [1, 1, 0, 0, 0, 0],
  186. [0, 0, 0, 1, 1, 0]])
  187. out, n = ndimage.label(data)
  188. assert_array_almost_equal(out, xp.asarray([[1, 0, 0, 0, 0, 0],
  189. [0, 0, 2, 2, 0, 0],
  190. [0, 0, 2, 2, 2, 0],
  191. [3, 3, 0, 0, 0, 0],
  192. [3, 3, 0, 0, 0, 0],
  193. [0, 0, 0, 4, 4, 0]]))
  194. assert n == 4
  195. @make_xp_test_case(ndimage.label)
  196. def test_label09(xp):
  197. data = xp.asarray([[1, 0, 0, 0, 0, 0],
  198. [0, 0, 1, 1, 0, 0],
  199. [0, 0, 1, 1, 1, 0],
  200. [1, 1, 0, 0, 0, 0],
  201. [1, 1, 0, 0, 0, 0],
  202. [0, 0, 0, 1, 1, 0]])
  203. struct = ndimage.generate_binary_structure(2, 2)
  204. struct = xp.asarray(struct)
  205. out, n = ndimage.label(data, struct)
  206. assert_array_almost_equal(out, xp.asarray([[1, 0, 0, 0, 0, 0],
  207. [0, 0, 2, 2, 0, 0],
  208. [0, 0, 2, 2, 2, 0],
  209. [2, 2, 0, 0, 0, 0],
  210. [2, 2, 0, 0, 0, 0],
  211. [0, 0, 0, 3, 3, 0]]))
  212. assert n == 3
  213. @make_xp_test_case(ndimage.label)
  214. def test_label10(xp):
  215. data = xp.asarray([[0, 0, 0, 0, 0, 0],
  216. [0, 1, 1, 0, 1, 0],
  217. [0, 1, 1, 1, 1, 0],
  218. [0, 0, 0, 0, 0, 0]])
  219. struct = ndimage.generate_binary_structure(2, 2)
  220. struct = xp.asarray(struct)
  221. out, n = ndimage.label(data, struct)
  222. assert_array_almost_equal(out, xp.asarray([[0, 0, 0, 0, 0, 0],
  223. [0, 1, 1, 0, 1, 0],
  224. [0, 1, 1, 1, 1, 0],
  225. [0, 0, 0, 0, 0, 0]]))
  226. assert n == 1
  227. @make_xp_test_case(ndimage.label)
  228. def test_label11(xp):
  229. for type in types:
  230. dtype = getattr(xp, type)
  231. data = xp.asarray([[1, 0, 0, 0, 0, 0],
  232. [0, 0, 1, 1, 0, 0],
  233. [0, 0, 1, 1, 1, 0],
  234. [1, 1, 0, 0, 0, 0],
  235. [1, 1, 0, 0, 0, 0],
  236. [0, 0, 0, 1, 1, 0]], dtype=dtype)
  237. out, n = ndimage.label(data)
  238. expected = [[1, 0, 0, 0, 0, 0],
  239. [0, 0, 2, 2, 0, 0],
  240. [0, 0, 2, 2, 2, 0],
  241. [3, 3, 0, 0, 0, 0],
  242. [3, 3, 0, 0, 0, 0],
  243. [0, 0, 0, 4, 4, 0]]
  244. expected = xp.asarray(expected)
  245. assert_array_almost_equal(out, expected)
  246. assert n == 4
  247. @skip_xp_backends(np_only=True, reason='inplace output is numpy-specific')
  248. @make_xp_test_case(ndimage.label)
  249. def test_label11_inplace(xp):
  250. for type in types:
  251. dtype = getattr(xp, type)
  252. data = xp.asarray([[1, 0, 0, 0, 0, 0],
  253. [0, 0, 1, 1, 0, 0],
  254. [0, 0, 1, 1, 1, 0],
  255. [1, 1, 0, 0, 0, 0],
  256. [1, 1, 0, 0, 0, 0],
  257. [0, 0, 0, 1, 1, 0]], dtype=dtype)
  258. n = ndimage.label(data, output=data)
  259. expected = [[1, 0, 0, 0, 0, 0],
  260. [0, 0, 2, 2, 0, 0],
  261. [0, 0, 2, 2, 2, 0],
  262. [3, 3, 0, 0, 0, 0],
  263. [3, 3, 0, 0, 0, 0],
  264. [0, 0, 0, 4, 4, 0]]
  265. expected = xp.asarray(expected)
  266. assert_array_almost_equal(data, expected)
  267. assert n == 4
  268. @make_xp_test_case(ndimage.label)
  269. def test_label12(xp):
  270. for type in types:
  271. dtype = getattr(xp, type)
  272. data = xp.asarray([[0, 0, 0, 0, 1, 1],
  273. [0, 0, 0, 0, 0, 1],
  274. [0, 0, 1, 0, 1, 1],
  275. [0, 0, 1, 1, 1, 1],
  276. [0, 0, 0, 1, 1, 0]], dtype=dtype)
  277. out, n = ndimage.label(data)
  278. expected = [[0, 0, 0, 0, 1, 1],
  279. [0, 0, 0, 0, 0, 1],
  280. [0, 0, 1, 0, 1, 1],
  281. [0, 0, 1, 1, 1, 1],
  282. [0, 0, 0, 1, 1, 0]]
  283. expected = xp.asarray(expected)
  284. assert_array_almost_equal(out, expected)
  285. assert n == 1
  286. @make_xp_test_case(ndimage.label)
  287. def test_label13(xp):
  288. for type in types:
  289. dtype = getattr(xp, type)
  290. data = xp.asarray([[1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1],
  291. [1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1],
  292. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  293. [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]],
  294. dtype=dtype)
  295. out, n = ndimage.label(data)
  296. expected = [[1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1],
  297. [1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1],
  298. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  299. [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
  300. expected = xp.asarray(expected)
  301. assert_array_almost_equal(out, expected)
  302. assert n == 1
  303. @skip_xp_backends(np_only=True, exceptions=["cupy"],
  304. reason='output=dtype is numpy-specific')
  305. @make_xp_test_case(ndimage.label)
  306. def test_label_output_typed(xp):
  307. data = xp.ones([5])
  308. for t in types:
  309. dtype = getattr(xp, t)
  310. output = xp.zeros([5], dtype=dtype)
  311. n = ndimage.label(data, output=output)
  312. assert_array_almost_equal(output,
  313. xp.ones(output.shape, dtype=output.dtype))
  314. assert n == 1
  315. @skip_xp_backends(np_only=True, exceptions=["cupy"],
  316. reason='output=dtype is numpy-specific')
  317. @make_xp_test_case(ndimage.label)
  318. def test_label_output_dtype(xp):
  319. data = xp.ones([5])
  320. for t in types:
  321. dtype = getattr(xp, t)
  322. output, n = ndimage.label(data, output=dtype)
  323. assert_array_almost_equal(output,
  324. xp.ones(output.shape, dtype=output.dtype))
  325. assert output.dtype == t
  326. @skip_xp_backends(np_only=True, reason="in-place output is numpy-specific")
  327. @make_xp_test_case(ndimage.label)
  328. def test_label_output_wrong_size(xp):
  329. data = xp.ones([5])
  330. for t in types:
  331. dtype = getattr(xp, t)
  332. output = xp.zeros([10], dtype=dtype)
  333. assert_raises(ValueError, ndimage.label, data, output=output)
  334. @make_xp_test_case(ndimage.label)
  335. def test_label_structuring_elements(xp):
  336. data = np.loadtxt(os.path.join(os.path.dirname(
  337. __file__), "data", "label_inputs.txt"))
  338. strels = np.loadtxt(os.path.join(
  339. os.path.dirname(__file__), "data", "label_strels.txt"))
  340. results = np.loadtxt(os.path.join(
  341. os.path.dirname(__file__), "data", "label_results.txt"))
  342. data = data.reshape((-1, 7, 7))
  343. strels = strels.reshape((-1, 3, 3))
  344. results = results.reshape((-1, 7, 7))
  345. data = xp.asarray(data)
  346. strels = xp.asarray(strels)
  347. results = xp.asarray(results)
  348. r = 0
  349. for i in range(data.shape[0]):
  350. d = data[i, :, :]
  351. for j in range(strels.shape[0]):
  352. s = strels[j, :, :]
  353. xp_assert_equal(ndimage.label(d, s)[0], results[r, :, :], check_dtype=False)
  354. r += 1
  355. @make_xp_test_case(ndimage.label, ndimage.find_objects)
  356. def test_ticket_742(xp):
  357. def SE(img, thresh=.7, size=4):
  358. mask = img > thresh
  359. rank = len(mask.shape)
  360. struct = ndimage.generate_binary_structure(rank, rank)
  361. struct = xp.asarray(struct)
  362. la, co = ndimage.label(mask,
  363. struct)
  364. _ = ndimage.find_objects(la)
  365. if np.dtype(np.intp) != np.dtype('i'):
  366. shape = (3, 1240, 1240)
  367. a = np.random.rand(np.prod(shape)).reshape(shape)
  368. a = xp.asarray(a)
  369. # shouldn't crash
  370. SE(a)
  371. @make_xp_test_case(ndimage.label)
  372. def test_gh_issue_3025(xp):
  373. """Github issue #3025 - improper merging of labels"""
  374. d = np.zeros((60, 320))
  375. d[:, :257] = 1
  376. d[:, 260:] = 1
  377. d[36, 257] = 1
  378. d[35, 258] = 1
  379. d[35, 259] = 1
  380. d = xp.asarray(d)
  381. assert ndimage.label(d, xp.ones((3, 3)))[1] == 1
  382. @make_xp_test_case(ndimage.label, ndimage.find_objects)
  383. class TestFindObjects:
  384. def test_label_default_dtype(self, xp):
  385. test_array = np.random.rand(10, 10)
  386. test_array = xp.asarray(test_array)
  387. label, no_features = ndimage.label(test_array > 0.5)
  388. assert label.dtype in (xp.int32, xp.int64)
  389. # Shouldn't raise an exception
  390. ndimage.find_objects(label)
  391. def test_find_objects01(self, xp):
  392. data = xp.ones([], dtype=xp.int64)
  393. out = ndimage.find_objects(data)
  394. assert out == [()]
  395. def test_find_objects02(self, xp):
  396. data = xp.zeros([], dtype=xp.int64)
  397. out = ndimage.find_objects(data)
  398. assert out == []
  399. def test_find_objects03(self, xp):
  400. data = xp.ones([1], dtype=xp.int64)
  401. out = ndimage.find_objects(data)
  402. assert out == [(slice(0, 1, None),)]
  403. def test_find_objects04(self, xp):
  404. data = xp.zeros([1], dtype=xp.int64)
  405. out = ndimage.find_objects(data)
  406. assert out == []
  407. def test_find_objects05(self, xp):
  408. data = xp.ones([5], dtype=xp.int64)
  409. out = ndimage.find_objects(data)
  410. assert out == [(slice(0, 5, None),)]
  411. def test_find_objects06(self, xp):
  412. data = xp.asarray([1, 0, 2, 2, 0, 3])
  413. out = ndimage.find_objects(data)
  414. assert out == [(slice(0, 1, None),),
  415. (slice(2, 4, None),),
  416. (slice(5, 6, None),)]
  417. def test_find_objects07(self, xp):
  418. data = xp.asarray([[0, 0, 0, 0, 0, 0],
  419. [0, 0, 0, 0, 0, 0],
  420. [0, 0, 0, 0, 0, 0],
  421. [0, 0, 0, 0, 0, 0],
  422. [0, 0, 0, 0, 0, 0],
  423. [0, 0, 0, 0, 0, 0]])
  424. out = ndimage.find_objects(data)
  425. assert out == []
  426. def test_find_objects08(self, xp):
  427. data = xp.asarray([[1, 0, 0, 0, 0, 0],
  428. [0, 0, 2, 2, 0, 0],
  429. [0, 0, 2, 2, 2, 0],
  430. [3, 3, 0, 0, 0, 0],
  431. [3, 3, 0, 0, 0, 0],
  432. [0, 0, 0, 4, 4, 0]])
  433. out = ndimage.find_objects(data)
  434. assert out == [(slice(0, 1, None), slice(0, 1, None)),
  435. (slice(1, 3, None), slice(2, 5, None)),
  436. (slice(3, 5, None), slice(0, 2, None)),
  437. (slice(5, 6, None), slice(3, 5, None))]
  438. def test_find_objects09(self, xp):
  439. data = xp.asarray([[1, 0, 0, 0, 0, 0],
  440. [0, 0, 2, 2, 0, 0],
  441. [0, 0, 2, 2, 2, 0],
  442. [0, 0, 0, 0, 0, 0],
  443. [0, 0, 0, 0, 0, 0],
  444. [0, 0, 0, 4, 4, 0]])
  445. out = ndimage.find_objects(data)
  446. assert out == [(slice(0, 1, None), slice(0, 1, None)),
  447. (slice(1, 3, None), slice(2, 5, None)),
  448. None,
  449. (slice(5, 6, None), slice(3, 5, None))]
  450. @make_xp_test_case(ndimage.value_indices)
  451. def test_value_indices01(xp):
  452. "Test dictionary keys and entries"
  453. data = xp.asarray([[1, 0, 0, 0, 0, 0],
  454. [0, 0, 2, 2, 0, 0],
  455. [0, 0, 2, 2, 2, 0],
  456. [0, 0, 0, 0, 0, 0],
  457. [0, 0, 0, 0, 0, 0],
  458. [0, 0, 0, 4, 4, 0]])
  459. vi = ndimage.value_indices(data, ignore_value=0)
  460. true_keys = [1, 2, 4]
  461. assert list(vi.keys()) == true_keys
  462. truevi = {k: xp.nonzero(data == k) for k in true_keys}
  463. vi = ndimage.value_indices(data, ignore_value=0)
  464. assert vi.keys() == truevi.keys()
  465. for key in vi.keys():
  466. assert len(vi[key]) == len(truevi[key])
  467. for v, true_v in zip(vi[key], truevi[key]):
  468. xp_assert_equal(v, true_v)
  469. @make_xp_test_case(ndimage.value_indices)
  470. def test_value_indices02(xp):
  471. "Test input checking"
  472. data = xp.zeros((5, 4), dtype=xp.float32)
  473. msg = "Parameter 'arr' must be an integer array"
  474. with assert_raises(ValueError, match=msg):
  475. ndimage.value_indices(data)
  476. @make_xp_test_case(ndimage.value_indices)
  477. def test_value_indices03(xp):
  478. "Test different input array shapes, from 1-D to 4-D"
  479. for shape in [(36,), (18, 2), (3, 3, 4), (3, 3, 2, 2)]:
  480. a = np.asarray((12*[1]+12*[2]+12*[3]), dtype=np.int32)
  481. a = np.reshape(a, shape)
  482. trueKeys = np.unique(a)
  483. a = xp.asarray(a)
  484. vi = ndimage.value_indices(a)
  485. assert list(vi.keys()) == list(trueKeys)
  486. for k in [int(x) for x in trueKeys]:
  487. trueNdx = xp.nonzero(a == k)
  488. assert len(vi[k]) == len(trueNdx)
  489. for vik, true_vik in zip(vi[k], trueNdx):
  490. xp_assert_equal(vik, true_vik)
  491. @make_xp_test_case(ndimage.sum)
  492. def test_sum01(xp):
  493. for type in types:
  494. dtype = getattr(xp, type)
  495. input = xp.asarray([], dtype=dtype)
  496. output = ndimage.sum(input)
  497. assert output == 0
  498. @make_xp_test_case(ndimage.sum)
  499. def test_sum02(xp):
  500. for type in types:
  501. dtype = getattr(xp, type)
  502. input = xp.zeros([0, 4], dtype=dtype)
  503. output = ndimage.sum(input)
  504. assert output == 0
  505. @make_xp_test_case(ndimage.sum)
  506. def test_sum03(xp):
  507. for type in types:
  508. dtype = getattr(xp, type)
  509. input = xp.ones([], dtype=dtype)
  510. output = ndimage.sum(input)
  511. assert_almost_equal(output, xp.asarray(1.0), check_0d=False)
  512. @make_xp_test_case(ndimage.sum)
  513. def test_sum04(xp):
  514. for type in types:
  515. dtype = getattr(xp, type)
  516. input = xp.asarray([1, 2], dtype=dtype)
  517. output = ndimage.sum(input)
  518. assert_almost_equal(output, xp.asarray(3.0), check_0d=False)
  519. @make_xp_test_case(ndimage.sum)
  520. def test_sum05(xp):
  521. for type in types:
  522. dtype = getattr(xp, type)
  523. input = xp.asarray([[1, 2], [3, 4]], dtype=dtype)
  524. output = ndimage.sum(input)
  525. assert_almost_equal(output, xp.asarray(10.0), check_0d=False)
  526. @make_xp_test_case(ndimage.sum)
  527. def test_sum06(xp):
  528. labels = np.asarray([], dtype=bool)
  529. labels = xp.asarray(labels)
  530. for type in types:
  531. dtype = getattr(xp, type)
  532. input = xp.asarray([], dtype=dtype)
  533. output = ndimage.sum(input, labels=labels)
  534. assert output == 0
  535. @make_xp_test_case(ndimage.sum)
  536. def test_sum07(xp):
  537. labels = np.ones([0, 4], dtype=bool)
  538. labels = xp.asarray(labels)
  539. for type in types:
  540. dtype = getattr(xp, type)
  541. input = xp.zeros([0, 4], dtype=dtype)
  542. output = ndimage.sum(input, labels=labels)
  543. assert output == 0
  544. @make_xp_test_case(ndimage.sum)
  545. def test_sum08(xp):
  546. labels = np.asarray([1, 0], dtype=bool)
  547. labels = xp.asarray(labels)
  548. for type in types:
  549. dtype = getattr(xp, type)
  550. input = xp.asarray([1, 2], dtype=dtype)
  551. output = ndimage.sum(input, labels=labels)
  552. assert output == 1
  553. @make_xp_test_case(ndimage.sum)
  554. def test_sum09(xp):
  555. labels = np.asarray([1, 0], dtype=bool)
  556. labels = xp.asarray(labels)
  557. for type in types:
  558. dtype = getattr(xp, type)
  559. input = xp.asarray([[1, 2], [3, 4]], dtype=dtype)
  560. output = ndimage.sum(input, labels=labels)
  561. assert_almost_equal(output, xp.asarray(4.0), check_0d=False)
  562. @make_xp_test_case(ndimage.sum)
  563. def test_sum10(xp):
  564. labels = np.asarray([1, 0], dtype=bool)
  565. input = np.asarray([[1, 2], [3, 4]], dtype=bool)
  566. labels = xp.asarray(labels)
  567. input = xp.asarray(input)
  568. output = ndimage.sum(input, labels=labels)
  569. assert_almost_equal(output, xp.asarray(2.0), check_0d=False)
  570. @make_xp_test_case(ndimage.sum)
  571. def test_sum11(xp):
  572. labels = xp.asarray([1, 2], dtype=xp.int8)
  573. for type in types:
  574. dtype = getattr(xp, type)
  575. input = xp.asarray([[1, 2], [3, 4]], dtype=dtype)
  576. output = ndimage.sum(input, labels=labels,
  577. index=2)
  578. assert_almost_equal(output, xp.asarray(6.0), check_0d=False)
  579. @make_xp_test_case(ndimage.sum)
  580. def test_sum12(xp):
  581. labels = xp.asarray([[1, 2], [2, 4]], dtype=xp.int8)
  582. for type in types:
  583. dtype = getattr(xp, type)
  584. input = xp.asarray([[1, 2], [3, 4]], dtype=dtype)
  585. output = ndimage.sum(input, labels=labels, index=xp.asarray([4, 8, 2]))
  586. assert_array_almost_equal(output, xp.asarray([4.0, 0.0, 5.0]))
  587. @make_xp_test_case(ndimage.sum)
  588. def test_sum_labels(xp):
  589. labels = xp.asarray([[1, 2], [2, 4]], dtype=xp.int8)
  590. for type in types:
  591. dtype = getattr(xp, type)
  592. input = xp.asarray([[1, 2], [3, 4]], dtype=dtype)
  593. output_sum = ndimage.sum(input, labels=labels, index=xp.asarray([4, 8, 2]))
  594. output_labels = ndimage.sum_labels(
  595. input, labels=labels, index=xp.asarray([4, 8, 2]))
  596. assert xp.all(output_sum == output_labels)
  597. assert_array_almost_equal(output_labels, xp.asarray([4.0, 0.0, 5.0]))
  598. @make_xp_test_case(ndimage.mean)
  599. def test_mean01(xp):
  600. labels = np.asarray([1, 0], dtype=bool)
  601. labels = xp.asarray(labels)
  602. for type in types:
  603. dtype = getattr(xp, type)
  604. input = xp.asarray([[1, 2], [3, 4]], dtype=dtype)
  605. output = ndimage.mean(input, labels=labels)
  606. assert_almost_equal(output, xp.asarray(2.0), check_0d=False)
  607. @make_xp_test_case(ndimage.mean)
  608. def test_mean02(xp):
  609. labels = np.asarray([1, 0], dtype=bool)
  610. input = np.asarray([[1, 2], [3, 4]], dtype=bool)
  611. labels = xp.asarray(labels)
  612. input = xp.asarray(input)
  613. output = ndimage.mean(input, labels=labels)
  614. assert_almost_equal(output, xp.asarray(1.0), check_0d=False)
  615. @make_xp_test_case(ndimage.mean)
  616. def test_mean03(xp):
  617. labels = xp.asarray([1, 2])
  618. for type in types:
  619. dtype = getattr(xp, type)
  620. input = xp.asarray([[1, 2], [3, 4]], dtype=dtype)
  621. output = ndimage.mean(input, labels=labels,
  622. index=2)
  623. assert_almost_equal(output, xp.asarray(3.0), check_0d=False)
  624. @make_xp_test_case(ndimage.mean)
  625. def test_mean04(xp):
  626. labels = xp.asarray([[1, 2], [2, 4]], dtype=xp.int8)
  627. with np.errstate(all='ignore'):
  628. for type in types:
  629. dtype = getattr(xp, type)
  630. input = xp.asarray([[1, 2], [3, 4]], dtype=dtype)
  631. output = ndimage.mean(input, labels=labels,
  632. index=xp.asarray([4, 8, 2]))
  633. # XXX: output[[0, 2]] does not work in array-api-strict; annoying
  634. # assert_array_almost_equal(output[[0, 2]], xp.asarray([4.0, 2.5]))
  635. assert output[0] == 4.0
  636. assert output[2] == 2.5
  637. assert xp.isnan(output[1])
  638. @make_xp_test_case(ndimage.minimum)
  639. def test_minimum01(xp):
  640. labels = np.asarray([1, 0], dtype=bool)
  641. labels = xp.asarray(labels)
  642. for type in types:
  643. dtype = getattr(xp, type)
  644. input = xp.asarray([[1, 2], [3, 4]], dtype=dtype)
  645. output = ndimage.minimum(input, labels=labels)
  646. assert_almost_equal(output, xp.asarray(1.0), check_0d=False)
  647. @make_xp_test_case(ndimage.minimum)
  648. def test_minimum02(xp):
  649. labels = np.asarray([1, 0], dtype=bool)
  650. input = np.asarray([[2, 2], [2, 4]], dtype=bool)
  651. labels = xp.asarray(labels)
  652. input = xp.asarray(input)
  653. output = ndimage.minimum(input, labels=labels)
  654. assert_almost_equal(output, xp.asarray(1.0), check_0d=False)
  655. @make_xp_test_case(ndimage.minimum)
  656. def test_minimum03(xp):
  657. labels = xp.asarray([1, 2])
  658. for type in types:
  659. dtype = getattr(xp, type)
  660. input = xp.asarray([[1, 2], [3, 4]], dtype=dtype)
  661. output = ndimage.minimum(input, labels=labels,
  662. index=2)
  663. assert_almost_equal(output, xp.asarray(2.0), check_0d=False)
  664. @make_xp_test_case(ndimage.minimum)
  665. def test_minimum04(xp):
  666. labels = xp.asarray([[1, 2], [2, 3]])
  667. for type in types:
  668. dtype = getattr(xp, type)
  669. input = xp.asarray([[1, 2], [3, 4]], dtype=dtype)
  670. output = ndimage.minimum(input, labels=labels,
  671. index=xp.asarray([2, 3, 8]))
  672. assert_array_almost_equal(output, xp.asarray([2.0, 4.0, 0.0]))
  673. @make_xp_test_case(ndimage.maximum)
  674. def test_maximum01(xp):
  675. labels = np.asarray([1, 0], dtype=bool)
  676. labels = xp.asarray(labels)
  677. for type in types:
  678. dtype = getattr(xp, type)
  679. input = xp.asarray([[1, 2], [3, 4]], dtype=dtype)
  680. output = ndimage.maximum(input, labels=labels)
  681. assert_almost_equal(output, xp.asarray(3.0), check_0d=False)
  682. @make_xp_test_case(ndimage.maximum)
  683. def test_maximum02(xp):
  684. labels = np.asarray([1, 0], dtype=bool)
  685. input = np.asarray([[2, 2], [2, 4]], dtype=bool)
  686. labels = xp.asarray(labels)
  687. input = xp.asarray(input)
  688. output = ndimage.maximum(input, labels=labels)
  689. assert_almost_equal(output, xp.asarray(1.0), check_0d=False)
  690. @make_xp_test_case(ndimage.maximum)
  691. def test_maximum03(xp):
  692. labels = xp.asarray([1, 2])
  693. for type in types:
  694. dtype = getattr(xp, type)
  695. input = xp.asarray([[1, 2], [3, 4]], dtype=dtype)
  696. output = ndimage.maximum(input, labels=labels,
  697. index=2)
  698. assert_almost_equal(output, xp.asarray(4.0), check_0d=False)
  699. @make_xp_test_case(ndimage.maximum)
  700. def test_maximum04(xp):
  701. labels = xp.asarray([[1, 2], [2, 3]])
  702. for type in types:
  703. dtype = getattr(xp, type)
  704. input = xp.asarray([[1, 2], [3, 4]], dtype=dtype)
  705. output = ndimage.maximum(input, labels=labels,
  706. index=xp.asarray([2, 3, 8]))
  707. assert_array_almost_equal(output, xp.asarray([3.0, 4.0, 0.0]))
  708. @make_xp_test_case(ndimage.maximum)
  709. def test_maximum05(xp):
  710. # Regression test for ticket #501 (Trac)
  711. x = xp.asarray([-3, -2, -1])
  712. assert ndimage.maximum(x) == -1
  713. @make_xp_test_case(ndimage.median)
  714. def test_median01(xp):
  715. a = xp.asarray([[1, 2, 0, 1],
  716. [5, 3, 0, 4],
  717. [0, 0, 0, 7],
  718. [9, 3, 0, 0]])
  719. labels = xp.asarray([[1, 1, 0, 2],
  720. [1, 1, 0, 2],
  721. [0, 0, 0, 2],
  722. [3, 3, 0, 0]])
  723. output = ndimage.median(a, labels=labels, index=xp.asarray([1, 2, 3]))
  724. assert_array_almost_equal(output, xp.asarray([2.5, 4.0, 6.0]))
  725. @make_xp_test_case(ndimage.median)
  726. def test_median02(xp):
  727. a = xp.asarray([[1, 2, 0, 1],
  728. [5, 3, 0, 4],
  729. [0, 0, 0, 7],
  730. [9, 3, 0, 0]])
  731. output = ndimage.median(a)
  732. assert_almost_equal(output, xp.asarray(1.0), check_0d=False)
  733. @make_xp_test_case(ndimage.median)
  734. def test_median03(xp):
  735. a = xp.asarray([[1, 2, 0, 1],
  736. [5, 3, 0, 4],
  737. [0, 0, 0, 7],
  738. [9, 3, 0, 0]])
  739. labels = xp.asarray([[1, 1, 0, 2],
  740. [1, 1, 0, 2],
  741. [0, 0, 0, 2],
  742. [3, 3, 0, 0]])
  743. output = ndimage.median(a, labels=labels)
  744. assert_almost_equal(output, xp.asarray(3.0), check_0d=False)
  745. @make_xp_test_case(ndimage.median)
  746. def test_median_gh12836_bool(xp):
  747. # test boolean addition fix on example from gh-12836
  748. a = np.asarray([1, 1], dtype=bool)
  749. a = xp.asarray(a)
  750. output = ndimage.median(a, labels=xp.ones((2,)), index=xp.asarray([1]))
  751. assert_array_almost_equal(output, xp.asarray([1.0]))
  752. @make_xp_test_case(ndimage.median)
  753. def test_median_no_int_overflow(xp):
  754. # test integer overflow fix on example from gh-12836
  755. a = xp.asarray([65, 70], dtype=xp.int8)
  756. output = ndimage.median(a, labels=xp.ones((2,)), index=xp.asarray([1]))
  757. assert_array_almost_equal(output, xp.asarray([67.5]))
  758. @make_xp_test_case(ndimage.variance)
  759. def test_variance01(xp):
  760. with np.errstate(all='ignore'):
  761. for type in types:
  762. dtype = getattr(xp, type)
  763. input = xp.asarray([], dtype=dtype)
  764. with warnings.catch_warnings():
  765. warnings.filterwarnings("ignore", "Mean of empty slice", RuntimeWarning)
  766. output = ndimage.variance(input)
  767. assert xp.isnan(output)
  768. @make_xp_test_case(ndimage.variance)
  769. def test_variance02(xp):
  770. for type in types:
  771. dtype = getattr(xp, type)
  772. input = xp.asarray([1], dtype=dtype)
  773. output = ndimage.variance(input)
  774. assert_almost_equal(output, xp.asarray(0.0), check_0d=False)
  775. @make_xp_test_case(ndimage.variance)
  776. def test_variance03(xp):
  777. for type in types:
  778. dtype = getattr(xp, type)
  779. input = xp.asarray([1, 3], dtype=dtype)
  780. output = ndimage.variance(input)
  781. assert_almost_equal(output, xp.asarray(1.0), check_0d=False)
  782. @make_xp_test_case(ndimage.variance)
  783. def test_variance04(xp):
  784. input = np.asarray([1, 0], dtype=bool)
  785. input = xp.asarray(input)
  786. output = ndimage.variance(input)
  787. assert_almost_equal(output, xp.asarray(0.25), check_0d=False)
  788. @make_xp_test_case(ndimage.variance)
  789. def test_variance05(xp):
  790. labels = xp.asarray([2, 2, 3])
  791. for type in types:
  792. dtype = getattr(xp, type)
  793. input = xp.asarray([1, 3, 8], dtype=dtype)
  794. output = ndimage.variance(input, labels, 2)
  795. assert_almost_equal(output, xp.asarray(1.0), check_0d=False)
  796. @make_xp_test_case(ndimage.variance)
  797. def test_variance06(xp):
  798. labels = xp.asarray([2, 2, 3, 3, 4])
  799. with np.errstate(all='ignore'):
  800. for type in types:
  801. dtype = getattr(xp, type)
  802. input = xp.asarray([1, 3, 8, 10, 8], dtype=dtype)
  803. output = ndimage.variance(input, labels, xp.asarray([2, 3, 4]))
  804. assert_array_almost_equal(output, xp.asarray([1.0, 1.0, 0.0]))
  805. @make_xp_test_case(ndimage.standard_deviation)
  806. def test_standard_deviation01(xp):
  807. with np.errstate(all='ignore'):
  808. for type in types:
  809. dtype = getattr(xp, type)
  810. input = xp.asarray([], dtype=dtype)
  811. with warnings.catch_warnings():
  812. warnings.filterwarnings("ignore", "Mean of empty slice", RuntimeWarning)
  813. output = ndimage.standard_deviation(input)
  814. assert xp.isnan(output)
  815. @make_xp_test_case(ndimage.standard_deviation)
  816. def test_standard_deviation02(xp):
  817. for type in types:
  818. dtype = getattr(xp, type)
  819. input = xp.asarray([1], dtype=dtype)
  820. output = ndimage.standard_deviation(input)
  821. assert_almost_equal(output, xp.asarray(0.0), check_0d=False)
  822. @make_xp_test_case(ndimage.standard_deviation)
  823. def test_standard_deviation03(xp):
  824. for type in types:
  825. dtype = getattr(xp, type)
  826. input = xp.asarray([1, 3], dtype=dtype)
  827. output = ndimage.standard_deviation(input)
  828. assert_almost_equal(output, xp.asarray(1.0), check_0d=False)
  829. @make_xp_test_case(ndimage.standard_deviation)
  830. def test_standard_deviation04(xp):
  831. input = np.asarray([1, 0], dtype=bool)
  832. input = xp.asarray(input)
  833. output = ndimage.standard_deviation(input)
  834. assert_almost_equal(output, xp.asarray(0.5), check_0d=False)
  835. @make_xp_test_case(ndimage.standard_deviation)
  836. def test_standard_deviation05(xp):
  837. labels = xp.asarray([2, 2, 3])
  838. for type in types:
  839. dtype = getattr(xp, type)
  840. input = xp.asarray([1, 3, 8], dtype=dtype)
  841. output = ndimage.standard_deviation(input, labels, 2)
  842. assert_almost_equal(output, xp.asarray(1.0), check_0d=False)
  843. @make_xp_test_case(ndimage.standard_deviation)
  844. def test_standard_deviation06(xp):
  845. labels = xp.asarray([2, 2, 3, 3, 4])
  846. with np.errstate(all='ignore'):
  847. for type in types:
  848. dtype = getattr(xp, type)
  849. input = xp.asarray([1, 3, 8, 10, 8], dtype=dtype)
  850. output = ndimage.standard_deviation(
  851. input, labels, xp.asarray([2, 3, 4])
  852. )
  853. assert_array_almost_equal(output, xp.asarray([1.0, 1.0, 0.0]))
  854. @make_xp_test_case(ndimage.standard_deviation)
  855. def test_standard_deviation07(xp):
  856. labels = xp.asarray([1])
  857. with np.errstate(all='ignore'):
  858. for type in types:
  859. if is_torch(xp) and type == 'uint8':
  860. pytest.xfail("value cannot be converted to type uint8 "
  861. "without overflow")
  862. dtype = getattr(xp, type)
  863. input = xp.asarray([-0.00619519], dtype=dtype)
  864. output = ndimage.standard_deviation(input, labels, xp.asarray([1]))
  865. assert_array_almost_equal(output, xp.asarray([0]))
  866. @make_xp_test_case(ndimage.minimum_position)
  867. def test_minimum_position01(xp):
  868. labels = np.asarray([1, 0], dtype=bool)
  869. labels = xp.asarray(labels)
  870. for type in types:
  871. dtype = getattr(xp, type)
  872. input = xp.asarray([[1, 2], [3, 4]], dtype=dtype)
  873. output = ndimage.minimum_position(input, labels=labels)
  874. assert output == (0, 0)
  875. @make_xp_test_case(ndimage.minimum_position)
  876. def test_minimum_position02(xp):
  877. for type in types:
  878. dtype = getattr(xp, type)
  879. input = xp.asarray([[5, 4, 2, 5],
  880. [3, 7, 0, 2],
  881. [1, 5, 1, 1]], dtype=dtype)
  882. output = ndimage.minimum_position(input)
  883. assert output == (1, 2)
  884. @make_xp_test_case(ndimage.minimum_position)
  885. def test_minimum_position03(xp):
  886. input = np.asarray([[5, 4, 2, 5],
  887. [3, 7, 0, 2],
  888. [1, 5, 1, 1]], dtype=bool)
  889. input = xp.asarray(input)
  890. output = ndimage.minimum_position(input)
  891. assert output == (1, 2)
  892. @make_xp_test_case(ndimage.minimum_position)
  893. def test_minimum_position04(xp):
  894. input = np.asarray([[5, 4, 2, 5],
  895. [3, 7, 1, 2],
  896. [1, 5, 1, 1]], dtype=bool)
  897. input = xp.asarray(input)
  898. output = ndimage.minimum_position(input)
  899. assert output == (0, 0)
  900. @make_xp_test_case(ndimage.minimum_position)
  901. def test_minimum_position05(xp):
  902. labels = xp.asarray([1, 2, 0, 4])
  903. for type in types:
  904. dtype = getattr(xp, type)
  905. input = xp.asarray([[5, 4, 2, 5],
  906. [3, 7, 0, 2],
  907. [1, 5, 2, 3]], dtype=dtype)
  908. output = ndimage.minimum_position(input, labels)
  909. assert output == (2, 0)
  910. @make_xp_test_case(ndimage.minimum_position)
  911. def test_minimum_position06(xp):
  912. labels = xp.asarray([1, 2, 3, 4])
  913. for type in types:
  914. dtype = getattr(xp, type)
  915. input = xp.asarray([[5, 4, 2, 5],
  916. [3, 7, 0, 2],
  917. [1, 5, 1, 1]], dtype=dtype)
  918. output = ndimage.minimum_position(input, labels, 2)
  919. assert output == (0, 1)
  920. @make_xp_test_case(ndimage.minimum_position)
  921. def test_minimum_position07(xp):
  922. labels = xp.asarray([1, 2, 3, 4])
  923. for type in types:
  924. dtype = getattr(xp, type)
  925. input = xp.asarray([[5, 4, 2, 5],
  926. [3, 7, 0, 2],
  927. [1, 5, 1, 1]], dtype=dtype)
  928. output = ndimage.minimum_position(input, labels,
  929. xp.asarray([2, 3]))
  930. assert output[0] == (0, 1)
  931. assert output[1] == (1, 2)
  932. @make_xp_test_case(ndimage.maximum_position)
  933. def test_maximum_position01(xp):
  934. labels = np.asarray([1, 0], dtype=bool)
  935. labels = xp.asarray(labels)
  936. for type in types:
  937. dtype = getattr(xp, type)
  938. input = xp.asarray([[1, 2], [3, 4]], dtype=dtype)
  939. output = ndimage.maximum_position(input,
  940. labels=labels)
  941. assert output == (1, 0)
  942. @make_xp_test_case(ndimage.maximum_position)
  943. def test_maximum_position02(xp):
  944. for type in types:
  945. dtype = getattr(xp, type)
  946. input = xp.asarray([[5, 4, 2, 5],
  947. [3, 7, 8, 2],
  948. [1, 5, 1, 1]], dtype=dtype)
  949. output = ndimage.maximum_position(input)
  950. assert output == (1, 2)
  951. @make_xp_test_case(ndimage.maximum_position)
  952. def test_maximum_position03(xp):
  953. input = np.asarray([[5, 4, 2, 5],
  954. [3, 7, 8, 2],
  955. [1, 5, 1, 1]], dtype=bool)
  956. input = xp.asarray(input)
  957. output = ndimage.maximum_position(input)
  958. assert output == (0, 0)
  959. @make_xp_test_case(ndimage.maximum_position)
  960. def test_maximum_position04(xp):
  961. labels = xp.asarray([1, 2, 0, 4])
  962. for type in types:
  963. dtype = getattr(xp, type)
  964. input = xp.asarray([[5, 4, 2, 5],
  965. [3, 7, 8, 2],
  966. [1, 5, 1, 1]], dtype=dtype)
  967. output = ndimage.maximum_position(input, labels)
  968. assert output == (1, 1)
  969. @make_xp_test_case(ndimage.maximum_position)
  970. def test_maximum_position05(xp):
  971. labels = xp.asarray([1, 2, 0, 4])
  972. for type in types:
  973. dtype = getattr(xp, type)
  974. input = xp.asarray([[5, 4, 2, 5],
  975. [3, 7, 8, 2],
  976. [1, 5, 1, 1]], dtype=dtype)
  977. output = ndimage.maximum_position(input, labels, 1)
  978. assert output == (0, 0)
  979. @make_xp_test_case(ndimage.maximum_position)
  980. def test_maximum_position06(xp):
  981. labels = xp.asarray([1, 2, 0, 4])
  982. for type in types:
  983. dtype = getattr(xp, type)
  984. input = xp.asarray([[5, 4, 2, 5],
  985. [3, 7, 8, 2],
  986. [1, 5, 1, 1]], dtype=dtype)
  987. output = ndimage.maximum_position(input, labels,
  988. xp.asarray([1, 2]))
  989. assert output[0] == (0, 0)
  990. assert output[1] == (1, 1)
  991. @make_xp_test_case(ndimage.maximum_position)
  992. def test_maximum_position07(xp):
  993. # Test float labels
  994. labels = xp.asarray([1.0, 2.5, 0.0, 4.5])
  995. for type in types:
  996. dtype = getattr(xp, type)
  997. input = xp.asarray([[5, 4, 2, 5],
  998. [3, 7, 8, 2],
  999. [1, 5, 1, 1]], dtype=dtype)
  1000. output = ndimage.maximum_position(input, labels,
  1001. xp.asarray([1.0, 4.5]))
  1002. assert output[0] == (0, 0)
  1003. assert output[1] == (0, 3)
  1004. @make_xp_test_case(ndimage.extrema, ndimage.minimum, ndimage.maximum,
  1005. ndimage.minimum_position, ndimage.maximum_position)
  1006. def test_extrema01(xp):
  1007. labels = np.asarray([1, 0], dtype=bool)
  1008. labels = xp.asarray(labels)
  1009. for type in types:
  1010. dtype = getattr(xp, type)
  1011. input = xp.asarray([[1, 2], [3, 4]], dtype=dtype)
  1012. output1 = ndimage.extrema(input, labels=labels)
  1013. output2 = ndimage.minimum(input, labels=labels)
  1014. output3 = ndimage.maximum(input, labels=labels)
  1015. output4 = ndimage.minimum_position(input,
  1016. labels=labels)
  1017. output5 = ndimage.maximum_position(input,
  1018. labels=labels)
  1019. assert output1 == (output2, output3, output4, output5)
  1020. @make_xp_test_case(ndimage.extrema, ndimage.minimum, ndimage.maximum,
  1021. ndimage.minimum_position, ndimage.maximum_position)
  1022. def test_extrema02(xp):
  1023. labels = xp.asarray([1, 2])
  1024. for type in types:
  1025. dtype = getattr(xp, type)
  1026. input = xp.asarray([[1, 2], [3, 4]], dtype=dtype)
  1027. output1 = ndimage.extrema(input, labels=labels,
  1028. index=2)
  1029. output2 = ndimage.minimum(input, labels=labels,
  1030. index=2)
  1031. output3 = ndimage.maximum(input, labels=labels,
  1032. index=2)
  1033. output4 = ndimage.minimum_position(input,
  1034. labels=labels, index=2)
  1035. output5 = ndimage.maximum_position(input,
  1036. labels=labels, index=2)
  1037. assert output1 == (output2, output3, output4, output5)
  1038. @make_xp_test_case(ndimage.extrema, ndimage.minimum, ndimage.maximum,
  1039. ndimage.minimum_position, ndimage.maximum_position)
  1040. def test_extrema03(xp):
  1041. labels = xp.asarray([[1, 2], [2, 3]])
  1042. for type in types:
  1043. if is_torch(xp) and type in ("uint16", "uint32", "uint64"):
  1044. pytest.xfail("https://github.com/pytorch/pytorch/issues/58734")
  1045. dtype = getattr(xp, type)
  1046. input = xp.asarray([[1, 2], [3, 4]], dtype=dtype)
  1047. output1 = ndimage.extrema(input,
  1048. labels=labels,
  1049. index=xp.asarray([2, 3, 8]))
  1050. output2 = ndimage.minimum(input,
  1051. labels=labels,
  1052. index=xp.asarray([2, 3, 8]))
  1053. output3 = ndimage.maximum(input, labels=labels,
  1054. index=xp.asarray([2, 3, 8]))
  1055. output4 = ndimage.minimum_position(input,
  1056. labels=labels,
  1057. index=xp.asarray([2, 3, 8]))
  1058. output5 = ndimage.maximum_position(input,
  1059. labels=labels,
  1060. index=xp.asarray([2, 3, 8]))
  1061. assert_array_almost_equal(output1[0], output2)
  1062. assert_array_almost_equal(output1[1], output3)
  1063. assert output1[2] == output4
  1064. assert output1[3] == output5
  1065. @make_xp_test_case(ndimage.extrema, ndimage.minimum, ndimage.maximum,
  1066. ndimage.minimum_position, ndimage.maximum_position)
  1067. def test_extrema04(xp):
  1068. labels = xp.asarray([1, 2, 0, 4])
  1069. for type in types:
  1070. if is_torch(xp) and type in ("uint16", "uint32", "uint64"):
  1071. pytest.xfail("https://github.com/pytorch/pytorch/issues/58734")
  1072. dtype = getattr(xp, type)
  1073. input = xp.asarray([[5, 4, 2, 5],
  1074. [3, 7, 8, 2],
  1075. [1, 5, 1, 1]], dtype=dtype)
  1076. output1 = ndimage.extrema(input, labels, xp.asarray([1, 2]))
  1077. output2 = ndimage.minimum(input, labels, xp.asarray([1, 2]))
  1078. output3 = ndimage.maximum(input, labels, xp.asarray([1, 2]))
  1079. output4 = ndimage.minimum_position(input, labels,
  1080. xp.asarray([1, 2]))
  1081. output5 = ndimage.maximum_position(input, labels,
  1082. xp.asarray([1, 2]))
  1083. assert_array_almost_equal(output1[0], output2)
  1084. assert_array_almost_equal(output1[1], output3)
  1085. assert output1[2] == output4
  1086. assert output1[3] == output5
  1087. @make_xp_test_case(ndimage.center_of_mass)
  1088. def test_center_of_mass01(xp):
  1089. expected = (0.0, 0.0)
  1090. for type in types:
  1091. dtype = getattr(xp, type)
  1092. input = xp.asarray([[1, 0], [0, 0]], dtype=dtype)
  1093. output = ndimage.center_of_mass(input)
  1094. assert output == expected
  1095. @make_xp_test_case(ndimage.center_of_mass)
  1096. def test_center_of_mass02(xp):
  1097. expected = (1, 0)
  1098. for type in types:
  1099. dtype = getattr(xp, type)
  1100. input = xp.asarray([[0, 0], [1, 0]], dtype=dtype)
  1101. output = ndimage.center_of_mass(input)
  1102. assert output == expected
  1103. @make_xp_test_case(ndimage.center_of_mass)
  1104. def test_center_of_mass03(xp):
  1105. expected = (0, 1)
  1106. for type in types:
  1107. dtype = getattr(xp, type)
  1108. input = xp.asarray([[0, 1], [0, 0]], dtype=dtype)
  1109. output = ndimage.center_of_mass(input)
  1110. assert output == expected
  1111. @make_xp_test_case(ndimage.center_of_mass)
  1112. def test_center_of_mass04(xp):
  1113. expected = (1, 1)
  1114. for type in types:
  1115. dtype = getattr(xp, type)
  1116. input = xp.asarray([[0, 0], [0, 1]], dtype=dtype)
  1117. output = ndimage.center_of_mass(input)
  1118. assert output == expected
  1119. @make_xp_test_case(ndimage.center_of_mass)
  1120. def test_center_of_mass05(xp):
  1121. expected = (0.5, 0.5)
  1122. for type in types:
  1123. dtype = getattr(xp, type)
  1124. input = xp.asarray([[1, 1], [1, 1]], dtype=dtype)
  1125. output = ndimage.center_of_mass(input)
  1126. assert output == expected
  1127. @make_xp_test_case(ndimage.center_of_mass)
  1128. def test_center_of_mass06(xp):
  1129. expected = (0.5, 0.5)
  1130. input = np.asarray([[1, 2], [3, 1]], dtype=bool)
  1131. input = xp.asarray(input)
  1132. output = ndimage.center_of_mass(input)
  1133. assert output == expected
  1134. @make_xp_test_case(ndimage.center_of_mass)
  1135. def test_center_of_mass07(xp):
  1136. labels = xp.asarray([1, 0])
  1137. expected = (0.5, 0.0)
  1138. input = np.asarray([[1, 2], [3, 1]], dtype=bool)
  1139. input = xp.asarray(input)
  1140. output = ndimage.center_of_mass(input, labels)
  1141. assert output == expected
  1142. @make_xp_test_case(ndimage.center_of_mass)
  1143. def test_center_of_mass08(xp):
  1144. labels = xp.asarray([1, 2])
  1145. expected = (0.5, 1.0)
  1146. input = np.asarray([[5, 2], [3, 1]], dtype=bool)
  1147. input = xp.asarray(input)
  1148. output = ndimage.center_of_mass(input, labels, 2)
  1149. assert output == expected
  1150. @make_xp_test_case(ndimage.center_of_mass)
  1151. def test_center_of_mass09(xp):
  1152. labels = xp.asarray((1, 2))
  1153. expected = xp.asarray([(0.5, 0.0), (0.5, 1.0)], dtype=xp.float64)
  1154. input = np.asarray([[1, 2], [1, 1]], dtype=bool)
  1155. input = xp.asarray(input)
  1156. output = ndimage.center_of_mass(input, labels, xp.asarray([1, 2]))
  1157. xp_assert_equal(xp.asarray(output), xp.asarray(expected))
  1158. @make_xp_test_case(ndimage.histogram)
  1159. def test_histogram01(xp):
  1160. expected = xp.ones(10)
  1161. input = xp.arange(10)
  1162. output = ndimage.histogram(input, 0, 10, 10)
  1163. assert_array_almost_equal(output, expected)
  1164. @make_xp_test_case(ndimage.histogram)
  1165. def test_histogram02(xp):
  1166. labels = xp.asarray([1, 1, 1, 1, 2, 2, 2, 2])
  1167. expected = xp.asarray([0, 2, 0, 1, 1])
  1168. input = xp.asarray([1, 1, 3, 4, 3, 3, 3, 3])
  1169. output = ndimage.histogram(input, 0, 4, 5, labels, 1)
  1170. assert_array_almost_equal(output, expected)
  1171. @skip_xp_backends(np_only=True, reason='object arrays')
  1172. @make_xp_test_case(ndimage.histogram)
  1173. def test_histogram03(xp):
  1174. labels = xp.asarray([1, 0, 1, 1, 2, 2, 2, 2])
  1175. expected1 = xp.asarray([0, 1, 0, 1, 1])
  1176. expected2 = xp.asarray([0, 0, 0, 3, 0])
  1177. input = xp.asarray([1, 1, 3, 4, 3, 5, 3, 3])
  1178. output = ndimage.histogram(input, 0, 4, 5, labels, (1, 2))
  1179. assert_array_almost_equal(output[0], expected1)
  1180. assert_array_almost_equal(output[1], expected2)
  1181. @make_xp_test_case(ndimage.mean, ndimage.variance, ndimage.standard_deviation,
  1182. ndimage.median, ndimage.minimum, ndimage.maximum)
  1183. def test_stat_funcs_2d(xp):
  1184. a = xp.asarray([[5, 6, 0, 0, 0], [8, 9, 0, 0, 0], [0, 0, 0, 3, 5]])
  1185. lbl = xp.asarray([[1, 1, 0, 0, 0], [1, 1, 0, 0, 0], [0, 0, 0, 2, 2]])
  1186. mean = ndimage.mean(a, labels=lbl, index=xp.asarray([1, 2]))
  1187. xp_assert_equal(mean, xp.asarray([7.0, 4.0], dtype=xp.float64))
  1188. var = ndimage.variance(a, labels=lbl, index=xp.asarray([1, 2]))
  1189. xp_assert_equal(var, xp.asarray([2.5, 1.0], dtype=xp.float64))
  1190. std = ndimage.standard_deviation(a, labels=lbl, index=xp.asarray([1, 2]))
  1191. assert_array_almost_equal(std, xp.sqrt(xp.asarray([2.5, 1.0], dtype=xp.float64)))
  1192. med = ndimage.median(a, labels=lbl, index=xp.asarray([1, 2]))
  1193. xp_assert_equal(med, xp.asarray([7.0, 4.0], dtype=xp.float64))
  1194. min = ndimage.minimum(a, labels=lbl, index=xp.asarray([1, 2]))
  1195. xp_assert_equal(min, xp.asarray([5, 3]), check_dtype=False)
  1196. max = ndimage.maximum(a, labels=lbl, index=xp.asarray([1, 2]))
  1197. xp_assert_equal(max, xp.asarray([9, 5]), check_dtype=False)
  1198. @skip_xp_backends("cupy", reason="no watershed_ift on CuPy")
  1199. @make_xp_test_case(ndimage.watershed_ift)
  1200. class TestWatershedIft:
  1201. def test_watershed_ift01(self, xp):
  1202. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0],
  1203. [0, 1, 1, 1, 1, 1, 0],
  1204. [0, 1, 0, 0, 0, 1, 0],
  1205. [0, 1, 0, 0, 0, 1, 0],
  1206. [0, 1, 0, 0, 0, 1, 0],
  1207. [0, 1, 1, 1, 1, 1, 0],
  1208. [0, 0, 0, 0, 0, 0, 0],
  1209. [0, 0, 0, 0, 0, 0, 0]], dtype=xp.uint8)
  1210. markers = xp.asarray([[-1, 0, 0, 0, 0, 0, 0],
  1211. [0, 0, 0, 0, 0, 0, 0],
  1212. [0, 0, 0, 0, 0, 0, 0],
  1213. [0, 0, 0, 1, 0, 0, 0],
  1214. [0, 0, 0, 0, 0, 0, 0],
  1215. [0, 0, 0, 0, 0, 0, 0],
  1216. [0, 0, 0, 0, 0, 0, 0],
  1217. [0, 0, 0, 0, 0, 0, 0]], dtype=xp.int8)
  1218. structure=xp.asarray([[1, 1, 1],
  1219. [1, 1, 1],
  1220. [1, 1, 1]])
  1221. out = ndimage.watershed_ift(data, markers, structure=structure)
  1222. expected = [[-1, -1, -1, -1, -1, -1, -1],
  1223. [-1, 1, 1, 1, 1, 1, -1],
  1224. [-1, 1, 1, 1, 1, 1, -1],
  1225. [-1, 1, 1, 1, 1, 1, -1],
  1226. [-1, 1, 1, 1, 1, 1, -1],
  1227. [-1, 1, 1, 1, 1, 1, -1],
  1228. [-1, -1, -1, -1, -1, -1, -1],
  1229. [-1, -1, -1, -1, -1, -1, -1]]
  1230. assert_array_almost_equal(out, xp.asarray(expected))
  1231. def test_watershed_ift02(self, xp):
  1232. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0],
  1233. [0, 1, 1, 1, 1, 1, 0],
  1234. [0, 1, 0, 0, 0, 1, 0],
  1235. [0, 1, 0, 0, 0, 1, 0],
  1236. [0, 1, 0, 0, 0, 1, 0],
  1237. [0, 1, 1, 1, 1, 1, 0],
  1238. [0, 0, 0, 0, 0, 0, 0],
  1239. [0, 0, 0, 0, 0, 0, 0]], dtype=xp.uint8)
  1240. markers = xp.asarray([[-1, 0, 0, 0, 0, 0, 0],
  1241. [0, 0, 0, 0, 0, 0, 0],
  1242. [0, 0, 0, 0, 0, 0, 0],
  1243. [0, 0, 0, 1, 0, 0, 0],
  1244. [0, 0, 0, 0, 0, 0, 0],
  1245. [0, 0, 0, 0, 0, 0, 0],
  1246. [0, 0, 0, 0, 0, 0, 0],
  1247. [0, 0, 0, 0, 0, 0, 0]], dtype=xp.int8)
  1248. out = ndimage.watershed_ift(data, markers)
  1249. expected = [[-1, -1, -1, -1, -1, -1, -1],
  1250. [-1, -1, 1, 1, 1, -1, -1],
  1251. [-1, 1, 1, 1, 1, 1, -1],
  1252. [-1, 1, 1, 1, 1, 1, -1],
  1253. [-1, 1, 1, 1, 1, 1, -1],
  1254. [-1, -1, 1, 1, 1, -1, -1],
  1255. [-1, -1, -1, -1, -1, -1, -1],
  1256. [-1, -1, -1, -1, -1, -1, -1]]
  1257. assert_array_almost_equal(out, xp.asarray(expected))
  1258. def test_watershed_ift03(self, xp):
  1259. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0],
  1260. [0, 1, 1, 1, 1, 1, 0],
  1261. [0, 1, 0, 1, 0, 1, 0],
  1262. [0, 1, 0, 1, 0, 1, 0],
  1263. [0, 1, 0, 1, 0, 1, 0],
  1264. [0, 1, 1, 1, 1, 1, 0],
  1265. [0, 0, 0, 0, 0, 0, 0]], dtype=xp.uint8)
  1266. markers = xp.asarray([[0, 0, 0, 0, 0, 0, 0],
  1267. [0, 0, 0, 0, 0, 0, 0],
  1268. [0, 0, 0, 0, 0, 0, 0],
  1269. [0, 0, 2, 0, 3, 0, 0],
  1270. [0, 0, 0, 0, 0, 0, 0],
  1271. [0, 0, 0, 0, 0, 0, 0],
  1272. [0, 0, 0, 0, 0, 0, -1]], dtype=xp.int8)
  1273. out = ndimage.watershed_ift(data, markers)
  1274. expected = [[-1, -1, -1, -1, -1, -1, -1],
  1275. [-1, -1, 2, -1, 3, -1, -1],
  1276. [-1, 2, 2, 3, 3, 3, -1],
  1277. [-1, 2, 2, 3, 3, 3, -1],
  1278. [-1, 2, 2, 3, 3, 3, -1],
  1279. [-1, -1, 2, -1, 3, -1, -1],
  1280. [-1, -1, -1, -1, -1, -1, -1]]
  1281. assert_array_almost_equal(out, xp.asarray(expected))
  1282. def test_watershed_ift04(self, xp):
  1283. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0],
  1284. [0, 1, 1, 1, 1, 1, 0],
  1285. [0, 1, 0, 1, 0, 1, 0],
  1286. [0, 1, 0, 1, 0, 1, 0],
  1287. [0, 1, 0, 1, 0, 1, 0],
  1288. [0, 1, 1, 1, 1, 1, 0],
  1289. [0, 0, 0, 0, 0, 0, 0]], dtype=xp.uint8)
  1290. markers = xp.asarray([[0, 0, 0, 0, 0, 0, 0],
  1291. [0, 0, 0, 0, 0, 0, 0],
  1292. [0, 0, 0, 0, 0, 0, 0],
  1293. [0, 0, 2, 0, 3, 0, 0],
  1294. [0, 0, 0, 0, 0, 0, 0],
  1295. [0, 0, 0, 0, 0, 0, 0],
  1296. [0, 0, 0, 0, 0, 0, -1]],
  1297. dtype=xp.int8)
  1298. structure=xp.asarray([[1, 1, 1],
  1299. [1, 1, 1],
  1300. [1, 1, 1]])
  1301. out = ndimage.watershed_ift(data, markers, structure=structure)
  1302. expected = [[-1, -1, -1, -1, -1, -1, -1],
  1303. [-1, 2, 2, 3, 3, 3, -1],
  1304. [-1, 2, 2, 3, 3, 3, -1],
  1305. [-1, 2, 2, 3, 3, 3, -1],
  1306. [-1, 2, 2, 3, 3, 3, -1],
  1307. [-1, 2, 2, 3, 3, 3, -1],
  1308. [-1, -1, -1, -1, -1, -1, -1]]
  1309. assert_array_almost_equal(out, xp.asarray(expected))
  1310. def test_watershed_ift05(self, xp):
  1311. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0],
  1312. [0, 1, 1, 1, 1, 1, 0],
  1313. [0, 1, 0, 1, 0, 1, 0],
  1314. [0, 1, 0, 1, 0, 1, 0],
  1315. [0, 1, 0, 1, 0, 1, 0],
  1316. [0, 1, 1, 1, 1, 1, 0],
  1317. [0, 0, 0, 0, 0, 0, 0]], dtype=xp.uint8)
  1318. markers = xp.asarray([[0, 0, 0, 0, 0, 0, 0],
  1319. [0, 0, 0, 0, 0, 0, 0],
  1320. [0, 0, 0, 0, 0, 0, 0],
  1321. [0, 0, 3, 0, 2, 0, 0],
  1322. [0, 0, 0, 0, 0, 0, 0],
  1323. [0, 0, 0, 0, 0, 0, 0],
  1324. [0, 0, 0, 0, 0, 0, -1]],
  1325. dtype=xp.int8)
  1326. structure = xp.asarray([[1, 1, 1],
  1327. [1, 1, 1],
  1328. [1, 1, 1]])
  1329. out = ndimage.watershed_ift(data, markers, structure=structure)
  1330. expected = [[-1, -1, -1, -1, -1, -1, -1],
  1331. [-1, 3, 3, 2, 2, 2, -1],
  1332. [-1, 3, 3, 2, 2, 2, -1],
  1333. [-1, 3, 3, 2, 2, 2, -1],
  1334. [-1, 3, 3, 2, 2, 2, -1],
  1335. [-1, 3, 3, 2, 2, 2, -1],
  1336. [-1, -1, -1, -1, -1, -1, -1]]
  1337. assert_array_almost_equal(out, xp.asarray(expected))
  1338. def test_watershed_ift06(self, xp):
  1339. data = xp.asarray([[0, 1, 0, 0, 0, 1, 0],
  1340. [0, 1, 0, 0, 0, 1, 0],
  1341. [0, 1, 0, 0, 0, 1, 0],
  1342. [0, 1, 1, 1, 1, 1, 0],
  1343. [0, 0, 0, 0, 0, 0, 0],
  1344. [0, 0, 0, 0, 0, 0, 0]], dtype=xp.uint8)
  1345. markers = xp.asarray([[-1, 0, 0, 0, 0, 0, 0],
  1346. [0, 0, 0, 1, 0, 0, 0],
  1347. [0, 0, 0, 0, 0, 0, 0],
  1348. [0, 0, 0, 0, 0, 0, 0],
  1349. [0, 0, 0, 0, 0, 0, 0],
  1350. [0, 0, 0, 0, 0, 0, 0]], dtype=xp.int8)
  1351. structure=xp.asarray([[1, 1, 1],
  1352. [1, 1, 1],
  1353. [1, 1, 1]])
  1354. out = ndimage.watershed_ift(data, markers, structure=structure)
  1355. expected = [[-1, 1, 1, 1, 1, 1, -1],
  1356. [-1, 1, 1, 1, 1, 1, -1],
  1357. [-1, 1, 1, 1, 1, 1, -1],
  1358. [-1, 1, 1, 1, 1, 1, -1],
  1359. [-1, -1, -1, -1, -1, -1, -1],
  1360. [-1, -1, -1, -1, -1, -1, -1]]
  1361. assert_array_almost_equal(out, xp.asarray(expected))
  1362. @skip_xp_backends(np_only=True, reason="inplace ops are numpy-specific")
  1363. def test_watershed_ift07(self, xp):
  1364. shape = (7, 6)
  1365. data = np.zeros(shape, dtype=np.uint8)
  1366. data = data.transpose()
  1367. data[...] = np.asarray([[0, 1, 0, 0, 0, 1, 0],
  1368. [0, 1, 0, 0, 0, 1, 0],
  1369. [0, 1, 0, 0, 0, 1, 0],
  1370. [0, 1, 1, 1, 1, 1, 0],
  1371. [0, 0, 0, 0, 0, 0, 0],
  1372. [0, 0, 0, 0, 0, 0, 0]], dtype=np.uint8)
  1373. data = xp.asarray(data)
  1374. markers = xp.asarray([[-1, 0, 0, 0, 0, 0, 0],
  1375. [0, 0, 0, 1, 0, 0, 0],
  1376. [0, 0, 0, 0, 0, 0, 0],
  1377. [0, 0, 0, 0, 0, 0, 0],
  1378. [0, 0, 0, 0, 0, 0, 0],
  1379. [0, 0, 0, 0, 0, 0, 0]], dtype=xp.int8)
  1380. out = xp.zeros(shape, dtype=xp.int16)
  1381. out = out.T
  1382. structure=xp.asarray([[1, 1, 1],
  1383. [1, 1, 1],
  1384. [1, 1, 1]])
  1385. ndimage.watershed_ift(data, markers, structure=structure,
  1386. output=out)
  1387. expected = [[-1, 1, 1, 1, 1, 1, -1],
  1388. [-1, 1, 1, 1, 1, 1, -1],
  1389. [-1, 1, 1, 1, 1, 1, -1],
  1390. [-1, 1, 1, 1, 1, 1, -1],
  1391. [-1, -1, -1, -1, -1, -1, -1],
  1392. [-1, -1, -1, -1, -1, -1, -1]]
  1393. assert_array_almost_equal(out, xp.asarray(expected))
  1394. @skip_xp_backends("cupy", reason="no watershed_ift on CuPy")
  1395. def test_watershed_ift08(self, xp):
  1396. # Test cost larger than uint8. See gh-10069.
  1397. data = xp.asarray([[256, 0],
  1398. [0, 0]], dtype=xp.uint16)
  1399. markers = xp.asarray([[1, 0],
  1400. [0, 0]], dtype=xp.int8)
  1401. out = ndimage.watershed_ift(data, markers)
  1402. expected = [[1, 1],
  1403. [1, 1]]
  1404. assert_array_almost_equal(out, xp.asarray(expected))
  1405. @skip_xp_backends("cupy", reason="no watershed_ift on CuPy")
  1406. def test_watershed_ift09(self, xp):
  1407. # Test large cost. See gh-19575
  1408. data = xp.asarray([[xp.iinfo(xp.uint16).max, 0],
  1409. [0, 0]], dtype=xp.uint16)
  1410. markers = xp.asarray([[1, 0],
  1411. [0, 0]], dtype=xp.int8)
  1412. out = ndimage.watershed_ift(data, markers)
  1413. expected = [[1, 1],
  1414. [1, 1]]
  1415. xp_assert_close(out, xp.asarray(expected), check_dtype=False)
  1416. @skip_xp_backends(np_only=True)
  1417. @pytest.mark.parametrize("dt", [np.intc, np.uintc])
  1418. @make_xp_test_case(ndimage.value_indices)
  1419. def test_gh_19423(dt, xp):
  1420. rng = np.random.default_rng(123)
  1421. max_val = 8
  1422. image = rng.integers(low=0, high=max_val, size=(10, 12)).astype(dtype=dt)
  1423. val_idx = ndimage.value_indices(image)
  1424. assert len(val_idx.keys()) == max_val