-
Notifications
You must be signed in to change notification settings - Fork 5
/
cat_io_checkdepfiles.m
103 lines (98 loc) · 3.74 KB
/
cat_io_checkdepfiles.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
function [S,Stype,removed] = cat_io_checkdepfiles(S,usedummy)
% _________________________________________________________________________
% Remove non-existing files from the (SPM dependency) variable S.
% If there is only one file than create a dummy file to avoid batch errors.
% However, this may cause larger problems and is false by default and print
% an error message (just print a message, no real error).
% Moreover, it is possible that removing an entry can cause problems in
% related datasets that count on a specific file order, so a message is
% printed even in this case ...
%
% S = cat_io_checkdepfiles(S);
%
% [S,Stype] = cat_io_checkdepfiles(S,usedummy)
% ______________________________________________________________________
%
% Christian Gaser, Robert Dahnke
% Structural Brain Mapping Group (https://neuro-jena.github.io)
% Departments of Neurology and Psychiatry
% Jena University Hospital
% ______________________________________________________________________
% $Id$
if ~exist('usedummy','var')
usedummy = 0;
end
Stype = '';
removed = 0;
if ischar(S)
for i = 1:size(S,1)
if ~exist(S(i,:),'file')
% Here, I remove just one element that is missing (e.g. one failed
% preprocessing). However, a following job (e.g. smoothing) does
% not count on it.
[pp,ff,ee] = spm_fileparts(S(i,:));
S(i,:) = '';
Stype = ee;
removed = 1;
% prevent interpreting backslash as escape character
removeFile = strrep(fullfile(pp,[ff ee]), '\', '\\');
cat_io_cprintf('warn',sprintf(' Remove "%s" from dependency list because it does not exist!\n',removeFile));
end
end
elseif iscell(S)
for i = 1:numel(S)
S{i} = cat_io_checkdepfiles( S{i} , usedummy );
end
elseif isstruct(S)
%%
FN = fieldnames(S);
for i = 1:numel(FN)
SFN = S.(FN{i});
for ii = 1:numel( S.(FN{i}) )
SFNi = SFN(ii);
[SFNi,SFNtype,removed] = cat_io_checkdepfiles( SFNi ,usedummy );
SFN(ii) = SFNi;
clear SFNi;
end
if isstruct(SFN)==0
if isscalar(SFN) && size(SFN{1},1)==0 && ~isequal(SFN,S.(FN{i}))
if usedummy
SFN = {create_dummy_volume(SFNtype)};
else
% Here, the full entry of a class of files becomes empty and this
% of cause will trouble an following job (empty imput).
% I give not further file information because these were given by
% the upper warning!
cat_io_cprintf('err',sprintf(['One or multiple files do not exist and were removed from the dependency list \n' ...
'and following batches will may not work correctly!\n\n']));
SFN = {};
end
elseif removed
cat_io_cprintf('warn',sprintf([
'One or multiple files do not exist and were removed from the dependency list' \n ...
'and following batches those file input number and order is relevant may do not work properly!\n\n']));
end
end
S.(FN{i}) = SFN;
clear SFN;
end
end
end
function Pdummy = create_dummy_volume(type)
switch type
case '.nii'
Pvol = fullfile(cat_get_defaults('extopts.pth_templates'),'cat.nii');
Pdummy = fullfile(fileparts(mfilename('fullpath')),'cattest','batchdummy.nii');
if ~exist( fileparts(Pdummy) , 'dir')
mkdir( fileparts(Pdummy) );
end
copyfile(Pvol,Pdummy);
case {'.xml','.csv','.txt',''}
Pvol = fullfile(cat_get_defaults('extopts.pth_templates'),'mori.csv');
Pdummy = fullfile(fileparts(mfilename('fullpath')),'cattest',['batchdummy' type]);
if ~exist( fileparts(Pdummy) , 'dir')
mkdir( fileparts(Pdummy) );
end
copyfile(Pvol,Pdummy);
end
end