-
Notifications
You must be signed in to change notification settings - Fork 1
/
DFT_Aggregator.m
402 lines (307 loc) · 16.4 KB
/
DFT_Aggregator.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
% Copyright (C) 2019 Robert F Cooper, created 2018-11-07
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
%
%
clear;
close all force;
% Add our support libraries to the path.
basePath = which('DFT_Aggregator.m');
[basePath ] = fileparts(basePath);
path(path, fullfile(basePath,'lib'));
[fNames,thispath ]=uigetfile(fullfile(pwd,'*.mat'),'Select all montages you wish to combine.', 'MultiSelect', 'on');
if ~all(iscell(fNames))
fNames = {fNames};
end
for f=1:length(fNames)
load( fullfile(thispath, fNames{f}), 'scaling');
scales(f) = scaling;
end
scaling = unique(scales);
if length(scaling)>1
error('The montage scales must be the same!');
end
global_eye = 'OD'; % Everything will be flipped to this.
foveal_coords = nan(length(fNames), 2);
montage_size = nan(length(fNames), 2);
montage_rect = cell(length(fNames), 1);
result_path = fullfile(thispath,'Results');
mkdir(result_path)
for f=1:length(fNames)
disp(['Calculating Foveal Location: ' num2str(f) ' of ' num2str(length(fNames))])
load( fullfile(thispath, fNames{f}), 'fovea_coords', 'imsize', 'unit');
montage_size(f,:) = imsize(1:2);
foveal_coords(f,:) = fovea_coords;
if isempty(strfind(fNames{f}, global_eye)) % If it doesn't match our eye, flip the fovea coordinates in the x direction.
foveal_coords(f,1) = montage_size(f,2)-foveal_coords(f,1);
end
montage_rect{f} = [ 1 1; % TLC
montage_size(f,2) 1; % TRC
montage_size(f,2) montage_size(f,1); % BRC
1 montage_size(f,1); % BLC
1 1];% TLC
% Shift all montage rectangles to a null point
montage_rect{f} = montage_rect{f}-foveal_coords(f,:);
end
clear fovea_coords
% Find the bounding rectangle
global_fovea_coords = min(cell2mat(montage_rect));
maxglobalbounds = max(cell2mat(montage_rect));
global_dimension = fliplr(ceil(maxglobalbounds-global_fovea_coords))+1; % Flipped so height/width (row/col), not width/height (x/y)
global_fovea_coords = round(-global_fovea_coords);
if ~exist('unit','var')
liststr = {'microns (mm density)','degrees'};
[unitind, oked] = listdlg('PromptString','Select output units:',...
'SelectionMode','single',...
'ListString',liststr);
unit = liststr{unitind};
if oked == 0
error('Cancelled by user.');
end
end
%% Add all of the dft information to the montage, and combine.
avg_density = zeros(global_dimension);
% weighted_avg_spacing = zeros(global_dimension);
avg_confidence = zeros(global_dimension);
combined_sum_map = zeros(global_dimension,'double');
% Find the weighted averages of all of the subjects.
for f=1:length(fNames)
disp(['Calculating Averages: ' num2str(f) ' of ' num2str(length(fNames))])
load( fullfile(thispath, fNames{f}), 'density_map_comb', 'blendederr_comb');
rowrange = round((montage_rect{f}(1,2):montage_rect{f}(3,2))+global_fovea_coords(2)+1);
colrange = round((montage_rect{f}(1,1):montage_rect{f}(3,1))+global_fovea_coords(1)+1);
if isempty(strfind(fNames{f}, global_eye)) % If it doesn't match our eye, flip the montage data.
density_map_comb = fliplr(density_map_comb);
blendederr_comb = fliplr(blendederr_comb);
end
% Lightly filter our data to remove any outliers in confidence or
% density.
[X, Y] = meshgrid(1:size(density_map_comb,2), 1:size(density_map_comb,1));
if strcmp(unit, 'microns (mm density)')
farrangemask = sqrt((X-foveal_coords(f,1)).^2 + (Y-foveal_coords(f,2)).^2) <= 800/scaling;
elseif strcmp(unit, 'degrees')
farrangemask = sqrt((X-foveal_coords(f,1)).^2 + (Y-foveal_coords(f,2)).^2) <= 2.5/scaling;
end
confcombfar = blendederr_comb.*~farrangemask;
confcombfar(confcombfar==0 | confcombfar>0.9)=NaN;
lowconffar = quantile(confcombfar(~isnan(confcombfar) ), [0.1]);
confcombclose = blendederr_comb.*farrangemask;
confcombclose(confcombclose==0 | confcombclose>0.9)=NaN;
lowconfclose = quantile(confcombclose(~isnan(confcombclose) ), [0.01]);
if strcmp(unit, 'microns (mm density)')
closerangemask = sqrt((X-foveal_coords(f,1)).^2 + (Y-foveal_coords(f,2)).^2) <= 400/scaling;
elseif strcmp(unit, 'degrees')
closerangemask = sqrt((X-foveal_coords(f,1)).^2 + (Y-foveal_coords(f,2)).^2) <= 1.25/scaling;
end
denscombclose = density_map_comb.*closerangemask;
denscombclose(denscombclose==0)=NaN;
densclose = quantile(denscombclose(~isnan(denscombclose)), [0.5]);
% If we're looking at data with these filenames, drop their long
% distance data as its quality is questionable.
if contains(fNames{f},'11101') || contains(fNames{f},'11092')
if strcmp(unit, 'microns (mm density)')
good_data_mask = sqrt((X-foveal_coords(f,1)).^2 + (Y-foveal_coords(f,2)).^2) <= 2400/scaling;
elseif strcmp(unit, 'degrees')
good_data_mask = sqrt((X-foveal_coords(f,1)).^2 + (Y-foveal_coords(f,2)).^2) <= 8/scaling;
end
density_map_comb = density_map_comb .*good_data_mask;
blendederr_comb = blendederr_comb .*good_data_mask;
end
% Remove the offending values.
density_map_comb(( (density_map_comb > densclose) & ~closerangemask )) = NaN;
density_map_comb(( (blendederr_comb < lowconffar) & ~farrangemask )) = NaN;
density_map_comb(( (blendederr_comb < lowconfclose) & farrangemask )) = NaN;
%Also remove them from our confidence.
blendederr_comb(isnan(density_map_comb)) = NaN;
avg_density( rowrange, colrange) = sum(cat(3, avg_density( rowrange, colrange), density_map_comb),3,'omitnan');
avg_confidence( rowrange, colrange) = sum(cat(3, avg_confidence( rowrange, colrange), blendederr_comb),3,'omitnan');
combined_sum_map( rowrange, colrange) = sum(cat(3, combined_sum_map( rowrange, colrange), ~isnan(density_map_comb)),3,'omitnan');
% figure(2); clf; hold on;
% subplot(1,2,1); imagesc(density_map_comb); axis image;
% subplot(1,2,2); imagesc(combined_sum_map); axis image;
% drawnow;
clear density_map_comb blendederr_comb rowrange colrange
end
avg_density = avg_density./combined_sum_map;
avg_confidence = avg_confidence./combined_sum_map;
%% To find global foveal mask
foveal_density_map=avg_density(global_fovea_coords(2)-768:global_fovea_coords(2)+768, global_fovea_coords(1)-768:global_fovea_coords(1)+768);
foveal_density_map = imgaussfilt(foveal_density_map, 8);
polar_foveal_density_map = imcart2pseudopolar(foveal_density_map, 1, 1,[769, 769],'cubic');
polar_foveal_density_map(polar_foveal_density_map==0) = NaN;
ridgeline = nan(size(polar_foveal_density_map,1), 1);
ridgeloc = nan(size(polar_foveal_density_map,1), 1);
% figure; imagesc(polar_foveal_density_map); axis image;
% hold on;
for p=1:size(polar_foveal_density_map,1)
if strcmp(unit, 'microns (mm density)')
[line, loc]=findpeaks(polar_foveal_density_map(p,:),'MinPeakProminence',4000,'SortStr', 'descend','NPeaks', 1);
elseif strcmp(unit, 'degrees')
[line, loc]=findpeaks(polar_foveal_density_map(p,:),'MinPeakProminence',750,'SortStr', 'descend','NPeaks', 1);
end
if ~isempty(line)
ridgeline(p)=line;
ridgeloc(p)=loc;
end
% plot(polar_foveal_density_map(p,:)); hold on;
% plot(ridgeloc(p), p,'r*');
end
hold off;
min_ridge = round(min(ridgeline));
[x,y]=pol2cart((0:2*pi/360:2*pi-(2*pi/360))',ridgeloc);
x=x+769;
y=y+769;
y(isnan(x)) =[];
x(isnan(x)) =[];
% imagesc(foveal_density_map); hold on; plot(x,y)
smfoveamask = poly2mask(x,y,size(foveal_density_map,1),size(foveal_density_map,2));
% Set our tolerance just below our minimum ridge size, vs the foveal min
% centralvals = polar_foveal_density_map(:, floor(min(ridgeloc)/2));
% centralvals = mean(centralvals(:),'omitnan');
% foveal_density_map(isnan(foveal_density_map)) = 0;
%
% tol = min_ridge-centralvals;
% smfoveamask = ~grayconnected(foveal_density_map, 769, 769, tol);
%
% smfoveamask=imerode(smfoveamask,strel('disk',50));
% figure; imagesc(smfoveamask)
%
% threshold_mask = true(size(avg_density));
threshold_mask(global_fovea_coords(2)-768:global_fovea_coords(2)+768, global_fovea_coords(1)-768:global_fovea_coords(1)+768) = smfoveamask;
% Ensure that we only see the averages of data with more than 20% of our points going into it.
threshold_mask = logical(threshold_mask.*(combined_sum_map>=0.2*max(combined_sum_map(:)) ));
%% Plot our masked data.
spacingcaxis = quantile(avg_density(threshold_mask(:)),[0.01 0.99]);
figure(1); imagesc(avg_density.*threshold_mask); title('Combined Spacing'); axis image;
clim(spacingcaxis); colorbar;
%% Output spacing image
rescaled_avg_spacing = (avg_density.*threshold_mask)-spacingcaxis(1);
rescaled_avg_spacing(rescaled_avg_spacing<0) = 0;
rescaled_avg_spacing = 255*(rescaled_avg_spacing./quantile(rescaled_avg_spacing(rescaled_avg_spacing~=0),0.99));
rescaled_avg_spacing(rescaled_avg_spacing>255) = 255;
imwrite(rescaled_avg_spacing, parula(256), fullfile(result_path, [num2str(length(fNames)) '_subjects_combined_spacing.tif']))
clear rescaled_avg_spacing;
%% Output confidence image
avg_confidence(avg_confidence==0) = NaN;
lowconffar = quantile(avg_confidence(threshold_mask(:)),0.001);
rescaled_avg_conf = (avg_confidence.*threshold_mask)-lowconffar;
rescaled_avg_conf(rescaled_avg_conf<0) = 0;
highconf = quantile(rescaled_avg_conf(rescaled_avg_conf~=0),0.995);
rescaled_avg_conf = 255*(rescaled_avg_conf./highconf);
rescaled_avg_conf(rescaled_avg_conf>255) = 255;
turbfull=flipud(turbo(512));
imwrite(rescaled_avg_conf, turbfull(19:274, :), fullfile(result_path,[num2str(length(fNames)) '_subjects_combined_conf.tif']))
figure(2); clf; imagesc((avg_confidence.*threshold_mask)); axis image; colormap(turbfull(19:274, :)); colorbar; title('Combined Confidence');
clim([lowconffar lowconffar+highconf]); colorbar;
saveas(gcf,fullfile(result_path,[num2str(length(fNames)) '_subjects_combined_conf.svg']));
clear rescaled_avg_conf;
%% Output density image
avg_density(avg_density==0) = NaN;
lowdens = quantile(avg_density(threshold_mask(:)),0.01)
rescaled_avg_density = (avg_density.*threshold_mask)-lowdens;
rescaled_avg_density(rescaled_avg_density<0) = 0;
highdens = quantile(rescaled_avg_density(rescaled_avg_density~=0),0.995)
rescaled_avg_density = 255*(rescaled_avg_density./highdens);
rescaled_avg_density(rescaled_avg_density>255) = 255;
figure(3); imagesc(avg_density.*threshold_mask); title('Combined Density'); axis image;
clim([lowdens lowdens+highdens]); colorbar;
imwrite(rescaled_avg_density, parula(256), fullfile(result_path,[num2str(length(fNames)) '_subjects_combined_density.tif']))
saveas(gcf,fullfile(result_path,[num2str(length(fNames)) '_subjects_combined_density.svg']));
clear rescaled_avg_density;
%% Output sum map
rescaled_sum_map = uint8( (255*(combined_sum_map./max(combined_sum_map(:)))).*threshold_mask );
figure(4); imagesc(combined_sum_map); title('Sum Map'); axis image; colormap winter; colorbar;
clim([0 max(combined_sum_map(:))]);
saveas(gcf,fullfile(result_path,[num2str(length(fNames)) '_subjects_sum_map.svg']));
imwrite(rescaled_sum_map, winter(256), fullfile(result_path,[num2str(length(fNames)) '_subjects_sum_map.tif']));
%% Determine average/stddev of all data.
value_map_variance = nan(global_dimension);
% For finding outliers:
% figure(1); clf; hold on;
% Find the std deviations of all of the subjects.
for f=1:length(fNames)
disp(['Calculating Standard Deviation: ' num2str(f) ' of ' num2str(length(fNames))])
load( fullfile(thispath, fNames{f}), 'density_map_comb', 'blendederr_comb');
rowrange = round((montage_rect{f}(1,2):montage_rect{f}(3,2))+global_fovea_coords(2)+1);
colrange = round((montage_rect{f}(1,1):montage_rect{f}(3,1))+global_fovea_coords(1)+1);
if isempty(strfind(fNames{f}, global_eye)) % If it doesn't match our eye, flip the montage data.
density_map_comb = fliplr(density_map_comb);
blendederr_comb = fliplr(blendederr_comb);
end
% Lightly filter our data to remove any outliers in confidence or
% density.
[X, Y] = meshgrid(1:size(density_map_comb,2), 1:size(density_map_comb,1));
if strcmp(unit, 'microns (mm density)')
farrangemask = sqrt((X-foveal_coords(f,1)).^2 + (Y-foveal_coords(f,2)).^2) <= 800/scaling;
elseif strcmp(unit, 'degrees')
farrangemask = sqrt((X-foveal_coords(f,1)).^2 + (Y-foveal_coords(f,2)).^2) <= 2.5/scaling;
end
confcombfar = blendederr_comb.*~farrangemask;
confcombfar(confcombfar==0)=NaN;
lowconffar = quantile(confcombfar(~isnan(confcombfar)), [0.10])
confcombclose = blendederr_comb.*farrangemask;
confcombclose(confcombclose==0)=NaN;
lowconfclose = quantile(confcombclose(~isnan(confcombclose)), [0.01])
if strcmp(unit, 'microns (mm density)')
closerangemask = sqrt((X-foveal_coords(f,1)).^2 + (Y-foveal_coords(f,2)).^2) <= 400/scaling;
elseif strcmp(unit, 'degrees')
closerangemask = sqrt((X-foveal_coords(f,1)).^2 + (Y-foveal_coords(f,2)).^2) <= 1.25/scaling;
end
denscombclose = density_map_comb.*closerangemask;
denscombclose(denscombclose==0)=NaN;
densclose = quantile(denscombclose(~isnan(denscombclose)), [0.5])
% If we're looking at data with these filenames, drop their long
% distance data as its quality is questionable.
if contains(fNames{f},'11101') || contains(fNames{f},'11092')
if strcmp(unit, 'microns (mm density)')
good_data_mask = sqrt((X-foveal_coords(f,1)).^2 + (Y-foveal_coords(f,2)).^2) <= 2400/scaling;
elseif strcmp(unit, 'degrees')
good_data_mask = sqrt((X-foveal_coords(f,1)).^2 + (Y-foveal_coords(f,2)).^2) <= 8/scaling;
end
density_map_comb = density_map_comb .*good_data_mask;
blendederr_comb = blendederr_comb .*good_data_mask;
end
% Remove the offending values.
density_map_comb(( (density_map_comb > densclose) & ~closerangemask )) = NaN;
density_map_comb(( (blendederr_comb < lowconffar) & ~farrangemask )) = NaN;
density_map_comb(( (blendederr_comb < lowconfclose) & farrangemask )) = NaN;
%Also remove them from our confidence.
blendederr_comb(isnan(density_map_comb)) = NaN;
density_map_comb = density_map_comb.*threshold_mask( rowrange, colrange);
density_map_comb(density_map_comb==0) = NaN;
thisdiff = sum( cat(3, density_map_comb, -avg_density( rowrange, colrange)) ,3).^2; % This line is good. Nans need to be carried through.
value_map_variance( rowrange, colrange) = sum( cat(3,value_map_variance( rowrange, colrange), thisdiff), 3,'omitnan');
clear thisdiff;
clear blendedim rowrange colrange
end
save( fullfile(result_path,[num2str(length(fNames)) '_aggregate_data.mat']), ...
'avg_density', 'avg_confidence','global_fovea_coords',...
'global_eye', 'threshold_mask', 'value_map_variance',...
'unit', 'scaling', 'montage_rect','combined_sum_map','-v7.3');
%%
value_std_dev = value_map_variance;
value_std_dev = value_std_dev./(combined_sum_map-1);
value_std_dev(isinf(value_std_dev)) =0;
value_std_dev = real(sqrt(value_std_dev));
lowdens = quantile(value_std_dev(threshold_mask(:)),0.01)
rescaled = (value_std_dev.*threshold_mask)-lowdens;
rescaled(rescaled<0) = 0;
highdens = quantile(rescaled(rescaled~=0),0.995)
rescaled = 255*(rescaled./highdens);
rescaled(rescaled>255) = 255;
imwrite(rescaled, parula(256), fullfile(result_path,[num2str(length(fNames)) '_subjects_combined_stddev.tif']))
% maskedspac = value_std_dev.*threshold_mask;
figure(3); imagesc(value_std_dev.*threshold_mask); title('Combined Spacing Std dev');
saveas(gcf,[num2str(length(fNames)) 'subjects_combined_stddev.png']);
saveas(gcf,[num2str(length(fNames)) 'subjects_combined_stddev.svg']);