[e9b4cc4] | 1 | |
---|
[66ff250] | 2 | import sys |
---|
[2bb37c3] | 3 | import time |
---|
[e54d2c32] | 4 | from data_util.calcthread import CalcThread |
---|
[e9b4cc4] | 5 | |
---|
[cc31608] | 6 | def map_getattr(classInstance, classFunc, *args): |
---|
| 7 | """ |
---|
| 8 | Take an instance of a class and a function name as a string. |
---|
| 9 | Execute class.function and return result |
---|
| 10 | """ |
---|
| 11 | return getattr(classInstance,classFunc)(*args) |
---|
| 12 | |
---|
| 13 | def map_apply(arguments): |
---|
| 14 | return apply(arguments[0], arguments[1:]) |
---|
[66ff250] | 15 | |
---|
[e9b4cc4] | 16 | class FitThread(CalcThread): |
---|
[ed2ea6a] | 17 | """Thread performing the fit """ |
---|
[e9b4cc4] | 18 | |
---|
[58e0c83] | 19 | def __init__(self, |
---|
[e54d2c32] | 20 | fn, |
---|
[66ff250] | 21 | page_id, |
---|
[e54d2c32] | 22 | handler, |
---|
[33dd2e5] | 23 | batch_outputs, |
---|
| 24 | batch_inputs=None, |
---|
[e54d2c32] | 25 | pars=None, |
---|
[e9b4cc4] | 26 | completefn = None, |
---|
| 27 | updatefn = None, |
---|
| 28 | yieldtime = 0.01, |
---|
[2296316] | 29 | worktime = 0.01, |
---|
[7db52f1] | 30 | ftol = None, |
---|
| 31 | reset_flag = False): |
---|
[e9b4cc4] | 32 | CalcThread.__init__(self,completefn, |
---|
| 33 | updatefn, |
---|
| 34 | yieldtime, |
---|
| 35 | worktime) |
---|
[e54d2c32] | 36 | self.handler = handler |
---|
| 37 | self.fitter = fn |
---|
[e9b4cc4] | 38 | self.pars = pars |
---|
[33dd2e5] | 39 | self.batch_inputs = batch_inputs |
---|
| 40 | self.batch_outputs = batch_outputs |
---|
[66ff250] | 41 | self.page_id = page_id |
---|
[2bb37c3] | 42 | self.starttime = time.time() |
---|
[e54d2c32] | 43 | self.updatefn = updatefn |
---|
[2296316] | 44 | #Relative error desired in the sum of squares. |
---|
| 45 | self.ftol = ftol |
---|
[7db52f1] | 46 | self.reset_flag = reset_flag |
---|
[e54d2c32] | 47 | |
---|
[e9b4cc4] | 48 | def isquit(self): |
---|
[ed2ea6a] | 49 | """ |
---|
[5062bbf] | 50 | :raise KeyboardInterrupt: when the thread is interrupted |
---|
| 51 | |
---|
[ed2ea6a] | 52 | """ |
---|
[e9b4cc4] | 53 | try: |
---|
| 54 | CalcThread.isquit(self) |
---|
| 55 | except KeyboardInterrupt: |
---|
| 56 | raise KeyboardInterrupt |
---|
[66ff250] | 57 | |
---|
[e9b4cc4] | 58 | def compute(self): |
---|
[ed2ea6a] | 59 | """ |
---|
[5062bbf] | 60 | Perform a fit |
---|
[ed2ea6a] | 61 | """ |
---|
[40953a9] | 62 | msg = "" |
---|
[cc31608] | 63 | try: |
---|
[33dd2e5] | 64 | import copy |
---|
[cc31608] | 65 | list_handler = [] |
---|
| 66 | list_curr_thread = [] |
---|
| 67 | list_ftol = [] |
---|
[7db52f1] | 68 | list_reset_flag = [] |
---|
[cc31608] | 69 | list_map_get_attr = [] |
---|
| 70 | list_fit_function = [] |
---|
| 71 | list_q = [] |
---|
| 72 | for i in range(len(self.fitter)): |
---|
[c935fac] | 73 | list_handler.append(self.handler) |
---|
[cc31608] | 74 | list_q.append(None) |
---|
[9c9a224] | 75 | list_curr_thread.append(self) |
---|
[cc31608] | 76 | list_ftol.append(self.ftol) |
---|
[7db52f1] | 77 | list_reset_flag.append(self.reset_flag) |
---|
[cc31608] | 78 | list_fit_function.append('fit') |
---|
| 79 | list_map_get_attr.append(map_getattr) |
---|
[8d57bdb] | 80 | #from multiprocessing import Pool |
---|
[67ae937] | 81 | inputs = zip(list_map_get_attr,self.fitter, list_fit_function, |
---|
[c935fac] | 82 | list_q, list_q, list_handler,list_curr_thread,list_ftol, |
---|
[7db52f1] | 83 | list_reset_flag) |
---|
[8d57bdb] | 84 | result = map(map_apply, inputs) |
---|
[2bb37c3] | 85 | |
---|
[33dd2e5] | 86 | self.complete(result=result, |
---|
| 87 | batch_inputs=self.batch_inputs, |
---|
| 88 | batch_outputs=self.batch_outputs, |
---|
[66ff250] | 89 | page_id=self.page_id, |
---|
[2bb37c3] | 90 | pars = self.pars, |
---|
| 91 | elapsed=time.time()-self.starttime) |
---|
[785c8233] | 92 | |
---|
[e54d2c32] | 93 | except KeyboardInterrupt, msg: |
---|
[e9b4cc4] | 94 | # Thread was interrupted, just proceed and re-raise. |
---|
| 95 | # Real code should not print, but this is an example... |
---|
[ad6dd4c] | 96 | #print "keyboard exception" |
---|
[67ae937] | 97 | #Stop on exception during fitting. Todo: need to put |
---|
| 98 | #some mssg and reset progress bar. |
---|
[2bb37c3] | 99 | |
---|
| 100 | if self.handler is not None: |
---|
| 101 | self.handler.error(msg=msg) |
---|
[66ff250] | 102 | except: |
---|
[f7f6886] | 103 | if self.handler is not None: |
---|
| 104 | self.handler.error(msg=str(sys.exc_value)) |
---|
[66ff250] | 105 | |
---|
| 106 | |
---|
| 107 | |
---|