test_measurements.py 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  1. from __future__ import division, print_function, absolute_import
  2. import os.path
  3. import numpy as np
  4. from numpy.testing import (assert_, assert_array_almost_equal, assert_equal,
  5. assert_almost_equal, assert_array_equal)
  6. from pytest import raises as assert_raises
  7. from scipy._lib._numpy_compat import suppress_warnings
  8. import scipy.ndimage as ndimage
  9. types = [np.int8, np.uint8, np.int16,
  10. np.uint16, np.int32, np.uint32,
  11. np.int64, np.uint64,
  12. np.float32, np.float64]
  13. np.mod(1., 1) # Silence fmod bug on win-amd64. See #1408 and #1238.
  14. class Test_measurements_stats(object):
  15. """ndimage.measurements._stats() is a utility function used by other functions."""
  16. def test_a(self):
  17. x = [0,1,2,6]
  18. labels = [0,0,1,1]
  19. index = [0,1]
  20. for shp in [(4,), (2,2)]:
  21. x = np.array(x).reshape(shp)
  22. labels = np.array(labels).reshape(shp)
  23. counts, sums = ndimage.measurements._stats(x, labels=labels, index=index)
  24. assert_array_equal(counts, [2, 2])
  25. assert_array_equal(sums, [1.0, 8.0])
  26. def test_b(self):
  27. # Same data as test_a, but different labels. The label 9 exceeds the
  28. # length of 'labels', so this test will follow a different code path.
  29. x = [0,1,2,6]
  30. labels = [0,0,9,9]
  31. index = [0,9]
  32. for shp in [(4,), (2,2)]:
  33. x = np.array(x).reshape(shp)
  34. labels = np.array(labels).reshape(shp)
  35. counts, sums = ndimage.measurements._stats(x, labels=labels, index=index)
  36. assert_array_equal(counts, [2, 2])
  37. assert_array_equal(sums, [1.0, 8.0])
  38. def test_a_centered(self):
  39. x = [0,1,2,6]
  40. labels = [0,0,1,1]
  41. index = [0,1]
  42. for shp in [(4,), (2,2)]:
  43. x = np.array(x).reshape(shp)
  44. labels = np.array(labels).reshape(shp)
  45. counts, sums, centers = ndimage.measurements._stats(x, labels=labels,
  46. index=index, centered=True)
  47. assert_array_equal(counts, [2, 2])
  48. assert_array_equal(sums, [1.0, 8.0])
  49. assert_array_equal(centers, [0.5, 8.0])
  50. def test_b_centered(self):
  51. x = [0,1,2,6]
  52. labels = [0,0,9,9]
  53. index = [0,9]
  54. for shp in [(4,), (2,2)]:
  55. x = np.array(x).reshape(shp)
  56. labels = np.array(labels).reshape(shp)
  57. counts, sums, centers = ndimage.measurements._stats(x, labels=labels,
  58. index=index, centered=True)
  59. assert_array_equal(counts, [2, 2])
  60. assert_array_equal(sums, [1.0, 8.0])
  61. assert_array_equal(centers, [0.5, 8.0])
  62. def test_nonint_labels(self):
  63. x = [0,1,2,6]
  64. labels = [0.0, 0.0, 9.0, 9.0]
  65. index = [0.0, 9.0]
  66. for shp in [(4,), (2,2)]:
  67. x = np.array(x).reshape(shp)
  68. labels = np.array(labels).reshape(shp)
  69. counts, sums, centers = ndimage.measurements._stats(x, labels=labels,
  70. index=index, centered=True)
  71. assert_array_equal(counts, [2, 2])
  72. assert_array_equal(sums, [1.0, 8.0])
  73. assert_array_equal(centers, [0.5, 8.0])
  74. class Test_measurements_select(object):
  75. """ndimage.measurements._select() is a utility function used by other functions."""
  76. def test_basic(self):
  77. x = [0,1,6,2]
  78. cases = [
  79. ([0,0,1,1], [0,1]), # "Small" integer labels
  80. ([0,0,9,9], [0,9]), # A label larger than len(labels)
  81. ([0.0,0.0,7.0,7.0], [0.0, 7.0]), # Non-integer labels
  82. ]
  83. for labels, index in cases:
  84. result = ndimage.measurements._select(x, labels=labels, index=index)
  85. assert_(len(result) == 0)
  86. result = ndimage.measurements._select(x, labels=labels, index=index, find_max=True)
  87. assert_(len(result) == 1)
  88. assert_array_equal(result[0], [1, 6])
  89. result = ndimage.measurements._select(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(x, labels=labels, index=index,
  93. find_min=True, find_min_positions=True)
  94. assert_(len(result) == 2)
  95. assert_array_equal(result[0], [0, 2])
  96. assert_array_equal(result[1], [0, 3])
  97. assert_equal(result[1].dtype.kind, 'i')
  98. result = ndimage.measurements._select(x, labels=labels, index=index,
  99. find_max=True, find_max_positions=True)
  100. assert_(len(result) == 2)
  101. assert_array_equal(result[0], [1, 6])
  102. assert_array_equal(result[1], [1, 2])
  103. assert_equal(result[1].dtype.kind, 'i')
  104. def test_label01():
  105. data = np.ones([])
  106. out, n = ndimage.label(data)
  107. assert_array_almost_equal(out, 1)
  108. assert_equal(n, 1)
  109. def test_label02():
  110. data = np.zeros([])
  111. out, n = ndimage.label(data)
  112. assert_array_almost_equal(out, 0)
  113. assert_equal(n, 0)
  114. def test_label03():
  115. data = np.ones([1])
  116. out, n = ndimage.label(data)
  117. assert_array_almost_equal(out, [1])
  118. assert_equal(n, 1)
  119. def test_label04():
  120. data = np.zeros([1])
  121. out, n = ndimage.label(data)
  122. assert_array_almost_equal(out, [0])
  123. assert_equal(n, 0)
  124. def test_label05():
  125. data = np.ones([5])
  126. out, n = ndimage.label(data)
  127. assert_array_almost_equal(out, [1, 1, 1, 1, 1])
  128. assert_equal(n, 1)
  129. def test_label06():
  130. data = np.array([1, 0, 1, 1, 0, 1])
  131. out, n = ndimage.label(data)
  132. assert_array_almost_equal(out, [1, 0, 2, 2, 0, 3])
  133. assert_equal(n, 3)
  134. def test_label07():
  135. data = np.array([[0, 0, 0, 0, 0, 0],
  136. [0, 0, 0, 0, 0, 0],
  137. [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. out, n = ndimage.label(data)
  142. assert_array_almost_equal(out, [[0, 0, 0, 0, 0, 0],
  143. [0, 0, 0, 0, 0, 0],
  144. [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. assert_equal(n, 0)
  149. def test_label08():
  150. data = np.array([[1, 0, 0, 0, 0, 0],
  151. [0, 0, 1, 1, 0, 0],
  152. [0, 0, 1, 1, 1, 0],
  153. [1, 1, 0, 0, 0, 0],
  154. [1, 1, 0, 0, 0, 0],
  155. [0, 0, 0, 1, 1, 0]])
  156. out, n = ndimage.label(data)
  157. assert_array_almost_equal(out, [[1, 0, 0, 0, 0, 0],
  158. [0, 0, 2, 2, 0, 0],
  159. [0, 0, 2, 2, 2, 0],
  160. [3, 3, 0, 0, 0, 0],
  161. [3, 3, 0, 0, 0, 0],
  162. [0, 0, 0, 4, 4, 0]])
  163. assert_equal(n, 4)
  164. def test_label09():
  165. data = np.array([[1, 0, 0, 0, 0, 0],
  166. [0, 0, 1, 1, 0, 0],
  167. [0, 0, 1, 1, 1, 0],
  168. [1, 1, 0, 0, 0, 0],
  169. [1, 1, 0, 0, 0, 0],
  170. [0, 0, 0, 1, 1, 0]])
  171. struct = ndimage.generate_binary_structure(2, 2)
  172. out, n = ndimage.label(data, struct)
  173. assert_array_almost_equal(out, [[1, 0, 0, 0, 0, 0],
  174. [0, 0, 2, 2, 0, 0],
  175. [0, 0, 2, 2, 2, 0],
  176. [2, 2, 0, 0, 0, 0],
  177. [2, 2, 0, 0, 0, 0],
  178. [0, 0, 0, 3, 3, 0]])
  179. assert_equal(n, 3)
  180. def test_label10():
  181. data = np.array([[0, 0, 0, 0, 0, 0],
  182. [0, 1, 1, 0, 1, 0],
  183. [0, 1, 1, 1, 1, 0],
  184. [0, 0, 0, 0, 0, 0]])
  185. struct = ndimage.generate_binary_structure(2, 2)
  186. out, n = ndimage.label(data, struct)
  187. assert_array_almost_equal(out, [[0, 0, 0, 0, 0, 0],
  188. [0, 1, 1, 0, 1, 0],
  189. [0, 1, 1, 1, 1, 0],
  190. [0, 0, 0, 0, 0, 0]])
  191. assert_equal(n, 1)
  192. def test_label11():
  193. for type in types:
  194. data = np.array([[1, 0, 0, 0, 0, 0],
  195. [0, 0, 1, 1, 0, 0],
  196. [0, 0, 1, 1, 1, 0],
  197. [1, 1, 0, 0, 0, 0],
  198. [1, 1, 0, 0, 0, 0],
  199. [0, 0, 0, 1, 1, 0]], type)
  200. out, n = ndimage.label(data)
  201. expected = [[1, 0, 0, 0, 0, 0],
  202. [0, 0, 2, 2, 0, 0],
  203. [0, 0, 2, 2, 2, 0],
  204. [3, 3, 0, 0, 0, 0],
  205. [3, 3, 0, 0, 0, 0],
  206. [0, 0, 0, 4, 4, 0]]
  207. assert_array_almost_equal(out, expected)
  208. assert_equal(n, 4)
  209. def test_label11_inplace():
  210. for type in types:
  211. data = np.array([[1, 0, 0, 0, 0, 0],
  212. [0, 0, 1, 1, 0, 0],
  213. [0, 0, 1, 1, 1, 0],
  214. [1, 1, 0, 0, 0, 0],
  215. [1, 1, 0, 0, 0, 0],
  216. [0, 0, 0, 1, 1, 0]], type)
  217. n = ndimage.label(data, output=data)
  218. expected = [[1, 0, 0, 0, 0, 0],
  219. [0, 0, 2, 2, 0, 0],
  220. [0, 0, 2, 2, 2, 0],
  221. [3, 3, 0, 0, 0, 0],
  222. [3, 3, 0, 0, 0, 0],
  223. [0, 0, 0, 4, 4, 0]]
  224. assert_array_almost_equal(data, expected)
  225. assert_equal(n, 4)
  226. def test_label12():
  227. for type in types:
  228. data = np.array([[0, 0, 0, 0, 1, 1],
  229. [0, 0, 0, 0, 0, 1],
  230. [0, 0, 1, 0, 1, 1],
  231. [0, 0, 1, 1, 1, 1],
  232. [0, 0, 0, 1, 1, 0]], type)
  233. out, n = ndimage.label(data)
  234. expected = [[0, 0, 0, 0, 1, 1],
  235. [0, 0, 0, 0, 0, 1],
  236. [0, 0, 1, 0, 1, 1],
  237. [0, 0, 1, 1, 1, 1],
  238. [0, 0, 0, 1, 1, 0]]
  239. assert_array_almost_equal(out, expected)
  240. assert_equal(n, 1)
  241. def test_label13():
  242. for type in types:
  243. data = np.array([[1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1],
  244. [1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1],
  245. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  246. [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]],
  247. type)
  248. out, n = ndimage.label(data)
  249. expected = [[1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1],
  250. [1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1],
  251. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  252. [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
  253. assert_array_almost_equal(out, expected)
  254. assert_equal(n, 1)
  255. def test_label_output_typed():
  256. data = np.ones([5])
  257. for t in types:
  258. output = np.zeros([5], dtype=t)
  259. n = ndimage.label(data, output=output)
  260. assert_array_almost_equal(output, 1)
  261. assert_equal(n, 1)
  262. def test_label_output_dtype():
  263. data = np.ones([5])
  264. for t in types:
  265. output, n = ndimage.label(data, output=t)
  266. assert_array_almost_equal(output, 1)
  267. assert output.dtype == t
  268. def test_label_output_wrong_size():
  269. data = np.ones([5])
  270. for t in types:
  271. output = np.zeros([10], t)
  272. assert_raises((RuntimeError, ValueError), ndimage.label, data, output=output)
  273. def test_label_structuring_elements():
  274. data = np.loadtxt(os.path.join(os.path.dirname(__file__), "data", "label_inputs.txt"))
  275. strels = np.loadtxt(os.path.join(os.path.dirname(__file__), "data", "label_strels.txt"))
  276. results = np.loadtxt(os.path.join(os.path.dirname(__file__), "data", "label_results.txt"))
  277. data = data.reshape((-1, 7, 7))
  278. strels = strels.reshape((-1, 3, 3))
  279. results = results.reshape((-1, 7, 7))
  280. r = 0
  281. for i in range(data.shape[0]):
  282. d = data[i, :, :]
  283. for j in range(strels.shape[0]):
  284. s = strels[j, :, :]
  285. assert_equal(ndimage.label(d, s)[0], results[r, :, :])
  286. r += 1
  287. def test_label_default_dtype():
  288. test_array = np.random.rand(10, 10)
  289. label, no_features = ndimage.label(test_array > 0.5)
  290. assert_(label.dtype in (np.int32, np.int64))
  291. # Shouldn't raise an exception
  292. ndimage.find_objects(label)
  293. def test_find_objects01():
  294. data = np.ones([], dtype=int)
  295. out = ndimage.find_objects(data)
  296. assert_(out == [()])
  297. def test_find_objects02():
  298. data = np.zeros([], dtype=int)
  299. out = ndimage.find_objects(data)
  300. assert_(out == [])
  301. def test_find_objects03():
  302. data = np.ones([1], dtype=int)
  303. out = ndimage.find_objects(data)
  304. assert_equal(out, [(slice(0, 1, None),)])
  305. def test_find_objects04():
  306. data = np.zeros([1], dtype=int)
  307. out = ndimage.find_objects(data)
  308. assert_equal(out, [])
  309. def test_find_objects05():
  310. data = np.ones([5], dtype=int)
  311. out = ndimage.find_objects(data)
  312. assert_equal(out, [(slice(0, 5, None),)])
  313. def test_find_objects06():
  314. data = np.array([1, 0, 2, 2, 0, 3])
  315. out = ndimage.find_objects(data)
  316. assert_equal(out, [(slice(0, 1, None),),
  317. (slice(2, 4, None),),
  318. (slice(5, 6, None),)])
  319. def test_find_objects07():
  320. data = np.array([[0, 0, 0, 0, 0, 0],
  321. [0, 0, 0, 0, 0, 0],
  322. [0, 0, 0, 0, 0, 0],
  323. [0, 0, 0, 0, 0, 0],
  324. [0, 0, 0, 0, 0, 0],
  325. [0, 0, 0, 0, 0, 0]])
  326. out = ndimage.find_objects(data)
  327. assert_equal(out, [])
  328. def test_find_objects08():
  329. data = np.array([[1, 0, 0, 0, 0, 0],
  330. [0, 0, 2, 2, 0, 0],
  331. [0, 0, 2, 2, 2, 0],
  332. [3, 3, 0, 0, 0, 0],
  333. [3, 3, 0, 0, 0, 0],
  334. [0, 0, 0, 4, 4, 0]])
  335. out = ndimage.find_objects(data)
  336. assert_equal(out, [(slice(0, 1, None), slice(0, 1, None)),
  337. (slice(1, 3, None), slice(2, 5, None)),
  338. (slice(3, 5, None), slice(0, 2, None)),
  339. (slice(5, 6, None), slice(3, 5, None))])
  340. def test_find_objects09():
  341. data = np.array([[1, 0, 0, 0, 0, 0],
  342. [0, 0, 2, 2, 0, 0],
  343. [0, 0, 2, 2, 2, 0],
  344. [0, 0, 0, 0, 0, 0],
  345. [0, 0, 0, 0, 0, 0],
  346. [0, 0, 0, 4, 4, 0]])
  347. out = ndimage.find_objects(data)
  348. assert_equal(out, [(slice(0, 1, None), slice(0, 1, None)),
  349. (slice(1, 3, None), slice(2, 5, None)),
  350. None,
  351. (slice(5, 6, None), slice(3, 5, None))])
  352. def test_sum01():
  353. for type in types:
  354. input = np.array([], type)
  355. output = ndimage.sum(input)
  356. assert_equal(output, 0.0)
  357. def test_sum02():
  358. for type in types:
  359. input = np.zeros([0, 4], type)
  360. output = ndimage.sum(input)
  361. assert_equal(output, 0.0)
  362. def test_sum03():
  363. for type in types:
  364. input = np.ones([], type)
  365. output = ndimage.sum(input)
  366. assert_almost_equal(output, 1.0)
  367. def test_sum04():
  368. for type in types:
  369. input = np.array([1, 2], type)
  370. output = ndimage.sum(input)
  371. assert_almost_equal(output, 3.0)
  372. def test_sum05():
  373. for type in types:
  374. input = np.array([[1, 2], [3, 4]], type)
  375. output = ndimage.sum(input)
  376. assert_almost_equal(output, 10.0)
  377. def test_sum06():
  378. labels = np.array([], bool)
  379. for type in types:
  380. input = np.array([], type)
  381. output = ndimage.sum(input, labels=labels)
  382. assert_equal(output, 0.0)
  383. def test_sum07():
  384. labels = np.ones([0, 4], bool)
  385. for type in types:
  386. input = np.zeros([0, 4], type)
  387. output = ndimage.sum(input, labels=labels)
  388. assert_equal(output, 0.0)
  389. def test_sum08():
  390. labels = np.array([1, 0], bool)
  391. for type in types:
  392. input = np.array([1, 2], type)
  393. output = ndimage.sum(input, labels=labels)
  394. assert_equal(output, 1.0)
  395. def test_sum09():
  396. labels = np.array([1, 0], bool)
  397. for type in types:
  398. input = np.array([[1, 2], [3, 4]], type)
  399. output = ndimage.sum(input, labels=labels)
  400. assert_almost_equal(output, 4.0)
  401. def test_sum10():
  402. labels = np.array([1, 0], bool)
  403. input = np.array([[1, 2], [3, 4]], bool)
  404. output = ndimage.sum(input, labels=labels)
  405. assert_almost_equal(output, 2.0)
  406. def test_sum11():
  407. labels = np.array([1, 2], np.int8)
  408. for type in types:
  409. input = np.array([[1, 2], [3, 4]], type)
  410. output = ndimage.sum(input, labels=labels,
  411. index=2)
  412. assert_almost_equal(output, 6.0)
  413. def test_sum12():
  414. labels = np.array([[1, 2], [2, 4]], np.int8)
  415. for type in types:
  416. input = np.array([[1, 2], [3, 4]], type)
  417. output = ndimage.sum(input, labels=labels,
  418. index=[4, 8, 2])
  419. assert_array_almost_equal(output, [4.0, 0.0, 5.0])
  420. def test_mean01():
  421. labels = np.array([1, 0], bool)
  422. for type in types:
  423. input = np.array([[1, 2], [3, 4]], type)
  424. output = ndimage.mean(input, labels=labels)
  425. assert_almost_equal(output, 2.0)
  426. def test_mean02():
  427. labels = np.array([1, 0], bool)
  428. input = np.array([[1, 2], [3, 4]], bool)
  429. output = ndimage.mean(input, labels=labels)
  430. assert_almost_equal(output, 1.0)
  431. def test_mean03():
  432. labels = np.array([1, 2])
  433. for type in types:
  434. input = np.array([[1, 2], [3, 4]], type)
  435. output = ndimage.mean(input, labels=labels,
  436. index=2)
  437. assert_almost_equal(output, 3.0)
  438. def test_mean04():
  439. labels = np.array([[1, 2], [2, 4]], np.int8)
  440. olderr = np.seterr(all='ignore')
  441. try:
  442. for type in types:
  443. input = np.array([[1, 2], [3, 4]], type)
  444. output = ndimage.mean(input, labels=labels,
  445. index=[4, 8, 2])
  446. assert_array_almost_equal(output[[0,2]], [4.0, 2.5])
  447. assert_(np.isnan(output[1]))
  448. finally:
  449. np.seterr(**olderr)
  450. def test_minimum01():
  451. labels = np.array([1, 0], bool)
  452. for type in types:
  453. input = np.array([[1, 2], [3, 4]], type)
  454. output = ndimage.minimum(input, labels=labels)
  455. assert_almost_equal(output, 1.0)
  456. def test_minimum02():
  457. labels = np.array([1, 0], bool)
  458. input = np.array([[2, 2], [2, 4]], bool)
  459. output = ndimage.minimum(input, labels=labels)
  460. assert_almost_equal(output, 1.0)
  461. def test_minimum03():
  462. labels = np.array([1, 2])
  463. for type in types:
  464. input = np.array([[1, 2], [3, 4]], type)
  465. output = ndimage.minimum(input, labels=labels,
  466. index=2)
  467. assert_almost_equal(output, 2.0)
  468. def test_minimum04():
  469. labels = np.array([[1, 2], [2, 3]])
  470. for type in types:
  471. input = np.array([[1, 2], [3, 4]], type)
  472. output = ndimage.minimum(input, labels=labels,
  473. index=[2, 3, 8])
  474. assert_array_almost_equal(output, [2.0, 4.0, 0.0])
  475. def test_maximum01():
  476. labels = np.array([1, 0], bool)
  477. for type in types:
  478. input = np.array([[1, 2], [3, 4]], type)
  479. output = ndimage.maximum(input, labels=labels)
  480. assert_almost_equal(output, 3.0)
  481. def test_maximum02():
  482. labels = np.array([1, 0], bool)
  483. input = np.array([[2, 2], [2, 4]], bool)
  484. output = ndimage.maximum(input, labels=labels)
  485. assert_almost_equal(output, 1.0)
  486. def test_maximum03():
  487. labels = np.array([1, 2])
  488. for type in types:
  489. input = np.array([[1, 2], [3, 4]], type)
  490. output = ndimage.maximum(input, labels=labels,
  491. index=2)
  492. assert_almost_equal(output, 4.0)
  493. def test_maximum04():
  494. labels = np.array([[1, 2], [2, 3]])
  495. for type in types:
  496. input = np.array([[1, 2], [3, 4]], type)
  497. output = ndimage.maximum(input, labels=labels,
  498. index=[2, 3, 8])
  499. assert_array_almost_equal(output, [3.0, 4.0, 0.0])
  500. def test_maximum05():
  501. # Regression test for ticket #501 (Trac)
  502. x = np.array([-3,-2,-1])
  503. assert_equal(ndimage.maximum(x),-1)
  504. def test_median01():
  505. a = np.array([[1, 2, 0, 1],
  506. [5, 3, 0, 4],
  507. [0, 0, 0, 7],
  508. [9, 3, 0, 0]])
  509. labels = np.array([[1, 1, 0, 2],
  510. [1, 1, 0, 2],
  511. [0, 0, 0, 2],
  512. [3, 3, 0, 0]])
  513. output = ndimage.median(a, labels=labels, index=[1, 2, 3])
  514. assert_array_almost_equal(output, [2.5, 4.0, 6.0])
  515. def test_median02():
  516. a = np.array([[1, 2, 0, 1],
  517. [5, 3, 0, 4],
  518. [0, 0, 0, 7],
  519. [9, 3, 0, 0]])
  520. output = ndimage.median(a)
  521. assert_almost_equal(output, 1.0)
  522. def test_median03():
  523. a = np.array([[1, 2, 0, 1],
  524. [5, 3, 0, 4],
  525. [0, 0, 0, 7],
  526. [9, 3, 0, 0]])
  527. labels = np.array([[1, 1, 0, 2],
  528. [1, 1, 0, 2],
  529. [0, 0, 0, 2],
  530. [3, 3, 0, 0]])
  531. output = ndimage.median(a, labels=labels)
  532. assert_almost_equal(output, 3.0)
  533. def test_variance01():
  534. olderr = np.seterr(all='ignore')
  535. try:
  536. for type in types:
  537. input = np.array([], type)
  538. with suppress_warnings() as sup:
  539. sup.filter(RuntimeWarning, "Mean of empty slice")
  540. output = ndimage.variance(input)
  541. assert_(np.isnan(output))
  542. finally:
  543. np.seterr(**olderr)
  544. def test_variance02():
  545. for type in types:
  546. input = np.array([1], type)
  547. output = ndimage.variance(input)
  548. assert_almost_equal(output, 0.0)
  549. def test_variance03():
  550. for type in types:
  551. input = np.array([1, 3], type)
  552. output = ndimage.variance(input)
  553. assert_almost_equal(output, 1.0)
  554. def test_variance04():
  555. input = np.array([1, 0], bool)
  556. output = ndimage.variance(input)
  557. assert_almost_equal(output, 0.25)
  558. def test_variance05():
  559. labels = [2, 2, 3]
  560. for type in types:
  561. input = np.array([1, 3, 8], type)
  562. output = ndimage.variance(input, labels, 2)
  563. assert_almost_equal(output, 1.0)
  564. def test_variance06():
  565. labels = [2, 2, 3, 3, 4]
  566. olderr = np.seterr(all='ignore')
  567. try:
  568. for type in types:
  569. input = np.array([1, 3, 8, 10, 8], type)
  570. output = ndimage.variance(input, labels, [2, 3, 4])
  571. assert_array_almost_equal(output, [1.0, 1.0, 0.0])
  572. finally:
  573. np.seterr(**olderr)
  574. def test_standard_deviation01():
  575. olderr = np.seterr(all='ignore')
  576. try:
  577. for type in types:
  578. input = np.array([], type)
  579. with suppress_warnings() as sup:
  580. sup.filter(RuntimeWarning, "Mean of empty slice")
  581. output = ndimage.standard_deviation(input)
  582. assert_(np.isnan(output))
  583. finally:
  584. np.seterr(**olderr)
  585. def test_standard_deviation02():
  586. for type in types:
  587. input = np.array([1], type)
  588. output = ndimage.standard_deviation(input)
  589. assert_almost_equal(output, 0.0)
  590. def test_standard_deviation03():
  591. for type in types:
  592. input = np.array([1, 3], type)
  593. output = ndimage.standard_deviation(input)
  594. assert_almost_equal(output, np.sqrt(1.0))
  595. def test_standard_deviation04():
  596. input = np.array([1, 0], bool)
  597. output = ndimage.standard_deviation(input)
  598. assert_almost_equal(output, 0.5)
  599. def test_standard_deviation05():
  600. labels = [2, 2, 3]
  601. for type in types:
  602. input = np.array([1, 3, 8], type)
  603. output = ndimage.standard_deviation(input, labels, 2)
  604. assert_almost_equal(output, 1.0)
  605. def test_standard_deviation06():
  606. labels = [2, 2, 3, 3, 4]
  607. olderr = np.seterr(all='ignore')
  608. try:
  609. for type in types:
  610. input = np.array([1, 3, 8, 10, 8], type)
  611. output = ndimage.standard_deviation(input, labels, [2, 3, 4])
  612. assert_array_almost_equal(output, [1.0, 1.0, 0.0])
  613. finally:
  614. np.seterr(**olderr)
  615. def test_standard_deviation07():
  616. labels = [1]
  617. olderr = np.seterr(all='ignore')
  618. try:
  619. for type in types:
  620. input = np.array([-0.00619519], type)
  621. output = ndimage.standard_deviation(input, labels, [1])
  622. assert_array_almost_equal(output, [0])
  623. finally:
  624. np.seterr(**olderr)
  625. def test_minimum_position01():
  626. labels = np.array([1, 0], bool)
  627. for type in types:
  628. input = np.array([[1, 2], [3, 4]], type)
  629. output = ndimage.minimum_position(input, labels=labels)
  630. assert_equal(output, (0, 0))
  631. def test_minimum_position02():
  632. for type in types:
  633. input = np.array([[5, 4, 2, 5],
  634. [3, 7, 0, 2],
  635. [1, 5, 1, 1]], type)
  636. output = ndimage.minimum_position(input)
  637. assert_equal(output, (1, 2))
  638. def test_minimum_position03():
  639. input = np.array([[5, 4, 2, 5],
  640. [3, 7, 0, 2],
  641. [1, 5, 1, 1]], bool)
  642. output = ndimage.minimum_position(input)
  643. assert_equal(output, (1, 2))
  644. def test_minimum_position04():
  645. input = np.array([[5, 4, 2, 5],
  646. [3, 7, 1, 2],
  647. [1, 5, 1, 1]], bool)
  648. output = ndimage.minimum_position(input)
  649. assert_equal(output, (0, 0))
  650. def test_minimum_position05():
  651. labels = [1, 2, 0, 4]
  652. for type in types:
  653. input = np.array([[5, 4, 2, 5],
  654. [3, 7, 0, 2],
  655. [1, 5, 2, 3]], type)
  656. output = ndimage.minimum_position(input, labels)
  657. assert_equal(output, (2, 0))
  658. def test_minimum_position06():
  659. labels = [1, 2, 3, 4]
  660. for type in types:
  661. input = np.array([[5, 4, 2, 5],
  662. [3, 7, 0, 2],
  663. [1, 5, 1, 1]], type)
  664. output = ndimage.minimum_position(input, labels, 2)
  665. assert_equal(output, (0, 1))
  666. def test_minimum_position07():
  667. labels = [1, 2, 3, 4]
  668. for type in types:
  669. input = np.array([[5, 4, 2, 5],
  670. [3, 7, 0, 2],
  671. [1, 5, 1, 1]], type)
  672. output = ndimage.minimum_position(input, labels,
  673. [2, 3])
  674. assert_equal(output[0], (0, 1))
  675. assert_equal(output[1], (1, 2))
  676. def test_maximum_position01():
  677. labels = np.array([1, 0], bool)
  678. for type in types:
  679. input = np.array([[1, 2], [3, 4]], type)
  680. output = ndimage.maximum_position(input,
  681. labels=labels)
  682. assert_equal(output, (1, 0))
  683. def test_maximum_position02():
  684. for type in types:
  685. input = np.array([[5, 4, 2, 5],
  686. [3, 7, 8, 2],
  687. [1, 5, 1, 1]], type)
  688. output = ndimage.maximum_position(input)
  689. assert_equal(output, (1, 2))
  690. def test_maximum_position03():
  691. input = np.array([[5, 4, 2, 5],
  692. [3, 7, 8, 2],
  693. [1, 5, 1, 1]], bool)
  694. output = ndimage.maximum_position(input)
  695. assert_equal(output, (0, 0))
  696. def test_maximum_position04():
  697. labels = [1, 2, 0, 4]
  698. for type in types:
  699. input = np.array([[5, 4, 2, 5],
  700. [3, 7, 8, 2],
  701. [1, 5, 1, 1]], type)
  702. output = ndimage.maximum_position(input, labels)
  703. assert_equal(output, (1, 1))
  704. def test_maximum_position05():
  705. labels = [1, 2, 0, 4]
  706. for type in types:
  707. input = np.array([[5, 4, 2, 5],
  708. [3, 7, 8, 2],
  709. [1, 5, 1, 1]], type)
  710. output = ndimage.maximum_position(input, labels, 1)
  711. assert_equal(output, (0, 0))
  712. def test_maximum_position06():
  713. labels = [1, 2, 0, 4]
  714. for type in types:
  715. input = np.array([[5, 4, 2, 5],
  716. [3, 7, 8, 2],
  717. [1, 5, 1, 1]], type)
  718. output = ndimage.maximum_position(input, labels,
  719. [1, 2])
  720. assert_equal(output[0], (0, 0))
  721. assert_equal(output[1], (1, 1))
  722. def test_maximum_position07():
  723. # Test float labels
  724. labels = np.array([1.0, 2.5, 0.0, 4.5])
  725. for type in types:
  726. input = np.array([[5, 4, 2, 5],
  727. [3, 7, 8, 2],
  728. [1, 5, 1, 1]], type)
  729. output = ndimage.maximum_position(input, labels,
  730. [1.0, 4.5])
  731. assert_equal(output[0], (0, 0))
  732. assert_equal(output[1], (0, 3))
  733. def test_extrema01():
  734. labels = np.array([1, 0], bool)
  735. for type in types:
  736. input = np.array([[1, 2], [3, 4]], type)
  737. output1 = ndimage.extrema(input, labels=labels)
  738. output2 = ndimage.minimum(input, labels=labels)
  739. output3 = ndimage.maximum(input, labels=labels)
  740. output4 = ndimage.minimum_position(input,
  741. labels=labels)
  742. output5 = ndimage.maximum_position(input,
  743. labels=labels)
  744. assert_equal(output1, (output2, output3, output4, output5))
  745. def test_extrema02():
  746. labels = np.array([1, 2])
  747. for type in types:
  748. input = np.array([[1, 2], [3, 4]], type)
  749. output1 = ndimage.extrema(input, labels=labels,
  750. index=2)
  751. output2 = ndimage.minimum(input, labels=labels,
  752. index=2)
  753. output3 = ndimage.maximum(input, labels=labels,
  754. index=2)
  755. output4 = ndimage.minimum_position(input,
  756. labels=labels, index=2)
  757. output5 = ndimage.maximum_position(input,
  758. labels=labels, index=2)
  759. assert_equal(output1, (output2, output3, output4, output5))
  760. def test_extrema03():
  761. labels = np.array([[1, 2], [2, 3]])
  762. for type in types:
  763. input = np.array([[1, 2], [3, 4]], type)
  764. output1 = ndimage.extrema(input, labels=labels,
  765. index=[2, 3, 8])
  766. output2 = ndimage.minimum(input, labels=labels,
  767. index=[2, 3, 8])
  768. output3 = ndimage.maximum(input, labels=labels,
  769. index=[2, 3, 8])
  770. output4 = ndimage.minimum_position(input,
  771. labels=labels, index=[2, 3, 8])
  772. output5 = ndimage.maximum_position(input,
  773. labels=labels, index=[2, 3, 8])
  774. assert_array_almost_equal(output1[0], output2)
  775. assert_array_almost_equal(output1[1], output3)
  776. assert_array_almost_equal(output1[2], output4)
  777. assert_array_almost_equal(output1[3], output5)
  778. def test_extrema04():
  779. labels = [1, 2, 0, 4]
  780. for type in types:
  781. input = np.array([[5, 4, 2, 5],
  782. [3, 7, 8, 2],
  783. [1, 5, 1, 1]], type)
  784. output1 = ndimage.extrema(input, labels, [1, 2])
  785. output2 = ndimage.minimum(input, labels, [1, 2])
  786. output3 = ndimage.maximum(input, labels, [1, 2])
  787. output4 = ndimage.minimum_position(input, labels,
  788. [1, 2])
  789. output5 = ndimage.maximum_position(input, labels,
  790. [1, 2])
  791. assert_array_almost_equal(output1[0], output2)
  792. assert_array_almost_equal(output1[1], output3)
  793. assert_array_almost_equal(output1[2], output4)
  794. assert_array_almost_equal(output1[3], output5)
  795. def test_center_of_mass01():
  796. expected = [0.0, 0.0]
  797. for type in types:
  798. input = np.array([[1, 0], [0, 0]], type)
  799. output = ndimage.center_of_mass(input)
  800. assert_array_almost_equal(output, expected)
  801. def test_center_of_mass02():
  802. expected = [1, 0]
  803. for type in types:
  804. input = np.array([[0, 0], [1, 0]], type)
  805. output = ndimage.center_of_mass(input)
  806. assert_array_almost_equal(output, expected)
  807. def test_center_of_mass03():
  808. expected = [0, 1]
  809. for type in types:
  810. input = np.array([[0, 1], [0, 0]], type)
  811. output = ndimage.center_of_mass(input)
  812. assert_array_almost_equal(output, expected)
  813. def test_center_of_mass04():
  814. expected = [1, 1]
  815. for type in types:
  816. input = np.array([[0, 0], [0, 1]], type)
  817. output = ndimage.center_of_mass(input)
  818. assert_array_almost_equal(output, expected)
  819. def test_center_of_mass05():
  820. expected = [0.5, 0.5]
  821. for type in types:
  822. input = np.array([[1, 1], [1, 1]], type)
  823. output = ndimage.center_of_mass(input)
  824. assert_array_almost_equal(output, expected)
  825. def test_center_of_mass06():
  826. expected = [0.5, 0.5]
  827. input = np.array([[1, 2], [3, 1]], bool)
  828. output = ndimage.center_of_mass(input)
  829. assert_array_almost_equal(output, expected)
  830. def test_center_of_mass07():
  831. labels = [1, 0]
  832. expected = [0.5, 0.0]
  833. input = np.array([[1, 2], [3, 1]], bool)
  834. output = ndimage.center_of_mass(input, labels)
  835. assert_array_almost_equal(output, expected)
  836. def test_center_of_mass08():
  837. labels = [1, 2]
  838. expected = [0.5, 1.0]
  839. input = np.array([[5, 2], [3, 1]], bool)
  840. output = ndimage.center_of_mass(input, labels, 2)
  841. assert_array_almost_equal(output, expected)
  842. def test_center_of_mass09():
  843. labels = [1, 2]
  844. expected = [(0.5, 0.0), (0.5, 1.0)]
  845. input = np.array([[1, 2], [1, 1]], bool)
  846. output = ndimage.center_of_mass(input, labels, [1, 2])
  847. assert_array_almost_equal(output, expected)
  848. def test_histogram01():
  849. expected = np.ones(10)
  850. input = np.arange(10)
  851. output = ndimage.histogram(input, 0, 10, 10)
  852. assert_array_almost_equal(output, expected)
  853. def test_histogram02():
  854. labels = [1, 1, 1, 1, 2, 2, 2, 2]
  855. expected = [0, 2, 0, 1, 1]
  856. input = np.array([1, 1, 3, 4, 3, 3, 3, 3])
  857. output = ndimage.histogram(input, 0, 4, 5, labels, 1)
  858. assert_array_almost_equal(output, expected)
  859. def test_histogram03():
  860. labels = [1, 0, 1, 1, 2, 2, 2, 2]
  861. expected1 = [0, 1, 0, 1, 1]
  862. expected2 = [0, 0, 0, 3, 0]
  863. input = np.array([1, 1, 3, 4, 3, 5, 3, 3])
  864. output = ndimage.histogram(input, 0, 4, 5, labels, (1,2))
  865. assert_array_almost_equal(output[0], expected1)
  866. assert_array_almost_equal(output[1], expected2)
  867. def test_stat_funcs_2d():
  868. a = np.array([[5,6,0,0,0], [8,9,0,0,0], [0,0,0,3,5]])
  869. lbl = np.array([[1,1,0,0,0], [1,1,0,0,0], [0,0,0,2,2]])
  870. mean = ndimage.mean(a, labels=lbl, index=[1, 2])
  871. assert_array_equal(mean, [7.0, 4.0])
  872. var = ndimage.variance(a, labels=lbl, index=[1, 2])
  873. assert_array_equal(var, [2.5, 1.0])
  874. std = ndimage.standard_deviation(a, labels=lbl, index=[1, 2])
  875. assert_array_almost_equal(std, np.sqrt([2.5, 1.0]))
  876. med = ndimage.median(a, labels=lbl, index=[1, 2])
  877. assert_array_equal(med, [7.0, 4.0])
  878. min = ndimage.minimum(a, labels=lbl, index=[1, 2])
  879. assert_array_equal(min, [5, 3])
  880. max = ndimage.maximum(a, labels=lbl, index=[1, 2])
  881. assert_array_equal(max, [9, 5])