source: sasview/src/sas/sasgui/perspectives/fitting/fit_thread.py @ 5251ec6

magnetic_scattrelease-4.2.2ticket-1009ticket-1249
Last change on this file since 5251ec6 was 5251ec6, checked in by Paul Kienzle <pkienzle@…>, 12 months ago

improved support for py37 in sasgui

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