Skip to content

Commit

Permalink
Fixed issue on native-space after manreco in elvis.
Browse files Browse the repository at this point in the history
  • Loading branch information
andreashorn committed Feb 26, 2016
1 parent 6136da3 commit 5402307
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 157 deletions.
File renamed without changes.
301 changes: 151 additions & 150 deletions ea_autocoord.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function ea_autocoord(options)
% This function is the main function of LEAD-DBS. It will generate a
% vector of coordinates.
% This function is the main function of LEAD-DBS. It will generate a
% vector of coordinates.
% Trajectory{1} will be the right trajectory, trajectory{2} the
% left one.
% For each hemisphere of the brain, this function will call the
Expand Down Expand Up @@ -31,196 +31,197 @@ function ea_autocoord(options)

if ~strcmp(options.patientname,'No Patient Selected') % only 3D-rendering viewer can be opened if no patient is selected.




if options.modality==2 % CT support
options.prefs.tranii=options.prefs.ctnii;
options.prefs.tranii_unnormalized=options.prefs.rawctnii_unnormalized;
end
results=nan;

if options.coregct.do
eval([options.coregct.method,'(options)']); % triggers the coregct function and passes the options struct to it.
try load([options.root,options.patientname,filesep,'ea_coregctmethod_applied']); end
if exist('coregct_method_applied','var')
try
coregct_method_applied{end+1}=options.coregct.method;
catch
clear coregct_method_applied



if options.modality==2 % CT support
options.prefs.tranii=options.prefs.ctnii;
options.prefs.tranii_unnormalized=options.prefs.rawctnii_unnormalized;
end
results=nan;

if options.coregct.do
eval([options.coregct.method,'(options)']); % triggers the coregct function and passes the options struct to it.
try load([options.root,options.patientname,filesep,'ea_coregctmethod_applied']); end
if exist('coregct_method_applied','var')
try
coregct_method_applied{end+1}=options.coregct.method;
catch
clear coregct_method_applied
coregct_method_applied{1}=options.coregct.method;
end
else
coregct_method_applied{1}=options.coregct.method;
end
else
coregct_method_applied{1}=options.coregct.method;
coregct_method_applied=options.coregct.method;
save([options.root,options.patientname,filesep,'ea_coregctmethod_applied'],'coregct_method_applied');
end
coregct_method_applied=options.coregct.method;
save([options.root,options.patientname,filesep,'ea_coregctmethod_applied'],'coregct_method_applied');
end

if options.coregctcheck


% export "control" niftis with wireframe of normal anatomy..
if options.coregctcheck


% export "control" niftis with wireframe of normal anatomy..

ea_show_ctcoregistration(options);

end

ea_show_ctcoregistration(options);

end


if options.normalize.do
try load([options.root,options.patientname,filesep,'ea_normmethod_applied']); end
if exist('norm_method_applied','var')
try
norm_method_applied{end+1}=options.normalize.method;
catch
clear norm_method_applied
if options.normalize.do
try load([options.root,options.patientname,filesep,'ea_normmethod_applied']); end
if exist('norm_method_applied','var')
try
norm_method_applied{end+1}=options.normalize.method;
catch
clear norm_method_applied
norm_method_applied{1}=options.normalize.method;
end
else
norm_method_applied{1}=options.normalize.method;
end
else
norm_method_applied{1}=options.normalize.method;
save([options.root,options.patientname,filesep,'ea_normmethod_applied'],'norm_method_applied');

eval([options.normalize.method,'(options)']); % triggers the normalization function and passes the options struct to it.


end
save([options.root,options.patientname,filesep,'ea_normmethod_applied'],'norm_method_applied');

eval([options.normalize.method,'(options)']); % triggers the normalization function and passes the options struct to it.


end




if options.dolc % perform lead connectome subroutine..

ea_perform_lc(options);

end



if options.atl.genpt % generate patient specific atlas set
ea_ptspecific_atl(options);

end

if options.atl.normalize % normalize patient-specific atlas-set.
ea_normalize_ptspecific_atl(options)
end



if options.normalize.check

if options.dolc % perform lead connectome subroutine..

ea_perform_lc(options);

end

% export "control" niftis with wireframe of normal anatomy..

ea_show_normalization(options);

end



if options.doreconstruction
ea_checkfiles(options);
if options.atl.genpt % generate patient specific atlas set
ea_ptspecific_atl(options);

end

if options.atl.normalize % normalize patient-specific atlas-set.
ea_normalize_ptspecific_atl(options)
end


for side=options.sides
%try
% call main routine reconstructing trajectory for one side.
[coords,trajvector{side},trajectory{side},tramat]=ea_reconstruct(patientname,options,side);

if options.normalize.check


% export "control" niftis with wireframe of normal anatomy..

%% refit electrodes starting from first electrode (this is redundant at
%% this point).

