convert_img_units

PURPOSE ^

CONVERT_IMG_UNITS change image data units

SYNOPSIS ^

function y = convert_img_units(img,arg1,arg2)

DESCRIPTION ^

CONVERT_IMG_UNITS change image data units 
  img = convert_img_units(img,new_unit) converts img.elem_data or img.node_data
  expressed in img.current_params into different units for the same 
  physical property
 
 Examples: 
  img = mk_image(mdl, 2, 'resisitivity');
  img = convert_img_units(img, 'conductivity');

  img = mk_image(mdl, 1);
  img.current_params = 'log_resistivity';
  img = convert_img_units(img, 'conductivity');

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function y = convert_img_units(img,arg1,arg2)
0002 %CONVERT_IMG_UNITS change image data units
0003 %  img = convert_img_units(img,new_unit) converts img.elem_data or img.node_data
0004 %  expressed in img.current_params into different units for the same
0005 %  physical property
0006 %
0007 % Examples:
0008 %  img = mk_image(mdl, 2, 'resisitivity');
0009 %  img = convert_img_units(img, 'conductivity');
0010 %
0011 %  img = mk_image(mdl, 1);
0012 %  img.current_params = 'log_resistivity';
0013 %  img = convert_img_units(img, 'conductivity');
0014 
0015 % (C) 2012-2014 Alistair Boyle and Bartlomiej Grychtol
0016 % License: GPL version 2 or 3
0017 % $Id: convert_img_units.m 5115 2015-06-14 16:31:18Z aadler $
0018 
0019 if ischar(img) && strcmp(img,'UNIT_TEST'); y = do_unit_test; return; end
0020 
0021 if nargin < 2
0022   error('EIDORS:ConvertUnits:Input', ...
0023       'Not enough input arguments.');
0024 end
0025 
0026 % convert_img_units(x, in, out);
0027 if isnumeric(img)
0028   % require 3 arguments
0029   if nargin < 3
0030     error('EIDORS:ConvertUnits:Input', ...
0031       'Two units are required to convert numeric values.');
0032   else
0033     x = img;
0034     cur_unit = arg1;
0035     new_unit = arg2;
0036     y = map_data(x, cur_unit, new_unit);
0037     return
0038   end
0039 end
0040 
0041 % convert_img_units(img, out);
0042 if any(isfield(img,{'elem_data','node_data'}))
0043   if nargin == 3
0044     cur_unit = arg1;
0045     new_unit = arg2;
0046   else
0047     try
0048       cur_unit = img.current_params;
0049       new_unit = arg1;
0050     catch
0051       error(['Don''t know what to convert. '...
0052               'Need either 3 arguments or img.current_params.']);
0053     end
0054   end
0055 else
0056   if nargin ==3
0057     cur_unit = arg1;
0058     img.data_mapper = cur_unit;
0059     new_unit = arg2;
0060   else
0061     new_unit = arg1;
0062   end
0063   img = data_mapper(img);
0064   cur_unit = img.current_params;
0065   
0066   % what to do about old parametrization??
0067 %   img = rmfield(img, 'cur_unit');
0068 end
0069 
0070 
0071 [x do_nodes] = get_data(img);
0072 
0073 y = map_data(x, cur_unit, new_unit);
0074 
0075 
0076 if do_nodes
0077   img.node_data = y;
0078 else
0079   img.elem_data = y;
0080 end
0081 img.current_params = new_unit;
0082 y = img;
0083 
0084 end
0085 
0086 function x = map_data(x, cur_unit, new_unit)
0087 
0088 if isempty(cur_unit) || isempty(new_unit)
0089   error('Unit string must not be empty.');
0090 end
0091 
0092 if strcmp(cur_unit, new_unit)
0093     return %nothing to do
0094 end
0095 
0096 f = str2func(sprintf('%s2%s',cur_unit,new_unit));
0097 try
0098    x = feval(f,x);
0099    x = fix_and_test(x);
0100 catch err
0101   if strcmp(err.identifier,'MATLAB:UndefinedFunction')
0102     cur_pre = regexp(cur_unit,'^(.*?)_','match');
0103     if ~isempty(cur_pre) && ismember(cur_pre{1}, {'log_', 'log10_'});
0104       l = length(cur_pre{1});
0105       x = reverse(cur_pre{1}, x);
0106       x = map_data(x, cur_unit(l+1:end), new_unit);
0107       x = fix_and_test(x);
0108       return
0109     end
0110     new_pre = regexp(new_unit,'^(.*?)_','match');
0111     if ~isempty(new_pre) && ismember(new_pre{1}, {'log_', 'log10_', 'abs_'});
0112       l = length(new_pre{1});
0113       x = map_data(x, cur_unit, new_unit(l+1:end));
0114       x = apply(new_pre{1}, x);
0115       x = fix_and_test(x);
0116       return
0117     end
0118     
0119     %no direct function for conversion, see if we can help
0120     error('EIDORS:ConversionNotSupported', errorstr(cur_unit, new_unit));
0121   else
0122     rethrow(err);
0123   end
0124 end
0125 
0126 end
0127 
0128 function x = fix_and_test(x)
0129   x(x == +inf) = +realmax;
0130   x(x == -inf) = -realmax;
0131   err_if_inf_or_nan(x, 'map_data-post');
0132 end
0133 
0134 function err_if_inf_or_nan(x, str)
0135   if any(isnan(x) | isinf(x))
0136       error(sprintf('bad %s (%d NaN, %d Inf of %d)', ...
0137                     str, ...
0138                     length(find(isnan(x))), ...
0139                     length(find(isinf(x))), ...
0140                     length(x)));
0141   end
0142 
0143 end
0144 
0145 function x = conductivity2resistivity(x)
0146   x = 1./x;
0147 end
0148 
0149 function x = resistivity2conductivity(x)
0150   x = 1./x;
0151 end
0152 
0153 function erstr = errorstr(cur_unit, new_unit)
0154   erstr = sprintf( ['Unit conversion from %s to %s is not supported.\n'...
0155              'Please write a function %s2%s.'], cur_unit, new_unit, cur_unit, new_unit);
0156 end
0157 
0158 function x = reverse(str, x);
0159   switch str
0160     case 'log10_'
0161       if any(x >= log10(realmax)-eps) warning('loss of precision -> inf'); end
0162       x = 10.^x;
0163     case 'log_'
0164       if any(x >= log(realmax)-eps) warning('loss of precision -> inf'); end
0165       x = exp(x);
0166     otherwise
0167       error('Cannot reverse %s', str);
0168   end
0169 end
0170 
0171 
0172 function x = apply(str, x)
0173   switch str
0174     case 'log10_'
0175       if any(x <= 0 + eps) warning('loss of precision -> -inf'); end
0176       x = log10(x);
0177     case 'log_'
0178       if any(x <= 0 + eps) warning('loss of precision -> -inf'); end
0179       x = log(x);
0180     case 'abs_'
0181       x = abs(x);
0182     otherwise
0183       error('Cannot apply %s', str);
0184   end
0185 end
0186   
0187 function [x, do_nodes] = get_data(img)
0188   do_nodes = 0;
0189   try
0190     x = img.elem_data;
0191   catch
0192     try
0193       x = img.node_data;
0194       do_nodes = 1;
0195     catch
0196       error('No elem_data or node_data found');
0197     end
0198   end
0199 end
0200 
0201 % test functions
0202 function pass = do_unit_test
0203   pass = 1;
0204   d = 1;
0205   while (d ~= 1) && (d ~= 0)
0206     d = rand(1);
0207   end
0208   disp('TEST: convert_img_units()');
0209   elem_types = {'conductivity', 'log_conductivity', 'log10_conductivity', ...
0210     'resistivity',  'log_resistivity',  'log10_resistivity'};
0211   expected = [d         log(d)         log10(d)      1./d      log(1./d)      log10(1./d); ...
0212     exp(d)    d              log10(exp(d)) 1./exp(d) log(1./exp(d)) log10(1./exp(d)); ...
0213     10.^d     log(10.^d )    d             1./10.^d  log(1./10.^d ) log10(1./10.^d ); ...
0214     1./d      log(1./d  )    log10(1./d)   d         log(d)         log10(d); ...
0215     1./exp(d) log(1./exp(d)) log10(1./exp(d)) exp(d) d              log10(exp(d)); ...
0216     1./10.^d  log(1./10.^d)  log10(1./10.^d)  10.^d  log(10.^d)     d ];
0217   for i = 1:length(elem_types)
0218     for j = 1:length(elem_types)
0219       pass = test_map_data(d, elem_types{i}, elem_types{j}, expected(i,j), pass);
0220     end
0221   end
0222 
0223   if pass
0224     disp('TEST: overall PASS');
0225   else
0226     disp('TEST: overall FAIL');
0227   end
0228 end
0229 
0230 
0231 function pass = test_map_data(data, in, out, expected, pass)
0232   fprintf('TEST: convert_img_units(%s -> %s)\n', in, out);
0233   if convert_img_units(data, in, out) ~= expected
0234      pass = 0;
0235      fprintf('TEST: FAIL convert_img_units(%s -> %s)\n', in, out);
0236   end
0237 end

Generated on Wed 21-Jun-2017 09:29:07 by m2html © 2005