Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model plugin #56

Open
wants to merge 46 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
8ef03a7
Created model template and modified pRFFit to make calls to pRFModelT…
Aug 24, 2016
b6d9bc5
Plugging in exponent model
Sep 3, 2016
b1a1d6b
Completed new file structure for models
akjags Sep 8, 2016
2f176ba
temp commit
akjags Sep 26, 2016
4c7ec7e
Adding files for cross-validation pipeline.
akjags Oct 8, 2016
3cbc3b6
Adding documentation for cross-val pipeline
akjags Oct 8, 2016
98712ff
Modifying trainPRF and testPRF
akjags Oct 27, 2016
fb1d199
Adding new on-the-fly cross validation, and grabbed applyConcatFilter…
akjags Oct 27, 2016
0b12c19
Adding difference of gaussians (DoG) model
akjags Nov 19, 2016
82d7419
Adding scripts to get best N voxels, and to plot model prediction acc…
akjags Nov 19, 2016
1f97d4b
minor changes to dispModel, which compares 2 voxels
akjags Nov 19, 2016
45edd8e
Cleaning up old files, reorganizing subprocedures into crossVal, rena…
akjags Nov 22, 2016
45603a6
rename pRF_crossValidate to pRFCrossVal
akjags Nov 22, 2016
8774530
rename pRF_crossValidate to pRFCrossVal
akjags Nov 22, 2016
e7f4d6d
Adding new models (ratio of gaussians) and bounded fminsearch methods
akjags Dec 12, 2016
0c4058f
Adding pRFSimulate script
akjags Jan 25, 2017
ec6a1f2
Adding analysis script and updating simulator
akjags Feb 4, 2017
7317239
Making changes to get pRF to run with double bars
akjags Feb 23, 2017
57d649d
temp commit
Mar 5, 2017
d3448f3
Merge branch 'master' of https://github.com/akjags/mrTools into model…
Mar 5, 2017
151ed67
Reorganizing git directories. Moving models into models folder and mo…
akjags Mar 10, 2017
5aaa19a
Updated to reverse old changes and make compatible with mglDoubleBars
akjags Mar 10, 2017
d29c658
Bug fixes for models code
akjags Jun 20, 2017
b25a090
Created model template and modified pRFFit to make calls to pRFModelT…
Aug 24, 2016
b38e84f
Plugging in exponent model
Sep 3, 2016
4f0f908
Completed new file structure for models
akjags Sep 8, 2016
4ce8d99
temp commit
akjags Sep 26, 2016
de5e2c3
Adding files for cross-validation pipeline.
akjags Oct 8, 2016
20caf9a
Adding documentation for cross-val pipeline
akjags Oct 8, 2016
d34d738
Modifying trainPRF and testPRF
akjags Oct 27, 2016
de1b57e
Adding new on-the-fly cross validation, and grabbed applyConcatFilter…
akjags Oct 27, 2016
852a820
Adding difference of gaussians (DoG) model
akjags Nov 19, 2016
ae9374c
Adding scripts to get best N voxels, and to plot model prediction acc…
akjags Nov 19, 2016
16d7085
minor changes to dispModel, which compares 2 voxels
akjags Nov 19, 2016
9cdba00
Cleaning up old files, reorganizing subprocedures into crossVal, rena…
akjags Nov 22, 2016
6375d4b
rename pRF_crossValidate to pRFCrossVal
akjags Nov 22, 2016
262e60c
rename pRF_crossValidate to pRFCrossVal
akjags Nov 22, 2016
36534b5
Adding new models (ratio of gaussians) and bounded fminsearch methods
akjags Dec 12, 2016
e82ba66
Adding pRFSimulate script
akjags Jan 25, 2017
2e1bd37
Adding analysis script and updating simulator
akjags Feb 4, 2017
83f68d9
Making changes to get pRF to run with double bars
akjags Feb 23, 2017
cf82038
temp commit
Mar 5, 2017
cd91469
Reorganizing git directories. Moving models into models folder and mo…
akjags Mar 10, 2017
5df0fa2
Updated to reverse old changes and make compatible with mglDoubleBars
akjags Mar 10, 2017
960ed60
Bug fixes for models code
akjags Jun 20, 2017
a294fa8
Merge branch 'model_plugin' of https://github.com/akjags/mrTools into…
akjags Jun 20, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
307 changes: 307 additions & 0 deletions mrLoadRet/Plugin/pRF/fminsearch/fminsearchbnd.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,307 @@
function [x,fval,exitflag,output] = fminsearchbnd(fun,x0,LB,UB,options,varargin)
% FMINSEARCHBND: FMINSEARCH, but with bound constraints by transformation
% usage: x=FMINSEARCHBND(fun,x0)
% usage: x=FMINSEARCHBND(fun,x0,LB)
% usage: x=FMINSEARCHBND(fun,x0,LB,UB)
% usage: x=FMINSEARCHBND(fun,x0,LB,UB,options)
% usage: x=FMINSEARCHBND(fun,x0,LB,UB,options,p1,p2,...)
% usage: [x,fval,exitflag,output]=FMINSEARCHBND(fun,x0,...)
%
% arguments:
% fun, x0, options - see the help for FMINSEARCH
%
% LB - lower bound vector or array, must be the same size as x0
%
% If no lower bounds exist for one of the variables, then
% supply -inf for that variable.
%
% If no lower bounds at all, then LB may be left empty.
%
% Variables may be fixed in value by setting the corresponding
% lower and upper bounds to exactly the same value.
%
% UB - upper bound vector or array, must be the same size as x0
%
% If no upper bounds exist for one of the variables, then
% supply +inf for that variable.
%
% If no upper bounds at all, then UB may be left empty.
%
% Variables may be fixed in value by setting the corresponding
% lower and upper bounds to exactly the same value.
%
% Notes:
%
% If options is supplied, then TolX will apply to the transformed
% variables. All other FMINSEARCH parameters should be unaffected.
%
% Variables which are constrained by both a lower and an upper
% bound will use a sin transformation. Those constrained by
% only a lower or an upper bound will use a quadratic
% transformation, and unconstrained variables will be left alone.
%
% Variables may be fixed by setting their respective bounds equal.
% In this case, the problem will be reduced in size for FMINSEARCH.
%
% The bounds are inclusive inequalities, which admit the
% boundary values themselves, but will not permit ANY function
% evaluations outside the bounds. These constraints are strictly
% followed.
%
% If your problem has an EXCLUSIVE (strict) constraint which will
% not admit evaluation at the bound itself, then you must provide
% a slightly offset bound. An example of this is a function which
% contains the log of one of its parameters. If you constrain the
% variable to have a lower bound of zero, then FMINSEARCHBND may
% try to evaluate the function exactly at zero.
%
%
% Example usage:
% rosen = @(x) (1-x(1)).^2 + 105*(x(2)-x(1).^2).^2;
%
% fminsearch(rosen,[3 3]) % unconstrained
% ans =
% 1.0000 1.0000
%
% fminsearchbnd(rosen,[3 3],[2 2],[]) % constrained
% ans =
% 2.0000 4.0000
%
% See test_main.m for other examples of use.
%
%
% See also: fminsearch, fminspleas
%
%
% Author: John D'Errico
% E-mail: [email protected]
% Release: 4
% Release date: 7/23/06

