forked from netstim/leaddbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathea_autocoord.m
303 lines (248 loc) · 12.1 KB
/
ea_autocoord.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
function ea_autocoord(options)
% 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
% reconstruction routine ea_autocoord_side and lateron call functions for
% manual correction of the results, render and slice views.
% __________________________________________________________________________________
% Copyright (C) 2014 Charite University Medicine Berlin, Movement Disorders Unit
% Andreas Horn
% set patientdir
options.prefs.patientdir = options.patientname;
% get accurate electrode specifications and save it in options.
options = ea_resolve_elspec(options);
directory = [options.root,options.patientname,filesep];
if options.dicomimp || options.assignnii % do DICOM-Import.
if options.dicomimp
if strcmp(options.patientname, 'No Patient Selected')
msgbox('Please choose patient directory first!','Error','error');
else
ea_dicom_import(options);
end
end
if options.assignnii
if strcmp(options.patientname, 'No Patient Selected')
msgbox('Please choose patient directory first!','Error','error');
else
outdir = [options.root, options.patientname, filesep];
% assign image type here
di = dir([outdir,'*.nii']);
di = ea_sortbytes(di);
for d=1:length(di)
dcfilename=[outdir,di(d).name];
ea_imageclassifier({dcfilename});
end
figs=allchild(0);
ids={figs.Tag};
[~,imclassfids]=ismember(ids,'imclassf');
if ~any(imclassfids)
msgbox('All NIfTI files have been assigned already.');
else
set(figs(logical(imclassfids)),'Visible','on');
end
if isempty(di)
msgbox('Could not find any NIfTI files to rename/assign.');
end
end
end
return % For now we recommend to do import & processing in separate run calls.
end
% check connectome-mapper tags
if isfield(options,'lcm')
ea_lcm(options);
end
if ~strcmp(options.patientname,'No Patient Selected') % only 3D-rendering viewer can be opened if no patient is selected.
% move files for compatibility
try ea_compat_patfolder(options); end
% assign/order anatomical images
[options,presentfiles]=ea_assignpretra(options);
% generate grid file
if ~exist(ea_niigz([directory,'grid.nii']),'file')
try
ea_gengrid(options);
end
end
% anat preprocess, only do once.
% a small hidden file '.pp' inside patient folder will show this has been done before.
if ~exist([directory,'.pp'],'file') && ~exist([directory,'ea_normmethod_applied.mat'],'file')
% apply reorientation/cropping and biasfieldcorrection
for fi=1:length(presentfiles)
ea_anatpreprocess([directory,presentfiles{fi}]);
end
% Reslice(interpolate) preoperative anatomical image if needed
try ea_resliceanat(options); end
try
fs = fopen([directory,'.pp'],'w');
fprintf(fs,'%s','anat preprocess done');
fclose(fs);
end
end
if options.modality == 2 % CT support
options.prefs.tranii=options.prefs.ctnii;
options.prefs.tranii_unnormalized=options.prefs.rawctnii_unnormalized;
if options.coregct.do && ~ea_coreglocked(options,['tp_',options.prefs.ctnii_coregistered]);
eval([options.coregct.method,'(options)']); % triggers the coregct function and passes the options struct to it.
ea_dumpnormmethod(options,options.coregct.method,'coregctmethod');
ea_tonemapct_file(options,'native'); % (Re-) compute tonemapped (native space) CT
ea_gencoregcheckfigs(options); % generate checkreg figures
end
end
if options.coregmr.do
% 1. coreg all available preop MRI
ea_checkcoregallmri(options,0,1); % check and coregister all preoperative MRIs here.
% 2. then coreg postop MRI to preop MRI
ea_coregmr(options);
end
if options.coregmr.check
options.normcoreg='coreg';
ea_checkcoreg(options);
end
if options.normalize.do
if ~(ea_coreglocked(options,'glanat')==2) || strcmp(options.normalize.method,'ea_normalize_apply_normalization') % =2 means permanent lock for normalizations and only happens if all preop anatomy files were approved at time of approving normalization.
if ea_coreglocked(options,'glanat')==1 && ~strcmp(options.normalize.method,'ea_normalize_apply_normalization') % in this case, only perform normalization if using a multispectral approach now.
[~,~,~,doit]=eval([options.normalize.method,'(''prompt'')']);
else
doit=1;
end
if doit || strcmp(options.normalize.method,'ea_normalize_apply_normalization')
clear doit
% 3. finally perform normalization based on dominant or all preop MRIs
ea_dumpnormmethod(options,options.normalize.method,'normmethod'); % has to come first due to applynormalization.
eval([options.normalize.method,'(options)']); % triggers the normalization function and passes the options struct to it.
if options.modality == 2 % (Re-) compute tonemapped (normalized) CT
ea_tonemapct_file(options,'mni');
end
% 4. generate coreg-check figs (all to all).
ea_gencoregcheckfigs(options); % generate checkreg figures
end
end
end
if isfield(options,'gencheckreg') % this case is an exception when calling from the Tools menu.
if options.gencheckreg
ea_gencoregcheckfigs(options); % generate checkreg figures
end
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_norm_ptspecific_atl(options)
end
if options.normalize.check
% export "control" niftis with wireframe of normal anatomy..
options.normcoreg='normalize';
ea_checkcoreg(options);
end
if options.doreconstruction
ea_checkfiles(options);
switch options.reconmethod
case 1 % TRAC/CORE
for side=options.sides
%try
% call main routine reconstructing trajectory for one side.
[coords,trajvector{side},trajectory{side},tramat]=ea_reconstruct(options.patientname,options,side);
% refit electrodes starting from first electrode (this is redundant at this point).
coords_mm{side} = ea_map_coords(coords', [directory,options.prefs.tranii])';
[~,distmm]=ea_calc_distance(options.elspec.eldist,trajvector{side},tramat(1:3,1:3),[directory,options.prefs.tranii]);
comp = ea_map_coords([0,0,0;trajvector{side}]', [directory,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
% transform trajectory to mm space:
for side=1:length(options.sides)
try
if ~isempty(trajectory{side})
trajectory{side}=ea_map_coords(trajectory{side}', [directory,options.prefs.tranii])';
end
end
end
options.hybridsave=1;
% save reconstruction results
ea_methods(options,...
['DBS-Electrodes were automatically pre-localized in native & template space using Lead-DBS software',...
' (Horn & Kuehn 2015; SCR_002915; http://www.lead-dbs.org).'],...
{'Horn, A., & Kuehn, A. A. (2015). Lead-DBS: a toolbox for deep brain stimulation electrode localizations and visualizations. NeuroImage, 107, 127?135. http://doi.org/10.1016/j.neuroimage.2014.12.002'});
case 2 % PaCER
elecmodels=PaCER([options.root,options.patientname,filesep,options.prefs.ctnii_coregistered],'finalDegree',1,'electrodeType',ea_mod2pacermod(options.elmodel));
for side=options.sides
coords_mm{side}=elecmodels{side}.getContactPositions3D;
for dim=1:3
trajectory{side}(:,dim)=linspace(coords_mm{side}(1,dim),coords_mm{side}(1,dim)+10*(coords_mm{side}(1,dim)-coords_mm{side}(end,dim)),20);
end
markers(side).head=coords_mm{side}(1,:);
markers(side).tail=coords_mm{side}(4,:);
normtrajvector{side}=(coords_mm{side}(1,:)-coords_mm{side}(end,:))/...
norm((coords_mm{side}(1,:)-coords_mm{side}(end,:)));
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
end
options.native=1;
options.hybridsave=1;
ea_methods(options,...
['DBS-Electrodes were automatically pre-localized in native & template space using the PaCER algorithm',...
' (Husch to appear; http://adhusch.github.io/PaCER/).'],...
{'Husch (to appear). PaCER - A fully automated method for electrode trajectory and contact reconstruction in deep brain stimulation.'});
end
elmodel=options.elmodel;
ea_save_reconstruction(coords_mm,trajectory,markers,elmodel,0,options);
if isfield(options,'hybridsave')
options=rmfield(options,'hybridsave');
end
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',[options.patientname,': Manual Height Correction'],'numbertitle','off');
%warning('off');
try
ea_maximize(mcfig);
end
ea_manualreconstruction(mcfig,options.patientname,options);
else
ea_write(options)
end
else
ea_write(options)
end
function di=ea_sortbytes(di)
if isempty(di)
return
end
for d=1:length(di)
bytesc(d)=di(d).bytes;
end
[~,order]=sort(bytesc,'ascend');
di=di(order);
function model=ea_mod2pacermod(model)
% current dictionary to translate between Lead-DBS and PaCER nomenclature.
% Hoping to standardize this in the future.
switch model
case 'Medtronic 3389'
% pass through (same nomenclature)
case 'Medtronic 3387'
% pass through (same nomenclature)
case 'Boston Scientific Vercise Directed'
model='Boston Vercise Directional';
otherwise
model=''; % 'Unkown Electrode Type'
end