-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathgetAnatFilename.m
More file actions
194 lines (153 loc) · 5.24 KB
/
getAnatFilename.m
File metadata and controls
194 lines (153 loc) · 5.24 KB
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
function [anatImage, anatDataDir] = getAnatFilename(varargin)
%
% Get the filename and the directory of some anat files for a given session and run.
% Unzips the files if necessary.
%
% It several images are available it will take the first one it finds.
%
% USAGE::
%
% [anatImage, anatDataDir] = getAnatFilename(BIDS, subLabel, opt, nbImgToReturn, tolerant)
%
% :param BIDS: dataset layout.
% See also: bids.layout, getData.
% :type BIDS: structure
%
% :param subLabel:
% :type subLabel: char
%
% :param opt: Options chosen for the analysis.
% See checkOptions.
% :type opt: structure
%
% :returns: - :anatImage: (string)
% - :anatDataDir: (string)
%
% (C) Copyright 2020 bidspm developers
% TODO try to channel this function via getInfo ?
args = inputParser;
addRequired(args, 'BIDS', @isstruct);
addRequired(args, 'opt', @isstruct);
addRequired(args, 'subLabel', @ischar);
addOptional(args, 'nbImgToReturn', 1);
addOptional(args, 'tolerant', false, @islogical);
parse(args, varargin{:});
BIDS = args.Results.BIDS;
opt = args.Results.opt;
subLabel = args.Results.subLabel;
nbImgToReturn = args.Results.nbImgToReturn;
tolerant = args.Results.tolerant;
checkAvailableSuffix(BIDS, subLabel, opt.bidsFilterFile.t1w, tolerant);
if isfield(opt.bidsFilterFile.t1w, 'ses')
checkAvailableSessions(BIDS, subLabel, opt.bidsFilterFile.t1w, tolerant);
end
filter = opt.bidsFilterFile.t1w;
filter.extension = '.nii';
filter.prefix = '';
filter.sub = regexify(subLabel);
% get all anat images for that subject fo that type
anat = bids.query(BIDS, 'data', filter);
if isempty(anat)
msg = sprintf('No anat file for:\n%s\n\n', ...
bids.internal.create_unordered_list(filter));
id = 'noAnatFile';
if tolerant
logger('WARNING', msg, 'id', id, 'filename', mfilename(), 'options', opt);
else
logger('ERROR', msg, 'id', id, 'filename', mfilename(), 'options', opt);
end
end
if numel(anat) > nbImgToReturn
tmp = anat(1:nbImgToReturn);
tmp = strrep(tmp, [BIDS.pth filesep], '');
tmp = bids.internal.format_path(tmp);
msg = sprintf(['More than %i anat file in:\n%s.', ...
'\n\nFound: %i.\nTaking the first %i:%s'], ...
nbImgToReturn, ...
bids.internal.format_path(BIDS.pth), ...
numel(anat), ...
nbImgToReturn, ...
bids.internal.create_unordered_list(tmp));
id = 'severalAnatFile';
logger('WARNING', msg, 'id', id, 'filename', mfilename());
end
anatDataDir = '';
anatImage = '';
if isempty(anat)
return
end
if nbImgToReturn == 1
anat = anat{1};
elseif nbImgToReturn == Inf
else
anat = anat(1:nbImgToReturn);
end
anatImage = unzipAndReturnsFullpathName(anat);
msg = sprintf(' selecting anat file(s): %s', ...
bids.internal.create_unordered_list(bids.internal.format_path(anat)));
logger('DEBUG', msg, 'options', opt, 'filename', mfilename());
tmpDir = {};
tmpImage = {};
for iFile = 1:size(anatImage, 1)
[tmpDir{end + 1, 1}, name, ext] = spm_fileparts(anatImage(iFile, :)); %#ok<*AGROW>
tmpImage{end + 1, 1} = [name ext];
end
anatDataDir = tmpDir;
anatImage = tmpImage;
if size(anatImage, 1) == 1
anatDataDir = char(anatDataDir);
anatImage = char(anatImage);
end
end
function checkAvailableSuffix(BIDS, subLabel, filter, tolerant)
anatSuffixes = filter.suffix;
filter = rmfield(filter, 'suffix');
if isfield(filter, 'ses')
filter = rmfield(filter, 'ses');
end
filter.sub = subLabel;
availableSuffixes = bids.query(BIDS, 'suffixes', filter);
containsSuffix = regexp(availableSuffixes, regexify(anatSuffixes), 'match');
if all(cellfun('isempty', containsSuffix))
msg = sprintf(['Requested anatomical suffix %s unavailable for subject %s.'...
'\nAll available suffixes:\n%s'], ...
anatSuffixes, ...
subLabel, ...
bids.internal.create_unordered_list(availableSuffixes));
id = 'requestedSuffixUnvailable';
if tolerant
logger('WARNING', msg, 'id', id, 'filename', mfilename());
else
logger('ERROR', msg, 'id', id, 'filename', mfilename());
end
end
end
function anatSession = checkAvailableSessions(BIDS, subLabel, filter, tolerant)
anatSession = filter.ses;
filter = rmfield(filter, 'ses');
filter.sub = subLabel;
sessions = bids.query(BIDS, 'sessions', filter);
if numel(sessions) == 0
sessions = {''};
end
if ~isempty(anatSession)
if all(~strcmp(anatSession, sessions))
msg = sprintf(['Requested session %s for anatomical unavailable for subject %s.', ...
'\nAll available sessions:\n%s.'], ...
anatSession, ...
subLabel, ...
bids.internal.create_unordered_list(sessions));
id = 'requestedSessionUnvailable';
if tolerant
logger('WARNING', msg, 'id', id, 'filename', mfilename());
else
logger('ERROR', msg, 'id', id, 'filename', mfilename());
end
end
else
anatSession = sessions;
end
if ~iscell(anatSession)
anatSession = {anatSession};
end
end