-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRicciDiffusion.py
190 lines (139 loc) · 5.62 KB
/
RicciDiffusion.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# RicciDiffusion.py
# diffusion considering also the curvature
# the quantity which must be difused is
# represented as a vector
import networkx as nx
import math
import importlib
import time
# matplotlib setting
import matplotlib.pyplot as plt
# to print logs in jupyter notebook
import logging
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.ERROR)
# load GraphRicciCuravture package
from GraphRicciCurvature.OllivierRicci import OllivierRicci
from GraphRicciCurvature.FormanRicci import FormanRicci
# this does not care if the curvature is positive or negative
def RicciEvolve (G,vec,n_step):
for t in range(n_step):
W = [0] * len(list(G.nodes))
for n1 in list(G.nodes):
for n2 in list(G.neighbors(n1)):
W[n1] += abs(G[n1][n2]["ricciCurvature"])
norm_vec = [0] * len(vec)
vec_copy = vec
for i in list(G.nodes):
if W[i] != 0:
norm_vec[i] = vec[i]/W[i]
for n1 in list(G.nodes):
for n2 in list(G.neighbors(n1)):
vec[n1] += abs(G[n1][n2]["ricciCurvature"]) * norm_vec[n2]
vec[n2] -= abs(G[n1][n2]["ricciCurvature"]) * norm_vec[n2]
check_parameter = abs( sum(vec)- sum(vec_copy) )/sum(vec)
if check_parameter < 0.0001:
vec = vec_copy
else:
print("mass is not conserved, something happened")
break
# this does
def OllivierRicciEvolve_sign (G,vec,n_step):
weights = {}
G_copy = G
# setting the weights on the nodes according to vex
for n in range(len(vec)):
weights[n] = vec[n]
# starting the real diffusion
for t in range(n_step):
# new weights on the nodes
nx.set_node_attributes(G,weights,'weight')
print('iteration %i, setting new weights' % t)
# new curvature
orc_G = OllivierRicci(G_copy)
orc_G.compute_ricci_curvature()
G_copy = orc_G.G.copy()
# W[n] is the sum of the curvatures of all the links
# connected to the node n (only negative matter here)
W = [0] * len(list(G.nodes))
for n1 in list(G.nodes):
for n2 in list(G.neighbors(n1)):
if(G_copy[n1][n2]["ricciCurvature"]<0):
W[n1] += abs(G[n1][n2]["ricciCurvature"])
norm_vec = [0] * len(vec)
for i in list(G.nodes):
if W[i] != 0:
norm_vec[i] = weights[i]/W[i]
for n1 in list(G.nodes):
for n2 in list(G.neighbors(n1)):
if ((G_copy[n1][n2]["ricciCurvature"]<0) and (n1<n2)):
random_parameter = random.uniform(0,1)
if random_parameter < 0.5:
weights[n1] += abs(G_copy[n1][n2]["ricciCurvature"]) * norm_vec[n2]
weights[n2] -= abs(G_copy[n1][n2]["ricciCurvature"]) * norm_vec[n2]
else:
weights[n2] += abs(G_copy[n1][n2]["ricciCurvature"]) * norm_vec[n1]
weights[n1] -= abs(G_copy[n1][n2]["ricciCurvature"]) * norm_vec[n1]
vec_copy = [0] * len(vec)
for n in range(len(vec)):
vec_copy[n] = weights[n]
return vec_copy
def FormanRicciEvolve_sign (G,vec,n_step):
weights = {}
G_copy = G
# setting the weights on the nodes according to vex
for n in range(len(vec)):
weights[n] = vec[n]
# starting the real diffusion
for t in range(n_step):
# new weights on the nodes
nx.set_node_attributes(G,weights,'weight')
print('iteration %i, setting new weights' % t)
# new curvature
orf_G = FormanRicci(G_copy)
orf_G.compute_ricci_curvature()
G_copy = orf_G.G.copy()
# W[n] is the sum of the curvatures of all the links
# connected to the node n (only negative matter here)
W = [0] * len(list(G.nodes))
for n1 in list(G.nodes):
for n2 in list(G.neighbors(n1)):
if(G_copy[n1][n2]["formanCurvature"]<0):
W[n1] += abs(G_copy[n1][n2]["formanCurvature"])
norm_vec = [0] * len(vec)
for i in list(G.nodes):
if W[i] != 0:
norm_vec[i] = weights[i]/W[i]
for n1 in list(G.nodes):
for n2 in list(G.neighbors(n1)):
if ((G_copy[n1][n2]["formanCurvature"]<0) and (n1<n2)):
random_parameter = random.uniform(0,1)
if random_parameter < 0.5:
weights[n1] += abs(G_copy[n1][n2]["formanCurvature"]) * norm_vec[n2]
weights[n2] -= abs(G_copy[n1][n2]["formanCurvature"]) * norm_vec[n2]
else:
weights[n2] += abs(G_copy[n1][n2]["formanCurvature"]) * norm_vec[n1]
weights[n1] -= abs(G_copy[n1][n2]["formanCurvature"]) * norm_vec[n1]
vec_copy = [0] * len(vec)
for n in range(len(vec)):
vec_copy[n] = weights[n]
return vec_copy
def OllivierEvolution_Flow(G, vec, n_step):
for i in range(n_step):
vec = OllivierRicciEvolve_sign(G, vec, 1)
orc = OllivierRicci(G)
orc.compute_ricci_flow(1)
G = orc.G.copy()
def FormanEvolution_Flow(G, vec, n_step):
for i in range(n_step):
vec = FormanRicciEvolve_sign(G, vec, 1)
orc = FormanRicci(G)
orc.compute_ricci_flow(1)
G = orc.G.copy()
def preprocces_Ollivier(G):
orf= OllivierRicci(G)
orf.compute_ricci_curvature()
return orf.G.copy()
def preprocces_Forman(G):
orf= FormanRicci(G)
orf.compute_ricci_curvature()
return orf.G.copy()