-
Notifications
You must be signed in to change notification settings - Fork 28
/
palm_miscwrite.m
253 lines (210 loc) · 8.31 KB
/
palm_miscwrite.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
244
245
246
247
248
249
250
251
252
253
function palm_miscwrite(varargin)
% Write various scalar data formats based on the struct X.
%
% palm_miscwrite(X);
%
% See 'palm_miscread.m' for a description of the contents of X.
%
% _____________________________________
% Anderson M. Winkler
% FMRIB / University of Oxford
% Aug/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/>.
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
X = varargin{1};
switch lower(X.readwith)
case 'textscan'
% Write a generic text file. Note that this doesn't recover
% the original file (spaces become newlines).
[~,~,fext] = fileparts(X.filename);
if isempty(fext)
X.filename = horzcat(X.filename,'.txt');
end
fid = fopen(X.filename,'w');
fprintf(fid,'%s\n',X.data{:});
fclose(fid);
case {'load','csvread'}
% Write a CSV file.
[~,~,fext] = fileparts(X.filename);
if isempty(fext) || ~ strcmpi(fext,'.csv')
X.filename = horzcat(X.filename,'.csv');
end
dlmwrite(X.filename,X.data,'delimiter',',','precision','%0.4f');
case 'vestread'
% Write an FSL "VEST" file
palm_vestwrite(X.filename,X.data);
case 'msetread'
% Write an MSET (matrix set) file
[~,~,fext] = fileparts(X.filename);
if isempty(fext) || ~ strcmpi(fext,'.mset')
X.filename = horzcat(X.filename,'.mset');
end
palm_msetwrite(X.filename,X.data);
case 'wb_command'
% Write a CIFTI file using the HCP Workbench
if nargin == 2
toscalar = varargin{2};
else
toscalar = false;
end
siz = size(X.data);
if siz(1) == 1 && siz(2) > 1
X.data = X.data';
end
palm_ciftiwrite(X.filename,X.data,X.extra,[],toscalar);
case 'cifti-matlab'
% Write a CIFTI file using the CIFTI-Matlab toolbox
% First deal with the file extension
[fpth,fnam,fext] = fileparts(X.filename);
if strcmpi(fext,['.',X.extra.cifti_file_extension])
X.filename = fullfile(fpth,[fnam,fext,'.nii']);
elseif ~ strcmpi(fext,'.nii')
X.filename = fullfile(fpth,[fnam,fext,'.',X.extra.cifti_file_extension,'.nii']);
end
tmp.cdata = X.data;
tmp.diminfo = X.extra.diminfo;
tmp.metadata = X.extra.metadata;
cifti_write(tmp,X.filename);
case 'ipt'
% Write NIFTI using the Image Processing Toolbox (or equivalent
% commands from Octave)
X.filename = horzcat(X.filename,'.nii');
tmp = ones(size(X.extra.hdr.ImageSize));
siz = size(X.data);
tmp(1:numel(siz)) = siz;
X.extra.hdr.ImageSize = tmp;
X.extra.hdr.DataType = class(X.data);
switch X.extra.hdr.DataType
case {'logical','uint8','int8'}
X.extra.hdr.BitsPerPixel = 8;
case {'char','uint16','int16'}
X.extra.hdr.BitsPerPixel = 16;
case {'single','uint32','int32'}
X.extra.hdr.BitsPerPixel = 32;
case {'double','uint64','int64'}
X.extra.hdr.BitsPerPixel = 64;
end
%X.extra.hdr.raw.dim(2:numel(tmp)+1) = tmp;
%X.extra.hdr.raw.dim(1) = find(logical(X.extra.hdr.raw.dim(2:end)-1),1,'last');
niftiwrite(X.data,X.filename,X.extra.hdr);
case 'nifticlass'
% Write using the NIFTI class.
[~,~,fext] = fileparts(X.filename);
if isempty(fext)
X.filename = horzcat(X.filename,'.nii');
end
dat = file_array( ...
X.filename, ...
size(X.data), ...
'FLOAT32-LE', ...
ceil(348/8)*8);
nii = nifti;
nii.dat = dat;
nii.mat = X.extra.mat;
if isfield(X.extra,'mat0')
nii.mat0 = X.extra.mat0;
else
nii.mat0 = X.extra.mat;
end
create(nii);
nii.dat(:,:,:) = X.data(:,:,:);
case 'spm_spm_vol'
% Write NIFTI with SPM
[~,~,fext] = fileparts(X.filename);
if isempty(fext)
X.filename = horzcat(X.filename,'.nii');
end
X.extra.fname = X.filename;
X.extra.dt(1) = spm_type('float32'); % for now, save everything as double
spm_write_vol(X.extra,X.data);
case 'fs_load_nifti'
% Write NIFTI with FreeSurfer
X.filename = horzcat(X.filename,'.nii.gz');
X.extra.hdr.vol = X.data;
X.extra.hdr.datatype = 64; % for now, save everything as double
X.extra.hdr.bitpix = 64; % for now, save everything as double
X.extra.hdr.scl_inter = 0; % ideally we'd rescale X.data based on the current scl_slope and scl_intercept. However, this introduces unneccessary rounding errors
X.extra.hdr.scl_slope = 1;
X.extra.hdr.cal_max = max(X.data(:));
X.extra.hdr.cal_min = min(X.data(:));
save_nifti(X.extra.hdr,X.filename);
case 'fsl_read_avw'
% Write NIFTI with FSL
if ~ isfield(X.extra,'vtype')
X.extra.vtype = 'd'; % for now, save everything as double
end
% The evalc is needed until that empty 'disp' goes away
try
[~] = evalc('save_avw(X.data,X.filename,X.extra.vtype,X.extra.voxsize)');
catch
save_avw(X.data,X.filename,X.extra.vtype,X.extra.voxsize);
end
case 'nii_load_nii'
% Write NIFTI with the NIFTI toolbox
X.filename = horzcat(X.filename,'.nii');
X.extra.img = X.data;
save_nii(X.extra,X.filename);
case 'dpxread'
% Write a DPX (DPV or DPF) file
palm_dpxwrite(X.filename,X.data,X.extra.crd,X.extra.idx);
case 'srfread'
% Write a SRF file
srfwrite(X.data.vtx,X.data.fac,X.filename);
case 'mz3'
% Write a MZ3 file
if numel(fieldnames(X.data)) == 2
writeMz3(X.filename,X.data.fac,X.data.vtx,X.extra.colours);
else
writeMz3(X.filename,X.extra.fac,X.extra.vtx,X.data);
end
case 'fs_read_curv'
% Write a FreeSurfer curvature file
write_curv(X.filename,X.data,X.extra.fnum);
case 'fs_read_surf'
% Write a FreeSurfer surface file
write_surf(X.filename,X.data.vtx,X.data.fac);
case 'fs_load_mgh'
% Write a FreeSurfer MGH file
[~,~,fext] = fileparts(X.filename);
if isempty(fext) || ~ ( strcmpi(fext,'.mgh') || strcmpi(fext,'.mgz'))
X.filename = horzcat(X.filename,'.mgz');
end
save_mgh(X.data,X.filename,X.extra.M,X.extra.mr_parms);
case 'fs_load_annot'
% Write a FreeSurfer annotation file
X.filename = horzcat(X.filename,'.annot');
write_annotation(X.filename,X.extra.vertices,X.data,X.extra.colortab);
case 'gifti'
% Write a GIFTI file
X.filename = horzcat(X.filename,'.gii');
for i = 1:length(X.extra.data)
if ~ numel(X.extra.data{i}.metadata.value)
X.extra.data{i}.metadata.value = '';
end
end
gii = gifti([]);
F = fieldnames(X.extra);
for f = 1:numel(F)
gii.private.(F{f}) = X.extra.(F{f});
end
if ~ isstruct(X.data)
gii.cdata = X.data';
end
save(gii,X.filename,X.extra.data{1}.attributes.Encoding);
end