www.gusucode.com > mbcmodels 工具箱 matlab 源码程序 > mbcmodels/Validate_LocalSelect.m

    function varargout=Validate_LocalSelect(action,varargin)
%VALIDATE_LOCALSELECT selects the best model for localmulti
%
%  Validate_LocalSelect('create', pmdev, fig) 

%  Copyright 2006-2015 The MathWorks, Inc. and Ford Global Technologies, Inc.

% ---------------------------------------------------------------------------------------
% 												Beginning of the switchyard
% ---------------------------------------------------------------------------------------
switch lower(action)
    case 'create'
        % setup the GUI
        varargout{1}=i_create(varargin{:});
    case 'select'
        % select the model(s)
        varargout{1}= i_Select(varargin{:});
    case 'close'
        % close function
        i_CloseFcn(varargin{:});
    case 'closefcn'
        i_CloseFcn;
    case 'update'
        varargout{1}=iUpdate(varargin{:});
end

% i_create
function fig=i_create(p,ParFig,ms)

View={};

% Put all relevant info into the userdata for the draw routine...

[Models,ColHead,s,SortKey,L,MultiOK,Tno]= i_Models(ParFig,p);
if isempty(Models)
    fig=[];
    return
end
ModelList= Models;
for i=1:length(Models)
    ModelList{i}= name(get(Models{i},'currentmodel'));
end

[fig, viewparent] = mv_ValidationTool('create',mfilename,p,Models,ParFig,4);
if isempty(fig)
   return
end
mv_ValidationTool('setmessage','Creating validation views for model...');

mv_ValidationTool('StatsTable',fig,ModelList,ColHead,s,SortKey);
mv_ValidationTool('setmessage','Creating validation views for model...');
mv_ValidationTool('setwaitbar','value',.03);

ptr= get(fig,'Pointer');
set(fig,'Pointer','watch');

% now start creating and adding views to the validationTool

% do some Obs/Pred plot
[View{1}, Lyt] =validate_prediction('create',viewparent,'VAL');
mv_ValidationTool('attachlayout', fig,Lyt, 1);
% update waitbar
mv_ValidationTool('setwaitbar','value',.06);

[View{2}, Lyt] = validate_residuals('create',viewparent,'VAL');
mv_ValidationTool('attachlayout', fig,Lyt, 2);
% update waitbar
mv_ValidationTool('setwaitbar','value',.13); 

[View{3}, Lyt] =mv_DialUp('create',viewparent,Models{1});
mv_ValidationTool('attachlayout', fig,Lyt, 3);
% update waitbar
mv_ValidationTool('setwaitbar','value',.53); 

[View{4}, Lyt] =validate_rstool('create',viewparent,Models{1},'VAL');
mv_ValidationTool('attachlayout', fig,Lyt, 4);
% update waitbar
mv_ValidationTool('setwaitbar','value',.81); 

mv_ValidationTool('add',fig,View)
% update waitbar
mv_ValidationTool('setwaitbar','value',1);



ud= get(fig,'UserData');
ud.Local= L;
ud.MultiOK= find(MultiOK);
ud.TestNo= Tno;
ud.MessageService = ms;
set(fig,'UserData',ud);

Xs= getdata(p.info,'VAL');

set(fig,'Name',sprintf('Multiple Models Selection for Test %1d',testnum(Xs)));
% select best model
BestInd= find(get(L,'currentindex')==ud.MultiOK);
ud.ListView.selectRows(BestInd); %#ok<FNDSB>

mv_ValidationTool('selectbest');

mv_ValidationTool('setcreatemessage',0);
mv_ValidationTool('view',fig,1)
% update waitbar
mv_ValidationTool('setwaitbar','value',0); 


set(fig,'CloseRequestFcn',[mfilename,'(''closefcn'')']);

set(fig,'Pointer',ptr);


% iUpdate
function fig = iUpdate(p,ParFig)

fig= mvf('ValidationTool');
if isempty(fig)
    % need to create a new val tool
    fig = i_create(p,ParFig);
else
    % if test number has changed then ask whether to select best model
    ud= get(fig,'UserData');
    View= GetViewData(MBrowser);
    if ud.TestNo ~=  View.MessageService.CurrentTest
        % Select best from last test but don't give option to cancel
        iSelectBest(fig,p,{'Yes','No'});
    end
    
    % refit alternative models
    [Models,ColHead,s,SortKey,L,MultiOK,Tno]= i_Models(ParFig,p);
    if isempty(Models)
        % delete figure
        delete(fig)
    else
        % update models in selection tool
        mv_ValidationTool('updatemodels',Models);

        ModelList= Models;
        for i=1:length(Models)
            ModelList{i}= name(get(Models{i},'currentmodel'));
        end
        % update stats tables
        mv_ValidationTool('StatsTable',fig,ModelList,ColHead,s,SortKey);

        % extra data required for local multi
        ud= get(fig,'UserData');
        ud.Local= L;
        ud.MultiOK= find(MultiOK);
        ud.TestNo= Tno;
        set(fig,'UserData',ud);

        Xs= getdata(p.info,'VAL');

        set(fig,'Name',sprintf('Multiple Models Selection for Test %1d',testnum(Xs)));
        % select best model
        BestInd= find(get(L,'currentindex')==ud.MultiOK);
        mv_ValidationTool('select',fig,BestInd)
        mv_ValidationTool('selectbest');

        % update ranges for cross-section and surface viewer
        mv_ValidationTool('updateranges',fig);
        
        % update current view
        mv_ValidationTool('updateview',fig)
        figure(fig)
    end
