Code covered by the BSD License  

Highlights from
Statistical Learning Toolbox

from Statistical Learning Toolbox by Dahua Lin
Functions for statistical learning, pattern recognition and computer vision, covering many topics.

Description of edl_readexpdefs
Home > sltoolbox > ExpDL > edl_readexpdefs.m

edl_readexpdefs

PURPOSE ^

EDL_READEXPDEFS Reads in an experiment definition from XML file

SYNOPSIS ^

function ED = edl_readexpdefs(filename)

DESCRIPTION ^

EDL_READEXPDEFS Reads in an experiment definition from XML file

 $ Syntax $
   - ED = edl_readexpdefs(filename)

 $ Arguments $
   - filename:     the filename of the experiment definition XML
   - ED:           the read experiment definition struct

 $ Description $
   - ED = edl_readexpdefs(filename) reads in an experiment definition
     struct from an XML file. The format of XML can be referred to 
     the edl.spec.txt. The struct ED has following fields:

       - name: the experiment definition name
       - selfpath: the absolute path of the experiment definition self
       - envconf: the filename of environment configuration
       - env:     the environment configuration struct
       - scriptdir: the script directory
       - reportdir: the report directory
       - mfiledir:  the m-files directory
       - logfile:   the path of log file
       - logger:    the logger

       - variables: a struct of all variables, 
           using variable names as field names
           using variable values as field values
       - scripts: the struct of scripts
           - using name as fieldnames, each field is a struct with
               - func:     the scripting function
               - path:     the path of the script file
               - ctrlpath: the path of the control file
               - params:   the other parameters
               - refs:     the struct array of referenced scripts
                   using role as field name
                   using script name as field value
               - refreps:  the struct array of referenced reports
                   using role as field name
                   using report name as field value            
               - refpaths: the struct array of referenced paths
                   using role as field name
                   using xml path of (script or report) as field value
               - refscopes: the struct array of scope names of references
                            (scripts | reports)
                   using role as field name
                   using scope names as field values
       - experiments: the struct of experiments
           - using name as fieldnames, each field is a struct with
               - func:         the experiment function
               - script:       the script name
               - scriptpath:   the script filepath
               - ctrlpath:     the control filepath
       - reports: the struct array of reports
           similar to scripts, except for that there is no ctrlpath
       
 $ History $
   - Created by Dahua Lin, on Aug 12nd, 2006

CROSS-REFERENCE INFORMATION ^

This function calls:
  • edl_readenvvars EDL_READENVVARS Reads in a file with environment variables
  • addfiles ADDFILES add a set of log files to the logger
  • sllog SLLOG Constructs a logger
  • slchangefilepart SLCHANGEFILEPART Changes some parts of the file path
  • slfilepart SLFILEPARTS Extracts a specified part of a file path string
  • slisabspath SLISABSPATH Judges whether the path string is a absolute path
  • sladdpath SLADDPATH Adds dirpath to precede the filenames
  • slparseprops SLPARSEPROPS Parses input parameters
  • xml_getattribs XML_GETATTRIBS Constructs an attribte struct from an XML element
