How to use class_method_meta method in pandera

Best Python code snippet using pandera_python

generate_graph.py

Source:generate_graph.py Github

copy

Full Screen

1import argparse2import json3from multiprocessing import Pool4from networkx.drawing import nx_agraph5import networkx as nx6from pandas.io import parsers7import pygraphviz8import torch9import numpy as np10from torch_geometric.data import Data11import os.path as osp12import pandas as pd13import collections14import tqdm15from utils.TokenizerW2V import TokenIns16import os17import pandas as pd18Declariation=1319VariableDeclaration=320word2vec_file="tokens/jars/emb_100.txt"21tokenizer_file="tokens/jars/fun.model"22def nx_to_graph_data_obj_simple(G):23 """24 Converts nx graph to pytorch geometric Data object. Assume node indices25 are numbered from 0 to num_nodes - 1. NB: Uses simplified atom and bond26 features, and represent as indices. NB: possible issues with27 recapitulating relative stereochemistry since the edges in the nx28 object are unordered.29 :param G: nx graph obj30 :return: pytorch geometric Data object31 """32 # nodes, node feat, node type33 atom_features_list = []34 atom_node_type_list = []35 variable_declar_mask_list = []36 ins_length_list = []37 for _, node in G.nodes(data=True):38 atom_feature = node['feat']39 atom_features_list.append(atom_feature)40 atom_node_type_list.append( node['type'] )41 ins_length_list.append( node["ins_length"] )42 if node['type'] == Declariation:43 variable_declar_mask_list.append( True )44 else:45 variable_declar_mask_list.append( False ) 46 x = torch.tensor(np.array(atom_features_list), dtype=torch.long )47 x_type = torch.tensor(np.array(atom_node_type_list), dtype=torch.long )48 ins_length = torch.tensor( np.array( ins_length_list), dtype=torch.long )49 variable_declar_mask = torch.tensor(np.array(variable_declar_mask_list ) , dtype=bool)50 # edges, edge type51 dv_mask_list = []52 if len(G.edges()) > 0: 53 edges_list = []54 edge_label_list = []55 edge_node_type_list = []56 for i, j, edge in G.edges(data=True):57 edge_type = edge['label'] 58 edges_list.append((i, j))59 edge_node_type_list.append(edge['type'])60 edge_label_list.append(edge_type)61 # edges_list.append((j, i))62 # edge_type_list.append(edge_type)63 if edge_type == VariableDeclaration:64 dv_mask_list.append(True)65 else:66 dv_mask_list.append(False)67 68 # data.edge_index: Graph connectivity in COO format with shape [2, num_edges]69 edge_index = torch.tensor(np.array(edges_list).T, dtype=torch.long )70 dv_mask = torch.tensor(np.array(dv_mask_list), dtype=bool)71 # data.edge_attr: Edge feature matrix with shape [num_edges, num_edge_features]72 # edge_type_list_np = np.array(edge_type_list)73 # print(np.array(edge_type_list))74 edge_attr = torch.tensor(np.array(edge_label_list), dtype=torch.long)75 edge_node_attr = torch.tensor(np.array(edge_node_type_list), dtype=torch.long)76 #print(edge_attr.size())77 else: # mol has no bonds78 edge_index = torch.empty((2, 0), dtype=torch.long)79 edge_attr = torch.empty((0), dtype=torch.long)80 edge_node_attr = torch.empty((2, 0), dtype=torch.long)81 dv_mask = torch.empty((0), dtype=torch.long)82 assert edge_index.shape[-1] == len(edge_attr), f"{edge_index}, {edge_attr.shape}, {len(G.nodes()) }"83 data = Data(x=x, edge_index=edge_index, edge_attr=edge_attr)84 data.node_type = x_type85 data.variable_declar_mask = variable_declar_mask86 data.dv_mask = dv_mask87 data.ins_length = ins_length88 return data89 90def preprocess(class_method_id_json, graph_json , rawins_json91 , nodetype, edgetype, tokenizer_word2vec, dataname, outputdir, mid_list):92 class_method_meta = json.load( open( class_method_id_json ) )93 inputgraph_meta = json.load( open( graph_json ) )94 rawins_meta = json.load( open( rawins_json ) )95 graph_id = [ ]96 graph_labels = []97 data_list = []98 class_file = collections.defaultdict(int)99 100 print(class_method_id_json)101 for class_name in tqdm.tqdm( class_method_meta):102 class_file[class_name.split("$")[0]+".java"]+=1103 for method_id in class_method_meta[ class_name ]:104 if method_id not in mid_list:105 continue106 metthod_meta = class_method_meta[ class_name ][ method_id ]107 if metthod_meta["name"] == "<init>":108 continue109 110 graph_string = inputgraph_meta[ method_id ]111 instructions = {}112 if method_id not in rawins_meta:113 print(f"{class_method_id_json}, {method_id} ")114 instructions.update( rawins_meta[ method_id ]["Local"] )115 instructions.update( rawins_meta[ method_id ]["Unit"] )116 g_meta = nx_agraph.from_agraph(pygraphviz.AGraph(graph_string, directed=True))117 if len(g_meta.nodes) == 0:118 continue119 simple_graph = nx.DiGraph()120 121 id_rename_mapping = {}122 for (nid, info) in list( g_meta.nodes(data=True) ):123 ntype = int( nodetype[ info["type"] ] )124 ins = instructions[nid]125 subwordsOfins = tokenizer_word2vec.get_tokens_id(ins.strip())126 feat = subwordsOfins + [ 0 ] * (100 - len(subwordsOfins) ) if len(subwordsOfins) < 100 else subwordsOfins[:100]127 simple_graph.add_node(nid, type= ntype, feat=feat,ins_length = min(100, len(subwordsOfins) ))128 for e1, e2, a in g_meta.edges( data=True ):129 if len( g_meta[e1][e2] ) > 1:130 #print( g_meta[e1][e2] )131 paralle_edges=[ edgetype[ g_meta[e1][e2][i]["label"]] for i in g_meta[e1][e2] ]132 paralle_edges = sorted(paralle_edges)133 if paralle_edges == [0, 1]:134 etype = int( edgetype[ "DataDependence|ControlDependence" ] ) 135 if paralle_edges == [0, 2]:136 etype = int( edgetype[ "Controlfow|DataDependence" ] )137 if paralle_edges == [1, 2]:138 etype = int( edgetype[ "Controlfow|ControlDependence" ] ) 139 if paralle_edges == [0, 1, 2]:140 etype= int( edgetype[ "Controlfow|ControlDependence|ControlDependence" ] )141 else:142 etype = int( edgetype[ a["label"] ] ) 143 simple_graph.add_edge( e1, e2, label=etype, type=[int(simple_graph.nodes[e1]["type"]), int(simple_graph.nodes[e2]["type"])] )144 145 # rename id from 0 to number of nodes - 1146 counter = 0147 for i in sorted(simple_graph):148 id_rename_mapping[i] = counter149 counter += 1150 simple_graph = nx.relabel_nodes( simple_graph, id_rename_mapping, copy=False )151 data_geometric = nx_to_graph_data_obj_simple( simple_graph )152 data_geometric.y = int(mid_list[method_id]["IsFlaky"])153 data_geometric.id = int(mid_list[method_id]["Counter"])154 # print(data_geometric.y )155 data_geometric.id = int(method_id) 156 data_list.append( data_geometric )157 graph_id.append( int(method_id) )158 del g_meta159 del simple_graph160 161 # torch.save([data_list,graph_labels, graph_id ], osp.join(outputdir, f"{dataname}.pt"))162 # json.dump(class_file, open( osp.join(outputdir, "class_file.json") , "w"))163 return [data_list,graph_labels, graph_id ]164def task(outputdir, dataname):165 df = pd.read_csv("graph_info.csv", index_col=0)166 id_list = {}167 counter = 0 168 data_list,graph_labels, graph_id = [], [], []169 for inputdir, df_group in df.groupby("project"):170 print(inputdir)171 mid_list = {}172 for index, row in df_group.iterrows():173 methodid = row["Method"]174 testname = row["Test"]175 isflaky = row["IsFlaky"]176 project = row["project"]177 mid_list[str(methodid)] = {"IsFlaky":isflaky, "Test":testname, "Counter":counter, "project":project }178 id_list[testname+"_"+str(methodid)] = {"Method":methodid, "IsFlaky":isflaky, "Test":testname, "Counter":counter, "project":project }179 counter += 1180 tokenizer_word2vec = TokenIns(181 word2vec_file=word2vec_file,182 tokenizer_file=tokenizer_file183 )184 class_method_id_json = os.path.join( inputdir, "class_method_id_mapping.json" )185 graph_json = os.path.join(inputdir, f"{dataname}.json")186 rawins_json = os.path.join(inputdir, "RawIns.json")187 nodetype = json.load( open("tokens/instruction_type.json") )188 edgetype = json.load( open("tokens/edge_type.json") )189 data = preprocess(class_method_id_json, graph_json, rawins_json, nodetype, edgetype, tokenizer_word2vec, dataname, outputdir, mid_list )190 data_list.extend(data[0])191 graph_labels.extend(data[1])192 graph_id.extend(data[2])193 json.dump( id_list, open("id_mapping.json", "w"), indent=6 )194 torch.save([data_list,graph_labels, graph_id ], osp.join(outputdir, f"{dataname}.pt"))195 return len(data_list)196from argparse import ArgumentParser197if __name__ == "__main__":198 parser = argparse.ArgumentParser()199 parser.add_argument("-o", "--output", dest="output", default="")200 args = parser.parse_args()201 202 203 outputdir = args.output204 os.makedirs( outputdir, exist_ok=True)205 for dataname in [ "DU_CFG", "DV_CFG", "DV_PDG", "DVDU_CFG", "ORG_CFG", "ORG_PDG" ]:206 task( outputdir, dataname )207 ...

Full Screen

Full Screen

test_inspection_utils.py

Source:test_inspection_utils.py Github

copy

Full Screen

...16 def regular_method_meta(cls):17 return cls1819 @classmethod20 def class_method_meta(mcs):21 return mcs2223 @staticmethod24 def static_method_meta():25 return 1262728class SomeClass(metaclass=SomeMeta):29 def regular_method(self):30 return self3132 @classmethod33 def class_method(cls):34 return cls ...

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 pandera 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