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