valid_inv_model

PURPOSE ^

[pass, err_str] = valid_fwd_model(imdl)

SYNOPSIS ^

function [pass, err_str] = valid_inv_model(imdl)

DESCRIPTION ^

 [pass, err_str] = valid_fwd_model(imdl)

 Confirms that a valid forward model structure exists or
 explain why a model is not valid.

 If called without an output argument (nargout=0), will
 error out if invalid. Otherwise the function is silent,
 returning an explaination of failures in err_str.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [pass, err_str] = valid_inv_model(imdl)
0002 % [pass, err_str] = valid_fwd_model(imdl)
0003 %
0004 % Confirms that a valid forward model structure exists or
0005 % explain why a model is not valid.
0006 %
0007 % If called without an output argument (nargout=0), will
0008 % error out if invalid. Otherwise the function is silent,
0009 % returning an explaination of failures in err_str.
0010 
0011 % (C) 2015 Alistair Boyle. License: GPL version 2 or version 3
0012 % $Id: valid_inv_model.m 5114 2015-06-14 16:26:04Z aadler $
0013 
0014 pass = 1;
0015 err_str = '';
0016 
0017 % it's a struct with fields
0018 if ~isstruct(imdl)
0019    pass = 0;
0020    err_str = [err_str '- not a struct\n'];
0021 end
0022 
0023 % required fields
0024 %      field         type
0025 f = {'name',        'char', ...
0026      'fwd_model',   'struct', ...
0027      'solve',       'function', ...
0028      'reconst_type','char', ... % 'absolute' or 'difference'
0029      'type',        'char'};
0030 %     'hyperparameter', 'numeric', ... % struct, func, or numeric?
0031 %     'RtR_prior',   'function', ... % function OR numeric
0032 %    'jacobian_bkgnd', 'struct', ... % struct OR numeric
0033 for i=1:length(f)/2
0034    x=2*(i-1)+1;
0035    y=x+1;
0036    if ~isfield(imdl, f{x})
0037       pass = 0;
0038       err_str = [err_str '- missing required field: "' f{x} '"\n'];
0039    elseif strcmp(f{y},'function')
0040       if ~isfunc(imdl.(f{x}))
0041          pass = 0;
0042          err_str = [err_str '- expected function (pointer or string): "' f{x} '"\n'];
0043       end
0044    elseif ~isa(imdl.(f{x}), f{y})
0045       pass = 0;
0046       err_str = [err_str '- required field "' f{x} '" is not a ' f{y} '\n'];
0047    end
0048 end
0049 % check the fwd_model
0050 [pass_local, err_str_local] = valid_fwd_model(imdl.fwd_model);
0051 if ~pass_local
0052    pass = 0;
0053    disp(err_str_local);
0054    err_str_local = strrep(err_str_local, ' "', ' "fwd_model.');
0055    err_str = [err_str err_str_local];
0056 end
0057 clear err_str_local pass_local;
0058 % check for correct 'type'
0059 if ~strcmp(imdl.type, 'inv_model')
0060    pass = 0;
0061    err_str = [err_str '- field "type" must be "inv_model"\n'];
0062 end
0063 
0064 % optional fields
0065 %      field       type
0066 f = {'rec_model',   'struct'};
0067 for i=1:length(f)/2
0068    x=2*(i-1)+1;
0069    y=x+1;
0070    if isfield(imdl, f{x}) && ~isa(imdl.(f{x}), f{y})
0071       pass = 0;
0072       err_str = [err_str '- optional field "' f{x} '" is not a ' f{y} '\n'];
0073    end
0074 end
0075 % check the rec_model
0076 if isfield(imdl, 'rec_model')
0077    [pass_local, err_str_local] = valid_fwd_model(imdl.rec_model, 'rec_model');
0078    if ~pass_local
0079       pass = 0;
0080       disp(err_str_local);
0081       err_str_local = strrep(err_str_local, ' "', ' "rec_model.');
0082       err_str = [err_str err_str_local];
0083    end
0084    clear err_str_local pass_local;
0085 end
0086 
0087 % illegal fields
0088 %      field
0089 f = {'inv_model', ... % no recursion
0090      'fmdl', ... % no short forms
0091      'fwd_mdl'};
0092 for i=1:length(f)
0093    x=i;
0094    if isfield(imdl, f{x})
0095       pass = 0;
0096       err_str = [err_str '- illegal field "' f{x} '" found\n'];
0097    end
0098 end
0099 
0100 % result
0101 if ~pass
0102    err_str = err_str(1:end-2); % drop last \n
0103 end
0104 if ( nargout == 0 ) && ~pass
0105    error(sprintf(['Reasons:\n' err_str]));
0106 end
0107 
0108 function t=isfunc(f)
0109 t=isa(f, 'function_handle') || isa(f, 'char');

Generated on Tue 31-Dec-2019 17:03:26 by m2html © 2005