-
Notifications
You must be signed in to change notification settings - Fork 28
/
palm_fliptree.m
243 lines (218 loc) · 7.95 KB
/
palm_fliptree.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
function Pset = palm_fliptree(Ptree,nP,cmc,idxout,maxP)
% Return a set of permutations from a permutation tree.
%
% Usage:
% Sset = palm_fliptree(Ptree,nS,cmc,idxout,maxS)
%
% Inputs:
% - Ptree : Tree with the dependence structure between
% observations, as generated by 'palm_tree'.
% - nS : Number of permutations. Use 0 for exhaustive.
% - cmc : A boolean indicating whether conditional
% Monte Carlo should be used or not. If not used,
% there is a possibility of having repeated
% permutations. The more possible permutations,
% the less likely to find repetitions.
% - idxout : (Optional) is supplied, Pset is an array of indices
% rather than a cell array with sparse matrices.
% - maxS : (Optional) Maximum number of possible sign flips.
% If not supplied, it's calculated internally. If
% supplied, it's not calculated internally and some
% warnings that could be printed are omitted.
% Also, this automatically allows nS>maxS (via CMC).
%
% Outputs:
% - Sset : A cell array of size nP by 1 containing sparse
% sign-flipping matrices.
%
% Reference:
% * Winkler AM, Webster MA, Vidaurre D, Nichols TE, Smith SM.
% Multi-level block permutation. Neuroimage. 2015;123:253-68.
%
% _____________________________________
% Anderson M. Winkler
% FMRIB / University of Oxford
% Nov/2013
% http://brainder.org
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% PALM -- Permutation Analysis of Linear Models
% Copyright (C) 2015 Anderson M. Winkler
%
% 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
% 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/>.
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Note that the varnames follow the pattern of the similar
% function palm_permtree.m, for easier readability.
% Get the number of possible sign-flips.
if nargin < 5,
maxP = palm_maxshuf(Ptree,'flips');
if nP > maxP,
nP = maxP; % the cap is only imposed if maxP isn't supplied
end
end
if nargin < 4,
idxout = false;
end
% Sign-flip #1 is no flip, regardless.
P = cell2mat(pickflip(Ptree,{},ones(size(Ptree,1)))');
P = horzcat(P,zeros(length(P),nP-1));
% All other sign flips up to nP
if nP == 0 || nP == maxP,
% This will compute exhaustively all possible sign flips,
% one branch at a time. If nP is too large print a warning.
if nP > 1e5 && nargin <= 3;
warning([...
'Number of possible sign-flips is %g.\n' ...
' Performing all exhaustively.'],maxP);
end
for p = 2:maxP,
Ptree = nextflip(Ptree);
P(:,p) = cell2mat(pickflip(Ptree,{},ones(size(Ptree,1)))');
end
elseif cmc || nP > maxP,
% Conditional Monte Carlo. Repeated sign flips allowed.
for p = 2:nP,
Ptree = randomflip(Ptree);
P(:,p) = cell2mat(pickflip(Ptree,{},ones(size(Ptree,1)))');
end
else
% Otherwise, repeated sign-flips are not allowed.
% For this to work, maxP needs to be reasonably larger than
% nP, otherwise it will take forever to run, so print a
% warning.
if nP > maxP/2 && nargin <= 3,
warning([
'The maximum number of sign flips (%g) is not much larger than\n' ...
'the number you chose to run (%d). This means it may take a while (from\n' ...
'a few seconds to several minutes) to find non-repeated sign flips.\n' ...
'Consider instead running exhaustively all possible' ...
'flips. It may be faster.'],maxP,nP);
end
% For each flip, keeps trying to find a new, non-repeated instance
for p = 2:nP,
whiletest = true;
while whiletest,
Ptree = randomflip(Ptree);
P(:,p) = cell2mat(pickflip(Ptree,{},ones(size(Ptree,1)))');
whiletest = any(all(bsxfun(@eq,P(:,p),P(:,1:p-1))));
end
end
end
% Sort correctly rows using the 1st permutation
[~,idx] = palm_permtree(Ptree,1,false);
P = P(idx,:);
% For compatibility, convert each permutaion to a sparse permutation
% matrix. This section may be removed in the future if the
% remaining of the code is modified.
if idxout,
Pset = P;
else
Pset = cell(nP,1);
for p = 1:nP,
Pset{p} = sparse(diag(P(:,p)));
end
end
% ==============================================================
function [Ptree,incremented] = nextflip(Ptree)
% Make the next sign flip of tree branches, and returns
% the shuffled tree. This can be used to compute exhaustively
% all possible sign flippings.
% Some vars for later
nU = size(Ptree,1);
% For each branch of the current node
for u = 1:nU,
if isempty(Ptree{u,2}),
% If the branches at this node cannot be considered for
% flipping, go to the deeper levels, if they exist.
if size(Ptree{u,3},2) > 1,
[Ptree{u,3},incremented] = nextflip(Ptree{u,3});
if incremented,
if u > 1,
Ptree(1:u-1,:) = resetflips(Ptree(1:u-1,:));
end
break;
end
end
else
% If the branches at this node are to be considered
% for sign-flippings (already being done or not)
if sum(Ptree{u,2}) < numel(Ptree{u,2}),
% If the current branch can be flipped, but haven't
% reached the last possibility yet, flip and break
% the loop.
Ptree{u,2} = palm_incrbin(Ptree{u,2});
incremented = true;
if u > 1,
Ptree(1:u-1,:) = resetflips(Ptree(1:u-1,:));
end
break;
else
% If the current branch could be flipped, but
% it's the last possibility, reset it and
% don't break the loop.
incremented = false;
end
end
end
% ==============================================================
function Ptree = resetflips(Ptree)
% Recursively reset all flips of a permutation tree
% back to the original state
for u = 1:size(Ptree,1),
if isempty(Ptree{u,2}) && size(Ptree{u,3},2) > 1,
Ptree{u,3} = resetflips(Ptree{u,3});
else
Ptree{u,2} = false(size(Ptree{u,2}));
end
end
% ==============================================================
function Ptree = randomflip(Ptree)
% Make the a random sign-flip of all branches in the tree.
% For each branch of the current node
nU = size(Ptree,1);
for u = 1:nU,
if isempty(Ptree{u,2}) == 1 && size(Ptree{u,3},2) > 1,
% Go down more levels
Ptree{u,3} = randomflip(Ptree{u,3});
else
% Or make a random flip if no deeper to go
Ptree{u,2} = rand(size(Ptree{u,2})) > .5;
end
end
% ==============================================================
function P = pickflip(Ptree,P,sgn)
% Take a tree in a given state and return the sign flip. This
% won't flip, only return the indices for the already flipped
% tree. This function is recursive and for the 1st iteration,
% P = {}, i.e., a 0x0 cell.
nU = size(Ptree,1);
if size(Ptree,2) == 3,
for u = 1:nU,
if isempty(Ptree{u,2}),
bidx = sgn(u)*ones(size(Ptree{u,3},1),1);
else
bidx = double(~Ptree{u,2});
bidx(Ptree{u,2}) = -1;
end
P = pickflip(Ptree{u,3},P,bidx);
end
elseif size(Ptree,2) == 1,
for u = 1:nU,
if numel(sgn) == 1,
v = 1;
else
v = u;
end
P{numel(P)+1} = sgn(v)*ones(size(Ptree{u}));
end
end