Best Python code snippet using Kiwi_python
graph_extraction.py
Source:graph_extraction.py
1import networkx as nx2from osgeo import ogr3import numpy as np4import sys5import itertools6from collections import deque7from rtree import Rtree8from tptk.common.douglas_peucker import DouglasPeucker9from tptk.common.spatial_func import SPoint10from tptk.common.mbr import MBR11from tptk.common.grid import Grid12from tptk.common.spatial_func import distance13class CenterNodePixel:14 def __init__(self, node_pixels):15 self.node_pixels = node_pixels16 self.i = sum([node_pixel[0] for node_pixel in node_pixels]) / len(node_pixels)17 self.j = sum([node_pixel[1] for node_pixel in node_pixels]) / len(node_pixels)18 def center_pixel(self):19 return int(self.i), int(self.j)20class GraphExtractor:21 INVALID = -122 BLANK = 023 EDGE = 124 NODE = 225 VISITED_NODE = 326 def __init__(self, epsilon, min_road_dist):27 self.segment_simplifier = DouglasPeucker(epsilon)28 self.min_road_dist = min_road_dist29 def extract(self, skeleton, mbr, target_path):30 assert skeleton.ndim == 2, 'the grey scale skeleton should only have 1 channel'31 # pad zero for safety32 grid = Grid(mbr, skeleton.shape[0], skeleton.shape[1])33 eval_mbr_padded = MBR(mbr.min_lat - 2 * grid.lat_interval, mbr.min_lng - 2 * grid.lng_interval,34 mbr.max_lat + 2 * grid.lat_interval, mbr.max_lng + 2 * grid.lng_interval)35 mbr = eval_mbr_padded36 skeleton_padded = np.zeros((skeleton.shape[0] + 4, skeleton.shape[1] + 4))37 skeleton_padded[2:skeleton.shape[0] + 2, 2:skeleton.shape[1] + 2] = skeleton38 skeleton = skeleton_padded39 nb_rows, nb_cols = skeleton.shape40 yscale = nb_rows / (mbr.max_lat - mbr.min_lat)41 xscale = nb_cols / (mbr.max_lng - mbr.min_lng)42 sys.stdout.write("Identifying road nodes pixels... ")43 sys.stdout.flush()44 binary_skeleton = (skeleton > 0).astype('int32')45 status_matrix = self.identify_node_pixels(binary_skeleton)46 sys.stdout.write("done.\n")47 sys.stdout.flush()48 sys.stdout.write("Detecting road network component... ")49 nodes, segments = self.detect_rn_component(status_matrix)50 sys.stdout.write("done.\n")51 sys.stdout.flush()52 sys.stdout.write("Constructing and saving road network... ")53 sys.stdout.flush()54 self.construct_undirected_rn(nodes, segments, target_path, nb_rows, xscale, yscale, mbr)55 sys.stdout.write("done.\n")56 sys.stdout.flush()57 def identify_node_pixels(self, skeleton):58 """59 22 23 08 09 1060 21 07 00 01 1161 20 06 -1 02 1262 19 05 04 03 1363 18 17 16 15 1464 :param skeleton:65 :return:66 """67 status_matrix = np.copy(skeleton)68 road_pixels = np.where(status_matrix == GraphExtractor.EDGE)69 nb_road_pixels = len(road_pixels[0])70 print('\n# of road pixels:{}'.format(nb_road_pixels))71 cnt = 172 for i, j in zip(road_pixels[0], road_pixels[1]):73 if (cnt % 100 == 0) or (cnt == nb_road_pixels):74 sys.stdout.write("\r" + str(cnt) + "/" + str(nb_road_pixels) + "... ")75 sys.stdout.flush()76 cnt += 177 # skip boundary78 if i < 2 or i >= status_matrix.shape[0] - 2 or j < 2 or j >= status_matrix.shape[1] - 2:79 continue80 p = [skeleton[i - 1][j], skeleton[i - 1][j + 1], skeleton[i][j + 1], skeleton[i + 1][j + 1],81 skeleton[i + 1][j], skeleton[i + 1][j - 1], skeleton[i][j - 1], skeleton[i - 1][j - 1],82 skeleton[i - 2][j], skeleton[i - 2][j + 1], skeleton[i - 2][j + 2], skeleton[i - 1][j + 2],83 skeleton[i][j + 2], skeleton[i + 1][j + 2], skeleton[i + 2][j + 2], skeleton[i + 2][j + 1],84 skeleton[i + 2][j], skeleton[i + 2][j - 1], skeleton[i + 2][j - 2], skeleton[i + 1][j - 2],85 skeleton[i][j - 2], skeleton[i - 1][j - 2], skeleton[i - 2][j - 2], skeleton[i - 2][j - 1]]86 fringe = [bool(p[8] and bool(p[7] or p[0] or p[1])),87 bool(p[9] and bool(p[0] or p[1])),88 bool(p[10] and p[1]),89 bool(p[11] and bool(p[1] or p[2])),90 bool(p[12] and bool(p[1] or p[2] or p[3])),91 bool(p[13] and bool(p[2] or p[3])),92 bool(p[14] and p[3]),93 bool(p[15] and bool(p[3] or p[4])),94 bool(p[16] and bool(p[3] or p[4] or p[5])),95 bool(p[17] and bool(p[4] or p[5])),96 bool(p[18] and p[5]),97 bool(p[19] and bool(p[5] or p[6])),98 bool(p[20] and bool(p[5] or p[6] or p[7])),99 bool(p[21] and bool(p[6] or p[7])),100 bool(p[22] and p[7]),101 bool(p[23] and bool(p[7] or p[0]))]102 connected_component_cnt = 0103 for k in range(0, len(fringe)):104 connected_component_cnt += int(not bool(fringe[k]) and bool(fringe[(k + 1) % len(fringe)]))105 if connected_component_cnt == 0:106 status_matrix[i][j] = GraphExtractor.BLANK107 elif (connected_component_cnt == 1) or (connected_component_cnt > 2):108 status_matrix[i][j] = GraphExtractor.NODE109 # if connected_component_cnt == 2, we think it is a normal internal node110 return status_matrix111 def detect_rn_component(self, status_matrix):112 node_pixels = np.where(status_matrix == GraphExtractor.NODE)113 nb_node_pixels = len(node_pixels[0])114 print('\n# of node pixels:{}'.format(nb_node_pixels))115 neighbor_deltas = [dxdy for dxdy in itertools.product([-1, 0, 1], [-1, 0, 1])116 if dxdy[0] != 0 or dxdy[1] != 0]117 # node pixel -> center node118 nodes = {}119 node_pixel_spatial_index = Rtree()120 # [node pixel sequence (start and end must be node pixel)]121 connected_segments = []122 cnt = 1123 center_nodes = []124 node_pixel_id = 0125 for i, j in zip(node_pixels[0], node_pixels[1]):126 if (cnt % 100 == 0) or (cnt == nb_node_pixels):127 sys.stdout.write("\r" + str(cnt) + "/" + str(nb_node_pixels) + "... ")128 sys.stdout.flush()129 cnt += 1130 if status_matrix[i][j] == GraphExtractor.VISITED_NODE:131 continue132 # region merge neighbor node pixels133 status_matrix[i][j] = GraphExtractor.VISITED_NODE134 candidates = [(i, j)]135 node_pixels = []136 while len(candidates) > 0:137 node_pixel = candidates.pop()138 node_pixels.append(node_pixel)139 m, n = node_pixel140 for dm, dn in neighbor_deltas:141 if status_matrix[m + dm][n + dn] == GraphExtractor.NODE:142 status_matrix[m + dm][n + dn] = GraphExtractor.VISITED_NODE143 candidates.append((m + dm, n + dn))144 center_node = CenterNodePixel(node_pixels)145 center_nodes.append(center_node)146 for node_pixel in node_pixels:147 nodes[node_pixel] = center_node148 node_pixel_spatial_index.insert(node_pixel_id, node_pixel, obj=node_pixel)149 node_pixel_id += 1150 # endregion151 # region find neighbor segments152 # mask current nodes, make sure the edge doesn't return to itself153 for m, n in node_pixels:154 status_matrix[m][n] = GraphExtractor.INVALID155 # find new road segment of the current node in each possible direction156 for node_pixel in node_pixels:157 connected_segment = self.detect_connected_segment(status_matrix, node_pixel)158 if connected_segment is not None:159 connected_segments.append(connected_segment)160 # restore masked nodes161 for m, n in node_pixels:162 status_matrix[m][n] = GraphExtractor.VISITED_NODE163 # endregion164 print('\n# of directly connected segments:{}'.format(len(connected_segments)))165 # there might be few edge pixels left, that should be fine166 nb_unprocessed_edge_pixels = np.sum(status_matrix[status_matrix == GraphExtractor.EDGE])167 print('unprocessed edge pixels:{}'.format(nb_unprocessed_edge_pixels))168 print('# of nodes:{}'.format(len(center_nodes)))169 print('# of segments:{}'.format(len(connected_segments)))170 return nodes, connected_segments171 def detect_connected_segment(self, status_matrix, start_node_pixel):172 """173 find a path ended with node pixel174 :param status_matrix: status175 :param start_node_pixel: start node pixel176 :return: [start_node_pixel, edge_pixel,...,end_node_pixel]177 """178 # for current implementation, we assume edge pixel has only two arcs179 # but it is possible that edge pixel has multiple connected component rather than 2,180 # because crossing are detected using outer pixels181 s = deque()182 neighbor_deltas = [dxdy for dxdy in itertools.product([-1, 0, 1], [-1, 0, 1])183 if dxdy[0] != 0 or dxdy[1] != 0]184 # add candidates to stack185 m, n = start_node_pixel186 for dm, dn in neighbor_deltas:187 if status_matrix[m + dm][n + dn] == GraphExtractor.EDGE:188 s.appendleft(((m + dm, n + dn), [start_node_pixel]))189 while len(s) > 0:190 (m, n), path = s.popleft()191 # end node pixel192 if status_matrix[m][n] == GraphExtractor.NODE or \193 status_matrix[m][n] == GraphExtractor.VISITED_NODE:194 path.append((m, n))195 return path196 # internal edge pixel197 elif status_matrix[m][n] == GraphExtractor.EDGE:198 # mark the edge as visited199 status_matrix[m][n] = GraphExtractor.BLANK200 new_path = path.copy()201 new_path.append((m, n))202 for dm, dn in neighbor_deltas:203 s.appendleft(((m + dm, n + dn), new_path))204 return None205 def construct_undirected_rn(self, nodes, segments, target_path, nb_rows, xscale, yscale, mbr):206 # node pixel -> road node207 road_nodes = {}208 eid = 0209 # we use coordinate tuples as key, consistent with networkx210 # !!! Though we construct DiGraph (compatible with networkx interface, one segment will only add once)211 # loading this data, we should call g.to_undirected()212 rn = nx.DiGraph()213 for segment in segments:214 coords = []215 # start node216 start_node_pixel = nodes[segment[0]].center_pixel()217 if start_node_pixel not in road_nodes:218 lat, lng = self.pixels_to_latlng(start_node_pixel, mbr, nb_rows, xscale, yscale)219 road_node = SPoint(lat, lng)220 geo_pt = ogr.Geometry(ogr.wkbPoint)221 geo_pt.AddPoint(lng, lat)222 rn.add_node((lng, lat))223 road_nodes[start_node_pixel] = road_node224 start_node = road_nodes[start_node_pixel]225 coords.append(start_node)226 start_node_key = (start_node.lng, start_node.lat)227 # internal nodes, we didn't create id for them228 for coord in segment[1:-1]:229 lat, lng = self.pixels_to_latlng(coord, mbr, nb_rows, xscale, yscale)230 coords.append(SPoint(lat, lng))231 # end node232 end_node_pixel = nodes[segment[-1]].center_pixel()233 if end_node_pixel not in road_nodes:234 lat, lng = self.pixels_to_latlng(end_node_pixel, mbr, nb_rows, xscale, yscale)235 road_node = SPoint(lat, lng)236 geo_pt = ogr.Geometry(ogr.wkbPoint)237 geo_pt.AddPoint(lng, lat)238 rn.add_node((lng, lat))239 road_nodes[end_node_pixel] = road_node240 end_node = road_nodes[end_node_pixel]241 coords.append(end_node)242 end_node_key = (end_node.lng, end_node.lat)243 # region add segment244 # skip loop245 if start_node_key == end_node_key:246 continue247 simplified_coords = self.segment_simplifier.simplify(coords)248 # skip too short segment249 if not self.is_valid(simplified_coords):250 continue251 # add forward segment252 geo_line = ogr.Geometry(ogr.wkbLineString)253 for simplified_coord in simplified_coords:254 geo_line.AddPoint(simplified_coord.lng, simplified_coord.lat)255 rn.add_edge(start_node_key, end_node_key, eid=eid, Wkb=geo_line.ExportToWkb(), type='pred')256 eid += 1257 # endregion258 rn.remove_nodes_from(list(nx.isolates(rn)))259 print('\n# of nodes:{}'.format(rn.number_of_nodes()))260 print('# of edges:{}'.format(rn.number_of_edges()))261 nx.write_shp(rn, target_path)262 return rn263 def is_valid(self, coords):264 dist = 0.0265 for i in range(len(coords) - 1):266 dist += distance(coords[i], coords[i + 1])267 return dist > self.min_road_dist268 def pixels_to_latlng(self, pixel, mbr, nb_rows, xscale, yscale):269 i, j = pixel...
hmm_seg.py
Source:hmm_seg.py
1mode_path = '../data/mid_data/hmm.mod'2filter_val = 0.023# 1ãå 载模å4with open(mode_path, 'r', encoding='utf-8') as f:5 pi = eval(f.readline())6 A = eval(f.readline())7 B = eval(f.readline())8# 2ãå®ç°viterbiç®æ³ï¼è·åæä¼è·¯å¾ï¼å¾å°æ¦çæ大çç¶æç»åæ¹å¼9STATUS_NUM = 410# å°è¯è½¬æ¢æå个åçå表:'å¨æè§å'=>['å¨','æ','è§','å']11def get_word_ch(word):12 ch_lst = []13 for ch in word:14 ch_lst.append(ch)15 return ch_lst16# ä¸èèæ¦çç¸åç17def veterbinodiff(sentence, sep=' '):18 ch_lst = get_word_ch(sentence)19 # print(ch_lst)20 # ææ¬åºåçé¿åº¦T21 ch_num = len(ch_lst)22 # åå§åç¶æç©éµ23 status_matrix = [[[0.0, 0] for col in range(ch_num)] for row in range(STATUS_NUM)]24 # ç¶æç§ç±»M个 m*125 for i in range(STATUS_NUM):26 # 第ä¸ä¸ªåçåå°æ¦ç27 if B[i].get(ch_lst[0], -1) != -1:28 cur_B = B[i][ch_lst[0]]29 else:30 cur_B = -100000.031 # Ïå¯ä»¥ç´æ¥ä½¿ç¨32 cur_pi = pi[i]33 status_matrix[i][0][0] = cur_pi + cur_B # åäºlogï¼ç¸ä¹å°±æ¯ç¸å 34 status_matrix[i][0][1] = i35 # M*M*(T-1)36 for i in range(1, ch_num): # 1:T => T-137 for j in range(STATUS_NUM): # M38 max_p = None39 max_status = None40 # ç»è¿jèç¹çåM个èç¹ä¸åå¨æ¦çæ大å¼ï¼åä»ä¸ä¸å±è¿æ¥å°è¿ä¸ªèç¹çç¼å·41 for k in range(STATUS_NUM): # M åä¸å±ç第k个s42 cur_A = A[k][j] # 转移æ¦ç43 cur_p = status_matrix[k][i - 1][0] + cur_A44 if max_p is None or max_p < cur_p:45 max_p = cur_p46 max_status = k47 if B[j].get(ch_lst[i], -1) != -1:48 cur_B = B[j][ch_lst[i]]49 else:50 cur_B = -100000.051 status_matrix[j][i][0] = max_p + cur_B52 status_matrix[j][i][1] = max_status53 # get max prob path54 max_end_p = None55 max_end_status = None56 # æ¾æåä¸å±ä¸æ¦çæ大çï¼ä¹å°±æ¯æç»è·¯å¾æ大ç57 for i in range(STATUS_NUM):58 if max_end_p is None or status_matrix[i][ch_num - 1][0] > max_end_p:59 max_end_p = status_matrix[i][ch_num - 1][0]60 max_end_status = i61 # æ ¹æ®ä¹åè®°å½çå¾åæ¾æä¼è·¯å¾62 best_status_lst = [0 for ch in range(ch_num)]63 best_status_lst[ch_num - 1] = max_end_status64 c = ch_num - 165 cur_best_status = max_end_status66 while c > 0:67 pre_best_status = status_matrix[cur_best_status][c][1]68 best_status_lst[c - 1] = pre_best_status69 cur_best_status = pre_best_status70 c -= 171 # å®ç°åè¯72 rest = ''73 rest += ch_lst[0]74 for i in range(1, ch_num):75 # i-1æ¯EãSæè
iæ¯BãS76 if best_status_lst[i - 1] in {2, 3} or best_status_lst[i] in {0, 3}:77 rest += sep78 rest += ch_lst[i]79 return rest80# æ¦çç¸åæ¶çå¤ç81def veterbisame(sentence, sep=' '):82 ch_lst = get_word_ch(sentence)83 # print(ch_lst)84 # ææ¬åºåçé¿åº¦T85 ch_num = len(ch_lst)86 # åå§åç¶æç©éµ87 status_matrix = [[[0.0, 0] for col in range(ch_num)] for row in range(STATUS_NUM)]88 # ç¶æç§ç±»M个 m*189 for i in range(STATUS_NUM):90 # 第ä¸ä¸ªåçåå°æ¦ç91 if B[i].get(ch_lst[0], -1) != -1:92 cur_B = B[i][ch_lst[0]]93 else:94 cur_B = -100000.095 # Ïå¯ä»¥ç´æ¥ä½¿ç¨96 cur_pi = pi[i]97 status_matrix[i][0][0] = cur_pi + cur_B # åäºlogï¼ç¸ä¹å°±æ¯ç¸å 98 status_matrix[i][0][1] = i99 # M*M*(T-1)100 for i in range(1, ch_num): # 1:T => T-1101 for j in range(STATUS_NUM): # M102 max_p = None103 max_status = None104 # ç»è¿jèç¹çåM个èç¹ä¸åå¨æ¦çæ大å¼ï¼åä»ä¸ä¸å±è¿æ¥å°è¿ä¸ªèç¹çç¼å·105 for k in range(STATUS_NUM): # M åä¸å±ç第k个s106 cur_A = A[k][j] # 转移æ¦ç107 cur_p = status_matrix[k][i - 1][0] + cur_A108 if max_p is None or max_p < cur_p:109 max_p = cur_p110 max_status = k111 if B[j].get(ch_lst[i], -1) != -1:112 cur_B = B[j][ch_lst[i]]113 else:114 cur_B = -100000.0115 status_matrix[j][i][0] = max_p + cur_B116 status_matrix[j][i][1] = max_status117 last_prob_lst = []118 for i in range(STATUS_NUM):119 last_prob_lst.append((status_matrix[i][ch_num - 1][0], status_matrix[i][ch_num - 1][1],i))120 sorted_last_lst = sorted(last_prob_lst, key=lambda x: x[0], reverse=True)121 # print(sorted_last_lst)122 def get_best_seg(max_end_status):123 # æ ¹æ®ä¹åè®°å½çå¾åæ¾æä¼è·¯å¾124 best_status_lst = [0 for ch in range(ch_num)]125 best_status_lst[ch_num - 1] = max_end_status126 c = ch_num - 1127 cur_best_status = max_end_status128 while c > 0:129 pre_best_status = status_matrix[cur_best_status][c][1]130 best_status_lst[c - 1] = pre_best_status131 cur_best_status = pre_best_status132 c -= 1133 # å®ç°åè¯134 rest = ''135 rest += ch_lst[0]136 for i in range(1, ch_num):137 # i-1æ¯EãSæè
iæ¯BãS138 if best_status_lst[i - 1] in {2, 3} or best_status_lst[i] in {0, 3}:139 rest += sep140 rest += ch_lst[i]141 return rest142 i = 0143 res = []144 max_end_p = sorted_last_lst[0][0]145 while (max_end_p - sorted_last_lst[i][0]) < filter_val:146 # è°ç¨è·åæä¼ååæ¹æ³147 max_end_status = sorted_last_lst[i][2]148 res.append(get_best_seg(max_end_status))149 i += 1150 return res151if __name__ == '__main__':152 s = 'å¾å°æ¦çæ大çç¶æç»åæ¹å¼'153 s1 = 'ç»å·´åè¯éä¼ç»åè¯æ§ï¼ç¶æä¼æ´å¤'154 s2 = ''...
linear_svm.py
Source:linear_svm.py
1import numpy as np2from sklearn import svm3from sklearn import preprocessing4from sklearn import utils5original_data_matrix = np.loadtxt("data_simulation.txt")6total_status = len(original_data_matrix)7def mapping(i):8 result = np.zeros(shape=6)9 return result10def status_matrix_transfer(origin, pos_flag):11 index = 012 for i in range(total_status):13 if pos_flag:14 if origin[i][7] > 0.5:15 index += 116 else:17 if origin[i][7] < 0.5:18 index += 119 status_matrix = np.zeros(shape=(index, 6))20 index = 021 for i in range(total_status):22 if pos_flag:23 if origin[i][7] < 0.5:24 continue25 else:26 if origin[i][7] > 0.5:27 continue28 status_matrix[index][0] = origin[i][0]29 status_matrix[index][1] = origin[i][3]30 status_matrix[index][2] = origin[i][1]31 status_matrix[index][3] = origin[i][4]32 status_matrix[index][4] = origin[i][2]33 status_matrix[index][5] = origin[i][5]34 # status_matrix[index][6] = origin[i][7]35 index += 136 return status_matrix37def input_matrix_transfer(origin):38 status_matrix = np.zeros(shape=(total_status, 6))39 for i in range(total_status):40 status_matrix[i][0] = origin[i][0]41 status_matrix[i][1] = origin[i][3]42 status_matrix[i][2] = origin[i][1]43 status_matrix[i][3] = origin[i][4]44 status_matrix[i][4] = origin[i][2]45 status_matrix[i][5] = origin[i][5]46 # status_matrix[index][6] = origin[i][7]47 return status_matrix48def output_matrix_transfer(origin):49 status_matrix = np.zeros(shape=total_status)50 for i in range(total_status):51 status_matrix[i] = origin[i][7]52 # status_matrix[index][6] = origin[i][7]53 return status_matrix54pos = status_matrix_transfer(original_data_matrix, 1)55neg = status_matrix_transfer(original_data_matrix, 0)56X = input_matrix_transfer(original_data_matrix)57Y = output_matrix_transfer(original_data_matrix)58lab_enc = preprocessing.LabelEncoder()59encoded = lab_enc.fit_transform(Y)60clf = svm.SVC()61print("ææ¥")62print(utils.multiclass.type_of_target(encoded))63clf.fit(X, encoded)64print("ææ¥")...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!