forked from GalateaWang/SGNs-master
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SGNs.py
128 lines (98 loc) · 3.25 KB
/
SGNs.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
# -*- coding: UTF-8 -*-
import argparse
import numpy as np
import networkx as nx
import classification
from itertools import chain
from sklearn.decomposition import PCA
import os
def parse_args():
'''
Parses the SGN arguments.
'''
parser = argparse.ArgumentParser(description="Run SGNs.")
parser.add_argument('--input', nargs='?', default='mutag',
help='Input graph path')
parser.add_argument('--label', nargs='?', default='mutag.Labels',
help='Input graph path')
parser.add_argument('--types', type=int, default=1,
help='Type of processing the features. Default is 1 or 2.')
parser.add_argument('--N', type=int, default=2, ## SGN0, SGN1
help='Number of convert to line graph. Default is 3.')
return parser.parse_args()
def read_graph(fullpath):
'''
Reads the input network in networkx.
'''
G = nx.read_edgelist(fullpath, delimiter=',', nodetype=int, data=(('weight',float),), create_using=nx.DiGraph())
return G
def read_label():
with open(args.label) as f:
singlelist = [line.strip()[-1] for line in f.readlines()]
labels = np.array(singlelist)
return labels
def feature_processing(all_features):
'''
:param all_features:
1:PCA;
2:get the mean value
:return: the reduced dimension network feature vector.
'''
if args.types == 1:
fea_list = []
for fea in all_features:
features = list(chain(*fea)) ## flatten a list
fea_list.append(features)
x = np.array(fea_list)
pca = PCA(n_components=11) # decided by the feature abstract method.
reduced_x = pca.fit_transform(x)
elif args.types == 2:
fea_list = []
for fea in all_features:
features = np.array(fea)
fea_list.append(list(np.mean(features, axis=0)))
reduced_x = np.array(fea_list)
elif args.types == 3:
fea_list = []
for fea in all_features:
features = list(chain(*fea)) ## flatten a list
fea_list.append(features)
reduced_x = np.array(fea_list)
else:
raise Exception("Invalid feature processing type!", type)
return reduced_x
def to_line(graph):
'''
:param graph
:return G_line: line/Subgraph network
'''
graph_to_line = nx.line_graph(graph)
graph_line = nx.convert_node_labels_to_integers(graph_to_line, first_label=0, ordering='default')
return graph_line
def main(args, fullpath):
'''
Transform graphs to different-order SGNs.
'''
nx_G = read_graph(fullpath)
cur= nx_G
sgn_features = list()
sgn_features.append(classification.character(nx_G)) ## add the original network feature
for n in range(args.N): ## line1,line2,line3
sgn = to_line(cur)
cur = sgn
feature = classification.character(cur) ## get the feature of one network
sgn_features.append(feature)
return sgn_features
if __name__ == "__main__":
args = parse_args()
all_features = []
files = os.listdir(args.input)
files.sort(key=lambda x: int(x.split('.')[0]))
for path in files:
full_path = os.path.join(args.input, path)
print('num_graph:', path)
features_list = main(args, full_path)
all_features.append(features_list)
reduced_x = feature_processing(all_features)
labels = read_label()
classification.result_class(reduced_x, labels)