source: sasview/sansview/perspectives/fitting/fit_thread.py @ 4225aed

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 4225aed was cc31608, checked in by Gervaise Alina <gervyh@…>, 13 years ago

working on batch fit

  • Property mode set to 100644
File size: 3.3 KB
RevLine 
[e9b4cc4]1
[66ff250]2import sys
[e54d2c32]3from data_util.calcthread import CalcThread
[e9b4cc4]4
[cc31608]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):
13    return apply(arguments[0], arguments[1:])
[66ff250]14
[e9b4cc4]15class FitThread(CalcThread):
[ed2ea6a]16    """Thread performing the fit """
[e9b4cc4]17   
[58e0c83]18    def __init__(self, 
[e54d2c32]19                  fn,
[66ff250]20                  page_id,
[e54d2c32]21                   handler,
22                  pars=None,
[e9b4cc4]23                 completefn = None,
24                 updatefn   = None,
25                 yieldtime  = 0.01,
[2296316]26                 worktime   = 0.01,
27                 ftol       = None):
[e9b4cc4]28        CalcThread.__init__(self,completefn,
29                 updatefn,
30                 yieldtime,
31                 worktime)
[e54d2c32]32        self.handler = handler
33        self.fitter = fn
[e9b4cc4]34        self.pars = pars
[66ff250]35        self.page_id = page_id
[e9b4cc4]36        self.starttime = 0
[e54d2c32]37        self.updatefn = updatefn
[2296316]38        #Relative error desired in the sum of squares.
39        self.ftol = ftol
[e54d2c32]40   
[e9b4cc4]41    def isquit(self):
[ed2ea6a]42        """
[5062bbf]43        :raise KeyboardInterrupt: when the thread is interrupted
44       
[ed2ea6a]45        """
[e9b4cc4]46        try:
47            CalcThread.isquit(self)
48        except KeyboardInterrupt:
49            raise KeyboardInterrupt
[66ff250]50       
[e9b4cc4]51    def compute(self):
[ed2ea6a]52        """
[5062bbf]53        Perform a fit
[ed2ea6a]54        """
[40953a9]55        msg = ""
[cc31608]56        try:
57            list_handler = []
58            list_curr_thread = [] 
59            list_ftol = []
60            list_map_get_attr = []
61            list_fit_function = []
62            list_q = []
63            for i in range(len(self.fitter)):
64                list_handler.append(None)
65                list_q.append(None)
66                list_curr_thread.append(None)
67                list_ftol.append(self.ftol)
68                list_fit_function.append('fit')
69                list_map_get_attr.append(map_getattr)
70            from multiprocessing import Pool
71            inputs = zip(list_map_get_attr,self.fitter, list_fit_function, list_handler, list_q, list_curr_thread,list_ftol)
72            print inputs
73            result =  Pool(1).map(func=map_apply, 
74                               iterable=inputs)
[66ff250]75            #self.handler.starting_fit()
[ed2ea6a]76            #Result from the fit
[cc31608]77            """
78                result = self.fitter.fit(handler=self.handler,
79                                         curr_thread=self,
80                                         ftol=self.ftol)
81            """
82            print "fithread result", result
[e9b4cc4]83            self.complete(result= result,
[66ff250]84                          page_id=self.page_id,
[6bbeacd4]85                          pars = self.pars)
[785c8233]86           
[e54d2c32]87        except KeyboardInterrupt, msg:
[e9b4cc4]88            # Thread was interrupted, just proceed and re-raise.
89            # Real code should not print, but this is an example...
[ad6dd4c]90            #print "keyboard exception"
[a6a7e8a]91            #Stop on exception during fitting. Todo: need to put some mssg and reset progress bar.
[66ff250]92            raise
93            #if self.handler is not None:
94            #    self.handler.error(msg=msg)
95        except:
96            raise
97            #if self.handler is not None:
98            #    self.handler.error(msg=str(sys.exc_value))
99           
100       
101   
Note: See TracBrowser for help on using the repository browser.