Ignore:
Timestamp:
Apr 27, 2012 9:22:40 AM (12 years ago)
Author:
Mathieu Doucet <doucetm@…>
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:
10bfeb3
Parents:
34f3ad0
Message:

make pylint happier

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sanscalculator/src/sans/calculator/resolution_calculator.py

    r5f3164c r8a621ac  
    11""" 
    22This object is a small tool to allow user to quickly 
    3 determine the variance in q  from the  
     3determine the variance in q  from the 
    44instrumental parameters. 
    55""" 
     
    1111from math import pi 
    1212from math import sqrt 
    13 import math  
    14 import scipy 
     13import math 
    1514import numpy 
    1615 
     
    1918#Gravitational acc. in cgs unit 
    2019_GRAVITY = 981.0 
     20 
    2121 
    2222class ResolutionCalculator(object): 
     
    8686        self.qyrange = [] 
    8787         
    88     def compute_and_plot(self, qx_value, qy_value, qx_min, qx_max,  
    89                           qy_min, qy_max, coord = 'cartesian'): 
     88    def compute_and_plot(self, qx_value, qy_value, qx_min, qx_max, 
     89                          qy_min, qy_max, coord='cartesian'): 
    9090        """ 
    9191        Compute the resolution 
     
    9898        # wavelength etc. 
    9999        lamda_list, dlamb_list = self.get_wave_list() 
    100         intens_list = []#self.get_intensity_list() 
    101  
     100        intens_list = [] 
    102101        sig1_list = [] 
    103102        sig2_list = [] 
     
    120119                        self.compute(lam, dlam, qx_value, qy_value, coord, tof) 
    121120            # make image 
    122             image = self.get_image(qx_value, qy_value, sigma_1, sigma_2,  
     121            image = self.get_image(qx_value, qy_value, sigma_1, sigma_2, 
    123122                            sigma_r, qx_min, qx_max, qy_min, qy_max, 
    124123                            coord, False) 
    125124             
    126             # Non tof mode to be speed up  
     125            # Non tof mode to be speed up 
    127126            #if num_lamda < 2: 
    128127            #    return self.plot_image(image) 
     
    140139            self.qxrange = [qx_min, qx_max] 
    141140            self.qyrange = [qy_min, qy_max] 
    142             #print qy_max+qy_min,qy_max,qy_min 
    143141            sig1_list.append(sigma_1) 
    144142            sig2_list.append(sigma_2) 
    145143            sigr_list.append(sigma_r) 
    146144            sigma1d_list.append(sigma1d) 
    147         # redraw image in global 2d q-space.    
     145        # redraw image in global 2d q-space. 
    148146        self.image_lam = [] 
    149147        total_intensity = 0 
     
    156154            dlam = dlamb_list[ind] 
    157155            intens = self.setup_tof(lam, dlam) 
    158             out = self.get_image(qx_value, qy_value, sig1_list[ind],  
    159                                    sig2_list[ind], sigr_list[ind],  
     156            out = self.get_image(qx_value, qy_value, sig1_list[ind], 
     157                                   sig2_list[ind], sigr_list[ind], 
    160158                                   qx_min, qx_max, qy_min, qy_max, coord) 
    161159            # this is the case of q being outside the detector 
     
    164162            image = out 
    165163            # set variance as sigmas 
    166             sigma_1 += sig1_list[ind] *  sig1_list[ind] * self.intensity 
     164            sigma_1 += sig1_list[ind] * sig1_list[ind] * self.intensity 
    167165            sigma_r += sigr_list[ind] * sigr_list[ind] * self.intensity 
    168166            sigma_2 += sig2_list[ind] * sig2_list[ind] * self.intensity 
     
    182180            self.sigma_2 = sqrt(sigma_2) 
    183181            self.sigma_1d = sqrt(sigma1d) 
    184             # rescale  
    185             max_im_val = 1 #image_out.max() 
     182            # rescale 
     183            max_im_val = 1 
    186184            if max_im_val > 0: 
    187185                image_out /= max_im_val 
     
    199197         
    200198        # plot image 
    201         return self.plot_image(self.image)  
     199        return self.plot_image(self.image) 
    202200     
    203201    def setup_tof(self, wavelength, wavelength_spread): 
     
    214212         
    215213        if wavelength == 0: 
    216             msg = "Can't compute the resolution: the wavelength is zero..."  
     214            msg = "Can't compute the resolution: the wavelength is zero..." 
    217215            raise RuntimeError, msg 
    218216        return self.intensity 
    219217         
    220     def compute(self, wavelength, wavelength_spread, qx_value, qy_value,  
    221                 coord = 'cartesian', tof=False): 
     218    def compute(self, wavelength, wavelength_spread, qx_value, qy_value, 
     219                coord='cartesian', tof=False): 
    222220        """ 
    223221        Compute the Q resoltuion in || and + direction of 2D 
     
    232230        if tof: 
    233231            # rectangular 
    234             tof_factor =  2 
     232            tof_factor = 2 
    235233        else: 
    236234            # triangular 
    237             tof_factor =  1 
     235            tof_factor = 1 
    238236        # Find polar values 
    239237        qr_value, phi = self._get_polar_value(qx_value, qy_value) 
     
    285283        sigma_1 += self.get_variance(rthree, l_two, phi, comp1) 
    286284        # for gravity term for 1d 
    287         sigma_1grav1d =  self.get_variance_gravity(l_ssa, l_sad, lamb, lamb_spread,  
    288                             phi, comp1, 'on') / tof_factor 
     285        sigma_1grav1d = self.get_variance_gravity(l_ssa, l_sad, lamb, 
     286                            lamb_spread, phi, comp1, 'on') / tof_factor 
    289287        # for wavelength spread 
    290288        # reserve for 1d calculation 
    291289        A_value = self._cal_A_value(lamb, l_ssa, l_sad) 
    292         sigma_wave_1, sigma_wave_1_1d = self.get_variance_wave(A_value,  
    293                                           radius, l_two, lamb_spread,  
     290        sigma_wave_1, sigma_wave_1_1d = self.get_variance_wave(A_value, 
     291                                          radius, l_two, lamb_spread, 
    294292                                          phi, 'radial', 'on') 
    295293        sigma_wave_1 /= tof_factor 
    296         sigma_wave_1_1d /=  tof_factor 
     294        sigma_wave_1_1d /= tof_factor 
    297295        # for 1d 
    298         variance_1d_1 = (sigma_1 + sigma_1grav1d) /2 + sigma_wave_1_1d 
     296        variance_1d_1 = (sigma_1 + sigma_1grav1d) / 2 + sigma_wave_1_1d 
    299297        # normalize 
    300298        variance_1d_1 = knot * knot * variance_1d_1 / 12 
     
    303301        #sigma_1 += sigma_wave_1 
    304302        # normalize 
    305         sigma_1 = knot*sqrt(sigma_1 / 12) 
    306         sigma_r = knot*sqrt(sigma_wave_1 / (tof_factor *12)) 
     303        sigma_1 = knot * sqrt(sigma_1 / 12) 
     304        sigma_r = knot * sqrt(sigma_wave_1 / (tof_factor *12)) 
    307305        # sigma in the phi/y direction 
    308306        # for source apperture 
     
    316314 
    317315        # for gravity term for 1d 
    318         sigma_2grav1d =  self.get_variance_gravity(l_ssa, l_sad, lamb, lamb_spread,  
    319                              phi, comp2, 'on') / tof_factor 
    320  
    321          
     316        sigma_2grav1d = self.get_variance_gravity(l_ssa, l_sad, lamb, 
     317                                lamb_spread, phi, comp2, 'on') / tof_factor 
     318 
    322319        # for wavelength spread 
    323320        # reserve for 1d calculation 
    324         sigma_wave_2, sigma_wave_2_1d = self.get_variance_wave(A_value,  
    325                                           radius, l_two, lamb_spread,  
    326                                           phi, 'phi', 'on')  
    327         sigma_wave_2 /=  tof_factor 
    328         sigma_wave_2_1d /=  tof_factor 
     321        sigma_wave_2, sigma_wave_2_1d = self.get_variance_wave(A_value, 
     322                                          radius, l_two, lamb_spread, 
     323                                          phi, 'phi', 'on') 
     324        sigma_wave_2 /= tof_factor 
     325        sigma_wave_2_1d /= tof_factor 
    329326        # for 1d 
    330327        variance_1d_2 = (sigma_2 + sigma_2grav1d) / 2 + sigma_wave_2_1d 
    331328        # normalize 
    332         variance_1d_2 = knot*knot*variance_1d_2 / 12 
     329        variance_1d_2 = knot * knot * variance_1d_2 / 12 
    333330         
    334331        # for 2d 
     
    336333        #sigma_2 += sigma_wave_2 
    337334        # normalize 
    338         sigma_2 =  knot * sqrt(sigma_2 / 12) 
     335        sigma_2 = knot * sqrt(sigma_2 / 12) 
    339336        sigma1d = sqrt(variance_1d_1 + variance_1d_2) 
    340337        # set sigmas 
     
    345342        return qr_value, phi, sigma_1, sigma_2, sigma_r, sigma1d 
    346343     
    347     def _within_detector_range(self,qx_value, qy_value): 
     344    def _within_detector_range(self, qx_value, qy_value): 
    348345        """ 
    349346        check if qvalues are within detector range 
     
    369366     
    370367    def get_image(self, qx_value, qy_value, sigma_1, sigma_2, sigma_r, 
    371                   qx_min, qx_max, qy_min, qy_max,  
    372                   coord = 'cartesian', full_cal=True):  
     368                  qx_min, qx_max, qy_min, qy_max, 
     369                  coord='cartesian', full_cal=True): 
    373370        """ 
    374371        Get the resolution in polar coordinate ready to plot 
     
    380377        """ 
    381378        # Get  qx_max and qy_max... 
    382         output = self._get_detector_qxqy_pixels() 
     379        self._get_detector_qxqy_pixels() 
    383380        
    384381        qr_value, phi = self._get_polar_value(qx_value, qy_value) 
    385382 
    386383        # Check whether the q value is within the detector range 
    387         msg = "Invalid input: Q value out of the detector range..." 
     384        #msg = "Invalid input: Q value out of the detector range..." 
    388385        if qx_min < self.qx_min: 
    389386            self.qx_min = qx_min 
     
    416413            qc_2 = 0.0 
    417414            # Calculate the 2D Gaussian distribution image 
    418             image = self._gaussian2d_polar(q_1, q_2, qc_1, qc_2,  
     415            image = self._gaussian2d_polar(q_1, q_2, qc_1, qc_2, 
    419416                                 sigma_1, sigma_2, sigma_r) 
    420417        else: 
     
    426423             
    427424            # Calculate the 2D Gaussian distribution image 
    428             image = self._gaussian2d(q_1, q_2, qc_1, qc_2,  
     425            image = self._gaussian2d(q_1, q_2, qc_1, qc_2, 
    429426                                     sigma_1, sigma_2, sigma_r) 
    430427        # out side of detector 
     
    445442        """ 
    446443        Plot image using pyplot 
    447         : image: 2d resolution image  
     444        : image: 2d resolution image 
    448445         
    449446        : return plt: pylab object 
     
    455452        plt.ylabel('$\\rm{Q}_{y} [A^{-1}]$') 
    456453        # Max value of the image 
    457         max = numpy.max(image) 
     454        # max = numpy.max(image) 
    458455        qx_min, qx_max, qy_min, qy_max = self.get_detector_qrange() 
    459456 
    460457        # Image 
    461         im = plt.imshow(image,  
    462                 extent = [qx_min, qx_max, qy_min, qy_max]) 
     458        im = plt.imshow(image, 
     459                extent=[qx_min, qx_max, qy_min, qy_max]) 
    463460 
    464461        # bilinear interpolation to make it smoother 
     
    473470        self.image = [] 
    474471         
    475     def get_variance(self, size = [], distance = 0, phi = 0, comp = 'radial'): 
     472    def get_variance(self, size=[], distance=0, phi=0, comp='radial'): 
    476473        """ 
    477474        Get the variance when the slit/pinhole size is given 
    478         : size: list that can be one(diameter for circular)  
     475        : size: list that can be one(diameter for circular) 
    479476                or two components(lengths for rectangular) 
    480477        : distance: [z, x] where z along the incident beam, x // qx_value 
     
    482479         
    483480        : return variance: sigma^2 
    484         """    
     481        """ 
    485482        # check the length of size (list) 
    486483        len_size = len(size) 
     
    509506        # for rectangular slit 
    510507        elif len_size == 2: 
    511             x_comp = size[0] * phi_x  
     508            x_comp = size[0] * phi_x 
    512509            y_comp = size[1] * phi_y 
    513510        # otherwise 
    514511        else: 
    515512            raise ValueError, " Improper input..." 
    516         # get them squared   
    517         sigma  = x_comp * x_comp  
     513        # get them squared 
     514        sigma  = x_comp * x_comp 
    518515        sigma += y_comp * y_comp 
    519516        # normalize by distance 
     
    522519        return sigma 
    523520 
    524     def get_variance_wave(self, A_value, radius, distance, spread, phi,  
    525                           comp = 'radial', switch = 'on'): 
     521    def get_variance_wave(self, A_value, radius, distance, spread, phi, 
     522                          comp='radial', switch='on'): 
    526523        """ 
    527524        Get the variance when the wavelength spread is given 
     
    547544                sigma1d *= (math.sin(phi)*math.sin(phi)) 
    548545            else: 
    549                 sigma1d *= 1   
    550             # sigma^2 for 2d   
     546                sigma1d *= 1 
     547            # sigma^2 for 2d 
    551548            # shift the coordinate due to the gravitational shift 
    552549            rad_x = radius * math.cos(phi) 
    553550            rad_y = A_value - radius * math.sin(phi) 
    554551            radius = math.sqrt(rad_x * rad_x + rad_y * rad_y) 
    555             # new phi  
     552            # new phi 
    556553            phi = math.atan2(-rad_y, rad_x) 
    557             self.gravity_phi = phi  
     554            self.gravity_phi = phi 
    558555            # calculate sigma^2 
    559556            sigma = 2 * math.pow(radius/distance*spread, 2) 
     
    563560                sigma *= (math.sin(phi)*math.sin(phi)) 
    564561            else: 
    565                 sigma *= 1           
     562                sigma *= 1 
    566563                 
    567564            return sigma, sigma1d 
    568565 
    569     def get_variance_gravity(self, s_distance, d_distance, wavelength, spread,  
    570                              phi, comp = 'radial', switch = 'on'): 
     566    def get_variance_gravity(self, s_distance, d_distance, wavelength, spread, 
     567                             phi, comp='radial', switch='on'): 
    571568        """ 
    572569        Get the variance from gravity when the wavelength spread is given 
     
    603600            return sigma 
    604601     
    605     def _cal_A_value(self, lamda, s_distance, d_distance):     
     602    def _cal_A_value(self, lamda, s_distance, d_distance): 
    606603        """ 
    607604        Calculate A value for gravity 
     
    617614        gravy = _GRAVITY 
    618615        # m/h 
    619         m_over_h = self.mass /h_constant 
     616        m_over_h = self.mass / h_constant 
    620617        # A value 
    621618        a_value = d_distance * (s_distance + d_distance) 
     
    641638        return self.wave.wavelength 
    642639     
     640    #TODO: why was this method duplicated? 
     641    #def get_spectrum(self): 
     642    #    """ 
     643    #    Get spectrum 
     644    #    """ 
     645    #    return self.wave.spectrum 
     646     
     647    def get_default_spectrum(self): 
     648        """ 
     649        Get default_spectrum 
     650        """ 
     651        return self.wave.get_default_spectrum() 
     652     
    643653    def get_spectrum(self): 
    644654        """ 
    645         Get spectrum 
    646         """ 
    647         return self.wave.spectrum    
    648      
    649     def get_default_spectrum(self): 
    650         """ 
    651         Get default_spectrum 
    652         """ 
    653         return self.wave.get_default_spectrum()     
    654      
    655     def get_spectrum(self): 
    656         """ 
    657655        Get _spectrum 
    658656        """ 
    659         return self.wave.get_spectrum()  
     657        return self.wave.get_spectrum() 
    660658          
    661659    def get_wavelength_spread(self): 
     
    693691        Get detector size 
    694692        """ 
    695         return self.detector.size    
     693        return self.detector.size 
    696694      
    697695    def get_source2sample_distance(self): 
     
    755753        """ 
    756754        self.spectrum = spectrum 
    757         self.wave.set_spectrum(spectrum)   
     755        self.wave.set_spectrum(spectrum) 
    758756           
    759757    def set_wavelength_spread(self, wavelength_spread): 
     
    787785         
    788786        : param size: [dia_value] or [x_value, y_value] 
    789         """         
     787        """ 
    790788        if len(size) < 1 or len(size) > 2: 
    791789            raise RuntimeError, "The length of the size must be one or two." 
     
    884882        return qx_min, qx_max, qy_min, qy_max 
    885883     
    886     def _rotate_z(self, x_value, y_value, theta= 0.0): 
     884    def _rotate_z(self, x_value, y_value, theta=0.0): 
    887885        """ 
    888886        Rotate x-y cordinate around z-axis by theta 
     
    895893        # rotate by theta 
    896894        x_prime = x_value * math.cos(theta) + y_value * math.sin(theta) 
    897         y_prime =  -x_value * math.sin(theta) + y_value * math.cos(theta) 
     895        y_prime = -x_value * math.sin(theta) + y_value * math.cos(theta) 
    898896     
    899897        return x_prime, y_prime 
    900898     
    901     def _gaussian2d(self, x_val, y_val, x0_val, y0_val,  
     899    def _gaussian2d(self, x_val, y_val, x0_val, y0_val, 
    902900                    sigma_x, sigma_y, sigma_r): 
    903901        """ 
     
    927925        y_p = -x_value * sin_phi + y_value * cos_phi 
    928926         
    929         new_sig_x = sqrt(sigma_r * sigma_r / (sigma_x * sigma_x ) + 1) 
    930         new_sig_y = sqrt(sigma_r * sigma_r / (sigma_y * sigma_y ) + 1) 
     927        new_sig_x = sqrt(sigma_r * sigma_r / (sigma_x * sigma_x) + 1) 
     928        new_sig_y = sqrt(sigma_r * sigma_r / (sigma_y * sigma_y) + 1) 
    931929        new_x = x_p * cos_phi / new_sig_x - y_p * sin_phi 
    932930        new_x /= sigma_x 
     
    934932        new_y /= sigma_y 
    935933 
    936         nu_value = -0.5 *(new_x * new_x + new_y * new_y) 
     934        nu_value = -0.5 * (new_x * new_x + new_y * new_y) 
    937935 
    938936        gaussian = numpy.exp(nu_value) 
     
    942940        return gaussian 
    943941 
    944     def _gaussian2d_polar(self, x_val, y_val, x0_val, y0_val,  
     942    def _gaussian2d_polar(self, x_val, y_val, x0_val, y0_val, 
    945943                        sigma_x, sigma_y, sigma_r): 
    946944        """ 
    947         Calculate 2D Gaussian distribution for polar coodinate  
     945        Calculate 2D Gaussian distribution for polar coodinate 
    948946        : x_val: x value 
    949947        : y_val: y value 
     
    957955        """ 
    958956        sigma_x = sqrt(sigma_x * sigma_x + sigma_r * sigma_r) 
    959         # call gaussian1d  
     957        # call gaussian1d 
    960958        gaussian  = self._gaussian1d(x_val, x0_val, sigma_x) 
    961959        gaussian *= self._gaussian1d(y_val, y0_val, sigma_y) 
     
    971969        : value: value 
    972970        : mean: mean value 
    973         : sigma: variance  
     971        : sigma: variance 
    974972         
    975973        : return: gaussian (value) 
     
    10281026        wavelength = self.wave.wavelength 
    10291027        # Gavity correction 
    1030         delta_y = self._get_beamcenter_drop() # in cm 
     1028        delta_y = self._get_beamcenter_drop()  # in cm 
    10311029         
    10321030        # detector_pix size 
     
    10421040        else: 
    10431041            raise ValueError, " Input value format error..." 
    1044         # Sample to detector distance = sample slit to detector  
     1042        # Sample to detector distance = sample slit to detector 
    10451043        # minus sample offset 
    10461044        sample2detector_distance = self.sample2detector_distance[0] - \ 
     
    10981096             
    10991097        # qx_value and qy_value values in array 
    1100         qx_value = qx_value.repeat(detector_pix_nums_y)  
     1098        qx_value = qx_value.repeat(detector_pix_nums_y) 
    11011099        qx_value = qx_value.reshape(detector_pix_nums_x, detector_pix_nums_y) 
    1102         qy_value = qy_value.repeat(detector_pix_nums_x)   
     1100        qy_value = qy_value.repeat(detector_pix_nums_x) 
    11031101        qy_value = qy_value.reshape(detector_pix_nums_y, detector_pix_nums_x) 
    11041102        qy_value = qy_value.transpose() 
     
    11101108        self.qy_max = numpy.max(qy_value) 
    11111109                 
    1112         # Appr. min and max values of the detector display limits  
     1110        # Appr. min and max values of the detector display limits 
    11131111        # i.e., edges of the last pixels. 
    1114         self.qy_min += self._get_qx(-0.5 * pix_y_size,  
     1112        self.qy_min += self._get_qx(-0.5 * pix_y_size, 
    11151113                                sample2detector_distance, wavelength) 
    1116         self.qy_max += self._get_qx(0.5 * pix_y_size,  
     1114        self.qy_max += self._get_qx(0.5 * pix_y_size, 
    11171115                                sample2detector_distance, wavelength) 
    11181116        #if self.qx_min == self.qx_max: 
    1119         self.qx_min += self._get_qx(-0.5 * pix_x_size,  
     1117        self.qx_min += self._get_qx(-0.5 * pix_x_size, 
    11201118                                sample2detector_distance, wavelength) 
    1121         self.qx_max += self._get_qx(0.5 * pix_x_size,  
     1119        self.qx_max += self._get_qx(0.5 * pix_x_size, 
    11221120                                    sample2detector_distance, wavelength) 
    11231121         
     
    11281126        self.detector_qy_max = self.qy_max 
    11291127         
    1130          
    1131  
    11321128        # try to set it as a Data2D otherwise pass (not required for now) 
    11331129        try: 
     
    11351131            output = Data2D() 
    11361132            inten = numpy.zeros_like(qx_value) 
    1137             output.data     = inten 
    1138             output.qx_data  = qx_value 
    1139             output.qy_data  = qy_value             
     1133            output.data    = inten 
     1134            output.qx_data = qx_value 
     1135            output.qy_data = qy_value 
    11401136        except: 
    11411137            pass 
    11421138         
    1143         return output#qx_value,qy_value 
     1139        return output 
    11441140         
    11451141    def _get_qx(self, dx_size, det_dist, wavelength): 
     
    11531149        plane_dist = dx_size 
    11541150        # full scattering angle on the x-axis 
    1155         theta  = numpy.arctan(plane_dist / det_dist) 
    1156         qx_value     = (2.0 * pi / wavelength) * numpy.sin(theta) 
    1157         return qx_value   
     1151        theta = numpy.arctan(plane_dist / det_dist) 
     1152        qx_value = (2.0 * pi / wavelength) * numpy.sin(theta) 
     1153        return qx_value 
    11581154     
    11591155    def _get_polar_value(self, qx_value, qy_value): 
     
    11871183        pos_y -= offset_y 
    11881184 
    1189         return pos_x, pos_y      
     1185        return pos_x, pos_y 
    11901186 
    11911187    def _get_beamcenter_drop(self): 
     
    11951191        :return delta y: the beam center drop in cm 
    11961192        """ 
    1197         # Check if mass == 0 (X-ray).  
     1193        # Check if mass == 0 (X-ray). 
    11981194        if self.mass == 0: 
    11991195            return 0 
     
    12111207        delta_y /= (velocity * velocity) 
    12121208 
    1213         return delta_y      
    1214  
    1215      
     1209        return delta_y 
Note: See TracChangeset for help on using the changeset viewer.