[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, |
---|
| 22 | pars=None, |
---|
[e9b4cc4] | 23 | completefn = None, |
---|
| 24 | updatefn = None, |
---|
| 25 | yieldtime = 0.01, |
---|
[2296316] | 26 | worktime = 0.01, |
---|
| 27 | ftol = None): |
---|
[e9b4cc4] | 28 | CalcThread.__init__(self,completefn, |
---|
| 29 | updatefn, |
---|
| 30 | yieldtime, |
---|
| 31 | worktime) |
---|
[e54d2c32] | 32 | self.handler = handler |
---|
| 33 | self.fitter = fn |
---|
[e9b4cc4] | 34 | self.pars = pars |
---|
[66ff250] | 35 | self.page_id = page_id |
---|
[e9b4cc4] | 36 | self.starttime = 0 |
---|
[e54d2c32] | 37 | self.updatefn = updatefn |
---|
[2296316] | 38 | #Relative error desired in the sum of squares. |
---|
| 39 | self.ftol = ftol |
---|
[e54d2c32] | 40 | |
---|
[e9b4cc4] | 41 | def isquit(self): |
---|
[ed2ea6a] | 42 | """ |
---|
[5062bbf] | 43 | :raise KeyboardInterrupt: when the thread is interrupted |
---|
| 44 | |
---|
[ed2ea6a] | 45 | """ |
---|
[e9b4cc4] | 46 | try: |
---|
| 47 | CalcThread.isquit(self) |
---|
| 48 | except KeyboardInterrupt: |
---|
| 49 | raise KeyboardInterrupt |
---|
[66ff250] | 50 | |
---|
[e9b4cc4] | 51 | def compute(self): |
---|
[ed2ea6a] | 52 | """ |
---|
[5062bbf] | 53 | Perform a fit |
---|
[ed2ea6a] | 54 | """ |
---|
[40953a9] | 55 | msg = "" |
---|
[cc31608] | 56 | try: |
---|
| 57 | list_handler = [] |
---|
| 58 | list_curr_thread = [] |
---|
| 59 | list_ftol = [] |
---|
| 60 | list_map_get_attr = [] |
---|
| 61 | list_fit_function = [] |
---|
| 62 | list_q = [] |
---|
| 63 | for i in range(len(self.fitter)): |
---|
| 64 | list_handler.append(None) |
---|
| 65 | list_q.append(None) |
---|
| 66 | list_curr_thread.append(None) |
---|
| 67 | list_ftol.append(self.ftol) |
---|
| 68 | list_fit_function.append('fit') |
---|
| 69 | list_map_get_attr.append(map_getattr) |
---|
| 70 | from multiprocessing import Pool |
---|
[67ae937] | 71 | inputs = zip(list_map_get_attr,self.fitter, list_fit_function, |
---|
| 72 | list_handler, list_q, list_curr_thread,list_ftol) |
---|
[cc31608] | 73 | result = Pool(1).map(func=map_apply, |
---|
| 74 | iterable=inputs) |
---|
[66ff250] | 75 | #self.handler.starting_fit() |
---|
[e9b4cc4] | 76 | self.complete(result= result, |
---|
[66ff250] | 77 | page_id=self.page_id, |
---|
[6bbeacd4] | 78 | pars = self.pars) |
---|
[785c8233] | 79 | |
---|
[e54d2c32] | 80 | except KeyboardInterrupt, msg: |
---|
[e9b4cc4] | 81 | # Thread was interrupted, just proceed and re-raise. |
---|
| 82 | # Real code should not print, but this is an example... |
---|
[ad6dd4c] | 83 | #print "keyboard exception" |
---|
[67ae937] | 84 | #Stop on exception during fitting. Todo: need to put |
---|
| 85 | #some mssg and reset progress bar. |
---|
[66ff250] | 86 | raise |
---|
| 87 | #if self.handler is not None: |
---|
| 88 | # self.handler.error(msg=msg) |
---|
| 89 | except: |
---|
| 90 | raise |
---|
| 91 | #if self.handler is not None: |
---|
| 92 | # self.handler.error(msg=str(sys.exc_value)) |
---|
| 93 | |
---|
| 94 | |
---|
| 95 | |
---|