-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpft_FractalDimensionCalculationOnMultipleFolders.m
357 lines (264 loc) · 13.2 KB
/
pft_FractalDimensionCalculationOnMultipleFolders.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% pft_FractalDimensionCalculationOnMultipleFolders %
% %
% A function to process all the sub-folders within a top-level folder. %
% %
% PFT - 15. 11. 2016. %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Clear the workspace as usual
clear all
close all
clc
fclose('all');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Locate a batch folder
if ispc
Username = getenv('Username');
Home = fullfile('C:', 'Users', Username, 'Desktop');
elseif isunix
[ Status, CmdOut ] = system('whoami');
Home = fullfile('home', CmdOut, 'Desktop');
elseif ismac
[ Status, CmdOut ] = system('whoami');
Home = fullfile('Users', CmdOut, 'Desktop');
end
TopLevelFolder = uigetdir(Home, 'Select a top-level folder with data folders inside');
if (TopLevelFolder == 0)
msgbox('No selection - quitting !', 'Exit');
return;
end
% Locate all the folders beneath
Listing = dir(TopLevelFolder);
Entries = { Listing.name };
Folders = [ Listing.isdir ];
Entries = Entries(Folders);
SingleDot = strcmp(Entries, '.');
Entries(SingleDot) = [];
DoubleDot = strcmp(Entries, '..');
Entries(DoubleDot) = [];
Results = strcmp(Entries, 'Automated FD Calculation Results - x4');
if ~isempty(Results)
Entries(Results) = [];
end
Results = strcmp(Entries, 'Automated FD Calculation Results - 0.25 mm pixels');
if ~isempty(Results)
Entries(Results) = [];
end
Entries = Entries';
if isempty(Entries)
msgbox('No sub-folders found.', 'Exit');
return;
end
NDIRS = length(Entries);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Set the basic processing parameters
% Fetch the acquisition slice order (as implemented in the segmentation files)
AcquisitionOrder = pft_GetAcquisitionOrder;
% Select the type of interpolation
InterpolationType = pft_GetInterpolationType;
% Set the default perimeter type - there is no choice here, and if the default cannot be created, then the brute-force Ansatz is applied
PerimeterType = 'Out from blood pool';
% Fetch the blood pool threshold parameters - 60-65 (64) pixels is optimum for Genscan, 38 for UKBB, according to TJWD's balanced/maximum probability study
[ MinimumPixelCount, ConnectedPercentage ] = pft_GetBloodPoolThresholdParameters;
% Ask whether to trim data for summary FD statistics
Ans = questdlg('Discard end slices for summary statistics ?', 'Processing decision', 'Yes', 'No', 'No');
switch Ans
case { '', 'No' }
DiscardEndSlices = 'No';
case 'Yes'
DiscardEndSlices = 'Yes';
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Select the output Excel sheet and back it up straightaway
SummaryFile = fullfile(TopLevelFolder, 'Summary-Auto-FD-v0.csv');
SummaryBackupFile = fullfile(TopLevelFolder, 'Summary-Auto-FD-v0-Backup.csv');
if (exist(SummaryFile, 'file') ~= 2)
Head = [ 'Folder,', ...
'Slice order in segmentation,', 'Interpolation,', 'Default perimeter drawn,', ...
'BP threshold (pixels),', 'Connection threshold (per cent),', ...
'Original resolution / mm,', 'Output resolution / mm,', ...
'Slices present,', ...
'FD - Slice 1,', 'Slice 2,', 'Slice 3,', 'Slice 4,', 'Slice 5,', ...
'Slice 6,', 'Slice 7,', 'Slice 8,', 'Slice 9,', 'Slice 10,', ...
'Slice 11,', 'Slice 12,', 'Slice 13,', 'Slice 14,', 'Slice 15,', ...
'Slice 16,', 'Slice 17,', 'Slice 18,', 'Slice 19,', 'Slice 20,', ...
'End slices discarded for statistics,', ...
'Slices evaluated,', 'Slices used,', ...
'Mean global FD,', ...
'Mean basal FD,', 'Mean apical FD,', ...
'Max. basal FD,', 'Max. apical FD' ];
fid = fopen(SummaryFile, 'at');
fprintf(fid, '%s\n', Head);
fclose(fid);
end
copyfile(SummaryFile, SummaryBackupFile);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% These are signalling error conditions for the o/p CSV file
MeagreBloodPool = -111;
SparseMyocardium = -222;
NoROICreated = -333;
FDMeasureFailed = 0.0; % Signal that an attempt was made, but failed - this will be excluded from the FD statistics
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Process all the suitable folders
switch InterpolationType
case 'Imresize - (x4 x4) - cubic'
if (exist(fullfile(TopLevelFolder, 'Automated FD Calculation Results - x4'), 'dir') ~= 7)
mkdir(TopLevelFolder, 'Automated FD Calculation Results - x4');
end
case 'Imresize - 0.25 mm pixels - cubic'
if (exist(fullfile(TopLevelFolder, 'Automated FD Calculation Results - 0.25 mm pixels'), 'dir') ~= 7)
mkdir(TopLevelFolder, 'Automated FD Calculation Results - 0.25 mm pixels');
end
end
h1 = waitbar(0, sprintf('Processed 0 of %1d folders', NDIRS), 'Units', 'normalized', 'Position', [0.225 0.45 0.2 0.1]);
set(h1, 'Name', 'Progress - folders');
for n = 1:NDIRS
switch InterpolationType
case 'Imresize - (x4 x4) - cubic'
if (exist(fullfile(TopLevelFolder, 'Automated FD Calculation Results - x4', Entries{n}), 'dir') ~= 7)
mkdir(fullfile(TopLevelFolder, 'Automated FD Calculation Results - x4'), Entries{n});
end
case 'Imresize - 0.25 mm pixels - cubic'
if (exist(fullfile(TopLevelFolder, 'Automated FD Calculation Results - 0.25 mm pixels', Entries{n}), 'dir') ~= 7)
mkdir(fullfile(TopLevelFolder, 'Automated FD Calculation Results - 0.25 mm pixels'), Entries{n});
end
end
SourceFolder = fullfile(TopLevelFolder, Entries{n});
switch InterpolationType
case 'Imresize - (x4 x4) - cubic'
TargetFolder = fullfile(TopLevelFolder, 'Automated FD Calculation Results - x4', Entries{n});
case 'Imresize - 0.25 mm pixels - cubic'
TargetFolder = fullfile(TopLevelFolder, 'Automated FD Calculation Results - 0.25 mm pixels', Entries{n});
end
[ ImageStack, SegmentationStack, BinaryMask, PerimeterStack, Conditions, OriginalResolution ] = ...
pft_ExtractMatchedAndShiftedImages(SourceFolder, AcquisitionOrder, MinimumPixelCount, ConnectedPercentage);
if isempty(ImageStack)
rmdir(TargetFolder, 's');
Data = sprintf('%s, %s %s', Entries{n}, repmat(' ,', [1, 35]), ' ');
fid = fopen(SummaryFile, 'at');
fprintf(fid, '%s\n', Data);
fclose(fid);
waitbar(n/NDIRS, h1, sprintf('Processed %1d of %1d folders', n, NDIRS));
continue;
end
[ NR, NC, NP ] = size(ImageStack);
switch InterpolationType
case 'Imresize - (x4 x4) - cubic'
OutputResolution = OriginalResolution/4.0;
case 'Imresize - 0.25 mm pixels - cubic'
OutputResolution = 0.25;
end
h2 = waitbar(0, sprintf('Processed 0 of %1d slices', NP), 'Units', 'normalized', 'Position', [0.525 0.45 0.2 0.1]);
set(h2, 'Name', 'Progress - slices');
FD = NaN(1, 20);
FractalDimensions = repmat({ 'NaN' }, [1, 20]);
for p = 1:NP
switch Conditions{p}
case 'Meagre blood pool'
Wzor = ImageStack(:, :, p);
Segmentation = SegmentationStack(:, :, p);
pft_WriteOriginallySizedImages(Wzor, Segmentation, p, TargetFolder);
FD(p) = MeagreBloodPool;
FractalDimensions{p} = 'Meagre blood pool';
pft_WriteAllBlankImages(p, TargetFolder, 'Meagre blood pool');
case 'Sparse myocardium'
Wzor = ImageStack(:, :, p);
Segmentation = SegmentationStack(:, :, p);
pft_WriteOriginallySizedImages(Wzor, Segmentation, p, TargetFolder);
FD(p) = SparseMyocardium;
FractalDimensions{p} = 'Sparse myocardium';
pft_WriteAllBlankImages(p, TargetFolder, 'Sparse myocardium');
case 'No ROI created'
Wzor = ImageStack(:, :, p);
Segmentation = SegmentationStack(:, :, p);
pft_WriteOriginallySizedImages(Wzor, Segmentation, p, TargetFolder);
FD(p) = NoROICreated;
FractalDimensions{p} = 'No ROI created';
pft_WriteAllBlankImages(p, TargetFolder, 'No ROI created');
case 'OK'
Wzor = ImageStack(:, :, p);
Mask = BinaryMask(:, :, p);
Segmentation = SegmentationStack(:, :, p);
Perimeter = PerimeterStack(:, :, p);
pft_WriteOriginallySizedImages(Wzor, Segmentation, p, TargetFolder);
pft_WriteOriginallySizedMask(Mask, p, TargetFolder);
s = regionprops(Mask, 'BoundingBox');
Wzor = imcrop(Wzor, s(1).BoundingBox);
Mask = imcrop(Mask, s(1).BoundingBox);
Segmentation = imcrop(Segmentation, s(1).BoundingBox);
Perimeter = imcrop(Perimeter, s(1).BoundingBox);
[ Wzor, Mask, Segmentation, Perimeter ] = ...
pft_InterpolateImages(Wzor, Mask, Segmentation, Perimeter, OriginalResolution, InterpolationType);
pft_WriteInputImages(Wzor, Mask, Segmentation, Perimeter, p, TargetFolder);
try
FD(p) = pft_JC_FractalDimensionCalculation(Wzor, Mask, p, TargetFolder);
FractalDimensions{p} = sprintf('%.9f', FD(p));
catch
FD(p) = FDMeasureFailed;
Conditions{p} = 'FD measure failed';
FractalDimensions{p} = 'FD measure failed';
pft_WriteBlankOutputImages(p, TargetFolder, 'FD measure failed');
end
end
waitbar(p/NP, h2, sprintf('Processed %1d of %1d slices', p, NP));
end
waitbar(1, h2, sprintf('Processed %1d of %1d slices', NP, NP));
pause(0.1);
close(h2);
waitbar(n/NDIRS, h1, sprintf('Processed %1d of %1d folders', n, NDIRS));
% Extract and process the FD values for the current stack, trimmed to the number of slices present
StackFD = FD(1:NP);
switch DiscardEndSlices
case 'No'
S = pft_JC_FDStatistics(StackFD, false);
case 'Yes'
S = pft_JC_FDStatistics(StackFD, true);
end
% Write out the formatted o/p as text
FormattedFDOutput = '';
for c = 1:19
switch Conditions{c}
case { 'Meagre blood pool', 'Sparse myocardium', 'No ROI created', 'FD measure failed' }
FormattedFDOutput = [ FormattedFDOutput sprintf('%s,', FractalDimensions{c}) ];
case 'OK'
if isnan(FD(c))
FormattedFDOutput = [ FormattedFDOutput 'NaN,' ];
else
FormattedFDOutput = [ FormattedFDOutput sprintf('%s,', FractalDimensions{c}) ];
end
end
end
switch Conditions{20}
case { 'Meagre blood pool', 'Sparse myocardium', 'No ROI created', 'FD measure failed' }
FormattedFDOutput = [ FormattedFDOutput sprintf('%s', FractalDimensions{20}) ];
case 'OK'
if isnan(FD(20))
FormattedFDOutput = [ FormattedFDOutput 'NaN' ];
else
FormattedFDOutput = [ FormattedFDOutput sprintf('%s', FractalDimensions{20}) ];
end
end
Data = sprintf('%s,%s,%s,%s,%1d,%.2f,%.9f,%.9f,%1d,%s,%s,%1d,%1d,%.9f,%.9f,%.9f,%.9f,%.9f', ...
Entries{n}, ...
AcquisitionOrder, InterpolationType, PerimeterType, ...
MinimumPixelCount, ConnectedPercentage, ...
OriginalResolution, OutputResolution, ...
NP, ...
FormattedFDOutput, ...
DiscardEndSlices, S.SlicesEvaluated, S.SlicesUsed, ...
S.MeanGlobalFD, ...
S.MeanBasalFD, S.MeanApicalFD, ...
S.MaxBasalFD, S.MaxApicalFD);
fid = fopen(SummaryFile, 'at');
fprintf(fid, '%s\n', Data);
fclose(fid);
end
waitbar(1, h1, sprintf('Processed %1d of %1d folders', NDIRS, NDIRS));
pause(0.1);
close(h1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Home, James !
h = msgbox('Done !', 'Quit', 'modal');
uiwait(h);
delete(h);