% size checks
xsize = size(x0);
x0 = x0(:);
n=length(x0);

if (nargin<3) || isempty(LB)
LB = repmat(-inf,n,1);
else
LB = LB(:);
end
if (nargin<4) || isempty(UB)
UB = repmat(inf,n,1);
else
UB = UB(:);
end

if (n~=length(LB)) || (n~=length(UB))
error 'x0 is incompatible in size with either LB or UB.'
end

% set default options if necessary
if (nargin<5) || isempty(options)
options = optimset('fminsearch');
end

% stuff into a struct to pass around
params.args = varargin;
params.LB = LB;
params.UB = UB;
params.fun = fun;
params.n = n;
% note that the number of parameters may actually vary if
% a user has chosen to fix one or more parameters
params.xsize = xsize;
params.OutputFcn = [];

% 0 --> unconstrained variable
% 1 --> lower bound only
% 2 --> upper bound only
% 3 --> dual finite bounds
% 4 --> fixed variable
params.BoundClass = zeros(n,1);
for i=1:n
k = isfinite(LB(i)) + 2*isfinite(UB(i));
params.BoundClass(i) = k;
if (k==3) && (LB(i)==UB(i))
params.BoundClass(i) = 4;
end
end

% transform starting values into their unconstrained
% surrogates. Check for infeasible starting guesses.
x0u = x0;
k=1;
for i = 1:n
switch params.BoundClass(i)
case 1
% lower bound only
if x0(i)<=LB(i)
% infeasible starting value. Use bound.
x0u(k) = 0;
else
x0u(k) = sqrt(x0(i) - LB(i));
end

