-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdp_node_base_support.m
executable file
·208 lines (140 loc) · 6.11 KB
/
dp_node_base_support.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
classdef dp_node_base_support < dp_node_core
% methods to support the running of nodes
% but that are not crucial for the logic
methods
function obj = update(obj, opt, mode) % set necessary properties
if (isempty(obj.name)), obj.name = class(obj); end
if (nargin < 2), opt.present = 1; end
if (nargin < 3), mode = obj.mode; end
% set mode first
obj.mode = mode;
obj.opt = dp_node_base.default_opt(opt);
if (~isempty(obj.mode))
obj.opt = obj.get_dpm().dp_opt(obj.opt);
end
% force outside do_try_catch
if (isfield(opt, 'do_try_catch'))
obj.opt.do_try_catch = opt.do_try_catch;
end
% Report options
obj.log(3, 'Options: %s', formattedDisplayText(obj.opt));
% set log options
obj.log_opt.verbose = obj.opt.verbose;
obj.log_opt.c_level = obj.opt.c_level;
% not sure if this is always a good idea, but let us try it
if (~isempty(obj.previous_node)) && (obj.opt.deep_mode)
obj.previous_node.do_dpm_passthrough = 1;
obj.previous_node.update(obj.opt, mode);
end
end
function [output, err] = run_fun(obj, fun, err_log_fun, do_try_catch)
if (nargin < 4), do_try_catch = obj.opt.do_try_catch; end
if (~do_try_catch) % normal run
output = fun();
err = [];
return;
else % catch errors
try
output = fun();
err = [];
return;
catch me
err_log_fun(me);
output = [];
err = me;
end
end
end
function analyze_output(obj, previous_outputs, outputs, errors)
% Check if we're missing outputs because of an error
% (do generous logging)
if (numel(outputs) == 0) && (numel(previous_outputs) > 0)
obj.log(0, '');
obj.log(0, 'This node (%s) produced no outputs', obj.name);
obj.log(0, ' despite having outputs to process from the previous node');
if (numel(errors) == 0)
obj.log(0, ' and no errors occurred during processing - unexpected!');
else
obj.log(0, ' probably beacuse one or more errors occurred.');
obj.log(0, ' ');
obj.log(0, ' First error:');
obj.log(0, ' ');
obj.log(0, '<a href="matlab: opentoline(''%s'', %d)">%s</a>', errors{1}.stack(1).file, errors{1}.stack(1).line, errors{1}.message);
obj.log(0, ' ');
obj.log(0, '%s', formattedDisplayText(errors{1}.stack(1)));
obj.log(0, ' ');
end
elseif (numel(errors) > 0)
obj.log(0, '%tNote: %i errors occurred (%i valid outputs out of %i previous outputs)', ...
numel(errors), numel(outputs), numel(previous_outputs));
end
end
function output = run_clean(obj, output)
% clean up temporary directory if asked to do so
if (~isstruct(output)), return; end
if (isfield(output, 'tmp')) && ...
(isfield(output.tmp, 'do_delete')) && ...
(output.tmp.do_delete)
msf_delete(output.tmp.bp);
end
end
% run the data processing mode's function here
function output = run_on_one(obj, input, output)
output = obj.get_dpm().run_on_one(input, output);
end
% for overloading
function outputs = process_outputs(obj, outputs)
1;
end
function pop = manage_po(obj, pop)
if (~msf_isfield(pop, 'id')), error('id field missing'); end
end
% compute input to this node from previous output
function input = run_po2i(obj, pop, varargin)
input = obj.po2i(pop);
% transfer id, output path, base path, if they exist
f = {'id', 'op', 'bp'};
for c = 1:numel(f)
if (isfield(pop, f{c}) && ~isfield(input, f{c}))
input.(f{c}) = pop.(f{c});
end
end
end
% compile output
function output = run_i2o(obj, input)
% check quality of input
f = {'id', 'bp'}; % op not needed at all times
for c = 1:numel(f)
if (~isfield(input, f{c}))
% xxx: better solution needed
obj.log(0, 'Mandatory input field missing (%s)', f{c});
error('Mandatory input field missing (%s)', f{c});
end
end
output = obj.i2o(input);
% check quality of input
f = {'id', 'op', 'bp'};
if (obj.do_i2o_pass) % pass all inputs to outputs
f = cat(2, fieldnames(input));
end
for c = 1:numel(f)
if (isfield(input, f{c}) && ~isfield(output, f{c}))
output.(f{c}) = input.(f{c});
end
end
end
end
methods (Static)
function opt = default_opt(opt)
opt = msf_ensure_field(opt, 'verbose', 0);
opt = msf_ensure_field(opt, 'do_try_catch', 1);
opt = msf_ensure_field(opt, 'iter_mode', 'iter');
opt = msf_ensure_field(opt, 'deep_mode', 0);
% do not write over existing data as per default
opt = msf_ensure_field(opt, 'do_overwrite', 0);
opt = msf_ensure_field(opt, 'c_level', 0);
opt.c_level = opt.c_level + 1;
opt.indent = zeros(1, 2*(opt.c_level - 1)) + ' ';
end
end
end