source: sasview/src/sas/perspectives/fitting/fit_thread.py @ 98f6916

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 98f6916 was 79492222, checked in by krzywon, 10 years ago

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

  • Property mode set to 100644
File size: 3.7 KB
RevLine 
[e9b4cc4]1
[66ff250]2import sys
[2bb37c3]3import time
[79492222]4from sas.data_util.calcthread import CalcThread
[e9b4cc4]5
[cc31608]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:])
[66ff250]15
[e9b4cc4]16class FitThread(CalcThread):
[ed2ea6a]17    """Thread performing the fit """
[e9b4cc4]18   
[58e0c83]19    def __init__(self, 
[e3efa6b3]20                 fn,
21                 page_id,
22                 handler,
23                 batch_outputs,
24                 batch_inputs=None,
25                 pars=None,
[e9b4cc4]26                 completefn = None,
27                 updatefn   = None,
[940aca7]28                 yieldtime  = 0.03,
29                 worktime   = 0.03,
[7db52f1]30                 ftol       = None,
31                 reset_flag = False):
[e3efa6b3]32        CalcThread.__init__(self,
33                 completefn,
[e9b4cc4]34                 updatefn,
35                 yieldtime,
36                 worktime)
[e54d2c32]37        self.handler = handler
38        self.fitter = fn
[e9b4cc4]39        self.pars = pars
[33dd2e5]40        self.batch_inputs = batch_inputs
41        self.batch_outputs = batch_outputs
[66ff250]42        self.page_id = page_id
[2bb37c3]43        self.starttime = time.time()
[e54d2c32]44        self.updatefn = updatefn
[2296316]45        #Relative error desired in the sum of squares.
46        self.ftol = ftol
[7db52f1]47        self.reset_flag = reset_flag
[e54d2c32]48   
[e9b4cc4]49    def isquit(self):
[ed2ea6a]50        """
[5062bbf]51        :raise KeyboardInterrupt: when the thread is interrupted
52       
[ed2ea6a]53        """
[e9b4cc4]54        try:
55            CalcThread.isquit(self)
56        except KeyboardInterrupt:
[aff2913]57            msg = "Fitting: terminated by the user."
58            raise KeyboardInterrupt, msg
[66ff250]59       
[e9b4cc4]60    def compute(self):
[ed2ea6a]61        """
[5062bbf]62        Perform a fit
[ed2ea6a]63        """
[40953a9]64        msg = ""
[cc31608]65        try:
[33dd2e5]66            import copy
[cc31608]67            list_handler = []
68            list_curr_thread = [] 
69            list_ftol = []
[7db52f1]70            list_reset_flag = []
[cc31608]71            list_map_get_attr = []
72            list_fit_function = []
73            list_q = []
74            for i in range(len(self.fitter)):
[c935fac]75                list_handler.append(self.handler)
[cc31608]76                list_q.append(None)
[9c9a224]77                list_curr_thread.append(self)
[cc31608]78                list_ftol.append(self.ftol)
[7db52f1]79                list_reset_flag.append(self.reset_flag)
[cc31608]80                list_fit_function.append('fit')
81                list_map_get_attr.append(map_getattr)
[8d57bdb]82            #from multiprocessing import Pool
[e3efa6b3]83            inputs = zip(list_map_get_attr, self.fitter, list_fit_function,
84                         list_q, list_q, list_handler,list_curr_thread,list_ftol,
[7db52f1]85                         list_reset_flag)
[8d57bdb]86            result =  map(map_apply, inputs)
[2bb37c3]87   
[33dd2e5]88            self.complete(result=result,
89                          batch_inputs=self.batch_inputs,
[e3efa6b3]90                          batch_outputs=self.batch_outputs,
[66ff250]91                          page_id=self.page_id,
[2bb37c3]92                          pars = self.pars,
93                          elapsed=time.time()-self.starttime)
[785c8233]94           
[e54d2c32]95        except KeyboardInterrupt, msg:
[e9b4cc4]96            # Thread was interrupted, just proceed and re-raise.
97            # Real code should not print, but this is an example...
[ad6dd4c]98            #print "keyboard exception"
[67ae937]99            #Stop on exception during fitting. Todo: need to put
100            #some mssg and reset progress bar.
[2bb37c3]101           
102            if self.handler is not None:
[986da97]103                self.handler.stop(msg=msg)
[66ff250]104        except:
[3d669e1]105            import traceback
[f7f6886]106            if self.handler is not None:
[3d669e1]107                self.handler.error(msg=traceback.format_exc())
[66ff250]108           
109       
[3d669e1]110   
Note: See TracBrowser for help on using the repository browser.