Home > vbmeg > functions > gui > project_mgr_dir > project_file_mgr.m

project_file_mgr

PURPOSE ^

This function manages project file.

SYNOPSIS ^

function [varargout] = project_file_mgr(varargin)

DESCRIPTION ^

 This function manages project file.
 Usage :
  key : 'make', 'load', 'add', 'get_all_parameters', 'get_parameter',
        'get_project_root', 'get_project_name', 'get_project_file'

  project_file_mgr('make', <filename>, <project_name>, <project_root>);
    projec file create. 

  project_file_mgr('load', <filename>); 
    load project file on memory.

  project_file_mgr('add', <parm_name>, <parm>);
    save parameter to file. you should 'load' project file before this operation.
    e.g project_file_mgr('add', 'brain_parm', brain_parm);

  project_file_mgr('get_all_parameters');
    return [parm, parm_name]
                parm : parameter list(Nx1)
           parm_name : parameter name list(Nx1)
    parameter order is sorted by timestamp(registered order.)

  project_file_mgr('get_parameter', <itemNo>)
    return [parm, parm_name]
                parm : parameter
           parm_name : parameter name
    itemNo : registered order.

  project_file_mgr('get_project_root');
    return proj_root

  project_file_mgr('get_project_name');
    return project_name

  project_file_mgr('get_project_file');
    return project file name loaded on memory.

 [NOTE]
  associate with project_mgr :
   project_file_mgr notify to the project_mgr when the status(persistent value)
   changed. And then, project_mgr receives this notify, project_mgr calls 
   project_file_mgr to get updated parameters.


 Copyright (C) 2011, ATR All Rights Reserved.
 License : New BSD License(see VBMEG_LICENSE.txt)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [varargout] = project_file_mgr(varargin)
