source: sasview/src/sas/qtgui/Perspectives/Fitting/FitThread.py @ 085e3c9d

ESS_GUI_iss959
Last change on this file since 085e3c9d was 235d766, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

Simple vs. complex constraints behaviour fixed.

  • Property mode set to 100644
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 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
73            inputs = list(zip(list_map_get_attr, self.fitter, list_fit_function,
74                         list_q, list_q, list_handler, list_curr_thread,
75                         list_reset_flag))
76            result = list(map(map_apply, inputs))
77            results = (result, time.time()-self.starttime)
78            if self.handler:
79                self.completefn(results)
80            else:
81                return (results)
82
83        except KeyboardInterrupt as msg:
84            # Thread was interrupted, just proceed and re-raise.
85            # Real code should not print, but this is an example...
86            #print "keyboard exception"
87            #Stop on exception during fitting. Todo: need to put
88            #some mssg and reset progress bar.
89
90            # Shouldn't this be re-raising? ConsoleUpdate doesn't act on it.
91            # raise KeyboardInterrupt
92            if self.handler is not None:
93                self.handler.stop(msg=msg)
94        except Exception as ex:
95            # print "ERROR IN FIT THREAD: ", traceback.format_exc()
96            if self.handler is not None:
97                self.handler.error(msg=str(ex))
98                self.completefn(None)
99            else:
100                return(None)
101
102
103
104
Note: See TracBrowser for help on using the repository browser.