comparison core/magnatagatune/sim_from_comparison_fair_components.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 function [partBinTrn, partBinTst, partBinNoTrn] = sim_from_comparison_fair_components(comparison, comparison_ids, k, trainpart, filename)
2 %
3 % [partBinTrn, partBinTst, partBinNoTrn] =
4 % sim_from_comparison_fair_components(comparison, comparison_ids, k, trainpart, [filename])
5
6 % creates a cross-validation partitioning of the
7 % similarity data in "multiG", PRESERVING the
8 % connected components in it during partitioning
9
10 % ---
11 % get the similarity multigraph and remove cycles
12 % ---
13 cprint(2, 'creating graph')
14 % Gm = ClipSimGraphMulti(comparison, comparison_ids);
15 % Gm.remove_cycles_length2;
16 cprint(2, 'loading Multigraph for Similarity Constraints')
17 load('comp_SimGraphMulti.mat', 'G');
18
19 % ---
20 % Note: we get the connected components in the graph
21 % and filter out those who have only one node
22 % ---
23 cprint(2, 'extracting connected components')
24 [Gs, s, id] = connected_components(G);
25
26 valid = find(s > 1);
27 Gsv = Gs(valid);
28
29 % ---
30 % We randomise the graph triplet order,
31 % as well as the in-component
32 % constraint succession be randomised here.
33 % ---
34 datPermu = randperm(numel(Gsv));
35 Gsv = Gsv(datPermu);
36
37 conPermu = zeros(numel(Gsv),3);
38 for i = 1:numel(Gsv)
39 conPermu(i,:) = randperm(3);
40 end
41
42 % ---
43 % NOTE: we try the easy route: partition the graphs
44 % and look at which constraints balance we end up with
45 % ---
46 P = cvpartition(numel(Gsv), 'k', k);
47
48 % ---
49 % here we export the graphs similarity test sets
50 % ---
51 cprint(2, 'export test similarity')
52 partBinTst = {};
53 for i = 1:P.NumTestSets % test runs
54 partBinTst{i} = zeros(0, 3);
55
56 tmp_idx = find(P.test(i));
57 for j = 1:numel(tmp_idx); % componens
58
59 % ---
60 % get the graphs which are associated
61 % to this set and save them into a new bin.
62 % ---
63 [weights, a, b, c] = Gsv(tmp_idx(j)).similarities();
64 partBinTst{i} = [partBinTst{i}; [a' b' c' weights]];
65 end
66 end
67
68
69 % ---
70 % Note: This uses a "truly" increasing training set
71 % to do the partial training partition
72 % ---
73 cprint(2, 'export train similarity')
74 for m = 1:numel(trainpart)
75
76 Ptrain(m) = cvpartition_trunctrain_incsubsets(P, trainpart(m));
77 end
78
79 % ---
80 % here we export the graph's similarity training sets
81 % ---
82 partBinTrn = {};
83 for i = 1:P.NumTestSets % train runs
84
85 for m = 1:numel(trainpart) % increasing training sets
86 partBinTrn{i,m} = zeros(0, 3);
87
88 tmp_idx = find(Ptrain(m).training(i));
89 for j = 1:numel(tmp_idx); % components
90
91 % ---
92 % get the graphs which are associated
93 % to this set and save them into a new bin.
94 % ---
95 [weights, a, b, c] = Gsv(tmp_idx(j)).similarities();
96
97 % ---
98 % NOTE: WE apply the inner-triplet permutation,
99 % and truncate it where necessary
100 % ---
101 tmp_permu = conPermu(tmp_idx(j),:);
102 if numel(a) < 3
103 tmp_permu = tmp_permu(tmp_permu <= numel(a));
104 end
105
106 a = a(tmp_permu);
107 b = b(tmp_permu);
108 c = c(tmp_permu);
109 weights = weights(tmp_permu);
110
111 % save the clips
112 partBinTrn{i,m} = [partBinTrn{i,m}; [a' b' c' weights]];
113 end
114 end
115 end
116
117 partBinNoTrn = {};
118 for i = 1:P.NumTestSets % train runs
119
120 for m = 1:numel(trainpart) % increasing training sets
121 partBinNoTrn{i,m} = zeros(0, 3);
122
123 tmp_idx = find(~Ptrain(m).training(i) & ~Ptrain(m).test(i));
124 for j = 1:numel(tmp_idx); % components
125
126 % ---
127 % get the graphs which are associated
128 % to this set and save them into a new bin.
129 % ---
130 [weights, a, b, c] = Gsv(tmp_idx(j)).similarities();
131
132 % ---
133 % NOTE: WE apply the inner-triplet permutation,
134 % and truncate it where necessary
135 % ---
136 tmp_permu = conPermu(tmp_idx(j),:);
137 if numel(a) < 3
138 tmp_permu = tmp_permu(tmp_permu <= numel(a));
139 end
140
141 a = a(tmp_permu);
142 b = b(tmp_permu);
143 c = c(tmp_permu);
144 weights = weights(tmp_permu);
145
146 % save the clips
147 partBinNoTrn{i,m} = [partBinNoTrn{i,m}; [a' b' c' weights]];
148 end
149 end
150 end
151
152 if nargin == 5
153 save(filename, 'partBinTrn', 'partBinTst', 'partBinNoTrn')
154 end
155
156 end