0002 % This function manages project file.
0003 % Usage :
0004 %  key : 'make', 'load', 'add', 'get_all_parameters', 'get_parameter',
0005 %        'get_project_root', 'get_project_name', 'get_project_file'
0006 %
0007 %  project_file_mgr('make', <filename>, <project_name>, <project_root>);
0008 %    projec file create.
0009 %
0010 %  project_file_mgr('load', <filename>);
0011 %    load project file on memory.
0012 %
0013 %  project_file_mgr('add', <parm_name>, <parm>);
0014 %    save parameter to file. you should 'load' project file before this operation.
0015 %    e.g project_file_mgr('add', 'brain_parm', brain_parm);
0016 %
0017 %  project_file_mgr('get_all_parameters');
0018 %    return [parm, parm_name]
0019 %                parm : parameter list(Nx1)
0020 %           parm_name : parameter name list(Nx1)
0021 %    parameter order is sorted by timestamp(registered order.)
0022 %
0023 %  project_file_mgr('get_parameter', <itemNo>)
0024 %    return [parm, parm_name]
0025 %                parm : parameter
0026 %           parm_name : parameter name
0027 %    itemNo : registered order.
0028 %
0029 %  project_file_mgr('get_project_root');
0030 %    return proj_root
0031 %
0032 %  project_file_mgr('get_project_name');
0033 %    return project_name
0034 %
0035 %  project_file_mgr('get_project_file');
0036 %    return project file name loaded on memory.
0037 %
0038 % [NOTE]
0039 %  associate with project_mgr :
0040 %   project_file_mgr notify to the project_mgr when the status(persistent value)
0041 %   changed. And then, project_mgr receives this notify, project_mgr calls
0042 %   project_file_mgr to get updated parameters.
0043 %
0044 %
0045 % Copyright (C) 2011, ATR All Rights Reserved.
0046 % License : New BSD License(see VBMEG_LICENSE.txt)
0047 mlock; 
0048 persistent project_name;
0049 persistent proj_root;
0050 persistent project_file;
0051 
0052 persistent all_parms;
0053 persistent parm_types;
0054 
0055 key = varargin{1};
0056 
0057 switch(key)
0058 
0059 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0060 %
0061 % File operation function
0062 %
0063 case 'make'
0064     try
0065         project_file = varargin{2};
0066         project_name = varargin{3};
0067         proj_root = varargin{4};
0068         vb_fsave(project_file, 'project_name', 'proj_root');
0069         notify_status_change;
0070         varargout{1} = false;
0071     catch
0072         varargout{1} = true;
0073     end
0074 
0075 case 'load'
0076     err = false;
0077     project_file = varargin{2};
0078     if exist(project_file, 'file')
0079         all_parms = [];
0080         parm_types = [];
0081         load(project_file, 'project_name', 'proj_root');
0082         contains = whos('-file', project_file, '*_parm');
0083         for k=1:length(contains)
0084             parm_name = contains(k).name;
0085             % *_parm : cell is loaded.
0086             tmp = load(project_file, parm_name);
0087             if ~iscell(tmp.(parm_name))
0088                 parm = cell(1);
0089                 parm{1} = tmp.parm_name;
0090             else
0091                 parm = tmp.(parm_name);
0092             end
0093             all_parms  = [all_parms; parm(:)];
0094             parm_types = [parm_types; cellstr(repmat(parm_name, length(parm), 1))];
0095         end
0096 
0097         % remove empty line
0098         empty_ix = [];
0099         for k=1:length(parm_types)
0100             if isempty(parm_types{k})
0101                 empty_ix = [empty_ix;k];
0102             end
0103         end
0104         if ~isempty(empty_ix)
0105             parm_types(empty_ix) = [];
0106         end
0107 
0108         % parameter sort by time_stamp
0109         time_stamps = [];
0110         for k=1:length(all_parms)
0111             if ~isfield(all_parms{k}, 'time_stamp')
0112                 break;
0113             end
0114             time_stamps = [time_stamps; all_parms{k}.time_stamp];
0115         end
0116         if ~isempty(time_stamps)
0117             [time_stamps, ix] = sortrows(time_stamps);
0118             all_parms = all_parms(ix);
0119             parm_types = parm_types(ix);
0120         end
0121 
0122         notify_status_change;
0123         err = false;
0124     else
0125         err = true;
0126     end
0127     varargout{1} = err;
0128 
0129 case 'add'
0130     if isempty(project_file) || ~exist(project_file, 'file')
0131        varargout{1} = false;
0132        return;
0133     end
0134     % caller_function is supposed to the name of job function.
0135     stack = dbstack;
0136     [a, caller_function] = vb_get_file_parts(stack(2).name);
0137     if nargin == 4
0138         caller_function = varargin{4};
0139     end
0140     parm_name = varargin{2};
0141     parm = varargin{3};
0142     
0143     % Add execution information
0144     parm.time_stamp      = datestr(fix(clock), 31);
0145     parm.caller_function = caller_function;
0146     
0147     Nparm = 0;
0148     tmp = whos('-file', project_file, parm_name);
0149     if length(tmp)
0150         load(project_file, parm_name);
0151         Nparm = length(eval(parm_name)); % Nparm = length(brain_parm)
0152     else
0153         command = [parm_name '=cell(0);'];
0154         eval(command);
0155     end
0156     command = [parm_name '{' num2str(Nparm+1) '} = parm;'];
0157     eval(command);
0158     
0159     vb_save(project_file, parm_name);
0160     project_file_mgr('load', project_file);
0161     notify_status_change;
0162     varargout{1} = true;
0163 
0164 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0165 % Parameters get function
0166 %
0167 case 'get_all_parameters'
0168     % return sorted data
0169     varargout{1} = all_parms;
0170     varargout{2} = parm_types;
0171 
0172 case 'get_parameter'
0173     itemNum = varargin{2};
0174     [all_parms, parm_types] = project_file_mgr('get_all_parameters');
0175     if length(all_parms) >= itemNum
0176         varargout{1} = all_parms{itemNum};
0177         varargout{2} = parm_types{itemNum};
0178     else
0179         varargout{1} = [];
0180         varargout{2} = [];
0181     end
0182 
0183 case 'get_project_root'
0184     varargout{1} = proj_root;
0185 case 'get_project_name'
0186     varargout{1} = project_name;
0187 case 'get_project_file'
0188     varargout{1} = project_file;
0189 end
0190 
0191 function notify_status_change
0192     current_hidden_state = get(0, 'ShowHiddenHandles');
0193     set(0, 'ShowHiddenHandles', 'on');
0194     fig = findobj('Tag', 'project_mgr');
0195     set(0, 'ShowHiddenHandles', current_hidden_state);
0196     if ~isempty(fig)
0197         project_mgr_view_update(fig);
0198     end

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005