-
Notifications
You must be signed in to change notification settings - Fork 1
/
adrian_one_corr.py
executable file
·346 lines (264 loc) · 11.9 KB
/
adrian_one_corr.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
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 14 11:21:34 2020
@author: Dhruv
"""
#loading the dataset
import numpy as np
import pandas as pd
import scipy.io
from functions import *
from wrappers import *
import ipyparallel
import os, sys
import neuroseries as nts
import time
import matplotlib.pyplot as plt
from Wavelets import MyMorlet as Morlet
from scipy.stats import kendalltau, pearsonr, wilcoxon
data_directory = '/media/DataDhruv/Dropbox (Peyrache Lab)/Peyrache Lab Team Folder/Data/AdrianPoSub/###AllPoSub'
#datasets = np.loadtxt(os.path.join(data_directory,'dataset_test.list'), delimiter = '\n', dtype = str, comments = '#')
datasets = np.loadtxt(os.path.join(data_directory,'dataset_Hor_DM.list'), delimiter = '\n', dtype = str, comments = '#')
rwpath = '/media/DataDhruv/Dropbox (Peyrache Lab)/Peyrache Lab Team Folder/Projects/PoSub-UPstate/Data'
allcoefs_up = []
allcoefs_dn = []
for s in datasets:
print(s)
name = s.split('/')[-1]
path = os.path.join(data_directory, s)
rawpath = os.path.join(rwpath,s)
spikes, shank = loadSpikeData(path)
n_channels, fs, shank_to_channel = loadXML(rawpath)
# ###############################################################################################
# # LOAD MAT FILES
# ###############################################################################################
filepath = os.path.join(path, 'Analysis')
listdir = os.listdir(filepath)
file = [f for f in listdir if 'BehavEpochs' in f]
behepochs = scipy.io.loadmat(os.path.join(filepath,file[0]))
file = [f for f in listdir if 'CellDepth' in f]
celldepth = scipy.io.loadmat(os.path.join(filepath,file[0]))
depth = celldepth['cellDep']
file = [f for f in listdir if 'MeanFR' in f]
mfr = scipy.io.loadmat(os.path.join(filepath,file[0]))
r_wake = mfr['rateS']
file = [f for f in listdir if 'CellTypes' in f]
celltype = scipy.io.loadmat(os.path.join(filepath,file[0]))
pyr = []
interneuron = []
hd = []
for i in range(len(spikes)):
if celltype['ex'][i] == 1 and celltype['gd'][i] == 1:
pyr.append(i)
for i in range(len(spikes)):
if celltype['fs'][i] == 1 and celltype['gd'][i] == 1:
interneuron.append(i)
for i in range(len(spikes)):
if celltype['hd'][i] == 1 and celltype['gd'][i] == 1:
hd.append(i)
# ###############################################################################################
# # LOAD UP AND DOWN STATE, NEW SWS AND NEW WAKE EPOCHS
# ###############################################################################################
file = os.path.join(rawpath, name +'.evt.py.dow')
if os.path.exists(file):
tmp = np.genfromtxt(file)[:,0]
tmp = tmp.reshape(len(tmp)//2,2)/1000
down_ep = nts.IntervalSet(start = tmp[:,0], end = tmp[:,1], time_units = 's')
file = os.path.join(rawpath, name +'.evt.py.upp')
if os.path.exists(file):
tmp = np.genfromtxt(file)[:,0]
tmp = tmp.reshape(len(tmp)//2,2)/1000
up_ep = nts.IntervalSet(start = tmp[:,0], end = tmp[:,1], time_units = 's')
file = os.path.join(rawpath, name +'.DM.new_sws.evt')
if os.path.exists(file):
tmp = np.genfromtxt(file)[:,0]
tmp = tmp.reshape(len(tmp)//2,2)/1000
new_sws_ep = nts.IntervalSet(start = tmp[:,0], end = tmp[:,1], time_units = 's')
file = os.path.join(rawpath, name +'.DM.new_wake.evt')
if os.path.exists(file):
tmp = np.genfromtxt(file)[:,0]
tmp = tmp.reshape(len(tmp)//2,2)/1000
new_wake_ep = nts.IntervalSet(start = tmp[:,0], end = tmp[:,1], time_units = 's')
###############################################################################################
# COMPUTE EVENT CROSS CORRS
###############################################################################################
binsize = 5
nbins = 1000
neurons = list(spikes.keys())
times = np.arange(0, binsize*(nbins+1), binsize) - (nbins*binsize)/2
cc = pd.DataFrame(index = times, columns = neurons)
tsd_up = up_ep.as_units('ms').start.values
tsd_dn = down_ep.as_units('ms').start.values
#UP State
ep_U = nts.IntervalSet(start = up_ep.start[0], end = up_ep.end.values[-1])
ep_D = nts.IntervalSet(start = down_ep.start[0], end = down_ep.end.values[-1])
# rates = []
# for i in neurons:
# spk2 = spikes[i].restrict(ep_U).as_units('ms').index.values
# tmp = crossCorr(tsd_up, spk2, binsize, nbins)
# fr = len(spk2)/ep_U.tot_length('s')
# rates.append(fr)
# cc[i] = tmp
# cc[i] = tmp/fr
# dd = cc[-250:250]
# #Cell types
# #ee = dd[pyr]
# ee = dd[interneuron]
# n = len(depth)
# #tmp = np.argsort(depth[pyr].flatten())
# tmp = np.argsort(depth[interneuron].flatten())
# #tmp = np.argsort(depth.flatten())
# desc = tmp[::-1][:n]
# order = []
# # for i in range(len(pyr)):
# # order.append(pyr[desc[i]])
# for i in range(len(interneuron)):
# order.append(interneuron[desc[i]])
# finalRates = ee[order]
# # finalRates = dd[desc]
# if len(ee.columns) > 5:
# # if len(dd.columns) > 5:
# plt.figure()
# plt.imshow(finalRates.T,extent=[-250 , 250, len(interneuron) , 1],aspect = 'auto', cmap = 'jet')
# # plt.imshow(finalRates.T,extent=[-250 , 250, len(neurons) , 1],aspect = 'auto', cmap = 'jet')
# # plt.imshow(finalRates.T,extent=[-250 , 250, len(pyr) , 1],aspect = 'auto', cmap = 'jet')
# plt.clim(0,5)
# plt.colorbar()
# plt.title('Event-related Xcorr, aligned to UP state onset_' + s)
# plt.ylabel('Neuron number')
# plt.xlabel('Lag (ms)')
# thresholdrate = 1.5 * np.array(rates)
# bin_50peak = []
# # for i in neurons:
# # for i in pyr:
# for i in interneuron:
# threshold = thresholdrate[i]
# neuron_rate = ee[i]
# # neuron_rate = dd[i]
# diff = abs(neuron_rate - threshold)
# ix = diff.idxmin()
# bin_50peak.append(ix)
# # posIxOnly = np.zeros((len(neurons),2))
# # posIxOnly[:,0] = bin_50peak
# # posIxOnly[:,1] = depth.flatten()
# posIxOnly = np.zeros((len(interneuron),2))
# posIxOnly[:,0] = bin_50peak
# posIxOnly[:,1] = depth[interneuron].flatten()
# # posIxOnly = np.zeros((len(pyr),2))
# # posIxOnly[:,0] = bin_50peak
# # posIxOnly[:,1] = depth[pyr].flatten()
# pos_ix = np.where(posIxOnly[:,0] > 0)
# n = posIxOnly[pos_ix]
# n1 = n[:,0]
# n2 = n[:,1]
# print(len(n))
# if len(n) > 5:
# coef, p = kendalltau(n1,n2)
# allcoefs_up.append(coef)
# plt.figure()
# plt.scatter(n1,n2, label = 'Kendall tau = ' + str(round(coef,4)))
# plt.title('Bin where FR > 50% baseline rate_' + s)
# plt.ylabel('Depth from top of probe (um)')
# plt.xlabel('Lag (ms)')
# plt.legend(loc = 'upper right')
#DOWN state
rates = []
for i in neurons:
spk2 = spikes[i].restrict(ep_D).as_units('ms').index.values
tmp = crossCorr(tsd_dn, spk2, binsize, nbins)
fr = len(spk2)/ep_D.tot_length('s')
rates.append(fr)
cc[i] = tmp
cc[i] = tmp/fr
dd = cc[-250:250]
#Cell types
ee = dd[pyr]
#ee = dd[interneuron]
n = len(depth)
tmp = np.argsort(depth[pyr].flatten())
# tmp = np.argsort(depth[interneuron].flatten())
#tmp = np.argsort(depth.flatten())
desc = tmp[::-1][:n]
order = []
for i in range(len(pyr)):
order.append(pyr[desc[i]])
# for i in range(len(interneuron)):
# order.append(interneuron[desc[i]])
finalRates = ee[order]
# finalRates = dd[desc]
if len(ee.columns) > 5:
# if len(dd.columns) > 5:
plt.figure()
# plt.imshow(finalRates.T,extent=[-250 , 250, len(interneuron) , 1],aspect = 'auto', cmap = 'jet')
# plt.imshow(finalRates.T,extent=[-250 , 250, len(neurons) , 1],aspect = 'auto', cmap = 'jet')
plt.imshow(finalRates.T,extent=[-250 , 250, len(pyr) , 1],aspect = 'auto', cmap = 'jet')
plt.clim(0,5)
plt.colorbar()
plt.title('Event-related Xcorr, aligned to DOWN state onset_' + s)
plt.ylabel('Neuron number')
plt.xlabel('Lag (ms)')
#sys.exit()
indexplot = []
for i in range(len(finalRates.columns)):
a = np.where(finalRates.iloc[:,i] < 0.5)
res = finalRates.iloc[:,i].index[a]
indexplot.append(res[0])
coef, p = kendalltau(indexplot,depth[pyr].flatten())
# allcoefs_up.append(coef)
allcoefs_dn.append(coef)
plt.figure()
plt.scatter(indexplot,depth[pyr].flatten(), label = 'Kendall tau = ' + str(round(coef,4)))
plt.title('Bin where FR < 50% baseline rate_' + s)
plt.ylabel('Depth from top of probe (um)')
plt.xlabel('Lag (ms)')
plt.legend(loc = 'upper right')
# thresholdrate = 0.5 * np.array(rates)
# bin_50peak = []
# # for i in neurons:
# for i in pyr:
# # for i in interneuron:
# threshold = thresholdrate[i]
# neuron_rate = ee[i]
# # neuron_rate = dd[i]
# diff = abs(neuron_rate - threshold)
# ix = diff.idxmin()
# bin_50peak.append(ix)
# # posIxOnly = np.zeros((len(neurons),2))
# # posIxOnly[:,0] = bin_50peak
# # posIxOnly[:,1] = depth.flatten()
# # posIxOnly = np.zeros((len(interneuron),2))
# # posIxOnly[:,0] = bin_50peak
# # posIxOnly[:,1] = depth[interneuron].flatten()
# posIxOnly = np.zeros((len(pyr),2))
# posIxOnly[:,0] = bin_50peak
# posIxOnly[:,1] = depth[pyr].flatten()
# # pos_ix = np.where(posIxOnly[:,0] < 0)
# # n = posIxOnly[pos_ix]
# # n1 = n[:,0]
# # n2 = n[:,1]
# print(len(posIxOnly))
# if len(posIxOnly) > 5:
# coef, p = kendalltau(posIxOnly[:,0],posIxOnly[:,1])
# # allcoefs_up.append(coef)
# allcoefs_dn.append(coef)
# plt.figure()
# plt.scatter(posIxOnly[:,0],posIxOnly[:,1], label = 'Kendall tau = ' + str(round(coef,4)))
# plt.title('Bin where FR < 50% baseline rate_' + s)
# plt.ylabel('Depth from top of probe (um)')
# plt.xlabel('Lag (ms)')
# plt.legend(loc = 'upper right')
#Out of loop
# z_up, p_up = wilcoxon(np.array(allcoefs_up)-0)
# plt.figure()
# plt.hist(allcoefs_up,label = 'p-value =' + str(round(p_up,4)))
# plt.axvline(np.mean(allcoefs_up), color = 'k')
# plt.title('Distribution of Kendall Tau for UP-state onset')
# plt.xlabel('Kendall tau value') rates = []
z_dn, p_dn = wilcoxon(np.array(allcoefs_dn)-0)
plt.figure()
plt.hist(allcoefs_dn,label = 'p =' + str(round(p_dn,4)))
plt.axvline(np.mean(allcoefs_dn), color = 'k')
plt.title('Distribution of Kendall Tau for DOWN-state onset')
plt.xlabel('Kendall tau value')
plt.ylabel('Number of sessions')
plt.legend(loc = 'upper right')