mdl_normalize

PURPOSE ^

MDL_NORMALIZE Check or set the normalize_measurements flag on a model.

SYNOPSIS ^

function out = mdl_normalize(mdl, val)

DESCRIPTION ^

MDL_NORMALIZE Check or set the normalize_measurements flag on a model.
  OUT = MDL_NORMALIZE(MDL) returns the value of the 
  'normalize_measurements' or 'normalize' field on MDL. If absent, the 
  default value is returned as set with EIDORS_DEFAULT. 

  MDL = MDL_NORMALIZE(MDL, VAL) sets the 'normalize_measurements' to VAL

  MDL = MDL_NORMALIZE(MDL, 'default') uses the EIDORS_DEFAULT value to set
  set the 'normalize_measurements' flag on the model.

  OUT = MDL_NORMALIZE('default') returns the EIDORS_DEFAULT value to set
  set the 'normalize_measurements' flag on the model.

 See also: FWD_MODEL_PARAMETERS, EIDORS_DEFAULT, EIDORS_STARTUP

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function out = mdl_normalize(mdl, val)
0002 %MDL_NORMALIZE Check or set the normalize_measurements flag on a model.
0003 %  OUT = MDL_NORMALIZE(MDL) returns the value of the
0004 %  'normalize_measurements' or 'normalize' field on MDL. If absent, the
0005 %  default value is returned as set with EIDORS_DEFAULT.
0006 %
0007 %  MDL = MDL_NORMALIZE(MDL, VAL) sets the 'normalize_measurements' to VAL
0008 %
0009 %  MDL = MDL_NORMALIZE(MDL, 'default') uses the EIDORS_DEFAULT value to set
0010 %  set the 'normalize_measurements' flag on the model.
0011 %
0012 %  OUT = MDL_NORMALIZE('default') returns the EIDORS_DEFAULT value to set
0013 %  set the 'normalize_measurements' flag on the model.
0014 %
0015 % See also: FWD_MODEL_PARAMETERS, EIDORS_DEFAULT, EIDORS_STARTUP
0016 
0017 % (C) 2012 Bartlomiej Grychtol. License: GPL v2 or v3
0018 % $Id: mdl_normalize.m 7043 2024-11-30 14:46:20Z aadler $
0019 
0020 if ischar(mdl) && strcmp(mdl, 'UNIT_TEST'); do_unit_test; return; end
0021 if ischar(mdl) && strcmp(mdl, 'default') 
0022    out = eidors_default; 
0023    return
0024 end
0025 
0026 % check that we got a fwd_model
0027 try 
0028    switch mdl.type
0029       case 'fwd_model'
0030          % do nothing
0031       case 'image'
0032          img = mdl;
0033          mdl = img.fwd_model;
0034       otherwise
0035          error('EIDORS:WrongInput','Expected fwd_model or image.');
0036    end
0037 catch
0038    warning('EIDORS:UnknownType','Object has no type. Assuming fwd_model.');
0039 end
0040 % do the real thing
0041 switch nargin
0042     case 1
0043         out = get_flag(mdl);
0044         if isempty(out)
0045             out = eidors_default;
0046             warning('EIDORS:normalize_flag_not_set', ...
0047                 'normalize_measurements flag not set on model. Using default: %d',out);
0048         end
0049     case 2
0050         if ischar(val) && strcmp(val, 'default')
0051            val = eidors_default;
0052         end
0053         out = set_flag(mdl,val);
0054         if exist('img','var') % input was an image
0055            img.fwd_model = out;
0056            out = img;
0057         end
0058     otherwise
0059         error('Wrong number of parameters');
0060 end
0061 
0062 
0063 
0064 function out = get_flag(mdl)
0065  out = [];
0066 % iterate over fields
0067 ff = fieldnames(mdl);
0068 for i = 1:length(ff);
0069     % if both are set, you're in trouble
0070     if any(strcmp(ff{i},{'normalize','normalize_measurements'}))
0071         out = mdl.(ff{i});
0072     else
0073         % won't hit 'normal' or anything that starts with 'normals'.
0074         token = regexp(ff{i},'normal[^s]+.*','match','once'); 
0075         if ~isempty(token);
0076             warning('Suspect field "%s" found on the model',token);
0077         end
0078         
0079     end
0080 end
0081 
0082 function mdl = set_flag(mdl, val)
0083 mdl = eidors_obj('set',mdl, 'normalize_measurements', val);
0084 
0085 function do_unit_test
0086 s = warning('query', 'backtrace');
0087 warning off backtrace;
0088 eidors_default('set','mdl_normalize',@(x) 0);
0089 mdl = mk_common_model('c2t2',16);
0090 fmdl = mdl.fwd_model;
0091 unit_test_cmp('Does it come normalized?', mdl_normalize(fmdl), 0);
0092 fmdl = mdl_normalize(fmdl,1);
0093 unit_test_cmp('Normalize it. How about now?', mdl_normalize(fmdl), 1);
0094 fmdl = mdl_normalize(fmdl,0);
0095 unit_test_cmp('De-normalize it. And now?', mdl_normalize(fmdl), 0);
0096 fmdl = rmfield(fmdl,'normalize_measurements');
0097 eidors_msg(mdl_normalize(fmdl));
0098 unit_test_cmp('Remove the flag', ...
0099    lastwarn, 'normalize_measurements flag not set on model. Using default: 0');
0100 fmdl.normalise_measurements = 1; % s, throw a warning, return default
0101 eidors_msg(mdl_normalize(fmdl));
0102 unit_test_cmp('Spelling error', ...
0103    lastwarn, 'normalize_measurements flag not set on model. Using default: 0');
0104 fmdl.normalize_my_gran = 1; % throw a warning, return default
0105 eidors_msg(mdl_normalize(fmdl));
0106 unit_test_cmp('And now let''s set it to something stupid', ...
0107    lastwarn, 'normalize_measurements flag not set on model. Using default: 0');
0108 fmdl.normalize = 1; % accept happily
0109 unit_test_cmp('Accept happily', mdl_normalize(fmdl), 1);
0110 eidors_msg('But an imdl is not acceptable!');
0111 try
0112     eidors_msg(mdl_normalize(mdl));
0113 catch
0114     unit_test_cmp('Just so.', 1,1);
0115 end
0116 warning(s);

Generated on Sun 29-Dec-2024 11:41:59 by m2html © 2005