1 | function [done,msg] = aedes_saveres(DATA,ROI,filename,varargin) |
---|
2 | % AEDES_SAVERES - Save Aedes results (statistics and ROIs) |
---|
3 | % |
---|
4 | % |
---|
5 | % Synopsis: |
---|
6 | % |
---|
7 | % Description: |
---|
8 | % |
---|
9 | % Examples: |
---|
10 | % |
---|
11 | % See also: |
---|
12 | % |
---|
13 | |
---|
14 | % This function is a part of Aedes - A graphical tool for analyzing |
---|
15 | % medical images |
---|
16 | % |
---|
17 | % Copyright (C) 2006 Juha-Pekka Niskanen <Juha-Pekka.Niskanen@uku.fi> |
---|
18 | % |
---|
19 | % Department of Physics, Department of Neurobiology |
---|
20 | % University of Kuopio, FINLAND |
---|
21 | % |
---|
22 | % This program may be used under the terms of the GNU General Public |
---|
23 | % License version 2.0 as published by the Free Software Foundation |
---|
24 | % and appearing in the file LICENSE.TXT included in the packaging of |
---|
25 | % this program. |
---|
26 | % |
---|
27 | % This program is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
---|
28 | % WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
---|
29 | |
---|
30 | |
---|
31 | done=false; |
---|
32 | msg=''; |
---|
33 | |
---|
34 | %% Default values for properties |
---|
35 | SaveType = 'all'; % Save ROIs and statistics by default |
---|
36 | FileName = 'Untitled'; % Filename(s) Untitled.res and Untitled.roi |
---|
37 | FilePath = pwd; % Use working directory by default |
---|
38 | ShowWbar = true; % Show waitbar by default |
---|
39 | RotateFlip = []; |
---|
40 | ConfirmOverwrite = true; |
---|
41 | |
---|
42 | |
---|
43 | %% Parse input arguments |
---|
44 | if nargin<3 |
---|
45 | msg = 'Too few input arguments'; |
---|
46 | return |
---|
47 | end |
---|
48 | |
---|
49 | %% Parse varargin |
---|
50 | for ii=1:2:length(varargin) |
---|
51 | switch lower(varargin{ii}) |
---|
52 | case {'wbar','waitbar'} |
---|
53 | if ischar(varargin{ii+1}) |
---|
54 | if strcmpi(varargin{ii+1},'on') |
---|
55 | ShowWbar = true; |
---|
56 | else |
---|
57 | ShowWbar = false; |
---|
58 | end |
---|
59 | else |
---|
60 | if varargin{ii+1}==1 |
---|
61 | ShowWbar = true; |
---|
62 | else |
---|
63 | ShowWbar = false; |
---|
64 | end |
---|
65 | end |
---|
66 | case 'savetype' |
---|
67 | SaveType = varargin{ii+1}; |
---|
68 | |
---|
69 | case 'rotateflip' |
---|
70 | RotateFlip = varargin{ii+1}; |
---|
71 | |
---|
72 | case 'confirmoverwrite' |
---|
73 | ConfirmOverwrite = varargin{ii+1}; |
---|
74 | |
---|
75 | otherwise |
---|
76 | done=false; |
---|
77 | msg=sprintf('Unknown parameter "%s"!',varargin{ii}); |
---|
78 | return |
---|
79 | end |
---|
80 | end |
---|
81 | |
---|
82 | [fp,fn,fe]=fileparts(filename); |
---|
83 | if any(strcmpi(fe,{'.roi','.res'})) |
---|
84 | FileName = fn; |
---|
85 | else |
---|
86 | FileName = [fn,fe]; |
---|
87 | end |
---|
88 | if ~isempty(fp) |
---|
89 | FilePath = [fp,filesep]; |
---|
90 | else |
---|
91 | FilePath = [pwd,filesep]; |
---|
92 | end |
---|
93 | |
---|
94 | if isstruct(DATA) |
---|
95 | DATA = {DATA}; |
---|
96 | elseif ~iscell(DATA) |
---|
97 | msg='Input argument "DATA" is not valid'; |
---|
98 | return |
---|
99 | end |
---|
100 | |
---|
101 | if isempty(ROI) || ~isstruct(ROI) |
---|
102 | msg='Input argument "ROI" is not valid'; |
---|
103 | return |
---|
104 | end |
---|
105 | |
---|
106 | %% Save results and/or ROIs |
---|
107 | switch SaveType |
---|
108 | case 'all' % Save both ROI and RES -------------------- |
---|
109 | |
---|
110 | %% Check if files to be written already exist |
---|
111 | tmp=dir(FilePath); |
---|
112 | fnames = {tmp(~[tmp(:).isdir]).name}; |
---|
113 | if any(strcmpi(fnames,[FileName,'.roi'])) |
---|
114 | RoiFileExists = true; |
---|
115 | else |
---|
116 | RoiFileExists = false; |
---|
117 | end |
---|
118 | if any(strcmpi(fnames,[FileName,'.res'])) |
---|
119 | ResFileExists = true; |
---|
120 | else |
---|
121 | ResFileExists = false; |
---|
122 | end |
---|
123 | |
---|
124 | if ResFileExists && ConfirmOverwrite |
---|
125 | resp=questdlg({['"',FilePath,FileName,'.res" already exists.'],... |
---|
126 | 'Overwrite?'},'Overwrite File?',... |
---|
127 | 'Yes','No','No'); |
---|
128 | if strcmpi(resp,'No') |
---|
129 | msg='Overwrite cancel'; |
---|
130 | return |
---|
131 | end |
---|
132 | end |
---|
133 | if RoiFileExists && ConfirmOverwrite |
---|
134 | resp=questdlg({['"',FilePath,FileName,'.roi" already exists.'],... |
---|
135 | 'Overwrite?'},'Overwrite File?',... |
---|
136 | 'Yes','No','No'); |
---|
137 | if strcmpi(resp,'No') |
---|
138 | msg='Overwrite cancel'; |
---|
139 | return |
---|
140 | end |
---|
141 | end |
---|
142 | |
---|
143 | if ShowWbar |
---|
144 | [h,txh]=aedes_calc_wait({'Saving ROI(s)...',... |
---|
145 | ['(',FilePath,FileName,'.roi)']}); |
---|
146 | end |
---|
147 | |
---|
148 | [ok,msg]=l_SaveRoi(DATA,ROI,FileName,FilePath,RotateFlip); |
---|
149 | if ~ok |
---|
150 | if ShowWbar |
---|
151 | delete(h) |
---|
152 | end |
---|
153 | return |
---|
154 | end |
---|
155 | set(txh,'string',{'Saving ROI Statistics...',... |
---|
156 | ['(',FilePath,FileName,'.res)']}) |
---|
157 | [ok,msg]=l_SaveRes(DATA,ROI,FileName,FilePath); |
---|
158 | if ~ok |
---|
159 | if ShowWbar |
---|
160 | delete(h) |
---|
161 | end |
---|
162 | return |
---|
163 | end |
---|
164 | if ShowWbar |
---|
165 | delete(h) |
---|
166 | end |
---|
167 | |
---|
168 | case 'roi' % Save only ROI ---------------------------- |
---|
169 | |
---|
170 | %% Check if files to be written already exist |
---|
171 | tmp=dir(FilePath); |
---|
172 | fnames = {tmp(~[tmp(:).isdir]).name}; |
---|
173 | if any(strcmpi(fnames,[FileName,'.roi'])) |
---|
174 | RoiFileExists = true; |
---|
175 | else |
---|
176 | RoiFileExists = false; |
---|
177 | end |
---|
178 | |
---|
179 | if RoiFileExists && ConfirmOverwrite |
---|
180 | resp=questdlg({['"',FilePath,FileName,'.roi" already exists.'],... |
---|
181 | 'Overwrite?'},'Overwrite File?',... |
---|
182 | 'Yes','No','No'); |
---|
183 | if strcmpi(resp,'No') |
---|
184 | msg='Overwrite cancel'; |
---|
185 | return |
---|
186 | end |
---|
187 | end |
---|
188 | |
---|
189 | if ShowWbar |
---|
190 | [h,txh]=aedes_calc_wait({'Saving ROI(s)...',... |
---|
191 | ['(',FilePath,FileName,'.roi)']}); |
---|
192 | end |
---|
193 | |
---|
194 | [ok,msg]=l_SaveRoi(DATA,ROI,FileName,FilePath,RotateFlip); |
---|
195 | if ~ok |
---|
196 | if ShowWbar |
---|
197 | delete(h) |
---|
198 | end |
---|
199 | return |
---|
200 | end |
---|
201 | if ShowWbar |
---|
202 | delete(h) |
---|
203 | end |
---|
204 | |
---|
205 | case 'res' % Save only RES ---------------------------- |
---|
206 | |
---|
207 | %% Check if files to be written already exist |
---|
208 | tmp=dir(FilePath); |
---|
209 | fnames = {tmp(~[tmp(:).isdir]).name}; |
---|
210 | if any(strcmpi(fnames,[FileName,'.res'])) |
---|
211 | ResFileExists = true; |
---|
212 | else |
---|
213 | ResFileExists = false; |
---|
214 | end |
---|
215 | |
---|
216 | if ResFileExists && ConfirmOverwrite |
---|
217 | resp=questdlg({['"',FilePath,FileName,'.res" already exists.'],... |
---|
218 | 'Overwrite?'},'Overwrite File?',... |
---|
219 | 'Yes','No','No'); |
---|
220 | if strcmpi(resp,'No') |
---|
221 | msg='Overwrite cancel'; |
---|
222 | return |
---|
223 | end |
---|
224 | end |
---|
225 | |
---|
226 | if ShowWbar |
---|
227 | [h,txh]=aedes_calc_wait({'Saving ROI Statistics...',... |
---|
228 | ['(',FilePath,FileName,'.res)']}); |
---|
229 | end |
---|
230 | |
---|
231 | [ok,msg]=l_SaveRes(DATA,ROI,FileName,FilePath); |
---|
232 | if ~ok |
---|
233 | if ShowWbar |
---|
234 | delete(h) |
---|
235 | end |
---|
236 | return |
---|
237 | end |
---|
238 | if ShowWbar |
---|
239 | delete(h) |
---|
240 | end |
---|
241 | |
---|
242 | otherwise |
---|
243 | msg='Unknown save type'; |
---|
244 | return |
---|
245 | end |
---|
246 | |
---|
247 | % No problems if this point is reached... |
---|
248 | done=true; |
---|
249 | return |
---|
250 | |
---|
251 | |
---|
252 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
253 | % Save ROIs |
---|
254 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
255 | function [ok,msg]=l_SaveRoi(DATA,ROI,FileName,FilePath,rotflip) |
---|
256 | |
---|
257 | ok = false; |
---|
258 | msg=''; |
---|
259 | |
---|
260 | % Generate timestamp |
---|
261 | DateTime = datestr(now); |
---|
262 | |
---|
263 | % Append file information to the save ROI file |
---|
264 | for ii=1:length(DATA) |
---|
265 | FileInfo.DataFileName{ii} = DATA{ii}.HDR.fname; |
---|
266 | FileInfo.DataPathName{ii} = DATA{ii}.HDR.fpath; |
---|
267 | end |
---|
268 | |
---|
269 | % Append rotate and flip information |
---|
270 | if iscell(rotflip) |
---|
271 | RotateFlip = rotflip; |
---|
272 | else |
---|
273 | RotateFlip.Rotate = zeros(1,length(DATA)); |
---|
274 | RotateFlip.Flip = zeros(1,length(DATA)); |
---|
275 | if ~isempty(rotflip) |
---|
276 | RotateFlip.Rotate = rotflip(1,:); |
---|
277 | RotateFlip.Flip = rotflip(2,:); |
---|
278 | end |
---|
279 | end |
---|
280 | |
---|
281 | % Save ROI(s) |
---|
282 | try |
---|
283 | save([FilePath,FileName,'.roi'],'ROI','DateTime','FileInfo','RotateFlip','-mat') |
---|
284 | catch |
---|
285 | msg={'Could not save ROI(s). Following error was returned:',lasterr}; |
---|
286 | return |
---|
287 | end |
---|
288 | |
---|
289 | % All went well |
---|
290 | ok=true; |
---|
291 | |
---|
292 | |
---|
293 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
294 | % Save RES |
---|
295 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
296 | function [ok,msg]=l_SaveRes(DATA,ROI,FileName,FilePath) |
---|
297 | |
---|
298 | ok = false; |
---|
299 | msg = ''; |
---|
300 | |
---|
301 | |
---|
302 | try |
---|
303 | Res = []; |
---|
304 | % Calculate results |
---|
305 | Res = aedes_roi_stats(DATA,ROI); |
---|
306 | if isempty(Res) |
---|
307 | msg = 'Error while calculating statistics. Could not save results.'; |
---|
308 | return |
---|
309 | end |
---|
310 | |
---|
311 | % Generate timestamp |
---|
312 | %DateTime = datestr(now); |
---|
313 | |
---|
314 | % Generate file info |
---|
315 | %for ii=1:length(DATA) |
---|
316 | % FileInfo.DataFileName{ii} = DATA{ii}.HDR.fname; |
---|
317 | % FileInfo.DataPathName{ii} = DATA{ii}.HDR.fpath; |
---|
318 | %end |
---|
319 | %Res.DateTime = DateTime; |
---|
320 | %Res.FileInfo = FileInfo; |
---|
321 | %Res.Stat = Stat; |
---|
322 | |
---|
323 | % Save results |
---|
324 | save([FilePath,FileName,'.res'],'Res','-mat') |
---|
325 | catch |
---|
326 | msg = {'Could not save results. Following error was returned:', ... |
---|
327 | lasterr}; |
---|
328 | return |
---|
329 | end |
---|
330 | |
---|
331 | % All went well |
---|
332 | ok = true; |
---|