-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcal_prediction.m
268 lines (202 loc) · 8.05 KB
/
cal_prediction.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
function [ para, BOLD_prediction, Rsquare ] = cal_prediction( which_data, which_model, which_type, fittime ,v2_mean_op , E_op ,w_d)
% This function is used to calculate the BOLD prediction of each dataset.
% The first value means dataset: e.p. Ca69_v1
% Second value means model: e.p. std
% Thrid value means type of the model: e.p.
% Load the dataset for our training the model
% Go foward to the right fold to get the data
addpath(genpath(fullfile(pwd,'ROImean')));
% load the right trainning data according to the dataset we choose
switch which_data
case {'Ca69_v1' , 'Ca69_v2' , 'Ca69_v3'}
load v_mean_69
switch which_data
case 'Ca69_v1'
v_mean = v_mean_69(1 , : );
case 'Ca69_v2'
v_mean = v_mean_69(2 , : );
case 'Ca69_v3'
v_mean = v_mean_69(3 , : );
end
case {'Ca05_v1' , 'Ca05_v2' , 'Ca05_v3'}
load v_mean_05;
switch which_data
case 'Ca05_v1'
v_mean = v_mean_05(1 , : );
case 'Ca05_v2'
v_mean = v_mean_05(2 , : );
case 'Ca05_v3'
v_mean = v_mean_05(3 , : );
end
case { 'K1_v1' , 'K1_v2' , 'K1_v3' }
load v_mean_K1;
switch which_data
case 'K1_v1'
v_mean = v_mean_K1(1 , : );
case 'K1_v2'
v_mean = v_mean_K1(2 , : );
case 'K1_v3'
v_mean = v_mean_K1(3 , : );
end
case { 'K2_v1' , 'K2_v2' , 'K2_v3' }
load v_mean_K2
switch which_data
case 'K2_v1'
v_mean = v_mean_K2( 1 , : );
case 'K2_v2'
v_mean = v_mean_K2(2 , : );
case 'K2_v3'
v_mean = v_mean_K2(3 , : );
end
case 'new'
v_mean = v2_mean_op;
otherwise
disp('Choose the right dataset')
end
% load input of our model
% Go forwads to the right fold
addpath(genpath(fullfile(pwd,'E')));
switch which_type
% Load the input of the model according to the kinds of model we choose
case 'orientation'
% Because this is a model care much about the variance of the
% orientation, we load Etot
switch which_data
case {'Ca69_v1' , 'Ca69_v2' , 'Ca69_v3'}
load E_ori_69;
E_ori = E_ori_69;
case {'Ca05_v1' , 'Ca05_v2' , 'Ca05_v3'}
load E_or_05
E_ori = E_ori_05;
case {'K1_v1' , 'K1_v2' , 'K1_v3' , 'K2_v1' , 'K2_v2' , 'K2_v3'}
load E_ori_K;
E_ori = E_ori_K;
case 'new'
E_ori = E_op;
otherwise
disp('input the right data')
end
% Then we begin our model fitting with random start point.
% Set up the random start points.
% Set up the boundary of paramters
LB= [ 0, 0, 0];
UB= [200, 700, 10];
PLB= [0.00005, 0, 0];
PUB=[150, 10, 2];
% Random the start point
x0_w = LB(1) + (UB(1) - LB(1))*rand(fittime, 1);
x0_g = LB(2) + (UB(2) - LB(2))*rand(fittime, 1);
x0_n = LB(3) + (UB(3) - LB(3))*rand(fittime, 1);
% Integrate them into sets
x0_set = [x0_w, x0_g, x0_n];
% Choose the which model we are going to fit and assign it into
% the function
fun=@(x) FUNF(x, E_ori, v_mean, which_model , 'orientation' );
% Run n times with multiple start points
for ii = 1: size(x0_set, 1)
% Sign
ii
% Find the optimal point with BADS
[x(ii, :), SSE(ii)]=bads(fun, x0_set(ii, :), LB, UB, PLB, PUB);
%[x(ii, :), SSE(ii)]=fmincon(fun, x0_set(ii, :), [], [], [],[], LB, UB);
%[x(ii, :), SSE(ii)]=fminsearch(fun, x0_set(ii, :));
end
% find the lowest SSE
trials = find(SSE == min(SSE));
para(:) = x(trials(1), :);
% Assign the parameter
lambda = para(1);
g = para(2);
n = para(3);
% calculate normalized energy cording the model we choose
switch which_model
case 'contrast'
% Energy model
d = E_ori; % ori x example x stimili
case 'normStd'
% std model
d = E_ori ./(1 + lambda.*std(E_ori , 1)); % ori x example x stimili
case 'normVar'
% var model
d = E_ori.^2 ./(1 + lambda^2.*var(E_ori, 1)); % ori x example x stimili
case 'normPower'
d = E_ori.^2./( 1 + lambda^2.*mean(E_ori.^2, 1)); % ori x example x stimili
otherwise
disp('Please select the right model')
end
% sum over orientation
s = squeeze(mean(d , 1)) ; % example x stimili
case 'space'
switch which_data
case {'Ca69_v1' , 'Ca69_v2' , 'Ca69_v3'}
load e_xy_69;
E_space = E_xy_69;
case {'Ca05_v1' , 'Ca05_v2' , 'Ca05_v3'}
load e_xy_05;
E_space = E_xy_05;
case {'K1_v1' , 'K1_v2' , 'K1_v3' , 'K2_v1' , 'K2_v2' , 'K2_v3'}
load e_xy_K;
E_space = E_xy_K;
case 'new'
E_space = E_op;
otherwise
disp('Please select the right mode for fitting')
end
%DISK
% Set up the random start points.
% Set up the boundary of paramters
LB= [ 0, 0, 0];
UB= [3, 100, 10];
PLB= [0.00005, 0, 0];
PUB=[1, 10, 2];
% Random the start point
x0_c = LB(1) + (UB(1) - LB(1))*rand(fittime, 1);
x0_g = LB(2) + (UB(2) - LB(2))*rand(fittime, 1);
x0_n = LB(3) + (UB(3) - LB(3))*rand(fittime, 1);
% Integrate them into sets
x0_set = [x0_c, x0_g, x0_n];
% Choose the which model we are going to fit and assign it into
% the function
fun=@(x) FUNF(x, E_space, v_mean, which_model, 'space', w_d);
% Run n times with multiple start points
for ii = 1: size(x0_set, 1)
% Sign
ii
% Find the optimal point with BADS
[x(ii, :), SSE(ii)]=bads(fun, x0_set(ii, :), LB, UB, PLB, PUB);
%[x(ii, :), SSE(ii)]=fmincon(fun, x0_set(ii, :), [], [], [],[], LB, UB);
%[x(ii, :), SSE(ii)]=fminsearch(fun, x0_set(ii, :));
end
% find the lowest SSE
trials = find(SSE == min(SSE));
para(:) = x(trials(1), :);
% Assign the parameter
c = para(1);
g = para(2);
n = para(3);
switch which_model
case 'SOC'
% Do a variance-like calculation
v = (E_space - c*mean(mean(E_space, 1) , 2)).^2; % X x Y x ep x stimuli
% Create a disk as weight
d = w_d.*v; % X x Y x ep x stimuli
otherwise
disp('choose the right model')
end
% Sum over spatial position
s = squeeze(mean(mean( d , 1) , 2)); % ep x stimuli
otherwise
disp('Please select the right mode for fitting')
end
% Nonlinearity
BOLD_prediction_ind = g.*s.^n; % ep x stimuli
% Sum over different examples
BOLD_prediction = squeeze(mean(BOLD_prediction_ind, 1)); % stimuli
% Ensure that the size of BOLD_prediction and v2_mean are the
% same
if isequal( size(v_mean) , size(BOLD_prediction)) == 0
BOLD_prediction = BOLD_prediction';
end
% calculate the Rsquare
Rsquare= 1 - var(v_mean - BOLD_prediction)/var(v_mean);
end