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