-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYiwei_EFC.m
More file actions
120 lines (109 loc) · 3.58 KB
/
Copy pathYiwei_EFC.m
File metadata and controls
120 lines (109 loc) · 3.58 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
function efc = efc_masked(img, mask)
% EFC_MASKED Entropy-Focus Criterion for 2D or 3D image
%
% efc = efc_masked(img)
% efc = efc_masked(img, mask)
%
% Inputs
% img : 2D or 3D image
% mask : logical/binary mask, same size as img
%
% Output
% efc : normalized entropy-focus criterion
% lower values indicate less ghosting / blurring
% -------------------------------------------------------------------------
% if no mask is given, the mask becomes the whole image with all voxels
% included
if nargin < 2 || isempty(mask)
mask = true(size(img));
else
mask = logical(mask);
end
xNorm = qc_normalize01(img, mask);
% use normalized magnitude inside mask only
x = xNorm(mask);
% remove NaNs
x = x(~isnan(x));
N = numel(x);
if N == 0
efc = NaN;
return;
end
%this is the energy of the masked intensities. It's what each voxel gets
%divided by, so the entropy is scale-independent.
bmax = sqrt(sum(x.^2));
if bmax == 0
efc = 0;
return;
end
%Energy-normalized intensities. Each p is a voxel's fraction of the total
%energy so the sum of p² = 1. It represents how concentrated the signal is
%in this voxel.
p = x / bmax;
small = 1e-16;
% same formula as MRIQC-style implementation
%Peaky, concentrated images give a less negative sum.
% Spread-out, blurred/ghosted images give a more negative sum.
num = sum(p .* log(p + small)); %entropy sum
%What a maximally spread uniform image of N voxels would give and dividing
%by it makes EFC comparable across imaages of different sizes.
efc_max = N * (1/sqrt(N)) * log(1/sqrt(N)); %maximum-entropy normalizer
%entropy normalized by the max-entropy
%Lower value = sharper; higher = more blur/ghosting
efc = num / efc_max; %output
end
function x = qc_normalize01(img, mask)
% QC_NORMALIZE01 Convert magnitude image data to [0, 1] for QC metrics.
%
% x = qc_normalize01(img)
% x = qc_normalize01(img, mask)
%
% The min/max range is estimated inside mask when a non-empty mask is given.
% Non-finite output values are set to zero.
%double precision array
x = double(abs(img));
if nargin < 2 || isempty(mask)
validMask = isfinite(x);
else
validMask = logical(mask) & isfinite(x);
end
if ~any(validMask(:))
x = zeros(size(x));
return;
end
%smallest intensityinside a valid region
minValue = min(x(validMask));
%subtracts the offset from every voxel, shifting the whole distribution
%down before scaling.
x = x - minValue;
%largest intensity inside the valid region after the min is subtracted
maxValue = max(x(validMask));
if maxValue > 0
x = x ./ maxValue;
else
x(:) = 0;
end
x(~isfinite(x)) = 0;
x = min(max(x, 0), 1);
end
noMocoFile = 'I:\yannic.delisle\scripts\sub-10\baseline_nomoco\x_nomoco_1bin_mathilda_manual_finalNu240.mat';
mocoFile = 'I:\yannic.delisle\scripts\sub-10\sliding_L16.0_S3.2\x_moco_mathilda_smoothedMotion_from_x_cs_tres3.2s_Nu120x120x120_delta2_sliding.mat';
% stevaMocoFile = 'I:\yannic.delisle\scripts\sub-06\sliding_L3.5_S0.7\x_refined_moco_steva_delta0p1_from_x_cs_tres0.7s_Nu120x120x120_delta2_sliding.mat';
maskPath = 'I:\yannic.delisle\scripts\sub-10\sliding_L16.0_S3.2\prep_teva_tres3.2s_Nu120x120x120.mat';
S = load(noMocoFile);
x = S.x_nomoco;
T = load(mocoFile, 'x_moco');
moco = T.x_moco;
m = load(maskPath, 'maskBrain');
mask = m.maskBrain;
% U = load(stevaMocoFile, 'x_refined_moco');
% steva = U.x_refined_moco;
if iscell(x)
x = x{1};
end
%resize so the mask is the same size as the image
if ~isequal(size(mask), size(moco))
mask = imresize3(single(mask), size(x), 'nearest') > 0.5;
end
format long
efc_masked(x, mask)