Initial commit. Please see the README
[golden_search.git] / correlation_checker.py
blob81a3624b238a4d9aef4969bf494ad5af8e08066c
1 from exc import IncompatibleUnits
3 def encode_utf8(st):
4 return unicode(st).encode('utf-8')
6 def myprint(*args):
7 print encode_utf8(' '.join(map(unicode, args)))
9 class CorrelationChecker:
10 def __init__(self, targets):
11 self.targets = targets
12 self.matchd = {}
14 def checkCandidate(self, num, verbose=False, verbose_limit=0.1):
15 for tid, t in enumerate(self.targets):
16 try:
17 diff = t.getDifference(num)
18 diffsofar = self.matchd.get(tid, None)
20 if diffsofar == None or diff < diffsofar[0]:
21 self.matchd[tid] = (diff, num)
22 if verbose and diff < verbose_limit:
23 self.printBestMatch(tid)
24 except IncompatibleUnits:
25 pass
26 except ZeroDivisionError:
27 pass
29 def printBestMatches(self):
30 tids = [ t[0] for t in sorted(self.matchd.items(), key=lambda t:t[1]) ]
31 for tid in tids:
32 self.printBestMatch(tid)
34 def printBestMatch(self, tid):
35 match = self.matchd.get(tid, None)
36 if match:
37 diff, num = match
38 t = self.targets[tid]
40 print
41 print "------ %.3f%%; "%(100*(1-diff),), diff
42 myprint(t, "=", num)
43 myprint(t.headStr())
44 myprint(num.headStr())