-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDynamicGraphWalkTest.py
180 lines (130 loc) · 4.83 KB
/
DynamicGraphWalkTest.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
from keras.utils import to_categorical
import numpy as np
import math
import matplotlib.pyplot as plt
import networkx as nx
class GraphWalkTest:
def __init__(self, time_delay, filename="graph.dot"):
'''
Unfinished
'''
self.time_delay = time_delay
self.time_counter = 0
self.G= nx.DiGraph(nx.nx_agraph.read_dot(filename))
self.output_size= self.G.number_of_nodes()
self.A = nx.adjacency_matrix(self.G)
self.A = self.A.todense()
self.A = np.array(self.A, dtype = np.float64)
for i in range(self.output_size):
accum = self.A[i].sum()
if accum != 0:
self.A[i]= self.A[i]/accum
else:
print("ERROR: Node ",i," without connections from found")
exit()
print(self.A[i])
#random start
self.output_class= np.random.randint(self.output_size)
self.previous_output_class= None
self.previous_previous_output_class= None
#self.plotGraph()
#self.getInput()
def getOutputSize(self):
return self.output_size
def updateTimeDelay(self):
self.time_counter+= 1
if self.time_counter > self.time_delay:
self.time_counter = 0
self.previous_previous_output_class= self.previous_output_class
self.previous_output_class= self.output_class
return True
else:
return False
#create an input pattern for the system
def getInput(self, reset = False):
update = self.updateTimeDelay()
if update == True:
#print(self.G)
#print(A.shape)
#transition= self.A[self.output_class]
#print(len(transition))
#print(transition.shape)
#print(transition)
self.previous_output_class= self.output_class
self.output_class= np.random.choice(self.output_size ,1 ,p= self.A[self.output_class])[0]
#print(choice)
#print(A[0])
#print(A[1])
#print(A[2])
noise_intensity= 0
if self.previous_output_class is None or self.previous_output_class == self.output_class:
input_value = to_categorical(self.output_class, self.output_size)*np.exp(-0.1*self.time_counter) + np.random.randn(self.output_size)*noise_intensity
else:
input_value = to_categorical(self.output_class, self.output_size)*np.exp(-0.1*self.time_counter) + np.random.randn(self.output_size)*noise_intensity + to_categorical(self.previous_output_class, self.output_size)*np.exp(-0.1*(self.time_counter+self.time_delay))
# noise_intensity=0
# if self.previous_output_class is None or np.array_equal(self.previous_output_class, self.output_class):
# input_value = self.output_class*np.exp(-0.1*self.time_counter) + np.random.randn(self.output_size)*noise_intensity
# else:
# if self.previous_previous_output_class is None or np.array_equal(self.previous_previous_output_class, self.previous_output_class):
# input_value = self.output_class*np.exp(-0.1*self.time_counter) + np.random.randn(self.output_size)*noise_intensity + self.previous_output_class*np.exp(-0.1*(self.time_counter+self.time_delay))
# else:
# input_value = self.output_class*np.exp(-0.1*self.time_counter) + np.random.randn(self.output_size)*noise_intensity + self.previous_output_class*np.exp(-0.1*(self.time_counter+self.time_delay)) + self.previous_previous_output_class*np.exp(-0.1*(self.time_counter+2.0*self.time_delay))
#
return input_value
#for i in range():
#print(A)
#D = np.diag(np.sum(A, axis=0))
#print(D)
#T = np.dot(np.linalg.inv(D),A)
#print(T)
# let's evaluate the degree matrix D
# ...and the transition matrix T
#exit()
#exit()
def getSequence(self, sequence_size):
#print(self.data.shape[0])
#print(input_sequence.shape)
#exit()
self.input_sequence = np.empty((sequence_size, self.output_size))
self.input_class = np.empty(sequence_size)
for i in range(sequence_size):
input_value = self.getInput()
#input_class.append(self.chunk)
#input_sequence.append(input_value)
self.input_class[i] = self.output_class
self.input_sequence[i] = input_value
return self.input_sequence, self.input_class
def plotGraph(self, save = True):
options = {
'node_size': 100,
'arrowstyle': '-|>',
'arrowsize': 12,
}
nx.draw_networkx(self.G, arrows=True,**options)
if save == True:
plt.savefig("graph_plot.png")
plt.show()
def plot(self, input_class, input_sequence = None, save = False):
a = np.asarray(input_class)
t = [i for i,value in enumerate(a)]
plt.plot(t, a)
if input_sequence != None:
sequence = [np.argmax(x) for x in input_sequence]
plt.plot(t, sequence)
if save == True:
plt.savefig("plot.png")
plt.show()
plt.close()
def plotSuperposed(self, input_class, input_sequence = None, save = False):
input_sequence= np.asarray(input_sequence)
t = [i for i,value in enumerate(input_sequence)]
#exit()
for i in range(input_sequence.shape[1]):
a = input_sequence[:,i]
plt.plot(t, a)
a = np.asarray(input_class)
plt.plot(t, a)
if save == True:
plt.savefig("plot.png")
plt.show()
plt.close()