source: sasview/src/sas/sasgui/perspectives/fitting/fit_thread.py @ 50fcb09

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalcmagnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 50fcb09 was ba8d326, checked in by Paul Kienzle <pkienzle@…>, 7 years ago

code cleanup

  • Property mode set to 100644
File size: 3.4 KB
Line 
1
2import sys
3import time
4from sas.sascalc.data_util.calcthread import CalcThread
5
6def 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
13def map_apply(arguments):
14    return apply(arguments[0], arguments[1:])
15
16class 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, completefn, updatefn, yieldtime, worktime)
32        self.handler = handler
33        self.fitter = fn
34        self.pars = pars
35        self.batch_inputs = batch_inputs
36        self.batch_outputs = batch_outputs
37        self.page_id = page_id
38        self.starttime = time.time()
39        self.updatefn = updatefn
40        #Relative error desired in the sum of squares.
41        self.reset_flag = reset_flag
42
43    def isquit(self):
44        """
45        :raise KeyboardInterrupt: when the thread is interrupted
46
47        """
48        try:
49            CalcThread.isquit(self)
50        except KeyboardInterrupt:
51            msg = "Fitting: terminated by the user."
52            raise KeyboardInterrupt, msg
53
54    def compute(self):
55        """
56        Perform a fit
57        """
58        msg = ""
59        try:
60            import copy
61            list_handler = []
62            list_curr_thread = []
63            list_reset_flag = []
64            list_map_get_attr = []
65            list_fit_function = []
66            list_q = []
67            for i in range(len(self.fitter)):
68                list_handler.append(self.handler)
69                list_q.append(None)
70                list_curr_thread.append(self)
71                list_reset_flag.append(self.reset_flag)
72                list_fit_function.append('fit')
73                list_map_get_attr.append(map_getattr)
74            #from multiprocessing import Pool
75            inputs = zip(list_map_get_attr, self.fitter, list_fit_function,
76                         list_q, list_q, list_handler, list_curr_thread,
77                         list_reset_flag)
78            result = map(map_apply, inputs)
79
80            self.complete(result=result,
81                          batch_inputs=self.batch_inputs,
82                          batch_outputs=self.batch_outputs,
83                          page_id=self.page_id,
84                          pars=self.pars,
85                          elapsed=time.time() - self.starttime)
86
87        except KeyboardInterrupt, msg:
88            # Thread was interrupted, just proceed and re-raise.
89            # Real code should not print, but this is an example...
90            #print "keyboard exception"
91            #Stop on exception during fitting. Todo: need to put
92            #some mssg and reset progress bar.
93
94            # Shouldn't this be re-raising? ConsoleUpdate doesn't act on it.
95            # raise KeyboardInterrupt
96            if self.handler is not None:
97                self.handler.stop(msg=msg)
98        except:  # catch-all: show every exception which stops the thread
99            import traceback
100            if self.handler is not None:
101                self.handler.error(msg=traceback.format_exc())
102
103
104
Note: See TracBrowser for help on using the repository browser.