-
Notifications
You must be signed in to change notification settings - Fork 0
/
figure3.py
326 lines (240 loc) · 10.9 KB
/
figure3.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# -*- coding: utf-8 -*-
"""
@author: Daniel Koch
This code reproduces the results shown in supplementary figure 3 from the study:
Koch D, Nandan A, Ramesan G, Tyukin I, Gorban A, Koseska A (2024):
Ghost channels and ghost cycles guiding long transients in dynamical systems
In: Physical Review Letters (forthcoming)
"""
loadData = True
# Import packages etc
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
import functions as fun
import models
import os
import sys
fileDirectory = os.path.dirname(os.path.abspath(__file__))
os.chdir(fileDirectory)
sys.path.append(os.path.join( os.path.dirname( __file__ ), '..' ))
path_data= os.path.join(fileDirectory, 'data')
if not os.path.exists(path_data):
os.makedirs(path_data)
# settings for plotting
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
plt.rcParams.update(
{
'text.usetex': False,
'font.family': 'stixgeneral',
'mathtext.fontset': 'stix',
}
)
inCm = 1/2.54
#%% ~~~~~~~~~~~~~~~~~~~ Heteroclinic channel ~~~~~~~~~~~~~~~~~~~
t_end = 100 #
dt = 0.05
timesteps = int(t_end/dt)
timepoints = np.linspace(0, t_end, timesteps+1)
x0s = np.linspace(0.515,0.95,6)
y0s = (lambda y: 1 - y)(x0s)
ICs = np.asarray([x0s, y0s]).T
#%% figure 3 (a) - simulation and saving data
if loadData == False:
seeds = [2,5]
fileNames = ['data\\simdat_HC4_6ICs_s5e-3.npy','data\\simdat_HC4_6ICs_s5e-2.npy']
sigmaVals = [5e-3,5e-2]
print('Figure 3 (a): Simulating...')
for i in range(2):
np.random.seed(seeds[i])
simDat = []
for ic in ICs:
sim = fun.RK4_na_noisy(models.sys_HC4,[],ic,0,dt,t_end, sigmaVals[i], naFun = None,naFunParams = None)
simDat.append(sim)
simDat = np.asarray(simDat)
simDat = np.reshape(simDat, (6,3,2000))
np.save(fileNames[i], simDat)
print('Figure 3 (a): Simulation complete.')
#%% figure 3 (a) - loading data and plotting
simDat_loNoise = np.load('data\\simdat_HC4_6ICs_s5e-3.npy')
simDat_hiNoise = np.load('data\\simdat_HC4_6ICs_s5e-2.npy')
norm = plt.Normalize(0,8)
cmap=cm.get_cmap('RdPu_r')
sm = plt.cm.ScalarMappable(norm=norm, cmap=cmap)
X = np.linspace(-0.1,5, 20)
Y = np.linspace(-0.1,5, 20)
grid = np.meshgrid(X, Y)
fig, ax = plt.subplots(figsize=(5,5))
fun.plot_streamline(ax,models.sys_HC4,[] ,10, grid, lw = 1.5)
plt.title('4-saddle heteroclinic channel, $\sigma=$'+"{:.4f}".format(5e-3),fontsize=16)
plt.xlabel('x',fontsize=16);plt.ylabel('y',fontsize=16)
plt.xlim(-0.35,5)
plt.ylim(-0.35,5)
for ic in ICs:
idx = np.where(ICs == ic)[0][0]
col = np.asarray(cmap(norm(idx))[0:3])
ax.plot(simDat_loNoise[idx,1,:],simDat_loNoise[idx,2,:],color=col,lw=2.5)
ax.scatter(simDat_loNoise[idx,1,0],simDat_loNoise[idx,2,0],marker='o',s=60,edgecolors='black',color=col)
x1,y1 = -0.2,1
x2,y2 = 1,-0.2
x3,y3 = np.asarray([x1,y1])+3.5
x4,y4 = np.asarray([x2,y2])+3.5
ax.plot([x1,x2,x4,x3,x1],np.asarray([y1,y2,y4,y3,y1])-0.1,'-k',lw=2)
fig, ax = plt.subplots(figsize=(5,5))
fun.plot_streamline(ax,models.sys_HC4,[] ,10, grid, lw = 1.5)
plt.title('4-saddle heteroclinic channel, $\sigma=$'+"{:.4f}".format(5e-2),fontsize=16)
plt.xlabel('x',fontsize=16);plt.ylabel('y',fontsize=16)
plt.xlim(-0.35,5)
plt.ylim(-0.35,5)
for ic in ICs:
idx = np.where(ICs == ic)[0][0]
col = np.asarray(cmap(norm(idx))[0:3])
ax.plot(simDat_hiNoise[idx,1,:],simDat_hiNoise[idx,2,:],color=col,lw=2.5)
ax.scatter(simDat_hiNoise[idx,1,0],simDat_hiNoise[idx,2,0],marker='o',s=60,edgecolors='black',color=col)
x1,y1 = -0.2,1
x2,y2 = 1,-0.2
x3,y3 = np.asarray([x1,y1])+3.5
x4,y4 = np.asarray([x2,y2])+3.5
ax.plot([x1,x2,x4,x3,x1],np.asarray([y1,y2,y4,y3,y1])-0.1,'-k',lw=2)
print('Figure 3 (a): plotting complete.')
#%% ~~~~~~~~~~~~~~~~~~~ Ghost channel ~~~~~~~~~~~~~~~~~~~
dt = 0.05
t_end = 300 #
timesteps = int(t_end/dt)
timepoints = np.linspace(0, t_end, timesteps+1)
x0s = np.linspace(0.515,0.95,6)
y0s = (lambda y: 1 - y)(x0s)
ICs = np.asarray([x0s, y0s]).T
sigmaValues = [0.0001,0.0002,0.0005,0.001,0.002,0.005,0.01,0.02,0.05,0.1,0.2]
#%% figure 3 (b) - simulation and saving data
if loadData == False:
seeds = [1,1]
fileNames = ['data\\simdat_GC4_6ICs_s5e-3.npy','data\\simdat_GC4_6ICs_s5e-2.npy']
sigmaVals = [5e-3,5e-2]
print('Figure 3 (b): Simulating...')
for i in range(2):
np.random.seed(seeds[i])
simDat = []
for ic in ICs:
sim = fun.RK4_na_noisy(models.sys_ghost4,[],ic,0,dt,t_end, sigmaVals[i], naFun = None,naFunParams = None)
simDat.append(sim)
ax.plot(sim[1,:],sim[2,:],lw=2.5,color=col)
simDat = np.asarray(simDat)
simDat = np.reshape(simDat, (6,3,6000))
np.save(fileNames[i], simDat)
print('Figure 3 (b): Simulation complete.')
#%% figure 3 (b) - loading data and plotting
simDat_loNoise = np.load('data\\simdat_GC4_6ICs_s5e-3.npy')
simDat_hiNoise = np.load('data\\simdat_GC4_6ICs_s5e-2.npy')
norm = plt.Normalize(0,8)
cmap=cm.get_cmap('RdPu_r')
sm = plt.cm.ScalarMappable(norm=norm, cmap=cmap)
X = np.linspace(-0.1,5, 20)
Y = np.linspace(-0.1,5, 20)
grid = np.meshgrid(X, Y)
fig, ax = plt.subplots(figsize=(5,5))
fun.plot_streamline(ax,models.sys_ghost4,[] ,10, grid, lw = 1.5)
plt.title('4-ghost channel, $\sigma=$'+"{:.4f}".format(sigmaValues[0]),fontsize=16)
plt.xlabel('x',fontsize=16);plt.ylabel('y',fontsize=16)
plt.xlim(-0.35,5)
plt.ylim(-0.35,5)
for ic in ICs:
idx = np.where(ICs == ic)[0][0]
col = np.asarray(cmap(norm(idx))[0:3])
ax.plot(simDat_loNoise[idx,1,:],simDat_loNoise[idx,2,:],color=col,lw=2.5)
ax.scatter(simDat_loNoise[idx,1,0],simDat_loNoise[idx,2,0],marker='o',s=60,edgecolors='black',color=col)
x1,y1 = -0.2,1
x2,y2 = 1,-0.2
x3,y3 = np.asarray([x1,y1])+3.5
x4,y4 = np.asarray([x2,y2])+3.5
ax.plot([x1,x2,x4,x3,x1],np.asarray([y1,y2,y4,y3,y1])-0.1,'-k',lw=2)
fig, ax = plt.subplots(figsize=(5,5))
fun.plot_streamline(ax,models.sys_ghost4,[] ,10, grid, lw = 1.5)
plt.title('4-ghost channel, $\sigma=$'+"{:.4f}".format(sigmaValues[1]),fontsize=16)
plt.xlabel('x',fontsize=16);plt.ylabel('y',fontsize=16)
plt.xlim(-0.35,5)
plt.ylim(-0.35,5)
for ic in ICs:
idx = np.where(ICs == ic)[0][0]
col = np.asarray(cmap(norm(idx))[0:3])
ax.plot(simDat_hiNoise[idx,1,:],simDat_hiNoise[idx,2,:],color=col,lw=2.5)
ax.scatter(simDat_hiNoise[idx,1,0],simDat_hiNoise[idx,2,0],marker='o',s=60,edgecolors='black',color=col)
x1,y1 = -0.2,1
x2,y2 = 1,-0.2
x3,y3 = np.asarray([x1,y1])+3.5
x4,y4 = np.asarray([x2,y2])+3.5
ax.plot([x1,x2,x4,x3,x1],np.asarray([y1,y2,y4,y3,y1])-0.1,'-k',lw=2)
print('Figure 3 (b): plotting complete.')
#%% ~~~~~~~~~~~~~~~~~~~ Trajectory quantifications for heteroclinic and Ghost channels ~~~~~~~~~~~~~~~~~~~
# caution: running these simulations can take multiple hours.
#%% figure 3 (c) - loading data and plotting - simulation and saving data
if loadData == False:
dt = 0.05
t_end = 500
timesteps = int(t_end/dt)
timepoints = np.linspace(0, t_end, timesteps+1)
nruns = 30 # only take even integers
# Heteroclinic channels
simulations_HC = []
np.random.seed(1)
for i in range(len(sigmaValues)):
sig = sigmaValues[i]
print('Figure 3 (c): Simulations for heteroclinic channel ' + str(int(i*100/len(sigmaValues))) + ' % complete.')
for ic in ICs:
for n in range(nruns):
sim = fun.RK4_na_noisy(models.sys_HC4,[],ic,0,dt,t_end, sig, naFun = None,naFunParams = None)
simulations_HC.append(sim)
simulations_HC = np.reshape(np.asarray(simulations_HC),(len(sigmaValues),len(ICs),nruns,3,timesteps))
np.save('data\\simdat_HC4_noisy_30runs.npy',simulations_HC)
# Ghost channels
simulations_ghost = []
np.random.seed(1)
for i in range(len(sigmaValues)):
sig = sigmaValues[i]
print('Figure 3 (c): Simulations for ghost channel ' + str(int(i*100/len(sigmaValues))) + ' % complete.')
for ic in ICs:
for i in range(nruns):
sim = fun.RK4_na_noisy(models.sys_ghost4,[],ic,0,dt,t_end, sig, naFun = None,naFunParams = None)
simulations_ghost.append(sim)
simulations_ghost = np.reshape(np.asarray(simulations_ghost),(len(sigmaValues),len(ICs),nruns,3,timesteps))
np.save('data\\simdat_GC4_noisy_30runs.npy',simulations_ghost)
#%% figure 3 (c) - loading and analyzing data
simulationsHC = np.load('data\\simdat_HC4_noisy_30runs.npy')
simulationsHC_ = np.reshape(simulationsHC, (simulationsHC.shape[0],simulationsHC.shape[1]*simulationsHC.shape[2],simulationsHC.shape[3],simulationsHC.shape[4]))
simulationsGC = np.load('data\\simdat_GC4_noisy_30runs.npy')
simulationsGC_ = np.reshape(simulationsGC, (simulationsGC.shape[0],simulationsGC.shape[1]*simulationsGC.shape[2],simulationsGC.shape[3],simulationsGC.shape[4]))
nruns = 30
EDsHC = []
SDsHC = []
EDsGC = []
SDsGC = []
for i in range(len(sigmaValues)):
print('Figure 3 (c): data analysis ' + str(int(i*100/len(sigmaValues))) + ' % complete.')
length_ICs = 6
s1 = simulationsHC_[i,:int(nruns*length_ICs/2),1:,::20]
s1_ = np.swapaxes(s1, 1, 2)
s2 = simulationsHC_[i,int(nruns*length_ICs/2):,1:,::20]
s2_ = np.swapaxes(s2, 1, 2)
best_path = fun.dtw_getWarpingPaths(s1_,s2_,'multiple repetitions', 'True')
ED, SD = fun.euklDist_trajectory(s1_[:,best_path[0],:],s2_[:,best_path[1],:],'replicate')
EDsHC.append(ED)
SDsHC.append(SD)
s1 = simulationsGC_[i,:int(nruns*length_ICs/2),1:,::20]
s1_ = np.swapaxes(s1, 1, 2)
s2 = simulationsGC_[i,int(nruns*length_ICs/2):,1:,::20]
s2_ = np.swapaxes(s2, 1, 2)
best_path = fun.dtw_getWarpingPaths(s1_,s2_,'multiple repetitions', 'True')
ED, SD = fun.euklDist_trajectory(s1_[:,best_path[0],:],s2_[:,best_path[1],:],'replicate')
EDsGC.append(ED)
SDsGC.append(SD)
# figure 3 (c) - plotting
myFig = plt.figure(figsize=(7.5*inCm,5.75*inCm))
plt.errorbar(sigmaValues,EDsHC,yerr=SDsHC/np.sqrt(nruns*5/2),fmt='-ok',ms=4,capsize=3,label='heteroclinic channel',lw=1)
plt.errorbar(sigmaValues,EDsGC,yerr=SDsGC/np.sqrt(5*nruns/2),fmt=':sk',mfc='w',mec='k',ecolor='k',ms=4,capsize=3,label='ghost channel',lw=1)
plt.yscale('log'); plt.xscale('log')
plt.ylabel('Euclidean distance ($\mu \pm$ SEM)',fontsize=10)
plt.xticks([1e-4,1e-3,1e-2,1e-1],fontsize=8)
plt.yticks([1e-1,1e0],fontsize=8)
plt.xlabel('$\sigma$',fontsize=11)
plt.legend()