forked from andypotatohy/roast
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvisualizeRes.m
More file actions
257 lines (223 loc) · 10.2 KB
/
Copy pathvisualizeRes.m
File metadata and controls
257 lines (223 loc) · 10.2 KB
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
function visualizeRes(P1,P2,T2,node,elem,face,inCurrent,hdrInfo,uniTag,showAll,varargin)
% visualizeRes(P1,P2,T2,node,elem,face,inCurrent,hdrInfo,uniTag,showAll,varargin)
%
% Display the simulation results. The 3D rendering is displayed in the
% world space, while the slice view is done in the voxel space.
%
% (c) Yu (Andy) Huang, Parra Lab at CCNY
% yhuang16@citymail.cuny.edu
% April 2018
% August 2019 callable by roast_target()
if ndims(varargin{1})==3
isRoast = 1;
vol_all = varargin{1}; ef_mag = varargin{2}; ef_all = varargin{3};
else
isRoast = 0;
C = varargin{1}; ef_mag = varargin{2}; ef_all = varargin{3}; targetCoord = varargin{4};
end
[dirname,baseFilename] = fileparts(P1);
if isempty(dirname), dirname = pwd; end
[~,baseFilenameRasRSPD] = fileparts(P2);
if isempty(T2)
baseFilenameRasRSPD = [baseFilenameRasRSPD '_T1orT2'];
else
baseFilenameRasRSPD = [baseFilenameRasRSPD '_T1andT2'];
end
mappingFile = [dirname filesep baseFilenameRasRSPD '_seg8.mat'];
if ~exist(mappingFile,'file')
error(['Mapping file ' mappingFile ' from SPM not found. Please check if you run through SPM segmentation in ROAST.']);
else
load(mappingFile,'image','Affine');
mri2mni = Affine*image(1).mat;
% mapping from MRI voxel space to MNI space
end
if showAll
if ~strcmp(baseFilename,'nyhead')
disp('showing MRI and segmentations...');
data = load_untouch_nii(P2); sliceshow(data.img,[],'gray',[],[],'MRI: Click anywhere to navigate.',[],mri2mni); drawnow
if ~isempty(T2) %T2 specified
data = load_untouch_nii(T2);
sliceshow(data.img,[],'gray',[],[],'MRI: T2. Click anywhere to navigate.',[],mri2mni); drawnow
end
else
disp('NEW YORK HEAD selected, there is NO MRI for it to show.')
end
end
masks = load_untouch_nii([dirname filesep baseFilenameRasRSPD '_masks.nii']);
allMask = masks.img;
numOfTissue = 6; % hard coded across ROAST. max(allMask(:));
if isRoast
gel = load_untouch_nii([dirname filesep baseFilename '_' uniTag '_mask_gel.nii']);
numOfGel = max(gel.img(:));
elec = load_untouch_nii([dirname filesep baseFilename '_' uniTag '_mask_elec.nii']);
% numOfElec = max(elec.img(:));
else
numOfGel = length(inCurrent);
indMonElec = find(abs(inCurrent)>1e-3); % this is not perfect
end
if showAll
allMaskShow = masks.img;
allMaskShow(gel.img>0) = numOfTissue + 1;
allMaskShow(elec.img>0) = numOfTissue + 2;
sliceshow(allMaskShow,[],[],[],'Tissue index','Segmentation. Click anywhere to navigate.',[],mri2mni)
drawnow
end
% node = node + 0.5; already done right after mesh
% scrsz = get(groot,'ScreenSize');
disp('generating 3D renderings...')
for i=1:3, node(:,i) = node(:,i)/hdrInfo.pixdim(i); end
% convert pseudo-world coordinates back to voxel coordinates so that the
% following conversion to pure-world space is meaningful
voxCoord = [node(:,1:3) ones(size(node,1),1)];
worldCoord = (hdrInfo.v2w*voxCoord')';
% do the 3D rendering in world space, to avoid confusion in left-right;
% sliceshow below is still in voxel space though
node(:,1:3) = worldCoord(:,1:3);
indNode_grayFace = face(find(face(:,4) == 2),1:3);
indNode_grayElm = elem(find(elem(:,5) == 2),1:4);
% node(:,1:3) = sms(node(:,1:3),indNode_grayFace);
% % smooth the surface that's to be displayed
% % just for display, the output data is not smoothed
% % very slow if mesh is big
if isRoast
indNode_elecFace = face(find(face(:,4) > numOfTissue+numOfGel),1:3);
indNode_elecElm = elem(find(elem(:,5) > numOfTissue+numOfGel),1:4);
inCurrentRange = [min(inCurrent) max(inCurrent)];
fid = fopen([dirname filesep baseFilename '_' uniTag '_v.pos']);
fgetl(fid);
C = textscan(fid,'%d %f');
fclose(fid);
C{2} = C{2} - min(C{2}); % re-reference the voltage
% dataShow = [node(C{1},1:3) C{2}];
color = nan(size(node,1),1);
color(C{1}) = C{2};
dataShow = [node(:,1:3) color];
figName = ['Voltage in Simulation: ' uniTag];
figure('Name',[figName '. Move your mouse to rotate.'],'NumberTitle','off');
set(gcf,'color','w');
colormap(jet);
plotmesh(dataShow,indNode_grayFace,indNode_grayElm,'LineStyle','none');
dataShowRange = [min(dataShow(unique(indNode_grayElm(:)),4)) max(dataShow(unique(indNode_grayElm(:)),4))];
dataShowForElec = interp1(inCurrentRange,dataShowRange,inCurrent);
for i=1:length(inCurrent)
% indNodeTemp = indNode_elecElm(find(label_elec==i),:);
% dataShow(unique(indNodeTemp(:)),4) = dataShowForElec(i);
dataShow(unique(elem(find(elem(:,5) == numOfTissue+numOfGel+i),1:4)),4) = dataShowForElec(i);
end % to show injected current intensities properly
hold on;
plotmesh(dataShow,indNode_elecFace,indNode_elecElm,'LineStyle','none');
axis off; rotate3d on;
% set(hp1,'SpecularColorReflectance',0,'SpecularExponent',50);
caxis(dataShowRange);
lightangle(-90,45)
lightangle(90,45)
lightangle(-90,-45)
hc1 = colorbar; set(hc1,'FontSize',18,'YAxisLocation','right');
title(hc1,'Voltage (mV)','FontSize',18);
a1 = gca;
a2 = axes('Color','none','Position',get(a1,'Position'),'XLim',get(a1,'XLim'),'YLim',get(a1,'YLim'),'ZLim',get(a1,'ZLim'));
axis off;
hc2 = colorbar; set(hc2,'FontSize',18,'YAxisLocation','right','Location','westoutside');
title(hc2,'Injected current (mA)','FontSize',18);
caxis(inCurrentRange);
axes(a1);
drawnow
fid = fopen([dirname filesep baseFilename '_' uniTag '_e.pos']);
fgetl(fid);
C = textscan(fid,'%d %f %f %f');
fclose(fid);
% dataShow = [node(C{1},1:3), C_ef_mag];
color = nan(size(node,1),1);
color(C{1}) = sqrt(C{2}.^2+C{3}.^2+C{4}.^2);
dataShow = [node(:,1:3) color];
figName = ['Electric field in Simulation: ' uniTag];
figure('Name',[figName '. Move your mouse to rotate.'],'NumberTitle','off');
set(gcf,'color','w');
colormap(jet);
plotmesh(dataShow,indNode_grayFace,indNode_grayElm,'LineStyle','none');
% dataShowVal = dataShow(unique(indNode_grayElm(:)),4);
dataShowRange = [min(dataShow(unique(indNode_grayElm(:)),4)) prctile(dataShow(unique(indNode_grayElm(:)),4),95)];
dataShowForElec = interp1(inCurrentRange,dataShowRange,inCurrent);
for i=1:length(inCurrent)
% indNodeTemp = indNode_elecElm(find(label_elec==i),:);
% dataShow(unique(indNodeTemp(:)),4) = dataShowForElec(i);
dataShow(unique(elem(find(elem(:,5) == numOfTissue+numOfGel+i),1:4)),4) = dataShowForElec(i);
end % to show injected current intensities properly
hold on;
plotmesh(dataShow,indNode_elecFace,indNode_elecElm,'LineStyle','none');
axis off; rotate3d on;
% set(hp2,'SpecularColorReflectance',0,'SpecularExponent',50);
caxis(dataShowRange);
lightangle(-90,45)
lightangle(90,45)
lightangle(-90,-45)
hc1 = colorbar; set(hc1,'FontSize',18,'YAxisLocation','right');
title(hc1,'Electric field (V/m)','FontSize',18);
a1 = gca;
a2 = axes('Color','none','Position',get(a1,'Position'),'XLim',get(a1,'XLim'),'YLim',get(a1,'YLim'),'ZLim',get(a1,'ZLim'));
axis off;
hc2 = colorbar; set(hc2,'FontSize',18,'YAxisLocation','right','Location','westoutside');
title(hc2,'Injected current (mA)','FontSize',18);
caxis(inCurrentRange);
axes(a1);
drawnow
else
indNode_elecFace = face(ismember(face(:,4),numOfTissue+numOfGel+indMonElec),1:3);
indNode_elecElm = elem(ismember(elem(:,5),numOfTissue+numOfGel+indMonElec),1:4);
inCurrentRange = [min(inCurrent) max(inCurrent)];
color = nan(size(node,1),1);
color(C(:,1)) = sqrt(sum(C(:,2:4).^2,2));
dataShow = [node(:,1:3) color];
figName = ['Electric field in Targeting: ' uniTag];
figure('Name',[figName '. Move your mouse to rotate.'],'NumberTitle','off');
set(gcf,'color','w');
colormap(jet);
plotmesh(dataShow,indNode_grayFace,indNode_grayElm,'LineStyle','none');
% dataShowVal = dataShow(unique(indNode_grayElm(:)),4);
dataShowRange = [min(dataShow(unique(indNode_grayElm(:)),4)) prctile(dataShow(unique(indNode_grayElm(:)),4),95)];
dataShowForElec = interp1(inCurrentRange,dataShowRange,inCurrent(indMonElec));
for i=1:length(indMonElec)
% indNodeTemp = indNode_elecElm(find(label_elec==i),:);
% dataShow(unique(indNodeTemp(:)),4) = dataShowForElec(i);
dataShow(unique(elem(find(elem(:,5) == numOfTissue+numOfGel+indMonElec(i)),1:4)),4) = dataShowForElec(i);
end % to show injected current intensities properly
hold on;
plotmesh(dataShow,indNode_elecFace,indNode_elecElm,'LineStyle','none');
axis off; rotate3d on;
% set(hp2,'SpecularColorReflectance',0,'SpecularExponent',50);
caxis(dataShowRange);
lightangle(-90,45)
lightangle(90,45)
lightangle(-90,-45)
hc1 = colorbar; set(hc1,'FontSize',18,'YAxisLocation','right');
title(hc1,'Electric field (V/m)','FontSize',18);
a1 = gca;
a2 = axes('Color','none','Position',get(a1,'Position'),'XLim',get(a1,'XLim'),'YLim',get(a1,'YLim'),'ZLim',get(a1,'ZLim'));
axis off;
hc2 = colorbar; set(hc2,'FontSize',18,'YAxisLocation','right','Location','westoutside');
title(hc2,'Injected current (mA)','FontSize',18);
caxis(inCurrentRange);
axes(a1);
drawnow
end
disp('generating slice views...');
brain = (allMask==1 | allMask==2);
nan_mask_brain = nan(size(brain));
nan_mask_brain(find(brain)) = 1;
cm = colormap(jet(2^11)); cm = [1 1 1;cm];
if isRoast
figName = ['Voltage in Simulation: ' uniTag];
sliceshow(vol_all.*nan_mask_brain,[],cm,[],'Voltage (mV)',[figName '. Click anywhere to navigate.'],[],mri2mni); drawnow
end
for i=1:size(ef_all,4), ef_all(:,:,:,i) = ef_all(:,:,:,i).*nan_mask_brain; end
ef_mag = ef_mag.*nan_mask_brain;
dataShowVal = ef_mag(~isnan(ef_mag(:)));
if isRoast
figName = ['Electric field in Simulation: ' uniTag];
sliceshow(ef_mag,[],cm,[min(dataShowVal) prctile(dataShowVal,95)],'Electric field (V/m)',[figName '. Click anywhere to navigate.'],ef_all,mri2mni); drawnow
else
for i=1:size(targetCoord,1)
figName = ['Electric field at Target ' num2str(i) ' in Targeting: ' uniTag];
sliceshow(ef_mag,targetCoord(i,:),cm,[min(dataShowVal) prctile(dataShowVal,95)],'Electric field (V/m)',[figName '. Click anywhere to navigate.'],ef_all,mri2mni); drawnow
end
end