def testAddLevel(self): """ Tests addLevel() """ # one level data hi_data = numpy.array([[1, 0, 2], [1, 2, 2]]) hi = Hierarchy(data=hi_data) hi.inset = [slice(3,5), slice(2,5)] hi.setIds(ids=[1,2]) hi.levelIds = [[], [1,2]] # segment (different inset) seg_data = numpy.array([[2, 1], [1, 1]]) seg = Segment(data=seg_data) seg.inset = [slice(3,5), slice(1,3)] # add hi.addLevel(segment=seg, level=2, check=False, shift=10) desired_data = numpy.array([[12, 1, 0, 2], [11, 1, 2, 2]]) desired_inset = [slice(3,5), slice(1,5)] np_test.assert_equal(hi.data, desired_data) np_test.assert_equal(hi.inset, desired_inset) np_test.assert_equal(hi.levelIds, [[], [1,2], [11,12]]) np_test.assert_equal(hi._higherIds, {1:11, 2:0})
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])
image_1in2 = Grey(image_ar_1[1:7, 1:9]) image_1in2.inset = [slice(1, 7), slice(1, 9)] # boundaries 1 bound_ar_inset_1 = numpy.array(\ [[3, 3, 3, 3, 3, 3, 3, 3], [5, 5, 5, 5, 5, 5, 5, 5], [5, 5, 5, 5, 5, 5, 5, 5], [5, 5, 5, 5, 5, 5, 5, 5], [5, 5, 5, 5, 5, 5, 5, 5], [4, 4, 4, 4, 4, 4, 4, 4]]) bound_ar_1 = numpy.zeros((10, 10), dtype=int) bound_ar_1[1:7, 1:9] = bound_ar_inset_1 bound_1 = Segment(bound_ar_1) bound_1in = Segment(bound_ar_inset_1) bound_1in.inset = [slice(1, 7), slice(1, 9)] # expected segmentation threshold_1 = list(range(8)) ids_1 = list(range(1, 15)) levelIds_1 = [[], [1, 2], [3, 4, 5], [6, 7, 8, 9], [10, 11], [12], [13], [14]] thresh_1 = [0, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, 7] data_1 = numpy.zeros((8, 4, 8), dtype='int') data_1[0] = numpy.zeros((4, 8), dtype='int') data_1[1] = numpy.array([[0, 1, 0, 0, 0, 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]]) data_1[2] = numpy.array([[0, 3, 0, 0, 0, 4, 4, 0], [0, 3, 0, 0, 0, 0, 4, 0], [0, 0, 0, 0, 0, 0, 4, 0], [5, 0, 0, 0, 0, 0, 4, 0]]) data_1[3] = numpy.array([[0, 6, 0, 0, 0, 7, 7, 0], [0, 6, 0, 0, 0, 0, 7, 0], [6, 6, 0, 0, 0, 0, 7, 0], [6, 0, 8, 0, 9, 0, 7, 0]]) data_1[4] = numpy.array([[0, 10, 0, 0, 11, 11, 11, 0],