end

% i_Models
function [Models,ColHead,S,SortKey,L,OKmulti,Tno]= i_Models(hFig,p)

figHandle = ancestor(hFig,'figure');

ptr= get(figHandle,'Pointer');
set(figHandle,'Pointer','watch');
mbH= MBrowser;
ValID= mbH.addStatusMsg('Calculating Validation Statistics');

drawnow
Tno= mbH.CurrentTest;
mdev = info(p);
[L,OK]= LocalModel(mdev,Tno);
if OK
    
    [Xs,Ys,DataOK]= FitData(mdev,Tno);
    Xs= Xs(DataOK,:);
    Ys= Ys(DataOK,1);
    % save to validation data
    mdev = valdata(mdev,Xs,Ys);
    
    if get(L,'allfitted')
        [S,ColHead] = AlternativeStatistics(L,Xs,Ys);
        OKmulti = isfinite(get(L,'selectionstatistic'));
    else
        % refit nonbest models
        [L,OKmulti,ColHead,SumStats]= listmodels(L,Xs,Ys);
        S= cat(1,SumStats{:});
        % get list of models (as local models)
        % update parameters from fits as this will update the local selection
        % statistics
        UpdateParams(mdev , Tno, allparameters(L), []);
        
    end
    Models= get(L,'localmultilist');
    Models= Models(OKmulti);
    S= S(OKmulti,:);
    SortKey=find( strcmp( get( L, 'Select' ),ColHead ) );

   [Xval,Yval]=getLocalValidationData(mdev,Tno);
   if ~isempty(Yval)
       ValRMSE = zeros(length(Models),1);
       for i=1:length(Models)
           ValRMSE(i) = LocalValidationRMSE(mdev,Tno,Models{i},Xval,Yval);
       end
       ColHead = [ColHead {'Validation RMSE'}];
       S = [S ValRMSE];
   end
   

else
    Models={};
    ColHead=[];
    S=[];
    SortKey=1;
    OKmulti= [];
end

mbH.removeStatusMsg(ValID);
set(figHandle,'Pointer',ptr);


% i_Select
function ModelInfo= i_Select(Models,ModNo,p)

ModelNos= ModNo;
m= Models(ModNo);

ModelInfo= {ModelNos,p,m};

% i_UpdateModel
function i_UpdateModel(hFig,p,BMIndex)

ud= get(hFig,'UserData');

BMIndex= ud.MultiOK(BMIndex);

L= set(ud.Local,'currentindex',BMIndex);
UpdateParams(p.info , ud.TestNo, allparameters(L), []);
update(ud.MessageService);

% i_CloseFcn
function i_CloseFcn(varargin)

hFig= mvf('ValidationTool');
% bring selection tool to the front
figure(hFig(1))
ud= get(hFig(1),'UserData');
p= ud.p;
drawnow

Resp = iSelectBest(hFig,p,{'Yes','No','Cancel'});

mbH= MBrowser;
if p.isBrowserNode 
    % update current model browser view if you are still on the same node
    % remove reference to selection tool so it doesn't get redrawn before
    % closing
    if strcmp(Resp,'Yes')
        mbH.ViewNode;
        % update listview (to show best model as blue icon)
        mbH.listview;
        mbH.doDrawText;
    end
end

if ~strcmp(Resp,'Cancel')
    p.valdata([]);
    p.history('clear');
    if isgraphics(hFig)
        delete(hFig)
    end
    figure(ud.ParFig);
else
    figure(hFig)
end

% iSelectBest
function Resp = iSelectBest(hFig,p,Btns)

ud= get(hFig(1),'UserData');

BestName= ud.ListView.getDataAt(ud.BestModel-1,0);
BMIndex =ud.BestModel;
Params = allparameters(ud.Local);
[~,OK]= LocalModel(p.info,ud.TestNo);
if OK && Params(1)~=ud.MultiOK(BMIndex)
    % only ask if the best model has changed and model is OK
    Xs= getdata(p.info,'VAL');

    Resp= questdlg(char('Do you want to select the model ',...
        ['    ''',BestName,''''],...
        sprintf('as the best model for Test %1d?',testnum(Xs))),...
        'Model Selection',Btns{:},'Yes');
    switch lower(Resp)
        case 'yes'
            % call particular close function (e.g. Validate_TwoStage)
            i_UpdateModel(hFig(1),p,BMIndex);
        case ''
            % Make sure that an empty response is translated into a valid
            % response 
            Resp = Btns{end};
    end
else
    Resp = 'No';
end