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

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

Initial commit of the P(r) inversion perspective.
Code merged from Jeff Krzywon's ESS_GUI_Pr branch.
Also, minor 2to3 mods to sascalc/sasgui to enble error free setup.

  • Property mode set to 100755
File size: 3.4 KB
RevLine 
[959eb01]1import sys
2import time
3from sas.sascalc.data_util.calcthread import CalcThread
4
5def map_getattr(classInstance, classFunc, *args):
6    """
7    Take an instance of a class and a function name as a string.
8    Execute class.function and return result
9    """
10    return  getattr(classInstance, classFunc)(*args)
11
12def map_apply(arguments):
[fa81e94]13    return arguments[0](*arguments[1:])
[959eb01]14
15class FitThread(CalcThread):
16    """Thread performing the fit """
17
18    def __init__(self,
19                 fn,
20                 page_id,
21                 handler,
22                 batch_outputs,
23                 batch_inputs=None,
24                 pars=None,
25                 completefn=None,
26                 updatefn=None,
27                 yieldtime=0.03,
28                 worktime=0.03,
29                 reset_flag=False):
[ba8d326]30        CalcThread.__init__(self, completefn, updatefn, yieldtime, worktime)
[959eb01]31        self.handler = handler
32        self.fitter = fn
33        self.pars = pars
34        self.batch_inputs = batch_inputs
35        self.batch_outputs = batch_outputs
36        self.page_id = page_id
37        self.starttime = time.time()
38        self.updatefn = updatefn
39        #Relative error desired in the sum of squares.
40        self.reset_flag = reset_flag
41
42    def isquit(self):
43        """
44        :raise KeyboardInterrupt: when the thread is interrupted
45
46        """
47        try:
48            CalcThread.isquit(self)
49        except KeyboardInterrupt:
50            msg = "Fitting: terminated by the user."
[fa81e94]51            raise KeyboardInterrupt(msg)
[959eb01]52
53    def compute(self):
54        """
55        Perform a fit
56        """
57        msg = ""
58        try:
59            import copy
60            list_handler = []
61            list_curr_thread = []
62            list_reset_flag = []
63            list_map_get_attr = []
64            list_fit_function = []
65            list_q = []
66            for i in range(len(self.fitter)):
67                list_handler.append(self.handler)
68                list_q.append(None)
69                list_curr_thread.append(self)
70                list_reset_flag.append(self.reset_flag)
71                list_fit_function.append('fit')
72                list_map_get_attr.append(map_getattr)
73            #from multiprocessing import Pool
[fa81e94]74            inputs = list(zip(list_map_get_attr, self.fitter, list_fit_function,
[959eb01]75                         list_q, list_q, list_handler, list_curr_thread,
[fa81e94]76                         list_reset_flag))
77            result = list(map(map_apply, inputs))
[959eb01]78
79            self.complete(result=result,
80                          batch_inputs=self.batch_inputs,
81                          batch_outputs=self.batch_outputs,
82                          page_id=self.page_id,
83                          pars=self.pars,
84                          elapsed=time.time() - self.starttime)
85
[fa81e94]86        except KeyboardInterrupt as msg:
[959eb01]87            # Thread was interrupted, just proceed and re-raise.
88            # Real code should not print, but this is an example...
89            #print "keyboard exception"
[ba8d326]90            #Stop on exception during fitting. Todo: need to put
[959eb01]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)
[ba8d326]97        except:  # catch-all: show every exception which stops the thread
[959eb01]98            import traceback
99            if self.handler is not None:
100                self.handler.error(msg=traceback.format_exc())
Note: See TracBrowser for help on using the repository browser.