coords_mm{side} = ea_map_coords(coords', [options.root,options.prefs.patientdir,filesep,options.prefs.tranii])';
ea_show_normalization(options);

end



if options.doreconstruction
ea_checkfiles(options);

[~,distmm]=ea_calc_distance(options.elspec.eldist,trajvector{side},tramat(1:3,1:3),[options.root,options.prefs.patientdir,filesep,options.prefs.tranii]);

comp = ea_map_coords([0,0,0;trajvector{side}]', [options.root,options.prefs.patientdir,filesep,options.prefs.tranii])'; % (XYZ_mm unaltered)
for side=options.sides
%try
% call main routine reconstructing trajectory for one side.
[coords,trajvector{side},trajectory{side},tramat]=ea_reconstruct(patientname,options,side);



%% refit electrodes starting from first electrode (this is redundant at
%% this point).

coords_mm{side} = ea_map_coords(coords', [options.root,options.prefs.patientdir,filesep,options.prefs.tranii])';


[~,distmm]=ea_calc_distance(options.elspec.eldist,trajvector{side},tramat(1:3,1:3),[options.root,options.prefs.patientdir,filesep,options.prefs.tranii]);

comp = ea_map_coords([0,0,0;trajvector{side}]', [options.root,options.prefs.patientdir,filesep,options.prefs.tranii])'; % (XYZ_mm unaltered)

trajvector{side}=diff(comp);



normtrajvector{side}=trajvector{side}./norm(trajvector{side});

for electrode=2:4

coords_mm{side}(electrode,:)=coords_mm{side}(1,:)-normtrajvector{side}.*((electrode-1)*distmm);

end
markers(side).head=coords_mm{side}(1,:);
markers(side).tail=coords_mm{side}(4,:);


orth=null(normtrajvector{side})*(options.elspec.lead_diameter/2);

markers(side).x=coords_mm{side}(1,:)+orth(:,1)';
markers(side).y=coords_mm{side}(1,:)+orth(:,2)'; % corresponding points in reality



coords_mm=ea_resolvecoords(markers,options);

end

trajvector{side}=diff(comp);


% if length(coords_mm)==4 % only one side was processed.
% if options.sides==1
% coords_mm=[nan(4,3);coords_mm];
% elseif options.sides==2
% coords_mm=[coords_mm;nan(4,3)];
% end
% end

normtrajvector{side}=trajvector{side}./norm(trajvector{side});

for electrode=2:4

coords_mm{side}(electrode,:)=coords_mm{side}(1,:)-normtrajvector{side}.*((electrode-1)*distmm);

end
markers(side).head=coords_mm{side}(1,:);
markers(side).tail=coords_mm{side}(4,:);


orth=null(normtrajvector{side})*(options.elspec.lead_diameter/2);

markers(side).x=coords_mm{side}(1,:)+orth(:,1)';
markers(side).y=coords_mm{side}(1,:)+orth(:,2)'; % corresponding points in reality


% transform trajectory to mm space:
for side=options.sides

try
if ~isempty(trajectory{side})
trajectory{side}=ea_map_coords(trajectory{side}', [options.root,patientname,filesep,options.prefs.tranii])';
end

end
end


% save reconstruction results
elmodel=options.elmodel;
ea_save_reconstruction(coords_mm,trajectory,markers,elmodel,0,options);

coords_mm=ea_resolvecoords(markers,options);

end



% if length(coords_mm)==4 % only one side was processed.
% if options.sides==1
% coords_mm=[nan(4,3);coords_mm];
% elseif options.sides==2
% coords_mm=[coords_mm;nan(4,3)];
% end
% end






% transform trajectory to mm space:
for side=options.sides
if options.manualheightcorrection
% load reconstruction results
% try
% [coords_mm,trajectory,markers,elmodel,manually_corrected]=ea_load_reconstruction(options);
% catch
% ea_error([patientname,': No reconstruction information found. Please run reconstruction first.']);
% end
% ea_save_reconstruction(coords_mm,trajectory,markers,elmodel,0,options);


mcfig=figure('name',[patientname,': Manual Height Correction'],'numbertitle','off');
warning('off');
try
if ~isempty(trajectory{side})
trajectory{side}=ea_map_coords(trajectory{side}', [options.root,patientname,filesep,options.prefs.tranii])';
end

ea_maximize(mcfig);
end

ea_manualreconstruction(mcfig,patientname,options);

else
ea_write(options)
end


% save reconstruction results
elmodel=options.elmodel;
ea_save_reconstruction(coords_mm,trajectory,markers,elmodel,0,options);

end


if options.manualheightcorrection
% load reconstruction results
% try
% [coords_mm,trajectory,markers,elmodel,manually_corrected]=ea_load_reconstruction(options);
% catch
% ea_error([patientname,': No reconstruction information found. Please run reconstruction first.']);
% end
% ea_save_reconstruction(coords_mm,trajectory,markers,elmodel,0,options);


mcfig=figure('name',[patientname,': Manual Height Correction'],'numbertitle','off');
warning('off');
try
ea_maximize(mcfig);
end

ea_manualreconstruction(mcfig,patientname,options);
else
ea_write(options)
end

else
ea_write(options)
end
Expand Down
22 changes: 22 additions & 0 deletions ea_largestcomponent_nii.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function ea_largestcomponent_nii(fname,cnum)
% helper function that reduces the nifti components to the largest
% component.

nii=ea_load_nii(fname);
nii.img=logical(nii.img);
C=bwconncomp(nii.img);


ls=cellfun(@length,C.PixelIdxList,'Uniformoutput',0);
ls=cell2mat(ls);
[~,ix]=sort(ls);

[pth,fn,ext]=fileparts(fname);

for cc=0:cnum-1
nii.img(:)=0;
nii.img(C.PixelIdxList{ix(end-cc)})=1;
nii.fname=[pth,fn,'_c',num2str(cc),ext];
spm_write_vol(nii,nii.img);

end
Loading

0 comments on commit 5402307

Please sign in to comment.