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 package net.obsearch.index;
26
27 import hep.aida.bin.StaticBin1D;
28 import org.apache.log4j.Logger;
29 import net.obsearch.asserts.OBAsserts;
30 import net.obsearch.exception.OBStorageException;
31 import net.obsearch.index.IndexDouble;
32 import net.obsearch.ob.OBDouble;
33 import net.obsearch.result.OBPriorityQueueDouble;
34 import net.obsearch.result.OBResultDouble;
35 import net.obsearch.index.utils.StatsUtil;
36 import net.obsearch.exception.PivotsUnavailableException;
37
38 import java.util.HashSet;
39 import java.util.Iterator;
40 import java.util.LinkedList;
41 import java.util.List;
42 import java.util.Set;
43
44 import static org.junit.Assert.assertEquals;
45 import static org.junit.Assert.assertFalse;
46 import static org.junit.Assert.assertTrue;
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 public abstract class TestFrameworkApproxDouble<O extends OBDouble> extends TestFrameworkDouble<O> {
76
77 private static final Logger logger = Logger.getLogger(TestFrameworkApproxDouble.class);
78
79 public TestFrameworkApproxDouble(Class<O> type, int dbSize, int querySize,
80 IndexDouble<O> index) {
81 super(type, dbSize, querySize, index);
82 }
83
84
85
86
87
88
89
90
91 public void search(IndexDouble < O > index, double range, byte k)
92 throws Exception {
93
94 index.resetStats();
95
96
97 String re = null;
98 logger.info("Matching begins...");
99
100 int i = 0;
101 long realIndex = index.databaseSize();
102 List < OBPriorityQueueDouble < O >> result = new LinkedList < OBPriorityQueueDouble < O >>();
103 while (i < this.queries.length) {
104 OBPriorityQueueDouble < O > x = new OBPriorityQueueDouble < O >(
105 k);
106 if (i % 100 == 0) {
107 logger.info("Matching " + i);
108 }
109
110 O s = queries[i];
111 index.searchOB(s, range, x);
112 result.add(x);
113 i++;
114 }
115 logger.info("Range: " + range + " k " + k + " " + index.getStats().toString());
116
117 int maxQuery = i;
118
119 Iterator < OBPriorityQueueDouble < O >> it = result.iterator();
120 StaticBin1D stats = new StaticBin1D();
121 i = 0;
122 int emptyResults = 0;
123 while (i < queries.length) {
124 if (i % 300 == 0) {
125 logger.info("Validating " + i + " of " + maxQuery);
126 }
127 O s = queries[i];
128 OBPriorityQueueDouble < O > x2 = new OBPriorityQueueDouble < O >(
129 k);
130 searchSequential( s, x2, index, range);
131 OBPriorityQueueDouble < O > x1 = it.next();
132
133
134 stats.add(ep(x1,x2,index));
135 if(x1.getSize() == 0 && x2.getSize() != 0){
136 emptyResults++;
137 }
138 i++;
139
140
141 }
142
143 logger.info("Finished EP calculation: ");
144 logger.info(StatsUtil.prettyPrintStats("EP", stats));
145
146 logger.info("Finished matching validation.");
147 assertFalse(it.hasNext());
148
149 logger.info("Zero queries: " + emptyResults);
150 }
151
152
153 private double ep(OBPriorityQueueDouble<O> x1, OBPriorityQueueDouble<O> x2, IndexDouble < O > index) throws OBStorageException{
154 List<OBResultDouble<O>> query = x1.getSortedElements();
155 List<OBResultDouble<O>> db = x2.getSortedElements();
156 int i = 0;
157 int result = 0;
158 Set<OBResultDouble<O>> s = new HashSet<OBResultDouble<O>>();
159 for(OBResultDouble<O> r : query){
160
161 int cx = 0;
162 for(OBResultDouble<O> c : db){
163 if(s.contains(c)){
164 cx++;
165 continue;
166 }
167 if(c.compareTo(r) == 0){
168 s.add(c);
169 result += cx - i;
170 break;
171 }
172 cx++;
173 }
174 i++;
175 }
176 if(query.size() == 0){
177 return 0;
178 }else{
179 double res = ((double)result)/ ((double)(query.size() * index.databaseSize()));
180 return res;
181 }
182 }
183
184 }