Added license info into the .py files.
[golden_search.git] / correlation_checker.py
blobea1c76cb28715df1c1970ae7e2853539ee33d070
1 # For licensing info see the included LICENSE file
3 # Josef Moudrik, <J dot Moudrik at standard google mail ending>, 2012
5 from exc import IncompatibleUnits
7 def encode_utf8(st):
8 return unicode(st).encode('utf-8')
10 def myprint(*args):
11 print encode_utf8(' '.join(map(unicode, args)))
13 class CorrelationChecker:
14 def __init__(self, targets):
15 self.targets = targets
16 self.matchd = {}
18 def checkCandidate(self, num, verbose=False, verbose_limit=0.1):
19 for tid, t in enumerate(self.targets):
20 try:
21 diff = t.getDifference(num)
22 diffsofar = self.matchd.get(tid, None)
24 if diffsofar == None or diff < diffsofar[0]:
25 self.matchd[tid] = (diff, num)
26 if verbose and diff < verbose_limit:
27 self.printBestMatch(tid)
28 except IncompatibleUnits:
29 pass
30 except ZeroDivisionError:
31 pass
33 def printBestMatches(self):
34 tids = [ t[0] for t in sorted(self.matchd.items(), key=lambda t:t[1]) ]
35 for tid in tids:
36 self.printBestMatch(tid)
38 def printBestMatch(self, tid):
39 match = self.matchd.get(tid, None)
40 if match:
41 diff, num = match
42 t = self.targets[tid]
44 print
45 print "------ %.3f%%; "%(100*(1-diff),), diff
46 myprint(t, "=", num)
47 myprint(t.headStr())
48 myprint(num.headStr())