-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProcessingPatterns.py
More file actions
159 lines (146 loc) · 5.42 KB
/
Copy pathProcessingPatterns.py
File metadata and controls
159 lines (146 loc) · 5.42 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from networkx.algorithms import isomorphism
from networkx.algorithms.isomorphism import ISMAGS
import networkx as nx
import numpy as np
import re
import sys
def load_graphs(fileName,TAILLE):
"""Load graphs from a file.
args: fileName (string) : the name of the file)
TAILLE (int) : the number of graphs in the file
return: graphs (list of networkx graphs) : the list of graphs
numbers (list of list of int) : the list of occurences of each graph
"""
numbers = []
for i in range(TAILLE):
numbers.append([])
vertices = np.zeros(TAILLE)
labelVertices = []
edges = np.zeros(TAILLE)
labelEdges = []
compteur=-1
numero=0
file = open(fileName, "r")
for line in file:
a = line
b = a.split(" ")
if b[0]=="t":
compteur=compteur+1
labelVertices.append([])
labelEdges.append([])
val = b[2]
val = re.sub("\n","",val)
val = int(val)
numero = val
if b[0]=="v":
vertices[compteur]=vertices[compteur]+1
val = b[2]
val = re.sub("\n","",val)
val = int(val)
labelVertices[compteur].append(val)
if b[0]=="e":
edges[compteur]=edges[compteur]+1
num1 = int(b[1])
num2 = int(b[2])
val = b[3]
val = re.sub("\n","",val)
val = int(val)
labelEdges[compteur].append((num1,num2,val))
if b[0]=="x":
temp= []
for j in range(1,len(b)):
val = b[j]
val = re.sub("\n","",val)
if not(val=="#") and not(val==""):
val = int(val)
temp.append(val)
numbers[numero]=temp
Graphs = []
for i in range(len(vertices)):
dicoNodes = {}
Graphs.append(nx.Graph())
for j in range(int(vertices[i])):
#tempDictionnaireNodes = {"color":labelVertices[i][j]}
dicoNodes[j]=labelVertices[i][j]
for j in range(int(edges[i])):
Graphs[i].add_edge(labelEdges[i][j][0],labelEdges[i][j][1],color=labelEdges[i][j][2])
Graphs[i].add_nodes_from([(node, {'color': attr}) for (node, attr) in dicoNodes.items()])
return Graphs,numbers
def countSubgraph(Graphs,Subgraphs,id_graphs,mode,name):
""" Compute number of occurences and induced subgraphs
Inputs : Graphs (list of networkx graphs) : the list of graphs
Subgraphs (list of networkx graphs) : the list of patterns
id_graphs (list of list of int) : list of graphs containing each pattern
mode (string) : "mono" or "iso" : mono for monomorphism and iso for isomorphism
"""
file=open(name,"w")
com = -1
for subgraph in Subgraphs[0:len(Subgraphs)]:
com=com+1
results=np.zeros(len(Graphs))
cork = np.zeros(len(Graphs))
compt=-1
for graph in Graphs:
compt=compt+1
count=0
if compt in id_graphs[com]:
GM = nx.isomorphism.GraphMatcher(graph,subgraph,node_match=lambda n1,n2:n1['color']==n2['color'],edge_match= lambda e1,e2: e1['color'] == e2['color'])
listeNode=[]
temp = []
v = set()
node = set()
vertexpoint=0
if mode=="mono":
for m in GM.subgraph_monomorphisms_iter():
new=True
liste = []
for key in m:
liste.append(key)
if key not in listeNode:
temp.append(key)
else:
new=False
if new:
vertexpoint=vertexpoint+1
listeNode.append(key)
a = frozenset(tuple(liste))
v.add(a)
results[compt] = len(v)
if mode=="iso":
for m in GM.subgraph_isomorphisms_iter():
new=True
liste=[]
for key in m:
liste.append(key)
if key not in listeNode:
temp.append(key)
else:
new=False
if new:
vertexpoint=vertexpoint+1
listeNode.append(key)
a = frozenset(tuple(liste))
v.add(a)
results[compt] = len(v)
st="t # "+str(com)+ "\n"
file.write(st)
stre="x "
for j in range(len(results)):
if results[j]>0:
stre=stre+str(j)+ "/" +str(results[j])+":"+str(cork[j])+" "
stre=stre+"\n"
if len(stre)>2:
file.write(stre)
return 0
def main(argv):
FILEGRAPHS="mutag_graph.txt"
FILESUBGRAPHS="mutag_pattern.txt"
LENGTHGRAPHS=188
LENGTHSUBGRAPHS=14479
Graphs,useless_var = load_graphs(FILEGRAPHS,LENGTHGRAPHS)
Subgraphs,id_graphs = load_graphs(FILESUBGRAPHS,LENGTHSUBGRAPHS)
countSubgraph(Graphs,Subgraphs,id_graphs,"mono","Mono.txt")
countSubgraph(Graphs,Subgraphs,id_graphs,"iso","Iso.txt")
if __name__ == "__main__":
print(sys.argv)
main(sys.argv[1:])