-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathspm_histN.m
More file actions
207 lines (194 loc) · 6.14 KB
/
spm_histN.m
File metadata and controls
207 lines (194 loc) · 6.14 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
function [V,W,C,BW,E] = spm_histN(X,varargin)
% _________________________________________________________________________
%
% Compute the (joint) histogram of a (multidimensional) dataset
%
% FORMAT [values,count] = spm_histN(X,nbins,...)
% FORMAT [values,count] = spm_histN(X,centres,...)
%
% MANDATORY
% ---------
% X - NxP matrix of observed values
%
% OPTIONAL
% --------
% nbins - 1x1 or 1xP number of bins [64]
% or
% centres - Mx1 ordered bin centres (or 1xP cell of Mpx1 bin centres)
%
% KEYWORD
% -------
% Weights - Nx1 Observations weights [1]
% KeepZero - Keep bins with zero observations [true]
% Missing - Keep rows with missing data [false]
% Additional bins are created for missing values.
% Reshape - Reshape output so that their lattice is M1xM2x... [false]
% Smooth - FWHM of the smoothing kernel (in bins) [0]
% Verbose - Verbosity level [0]
%
% OUTPUT
% ------
% values - MxP matrix of multidimensional values (or M1x...xP if Reshape)
% count - Mx1 vector of counts (or M1x...x1 if Reshape)
% centres - 1xP cell of Mpx1 bin centres
% widths - 1xP vector (or 1xP cell of Mpx1 vectors) bin widths
%
% (M can be smaller that the specified number of bins if KeepZero = false)
%__________________________________________________________________________
% Copyright (C) 2018 Wellcome Centre for Human Neuroimaging
% -------------------------------------------------------------------------
% Parse inputs
p = inputParser;
p.FunctionName = 'spm_histN';
p.addRequired('X', @isnumeric);
p.addOptional('B', 64, @(arg) isnumeric(arg) || iscell(arg));
p.addParameter('Weights', 1, @(arg) isscalar(arg) || (numel(arg) == size(X,1)));
p.addParameter('KeepZero', true, @isscalar);
p.addParameter('Missing', false, @isscalar);
p.addParameter('Reshape', false, @isscalar);
p.addParameter('Smooth', 0, @isnumeric);
p.addParameter('Verbose', 0, @isscalar);
p.parse(X, varargin{:});
B = p.Results.B;
Wi = p.Results.Weights;
Wi = Wi(:);
if p.Results.Reshape && ~p.Results.KeepZero
error(['To reshape, all values must be kept. ' ...
'Use (''KeepZero'',true) with (''Reshape'',true).'])
end
% -------------------------------------------------------------------------
% Discard missing values
if ~p.Results.Missing
missing = any(isnan(X),2);
X = X(~missing,:);
if ~isscalar(Wi)
Wi = Wi(~missing);
end
end
% -------------------------------------------------------------------------
% Compute bin centres / edges
P = size(X,2); % Number of channels
N = size(X,1); % Number of observations
minval = min(X, [], 1, 'omitnan'); % Min value / channel
maxval = max(X, [], 1, 'omitnan'); % Max value / channel
if ~iscell(B) && size(B,1) == 1
% Number of bins provided
E = B;
if numel(B) < P
E = spm_padarray(E, [0, P-numel(B)], 'replicate', 'post');
end
BW = (maxval - minval)./B;
E = num2cell(E);
else
% Bin centres provided
if ~iscell(B)
if size(B,2) == 1
B = repmat(B(:),1,P);
end
B = num2cell(B, 1);
end
E = cell(1,P);
BW = cell(1,P);
for c=1:P
E{c} = (B{c}(2:end) + B{c}(1:end-1))/2;
E{c} = [minval(c); E{c}; maxval(c)]';
BW{c} = E{c}(2:end) - E{c}(1:end-1);
end
end
clear B
% -------------------------------------------------------------------------
% Discretize data
I = cell(1,P); % Bin index for each observation
V = cell(1,P); % Bin edges
dim = zeros(1,P);
hasnan = zeros(1,P,'logical');
for c=1:P
[I{c},V{c}] = discretize(X(:,c),E{c}); % /!\ in a toolbox?
I{c} = single(I{c});
I{c}(isnan(I{c})) = numel(V{c});
V{c} = (V{c}(2:end) + V{c}(1:end-1))/2; % Edge to centre
V{c} = V{c}(:);
hasnan(c) = any(isnan(X(:,c)));
dim(c) = numel(V{c}) + hasnan(c);
if hasnan(c)
V{c}(end+1) = NaN;
end
end
clear E X
% -------------------------------------------------------------------------
% Count
if numel(dim) == 1
linI = [I{:}];
else
linI = sub2ind(dim, I{:}); % Convert P-dimensional index to (unique) linear index
end
linI = uint64(linI);
clear I
if isscalar(Wi)
Wi = Wi * ones(size(linI));
end
Wi = double(Wi);
W = spm_hist(linI, Wi, prod(dim)); % TODO: push new spm_hist
W = W(:);
clear linI Wi
% -------------------------------------------------------------------------
% Create matrix of multivariate bin centres
C = V;
V = mycombvec(V{:});
% -------------------------------------------------------------------------
% Smooth
if p.Results.Smooth
W = reshape(W, dim);
lim = ceil(4/2.355*p.Results.Smooth);
ker = spm_smoothkern(p.Results.Smooth, -lim:lim, 0);
ker = ker(ker~=0);
for c=1:P
if hasnan(c)
W1 = W;
subs = cell(1,P);
[subs{:}] = deal(':');
subs{c} = 1:size(W,c)-1;
W = subsref(W1, struct('type', '()', 'subs', {subs}));
end
W = convn(W, reshape(ker, [ones(1,c-1) numel(ker) 1]), 'same');
if hasnan(c)
[W1,W] = deal(W,W1);
W = subsasgn(W, struct('type', '()', 'subs', {subs}), W1);
clear W1
end
end
W = W(:);
end
% -------------------------------------------------------------------------
% Reshape
if p.Results.Reshape
W = reshape(W, dim);
V = reshape(V, [dim P]);
end
% -------------------------------------------------------------------------
% Remove empty bins
if ~p.Results.KeepZero
empty = W == 0;
W = W(~empty);
V = V(~empty,:);
end
function x = mycombvec(varargin)
% Create all possible combinations of input values.
%
% FORMAT x = mycombvec(y1,y2,...,yN)
% y1,y2,...,yN must be column vectors
% x is an MxN matrix, where M is the number of combinations
%
% /!\ It does not have the exact same behaviour as Matlab's combvec.
% It is equivalent to: `x = combvec(y1(:)',y2(:)',...)'`
x = varargin{1}(:);
varargin = varargin(2:end);
while ~isempty(varargin)
y = varargin{1}(:);
Nx = size(x,1);
Ny = size(y,1);
x = repmat(x, [Ny 1]);
y = repmat(y.', [Nx 1]);
x = [x y(:)];
varargin = varargin(2:end);
end