Home > vbmeg > functions > gui > preAnalysis > leadfield > pa_leadfield_util.m

pa_leadfield_util

PURPOSE ^

This function is subcontracting function of leadfield GUI.

SYNOPSIS ^

function [varargout] = pa_leadfield_util(fig, command, parm)

DESCRIPTION ^

 This function is subcontracting function of leadfield GUI.
 [USAGE]
    [varargout] = pa_leadfield_util(<fig>, <command> [,parm]);
 [IN]
        fig : figure handle of leadfield gui.
    command : utility type
       parm : other parameters
 [OUT]
    varargout{n}: return value
           @command : 'get_basis_mode'  ==> BASIS_MODE
           @command : 'get_bem_mode'    ==> BEM_MODE
           @command : 'get_normal_mode' ==> NORMAL_MODE

 Copyright (C) 2011, ATR All Rights Reserved.
 License : New BSD License(see VBMEG_LICENSE.txt)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [varargout] = pa_leadfield_util(fig, command, parm)
0002 % This function is subcontracting function of leadfield GUI.
0003 % [USAGE]
0004 %    [varargout] = pa_leadfield_util(<fig>, <command> [,parm]);
0005 % [IN]
0006 %        fig : figure handle of leadfield gui.
0007 %    command : utility type
0008 %       parm : other parameters
0009 % [OUT]
0010 %    varargout{n}: return value
0011 %           @command : 'get_basis_mode'  ==> BASIS_MODE
0012 %           @command : 'get_bem_mode'    ==> BEM_MODE
0013 %           @command : 'get_normal_mode' ==> NORMAL_MODE
0014 %
0015 % Copyright (C) 2011, ATR All Rights Reserved.
0016 % License : New BSD License(see VBMEG_LICENSE.txt)
0017 
0018 %
0019 % --- Previous check
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 % --- Main Procedure
0030 %
0031 
0032 % load data(@see pa_leadfield_create)
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         % return const basis mode number
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         % return const bem mode number
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         % return const normal mode number
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 % save data
0064 guidata(fig, data);
0065 
0066 function update_output_fname(H)
0067 % This function makes output filename and sets filename to GUI.
0068 % [IN]
0069 %    H : GUI component handles
0070 % [OUT]
0071 %    none
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     %make basis filename
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 % This function changes Exec button status.(clickable or not)
0095 % [IN]
0096 %    H : GUI component handles
0097 % [OUT]
0098 %    none
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         % Head file check.
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         % Head center check.
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         % Sphere radius check.
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         % Conductivity check
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 % This function sets GUI basis mode.
0152 % (basis_mode ---> GUI popup value)
0153 % [IN]
0154 %    mode : basis mode
0155 %       H : GUI component handles
0156 % [OUT]
0157 %    none
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 % This function returns basis mode.
0176 % (GUI popup value ---> basis_mode)
0177 % [IN]
0178 %    H : GUI component handles
0179 % [OUT]
0180 %    mode : basis mode
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 % This function sets GUI bem mode.
0198 % (bem_mode ---> GUI popup value)
0199 % [IN]
0200 %    mode : bem mode
0201 %       H : GUI component handles
0202 % [OUT]
0203 %    none
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 % This function returns bem mode.
0256 % (GUI popup value ---> bem_mode)
0257 % [IN]
0258 %    H : GUI component handles
0259 % [OUT]
0260 %    mode : bem mode
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 % This function sets GUI normal mode.
0283 % (normal_mode ---> GUI popup value)
0284 % [IN]
0285 %    mode : normal mode
0286 %       H : GUI component handles
0287 % [OUT]
0288 %    none
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 % This function returns normal_mode by constant value.
0305 % (GUI popup value ---> normal_mode)
0306 % [IN]
0307 %    H : GUI component handles
0308 % [OUT]
0309 %    mode : normal mode
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 % This function execute vb_job_leadfield with set parameter.
0326 % [IN]
0327 %    data : data of leafield gui(@see pa_leadfield_create)
0328 % [OUT]
0329 %    none
0330 
0331     H = data.H; % component handles
0332     
0333     %%%%%%%%%%%%%%%%%%%%
0334     % Parameter setting
0335     %%%%%%%%%%%%%%%%%%%%
0336     % project root directory
0337     proj_root = get(H.prjroot_edit, 'String');
0338 
0339     % advanced parameters are set here.
0340     basis_parm = data.basis_parm;
0341 
0342     % MEG-MAT file
0343     basis_parm.meg_file   = get(H.megmat_file_edit, 'String');
0344 
0345     % BRAIN file
0346     basis_parm.brain_file = get(H.brain_file_edit, 'String');
0347     
0348     % AREA file
0349     basis_parm.area_file  = get(H.area_file_edit, 'String');
0350 
0351     % HEAD file
0352     basis_parm.head_file  = get(H.head_file_edit, 'String');
0353 
0354     % MEG/EEG device name
0355     basis_parm.device     = [];
0356 
0357     % Center of the head
0358     basis_parm.Vcenter    = eval([ '[', get(H.center_head_edit, 'String'), ']' ]);
0359     
0360     % bem_mode: LFv@
0361     %  MEG
0362     %      BASIS_MEG_SPHERE    : MEG f  (Sarvas
0363     %      BASIS_MEG_BEM       : MEG |eVAK[L@Ad    %      BASIS_MEG_HARMONICS : MEG WJ
0364     %  EEG
0365     %      BASIS_EEG_SPHERE    : EEG 3wf
0366     %      BASIS_EEG_BEM       : EEG |eVAK[L@
0367     basis_parm.bem_mode   = get_bem_mode(H);
0368 
0369     % Basis_mode : number of independent curent direction
0370     %       CURRENT_NORMAL  :  current vextor = xx(n,:)
0371     %       CURRENT_TANGENT :  current vextor = orthogonal vectors to V(n,:)
0372     %       CURRENT_3D      :  current vextor = x, y, z -direction
0373     basis_parm.Basis_mode = get_basis_mode(H);
0374 
0375     % normal_mode: Average mode for normal direction
0376     %       NORMAL_ADJACENT_MEAN  = 0;  % _@    %       NORMAL_NEIGHBOR_MEAN  = 1;  % BV]fT_@    basis_parm.normal_mode = get_normal_mode(H);
0377 
0378     % Conductivity
0379     basis_parm.sigma = eval([ '[', get(H.conductivity_edit, 'String'), ']' ]);
0380     
0381     % Sphere Radius
0382     basis_parm.radius = eval([ '[', get(H.sphere_radius_edit, 'String'), ']' ]);
0383 
0384     % BASIS file(output)
0385     basis_parm.basis_file = get(H.basis_file_edit, 'String');
0386 
0387     %
0388     % --- Advanced parameters
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         % execute job
0397         %%%%%%%%%%%%%%%%%%%%
0398         str = get(H.exec_push, 'String');
0399         set(H.exec_push, 'String', 'Processing...');
0400         pause(0.01);%for label string update
0401         vb_job_leadfield(proj_root, basis_parm);
0402         set(H.exec_push, 'String', 'Exec');
0403     else
0404         % Show error reason.
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 % This function resets brain GUI.
0425 % [IN]
0426 %    H : GUI component handles
0427 % [OUT]
0428 %    none
0429 
0430     parm = vb_set_basis_parm;
0431     pa_leadfield_set_parm(H.figure, parm);
0432 %     set(H.megmat_file_edit ,'String', '');
0433 %     set(H.brain_file_edit ,'String', '');
0434 %     set(H.area_file_edit ,'String', '');
0435 %     set(H.head_file_edit ,'String', '');
0436 %     set(H.areakey_edit, 'String', '');
0437 %     set(H.save_dir_edit ,'String', '');
0438 %     set(H.basis_file_edit ,'String', '');
0439 %
0440 %     set(H.BemMode_popup, 'Value', 1);
0441 %     set(H.BasisMode_popup, 'Value', 1);
0442 %     set(H.NormalMode_popup, 'Value', 2);
0443     
0444 function check_model(H)
0445 % This function display forward model.
0446 % [IN]
0447 %    H:
0448 % [OUT]
0449 %    none
0450 
0451 global vbmeg_inst;
0452 define = vbmeg_inst.const;
0453 
0454 % move to some default parameter set in future
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 % Project root directory
0461 proj_root = get(H.prjroot_edit, 'String');
0462 
0463 % MEG-MAT file
0464 meg_file   = get(H.megmat_file_edit, 'String');
0465 
0466 % BRAIN file
0467 brain_file = get(H.brain_file_edit, 'String');
0468     
0469 % HEAD file
0470 head_file  = get(H.head_file_edit, 'String');
0471 
0472 % Leadfield calculation method
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 % Plot sensor
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   % MEG sphere
0505 elseif bem_mode == define.BASIS_EEG_SPHERE
0506   % EEG sphere
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

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005