Ejemplo n.º 1
0
 def process(self, points):
     #print("%%%%%%%%%%%%%%%%",len(points))
     self.X_points = []
     self.Y_points = []
     for i in range(len(points)):
         self.X_points.append(points[i][0])
         self.Y_points.append(points[i][1])
     #print("self.X_points长度:",len(self.X_points))
     #print(self.X_points)
     #print("points:",points)
     #a,u,sig=get_param()
     #controller = GaussianController(path='four_walls.pcd', verbose=verbose)
     #parts=controller.get_parts()
     #for part in parts:
     X_array = np.array(self.X_points)
     Y_array = np.array(self.Y_points)
     if (0 < get_slope(points)
             and get_slope(points) < 45) or (135 < get_slope(points)
                                             and get_slope(points) < 180):
         popt, pcov = curve_fit(self.gaussian_mix,
                                X_array,
                                Y_array,
                                maxfev=500000)
         return [popt[0], popt[1], popt[2]]
     else:
         popt, pcov = curve_fit(self.gaussian_mix,
                                Y_array,
                                X_array,
                                maxfev=500000)
         return [popt[0], popt[1], popt[2]]
Ejemplo n.º 2
0
def get_lines(img, rho, theta, threshold, min_line_len, max_line_gap):
    # convert to grayscale
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # perform gaussian blur
    blur = gaussian_blur(img_gray, kernel_size=17)

    # perform edge detection
    canny_edges = canny(blur, low_threshold=50, high_threshold=70)

    detected_lines = hough_lines(img=canny_edges,
                                 rho=rho,
                                 theta=theta,
                                 threshold=threshold,
                                 min_line_len=min_line_len,
                                 max_line_gap=max_line_gap)

    candidate_lines = []
    for line in detected_lines:
        for x1, y1, x2, y2 in line:
            slope = get_slope(x1, y1, x2, y2)
            if 0.5 <= np.abs(slope) <= 2:
                candidate_lines.append({
                    "slope": slope,
                    "bias": get_bias(slope, x1, y1)
                })

    lane_lines = compute_candidates(candidate_lines, img_gray.shape)

    return lane_lines
Ejemplo n.º 3
0
 def rotate_to_target(self, target):
     """
     Rotate members of the cluster to a target location.
     Compute the vector for each member of the cluster in which it needs to move to get to a specific target.
     :param target:
     :return: A mapping of action for each agent in the cluster.
     """
     for idx, agent in self.agents.items():
         agent.set_orientation(
             math.atan(
                 get_slope(target[0], target[1],
                           agent.get_position()[0],
                           agent.get_position()[1])))
Ejemplo n.º 4
0
    def move_to_target(self, target):
        for idx, agent in self.agents.items():
            # print("Agent position: " + str(agent.get_position()))
            # print("Target: " + str(target))
            if abs(agent.get_position()[0] - target[0]) < 1e-10:
                theta = 0
            else:
                slope = get_slope(target[0], target[1],
                                  agent.get_position()[0],
                                  agent.get_position()[1])
                theta = math.atan(slope)

            factor = -1 if agent.get_position()[0] > target[0] else 1
            agent.action = np.array([math.cos(theta),
                                     math.sin(theta)]) * factor

        self.allocated_action = False
Ejemplo n.º 5
0
def get_trendline_list_4_quarter(close_price_series):
    """
    日付降順の終値リストに指定されている期間で株価を取得し1時近似を取得する
    args:
        n日分の日付で昇順のリスト
    """
    QUARTER_NUM = 4
    close_price_series_length = len(close_price_series)
    # ループ用変数
    close_price_series_length_quarter = int(close_price_series_length/QUARTER_NUM)
    start_index = 0
    loop_length = close_price_series_length_quarter
    trendline_list = []
    slope_list = []
    intercept_list = []
    # 平均
    close_price_avg = close_price_series.mean()
    # データが少なすぎる場合はスキップ
    if close_price_series_length < QUARTER_NUM:
        print('データが少ないためスキップします')
        return trendline_list
    # 指定期間の4等分の上昇量を取得する
    for i in range(0,close_price_series_length,close_price_series_length_quarter):
        try:
            index = i
            if i>0:
                index = i-1
            slope,intercept =\
                 get_slope(close_price_series[index:i+close_price_series_length_quarter])
            # 傾きと切片を割合で示す
            slope_list.append(round(slope*100/close_price_avg,3))
            intercept_list.append(round(intercept*100/close_price_avg,3))
            loop_length += close_price_series_length_quarter
            trendline_list.append({'slope':slope,'intercept':intercept})
        except Exception:
            print(f'1次近似取得時にエラー発生。スキップします。')
            print(traceback.format_exc())
            continue
    return slope_list,intercept_list
Ejemplo n.º 6
0
count = 0


def func3(
    x,
    a1,
    m1,
    s1,
):
    return a1 * np.exp(-((x - m1) / s1)**2)
    #print(len(part.points))


