-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunifiedData_populationAnlaysis_190605.m
executable file
·476 lines (400 loc) · 14.8 KB
/
unifiedData_populationAnlaysis_190605.m
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
%post-unification script!
scatfold = '/Users/galileo/Dropbox (HMS)/p2/correctedScatterplots_190603';
load('/Users/galileo/Dropbox (HMS)/p2/dataTable_p2.mat', 'T')
singleAntData = matfile('/Users/galileo/Dropbox (HMS)/p2/singleAntennaData.mat');
% load stuff up. Assume X vars are different
for t = 1:size(T,1)
mfile = matfile(T.linkToDataFile{t});
data(t).flyNum = T.flynum(t);
data(t).aL = mfile.aL;
data(t).aR = mfile.aR;
data(t).dir = mfile.directions;
data(t).int = mfile.intensities;
data(t).Vm = mfile.meanfiltVm;
data(t).FR = mfile.meanFR;
end
clear mfile
% I30 = intensities == 30;
% I60 = intensities == 60;
% I120 = intensities == 120;
%% population direction tuning curve at highest intensity
for t = 1:7
aR = data(t).aR;
aL = data(t).aL;
directions = data(t).dir;
intensities = data(t).int;
meanFR = data(t).FR;
I30 = intensities == 30; %patch
I60 = intensities == 60;
I120 = intensities == 120;
unifSingAntData(t).dirI30 = meanFR(I30)-meanFR(1);
unifSingAntData(t).dirI60 = meanFR(I60)-meanFR(1);
unifSingAntData(t).dirI120 = meanFR(I120)-meanFR(1);
end
figure; hold on
h = plot(directions(I120), cat(1,unifSingAntData.dirI120), '-o', 'MarkerSize', 4);
set(h, {'MarkerFaceColor'}, get(h,'Color'));
legend(num2str(T.flynum))
legend boxoff
legend('Location', 'SouthEast')
ylabel('FR (Hz)')
savefig(fullfile(scatfold, sprintf('popul_dirTuning_FR_RestSubtr_I120')))
export_fig(fullfile(scatfold, sprintf('popul_dirTuning_FR_RestSubtr_I120.pdf')))
% normalize between -1 and 1 first
Y = cat(1,unifSingAntData.dirI120);
Y = bsxfun(@rdivide, Y, max(Y,[],2) );
NegPeakDivide = max(ones(size(Y,1), 1), abs(min(Y,[],2)) );
Y = bsxfun(@rdivide, Y, NegPeakDivide );
figure; hold on
h = plot(directions(I120), Y, '-o', 'MarkerSize', 4);
set(h, {'MarkerFaceColor'}, get(h,'Color'));
legend(num2str(T.flynum))
legend boxoff
legend('Location', 'SouthEast')
ylabel('norm -1 to 1 FR')
savefig(fullfile(scatfold, sprintf('popul_dirTuning_normFR_I120')))
export_fig(fullfile(scatfold, sprintf('popul_dirTuning_normFR_I120.pdf')))
% smooth a bit?
Y = cat(1,unifSingAntData.dirI120);
Y = movmean(Y, 3, 2);
Y = bsxfun(@rdivide, Y, max(Y,[],2) );
NegPeakDivide = max(ones(size(Y,1), 1), abs(min(Y,[],2)) );
Y = bsxfun(@rdivide, Y, NegPeakDivide );
figure; hold on
h = plot(directions(I120), Y, '-o', 'MarkerSize', 4);
set(h, {'MarkerFaceColor'}, get(h,'Color'));
legend(num2str(T.flynum))
legend boxoff
legend('Location', 'SouthEast')
ylabel('norm -1 to 1: smoothed(FR), movmean 3')
savefig(fullfile(scatfold, sprintf('popul_dirTuning_normsmoothFR_I120')))
export_fig(fullfile(scatfold, sprintf('popul_dirTuning_normsmoothFR_I120.pdf')))
%% sort out single antennae contribution
for t = 1:size(T,1)
aR = data(t).aR;
aL = data(t).aL;
directions = data(t).dir;
intensities = data(t).int;
meanFR = data(t).FR;
if ~ismember(t, 8:10)
ixL = find(aR==0);
ixL = ixL([2:4,1,5:7]);
aL_ixL = aL(ixL);
ixR = find(aL==0);
ixR = ixR([2:4,1,5:7]);
aR_ixR = aR(ixR); % xaxis - intervals are not linear, plus values might not be sorted? - but they are
unifSingAntData(t).aL_only = aL_ixL;
unifSingAntData(t).aR_only = aR_ixR;
unifSingAntData(t).FR_L_only = meanFR(ixL);
unifSingAntData(t).FR_R_only = meanFR(ixR);
else % show single antenna contribution for 223 233 flies
aL_ixL = aL(aR==0); %these are already sorted!
aR_ixR = aR(aL==0); %these are already sorted!
unifSingAntData(t).aL_only = aL_ixL;
unifSingAntData(t).aR_only = aR_ixR;
unifSingAntData(t).FR_L_only = meanFR(aR==0);
unifSingAntData(t).FR_R_only = meanFR(aL==0);
end
end
clearvars -except unifSingAntData data T singleAntData scatfold
%% plot all lines for each antenna
% figure; hold on
% for t = 1:size(T,1)
% plot(unifData(t).aL_only, unifData(t).FR_L_only);
% end % all over the place
figure; hold on
a = gca;
a.ColorOrder = cat(1, a.ColorOrder, [38 34 97; 235 0 139; 0 104 56]./255);
for t = 1:size(T,1)
zero = unifSingAntData(t).aL_only==0;
plot(unifSingAntData(t).aL_only, unifSingAntData(t).FR_L_only - unifSingAntData(t).FR_L_only(zero));
end
title('L antenna')
legend(num2str(T.flynum))
legend('Location', 'Best')
ylim([-8 10])
export_fig(fullfile(scatfold, sprintf('allFlies_singleAntenna_LEFTmoving.pdf')))
figure; hold on
a = gca;
a.ColorOrder = cat(1, a.ColorOrder, [38 34 97; 235 0 139; 0 104 56]./255);
for t = 1:size(T,1)
zero = unifSingAntData(t).aL_only==0; %L-R does not matter, there is just one zero.
plot(unifSingAntData(t).aR_only, unifSingAntData(t).FR_R_only - unifSingAntData(t).FR_R_only(zero));
end
title('R antenna')
legend(num2str(T.flynum))
legend('Location', 'Best')
ylim([-8 10])
export_fig(fullfile(scatfold, sprintf('allFlies_singleAntenna_RIGHTmoving.pdf')))
%% plot one cell at the time, with CI of zero - some error, but already done in bilateralAnt_unify_datatable...
% for t = 9:size(T,1)
% figure; hold on
% title(T.flynum(t))
% zero = unifData(t).aL_only==0;
% CI_centered = singleAntData.CI95bootstrap(1,t);
% CI_centered = CI_centered{1} - unifData(t).FR_L_only(zero);
% plot(unifData(t).aL_only, unifData(t).FR_L_only - unifData(t).FR_L_only(zero), '-<');
% plot(unifData(t).aL_only, unifData(t).FR_R_only - unifData(t).FR_R_only(zero), '->');
% plot([unifData(t).aL_only(1), unifData(t).aL_only(end)], [unifData(t).FR_L_only(zero)+CI_centered(2), unifData(t).FR_L_only(zero)+CI_centered(2)], ':k')
% plot([unifData(t).aL_only(1), unifData(t).aL_only(end)], [unifData(t).FR_L_only(zero)+CI_centered(1), unifData(t).FR_L_only(zero)+CI_centered(1)], ':k')
% end
%% averaging push and averaging pull displacements
for t = 1:size(T,1)
zeroI = find(unifSingAntData(t).aL_only==0);
FR_L = unifSingAntData(t).FR_L_only - unifSingAntData(t).FR_L_only(zeroI);
FR_R = unifSingAntData(t).FR_R_only - unifSingAntData(t).FR_R_only(zeroI);
analyzedData.pullL(t) = mean(FR_L(1:zeroI-1));
analyzedData.pullR(t) = mean(FR_R(1:zeroI-1));
analyzedData.pushL(t) = mean(FR_L(zeroI+1:end));
analyzedData.pushR(t) = mean(FR_R(zeroI+1:end));
analyzedData.pullX(t) = mean(unifSingAntData(t).aL_only(1:zeroI-1));
analyzedData.pushX(t) = mean(unifSingAntData(t).aL_only(zeroI+1:end));
end
figure; hold on
a = gca;
a.ColorOrder = cat(1, a.ColorOrder, [38 34 97; 235 0 139; 0 104 56]./255);
plot([analyzedData.pullX; zeros(1,size(T,1)); analyzedData.pushX],...
[analyzedData.pullL; zeros(1,size(T,1)); analyzedData.pushL], '-')
ylim([-5 5])
export_fig(fullfile(scatfold, sprintf('allFlies_singleAnt_avgPULLavgPUSH_LEFTmoving.pdf')))
figure; hold on
a = gca;
a.ColorOrder = cat(1, a.ColorOrder, [38 34 97; 235 0 139; 0 104 56]./255);
plot([analyzedData.pullX; zeros(1,size(T,1)); analyzedData.pushX],...
[analyzedData.pullR; zeros(1,size(T,1)); analyzedData.pushR], '-')
ylim([-5 5])
legend(num2str(T.flynum))
legend('Location', 'Best')
export_fig(fullfile(scatfold, sprintf('allFlies_singleAnt_avgPULLavgPUSH_RIGHTmoving.pdf')))
%% linear summation - scatter and interpolated 2d map
clearvars -except unifSingAntData data T singleAntData scatfold
for t = 1:10 %size(T,1)
aR = data(t).aR;
aL = data(t).aL;
directions = data(t).dir;
intensities = data(t).int;
meanFR = data(t).FR;
if ~ismember(t, 8:10)
zero_joint = 1;
ixL = find(aR==0);
ixL = ixL([2:4,1,5:7]);
aL_ixL = aL(ixL);
ixR = find(aL==0);
ixR = ixR([2:4,1,5:7]);
aR_ixR = aR(ixR); % xaxis - intervals are not linear, plus values might not be sorted? - but they are
else % 223 233 flies
zero_joint = 25;
ixL = find(aR==0);
ixR = find(aL==0);
aL_ixL = aL(aR==0); %these are already sorted!
aR_ixR = aR(aL==0); %these are already sorted!
end
%% reg scatter - symmetrical colorbar (test)
aLExt = [-20; aL];
aRExt = [-20; aR];
d1 = meanFR(zero_joint) - min(meanFR);
d2 = max(meanFR) - meanFR(zero_joint);
if d1 <= d2
meanFRExt = [meanFR(zero_joint)-d2, meanFR];
else
meanFRExt = [meanFR(zero_joint)+d1, meanFR];
end
figure; hold on; axis image
xlabel(' Left antenna (ipsi) - angular displ')
ylabel(' Right antenna (contra) - angular displ')
scatter(aLExt, aRExt, 200, meanFRExt-meanFR(zero_joint), 'filled', 'MarkerEdgeColor', 'k')
title(sprintf('Vm (N = %d)\n fly %d', T.NtrialsIncluded(t), T.flynum(t)), 'Interpreter', 'none')
colormap(bluewhitered(256)), cb = colorbar;
cb.TickLabels = num2str((cb.Ticks+meanFR(zero_joint))', '%2.1f');
cb.Label.String = '(Hz)';
xlim([-15 15])
ylim([-15 15])
set(gca, 'TickDir', 'out') % OK!
%
export_fig(fullfile(scatfold, sprintf('dtAVG_FR_scatter_symmetricalCB_fly%d.pdf', T.flynum(t))))
close
%% reg scatter
% figure; hold on; axis image
% xlabel(' Left antenna (ipsi) - angular displ')
% ylabel(' Right antenna (contra) - angular displ')
% scatter(aL, aR, 200, meanFR-meanFR(zero_joint), 'filled', 'MarkerEdgeColor', 'k')
%
% title(sprintf('Vm (N = %d)\n fly %d', T.NtrialsIncluded(t), T.flynum(t)), 'Interpreter', 'none')
% colormap(bluewhitered(256)), cb = colorbar;
% cb.TickLabels = num2str((cb.Ticks+meanFR(zero_joint))', '%2.1f');
% cb.Label.String = '(Hz)';
% xlim([-15 15])
% ylim([-15 15])
% set(gca, 'TickDir', 'out') % OK!
%
%% calculate scatter and interpolation: -redo this 0605
%assume each antenna coparticipates equally to rest: split rest in half
%and subtract it from single antenna data:
LonlyFR = meanFR(ixL) - meanFR(zero_joint)/2;
RonlyFR = meanFR(ixR) - meanFR(zero_joint)/2;
[Xq,Yq] = meshgrid(min(aL):0.25:max(aL));
% make scattered data combinations Z
[Lx,Ry] = meshgrid(aL_ixL, aR_ixR);
[FRx,FRy] = meshgrid(LonlyFR, RonlyFR);
Z = (FRx+FRy); % ---------------- OK
F_data = scatteredInterpolant(aL(:),aR(:),meanFR(:));
F_data.Method = 'natural';
ExtrapolationMethod = 'nearest';
VDq = F_data(Xq, Yq);
% lin sum interpolate
F = scatteredInterpolant(Lx(:),Ry(:),Z(:));
F.Method = 'natural';
ExtrapolationMethod = 'nearest';
Vq = F(Xq, Yq);
%% difference map
VDqS = VDq - meanFR(zero_joint);
VqS = Vq - meanFR(zero_joint);
% subtract (observed) - nrm(expected)
mapDiff = VDqS - VqS;
% crop the borders that contain larger errors
redx = 11.3;
redXq = Xq;
redXq(Xq<-redx | Xq>redx) = nan;
redXq(Yq<-redx | Yq>redx) = nan;
redYq = Yq;
redYq(Xq<-redx | Xq>redx) = nan;
redYq(Yq<-redx | Yq>redx) = nan;
mapDiff(Xq<-redx | Xq>redx) = nan;
mapDiff(Yq<-redx | Yq>redx) = nan;
mD = mapDiff;
% plot with histogram
figure
% subplot(1,3,[1,2])
h = surf(redXq, redYq, mD) ;
az = 0;
el = 90;
view(az, el);
xlim([-15 15])
ylim([-15 15])
h.EdgeColor = 'none';
axis on
ax = gca;
ax.XTick = [];
ax.YTick = [];
ax.XAxis.Color = 'w';
ax.YAxis.Color = 'w';
axis square
colormap(jet)
clim = get(gca,'CLim');
set(gca,'CLim',[-max(abs(clim)), max(abs(clim))]);
cb = colorbar;
cb.TickDirection = 'out';
% cb.Location = 'westoutside';
b.FontName = 'Arial';
colormap(jet)
export_fig(fullfile(scatfold, sprintf('diffMap_FR__NOhistogram_%d.pdf', T.flynum(t))))
% subplot(1,3,3)
% h = histogram(mapDiff(:));
% h.Orientation = 'horizontal';
% set(gca,'YLim',[-max(abs(clim)), max(abs(clim))]);
% h.FaceAlpha = 1;
% h.FaceColor = [0.4 0.4 0.4];
% a = gca;
% a.TickDir = 'out';
% a.XAxis.Visible = 'off';
% a.Box = 'off';
% a.YAxis.Visible = 'off';
% export_fig(fullfile(scatfold, sprintf('diffMap_FR__plushistogram_%d.pdf', T.flynum(t))))
% %% normalize:
% if max(VDqS(:)) > abs(min(VDqS(:)))
% %normalize by the maximum
% VDqS = VDqS./max(VDqS(:));
% VqS = VqS./max(VqS(:));
% else
% VDqS = VDqS./abs(min(VDqS(:)));
% VqS = VqS./abs(min(VqS(:)));
% end
%
%
%
% % subtract norm(observed) - norm(expected)
% mapDiff = VDqS - VqS;
% figure; imagesc(mapDiff);
% set(gca,'CLim',[-1,1])
% colormap(jet)
% cb = colorbar;
% cb.Ticks = -1:0.25:1;
% axis square
% axis off
% title(sprintf('norm(observed) - norm(modeled)\nfly %d', T.flynum(t)))
% export_fig(fullfile(scatfold, sprintf('diffMap_-1to1_%d.pdf', T.flynum(t))))
%
%
%
%% plot scatter of lin sum
% % figure; hold on; axis image
% % xlabel('L')
% % ylabel('R')
% % scatter(Lx(:), Ry(:), 200, Z(:)-meanFR(zero_joint), 'filled', 'MarkerEdgeColor', 'k')
% % colormap(bluewhitered(256)), cb = colorbar;
% % cb.TickLabels = num2str((cb.Ticks+meanFR(zero_joint))', '%2.1f');
% % xlim([-15 15])
% % ylim([-15 15])
% % set(gca, 'TickDir', 'out')
% % title(sprintf('linear sum of FR of either antenna\nfly %d', T.flynum(t)), 'Interpreter', 'none')
% % export_fig(fullfile(scatfold, sprintf('linSum_scatter_%d.pdf', T.flynum(t))))
% %
% %
%% plot interpolation of lin sum
% figure;
% xlabel('L (deg)')
% ylabel('R (deg)')
% h = surf(Xq,Yq,VqS);
%
% az = 0;
% el = 90;
% view(az, el);
% xlim([-15 15])
% ylim([-15 15])
% axis square
% a = gca;
% a.XAxis.Visible = 'off';
% a.YAxis.Visible = 'off';
% a.XGrid = 'off';
% a.YGrid = 'off';
% h.EdgeColor = 'none';
%
% colormap(bluewhitered(256))
% cb1 = colorbar;
% cb1.TickLabels = num2str((cb1.Ticks+meanFR(zero_joint))', '%2.1f');
% % cb1.Label.String = '(Hz)';
%
% title(sprintf('interp from single antenna data - fly %d',T.flynum(t)), 'Interpreter', 'none')
% export_fig(fullfile(scatfold, sprintf('interp_linSum_scatter_%d.pdf', T.flynum(t))))
% close
% clear cb1
% %
% % % % will not work in the same figure; as bluewhitered applies to all
% % % axes..... still, important to recalculate it with the same
% % % coordinates
% % figure
% % xlabel('L (deg)')
% % ylabel('R (deg)')
% % h = surf(Xq,Yq,VDq-meanFR(zero_joint));
% %
% % az = 0;
% % el = 90;
% % view(az, el);
% %
% % xlim([-15 15])
% % ylim([-15 15])
% % axis square
% % a = gca;
% % a.XAxis.Visible = 'off';
% % a.YAxis.Visible = 'off';
% % a.XGrid = 'off';
% % a.YGrid = 'off';
% % h.EdgeColor = 'none';
% %
% % colormap(bluewhitered(256)), cb2 = colorbar;
% % cb2.TickLabels = num2str((cb2.Ticks+meanFR(zero_joint))', '%2.1f');
% % cb2.Label.String = '(Hz)';
% %
% % title('(recorded) joint-antenna data', 'Interpreter', 'none')
cab(1)
end