def get_next_line(self, point): self.past_points.append(point) if len(self.past_points) > self.MAX_POINT: self.past_points.popleft() self.last_time = time.time() if len(self.past_points) == self.MAX_POINT: max_movement = 0 for pt2, pt1 in zip(list(self.past_points)[1:], list(self.past_points)[:-1]): movement = np.linalg.norm(pt2 - pt1) if movement > max_movement: max_movement = movement if (max_movement / (time.time() - self.last_time)) < 0.1: return None points = Points(list(self.past_points)) line_fit = Line.best_fit(points) direction = np.array(line_fit.direction) # I defined this side will be the positive direction. if direction[0] < 0: direction *= -1 direction = direction / np.linalg.norm(direction) return direction else: return None
def fit_line_direction(points): direction = list() for frame in range(0, len(points)): points_ = Points(np.reshape(np.array(points[frame, :]), (-1, 3))) line_fit = Line.best_fit(points_) direction.append(np.array(line_fit.direction)) return np.array(direction)
def test_from_points(objs): point_a, vector = objs point_b = point_a + vector line = Line(point_a, vector) line_from_points = Line.from_points(point_a, point_b) assert line.is_close(line_from_points, abs_tol=ATOL) # The line of best fit should be the same # as the line from two points. line_fit = Line.best_fit([point_a, point_b]) assert line_fit.is_close(line_from_points, abs_tol=ATOL)
def regression(centers=[]): points = Points(centers) #convert data line_fit = Line.best_fit(points) #take line of best fit p = line_fit.point v = line_fit.direction p0 = p - p[2] * v / v[2] #initial center v0 = v / v[2] #initial direction new_centers = [] #linearized centers i = z1 #start with first full slice while i <= end: #end with last full slice pn = p0 + i * v0 #next center new_centers.append(pn) i += 1 return new_centers
def test_best_fit_line(data): n_points = data.draw(st.integers(min_value=2, max_value=5)) dim = data.draw(st.integers(min_value=2, max_value=4)) points = Points([data.draw(arrays_fixed(dim)) for _ in range(n_points)]) assume(not points.are_concurrent(tol=ATOL)) line = data.draw(lines(dim)) line_fit = Line.best_fit(points) error_line = line.sum_squares(points) error_fit = line_fit.sum_squares(points) assert error_fit <= error_line + ATOL
""" 3D Line of Best Fit =================== Fit a line to multiple 3D points. """ from skspatial.objects import Line from skspatial.objects import Points from skspatial.plotting import plot_3d points = Points([ [0, 0, 0], [1, 1, 0], [2, 3, 2], [3, 2, 3], [4, 5, 4], [6, 5, 5], [6, 6, 5], [7, 6, 7], ], ) line_fit = Line.best_fit(points) plot_3d( line_fit.plotter(t_1=-7, t_2=7, c='k'), points.plotter(c='b', depthshade=False), )
def test_best_fit_line(points, line_expected): line_fit = Line.best_fit(np.array(points)) assert line_fit.is_close(line_expected) assert line_fit.point.is_close(line_expected.point)
def test_best_fit_line_failure(points): with pytest.raises(Exception): Line.best_fit(points)
def test_best_fit_failure(points, message_expected): with pytest.raises(ValueError, match=message_expected): Line.best_fit(points)