-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtag_aligned_plots.py
418 lines (333 loc) · 15.6 KB
/
tag_aligned_plots.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
# %%
import matplotlib.pyplot as plt
import matplotlib
import pandas as pd
import numpy as np
from pathlib import Path
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from rich.progress import track
import pandas as pd
from myterial import cyan, teal, indigo, orange, salmon, grey_light, grey, grey_dark, blue_grey_darker
from myterial import brown_dark, grey, blue_grey_dark, grey_darker, salmon_darker, orange_darker, blue_grey
from scipy.stats import zscore
from collections import namedtuple
from pyrnn.analysis.dimensionality import get_n_components_with_pca
from scipy.stats import ttest_ind, linregress
from statsmodels.stats.multitest import multipletests
from pyinspect import install_traceback
install_traceback()
from fcutils.plotting.utils import calc_nrows_ncols, clean_axes, save_figure
from fcutils.maths.utils import rolling_mean, derivative
from fcutils.plotting.plot_distributions import plot_kde
from vgatPAG.database.db_tables import ManualBehaviourTags, Roi, Sessions
from Analysis import get_session_data, get_session_tags, get_tags_sequences, speed_color, shelt_dist_color, get_active_rois
# %%
pre_pos_s = 1.5
pre_pos = int(pre_pos_s*30)
n_s_pre, n_s_post = 5, 2
n_frames_pre = n_s_pre * 30
n_frames_post = n_s_post * 30
xlbl = dict(
xlabel='time from tag\n(s)',
xlim=[0, n_frames_post + n_frames_pre],
xticks=[0, n_frames_pre - pre_pos, n_frames_pre, n_frames_pre+pre_pos, n_frames_pre+n_frames_post],
xticklabels=[-n_s_pre, -pre_pos_s, 0, pre_pos_s, n_s_post]
)
fld = Path('D:\\Dropbox (UCL)\\Project_vgatPAG\\analysis\\doric\\Fede\\\ddf_tag_aligned_all_tags_NOSMOOTHING')
# %%
def dff(sig):
th = np.nanpercentile(sig[:n_frames_pre], 30)
return rolling_mean((sig - th)/th, 3)
def get_slope(y):
x = np.arange(len(y))
lr = linregress(x, y)
return lr.slope
def get_intercept(y):
x = np.arange(len(y))
lr = linregress(x, y)
return lr.intercept
def plot_with_slope(ax, x0, x1, slope, intercept, **kwargs):
a, b = 0, x1-x0
y0, y1 = (a * slope) + intercept, (b * slope) + intercept
ax.plot([x0, x1], [y0, y1], **kwargs)
def get_significant_slopes(slopes):
ps = [ttest_ind(slopes['baseline'], v).pvalue for v in slopes.values()]
corrected = multipletests(ps, method='bonferroni')
return corrected[0]
def rel(event, stim):
return event - stim + n_frames_pre
def complete_X_Y(seq):
start = seq.STIM - n_frames_pre
end = seq.E + n_frames_post
# define x/y coord sets for plotting
stim = rel(seq.STIM, seq.STIM)
H = rel(seq.H, seq.STIM)
B = rel(seq.B, seq.STIM)
C = rel(seq.C, seq.STIM)
E = rel(seq.E, seq.STIM)
# xonset = np.arange(onset)
xstim = np.arange(0, stim)
xH = np.arange(stim, H)
xB = np.arange(H, B)
xC = np.arange(B, C)
xE = np.arange(C, E)
xend = np.arange(E, E+n_frames_post)
X = (xstim, xH, xB, xC, xE, xend)
Y = ((0, stim), (stim, H), (H, B), (B, C), (C, E), (E, rel(end, seq.STIM)))
T = ('STIM', 'H', 'B', 'C', 'E', 'after')
return start, end , X, Y, T
def get_onset(sig):
'''
Gets onset by looiking at last
time the signal ramped up before a max
'''
smoothed = rolling_mean(sig, 20)
atmax = np.argmax(smoothed)
deriv = derivative(smoothed)
onset = None
for th in (0.001, 0.01, 0.1):
try:
onset = np.where(np.abs(deriv[:atmax])<=0.001)[0][-1]
except IndexError:
pass
else:
break
return onset
# %%
lbls = ('baseline', 'stim', 'start', 'run', 'shelter', 'stop')
ACTIVE = dict(mouse=[], date=[], roi=[], active=[])
for sess in Sessions.fetch(as_dict=True):
print(sess['mouse'], sess['date'])
data, rois = get_session_data(sess['mouse'], sess['date'], roi_data_type='raw')
if data is None: continue
rois[data.is_rec==0] = np.nan
tags = get_session_tags(sess['mouse'], sess['date'],
etypes=('visual', 'audio', 'audio_visual'),
ttypes=('H', 'B', 'C', 'E'))
# get tags sequences
sequences = get_tags_sequences(tags)
if len(set([s.STIM for s in sequences])) <3: continue
# Loop over ROIs
for roi in rois.columns:
slopes = {k:[] for k in ('baseline', 'STIM', 'H', 'B', 'C', 'E', 'after')}
slopes_colors = {k:c for k,c in zip(slopes.keys(), (grey, salmon, orange, teal, indigo, blue_grey_dark))}
# Create a figure
f, axarr = plt.subplots(nrows=2, ncols=5, figsize=(18, 9), sharex=False, gridspec_kw={'width_ratios': [4, 1, 1, 1, 1]})
ax0 = axarr[0, 0].twinx()
axarr[0, 0].axvline(n_frames_pre, lw=2, color=salmon)
axarr[1, 0].axvline(n_frames_pre, lw=2, color=salmon)
tag_axes = [axarr[0, 1], axarr[0, 2], axarr[0, 3], axarr[1, 1], axarr[1, 2], ]
incomplete_tag_axes = [axarr[0, 1], axarr[0, 2], axarr[1, 3]]
incomplete_tag_axes2 = [axarr[0, 1], axarr[0, 2], axarr[0, 3], axarr[1, 3]]
# Loop over sequences
traces = []
prev_stim, has_legend, longest = 0, False, 0
for sn, seq in enumerate(sequences):
# check we didn't have a stim too close
if seq.STIM - prev_stim < 60*30:
print('Stim too close')
continue
else:
pre_stim = seq.STIM
# Check for aborted escapes
if seq.E is None or seq.C is None or seq.B is None or seq.H is None: continue
start, end, X, Y, T = complete_X_Y(seq)
colors = (grey, salmon, orange, teal, indigo, grey)
x_col, s_col = shelt_dist_color, speed_color
lab = lbls
if data.is_rec[start] == 0: continue # stim when not recording
if end-start > longest:
longest = end - start
# plot tracking
axarr[0, 0].plot(data.x[start:end].values, lw=2, color=x_col, alpha=1)
ax0.plot(data.s[start:end].values, lw=2, color=s_col, alpha=.5)
# plot ROI signal
sig = dff(rois[roi][start:end])
# Get sognal onset
onset = get_onset(sig)
if onset is not None:
axarr[1, 0].scatter(onset, sig[onset], lw=4, color=[.2, .2, .2], zorder=200)
axarr[1, 4].plot(sig[onset-30:onset+30], color=blue_grey_dark)
# Get a distribution of signal slopes in baseline
K = 20
baseline = sig[n_frames_pre-K:n_frames_pre]
window = 5
slope = pd.Series(baseline).rolling(window, min_periods=1, center=True).apply(get_slope, raw=True)
intercept = pd.Series(baseline).rolling(window, min_periods=1, center=True).apply(get_intercept, raw=True)
slopes['baseline'].extend(list(slope))
for n in range(int(len(baseline)/window)):
x0, x1 = n * window - (window/2) + n_frames_pre-K, (n+1) * window - (window/2) + n_frames_pre-K
# try:
# plot_with_slope(axarr[1, 0], x0, x1, slope[n*window], intercept[n*window], lw=2, color='g', zorder=100)
# except:
# pass
# Plot sequence chunks
for n, (x, (t0, t1), tag, color) in enumerate(zip(X, Y, T, colors)):
axarr[1, 0].plot(x, sig[t0:t1], lw=3, color=color, label=lab[n] if not has_legend else None)
# Get slopes
if tag != 'after':
slope = pd.Series(sig[t1:t1+K]).rolling(window, min_periods=1, center=True).apply(get_slope, raw=True)
intercept = pd.Series(sig[t1:t1+K]).rolling(window, min_periods=1, center=True).apply(get_intercept, raw=True)
for m in range(int(K/window)):
x0, x1 = m * window - (window/3) + 31 , (m+1) * window - (window/3) + 31
# try:
# plot_with_slope(tag_axes[n], x0, x1, slope[m*window], intercept[m*window], lw=3, color=blue_grey_darker, zorder=100)
# except:
# pass
slopes[tag].extend(list(slope))
if n > 0 and n < 4:
try:
axarr[0, 0].scatter(x[0], data.x[t0+start], color=color, lw=1,
edgecolors=[.2, .2, .2], s=100, zorder=100)
axarr[1, 0].scatter(x[0], sig[t0], color=color, lw=1,
edgecolors=[.2, .2, .2], s=100, zorder=100,)
except:
pass
# Plot tags aligned
try:
if n < len(colors) - 1:
tag_axes[n].plot(np.arange(30), sig[t1-30:t1], color=colors[n], lw=3, alpha=.6)
tag_axes[n].plot(np.arange(30, 60), sig[t1:t1+30], color=colors[n+1], lw=3, alpha=.6)
tag_axes[n].scatter(30, sig[t1], s=100, zorder=100, color=colors[n+1], lw=1, edgecolors=[.3, .3, .3])
except:
continue
has_legend = True
# Get if ROI active
significant = get_significant_slopes(slopes)
ACTIVE['mouse'].append(sess['mouse'])
ACTIVE['date'].append(sess['date'])
ACTIVE['roi'].append(roi)
if np.any(significant):
ACTIVE['active'].append(True)
else:
ACTIVE['active'].append(False)
# Plot slopes KDE
for n, (col, (k, slps)) in enumerate(zip(slopes_colors.values(), slopes.items())):
bins = np.linspace(-.2, .2, 10)
if significant[n]:
alpha = 1
else:
alpha = .2
plot_kde(ax=axarr[1, 3], data=slps, color=col, alpha=alpha, normto=.8, z=6-n, label=k)
axarr[1, 3].axvline(0, color=[.5, .5, .5], lw=2, zorder=-1)
axarr[1, 3].set(yticks=[], xlabel='Mean slope', ylabel='Slopes distribution')
# style axes
axarr[1, 0].legend()
ax0.set(title='Behaviour', ylim=[0, 150], ylabel='speed\n$\\frac{cm}{s}$')
if longest == 0: continue
x = np.arange(60, longest+30, 30)
axarr[1, 0].set(title='Signal', ylabel=f'{roi}\nDFF',
xlim=[x.min(), x.max()],
xticks=x, xticklabels=((x-n_frames_pre)/30).astype(np.int32), xlabel='s')
axarr[0, 0].set(ylim=[0, 80], ylabel='X position\n$cm$', xticks=[], xlim=[x.min(), x.max()],)
clean_axes(f)
axes, titles = tag_axes, ('stim', 'start', 'run', 'shelter', 'stop')
for ax, ttl in zip(tag_axes, titles):
ax.set(title=ttl)
ax.set(xticks=[0, 30, 60], xticklabels=[-1, 0, 1], xlabel='s', yticks=[], ylim=axarr[1, 0].get_ylim())
ax.spines['left'].set_visible(False)
ax.axvline(30, color=[.2, .2, .2], lw=3)
axarr[1, 4].set(title='Onset')
axarr[1, 4].set(xticks=[0, 30, 60], xticklabels=[-1, 0, 1], xlabel='s', yticks=[], ylim=axarr[1, 0].get_ylim())
axarr[1, 4].axvline(30, color=[.2, .2, .2], lw=3)
save_figure(f, fld/f'{sess["mouse"]}_{sess["date"]}__{roi}_active_{ACTIVE["active"][-1]}', verbose=False)
plt.close(f)
# break
# break
pd.DataFrame(ACTIVE).to_hdf('ACTIVE_ROIS.h5', key='hdf')
# %%
'''
Make a similar plot but looking at the first PC of each FOV
'''
for sess in Sessions.fetch(as_dict=True):
print(sess['mouse'], sess['date'])
data, rois = get_session_data(sess['mouse'], sess['date'], roi_data_type='raw')
if data is None: continue
tags = get_session_tags(sess['mouse'], sess['date'],
etypes=('visual', 'audio', 'audio_visual'),
ttypes=('H', 'B', 'C', 'E'))
# get tags sequences
sequences = get_tags_sequences(tags)
if len(set([s.STIM for s in sequences])) <3: continue
# get active ROIs
rois = get_active_rois(rois, sequences, sess)
signals = rois.values[data.is_rec==1, :].astype(np.float32)
scaler = StandardScaler().fit(signals)
pca = PCA(n_components=1).fit(scaler.transform(signals))
pc = pca.transform(scaler.transform(rois.values))
# Create a figure
f, axarr = plt.subplots(nrows=2, ncols=4, figsize=(18, 9), sharex=False, gridspec_kw={'width_ratios': [4, 1, 1, 1]})
ax0 = axarr[0, 0].twinx()
axarr[0, 0].axvline(n_frames_pre, lw=2, color=salmon)
axarr[1, 0].axvline(n_frames_pre, lw=2, color=salmon)
tag_axes = [axarr[0, 1], axarr[0, 2], axarr[0, 3], axarr[1, 1], axarr[1, 2], ]
incomplete_tag_axes = [axarr[0, 1], axarr[0, 2], axarr[1, 3]]
incomplete_tag_axes2 = [axarr[0, 1], axarr[0, 2], axarr[0, 3], axarr[1, 3]]
# Loop over sequences
traces = []
prev_stim, has_legend, longest = 0, False, 0
for sn, seq in enumerate(sequences):
# check we didn't have a stim too close
if seq.STIM - prev_stim < 60*30:
print('Stim too close')
continue
else:
pre_stim = seq.STIM
# Check for aborted escapes
if seq.E is None or seq.C is None or seq.B is None or seq.H is None: continue
start, end, X, Y, T = complete_X_Y(seq)
colors = (grey, salmon, orange, teal, indigo, grey)
x_col, s_col = shelt_dist_color, speed_color
lab = lbls
if data.is_rec[start] == 0: continue # stim when not recording
if end-start > longest:
longest = end - start
# plot tracking
axarr[0, 0].plot(data.x[start:end].values, lw=2, color=x_col, alpha=1)
ax0.plot(data.s[start:end].values, lw=2, color=s_col, alpha=.5)
# plot ROI signal
sig = pc[start:end]
# Plot sequence chunks
for n, (x, (t0, t1), tag, color) in enumerate(zip(X, Y, T, colors)):
axarr[1, 0].plot(x, sig[t0:t1], lw=3, color=color, label=lab[n] if not has_legend else None)
if n > 0 and n < 4:
try:
axarr[0, 0].scatter(x[0], data.x[t0+start], color=color, lw=1,
edgecolors=[.2, .2, .2], s=100, zorder=100)
axarr[1, 0].scatter(x[0], sig[t0], color=color, lw=1,
edgecolors=[.2, .2, .2], s=100, zorder=100,)
except:
pass
# Plot tags aligned
try:
if n < len(colors) - 1:
tag_axes[n].plot(np.arange(30), sig[t1-30:t1], color=colors[n], lw=3, alpha=.6)
tag_axes[n].plot(np.arange(30, 60), sig[t1:t1+30], color=colors[n+1], lw=3, alpha=.6)
tag_axes[n].scatter(30, sig[t1], s=100, zorder=100, color=colors[n+1], lw=1, edgecolors=[.3, .3, .3])
except:
continue
has_legend = True
# style axes
axarr[1, 0].legend()
ax0.set(title='Behaviour', ylim=[0, 150], ylabel='speed\n$\\frac{cm}{s}$')
if longest == 0: continue
x = np.arange(60, longest+30, 30)
axarr[1, 0].set(title='Signal', ylabel=f'FIRST PC ON ACTIVE ROIS',
xlim=[x.min(), x.max()],
xticks=x, xticklabels=((x-n_frames_pre)/30).astype(np.int32), xlabel='s')
axarr[0, 0].set(ylim=[0, 80], ylabel='X position\n$cm$', xticks=[], xlim=[x.min(), x.max()],)
clean_axes(f)
axarr[-1, -1].axis('off')
axes, titles = tag_axes, ('stim', 'start', 'run', 'shelter', 'stop')
for ax, ttl in zip(tag_axes, titles):
ax.set(title=ttl)
ax.set(xticks=[0, 30, 60], xticklabels=[-1, 0, 1], xlabel='s', yticks=[], ylim=axarr[1, 0].get_ylim())
ax.spines['left'].set_visible(False)
ax.axvline(30, color=[.2, .2, .2], lw=3)
save_figure(f, fld/f'{sess["mouse"]}_{sess["date"]}__FIRST_PC', verbose=False)
plt.close(f)
# break
# # %%
# # %%
# # %%