comparison core/magnatagatune/ClipSimGraphMD.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 % ---
2 % class ClipSimGraphMD
3 % Directed graph representing comparative clip similarities.
4 % EACH pair of vertices has just ONE directed edge connecting them
5 %
6 % each node represents a pair of clips,
7 % an edge (a,b) -> (a,c) is present if there is at least one
8 % users judging clip a more similar to clip b than to clip c
9 %
10 % The Edges thus represent the (are nearer to each othen than)
11 % expression
12 % ---
13
14 classdef ClipSimGraphMD < ClipSimGraph & handle
15
16
17 properties
18
19 % ---
20 % History of edge weights, is a nodes x nodes cell.
21 %
22 % E_hist(i,j) = m x 2 Matrix containing
23 % Pair of information for each set [votes votes_complete]
24 hE; % History of edge weights
25 end
26
27 % ---
28 % the methods
29 % ---
30 methods
31
32 % constructor of the graph
33 function G = ClipSimGraphMD(comparison, comparison_ids)
34
35 if nargin == 0
36 % ---
37 % empty constructor
38 % ---
39
40 elseif nargin == 1
41 % todo: copy graph
42
43 elseif nargin == 2
44
45
46 % ---
47 % handle automatic or manual input
48 % ---
49 for i = 1:size(comparison,1)
50
51 % get clips and votes
52 clips = comparison_ids(comparison(i,1:3));
53 votes = comparison(i,4:6);
54
55 % for each triplet position create an edge reflecting the
56 % absolute and relative voting for this position
57
58 % ---
59 % NOTE: we index 0 - 2 to ease the mod
60 % calculaton for the remaining indices
61 % ---
62 for v = 0:2
63
64 % ---
65 % has at least one user voted this clip out?
66 % If so, treat it as an outlier and determine score
67 % ---
68 if votes(v+1) > 0
69
70 a = mod(v+1, 3)+1;
71 b = mod(v+2, 3)+1;
72 c = v+1;
73
74
75 % ---
76 % every outlier vote results in two
77 % dissimilarity equations, favored by
78 % the people who voted for the outlier
79 % ---
80
81 % edge 1
82 add_edge(G, clips(a), clips(b), clips(c), votes(v+1), sum(votes));
83
84 % edge 2
85 add_edge(G, clips(b), clips(a), clips(c), votes(v+1), sum(votes));
86 end
87 end
88
89 end
90 end
91
92 % end constructor function
93 end
94
95 % adds a node stating clip a is more near %
96 % to clip b then clip c
97 function Vid = add_node(G, a, b)
98
99 Vid = add_node@ClipSimGraph(G, a, b);
100
101 G.hE{Vid,Vid} = [];
102
103 end
104
105 function remove_node(G, a, b)
106
107 if nargin == 3
108
109 V1 = G.node(a, b);
110 elseif nargin == 2
111
112 V1 = a;
113 end
114
115 if ~isempty(Vid)
116
117 % ---
118 % look for edges connected to Vid
119 % and delete their histograms
120 % ---
121 G.hE(:,Vid) = [];
122 G.hE(Vid,:) = [];
123
124 % ---
125 % TODO: Efficiently Remove the actual node from the
126 % GRAPH
127 % ---
128 G.Vclips(Vid,:) = [];
129
130 end
131 end
132
133 % ---
134 % add an edge saying sim(a,b) > sim(a,c)
135 % ---
136 function add_edge(G, a, b, c, votes, allvotes)
137
138 V1 = add_node(G, a, b);
139 V2 = add_node(G, a, c);
140
141 % ---
142 % save new weight values into histogram
143 % ---
144 if isempty(G.hE(V1, V2))
145
146 G.hE{V1,V2} = [votes allvotes];
147 else
148 G.hE{V1,V2} = [G.hE{V1,V2}; votes allvotes];
149 end
150
151 % ---
152 % update Edges
153 % ---
154 G.update_E(a, b, c);
155
156 end
157
158 function remove_edge(G, a, b, c)
159
160 if nargin == 4
161
162 V1 = G.node(a, b);
163 V2 = G.node(a, c);
164 elseif nargin == 3
165
166 V1 = a;
167 V2 = b;
168 end
169
170 if ~isempty(V1) && ~isempty(V2)
171
172 % set Edge to zero
173 G.hE{V1, V2} = [];
174 else
175
176 error 'nodes not found';
177 end
178 end
179
180 % ---
181 % updates the edge weight given 3 clip ids or
182 % two nodes, based on the edges history
183 %
184 % The specified (V1,V2) and the symmetric edges' (V2,V1) weights
185 % are evaluated and the stronger edge will get
186 % the excessive weight while the loosing edge
187 % will be deleted
188 % ---
189 function update_E(G, a, b, c)
190 % update_E(G, a, b, c)
191 % update_E(G, V1, V2)
192
193 % determine the type of input parameters
194 if nargin == 4
195
196 V1 = G.node(a, b);
197 V2 = G.node(a, c);
198 elseif nargin == 3
199
200 V1 = a;
201 V2 = b;
202 end
203
204 % ---
205 % calculate weighted sum for specified edge
206 % and for symmetric edge
207 % ---
208 thisw = unbal_edge_weight(G, V1, V2);
209 symmw = unbal_edge_weight(G, V2, V1);
210
211 % ---
212 % the two //competing// weights are now balanced
213 % ---
214 w = thisw - symmw;
215
216 % ---
217 % set both edges
218 % ---
219 G.E(V1,V2) = max(w,0);
220 G.E(V2,V1) = -min(w,0);
221
222 % if w >= 0
223 % G.E(V1,V2) = w;
224 % G.E(V2,V1) = 0;
225 % elseif w < 0
226 % G.E(V1,V2) = 0;
227 % G.E(V2,V1) = -w;
228 % end
229 end
230
231 end
232
233 methods (Hidden)
234
235 % ---
236 % This is the core function of this Graph.
237 % it allows for the calculation of a single Edge,
238 % before it is balanced with its symmetrical counteredge
239 %
240 % ---
241 function thisw = unbal_edge_weight(G, V1, V2)
242 % ---
243 % trivial cases
244 % ---
245 if isempty(G.hE(V1, V2)) || isempty(G.hE{V1, V2})
246 thisw = 0;
247 return;
248 end
249
250 % ---
251 % Evaluate the single historical entries
252 % and sum up
253 % ---
254 thisw = sum(G.hE{V1, V2},1);
255
256 % ---
257 % now divide the single votes by the number of
258 % votes totally made with the option to improve
259 % this edge
260 % ---
261 thisw = thisw(1) / thisw(2);
262
263 end
264 end
265 end