0001 function [varargout] = pa_leadfield_util(fig, command, parm)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021 if ~exist('fig', 'var') || isempty(fig) || ~ishandle(fig)
0022 error('invalid figure was specified.');
0023 end
0024 if ~exist('command', 'var') || isempty(command)
0025 error('invalid command was specified.');
0026 end
0027
0028
0029
0030
0031
0032
0033 data = guidata(fig);
0034
0035 switch(command)
0036 case 'update_output_fname'
0037 update_output_fname(data.H);
0038 case 'update_exec_push_status'
0039 update_exec_push_status(data.H);
0040 case 'set_basis_mode'
0041 set_basis_mode(parm, data.H);
0042 case 'get_basis_mode'
0043
0044 varargout{1} = get_basis_mode(data.H);
0045 case 'set_bem_mode'
0046 set_bem_mode(parm, data.H);
0047 case 'get_bem_mode'
0048
0049 varargout{1} = get_bem_mode(data.H);
0050 case 'set_normal_mode'
0051 set_normal_mode(parm, data.H);
0052 case 'get_normal_mode'
0053
0054 varargout{1} = get_normal_mode(data.H);
0055 case 'exec_leadfield'
0056 exec_leadfield(data);
0057 case 'reset_parameter'
0058 reset_parameter(data.H);
0059 case 'check_model'
0060 check_model(data.H);
0061 end
0062
0063
0064 guidata(fig, data);
0065
0066 function update_output_fname(H)
0067
0068
0069
0070
0071
0072
0073 global vbmeg_inst;
0074 define = vbmeg_inst.const;
0075
0076 save_dir = get(H.save_dir_edit, 'String');
0077
0078 megmat_file = get(H.megmat_file_edit, 'String');
0079
0080 [fpath, fname, fext] = vb_get_file_parts(megmat_file);
0081
0082 base_name = [fname, fext];
0083 dot_ix = strfind(base_name, '.');
0084 if ~isempty(dot_ix)
0085 base_name = base_name(1:dot_ix(end-1)-1);
0086 end
0087
0088
0089 basis_file = [save_dir, filesep, base_name, define.BASIS_EXTENSION];
0090
0091 set(H.basis_file_edit, 'String', basis_file);
0092
0093 function update_exec_push_status(H)
0094
0095
0096
0097
0098
0099
0100 global vbmeg_inst;
0101 define = vbmeg_inst.const;
0102
0103 if ~isempty(get(H.megmat_file_edit, 'String')) & ...
0104 ~isempty(get(H.brain_file_edit, 'String')) & ...
0105 ~isempty(get(H.save_dir_edit, 'String')) & ...
0106 ~isempty(get(H.basis_file_edit, 'String'))
0107
0108
0109 mode = get_bem_mode(H);
0110 if (mode == define.BASIS_MEG_BEM) || ...
0111 (mode == define.BASIS_EEG_BEM) || ...
0112 (mode == define.BASIS_MEG_HARMONICS)
0113 if isempty(get(H.head_file_edit, 'String'))
0114 set(H.exec_push, 'Enable', 'off');
0115 return;
0116 end
0117 end
0118
0119
0120 if (mode == define.BASIS_MEG_SPHERE) || ...
0121 (mode == define.BASIS_EEG_SPHERE)
0122 if isempty(get(H.center_head_edit, 'String'))
0123 set(H.exec_push, 'Enable', 'off');
0124 return;
0125 end
0126 end
0127
0128
0129 if (mode == define.BASIS_EEG_SPHERE)
0130 if isempty(get(H.sphere_radius_edit, 'String'))
0131 set(H.exec_push, 'Enable', 'off');
0132 return;
0133 end
0134 end
0135
0136
0137 if (mode == define.BASIS_EEG_SPHERE) || ...
0138 (mode == define.BASIS_EEG_BEM)
0139 if isempty(get(H.conductivity_edit, 'String'))
0140 set(H.exec_push, 'Enable', 'off');
0141 return;
0142 end
0143 end
0144
0145 set(H.exec_push, 'Enable', 'on');
0146 else
0147 set(H.exec_push, 'Enable', 'off');
0148 end
0149
0150 function set_basis_mode(mode, H)
0151
0152
0153
0154
0155
0156
0157
0158
0159 global vbmeg_inst;
0160 define = vbmeg_inst.const;
0161
0162 if mode == define.CURRENT_NORMAL
0163 value = 1;
0164 elseif mode == define.CURRENT_TANGENT
0165 value = 2;
0166 elseif mode == define.CURRENT_3D
0167 value = 3;
0168 else
0169 disp('Illigal usage:set_basis_mode().');
0170 end
0171
0172 set(H.BasisMode_popup, 'Value', value);
0173
0174 function mode = get_basis_mode(H)
0175
0176
0177
0178
0179
0180
0181
0182 global vbmeg_inst;
0183 define = vbmeg_inst.const;
0184
0185 value = get(H.BasisMode_popup, 'Value');
0186 if value == 1
0187 mode = define.CURRENT_NORMAL;
0188 elseif value == 2
0189 mode = define.CURRENT_TANGENT;
0190 elseif value == 3
0191 mode = define.CURRENT_3D;
0192 else
0193 disp('Invalid Basis mode!!');
0194 end
0195
0196 function set_bem_mode(mode, H)
0197
0198
0199
0200
0201
0202
0203
0204
0205 global vbmeg_inst;
0206 define = vbmeg_inst.const;
0207
0208 gui_set_head_center(H, 'off');
0209 gui_set_headfile(H, 'off');
0210 gui_set_conductivity(H, 'off');
0211 gui_set_sphere_radius(H, 'off');
0212
0213 if mode == define.BASIS_MEG_SPHERE
0214 value = 1;
0215 gui_set_head_center(H, 'on');
0216 elseif mode == define.BASIS_MEG_BEM
0217 value = 2;
0218 gui_set_headfile(H, 'on');
0219 elseif mode == define.BASIS_MEG_HARMONICS
0220 value = 3;
0221 gui_set_headfile(H, 'on');
0222 elseif mode == define.BASIS_EEG_SPHERE
0223 value = 4;
0224 gui_set_head_center(H, 'on');
0225 gui_set_sphere_radius(H, 'on');
0226 gui_set_conductivity(H, 'on');
0227 elseif mode == define.BASIS_EEG_BEM
0228 value = 5;
0229 gui_set_headfile(H, 'on');
0230 gui_set_conductivity(H, 'on');
0231 else
0232 disp('Illigal usage : set_bem_mode().');
0233 end
0234 set(H.BemMode_popup, 'Value', value);
0235
0236 function gui_set_sphere_radius(H, on_off)
0237 set(H.sphere_radius_label, 'Enable', on_off);
0238 set(H.sphere_radius_edit, 'Enable', on_off);
0239
0240 function gui_set_head_center(H, on_off)
0241 set(H.center_head_label, 'Enable', on_off);
0242 set(H.center_head_edit, 'Enable', on_off);
0243 set(H.center_head_push, 'Enable', on_off);
0244
0245 function gui_set_conductivity(H, on_off)
0246 set(H.conductivity_label, 'Enable', on_off);
0247 set(H.conductivity_edit, 'Enable', on_off);
0248
0249 function gui_set_headfile(H, on_off)
0250 set(H.headfile_label, 'Enable', on_off);
0251 set(H.head_file_edit, 'Enable', on_off);
0252 set(H.head_file_push, 'Enable', on_off);
0253
0254 function mode = get_bem_mode(H)
0255
0256
0257
0258
0259
0260
0261
0262 global vbmeg_inst;
0263 define = vbmeg_inst.const;
0264
0265 value = get(H.BemMode_popup, 'Value');
0266
0267 if value == 1
0268 mode = define.BASIS_MEG_SPHERE;
0269 elseif value == 2
0270 mode = define.BASIS_MEG_BEM;
0271 elseif value == 3
0272 mode = define.BASIS_MEG_HARMONICS;
0273 elseif value == 4
0274 mode = define.BASIS_EEG_SPHERE;
0275 elseif value == 5
0276 mode = define.BASIS_EEG_BEM;
0277 else
0278 disp('Invalid bem mode!!');
0279 end
0280
0281 function set_normal_mode(mode, H)
0282
0283
0284
0285
0286
0287
0288
0289
0290 global vbmeg_inst;
0291 define = vbmeg_inst.const;
0292
0293 if mode == define.NORMAL_ADJACENT_MEAN
0294 value = 1;
0295 elseif mode == define.NORMAL_NEIGHBOR_MEAN
0296 value = 2;
0297 else
0298 disp('Illigal usage : set_normal_mode().');
0299 end
0300
0301 set(H.NormalMode_popup, 'Value', value);
0302
0303 function mode = get_normal_mode(H)
0304
0305
0306
0307
0308
0309
0310
0311 global vbmeg_inst;
0312 define = vbmeg_inst.const;
0313
0314 value = get(H.NormalMode_popup, 'Value');
0315
0316 if value == 1
0317 mode = define.NORMAL_ADJACENT_MEAN;
0318 elseif value == 2
0319 mode = define.NORMAL_NEIGHBOR_MEAN;
0320 else
0321 disp('Illigal usage : get_normal_mode().');
0322 end
0323
0324 function exec_leadfield(data)
0325
0326
0327
0328
0329
0330
0331 H = data.H;
0332
0333
0334
0335
0336
0337 proj_root = get(H.prjroot_edit, 'String');
0338
0339
0340 basis_parm = data.basis_parm;
0341
0342
0343 basis_parm.meg_file = get(H.megmat_file_edit, 'String');
0344
0345
0346 basis_parm.brain_file = get(H.brain_file_edit, 'String');
0347
0348
0349 basis_parm.area_file = get(H.area_file_edit, 'String');
0350
0351
0352 basis_parm.head_file = get(H.head_file_edit, 'String');
0353
0354
0355 basis_parm.device = [];
0356
0357
0358 basis_parm.Vcenter = eval([ '[', get(H.center_head_edit, 'String'), ']' ]);
0359
0360
0361
0362
0363
0364
0365
0366
0367 basis_parm.bem_mode = get_bem_mode(H);
0368
0369
0370
0371
0372
0373 basis_parm.Basis_mode = get_basis_mode(H);
0374
0375
0376
0377
0378
0379 basis_parm.sigma = eval([ '[', get(H.conductivity_edit, 'String'), ']' ]);
0380
0381
0382 basis_parm.radius = eval([ '[', get(H.sphere_radius_edit, 'String'), ']' ]);
0383
0384
0385 basis_parm.basis_file = get(H.basis_file_edit, 'String');
0386
0387
0388
0389
0390 basis_parm.area_key = get(H.areakey_edit, 'String');
0391
0392 err_code = vb_basisparm_check(basis_parm, proj_root);
0393
0394 if err_code == 0
0395
0396
0397
0398 str = get(H.exec_push, 'String');
0399 set(H.exec_push, 'String', 'Processing...');
0400 pause(0.01);
0401 vb_job_leadfield(proj_root, basis_parm);
0402 set(H.exec_push, 'String', 'Exec');
0403 else
0404
0405 err_table = ...
0406 {1, 'basis_parm is empty.';
0407 2, 'Project root directory is not specified.';
0408 4, 'Cortical model file is invalid.';
0409 8, 'MEG/EEG file is invalid.';
0410 16, 'Normal mode is invalid.';
0411 32, 'Basis mode is invalid.';
0412 64, 'Cortical area file or AreaID is invalid. ';
0413 128,sprintf('%s\n%s', 'Coordinate type of sensor is invalid.', ...
0414 'Did you specify POS-MAT file when MEG/EEG file was made?')};
0415 for k=1:size(err_table)
0416 if bitand(err_code, err_table{k, 1})
0417 errordlg(err_table{k, 2}, 'error');
0418 return;
0419 end
0420 end
0421 end
0422
0423 function reset_parameter(H)
0424
0425
0426
0427
0428
0429
0430 parm = vb_set_basis_parm;
0431 pa_leadfield_set_parm(H.figure, parm);
0432
0433
0434
0435
0436
0437
0438
0439
0440
0441
0442
0443
0444 function check_model(H)
0445
0446
0447
0448
0449
0450
0451 global vbmeg_inst;
0452 define = vbmeg_inst.const;
0453
0454
0455 head_color = [0.9 0.8 0.8; 0.9 0.9 1.0; 1.0 0.8 0.8; ...
0456 1.0 0.8 0.8; 1.0 0.8 0.8; 1.0 0.8 0.8];
0457 brain_color = [0.8 0.8 0.8];
0458 falpha = [0.4 0.3 0.2 0.2];
0459
0460
0461 proj_root = get(H.prjroot_edit, 'String');
0462
0463
0464 meg_file = get(H.megmat_file_edit, 'String');
0465
0466
0467 brain_file = get(H.brain_file_edit, 'String');
0468
0469
0470 head_file = get(H.head_file_edit, 'String');
0471
0472
0473 bem_mode = get_bem_mode(H);
0474
0475 if isempty(proj_root), return; end
0476
0477 flag = 0;
0478
0479 if ~isempty(brain_file)
0480 h = figure;
0481 set(h, 'Menubar', 'none');
0482 set(h, 'Name', 'Head model/Sensor location', 'NumberTitle', 'off');
0483 set(h, 'Unit', 'normalized', 'Position', [0.3, 0.3, 0.5, 0.5]);
0484 hold on; flag = 1;
0485 V = vb_load_cortex([proj_root '/' brain_file], 'subj');
0486 plot3(V(:,1), V(:,2), V(:,3), 'b.');
0487 rotate3d(h, 'on');
0488 set(gca, 'Color', 'k');
0489 axis equal;
0490
0491 end
0492
0493
0494 if ~isempty(meg_file)
0495 if ~flag, figure; hold on; flag = 1; end
0496 pick = vb_load_sensor([proj_root '/' meg_file]);
0497 if ~isempty(pick)
0498 plot3(pick(:,1),pick(:,2),pick(:,3),'bo');
0499 end
0500 axis equal;
0501 end
0502
0503 if bem_mode == define.BASIS_MEG_SPHERE
0504
0505 elseif bem_mode == define.BASIS_EEG_SPHERE
0506
0507 elseif ~isempty(head_file)
0508 if ~flag, figure; hold on; flag = 1; end
0509 head_file = [proj_root '/' head_file];
0510 Nlayer = vb_head_get_layer_num(head_file);
0511
0512 for l=1:Nlayer
0513 headinfo = vb_head_get_specified_headinfo(head_file,l);
0514 patch('Faces',headinfo.Fhead,'Vertices',headinfo.Vhead,...
0515 'FaceColor',head_color(l,:),'EdgeColor','none',...
0516 'FaceAlpha',falpha(l));
0517 end
0518 end
0519
0520 if flag==1,
0521 axis equal;
0522 axis tight;
0523 xlabel('X [m]');
0524 ylabel('Y [m]');
0525 zlabel('Z [m]');
0526 end