Current reconstruction using Bayesian inverse filter. [syntax] [Zact,Jinfo,bayes_parm,vb_parm,MEGinfo,Jext,Pointlist] ... = vbmeg_current_reconstruct_z(proj_root, curr_parm) [input] proj_root: <<string>> VBMEG project root directory. curr_parm: <<struct>> Parameters for current estimation. --- fields of curr_parm bayesfile : <<string>> Model parameter file (.bayes.mat). currfile : <<string>> Cortical current file (.curr.mat), created by this function. trial_average: <optional> <<bool>> If true, = [ON] : average current over all sessions = OFF : current for each session ix_area : <optional> Vertex indices to calculate estimated current. If 'ix_area' is empty or not given, cortical currents in the active region are calculated. tsubsmpl : <optional> <bosolete> Specify subsampled time index. If 'tsubsmpl' is empty or not given, time subsampling is not done. dsampf : <optional> <<int>> Specify frequency of downsampling. This value must be smaller than the original sampling frequency of M/EEG data. overlap_mode : <optional> <<int>> = 0 : current is averaged over overlapped time window = 1 : current is not averaged for overlapped window current time series of each time windows are concatenated sequentially for spectral analysis verbose : <<bool>> Verbose flag. --- [note] If following field is given, these values are used instead of bayes_parm field in result file: --- curr_parm.basisfile curr_parm.megfile curr_parm.twin_meg curr_parm.Tperiod curr_parm.Tnext curr_parm.extra.basisfile (for extra dipole) --- [output] Zact : active current Zact(n,t,:) is the current at the vertex 'ix_act(n)' & the time 't' Zact(Nact,Nsample) for trial_average = ON Zact(Nact,Nsample,Ntrials) for trial_average = OFF Nact : # of active region, Nsample : # of time sample, Ntrials : # of trials in all session] --- fields of Jinfo version : <<string>> Version of cortical current file. curr_type : <<string>> 'Z-current'. It can be 'J-current' for VBMEG version 0.8 or older. Wact : <<float matrix>> Smoothing Gaussian filter, mapping from Z-current to J-current. ix_act : <<int vector>>: Vertex indices of Z-current. ix_act_ex : <<int vector>>: Vertex indices of J-current. Lact : <<int>> Number of current direction at one vertex. Tsample : <<int vector>> Time sample indices of the original MEG data. length(Tsample) == size(Zact,2) == size(Jact,2). Tmsec : <<float vector>> Time in msec. SampleFreq: <<float>> Sample frequency of cortical current, not original M/EEG signal [Hz]. Pretrigger: <<int>> Time points of the length of the pretrigger period of cortical current data. It is neither actual time nor time points of the original M/EEG signal. Ntrial : <<int>> Number of trials of estimated current. patch_norm: <<bool>> Cortical current is patch size normalized (Am/m^2) or not (Am). Tix : <<L x 1 cell>> Time sample indices of each time window. Zact(:,Tix{n},:) is the set of Z-current within the n-th time window. --- * Inverse filter calculation is done in vb_invfilter_calc [history] 2006-09-03 M. Sato * Non-overlapped concatenation mode is added for spectral analysis 2008-08-19 Taku Yoshioka Extra dipole support 2008-09-30 Taku Yoshioka Minor change for variables in Jinfo 2008-10-23 Taku Yoshioka Bug fix for current estimation without extra dipoles 2009-04-02 Taku Yoshioka Parameter name changed within this code for readability (just replacing 'resultfile' to bayesfile) 2010-03-01 M. Sato Bug fix for Wact index and fieldname(tsubsamp->tsubsmpl) 2010-12-06 taku-y [enhancement] curr_parm.dsampf supported. [minor] Following fields of Jinfo set in this function: SampleFreq Pretrigger Tmsec [trivial] Jinfo.version = vb_version. 2010-12-07 taku-y [trivial] Jinfo.version = vbmeg('version'); 2011-05-11 takiu-y [debug] Jinfo.Tmsec corrected. 2011-06-28 taku-y [minor] Jinfo.Tix added. Copyright (C) 2011, ATR All Rights Reserved. License : New BSD License(see VBMEG_LICENSE.txt)
0001 function [Zact,Jinfo,bayes_parm,vb_parm,MEGinfo,Jext,Pointlist] ... 0002 = vb_current_reconstruct_z(proj_root, curr_parm) 0003 % Current reconstruction using Bayesian inverse filter. 0004 % 0005 % [syntax] 0006 % [Zact,Jinfo,bayes_parm,vb_parm,MEGinfo,Jext,Pointlist] ... 0007 % = vbmeg_current_reconstruct_z(proj_root, curr_parm) 0008 % 0009 % [input] 0010 % proj_root: <<string>> VBMEG project root directory. 0011 % curr_parm: <<struct>> Parameters for current estimation. 0012 % --- fields of curr_parm 0013 % bayesfile : <<string>> Model parameter file (.bayes.mat). 0014 % currfile : <<string>> Cortical current file (.curr.mat), created 0015 % by this function. 0016 % trial_average: <optional> <<bool>> If true, 0017 % = [ON] : average current over all sessions 0018 % = OFF : current for each session 0019 % ix_area : <optional> Vertex indices to calculate estimated 0020 % current. If 'ix_area' is empty or not given, cortical 0021 % currents in the active region are calculated. 0022 % tsubsmpl : <optional> <bosolete> Specify subsampled time 0023 % index. If 'tsubsmpl' is empty or not given, time 0024 % subsampling is not done. 0025 % dsampf : <optional> <<int>> Specify frequency of 0026 % downsampling. This value must be smaller than the 0027 % original sampling frequency of M/EEG data. 0028 % overlap_mode : <optional> <<int>> 0029 % = 0 : current is averaged over overlapped time window 0030 % = 1 : current is not averaged for overlapped window 0031 % current time series of each time windows 0032 % are concatenated sequentially for spectral analysis 0033 % verbose : <<bool>> Verbose flag. 0034 % --- 0035 % 0036 % [note] If following field is given, these values are used instead of 0037 % bayes_parm field in result file: 0038 % --- 0039 % curr_parm.basisfile 0040 % curr_parm.megfile 0041 % curr_parm.twin_meg 0042 % curr_parm.Tperiod 0043 % curr_parm.Tnext 0044 % curr_parm.extra.basisfile (for extra dipole) 0045 % --- 0046 % 0047 % [output] 0048 % Zact : active current 0049 % 0050 % Zact(n,t,:) is the current at the vertex 'ix_act(n)' & the time 't' 0051 % Zact(Nact,Nsample) for trial_average = ON 0052 % Zact(Nact,Nsample,Ntrials) for trial_average = OFF 0053 % Nact : # of active region, 0054 % Nsample : # of time sample, 0055 % Ntrials : # of trials in all session] 0056 % --- fields of Jinfo 0057 % version : <<string>> Version of cortical current file. 0058 % curr_type : <<string>> 'Z-current'. It can be 'J-current' for VBMEG 0059 % version 0.8 or older. 0060 % Wact : <<float matrix>> Smoothing Gaussian filter, mapping from 0061 % Z-current to J-current. 0062 % ix_act : <<int vector>>: Vertex indices of Z-current. 0063 % ix_act_ex : <<int vector>>: Vertex indices of J-current. 0064 % Lact : <<int>> Number of current direction at one vertex. 0065 % Tsample : <<int vector>> Time sample indices of the original MEG 0066 % data. length(Tsample) == size(Zact,2) == size(Jact,2). 0067 % Tmsec : <<float vector>> Time in msec. 0068 % SampleFreq: <<float>> Sample frequency of cortical current, not 0069 % original M/EEG signal [Hz]. 0070 % Pretrigger: <<int>> Time points of the length of the pretrigger 0071 % period of cortical current data. It is neither actual time 0072 % nor time points of the original M/EEG signal. 0073 % Ntrial : <<int>> Number of trials of estimated current. 0074 % patch_norm: <<bool>> Cortical current is patch size normalized 0075 % (Am/m^2) or not (Am). 0076 % Tix : <<L x 1 cell>> Time sample indices of each time window. 0077 % Zact(:,Tix{n},:) is the set of Z-current within the n-th 0078 % time window. 0079 % --- 0080 % 0081 % * Inverse filter calculation is done in vb_invfilter_calc 0082 % 0083 % [history] 0084 % 2006-09-03 M. Sato 0085 % * Non-overlapped concatenation mode is added for spectral analysis 0086 % 2008-08-19 Taku Yoshioka 0087 % Extra dipole support 0088 % 2008-09-30 Taku Yoshioka 0089 % Minor change for variables in Jinfo 0090 % 2008-10-23 Taku Yoshioka 0091 % Bug fix for current estimation without extra dipoles 0092 % 2009-04-02 Taku Yoshioka 0093 % Parameter name changed within this code for readability 0094 % (just replacing 'resultfile' to bayesfile) 0095 % 2010-03-01 M. Sato 0096 % Bug fix for Wact index and fieldname(tsubsamp->tsubsmpl) 0097 % 2010-12-06 taku-y 0098 % [enhancement] curr_parm.dsampf supported. 0099 % [minor] Following fields of Jinfo set in this function: 0100 % SampleFreq 0101 % Pretrigger 0102 % Tmsec 0103 % [trivial] Jinfo.version = vb_version. 0104 % 2010-12-07 taku-y 0105 % [trivial] Jinfo.version = vbmeg('version'); 0106 % 2011-05-11 takiu-y 0107 % [debug] Jinfo.Tmsec corrected. 0108 % 2011-06-28 taku-y 0109 % [minor] Jinfo.Tix added. 0110 % 0111 % Copyright (C) 2011, ATR All Rights Reserved. 0112 % License : New BSD License(see VBMEG_LICENSE.txt) 0113 0114 if ~isempty(proj_root) 0115 bayesfile = fullfile(proj_root, curr_parm.bayesfile); 0116 else 0117 bayesfile = curr_parm.bayesfile; 0118 end 0119 0120 % 0121 % Verbose level setting 0122 % (note: 'verbose_level' is not related to input variable 0123 % 'curr_parm.verbose'. So two configurations relating to message display 0124 % are coexisting in this function.) 0125 % 0126 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0127 global vbmeg_inst 0128 verbose_const = vb_define_verbose; 0129 0130 if isempty(vbmeg_inst) | ~isfield(vbmeg_inst,'verbose_level'), 0131 verbose_level = verbose_const.VERBOSE_LEVEL_NOTICE; 0132 else 0133 verbose_level = vbmeg_inst.verbose_level; 0134 end 0135 0136 % 0137 % load VBMEG estimated result 0138 % 0139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0140 load(bayesfile, 'bayes_parm','Model','vb_parm','Model_ext','Pointlist'); 0141 0142 % 0143 % check parameter of 'curr_parm' 0144 % 0145 % Values of 'curr_parm' fields dominates over 0146 % those of 'bayes_parm' in bayesfile 0147 % 0148 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0149 [bayes_parm,ix_area,trial_average,tsubsamp,overlap_mode,dsampf] ... 0150 = check_arg(bayes_parm, curr_parm); 0151 0152 if ~isempty(proj_root); 0153 bayes_parm_abs = vb_parm_absolute_path(proj_root, bayes_parm); 0154 else 0155 bayes_parm_abs = bayes_parm; 0156 end 0157 0158 % 0159 % MEG data preparation 0160 % B : MEG data 0161 % 0162 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0163 [B,Ntrials,Nch,Tsample,Twindow,Tmsec] ... 0164 = vb_megdata_preparation(bayes_parm_abs); 0165 MEGinfo = vb_load_meg_info(bayes_parm_abs.megfile{1}); 0166 0167 % 0168 % Preparation of lead fields 0169 % Gact : leadfield of focal window 0170 % ix_act : Vertex index corresponding to active current Zact 0171 % ix_act_ex : Vertex index corresponding to active current Jact 0172 % Wact : Spatial smoothing matrix of focal window 0173 % 0174 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0175 0176 % Focal window 0177 vb_disp('--- Lead field matrix of focal window'); 0178 0179 lf_parm.brainfile = bayes_parm_abs.brainfile; 0180 lf_parm.areafile = bayes_parm_abs.areafile; 0181 lf_parm.patch_norm = bayes_parm_abs.patch_norm; 0182 lf_parm.expand_spatial_filter = bayes_parm_abs.expand_spatial_filter; 0183 lf_parm.spatial_smoother = bayes_parm_abs.spatial_smoother; 0184 lf_parm.basisfile = bayes_parm_abs.basisfile; 0185 lf_parm.area_key = bayes_parm_abs.area_key; 0186 lf_parm.reduce = bayes_parm_abs.reduce; 0187 lf_parm.Rfilt = bayes_parm_abs.Rfilt; 0188 lf_parm.remove_area_key = []; 0189 0190 [Gact, ix_act, ix_act_ex, Wact, Lact] = ... 0191 vb_leadfield_preparation(lf_parm); 0192 0193 % Extra dipole 0194 if isfield(bayes_parm_abs,'extra') & ~isempty(bayes_parm_abs.extra), 0195 vb_struct2vars(bayes_parm_abs,{'extra'}); 0196 fprintf('--- Lead field matrix of extra dipoles \n'); 0197 for n=1:length(extra.basisfile) 0198 tmp = vb_load_basis(extra.basisfile{n}); 0199 Gext{n} = tmp'; 0200 end 0201 else 0202 Gext = []; 0203 end 0204 0205 % 0206 % Area index in which current is calculated 0207 % 0208 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0209 if ~isempty(ix_area), 0210 % Select vertex index 'ix_area' within the active current region 0211 [jx_area_ex, ix_area_ex] = vb_index2indexsub(ix_area, ix_act_ex); 0212 else 0213 jx_area_ex = 1:length(ix_act_ex); 0214 end 0215 0216 Wact = Wact(jx_area_ex,:); 0217 jx_act = find( sum(Wact, 1) > 0); 0218 Wact = Wact(:,jx_act); 0219 0220 % active index of Z-current 0221 ix_act = ix_act(jx_act); 0222 % active index of J-current 0223 ix_act_ex = ix_act_ex(jx_area_ex); 0224 0225 % # of active vertex 0226 Njact_area = length(jx_act); 0227 0228 % # of extra dipoles 0229 if ~isempty(Gext), Njext = size(Gext{1},2); 0230 else Njext = 0; end 0231 0232 % 0233 % Constant 0234 % 0235 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0236 Nsession = length(B); % Number of sessions 0237 Ntotal = sum(Ntrials); % Total number of trials in all sessions 0238 0239 % 0240 % Temporal subsampling index 0241 % 0242 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0243 sampf = MEGinfo.SampleFreq; 0244 0245 if ~isempty(dsampf), 0246 tsubsamp = ceil(1:sampf/dsampf:Tsample); 0247 Jinfo.SampleFreq = dsampf; 0248 else 0249 if isempty(tsubsamp), tsubsamp = 1:Tsample; end 0250 Jinfo.SampleFreq = sampf; 0251 end 0252 0253 %Jinfo.Tmsec = Tmsec(tsubsamp); 0254 [tmp,ix] = min(abs(Tmsec(tsubsamp))); 0255 Jinfo.Pretrigger = ix; 0256 0257 % 0258 % Temporal smoothing window weight 0259 % 0260 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0261 [Tweight ,Tindex, Nindex, Nsample] = ... 0262 vb_overlapped_timewindow_weight(Twindow, Tsample, tsubsamp, overlap_mode); 0263 0264 Nwindow = length(Nindex); % # of time window 0265 Jinfo.Tix = Nindex; 0266 0267 if overlap_mode == 1, 0268 vb_disp('Non-overlapped concatenation mode'); 0269 end 0270 0271 % 0272 % Initialization 0273 % 0274 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0275 if trial_average == ON 0276 % Current averaged over trials 0277 Zact = zeros(Njact_area,Nsample); 0278 Jext = zeros(Njext,Nsample); 0279 else 0280 % Current for each trial 0281 Zact = zeros(Njact_area,Nsample,Ntotal); 0282 Jext = zeros(Njext,Nsample,Ntotal); 0283 end 0284 0285 % 0286 % Estimated current variance 0287 % 0288 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0289 a_inv = Model.a; 0290 Cov = Model.Cov; 0291 if ~isempty(Model_ext), e_inv = Model_ext.a; 0292 else e_inv = []; end 0293 0294 % 0295 % Time window loop 0296 % 0297 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0298 for j=1:Nwindow 0299 % Subsampling time index 0300 Tid = Tindex{j}; % subsampled time index 0301 Nid = Nindex{j}; % index in the total subsampled data 0302 Nt = length(Tid); 0303 0304 if Nt == 0, continue; end 0305 0306 % Session loop 0307 ntrial = 1; 0308 0309 for n=1:Nsession 0310 Ntry = Ntrials(n); 0311 0312 G = Gact{n}; % Ga 0313 if ~isempty(Gext), Ge = Gext{n}; else Ge = []; end 0314 Covs = Cov{n}; % Sg 0315 Nch = size(G,1); 0316 0317 %%%% Calculate Inverse filter for current reconstruction 0318 if ~isempty(e_inv), 0319 [KW,KW_ext] ... 0320 = vb_invfilter_z(a_inv(:,j),G,Covs,jx_act,e_inv(:,j),Ge); 0321 else 0322 [KW,KW_ext] ... 0323 = vb_invfilter_z(a_inv(:,j),G,Covs,jx_act,[],[]); 0324 end 0325 0326 %%%% Time window smoothing 0327 weight = Tweight{j}; 0328 0329 if trial_average == ON 0330 % MEG trial_average 0331 Bt = vb_repmultiply(B{n}(:,Tid), weight); 0332 % Current reconstruction 0333 Zact(:,Nid) = Zact(:,Nid) + (KW * Bt); 0334 if ~isempty(KW_ext), Jext(:,Nid) = Jext(:,Nid) + (KW_ext * Bt); end 0335 else 0336 % MEG for each trial 0337 Bt = B{n}(:,Tid,:); 0338 for m=1:Ntry 0339 Bt(:,:,m) = vb_repmultiply(Bt(:,:,m) , weight); 0340 end 0341 % Trial index 0342 ix_try = [ntrial:ntrial+Ntry-1]; 0343 ntrial = ntrial+Ntry; 0344 0345 % Current reconstruction 0346 Zact(:,Nid,ix_try) = Zact(:,Nid,ix_try) ... 0347 + reshape( KW * reshape( Bt, [Nch, Nt*Ntry]), ... 0348 [Njact_area, Nt, Ntry]); 0349 if ~isempty(KW_ext), 0350 Jext(:,Nid,ix_try) = Jext(:,Nid,ix_try) ... 0351 + reshape( KW_ext * reshape( Bt, [Nch, Nt*Ntry]), ... 0352 [Njext, Nt, Ntry]); 0353 end 0354 end 0355 end % Session loop END 0356 end % Timewindow loop END 0357 0358 if trial_average == ON 0359 Zact = Zact/Nsession; 0360 Jext = Jext/Nsession; 0361 end 0362 0363 % 0364 % Current Info 0365 % 0366 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0367 Jinfo.version = '2.0-0.b.9'; %vbmeg('version'); 0368 Jinfo.curr_type = 'Z-current'; 0369 0370 Jinfo.Lact = Lact; 0371 Jinfo.Wact = Wact; 0372 Jinfo.ix_act = ix_act; 0373 Jinfo.ix_act_ex = ix_act_ex; 0374 Jinfo.NJact = Njact_area; 0375 Jinfo.Nsession = Nsession; 0376 Jinfo.jactdir = []; 0377 Jinfo.Ntrial = Ntrials; 0378 0379 % ix_act : Vertex index corresponding to active current Zact 0380 % ix_act_ex : Vertex index corresponding to active current Jact 0381 % Wact : Spatial smoothing matrix of focal window 0382 % Jact = Wact * Zact 0383 0384 % Actual time corresponding to columns of Zact, supporting overlap mode 0385 % and non-overlapped concatenation mode 0386 Tid_all = []; 0387 Nid_all = []; 0388 for j=1:Nwindow 0389 Tid_all = [Tid_all Tindex{j}]; 0390 Nid_all = [Nid_all Nindex{j}]; 0391 end 0392 0393 if overlap_mode==false, 0394 ix = unique(Tid_all); 0395 Jinfo.Tmsec = Tmsec(ix); 0396 else 0397 Jinfo.Tmsec = Tmsec(Tid_all); 0398 end 0399 0400 %Tstart = bayes_parm.twin_meg(1); 0401 %Tend = bayes_parm.twin_meg(2); 0402 %if isempty(tsubsamp) 0403 % Jinfo.Tsample = Tstart:Tend; 0404 %else 0405 % Jinfo.Tsample = tsubsamp + Tstart - 1; 0406 %end 0407 0408 return; 0409 0410 %%%% --------------- 0411 function [bayes_parm, ix_area, trial_average, tsubsamp, overlap_mode,dsampf] = ... 0412 check_arg(bayes_parm,curr_parm) 0413 0414 if isfield(curr_parm,'basisfile'), 0415 bayes_parm.basisfile = curr_parm.basisfile; 0416 end; 0417 if isfield(curr_parm,'megfile'), 0418 bayes_parm.megfile = curr_parm.megfile ; 0419 end; 0420 if isfield(curr_parm,'twin_meg'), 0421 bayes_parm.twin_meg = curr_parm.twin_meg ; 0422 end; 0423 if isfield(curr_parm,'Tperiod'), 0424 bayes_parm.Tperiod = curr_parm.Tperiod ; 0425 end; 0426 if isfield(curr_parm,'Tnext'), 0427 bayes_parm.Tnext = curr_parm.Tnext ; 0428 end; 0429 0430 if ~isfield(curr_parm,'trial_average'), 0431 trial_average = ON; 0432 else 0433 trial_average = curr_parm.trial_average; 0434 end; 0435 0436 bayes_parm.trial_average = trial_average; 0437 0438 if ~isfield(curr_parm,'ix_area'), 0439 ix_area = []; 0440 else 0441 ix_area = curr_parm.ix_area; 0442 end; 0443 if ~isfield(curr_parm,'tsubsmpl'), 0444 tsubsamp = []; 0445 else 0446 tsubsamp = curr_parm.tsubsmpl; 0447 end; 0448 if ~isfield(curr_parm,'overlap_mode'), 0449 overlap_mode = 0; 0450 else 0451 overlap_mode = curr_parm.overlap_mode; 0452 end; 0453 0454 if isfield(curr_parm,'extra'), 0455 if isfield(curr_parm.extra,'basisfile'), 0456 bayes_parm.extra.basisfile = curr_parm.extra.basisfile; 0457 end; 0458 end; 0459 0460 if ~isfield(curr_parm,'dsampf'), 0461 dsampf = []; 0462 else 0463 dsampf = curr_parm.dsampf; 0464 end; 0465 0466 return;