-
Notifications
You must be signed in to change notification settings - Fork 6
/
calculate_buglocator_graph_features.py
executable file
·136 lines (106 loc) · 4.19 KB
/
calculate_buglocator_graph_features.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Usage: %(scriptName) <tokenized_source_snapshot_file> <data_prefix>
Requires results of "java-ast-extractor-graph-notes.jar" in repository
"""
import collections as col
import json
from timeit import default_timer
import datetime
import networkx as nx
import sys
from scipy import sparse
from project_import_graph_features import process_graph
def main():
print("Start", datetime.datetime.now().isoformat())
before = default_timer()
tokenized_source_snapshot_file = sys.argv[1]
print("tokenized source snapshot file", tokenized_source_snapshot_file)
data_prefix = sys.argv[2]
print("data_prefix", data_prefix)
soruce_snapshot = load_source_snapshot_file(tokenized_source_snapshot_file)
calculate_graph_features(data_prefix, soruce_snapshot)
after = default_timer()
total = after - before
print("End", datetime.datetime.now().isoformat())
print("total time ", total)
def load_source_snapshot_file(soruce_snapshot_file_path):
with open(soruce_snapshot_file_path, 'r') as infile:
source_snapshot = json.load(infile)
return source_snapshot
def calculate_graph_features(data_prefix, soruce_snapshot):
graph_features_data_list = []
graph_features_lookup = {}
file_to_imports = {}
file_to_class_name = {}
for file_index in soruce_snapshot:
imports = soruce_snapshot[file_index]['graph']
file_to_imports[file_index] = imports
if 'className' in imports and imports['className'] is not None and imports['className'] != "":
class_name = imports['className']
class_name = class_name.replace(".", "")
file_to_class_name[file_index] = class_name
graph_data = process_graph_results(file_to_imports)
current_index = 0
for file_index in soruce_snapshot:
try:
current_node_name = file_to_class_name[file_index]
# print(current_node_name)
# print(graph_data.loc[current_node_name])
# exit(0)
values = graph_data.loc[current_node_name]
feature_15 = values['in']
feature_16 = values['out']
feature_17 = values['pr']
feature_18 = values['a']
feature_19 = values['h']
except KeyError:
feature_15 = 0.0
feature_16 = 0.0
feature_17 = 0.0
feature_18 = 0.0
feature_19 = 0.0
current_features = sparse.coo_matrix([feature_15, feature_16, feature_17, feature_18, feature_19])
# print(current_features.shape)
# print(current_features)
# exit(0)
graph_features_data_list.append(current_features)
graph_features_lookup[file_index] = current_index
current_index += 1
graph_features_data = sparse.vstack(graph_features_data_list)
sparse.save_npz(data_prefix + '_graph_features_data', graph_features_data)
with open(data_prefix + '_graph_features_index_lookup', 'w') as outfile:
json.dump(graph_features_lookup, outfile)
def process_graph_results(file_to_imports):
graph_preparation = col.defaultdict(list)
class_names_to_paths = {}
__c = 0
__d = 0
for file, class_details in file_to_imports.items():
__d += 1
try:
class_name = class_details['className']
class_name = class_name.replace(".", "")
except Exception:
__c += 1
continue
class_names_to_paths[class_name] = class_details['fileName']
dependencies = class_details['dependencies']
for dependency, _ in dependencies.items():
graph_preparation[class_name].append(dependency.replace(".", ""))
if len(graph_preparation[class_name]) == 0:
#print("AAAAAAAAAAAA", class_name)
del graph_preparation[class_name]
# print(graph_preparation)
G = nx.from_dict_of_lists(graph_preparation, nx.DiGraph())
# print(__c / __d)
# exit()
# print(G)
#print("Nodes", G.number_of_nodes())
#print("Edges", G.degree(weight='weight'))
# print(len(list(G.nodes())))
# print(list(G.nodes()))
#exit()
return process_graph(G)
if __name__ == '__main__':
main()