def _re_bound(self,plot_bounding_box,mat,box,density): # CEBHACKALERT: for Julien... # If plot_bounding_box is that of a Sheet, it will already have been # setup so that the density in the x direction and the density in the # y direction are equal. # If plot_bounding_box comes from elsewhere (i.e. you create it from # arbitrary bounds), it might need to be adjusted to ensure the density # in both directions is the same (see Sheet.__init__()). I don't know where # you want to do that; presumably the code should be common to Sheet and # where it's used in the plotting? # # It's possible we can move some of the functionality # into SheetCoordinateSystem. if plot_bounding_box.containsbb_exclusive(box): ct = SheetCoordinateSystem(plot_bounding_box,density,density) new_mat = np.zeros(ct.shape,dtype=np.float) r1,r2,c1,c2 = Slice(box,ct) new_mat[r1:r2,c1:c2] = mat else: scs = SheetCoordinateSystem(box,density,density) s=Slice(plot_bounding_box,scs) s.crop_to_sheet(scs) new_mat = s.submatrix(mat) return new_mat
def test_bounds2slice(self): # test that if you ask to slice the matrix with the sheet's BoundingBox, you # get back the whole matrix sheet_bb = BoundingBox(points=((-0.5, -0.5), (0.5, 0.5))) ct = SheetCoordinateSystem(sheet_bb, 10) slice_ = Slice(sheet_bb, ct) true_slice = (0, 10, 0, 10 ) # inclusive left boundary, exclusive right boundary self.assertEqual(tuple(slice_.tolist()), true_slice) # for the following tests, the values have been all computed by hand and then # tested (by JC). The boundingbox and density tested have been chosen randomly, # then drawn to get the slice from it. # Test with 20 density. ct = SheetCoordinateSystem(sheet_bb, 20, 20) bb = BoundingBox(points=((-0.05, -0.20), (0.20, 0.05))) slice_ = Slice(bb, ct) true_slice = (9, 14, 9, 14) self.assertEqual(tuple(slice_.tolist()), true_slice) bb = BoundingBox(points=((-0.40, 0), (-0.30, 0.30))) slice_ = Slice(bb, ct) true_slice = (4, 10, 2, 4) self.assertEqual(tuple(slice_.tolist()), true_slice) bb = BoundingBox(points=((0.15, 0.10), (0.30, 0.30))) slice_ = Slice(bb, ct) true_slice = (4, 8, 13, 16) self.assertEqual(tuple(slice_.tolist()), true_slice) bb = BoundingBox(points=((-0.05, -0.45), (0.10, -0.25))) slice_ = Slice(bb, ct) true_slice = (15, 19, 9, 12) self.assertEqual(tuple(slice_.tolist()), true_slice) # test with 7 density sheet. bb = BoundingBox(points=((-0.5 + 2.0 / 7.0, 0.5 - 2.0 / 7.0), (-0.5 + 4.0 / 7.0, 0.5))) ct = SheetCoordinateSystem(sheet_bb, 7) slice_ = Slice(bb, ct) true_slice = (0, 2, 2, 4) self.assertEqual(tuple(slice_.tolist()), true_slice) #(4x4 matrix) ct = SheetCoordinateSystem(BoundingBox(radius=0.2), xdensity=10, ydensity=10) test_bounds = BoundingBox(radius=0.1) slice_ = Slice(test_bounds, ct) r1, r2, c1, c2 = slice_ self.assertEqual((r1, r2, c1, c2), (1, 3, 1, 3))
def makeBox(self): self.box = BoundingBox(points=((self.left,self.bottom), (self.right,self.top))) self.ct = SheetCoordinateSystem(self.box,self.density,self.density) # float bounds for matrix coordinates: these # values are actually outside the matrix self.rbound = self.density*(self.top-self.bottom) self.cbound = self.density*(self.right-self.left) #self.cbound = int(self.density*(self.right-self.left)) / float((self.right-self.left)) #self.rbound = int(self.density*(self.top-self.bottom)) / float((self.top-self.bottom)) # CEBALERT: this is supposed to be a small distance D = 0.00001 # Sheet values around the edge of the BoundingBox self.just_in_right_x = self.right - D self.just_in_bottom_y = self.bottom + D self.just_out_top_y = self.top + D self.just_out_left_x = self.left - D # Matrix values around the edge of the matrix self.just_out_right_idx = self.rbound + D self.just_out_bottom_idx = self.cbound + D self.just_out_top_idx = 0.0 - D self.just_out_left_idx = 0.0 - D
def test_coordinate_position(self): """ these tests duplicate some of the earlier ones, except these use a matrix with non-integer (right-left) and (top-bottom). This is an important test case for the definition of density; without it, the tests above could be passed by a variety of sheet2matrix, bounds2shape functions, etc. CEBALERT: transfer the box to TestBox3Coordinates and have these tests run in the framework. """ l, b, r, t = (-0.8, -0.8, 0.8, 0.8) # mimics that a sheet recalculates its density) density = int(16 * (r - l)) / float(r - l) bounds = BoundingBox(points=((l, b), (r, t))) ct = SheetCoordinateSystem(bounds, density, density) self.assertEqual(ct.sheet2matrixidx(0.8, 0.8), (0, 24 + 1)) self.assertEqual(ct.sheet2matrixidx(0.0, 0.0), (12, 12)) self.assertEqual(ct.sheet2matrixidx(-0.8, -0.8), (24 + 1, 0)) self.assertEqual(ct.matrixidx2sheet(24, 0), (((r - l) / int(density * (r - l)) / 2.0) + l, (t - b) / int(density * (t - b)) / 2.0 + b)) self.assertEqual(ct.matrixidx2sheet(0, 0), (((r - l) / int(density * (r - l)) / 2.0) + l, (t - b) / int(density * (t - b)) * (int(density * (t - b)) - 0.5) + b)) x, y = ct.matrixidx2sheet(0, 0) self.assertTrue(bounds.contains(x, y)) self.assertEqual((0, 0), ct.sheet2matrixidx(x, y)) x, y = ct.matrixidx2sheet(25, 25) self.assertFalse(bounds.contains(x, y)) self.assertNotEqual((24, 24), ct.sheet2matrixidx(x, y)) x, y = ct.matrixidx2sheet(0, 24) self.assertTrue(bounds.contains(x, y)) self.assertEqual((0, 24), ct.sheet2matrixidx(x, y)) x, y = ct.matrixidx2sheet(24, 0) self.assertTrue(bounds.contains(x, y)) self.assertEqual((24, 0), ct.sheet2matrixidx(x, y))
def test_slice2bounds(self): # test that if you ask to slice the matrix with the sheet's BoundingBox, you # get back the whole matrix # (I chose to use a 7 density, I don't know why I like 7 so much, it is kind of mystical) sheet_bb = BoundingBox(points=((-0.5,-0.5),(0.5,0.5))) ct = SheetCoordinateSystem(sheet_bb,7) slice_ = (0,7,0,7) bounds = BoundingBox(points=Slice._slicespec2boundsspec(slice_,ct)) true_bounds_lbrt = (-0.5,-0.5,0.5,0.5) for a,b in zip(bounds.lbrt(),true_bounds_lbrt): self.assertAlmostEqual(a,b) # for the following tests, the values have been all computed # by hand and then tested (by JC). The boundingbox and density # tested have been chosen randomly, then drawn to get the slice # from it. # Test for 10 density ct = SheetCoordinateSystem(sheet_bb,10) slice_ = (0,9,1,5) bounds = BoundingBox(points=Slice._slicespec2boundsspec(slice_,ct)) true_bounds_lbrt = (-0.4,-0.4,0,0.5) for a,b in zip(bounds.lbrt(),true_bounds_lbrt): self.assertAlmostEqual(a,b) slice_ = (2,3,7,10) bounds = BoundingBox(points=Slice._slicespec2boundsspec(slice_,ct)) true_bounds_lbrt = (0.2,0.2,0.5,0.3) for a,b in zip(bounds.lbrt(),true_bounds_lbrt): self.assertAlmostEqual(a,b) # Test for 7 density ct = SheetCoordinateSystem(sheet_bb,7) slice_ = (3,7,2,5) bounds = BoundingBox(points=Slice._slicespec2boundsspec(slice_,ct)) true_bounds_lbrt = (-0.5+2.0/7.0,-0.5,-0.5+5.0/7.0,0.5-3.0/7.0) for a,b in zip(bounds.lbrt(),true_bounds_lbrt): self.assertAlmostEqual(a,b) slice_ = (2,6,0,1) bounds = BoundingBox(points=Slice._slicespec2boundsspec(slice_,ct)) true_bounds_lbrt = (-0.5,0.5-6.0/7.0,-0.5+1.0/7.0,0.5-2.0/7.0) for a,b in zip(bounds.lbrt(),true_bounds_lbrt): self.assertAlmostEqual(a,b) # Test for 25 density ct = SheetCoordinateSystem(sheet_bb,25) slice_ = (0,25,4,10) bounds = BoundingBox(points=Slice._slicespec2boundsspec(slice_,ct)) true_bounds_lbrt = (-0.5+4.0/25.0,-0.5,-0.5+10.0/25.0,0.5) for a,b in zip(bounds.lbrt(),true_bounds_lbrt): self.assertAlmostEqual(a,b) slice_ = (7,18,3,11) bounds = BoundingBox(points=Slice._slicespec2boundsspec(slice_,ct)) true_bounds_lbrt = (-0.5+3.0/25.0,0.5-18.0/25.0,-0.5+11.0/25.0,0.5-7.0/25.0) for a,b in zip(bounds.lbrt(),true_bounds_lbrt): self.assertAlmostEqual(a,b)
def test_slice2bounds_bounds2slice(self): bb = BoundingBox(points=((-0.5,-0.5),(0.5,0.5))) ct = SheetCoordinateSystem(bb,10) slice_ =(0,3,7,8) bounds = BoundingBox(points=Slice._slicespec2boundsspec(slice_,ct)) test_slice = Slice(bounds,ct) for a,b in zip(slice_,test_slice): self.assertEqual(a,b) slice_ =(4,7,8,10) bounds = BoundingBox(points=Slice._slicespec2boundsspec(slice_,ct)) test_slice = Slice(bounds,ct) for a,b in zip(slice_,test_slice): self.assertEqual(a,b) slice_ =(2,3,4,8) bounds = BoundingBox(points=Slice._slicespec2boundsspec(slice_,ct)) test_slice = Slice(bounds,ct) for a,b in zip(slice_,test_slice): self.assertEqual(a,b) slice_ =(0,3,9,10) bounds = BoundingBox(points=Slice._slicespec2boundsspec(slice_,ct)) test_slice = Slice(bounds,ct) for a,b in zip(slice_,test_slice): self.assertEqual(a,b) bb = BoundingBox(points=((-0.75,-0.5),(0.75,0.5))) ct = SheetCoordinateSystem(bb,20,20) slice_ =(9,14,27,29) bounds = BoundingBox(points=Slice._slicespec2boundsspec(slice_,ct)) test_slice = Slice(bounds,ct) for a,b in zip(slice_,test_slice): self.assertEqual(a,b) slice_ =(0,6,0,7) bounds = BoundingBox(points=Slice._slicespec2boundsspec(slice_,ct)) test_slice = Slice(bounds,ct) for a,b in zip(slice_,test_slice): self.assertEqual(a,b) slice_ =(6,10,11,29) bounds = BoundingBox(points=Slice._slicespec2boundsspec(slice_,ct)) test_slice = Slice(bounds,ct) for a,b in zip(slice_,test_slice): self.assertEqual(a,b) bb = BoundingBox(points=((-0.5,-0.5),(0.5,0.5))) ct = SheetCoordinateSystem(bb,7) slice_ =(4,7,2,3) bounds = BoundingBox(points=Slice._slicespec2boundsspec(slice_,ct)) test_slice = Slice(bounds,ct) for a,b in zip(slice_,test_slice): self.assertEqual(a,b) slice_ =(0,7,0,7) bounds = BoundingBox(points=Slice._slicespec2boundsspec(slice_,ct)) test_slice = Slice(bounds,ct) for a,b in zip(slice_,test_slice): self.assertEqual(a,b)