-
Notifications
You must be signed in to change notification settings - Fork 98
/
ea_assignbackdrop.m
253 lines (220 loc) · 8.67 KB
/
ea_assignbackdrop.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
function varargout=ea_assignbackdrop(bdstring,options,subpat,native)
if ~exist('subpat','var')
subpat='Patient';
end
if ~exist('native','var')
if isfield(options,'native')
native = options.native;
else
native = 0; % default
end
end
BDlist=getbdlist;
if strcmp(bdstring, 'list')
% determine whether we are in No patient mode (could be called from
% lead group or called from an empty patient viewer / lead anatomy
if ~exist('options','var')
options.patientname='';
end
% by default assuming patient mode, preop and postop images exist.
nopatientmode=0;
haspreop=1;
haspostop=1;
% check patient mode
if isfield(options,'groupmode')
nopatientmode = options.groupmode;
elseif ~isfield(options, 'subj')
nopatientmode=1;
end
% check if preop and postop images exist
if ~nopatientmode
if ~isfile(options.subj.preopAnat.(options.subj.AnchorModality).coreg)
haspreop=0;
end
if native
if strcmp(options.subj.postopModality, 'MRI') && ~isfile(options.subj.postopAnat.ax_MRI.coreg)
haspostop = 0;
elseif strcmp(options.subj.postopModality, 'CT') && ~isfile(options.subj.postopAnat.CT.coreg)
haspostop = 0;
end
else
if strcmp(options.subj.postopModality, 'MRI') && ~isfile(options.subj.postopAnat.ax_MRI.norm)
haspostop = 0;
elseif strcmp(options.subj.postopModality, 'CT') && ~isfile(options.subj.postopAnat.CT.norm)
haspostop = 0;
end
end
end
if ~haspostop && ~haspreop
nopatientmode=1;
end
if nopatientmode
varargout{1}=ea_standardspacelist;
else
if haspreop
preopfiles = fieldnames(options.subj.coreg.anat.preop);
preop=cellfun(@(x) [subpat, ' Pre-OP (' x ')'], preopfiles, 'Uniform', 0)';
else
preop={''};
end
if haspostop
if strcmp(options.subj.postopModality, 'CT')
postop = {[subpat,' Post-OP'], [subpat,' Post-OP (Tone-mapped)']};
else
postop = {[subpat,' Post-OP']};
end
else
postop={''};
end
if native
varargout{1}=[preop, postop];
else
varargout{1}=[ea_standardspacelist, preop, postop];
end
end
if ~native
% check for additional template backdrops
for bd=1:length(BDlist{1})
varargout{1} = [varargout{1}, BDlist{2}{bd}];
end
end
% add manual choose:
varargout{1} = [varargout{1}, {'Choose...'}];
elseif regexp(bdstring, ['^', subpat,' Pre-OP \(.*\)$']) % pattern: "Patient Pre-OP (*)"
whichpreop = regexp(bdstring, ['(?<=^', subpat,' Pre-OP \()(.*)(?=\))'],'match','once');
if native
vol = spm_vol(options.subj.preopAnat.(whichpreop).coreg);
else
normImage = options.subj.preopAnat.(options.subj.AnchorModality).norm;
normImage = strrep(normImage, options.subj.AnchorModality, whichpreop);
if ~isfile(normImage)
ea_apply_normalization_tofile(options, options.subj.preopAnat.(whichpreop).coreg, normImage, 0, 1);
end
vol = spm_vol(normImage);
end
varargout{1} = vol;
varargout{2} = vol;
varargout{3} = vol;
elseif strcmp(bdstring, [subpat, ' Post-OP'])
[Vtra,Vcor,Vsag] = assignpatspecific(options, native);
varargout{1} = Vtra;
varargout{2} = Vcor;
varargout{3} = Vsag;
elseif strcmp(bdstring, [subpat, ' Post-OP (Tone-mapped)'])
[Vtra,Vcor,Vsag] = assignpatspecific(options, native, 1);
varargout{1} = Vtra;
varargout{2} = Vcor;
varargout{3} = Vsag;
elseif strcmp(bdstring, 'BigBrain 100 um ICBM 152 2009b Sym (Amunts 2013)')
% if ~ea_checkinstall('bigbrain',0,1)
% ea_error('BigBrain is not installed and could not be installed automatically. Please make sure that Matlab is connected to the internet.');
% end
% varargout{1}=spm_vol(fullfile(ea_space(options),'bigbrain_2015_100um_bb.nii'));
% varargout{2}=varargout{1};
% varargout{3}=varargout{1};
elseif regexp(bdstring, ['^',ea_getspace,' ']) % pattern: "MNI152NLin2009bAsym *"
template=lower(regexp(bdstring, '(?<= )[^\W+]+(?= \()', 'match', 'once'));
varargout{1}=spm_vol(ea_niigz(fullfile(ea_space,template)));
varargout{2}=varargout{1};
varargout{3}=varargout{1};
elseif strcmp(bdstring,'Choose...')
[file,path]=uigetfile('*.nii',"MultiSelect","off");
varargout{1}=spm_vol(ea_niigz(fullfile(path,file)));
varargout{2}=varargout{1};
varargout{3}=varargout{1};
elseif ismember(bdstring,BDlist{2})
[~,ix]=ismember(bdstring,BDlist{2});
varargout{1}=ea_load_nii([ea_space,'backdrops',filesep,BDlist{1}{ix}]);
varargout{2}=varargout{1};
varargout{3}=varargout{1};
else % custom backdrop file
varargout{1}=spm_vol(bdstring);
varargout{2}=varargout{1};
varargout{3}=varargout{1};
end
function [Vtra,Vcor,Vsag] = assignpatspecific(options, native, tonemapped)
scrfSuffix = '';
if isfile(options.subj.recon.recon)
load(options.subj.recon.recon, 'reco');
if isfield(reco, 'scrf')
scrfSuffix = 'Scrf';
end
end
if native
switch options.subj.postopModality
case 'MRI'
if ~isempty(scrfSuffix) && ~isfile(options.subj.postopAnat.ax_MRI.coregScrf)
ea_genscrfimages(options.subj, 'coreg');
end
Vtra = spm_vol(options.subj.postopAnat.ax_MRI.(['coreg', scrfSuffix]));
if isfield(options.subj.postopAnat, 'cor_MRI') && isfile(options.subj.postopAnat.cor_MRI.(['coreg', scrfSuffix]))
Vcor = spm_vol(options.subj.postopAnat.cor_MRI.(['coreg', scrfSuffix]));
else
Vcor = Vtra;
end
if isfield(options.subj.postopAnat, 'sag_MRI') && isfile(options.subj.postopAnat.sag_MRI.(['coreg', scrfSuffix]))
Vsag = spm_vol(options.subj.postopAnat.sag_MRI.(['coreg', scrfSuffix]));
else
Vsag = Vtra;
end
case 'CT'
if ~isempty(scrfSuffix) && ~isfile(options.subj.postopAnat.CT.coregScrf)
ea_genscrfimages(options.subj, 'coreg');
end
if exist('tonemapped', 'var') && tonemapped
Vtra = spm_vol(options.subj.postopAnat.CT.(['coregTonemap', scrfSuffix]));
else
Vtra = spm_vol(options.subj.postopAnat.CT.(['coreg', scrfSuffix]));
end
Vcor = Vtra;
Vsag = Vtra;
end
else
switch options.subj.postopModality
case 'MRI'
if ~isempty(scrfSuffix) && ~isfile(options.subj.postopAnat.ax_MRI.normScrf)
if ~isfile(options.subj.postopAnat.ax_MRI.coregScrf)
ea_genscrfimages(options.subj, 'coreg');
end
ea_genscrfimages(options.subj, 'norm');
end
Vtra = spm_vol(options.subj.postopAnat.ax_MRI.(['norm', scrfSuffix]));
if isfield(options.subj.postopAnat, 'cor_MRI') && isfile(options.subj.postopAnat.cor_MRI.(['norm', scrfSuffix]))
Vcor = spm_vol(options.subj.postopAnat.cor_MRI.(['norm', scrfSuffix]));
else
Vcor = Vtra;
end
if isfield(options.subj.postopAnat, 'sag_MRI') && isfile(options.subj.postopAnat.sag_MRI.(['norm', scrfSuffix]))
Vsag = spm_vol(options.subj.postopAnat.sag_MRI.(['norm', scrfSuffix]));
else
Vsag = Vtra;
end
case 'CT'
if ~isempty(scrfSuffix) && ~isfile(options.subj.postopAnat.CT.normScrf)
if ~isfile(options.subj.postopAnat.CT.coregScrf)
ea_genscrfimages(options.subj, 'coreg');
end
ea_genscrfimages(options.subj, 'norm');
end
if exist('tonemapped', 'var') && tonemapped
Vtra = spm_vol(options.subj.postopAnat.CT.(['normTonemap', scrfSuffix]));
else
Vtra = spm_vol(options.subj.postopAnat.CT.(['norm', scrfSuffix]));
end
Vcor = Vtra;
Vsag = Vtra;
end
end
function standardlist=ea_standardspacelist
spacedef=ea_getspacedef;
standardlist=cell(1,length(spacedef.templates));
for t=1:length(spacedef.templates)
standardlist{t}=[spacedef.name,' ',upper(spacedef.templates{t}),' (',spacedef.citation{1},')'];
end
function BDlist=getbdlist
BDlist=[{[]},{[]}]; % empty.
if exist([ea_space,'backdrops',filesep,'backdrops.txt'],'file')
fid=fopen([ea_space,'backdrops',filesep,'backdrops.txt']);
BDlist=textscan(fid,'%s %s');
BDlist{2}=ea_underscore2space(BDlist{2});
end