-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtf_node.m
235 lines (202 loc) · 7.58 KB
/
tf_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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
classdef tf_node < handle & matlab.mixin.Heterogeneous
properties
parent
id
tf
num_layers
end
methods
% cumulative transfer function upon reaching this node
function t = tot_tf(obj)
if isa(obj.parent, 'tf_node')
t = obj.parent.tot_tf*obj.tf;
else
t = obj.tf;
end
end
function pars = parents(obj)
pars = [];
p = obj.parent;
while isa(p, 'tf_node')
pars = [pars p.id];
p = p.parent;
end
end
function nds = all_nodes(obj)
todo = [obj];
nds = [];
while numel(todo) > 0
curr = todo(1);
todo = todo(2:end);
nds = [nds curr];
todo = [todo curr.children];
end
end
function lvs = leaves(obj)
nds = obj.all_nodes;
inds = zeros(size(nds));
for ii = 1:numel(nds)
inds(ii) = (numel(nds(ii).children) == 0);
end
lvs = nds(inds == 1);
end
function em = emitted(obj)
lvs = obj.leaves;
inds = zeros(size(lvs));
% check if transmitting into final layer
for ii = 1:numel(lvs)
if isa(lvs(ii), 'interface_node')
if lvs(ii).into == obj.num_layers && lvs(ii).type == 1
inds(ii) = 1;
end
else
if lvs(ii).index == obj.num_layers
inds(ii) = 1;
end
end
end
em = lvs(inds == 1);
end
% returns the reflection nodes in the path to the current node
function rs = reflections(obj)
curr_node = obj;
rs = [];
while isa(curr_node, 'tf_node')
if isa(curr_node, 'interface_node')
if curr_node.type == -1
rs = [rs curr_node];
end
end
curr_node = curr_node.parent;
end
end
% returns the transmission nodes in the path to the current node
function ts = transmissions(obj)
curr_node = obj;
ts = [];
while isa(curr_node, 'tf_node')
if isa(curr_node, 'interface_node')
if curr_node.type == 1
ts = [ts curr_node];
end
end
curr_node = curr_node.parent;
end
end
% determines if the path to an emitted node includes
% cross-layer etalons
function t_or_f = crosslayer(obj)
refs = obj.reflections;
froms = arrayfun(@(x) x.from, refs);
num_trans = numel(obj.transmissions);
t_or_f = num_trans > (obj.num_layers - 1);
end
function tf_bd = tot_tf_breakdown(obj)
emit = obj.emitted;
cross_layer_inds = arrayfun(@(x) x.crosslayer, emit);
no_ref_inds = arrayfun(@(x) numel(x.reflections) == 0,...
emit);
single_layer_inds = ~cross_layer_inds & ~no_ref_inds;
tf_no_ref = sum(arrayfun(@tot_tf, emit(no_ref_inds)));
tf_one_layer = sum(arrayfun(@tot_tf, emit(single_layer_inds)));
tf_cross = sum(arrayfun(@tot_tf, emit(cross_layer_inds)));
tf_bd = [tf_no_ref, tf_one_layer, tf_cross];
end
function no_ref = tot_tf_no_ref(obj)
bd = obj.tot_tf_breakdown;
no_ref = bd(1);
end
function one_layer = tot_tf_one_layer(obj)
bd = obj.tot_tf_breakdown;
one_layer = bd(1) + bd(2);
end
function cross_layer = tot_tf_cross_layer(obj)
bd = obj.tot_tf_breakdown;
cross_layer = bd(1) + bd(2) + bd(3);
end
function t = tot_tf_all_leaves(obj)
t = sum(arrayfun(@tot_tf, obj.emitted));
end
%% functions for displaying tree
function vec = tree_vec(obj)
nds = obj.all_nodes;
vec = zeros(1, numel(nds));
for ii = 1:numel(nds)
nds(ii).id = ii;
end
for ii = 1:numel(nds)
inds = arrayfun(@(x) x.id, nds(ii).children);
vec(inds) = nds(ii).id;
end
end
function f = show(obj)
f = figure();
nds = obj.all_nodes;
vec = obj.tree_vec;
treeplot(vec)
[x, y] = treelayout(vec);
for ii = 1:numel(nds)
ind = nds(ii).id;
if isa(nds(ii), 'interface_node')
color = [1 0.6 0.6];
if nds(ii).type == -1
color = [1 0.7 0.5];
end
text(x(ind), y(ind), nds(ii).to_s,...
'backgroundcolor', color ,...
'horizontalalignment', 'center', 'color', 'w', ...
'fontweight', 'bold', 'fontsize', 8)
else
text(x(ind), y(ind), nds(ii).to_s,...
'backgroundcolor', '[0.6 0.6 1]',...
'horizontalalignment', 'center', 'color', 'w', ...
'fontweight', 'bold', 'fontsize', 8)
end
hold on
end
end
function show_tf(obj)
figure()
nds = obj.all_nodes;
vec = obj.tree_vec;
treeplot(vec)
set(allchild(gca), 'markersize', 1)
[x, y] = treelayout(vec);
hold on
for ii = 1:numel(nds)
plot(x(ii), y(ii), 'ok',...
'markersize', 30*abs(nds(ii).tot_tf),...
'markerfacecolor', [0.5 0.5 0.5])
end
end
function dot(obj)
fprintf('graph Tree {\n')
nodes = obj.all_nodes();
for ii = 1:numel(nodes)
nodes(ii).id = ii;
end
em_pars = [];
for n = obj.emitted()
em_pars = [em_pars n.id n.parents];
end
em_pars = unique(em_pars);
for ii = 1:numel(nodes)
node = nodes(ii);
% output node label
fprintf(node.dot_label(ismember(node.id, em_pars)))
%output node connections
cs = node.children();
for jj = 1:numel(cs)
% is this node part of an emitted path?
em = ismember(cs(jj).id, em_pars);
color = '';
if em
color = '[color="#000000", penwidth=4.0]';
end
fprintf('%d -- %d %s\n', node.id, cs(jj).id, color)
end
end
fprintf('}\n')
end
end
end