Package SloppyCell :: Module Observers
[hide private]

Source Code for Module SloppyCell.Observers

 1  import os 
 2  import time 
 3   
 4  import scipy 
 5   
 6  import Utility 
 7   
15   
16 -class CostPrinter:
17 - def __init__(self, skip=1, print_params=False, print_best_params=False):
18 self.skip = skip 19 self.print_params = print_params 20 self.print_best_params = print_best_params 21 self.reset()
22
23 - def __call__(self, **args):
24 if args['event'] == 'evaluation': 25 cost = args['cost'] 26 params = args['params'] 27 28 if cost < self.lowest_cost: 29 self.lowest_cost = cost 30 self.best_params = params.copy() 31 if self.ii % self.skip == 0: 32 print 'call %i: cost: %g, best so far: %g' % (self.ii, cost, 33 self.lowest_cost) 34 os.sys.stdout.flush() 35 if self.print_params: 36 print params 37 if self.print_best_params: 38 print self.best_params 39 self.ii += 1
40
41 - def reset(self):
42 self.ii = 0 43 self.lowest_cost = scipy.inf 44 self.best_params = None
45 49
50 -class CostEmailer:
51 - def __init__(self, interval, from_addr, to_addr):
52 self.interval = interval 53 self.from_addr, self.to_addr = from_addr, to_addr 54 self.reset()
55
56 - def __call__(self, **args):
57 if args['event'] == 'evaluation': 58 cost = args['cost'] 59 params = args['params'] 60 61 if cost < self.lowest_cost: 62 self.lowest_cost = cost 63 self.best_params = params.copy() 64 65 if time.time() - self.last_sent > self.interval * 3600: 66 lines = [] 67 lines.append('Best cost so far: %f' % self.lowest_cost) 68 lines.append('Corresponding to parameters: %s' 69 % str(self.best_params)) 70 msg = os.linesep.join(lines) 71 72 Utility.send_email(self.to_addr, self.from_addr, 73 "SloppyCell job update", 74 msg) 75 self.last_sent = time.time() 76 77 self.ii += 1
78
79 - def reset(self):
80 self.ii = 0 81 self.lowest_cost = scipy.inf 82 self.best_params = None 83 self.last_sent = time.time()
84