def testLength(self): # setup bound_data = numpy.array([[2, 2, 2, 2, 2, 2, 2, 2, 2, 2], [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], [4, 4, 4, 4, 4, 4, 4, 4, 4, 4]]) bound = Segment(bound_data) seg_data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 3, 3, 0, 0, 5, 5, 0], [0, 1, 3, 0, 0, 0, 0, 5, 0, 0], [1, 1, 0, 3, 0, 5, 5, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]) seg = Segment(seg_data) con = Contact() con.findContacts(segment=seg, boundary=bound) # b2b, straight mode mor = Morphology() mor.getLength(segments=seg, boundaries=bound, contacts=con, distance='b2b', line='straight', position=True) assert_almost_equal(mor.length[seg.ids], [4, 4, numpy.sqrt(17)]) assert_equal(mor.end1[seg.ids], numpy.array([[0, 1], [0, 3], [0, 7]])) assert_equal(mor.end2[seg.ids], numpy.array([[4, 1], [4, 3], [4, 6]])) # check lengths in straight mode mor = Morphology() mor.getLength(segments=seg, boundaries=bound, contacts=con, distance='c2c', line='straight', position=True) assert_almost_equal(mor.length[seg.ids], [2, 2, numpy.sqrt(5)]) assert_equal(mor.end1[seg.ids], numpy.array([[1, 1], [1, 3], [1, 7]])) assert_equal(mor.end2[seg.ids], numpy.array([[3, 1], [3, 3], [3, 6]])) # b2c, straight mode mor = Morphology() mor.getLength(segments=seg, boundaries=bound, contacts=con, distance='b2c', line='straight', position=True) assert_almost_equal(mor.length[seg.ids], [3, 3, numpy.sqrt(10)]) assert_equal(mor.end1[seg.ids], numpy.array([[0, 1], [0, 3], [0, 7]])) assert_equal(mor.end2[seg.ids], numpy.array([[3, 1], [3, 3], [3, 6]])) # c2b, straight mode mor = Morphology() mor.getLength(segments=seg, boundaries=bound, contacts=con, distance='c2b', line='straight', position=True) assert_almost_equal(mor.length[seg.ids], [3, 3, numpy.sqrt(10)]) assert_equal(mor.end1[seg.ids], numpy.array([[1, 1], [1, 3], [1, 7]])) assert_equal(mor.end2[seg.ids], numpy.array([[4, 1], [4, 3], [4, 6]])) # b2b in mid mode bound.data[1, 0] = 1 mor = Morphology() mor.getLength(segments=seg, boundaries=bound, contacts=con, distance='b2b', line='mid', position=True) assert_almost_equal(mor.length[seg.ids], [4, 2 * numpy.sqrt(5), 2 + numpy.sqrt(5)]) assert_equal(mor.end1[seg.ids], numpy.array([[0, 1], [0, 3], [0, 7]])) assert_equal(mor.end2[seg.ids], numpy.array([[4, 1], [4, 3], [4, 6]])) # c2c in mid mode bound.data[1, 0] = 1 mor = Morphology() mor.getLength(segments=seg, boundaries=bound, contacts=con, distance='c2c', line='mid', position=True) assert_almost_equal(mor.length[seg.ids], [2, 2 * numpy.sqrt(2), 1 + numpy.sqrt(2)]) assert_equal(mor.end1[seg.ids], numpy.array([[1, 1], [1, 3], [1, 7]])) assert_equal(mor.end2[seg.ids], numpy.array([[3, 1], [3, 3], [3, 6]])) # c2b in mid mode bound.data[1, 0] = 1 mor = Morphology() mor.getLength(segments=seg, boundaries=bound, contacts=con, distance='c2b', line='mid', position=True) assert_almost_equal( mor.length[seg.ids], [3, numpy.sqrt(5) + numpy.sqrt(2), 1 + numpy.sqrt(5)]) assert_equal(mor.end1[seg.ids], numpy.array([[1, 1], [1, 3], [1, 7]])) assert_equal(mor.end2[seg.ids], numpy.array([[4, 1], [4, 3], [4, 6]])) # b2c in mid mode bound.data[1, 0] = 1 mor = Morphology() mor.getLength(segments=seg, boundaries=bound, contacts=con, distance='b2c', line='mid', position=True) assert_almost_equal( mor.length[seg.ids], [3, numpy.sqrt(5) + numpy.sqrt(2), 2 + numpy.sqrt(2)]) assert_equal(mor.end1[seg.ids], numpy.array([[0, 1], [0, 3], [0, 7]])) assert_equal(mor.end2[seg.ids], numpy.array([[3, 1], [3, 3], [3, 6]])) # check length when segment far from the smallest inter-boundary # distance wedge_bound_data = numpy.array([[3, 3, 3, 3, 3, 0], [3, 3, 3, 3, 0, 0], [3, 3, 3, 0, 0, 0], [3, 3, 0, 0, 0, 0], [3, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [2, 2, 2, 2, 2, 2]]) wedge_bound = Segment(data=wedge_bound_data) segment_data = numpy.array([[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 5, 5, 5], [0, 0, 0, 0, 0, 5], [0, 0, 0, 0, 0, 5], [0, 0, 0, 5, 5, 5], [0, 0, 0, 0, 0, 0]]) seg = Segment(data=segment_data) con = Contact() con.findContacts(segment=seg, boundary=wedge_bound) mor = Morphology() mor.getLength(segments=seg, boundaries=wedge_bound, contacts=con, distance='c2c', line='straight', position=True) assert_almost_equal(mor.length[seg.ids], [3]) mor = Morphology() mor.getLength(segments=seg, boundaries=wedge_bound, contacts=con, distance='c2c', line='mid', position=True) assert_almost_equal(mor.length[seg.ids], [2 + numpy.sqrt(5.)]) mor = Morphology() mor.getLength(segments=seg, boundaries=wedge_bound, contacts=con, distance='c2c', line='mid-seg', position=True) assert_almost_equal(mor.length[seg.ids], [2 + numpy.sqrt(5.)]) # boundary not in the smallest segment inset bound_data = numpy.array([[1, 1, 1], [0, 0, 0], [2, 2, 2]]) bound = Segment(data=bound_data) seg_data = numpy.array([[0, 0, 0], [0, 5, 0], [0, 0, 0]]) seg = Segment(data=seg_data) con = Contact() con.findContacts(segment=seg, boundary=bound) mor = Morphology() mor.getLength(segments=seg, boundaries=bound, contacts=con, distance='c2c', line='straight', position=False) assert_almost_equal(mor.length[seg.ids], [0]) # boundary not in the given segment inset bound_data = numpy.array([[1, 1, 1], [0, 0, 0], [2, 2, 2]]) bound = Segment(data=bound_data) bound.inset = [slice(1, 4), slice(2, 5)] seg_data = numpy.array([[0, 0, 0], [0, 5, 0], [0, 0, 0]]) seg = Segment(data=seg_data) seg.inset = [slice(1, 4), slice(2, 5)] con = Contact() con.findContacts(segment=seg, boundary=bound) seg.useInset(inset=[slice(2, 4), slice(2, 5)], mode='abs') mor = Morphology() mor.getLength(segments=seg, boundaries=bound, contacts=con, distance='c2c', line='straight', position=False) assert_almost_equal(mor.length[seg.ids], [0])
def testMake(self): """ Tests make() """ conn, contacts = \ Connected.make(image=common.image_1, boundary=common.bound_1, thresh=4, boundaryIds=[3, 4], mask=5, nBoundary=1, boundCount='ge') np_test.assert_equal(conn.ids, [1, 2]) i1 = conn.data[2, 2] i2 = conn.data[2, 5] desired = numpy.zeros((10, 10), dtype=int) desired[2:6, 1:9] = numpy.array(\ [[0, 1, 0, 0, 2, 2, 2, 0], [0, 1, 0, 0, 2, 0, 2, 0], [1, 1, 1, 0, 2, 0, 2, 2], [1, 0, 1, 0, 2, 0, 2, 0]]) self.id_correspondence(conn.data, desired) conn, contacts = \ Connected.make(image=common.image_1, boundary=common.bound_1, thresh=4, boundaryIds=[3, 4], mask=5, nBoundary=1, boundCount='exact') np_test.assert_equal(conn.ids, []) conn, contacts = \ Connected.make(image=common.image_1, boundary=common.bound_1, thresh=2, boundaryIds=[3, 4], mask=5, nBoundary=2, boundCount='eq') np_test.assert_equal(conn.ids, [1]) conn, contacts = \ Connected.make(image=common.image_1, boundary=common.bound_1, thresh=2, boundaryIds=[3, 4], mask=5, nBoundary=1, boundCount='at_most') np_test.assert_equal(conn.ids, [1, 2]) # test ids and data conn, contacts = \ Connected.make(image=common.image_1, boundary=common.bound_1, thresh=2, boundaryIds=[3, 4], mask=5, nBoundary=1, boundCount='at_least') np_test.assert_equal(conn.ids, [1, 2, 3]) desired = numpy.zeros((10, 10), dtype=int) desired[2:6, 1:9] = numpy.array(\ [[0, 1, 0, 0, 0, 3, 3, 0], [0, 1, 0, 0, 0, 0, 3, 0], [0, 0, 0, 0, 0, 0, 3, 0], [2, 0, 0, 0, 0, 0, 3, 0]]) self.id_correspondence(conn.data, desired) # use insets conn, contacts = Connected.make(image=common.image_1in2, boundary=common.bound_1in, thresh=2, boundaryIds=[3, 4], mask=5, nBoundary=1, boundCount='at_least') np_test.assert_equal(conn.ids, [1, 2, 3]) self.id_correspondence(conn.data, desired[1:7, 1:9]) # mask Segment mask = Segment(data=numpy.where(common.bound_1.data == 5, 1, 0)) image_inset = copy(common.image_1.inset) bound_inset = copy(common.bound_1.inset) image_data = common.image_1.data.copy() bound_data = common.bound_1.data.copy() conn, contacts = Connected.make(image=common.image_1, boundary=common.bound_1, thresh=2., boundaryIds=[3, 4], mask=mask, nBoundary=1, boundCount='at_least') np_test.assert_equal(conn.ids, [1, 2, 3]) desired = numpy.zeros((10, 10), dtype=int) desired[2:6, 1:9] = numpy.array([[0, 1, 0, 0, 0, 3, 3, 0], [0, 1, 0, 0, 0, 0, 3, 0], [0, 0, 0, 0, 0, 0, 3, 0], [2, 0, 0, 0, 0, 0, 3, 0]]) np_test.assert_equal(conn.data > 0, desired > 0) np_test.assert_equal(image_inset, common.image_1.inset) np_test.assert_equal(bound_inset, common.bound_1.inset) np_test.assert_equal(image_data, common.image_1.data) np_test.assert_equal(bound_data, common.bound_1.data) # boundary inset, mask Segment same inset mask = Segment(data=numpy.where(common.bound_1in.data == 5, 1, 0)) mask.setInset(inset=[slice(1, 7), slice(1, 9)], mode='abs') conn, contacts = Connected.make(image=common.image_1, boundary=common.bound_1in, thresh=2, boundaryIds=[3, 4], mask=mask, nBoundary=1, boundCount='at_least') np_test.assert_equal(conn.ids, [1, 2, 3]) np_test.assert_equal(conn.data.shape, (6, 8)) np_test.assert_equal(conn.inset, [slice(1, 7), slice(1, 9)]) desired = numpy.zeros((6, 8), dtype=int) desired[1:5, 0:8] = numpy.array([[0, 1, 0, 0, 0, 3, 3, 0], [0, 1, 0, 0, 0, 0, 3, 0], [0, 0, 0, 0, 0, 0, 3, 0], [2, 0, 0, 0, 0, 0, 3, 0]]) np_test.assert_equal(conn.data > 0, desired > 0) # boundary inset, mask Segment smaller inset (inside boundaries) mask = Segment(data=numpy.where(common.bound_1in.data == 5, 1, 0)) mask.setInset(inset=[slice(1, 7), slice(1, 9)], mode='abs') mask.useInset([slice(2, 6), slice(1, 9)], mode='abs') conn, contacts = Connected.make(image=common.image_1, boundary=common.bound_1in, thresh=2, boundaryIds=[3, 4], mask=mask, nBoundary=1, boundCount='at_least') np_test.assert_equal(conn.ids, [1, 2, 3]) np_test.assert_equal(conn.data.shape, (6, 8)) np_test.assert_equal(conn.inset, [slice(1, 7), slice(1, 9)]) desired = numpy.zeros((6, 8), dtype=int) desired[1:5, 0:8] = numpy.array([[0, 1, 0, 0, 0, 3, 3, 0], [0, 1, 0, 0, 0, 0, 3, 0], [0, 0, 0, 0, 0, 0, 3, 0], [2, 0, 0, 0, 0, 0, 3, 0]]) np_test.assert_equal(conn.data > 0, desired > 0) # boundary inset, mask Segment even smaller inset (inside boundaries) mask = Segment(data=numpy.where(common.bound_1in.data == 5, 1, 0)) mask.setInset(inset=[slice(1, 7), slice(1, 9)], mode='abs') mask.useInset([slice(2, 6), slice(2, 9)], mode='abs') image_inset = copy(common.image_1.inset) bound_inset = copy(common.bound_1in.inset) image_data = common.image_1.data.copy() bound_data = common.bound_1in.data.copy() conn, contacts = Connected.make(image=common.image_1, boundary=common.bound_1in, thresh=2, boundaryIds=[3, 4], mask=mask, nBoundary=1, boundCount='at_least') np_test.assert_equal(conn.ids, [1, 2]) np_test.assert_equal(conn.data.shape, (6, 8)) np_test.assert_equal(conn.inset, [slice(1, 7), slice(1, 9)]) desired = numpy.zeros((6, 8), dtype=int) desired[1:5, 1:8] = numpy.array([[1, 0, 0, 0, 2, 2, 0], [1, 0, 0, 0, 0, 2, 0], [0, 0, 0, 0, 0, 2, 0], [0, 0, 0, 0, 0, 2, 0]]) np_test.assert_equal(conn.data, desired) np_test.assert_equal(image_inset, common.image_1.inset) np_test.assert_equal(bound_inset, common.bound_1in.inset) np_test.assert_equal(image_data, common.image_1.data) np_test.assert_equal(bound_data, common.bound_1in.data) # image smaller than boundaries mask = Segment(data=numpy.where(common.bound_1in.data == 5, 1, 0)) mask.setInset(inset=[slice(1, 7), slice(1, 9)], mode='abs') mask.useInset([slice(2, 6), slice(1, 9)], mode='abs') image_inset = copy(common.image_1in.inset) image_data = common.image_1in.data.copy() bound_inset = copy(common.bound_1in.inset) bound_data = common.bound_1in.data.copy() conn, contacts = Connected.make(image=common.image_1in, boundary=common.bound_1in, thresh=2, boundaryIds=[3, 4], mask=mask, nBoundary=1, boundCount='at_least') np_test.assert_equal(conn.ids, [1, 2, 3]) np_test.assert_equal(conn.data.shape, (6, 8)) np_test.assert_equal(conn.inset, [slice(1, 7), slice(1, 9)]) desired = numpy.zeros((6, 8), dtype=int) desired[1:5, 0:8] = numpy.array([[0, 1, 0, 0, 0, 3, 3, 0], [0, 1, 0, 0, 0, 0, 3, 0], [0, 0, 0, 0, 0, 0, 3, 0], [2, 0, 0, 0, 0, 0, 3, 0]]) np_test.assert_equal(conn.data > 0, desired > 0) np_test.assert_equal(image_inset, common.image_1in.inset) np_test.assert_equal(image_data, common.image_1in.data) np_test.assert_equal(bound_inset, common.bound_1in.inset) np_test.assert_equal(bound_data, common.bound_1in.data) # image smaller than boundaries and intersects with free, boundaries # intersects with free image = Grey(data=common.image_1.data.copy()) image.useInset(inset=[slice(2, 6), slice(2, 9)], mode='abs') image_inset = copy(image.inset) image_data = image.data.copy() common.bound_1in.useInset(inset=[slice(1, 7), slice(1, 8)], mode='abs') bound_1in_inset = copy(common.bound_1in.inset) bound_data = common.bound_1in.data.copy() mask = Segment(data=numpy.where(common.bound_1in.data == 5, 1, 0)) mask.setInset(inset=[slice(1, 7), slice(1, 9)], mode='abs') mask.useInset([slice(2, 6), slice(1, 9)], mode='abs') conn, contacts = Connected.make(image=image, boundary=common.bound_1in, thresh=3, boundaryIds=[3, 4], mask=mask, nBoundary=1, boundCount='at_least') np_test.assert_equal(conn.ids, [1, 2, 3, 4]) np_test.assert_equal(conn.data.shape, (6, 7)) np_test.assert_equal(conn.inset, [slice(1, 7), slice(1, 8)]) desired = numpy.zeros((6, 7), dtype=int) desired[1:5, 0:8] = numpy.array([[0, 1, 0, 0, 0, 3, 3], [0, 1, 0, 0, 0, 0, 3], [0, 1, 0, 0, 0, 0, 3], [0, 0, 4, 0, 2, 0, 3]]) np_test.assert_equal(conn.data > 0, desired > 0) np_test.assert_equal(image_inset, image.inset) np_test.assert_equal(bound_1in_inset, common.bound_1in.inset) np_test.assert_equal(image_data, image.data) np_test.assert_equal(bound_data, common.bound_1in.data) common.bound_1in.useInset(inset=[slice(1, 7), slice(1, 9)], mode='abs', expand=True)
def testMakeFree(self): """ Tests makeFree() """ # int mask free = common.bound_1.makeFree(ids=[3, 4], size=0, mask=5, update=False) np_test.assert_equal(free.data.shape, (10, 10)) np_test.assert_equal(free.data, numpy.where(common.bound_1.data == 5, 1, 0)) # ndarray mask mask = numpy.where(common.bound_1.data == 5, 2, 0) free = common.bound_1.makeFree(ids=[3, 4], size=0, mask=mask, update=False) np_test.assert_equal(free.data.shape, (10, 10)) np_test.assert_equal(free.data, numpy.where(common.bound_1.data == 5, 1, 0)) # Segment mask mask = Segment(data=numpy.where(common.bound_1.data == 5, 3, 0)) free = common.bound_1.makeFree(ids=[3, 4], size=0, mask=mask, update=False) np_test.assert_equal(free.data.shape, (10, 10)) np_test.assert_equal(free.data, numpy.where(common.bound_1.data == 5, 1, 0)) # boundary inset, Segment mask full size mask = Segment(data=numpy.where(common.bound_1.data == 5, 3, 0)) free = common.bound_1in.makeFree(ids=[3, 4], size=0, mask=mask, update=False) np_test.assert_equal(free.data.shape, (6, 8)) np_test.assert_equal(free.data, numpy.where(common.bound_1in.data == 5, 1, 0)) np_test.assert_equal(free.inset, [slice(1, 7), slice(1, 9)]) # boundary inset, mask Segment same inset mask = Segment(data=numpy.where(common.bound_1.data == 5, 3, 0)) mask.useInset(inset=[slice(1, 7), slice(1, 9)]) free = common.bound_1in.makeFree(ids=[3, 4], size=0, mask=mask, update=False) np_test.assert_equal(free.data.shape, (6, 8)) np_test.assert_equal(free.data, numpy.where(common.bound_1in.data == 5, 1, 0)) np_test.assert_equal(free.inset, [slice(1, 7), slice(1, 9)]) # boundary full size, mask Segment inset that includes boundary mask = Segment(data=numpy.where(common.bound_1.data == 5, 3, 0)) mask.useInset(inset=[slice(1, 7), slice(1, 9)]) free = common.bound_1.makeFree(ids=[3, 4], size=0, mask=mask, update=False) np_test.assert_equal(free.data.shape, (10, 10)) np_test.assert_equal(free.data[1:7, 1:9], numpy.where(common.bound_1in.data == 5, 1, 0)) np_test.assert_equal(free.inset, [slice(0, 10), slice(0, 10)]) # boundary full size, mask Segment inset that doesn't include boundary mask = Segment(data=numpy.where(common.bound_1.data == 5, 3, 0)) mask.useInset(inset=[slice(2, 6), slice(1, 9)]) free = common.bound_1.makeFree(ids=[3, 4], size=0, mask=mask, update=False) np_test.assert_equal(free.data.shape, (10, 10)) np_test.assert_equal(free.data[1:7, 1:9], numpy.where(common.bound_1in.data == 5, 1, 0)) np_test.assert_equal(free.inset, [slice(0, 10), slice(0, 10)]) # boundary inset, mask smaller inset that doesn't include boundary mask = Segment(data=numpy.where(common.bound_1.data == 5, 3, 0)) mask.useInset(inset=[slice(2, 6), slice(1, 9)]) free = common.bound_1in.makeFree(ids=[3, 4], size=0, mask=mask, update=False) np_test.assert_equal(free.data.shape, (6, 8)) np_test.assert_equal(free.data, numpy.where(common.bound_1in.data == 5, 1, 0)) np_test.assert_equal(free.inset, [slice(1, 7), slice(1, 9)]) # boundary smaller inset than mask mask = Segment(data=numpy.where(common.bound_1.data == 5, 3, 0)) mask.useInset(inset=[slice(2, 6), slice(1, 9)]) common.bound_1in.useInset([slice(1, 7), slice(2, 7)], mode='absolute') free = common.bound_1in.makeFree(ids=[3, 4], size=0, mask=mask, update=False) np_test.assert_equal(free.data.shape, (6, 5)) np_test.assert_equal( free.data, numpy.where(common.bound_1.data == 5, 1, 0)[1:7, 2:7]) np_test.assert_equal(free.inset, [slice(1, 7), slice(2, 7)])