for part in parts:
    #print(part.points)
    print(get_slope(part.points))
    print(len(part.points))
    count += 1
    if (0 < get_slope(part.points) and get_slope(part.points) < 45) or (
            get_slope(part.points) > 135 and get_slope(part.points) < 180):
        #print(part.points)
        #print(get_slope(part.points))
        #print("########################")
        xy_lim = get_xy_lim(part.points)
        #print(part.points)
        #print(xy_lim[0])
        #print("####################")
        #print(len(part.points))
        #print("#####xy_lim",xy_lim)
        X_ = np.arange(xy_lim[0], xy_lim[1], 0.1)
        if (i == 7):
Ejemplo n.º 7
0
 def get_average_gpu_slope(self):
     gpu_slopes = list(map(lambda i: get_slope(self.gpu_timestamps_list[i], self.gpu_timestamps_list[i+1], self.gpu_scores_list[i], self.gpu_scores_list[i+1]), range(0, self.gpu_num_rounds)))
     assert len(gpu_slopes) != 0
     return np.mean(gpu_slopes)
Ejemplo n.º 8
0
    def process(self, points):
        """
        用拉格朗日乘数法,对点进行分段线性拟合
        @param points: points need to fit
        @return: list of intersections
        """
        self.parameters = []
        # Rotate points to horizontal
        slope = get_slope(points)
        matrix = get_rotation_matrix(-slope)
        new_points = []
        for p in points:
            vec = np.array([p[0], p[1]])
            new_points.append(list(matrix.dot(vec)))

        # get interval and steps
        interval = []
        interval.append(min([i[0] for i in new_points]))
        interval.append(max([i[0] for i in new_points]))
        step = (interval[1] - interval[0]) / float(self.segments_count)

        # get points ready
        x = [i[0] for i in new_points]
        y = [i[1] for i in new_points]

        # get dimension
        param_count = self.segments_count * 2
        dimension = self.segments_count * 3 - 1

        # way to get the matrix
        def getRows(index):
            """
            计算矩阵的每一行,一次计算两行
            """
            row1 = [0 for i in range(dimension)]
            row2 = [0 for i in range(dimension)]
            left = interval[0] + step * index
            right = left + step
            points = [p for p in new_points if left <= p[0] < right]
            row1[index * 2] = sum([i[0] * i[0] for i in points])
            row1[index * 2 + 1] = sum([i[0] for i in points])
            row2[index * 2] = row1[index * 2 + 1]
            row2[index * 2 + 1] = len(points)
            if index != 0:
                row1[param_count + index - 1] = -0.5
                row2[param_count + index - 1] = 0.5
            if index != param_count / 2 - 1:
                row1[param_count + index] = 0.5
                row2[param_count + index] = -0.5
            two_elems = [0, 0]
            two_elems[0] = sum([i[0] * i[1] for i in points])
            two_elems[1] = sum([i[1] for i in points])
            return [row1, row2], two_elems

        # solve the equation
        a = []  # Ax = b, a will be used to generate A
        b = []
        for i in range(self.segments_count
                       ):  # generate the first segments_count * 2 rows
            two_rows, two_elems = getRows(i)  # i is the segment count
            a += two_rows
            b += two_elems
        for i in range(self.segments_count - 1):
            temp = [0 for j in range(dimension)]
            temp[i * 2] = self.interval[0] + step * (i + 1)
            temp[i * 2 + 1] = 1
            temp[i * 2 + 2] = -temp[i * 2]
            temp[i * 2 + 3] = -1
            a.append(temp)
            b.append(0)
        A = np.matrix(a)
        b = np.array(b)
        singular = False
        if np.linalg.cond(
                A) < 1 / sys.float_info.epsilon:  # to avoid singular matrix
            result = np.linalg.solve(A, b)
            parameters = [[result[i * 2], result[i * 2 + 1]]
                          for i in range(self.segments_count)]
            for i in range(len(parameters)):
                self.parameters.append(rotate_line(parameters[i], slope))
        else:
            singular = True

        # rotate things back
        interval = [min([i[0] for i in points]), max([i[1] for i in points])]

        # print everything
        if self.verbose:
            np.set_printoptions(precision=3, suppress=True)
            print("\nA: ")
            print(A)
            if not singular:
                print("\nx: ")
                print(result)
            else:
                print("\nNOTICE: A is singular, no result is available.")
            print("\nb: ")
            print(b)
            print("\nSegments: " + str(self.segments_count))
            print("\nk and b: ")
            print([[round(i[0], 3), round(i[1], 3)] for i in self.parameters])
            print("\nInterval: ")
            print(interval)

        # return intersections
        x = []
        y = []
        for i in range(len(self.parameters) + 1):
            if i == 0:  # 左边端点
                param = self.parameters[0]
                x.append(interval[0])
                y.append(x[0] * param[0] + param[1])
            elif i == len(self.parameters):  # 右边端点
                param = self.parameters[i - 1]
                x.append(interval[1])
                y.append(x[len(x) - 1] * param[0] + param[1])
            else:  # 两条折线的交点
                param1 = self.parameters[i - 1]
                param2 = self.parameters[i]
                intersection = get_intersection(param1, param2)
                x.append(intersection[0])
                y.append(intersection[1])
        return [[i, j] for i, j in zip(x, y)]