|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| function [prob3d, mri] = dipoledensity(dipplotargs, varargin)
|
|
|
|
|
|
|
|
|
| prob3d = []; mri = [];
|
| if nargin < 1
|
| help dipoledensity
|
| return
|
| end
|
|
|
| g = finputcheck(varargin, { 'subjind' 'integer' [] [];
|
| 'method' 'string' { 'relentropy','entropy','distance','alldistance' } 'alldistance';
|
| 'methodparam' 'real' [] 20;
|
| 'weight' { 'real','cell' } [] [];
|
| 'smooth' 'real' [] 0;
|
| 'nsessions' 'integer' [] 1;
|
| 'subsample' 'integer' [] 2;
|
| 'plotargs' 'cell' [] {};
|
| 'plot' 'string' { 'on','off' } fastif(nargout == 0, 'on', 'off');
|
| 'dipplot' 'string' { 'on','off' } 'off';
|
| 'coordformat' 'string' { 'mni','spherical' } 'mni';
|
| 'normalization' 'string' { 'on','off' } 'on';
|
| 'volmesh_fname' 'string' [] 'volmesh_local.mat';
|
| 'mri' { 'struct','string' } [] '';
|
| 'norm2JointProb' 'string' { 'on','off' } 'off'});
|
| if isstr(g), error(g); end;
|
| if ~strcmpi(g.method, 'alldistance') & isempty(g.subjind)
|
| error('Subject indices are required for this method');
|
| end;
|
| if ~iscell(g.weight), g.weight = { g.weight }; end;
|
|
|
|
|
|
|
| if ~iscell(dipplotargs)
|
| if ~isstruct(dipplotargs)
|
| if size(dipplotargs,1) == 3, dipplotargs = dipplotargs';
|
| elseif size(dipplotargs,2) ~= 3
|
| error('If an array of dipoles is given as entry, there must be 3 columns or 3 rows for x y z');
|
| end;
|
| model = [];
|
| for idip = 1:length(dipplotargs)
|
| model(idip).posxyz = dipplotargs(idip,:);
|
| model(idip).momxyz = [1 0 0];
|
| model(idip).rv = 0.5;
|
| end;
|
| dipplotargs = model;
|
| end;
|
| dipplotargs = { dipplotargs 'coordformat' g.coordformat };
|
| else
|
| dipplotargs = { dipplotargs{:} 'coordformat' g.coordformat };
|
| end;
|
| struct = dipplot(dipplotargs{:}, 'plot', g.dipplot);
|
| if nargout == 0
|
| drawnow;
|
| end;
|
|
|
| % retrieve coordinates in MNI space
|
| % ---------------------------------
|
| if 0 % deprecated
|
| % find dipoles
|
| % ------------
|
| hmesh = findobj(gcf, 'tag', 'mesh');
|
| if isempty(hmesh), error('Current figure must contain dipoles'); end;
|
| hh = [];
|
| disp('Finding dipoles...');
|
| dips = zeros(1,200);
|
| for index = 1:1000
|
| hh = [ hh(:); findobj(gcf, 'tag', ['dipole' int2str(index) ]) ];
|
| dips(index) = length(findobj(gcf, 'tag', ['dipole' int2str(index) ]));
|
| end;
|
|
|
| disp('Retrieving dipole positions ...');
|
| count = 1;
|
| for index = 1:length(hh)
|
| tmp = get(hh(index), 'userdata');
|
| if length(tmp) == 1
|
| allx(count) = tmp.eleccoord(1,1);
|
| ally(count) = tmp.eleccoord(1,2);
|
| allz(count) = tmp.eleccoord(1,3);
|
| alli(count) = index;
|
| count = count + 1;
|
| end;
|
| end;
|
| end;
|
|
|
| % check weights
|
| % -------------
|
| if ~isempty(g.weight{1})
|
| if ~iscell(g.weight)
|
| if length(g.weight) ~= length(struct)
|
| error('There must be as many elements in the weight matrix as there are dipoles')
|
| end;
|
| else
|
| if length(g.weight{1}) ~= length(struct) || length(g.weight{1}) ~= length(g.weight{end})
|
| error('There must be as many elements in the weight matrix as there are dipoles')
|
| end;
|
| end;
|
| else
|
| g.weight = { ones( 1, length(struct)) };
|
| end;
|
| if ~isempty(g.subjind)
|
| if length(g.subjind) ~= length(struct)
|
| error('There must be as many element in the subject matrix as there are dipoles')
|
| end;
|
| else
|
| g.subjind = ones( 1, length(struct));
|
| end;
|
|
|
| % decoding dipole locations
|
| % -------------------------
|
| disp('Retrieving dipole positions ...');
|
| count = 1;
|
| for index = 1:length(struct)
|
| dips = size(struct(index).eleccoord,1);
|
| for dip = 1:dips
|
| allx(count) = struct(index).eleccoord(dip,1);
|
| ally(count) = struct(index).eleccoord(dip,2);
|
| allz(count) = struct(index).eleccoord(dip,3);
|
| alli(count) = index;
|
| allw1(count) = g.weight{1}( index)/dips;
|
| allw2(count) = g.weight{end}(index)/dips;
|
| alls(count) = g.subjind(index);
|
| count = count + 1;
|
| end;
|
| end;
|
| g.weight{1} = allw1;
|
| g.weight{end} = allw2;
|
| g.subjind = alls;
|
|
|
| % read MRI file
|
| % -------------
|
| if isempty(g.mri) % default MRI file
|
| dipfitdefs;
|
| load('-mat', template_models(1).mrifile); % load mri variable
|
| g.mri = mri;
|
| end
|
| if isstr(g.mri)
|
| try,
|
| mri = load('-mat', g.mri);
|
| mri = mri.mri;
|
| catch,
|
| disp('Failed to read Matlab file. Attempt to read MRI file using function read_fcdc_mri');
|
| try,
|
| warning off;
|
| mri = read_fcdc_mri(g.mri);
|
| mri.anatomy = round(gammacorrection( mri.anatomy, 0.8));
|
| mri.anatomy = uint8(round(mri.anatomy/max(reshape(mri.anatomy, prod(mri.dim),1))*255));
|
| % WARNING: if using double instead of int8, the scaling is different
|
| % [-128 to 128 and 0 is not good]
|
| % WARNING: the transform matrix is not 1, 1, 1 on the diagonal, some slices may be
|
| % misplaced
|
| warning on;
|
| catch,
|
| error('Cannot load file using read_fcdc_mri');
|
| end;
|
| end;
|
| g.mri = mri; % output the anatomic mri image
|
| end;
|
|
|
|
|
| % reserve array for density
|
| % -------------------------
|
| prob3d = {zeros(ceil(g.mri.dim/g.subsample)) };
|
| for i = 2:length(g.weight), prob3d{i} = prob3d{1}; end;
|
|
|
| % compute voxel size
|
| % ------------------
|
| point1 = g.mri.transform * [ 1 1 1 1 ]';
|
| point2 = g.mri.transform * [ 2 2 2 1 ]';
|
| voxvol = sum((point1(1:3)-point2(1:3)).^2)*g.subsample^3; % in mm
|
|
|
| % compute global subject entropy if necessary
|
| % -------------------------------------------
|
| vals = unique_bc(g.subjind); % the unique subject indices
|
| if strcmpi(g.method, 'relentropy') | strcmpi(g.method, 'entropy') %%%%% entropy %%%%%%%
|
| newind = zeros(size(g.subjind));
|
| for index = 1:length(vals) % foreach subject in the cluster
|
| tmpind = find(g.subjind == vals(index)); % dipoles for the subject
|
| totcount(index) = length(tmpind); % store the number of subject dipoles
|
| newind(tmpind) = index; % put subject index into newind
|
| end;
|
| g.subjind = newind;
|
| gp = totcount/sum(totcount);
|
| globent = -sum(gp.*log(gp));
|
| end;
|
|
|
| % compute volume inside head mesh
|
| % -------------------------------
|
| dipfitdefs; % get the location of standard BEM volume file
|
| tmp = load('-mat',DIPOLEDENSITY_STDBEM); % load MNI mesh
|
|
|
| if isempty(g.volmesh_fname) % default
|
| filename = [ '/home/arno/matlab/MNI_VoxelTsearch' int2str(g.subsample) '.mat' ];
|
| else
|
| filename = g.volmesh_fname; %
|
| end
|
| if ~exist(filename)
|
| disp('Computing volume within head mesh...');
|
| [X Y Z] = meshgrid(g.mri.xgrid(1:g.subsample:end)+g.subsample/2, ...
|
| g.mri.ygrid(1:g.subsample:end)+g.subsample/2, ...
|
| g.mri.zgrid(1:g.subsample:end)+g.subsample/2);
|
| [indX indY indZ ] = meshgrid(1:length(g.mri.xgrid(1:g.subsample:end)), ...
|
| 1:length(g.mri.ygrid(1:g.subsample:end)), ...
|
| 1:length(g.mri.zgrid(1:g.subsample:end)));
|
| allpoints = [ X(:)' ; Y(:)' ; Z(:)' ];
|
| allinds = [ indX(:)' ; indY(:)'; indZ(:)' ];
|
| allpoints = g.mri.transform * [ allpoints ; ones(1, size(allpoints,2)) ];
|
| allpoints(4,:) = [];
|
|
|
| olddir = pwd;
|
| tmppath = which('ft_electroderealign');
|
| tmppath = fullfile(fileparts(tmppath), 'private');
|
| cd(tmppath);
|
| [Inside Outside] = find_inside_vol(allpoints', tmp.vol);
|
| cd(olddir);
|
| disp('Done.');
|
|
|
| if 0
|
| P = tmp.vol.bnd(1).pnt;
|
| T = delaunayn(P);
|
|
|
|
|
| IO = tsearchn(P, T, allpoints');
|
| Inside = find(isnan(IO));
|
| Outside = find(~isnan(IO));
|
| disp('Done.');
|
| end; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
| try,
|
| save('-mat', filename, 'allpoints', 'allinds', 'Inside', 'Outside');
|
| disp('Saving file containing inside/outide voxel indices...');
|
| catch, end;
|
| else
|
| disp('Loading file containing inside/outide voxel indices...');
|
| load('-mat',filename);
|
| end;
|
| InsidePoints = allpoints(:, Inside);
|
| InsideIndices = allinds(:, Inside);
|
|
|
| % scan grid and compute entropy at each voxel
|
| % -------------------------------------------
|
| edges = [0.5:1:length(vals)+0.5];
|
|
|
| if ~strcmpi(g.method, 'alldistance')
|
| fprintf('Computing (of
|
|
|
|
|
| for i = 1:size(InsideIndices,2)
|
|
|
| alldists = (InsidePoints(1,i) - allx).^2 ...
|
| + (InsidePoints(2,i) - ally).^2 ...
|
| + (InsidePoints(3,i) - allz).^2;
|
| [tmpsort indsort] = sort(alldists);
|
| tmpweights{1} = g.weight{1}( indsort);
|
| tmpweights{end} = g.weight{end}(indsort);
|
|
|
| if strcmpi(g.method, 'relentropy') | strcmpi(g.method, 'entropy')
|
|
|
| subjs = g.subjind(indsort(1:g.methodparam));
|
| p = histc(subjs, edges);
|
| if strcmpi(g.method, 'relentropy')
|
| p = p(1:end-1)./totcount;
|
|
|
| end;
|
| p = p/sum(p);
|
| p(find(p == 0)) = [];
|
| for tmpi = 1:length(g.weight)
|
| prob3d{1}(InsideIndices(1,i), InsideIndices(2,i), InsideIndices(3,i)) = -sum(p.*log(p));
|
| end;
|
| else
|
|
|
| ordsubjs = g.subjind(indsort);
|
| for index = 1:length(vals)
|
| tmpind = find(ordsubjs == vals(index));
|
| if strcmpi(g.method,'distance')
|
| use_dipoles(index) = tmpind(1);
|
| end
|
| end;
|
| for tmpi = 1:length(g.weight)
|
| prob3d{tmpi}(InsideIndices(1,i), InsideIndices(2,i), InsideIndices(3,i)) = ...
|
| sum(tmpweights{tmpi}(use_dipoles).*exp(-tmpsort(use_dipoles)/ ...
|
| (2*g.methodparam^2)));
|
| end;
|
| end;
|
| if mod(i,100) == 0, fprintf('%d ', i); end;
|
| end;
|
| else
|
|
|
|
|
|
|
|
|
|
|
| fprintf('Computing (of %d):', size(allx,2));
|
| for tmpi=1:length(g.weight)
|
| tmpprob{tmpi} = zeros(1, size(InsidePoints,2));
|
| end;
|
| if length(g.weight) > 1, tmpprob2 = tmpprob; end;
|
| for i = 1:size(allx,2)
|
| alldists = (InsidePoints(1,:) - allx(i)).^2 + ...
|
| (InsidePoints(2,:) - ally(i)).^2 + ...
|
| (InsidePoints(3,:) - allz(i)).^2;
|
|
|
| for tmpi=1:length(g.weight)
|
| tmpprob{tmpi} = tmpprob{tmpi} + g.weight{tmpi}(i)*exp(-alldists/(2*g.methodparam^2));
|
| if any(isinf(tmpprob{tmpi})), error('Infinite value in probability calculation'); end;
|
| end;
|
| if mod(i,50) == 0, fprintf('%d ', i); end;
|
| end;
|
|
|
|
|
| for i = 1:length(Inside)
|
| pnts = allinds(:,Inside(i));
|
| for tmpi = 1:length(g.weight)
|
| prob3d{tmpi}(pnts(1), pnts(2), pnts(3)) = tmpprob{tmpi}(i);
|
| end;
|
| end;
|
|
|
| end;
|
| fprintf('\n');
|
|
|
|
|
|
|
| if strcmpi(g.method, 'alldistance') && strcmpi(g.normalization,'on')
|
| for i =1:length(g.weight)
|
| disp('Normalizing to dipole/mm^3');
|
| if any(prob3d{i}(:)<0)
|
| fprintf('WARNING: Some probabilities are negative, this will likely cause problems when normalizing probabilities.\n');
|
| fprintf('It is highly recommended to turn normaliziation off by using ''normalization'' key to ''off''.\n');
|
| end;
|
| totval = sum(prob3d{i}(:));
|
| switch g.norm2JointProb
|
| case 'off'
|
| totdip = size(allx,2);
|
| voxvol;
|
| prob3d{i} = prob3d{i}/totval*totdip/voxvol*1000;
|
| prob3d{i} = prob3d{i}/g.nsessions;
|
| case 'on'
|
| prob3d{i} = prob3d{i}/totval;
|
| end
|
| end;
|
| end;
|
|
|
|
|
|
|
| if g.subsample ~= 1
|
| for i =1:length(g.weight)
|
| prob3d{i} = prob3d{i}/g.subsample;
|
| newprob3d = zeros(g.mri.dim);
|
| X = ceil(g.mri.xgrid/g.subsample);
|
| Y = ceil(g.mri.ygrid/g.subsample);
|
| Z = ceil(g.mri.zgrid/g.subsample);
|
| for index = 1:size(newprob3d,3)
|
| newprob3d(:,:,index) = prob3d{i}(X,Y,Z(index));
|
| end;
|
| prob3d{i} = newprob3d;
|
| end;
|
| end;
|
|
|
|
|
|
|
| if g.smooth ~= 0
|
| disp('Smoothing...');
|
| for i =1:length(g.weight)
|
| prob3d{i} = smooth3d(prob3d{i}, g.smooth);
|
| end;
|
| end;
|
|
|
|
|
|
|
| if strcmpi(g.plot, 'off')
|
| close gcf;
|
| else
|
| mri3dplot( prob3d, g.mri, g.plotargs{:});
|
| end;
|
| return;
|
|
|
|
|
| function [inside, outside] = find_inside_vol(pos, vol);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| warning('find_inside_vol is obsolete and will be removed, please use ft_inside_vol');
|
| inside = ft_inside_vol(pos, vol);
|
|
|
| outside = find(~inside);
|
| inside = find(inside);
|
|
|