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 doThreshold(self, threshold, pickDark=True): """ Basic thresholding of self.data. Returns a Segment object whose attribute data is the boolean array (of the same size as self.data) with elements <= threshold labeled True. If pickDark is False, elements >= threshold are True. Arguments: - threshold: threshold - pickDark: if True elements below threshold are labeled, otherwise the element above thresholds are labeled """ # set attributes and check if data exist self.threshold = threshold self.pickDark = pickDark # threshold try: if pickDark: t_data = (self.data <= threshold) else: t_data = self.data >= threshold except AttributeError: print("Data not found.") raise # make a Segment instance and preserve offset and inset from .segment import Segment seg = Segment(data=t_data, copy=False, clean=False) seg.copyPositioning(self, saveFull=True) return seg
def testCalculate(self): """ Tests calculate """ # one region, mean shapes = common.make_shapes() dist = DistanceTo(segments=shapes) dist.calculate(regionIds=[3], regions=self.bound, mode='mean') np_test.assert_almost_equal(dist._distance, self.dist_mean[0, :]) np_test.assert_almost_equal(dist.distance[dist.ids], self.dist_mean[0, :]) np_test.assert_almost_equal(dist._closestRegion, [3] * 4) np_test.assert_almost_equal(dist.closestRegion[dist.ids], [3] * 4) # two regions, min shapes = common.make_shapes() dist = DistanceTo(segments=shapes) dist.calculate(regionIds=[3, 4], regions=self.bound, mode='min') np_test.assert_almost_equal(dist._distance, self.dist_cl_min) np_test.assert_almost_equal(dist.distance[dist.ids], self.dist_cl_min) np_test.assert_almost_equal(dist._closestRegion, self.cl_reg_min) np_test.assert_almost_equal(dist.closestRegion[dist.ids], self.cl_reg_min) # segments and regions from the same object shapes = common.make_shapes() dist = DistanceTo(segments=shapes, ids=[1, 4, 6]) dist.calculate(regionIds=[3], mode='min') np_test.assert_almost_equal(dist.distance[dist.ids], self.seg_3_min) # segments and regions from the same object shapes = common.make_shapes() dist = DistanceTo(segments=shapes, ids=[1, 6]) dist.calculate(regionIds=[3, 4], mode='min') np_test.assert_almost_equal(dist.distance[dist.ids], self.seg_3_4_min) np_test.assert_almost_equal(dist.closestRegion[dist.ids], self.seg_3_4_closest) # insets shapes = common.make_shapes() shapes.makeInset(ids=[1, 3]) bound = Segment(self.bound.data) bound.makeInset(ids=[4]) dist = DistanceTo(segments=shapes, ids=[1, 3]) dist.calculate(regionIds=[4], regions=bound, mode='min') np_test.assert_almost_equal(dist.distance[dist.ids], [6, numpy.sqrt(29)]) # median, surface 1 shapes = common.make_shapes() dist = DistanceTo(segments=shapes) all_dist = dist.calculate(segments=shapes, regionIds=[3, 4], regions=self.bound, mode='median', surface=1) np_test.assert_almost_equal(dist._distance, self.dist_cl_median_s1) np_test.assert_equal(dist._closestRegion, self.cl_median_s1)
def make_shapes(): """ Makes an array containing few different shapes, makes a Segmentation object and returns it. """ # empty array shapes = numpy.zeros(shape=(10, 10), dtype=int) # add square shapes[1:4, 1:4] = 1 # add circle shapes[0:5, 5:10] = 3 shapes[0, 5] = 0 shapes[0, 9] = 0 shapes[4, 9] = 0 shapes[4, 5] = 0 # add elipse shapes[7:10, 1:7] = [[0, 4, 4, 4, 0, 0], [4, 4, 4, 4, 4, 0], [0, 4, 4, 4, 4, 4]] # add uneven shapes[6:10, 5:9] = [[6, 6, 0, 0], [0, 6, 6, 0], [4, 0, 6, 6], [4, 4, 0, 6]] # instantiate and return return Segment(shapes)
def testAggregate(self): """ Tests aggregate() """ # calculate density = Density() density.calculate(image=self.grey, segments=self.segments) new = density.aggregate(ids=[[1, 3, 6], [4]]) # make aggregated segments desired_seg_data = self.segments.data.copy() desired_seg_data[desired_seg_data == 3] = 1 desired_seg_data[desired_seg_data == 6] = 1 desired_seg = Segment(desired_seg_data) desired = Density() desired.calculate(image=self.grey, segments=desired_seg) # test new_ids = numpy.concatenate([[0], new.ids]) desired_ids = numpy.concatenate([[0], desired.ids]) np_test.assert_almost_equal(new.mean[new_ids], desired.mean[desired_ids]) np_test.assert_almost_equal(new.std[new_ids], desired.std[desired_ids]) np_test.assert_equal(new.min[new_ids], desired.min[desired_ids]) np_test.assert_equal(new.max[new_ids], desired.max[desired_ids]) np_test.assert_equal(new.volume[new_ids], desired.volume[desired_ids])
def setUp(self): """ """ ar_1 = numpy.array([[1, 1, 1, 1, 1, 0, 0, 2, 2], [0, 0, 0, 0, 0, 0, 0, 2, 2], [0, 0, 3, 0, 0, 0, 0, 0, 0], [0, 3, 3, 0, 0, 0, 0, 0, 4]]) self.segments_1 = Segment(data=ar_1)
def makeSlantedLayers(self, item): if item == 1: data = numpy.array([[0, 0, 0, 0, 1, 2], [0, 0, 0, 1, 2, 2], [0, 0, 1, 2, 2, 3], [0, 1, 2, 2, 3, 3], [5, 2, 2, 3, 3, 0], [5, 2, 3, 3, 0, 0], [5, 5, 5, 3, 0, 0]]) segments = Segment(data) return segments elif item == 2: data = numpy.array([[5, 5, 5, 1, 0, 0], [5, 5, 1, 1, 2, 0], [5, 5, 1, 2, 2, 3], [5, 1, 1, 2, 3, 3], [0, 1, 2, 2, 3, 6], [0, 0, 2, 3, 3, 6], [0, 0, 0, 3, 6, 6]]) layers = Segment(data) return layers
def makeHorizontalLayers(self, item): if item == 1: data = numpy.array([[0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 0], [0, 2, 2, 2, 2, 0], [0, 3, 3, 3, 3, 0], [0, 4, 4, 4, 4, 0], [0, 5, 5, 5, 5, 0]]) segments = Segment(data) region = numpy.zeros(shape=data.shape) region[:, 0] = 5 return segments, region elif item == 2: data = numpy.array([[0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 0], [0, 2, 2, 2, 0, 0], [0, 0, 3, 3, 3, 0], [0, 0, 4, 4, 4, 0], [0, 5, 5, 5, 5, 0]]) layers = Segment(data) return layers
def setUp(self): # to avoid problems when running multiple tests try: importlib.reload(common) except AttributeError: pass # Python2 # make image bound_data = numpy.zeros((15, 15), dtype=int) bound_data[slice(2, 12), slice(3, 12)] = numpy.array(\ [[7, 7, 7, 7, 7, 7, 7, 7, 7], [0, 1, 1, 1, 1, 2, 2, 2, 0], [0, 1, 1, 1, 1, 2, 2, 2, 0], [6, 6, 5, 5, 5, 5, 5, 5, 6], [6, 6, 5, 5, 5, 5, 5, 5, 6], [6, 6, 5, 5, 5, 5, 5, 5, 6], [6, 6, 5, 5, 5, 5, 5, 5, 6], [0, 3, 3, 3, 3, 3, 3, 3, 0], [3, 3, 3, 3, 3, 3, 3, 3, 3], [8, 8, 8, 8, 9, 9, 8, 8, 8]]) self.bound_1 = Segment(bound_data)
def setUp(self): """ """ importlib.reload( common) # to avoid problems when running multiple tests # trivial flat seg_data = numpy.array([[0, 0, 1, 0, 0], [0, 1, 1, 0, 0], [0, 1, 1, 1, 1], [0, 1, 0, 0, 0]]) seg_data = numpy.expand_dims(seg_data, 0) self.trivial_flat = Segment(data=seg_data) # trivial 3d seg_data = numpy.array([[[0, 0, 2, 0, 0], [0, 2, 2, 0, 0], [0, 2, 2, 2, 2], [0, 2, 0, 0, 0]], [[0, 2, 0, 2, 2], [0, 2, 2, 2, 2], [2, 2, 2, 2, 2], [0, 0, 0, 2, 2]]]) self.trivial = Segment(data=seg_data) # loop seg_data = numpy.array([[[0, 0, 3, 3, 3, 0], [3, 3, 3, 0, 3, 3], [3, 3, 3, 0, 0, 3], [3, 0, 0, 0, 0, 0]], [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 3, 3, 3, 3, 3], [0, 0, 0, 0, 3, 0]]]) self.loop = Segment(data=seg_data) # small sphere seg_data = numpy.array([[[0, 1, 1, 1, 0, 0], [0, 1, 1, 1, 0, 0], [0, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0]], [[0, 1, 1, 1, 0, 0], [0, 1, 0, 1, 0, 0], [0, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0]], [[0, 1, 1, 1, 0, 0], [0, 1, 1, 1, 0, 0], [0, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0]]]) self.small_sphere = Segment(data=seg_data) # sphere seg_data = numpy.array([[[0, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 0]], [[0, 1, 1, 1, 1, 0], [0, 1, 1, 0, 1, 0], [0, 1, 0, 0, 1, 1], [0, 1, 1, 1, 1, 1]], [[0, 0, 1, 1, 1, 0], [0, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 0]]]) self.sphere = Segment(data=seg_data) # torus seg_data = numpy.array([[[4, 4, 4, 4, 4, 4, 4], [4, 4, 4, 4, 4, 4, 4], [4, 4, 4, 4, 4, 4, 4], [4, 4, 4, 0, 4, 4, 4], [4, 4, 4, 4, 4, 4, 4], [4, 4, 4, 4, 4, 4, 4], [4, 4, 4, 4, 4, 4, 4]], [[4, 4, 4, 4, 4, 4, 4], [4, 0, 0, 0, 0, 0, 4], [4, 0, 4, 4, 4, 0, 4], [4, 0, 4, 0, 4, 0, 4], [4, 0, 4, 4, 4, 0, 4], [4, 0, 0, 0, 0, 0, 4], [4, 4, 4, 4, 4, 4, 4]], [[4, 4, 4, 4, 4, 4, 4], [4, 4, 4, 4, 4, 4, 4], [4, 4, 4, 4, 4, 4, 4], [4, 4, 4, 0, 4, 4, 4], [4, 4, 4, 4, 4, 4, 4], [4, 4, 4, 4, 4, 4, 4], [4, 4, 4, 4, 4, 4, 4]]]) self.torus = Segment(data=seg_data)
def setUp(self): """ """ bound_ar = numpy.array( [[1, 1, 1, 1, 1, 0, 2, 2, 2], [0, 0, 0, 0, 0, 0, 0, 2, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 3, 0, 0, 0, 0, 0, 0], [0, 3, 3, 0, 0, 0, 4, 4, 4], [0, 3, 3, 0, 0, 0, 4, 4, 4]]) self.bound = Segment(data=bound_ar) segment_ar = numpy.array( [[0, 0, 0, 0, 0, 5, 0, 0, 0], [1, 0, 6, 0, 0, 0, 8, 0, 0], [1, 0, 6, 0, 0, 0, 0, 0, 0], [1, 0, 0, 7, 7, 0, 9, 0, 0], [1, 0, 0, 0, 7, 0, 0, 0, 0], [0, 0, 0, 0, 7, 7, 0, 0, 0]]) self.segments = Segment(data=segment_ar) self.contacts = Contact() self.contacts.findContacts( segment=self.segments, boundary=self.bound, count=False)
def setUp(self): """ """ importlib.reload( common) # to avoid problems when running multiple tests # regions bound_data = numpy.zeros((10, 10), dtype=int) bound_data[0:5, 0] = 3 bound_data[9, 0:5] = 4 self.bound = Segment(bound_data) # expected distances to region self.dist_mean = numpy.array([[2, 7, 5.46184388, 7.49631542], [7, 7.72255285, 1.10878566, 3.49817009]]) self.dist_min = numpy.array([[1, 5, numpy.sqrt(13), numpy.sqrt(29)], [6, numpy.sqrt(29), 0, numpy.sqrt(8)]]) self.dist_cl_min = numpy.array([1, 5, 0, numpy.sqrt(8)]) self.cl_reg_min = numpy.array([3, 3, 4, 4]) self.dist_max = numpy.array([[3, 9, numpy.sqrt(61), numpy.sqrt(89)], [8, numpy.sqrt(97), 2, numpy.sqrt(17)]]) self.dist_median = numpy.array([[2, 7, numpy.sqrt(29), numpy.sqrt(58)], [7, numpy.sqrt(61), 1, numpy.sqrt(13)]]) self.dist_center = numpy.array([[2, 7, numpy.sqrt(25), numpy.sqrt(58)], [7, numpy.sqrt(58), 1, numpy.sqrt(13)]]) self.dist_median_s1 = numpy.array( [[2, 6.5, numpy.sqrt(25), numpy.sqrt(58)], [7, (numpy.sqrt(61) + numpy.sqrt(50)) / 2, 2, numpy.sqrt(13)]]) self.dist_cl_median_s1 = numpy.array([2, 6.5, 2, numpy.sqrt(13)]) self.cl_median_s1 = numpy.array([3, 3, 4, 4]) # expected distances among segments self.seg_3_min = numpy.array([2, numpy.sqrt(13), 2]) self.seg_3_4_min = numpy.array([2, numpy.sqrt(2)]) self.seg_3_4_closest = numpy.array([3, 4])
def setUp(self): # make image self.image_1 = Image(numpy.arange(100).reshape(10,10)) # make cleft 1 cleft_data_1 = numpy.zeros((10,10), dtype=int) cleft_data_1[slice(1,7), slice(3,9)] = numpy.array(\ [[0, 5, 5, 5, 0, 0], [2, 5, 5, 5, 4, 4], [2, 5, 5, 5, 4, 4], [2, 2, 5, 5, 5, 4], [2, 5, 5, 5, 4, 4], [2, 5, 5, 5, 4, 4]]) self.bound_1 = Segment(cleft_data_1) self.cleft_1 = Cleft(data=self.bound_1.data, bound1Id=2, bound2Id=4, cleftId=5, copy=True, clean=False) self.cleft_1_ins = Cleft(data=self.bound_1.data[1:7, 3:9], bound1Id=2, bound2Id=4, cleftId=5, copy=True, clean=False) self.cleft_1_ins.inset = [slice(1,7), slice(3,9)] # make big cleft and image self.cleft_2, self.image_2 = common.make_cleft_layers_example()
def testGetStructureFootprint(self): """ Tests getStructureFootprint() """ # 2d conn 1 seg = Segment(numpy.zeros(shape=(3, 4))) se, foot = seg.getStructureFootprint(connectivity=1) desired = scipy.ndimage.generate_binary_structure(2, 1) np_test.assert_equal(foot, desired) desired = desired.astype(int) desired[1, 1] = 0 np_test.assert_equal(se, desired) # 3d conn 2 seg = Segment(numpy.zeros(shape=(3, 4, 5))) se, foot = seg.getStructureFootprint(connectivity=2) desired = scipy.ndimage.generate_binary_structure(3, 2) np_test.assert_equal(foot, desired) desired = desired.astype(int) desired[1, 1, 1] = 0 np_test.assert_equal(se, desired) # 3d conn -1 (3) seg = Segment(numpy.zeros(shape=(3, 4, 5))) se, foot = seg.getStructureFootprint(connectivity=-1) desired = scipy.ndimage.generate_binary_structure(3, 3) np_test.assert_equal(foot, desired) desired = desired.astype(int) desired[1, 1, 1] = 0 np_test.assert_equal(se, desired) # 2d conn [2] seg = Segment(numpy.zeros(shape=(3, 4))) se, foot = seg.getStructureFootprint(connectivity=[2]) desired = scipy.ndimage.generate_binary_structure(2, 2) np_test.assert_equal(foot, desired) desired = desired.astype(int) * numpy.sqrt(2) desired[1, 1] = 0 np_test.assert_almost_equal(se, desired) # 3d conn [1,2,3] seg = Segment(numpy.zeros(shape=(3, 4, 5))) se, foot = seg.getStructureFootprint(connectivity=[3, 1, 2]) desired = scipy.ndimage.generate_binary_structure(3, 3) np_test.assert_equal(foot, desired) desired = numpy.array([[[3., 2, 3], [2, 1, 2], [3, 2, 3]], [[2, 1, 2], [1, 0, 1], [2, 1, 2]], [[3, 2, 3], [2, 1, 2], [3, 2, 3]]]) np_test.assert_almost_equal(se**2, desired)
def testCalculate(self): """ Tests calculate, getEuler, countFaces and calculateHomologyRank methods. """ # calculate topo = Topology(segments=common.segment_3) ids = numpy.insert(topo.ids, 0, 0) topo.calculate() # test np_test.assert_equal(topo.euler[ids], common.euler_3) np_test.assert_equal(topo.nObjects[ids], common.objects_3) np_test.assert_equal(topo.nLoops[ids], common.loops_3) np_test.assert_equal(topo.nHoles[ids], common.holes_3) np_test.assert_equal(topo.homologyRank[ids, 0], common.objects_3) np_test.assert_equal(topo.homologyRank[ids, 1], common.loops_3) np_test.assert_equal(topo.homologyRank[ids, 2], common.holes_3) np_test.assert_equal(topo.homologyRank[ids, 3], numpy.zeros(8)) np_test.assert_equal(topo.nFaces[ids], common.faces_3) # add a nonexisting index topo.calculate(ids=numpy.append(topo.ids, 23)) np_test.assert_equal(topo.homologyRank[23, 0], 0) np_test.assert_equal(topo.homologyRank[23, 1], 0) np_test.assert_equal(topo.homologyRank[23, 2], 0) np_test.assert_equal(topo.homologyRank[23, 3], 0) # trivial flat topo = Topology(segments=self.trivial_flat) topo.calculate() np_test.assert_equal(topo.euler[1], 1) np_test.assert_equal(topo.nLoops[1], 0) np_test.assert_equal(topo.nHoles[1], 0) # trivial topo = Topology(segments=self.trivial) topo.calculate() np_test.assert_equal(topo.euler[2], 1) np_test.assert_equal(topo.nLoops[2], 0) np_test.assert_equal(topo.nHoles[2], 0) # loop topo = Topology(segments=self.loop) topo.calculate() np_test.assert_equal(topo.euler[3], 0) np_test.assert_equal(topo.nLoops[3], 1) np_test.assert_equal(topo.nHoles[3], 0) # small sphere topo = Topology(segments=self.small_sphere) topo.calculate() np_test.assert_equal(topo.euler[1], 2) np_test.assert_equal(topo.nLoops[1], 0) np_test.assert_equal(topo.nHoles[1], 1) # sphere topo = Topology(segments=self.sphere) topo.calculate() np_test.assert_equal(topo.euler[1], 2) np_test.assert_equal(topo.nLoops[1], 0) np_test.assert_equal(topo.nHoles[1], 1) # torus topo = Topology(segments=self.torus) topo.calculate() np_test.assert_equal(topo.euler[4], 0) np_test.assert_equal(topo.nLoops[4], 2) np_test.assert_equal(topo.nHoles[4], 1) # no segments data_0 = numpy.zeros(shape=(2, 4, 3), dtype=int) seg_0 = Segment(data=data_0) topo_0 = Topology(segments=seg_0) topo_0.calculate() np_test.assert_equal(topo_0.euler, [0]) np_test.assert_equal(topo_0.nFaces, numpy.array([[0, 0, 0, 0]])) # segments exist but no ids data_1 = numpy.ones(shape=(2, 4, 3), dtype=int) seg_1 = Segment(data=data_1) topo_1 = Topology(segments=seg_1) topo_1.calculate(ids=[2, 4]) np_test.assert_equal(topo_1.euler, [0, 0, 0, 0, 0]) np_test.assert_equal(topo_1.nFaces, numpy.zeros(shape=(5, 4), dtype=int))
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 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_1in = Grey(image_ar_inset_1) image_1in.inset = [slice(2, 6), slice(1, 9)] 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],
def testLayersBetween(self): """ Tests different nLayers and width arguments in makeLayersBetween(). """ # w nLayers, no extra layers layers, width = self.bound_1.makeLayersBetween(nLayers=2, bound_1=[1, 2], bound_2=3, mask=5) layers.useInset(inset=self.bound_1.inset, mode='abs', expand=True) desired = numpy.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, 1, 1, 1, 1, 1, 1, 0], [0, 0, 1, 1, 1, 1, 1, 1, 0], [0, 0, 2, 2, 2, 2, 2, 2, 0], [0, 0, 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]]) np_test.assert_almost_equal(width, 4) np_test.assert_equal(layers.data[2:12, 3:12], desired) # w multi id mask and extra layers on boundaries and extra regions layers, width = self.bound_1.makeLayersBetween(nLayers=2, bound_1=numpy.array( [1, 2]), bound_2=3, mask=[5, 6], nExtraLayers=1, extra_1=7, extra_2=[8, 9], maxDistance=5.5) layers.useInset(inset=self.bound_1.inset, mode='abs', expand=True) desired = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 0], [0, 2, 2, 2, 2, 2, 2, 2, 0], [2, 2, 2, 2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3, 3, 3, 3], [0, 3, 3, 3, 3, 3, 3, 3, 0], [0, 4, 4, 4, 4, 4, 4, 4, 0], [4, 4, 4, 4, 4, 4, 4, 4, 4], [0, 0, 0, 0, 0, 0, 0, 0, 0]]) np_test.assert_almost_equal(width, 4) np_test.assert_equal(layers.data[2:12, 3:12], desired) # w multi id mask and extra layers on boundaries and extra regions # and layers thinner than 1 layers, width = self.bound_1.makeLayersBetween(nLayers=8, bound_1=numpy.array( [1, 2]), bound_2=3, mask=[5, 6], nExtraLayers=2, extra_1=7, extra_2=[8, 9], maxDistance=5.5) layers.useInset(inset=self.bound_1.inset, mode='abs', expand=True) # possibly ambiguous layer assignment np_test.assert_equal( ((layers.data[4, 4:11] >= 1) & (layers.data[4, 4:11] <= 2)).all(), True) np_test.assert_equal(((layers.data[9, 4:11] >= 11) & (layers.data[9, 4:11] <= 12)).all(), True) # w multi id mask and extra layers on boundaries and extra regions layers, width = self.bound_1.makeLayersBetween(nLayers=2, bound_1=numpy.array( [1, 2]), bound_2=3, mask=[5, 6], nExtraLayers=2, extra_1=7, extra_2=[8, 9], maxDistance=5.5) layers.useInset(inset=self.bound_1.inset, mode='abs', expand=True) desired = numpy.array([[1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 2, 2, 2, 2, 2, 2, 2, 0], [0, 2, 2, 2, 2, 2, 2, 2, 0], [0, 3, 3, 3, 3, 3, 3, 3, 0], [3, 3, 3, 3, 3, 3, 3, 3, 3], [4, 4, 4, 4, 4, 4, 4, 4, 4], [0, 4, 4, 4, 4, 4, 4, 4, 0], [0, 5, 5, 5, 5, 5, 5, 5, 0], [5, 5, 5, 5, 5, 5, 5, 5, 5], [6, 6, 6, 6, 6, 6, 6, 6, 6]]) np_test.assert_almost_equal(width, 4) np_test.assert_equal(layers.data[2:12, 3:12], desired) # w maxDistance and no fill data = numpy.array([[1, 1, 1, 1, 1, 1, 1], [1, 1, 5, 5, 5, 1, 1], [5, 5, 5, 5, 5, 5, 5], [5, 5, 5, 5, 5, 5, 5], [3, 3, 5, 5, 5, 3, 3], [3, 3, 7, 7, 7, 7, 7]]) seg = Segment(data) layers, width = seg.makeLayersBetween(nLayers=2, bound_1=numpy.array([1]), bound_2=[3, 7], mask=[5], nExtraLayers=1, maxDistance=4, fill=False) desired = numpy.array([[0, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 1, 1], [2, 2, 2, 0, 2, 2, 2], [3, 3, 3, 0, 3, 3, 3], [4, 4, 0, 0, 0, 4, 4], [0, 0, 0, 0, 0, 0, 0]]) np_test.assert_equal(layers.data, desired) np_test.assert_almost_equal(width, 2.) # w maxDistance, with fill, mode median layers, width = seg.makeLayersBetween(nLayers=2, bound_1=numpy.array([1]), bound_2=[3, 7], mask=[5], nExtraLayers=1, between='median', maxDistance=3, fill=True) desired = numpy.array([[0, 1, 1, 1, 1, 1, 0], [1, 1, 2, 2, 2, 1, 1], [2, 2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3, 3], [4, 4, 3, 3, 3, 4, 4], [0, 4, 4, 4, 4, 4, 0]]) np_test.assert_equal(layers.data, desired) np_test.assert_almost_equal(width, 2.) # w maxDistance, with fill, mode mean layers, width = seg.makeLayersBetween(nLayers=2, bound_1=numpy.array([1]), bound_2=[3, 7], mask=[5], nExtraLayers=1, between='mean', maxDistance=3, fill=True) desired = numpy.array([[0, 1, 1, 1, 1, 1, 0], [1, 1, 2, 2, 2, 1, 1], [2, 2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3, 3], [4, 4, 3, 3, 3, 4, 4], [0, 4, 4, 4, 4, 4, 0]]) np_test.assert_equal(layers.data, desired) np_test.assert_almost_equal( width, ((4 * 3 + 2 * numpy.sqrt(17) + numpy.sqrt(20)) / 7. - 1)) # funny shaped mask segments_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]]) mask_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=segments_data) layers, width = seg.makeLayersBetween(bound_1=3, bound_2=2, mask=mask_data, between='min') desired_layers = numpy.array([[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 2], [0, 0, 0, 0, 0, 3], [0, 0, 0, 3, 3, 3], [0, 0, 0, 0, 0, 0]]) np_test.assert_almost_equal(width, numpy.sqrt(17) - 1) np_test.assert_equal(layers.data, desired_layers)
class TestSegment(np_test.TestCase): """ """ def setUp(self): # to avoid problems when running multiple tests try: importlib.reload(common) except AttributeError: pass # Python2 # make image bound_data = numpy.zeros((15, 15), dtype=int) bound_data[slice(2, 12), slice(3, 12)] = numpy.array(\ [[7, 7, 7, 7, 7, 7, 7, 7, 7], [0, 1, 1, 1, 1, 2, 2, 2, 0], [0, 1, 1, 1, 1, 2, 2, 2, 0], [6, 6, 5, 5, 5, 5, 5, 5, 6], [6, 6, 5, 5, 5, 5, 5, 5, 6], [6, 6, 5, 5, 5, 5, 5, 5, 6], [6, 6, 5, 5, 5, 5, 5, 5, 6], [0, 3, 3, 3, 3, 3, 3, 3, 0], [3, 3, 3, 3, 3, 3, 3, 3, 3], [8, 8, 8, 8, 9, 9, 8, 8, 8]]) self.bound_1 = Segment(bound_data) def testFindNonUnique(self): """ Tests findNonUnique() """ data = numpy.array([[0, 2, 3, 4], [2, 3, 3, 4], [4, 5, 5, 7]]) # unique seg = Segment(data=data, ids=[3, 5, 7]) nonu = seg.findNonUnique() np_test.assert_equal(nonu['many'], []) np_test.assert_equal(nonu['empty'], []) # non unique seg = Segment(data=data) seg.setIds(ids=[2, 3, 4, 5, 6, 7, 8]) nonu = seg.findNonUnique() np_test.assert_equal(nonu['many'], [2, 4]) np_test.assert_equal(nonu['empty'], [6, 8]) # non-unique, some ids excluded seg = Segment(data=data) seg.setIds(ids=[3, 4, 5, 6, 7]) nonu = seg.findNonUnique() np_test.assert_equal(nonu['many'], [4]) np_test.assert_equal(nonu['empty'], [6]) def testRemove(self): """ Tests remove() with mode 'remove' """ removed = self.bound_1.remove(data=self.bound_1.data[2:12, 3:12], ids=[2, 3, 5, 9], value=4) desired = numpy.array(\ [[7, 7, 7, 7, 7, 7, 7, 7, 7], [0, 1, 1, 1, 1, 4, 4, 4, 0], [0, 1, 1, 1, 1, 4, 4, 4, 0], [6, 6, 4, 4, 4, 4, 4, 4, 6], [6, 6, 4, 4, 4, 4, 4, 4, 6], [6, 6, 4, 4, 4, 4, 4, 4, 6], [6, 6, 4, 4, 4, 4, 4, 4, 6], [0, 4, 4, 4, 4, 4, 4, 4, 0], [4, 4, 4, 4, 4, 4, 4, 4, 4], [8, 8, 8, 8, 4, 4, 8, 8, 8]]) np_test.assert_equal(removed, desired) def testLayersFrom(self): """ Tests makeLayersFrom(). """ # no extra layers layers = \ self.bound_1.makeLayersFrom(bound=[1,2], thick=1, nLayers=3, mask=5) layers.useInset(inset=self.bound_1.inset, mode='abs', expand=True) desired = numpy.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, 1, 1, 1, 1, 1, 1, 0], [0, 0, 2, 2, 2, 2, 2, 2, 0], [0, 0, 3, 3, 3, 3, 3, 3, 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, 0]]) np_test.assert_equal(layers.data[2:12, 3:12], desired) # thick = 2, no extra layers layers = \ self.bound_1.makeLayersFrom(bound=[1,2], thick=2, nLayers=2, mask=5) layers.useInset(inset=self.bound_1.inset, mode='abs', expand=True) desired = numpy.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, 1, 1, 1, 1, 1, 1, 0], [0, 0, 1, 1, 1, 1, 1, 1, 0], [0, 0, 2, 2, 2, 2, 2, 2, 0], [0, 0, 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]]) np_test.assert_equal(layers.data[2:12, 3:12], desired) # thick = 2, w extra layers layers = \ self.bound_1.makeLayersFrom(bound=[1,2], thick=2, nLayers=2, mask=5, nExtraLayers=1) layers.useInset(inset=self.bound_1.inset, mode='abs', expand=True) desired = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 2, 2, 2, 2, 2, 2, 0], [0, 0, 2, 2, 2, 2, 2, 2, 0], [0, 0, 3, 3, 3, 3, 3, 3, 0], [0, 0, 3, 3, 3, 3, 3, 3, 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]]) np_test.assert_equal(layers.data[2:12, 3:12], desired) # thick = 2, w extra layers layers = \ self.bound_1.makeLayersFrom(bound=[1,2], thick=3, nLayers=1, mask=[5, 6], nExtraLayers=1, extra=7) layers.useInset(inset=self.bound_1.inset, mode='abs', expand=True) desired = numpy.array([[1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 0], [2, 2, 2, 2, 2, 2, 2, 2, 2], [2, 2, 2, 2, 2, 2, 2, 2, 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, 0, 0, 0, 0, 0, 0]]) np_test.assert_equal(layers.data[2:12, 3:12], desired) def testLayersBetweenLabels(self): """ Tests different mask, boundary and extra regions assignments in makeLayersBetween(). Also checks size of the data array and offset. """ # no extra layers layers, width = \ self.bound_1.makeLayersBetween(bound_1=[1,2], bound_2=3, mask=5) desired = numpy.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, 1, 1, 1, 1, 1, 1, 0], [0, 0, 2, 2, 2, 2, 2, 2, 0], [0, 0, 3, 3, 3, 3, 3, 3, 0], [0, 0, 4, 4, 4, 4, 4, 4, 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]]) np_test.assert_equal(layers.data, desired[1:9, :]) layers.useInset(inset=self.bound_1.inset, mode='abs', expand=True) np_test.assert_almost_equal(width, 4) np_test.assert_equal(layers.data[2:12, 3:12], desired) # w extra layers on boundaries layers, width = self.bound_1.makeLayersBetween(bound_1=[1, 2], bound_2=3, mask=5, nExtraLayers=3) layers.useInset(inset=self.bound_1.inset, mode='abs', expand=True) desired = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 2, 2, 2, 2, 2, 2, 2, 0], [0, 3, 3, 3, 3, 3, 3, 3, 0], [0, 0, 4, 4, 4, 4, 4, 4, 0], [0, 0, 5, 5, 5, 5, 5, 5, 0], [0, 0, 6, 6, 6, 6, 6, 6, 0], [0, 0, 7, 7, 7, 7, 7, 7, 0], [0, 8, 8, 8, 8, 8, 8, 8, 0], [10, 9, 9, 9, 9, 9, 9, 9, 9], [0, 0, 0, 0, 0, 0, 0, 0, 0]]) np_test.assert_almost_equal(width, 4) np_test.assert_equal(layers.data[2:12, 3:12], desired) # w extra layers on boundaries and extra regions layers, width = self.bound_1.makeLayersBetween(bound_1=[1, 2], bound_2=3, mask=5, nExtraLayers=3, extra_1=7, extra_2=[8, 9]) layers.useInset(inset=self.bound_1.inset, mode='abs', expand=True) desired = numpy.array([[0, 1, 1, 1, 1, 1, 1, 1, 1], [0, 2, 2, 2, 2, 2, 2, 2, 0], [0, 3, 3, 3, 3, 3, 3, 3, 0], [0, 0, 4, 4, 4, 4, 4, 4, 0], [0, 0, 5, 5, 5, 5, 5, 5, 0], [0, 0, 6, 6, 6, 6, 6, 6, 0], [0, 0, 7, 7, 7, 7, 7, 7, 0], [0, 8, 8, 8, 8, 8, 8, 8, 0], [10, 9, 9, 9, 9, 9, 9, 9, 9], [0, 10, 10, 10, 10, 10, 10, 10, 10]]) np_test.assert_almost_equal(width, 4) np_test.assert_equal(layers.data[2:12, 3:12], desired) # w multi id mask and extra layers on boundaries and extra regions layers, width = self.bound_1.makeLayersBetween(bound_1=numpy.array( [1, 2]), bound_2=3, mask=[5, 6], nExtraLayers=3, extra_1=7, extra_2=[8, 9], maxDistance=5.5) layers.useInset(inset=self.bound_1.inset, mode='abs', expand=True) desired = numpy.array([[1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 2, 2, 2, 2, 2, 2, 2, 0], [0, 3, 3, 3, 3, 3, 3, 3, 0], [0, 4, 4, 4, 4, 4, 4, 4, 0], [5, 5, 5, 5, 5, 5, 5, 5, 5], [6, 6, 6, 6, 6, 6, 6, 6, 6], [0, 7, 7, 7, 7, 7, 7, 7, 0], [0, 8, 8, 8, 8, 8, 8, 8, 0], [9, 9, 9, 9, 9, 9, 9, 9, 9], [10, 10, 10, 10, 10, 10, 10, 10, 10]]) np_test.assert_almost_equal(width, 4) np_test.assert_equal(layers.data[2:12, 3:12], desired) def testLayersBetween(self): """ Tests different nLayers and width arguments in makeLayersBetween(). """ # w nLayers, no extra layers layers, width = self.bound_1.makeLayersBetween(nLayers=2, bound_1=[1, 2], bound_2=3, mask=5) layers.useInset(inset=self.bound_1.inset, mode='abs', expand=True) desired = numpy.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, 1, 1, 1, 1, 1, 1, 0], [0, 0, 1, 1, 1, 1, 1, 1, 0], [0, 0, 2, 2, 2, 2, 2, 2, 0], [0, 0, 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]]) np_test.assert_almost_equal(width, 4) np_test.assert_equal(layers.data[2:12, 3:12], desired) # w multi id mask and extra layers on boundaries and extra regions layers, width = self.bound_1.makeLayersBetween(nLayers=2, bound_1=numpy.array( [1, 2]), bound_2=3, mask=[5, 6], nExtraLayers=1, extra_1=7, extra_2=[8, 9], maxDistance=5.5) layers.useInset(inset=self.bound_1.inset, mode='abs', expand=True) desired = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 0], [0, 2, 2, 2, 2, 2, 2, 2, 0], [2, 2, 2, 2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3, 3, 3, 3], [0, 3, 3, 3, 3, 3, 3, 3, 0], [0, 4, 4, 4, 4, 4, 4, 4, 0], [4, 4, 4, 4, 4, 4, 4, 4, 4], [0, 0, 0, 0, 0, 0, 0, 0, 0]]) np_test.assert_almost_equal(width, 4) np_test.assert_equal(layers.data[2:12, 3:12], desired) # w multi id mask and extra layers on boundaries and extra regions # and layers thinner than 1 layers, width = self.bound_1.makeLayersBetween(nLayers=8, bound_1=numpy.array( [1, 2]), bound_2=3, mask=[5, 6], nExtraLayers=2, extra_1=7, extra_2=[8, 9], maxDistance=5.5) layers.useInset(inset=self.bound_1.inset, mode='abs', expand=True) # possibly ambiguous layer assignment np_test.assert_equal( ((layers.data[4, 4:11] >= 1) & (layers.data[4, 4:11] <= 2)).all(), True) np_test.assert_equal(((layers.data[9, 4:11] >= 11) & (layers.data[9, 4:11] <= 12)).all(), True) # w multi id mask and extra layers on boundaries and extra regions layers, width = self.bound_1.makeLayersBetween(nLayers=2, bound_1=numpy.array( [1, 2]), bound_2=3, mask=[5, 6], nExtraLayers=2, extra_1=7, extra_2=[8, 9], maxDistance=5.5) layers.useInset(inset=self.bound_1.inset, mode='abs', expand=True) desired = numpy.array([[1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 2, 2, 2, 2, 2, 2, 2, 0], [0, 2, 2, 2, 2, 2, 2, 2, 0], [0, 3, 3, 3, 3, 3, 3, 3, 0], [3, 3, 3, 3, 3, 3, 3, 3, 3], [4, 4, 4, 4, 4, 4, 4, 4, 4], [0, 4, 4, 4, 4, 4, 4, 4, 0], [0, 5, 5, 5, 5, 5, 5, 5, 0], [5, 5, 5, 5, 5, 5, 5, 5, 5], [6, 6, 6, 6, 6, 6, 6, 6, 6]]) np_test.assert_almost_equal(width, 4) np_test.assert_equal(layers.data[2:12, 3:12], desired) # w maxDistance and no fill data = numpy.array([[1, 1, 1, 1, 1, 1, 1], [1, 1, 5, 5, 5, 1, 1], [5, 5, 5, 5, 5, 5, 5], [5, 5, 5, 5, 5, 5, 5], [3, 3, 5, 5, 5, 3, 3], [3, 3, 7, 7, 7, 7, 7]]) seg = Segment(data) layers, width = seg.makeLayersBetween(nLayers=2, bound_1=numpy.array([1]), bound_2=[3, 7], mask=[5], nExtraLayers=1, maxDistance=4, fill=False) desired = numpy.array([[0, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 1, 1], [2, 2, 2, 0, 2, 2, 2], [3, 3, 3, 0, 3, 3, 3], [4, 4, 0, 0, 0, 4, 4], [0, 0, 0, 0, 0, 0, 0]]) np_test.assert_equal(layers.data, desired) np_test.assert_almost_equal(width, 2.) # w maxDistance, with fill, mode median layers, width = seg.makeLayersBetween(nLayers=2, bound_1=numpy.array([1]), bound_2=[3, 7], mask=[5], nExtraLayers=1, between='median', maxDistance=3, fill=True) desired = numpy.array([[0, 1, 1, 1, 1, 1, 0], [1, 1, 2, 2, 2, 1, 1], [2, 2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3, 3], [4, 4, 3, 3, 3, 4, 4], [0, 4, 4, 4, 4, 4, 0]]) np_test.assert_equal(layers.data, desired) np_test.assert_almost_equal(width, 2.) # w maxDistance, with fill, mode mean layers, width = seg.makeLayersBetween(nLayers=2, bound_1=numpy.array([1]), bound_2=[3, 7], mask=[5], nExtraLayers=1, between='mean', maxDistance=3, fill=True) desired = numpy.array([[0, 1, 1, 1, 1, 1, 0], [1, 1, 2, 2, 2, 1, 1], [2, 2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3, 3], [4, 4, 3, 3, 3, 4, 4], [0, 4, 4, 4, 4, 4, 0]]) np_test.assert_equal(layers.data, desired) np_test.assert_almost_equal( width, ((4 * 3 + 2 * numpy.sqrt(17) + numpy.sqrt(20)) / 7. - 1)) # funny shaped mask segments_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]]) mask_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=segments_data) layers, width = seg.makeLayersBetween(bound_1=3, bound_2=2, mask=mask_data, between='min') desired_layers = numpy.array([[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 2], [0, 0, 0, 0, 0, 3], [0, 0, 0, 3, 3, 3], [0, 0, 0, 0, 0, 0]]) np_test.assert_almost_equal(width, numpy.sqrt(17) - 1) np_test.assert_equal(layers.data, desired_layers) def makeHorizontalLayers(self, item): if item == 1: data = numpy.array([[0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 0], [0, 2, 2, 2, 2, 0], [0, 3, 3, 3, 3, 0], [0, 4, 4, 4, 4, 0], [0, 5, 5, 5, 5, 0]]) segments = Segment(data) region = numpy.zeros(shape=data.shape) region[:, 0] = 5 return segments, region elif item == 2: data = numpy.array([[0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 0], [0, 2, 2, 2, 0, 0], [0, 0, 3, 3, 3, 0], [0, 0, 4, 4, 4, 0], [0, 5, 5, 5, 5, 0]]) layers = Segment(data) return layers def makeSlantedLayers(self, item): if item == 1: data = numpy.array([[0, 0, 0, 0, 1, 2], [0, 0, 0, 1, 2, 2], [0, 0, 1, 2, 2, 3], [0, 1, 2, 2, 3, 3], [5, 2, 2, 3, 3, 0], [5, 2, 3, 3, 0, 0], [5, 5, 5, 3, 0, 0]]) segments = Segment(data) return segments elif item == 2: data = numpy.array([[5, 5, 5, 1, 0, 0], [5, 5, 1, 1, 2, 0], [5, 5, 1, 2, 2, 3], [5, 1, 1, 2, 3, 3], [0, 1, 2, 2, 3, 6], [0, 0, 2, 3, 3, 6], [0, 0, 0, 3, 6, 6]]) layers = Segment(data) return layers def testElementDistanceToRegion(self): """ Tests elementDistanceToRegion """ # make layers hor_seg, hor_reg = self.makeHorizontalLayers(1) sl_seg = self.makeSlantedLayers(1) sl_reg = sl_seg.data == 5 # horizontal, euclidean dist = hor_seg.elementDistanceToRegion(ids=[2, 3, 4, 5], region=hor_reg) desired = numpy.array([[-1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1], [-1, 1., 2, 3, 4, -1], [-1, 1, 2, 3, 4, -1], [-1, 1, 2, 3, 4, -1], [-1, 1, 2, 3, 4, -1]]) np_test.assert_almost_equal(dist, desired) # slanted, euclidean dist = sl_seg.elementDistanceToRegion(ids=[1, 2, 3], region=sl_reg) def sq(x): return numpy.sqrt(x) desired = numpy.array([[-1, -1, -1, -1, sq(32), sq(41)], [-1, -1, -1, sq(18), sq(25), sq(34)], [-1, -1, sq(8), sq(13), sq(20), sq(25)], [-1, sq(2), sq(5), sq(10), sq(13), sq(18)], [-1, 1, 2, sq(5), sq(8), -1], [-1, 1, 1, sq(2), -1, -1], [-1, -1, -1, 1, -1, -1]]) np_test.assert_almost_equal(dist, desired) # slanted, geodesic conn 1 seg = self.makeSlantedLayers(1) dist = seg.elementDistanceToRegion(ids=[2, 3], region=(seg.data == 5), metric='geodesic', connectivity=1) desired = numpy.array([[-1, -1, -1, -1, -1, 9], [-1, -1, -1, -1, 7, 8], [-1, -1, -1, 5, 6, 7], [-1, -1, 3, 4, 5, 6], [-1, 1, 2, 3, 4, -1], [-1, 1, 1, 2, -1, -1], [-1, -1, -1, 1, -1, -1]]) np_test.assert_equal(dist, desired) # slanted, geodesic conn 2 seg = self.makeSlantedLayers(1) dist = seg.elementDistanceToRegion(ids=[1, 2, 3], region=(seg.data == 5), metric='geodesic', connectivity=2) desired = numpy.array([[-1, -1, -1, -1, 4, 5], [-1, -1, -1, 3, 4, 5], [-1, -1, 2, 3, 4, 4], [-1, 1, 2, 3, 3, 3], [-1, 1, 2, 2, 2, -1], [-1, 1, 1, 1, -1, -1], [-1, -1, -1, 1, -1, -1]]) np_test.assert_equal(dist, desired) # slanted, euclidean-geodesic seg = self.makeSlantedLayers(1) dist = seg.elementDistanceToRegion(ids=[1, 2, 3], region=(seg.data == 5), metric='euclidean-geodesic') sq2 = numpy.sqrt(2) desired = numpy.array( [[-1, -1, -1, -1, 4 * sq2, 1 + 4 * sq2], [-1, -1, -1, 3 * sq2, 1 + 3 * sq2, 2 + 3 * sq2], [-1, -1, 2 * sq2, 1 + 2 * sq2, 2 + 2 * sq2, 1 + 3 * sq2], [-1, sq2, 1 + sq2, 2 + sq2, 1 + 2 * sq2, 3 * sq2], [-1, 1, 2, 1 + sq2, 2 * sq2, -1], [-1, 1, 1, sq2, -1, -1], [-1, -1, -1, 1, -1, -1]]) np_test.assert_almost_equal(dist, desired) def testElementDistanceToRim(self): """ Tests elementDistanceToRim() """ hor_cl = self.makeHorizontalLayers(2) sla_cl = self.makeSlantedLayers(2) def sq(x): return numpy.sqrt(x) # horizontal, euclidean, out dist = hor_cl.elementDistanceToRim(ids=[2, 3, 4], metric='euclidean', rimId=0, rimLocation='out', rimConnectivity=1) desired = numpy.array([[-1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1], [-1, 1, numpy.sqrt(2), 1, -1, -1], [-1, -1, 1, numpy.sqrt(2), 1, -1], [-1, -1, 1, 2, 1, -1], [-1, -1, -1, -1, -1, -1]]) np_test.assert_almost_equal(dist, desired) # horizontal, euclidean, in dist = hor_cl.elementDistanceToRim(ids=[2, 3, 4], metric='euclidean', rimId=0, rimLocation='in', rimConnectivity=1) desired = numpy.array([[-1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1], [-1, 0, 1, 0, -1, -1], [-1, -1, 0, 1, 0, -1], [-1, -1, 0, 1, 0, -1], [-1, -1, -1, -1, -1, -1]]) np_test.assert_almost_equal(dist, desired) # horizontal, euclidean, in, conn -1 dist = hor_cl.elementDistanceToRim(ids=[2, 3, 4], metric='euclidean', rimId=0, rimLocation='in', rimConnectivity=-1) desired = numpy.array([[-1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1], [-1, 0, 0, 0, -1, -1], [-1, -1, 0, 0, 0, -1], [-1, -1, 0, 1, 0, -1], [-1, -1, -1, -1, -1, -1]]) np_test.assert_almost_equal(dist, desired) # slanted, euclidean, out dist = sla_cl.elementDistanceToRim(ids=[1, 2, 3], metric='euclidean', rimId=0, rimLocation='out', rimConnectivity=1) desired = numpy.array([[-1, -1, -1, 1, -1, -1], [-1, -1, sq(5), sq(2), 1, -1], [-1, -1, sq(8), sq(5), sq(2), 1], [-1, sq(2), sq(5), sq(8), sq(5), 2], [-1, 1, sq(2), sq(5), sq(8), -1], [-1, -1, 1, sq(2), sq(5), -1], [-1, -1, -1, 1, -1, -1]]) np_test.assert_almost_equal(dist, desired) # slanted, geodesic, conn 1, out dist = sla_cl.elementDistanceToRim(ids=[1, 2, 3], metric='geodesic', connectivity=1, rimId=0, rimLocation='out', rimConnectivity=1) desired = numpy.array([[-1, -1, -1, 1, -1, -1], [-1, -1, 3, 2, 1, -1], [-1, -1, 4, 3, 2, 1], [-1, 2, 3, 4, 3, 2], [-1, 1, 2, 3, 4, -1], [-1, -1, 1, 2, 3, -1], [-1, -1, -1, 1, -1, -1]]) np_test.assert_equal(dist, desired) # slanted, geodesic, conn 1, in dist = sla_cl.elementDistanceToRim(ids=[1, 2, 3], metric='geodesic', connectivity=1, rimId=0, rimLocation='in', rimConnectivity=1) desired = numpy.array([[-1, -1, -1, 0, -1, -1], [-1, -1, 2, 1, 0, -1], [-1, -1, 3, 2, 1, 0], [-1, 1, 2, 3, 2, 1], [-1, 0, 1, 2, 3, -1], [-1, -1, 0, 1, 2, -1], [-1, -1, -1, 0, -1, -1]]) np_test.assert_equal(dist, desired) # slanted, geodesic, conn 2, in dist = sla_cl.elementDistanceToRim(ids=[1, 2, 3], metric='geodesic', connectivity=2, rimId=0, rimLocation='in', rimConnectivity=1) desired = numpy.array([[-1, -1, -1, 0, -1, -1], [-1, -1, 1, 1, 0, -1], [-1, -1, 2, 1, 1, 0], [-1, 1, 1, 2, 1, 1], [-1, 0, 1, 1, 2, -1], [-1, -1, 0, 1, 1, -1], [-1, -1, -1, 0, -1, -1]]) np_test.assert_equal(dist, desired) # slanted, euclidean-geodesic, in dist = sla_cl.elementDistanceToRim(ids=[1, 2, 3], metric='euclidean-geodesic', rimId=0, rimLocation='in', rimConnectivity=1) desired = numpy.array([[-1, -1, -1, 0, -1, -1], [-1, -1, sq(2), 1, 0, -1], [-1, -1, 1 + sq(2), sq(2), 1, 0], [-1, 1, sq(2), 1 + sq(2), sq(2), 1], [-1, 0, 1, sq(2), 1 + sq(2), -1], [-1, -1, 0, 1, sq(2), -1], [-1, -1, -1, 0, -1, -1]]) np_test.assert_equal(dist, desired) def testDistanceFromOrigin(self): """ Tests distanceFromOrigin() """ hor_cl = self.makeHorizontalLayers(2) origins = {2: [2, 2], 3: [3, 3], 4: [4, 3]} # horizontal, euclidean dist = hor_cl.distanceFromOrigin(origins=origins, metric='euclidean') desired = numpy.array([[-1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1], [-1, 1, 0, 1, -1, -1], [-1, -1, 1, 0, 1, -1], [-1, -1, 1, 0, 1, -1], [-1, -1, -1, -1, -1, -1]]) np_test.assert_almost_equal(dist, desired) sla_cl = self.makeSlantedLayers(2) origins = {1: [2, 2], 2: [3, 3], 3: [4, 4]} def sq(x): return numpy.sqrt(x) # slanted, euclidean dist = sla_cl.distanceFromOrigin(origins=origins, metric='euclidean') desired = numpy.array([[-1, -1, -1, sq(5), -1, -1], [-1, -1, 1, sq(2), sq(5), -1], [-1, -1, 0, 1, sq(2), sq(5)], [-1, sq(2), 1, 0, 1, sq(2)], [-1, sq(5), sq(2), 1, 0, -1], [-1, -1, sq(5), sq(2), 1, -1], [-1, -1, -1, sq(5), -1, -1]]) np_test.assert_almost_equal(dist, desired) # slanted, geodesic conn 1 dist = sla_cl.distanceFromOrigin(origins=origins, metric='geodesic', connectivity=1) desired = numpy.array([[-1, -1, -1, 3, -1, -1], [-1, -1, 1, 2, 3, -1], [-1, -1, 0, 1, 2, 3], [-1, 2, 1, 0, 1, 2], [-1, 3, 2, 1, 0, -1], [-1, -1, 3, 2, 1, -1], [-1, -1, -1, 3, -1, -1]]) np_test.assert_equal(dist, desired) # slanted, geodesic conn 2 dist = sla_cl.distanceFromOrigin(origins=origins, metric='geodesic', connectivity=2) desired = numpy.array([[-1, -1, -1, 2, -1, -1], [-1, -1, 1, 1, 2, -1], [-1, -1, 0, 1, 1, 2], [-1, 1, 1, 0, 1, 1], [-1, 2, 1, 1, 0, -1], [-1, -1, 2, 1, 1, -1], [-1, -1, -1, 2, -1, -1]]) np_test.assert_equal(dist, desired) # slanted, euclidean-geodesic dist = sla_cl.distanceFromOrigin(origins=origins, metric='euclidean-geodesic') desired = numpy.array([[-1, -1, -1, 1 + sq(2), -1, -1], [-1, -1, 1, sq(2), 1 + sq(2), -1], [-1, -1, 0, 1, sq(2), 1 + sq(2)], [-1, sq(2), 1, 0, 1, sq(2)], [-1, 1 + sq(2), sq(2), 1, 0, -1], [-1, -1, 1 + sq(2), sq(2), 1, -1], [-1, -1, -1, 1 + sq(2), -1, -1]]) np_test.assert_almost_equal(dist, desired) def testGetStructureFootprint(self): """ Tests getStructureFootprint() """ # 2d conn 1 seg = Segment(numpy.zeros(shape=(3, 4))) se, foot = seg.getStructureFootprint(connectivity=1) desired = scipy.ndimage.generate_binary_structure(2, 1) np_test.assert_equal(foot, desired) desired = desired.astype(int) desired[1, 1] = 0 np_test.assert_equal(se, desired) # 3d conn 2 seg = Segment(numpy.zeros(shape=(3, 4, 5))) se, foot = seg.getStructureFootprint(connectivity=2) desired = scipy.ndimage.generate_binary_structure(3, 2) np_test.assert_equal(foot, desired) desired = desired.astype(int) desired[1, 1, 1] = 0 np_test.assert_equal(se, desired) # 3d conn -1 (3) seg = Segment(numpy.zeros(shape=(3, 4, 5))) se, foot = seg.getStructureFootprint(connectivity=-1) desired = scipy.ndimage.generate_binary_structure(3, 3) np_test.assert_equal(foot, desired) desired = desired.astype(int) desired[1, 1, 1] = 0 np_test.assert_equal(se, desired) # 2d conn [2] seg = Segment(numpy.zeros(shape=(3, 4))) se, foot = seg.getStructureFootprint(connectivity=[2]) desired = scipy.ndimage.generate_binary_structure(2, 2) np_test.assert_equal(foot, desired) desired = desired.astype(int) * numpy.sqrt(2) desired[1, 1] = 0 np_test.assert_almost_equal(se, desired) # 3d conn [1,2,3] seg = Segment(numpy.zeros(shape=(3, 4, 5))) se, foot = seg.getStructureFootprint(connectivity=[3, 1, 2]) desired = scipy.ndimage.generate_binary_structure(3, 3) np_test.assert_equal(foot, desired) desired = numpy.array([[[3., 2, 3], [2, 1, 2], [3, 2, 3]], [[2, 1, 2], [1, 0, 1], [2, 1, 2]], [[3, 2, 3], [2, 1, 2], [3, 2, 3]]]) np_test.assert_almost_equal(se**2, desired) def testElementGeodesicDistanceToRegion(self): """ Tests elementGeodesicDistanceToRegion() """ # connectivity 1, horizontal seg, reg = self.makeHorizontalLayers(1) dist = seg.elementGeodesicDistanceToRegion(ids=[1, 2, 4, 5], region=reg > 0, connectivity=1) desired = numpy.array([[-1., -1, -1, -1, -1, -1], [-1, 1, 2, 3, 4, -1], [-1, 1, 2, 3, 4, -1], [-1, -1, -1, -1, -1, -1], [-1, 1, 2, 3, 4, -1], [-1, 1, 2, 3, 4, -1]]) np_test.assert_equal(dist, desired) # connectivity 1, slanted seg = self.makeSlantedLayers(2) reg = seg.data == 0 reg[1, :] = True dist = seg.elementGeodesicDistanceToRegion(ids=[1, 2, 3], region=reg, connectivity=1) desired = numpy.array([[-1, -1, -1, 1, -1, -1], [-1, -1, 0, 0, 0, -1], [-1, -1, 1, 1, 1, 1], [-1, 2, 2, 2, 3, 2], [-1, 1, 2, 3, 4, -1], [-1, -1, 1, 2, 3, -1], [-1, -1, -1, 1, -1, -1]]) np_test.assert_equal(dist, desired) # connectivity 1, slanted seg = self.makeSlantedLayers(1) dist = seg.elementGeodesicDistanceToRegion(ids=[2, 3], region=(seg.data == 5), connectivity=1) desired = numpy.array([[-1, -1, -1, -1, -1, 9], [-1, -1, -1, -1, 7, 8], [-1, -1, -1, 5, 6, 7], [-1, -1, 3, 4, 5, 6], [-1, 1, 2, 3, 4, -1], [-1, 1, 1, 2, -1, -1], [-1, -1, -1, 1, -1, -1]]) np_test.assert_equal(dist, desired) # connectivity 2, slanted seg = self.makeSlantedLayers(1) dist = seg.elementGeodesicDistanceToRegion(ids=[1, 2, 3], region=(seg.data == 5), connectivity=2) desired = numpy.array([[-1, -1, -1, -1, 4, 5], [-1, -1, -1, 3, 4, 5], [-1, -1, 2, 3, 4, 4], [-1, 1, 2, 3, 3, 3], [-1, 1, 2, 2, 2, -1], [-1, 1, 1, 1, -1, -1], [-1, -1, -1, 1, -1, -1]]) np_test.assert_equal(dist, desired) # connectivity [1,2], slanted seg = self.makeSlantedLayers(1) dist = seg.elementGeodesicDistanceToRegion(ids=[1, 2, 3], region=(seg.data == 5), connectivity=[1, 2]) sq2 = numpy.sqrt(2) desired = numpy.array( [[-1, -1, -1, -1, 4 * sq2, 1 + 4 * sq2], [-1, -1, -1, 3 * sq2, 1 + 3 * sq2, 2 + 3 * sq2], [-1, -1, 2 * sq2, 1 + 2 * sq2, 2 + 2 * sq2, 1 + 3 * sq2], [-1, sq2, 1 + sq2, 2 + sq2, 1 + 2 * sq2, 3 * sq2], [-1, 1, 2, 1 + sq2, 2 * sq2, -1], [-1, 1, 1, sq2, -1, -1], [-1, -1, -1, 1, -1, -1]]) np_test.assert_almost_equal(dist, desired) 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)])
def getSegmentDensity(self, segment=None, ids=None, offset=None): """ Calculates basic statistics for the grey values (densitity) of segmented data. The statistical analysis is done for each segment separately, segment density means, background and total. This method returns four corresponding instances of Statistics. Each instance contains attributes mean, std, min, max, minPos and maxPos. The type of these attributes for different statistisc is as follows: - each segment of data separately: ndarray indexed by ids, where 0-th element is the results for all segments taken together - segment density means: numbers - background: numbers - total: numbers First parses the segment and ids arguments and sets appropriate attributes (self.segment, self.ids, self.segmentOffset). Arrays self.data and self.segment (set from segment) are intersected taking into account self.segmentOffset The statistical analysis is done on the intersection in all cases. Arguments: - segments: segmented image, either as an Segment object or an ndarray - ids: array (or other iterable) of segment ids. If None, the analysis is done for segment.ids. Returns: - Statistics objects containg results for density, meanDensity, background, total (in this order). Each of them has attributes: mean, std, min, max, minPos, maxPos. """ # make Segment instance and set ids from pyto.segmentation.segment import Segment if isinstance(segment, numpy.ndarray): segment = Segment(data=segment, ids=ids, copy=False) if ids is None: ids = segment.ids else: ids = numpy.asarray(ids) # get an inset of self.data corresponding to segment restricted = self.usePositioning(image=segment, new=True) image_inset = restricted.data # get density for all segments density = self.getSegmentStats(data=image_inset, segment=segment.data, ids=ids, offset=offset) # statistics on segment density means try: maxId = ids.max() except ValueError: maxId = 0 idMask = numpy.zeros(maxId + 1, dtype=int) idMask[ids] = 1 meanDensity = self.getSegmentStats(data=density.mean, segment=idMask, ids=1) # get background density # bkg = numpy.where(self.segmentInset==0, 1, 0) background = self.getSegmentStats(data=image_inset, segment=segment.data, ids=0, offset=offset) # get total density of the inset all = numpy.ones(shape=segment.data.shape, dtype=int) total = self.getSegmentStats(data=image_inset, segment=all, ids=1, offset=offset) # return all stats return density, meanDensity, background, total
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)])
def testFindNonUnique(self): """ Tests findNonUnique() """ data = numpy.array([[0, 2, 3, 4], [2, 3, 3, 4], [4, 5, 5, 7]]) # unique seg = Segment(data=data, ids=[3, 5, 7]) nonu = seg.findNonUnique() np_test.assert_equal(nonu['many'], []) np_test.assert_equal(nonu['empty'], []) # non unique seg = Segment(data=data) seg.setIds(ids=[2, 3, 4, 5, 6, 7, 8]) nonu = seg.findNonUnique() np_test.assert_equal(nonu['many'], [2, 4]) np_test.assert_equal(nonu['empty'], [6, 8]) # non-unique, some ids excluded seg = Segment(data=data) seg.setIds(ids=[3, 4, 5, 6, 7]) nonu = seg.findNonUnique() np_test.assert_equal(nonu['many'], [4]) np_test.assert_equal(nonu['empty'], [6])