-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmotifs.py
162 lines (122 loc) · 3.38 KB
/
motifs.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
"""
This file implements the baseline algorithm for motif discovery in hypergraphs.
"""
from hypergraph import hypergraph
from utils import *
from loaders import *
H_O = True
L_O = []
def count_motifs(edges, N, TOT):
print(len(edges))
mapping, labeling = generate_motifs(N)
z = set()
for e in edges:
for n in e:
z.add(n)
graph = {}
T = {}
for e in edges:
e = list(sorted(e))
if H_O:
T[tuple(e)] = 1
for I in range(len(e)):
for J in range(I+1, len(e)):
i = e[I]
j = e[J]
if not H_O:
T[tuple(sorted([i,j]))] = 1
if i in graph:
graph[i].add(j)
else:
graph[i] = set([j])
if j in graph:
graph[j].add(i)
else:
graph[j] = set([i])
global L_O
L_O = list(T.keys())
def count_motif(nodes):
nodes = tuple(sorted(tuple(nodes)))
p_nodes = power_set(nodes)
motif = []
for edge in p_nodes:
if len(edge) >= 2:
edge = tuple(sorted(list(edge)))
if edge in T:
motif.append(edge)
conn = is_connected(motif, N)
if not conn:
return
m = {}
idx = 1
for i in nodes:
m[i] = idx
idx += 1
labeled_motif = []
for e in motif:
new_e = []
for node in e:
new_e.append(m[node])
new_e = tuple(sorted(new_e))
labeled_motif.append(new_e)
labeled_motif = tuple(sorted(labeled_motif))
if labeled_motif in labeling:
labeling[labeled_motif] += 1
def graph_extend(sub, ext, v, n_sub):
if len(sub) == N:
count_motif(sub)
return
while len(ext) > 0:
w = ext.pop()
tmp = set(ext)
for u in graph[w]:
if u not in sub and u not in n_sub and u > v:
tmp.add(u)
new_sub = set(sub)
new_sub.add(w)
new_n_sub = set(n_sub).union(set(graph[w]))
graph_extend(new_sub, tmp, v, new_n_sub)
c = 0
k = 0
for v in graph.keys():
v_ext = set()
for u in graph[v]:
if u > v:
v_ext.add(u)
k += 1
if k % 5 == 0:
print(k, len(z), TOT)
graph_extend(set([v]), v_ext, v, set(graph[v]))
c += 1
out = []
for motif in mapping.keys():
count = 0
for label in mapping[motif]:
count += labeling[label]
out.append((motif, count))
out = list(sorted(out))
D = {}
for i in range(len(out)):
D[i] = out[i][0]
return out
N = 3
results = []
output = {}
edges = load_high_school(N)
m = count_motifs(edges, N, -1)
output['motifs'] = m
print(output['motifs'])
STEPS = len(edges)*10
ROUNDS = 10
for i in range(ROUNDS):
if not H_O:
e1 = hypergraph(edges)
else:
e1 = hypergraph(L_O)
e1.MH(label='stub', n_steps=STEPS)
m1 = count_motifs(e1.C, N, i)
results.append(m1)
output['config_model'] = results
delta = diff_sum(output['motifs'], output['config_model'])
norm_delta = norm_vector(delta)
print(norm_delta)