-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstartExample.m
More file actions
137 lines (107 loc) · 4.17 KB
/
startExample.m
File metadata and controls
137 lines (107 loc) · 4.17 KB
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
%% Generalized linear model fit to calcium signals
% includes example test case
%
% Alex Kwan, 2018
%
% AK wrote the code based on the procedures described in
% Pinto and Dan, Neuron 2015
clear all;
% set up figure plotting
setup_figprop;
%% Generate mock data
% --- create mock behavior time stamps
numTrial = 100; %number of trials
trialDur = 10; %each trial is 10-second long
dt = 0.01;
t = [0:dt:numTrial*trialDur]';
stimTime = [trialDur+1:trialDur:t(end)]'; %stimulus appears 1 s after start of trial (except the first trial)
choiceTime = stimTime + 0.5 + 2*rand(size(stimTime)); %animal responds with variable delay
% --- create mock calcium signals
dff=zeros(length(t),1);
% each stimulus would elicit a synaptic-potential-like dF/F
t_kernel=[-5:dt:5]';
tau = 0.5; ampl = 1;
kernel_stim=ampl.*t_kernel./tau.*exp(-t_kernel/tau);
kernel_stim(1:ceil(end/2))=0;
for j=1:length(stimTime)
dff=dff+[zeros(round((stimTime(j)-t(1)+t_kernel(1))/dt),1); kernel_stim; zeros(length(dff)-length(zeros(1,round((stimTime(j)-t(1)+t_kernel(1))/dt)))-length(kernel_stim),1)];
end
% each choice would elicit a synaptic-potential-like dF/F
% (at half the initial amplitude, and twice the time constant)
tau = 1; ampl = -0.5;
kernel_choice=ampl.*t_kernel./tau.*exp(-t_kernel/tau);
kernel_choice(1:ceil(end/2))=0;
for j=1:length(choiceTime)
dff=dff+[zeros(round((choiceTime(j)-t(1)+t_kernel(1))/dt),1); kernel_choice; zeros(length(dff)-length(zeros(1,round((choiceTime(j)-t(1)+t_kernel(1))/dt)))-length(kernel_choice),1)];
end
% add noise
dff = dff + 0.05*randn(size(dff));
% --- plot the mock data, first 100 seconds
figure;
subplot(4,1,1); hold on;
for j=1:length(stimTime)
h1=plot(stimTime(j)*[1 1],[0 1],'k');
end
for j=1:length(choiceTime)
h2=plot(choiceTime(j)*[1 1],[0 1],'r');
end
ylabel('Stim and Choice');
xlim([t(1) 100]);
legend([h1;h2],[{'Stim'};{'Choice'}]);
subplot(4,1,2); hold on;
plot(t,dff,'k-','LineWidth',2);
xlabel('Time (s) [first 100 s]');
ylabel('dF/F');
xlim([t(1) 100]);
print(gcf,'-djpeg','mockdata-raw'); %jpeg format
%% Compare ideal kernel with the trial-averaged signals
% Trial averaging at the event times
window=[-3 3];
[sigbyTrial_stim, tbyTrial_stim] = align_signal(t,dff,stimTime,window);
[sigbyTrial_choice, tbyTrial_choice] = align_signal(t,dff,choiceTime,window);
figure;
subplot(2,1,1); hold on;
plot(tbyTrial_stim,nanmean(sigbyTrial_stim,2),'r');
plot(t_kernel,kernel_stim,'k--');
xlabel('Time from stim (s)');
legend('Estimate using trial-averaging','Kernel used to create the data');
xlim(window); ylim([-0.3 0.6]);
subplot(2,1,2); hold on;
plot(tbyTrial_choice,nanmean(sigbyTrial_choice,2),'b');
plot(t_kernel,kernel_choice,'k--');
xlabel('Time from choice (s)');
legend('Estimate using trial-averaging','Kernel used to create the data');
xlim(window); ylim([-0.3 0.6]);
print(gcf,'-djpeg','mockdata-trialavg'); %jpeg format
%% Generalized linear regression
params=[];
% --- define the predictors
% the task events that will be used to construct GLM to predict neural activity
%predictor 1: stim
params.eventTime{1} = stimTime;
params.eventLabel{1} = 'Stimulus';
%predictor 2: choice
params.eventTime{2} = choiceTime;
params.eventLabel{2} = 'Choice';
%how far do the predictors go backward and forward in time?
params.window = window;
% --- define the time periods during which the predictors will be used to
% predict the calcium signals (for real data, will ignore periods when the
% animal was inactive and not performing any actions)
%only perform the analysis between intervals define by these times
params.trigTime(:,1) = stimTime - 3;
params.trigTime(:,2) = stimTime + 3;
% --- fit GLM and plot
[glreg, glreg_fit]=gl_regr( dff, t, params.trigTime, params.eventTime, params );
%plot_gl_regr_fit(glreg_fit,glreg,params);
plot_gl_regr_beta(glreg,params);
% --- also plot the kernel on the same plots, for comparison
subplot(2,1,1); hold on;
plot(t_kernel,kernel_stim,'k--');
legend('Estimate using GLM','Kernel used to create the data');
xlim(window); ylim([-0.3 0.6]);
subplot(2,1,2); hold on;
plot(t_kernel,kernel_choice,'k--');
legend('Estimate using GLM','Kernel used to create the data');
xlim(window); ylim([-0.3 0.6]);
print(gcf,'-djpeg','mockdata-glmfit'); %jpeg format