0001 function out = mk_library_model(shape,elec_pos,elec_shape,maxsz,nfft,scale)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047 if nargin < 6; scale = 1; end
0048 if nargin < 5; nfft = 50; end
0049
0050 if ischar(shape)
0051 switch shape
0052 case 'LIBRARY_PATH'
0053 switch nargin
0054 case 1
0055 out = get_path;
0056 case 2
0057 set_path(elec_pos);
0058 end
0059 case 'list'
0060 out = list_predef_model_strings;
0061 case 'UNIT_TEST'
0062 out = do_unit_test; return;
0063 otherwise
0064 fcol = find(shape==':');
0065 extra = false;
0066 if length(fcol)>=1;
0067 extra = shape(fcol+1:end);
0068 shape = shape(1:fcol-1);
0069 end
0070 out = predef_model(shape);
0071 switch extra
0072 case false;
0073 case 'ctr';
0074 out.nodes = out.nodes - mean(out.nodes);
0075 case 'hctr';
0076 out.nodes(:,1:2) = out.nodes(:,1:2) - mean(out.nodes(:,1:2));
0077 case 'vctr';
0078 out.nodes(:,3) = out.nodes(:,3) - mean(out.nodes(:,3));
0079 otherwise; error(['parameter "',extra,'" not recognized']);
0080 end
0081 out = mdl_normalize(out, 0);
0082 end
0083 else
0084 if ~iscell(shape)
0085 shape = {shape, 'boundary'};
0086 elseif numel(shape) == 1
0087 shape{2} = 'boundary';
0088 end
0089 fname = make_filename(shape,elec_pos,elec_shape,maxsz, nfft, scale);
0090 out = load_stored_model(fname);
0091 if ~isempty(out)
0092 return
0093 end
0094 s_shape = split_var_strings(shape(2:end));
0095 shapes = shape_library('get',shape{1},s_shape(1,:));
0096 if ~iscell(shapes), shapes = {shapes}; end
0097
0098 for i = 1:numel(shapes)
0099 eval(sprintf('shapes{i} = %f*shapes{i}%s;',scale(1),s_shape{2,i}));
0100 end
0101 if ischar(elec_pos) && strcmp(elec_pos,'original')
0102 el = shape_library('get',shape{1},'electrodes');
0103 electh= atan2(el(:,2),el(:,1))*180/pi;
0104 elec_pos = [electh,0.5*ones(size(electh))];
0105 end
0106
0107 scaleH = prod(scale);
0108 if nfft > 0
0109 [fmdl, mat_idx] = ng_mk_extruded_model({scaleH,shapes,[4,nfft],maxsz},...
0110 elec_pos,elec_shape);
0111 else
0112 [fmdl, mat_idx] = ng_mk_extruded_model({scaleH,shapes,0,maxsz},...
0113 elec_pos,elec_shape);
0114 end
0115 fmdl.nodes = fmdl.nodes/scale(1);
0116 fmdl.mat_idx = mat_idx;
0117 store_model(fmdl,fname)
0118 out = fmdl;
0119 out = mdl_normalize(out, 'default');
0120 end
0121
0122
0123
0124
0125 function out = load_stored_model(fname)
0126 out = [];
0127 if eidors_debug('query','mk_library_model')
0128 return
0129 end
0130
0131
0132 if ng_write_opt('EXIST:ng.opt')
0133 eidors_msg('Custom ng.opt defined. Not using store',2);
0134 return
0135 end
0136 library_path = get_path();
0137 if isempty(library_path); return; end
0138
0139 fname = [library_path '/' fname '.mat'];
0140 if exist(fname,'file')
0141 eidors_msg('MK_LIBRARY_MODEL: Using stored model');
0142 load(fname);
0143 out = fmdl;
0144 end
0145
0146 function store_model(fmdl,fname)
0147 if ng_write_opt('EXIST:ng.opt')
0148 eidors_msg('Custom ng.opt defined. Not saving to store',2);
0149 return
0150 end
0151 library_path = get_path();
0152 if isempty(library_path); return; end
0153 fname = [library_path '/' fname '.mat'];
0154 if exist('OCTAVE_VERSION');
0155 savver = '-v7';
0156 else
0157 savver = '-v7.3';
0158 end
0159 save(fname,savver,'fmdl');
0160
0161 function out = build_if_needed(cmd,str)
0162 out = [];
0163 out = load_stored_model(str);
0164 if isempty(out)
0165 if ~iscell(cmd)
0166 cmd = {cmd};
0167 end
0168 for i = 1:length(cmd)
0169 if i ==1
0170 eval(['out = ' cmd{i} ';']);
0171 else
0172 eval(cmd{i});
0173 end
0174 end
0175 store_model(out,str);
0176 end
0177
0178
0179
0180 function out = list_predef_model_strings
0181 out = {
0182 'adult_male_16el';
0183 'adult_male_32el';
0184 'adult_male_16el_lungs';
0185 'adult_male_32el_lungs';
0186 'adult_male_2x16el';
0187 'adult_male_2x32el';
0188 'adult_male_2x16el_lungs';
0189 'adult_male_2x32el_lungs';
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199 'cylinder_16x1el_coarse';
0200 'cylinder_16x1el_fine';
0201 'cylinder_16x1el_vfine';
0202 'cylinder_16x2el_coarse';
0203 'cylinder_16x2el_fine';
0204 'cylinder_16x2el_vfine';
0205 'neonate_16el';
0206 'neonate_32el';
0207 'neonate_16el_lungs';
0208 'neonate_32el_lungs';
0209 'pig_23kg_16el';
0210 'pig_23kg_32el';
0211 'pig_23kg_16el_lungs';
0212 'pig_23kg_32el_lungs';
0213 'lamb_newborn_16el';
0214 'lamb_newborn_32el';
0215 'lamb_newborn_16el_lungs';
0216 'lamb_newborn_32el_lungs';
0217 'lamb_newborn_16el_organs';
0218 'lamb_newborn_32el_organs';
0219
0220 'horse_16el';
0221 'horse_32el';
0222 'horse_2x16el';
0223 'horse_16el_lungs';
0224 'horse_32el_lungs';
0225 'horse_2x16el_lungs';
0226 'beagle_16el';
0227 'beagle_32el';
0228 'beagle_16el_lungs';
0229 'beagle_32el_lungs';
0230 'beagle_16el_rectelec';
0231 'beagle_32el_rectelec';
0232 'beagle_16el_lungs_rectelec';
0233 'beagle_32el_lungs_rectelec';
0234 };
0235
0236
0237
0238 function out = predef_model(str)
0239 switch str
0240 case 'adult_male_16el'
0241 out = mk_library_model({'adult_male','boundary'},...
0242 [16 1 0.5],[0.05],0.08);
0243 case 'adult_male_2x16el'
0244 elayers = [0.65,0.35];
0245 out = mk_library_model({'adult_male','boundary'},...
0246 [16 1 elayers],[0.05],0.08);
0247 case 'adult_male_32el'
0248 out = mk_library_model({'adult_male','boundary'},...
0249 [32 1 0.5],[0.05],0.08);
0250 case 'adult_male_2x32el'
0251 elayers = [0.65,0.35];
0252 out = mk_library_model({'adult_male','boundary'},...
0253 [32 1 elayers],[0.05],0.08);
0254 case 'adult_male_16el_lungs'
0255 out = mk_library_model({'adult_male','boundary','left_lung','right_lung'},...
0256 [16 1 0.5],[0.05],0.08);
0257 case 'adult_male_2x16el_lungs'
0258 elayers = [0.65,0.35];
0259 out = mk_library_model({'adult_male','boundary','left_lung','right_lung'},...
0260 [16 1 elayers],[0.05],0.08);
0261 case 'adult_male_32el_lungs'
0262 out = mk_library_model({'adult_male','boundary','left_lung','right_lung'},...
0263 [32 1 0.5],[0.05],0.08);
0264 case 'adult_male_2x32el_lungs'
0265 elayers = [0.65,0.35];
0266 out = mk_library_model({'adult_male','boundary','left_lung','right_lung'},...
0267 [32 1 elayers],[0.05],0.08);
0268
0269 case {'adult_male_grychtol2016a_1x32'
0270 'adult_male_grychtol2016a_2x16'
0271 'adult_male_thorax_1x32]'
0272 'adult_male_thorax_1x16'
0273 'adult_male_thorax_2x16'
0274 'adult_female_thorax_1x32'
0275 'adult_female_thorax_1x16'
0276 'adult_female_thorax_2x16'}
0277 warning('EIDORS:DeprecatedInterface', ...
0278 ['MK_LIBRARY_MODEL(''%s'') is deprecated. '...
0279 'Use mk_thorax_model(''%s'') instead.'], str, str);
0280 out = mk_thorax_model(str);
0281 try out = out.fwd_model; end
0282
0283 case 'cylinder_16x1el_coarse'
0284 out = build_if_needed(...
0285 'ng_mk_cyl_models([10,15],[16,5],[0.5,0,0.18])', str);
0286 case 'cylinder_16x1el_fine'
0287 out = build_if_needed(...
0288 'ng_mk_cyl_models([10,15,1.1],[16,5],[0.5,0,0.15])',str);
0289 case 'cylinder_16x1el_vfine'
0290 out = build_if_needed(...
0291 'ng_mk_cyl_models([10,15,0.8],[16,5],[0.5,0,0.08])',str);
0292 case 'cylinder_16x2el_coarse'
0293 out = build_if_needed(...
0294 'ng_mk_cyl_models([30,15],[16,10,20],[0.5,0,0.18])',str);
0295 case 'cylinder_16x2el_fine'
0296 out = build_if_needed(...
0297 'ng_mk_cyl_models([30,15,1.5],[16,10,20],[0.5,0,0.15])',str);
0298 case 'cylinder_16x2el_vfine'
0299 out = build_if_needed(...
0300 'ng_mk_cyl_models([30,15,0.8],[16,10,20],[0.5,0,0.08])',str);
0301
0302
0303 case 'neonate_16el'
0304 out = mk_library_model({'neonate','boundary'},[16 1 0.5],[0.1 0 -1 0 60],0.08,49);
0305 case 'neonate_32el'
0306 out = mk_library_model({'neonate','boundary'},[32 1 0.5],[0.06 0 -1 0 60],0.08,49);
0307 case 'neonate_16el_lungs'
0308 out = mk_library_model({'neonate','boundary','left_lung','right_lung'},[16 1 0.5],[0.1 0 -1 0 60],0.08,49);
0309 case 'neonate_32el_lungs'
0310 out = mk_library_model({'neonate','boundary','left_lung','right_lung'},[32 1 0.5],[0.06 0 -1 0 60],0.08,49);
0311
0312 case 'pig_23kg_16el'
0313 out = mk_library_model({'pig_23kg','boundary'},...
0314 [16 1 0.5],[0.05 0 -1 0 60],0.08);
0315 case 'pig_23kg_32el'
0316 out = mk_library_model({'pig_23kg','boundary'},...
0317 [32 1 0.5],[0.05 0 -1 0 60],0.08);
0318 case 'pig_23kg_16el_lungs'
0319 out = mk_library_model({'pig_23kg','boundary','lungs(1:2:end,:)'},...
0320 [16 1 0.5],[0.05 0 -1 0 60],0.08);
0321 case 'pig_23kg_32el_lungs'
0322 out = mk_library_model({'pig_23kg','boundary','lungs(1:2:end,:)'},...
0323 [32 1 0.5],[0.05 0 -1 0 60],0.08);
0324
0325 case 'lamb_newborn_16el'
0326
0327
0328
0329
0330
0331 out = mk_library_model({'lamb_newborn','boundary'},[16,1.995,104],[1],10,0,208);
0332 case 'lamb_newborn_32el'
0333
0334 out = mk_library_model({'lamb_newborn','boundary'},[32,1.980,104],[1],15,50,208);
0335 out.electrode = out.electrode([2:32,1]);
0336 case 'lamb_newborn_16el_organs'
0337 out = mk_library_model({'lamb_newborn','boundary','lungs','heart'},[16,1.995,104],[1],10,0,208);
0338 case 'lamb_newborn_16el_lungs'
0339 out = mk_library_model({'lamb_newborn','boundary','lungs'},[16,1.995,104],[1],10,0,208);
0340 case 'lamb_newborn_32el_lungs'
0341
0342 out = mk_library_model({'lamb_newborn','boundary','lungs'},[32,1.980,104],[1],15,50,208);
0343 out.electrode = out.electrode([2:32,1]);
0344 case 'lamb_newborn_32el_organs'
0345
0346 out = mk_library_model({'lamb_newborn','boundary','lungs','heart'},[32,1.980,104],[1],15,50,208);
0347 out.electrode = out.electrode([2:32,1]);
0348
0349 case 'horse_16el';
0350
0351 scale = [100,.35 ]; Nelec=16;
0352 elhig = prod(scale)/2;
0353 out = mk_library_model({'horse','boundary'},[Nelec,1.51,elhig],[2,6,0.5],2,50,scale);
0354 case 'horse_32el';
0355 scale = [100,.35 ]; Nelec=32;
0356 elhig = prod(scale)/2;
0357 out = mk_library_model({'horse','boundary'},[Nelec,1.51,elhig],[2,6,0.5],2,50,scale);
0358 case 'horse_2x16el';
0359 scale = [100,.35 ]; Nelec=16;
0360 elhig = [5,3]*prod(scale)/8;
0361 out = mk_library_model({'horse','boundary'},[Nelec,1.51,elhig],[2,3,0.5],2,50,scale);
0362 case 'horse_16el_lungs';
0363 scale = [100,.5 ]; Nelec=16;
0364 elhig = prod(scale)/2;
0365 out = mk_library_model({'horse','boundary','left_lung','right_lung','heart'},[Nelec,1.51,elhig],[2,6,0.5],2,50,scale);
0366 case 'horse_32el_lungs';
0367 scale = [100,.35 ]; Nelec=32;
0368 elhig = prod(scale)/2;
0369 out = mk_library_model({'horse','boundary','left_lung','right_lung','heart'},[Nelec,1.51,elhig],[2,6,0.5],2,50,scale);
0370 case 'horse_2x16el_lungs';
0371 scale = [100,.5 ]; Nelec=16;
0372 elhig = [5,3]*prod(scale)/8;
0373 out = mk_library_model({'horse','boundary','left_lung','right_lung','heart'},[Nelec,1.51,elhig],[2,3,0.5],2,50,scale);
0374 case 'beagle_16el';
0375 scale = 49;
0376 out = mk_library_model({'beagle','boundary'}, ...
0377 [16 1 scale*0.5],[2,0,0.10],10,0,49);
0378 case 'beagle_32el';
0379 scale = 49;
0380 out = mk_library_model({'beagle','boundary'}, ...
0381 [32 1 scale*0.5],[2,0,0.10],10,0,49);
0382 case 'beagle_16el_rectelec';
0383 scale = 49;
0384 out = mk_library_model({'beagle','boundary'}, ...
0385 [16 1 scale*0.5],8*[0.25,1,0.05],10,0,49);
0386 case 'beagle_32el_rectelec';
0387 scale = 49;
0388 out = mk_library_model({'beagle','boundary'}, ...
0389 [16 1 scale*0.5],8*[0.25,1,0.05],10,0,49);
0390
0391 case 'beagle_16el_lungs';
0392 scale = 49;
0393 out = mk_library_model({'beagle','boundary','left_lung','right_lung'}, ...
0394 [16 1 scale*0.5],[2,0,0.10],10,0,49);
0395
0396 case 'beagle_16el_lungs_rectelec';
0397 scale = 49;
0398 out = mk_library_model({'beagle','boundary','left_lung','right_lung'}, ...
0399 [16 1 scale*0.5],8*[0.25,1,0.05],10,0,49);
0400
0401 case 'beagle_32el_lungs';
0402 scale = 49;
0403 out = mk_library_model({'beagle','boundary','left_lung','right_lung'}, ...
0404 [32 1 scale*0.5],[1.5,0,0.10],10,0,49);
0405
0406 case 'beagle_32el_lungs_rectelec';
0407 scale = 49;
0408 out = mk_library_model({'beagle','boundary','left_lung','right_lung'}, ...
0409 [32 1 scale*0.5],8*[0.25,1,0.05],10,0,49);
0410
0411 otherwise
0412 error('No such model');
0413 end
0414
0415 out.name = str;
0416
0417
0418 function str = make_filename(shape, elec_pos, elec_shape, ...
0419 maxsz, nfft, scale);
0420
0421 str = shape{1};
0422 shape(1) = [];
0423 shape = sort(shape);
0424 for i = 1:numel(shape)
0425 str = [str '_' shape{i}];
0426 end
0427 str = [str '_EP'];
0428 for i = 1:numel(elec_pos)
0429 str = [str '_' num2str(elec_pos(i))];
0430 end
0431 str = [str '_ES'];
0432 if ischar(elec_shape)
0433 str = [str '_' elec_shape];
0434 else
0435 for i = 1:numel(elec_shape)
0436 str = [str '_' num2str(elec_shape(i))];
0437 end
0438 end
0439 if ~isempty(maxsz)
0440 str = [str '_maxsz_' num2str(maxsz)];
0441 end
0442 if ~isempty(nfft)
0443 str = [str '_nfft_' num2str(nfft)];
0444 end
0445 if ~isempty(scale)
0446 str = [str '_scale' sprintf('%f_',scale)];
0447 end
0448
0449
0450 str = strrep(str,':','-');
0451
0452 function clean = split_var_strings(strc)
0453 for i = 1:numel(strc)
0454 [clean{1,i} clean{2,i}] = strtok(strc{i},'([{');
0455 end
0456
0457
0458 function out = get_path
0459 global eidors_objects
0460 out = eidors_objects.model_cache;
0461
0462 function set_path(val)
0463 global eidors_objects
0464 eidors_objects.model_cache = val;
0465 if isempty(val); return; end
0466
0467 if ~exist(val,'dir')
0468 ver= eidors_obj('interpreter_version');
0469 if ver.ver<4 || ver.ver>=7
0470 mkdir(val);
0471 else
0472
0473 system(['mkdir ',val]);
0474 end
0475 end
0476
0477 function out = do_unit_test
0478 models = mk_library_model('list');
0479 deprecated = {'adult_male_grychtol2016a_1x32'
0480 'adult_male_grychtol2016a_2x16'
0481 'adult_male_thorax_1x32]'
0482 'adult_male_thorax_1x16'
0483 'adult_male_thorax_2x16'
0484 'adult_female_thorax_1x32'
0485 'adult_female_thorax_1x16'
0486 'adult_female_thorax_2x16'};
0487 if exist('OCTAVE_VERSION');
0488 deprecated={};
0489 end
0490 models = vertcat(deprecated, models);
0491 n_models = numel(models);
0492 sqrt_n_models = ceil(sqrt(n_models));
0493 error_list = {};
0494 for i = 1:numel(models)
0495 eidors_msg('\n\n\n DOING MODEL (%s)\n\n\n',models{i},0);
0496 try
0497 mdl = mk_library_model(models{i});
0498 catch
0499 error_list = [error_list, models(i)];
0500 continue;
0501 end
0502 img = mk_image(mdl,1);
0503 try
0504 n = numel(mdl.mat_idx);
0505 catch
0506 n =1;
0507 end
0508 if n >1
0509 for j = 2:n
0510 img.elem_data(mdl.mat_idx{j}) = 0.25;
0511 end
0512 end
0513 subplot(sqrt_n_models, sqrt_n_models,i);
0514 show_fem(img,[0,1,0]); axis off;
0515 title(models{i},'Interpreter','none');
0516 drawnow
0517 end
0518
0519 if ~isempty(error_list)
0520 disp('There were errors in generating the following models:')
0521 disp(error_list);
0522 end
0523
0524 out = mk_library_model('pig_23kg_16el');
0525 unit_test_cmp('pig_23kg_16el', max(out.nodes),[1.004200000000000,0.998200000000000,1.000000000000000],1e-10);
0526
0527 out = mk_library_model('pig_23kg_16el:ctr');
0528 unit_test_cmp('pig_23kg_16el:ctr', max(out.nodes),[0.995060653391834,1.064458637676478,0.501929982309894],1e-10);
0529
0530 out = mk_library_model('pig_23kg_16el:hctr');
0531 unit_test_cmp('pig_23kg_16el:hctr', max(out.nodes),[0.995060653391834,1.064458637676478,1.000000000000000],1e-10);
0532
0533 out = mk_library_model('pig_23kg_16el:vctr');
0534 unit_test_cmp('pig_23kg_16el:vctr', max(out.nodes),[1.004200000000000,0.998200000000000,0.501929982309894],1e-10);
0535
0536 out = mk_library_model({'pig_23kg','boundary','lungs(1:2:end,:)'},[32 1 0.5],[0.05 0 -1 0 60],0.08);
0537 unit_test_cmp('pig_23kg', max(out.nodes),[1.004200000000000 0.998200000000000 1.000000000000000],1e-10);
0538