-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfigures.py
483 lines (416 loc) · 20.7 KB
/
figures.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
import numpy as np
import pandas as pd
from collections import Counter
from sklearn.neighbors import KernelDensity
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
from mpl_toolkits.axes_grid1 import AxesGrid
from matplotlib import rc
# fonttype setting required for CorelDraw compatibility
rc('pdf',fonttype=42)
##########################################################################
# Main program to run analysis and create figures. #
# Requires that you first run trialSim.py for each simulation condition. #
# Run by typing 'python figures.py' in the terminal window. #
# Figures will not display but will be saved to the folder #
# this file is saved to. #
##########################################################################
def main():
controlnorm, gabanorm, achnorm, max_ach, \
control, ach, \
unique, counts, \
int_g_e, int_g_i, \
reduce_tonic = run_analysis()
plot_spike_prob(controlnorm, gabanorm, achnorm, max_ach, control, ach)
plot_connections_summary(counts, max_ach, int_g_e, int_g_i, reduce_tonic)
plot_conductance_scatter_heatmap(int_g_e, int_g_i, max_ach)
plot_input_distributions(int_g_e, int_g_i,reduce_tonic, max_ach)
plot_kde_inputs(int_g_e, int_g_i,reduce_tonic, max_ach)
plot_inputs_summary(int_g_e, int_g_i, max_ach, reduce_tonic)
###########################################################
# Run analysis and return data required to create figures #
###########################################################
def run_analysis():
# Load arrays listing all active MF->GrC pairs
control_connections = np.load('controlConnections.npy', allow_pickle=True)
ach_connections = np.load('achConnections.npy', allow_pickle=True)
# Check to make sure connections are the same for both conditions
shared_connections = control_connections == ach_connections
if np.all(shared_connections):
connections = control_connections
else:
print('WARNING: Connections are not the same between conditions')
# Calculate number of unique granule cells receiving active MF input
GrC = connections[:,1]
unique,counts = np.unique(GrC,return_counts=True)
# Load spike probability arrays
control_all = np.load('randomized_controlSpikeProb.npy', allow_pickle=True)
gaba_all = np.load('randomized_gabaSpikeProb.npy', allow_pickle=True)
ach_all = np.load('randomized_achSpikeProb.npy', allow_pickle=True)
nt = control_all.shape[1]
# Only consider unique GrCs receiving active MF input
control_unique = control_all[unique,:]
gaba_unique = gaba_all[unique,:]
ach_unique = ach_all[unique,:]
# Get rid of entirely quiescent cells in control
indices_nonzero = ~np.all(control_unique==0,axis=1)
control_nonzero = control_unique[indices_nonzero,1000:1060]
gaba_nonzero = gaba_unique[indices_nonzero,1000:1060]
ach_nonzero = ach_unique[indices_nonzero,1000:1060]
# Update cell counts
unique = unique[indices_nonzero]
counts = counts[indices_nonzero]
# Now select cells for spiking in control that matches experiment
max_stim_two = np.max(control_nonzero[:,18:22],axis=1)
max_stim_three = np.max(control_nonzero[:,28:32],axis=1)
selection_criteria = (max_stim_two <= 0.6) & (max_stim_three <= max_stim_two)
control = control_nonzero[selection_criteria,:]
gaba = gaba_nonzero[selection_criteria,:]
ach = ach_nonzero[selection_criteria,:]
# Update cell counts again
unique = unique[selection_criteria]
counts = counts[selection_criteria]
# Normalize to control condition (max-min)
controlmax = np.max(control,axis=1)
controlnorm = control / controlmax[:,None]
gabanorm = gaba / controlmax[:,None]
achnorm = ach / controlmax[:,None]
# Maximum normalized spike probability in muscarine
max_ach = np.max(achnorm,axis=1)
# Load synaptic weight dictionaries
control_weights = np.load('controlWeights.npy', allow_pickle=True)
ach_weights = np.load('achWeights.npy', allow_pickle=True)
x = control_weights.item()
y = ach_weights.item()
# Check to make sure weights are consistent for both conditions
shared_weights = {k: x[k] for k in x if k in y and x[k]==y[k]}
if len(shared_weights) < len(x):
print('WARNING: Weights are not the same between conditions')
else:
weights = x
# Load excitatory and inhibitory conductance traces
g_e = np.load('controlCondE.npy', allow_pickle=True)
g_i = np.load('controlCondI.npy', allow_pickle=True)
if g_e.shape[1] != nt:
print('WARNING: StateMonitor sampling does not match resolution of spiking data')
else:
start = 1000
stop = 1060
# Make sure you only saved traces for unique GrCs receiving active MF input
if g_e.shape[0] != control_unique.shape[0]:
print('WARNING: Did not save excitatory conductances for only unique GrCs')
else:
g_e = g_e[indices_nonzero,start:stop]
g_e = g_e[selection_criteria,:]
if g_i.shape[0] != control_unique.shape[0]:
print('WARNING: Did not save inhibitory conductances for only unique GrCs')
else:
g_i = g_i[indices_nonzero,start:stop]
g_i = g_i[selection_criteria,:]
#Integrate conductance traces
nsamp = stop-start
xval = np.linspace(0,60,nsamp)
int_g_e = np.trapz(g_e,xval,axis=1)
int_g_i = np.trapz(g_i,xval,axis=1)
# Load array with the fraction of tonic inhibition in muscarine
reduce_tonic = np.load('tonicReduction.npy', allow_pickle=True)
if len(reduce_tonic) != control_unique.shape[0]:
print('WARNING: Did not save tonic reduction for only unique GrCs')
else:
reduce_tonic = reduce_tonic[indices_nonzero]
reduce_tonic = reduce_tonic[selection_criteria]
return controlnorm, gabanorm, achnorm, max_ach, \
control, ach, \
unique, counts, \
int_g_e, int_g_i, \
reduce_tonic
########################################################
# Code to construct spike probability figure; #
# Panel 1: Normalized spike probability plot for cells #
# with increasing probability in muscarine. #
# Panel 2: Normalized spike probability plot for cells #
# with decreasing probaility in muscarine. #
# Panel 3: Summary scatter plot of peak probability #
# in muscarine vs. control #
########################################################
def plot_spike_prob(controlnorm, gabanorm, achnorm, max_ach, control, ach):
ach_increase = max_ach > 1
ach_decrease = max_ach < 1
n_increase = np.sum(ach_increase)
n_decrease = np.sum(ach_decrease)
mean_control_up = np.mean(controlnorm[ach_increase,:],axis=0)
mean_gaba_up = np.mean(gabanorm[ach_increase,:],axis=0)
mean_ach_up = np.mean(achnorm[ach_increase,:],axis=0)
mean_control_down = np.mean(controlnorm[ach_decrease,:],axis=0)
mean_gaba_down = np.mean(gabanorm[ach_decrease,:],axis=0)
mean_ach_down = np.mean(achnorm[ach_decrease,:],axis=0)
nt = controlnorm.shape[1]
stim_times = [1010, 1020, 1030]
cond = max_ach != 1
fig, ax = plt.subplots(1, 3, figsize=(12,4))
# Need this backend for figures to be compatible with CorelDraw
with PdfPages('spike_prob.pdf') as pdf:
ax[0].plot(np.arange(nt)-10,mean_control_up,'k')
ax[0].plot(np.arange(nt)-10,mean_gaba_up,'r')
ax[0].plot(np.arange(nt)-10,mean_ach_up,'b')
bottom,top = ax[0].get_ylim()
ax[0].text(0,top-0.5,'n={}'.format(n_increase))
ax[0].legend(['Control','GABAzine','Muscarine'])
ax[0].set_xlabel('time (ms)')
ax[0].set_ylabel('spike probability (norm.)')
ax[1].plot(np.arange(nt)-10,mean_control_down,'k')
ax[1].plot(np.arange(nt)-10,mean_gaba_down,'r')
ax[1].plot(np.arange(nt)-10,mean_ach_down,'b')
bottom,top = ax[1].get_ylim()
ax[1].text(0,top-0.2,'n={}'.format(n_decrease))
ax[1].set_xlabel('time (ms)')
x = np.max(control[cond],axis=1)
y = np.max(ach[cond], axis=1)
c = Counter(zip(x,y))
s = [2*c[(i,j)] for i,j in zip(x,y)]
ax[2].scatter(x,y,c='k',s=s)
ax[2].set_xlabel('Control peak spike probability')
ax[2].set_ylabel('Muscarine peak spike probability')
start,stop = ax[2].get_xlim()
x = np.linspace(start,stop)
y = x
ax[2].plot(x,y,'--k')
pdf.savefig()
plt.close()
###############################################################################
# Code to construct connection summary figure; #
# Panel 1: Max. probability in muscarine vs. number active MF inputs. #
# Panel 2: Max. probability in muscarine vs. Avg. net conductance #
# Panel 3: Max. probability in muscarine vs. Fraction reduction in tonic inh. #
###############################################################################
def plot_connections_summary(counts, max_ach, int_g_e, int_g_i, reduce_tonic):
avg_g_e = int_g_e/60
avg_g_i = int_g_i/60
avg_g_sub = avg_g_e - avg_g_i
cond = (max_ach != 1) & (max_ach != 0)
nospikes = max_ach == 0
# Generate figure object
fig,ax = plt.subplots(1, 3, figsize=(12,4))
with PdfPages('compare_connections.pdf') as pdf:
# Marker size proportional to number of instances
c = Counter(zip(counts[cond],max_ach[cond]))
s = [2*c[(i,j)] for i,j in zip(counts[cond],max_ach[cond])]
cc = Counter(zip(counts[nospikes],max_ach[nospikes]))
ss = [2*cc[(i,j)] for i,j in zip(counts[nospikes],max_ach[nospikes])]
# Plot maximum spike probability in muscarine against number of active inputs
ax[0].scatter(counts[cond],np.log(max_ach[cond]),c='k',s=s)
ax[0].scatter(counts[nospikes],np.full(sum(nospikes),-2),c='k',s=ss)
ax[0].set_xticks(np.arange(0,5,1))
ax[0].set_xlabel('No. active MF inputs')
ax[0].set_ylabel('Log max. spike prob. in muscarine (norm)')
ax[1].scatter(avg_g_sub[cond], np.log(max_ach[cond]), c='k')
ax[1].scatter(avg_g_sub[nospikes],np.full(sum(nospikes),-2),c='k')
ax[1].set_xlabel('Avg. net conductance (nS)')
# Plot maximum spike probability in muscarine against reduction in tonic inhib.
ax[2].scatter((1-reduce_tonic[cond]),np.log(max_ach[cond]),c='k')
ax[2].scatter((1-reduce_tonic[nospikes]),np.full(sum(nospikes),-2),c='k')
ax[2].set_xlabel('Fractional reduction in tonic inhibition')
ax[2].set_xlim(0,1)
pdf.savefig()
plt.close()
################################################################################
# Code to construct figure to visualize inh. vs. exc. conductance, with points #
# color coded based on max. spike probability in muscarine #
################################################################################
def plot_conductance_scatter_heatmap(int_g_e, int_g_i, max_ach):
# Don't plot cells whose spike probability stayed the same
cond = (max_ach != 1) & (max_ach != 0)
nospikes = max_ach == 0
avg_g_e = int_g_e/60
avg_g_i = int_g_i/60
colors = np.log(max_ach[cond])
orig_colormap = plt.cm.coolwarm
vmax = np.max(colors)
vmin = np.min(colors)
new_mid = 1-vmax/(vmax+abs(vmin))
shifted_cmap = shiftedColorMap(orig_colormap,midpoint=new_mid,name='shifted')
plt.figure()
with PdfPages('conductance_scatter_heatmap.pdf') as pdf:
plt.scatter(avg_g_e[nospikes],avg_g_i[nospikes],c='k')
sc = plt.scatter(avg_g_e[cond],avg_g_i[cond],c=colors,cmap=shifted_cmap)
cb = plt.colorbar(sc)
cb.set_label('Log max. spike prob in muscarine (norm.)',rotation=270)
plt.xlim(-0.5,6)
plt.ylim(-0.5,6)
plt.xlabel('Avg. excitatory conductance (nS)')
plt.ylabel('Avg. inhibitory conductnace (nS)')
pdf.savefig()
plt.close()
def plot_input_distributions(int_g_e, int_g_i,reduce_tonic, max_ach):
increase = max_ach > 1
decrease = max_ach < 1
avg_g_e = int_g_e/60
avg_g_i = int_g_i/60
avg_g_net = avg_g_e-avg_g_i
max_g_e = np.max(avg_g_e)
max_g_i = np.max(avg_g_i)
max_g_net = np.max(avg_g_net)
min_g_net = np.min(avg_g_net)
tonic_reduction = 1-reduce_tonic
max_tonic = np.max(tonic_reduction)
min_tonic = np.min(tonic_reduction)
binsize = 0.1
binmax = max(max_g_e,max_g_i)
bins = np.arange(0,binmax,binsize)
bins_net = np.arange(min_g_net,max_g_net,binsize)
bins_tonic = np.arange(min_tonic,max_tonic,binsize*0.1)
fig,ax = plt.subplots(2,2,figsize=(12,8))
with PdfPages('input_distributions.pdf') as pdf:
ax[0,0].hist(avg_g_e[increase],bins=bins,density=True,histtype='step',color='r',label='increase')
ax[0,0].hist(avg_g_e[decrease],bins=bins,density=True,histtype='step',color='b',label='decrease')
ax[0,0].set_xlabel('Avg. excitatory conductance (nS)')
ax[0,0].set_ylabel('Density')
ax[0,0].legend(loc='upper right')
ax[0,1].hist(avg_g_i[increase],bins=bins,density=True,histtype='step',color='r')
ax[0,1].hist(avg_g_i[decrease],bins=bins,density=True,histtype='step',color='b')
ax[0,1].set_xlabel('Avg. inhibitory conductance (nS)')
ax[0,1].set_ylabel('Density')
ax[1,0].hist(avg_g_net[increase],bins=bins_net,density=True,histtype='step',color='r')
ax[1,0].hist(avg_g_net[decrease],bins=bins_net,density=True,histtype='step',color='b')
ax[1,0].set_xlabel('Avg. net conductance (nS)')
ax[1,0].set_ylabel('Density')
ax[1,1].hist(tonic_reduction[increase],bins=bins_tonic,density=True,histtype='step',color='r')
ax[1,1].hist(tonic_reduction[decrease],bins=bins_tonic,density=True,histtype='step',color='b')
ax[1,1].set_xlabel('Frac. reduction in tonic inhibition')
ax[1,1].set_ylabel('Density')
pdf.savefig()
plt.close()
def plot_kde_inputs(int_g_e,int_g_i,reduce_tonic, max_ach):
increase = max_ach > 1
decrease = max_ach < 1
avg_g_e = int_g_e/60
avg_g_i = int_g_i/60
avg_g_net = avg_g_e-avg_g_i
tonic_reduction = 1-reduce_tonic
max_g_e = np.max(avg_g_e)
max_g_i = np.max(avg_g_i)
binmax = max(max_g_e,max_g_i)+1
binmin_net = np.min(avg_g_net)-1
binmax_net = np.max(avg_g_net)+1
bandwidth = 0.1
X_plot = np.linspace(-1,binmax,int(2*binmax/bandwidth))[:,np.newaxis]
X_plot_net = np.linspace(binmin_net,binmax_net,int(2*binmax/bandwidth))[:,np.newaxis]
bandwidth_tonic = 0.01
X_plot_tonic = np.linspace(0,1,int(2*1/bandwidth_tonic))[:,np.newaxis]
kde_increase_e = KernelDensity(kernel='gaussian',bandwidth=bandwidth).fit(avg_g_e[increase,np.newaxis])
kde_decrease_e = KernelDensity(kernel='gaussian',bandwidth=bandwidth).fit(avg_g_e[decrease,np.newaxis])
kde_increase_i = KernelDensity(kernel='gaussian',bandwidth=bandwidth).fit(avg_g_i[increase,np.newaxis])
kde_decrease_i = KernelDensity(kernel='gaussian',bandwidth=bandwidth).fit(avg_g_i[decrease,np.newaxis])
kde_increase_net = KernelDensity(kernel='gaussian',bandwidth=bandwidth).fit(avg_g_net[increase,np.newaxis])
kde_decrease_net = KernelDensity(kernel='gaussian',bandwidth=bandwidth).fit(avg_g_net[decrease,np.newaxis])
kde_increase_tonic = KernelDensity(kernel='gaussian',bandwidth=bandwidth).fit(tonic_reduction[increase,np.newaxis])
kde_decrease_tonic = KernelDensity(kernel='gaussian',bandwidth=bandwidth).fit(tonic_reduction[decrease,np.newaxis])
logdens_increase_e = kde_increase_e.score_samples(X_plot)
logdens_decrease_e = kde_decrease_e.score_samples(X_plot)
logdens_increase_i = kde_increase_i.score_samples(X_plot)
logdens_decrease_i = kde_decrease_i.score_samples(X_plot)
logdens_increase_net = kde_increase_net.score_samples(X_plot_net)
logdens_decrease_net = kde_decrease_net.score_samples(X_plot_net)
logdens_increase_tonic = kde_increase_tonic.score_samples(X_plot_tonic)
logdens_decrease_tonic = kde_decrease_tonic.score_samples(X_plot_tonic)
fig,ax = plt.subplots(2,2,figsize=(12,8))
with PdfPages('inputs_kde.pdf') as pdf:
ax[0,0].plot(X_plot[:,0],np.exp(logdens_increase_e),'-r',label='increase')
ax[0,0].plot(X_plot[:,0],np.exp(logdens_decrease_e),'-b',label='decrease')
ax[0,0].set_xlim(0,binmax-1)
ax[0,0].legend(loc='upper right')
ax[0,0].set_xlabel('Avg. exc. conductance (nS)')
ax[0,0].set_ylabel('Density')
ax[0,1].plot(X_plot[:,0],np.exp(logdens_increase_i),'-r')
ax[0,1].plot(X_plot[:,0],np.exp(logdens_decrease_i),'-b')
ax[0,1].set_xlim(0,binmax-1)
ax[0,1].set_xlabel('Avg. inh. conductance (nS)')
ax[0,1].set_ylabel('Density')
ax[1,0].plot(X_plot_net[:,0],np.exp(logdens_increase_net),'-r')
ax[1,0].plot(X_plot_net[:,0],np.exp(logdens_decrease_net),'-b')
ax[1,0].set_xlim(binmin_net,binmax_net)
ax[1,0].set_xlabel('Avg. net conductance (nS)')
ax[1,0].set_ylabel('Density')
ax[1,1].plot(X_plot_tonic[:,0],np.exp(logdens_increase_tonic),'-r')
ax[1,1].plot(X_plot_tonic[:,0],np.exp(logdens_decrease_tonic),'-b')
ax[1,1].set_xlim(0,1)
ax[1,1].set_xlabel('Frac. reduction in tonic inhibition')
ax[1,1].set_ylabel('Density')
pdf.savefig()
plt.close()
def plot_inputs_summary(int_g_e, int_g_i, max_ach, reduce_tonic):
exc = int_g_e/60
inh = int_g_i/60
net = exc-inh
increase = max_ach > 1
decrease = max_ach < 1
mean_inc_e = np.mean(exc[increase])
sem_inc_e = np.std(exc[increase])/np.sqrt(sum(increase))
mean_dec_e = np.mean(exc[decrease])
sem_dec_e = np.std(exc[decrease])/np.sqrt(sum(decrease))
mean_inc_i = np.mean(inh[increase])
sem_inc_i = np.std(inh[increase])/np.sqrt(sum(increase))
mean_dec_i = np.mean(inh[decrease])
sem_dec_i = np.mean(inh[decrease])/np.sqrt(sum(decrease))
mean_inc_net = np.mean(net[increase])
sem_inc_net = np.std(net[increase])/np.sqrt(sum(increase))
mean_dec_net = np.mean(net[decrease])
sem_dec_net = np.std(net[decrease])/np.sqrt(sum(decrease))
reduce = 1-reduce_tonic
mean_inc_reduce = np.mean(reduce[increase])
sem_inc_reduce = np.std(reduce[increase])/np.sqrt(sum(increase))
mean_dec_reduce = np.mean(reduce[decrease])
sem_dec_reduce = np.std(reduce[decrease])/np.sqrt(sum(decrease))
fig,ax = plt.subplots(2, 2, figsize=(12,4))
with PdfPages('inputs_summary.pdf') as pdf:
# ax[0,0].scatter([1,2],[mean_dec_e,mean_inc_e],c='k')
ax[0,0].errorbar([1,2],[mean_dec_e,mean_inc_e],[sem_dec_e,sem_inc_e],fmt='ok',ecolor='k')
ax[0,0].set_xticks(np.arange(0,3,1))
ax[0,0].set_xticklabels(['','Decreasing','Increasing'])
ax[0,0].set_ylabel('ge (nS)')
ax[0,0].set_xlim(0,3)
# ax[0,1].scatter([1,2],[mean_dec_i,mean_inc_i],c='k')
ax[0,1].errorbar([1,2],[mean_dec_i,mean_inc_i],[sem_dec_i,sem_inc_i],fmt='ok',ecolor='k')
ax[0,1].set_xticks(np.arange(0,3,1))
ax[0,1].set_xticklabels(['','Decreasing','Increasing'])
ax[0,1].set_ylabel('gi (nS)')
ax[0,1].set_xlim(0,3)
# ax[1,0].scatter([1,2],[mean_dec_net,mean_inc_net],c='k')
ax[1,0].errorbar([1,2],[mean_dec_net,mean_inc_net],[sem_dec_net,sem_inc_net],fmt='ok',ecolor='k')
ax[1,0].set_xticks(np.arange(0,3,1))
ax[1,0].set_xticklabels(['','Decreasing','Increasing'])
ax[1,0].set_ylabel('gnet (nS)')
ax[1,0].set_xlim(0,3)
# ax[1,1].scatter([1,2],[mean_dec_reduce,mean_inc_reduce],c='k')
ax[1,1].errorbar([1,2],[mean_dec_reduce,mean_inc_reduce],[sem_dec_reduce,sem_inc_reduce],fmt='ok',ecolor='k')
ax[1,1].set_xticks(np.arange(0,3,1))
ax[1,1].set_xticklabels(['','Decreasing','Increasing'])
ax[1,1].set_ylabel('gt reduction')
ax[1,1].set_xlim(0,3)
ax[1,1].set_ylim(0,1)
pdf.savefig()
plt.close()
def shiftedColorMap(cmap,start=0,midpoint=0.5,stop=1.0, name='shiftedcmap'):
cdict = {
'red': [],
'green': [],
'blue': [],
'alpha': []
}
# Regular index to compute colors
reg_index = np.linspace(start,stop,257)
# Shifted index to match the data
shift_index = np.hstack([np.linspace(0.0,midpoint,128,endpoint=False),
np.linspace(midpoint,1.0,129,endpoint=True)])
for ri,si in zip(reg_index,shift_index):
r,g,b,a = cmap(ri)
cdict['red'].append((si,r,r))
cdict['green'].append((si,g,g))
cdict['blue'].append((si,b,b,))
cdict['alpha'].append((si,a,a))
newcmap = matplotlib.colors.LinearSegmentedColormap(name,cdict)
plt.colormaps.register(newcmap)
return newcmap
if __name__ == '__main__':
main()