mk_image

PURPOSE ^

MK_IMAGE: create eidors image object

SYNOPSIS ^

function img= mk_image(mdl, elem_data, params, name)

DESCRIPTION ^

 MK_IMAGE: create eidors image object
   img= mk_image(mdl, elem_data, name)

 Utility function to create an eidors_image object:
 Usage 1:
   img = mk_image( inv_model )  -> uses jacobian backgnd for conductivity

 Usage 2: mdl can be a fwd_model or inv_model
   img = mk_image( mdl, 1 ) -> uniform image with conductivity 1
   img = mk_image( mdl, 2*ones(n_elems,1) ) -> uniform with c=2
   img = mk_image( mdl, 2*ones(n_nodes,1) ) -> image with node data
   img = mk_image( mdl, 1, 'This name') -> Specify a 'name' attribute
   img = mk_image( mdl, ones(n_elems,3) );

 Usage 3: create image from previous image, override conductity
  img = mk_image( other_image, 2 ) -> image with c=2

 Usage 4: create image with specific 'params' properties
  img = mk_image( mdl, 3*ones(64,1),'resistivity' ); % resistivity image

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function img= mk_image(mdl, elem_data, params, name)
0002 % MK_IMAGE: create eidors image object
0003 %   img= mk_image(mdl, elem_data, name)
0004 %
0005 % Utility function to create an eidors_image object:
0006 % Usage 1:
0007 %   img = mk_image( inv_model )  -> uses jacobian backgnd for conductivity
0008 %
0009 % Usage 2: mdl can be a fwd_model or inv_model
0010 %   img = mk_image( mdl, 1 ) -> uniform image with conductivity 1
0011 %   img = mk_image( mdl, 2*ones(n_elems,1) ) -> uniform with c=2
0012 %   img = mk_image( mdl, 2*ones(n_nodes,1) ) -> image with node data
0013 %   img = mk_image( mdl, 1, 'This name') -> Specify a 'name' attribute
0014 %   img = mk_image( mdl, ones(n_elems,3) );
0015 %
0016 % Usage 3: create image from previous image, override conductity
0017 %  img = mk_image( other_image, 2 ) -> image with c=2
0018 %
0019 % Usage 4: create image with specific 'params' properties
0020 %  img = mk_image( mdl, 3*ones(64,1),'resistivity' ); % resistivity image
0021 
0022 % (C) 2008-12 Andy Adler and Bartlomiej Grychtol.
0023 % Licenced under GPL version 2 or 3
0024 % $Id: mk_image.m 6926 2024-05-31 15:34:13Z bgrychtol $
0025 
0026 if ischar(mdl) && strcmp(mdl,'UNIT_TEST'); do_unit_test; return; end
0027 
0028 default_params = no_params; % later: conductivity ?
0029 default_name    = 'Created by mk_image';
0030 if nargin<3
0031     name = default_name;
0032     params = default_params;
0033 elseif nargin < 4 % preserve old interface
0034     if ismember(params,supported_params)
0035         name = default_name;
0036     else
0037         name = params;
0038         params = default_params;
0039     end
0040 end
0041 
0042 % if nargin<2;
0043 %    try
0044 %      elem_data = mdl.jacobian_bkgnd.value;
0045 %    catch
0046 %      error('mk_image: for one parameter, needs a mdl.jacobian_bkgnd field');
0047 %    end
0048 % end
0049 
0050 switch mdl.type
0051    case 'inv_model'
0052       if nargin == 1
0053          img = calc_jacobian_bkgnd(mdl);
0054          return
0055       else
0056          mdl = mdl.fwd_model;
0057          % warning('Ignoring inv_model.jacobian_bkgnd in favour of the supplied elem_data');
0058       end
0059    case 'fwd_model'
0060       mdl = mdl; % keep model
0061    case 'image'
0062       if nargin == 1
0063          img = data_mapper(mdl);
0064          return
0065       end
0066       mdl = mdl.fwd_model;
0067    otherwise; error('mk_image: no inv_model, fwd_model or image object');
0068 end
0069 
0070 img = eidors_obj('image',name);
0071 img.fwd_model = mdl;
0072 if isfield(mdl,'show_slices');
0073     img.show_slices = mdl.show_slices;
0074 end
0075 img = fill_in_data(img,elem_data,params);
0076 % img.current_params = params;
0077 
0078 % standard field order
0079 img = eidors_obj('set', img);
0080 
0081 function str = no_params
0082 str = 'unspecified';
0083 
0084 function img = fill_in_data(img,elem_data,params)
0085   % If image is presented as row vector, then transpose
0086   %  Shouldn't happen, but for matlab bugs
0087   if size(elem_data,1) == 1 && ...
0088      size(elem_data,2) >  1 
0089      elem_data = elem_data.';
0090   end
0091   switch size(elem_data,1)
0092      case 1
0093         sz = size(img.fwd_model.elems,1);
0094         elem_data_mat =    NaN*ones(sz,1);
0095         elem_data_mat(:) = elem_data;
0096         if strcmp(params,no_params);
0097             img.elem_data = elem_data_mat;
0098         else
0099             img.(params).elem_data = elem_data_mat;
0100         end
0101 
0102      case num_elems( img )
0103         if strcmp(params,no_params);
0104             img.elem_data = elem_data;
0105         else
0106             img.(params).elem_data = elem_data;
0107         end
0108           
0109       case num_nodes( img )
0110         if strcmp(params,no_params);
0111             img.node_data = elem_data;
0112         else
0113             img.(params).node_data = elem_data;
0114         end
0115         
0116       case size(img.fwd_model.coarse2fine,2)
0117         if strcmp(params,no_params);
0118             img.elem_data = elem_data;
0119         else
0120             img.(params).elem_data = elem_data;
0121         end
0122 
0123      otherwise
0124         error('Don''t understand number of elements.');
0125   end
0126 
0127 % TESTS:
0128 function do_unit_test
0129    imdl = mk_common_model('a2c2',8);
0130    im0 = mk_image( imdl );
0131    unit_test_cmp('im1',im0.elem_data, ones(64,1) );
0132 
0133    im0 = mk_image( imdl, 2 ); % warning
0134    unit_test_cmp('im2',im0.elem_data, 2*ones(64,1) );
0135 
0136    im0 = mk_image( imdl.fwd_model, 3*ones(64,1) );
0137    unit_test_cmp('im3',im0.elem_data, 3*ones(64,1) );
0138 
0139    im0 = mk_image(im0); % no change
0140    unit_test_cmp('im4',im0.elem_data, 3*ones(64,1) );
0141 
0142    im0.conductivity.elem_data = im0.elem_data;
0143    im0 = rmfield(im0, 'elem_data');
0144    im1 = mk_image(im0); % use data_mapper
0145    unit_test_cmp('im5',im1.elem_data, 3*ones(64,1) );
0146 
0147    im0.resistivity.node_data = 5;
0148    im0.data_mapper = 'resistivity';
0149    im1 = mk_image(im0); % use data_mapper
0150    unit_test_cmp('im6',im1.node_data(5), 5 );
0151 
0152    imdl.jacobian_bkgnd = rmfield(imdl.jacobian_bkgnd, 'value');
0153    imdl.jacobian_bkgnd.conductivity = im0.conductivity;
0154    im2 = mk_image(imdl);
0155    unit_test_cmp('im7',im2.conductivity.elem_data, 3*ones(64,1) );
0156 
0157    im0 = mk_image( imdl.fwd_model, 3*ones(64,1),'my_name' );
0158    unit_test_cmp('im8a',im0.elem_data, 3*ones(64,1) );
0159 
0160    im0 = mk_image( imdl.fwd_model, 3*ones(64,1),'resistivity' );
0161    unit_test_cmp('im8b',im0.resistivity.elem_data, 3*ones(64,1) );
0162 
0163    im0 = mk_image( imdl.fwd_model, 2*ones(64,3),'resistivity' );
0164    unit_test_cmp('im8c',im0.resistivity.elem_data, 2*ones(64,3) );
0165 
0166    im0 = mk_image( imdl.fwd_model, 3*ones(64,1),'resistivity','my name' );
0167    unit_test_cmp('im9a',im0.resistivity.elem_data, 3*ones(64,1) );
0168    unit_test_cmp('im9b',im0.name, 'my name' );

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