-
Notifications
You must be signed in to change notification settings - Fork 7
/
swe_select.m
277 lines (260 loc) · 10.8 KB
/
swe_select.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
function varargout = swe_select(varargin)
% File selector
% This is a copy of swe_select but able to handle CIfTI files as well.
% FORMAT [t,sts] = swe_select(n,typ,mesg,sel,wd,filt,frames)
% n - number of files [Default: Inf]
% A single value or a range. e.g.
% 1 - select one file
% Inf - select any number of files
% [1 Inf] - select 1 to Inf files
% [0 1] - select 0 or 1 files
% [10 12] - select from 10 to 12 files
% typ - file type [Default: 'any']
% 'any' - all files
% 'image' - Image files (".img" and ".nii")
% Note that it gives the option to select individuals
% volumes of the images.
% 'mesh' - Surface mesh files (".gii")
% 'xml' - XML files
% 'mat' - MATLAB .mat files or .txt files (assumed to contain
% ASCII representation of a 2D-numeric array)
% 'batch' - SPM batch files (.m or .mat)
% 'dir' - select a directory
% Other strings act as a filter to regexp. This means that
% e.g. DCM*.mat files should have a typ of '^DCM.*\.mat$'
% mesg - a prompt [Default: 'Select files...']
% sel - list of already selected files [Default: {}]
% wd - directory to start off in [Default: pwd]
% filt - value for user-editable filter [Default: '.*']
% frames - image frame numbers to include [Default: '1']
%
% t - selected files
% sts - status (1 means OK, 0 means window quit)
%
% FORMAT [files,dirs] = swe_select('List',direc,filt)
% Return files matching the filter 'filt' and directories within 'direc'
% direc - directory to search [Default: pwd]
% filt - filter to select files with regexp, e.g. '^w.*\.img$' [Default: '.*']
%
% files - files matching 'filt' in directory 'direc'
% dirs - subdirectories of 'direc'
%
% FORMAT [files,dirs] = swe_select('ExtList',direc,filt,frames)
% As above, but for selecting frames of 4D NIfTI files
% frames - vector of frames to select (defaults to 1, if not specified).
% If the frame number is Inf, all frames for the matching images
% are listed.
%
% FORMAT [files,dirs] = swe_select('FPList',direc,filt)
% FORMAT [files,dirs] = swe_select('ExtFPList',direc,filt,frames)
% As above, but return files with full paths (i.e. prefixes 'direc' to each)
%
% FORMAT [files,dirs] = swe_select('FPListRec',direc,filt)
% FORMAT [files,dirs] = swe_select('ExtFPListRec',direc,filt,frames)
% As above, but return files with full paths (i.e. prefixes 'direc' to each)
% and search through sub directories recursively.
%
% FORMAT [dirs] = swe_select('List',direc,'dir',filt)
% FORMAT [dirs] = swe_select('FPList',direc,'dir',filt)
% FORMAT [dirs] = swe_select('FPListRec',direc,'dir',filt)
% Return directory names matching filter 'filt' within 'direc'
%__________________________________________________________________________
% Copyright (C) 2005-2015 Wellcome Trust Centre for Neuroimaging
% John Ashburner
% Modified by Bryan Guillaume
% Version Info: $Format:%ci$ $Format:%h$
% For developers:
%--------------------------------------------------------------------------
% FORMAT cpath = swe_select('CPath',path,cwd)
% Canonicalise paths: prepend cwd to relative paths, process '..' & '.'
% directories embedded in path.
% path - string matrix containing path name
% cwd - current working directory [Default: '.']
% cpath - canonicalised paths, in same format as input path argument
%
% FORMAT [t,ind] = swe_select('Filter',files,typ,filt,frames)
% Filter the list of files (cell or char array) in the same way as the
% GUI would do. There is an additional typ 'extimage' which will match
% images with frame specifications, too. Also, there is a typ 'extdir',
% which will match canonicalised directory names. The 'frames' argument
% is currently ignored, i.e. image files will not be filtered out if
% their frame numbers do not match.
% t returns the filtered list (cell or char array, depending on input),
% ind an index array, such that t = files{ind}, or t = files(ind,:).
%
% FORMAT swe_select('PrevDirs',dir)
% Add directory dir to list of previous directories.
% FORMAT dirs = swe_select('PrevDirs')
% Retrieve list of previous directories.
%
% FORMAT files = swe_select('Expand',files)
% Return a list of image filenames with appended frame numbers.
persistent isInitSelect;
if isempty(isInitSelect)
isInitSelect = true;
swe_select('init');
if nargin == 1 && strcmpi(varargin{1},'init'), return; end
end
%-Commands that are not passed to cfg_getfile
local_cmds = {'regfilter', 'init', 'expand'};
if nargin && ischar(varargin{1}) && any(strcmpi(varargin{1},local_cmds))
switch lower(varargin{1})
case 'init'
if ~isdeployed && ~exist('cfg_getfile','file')
addpath(fullfile(spm('dir'),'matlabbatch'));
end
swe_select('regfilter');
swe_select('prevdirs',spm('Dir'));
case 'regfilter'
% Regexp based filters without special handlers
cfg_getfile('regfilter', 'mesh', {'\.gii$'});
cfg_getfile('regfilter', 'gifti', {'\.gii$'});
cfg_getfile('regfilter', 'nifti', {'\.nii$','\.img$'});
% Filter for 3D images that handles frame expansion
frames = cfg_entry;
frames.name = 'Frames';
frames.tag = 'frames';
frames.strtype = 'n';
frames.num = [1 Inf];
frames.val = {1};
cfg_getfile('regfilter', 'image',...
{'.*\.nii(,\d+){0,2}$','.*\.img(,\d+){0,2}$'},...
false, @swe_select_image, {frames});
case 'expand'
varargout{1} = swe_select_expand(varargin{2});
if ischar(varargin{2}), varargout{1} = char(varargout{1}); end
end
else
needchar = false;
% cfg_getfile expects cellstr arguments for multi-line strings
if nargin > 1 && ischar(varargin{1}) && ...
ismember(lower(varargin{1}),{'filter','cpath'}) && ischar(varargin{2})
varargin{2} = cellstr(varargin{2});
needchar = true;
elseif nargin > 0 && ischar(varargin{1}) && ...
ismember(lower(varargin{1}),{'extlist','extfplist','extfplistrec'})
varargin{1} = varargin{1}(4:end);
if nargin > 3
varargin{5} = struct('frames', varargin{4});
else
varargin{5} = struct('frames', Inf);
end
varargin{4} = varargin{3};
varargin{3} = 'image';
elseif nargin > 6 && isnumeric(varargin{1})
varargin{7} = struct('frames', varargin{7});
end
[t, sts] = cfg_getfile(varargin{:});
% cfg_getfile returns cellstr arrays, convert to char arrays
if nargin > 0 && ischar(varargin{1})
switch lower(varargin{1})
case {'filter','cpath'}
if needchar
t = char(t);
end
case {'list','fplist','extlist','extfplist','fplistrec','extfplistrec'}
t = char(t);
sts = char(sts);
end
else
t = char(t);
end
varargout{1} = t;
if nargout > 1
varargout{2} = sts;
end
end
%==========================================================================
% FUNCTION varargout = swe_select_image(cmd, varargin)
%==========================================================================
function varargout = swe_select_image(cmd, varargin)
% Implements extended filtering for NIfTI images (including 3D frame
% selection)
switch lower(cmd)
case 'list'
dr = varargin{1};
files = varargin{2};
prms = varargin{3};
frames = prms.frames;
ii = cell(1,numel(files));
if numel(frames)==1 && isnan(frames)
[ii{:}] = deal(1);
elseif numel(frames)~=1 || frames(1)~=1
% if domsg
% msg(ob,['Reading headers of ' num2str(numel(f)) ' images...']);
% end
for i=1:numel(files)
try
n = swe_select_get_nbframes(fullfile(dr,files{i}));
d4 = (1:n)';
catch
d4 = 1;
end
if all(isfinite(frames))
ii{i} = intersect(d4, frames(:))';
else
ii{i} = d4(:)';
end
end
elseif numel(frames)==1 && frames(1)==1
[ii{:}] = deal(1);
end
% if domsg
% msg(ob,['Listing ' num2str(numel(f)) ' files...']);
% end
% Combine filename and frame number(s)
nii = cellfun(@numel, ii);
cfiles = cell(sum(nii),1);
fi = cell(numel(files),1);
for k = 1:numel(fi)
fi{k} = k*ones(1,nii(k));
end
ii = [ii{:}];
fi = [fi{:}]';
if numel(frames)==1 && isnan(frames)
cfiles = files;
else
for i=1:numel(cfiles)
cfiles{i} = sprintf('%s,%d', files{fi(i)}, ii(i));
end
end
varargout{1} = cfiles;
varargout{2} = fi;
case 'filter'
% Do not filter for frame numbers
varargout{1} = varargin{1};
varargout{2} = 1:numel(varargout{1});
end
%==========================================================================
% FUNCTION ofiles = swe_select_expand(ifiles)
%==========================================================================
function ofiles = swe_select_expand(ifiles)
ifiles = cellstr(ifiles);
ofiles = {};
for i=1:numel(ifiles)
[p,f,e,n] = spm_fileparts(ifiles{i});
if ~isempty(n)
ofiles = [ofiles; ifiles{i}];
else
n = swe_select_get_nbframes(ifiles{i});
vfiles = cell(n,1);
for j=1:n
vfiles{j} = [ifiles{i} ',' num2str(j)];
end
ofiles = [ofiles; vfiles];
end
end
%==========================================================================
% FUNCTION n = swe_select_get_nbframes(file)
%==========================================================================
function n = swe_select_get_nbframes(file)
file_ext = swe_get_file_extension(file);
isCifti = strcmpi(file_ext,'.dtseries.nii') || strcmpi(file_ext,'.dscalar.nii');
if isCifti
ciftiObject = swe_cifti(file);
n = ciftiObject.dat.dim(5);
else
N = nifti(file);
dim = [N.dat.dim 1 1 1 1 1];
n = dim(4);
end