-
Notifications
You must be signed in to change notification settings - Fork 1
/
CSF_base.m
438 lines (370 loc) · 17.7 KB
/
CSF_base.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
classdef CSF_base
%Super class of all Spatio-chromatic CSF models
properties
par; % Model parameters as a structure
mean_error;
mean_std;
end
methods( Abstract )
% A short name that could be used as a part of a file name
name = short_name( obj )
% A general interface to compute sensitivity, which takes as input a
% structure with the parameters values. This allows to add/remove
% parameters without changing the interface, more flexible use of
% parameters (e.g. either LMS or luminance of the background) and
% should be less error prone.
%
% pars is the structure with the field names that closely match
% the column names in the data files:
%
% luminance - luminance in cd/m^2. D65 background is assumed.
% lms_bkg - specify background colour and luminance
% s_frequency - spatial frequency in cpd
% t_frequency - temporal frequency in Hz (default is 0)
% orientation - orientation in deg (default is 0)
% lms_delta - modulation direction in the LMS colour space (default
% D65 luminance modulation)
% area - area of the stimulus in deg^2
% ge_sigma - radius of the gaussian envelope in deg
% eccentricity - eccentricity in deg (default 0)
% vis_field - orientation in the visual field. See README.md
% (default 0)
%
% All parameters can be specified using multidemensional arrays as
% long as of they have compatible (broadcastable) sizes. For example
% if lum has the size [10 1] and s_freq has the size [1 5], calling:
%
% csf_pars = struct( 'luminance', lum, 's_frequency', s_freq, 'area', 1 );
% S = csf_model.sensitivity( csf_pars );
%
% will return S of the size [10 5] computed for all combinations of
% lum and s_freq.
%
% 'lms_bkg' and 'lms_delta' must have their last dimension of size 3.
%
% For best performance, pass vectors with the the required number
% of parameters. Do not call the sensitivity() function in a loop.
%
% You must specify 'luminance' or 'lms_bkg' but not both.
% You must specify 'area' or 'ge_sigma' but not both.
%
% Example:
%
% csf_model = <model_name>()
% csf_pars = struct( 's_frequency', 4, 't_frequency', 1, 'orientation', 0, 'lms_bkg', [0.7443 0.3054 0.0157], 'area', 1, 'eccentricity', 0 );
% S = csf_model.sensitivity( csf_pars );
S = sensitivity( obj, pars );
end
methods
function str = full_name( obj )
str = obj.short_name();
end
% Internally used for training CSF
function obj = set_pars( obj, pars_vector )
% Set the parameters of the model, supplied as a row vector
% (used for optimizing the parameters of the model)
assert( ~isempty( obj.par ) ); % obj.par must be initialized before calling this function
obj.par = obj.param2struct( obj.par, pars_vector );
end
% Internally used for training CSF
function pars_vector = get_pars( obj )
% Get the parameters of the model as a row vector
% (used for optimizing the parameters of the model)
pars_vector = obj.struct2param( obj.par );
end
% Predict the sensitivity for a detection of a Gabour patch of certain chromatic
% direction and amplitide.
%
% S = sensitivity_stolms( obj, s_freq, t_freq, orientation, LMS_bkg, LMS_delta, area, eccentricity );
%
% Important: all parameters must be column vectors. LMS_bkg and
% LMS_delta are Nx3 matrices
%
% freq - spatial frequency in cpd
% LMS_bkg - LMS of the background colour (CIE2006 CMF)
% LMS_delta - colour direction vector in the LMS space (LMS_peak-LMS_mean)
% area - area in deg^2
%
% The method returns:
% S - Sensitivity (the inverse of cone contrast at the threshold)
function S = sensitivity_stolms( obj, s_freq, t_freq, orientation, LMS_bkg, LMS_delta, area, eccentricity )
csf_pars = struct( 's_frequency', s_freq, 't_frequency', t_freq, 'orientation', orientation, 'lms_bkg', LMS_bkg, 'lms_delta', LMS_delta, 'area', area, 'eccentricity', eccentricity );
S = obj.sensitivity( csf_pars );
end
function S = sensitivity_stolms_jov( obj, s_freq, t_freq, orientation, LMS_bkg, LMS_delta, area, eccentricity, col_dir )
csf_pars = struct( 's_frequency', s_freq, 't_frequency', t_freq, 'orientation', orientation, 'lms_bkg', LMS_bkg, 'lms_delta', LMS_delta, 'area', area, 'eccentricity', eccentricity );
if strcmp(class(obj.csf_model), 'CSF_Wuerger2020')
S = obj.sensitivity( csf_pars, col_dir );
else
S = obj.sensitivity( csf_pars );
end
end
function S = sensitivity_stolmsv( obj, s_freq, t_freq, orientation, LMS_bkg, LMS_delta, area, eccentricity, vis_field )
csf_pars = struct( 's_frequency', s_freq, 't_frequency', t_freq, 'orientation', orientation, 'lms_bkg', LMS_bkg, 'lms_delta', LMS_delta, 'area', area, 'eccentricity', eccentricity, 'vis_field', vis_field );
S = obj.sensitivity( csf_pars );
end
function S = sensitivity_stolmsv_jov( obj, s_freq, t_freq, orientation, LMS_bkg, LMS_delta, area, eccentricity, vis_field, col_dir )
csf_pars = struct( 's_frequency', s_freq, 't_frequency', t_freq, 'orientation', orientation, 'lms_bkg', LMS_bkg, 'lms_delta', LMS_delta, 'area', area, 'eccentricity', eccentricity, 'vis_field', vis_field );
if strcmp(class(obj.csf_model), 'CSF_Wuerger2020')
S = obj.sensitivity( csf_pars, col_dir );
else
S = obj.sensitivity( csf_pars );
end
end
function S = sensitivity_stolms_edge( obj, t_freq, orientation, LMS_bkg, LMS_delta, ge_sigma, eccentricity )
csf_pars = struct( 't_frequency', t_freq, 'orientation', orientation, 'lms_bkg', LMS_bkg, 'lms_delta', LMS_delta, 'ge_sigma', ge_sigma, 'eccentricity', eccentricity);
S = obj.sensitivity_edge( csf_pars );
end
function S = sensitivity_stolms_edge_jov( obj, t_freq, orientation, LMS_bkg, LMS_delta, ge_sigma, eccentricity, col_dir )
csf_pars = struct( 't_frequency', t_freq, 'orientation', orientation, 'lms_bkg', LMS_bkg, 'lms_delta', LMS_delta, 'ge_sigma', ge_sigma, 'eccentricity', eccentricity);
if strcmp(class(obj.csf_model), 'CSF_Wuerger2020')
S = obj.csf_model.sensitivity_edge( csf_pars, col_dir );
else
S = obj.sensitivity_edge( csf_pars );
end
end
% Test whether all the parameters are correct size, that the
% names are correct, set the default values for the missing
% parameters.
% pars - the csf_params structure
% requires - a cell array with the selected parameters that are
% required. Used for the multually exclusive
% parameters, such as 'luminance'/'lms_bkg' and
% 'area'/'ge_sigma' so that one of them is computed as
% needed, but not both.
% expand - if true, all the parameters are expanded to have the
% same size.
function pars = test_complete_params(obj, pars, requires, expand )
if ~exist( 'expand', 'var' )
expand = false;
end
valid_names = { 'luminance', 'lms_bkg', 'lms_delta', 's_frequency', 't_frequency', 'orientation', 'area', 'width', 'height', 'ge_sigma', 'eccentricity', 'vis_field' };
fn = fieldnames( pars );
% N = 1; % The size of the vector
cur_par = 1;
for kk=1:length(fn)
if ~ismember( fn{kk}, valid_names )
error( 'Parameter structure contains unrecognized field ''%s''', fn{kk} );
end
% Check whether the parameters can be broadcasted
try
param = pars.(fn{kk});
if ismember( fn{kk}, { 'lms_bkg', 'lms_delta' } )
p_sz = size(param);
if p_sz(end) ~= 3
error( 'The last dimension of ''%s'' must have size 3', fn{kk} );
end
%param = reshape( param, [p_sz(1:(end-1)) 1 3] );
end
cur_par = cur_par .* param;
catch
error( 'Parameter %s cannot be broadcasted', fn{kk});
end
% if numel(pars.(fn{kk})) > 1
% Nc = numel(pars.(fn{kk}))/par_len;
% if N==1
% N = Nc;
% else
% if Nc~=1 && N ~= Nc
% error( 'Inconsistent size of the parameter ''%s''', fn{kk} );
% end
% end
% end
end
if ismember( 'luminance', requires )
if ~isfield( pars, 'luminance')
if ~isfield( pars, 'lms_bkg')
error( 'You need to pass either luminance or lms_bkg parameter.')
end
pars.luminance = CSF_base.last_dim(pars.lms_bkg,1) + CSF_base.last_dim(pars.lms_bkg,2);
end
end
if ismember( 'lms_bkg', requires )
if ~isfield( pars, 'lms_bkg')
if ~isfield( pars, 'luminance')
error( 'You need to pass either luminance or lms_bkg parameter.')
end
%error( 'Not implemented' )
pars.lms_bkg = [0.6991 0.3009 0.0198] .* pars.luminance;
end
end
if ismember( 'ge_sigma', requires )
if ~isfield( pars, 'ge_sigma')
if ~isfield( pars, 'area')
error( 'You need to pass either ge_sigma or area parameter.')
end
pars.ge_sigma = sqrt(pars.area/pi);
end
end
if ismember( 'area', requires )
if ~isfield( pars, 'area')
if ~isfield( pars, 'ge_sigma')
error( 'You need to pass either ge_sigma or area parameter.')
end
pars.area = pi*pars.ge_sigma.^2;
end
end
% Default parameter values
def_pars = struct( 'eccentricity', 0, 'vis_field', 180, 'orientation', 0, 't_frequency', 0, 'lms_delta', [0.6855 0.2951 0.0194] );
fn_dp = fieldnames( def_pars );
for kk=1:length(fn_dp)
if ~isfield(pars, fn_dp{kk})
pars.(fn_dp{kk}) = def_pars.(fn_dp{kk});
end
end
% if expand && N>1
% % Make all parameters the same height
% fn = fieldnames( pars );
% for kk=1:length(fn)
% if size(pars.(fn{kk}),1)==1
% pars.(fn{kk}) = repmat( pars.(fn{kk}), [N 1]);
% end
% end
% end
end
function print( obj, fh )
% Print the model parameters in a format ready to be pasted into
% get_default_par()
obj.print_struct( fh, 'p.', obj.par );
end
function print_struct( obj, fh, struct_name, s )
% Print the model parameters in a format ready to be pasted into
% get_default_par()
fn = fieldnames( s );
for ff=1:length(fn)
if ismember( fn{ff}, { 'cm', 'ds', 'sust', 'trans' } )
continue;
end
if isstruct(s.(fn{ff}))
obj.print_struct( fh, strcat( struct_name, fn{ff}, '.' ), s.(fn{ff}) );
else
fprintf( fh, '\t%s%s = ', struct_name, fn{ff} );
obj.print_vector( fh, s.(fn{ff}) );
fprintf( fh, ';\n' );
end
end
end
end
methods(Static)
function to = update_struct( from, to )
fn = fieldnames(from);
for ff = 1:length(fn)
if isfield(to, fn{ff})
if numel(to.(fn{ff}))==1 && isstruct(to.(fn{ff}))
to.(fn{ff}) = CSF_base.update_struct( from.(fn{ff}), to.(fn{ff}) );
else
assert( all( size(to.(fn{ff})) == size(from.(fn{ff})) ) );
to.(fn{ff}) = from.(fn{ff});
end
end
end
end
function Y = sel_dim(X,d)
cln(1:ndims(X)) = {1};
if d>1
cln(d) = {':'};
else
cln(end-d+1) = {':'};
end
Y = X(cln{:});
end
function Y = last_dim(X,d)
cln(1:ndims(X)) = { ':' };
cln{end} = d;
Y = X(cln{:});
end
function [s, pos] = param2struct( s, pars_vector )
% Convert a vector with the parameters to a structure with the
% same fields as in the structure "s".
pos = 1;
for cc=1:length(s)
ff = fieldnames(s(cc));
for kk=1:length(ff)
if isstruct(s(cc).(ff{kk}))
[s(cc).(ff{kk}), pos_ret] = CSF_base.param2struct(s(cc).(ff{kk}), pars_vector(pos:end) );
pos = pos+pos_ret-1;
else
N = length(s(cc).(ff{kk}));
s(cc).(ff{kk}) = pars_vector(pos:(pos+N-1));
pos = pos + N;
end
end
end
% if (pos-1) ~= length(pars_vector)
% error( 'The parameter vector contains %d elements while the model has %d optimized parameters. Perhaps the optimized for a different set of datasets?', length(pars_vector), (pos-1) );
% end
end
function pars_vector = struct2param( s )
% Convert a structure to a vector with the parameters
pars_vector = [];
for cc=1:length(s)
ff = fieldnames(s(cc));
for kk=1:length(ff)
if isstruct( s(cc).(ff{kk}) )
pars_vector = cat( 2, pars_vector, CSF_base.struct2param(s(cc).(ff{kk})) );
else
pars_vector = cat( 2, pars_vector, s(cc).(ff{kk}) );
end
end
end
end
function v = get_lum_dep( pars, L )
% A family of functions modeling luminance dependency
%log_lum = log10(L);
switch length(pars)
case 1
% Constant
v = ones(size(L)) * pars(1);
case 2
% Linear in log
v = pars(2)*L.^pars(1);
%v = 10.^(pars(1)*log_lum + log10(pars(2)));
case 3
% Log parabola
% v = pars(1) * 10.^(exp( -(log_lum-pars(2)).^2/pars(3) ));
% A single hyperbolic function
v = pars(1)*(1+pars(2)./L).^(-pars(3));
case 5
% Two hyperbolic functions
v = pars(1)*(1+pars(2)./L).^(-pars(3)) .* (1-(1+pars(4)./L).^(-pars(5)));
otherwise
error( 'not implemented' );
end
end
function v = get_lum_dep_dec( pars, L )
% A family of functions modeling luminance dependency.
% The same as abobe but the functions are decreasing with
% luminance
log_lum = log10(L);
switch length(pars)
case 1
% Constant
v = ones(size(L)) * pars(1);
case 2
% Linear in log
v = 10.^(-pars(1)*log_lum + pars(2));
case 3
% A single hyperbolic function
v = pars(1)* (1-(1+pars(2)./L).^(-pars(3)));
case 5
% Two hyperbolic functions
error( 'TODO' );
v = pars(1)*(1+pars(2)./L).^(-pars(3)) .* (1-(1+pars(4)./L).^(-pars(5)));
otherwise
error( 'not implemented' );
end
end
function p = get_dataset_par()
p = struct();
end
function print_vector( fh, vec )
if length(vec)>1
fprintf( fh, '[ ' );
fprintf( fh, '%g ', vec );
fprintf( fh, ']' );
else
fprintf( fh, '%g', vec );
end
end
end
end