[959eb01] | 1 | |
---|
| 2 | import sys |
---|
| 3 | import time |
---|
| 4 | from sas.sascalc.data_util.calcthread import CalcThread |
---|
| 5 | |
---|
| 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:]) |
---|
| 15 | |
---|
| 16 | class FitThread(CalcThread): |
---|
| 17 | """Thread performing the fit """ |
---|
| 18 | |
---|
| 19 | def __init__(self, |
---|
| 20 | fn, |
---|
| 21 | page_id, |
---|
| 22 | handler, |
---|
| 23 | batch_outputs, |
---|
| 24 | batch_inputs=None, |
---|
| 25 | pars=None, |
---|
| 26 | completefn=None, |
---|
| 27 | updatefn=None, |
---|
| 28 | yieldtime=0.03, |
---|
| 29 | worktime=0.03, |
---|
| 30 | reset_flag=False): |
---|
| 31 | CalcThread.__init__(self, |
---|
| 32 | completefn, |
---|
| 33 | updatefn, |
---|
| 34 | yieldtime, |
---|
| 35 | worktime) |
---|
| 36 | self.handler = handler |
---|
| 37 | self.fitter = fn |
---|
| 38 | self.pars = pars |
---|
| 39 | self.batch_inputs = batch_inputs |
---|
| 40 | self.batch_outputs = batch_outputs |
---|
| 41 | self.page_id = page_id |
---|
| 42 | self.starttime = time.time() |
---|
| 43 | self.updatefn = updatefn |
---|
| 44 | #Relative error desired in the sum of squares. |
---|
| 45 | self.reset_flag = reset_flag |
---|
| 46 | |
---|
| 47 | def isquit(self): |
---|
| 48 | """ |
---|
| 49 | :raise KeyboardInterrupt: when the thread is interrupted |
---|
| 50 | |
---|
| 51 | """ |
---|
| 52 | try: |
---|
| 53 | CalcThread.isquit(self) |
---|
| 54 | except KeyboardInterrupt: |
---|
| 55 | msg = "Fitting: terminated by the user." |
---|
| 56 | raise KeyboardInterrupt, msg |
---|
| 57 | |
---|
| 58 | def compute(self): |
---|
| 59 | """ |
---|
| 60 | Perform a fit |
---|
| 61 | """ |
---|
| 62 | msg = "" |
---|
| 63 | try: |
---|
| 64 | import copy |
---|
| 65 | list_handler = [] |
---|
| 66 | list_curr_thread = [] |
---|
| 67 | list_reset_flag = [] |
---|
| 68 | list_map_get_attr = [] |
---|
| 69 | list_fit_function = [] |
---|
| 70 | list_q = [] |
---|
| 71 | for i in range(len(self.fitter)): |
---|
| 72 | list_handler.append(self.handler) |
---|
| 73 | list_q.append(None) |
---|
| 74 | list_curr_thread.append(self) |
---|
| 75 | list_reset_flag.append(self.reset_flag) |
---|
| 76 | list_fit_function.append('fit') |
---|
| 77 | list_map_get_attr.append(map_getattr) |
---|
| 78 | #from multiprocessing import Pool |
---|
| 79 | inputs = zip(list_map_get_attr, self.fitter, list_fit_function, |
---|
| 80 | list_q, list_q, list_handler, list_curr_thread, |
---|
| 81 | list_reset_flag) |
---|
| 82 | result = map(map_apply, inputs) |
---|
| 83 | |
---|
| 84 | self.complete(result=result, |
---|
| 85 | batch_inputs=self.batch_inputs, |
---|
| 86 | batch_outputs=self.batch_outputs, |
---|
| 87 | page_id=self.page_id, |
---|
| 88 | pars=self.pars, |
---|
| 89 | elapsed=time.time() - self.starttime) |
---|
| 90 | |
---|
| 91 | except KeyboardInterrupt, msg: |
---|
| 92 | # Thread was interrupted, just proceed and re-raise. |
---|
| 93 | # Real code should not print, but this is an example... |
---|
| 94 | #print "keyboard exception" |
---|
| 95 | #Stop on exception during fitting. Todo: need to put |
---|
| 96 | #some mssg and reset progress bar. |
---|
| 97 | |
---|
| 98 | # Shouldn't this be re-raising? ConsoleUpdate doesn't act on it. |
---|
| 99 | # raise KeyboardInterrupt |
---|
| 100 | if self.handler is not None: |
---|
| 101 | self.handler.stop(msg=msg) |
---|
| 102 | except: |
---|
| 103 | import traceback |
---|
| 104 | if self.handler is not None: |
---|
| 105 | self.handler.error(msg=traceback.format_exc()) |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | |
---|