-
Notifications
You must be signed in to change notification settings - Fork 1
/
prepAS.m
205 lines (204 loc) · 7.08 KB
/
prepAS.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
%% ---- Prepare automated segmentation masks ---- %%
% This function is to prepare the automated segmentations masks
% that we are going to use in the
% study "The Reproducibility of deep learning-based segmentation of the
% prostate on T2-weighted MR images".
%
% Sunoqrot, M.R.S.; Selnæs, K.M.; Sandsmark, E.; Langørgen, S.; Bertilsson,
% H.; Bathen, T.F.; Elschot, M. The Reproducibility of Deep Learning-Based
% Segmentation of the Prostate Gland and Zones on T2-Weighted MR Images.
% Diagnostics 2021, 11, 1690. https://doi.org/10.3390/diagnostics11091690
% https://www.mdpi.com/2075-4418/11/9/1690
%
% By Mohammed R. S. Sunoqrot, MR cancer group, NTNU, Trondheim, Norway
% 23.Nov.2020
%
% Input:
% 1. masterPath: The path to the master analysis file. (string)
%
function prepAS(masterPath)
%% Paths
% The main output folder
asPath = fullfile(masterPath,'Data','Segmentation','Automated');
if ~exist(asPath,'dir')
mkdir(asPath)
end
% The automated segmentations path
aSegmentationPath = fullfile(masterPath,'Automated');
%% Types
type1 = {'V-Net'};
type2 = {'nnU-Net'};
%% Type 1
%-- Copy and correct WP and PZ masks
% Loop over the segmenation networks
aD = dir(aSegmentationPath);
aD = aD(~ismember({aD.name},{'.','..'}));
aD = aD(~contains({aD.name},type2));
for ii = 1:numel(aD)
% Network output folder
oN = fullfile(asPath,aD(ii).name);
if ~exist(oN,'dir')
mkdir(oN)
end
% Loop over the region
nD = dir(fullfile(aSegmentationPath,aD(ii).name));
nD = nD(~ismember({nD.name},{'.','..'}));
for jj = 1:numel(nD)
% Region output folder
oR = fullfile(oN,nD(jj).name);
if ~exist(oR,'dir')
mkdir(oR)
end
% Loop over scans
rD = dir(fullfile(aSegmentationPath,aD(ii).name,nD(jj).name));
rD = rD(~ismember({rD.name},{'.','..'}));
for kk = 1:numel(rD)
% Scan output folder
oS = fullfile(oN,nD(jj).name,rD(kk).name);
if ~exist(oS,'dir')
mkdir(oS)
end
% Loop over cases
sD = dir(fullfile(aSegmentationPath,aD(ii).name,nD(jj).name,rD(kk).name));
sD = sD(~ismember({sD.name},{'.','..'}));
sD = sD(~contains({sD.name},'tested'));
for ll = 1:numel(sD)
source = fullfile(aSegmentationPath,aD(ii).name,...
nD(jj).name,rD(kk).name,sD(ll).name);
target = fullfile(oS,sD(ll).name);
if ~isfile(target)
% to make it readable with matlab later and save in
% target
[StrData, ~, ~] = elxMetaIOFileToStrDatax(source, 0);
[~, ~] = elxStrDataxToMetaIOFile(StrData, target, 0);
end
end
end
end
end
%-- Create TZ
% Loop over the segmenation networks
for ii = 1:numel(aD)
% Loop over the scans
scan = {'Scan1','Scan2'};
for jj = 1:numel(scan)
% Loop over cases
wpD = dir(fullfile(asPath,aD(ii).name,'WP',scan{jj},'*.mhd'));
pzD = dir(fullfile(asPath,aD(ii).name,'PZ',scan{jj},'*.mhd'));
tzD = fullfile(asPath,aD(ii).name,'TZ',scan{jj});
if ~exist(tzD,'dir')
mkdir(tzD)
end
for kk = 1:numel(wpD)
wpF = fullfile(wpD(kk).folder,wpD(kk).name);
pzF = fullfile(pzD(kk).folder,pzD(kk).name);
tzF = fullfile(tzD,wpD(kk).name);
if ~isfile(tzF)
% Read WP and PZ
[wp, ~, ~] = elxMetaIOFileToStrDatax(wpF, 0);
[pz, ~, ~] = elxMetaIOFileToStrDatax(pzF, 0);
tz = wp;
tz.Data(pz.Data==1) = 0;
[~, ~] = elxStrDataxToMetaIOFile(tz, tzF, 0);
end
end
end
end
%% Type 2
% Loop over the segmenation networks
aD = dir(aSegmentationPath);
aD = aD(~ismember({aD.name},{'.','..'}));
aD = aD(~contains({aD.name},type1));
for ii = 1:numel(aD)
% Network output folder
oN = fullfile(asPath,aD(ii).name);
if ~exist(oN,'dir')
mkdir(oN)
end
% Loop over scans
rD = dir(fullfile(aSegmentationPath,aD(ii).name));
rD = rD(~ismember({rD.name},{'.','..'}));
for kk = 1:numel(rD)
% Scan output folder
oS = fullfile(masterPath,'Temp',aD(ii).name,rD(kk).name);
mkdir(oS)
% Target output paths
oSwp = fullfile(oN,'WP',rD(kk).name);
oSpz = fullfile(oN,'PZ',rD(kk).name);
oStz = fullfile(oN,'TZ',rD(kk).name);
if ~exist(oSwp,'dir')
mkdir(oSwp)
end
if ~exist(oSpz,'dir')
mkdir(oSpz)
end
if ~exist(oStz,'dir')
mkdir(oStz)
end
% Activate environment to use python
if isunix
setenv('PATH','.../Anaconda/anaconda3/envs/qcs/bin');
end
% Wrtie paths to txt file to read in python
fileID = fopen(fullfile(masterPath,'paths.txt'),'w');
fprintf(fileID,'%s\n',...
fullfile(aSegmentationPath,aD(ii).name,rD(kk).name));
fprintf(fileID,'%s\n',oS);
fclose(fileID);
% Use SimpleITK from python to conver nii.gz to mhd and
% copy file
[~,~] = system("python niigztomhdnnUNet.py");
% reset default environment
if isunix
setenv('PATH','/usr/bin');
rehash toolboxcache;
savepath
end
delete paths.txt
% Correct masks header, Delete some tags to enable ElastixFromMatlab toolbox
% Loop over the cases
dirlist = dir(fullfile(oS,'*.mhd'));
A = cell(1,1);
for mm = 1:numel(dirlist)
filename = fullfile(dirlist(mm).folder,dirlist(mm).name);
% Read mhd header into cell A
fid = fopen(filename,'r');
i = 1;
tline = fgetl(fid);
A{i} = tline;
while ischar(tline)
i = i+1;
tline = fgetl(fid);
A{i} = tline;
end
fclose(fid);
% Change cell A and save in B
B = {A{1:10},A{58:60}}; % new cell array with the wanted lines
% Write cell B into mhd header
fid = fopen(filename, 'w');
for i = 1:numel(B)
fprintf(fid,'%s\n', B{i});
end
fclose(fid);
% Get WP, PZ and TZ masks and save them to there target
[StrDatax, ~, ~] = elxMetaIOFileToStrDatax(filename, 0);
WP = StrDatax;
WP.Data = zeros(size(WP.Data));
PZ = StrDatax;
PZ.Data = zeros(size(PZ.Data));
TZ = StrDatax;
TZ.Data = zeros(size(TZ.Data));
% Get masks
WP.Data(StrDatax.Data>0) = 1;
PZ.Data(StrDatax.Data==1) = 1;
TZ.Data(StrDatax.Data==2) = 1;
% Save
[~, ~] = elxStrDataxToMetaIOFile(WP, fullfile(oSwp,dirlist(mm).name), 0);
[~, ~] = elxStrDataxToMetaIOFile(PZ, fullfile(oSpz,dirlist(mm).name), 0);
[~, ~] = elxStrDataxToMetaIOFile(TZ, fullfile(oStz,dirlist(mm).name), 0);
end
end
end
% Remove Temp folder
rmdir(fullfile(masterPath,'Temp'),'s')
end