-
Notifications
You must be signed in to change notification settings - Fork 28
/
palm_dimreorder.m
100 lines (88 loc) · 3.7 KB
/
palm_dimreorder.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
function Y = palm_dimreorder(X,sqz)
% Permute the dimensions of data read with palm_miscread such that
% the permutable dimension is the last. If the data had been permuted
% already, then reorder back to the original.
%
% Usage:
% Y = palm_dimreorder(X,sqz)
%
% X : Data structure from palm_miscread.
% szq : Squeeze the permutable dimension? (true/false)
% Y : Like X, but with the dimensions permuted.
%
% For reordering an array only, see palm_convNto2 and palm_conv2toN.
%
% _____________________________________
% Anderson M. Winkler
% UTRGV
% Mar/2024
% http://brainder.org
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% PALM -- Permutation Analysis of Linear Models
% Copyright (C) 2024 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/>.
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
switch X.readwith
case 'cifti-matlab'
% Operate on a copy
Y = X;
if isfield(X.extra,'oldorder')
% If a field specifying an old order is present, either squeeze
% the last dimension, or reorder back to original
if sqz
% Cannot drop to less than two dimensions
if numel(Y.extra.diminfo) > 2
Y.extra.diminfo(end) = [];
end
idxdimdrop = Y.extra.oldorder == numel(Y.extra.oldorder);
Y.extra.oldorder(idxdimdrop) = [];
% Adjust the file extension
switch lower(Y.extra.cifti_file_extension)
case {'pconnscalar','pconnseries'}
Y.extra.cifti_file_extension = 'pconn';
case {'ptseries'}
Y.extra.cifti_file_extension = 'pscalar';
case {'dtseries'}
Y.extra.cifti_file_extension = 'dscalar';
end
else
% Reorder the dims back, keeping the file type
Y.data = permute(X.data,X.extra.oldorder);
Y.extra.diminfo = Y.extra.diminfo(X.extra.oldorder);
Y.extra = rmfield(Y.extra,'oldorder');
end
else
% Otherwise, we want to reorder such that the permutable
% dimension is the last.
% Locate the dimension type for each dimension
nD = numel(X.extra.diminfo);
Dtypes = cell(1,nD);
for d = 1:nD
Dtypes{d} = X.extra.diminfo{d}.type;
end
% Identify the permutable dimension
pdim = find(strcmpi('scalars',Dtypes) | strcmpi('series',Dtypes));
if numel(pdim) ~= 1
error('Cannot determine the permutation dimension in CIFTI file: %s', X.filename);
end
% Reorder the data such that the permutable dimension is the last
neworder = [setdiff(1:nD,pdim) pdim];
Y.data = permute(X.data,neworder);
Y.extra.diminfo = Y.extra.diminfo(neworder);
[~,Y.extra.oldorder] = sort(neworder);
end
otherwise
error('This function currently does not work with files read with %s.', X.readwith);
end