How to use code2string method in fMBT

Best Python code snippet using fMBT_python

util.py

Source:util.py Github

copy

Full Screen

...88 parent_name=name89 node_3thlayer=dict(name=parent_name,children=[])90 node_2thlayer['children'].append(node_3thlayer)91 for children in value:92 node_4thlayer=dict(name=code2string(children,curs),children=[])93 node_3thlayer['children'].append(node_4thlayer)94 else:95 # three layer96 97 node_2thlayer=dict(name=parent_name,children=[])98 snomed_hierarchy['children'].append(node_2thlayer)99 for concept in cluster_map[cluster_map['cluster']==i]['concept_name']:100 node_3thlayer=dict(name=concept,children=[])101 node_2thlayer['children'].append(node_3thlayer) 102 json.dump(snomed_hierarchy, open(os.path.join("static","data","snomed_hierarchy_four_layer_tree.json"), "w"))103# three-layer-tree104def build_snomed_hierarchy_three_layer_tree(snomed_codes,curs,p2c):105 106 node_dic={}107 for code in snomed_codes:108 concept=code2string(code,curs)109 top_concept=code2topconcept(code,curs)110 if concept !="":111 if top_concept not in node_dic:112 node_dic[top_concept]=[concept]113 elif concept not in node_dic[top_concept]:114 node_dic[top_concept].append(concept)115 snomed_hierarchy={'name':'snomed','children':[]}116 for key,value in node_dic.items():117 node_2thlayer=dict(name=key,children=[])118 snomed_hierarchy['children'].append(node_2thlayer)119 for each in value:120 node_3rdlayer=dict(name=each,children=[])121 node_2thlayer['children'].append(node_3rdlayer)122 json.dump(snomed_hierarchy, open(".\\static\\data\\snomed_hierarchy_three_layer_tree.json", "w"))123# first version four layer tree- seperate 124def build_snomed_hierarchy_four_layer_tree_no_name(snomed_codes,top_level,curs,p2c):125 #check the number of independent cluster:126 # find cluster127 cluster_dic={}128 model=snomed_embedding()129 node_dic={}130 for top_code in top_level:131 node_dic[top_code]=[]132 childrens=p2c[top_code]133 for children in childrens:134 get_all_the_children_concept(top_code,children,p2c,snomed_codes,node_dic)135 snomed_hierarchy={'name':'snomed','children':[]}136 for key, value in node_dic.items():137 top_concept=code2string(key,curs)138 X = np.zeros(shape=(1,200))139 snomed_vector={}140 label_list=[]141 for code in value:142 concept =code2string(code,curs)143 if concept !="" and code in model.keys():144 label_list.append(concept)145 X=np.concatenate((X,model[code].reshape(1,200)),axis=0) 146 147 X=np.delete(X,0,axis=0)148 if(len(X)!=0):149 cluster_num=int(len(value)/3)+1150 kmeans = KMeans(n_clusters=cluster_num, init='k-means++', max_iter=300, n_init=10, random_state=0)151 pred_y = kmeans.fit_predict(X)152 cluster_map = pd.DataFrame()153 cluster_map['concept_name'] = label_list154 cluster_map['cluster'] = kmeans.labels_155 156 node_2thlayer=dict(name=top_concept,children=[])157 snomed_hierarchy['children'].append(node_2thlayer)158 for i in range(cluster_num):159 node_3thlayer=dict(name="",children=[])160 node_2thlayer['children'].append(node_3thlayer)161 for each in cluster_map[cluster_map.cluster == i]['concept_name']:162 node_4rdlayer=dict(name=each,children=[])163 node_3thlayer['children'].append(node_4rdlayer)164 cluster_dic[top_concept]=cluster_map165 json.dump(snomed_hierarchy, open(".\\static\\data\\snomed_hierarchy_four_layer_tree.json", "w"))166 return cluster_dic167 # three-layer-tree168def build_snomed_hierarchy_three_layer_tree(snomed_codes,curs,p2c):169 170 node_dic={}171 for code in snomed_codes:172 concept=code2string(code,curs)173 top_concept=code2topconcept(code,curs)174 if concept !="":175 if top_concept not in node_dic:176 node_dic[top_concept]=[concept]177 elif concept not in node_dic[top_concept]:178 node_dic[top_concept].append(concept)179 snomed_hierarchy={'name':'snomed','children':[]}180 for key,value in node_dic.items():181 node_2thlayer=dict(name=key,children=[])182 snomed_hierarchy['children'].append(node_2thlayer)183 for each in value:184 node_3rdlayer=dict(name=each,children=[])185 node_2thlayer['children'].append(node_3rdlayer)186 json.dump(snomed_hierarchy, open(".\\static\\data\\snomed_hierarchy_three_layer_tree.json", "w"))187 # the original dag188def build_snomed_hierarchy_dag(snomed_codes,top_level,curs,p2c):189 190 edge={}191 tagged_nodes={}192 with open(".\\static\\data\\DAG\\dag.dot", "w") as file:193 file.write('digraph g {'+'\n')194 for top_code in top_level:195 add_snomed_node_for_dag(top_code,'snomed',file,curs,p2c,edge,snomed_codes,tagged_nodes)196 file.write('}')197 198 #json.dump(edge, open(".\\static\\data\\edge.json", "w"))199def build_snomed_hierarchy_tree(snomed_codes,top_level,curs,p2c):200 201 snomed_hierarchy={'name':'snomed','children':[]}202 for top_code in top_level:203 add_snomed_node(top_code,snomed_hierarchy,curs,p2c)204 205 json.dump(snomed_hierarchy, open(".\\static\\data\\snomed_hierarchy.json", "w"))206 def add_snomed_node_for_dag_three_layers(top_concept,node,file,curs,p2c,snomed_codes,node_dic):207 if node not in node_dic.keys():208 concept=code2string(node,curs)209 if concept !='':210 if node in snomed_codes:211 concept=code2string(node,curs)212 file.write(top_concept+'->'+concept+';\n')213 node_dic[node]=1214 if node in p2c.keys():215 childrens=p2c[node]216 for children in childrens:217 add_snomed_node_for_dag_three_layers(top_concept,children,file,curs,p2c,snomed_codes,node_dic)218'''219three layer built using dot220def build_snomed_hierarchy_three_layer_dag(snomed_codes,top_level,curs,p2c):221 node_dic={}222 with open(".\\static\\data\\dag_three_layers.dot", "w") as file:223 file.write('digraph g {'+'\n')224 for top_code in top_level:225 concept=code2string(top_code,curs)226 file.write('snomed->'+concept+';\n')227 childrens=p2c[top_code]228 for children in childrens:229 add_snomed_node_for_dag_three_layers(concept,children,file,curs,p2c,snomed_codes,node_dic)230 file.write('}')231def add_snomed_node_for_dag_three_layers(top_concept,node,file,curs,p2c,snomed_codes,node_dic):232 if node not in node_dic.keys():233 concept=code2string(node,curs)234 if concept !='':235 if node in snomed_codes:236 concept=code2string(node,curs)237 file.write(top_concept+'->'+concept+';\n')238 node_dic[node]=1239 if node in p2c.keys():240 childrens=p2c[node]241 for children in childrens:242 add_snomed_node_for_dag_three_layers(top_concept,children,file,curs,p2c,snomed_codes,node_dic)243#dag 0244def build_clinical_finding_cluster_dag(snomed_codes,cluster_dic,curs,p2c):245 edge={}246 tagged_nodes={}247 color=['red','blue','orange','green','yellow','gray']248 with open(".\\static\\data\\DAG\\dag_clinical_finding.dot", "w") as file:249 file.write('digraph g {'+'\n')250 add_snomed_node_for_dag_non_color('404684003','snomed',file,curs,p2c,edge,snomed_codes,tagged_nodes)251 #color cluster252 top_concept=code2string('404684003',curs)253 cluster_map=cluster_dic[top_concept]254 255 for each in cluster_map.itertuples():256 concept=each[1]257 cluster=each[2]258 file.write(concept+' [style=filled, fillcolor='+color[cluster]+'];\n')259 file.write('}')260# dag 1261def build_substance_pharmecutical_dag(snomed_codes,cluster_dic,curs,p2c):262 edge={}263 tagged_nodes={}264 color=['aliceblue','antiquewhite','cyan', 'aquamarine','springgreen' ,'turquoise','violet','yellow','yellowgreen','tomato','darkorange','darkorchid','maroon','darksalmon','darkseagreen','darkslateblue','darkslategray','green','greenyellow','skyblue','goldenrod','lightslategray','mediumblue','royalblue','red','mediumvioletred']265 with open(".\\static\\data\\DAG\\substance_pharmaceutical.dot", "w") as file:266 file.write('digraph g {'+'\n')267 for top_code in ['105590001','373873005']:268 add_snomed_node_for_dag_non_color(top_code,'snomed',file,curs,p2c,edge,snomed_codes,tagged_nodes)269 270 271 node_dic={}272 for top_code in ['105590001','373873005']:273 node_dic[top_code]=[]274 childrens=p2c[top_code]275 for children in childrens:276 get_all_the_children_concept(top_code,children,p2c,snomed_codes,node_dic)277 model=snomed_embedding()278 X = np.zeros(shape=(1,200))279 label_list=[]280 for key, value in node_dic.items():281 for code in value:282 concept =code2string(code,curs)283 if concept !="" and code in model.keys():284 label_list.append(concept)285 X=np.concatenate((X,model[code].reshape(1,200)),axis=0) 286 X=np.delete(X,0,axis=0)287 if(len(X)!=0):288 cluster_num=int(len(label_list)/3)+1289 kmeans = KMeans(n_clusters=cluster_num, init='k-means++', max_iter=300, n_init=10, random_state=0)290 pred_y = kmeans.fit_predict(X)291 cluster_map = pd.DataFrame()292 cluster_map['concept_name'] = label_list293 cluster_map['cluster'] = kmeans.labels_294 #color cluster295 296 for each in cluster_map.itertuples():297 concept=each[1]298 cluster=each[2]299 file.write(concept+' [style=filled, fillcolor='+color[cluster]+'];\n')300 file.write('}')301# dag 2302'''303def word2aui(word):304 #word2sui={}305 #for word in words:306 #better use API?307 f = open("MRCONSO.RRF",'rb')308 aui_index={}309 for line in f.readlines():310 line=str(line)311 content = re.compile("^b'(.*)'$", re.S)312 content=re.findall(content, line)313 if (content==[]):314 content = re.compile('^b\"(.*)\"$', re.S)315 content=re.findall(content, line)316 content=content[0].strip('\\n')317 content=content.split('|')318 string=content[14].lower()319 if re.search(word,string):320 aui_index[content[7]]={}321 aui_index[content[7]]['CUI']=content[0]322 aui_index[content[7]]['LAT']=content[1]323 aui_index[content[7]]['TS']=content[2]324 aui_index[content[7]]['LUI']=content[3]325 aui_index[content[7]]['STT']=content[4]326 aui_index[content[7]]['SUI']=content[5]327 aui_index[content[7]]['ISPREF']=content[6]328 aui_index[content[7]]['SAUI']=content[8]329 aui_index[content[7]]['SCUI']=content[9]330 aui_index[content[7]]['SDUI']=content[10]331 aui_index[content[7]]['SAB']=content[11]332 aui_index[content[7]]['TTY']=content[12]333 aui_index[content[7]]['CODE']=content[13]334 aui_index[content[7]]['STR']=content[14]335 aui_index[content[7]]['SRL']=content[15]336 aui_index[content[7]]['SUPPRESS']=content[16]337 aui_index[content[7]]['CVF']=content[17]338 return aui_index339 340def sui2cui(aui_index):341 sui2cui={}342 for item in aui_index.values():343 if item['SUI']+' '+item['STR'] not in sui2cui.keys():344 345 sui2cui[item['SUI']+' '+item['STR']]=[item['CUI']]346 else:347 sui2cui[item['SUI']+' '+item['STR']].append(item['CUI'])348 return sui2cui349 def add_node(node, parent ):350 # First create the new node and append it to its parent's children351 newNode = dict( node_id=node.id, children=[] )352 parent["children"].append( newNode )353 # Recursively add the current node's children354 if node.left: add_node( node.left, newNode )355 if node.right: add_node( node.right, newNode )356def label_tree( n ,id2name):357 # If the node is a leaf, then we have its name358 if len(n["children"]) == 0:359 n["name"] = id2name[n["node_id"]] 360 if len(n["children"])==1:361 label_tree(n["children"][0],id2name)362 n["name"] = ""363 if len(n["children"])==2:364 label_tree(n["children"][0],id2name)365 label_tree(n["children"][1],id2name)366 n["name"] = ""367 368 # If not, flatten all the leaves in the node's subtree369 #else:370 # leafNames = reduce(lambda ls, c: ls + label_tree(c,id2name), n["children"], [])371 # Delete the node id since we don't need it anymore and372 # it makes for cleaner JSON373 del n["node_id"]374 # Labeling convention: "-"-separated leaf names375 #n["name"] = name = "-".join(sorted(map(str, leafNames)))376 377 def add_snomed_node_for_dag(node,parent,file,curs,p2c,edge_dic,snomed_codes,tagged_nodes):378 379 concept=code2string(node,curs)380 if concept !="":381 edge=parent+concept382 if edge not in edge_dic.keys():383 file.write(parent+'->'+concept+';\n')384 if node in snomed_codes and node not in tagged_nodes.keys():385 file.write(concept+' [style=filled, fillcolor=green];\n')386 tagged_nodes[node]=1387 edge_dic[edge]=1388 if node in p2c.keys():389 childrens=p2c[node]390 for children in childrens:391 add_snomed_node_for_dag(children,concept,file,curs,p2c,edge_dic,snomed_codes,tagged_nodes)392 def add_snomed_node(node,parent,curs,p2c):393 concept=code2string(node,curs)394 if concept !="" :395 newNode=dict(name=concept,children=[])396 parent["children"].append(newNode)397 if node in p2c.keys():398 childrens=p2c[node]399 for children in childrens:400 add_snomed_node(children,newNode,curs,p2c)401 def snomed_collocation(parsed_result,curs):402 '''403 word_frequency={}404 collocation_frequency={}405 406 for i in range(len(parsed_result['docs'])):407 for j in range(len(parsed_result['docs'][i]['code'])):408 cui_1=parsed_result['docs'][i]['code'][j]409 # calculate word frequency410 if (cui_1 not in word_frequency.keys()):411 word_frequency[cui_1]=1412 else:413 word_frequency[cui_1]+=1414 # calculate collocation frequency415 for cui_2 in parsed_result['docs'][i]['code'][j+1:]:416 if (cui_1,cui_2) in collocation_frequency.keys():417 collocation_frequency[(cui_1,cui_2)]+=1418 elif (cui_2,cui_1) in collocation_frequency.keys():419 collocation_frequency[(cui_2,cui_1)]+=1420 else:421 collocation_frequency[(cui_1,cui_2)]=1422 #calculate PMI:423 PMI={}424 for key,value in collocation_frequency.items():425 print(key)426 '''427 model=snomed_embedding()428 label_list=[]429 lacked_label_list=[]430 matrix = np.zeros(shape=(1,200))431 snomed_vector={}432 snomed_codes=[]433 434 for i in range(len(parsed_result['docs'])):435 print("process {} documents".format(i))436 437 for snomed_code in parsed_result['docs'][i]['code']:438 concept=code2string(snomed_code,curs)439 if concept !="" :440 if snomed_code not in snomed_codes:441 snomed_codes.append(snomed_code)442 if concept not in snomed_vector.keys():443 if snomed_code in model.keys():444 snomed_vector[concept]=model[snomed_code].reshape(1,200) 445 elif snomed_code not in lacked_label_list:446 lacked_label_list.append(snomed_code)447 for key,value in snomed_vector.items():448 label_list.append(key)449 matrix=np.concatenate((matrix,value),axis=0)450 matrix=np.delete(matrix,0,axis=0)451 clusters = linkage(matrix, 'single')452 #dendrogram(linked,orientation='top',labels=label_list,distance_sort='ascending', orientation = 'right')453 454 T = to_tree( clusters , rd=False )455 d3Dendro = dict(children=[], name="cui-code")456 add_node( T, d3Dendro ) 457 id2name = dict(zip(range(len(label_list)), label_list)) 458 label_tree( d3Dendro["children"][0], id2name)459 # Output to JSON460 json.dump(d3Dendro, open(".\\static\\data\\snomed_cluster.json", "w"), sort_keys=True, indent=4)461 return snomed_codes462 def cui_collocation(parsed_result):463 model=cui_embedding()464 label_list=[]465 lacked_label_list=[]466 matrix = np.zeros(shape=(1,500))467 API_KEY="35320203-e665-46d7-b21f-0c1fa1260f22"468 result=requests.post("https://utslogin.nlm.nih.gov/cas/v1/api-key",{'apikey':API_KEY}).text469 soup=Soup(result)470 TGT_KEY=soup.form['action']471 cui_vector={}472 for i in range(len(parsed_result['docs'])):473 print("process {} documents".format(i))474 475 for cui in parsed_result['docs'][i]['cui']:476 concept=cui2word(cui,TGT_KEY)477 if concept not in cui_vector.keys():478 if cui in model.index:479 cui_vector[concept]=model.loc[cui].as_matrix().reshape(1,500) 480 elif cui not in lacked_label_list:481 lacked_label_list.append(cui)482 for key,value in cui_vector.items():483 label_list.append(key)484 matrix=np.concatenate((matrix,value),axis=0)485 matrix=np.delete(matrix,0,axis=0)486 clusters = linkage(matrix, 'single')487 #dendrogram(linked,orientation='top',labels=label_list,distance_sort='ascending', orientation = 'right')488 T = to_tree( clusters , rd=False )489 d3Dendro = dict(children=[], name="cui-code")490 add_node( T, d3Dendro ) 491 id2name = dict(zip(range(len(label_list)), label_list)) 492 label_tree( d3Dendro["children"][0], id2name)493 # Output to JSON494 json.dump(d3Dendro, open(".\\static\\data\\d3-dendrogram.json", "w"), sort_keys=True, indent=4)495def snomed_collocation(parsed_result,curs):496 497 model=snomed_embedding()498 label_list=[]499 lacked_label_list=[]500 matrix = np.zeros(shape=(1,200))501 snomed_vector={}502 snomed_codes=[]503 504 for i in range(len(parsed_result['docs'])):505 print("process {} documents".format(i))506 507 for snomed_code in parsed_result['docs'][i]['code']:508 concept=code2string(snomed_code,curs)509 if concept !="" :510 if snomed_code not in snomed_codes:511 snomed_codes.append(snomed_code)512 if concept not in snomed_vector.keys():513 if snomed_code in model.keys():514 snomed_vector[concept]=model[snomed_code].reshape(1,200) 515 elif snomed_code not in lacked_label_list:516 lacked_label_list.append(snomed_code)517 for key,value in snomed_vector.items():518 label_list.append(key)519 matrix=np.concatenate((matrix,value),axis=0)520 matrix=np.delete(matrix,0,axis=0)521 clusters = linkage(matrix, 'single')522 #dendrogram(linked,orientation='top',labels=label_list,distance_sort='ascending', orientation = 'right')...

