Changeset be6e99a in sasview for calculator/resolution_calculator.py


Ignore:
Timestamp:
Apr 8, 2011 1:19:10 PM (13 years ago)
Author:
Jae Cho <jhjcho@…>
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, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
ef73618
Parents:
cdb3a9a
Message:

fixed problems in the estimator

File:
1 edited

Legend:

Unmodified
Added
Removed
  • calculator/resolution_calculator.py

    r19637b1 rbe6e99a  
    1111from math import pi 
    1212from math import sqrt 
    13 from math import cos 
    14 from math import sin 
    15 from math import atan 
    16 from math import atan2 
    17 from math import pow 
    18 from math import asin 
    19 from math import tan 
    20  
     13import math  
     14import scipy 
    2115import numpy 
    2216 
     
    4337        self.image = [] 
    4438        # resolutions 
     39        # lamda in r-direction 
     40        self.sigma_lamda = 0 
     41        # x-dir (no lamda) 
    4542        self.sigma_1 = 0 
     43        #y-dir (no lamda) 
    4644        self.sigma_2 = 0 
     45        # 1D total 
    4746        self.sigma_1d = 0 
    4847        # q min and max 
     
    7473         
    7574    def compute_and_plot(self, qx_value, qy_value, qx_min, qx_max,  
    76                           qy_min, qy_max, coord = 'polar'): 
     75                          qy_min, qy_max, coord = 'cartesian'): 
    7776        """ 
    7877        Compute the resolution 
     
    8180        """ 
    8281        # compute 2d resolution 
    83         _, _, sigma_1, sigma_2 = self.compute(qx_value, qy_value, coord) 
     82        _, _, sigma_1, sigma_2, sigma_r = \ 
     83                            self.compute(qx_value, qy_value, coord) 
    8484        # make image 
    85         image = self.get_image(qx_value, qy_value, sigma_1, sigma_2,  
     85        image = self.get_image(qx_value, qy_value, sigma_1, sigma_2, sigma_r,  
    8686                               qx_min, qx_max, qy_min, qy_max, coord) 
    8787        # plot image 
    8888        return self.plot_image(image)  
    8989 
    90     def compute(self, qx_value, qy_value, coord = 'polar'): 
     90    def compute(self, qx_value, qy_value, coord = 'cartesian'): 
    9191        """ 
    9292        Compute the Q resoltuion in || and + direction of 2D 
     
    9494        : qy_value: y component of q 
    9595        """ 
     96        coord = 'cartesian' 
    9697        # make sure to update all the variables need. 
    9798        self.get_all_instrument_params() 
     
    113114            theta = pi/2 
    114115        else: 
    115             theta = asin(qr_value/knot) 
     116            theta = math.asin(qr_value/knot) 
    116117        # source aperture size 
    117118        rone = self.source_aperture_size 
     
    135136        lp_cor = (l_ssa * l_two) / (l_one + l_two) 
    136137        # the radial distance to the pixel from the center of the detector 
    137         radius = tan(theta)*l_two 
     138        radius = math.tan(theta)*l_two 
    138139        #Lp = l_one*l_two/(l_one+l_two) 
    139140        # default polar coordinate 
     
    158159        # reserve for 1d calculation 
    159160        sigma_wave_1 = self.get_variance_wave(radius, l_two, lamb_spread,  
    160                                           phi, comp1, 'on') 
     161                                          phi, 'radial', 'on') 
    161162        # for 1d 
    162163        variance_1d_1 = sigma_1/2 +sigma_wave_1 
     
    165166         
    166167        # for 2d 
    167         sigma_1 += sigma_wave_1 
     168        #sigma_1 += sigma_wave_1 
    168169        # normalize 
    169170        sigma_1 = knot*sqrt(sigma_1/12) 
    170  
     171        sigma_r = knot*sqrt(sigma_wave_1/12) 
    171172        # sigma in the phi/y direction 
    172173        # for source apperture 
    173174        sigma_2  = self.get_variance(rone, l1_cor, phi, comp2) 
     175 
    174176        # for sample apperture 
    175177        sigma_2 += self.get_variance(rtwo, lp_cor, phi, comp2) 
     178 
    176179        # for detector pix 
    177180        sigma_2 += self.get_variance(rthree, l_two, phi, comp2) 
     181 
    178182        # for gravity term 
    179183        sigma_2 +=  self.get_variance_gravity(l_ssa, l_sad, lamb, lamb_spread,  
    180184                             phi, comp2, 'on') 
     185 
     186         
    181187        # for wavelength spread 
    182188        # reserve for 1d calculation 
    183189        sigma_wave_2 = self.get_variance_wave(radius, l_two, lamb_spread,  
    184                                           phi, comp2, 'on')  
     190                                          phi, 'phi', 'on')  
    185191        # for 1d 
    186192        variance_1d_2 = sigma_2/2 +sigma_wave_2 
     
    189195         
    190196        # for 2d 
    191         sigma_2 += sigma_wave_2 
     197        #sigma_2 =  knot*sqrt(sigma_2/12) 
     198        #sigma_2 += sigma_wave_2 
    192199        # normalize 
    193200        sigma_2 =  knot*sqrt(sigma_2/12) 
     
    195202        # set sigmas 
    196203        self.sigma_1 = sigma_1 
     204        self.sigma_lamd = sigma_r 
    197205        self.sigma_2 = sigma_2 
    198206         
    199207        self.sigma_1d = sqrt(variance_1d_1 + variance_1d_2) 
    200         return qr_value, phi, sigma_1, sigma_2 
    201      
    202     def get_image(self, qx_value, qy_value, sigma_1, sigma_2, 
    203                   qx_min, qx_max, qy_min, qy_max, coord = 'polar'):  
     208        return qr_value, phi, sigma_1, sigma_2, sigma_r 
     209     
     210    def get_image(self, qx_value, qy_value, sigma_1, sigma_2, sigma_r, 
     211                  qx_min, qx_max, qy_min, qy_max, coord = 'cartesian'):  
    204212        """ 
    205213        Get the resolution in polar coordinate ready to plot 
     
    217225        #qy_min = self.qy_min 
    218226        #qy_max = self.qy_max 
    219  
    220         # Find polar values 
    221227        qr_value, phi = self._get_polar_value(qx_value, qy_value) 
    222          
     228 
    223229        # Check whether the q value is within the detector range 
    224230        msg = "Invalid input: Q value out of the detector range..." 
     
    242248        y_val = numpy.arange(self.qy_max, self.qy_min, -dy_size) 
    243249        q_1, q_2 = numpy.meshgrid(x_val, y_val) 
    244  
     250        #q_phi = numpy.arctan(q_1,q_2) 
    245251        # check whether polar or cartesian 
    246252        if coord == 'polar': 
     253            # Find polar values 
     254            qr_value, phi = self._get_polar_value(qx_value, qy_value) 
    247255            q_1, q_2 = self._rotate_z(q_1, q_2, phi) 
    248256            qc_1 = qr_value 
    249257            qc_2 = 0.0 
     258            # Calculate the 2D Gaussian distribution image 
     259            image = self._gaussian2d_polar(q_1, q_2, qc_1, qc_2,  
     260                                 sigma_1, sigma_2, sigma_r) 
    250261        else: 
    251262            # catesian coordinate 
     
    255266            qc_2 = qy_value 
    256267             
    257         # Calculate the 2D Gaussian distribution image 
    258         image = self._gaussian2d(q_1, q_2, qc_1, qc_2, sigma_1, sigma_2) 
     268            # Calculate the 2D Gaussian distribution image 
     269            image = self._gaussian2d(q_1, q_2, qc_1, qc_2,  
     270                                     sigma_1, sigma_2, sigma_r) 
    259271        # Add it if there are more than one inputs. 
    260272        if len(self.image) > 0: 
     
    309321        # define sigma component direction 
    310322        if comp == 'radial': 
    311             phi_x = cos(phi) 
    312             phi_y = sin(phi) 
     323            phi_x = math.cos(phi) 
     324            phi_y = math.sin(phi) 
    313325        elif comp == 'phi': 
    314             phi_x = sin(phi) 
    315             phi_y = cos(phi) 
     326            phi_x = math.sin(phi) 
     327            phi_y = math.cos(phi) 
    316328        elif comp == 'x': 
    317329            phi_x = 1 
     
    362374        else: 
    363375            # calculate sigma^2 
    364             sigma = 2 * pow(radius/distance*spread, 2) 
     376            sigma = 2 * math.pow(radius/distance*spread, 2) 
    365377            if comp == 'x': 
    366                 sigma *= (cos(phi)*cos(phi)) 
     378                sigma *= (math.cos(phi)*math.cos(phi)) 
    367379            elif comp == 'y': 
    368                 sigma *= (sin(phi)*sin(phi)) 
     380                sigma *= (math.sin(phi)*math.sin(phi)) 
    369381            else: 
    370382                sigma *= 1           
     
    403415            # A value 
    404416            a_value = d_distance * (s_distance + d_distance) 
    405             a_value *= pow(m_over_h / 2, 2) 
     417            a_value *= math.pow(m_over_h / 2, 2) 
    406418            a_value *= gravy 
    407419            # unit correction (1/cm to 1/A) for A and d_distance below 
     
    409421             
    410422            # calculate sigma^2 
    411             sigma = pow(a_value / d_distance, 2) 
    412             sigma *= pow(wavelength, 4) 
    413             sigma *= pow(spread, 2) 
     423            sigma = math.pow(a_value / d_distance, 2) 
     424            sigma *= math.pow(wavelength, 4) 
     425            sigma *= math.pow(spread, 2) 
    414426            sigma *= 8 
    415427             
    416428            # only for the polar coordinate 
    417             if comp == 'radial': 
    418                 sigma *= (sin(phi) * sin(phi)) 
    419             elif comp == 'phi': 
    420                 sigma *= (cos(phi) * cos(phi)) 
     429            #if comp == 'radial': 
     430            #    sigma *= (math.sin(phi) * math.sin(phi)) 
     431            #elif comp == 'phi': 
     432            #    sigma *= (math.cos(phi) * math.cos(phi)) 
    421433             
    422434            return sigma 
     
    521533        """ 
    522534        self.wave.set_mass(mass) 
     535        self.mass = mass 
    523536         
    524537    def set_sample_aperture_size(self, size): 
     
    603616        """         
    604617        # rotate by theta 
    605         x_prime = x_value * cos(theta) + y_value * sin(theta) 
    606         y_prime =  -x_value * sin(theta) + y_value * cos(theta) 
     618        x_prime = x_value * math.cos(theta) + y_value * math.sin(theta) 
     619        y_prime =  -x_value * math.sin(theta) + y_value * math.cos(theta) 
    607620     
    608621        return x_prime, y_prime 
    609622     
    610     def _gaussian2d(self, x_val, y_val, x0_val, y0_val, sigma_x, sigma_y): 
     623    def _gaussian2d(self, x_val, y_val, x0_val, y0_val,  
     624                    sigma_x, sigma_y, sigma_r): 
    611625        """ 
    612626        Calculate 2D Gaussian distribution 
     
    620634        : return: gaussian (value) 
    621635        """ 
     636        # phi values at each points (not at the center) 
     637        x_value = x_val - x0_val 
     638        y_value = y_val - y0_val 
     639        phi_i = numpy.arctan2(y_val, x_val) 
     640 
     641        sin_phi = numpy.sin(phi_i) 
     642        cos_phi = numpy.cos(phi_i) 
     643         
     644        x_p = x_value * cos_phi + y_value * sin_phi 
     645        y_p = -x_value * sin_phi + y_value * cos_phi 
     646         
     647        new_x = x_p * cos_phi / (sigma_r / sigma_x + 1) - y_p * sin_phi 
     648        new_x /= sigma_x 
     649        new_y = x_p * sin_phi / (sigma_r / sigma_y + 1) + y_p * cos_phi 
     650        new_y /= sigma_y 
     651 
     652        nu_value = -0.5 *(new_x * new_x + new_y * new_y) 
     653 
     654        gaussian = numpy.exp(nu_value) 
     655        # normalizing factor correction 
     656        gaussian /= gaussian.sum() 
     657 
     658        return gaussian 
     659 
     660    def _gaussian2d_polar(self, x_val, y_val, x0_val, y0_val,  
     661                        sigma_x, sigma_y, sigma_r): 
     662        """ 
     663        Calculate 2D Gaussian distribution for polar coodinate  
     664        : x_val: x value 
     665        : y_val: y value 
     666        : x0_val: mean value in x-axis 
     667        : y0_val: mean value in y-axis 
     668        : sigma_x: variance in r-direction 
     669        : sigma_y: variance in phi-direction 
     670        : sigma_r: wavelength variance in r-direction 
     671         
     672        : return: gaussian (value) 
     673        """ 
     674        sigma_x = sqrt(sigma_x * sigma_x + sigma_r * sigma_r) 
    622675        # call gaussian1d  
    623676        gaussian  = self._gaussian1d(x_val, x0_val, sigma_x) 
     
    628681            gaussian *= sqrt(2 * pi) 
    629682        return gaussian 
    630  
     683     
    631684    def _gaussian1d(self, value, mean, sigma): 
    632685        """ 
     
    660713        : return phi: the azimuthal angle of q on x-y plane 
    661714        """ 
     715        phi = math.atan2(qy_value, qx_value) 
     716        return phi 
    662717        # default 
    663718        phi = 0 
     
    674729        else: 
    675730            # the angle 
    676             phi = atan2(qy_value, qx_value) 
     731            phi = math.atan2(qy_value, qx_value) 
    677732 
    678733        return phi 
     
    811866        plane_dist = dx_size 
    812867        # full scattering angle on the x-axis 
    813         theta  = atan(plane_dist / det_dist) 
    814         qx_value     = (2.0 * pi / wavelength) * sin(theta) 
     868        theta  = math.atan(plane_dist / det_dist) 
     869        qx_value     = (2.0 * pi / wavelength) * math.sin(theta) 
    815870        return qx_value   
    816871     
Note: See TracChangeset for help on using the changeset viewer.