def test_scaler(): N = 10 u = np.random.rand(N) + 1j * np.random.rand(N) s = Scaler(u) print('s:', s) v = s.predict(u) w = s.inverse(v) assert close(np.min(np.real(v)), 0.0) assert close(np.min(np.imag(v)), 0.0) assert close(np.max(np.abs(v)), 1.0) assert close_np(u, w)
def test_point(): x1, x2 = 0.0, 1.0 y1, y2 = 0.0, 0.5 x1_, y1_ = x1 + EPS / 2, y1 - EPS / 2 p1 = Point(x1 + y1 * 1j) p1_ = Point(x1_ + y1_ * 1j) p2 = Point(x2 + y1 * 1j) p3 = Point(x2 + y2 * 1j) assert close(p2.x, x2) assert close(p2.y, y1) assert p1 < p2 and p2 < p3 and p1 < p3 assert p1 == p1_
def test_sym_contour(): u = [0. + 5.j, 1. - 1.j, 2. + 5.j, 1. + 6.j] sc = SymContour(u, n_max_pixels=1) print('sc:', sc) ch = sc.Hull_based assert len(ch) == len(u) * 2 ap = sc.Approximate assert len(ap) > 0 sa = sc.Axis_list assert len(sa) == 1 a = sa[0] print('a:', a) assert close(a.Vec.angle, np.pi / 2, MIN_THETA_DIFF) p1, p2 = a.vertexes(u) assert close(p1.z, u[1]) and close(p2.z, u[3])
def test_contour(): u = [0. + 5.j, 1. + 1.j, 3. + 6.j, 2. + 7.j] area = 8.5 per = 3 * 2**0.5 + 17**0.5 + 29**0.5 mult_coef = 2 c = Contour(u, mult_coef) print('c:', c) assert close(c.Area, area) assert close(c.Perimeter, per) p = c.Pixels p_next = np.roll(p, 1) assert np.all(np.abs(p - p_next) <= 2**0.5 + EPS) ch = c.Convex_hull assert close_np(c.origin, ch.origin) m = c.Edge_middles assert len(m) == len(u) assert (u[0] + u[1]) / 2 in m
def test_axis_basic(): z1 = 0 + 0j z2 = 1 + 0j z3 = 0 + 1j z_vec = Vector(Point(z2 - z1)) print('z_vec:', z_vec) line = Axis(Point(z1), Point(z2)) print('line:', line) line2 = Axis(Point(z1), Point(z3)) print('line2:', line2) u = [z3] vec = line.Vec print('vec', vec) assert close(line.z1, z1) assert close(line.z2, z2) assert vec.collinear(z_vec) assert close(abs(vec.z), 1.0) assert line.intersection(line) is None assert line.intersection(line2) == Point(z1) assert close_np(line.distance(u), [1.0]) assert line.nearest_ind(u) == 0
def __eq__(self, other): return close(self.z, other.z, 2 * EPS)
def collinear(self, other): if close(abs(self.z), 0.0) or close(abs(other.z), 0.0): return True theta1 = np.angle(self.z) % np.pi theta2 = np.angle(other.z) % np.pi return close(theta1, theta2, MIN_THETA_DIFF)