This function is called by:
  • edl_go EDL_GO The Top interface for doing experiments in EDL

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function ED = edl_readexpdefs(filename)
0002 %EDL_READEXPDEFS Reads in an experiment definition from XML file
0003 %
0004 % $ Syntax $
0005 %   - ED = edl_readexpdefs(filename)
0006 %
0007 % $ Arguments $
0008 %   - filename:     the filename of the experiment definition XML
0009 %   - ED:           the read experiment definition struct
0010 %
0011 % $ Description $
0012 %   - ED = edl_readexpdefs(filename) reads in an experiment definition
0013 %     struct from an XML file. The format of XML can be referred to
0014 %     the edl.spec.txt. The struct ED has following fields:
0015 %
0016 %       - name: the experiment definition name
0017 %       - selfpath: the absolute path of the experiment definition self
0018 %       - envconf: the filename of environment configuration
0019 %       - env:     the environment configuration struct
0020 %       - scriptdir: the script directory
0021 %       - reportdir: the report directory
0022 %       - mfiledir:  the m-files directory
0023 %       - logfile:   the path of log file
0024 %       - logger:    the logger
0025 %
0026 %       - variables: a struct of all variables,
0027 %           using variable names as field names
0028 %           using variable values as field values
0029 %       - scripts: the struct of scripts
0030 %           - using name as fieldnames, each field is a struct with
0031 %               - func:     the scripting function
0032 %               - path:     the path of the script file
0033 %               - ctrlpath: the path of the control file
0034 %               - params:   the other parameters
0035 %               - refs:     the struct array of referenced scripts
0036 %                   using role as field name
0037 %                   using script name as field value
0038 %               - refreps:  the struct array of referenced reports
0039 %                   using role as field name
0040 %                   using report name as field value
0041 %               - refpaths: the struct array of referenced paths
0042 %                   using role as field name
0043 %                   using xml path of (script or report) as field value
0044 %               - refscopes: the struct array of scope names of references
0045 %                            (scripts | reports)
0046 %                   using role as field name
0047 %                   using scope names as field values
0048 %       - experiments: the struct of experiments
0049 %           - using name as fieldnames, each field is a struct with
0050 %               - func:         the experiment function
0051 %               - script:       the script name
0052 %               - scriptpath:   the script filepath
0053 %               - ctrlpath:     the control filepath
0054 %       - reports: the struct array of reports
0055 %           similar to scripts, except for that there is no ctrlpath
0056 %
0057 % $ History $
0058 %   - Created by Dahua Lin, on Aug 12nd, 2006
0059 %
0060 
0061 %% Read file
0062 
0063 xdoc = xmlread(filename);
0064 edparent = slfilepart(filename, 'parent');
0065 docelem = xdoc.getDocumentElement;
0066 
0067 
0068 %% Read Header
0069 
0070 headerfns = { ...
0071     'name', ...
0072     'envconf', ...
0073     'scriptdir', ...
0074     'reportdir', ...
0075     'mfiledir', ...
0076     'logfile'};
0077 ED = cell2struct(cell(1, length(headerfns)), headerfns, 2);
0078 headers = xml_getattribs(docelem);
0079 ED = slparseprops(ED, headers);
0080 
0081 if slisabspath(filename)
0082     ED.selfpath = filename;
0083 else
0084     ED.selfpath = sladdpath(filename, cd());
0085 end
0086 
0087 if isempty(ED.envconf)
0088     ED.env = [];
0089 else
0090     envpath = sladdpath(ED.envconf, edparent);
0091     ED.env = edl_readenvvars(envpath);
0092 end
0093 
0094 if isempty(ED.env) || ~isfield(ED.env, 'envname')
0095     error('edl:parseerror', ...
0096         'The environment must has variable named %s', 'envname');
0097 end
0098 envname = ED.env.envname;
0099 
0100 ED.logger = sllog('rootpath', edparent);
0101 if ~isempty(ED.logfile)
0102     ED.logfile = [ED.logfile, '.', envname, '.log'];
0103     ED.logger = addfiles(ED.logger, ED.logfile);
0104 end
0105 
0106 %% First-Pass Parsing
0107 % 1. Build variable table progressively
0108 % 2. Build basic structures
0109 % 3. Translate variables
0110 
0111 ED.variables = [];
0112 ED.scripts = [];
0113 ED.experiments = [];
0114 ED.reports = [];
0115 
0116 ED = first_pass(ED, docelem, struct('workdir', ''));
0117 
0118 
0119 %% Second-Pass Parsing
0120 % 1. Extract paths from cross-references
0121 %   (a) build refpaths for scripts and reports
0122 %   (b) build scriptpath and ctrlpath for experiments
0123 %
0124 
0125 % 1.(a)
0126 ED.scripts = build_refpaths(ED, ED.scripts);
0127 ED.reports = build_refpaths(ED, ED.reports);
0128 
0129 % 1.(b)
0130 ED.experiments = build_exppaths(ED, ED.experiments);
0131 
0132 
0133 
0134 %% Core function for First-Pass (recursive invoking)
0135 
0136 function ED = first_pass(ED, xelem, groupvars)
0137 
0138 nodeList = xelem.getChildNodes;
0139 n = nodeList.getLength;
0140 
0141 for i = 1 : n   % enumerate all children
0142     
0143     node = nodeList.item(i-1);    
0144     if (node.getNodeType == node.ELEMENT_NODE)       
0145         tag = char(node.getTagName);
0146         
0147         switch tag
0148             
0149             case 'Var'
0150                 [varname, varval] = parse_var(ED, node);
0151                 ED.variables.(varname) = varval;
0152                 
0153             case 'Script'
0154                 [scriptname, scriptstruct] = parse_script(ED, node);
0155                 scriptstruct.params = weak_update(scriptstruct.params, groupvars);
0156                 ED.scripts.(scriptname) = scriptstruct;
0157             
0158             case 'Report'
0159                 [reportname, reportstruct] = parse_report(ED, node);
0160                 ED.reports.(reportname) = reportstruct;
0161                 
0162             case 'Experiment'
0163                 [expname, expstruct] = parse_experiment(ED, node);
0164                 ED.experiments.(expname) = expstruct; 
0165                 
0166             case 'Group'                
0167                 subgroupvars = parse_group(ED, groupvars, node);
0168                 ED = first_pass(ED, node, subgroupvars);
0169                 
0170             otherwise
0171                 error('edl:parseerror', ...
0172                     'Invalid element with tag name %s', tag);                            
0173         end
0174                 
0175     end
0176     
0177 end
0178 
0179 
0180 %% Core functions for Second-Pass
0181 
0182 function S = build_refpaths(ED, S)
0183 
0184 fns = fieldnames(S);
0185 n = length(fns);
0186 
0187 for i = 1 : n
0188     fn = fns{i};
0189     
0190     S.(fn) = build_refpaths_fortype(ED, fn, S.(fn), 'scripts', 'refs');
0191     S.(fn) = build_refpaths_fortype(ED, fn, S.(fn), 'reports', 'refreps');
0192         
0193 end
0194 
0195 
0196 function S = build_refpaths_fortype(ED, name, S, typeset, typefield)
0197 
0198 if ~isfield(S, 'refpaths')
0199     S.refpaths = [];
0200 end
0201 if ~isfield(S, 'refscopes')
0202     S.refscopes = [];
0203 end
0204 
0205 curset = S.(typefield);
0206 if isempty(curset)
0207     return;
0208 end
0209 
0210 fns = fieldnames(curset);
0211 n = length(fns);
0212 pool = ED.(typeset);
0213 
0214 for i = 1 : n
0215     role = fns{i};    
0216     refname = curset.(role);
0217     if ~isfield(pool, refname)
0218         error('edl:parseerror', ...
0219             'The reference with role %s (name = %s) in %s is not found in pool %s', ...
0220             role, refname, name, typeset);
0221     end
0222     
0223     S.refpaths.(role) = pool.(refname).path;    
0224     S.refscopes.(role) = typeset;
0225 end
0226 
0227 
0228 function ES = build_exppaths(ED, ES)
0229 
0230 if isempty(ES)
0231     return;
0232 end
0233 
0234 fns = fieldnames(ES);
0235 n = length(fns);
0236 
0237 for i = 1 : n
0238     fn = fns{i};
0239     scriptname = ES.(fn).script;
0240     if ~isfield(ED.scripts, scriptname)
0241         error('edl:parseerror', ...
0242             'The referred script named %s for %s is not found', ...
0243             scriptname, fn);
0244     end
0245     
0246     curscript = ED.scripts.(scriptname);
0247     
0248     ES.(fn).scriptpath = curscript.path;
0249     ES.(fn).ctrlpath = curscript.ctrlpath;
0250 end
0251 
0252 
0253 
0254 %% Node Parsing functions
0255 
0256 % variable parsing
0257 
0258 function [vname, vval] = parse_var(ED, xelem)
0259 
0260 vname = make_name(ED, 'variables', xelem);
0261 vval = get_tattrib(ED, xelem, 'variables', vname, 'val');
0262 
0263 
0264 % script parsing
0265 
0266 function [sname, scr] = parse_script(ED, xelem)
0267 
0268 sname = make_name(ED, 'scripts', xelem);
0269 
0270 scr.func = get_tattrib(ED, xelem, 'scripts', sname, 'func');
0271 scr.func = sladdpath(scr.func, ED.mfiledir);
0272 
0273 scr.path = '';
0274 scr.ctrlpath = '';
0275 scr = update_fields(ED, scr, xelem, {'path', 'ctrlpath'});
0276 scr.params = get_tattribs(ED, xelem, ...
0277     'exclude', {'name', 'func', 'path', 'ctrlpath'});
0278 
0279 if isempty(scr.path)
0280     scr.path = [sname, '.script.xml'];
0281 end
0282 if isempty(scr.ctrlpath)
0283     scrfiletitle = slfilepart(scr.path, 'title');
0284     scr.ctrlpath = slchangefilepart(scr.path, 'title', [scrfiletitle, '.control']);
0285 end
0286 
0287 scr.path = sladdpath(scr.path, ED.scriptdir);
0288 
0289 scr = build_reftables(ED, scr, xelem);
0290 
0291 
0292 % report parsing
0293 
0294 function [rname, rep] = parse_report(ED, xelem)
0295 
0296 rname = make_name(ED, 'reports', xelem);
0297 
0298 rep.func = get_tattrib(ED, xelem, 'reports', rname, 'func');
0299 rep.func = sladdpath(rep.func, ED.mfiledir);
0300 
0301 rep.path = '';
0302 rep = update_fields(ED, rep, xelem, {'path'});
0303 rep.params = get_tattribs(ED, xelem, ...
0304     'exclude', {'name', 'func', 'path', 'ctrlpath'});
0305 
0306 if isempty(rep.path)
0307     rep.path = [rname, '.report.xml'];
0308 end
0309 rep.path = sladdpath(rep.path, ED.reportdir);
0310 
0311 rep = build_reftables(ED, rep, xelem);
0312 
0313 
0314 % experiment parsing
0315 
0316 function [expname, es] = parse_experiment(ED, xelem)
0317 
0318 expname = make_name(ED, 'experiments', xelem);
0319 
0320 es.func = get_tattrib(ED, xelem, 'experiments', expname, 'func'); 
0321 es.func = sladdpath(es.func, ED.mfiledir);
0322 
0323 es.script = '';
0324 es = update_fields(ED, es, xelem, {'script'});
0325 
0326 if isempty(es.script)
0327     es.script = expname;
0328 end
0329 
0330 
0331 % group parsing
0332 
0333 function newvars = parse_group(ED, gvars, xelem)
0334 
0335 newvars = get_tattribs(ED, xelem, 'exclude', {'title'});
0336 
0337 if ~isfield(newvars, 'workdir') || isempty(newvars.workdir)
0338     newvars.workdir = gvars.workdir;
0339 else
0340     newvars.workdir = sladdpath(newvars.workdir, gvars.workdir);
0341 end
0342 
0343 newvars = weak_update(newvars, gvars);
0344 
0345 
0346 
0347 
0348 
0349 
0350 %% Auxiliary functions
0351 
0352 
0353 function val = translate_attribval(ED, attrval)
0354 
0355 val = attrval;
0356 if length(val) > 1 && val(1) == '$'
0357     varname = val(2:end);
0358     if ~isfield(ED.variables, varname)
0359         error('edl:parseerror', ...
0360             'The variable %s is not found', varname);
0361     end
0362     val = ED.variables.(varname);
0363 end
0364 
0365 
0366 function aval = get_tattrib(ED, xelem, scope, elemname, attrname)
0367 
0368 if xelem.hasAttribute(attrname)
0369     aval = char(xelem.getAttribute(attrname));
0370     aval = translate_attribval(ED, aval);
0371 else
0372     error('edl:parseerror', ...
0373         'The attribute %s is not found in %s of %s', attrname, elemname, scope);
0374 end
0375 
0376 
0377 function A = get_tattribs(ED, xelem, varargin)
0378 
0379 A = xml_getattribs(xelem, varargin{:});
0380 
0381 if ~isempty(A)
0382     fns = fieldnames(A);
0383     n = length(fns);
0384     for i = 1 : n
0385         fn = fns{i};
0386         A.(fn) = translate_attribval(ED, A.(fn));
0387     end
0388 end    
0389 
0390 
0391 function name = make_name(ED, scope, xelem)
0392 
0393 if xelem.hasAttribute('name')
0394     name = char(xelem.getAttribute('name'));
0395     if isfield(ED.(scope), name)
0396         error('edl:parseerror', ...
0397             'Redefinition of %s in %s', name, scope);
0398     end
0399 else
0400     error('edl:parseerror', ...
0401         'Encounter an element without name in %s', scope);
0402 end
0403 
0404 
0405 function S = update_fields(ED, S, xelem, fns)
0406 
0407 S = slparseprops(S, get_tattribs(ED, xelem, ...
0408     'select', fns, ...
0409     'forceexist', false));
0410 
0411 
0412 function S = weak_update(S, Scomp)
0413 
0414 fns = fieldnames(Scomp);
0415 n = length(fns);
0416 for i = 1 : n
0417     fn = fns{i};
0418     if ~isfield(S, fn)
0419         S.(fn) = Scomp.(fn);
0420     end
0421 end
0422 
0423 
0424 function S = build_reftables(ED, S, xelem)
0425 
0426 refNodes = xelem.getElementsByTagName('Ref');
0427 nrs = refNodes.getLength;
0428 
0429 S.refs = [];
0430 S.refreps = [];
0431 
0432 for i = 1 : nrs
0433     curnode = refNodes.item(i-1);
0434     cur = get_tattribs(ED, curnode, 'select', {'role', 'name'});
0435     S.refs.(cur.role) = cur.name;
0436 end
0437 
0438 refrepNodes = xelem.getElementsByTagName('RefReport');
0439 nrr = refrepNodes.getLength;
0440 
0441 for i = 1 : nrr
0442     curnode = refrepNodes.item(i-1);
0443     cur = get_tattribs(ED, curnode, 'select', {'role', 'name'});
0444     S.refreps.(cur.role) = cur.name;
0445 end
0446 
0447 
0448 
0449 
0450 
0451 
0452 
0453

Generated on Wed 20-Sep-2006 12:43:11 by m2html © 2003

Contact us at files@mathworks.com