-
Notifications
You must be signed in to change notification settings - Fork 0
/
NINGA_ciftiread.m
107 lines (99 loc) · 3.6 KB
/
NINGA_ciftiread.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
function [data,extra] = NINGA_ciftiread(varargin)
% Provides limited support for reading CIFTI files
% (surface only) using the wb_command as the backend.
%
% [data,extra] = palm_ciftiread(filename,temp_prefix,wb_command)
%
% Inputs:
% - filename : Filename of the CIFTI (surface only, dtscalar).
% - temp_prefix : (Optional) Prefix, possibly prepended by a full path
% where temporary files will be safed. If omitted,
% the current directory will be used.
% - wb_command : (Optional) Full path to the executable wb_command.
%
% Outputs:
% - data : Array with the actual data.
% - extra : Extra information needed to save back as CIFTI.
%
% _____________________________________
% Anderson M. Winkler
% FMRIB / University of Oxford
% Apr/2015
% 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/>.
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Handle input arguments
narginchk(1,3);
filename = varargin{1};
if nargin >= 2,
temp_prefix = varargin{2};
else
temp_prefix = '';
end
if nargin == 3,
wb_command = varargin{3};
else
wb_command = 'wb_command';
end
% Test the wb_command
try %#ok
[status,wb_output] = system(wb_command);
end
if status ~= 0,
disp(wb_output);
error('Test failed for your architecture. CIFTI will not be read.');
end
% Deal with the location of the temporary files
alphabet = ['a':'z' 'A':'Z' '0':'9'];
rand_prefix = alphabet(randi(numel(alphabet),[1 6]));
% If no temporary prefix has been supplied, the file will be
% saved to the current directory, with temp_prefix only.
if ~ isempty(temp_prefix),
rand_prefix = strcat(temp_prefix,'_',rand_prefix);
end
temp_gii = strcat(rand_prefix,'.gii');
% Convert CIFTI to GIFTI using the Workbench. This line causes troubles in
% Matlab for Mac, because of the paths of the linked libraries of
% the Workbench. Hopefully in the future there will be stable CIFTI I/O
% that will work natively in Matlab and Octave.
% Also note that the wb_command must be accessible with the environmental var PATH.
[~] = system(sprintf('%s -cifti-convert -to-gifti-ext %s %s',wb_command,filename,temp_gii));
% Load the temporary GIFTI file. Note that there are no mapped file-arrays.
gii = gifti(temp_gii);
if isfield(gii,'cdata'),
data = gii.cdata';
end
if isfield(gii,'vertices') && isfield(gii,'faces'),
data.vtx = gii.vertices;
data.fac = gii.faces;
if isfield(gii,'mat'),
vtx = [data.vtx ones(size(data.vtx,1),1)];
vtx = vtx * gii.mat;
data.vtx = vtx(:,1:3);
end
end
for d = numel(gii.private.data):-1:1,
gii.private.data{d}.data = [];
end
extra = gii.private;
% Delete the temporary GIFTI file.
% Note that here the extension is "data" because it comes from the
% Workbench. In the writing function, it's just "dat", because it
% comes from the GIFTI I/O.
delete(temp_gii);
delete(strcat(temp_gii,'.data'));