www.gusucode.com > mbctools 工具箱 matlab 源码程序 > mbctools/+tpsetup/DatasetSelector.m

    classdef DatasetSelector < handle
    %DatasetSelector select dataset for modelling
    
    %  Copyright 2014-2016 The MathWorks, Inc. and Ford Global Technologies, Inc.

    properties (SetAccess=private)
        %Layout main layout
        Layout
        %DataPopup popup to select data for modelling
        DataPopup
        %ValidationPopup popup for validation data selection
        ValidationPopup
        %ValidationPercentage spinner to select percentage of fit data to use for validation
        ValidationPercentage
        %DataMessage message text for data
        DataMessage
    end
    
    properties (Access=private)
        %Parent parent of component
        Parent
        %Listeners store for listeners
        Listeners
        %Model data model objec
        Model
    end

    
    methods
        function obj = DatasetSelector(Parent,Model)
        %DatasetSelector - constructor
        obj.Parent = Parent;
        obj.Model = Model;

        
        obj.Layout = mbcgui.container.layoutpanel('Parent',Parent,...
            'Title','Data',...
            'BorderType', 'etchedin', ...
            'LayoutBorder', [10 10 10 3]);
        
        obj.DataPopup = uicontrol('Parent',obj.Layout,...
            'Style','popupmenu',...
            'BackgroundColor','w',...
            'Tag', 'DataList',...
            'Callback',@obj.selectData);
        
        hData = xregGui.labelcontrol('Parent', obj.Layout, ...
            'String', 'Data set:', ...
            'LabelSizeMode', 'absolute', ...
            'ControlSizeMode', 'absolute', ...
            'LabelSize', 100, ...
            'ControlSize', 250, ...
            'Control', obj.DataPopup);
        
        hFile = xregGui.iconuicontrol('Parent', obj.Layout,...
            'TooltipString', 'Import from file...',...
            'ImageFile', xregrespath('import_ts_16.bmp'),...
            'TransparentColor', [0 255 0],...
            'Tag', 'importFile',...
            'callback', {@obj.importData,'file'});

        hWksp = xregGui.iconuicontrol('Parent', obj.Layout,...
            'TooltipString', 'Import from Workspace...',...
            'TransparentColor', [0 255 0],...
            'ImageFile', xregrespath('variable_import.bmp'),...
            'callback', {@obj.importData,'workspace'});

        obj.ValidationPopup = uicontrol('Parent',obj.Layout,...
            'Style','popupmenu',...
            'BackgroundColor','w',...
            'String',{'<none>';'<random>';'<last>'},...
            'Tag', 'ValidationList',...
            'Callback',@obj.selectValData);
        
        hValData = xregGui.labelcontrol('Parent', obj.Layout, ...
            'String', 'Validation data set:', ...
            'LabelSizeMode', 'absolute', ...
            'ControlSizeMode', 'absolute', ...
            'LabelSize', 100, ...
            'ControlSize', 250, ...
            'Control', obj.ValidationPopup);
        
        obj.ValidationPercentage = mbcgui.widget.Spinner('Parent',obj.Layout,...
            'Min',0,...
            'Max',100,...
            'Value',20,...
            'Enable','off',...
            'Callback',@obj.selectValPercentage);
        hValPercent = xregGui.labelcontrol('Parent', obj.Layout, ...
            'String', 'Percentage of data to use for validation:', ...
            'LabelSizeMode', 'absolute', ...
            'ControlSizeMode', 'absolute', ...
            'LabelSize', 270, ...
            'ControlSize', 80, ...
            'Control', obj.ValidationPercentage);
        
        obj.DataMessage = uicontrol('Parent',obj.Layout,...
            'Style','text',...
            'HorizontalAlignment','left',....
            'Tag', 'DataMessage');
        rowsizes=[25*ones(1,3) 30 -1];

        Lyt = xreggridbaglayout(obj.Layout,...
            'dimension',[5, 4],...
            'elements',{hData,hValData,hValPercent,obj.DataMessage,[],...
            hFile,[],[],[],[],...
            hWksp,[],[],[],[],...
            [],[],[],[],[]},...
            'rowsizes',rowsizes,...
            'colsizes',[360 25 25,-1],...
            'gapy',2,...
            'mergeblock',{[3,3],[1 3]},...% for validation controls
            'mergeblock',{[4,4],[1 4]},...% for data message controls
            'gapx',5);
        
        set(obj.Layout,'LayoutComponent',{Lyt})
        
        obj.Listeners = [event.proplistener(obj.Model,findprop(obj.Model,'ValidationType'),'PostSet',@obj.onValidationTypeChange)
            event.proplistener(obj.Model,findprop(obj.Model,'Selected'),'PostSet',@obj.onSelectedTestplan)];
        
        end
        
        function update(obj)
        %update the component
        
        if ~hasData(obj.Model)
            set(obj.DataPopup,...
                'String','<No data available>',...
                'Value',1,...
                'Enable','off')
            set(obj.ValidationPopup,...
                'String','<No data available>',...
                'Value',1,...
                'Enable','off')
            set(obj.ValidationPercentage,'Enable','off');
            
        else
            set(obj.DataPopup,...
                'Enable','on',...
                'String',pveceval(obj.Model.pDataList,@(d) get(d,'label')),...
                'Value',obj.Model.pDataIndex);
            pValList = obj.Model.pValidationSets;
            ValList = [{'<none>';'<random>';'<last>'}; pveceval(pValList(:),@(d) get(d,'label'))];
            set(obj.ValidationPercentage,'Enable','off');
            switch obj.Model.ValidationType
                case 'none'
                    pos = 1;
                case 'random'
                    pos = 2;
                case 'last'
                    pos = 3;
                otherwise
                    pos = find(pValList==obj.Model.ValidationData)+3;
                    if isempty(pos)
                       pos = 1; 
                    end
            end
                    
            if get(obj.ValidationPopup,'Value')>3
                set(obj.ValidationPopup,'Value',1);
                
            elseif get(obj.ValidationPopup,'Value')>1
                set(obj.ValidationPercentage,'Enable','on');
            end
            set(obj.ValidationPopup,...
                'Enable','on',...
                'String',ValList,'Value',pos);
            
            updateMessage(obj)
            
        end
        
        end
        
        function updateMessage(obj)
        
        ssf = info(obj.Model.pData);
        T = obj.Model.Selected;
        if numstages(T)==1 && ~isOneStage(ssf)
            msg = 'Two-stage data selected for a one-stage test plan. Data will be converted to one-stage and test filters will be removed.';
        elseif numstages(T)>1 && isOneStage(ssf)
            msg = 'One-stage data selected for a two-stage test plan. You will need to define test groups.';
        else
            msg = '';
        end
        obj.DataMessage.String = msg;

        end
        
        function selectValData(obj,h,~)
        %selectValData select validation data
        index = get(h,'Value');
        ValList = get(h,'String');
        if index==1
            % no validation data
            obj.Model.ValidationType = 'none';
            obj.Model.ValidationData = xregpointer;
        elseif index<=3
            % random or last => need percentage
            t = regexp(ValList{index},'^<(.*?)>$','tokens','once');
            obj.Model.ValidationData = xregpointer;
            obj.Model.ValidationType = t{1};
        else
            % select dataset
            obj.Model.ValidationData = obj.Model.pValidationSets( index-3 );
            obj.Model.ValidationType = 'data';
        end
        
        end
        
        function onValidationTypeChange(obj,~,~)
        
        update(obj)
        end
        
        function selectData(obj,h,~)
        %selectData select fitting data set
        
        pDataList = obj.Model.pDataList;
        index = get(h,'Value');
        
        TestplanIndex = obj.Model.SelectedIndex;
        % change data
        obj.Model.pData = pDataList(index);
        if ~isempty(TestplanIndex)
            selectTestplan(obj.Model,TestplanIndex)
        end
         updateMessage(obj)
        
        end
        
        function importData(obj, ~,~,Source)
        %importData import data from file or workspace
        switch Source
            case 'file'
                % Get the data using the data loading wizard
                [ss,OK] = xregImportDataFile;
            case 'workspace'
                % Find the names of the variables in the base workspace
                varNames = evalin('base','who');
                % Initialise the base structure to ensure a suitable result if there are no
                % variables in the workspace
                base = struct;
                for i = 1:length(varNames)
                    % Get copies of the variables in the base workspace
                    base.(varNames{i}) = evalin('base',varNames{i});
                end
                % Open the import wizard
                [OK, ss] = xregImportDataStructure(base, 'Workspace', ancestor(obj.Parent,'figure'));
            otherwise
                OK = false;
        end
        % Did it all work out OK
        if OK
            % Create a viable sweepsetfilter object
            [~, pSSF] = createDataObject(obj.Model.Project.info);
            iSetSweepsetfilterData(pSSF,ss);
            obj.Model.pData = pSSF;
        end
        
        end

        function selectValPercentage(obj,h,~)
        obj.Model.ValidationPercent = get(h,'Value');
        
        end
        
        function onSelectedTestplan(obj,~,~)
        
        update(obj)
        end
        
    end
    
end

function iSetSweepsetfilterData(pSSF,ss)
% Sort the variables alphabetically
ss = sort(ss);
% Add the filename to the sweepset
% Set the sweepset for the sweepsetfilter (This will trigger an UpdateAll in the
% sweepsetfilter and hence all relevant events will be triggered)
ssf = setSweepset(pSSF.info, ss);
% Do we need to set any default test groupings?
if isempty(get(ssf, 'definetests'))
    % TO DO - Get these names from the user settings
    names = {'LOGNO' 'logno' 'TESTNUM' 'testnum'};
    [~, ind] = find(ss, names);
    % Remove the not found names
    ind = find(ind ~= -1);
    if ~isempty(ind)
        % Create the test definition structure - note use of eps rather
        % than zero since we require the change to be greater than or equal
        % to the tolerance
        testDefinition = struct('variable', names{ind(1)}, 'tolerance', eps, 'reorder', false, 'testnumAlias', names{ind(1)});
    else
        testDefinition = struct('variable', '#rec', 'tolerance', 1, 'reorder', false, 'testnumAlias', '');
    end
    % Add it to the
    ssf = modifyTestDefinition(ssf, testDefinition);
end
pSSF.info = pSSF.assign(ssf);

end