% increment k
k=k+1;
case 2
% upper bound only
if x0(i)>=UB(i)
% infeasible starting value. use bound.
x0u(k) = 0;
else
x0u(k) = sqrt(UB(i) - x0(i));
end

% increment k
k=k+1;
case 3
% lower and upper bounds
if x0(i)<=LB(i)
% infeasible starting value
x0u(k) = -pi/2;
elseif x0(i)>=UB(i)
% infeasible starting value
x0u(k) = pi/2;
else
x0u(k) = 2*(x0(i) - LB(i))/(UB(i)-LB(i)) - 1;
% shift by 2*pi to avoid problems at zero in fminsearch
% otherwise, the initial simplex is vanishingly small
x0u(k) = 2*pi+asin(max(-1,min(1,x0u(k))));
end

% increment k
k=k+1;
case 0
% unconstrained variable. x0u(i) is set.
x0u(k) = x0(i);

% increment k
k=k+1;
case 4
% fixed variable. drop it before fminsearch sees it.
% k is not incremented for this variable.
end

end
% if any of the unknowns were fixed, then we need to shorten
% x0u now.
if k<=n
x0u(k:n) = [];
end

% were all the variables fixed?
if isempty(x0u)
% All variables were fixed. quit immediately, setting the
% appropriate parameters, then return.

% undo the variable transformations into the original space
x = xtransform(x0u,params);

% final reshape
x = reshape(x,xsize);

% stuff fval with the final value
fval = feval(params.fun,x,params.args{:});

% fminsearchbnd was not called
exitflag = 0;

output.iterations = 0;
output.funcCount = 1;
output.algorithm = 'fminsearch';
output.message = 'All variables were held fixed by the applied bounds';

% return with no call at all to fminsearch
return
end

% Check for an outputfcn. If there is any, then substitute my
% own wrapper function.
if ~isempty(options.OutputFcn)
params.OutputFcn = options.OutputFcn;
options.OutputFcn = @outfun_wrapper;
end

% now we can call fminsearch, but with our own
% intra-objective function.
[xu,fval,exitflag,output] = fminsearch(@intrafun,x0u,options,params);

% undo the variable transformations into the original space
x = xtransform(xu,params);

% final reshape to make sure the result has the proper shape
x = reshape(x,xsize);

% Use a nested function as the OutputFcn wrapper
function stop = outfun_wrapper(x,varargin);
% we need to transform x first
xtrans = xtransform(x,params);

% then call the user supplied OutputFcn
stop = params.OutputFcn(xtrans,varargin{1:(end-1)});

end

end % mainline end

% ======================================
% ========= begin subfunctions =========
% ======================================
function fval = intrafun(x,params)
% transform variables, then call original function

% transform
xtrans = xtransform(x,params);

% and call fun
fval = feval(params.fun,reshape(xtrans,params.xsize),params.args{:});

end % sub function intrafun end

% ======================================
function xtrans = xtransform(x,params)
% converts unconstrained variables into their original domains

xtrans = zeros(params.xsize);
% k allows some variables to be fixed, thus dropped from the
% optimization.
k=1;
for i = 1:params.n
switch params.BoundClass(i)
case 1
% lower bound only
xtrans(i) = params.LB(i) + x(k).^2;

k=k+1;
case 2
% upper bound only
xtrans(i) = params.UB(i) - x(k).^2;

k=k+1;
case 3
% lower and upper bounds
xtrans(i) = (sin(x(k))+1)/2;
xtrans(i) = xtrans(i)*(params.UB(i) - params.LB(i)) + params.LB(i);
% just in case of any floating point problems
xtrans(i) = max(params.LB(i),min(params.UB(i),xtrans(i)));

k=k+1;
case 4
% fixed variable, bounds are equal, set it at either bound
xtrans(i) = params.LB(i);
case 0
% unconstrained variable.
xtrans(i) = x(k);

k=k+1;
end
end

end % sub function xtransform end





Loading