def test_connected_regions(self): labels, regions = lulu.connected_regions(self.img) assert_array_equal(labels, self.img) assert_equal(len(regions), 3) crh.set_value(regions[0], 5) assert_array_equal(crh.todense(regions[0]), [[5, 5, 5, 5, 0], [5, 0, 0, 0, 0], [5, 0, 0, 0, 0], [5, 5, 5, 5, 0], [5, 5, 5, 5, 0]]) assert_array_equal(crh.todense(regions[1]), [[0, 0, 0, 0, 1], [0, 0, 0, 0, 1], [0, 0, 0, 0, 1], [0, 0, 0, 0, 1], [0, 0, 0, 0, 1]]) assert_array_equal(crh.todense(regions[2]), [[0, 0, 0, 0, 0], [0, 2, 2, 2, 0], [0, 2, 2, 2, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]])
def test_merge_diagonal(self): a = ConnectedRegion(shape=(2, 4), value=1, rowptr=[0, 2, 4], colptr=[0, 1, 1, 2]) b = ConnectedRegion(shape=(2, 4), value=1, rowptr=[0, 2, 4], colptr=[2, 3, 3, 4]) crh.merge(a, b) assert_array_equal(crh.todense(a), [[1, 0, 1, 0], [0, 1, 0, 1]]) assert_equal(crh.nnz(a), 4) a = ConnectedRegion(shape=(2, 2), value=1, rowptr=[0, 2, 4], colptr=[0, 1, 1, 2]) b = ConnectedRegion(shape=(2, 2), value=1, rowptr=[0, 2, 4], colptr=[1, 2, 0, 1]) crh.merge(a, b) assert_array_equal(crh.todense(a), [[1, 1], [1, 1]]) assert_equal(crh.nnz(a), 4)
def test_pickle(self): import pickle from StringIO import StringIO s = StringIO() pickle.dump(self.c, s) s.seek(0) new_c = pickle.load(s) assert np.all(crh.todense(new_c) == crh.todense(self.c))
def test_pickle(self): import pickle try: from StringIO import StringIO except: from io import BytesIO as StringIO s = StringIO() pickle.dump(self.c, s) s.seek(0) new_c = pickle.load(s) assert np.all(crh.todense(new_c) == crh.todense(self.c))
def test_maximal(self): c = ConnectedRegion(shape=(3, 3), value=1, rowptr=[0, 2], colptr=[0, 1]) img = crh.todense(c) assert_equal(crh.boundary_maximum(c, img), 0) crh.set_value(c, -1) assert_equal(crh.boundary_maximum(c, img), 0)
def test_reshape(self): d = crh.copy(self.c) crh.reshape(d, (4, 5)) assert_array_equal(crh.todense(d), self.dense[:4, :]) crh.reshape(d, (5, 5)) crh.reshape(d) assert_array_equal(crh.get_shape(d), (4, 5))
def test_start_row(self): c = ConnectedRegion(shape=(2,2), value=1, start_row=0, rowptr=[0,2], colptr=[0,1]) assert_array_equal(crh.todense(c), [[1, 0], [0, 0]]) crh.set_start_row(c, 0) crh.set_start_row(c, 1) assert_raises(ValueError, crh.set_start_row, c, 2)
def test_start_row(self): c = ConnectedRegion(shape=(2, 2), value=1, start_row=0, rowptr=[0, 2], colptr=[0, 1]) assert_array_equal(crh.todense(c), [[1, 0], [0, 0]]) crh.set_start_row(c, 0) crh.set_start_row(c, 1) assert_raises(ValueError, crh.set_start_row, c, 2)
def test_merge(self): a = ConnectedRegion(shape=(3, 3), value=1, rowptr=[0, 2, 4, 6], colptr=[0, 3, 0, 1, 0, 3]) b = ConnectedRegion(shape=(3, 3), value=1, start_row=1, rowptr=[0,2], colptr=[1,3]) crh.merge(a, b) assert_array_equal(crh.todense(a), np.ones((3, 3))) a = ConnectedRegion(shape=(1, 2), value=2, rowptr=[0, 2], colptr=[1, 2]) b = ConnectedRegion(shape=(1, 3), value=1, rowptr=[0, 2], colptr=[2, 3]) crh.merge(a, b) assert_array_equal(crh.todense(a), [[0, 2, 2]])
def test_outside_boundary_beyond_border(self): c = ConnectedRegion(shape=(2, 2), value=1, rowptr=[0, 2, 4], colptr=[0, 1, 1, 2]) assert_array_equal(crh.todense(c), np.eye(2)) y, x = crh.outside_boundary(c) assert_array_equal(iarr.to_list(y), [-1, -1, -1, 0, 0, 0, 1, 1, 1, 2, 2, 2]) assert_array_equal(iarr.to_list(x), [-1, 0, 1, -1, 1, 2, -1, 0, 2, 0, 1, 2])
def test_merge_different_shapes(self): a = ConnectedRegion(shape=(4, 3), value=1, start_row=1, rowptr=[0, 4, 8, 10], colptr=[0, 1, 2, 3, 0, 1, 2, 3, 0, 3]) b = ConnectedRegion(shape=(3, 4), value=1, rowptr=[0, 2, 6, 10], colptr=[1, 4, 1, 2, 3, 4, 1, 2, 3, 4]) crh.merge(a, b) assert_array_equal(crh.todense(a), [[0, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 0]])
def test_internal_build_ops(self): c = ConnectedRegion(shape=(2, 2), rowptr=[0]) assert_equal(crh._current_row(c), 0) crh._append_colptr(c, 0, 1) crh._new_row(c) assert_equal(crh._current_row(c), 1) crh._append_colptr(c, 1, 2) crh._new_row(c) crh.set_value(c, 5) assert_array_equal(crh.todense(c), [[5, 0], [0, 5]])
import sys sys.path.append('..') import numpy as np import matplotlib.pyplot as plt from lulu import ConnectedRegion from lulu import connected_region_handler as crh from lulu import int_array c = ConnectedRegion(shape=(5,5), value=1, start_row=1, rowptr=[0,4,6,10,14], colptr=[2,3,4,5,0,5,0,1,2,5,0,2,3,5]) print(crh.todense(c)) dense = np.zeros((7,7,3)) dense[1:6, 1:6, 0] = crh.todense(c) plt.subplot(1, 2, 1) plt.imshow(dense, interpolation='nearest') plt.title('Connected region') plt.xticks([]) plt.yticks([]) ii, jj = crh.outside_boundary(c) dense_outside = dense.copy() for i, j in zip(int_array.to_list(ii), int_array.to_list(jj)): dense_outside[i + 1, j + 1] = [0, 1, 0]
def test_contains(self): d = crh.todense(self.c) for y, x in np.ndindex(crh.get_shape(self.c)): crh.contains(self.c, y, x) == d[y, x]
sys.path.append('..') import numpy as np import matplotlib.pyplot as plt from lulu import ConnectedRegion from lulu import connected_region_handler as crh from lulu import int_array c = ConnectedRegion(shape=(5, 5), value=1, start_row=1, rowptr=[0, 4, 6, 10, 14], colptr=[2, 3, 4, 5, 0, 5, 0, 1, 2, 5, 0, 2, 3, 5]) print(crh.todense(c)) dense = np.zeros((7, 7, 3)) dense[1:6, 1:6, 0] = crh.todense(c) plt.subplot(1, 2, 1) plt.imshow(dense, interpolation='nearest') plt.title('Connected region') plt.xticks([]) plt.yticks([]) ii, jj = crh.outside_boundary(c) dense_outside = dense.copy() for i, j in zip(int_array.to_list(ii), int_array.to_list(jj)): dense_outside[i + 1, j + 1] = [0, 1, 0]
sys.path.append('..') import numpy as np import matplotlib.pyplot as plt from lulu import ConnectedRegion from lulu import connected_region_handler as crh from lulu import int_array c = ConnectedRegion(shape=(5, 5), value=1, start_row=1, rowptr=[0, 4, 6, 10, 14], colptr=[2, 3, 4, 5, 0, 5, 0, 1, 2, 5, 0, 2, 3, 5]) print crh.todense(c) dense = np.zeros((7, 7, 3)) dense[1:6, 1:6, 0] = crh.todense(c) plt.subplot(1, 2, 1) plt.imshow(dense, interpolation='nearest') plt.title('Connected region') plt.xticks([]) plt.yticks([]) ii, jj = crh.outside_boundary(c) dense_outside = dense.copy() for i, j in zip(int_array.to_list(ii), int_array.to_list(jj)): dense_outside[i + 1, j + 1] = [0, 1, 0]
def test_copy(self): x = [0, 1] c = ConnectedRegion(shape=(1, 1), value=1, rowptr=[0, 2], colptr=x) d = crh.copy(c) assert crh.todense(d) == crh.todense(c)
def test_basic(self): assert_array_equal(crh.todense(self.c), self.dense) assert_array_equal(crh.todense(crh.copy(self.c)), self.dense)
import sys sys.path.append('..') import numpy as np import matplotlib.pyplot as plt from lulu import ConnectedRegion from lulu import connected_region_handler as crh from lulu import int_array c = ConnectedRegion(shape=(5,5), value=1, start_row=1, rowptr=[0,4,6,10,14], colptr=[2,3,4,5,0,5,0,1,2,5,0,2,3,5]) print crh.todense(c) dense = np.zeros((7,7,3)) dense[1:6, 1:6, 0] = crh.todense(c) plt.subplot(1, 2, 1) plt.imshow(dense, interpolation='nearest') plt.title('Connected region') plt.xticks([]) plt.yticks([]) ii, jj = crh.outside_boundary(c) dense_outside = dense.copy() for i, j in zip(int_array.to_list(ii), int_array.to_list(jj)): dense_outside[i + 1, j + 1] = [0, 1, 0]
def test_copy(self): x = [0, 1] c = ConnectedRegion(shape=(1,1), value=1, rowptr=[0, 2], colptr=x) d = crh.copy(c) assert crh.todense(d) == crh.todense(c)