12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106 |
- from __future__ import division, print_function, absolute_import
- import os.path
- import numpy as np
- from numpy.testing import (assert_, assert_array_almost_equal, assert_equal,
- assert_almost_equal, assert_array_equal)
- from pytest import raises as assert_raises
- from scipy._lib._numpy_compat import suppress_warnings
- import scipy.ndimage as ndimage
- types = [np.int8, np.uint8, np.int16,
- np.uint16, np.int32, np.uint32,
- np.int64, np.uint64,
- np.float32, np.float64]
- np.mod(1., 1) # Silence fmod bug on win-amd64. See #1408 and #1238.
- class Test_measurements_stats(object):
- """ndimage.measurements._stats() is a utility function used by other functions."""
- def test_a(self):
- x = [0,1,2,6]
- labels = [0,0,1,1]
- index = [0,1]
- for shp in [(4,), (2,2)]:
- x = np.array(x).reshape(shp)
- labels = np.array(labels).reshape(shp)
- counts, sums = ndimage.measurements._stats(x, labels=labels, index=index)
- assert_array_equal(counts, [2, 2])
- assert_array_equal(sums, [1.0, 8.0])
- def test_b(self):
- # Same data as test_a, but different labels. The label 9 exceeds the
- # length of 'labels', so this test will follow a different code path.
- x = [0,1,2,6]
- labels = [0,0,9,9]
- index = [0,9]
- for shp in [(4,), (2,2)]:
- x = np.array(x).reshape(shp)
- labels = np.array(labels).reshape(shp)
- counts, sums = ndimage.measurements._stats(x, labels=labels, index=index)
- assert_array_equal(counts, [2, 2])
- assert_array_equal(sums, [1.0, 8.0])
- def test_a_centered(self):
- x = [0,1,2,6]
- labels = [0,0,1,1]
- index = [0,1]
- for shp in [(4,), (2,2)]:
- x = np.array(x).reshape(shp)
- labels = np.array(labels).reshape(shp)
- counts, sums, centers = ndimage.measurements._stats(x, labels=labels,
- index=index, centered=True)
- assert_array_equal(counts, [2, 2])
- assert_array_equal(sums, [1.0, 8.0])
- assert_array_equal(centers, [0.5, 8.0])
- def test_b_centered(self):
- x = [0,1,2,6]
- labels = [0,0,9,9]
- index = [0,9]
- for shp in [(4,), (2,2)]:
- x = np.array(x).reshape(shp)
- labels = np.array(labels).reshape(shp)
- counts, sums, centers = ndimage.measurements._stats(x, labels=labels,
- index=index, centered=True)
- assert_array_equal(counts, [2, 2])
- assert_array_equal(sums, [1.0, 8.0])
- assert_array_equal(centers, [0.5, 8.0])
- def test_nonint_labels(self):
- x = [0,1,2,6]
- labels = [0.0, 0.0, 9.0, 9.0]
- index = [0.0, 9.0]
- for shp in [(4,), (2,2)]:
- x = np.array(x).reshape(shp)
- labels = np.array(labels).reshape(shp)
- counts, sums, centers = ndimage.measurements._stats(x, labels=labels,
- index=index, centered=True)
- assert_array_equal(counts, [2, 2])
- assert_array_equal(sums, [1.0, 8.0])
- assert_array_equal(centers, [0.5, 8.0])
- class Test_measurements_select(object):
- """ndimage.measurements._select() is a utility function used by other functions."""
- def test_basic(self):
- x = [0,1,6,2]
- cases = [
- ([0,0,1,1], [0,1]), # "Small" integer labels
- ([0,0,9,9], [0,9]), # A label larger than len(labels)
- ([0.0,0.0,7.0,7.0], [0.0, 7.0]), # Non-integer labels
- ]
- for labels, index in cases:
- result = ndimage.measurements._select(x, labels=labels, index=index)
- assert_(len(result) == 0)
- result = ndimage.measurements._select(x, labels=labels, index=index, find_max=True)
- assert_(len(result) == 1)
- assert_array_equal(result[0], [1, 6])
- result = ndimage.measurements._select(x, labels=labels, index=index, find_min=True)
- assert_(len(result) == 1)
- assert_array_equal(result[0], [0, 2])
- result = ndimage.measurements._select(x, labels=labels, index=index,
- find_min=True, find_min_positions=True)
- assert_(len(result) == 2)
- assert_array_equal(result[0], [0, 2])
- assert_array_equal(result[1], [0, 3])
- assert_equal(result[1].dtype.kind, 'i')
- result = ndimage.measurements._select(x, labels=labels, index=index,
- find_max=True, find_max_positions=True)
- assert_(len(result) == 2)
- assert_array_equal(result[0], [1, 6])
- assert_array_equal(result[1], [1, 2])
- assert_equal(result[1].dtype.kind, 'i')
- def test_label01():
- data = np.ones([])
- out, n = ndimage.label(data)
- assert_array_almost_equal(out, 1)
- assert_equal(n, 1)
- def test_label02():
- data = np.zeros([])
- out, n = ndimage.label(data)
- assert_array_almost_equal(out, 0)
- assert_equal(n, 0)
- def test_label03():
- data = np.ones([1])
- out, n = ndimage.label(data)
- assert_array_almost_equal(out, [1])
- assert_equal(n, 1)
- def test_label04():
- data = np.zeros([1])
- out, n = ndimage.label(data)
- assert_array_almost_equal(out, [0])
- assert_equal(n, 0)
- def test_label05():
- data = np.ones([5])
- out, n = ndimage.label(data)
- assert_array_almost_equal(out, [1, 1, 1, 1, 1])
- assert_equal(n, 1)
- def test_label06():
- data = np.array([1, 0, 1, 1, 0, 1])
- out, n = ndimage.label(data)
- assert_array_almost_equal(out, [1, 0, 2, 2, 0, 3])
- assert_equal(n, 3)
- def test_label07():
- data = np.array([[0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0]])
- out, n = ndimage.label(data)
- assert_array_almost_equal(out, [[0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0]])
- assert_equal(n, 0)
- def test_label08():
- data = np.array([[1, 0, 0, 0, 0, 0],
- [0, 0, 1, 1, 0, 0],
- [0, 0, 1, 1, 1, 0],
- [1, 1, 0, 0, 0, 0],
- [1, 1, 0, 0, 0, 0],
- [0, 0, 0, 1, 1, 0]])
- out, n = ndimage.label(data)
- assert_array_almost_equal(out, [[1, 0, 0, 0, 0, 0],
- [0, 0, 2, 2, 0, 0],
- [0, 0, 2, 2, 2, 0],
- [3, 3, 0, 0, 0, 0],
- [3, 3, 0, 0, 0, 0],
- [0, 0, 0, 4, 4, 0]])
- assert_equal(n, 4)
- def test_label09():
- data = np.array([[1, 0, 0, 0, 0, 0],
- [0, 0, 1, 1, 0, 0],
- [0, 0, 1, 1, 1, 0],
- [1, 1, 0, 0, 0, 0],
- [1, 1, 0, 0, 0, 0],
- [0, 0, 0, 1, 1, 0]])
- struct = ndimage.generate_binary_structure(2, 2)
- out, n = ndimage.label(data, struct)
- assert_array_almost_equal(out, [[1, 0, 0, 0, 0, 0],
- [0, 0, 2, 2, 0, 0],
- [0, 0, 2, 2, 2, 0],
- [2, 2, 0, 0, 0, 0],
- [2, 2, 0, 0, 0, 0],
- [0, 0, 0, 3, 3, 0]])
- assert_equal(n, 3)
- def test_label10():
- data = np.array([[0, 0, 0, 0, 0, 0],
- [0, 1, 1, 0, 1, 0],
- [0, 1, 1, 1, 1, 0],
- [0, 0, 0, 0, 0, 0]])
- struct = ndimage.generate_binary_structure(2, 2)
- out, n = ndimage.label(data, struct)
- assert_array_almost_equal(out, [[0, 0, 0, 0, 0, 0],
- [0, 1, 1, 0, 1, 0],
- [0, 1, 1, 1, 1, 0],
- [0, 0, 0, 0, 0, 0]])
- assert_equal(n, 1)
- def test_label11():
- for type in types:
- data = np.array([[1, 0, 0, 0, 0, 0],
- [0, 0, 1, 1, 0, 0],
- [0, 0, 1, 1, 1, 0],
- [1, 1, 0, 0, 0, 0],
- [1, 1, 0, 0, 0, 0],
- [0, 0, 0, 1, 1, 0]], type)
- out, n = ndimage.label(data)
- expected = [[1, 0, 0, 0, 0, 0],
- [0, 0, 2, 2, 0, 0],
- [0, 0, 2, 2, 2, 0],
- [3, 3, 0, 0, 0, 0],
- [3, 3, 0, 0, 0, 0],
- [0, 0, 0, 4, 4, 0]]
- assert_array_almost_equal(out, expected)
- assert_equal(n, 4)
- def test_label11_inplace():
- for type in types:
- data = np.array([[1, 0, 0, 0, 0, 0],
- [0, 0, 1, 1, 0, 0],
- [0, 0, 1, 1, 1, 0],
- [1, 1, 0, 0, 0, 0],
- [1, 1, 0, 0, 0, 0],
- [0, 0, 0, 1, 1, 0]], type)
- n = ndimage.label(data, output=data)
- expected = [[1, 0, 0, 0, 0, 0],
- [0, 0, 2, 2, 0, 0],
- [0, 0, 2, 2, 2, 0],
- [3, 3, 0, 0, 0, 0],
- [3, 3, 0, 0, 0, 0],
- [0, 0, 0, 4, 4, 0]]
- assert_array_almost_equal(data, expected)
- assert_equal(n, 4)
- def test_label12():
- for type in types:
- data = np.array([[0, 0, 0, 0, 1, 1],
- [0, 0, 0, 0, 0, 1],
- [0, 0, 1, 0, 1, 1],
- [0, 0, 1, 1, 1, 1],
- [0, 0, 0, 1, 1, 0]], type)
- out, n = ndimage.label(data)
- expected = [[0, 0, 0, 0, 1, 1],
- [0, 0, 0, 0, 0, 1],
- [0, 0, 1, 0, 1, 1],
- [0, 0, 1, 1, 1, 1],
- [0, 0, 0, 1, 1, 0]]
- assert_array_almost_equal(out, expected)
- assert_equal(n, 1)
- def test_label13():
- for type in types:
- data = np.array([[1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1],
- [1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1],
- [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
- [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]],
- type)
- out, n = ndimage.label(data)
- expected = [[1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1],
- [1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1],
- [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
- [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
- assert_array_almost_equal(out, expected)
- assert_equal(n, 1)
- def test_label_output_typed():
- data = np.ones([5])
- for t in types:
- output = np.zeros([5], dtype=t)
- n = ndimage.label(data, output=output)
- assert_array_almost_equal(output, 1)
- assert_equal(n, 1)
- def test_label_output_dtype():
- data = np.ones([5])
- for t in types:
- output, n = ndimage.label(data, output=t)
- assert_array_almost_equal(output, 1)
- assert output.dtype == t
- def test_label_output_wrong_size():
- data = np.ones([5])
- for t in types:
- output = np.zeros([10], t)
- assert_raises((RuntimeError, ValueError), ndimage.label, data, output=output)
- def test_label_structuring_elements():
- data = np.loadtxt(os.path.join(os.path.dirname(__file__), "data", "label_inputs.txt"))
- strels = np.loadtxt(os.path.join(os.path.dirname(__file__), "data", "label_strels.txt"))
- results = np.loadtxt(os.path.join(os.path.dirname(__file__), "data", "label_results.txt"))
- data = data.reshape((-1, 7, 7))
- strels = strels.reshape((-1, 3, 3))
- results = results.reshape((-1, 7, 7))
- r = 0
- for i in range(data.shape[0]):
- d = data[i, :, :]
- for j in range(strels.shape[0]):
- s = strels[j, :, :]
- assert_equal(ndimage.label(d, s)[0], results[r, :, :])
- r += 1
- def test_label_default_dtype():
- test_array = np.random.rand(10, 10)
- label, no_features = ndimage.label(test_array > 0.5)
- assert_(label.dtype in (np.int32, np.int64))
- # Shouldn't raise an exception
- ndimage.find_objects(label)
- def test_find_objects01():
- data = np.ones([], dtype=int)
- out = ndimage.find_objects(data)
- assert_(out == [()])
- def test_find_objects02():
- data = np.zeros([], dtype=int)
- out = ndimage.find_objects(data)
- assert_(out == [])
- def test_find_objects03():
- data = np.ones([1], dtype=int)
- out = ndimage.find_objects(data)
- assert_equal(out, [(slice(0, 1, None),)])
- def test_find_objects04():
- data = np.zeros([1], dtype=int)
- out = ndimage.find_objects(data)
- assert_equal(out, [])
- def test_find_objects05():
- data = np.ones([5], dtype=int)
- out = ndimage.find_objects(data)
- assert_equal(out, [(slice(0, 5, None),)])
- def test_find_objects06():
- data = np.array([1, 0, 2, 2, 0, 3])
- out = ndimage.find_objects(data)
- assert_equal(out, [(slice(0, 1, None),),
- (slice(2, 4, None),),
- (slice(5, 6, None),)])
- def test_find_objects07():
- data = np.array([[0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0]])
- out = ndimage.find_objects(data)
- assert_equal(out, [])
- def test_find_objects08():
- data = np.array([[1, 0, 0, 0, 0, 0],
- [0, 0, 2, 2, 0, 0],
- [0, 0, 2, 2, 2, 0],
- [3, 3, 0, 0, 0, 0],
- [3, 3, 0, 0, 0, 0],
- [0, 0, 0, 4, 4, 0]])
- out = ndimage.find_objects(data)
- assert_equal(out, [(slice(0, 1, None), slice(0, 1, None)),
- (slice(1, 3, None), slice(2, 5, None)),
- (slice(3, 5, None), slice(0, 2, None)),
- (slice(5, 6, None), slice(3, 5, None))])
- def test_find_objects09():
- data = np.array([[1, 0, 0, 0, 0, 0],
- [0, 0, 2, 2, 0, 0],
- [0, 0, 2, 2, 2, 0],
- [0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0],
- [0, 0, 0, 4, 4, 0]])
- out = ndimage.find_objects(data)
- assert_equal(out, [(slice(0, 1, None), slice(0, 1, None)),
- (slice(1, 3, None), slice(2, 5, None)),
- None,
- (slice(5, 6, None), slice(3, 5, None))])
- def test_sum01():
- for type in types:
- input = np.array([], type)
- output = ndimage.sum(input)
- assert_equal(output, 0.0)
- def test_sum02():
- for type in types:
- input = np.zeros([0, 4], type)
- output = ndimage.sum(input)
- assert_equal(output, 0.0)
- def test_sum03():
- for type in types:
- input = np.ones([], type)
- output = ndimage.sum(input)
- assert_almost_equal(output, 1.0)
- def test_sum04():
- for type in types:
- input = np.array([1, 2], type)
- output = ndimage.sum(input)
- assert_almost_equal(output, 3.0)
- def test_sum05():
- for type in types:
- input = np.array([[1, 2], [3, 4]], type)
- output = ndimage.sum(input)
- assert_almost_equal(output, 10.0)
- def test_sum06():
- labels = np.array([], bool)
- for type in types:
- input = np.array([], type)
- output = ndimage.sum(input, labels=labels)
- assert_equal(output, 0.0)
- def test_sum07():
- labels = np.ones([0, 4], bool)
- for type in types:
- input = np.zeros([0, 4], type)
- output = ndimage.sum(input, labels=labels)
- assert_equal(output, 0.0)
- def test_sum08():
- labels = np.array([1, 0], bool)
- for type in types:
- input = np.array([1, 2], type)
- output = ndimage.sum(input, labels=labels)
- assert_equal(output, 1.0)
- def test_sum09():
- labels = np.array([1, 0], bool)
- for type in types:
- input = np.array([[1, 2], [3, 4]], type)
- output = ndimage.sum(input, labels=labels)
- assert_almost_equal(output, 4.0)
- def test_sum10():
- labels = np.array([1, 0], bool)
- input = np.array([[1, 2], [3, 4]], bool)
- output = ndimage.sum(input, labels=labels)
- assert_almost_equal(output, 2.0)
- def test_sum11():
- labels = np.array([1, 2], np.int8)
- for type in types:
- input = np.array([[1, 2], [3, 4]], type)
- output = ndimage.sum(input, labels=labels,
- index=2)
- assert_almost_equal(output, 6.0)
- def test_sum12():
- labels = np.array([[1, 2], [2, 4]], np.int8)
- for type in types:
- input = np.array([[1, 2], [3, 4]], type)
- output = ndimage.sum(input, labels=labels,
- index=[4, 8, 2])
- assert_array_almost_equal(output, [4.0, 0.0, 5.0])
- def test_mean01():
- labels = np.array([1, 0], bool)
- for type in types:
- input = np.array([[1, 2], [3, 4]], type)
- output = ndimage.mean(input, labels=labels)
- assert_almost_equal(output, 2.0)
- def test_mean02():
- labels = np.array([1, 0], bool)
- input = np.array([[1, 2], [3, 4]], bool)
- output = ndimage.mean(input, labels=labels)
- assert_almost_equal(output, 1.0)
- def test_mean03():
- labels = np.array([1, 2])
- for type in types:
- input = np.array([[1, 2], [3, 4]], type)
- output = ndimage.mean(input, labels=labels,
- index=2)
- assert_almost_equal(output, 3.0)
- def test_mean04():
- labels = np.array([[1, 2], [2, 4]], np.int8)
- olderr = np.seterr(all='ignore')
- try:
- for type in types:
- input = np.array([[1, 2], [3, 4]], type)
- output = ndimage.mean(input, labels=labels,
- index=[4, 8, 2])
- assert_array_almost_equal(output[[0,2]], [4.0, 2.5])
- assert_(np.isnan(output[1]))
- finally:
- np.seterr(**olderr)
- def test_minimum01():
- labels = np.array([1, 0], bool)
- for type in types:
- input = np.array([[1, 2], [3, 4]], type)
- output = ndimage.minimum(input, labels=labels)
- assert_almost_equal(output, 1.0)
- def test_minimum02():
- labels = np.array([1, 0], bool)
- input = np.array([[2, 2], [2, 4]], bool)
- output = ndimage.minimum(input, labels=labels)
- assert_almost_equal(output, 1.0)
- def test_minimum03():
- labels = np.array([1, 2])
- for type in types:
- input = np.array([[1, 2], [3, 4]], type)
- output = ndimage.minimum(input, labels=labels,
- index=2)
- assert_almost_equal(output, 2.0)
- def test_minimum04():
- labels = np.array([[1, 2], [2, 3]])
- for type in types:
- input = np.array([[1, 2], [3, 4]], type)
- output = ndimage.minimum(input, labels=labels,
- index=[2, 3, 8])
- assert_array_almost_equal(output, [2.0, 4.0, 0.0])
- def test_maximum01():
- labels = np.array([1, 0], bool)
- for type in types:
- input = np.array([[1, 2], [3, 4]], type)
- output = ndimage.maximum(input, labels=labels)
- assert_almost_equal(output, 3.0)
- def test_maximum02():
- labels = np.array([1, 0], bool)
- input = np.array([[2, 2], [2, 4]], bool)
- output = ndimage.maximum(input, labels=labels)
- assert_almost_equal(output, 1.0)
- def test_maximum03():
- labels = np.array([1, 2])
- for type in types:
- input = np.array([[1, 2], [3, 4]], type)
- output = ndimage.maximum(input, labels=labels,
- index=2)
- assert_almost_equal(output, 4.0)
- def test_maximum04():
- labels = np.array([[1, 2], [2, 3]])
- for type in types:
- input = np.array([[1, 2], [3, 4]], type)
- output = ndimage.maximum(input, labels=labels,
- index=[2, 3, 8])
- assert_array_almost_equal(output, [3.0, 4.0, 0.0])
- def test_maximum05():
- # Regression test for ticket #501 (Trac)
- x = np.array([-3,-2,-1])
- assert_equal(ndimage.maximum(x),-1)
- def test_median01():
- a = np.array([[1, 2, 0, 1],
- [5, 3, 0, 4],
- [0, 0, 0, 7],
- [9, 3, 0, 0]])
- labels = np.array([[1, 1, 0, 2],
- [1, 1, 0, 2],
- [0, 0, 0, 2],
- [3, 3, 0, 0]])
- output = ndimage.median(a, labels=labels, index=[1, 2, 3])
- assert_array_almost_equal(output, [2.5, 4.0, 6.0])
- def test_median02():
- a = np.array([[1, 2, 0, 1],
- [5, 3, 0, 4],
- [0, 0, 0, 7],
- [9, 3, 0, 0]])
- output = ndimage.median(a)
- assert_almost_equal(output, 1.0)
- def test_median03():
- a = np.array([[1, 2, 0, 1],
- [5, 3, 0, 4],
- [0, 0, 0, 7],
- [9, 3, 0, 0]])
- labels = np.array([[1, 1, 0, 2],
- [1, 1, 0, 2],
- [0, 0, 0, 2],
- [3, 3, 0, 0]])
- output = ndimage.median(a, labels=labels)
- assert_almost_equal(output, 3.0)
- def test_variance01():
- olderr = np.seterr(all='ignore')
- try:
- for type in types:
- input = np.array([], type)
- with suppress_warnings() as sup:
- sup.filter(RuntimeWarning, "Mean of empty slice")
- output = ndimage.variance(input)
- assert_(np.isnan(output))
- finally:
- np.seterr(**olderr)
- def test_variance02():
- for type in types:
- input = np.array([1], type)
- output = ndimage.variance(input)
- assert_almost_equal(output, 0.0)
- def test_variance03():
- for type in types:
- input = np.array([1, 3], type)
- output = ndimage.variance(input)
- assert_almost_equal(output, 1.0)
- def test_variance04():
- input = np.array([1, 0], bool)
- output = ndimage.variance(input)
- assert_almost_equal(output, 0.25)
- def test_variance05():
- labels = [2, 2, 3]
- for type in types:
- input = np.array([1, 3, 8], type)
- output = ndimage.variance(input, labels, 2)
- assert_almost_equal(output, 1.0)
- def test_variance06():
- labels = [2, 2, 3, 3, 4]
- olderr = np.seterr(all='ignore')
- try:
- for type in types:
- input = np.array([1, 3, 8, 10, 8], type)
- output = ndimage.variance(input, labels, [2, 3, 4])
- assert_array_almost_equal(output, [1.0, 1.0, 0.0])
- finally:
- np.seterr(**olderr)
- def test_standard_deviation01():
- olderr = np.seterr(all='ignore')
- try:
- for type in types:
- input = np.array([], type)
- with suppress_warnings() as sup:
- sup.filter(RuntimeWarning, "Mean of empty slice")
- output = ndimage.standard_deviation(input)
- assert_(np.isnan(output))
- finally:
- np.seterr(**olderr)
- def test_standard_deviation02():
- for type in types:
- input = np.array([1], type)
- output = ndimage.standard_deviation(input)
- assert_almost_equal(output, 0.0)
- def test_standard_deviation03():
- for type in types:
- input = np.array([1, 3], type)
- output = ndimage.standard_deviation(input)
- assert_almost_equal(output, np.sqrt(1.0))
- def test_standard_deviation04():
- input = np.array([1, 0], bool)
- output = ndimage.standard_deviation(input)
- assert_almost_equal(output, 0.5)
- def test_standard_deviation05():
- labels = [2, 2, 3]
- for type in types:
- input = np.array([1, 3, 8], type)
- output = ndimage.standard_deviation(input, labels, 2)
- assert_almost_equal(output, 1.0)
- def test_standard_deviation06():
- labels = [2, 2, 3, 3, 4]
- olderr = np.seterr(all='ignore')
- try:
- for type in types:
- input = np.array([1, 3, 8, 10, 8], type)
- output = ndimage.standard_deviation(input, labels, [2, 3, 4])
- assert_array_almost_equal(output, [1.0, 1.0, 0.0])
- finally:
- np.seterr(**olderr)
- def test_standard_deviation07():
- labels = [1]
- olderr = np.seterr(all='ignore')
- try:
- for type in types:
- input = np.array([-0.00619519], type)
- output = ndimage.standard_deviation(input, labels, [1])
- assert_array_almost_equal(output, [0])
- finally:
- np.seterr(**olderr)
- def test_minimum_position01():
- labels = np.array([1, 0], bool)
- for type in types:
- input = np.array([[1, 2], [3, 4]], type)
- output = ndimage.minimum_position(input, labels=labels)
- assert_equal(output, (0, 0))
- def test_minimum_position02():
- for type in types:
- input = np.array([[5, 4, 2, 5],
- [3, 7, 0, 2],
- [1, 5, 1, 1]], type)
- output = ndimage.minimum_position(input)
- assert_equal(output, (1, 2))
- def test_minimum_position03():
- input = np.array([[5, 4, 2, 5],
- [3, 7, 0, 2],
- [1, 5, 1, 1]], bool)
- output = ndimage.minimum_position(input)
- assert_equal(output, (1, 2))
- def test_minimum_position04():
- input = np.array([[5, 4, 2, 5],
- [3, 7, 1, 2],
- [1, 5, 1, 1]], bool)
- output = ndimage.minimum_position(input)
- assert_equal(output, (0, 0))
- def test_minimum_position05():
- labels = [1, 2, 0, 4]
- for type in types:
- input = np.array([[5, 4, 2, 5],
- [3, 7, 0, 2],
- [1, 5, 2, 3]], type)
- output = ndimage.minimum_position(input, labels)
- assert_equal(output, (2, 0))
- def test_minimum_position06():
- labels = [1, 2, 3, 4]
- for type in types:
- input = np.array([[5, 4, 2, 5],
- [3, 7, 0, 2],
- [1, 5, 1, 1]], type)
- output = ndimage.minimum_position(input, labels, 2)
- assert_equal(output, (0, 1))
- def test_minimum_position07():
- labels = [1, 2, 3, 4]
- for type in types:
- input = np.array([[5, 4, 2, 5],
- [3, 7, 0, 2],
- [1, 5, 1, 1]], type)
- output = ndimage.minimum_position(input, labels,
- [2, 3])
- assert_equal(output[0], (0, 1))
- assert_equal(output[1], (1, 2))
- def test_maximum_position01():
- labels = np.array([1, 0], bool)
- for type in types:
- input = np.array([[1, 2], [3, 4]], type)
- output = ndimage.maximum_position(input,
- labels=labels)
- assert_equal(output, (1, 0))
- def test_maximum_position02():
- for type in types:
- input = np.array([[5, 4, 2, 5],
- [3, 7, 8, 2],
- [1, 5, 1, 1]], type)
- output = ndimage.maximum_position(input)
- assert_equal(output, (1, 2))
- def test_maximum_position03():
- input = np.array([[5, 4, 2, 5],
- [3, 7, 8, 2],
- [1, 5, 1, 1]], bool)
- output = ndimage.maximum_position(input)
- assert_equal(output, (0, 0))
- def test_maximum_position04():
- labels = [1, 2, 0, 4]
- for type in types:
- input = np.array([[5, 4, 2, 5],
- [3, 7, 8, 2],
- [1, 5, 1, 1]], type)
- output = ndimage.maximum_position(input, labels)
- assert_equal(output, (1, 1))
- def test_maximum_position05():
- labels = [1, 2, 0, 4]
- for type in types:
- input = np.array([[5, 4, 2, 5],
- [3, 7, 8, 2],
- [1, 5, 1, 1]], type)
- output = ndimage.maximum_position(input, labels, 1)
- assert_equal(output, (0, 0))
- def test_maximum_position06():
- labels = [1, 2, 0, 4]
- for type in types:
- input = np.array([[5, 4, 2, 5],
- [3, 7, 8, 2],
- [1, 5, 1, 1]], type)
- output = ndimage.maximum_position(input, labels,
- [1, 2])
- assert_equal(output[0], (0, 0))
- assert_equal(output[1], (1, 1))
- def test_maximum_position07():
- # Test float labels
- labels = np.array([1.0, 2.5, 0.0, 4.5])
- for type in types:
- input = np.array([[5, 4, 2, 5],
- [3, 7, 8, 2],
- [1, 5, 1, 1]], type)
- output = ndimage.maximum_position(input, labels,
- [1.0, 4.5])
- assert_equal(output[0], (0, 0))
- assert_equal(output[1], (0, 3))
- def test_extrema01():
- labels = np.array([1, 0], bool)
- for type in types:
- input = np.array([[1, 2], [3, 4]], type)
- output1 = ndimage.extrema(input, labels=labels)
- output2 = ndimage.minimum(input, labels=labels)
- output3 = ndimage.maximum(input, labels=labels)
- output4 = ndimage.minimum_position(input,
- labels=labels)
- output5 = ndimage.maximum_position(input,
- labels=labels)
- assert_equal(output1, (output2, output3, output4, output5))
- def test_extrema02():
- labels = np.array([1, 2])
- for type in types:
- input = np.array([[1, 2], [3, 4]], type)
- output1 = ndimage.extrema(input, labels=labels,
- index=2)
- output2 = ndimage.minimum(input, labels=labels,
- index=2)
- output3 = ndimage.maximum(input, labels=labels,
- index=2)
- output4 = ndimage.minimum_position(input,
- labels=labels, index=2)
- output5 = ndimage.maximum_position(input,
- labels=labels, index=2)
- assert_equal(output1, (output2, output3, output4, output5))
- def test_extrema03():
- labels = np.array([[1, 2], [2, 3]])
- for type in types:
- input = np.array([[1, 2], [3, 4]], type)
- output1 = ndimage.extrema(input, labels=labels,
- index=[2, 3, 8])
- output2 = ndimage.minimum(input, labels=labels,
- index=[2, 3, 8])
- output3 = ndimage.maximum(input, labels=labels,
- index=[2, 3, 8])
- output4 = ndimage.minimum_position(input,
- labels=labels, index=[2, 3, 8])
- output5 = ndimage.maximum_position(input,
- labels=labels, index=[2, 3, 8])
- assert_array_almost_equal(output1[0], output2)
- assert_array_almost_equal(output1[1], output3)
- assert_array_almost_equal(output1[2], output4)
- assert_array_almost_equal(output1[3], output5)
- def test_extrema04():
- labels = [1, 2, 0, 4]
- for type in types:
- input = np.array([[5, 4, 2, 5],
- [3, 7, 8, 2],
- [1, 5, 1, 1]], type)
- output1 = ndimage.extrema(input, labels, [1, 2])
- output2 = ndimage.minimum(input, labels, [1, 2])
- output3 = ndimage.maximum(input, labels, [1, 2])
- output4 = ndimage.minimum_position(input, labels,
- [1, 2])
- output5 = ndimage.maximum_position(input, labels,
- [1, 2])
- assert_array_almost_equal(output1[0], output2)
- assert_array_almost_equal(output1[1], output3)
- assert_array_almost_equal(output1[2], output4)
- assert_array_almost_equal(output1[3], output5)
- def test_center_of_mass01():
- expected = [0.0, 0.0]
- for type in types:
- input = np.array([[1, 0], [0, 0]], type)
- output = ndimage.center_of_mass(input)
- assert_array_almost_equal(output, expected)
- def test_center_of_mass02():
- expected = [1, 0]
- for type in types:
- input = np.array([[0, 0], [1, 0]], type)
- output = ndimage.center_of_mass(input)
- assert_array_almost_equal(output, expected)
- def test_center_of_mass03():
- expected = [0, 1]
- for type in types:
- input = np.array([[0, 1], [0, 0]], type)
- output = ndimage.center_of_mass(input)
- assert_array_almost_equal(output, expected)
- def test_center_of_mass04():
- expected = [1, 1]
- for type in types:
- input = np.array([[0, 0], [0, 1]], type)
- output = ndimage.center_of_mass(input)
- assert_array_almost_equal(output, expected)
- def test_center_of_mass05():
- expected = [0.5, 0.5]
- for type in types:
- input = np.array([[1, 1], [1, 1]], type)
- output = ndimage.center_of_mass(input)
- assert_array_almost_equal(output, expected)
- def test_center_of_mass06():
- expected = [0.5, 0.5]
- input = np.array([[1, 2], [3, 1]], bool)
- output = ndimage.center_of_mass(input)
- assert_array_almost_equal(output, expected)
- def test_center_of_mass07():
- labels = [1, 0]
- expected = [0.5, 0.0]
- input = np.array([[1, 2], [3, 1]], bool)
- output = ndimage.center_of_mass(input, labels)
- assert_array_almost_equal(output, expected)
- def test_center_of_mass08():
- labels = [1, 2]
- expected = [0.5, 1.0]
- input = np.array([[5, 2], [3, 1]], bool)
- output = ndimage.center_of_mass(input, labels, 2)
- assert_array_almost_equal(output, expected)
- def test_center_of_mass09():
- labels = [1, 2]
- expected = [(0.5, 0.0), (0.5, 1.0)]
- input = np.array([[1, 2], [1, 1]], bool)
- output = ndimage.center_of_mass(input, labels, [1, 2])
- assert_array_almost_equal(output, expected)
- def test_histogram01():
- expected = np.ones(10)
- input = np.arange(10)
- output = ndimage.histogram(input, 0, 10, 10)
- assert_array_almost_equal(output, expected)
- def test_histogram02():
- labels = [1, 1, 1, 1, 2, 2, 2, 2]
- expected = [0, 2, 0, 1, 1]
- input = np.array([1, 1, 3, 4, 3, 3, 3, 3])
- output = ndimage.histogram(input, 0, 4, 5, labels, 1)
- assert_array_almost_equal(output, expected)
- def test_histogram03():
- labels = [1, 0, 1, 1, 2, 2, 2, 2]
- expected1 = [0, 1, 0, 1, 1]
- expected2 = [0, 0, 0, 3, 0]
- input = np.array([1, 1, 3, 4, 3, 5, 3, 3])
- output = ndimage.histogram(input, 0, 4, 5, labels, (1,2))
- assert_array_almost_equal(output[0], expected1)
- assert_array_almost_equal(output[1], expected2)
- def test_stat_funcs_2d():
- a = np.array([[5,6,0,0,0], [8,9,0,0,0], [0,0,0,3,5]])
- lbl = np.array([[1,1,0,0,0], [1,1,0,0,0], [0,0,0,2,2]])
- mean = ndimage.mean(a, labels=lbl, index=[1, 2])
- assert_array_equal(mean, [7.0, 4.0])
- var = ndimage.variance(a, labels=lbl, index=[1, 2])
- assert_array_equal(var, [2.5, 1.0])
- std = ndimage.standard_deviation(a, labels=lbl, index=[1, 2])
- assert_array_almost_equal(std, np.sqrt([2.5, 1.0]))
- med = ndimage.median(a, labels=lbl, index=[1, 2])
- assert_array_equal(med, [7.0, 4.0])
- min = ndimage.minimum(a, labels=lbl, index=[1, 2])
- assert_array_equal(min, [5, 3])
- max = ndimage.maximum(a, labels=lbl, index=[1, 2])
- assert_array_equal(max, [9, 5])
|