[7adc2a8] | 1 | import sys |
---|
| 2 | import time |
---|
| 3 | import copy |
---|
| 4 | import traceback |
---|
| 5 | |
---|
| 6 | from sas.sascalc.data_util.calcthread import CalcThread |
---|
| 7 | |
---|
| 8 | def map_getattr(classInstance, classFunc, *args): |
---|
| 9 | """ |
---|
| 10 | Take an instance of a class and a function name as a string. |
---|
| 11 | Execute class.function and return result |
---|
| 12 | """ |
---|
| 13 | return getattr(classInstance, classFunc)(*args) |
---|
| 14 | |
---|
| 15 | def map_apply(arguments): |
---|
[b0b09b9] | 16 | return arguments[0](*arguments[1:]) |
---|
[7adc2a8] | 17 | |
---|
| 18 | class FitThread(CalcThread): |
---|
| 19 | """Thread performing the fit """ |
---|
| 20 | |
---|
| 21 | def __init__(self, |
---|
| 22 | fn, |
---|
| 23 | page_id, |
---|
| 24 | handler, |
---|
| 25 | batch_outputs, |
---|
| 26 | batch_inputs=None, |
---|
| 27 | pars=None, |
---|
| 28 | completefn=None, |
---|
| 29 | updatefn=None, |
---|
| 30 | yieldtime=0.03, |
---|
| 31 | worktime=0.03, |
---|
| 32 | reset_flag=False): |
---|
| 33 | CalcThread.__init__(self, |
---|
| 34 | completefn, |
---|
| 35 | updatefn, |
---|
| 36 | yieldtime, |
---|
| 37 | worktime) |
---|
| 38 | self.handler = handler |
---|
| 39 | self.fitter = fn |
---|
| 40 | self.pars = pars |
---|
| 41 | self.batch_inputs = batch_inputs |
---|
| 42 | self.batch_outputs = batch_outputs |
---|
| 43 | self.page_id = page_id |
---|
| 44 | self.starttime = time.time() |
---|
| 45 | self.updatefn = updatefn |
---|
| 46 | #Relative error desired in the sum of squares. |
---|
| 47 | self.reset_flag = reset_flag |
---|
| 48 | |
---|
| 49 | def isquit(self): |
---|
| 50 | """ |
---|
| 51 | :raise KeyboardInterrupt: when the thread is interrupted |
---|
| 52 | """ |
---|
| 53 | try: |
---|
| 54 | CalcThread.isquit(self) |
---|
| 55 | except KeyboardInterrupt: |
---|
| 56 | msg = "Fitting: terminated by the user." |
---|
[b0b09b9] | 57 | raise KeyboardInterrupt(msg) |
---|
[7adc2a8] | 58 | |
---|
| 59 | def compute(self): |
---|
| 60 | """ |
---|
| 61 | Perform a fit |
---|
| 62 | """ |
---|
| 63 | msg = "" |
---|
| 64 | try: |
---|
| 65 | fitter_size = len(self.fitter) |
---|
| 66 | list_handler = [self.handler]*fitter_size |
---|
| 67 | list_curr_thread = [self]*fitter_size |
---|
| 68 | list_reset_flag = [self.reset_flag]*fitter_size |
---|
| 69 | list_map_get_attr = [map_getattr]*fitter_size |
---|
| 70 | list_fit_function = ['fit']*fitter_size |
---|
| 71 | list_q = [None]*fitter_size |
---|
[ee18d33] | 72 | |
---|
[b0b09b9] | 73 | inputs = list(zip(list_map_get_attr, self.fitter, list_fit_function, |
---|
[7adc2a8] | 74 | list_q, list_q, list_handler, list_curr_thread, |
---|
[b0b09b9] | 75 | list_reset_flag)) |
---|
| 76 | result = list(map(map_apply, inputs)) |
---|
[ee18d33] | 77 | results = (result, time.time()-self.starttime) |
---|
[7adc2a8] | 78 | if self.handler: |
---|
| 79 | self.completefn(results) |
---|
| 80 | else: |
---|
| 81 | return (results) |
---|
| 82 | |
---|
[b0b09b9] | 83 | except KeyboardInterrupt as msg: |
---|
[7adc2a8] | 84 | # Thread was interrupted, just proceed and re-raise. |
---|
| 85 | # Real code should not print, but this is an example... |
---|
| 86 | #print "keyboard exception" |
---|
| 87 | #Stop on exception during fitting. Todo: need to put |
---|
| 88 | #some mssg and reset progress bar. |
---|
| 89 | |
---|
| 90 | # Shouldn't this be re-raising? ConsoleUpdate doesn't act on it. |
---|
| 91 | # raise KeyboardInterrupt |
---|
| 92 | if self.handler is not None: |
---|
| 93 | self.handler.stop(msg=msg) |
---|
| 94 | except Exception as ex: |
---|
| 95 | # print "ERROR IN FIT THREAD: ", traceback.format_exc() |
---|
| 96 | if self.handler is not None: |
---|
| 97 | self.handler.error(msg=traceback.format_exc()) |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | |
---|
| 101 | |
---|