source: sasview/src/sas/qtgui/Perspectives/Fitting/FitThread.py @ 7adc2a8

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 7adc2a8 was 7adc2a8, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

Allow old style threads for fitting

  • Property mode set to 100755
File size: 3.2 KB
Line 
1import sys
2import time
3import copy
4import traceback
5
6from sas.sascalc.data_util.calcthread import CalcThread
7
8def 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
15def map_apply(arguments):
16    return apply(arguments[0], arguments[1:])
17
18class 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."
57            raise KeyboardInterrupt, msg
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
72            inputs = zip(list_map_get_attr, self.fitter, list_fit_function,
73                         list_q, list_q, list_handler, list_curr_thread,
74                         list_reset_flag)
75            result = map(map_apply, inputs)
76            results = (result[0], time.time()-self.starttime)
77            if self.handler:
78                self.completefn(results)
79            else:
80                return (results)
81
82        except KeyboardInterrupt, msg:
83            # Thread was interrupted, just proceed and re-raise.
84            # Real code should not print, but this is an example...
85            #print "keyboard exception"
86            #Stop on exception during fitting. Todo: need to put
87            #some mssg and reset progress bar.
88
89            # Shouldn't this be re-raising? ConsoleUpdate doesn't act on it.
90            # raise KeyboardInterrupt
91            if self.handler is not None:
92                self.handler.stop(msg=msg)
93        except Exception as ex:
94            # print "ERROR IN FIT THREAD: ", traceback.format_exc()
95            if self.handler is not None:
96                self.handler.error(msg=traceback.format_exc())
97
98
99
100
Note: See TracBrowser for help on using the repository browser.