source: sasview/src/sas/perspectives/fitting/fit_thread.py @ 6e653582

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 6e653582 was 79492222, checked in by krzywon, 9 years ago

Changed the file and folder names to remove all SANS references.

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