[e9b4cc4] | 1 | |
---|
[66ff250] | 2 | import sys |
---|
[2bb37c3] | 3 | import time |
---|
[79492222] | 4 | from sas.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, |
---|
[e3efa6b3] | 20 | fn, |
---|
| 21 | page_id, |
---|
| 22 | handler, |
---|
| 23 | batch_outputs, |
---|
| 24 | batch_inputs=None, |
---|
| 25 | pars=None, |
---|
[e9b4cc4] | 26 | completefn = None, |
---|
| 27 | updatefn = None, |
---|
[940aca7] | 28 | yieldtime = 0.03, |
---|
| 29 | worktime = 0.03, |
---|
[7db52f1] | 30 | ftol = None, |
---|
| 31 | reset_flag = False): |
---|
[e3efa6b3] | 32 | CalcThread.__init__(self, |
---|
| 33 | completefn, |
---|
[e9b4cc4] | 34 | updatefn, |
---|
| 35 | yieldtime, |
---|
| 36 | worktime) |
---|
[e54d2c32] | 37 | self.handler = handler |
---|
| 38 | self.fitter = fn |
---|
[e9b4cc4] | 39 | self.pars = pars |
---|
[33dd2e5] | 40 | self.batch_inputs = batch_inputs |
---|
| 41 | self.batch_outputs = batch_outputs |
---|
[66ff250] | 42 | self.page_id = page_id |
---|
[2bb37c3] | 43 | self.starttime = time.time() |
---|
[e54d2c32] | 44 | self.updatefn = updatefn |
---|
[2296316] | 45 | #Relative error desired in the sum of squares. |
---|
| 46 | self.ftol = ftol |
---|
[7db52f1] | 47 | self.reset_flag = reset_flag |
---|
[e54d2c32] | 48 | |
---|
[e9b4cc4] | 49 | def isquit(self): |
---|
[ed2ea6a] | 50 | """ |
---|
[5062bbf] | 51 | :raise KeyboardInterrupt: when the thread is interrupted |
---|
| 52 | |
---|
[ed2ea6a] | 53 | """ |
---|
[e9b4cc4] | 54 | try: |
---|
| 55 | CalcThread.isquit(self) |
---|
| 56 | except KeyboardInterrupt: |
---|
[aff2913] | 57 | msg = "Fitting: terminated by the user." |
---|
| 58 | raise KeyboardInterrupt, msg |
---|
[66ff250] | 59 | |
---|
[e9b4cc4] | 60 | def compute(self): |
---|
[ed2ea6a] | 61 | """ |
---|
[5062bbf] | 62 | Perform a fit |
---|
[ed2ea6a] | 63 | """ |
---|
[40953a9] | 64 | msg = "" |
---|
[cc31608] | 65 | try: |
---|
[33dd2e5] | 66 | import copy |
---|
[cc31608] | 67 | list_handler = [] |
---|
| 68 | list_curr_thread = [] |
---|
| 69 | list_ftol = [] |
---|
[7db52f1] | 70 | list_reset_flag = [] |
---|
[cc31608] | 71 | list_map_get_attr = [] |
---|
| 72 | list_fit_function = [] |
---|
| 73 | list_q = [] |
---|
| 74 | for i in range(len(self.fitter)): |
---|
[c935fac] | 75 | list_handler.append(self.handler) |
---|
[cc31608] | 76 | list_q.append(None) |
---|
[9c9a224] | 77 | list_curr_thread.append(self) |
---|
[cc31608] | 78 | list_ftol.append(self.ftol) |
---|
[7db52f1] | 79 | list_reset_flag.append(self.reset_flag) |
---|
[cc31608] | 80 | list_fit_function.append('fit') |
---|
| 81 | list_map_get_attr.append(map_getattr) |
---|
[8d57bdb] | 82 | #from multiprocessing import Pool |
---|
[e3efa6b3] | 83 | inputs = zip(list_map_get_attr, self.fitter, list_fit_function, |
---|
| 84 | list_q, list_q, list_handler,list_curr_thread,list_ftol, |
---|
[7db52f1] | 85 | list_reset_flag) |
---|
[8d57bdb] | 86 | result = map(map_apply, inputs) |
---|
[2bb37c3] | 87 | |
---|
[33dd2e5] | 88 | self.complete(result=result, |
---|
| 89 | batch_inputs=self.batch_inputs, |
---|
[e3efa6b3] | 90 | batch_outputs=self.batch_outputs, |
---|
[66ff250] | 91 | page_id=self.page_id, |
---|
[2bb37c3] | 92 | pars = self.pars, |
---|
| 93 | elapsed=time.time()-self.starttime) |
---|
[785c8233] | 94 | |
---|
[e54d2c32] | 95 | except KeyboardInterrupt, msg: |
---|
[e9b4cc4] | 96 | # Thread was interrupted, just proceed and re-raise. |
---|
| 97 | # Real code should not print, but this is an example... |
---|
[ad6dd4c] | 98 | #print "keyboard exception" |
---|
[67ae937] | 99 | #Stop on exception during fitting. Todo: need to put |
---|
| 100 | #some mssg and reset progress bar. |
---|
[2bb37c3] | 101 | |
---|
| 102 | if self.handler is not None: |
---|
[986da97] | 103 | self.handler.stop(msg=msg) |
---|
[66ff250] | 104 | except: |
---|
[3d669e1] | 105 | import traceback |
---|
[f7f6886] | 106 | if self.handler is not None: |
---|
[3d669e1] | 107 | self.handler.error(msg=traceback.format_exc()) |
---|
[66ff250] | 108 | |
---|
| 109 | |
---|
[3d669e1] | 110 | |
---|