source: sasview/src/sas/qtgui/Perspectives/Fitting/FitThread.py @ 722b7d6

ESS_GUIESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 722b7d6 was 28d070a, checked in by Torin Cooper-Bennun <torin.cooper-bennun@…>, 6 years ago

log an error when fitting fails

  • Property mode set to 100644
File size: 3.4 KB
Line 
1import sys
2import time
3import copy
4import traceback
5import logging
6
7from sas.sascalc.data_util.calcthread import CalcThread
8
9logger = logging.getLogger(__name__)
10
11def 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
18def map_apply(arguments):
19    return arguments[0](*arguments[1:])
20
21class 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."
60            raise KeyboardInterrupt(msg)
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
75
76            inputs = list(zip(list_map_get_attr, self.fitter, list_fit_function,
77                         list_q, list_q, list_handler, list_curr_thread,
78                         list_reset_flag))
79            result = list(map(map_apply, inputs))
80            results = (result, time.time()-self.starttime)
81            if self.handler:
82                self.completefn(results)
83            else:
84                return (results)
85
86        except KeyboardInterrupt as msg:
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:
98            logger.error("Fitting failed: %s", traceback.format_exc())
99            # print "ERROR IN FIT THREAD: ", traceback.format_exc()
100            if self.handler is not None:
101                self.handler.error(msg=str(ex))
102                self.completefn(None)
103            else:
104                return(None)
105
106
107
108
Note: See TracBrowser for help on using the repository browser.