Changeset a2db1ab in sasview


Ignore:
Timestamp:
Jul 11, 2016 5:30:51 AM (8 years ago)
Author:
lewis
Branches:
master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
fb7fcec
Parents:
02a8779
Message:

Compute transform on a separate thread

Location:
src/sas
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sascalc/corfunc/corfunc_calculator.py

    ra684c64 ra2db1ab  
    1010from numpy.linalg import lstsq 
    1111from sas.sascalc.dataloader.data_info import Data1D 
     12from sas.sascalc.corfunc.transform_thread import TransformThread 
    1213 
    1314class CorfuncCalculator(object): 
     
    6667        self.upperq = upperq 
    6768        self.background = 0 
     69        self._transform_thread = None 
    6870 
    6971    def set_data(self, data, scale=1): 
     
    120122        return extrapolation 
    121123 
    122     def compute_transform(self, extrapolation, background=None): 
     124    def compute_transform(self, extrapolation, background=None, 
     125        completefn=None, updatefn=None): 
    123126        """ 
    124127        Transform an extrapolated scattering curve into a correlation function. 
     
    127130        :param background: The background value (if not provided, previously 
    128131            calculated value will be used) 
     132        :param completefn: The function to call when the transform calculation 
     133            is complete` 
     134        :param updatefn: The function to call to update the GUI with the status 
     135            of the transform calculation 
    129136        :return: The transformed data 
    130137        """ 
    131         qs = extrapolation.x 
    132         iqs = extrapolation.y 
    133         q = self._data.x 
     138        if self._transform_thread is not None: 
     139            if self._transform_thread.isrunning(): return 
     140 
    134141        if background is None: background = self.background 
    135142 
    136         gamma = dct((iqs-background)*qs**2) 
    137         gamma = gamma / gamma.max() 
    138         xs = np.pi*np.arange(len(qs),dtype=np.float32)/(q[1]-q[0])/len(qs) 
    139  
    140         transform = Data1D(xs, gamma) 
    141  
    142         return transform 
     143        self._transform_thread = TransformThread(self._data, extrapolation, 
     144        background, completefn=completefn, updatefn=updatefn) 
     145        self._transform_thread.queue() 
     146 
     147    def transform_isrunning(self): 
     148        if self._transform_thread is None: return False 
     149        return self._transform_thread.isrunning() 
     150 
     151    def stop_transform(self): 
     152        if self._transform_thread.isrunning(): 
     153            self._transform_thread.stop() 
    143154 
    144155    def extract_parameters(self, transformed_data): 
  • src/sas/sasgui/perspectives/corfunc/corfunc_panel.py

    r02a8779 ra2db1ab  
    204204        Called when Transform button is pressed. 
    205205        """ 
    206         try: 
    207             transformed_data = self._calculator.compute_transform( 
    208                 self._extrapolated_data, self.background) 
    209         except: 
    210             transformed_data = None 
    211         if transformed_data is None: 
     206        if not self._calculator.transform_isrunning(): 
     207            self._calculator.compute_transform(self._extrapolated_data, 
     208                self.background, completefn=self.transform_complete, 
     209                updatefn=self.transform_update) 
     210            self._transform_btn.SetLabel("Stop Tansform") 
     211        else: 
     212            self._calculator.stop_transform() 
     213            self.transform_update("Fourier transform cancelled.") 
     214            self._transform_btn.SetLabel("Tansform") 
     215 
     216    def transform_update(self, msg=""): 
     217        """ 
     218        Called from TransformThread to update on status of calculation 
     219        """ 
     220        wx.PostEvent(self._manager.parent, 
     221            StatusEvent(status=msg)) 
     222 
     223    def transform_complete(self, transform=None): 
     224        """ 
     225        Called from TransformThread when calculation has completed 
     226        """ 
     227        if transform is None: 
    212228            msg = "Error calculating Transform." 
    213229            wx.PostEvent(self._manager.parent, 
     
    215231            self._extract_btn.Disable() 
    216232            return 
    217         self._transformed_data = transformed_data 
     233        self._transformed_data = transform 
    218234        import numpy as np 
    219         plot_x = transformed_data.x[np.where(transformed_data.x <= 200)] 
    220         plot_y = transformed_data.y[np.where(transformed_data.x <= 200)] 
     235        plot_x = transform.x[np.where(transform.x <= 200)] 
     236        plot_y = transform.y[np.where(transform.x <= 200)] 
    221237        self._manager.show_data(Data1D(plot_x, plot_y), TRANSFORM_LABEL) 
     238        self._transform_btn.SetLabel("Tansform") 
    222239        self._extract_btn.Enable() 
    223240 
Note: See TracChangeset for help on using the changeset viewer.