-
Notifications
You must be signed in to change notification settings - Fork 0
/
ntools_elec_plotGroup.m
326 lines (278 loc) · 9.58 KB
/
ntools_elec_plotGroup.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
function ntools_elec_plotGroup(varargin)
% ntools_elec_plotGroup(elec_file,surf_file,'labelshow',0/1,'genimg',0/1)
%
% a stand-alone program that shows ieeg electrodes on the pial surface and
% save the images into textfile folder/images/. Default saving name is
% PatientID(NYxx)_space_elecLabel_viewpoint_hemisphere.png
%
% space could be T1 or MNI
% elecLabel: numbers in the input elec_file, e.g. 1,2,3,4,5. Type the
% group name when the script asks for description
% viewpoint: could be left, right, top, below, back or front
% hemisphere: could be lh, rh, or both
%
% required input:
% elec_text: text or xls(x) file with xyz electrode coordinates
% pial_mat: matlab structure with pial surface
% plot: elecLabel to plot, e.g. 2,4
% labelshow: to show the electrode labels (1) or not (0)
% genimg: to save the images (1) or not (0)
% groupname: How do you want to describe these electrodes, e.g.
% 'seizure onset', or leave it blank ([])
%
% Usage: run ntools_elec_plot in command window
% the gui is prompted out for file selection
% or: ntools_elec_plot(textfilename, {'lh.mat', 'rh.mat'})
% ntools_elec_plot(textfilename,lh.mat,'plot',[2,3,9],'labelshow',1,'genimg',1,'groupname','seizure onset');
%
% by Hugh Wang, Xiuyuan.Wang@nyumc.org, Aug 13th, 2013
%
% change colorbar properties according to MATLAB R2014b colorbar object
% modified by Hugh Wang, July 27th, 2016
%% Get the elec info
if nargin==0
[FileName,PathName] = uigetfile({'*.xlsx';'*.txt';'*.xls'},'Select the electrodes text file',pwd);
[surfname, surfpath] = uigetfile('*.mat','Select the patient brain surf',PathName,'MultiSelect','on');
surf = strcat(surfpath,surfname);
elseif nargin>=2
aa = strfind(varargin{1},'/');
if isempty(aa)
PathName = pwd;
FileName = varargin{1};
else
FileName = varargin{1}(aa(end)+1:end);
PathName = varargin{1}(1:aa(end));
end
surf = varargin{2};
try labelshow = varargin{find(strcmp('labelshow',varargin))+1}; catch err; end
try genimg = varargin{find(strcmp('genimg',varargin))+1}; catch err; end
try showpart = varargin{find(strcmp('groupname',varargin))+1}; catch err; end
try plt = varargin{find(strcmp('plot',varargin))+1}; catch err; end
end
if exist(fullfile(PathName, FileName),'file')
[~,~,ext] = fileparts(FileName);
if strcmpi(ext,'.txt')
fid = fopen(fullfile(PathName, FileName));
elec_all = textscan(fid,'%s %f %f %f %f');
elec_cell = [elec_all{1},num2cell(elec_all{2}),num2cell(elec_all{3}),num2cell(elec_all{4})];
fclose(fid);
elseif strcmpi(ext,'.xls') || strcmpi(ext,'.xlsx')
[~,~,elec_all] = xlsread(fullfile(PathName,FileName));
elec_cell = [elec_all{1},num2cell(elec_all{2}),num2cell(elec_all{3}),num2cell(elec_all{4})];
end
else
elec_cell = [];
end
%% Get the filename info
b = strfind(FileName,'_');
Pname = FileName(1:b(1)-1);
if ~isempty(strfind(upper(FileName),'T1'))
space = '_T1_';
elseif ~isempty(strfind(upper(FileName),'MNI'))
space = '_MNI_';
else
space = '_';
end
if length(surf)==2
sph = 'both';
else
sph = regexp(lower(surf),'[r,l]h','match');
sph = char(sph{:});
end
%% Separate the elecs by category numbers
catnum = elec_all{5};
uninum = unique(catnum,'sorted');
%uninum = unique(catnum);
cmap = jet(length(uninum));
%% Plot the elecs
if ~exist('plt','var')
plt = input(sprintf('What category do you want to plot? (%s~%s): ',num2str(uninum(1)),num2str(uninum(end))),'s');
plt = str2num(plt);
end
if ~exist('showpart','var')
showpart = input('How do you want to describe these electrodes? (e.g. seizure onset): ','s');
end
showpart = regexprep(showpart,' ','_');
if ~exist('labelshow','var')
labelshow = menu('Do you want to show the label?','Yes','No');
end
if ~exist('genimg','var')
genimg = menu('Do you want to save the images?','Yes', 'No');
end
if strcmp(sph,'both')
surf_brain.sph1 = load(surf{1});
surf_brain.sph2 = load(surf{2});
else
surf_brain = load(surf);
end
nyu_plot(surf_brain,sph,[],[]);
for i=1:length(plt)
idx = find(catnum==plt(i));
elec = cell2mat(elec_cell(idx,2:4));
elec_name = char(elec_cell(idx,1));
col = cmap(uninum==plt(i),:);
for j=1:size(elec,1)
plotSpheres(elec(j,1),elec(j,2),elec(j,3),2,col);
if labelshow==1
[xx, yy, zz] = adjust_elec_label(elec(j,:)); % default radius = 2
text('Position',[xx yy zz],'String',elec_name(j,:),'Color','w','VerticalAlignment','top');
end
end
hold on;
end
colormap(cmap);
% cbar = colorbar('YTickLabel',{uninum},'Clim',[uninum(1),uninum(end)]);
% ytick_range = get(cbar,'YLim');
% interval = (ytick_range(2)-ytick_range(1))/(length(uninum));
% ytick = [ytick_range(1):interval:ytick_range(2)]+interval/2;
% set(cbar,'YTick',ytick);
% starting 2014b, colorbar returns a colorbar object instead of axis
cbar = colorbar('TickLabels',num2cell(uninum));
ytick_range = get(cbar,'Limits');
interval = (ytick_range(2)-ytick_range(1))/(length(uninum));
ytick = [ytick_range(1):interval:ytick_range(2)]+interval/2;
set(cbar,'Ticks',ytick);
hold off;
%% save images
if genimg==1
if ~exist([PathName '\images\'],'dir')
mkdir([PathName '\images\']);
end
if labelshow==1
label = '_label';
else
label = [];
end
format = 'fig';
if strcmp(sph,'lh')
view(270, 0);
saveas(gcf,[PathName,'\images\',Pname,space,showpart,'_lateral_',sph,label],format);
view(90,0);
saveas(gcf,[PathName,'\images\',Pname,space,showpart,'_mesial_',sph,label],format);
elseif strcmp(sph,'rh')
view(270, 0);
saveas(gcf,[PathName,'\images\',Pname,space,showpart,'_mesial_',sph,label],format);
view(90,0);
saveas(gcf,[PathName,'\images\',Pname,space,showpart,'_lateral_',sph,label],format);
elseif strcmp(sph,'both')
view(270, 0);
saveas(gcf,[PathName,'\images\',Pname,space,showpart,'_left_',sph,label],format);
view(90,0);
saveas(gcf,[PathName,'\images\',Pname,space,showpart,'_right_',sph,label],format);
end
view(0,0);
saveas(gcf,[PathName,'\images\',Pname,space,showpart,'_posterior_',sph,label],format);
view(180,0);
saveas(gcf,[PathName,'\images\',Pname,space,showpart,'_frontal_',sph,label],format);
view(90,90);
saveas(gcf,[PathName,'\images\',Pname,space,showpart,'_dorsal_',sph,label],format);
view(90,-90);
set(light,'Position',[1 0 -1]);
saveas(gcf,[PathName,'\images\',Pname,space,showpart,'_ventral_',sph,label],format);
else
return;
end
end
%% subfunction
%% nyu_plot
function nyu_plot(surf_brain,sph,elec,elecname,color,label,radius,alpha)
if ~exist('color','var')
color = 'w';
end
if ~exist('label','var')
label = 0;
end
if ~exist('alpha','var')
alpha = 1;
end
if ~exist('radius','var')
radius = 2;
end
figure;
col = [.7 .7 .7];
if strcmp(sph,'both')
sub_sph1.vert = surf_brain.sph1.coords;
sub_sph1.tri = surf_brain.sph1.faces;
sub_sph2.vert = surf_brain.sph2.coords;
sub_sph2.tri = surf_brain.sph2.faces;
col1=repmat(col(:)', [size(sub_sph1.vert, 1) 1]);
col2=repmat(col(:)', [size(sub_sph2.vert, 1) 1]);
trisurf(sub_sph1.tri, sub_sph1.vert(:, 1), sub_sph1.vert(:, 2),sub_sph1.vert(:, 3),...
'FaceVertexCData', col1,'FaceColor', 'interp','FaceAlpha',alpha);
hold on;
trisurf(sub_sph2.tri, sub_sph2.vert(:, 1), sub_sph2.vert(:, 2), sub_sph2.vert(:, 3),...
'FaceVertexCData', col2,'FaceColor', 'interp','FaceAlpha',alpha);
else
if isfield(surf_brain,'coords')==0
sub.vert = surf_brain.surf_brain.coords;
sub.tri = surf_brain.surf_brain.faces;
else
sub.vert = surf_brain.coords;
sub.tri = surf_brain.faces;
end
col=repmat(col(:)', [size(sub.vert, 1) 1]);
trisurf(sub.tri, sub.vert(:, 1), sub.vert(:, 2), sub.vert(:, 3),...
'FaceVertexCData', col,'FaceColor', 'interp','FaceAlpha',alpha);
end
shading interp;
lighting gouraud;
material dull;
light;
axis off
hold on;
for i=1:size(elec,1)
plotSpheres(elec(i,1),elec(i,2),elec(i,3),radius,color);
if label==1
[x, y, z] = adjust_elec_label(elec(i,:),radius);
text('Position',[x y z],'String',elecname(i,:),'Color','w','VerticalAlignment','top');
end
end
set(light,'Position',[-1 0 1]);
if strcmp(sph,'lh')
view(270, 0);
elseif strcmp(sph,'rh')
view(90,0);
elseif strcmp(sph,'both')
view(90,90);
end
set(gcf, 'color','white','InvertHardCopy', 'off');
axis tight;
axis equal;
end
%%
function [shand]=plotSpheres(spheresX, spheresY, spheresZ, spheresRadius,varargin)
if nargin>4,
col=varargin{:};
end
spheresRadius = ones(length(spheresX),1).*spheresRadius;
% set up unit sphere information
numSphereFaces = 25;
[unitSphereX, unitSphereY, unitSphereZ] = sphere(numSphereFaces);
sphereCount = length(spheresRadius);
% for each given sphere, shift the scaled unit sphere by the
% location of the sphere and plot
for i=1:sphereCount
sphereX = spheresX(i) + unitSphereX*spheresRadius(i);
sphereY = spheresY(i) + unitSphereY*spheresRadius(i);
sphereZ = spheresZ(i) + unitSphereZ*spheresRadius(i);
%shand=surface(sphereX, sphereY, sphereZ,'FaceColor',col,'EdgeColor',col,'Tag','btq_sphere');
shand=surface(sphereX, sphereY, sphereZ,'FaceColor',col,'EdgeColor','none','AmbientStrength',0.7);
end
end
%% adjust_elec_label
function [x, y, z] = adjust_elec_label(elec,radius)
if ~exist('radius','var')
radius = 2;
end
if elec(1)>0
x = elec(1)+radius;
else
x = elec(1)-radius;
end
if elec(3)>0
z = elec(3)+radius;
else
z = elec(3)-radius;
end
y = elec(2);
end