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 = [proj_root filesep 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 \n'); 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.basisfile = bayes_parm_abs.basisfile; 0184 lf_parm.area_key = bayes_parm_abs.area_key; 0185 lf_parm.reduce = bayes_parm_abs.reduce; 0186 lf_parm.Rfilt = bayes_parm_abs.Rfilt; 0187 lf_parm.remove_area_key = []; 0188 0189 [Gact, ix_act, ix_act_ex, Wact, Lact] = ... 0190 vb_leadfield_preparation(lf_parm); 0191 0192 % Extra dipole 0193 if isfield(bayes_parm_abs,'extra') & ~isempty(bayes_parm_abs.extra), 0194 vb_struct2vars(bayes_parm_abs,{'extra'}); 0195 fprintf('--- Lead field matrix of extra dipoles \n'); 0196 for n=1:length(extra.basisfile) 0197 tmp = vb_load_basis(extra.basisfile{n}); 0198 Gext{n} = tmp'; 0199 end 0200 else 0201 Gext = []; 0202 end 0203 0204 % 0205 % Area index in which current is calculated 0206 % 0207 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0208 if ~isempty(ix_area), 0209 % Select vertex index 'ix_area' within the active current region 0210 [jx_area_ex, ix_area_ex] = vb_index2indexsub(ix_area, ix_act_ex); 0211 else 0212 jx_area_ex = 1:length(ix_act_ex); 0213 end 0214 0215 Wact = Wact(jx_area_ex,:); 0216 jx_act = find( sum(Wact, 1) > 0); 0217 Wact = Wact(:,jx_act); 0218 0219 % active index of Z-current 0220 ix_act = ix_act(jx_act); 0221 % active index of J-current 0222 ix_act_ex = ix_act_ex(jx_area_ex); 0223 0224 % # of active vertex 0225 Njact_area = length(jx_act); 0226 0227 % # of extra dipoles 0228 if ~isempty(Gext), Njext = size(Gext{1},2); 0229 else Njext = 0; end 0230 0231 % 0232 % Constant 0233 % 0234 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0235 Nsession = length(B); % Number of sessions 0236 Ntotal = sum(Ntrials); % Total number of trials in all sessions 0237 0238 % 0239 % Temporal subsampling index 0240 % 0241 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0242 sampf = MEGinfo.SampleFreq; 0243 0244 if ~isempty(dsampf), 0245 tsubsamp = ceil(1:sampf/dsampf:Tsample); 0246 Jinfo.SampleFreq = dsampf; 0247 else 0248 if isempty(tsubsamp), tsubsamp = 1:Tsample; end 0249 Jinfo.SampleFreq = sampf; 0250 end 0251 0252 %Jinfo.Tmsec = Tmsec(tsubsamp); 0253 [tmp,ix] = min(abs(Tmsec(tsubsamp))); 0254 Jinfo.Pretrigger = ix; 0255 0256 % 0257 % Temporal smoothing window weight 0258 % 0259 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0260 [Tweight ,Tindex, Nindex, Nsample] = ... 0261 vb_overlapped_timewindow_weight(Twindow, Tsample, tsubsamp, overlap_mode); 0262 0263 Nwindow = length(Nindex); % # of time window 0264 Jinfo.Tix = Nindex; 0265 0266 if overlap_mode == 1, 0267 vb_disp('Non-overlapped concatenation mode\n'); 0268 end 0269 0270 % 0271 % Initialization 0272 % 0273 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0274 if trial_average == ON 0275 % Current averaged over trials 0276 Zact = zeros(Njact_area,Nsample); 0277 Jext = zeros(Njext,Nsample); 0278 else 0279 % Current for each trial 0280 Zact = zeros(Njact_area,Nsample,Ntotal); 0281 Jext = zeros(Njext,Nsample,Ntotal); 0282 end 0283 0284 % 0285 % Estimated current variance 0286 % 0287 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0288 a_inv = Model.a; 0289 Cov = Model.Cov; 0290 if ~isempty(Model_ext), e_inv = Model_ext.a; 0291 else e_inv = []; end 0292 0293 % 0294 % Time window loop 0295 % 0296 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0297 for j=1:Nwindow 0298 % Subsampling time index 0299 Tid = Tindex{j}; % subsampled time index 0300 Nid = Nindex{j}; % index in the total subsampled data 0301 Nt = length(Tid); 0302 0303 if Nt == 0, continue; end 0304 0305 % Session loop 0306 ntrial = 1; 0307 0308 for n=1:Nsession 0309 Ntry = Ntrials(n); 0310 0311 G = Gact{n}; % Ga 0312 if ~isempty(Gext), Ge = Gext{n}; else Ge = []; end 0313 Covs = Cov{n}; % Sg 0314 Nch = size(G,1); 0315 0316 %%%% Calculate Inverse filter for current reconstruction 0317 if ~isempty(e_inv), 0318 [KW,KW_ext] ... 0319 = vb_invfilter_z(a_inv(:,j),G,Covs,jx_act,e_inv(:,j),Ge); 0320 else 0321 [KW,KW_ext] ... 0322 = vb_invfilter_z(a_inv(:,j),G,Covs,jx_act,[],[]); 0323 end 0324 0325 %%%% Time window smoothing 0326 weight = Tweight{j}; 0327 0328 if trial_average == ON 0329 % MEG trial_average 0330 Bt = vb_repmultiply(B{n}(:,Tid), weight); 0331 % Current reconstruction 0332 Zact(:,Nid) = Zact(:,Nid) + (KW * Bt); 0333 if ~isempty(KW_ext), Jext(:,Nid) = Jext(:,Nid) + (KW_ext * Bt); end 0334 else 0335 % MEG for each trial 0336 Bt = B{n}(:,Tid,:); 0337 for m=1:Ntry 0338 Bt(:,:,m) = vb_repmultiply(Bt(:,:,m) , weight); 0339 end 0340 % Trial index 0341 ix_try = [ntrial:ntrial+Ntry-1]; 0342 ntrial = ntrial+Ntry; 0343 0344 % Current reconstruction 0345 Zact(:,Nid,ix_try) = Zact(:,Nid,ix_try) ... 0346 + reshape( KW * reshape( Bt, [Nch, Nt*Ntry]), ... 0347 [Njact_area, Nt, Ntry]); 0348 if ~isempty(KW_ext), 0349 Jext(:,Nid,ix_try) = Jext(:,Nid,ix_try) ... 0350 + reshape( KW_ext * reshape( Bt, [Nch, Nt*Ntry]), ... 0351 [Njext, Nt, Ntry]); 0352 end 0353 end 0354 end % Session loop END 0355 end % Timewindow loop END 0356 0357 if trial_average == ON 0358 Zact = Zact/Nsession; 0359 Jext = Jext/Nsession; 0360 end 0361 0362 % 0363 % Current Info 0364 % 0365 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0366 Jinfo.version = '1.0-0.a.5'; %vbmeg('version'); 0367 Jinfo.curr_type = 'Z-current'; 0368 0369 Jinfo.Lact = Lact; 0370 Jinfo.Wact = Wact; 0371 Jinfo.ix_act = ix_act; 0372 Jinfo.ix_act_ex = ix_act_ex; 0373 Jinfo.NJact = Njact_area; 0374 Jinfo.Nsession = Nsession; 0375 Jinfo.jactdir = []; 0376 Jinfo.Ntrial = Ntrials; 0377 0378 % ix_act : Vertex index corresponding to active current Zact 0379 % ix_act_ex : Vertex index corresponding to active current Jact 0380 % Wact : Spatial smoothing matrix of focal window 0381 % Jact = Wact * Zact 0382 0383 % Actual time corresponding to columns of Zact, supporting overlap mode 0384 % and non-overlapped concatenation mode 0385 Tid_all = []; 0386 Nid_all = []; 0387 for j=1:Nwindow 0388 Tid_all = [Tid_all Tindex{j}]; 0389 Nid_all = [Nid_all Nindex{j}]; 0390 end 0391 0392 if overlap_mode==false, 0393 ix = unique(Tid_all); 0394 Jinfo.Tmsec = Tmsec(ix); 0395 else 0396 Jinfo.Tmsec = Tmsec(Tid_all); 0397 end 0398 0399 %Tstart = bayes_parm.twin_meg(1); 0400 %Tend = bayes_parm.twin_meg(2); 0401 %if isempty(tsubsamp) 0402 % Jinfo.Tsample = Tstart:Tend; 0403 %else 0404 % Jinfo.Tsample = tsubsamp + Tstart - 1; 0405 %end 0406 0407 return; 0408 0409 %%%% --------------- 0410 function [bayes_parm, ix_area, trial_average, tsubsamp, overlap_mode,dsampf] = ... 0411 check_arg(bayes_parm,curr_parm) 0412 0413 if isfield(curr_parm,'basisfile'), 0414 bayes_parm.basisfile = curr_parm.basisfile; 0415 end; 0416 if isfield(curr_parm,'megfile'), 0417 bayes_parm.megfile = curr_parm.megfile ; 0418 end; 0419 if isfield(curr_parm,'twin_meg'), 0420 bayes_parm.twin_meg = curr_parm.twin_meg ; 0421 end; 0422 if isfield(curr_parm,'Tperiod'), 0423 bayes_parm.Tperiod = curr_parm.Tperiod ; 0424 end; 0425 if isfield(curr_parm,'Tnext'), 0426 bayes_parm.Tnext = curr_parm.Tnext ; 0427 end; 0428 0429 if ~isfield(curr_parm,'trial_average'), 0430 trial_average = ON; 0431 else 0432 trial_average = curr_parm.trial_average; 0433 end; 0434 0435 bayes_parm.trial_average = trial_average; 0436 0437 if ~isfield(curr_parm,'ix_area'), 0438 ix_area = []; 0439 else 0440 ix_area = curr_parm.ix_area; 0441 end; 0442 if ~isfield(curr_parm,'tsubsmpl'), 0443 tsubsamp = []; 0444 else 0445 tsubsamp = curr_parm.tsubsmpl; 0446 end; 0447 if ~isfield(curr_parm,'overlap_mode'), 0448 overlap_mode = 0; 0449 else 0450 overlap_mode = curr_parm.overlap_mode; 0451 end; 0452 0453 if isfield(curr_parm,'extra'), 0454 if isfield(curr_parm.extra,'basisfile'), 0455 bayes_parm.extra.basisfile = curr_parm.extra.basisfile; 0456 end; 0457 end; 0458 0459 if ~isfield(curr_parm,'dsampf'), 0460 dsampf = []; 0461 else 0462 dsampf = curr_parm.dsampf; 0463 end; 0464 0465 return;