-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdp_node.m
executable file
·189 lines (130 loc) · 5.26 KB
/
dp_node.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
classdef dp_node < dp_node_base
% intention of this node is to implement methods dealing with file io
methods
function obj = dp_node()
obj.dpm_list = {...
dpm_iter(obj), ...
dpm_report(obj), ...
dpm_execute(obj), ...
dpm_debug(obj), ...
dpm_mgui(obj), ...
dpm_visualize(obj)};
end
function output = run_clean(obj, output)
% xxx: move this, make it private, and then
% have it run function handles instead
% clean up temporary directory if asked to do so
if (~isstruct(output)), return; end
if (~isfield(output, 'tmp')), return; end
output.tmp = msf_ensure_field(output.tmp, 'do_delete', 0);
if (output.tmp.do_delete)
msf_delete(output.tmp.bp);
end
end
function [status, f, age] = input_exist(obj, input)
[status, f, age] = obj.io_exist2(input, obj.input_test);
end
function [status, f, age] = output_exist(obj, output)
[status, f, age] = obj.io_exist2(output, obj.output_test);
end
function output = visualize(obj, input, output)
for c = 1:numel(vis.field_names) % expect a cell array
field_name = vis.field_names{c};
% Determine output name
msf_mkdir(vis.bp);
name = output.id;
name = strrep(name, '/', '_');
name = strrep(name, '\', '_');
name = strcat(name);
output.img_fns{c} = fullfile(vis.bp, ...
obj.node.name, field_name, [name '.png']);
% Find nii filename
nii_fn = output.(field_name);
if (~exist(nii_fn, 'file'))
obj.node.log('%s: %s not found (%s)', outout.id, nii_fn, field_name);
continue;
end
[I,h] = mdm_nii_read(nii_fn);
I = mgui_misc_flip_volume(I, mdm_nii_oricode(h), 'LAS');
nk = min(25, size(I,3));
kmod = round(size(I,3) / nk);
k = max(1, round( (size(I,3) - nk * kmod) / 2));
ni = 1 + floor(sqrt(nk));
nj = ceil(nk / ni);
B = [];
for i = 1:ni
A = [];
for j = 1:nj
if (k > size(I,3))
A = cat(1, A, zeros(size(I, [1 2])));
else
A = cat(1, A, I(:,:,k, 1));
end
k = k + kmod;
end
B = cat(2, B, A);
end
msf_clf;
msf_imagesc(B);
[~,name] = msf_fileparts(nii_fn);
title(strrep(name, '_', ' '));
colormap gray;
clim([0 quantile(B(:), 0.99)]);
pause(0.1);
msf_mkdir(fileparts(output.img_fns{c}));
print(output.img_fns{c}, '-dpng');
obj.node.log('%s: %s done', output.id, field_name);
end
end
end
methods (Hidden, Static)
% run a function on the input/output structures
% these should be moved elsewhere
% sorry for the poor naming
function [status, f, age] = io_exist2(io, test_fields)
if (~isempty(test_fields)) % test only the fields asked for
for c = 1:numel(test_fields)
if (~isfield(io, test_fields{c}))
disp(fieldnames(io)); % error will be thrown
end
tmp.(test_fields{c}) = io.(test_fields{c});
end
do_pass_empty = 0;
else
tmp = io;
do_pass_empty = 1;
end
[status, f, age] = dp_node.io_exist(tmp, do_pass_empty);
end
function [status, f, age] = io_exist(io, do_pass_empty)
if (nargin < 2)
do_pass_empty = 1;
end
% select fields with names ending with _fn
f = fieldnames(io);
f = f(cellfun(@(x) ~isempty(strfind(x(max(1,(end-2)):end), '_fn')), f));
% Report the presence of the files
status = zeros(size(f));
age = zeros(size(f));
for c = 1:numel(f)
status(c) = exist(io.(f{c}), 'file') == 2;
if (status(c))
d = dir(io.(f{c}));
age(c) = d.datenum;
else
age(c) = NaN;
end
% allow empties to pass
if (isempty(io.(f{c}))) && (do_pass_empty)
status(c) = 1;
end
end
status2 = cellfun( @(x) exist(io.(x), 'file') == 2, f);
end
function str = join_cell_str(f)
g = @(x) x(1:(end-3));
str = g(cell2mat(cellfun(@(x) cat(2, x, ' / '), f, ...
'UniformOutput', false)));
end
end
end