source: sasview/sansview/perspectives/fitting/gui_thread.py @ 9e8c150

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 9e8c150 was f72333f, checked in by Jae Cho <jhjcho@…>, 15 years ago

Plugged in 2D smear: traditional over-sampling method

  • Property mode set to 100644
File size: 4.8 KB
RevLine 
[41340860]1
2
3import time
[2b63df0]4import numpy
5import copy
6import math
[41340860]7import sys
8import wx
9
[e733619]10from data_util.calcthread import CalcThread
[077809c]11from sans.fit.AbstractFitEngine import FitData2D, FitData1D, SansAssembly
[f72333f]12from DataLoader.smearing_2d import Smearer2D
[41340860]13
[2b63df0]14class CalcChisqr1D(CalcThread):
[41340860]15    """
[2b63df0]16       Compute chisqr
[41340860]17    """
[077809c]18    def __init__(self, data1d, model,
[2b63df0]19                 smearer=None,
20                 qmin=None,
21                 qmax=None,
[41340860]22                 completefn = None,
23                 updatefn   = None,
24                 yieldtime  = 0.01,
25                 worktime   = 0.01
26                 ):
27        CalcThread.__init__(self,completefn,
28                 updatefn,
29                 yieldtime,
30                 worktime)
[077809c]31       
32        if model ==None or data1d ==None:
33            raise ValueError, "Need data and model to compute chisqr"
34       
35        if data1d.__class__.__name__ !="Data1D":
36            msg= str(data1d.__class__.__name__)
37            raise ValueError, "need Data1D to compute chisqr. Current class %s"%msg
[425ef040]38
39        self.fitdata= FitData1D(x=data1d.x,y=data1d.y,dx=data1d.dx,dy=data1d.dy, smearer=smearer)
[077809c]40        self.fitdata.setFitRange(qmin=qmin,qmax=qmax)
[2b63df0]41        self.model = model
[077809c]42       
[41340860]43        self.starttime = 0 
44       
[2b63df0]45    def isquit(self):
46        """
47             @raise KeyboardInterrupt: when the thread is interrupted
48        """
49        try:
50            CalcThread.isquit(self)
51        except KeyboardInterrupt:
52            raise KeyboardInterrupt   
53       
54       
[41340860]55    def compute(self):
56        """
57            Compute the data given a model function
58        """
[2b63df0]59        self.starttime = time.time()
[52611f5]60       
[2b63df0]61        output= None
62        res=[]
[077809c]63        try:
[785c8233]64            res = self.fitdata.residuals(self.model.evalDistribution)
[2b63df0]65            sum=0
66            for item in res:
67                # Check whether we need to bail out
68                self.isquit() 
69                if numpy.isfinite(item):
[077809c]70                    sum +=item*item
71            if len(res)>0:
72                output = sum/ len(res)
73           
[2b63df0]74            elapsed = time.time()-self.starttime
75            self.complete(output= output,  elapsed=elapsed)
76           
77        except KeyboardInterrupt:
78            # Thread was interrupted, just proceed and re-raise.
79            # Real code should not print, but this is an example...
[8d78399]80            raise
[bfe4644]81     
[2b63df0]82       
83class CalcChisqr2D(CalcThread):
84    """
85       Compute chisqr
86    """
87   
[077809c]88    def __init__(self,data2d, model,
[f72333f]89                 smearer,
[2b63df0]90                 qmin,
91                 qmax,
92                 completefn = None,
93                 updatefn   = None,
94                 yieldtime  = 0.01,
95                 worktime   = 0.01
96                 ):
97        CalcThread.__init__(self,completefn,
98                 updatefn,
99                 yieldtime,
100                 worktime)
[077809c]101       
102        if model ==None or data2d ==None:
103            raise ValueError, "Need data and model to compute chisqr"
104       
105        if data2d.__class__.__name__ !="Data2D":
106            msg= str(data2d.__class__.__name__)
107            raise ValueError, "need Data2D to compute chisqr. Current class %s"%msg
[f72333f]108        self.smearer = smearer
[425ef040]109        self.fitdata = FitData2D(sans_data2d=data2d ,data=data2d.data, err_data=data2d.err_data)
[077809c]110        self.fitdata.setFitRange(qmin=qmin,qmax=qmax)
111     
[2b63df0]112        self.model = model
[f72333f]113
[2b63df0]114        self.starttime = 0 
115       
116    def isquit(self):
117        """
118             @raise KeyboardInterrupt: when the thread is interrupted
119        """
120        try:
121            CalcThread.isquit(self)
122        except KeyboardInterrupt:
123            raise KeyboardInterrupt   
124       
125       
126    def compute(self):
127        """
128            Compute the data given a model function
129        """
130        self.starttime = time.time()
[f72333f]131       
[2b63df0]132        output= None
133        res=[]
134        try:
[f72333f]135            if self.smearer == None:
136                fn = self.model.evalDistribution
137            else:
138                # Set model in smearer
139                self.smearer.set_model(self.model)
140                # set fn
141                fn = self.smearer
142                # set self.smearer != None in fitdata
143                self.fitdata.set_smearer(fn)
144
145            res = self.fitdata.residuals(fn)
[2b63df0]146            sum=0
147            for item in res:
148                # Check whether we need to bail out
149                self.isquit() 
150                if numpy.isfinite(item):
[077809c]151                    sum +=item*item
152            if len(res)>0:
153                output = sum/ len(res)
154           
[2b63df0]155            elapsed = time.time()-self.starttime
156            self.complete(output= output,  elapsed=elapsed)
157           
158        except KeyboardInterrupt:
159            # Thread was interrupted, just proceed and re-raise.
160            # Real code should not print, but this is an example...
[8d78399]161            raise 
[bfe4644]162       
Note: See TracBrowser for help on using the repository browser.