Full Screen

Full Screen

pcaefin.py

Source:pcaefin.py Github

copy

Full Screen

...25 i += 126 string_list.append(string[:-4])27 return string_list28#将编码转换为字符29def code2string(code,code_len):30 string = ''31 for j in xrange(0,code_len,2):32 string += chr(int((code[j]+code[j+1]),16))33 return string34fl = walk('script')35for fn in fl:36 #过滤非txt文件37 if not fnmatch.fnmatch(fn,'*.txt'):38 continue39 print fn40 lines = codecs.open(fn,'rb','utf16').readlines()41 #获得文本列表42 string_list = makestr(lines)43 44 dst = open(fn[:-4],'rb+')45 start = 0x3395aa46 indexoffset = 0x7a8f0647 dst.seek(start)48 offset_list = []49 j = 050 #逐条写入文本列表里面的文本51 for string in string_list:52 offset = dst.tell()-start53 charlist = []54 len_string = len(string)55 i = 056 while i < len_string:57 char = string[i]58 59 if char == u'▲':60 charlist.append(code2string('07000800',8))61 elif char == u'△':62 charlist.append(code2string('07000900',8))63 elif char == u'◆':64 charlist.append(code2string('07000400',8))65 elif char == u'▽':66 charlist.append(code2string('0000',4))67 elif char == u'◎':68 charlist.append(code2string('07000600',8))69 elif char == u'▼':70 charlist.append(code2string('0a00',4))71 elif char == u'★':72 charlist.append(code2string('07000100',8))73 elif char == '\r':74 charlist.append(code2string('0d00',4))75 elif char == '\n':76 charlist.append(code2string('0a00',4))77 else:78 charlist.append(char.encode('utf-16-le'))79 i += 180 charlist.append(code2string('0000',4))81 code = ''.join(charlist)82 dst.write(code)83 offset_list.append(struct.pack('I',offset))84 85 dst.write(struct.pack('I', len(offset_list)))86 for of in offset_list:87 dst.write(of)...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run fMBT automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful