def test_extrapolate(self): a = Point(1, 0) origin = Point(0, 0) # make sure the right type is returned result = a.extrapolate(1, 1) self.assertTrue(isinstance(a, Point), "return value of extrapolate should be a Point") self.assertFalse(a is result, "return value of extrapolate should be a new Point") result = Point(1, 1).extrapolate(1, 2) self.assertEqual(result.x, 2.4142135623730949, "incorrect x: Point(1,1).extrapolate(-1,2)") self.assertEqual(result.y, 2.4142135623730949, "incorrect y: Point(1,1).extrapolate(-1,2)") result = Point(1, 1).extrapolate(-1, 2) self.assertEqual(result.x, 2.4142135623730949, "incorrect x: Point(1,1).extrapolate(1,2)") self.assertEqual(result.y, -0.41421356237309492, "incorrect y: Point(1,1).extrapolate(1,2)") result = Point(1, 1).extrapolate(0, 2) self.assertEqual(result.x, 3, "incorrect x: Point(1,1).extrapolate(0,2)") self.assertEqual(result.y, 1, "incorrect y: Point(1,1).extrapolate(0,2)") result = Point(1, 1).extrapolate(0, -2) self.assertEqual(result.x, -1, "incorrect x: Point(1,1).extrapolate(0,-2)") self.assertEqual(result.y, 1, "incorrect y: Point(1,1).extrapolate(0,-2)") result = Point(1, 1).extrapolate(None, 3) self.assertEqual(result.x, 1, "incorrect x: Point(1,1).extrapolate(None, 3)") self.assertEqual(result.y, 4, "incorrect y: Point(1,1).extrapolate(None, 3)")
def test_point_should_not_be_equal_to_any_other_object(self): p1 = Point(3,4); fakePoint = {}; fakePoint['__x'] = 3; fakePoint['__y'] = 4; try: self.assertFalse(p1.equals(fakePoint)) except Exception, e: self.assertEqual(e.message ,str(fakePoint)+' is not Point instance');
def test_distance(self): a = Point(0, 0) b = Point(3, 4) c = Point(2, 0) error_msg1 = "incorrect distance between (%d, %d) and (%d, %d)" % (a.x, a.y, c.x, c.y) error_msg2 = "incorrect distance between (%d, %d) and (%d, %d)" % (a.x, a.y, b.x, b.y) self.assertEqual(a.distance(c), 2, error_msg1) self.assertEqual(a.distance(b), 5, error_msg2)
def solve_direct(p1, s, a1): p2 = Point(phi = -0.6, lambda_ = 0) a2 = 0 dec = 4 while round((p2.phi - p1.phi), dec) != round(get_delta_phi(p1, p2, s), dec): p2.phi = p2.phi + math.pow(10, -dec) print p2.phi dec = 4 while round((a2 - a1), dec) != round(get_delta_a(p1, p2, s), dec): a2 = a2 + math.pow(10, -dec) print a2
def __init__(self, x=0, y=0, radius=1): if radius < 0: raise ValueError("promien ujemny") self.pt = Point(x, y) self.radius = radius
def test_point_should_be_equal_to_same_point(self): p1 = Point(3,4); p2 = Point(3,4); self.assertTrue(p1.equals(p2)); self.assertTrue(p2.equals(p1));
def center(self): # zwraca środek trójkąta return Point((self.pt1.x + self.pt2.x + self.pt3.x) / 3., (self.pt1.y + self.pt2.y + self.pt3.y) / 3.)
def converge_to_centre(pp, c, nDirections=5, maxRadius=50, pauseTime=0): """ converge_to_centre(pp, c) Given a set of points and an initial center point c, will find a better estimate of the center. Returns (c, L), with L indices in pp that were uses to fit the final circle. """ # Shorter names N = nDirections pi = np.pi # Init point to return on error ce = Point(0,0) ce.r = 0 # Are there enough points? if len(pp) < 3: return ce, [] # Init a pointset of centers we've has so far cc = Pointset(2) # Init list with vis objects (to be able to delete them) showObjects = [] if pauseTime: fig = vv.gcf() while c not in cc: # Add previous center cc.append(c) # Calculate distances and angles dists = c.distance(pp) angs = c.angle2(pp) # Get index of closest points i, = np.where(dists==dists.min()) i = iClosest = int(i[0]) # Offset the angles with the angle relative to the closest point. refAng = angs[i] angs = subtract_angles(angs, refAng) # Init L, the indices to the closest point in each direction L = [] # Get closest point on N directions for angle in [float(angnr)/N*2*pi for angnr in range(N)]: # Get indices of points that are in this direction dangs = subtract_angles(angs, angle) I, = np.where(np.abs(dangs) < pi/N ) # Select closest if len(I): distSelection = dists[I] minDist = distSelection.min() J, = np.where(distSelection==minDist) if len(J) and minDist < maxRadius: L.append( int(I[J[0]]) ) # Check if ok if len(L) < 3: return ce, [] # Remove spurious points (points much furter away that the 3 closest) distSelection = dists[L] tmp = [d for d in distSelection] d3 = sorted(tmp)[2] # Get distance of 3th closest point I, = np.where(distSelection < d3*2) L = [L[i] for i in I] # Select points ppSelect = Pointset(2) for i in L: ppSelect.append(pp[i]) # Refit circle cnew = fit_cirlce(ppSelect,False) if cnew.r==0: return ce, [] # Show if pauseTime>0: # Delete for ob in showObjects: ob.Destroy() # Plot center and new center ob1 = vv.plot(c, ls='', ms='x', mc='r', mw=10, axesAdjust=0) ob2 = vv.plot(cnew, ls='', ms='x', mc='r', mw=10, mew=0, axesAdjust=0) # Plot selection points ob3 = vv.plot(ppSelect, ls='', ms='.', mc='y', mw=10, axesAdjust=0) # Plot lines dividing the directions tmpSet1 = Pointset(2) tmpSet2 = Pointset(2) for angle in [float(angnr)/N*2*pi for angnr in range(N)]: angle = -subtract_angles(angle, refAng) dx, dy = np.cos(angle), np.sin(angle) tmpSet1.append(c.x, c.y) tmpSet1.append(c.x+dx*d3*2, c.y+dy*d3*2) for angle in [float(angnr+0.5)/N*2*pi for angnr in range(N)]: angle = -subtract_angles(angle, refAng) dx, dy = np.cos(angle), np.sin(angle) tmpSet2.append(c.x, c.y) tmpSet2.append(c.x+dx*d3*2, c.y+dy*d3*2) ob4 = vv.plot(tmpSet1, lc='y', ls='--', axesAdjust=0) ob5 = vv.plot(tmpSet2, lc='y', lw=3, axesAdjust=0) # Store objects and wait showObjects = [ob1,ob2,ob3,ob4,ob5] fig.DrawNow() time.sleep(pauseTime) # Use new c = cnew # Done for ob in showObjects: ob.Destroy() return c, L
# Fit circle and sample and show c = fit_cirlce(pp) cc = sample_circle(c, 32) self._cc.SetPoints(cc) # Draw self._cc.Draw() ## Tests if __name__ == "__main__": import visvis as vv tester1 = ChopstickCriteriaTester() # tester2 = FitCircleTester() if False: pp = Pointset(2) pp.append(1,1) pp.append(10,3) pp.append(8,8) pp.append(2,6) pp.append(1,2) fig = vv.figure(103) fig.Clear() a = vv.gca() a.daspectAuto = False fig.position = -799.00, 368.00, 544.00, 382.00 vv.plot(pp, ls='', ms='.', mc='g') c2 = converge_to_centre(pp, Point(6,5), 5, pauseTime=0.3)
def center(self): # zwraca środek prostokąta return Point(self.pt1.x + (self.pt2.x - self.pt1.x) / 2., self.pt1.y + (self.pt2.y - self.pt1.y) / 2.)
def next(self): result = self.next_point self.next_point = Point(self.next_point.x + 1, self.next_point.y + 1) return result
def __init__(self, x1=0, y1=0, x2=0, y2=0): if (x1 > x2 or y1 > y2): raise ValueError("Invalid arguments!") self.pt1 = Point(x1, y1) self.pt2 = Point(x2, y2)
def test_center(self): assert (Rectangle(2, 2, 4, 4).center() == Point(3, 3))
def __init__(self, x1, y1, x2, y2): self.pt1 = Point(x1, y1) self.pt2 = Point(x2, y2)
def center(self): # zwraca środek prostokąta return Point((self.pt1.x + self.pt2.x) / 2, (self.pt1.y + self.pt2.y) / 2)
def __init__(self, x1=0, y1=0, x2=0, y2=0): self.pt1 = Point(x1, y1) self.pt2 = Point(x2, y2)
def test_center(self): self.assertEqual(Rectangle(1.5, 2, 5.5, 6).center(), Point(3.5, 4)) self.assertEqual(Rectangle(-1, -1, 1, 1).center(), Point(0, 0)) self.assertEqual(Rectangle(-2, -3, 1, 5).center(), Point(-0.5, 1)) self.assertEqual( Rectangle(-9, 0.5, -5.5, 1).center(), Point(-7.25, 0.75))
def test_distance(): points = [Point(0, 0), Point(1, 0), Point(0, 1)] dists = pdist.distance(points) assert dists[0] == approx(1.0) assert dists[1] == approx(1.0) assert dists[2] == approx(math.sqrt(2))
def move(self, x, y): self.pt1 += Point(x, y) self.pt2 += Point(x, y) return self # przesunięcie o (x, y)
def __init__(self): self.next_point = Point(0, 0)
def center(self): return Point(((self.pt1.x + self.pt2.x) / 2), ((self.pt1.y + self.pt2.y) / 2))
def test_center(self): self.assertEqual(r1.center(), Point(4, 5))
def test_move(self): self.assertEqual(Rectangle.move(Rectangle(1, 2, 3, 4), 0, 1), (Point(1, 3), Point(3, 5)))
def __init__(self, name: str, points: int, date_str: str): data = {'payer_name': name, 'points': Point(points, date_str)} super().__init__(**data)
def __init__(self, x1, y1, x2, y2): # Chcemy, aby x1 < x2, y1 < y2. self.pt1 = Point(x1, y1) self.pt2 = Point(x2, y2) if x1 >= x2 or y1 >= y2: raise ValueError("To nie jest prostokat")
def detect_points(slice, th_gc=2000, th_minHU=300, sigma=1.6): """ Detect points Detects points on a stent in the given slice. Slice should be a numpy array. - The Gaussian Curvature should be above a threshold (needle detection) (th_gc) - An absolute (weak) threshold is used based on the Houndsfield units (th_minHU) - Streak artifacts are suppressed - sigma is the used scale at which the GC is calculated. """ # Make sure that the slice is a float if slice.dtype not in [np.float32, np.float64]: slice = slice.astype(np.float32) # Create slice to supress streak artifacts # Where streak artifacts are present, the image is inverted, anywhere else # its zero. By also calculating derivatives on this image and than adding # it, the threshold will never be reached where the streak artifacts are. sliceStreak = th_streHU - slice sliceStreak[sliceStreak<0] = 0 # Create new point set to store points pp = Pointset(2) # Calculate Gaussian curvature if True: Lxx = gaussfun.gfilter(slice, sigma, [0,2]) Ltmp = gaussfun.gfilter(sliceStreak, sigma, [0,2]) Lxx = Lxx+2*Ltmp Lxx[Lxx>0]=0; Lyy = gaussfun.gfilter(slice, sigma, [2,0]) Ltmp = gaussfun.gfilter(sliceStreak, sigma, [2,0]) Lyy = Lyy+2*Ltmp Lyy[Lyy>0]=0; Lgc = Lxx * Lyy # Make a smoothed version slice_smoothed = gaussfun.gfilter(slice, 0.5, 0) # Make a selection of candidate pixels Iy,Ix = np.where( (slice > th_minHU) & (Lgc > th_gc) ) # Mask to detect clashes clashMask = np.zeros(slice.shape, dtype=np.bool) # Detect local maxima for x,y in zip(Ix,Iy): if x==0 or y==0 or x==slice.shape[1]-1 or y==slice.shape[0]-1: continue # Select patch patch1 = slice[y-1:y+2,x-1:x+2] patch2 = slice_smoothed[y-1:y+2,x-1:x+2] if slice[y,x] == patch1.max():# and slice_smoothed[y,x] == patch2.max(): # Found local max (allowing shared max) # Not if next to another found point if clashMask[y,x]: continue # Not a streak artifact if patch2.min() <= th_streHU: continue # Subpixel #dx,dy = subpixel.fitLQ2_5(patch1) # Store pp.append( x, y ) clashMask[y-1:y+2,x-1:x+2] = 1 # Express points in world coordinates and return if isinstance(slice, Aarray): ori = [i for i in reversed(slice.origin)] sam = [i for i in reversed(slice.sampling)] pp *= Point(sam) pp += Point(ori) return pp
def test_center(self): self.assertEqual(Rectangle.center(Rectangle(0, 0, 1, 1)), Point(0.5, 0.5))
def test_center(self): self.assertEqual(t1.center(), Point(2, 2))
def test_point_should_give_distance_betwen_an_given_other_point(self): p1 = Point(1,2); p2 = Point(4,5); self.assertEqual(p1.distanceFrom(p2) ,4.24);
def test_move(self): pt = Point(4, 5) pt.move(2, 3) self.assertEqual(pt.x, 2, "pt.x did not move from 4 to 2") self.assertEqual(pt.y, 3, "pt.y did not move from 5 to 3")
def test_center(self): self.assertTrue(Rectangle.center(Rectangle(2, 2, 4, 4)) == Point(3, 3))
def center(self): # zwraca środek prostokąta return Point(((self.pt2.x - self.pt1.x) / 2), ((self.pt2.y - self.pt1.y) / 2))
def move(self, x, y): foo = copy.deepcopy(self) foo.pt = foo.pt + Point(x, y) return foo
def test_center(self): self.assertTrue(Rectangle(0, 0, 2, 2).center() == Point(1, 1)) self.assertFalse(Rectangle(0, 0, 2, 2).center() == Point(2, 1))
def test_center_tri(self): self.assertEqual(self.t2.center(), Point(1,3.0)) self.assertNotEqual(self.t1.center(), Point(1,1))
def test_translate(self): pt = Point(4, 5) pt.translate(-1, 1) self.assertEqual(pt.x, 3, "pt.x did not translate by -1 from 4 to 3") self.assertEqual(pt.y, 6, "pt.y did not translate by 1 from 5 to 6")
def center(self): # zwraca środek prostokąta point = Point() point.x = (self.pt1.x + self.pt2.x) / 2.0 point.y = (self.pt1.y + self.pt2.y) / 2.0 return point
def test_slope(self): a = Point(1, 1) b = Point(2, 2) self.assertEqual(a.slope(b), 1, "incorrect slope between (%d, %d) and (%d, %d)" % (a.x, a.y, b.x, b.y)) a = Point(1, 1) b = Point(2, 3) self.assertEqual(a.slope(b), 2, "incorrect slope between (%d, %d) and (%d, %d)" % (a.x, a.y, b.x, b.y)) a = Point(1, 1) b = Point(3, 2) self.assertEqual(a.slope(b), 0.5, "incorrect slope between (%d, %d) and (%d, %d)" % (a.x, a.y, b.x, b.y)) a = Point(5, 3) b = Point(6, 2) self.assertEqual(a.slope(b), -1, "incorrect slope between (%d, %d) and (%d, %d)" % (a.x, a.y, b.x, b.y)) a = Point(5, 3) b = Point(4, 3) self.assertEqual(a.slope(b), 0, "incorrect slope between (%d, %d) and (%d, %d)" % (a.x, a.y, b.x, b.y)) a = Point(5, 3) b = Point(5, 0) self.assertIsNone(a.slope(b), "incorrect slope between (%d, %d) and (%d, %d)" % (a.x, a.y, b.x, b.y))
def test_center(self): self.assertEqual( Triangle(0, 0, 0, 8, 8, 8).center(), Point(2.00, 5.00)) self.assertEqual( Triangle(2, 2, 2, 5, 5, 5).center(), Point(3.00, 4.00))