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