Ignore:
Timestamp:
Apr 27, 2012 10:42:24 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:
7d6351e
Parents:
10bfeb3
Message:

Pep-8-ification

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sansdataloader/src/sans/dataloader/manipulations.py

    r47045e6 rf60a8c2  
    1  
     1""" 
     2Data manipulations for 2D data sets. 
     3Using the meta data information, various types of averaging 
     4are performed in Q-space 
     5""" 
    26##################################################################### 
    37#This software was developed by the University of Tennessee as part of the 
    48#Distributed Data Analysis of Neutron Scattering Experiments (DANSE) 
    5 #project funded by the US National Science Foundation.  
     9#project funded by the US National Science Foundation. 
    610#See the license text in license.txt 
    711#copyright 2008, University of Tennessee 
    812###################################################################### 
    913 
    10 """ 
    11 Data manipulations for 2D data sets. 
    12 Using the meta data information, various types of averaging 
    13 are performed in Q-space  
    14 """ 
    1514#TODO: copy the meta data from the 2D object to the resulting 1D object 
    1615import math 
     
    3332    theta = 0.5 * math.atan(plane_dist/det_dist) 
    3433    return (4.0 * math.pi/wavelength) * math.sin(theta) 
     34 
    3535 
    3636def get_q_compo(dx, dy, det_dist, wavelength, compo=None): 
     
    5555    return out 
    5656 
     57 
    5758def flip_phi(phi): 
    5859    """ 
     
    6364    Pi = math.pi 
    6465    if phi < 0: 
    65         phi_out = phi  + (2 * Pi) 
     66        phi_out = phi + (2 * Pi) 
    6667    elif phi > (2 * Pi): 
    67         phi_out = phi  - (2 * Pi) 
     68        phi_out = phi - (2 * Pi) 
    6869    else: 
    69         phi_out = phi  
     70        phi_out = phi 
    7071    return phi_out 
     72 
    7173 
    7274def reader2D_converter(data2d=None): 
     
    9395    qy_data = new_y.flatten() 
    9496    q_data = numpy.sqrt(qx_data*qx_data + qy_data*qy_data) 
    95     if data2d.err_data == None or numpy.any(data2d.err_data <= 0):  
     97    if data2d.err_data == None or numpy.any(data2d.err_data <= 0): 
    9698        new_err_data = numpy.sqrt(numpy.abs(new_data)) 
    9799    else: 
    98100        new_err_data = data2d.err_data.flatten() 
    99     mask    = numpy.ones(len(new_data), dtype=bool) 
    100  
     101    mask = numpy.ones(len(new_data), dtype=bool) 
     102 
     103    #TODO: make sense of the following two lines... 
    101104    output = Data2D() 
    102105    output = data2d 
     
    109112 
    110113    return output 
     114 
    111115 
    112116class _Slab(object): 
     
    149153            raise RuntimeError, msg 
    150154         
    151         # Get data  
     155        # Get data 
    152156        data = data2D.data[numpy.isfinite(data2D.data)] 
    153157        q_data = data2D.q_data[numpy.isfinite(data2D.data)] 
    154158        err_data = data2D.err_data[numpy.isfinite(data2D.data)] 
    155         qx_data = data2D.qx_data[numpy.isfinite(data2D.data)]  
     159        qx_data = data2D.qx_data[numpy.isfinite(data2D.data)] 
    156160        qy_data = data2D.qy_data[numpy.isfinite(data2D.data)] 
    157161              
     
    159163        if maj == 'x': 
    160164            if self.fold: 
    161                 x_min = 0      
    162             else:  x_min = self.x_min 
    163             nbins = int(math.ceil((self.x_max - x_min)/self.bin_width)) 
     165                x_min = 0 
     166            else: 
     167                x_min = self.x_min 
     168            nbins = int(math.ceil((self.x_max - x_min) / self.bin_width)) 
    164169            qbins = self.bin_width * numpy.arange(nbins) + x_min 
    165170        elif maj == 'y': 
    166             if self.fold: y_min = 0      
    167             else:  y_min = self.y_min 
     171            if self.fold: 
     172                y_min = 0 
     173            else: 
     174                y_min = self.y_min 
    168175            nbins = int(math.ceil((self.y_max - y_min)/self.bin_width)) 
    169             qbins = self.bin_width * numpy.arange(nbins) + y_min           
     176            qbins = self.bin_width * numpy.arange(nbins) + y_min 
    170177        else: 
    171178            raise RuntimeError, "_Slab._avg: unrecognized axis %s" % str(maj) 
    172179                                 
    173         x  = numpy.zeros(nbins) 
    174         y  = numpy.zeros(nbins) 
     180        x = numpy.zeros(nbins) 
     181        y = numpy.zeros(nbins) 
    175182        err_y = numpy.zeros(nbins) 
    176183        y_counts = numpy.zeros(nbins) 
    177184 
    178         # Average pixelsize in q space                                                 
    179         for npts in range(len(data)):  
    180             # default frac                                                
     185        # Average pixelsize in q space 
     186        for npts in range(len(data)): 
     187            # default frac 
    181188            frac_x = 0 
    182189            frac_y = 0 
     
    191198                continue 
    192199            # binning: find axis of q 
    193             if maj == 'x':  
     200            if maj == 'x': 
    194201                q_value = qx_data[npts] 
    195                 min = x_min  
    196             if maj == 'y':  
    197                 q_value = qy_data[npts]  
     202                min = x_min 
     203            if maj == 'y': 
     204                q_value = qy_data[npts] 
    198205                min = y_min  
    199206            if self.fold and q_value < 0: 
    200                 q_value = -q_value    
     207                q_value = -q_value 
    201208            # bin 
    202             i_q = int(math.ceil((q_value - min)/self.bin_width)) - 1 
     209            i_q = int(math.ceil((q_value - min) / self.bin_width)) - 1 
    203210             
    204211            # skip outside of max bins 
     
    206213                continue 
    207214             
    208             # give it full weight 
    209             #frac = 1  
    210              
    211215            #TODO: find better definition of x[i_q] based on q_data 
    212             x[i_q] += frac * q_value#min + (i_q + 1) * self.bin_width / 2.0 
     216            x[i_q] += frac * q_value  # min + (i_q + 1) * self.bin_width / 2.0 
    213217            y[i_q] += frac * data[npts] 
    214218             
    215219            if err_data == None or err_data[npts] == 0.0: 
    216                 if data[npts] < 0: data[npts] = -data[npts] 
     220                if data[npts] < 0: 
     221                    data[npts] = -data[npts] 
    217222                err_y[i_q] += frac * frac * data[npts] 
    218223            else: 
    219224                err_y[i_q] += frac * frac * err_data[npts] * err_data[npts] 
    220             y_counts[i_q]  += frac 
     225            y_counts[i_q] += frac 
    221226                 
    222         # Average the sums        
     227        # Average the sums 
    223228        for n in range(nbins): 
    224229            err_y[n] = math.sqrt(err_y[n]) 
    225230           
    226231        err_y = err_y / y_counts 
    227         y    = y / y_counts 
    228         x    = x / y_counts 
    229         idx = (numpy.isfinite(y) & numpy.isfinite(x))  
     232        y = y / y_counts 
     233        x = x / y_counts 
     234        idx = (numpy.isfinite(y) & numpy.isfinite(x)) 
    230235         
    231236        if not idx.any():  
    232             msg = "Average Error: No points inside ROI to average..."  
     237            msg = "Average Error: No points inside ROI to average..." 
    233238            raise ValueError, msg 
    234239        #elif len(y[idx])!= nbins: 
     
    237242        return Data1D(x=x[idx], y=y[idx], dy=err_y[idx]) 
    238243         
     244         
    239245class SlabY(_Slab): 
    240246    """ 
     
    251257        return self._avg(data2D, 'y') 
    252258         
     259         
    253260class SlabX(_Slab): 
    254261    """ 
     
    264271         
    265272        """ 
    266         return self._avg(data2D, 'x')  
    267          
     273        return self._avg(data2D, 'x') 
     274 
     275 
    268276class Boxsum(object): 
    269277    """ 
     
    282290    def __call__(self, data2D): 
    283291        """ 
    284         Perform the sum in the region of interest  
     292        Perform the sum in the region of interest 
    285293          
    286294        :param data2D: Data2D object 
     
    293301        # Average the sums 
    294302        counts = 0 if y_counts == 0 else y 
    295         error  = 0 if y_counts == 0 else math.sqrt(err_y) 
     303        error = 0 if y_counts == 0 else math.sqrt(err_y) 
    296304         
    297305        return counts, error 
     
    299307    def _sum(self, data2D): 
    300308        """ 
    301         Perform the sum in the region of interest  
    302          
    303         :param data2D: Data2D object 
    304          
    305         :return: number of counts,  
     309        Perform the sum in the region of interest 
     310         
     311        :param data2D: Data2D object 
     312         
     313        :return: number of counts, 
    306314            error on number of counts, number of entries summed 
    307315         
     
    311319            msg += "of detectors: %g" % len(data2D.detector) 
    312320            raise RuntimeError, msg 
    313         # Get data  
     321        # Get data 
    314322        data = data2D.data[numpy.isfinite(data2D.data)] 
    315323        q_data = data2D.q_data[numpy.isfinite(data2D.data)] 
    316324        err_data = data2D.err_data[numpy.isfinite(data2D.data)] 
    317         qx_data = data2D.qx_data[numpy.isfinite(data2D.data)]  
     325        qx_data = data2D.qx_data[numpy.isfinite(data2D.data)] 
    318326        qy_data = data2D.qy_data[numpy.isfinite(data2D.data)] 
    319327    
    320         y  = 0.0 
     328        y = 0.0 
    321329        err_y = 0.0 
    322330        y_counts = 0.0 
    323331 
    324         # Average pixelsize in q space                                                 
    325         for npts in range(len(data)):  
    326             # default frac                                                
    327             frac_x = 0  
    328             frac_y = 0                  
     332        # Average pixelsize in q space 
     333        for npts in range(len(data)): 
     334            # default frac 
     335            frac_x = 0 
     336            frac_y = 0 
    329337             
    330338            # get min and max at each points 
     
    337345            if self.y_min <= qy and self.y_max > qy: 
    338346                frac_y = 1 
    339             #Find the fraction along each directions             
     347            #Find the fraction along each directions 
    340348            frac = frac_x * frac_y 
    341349            if frac == 0: 
     
    348356            else: 
    349357                err_y += frac * frac * err_data[npts] * err_data[npts] 
    350             y_counts += frac      
     358            y_counts += frac 
    351359        return y, err_y, y_counts 
    352360 
    353361 
    354        
    355362class Boxavg(Boxsum): 
    356363    """ 
     
    363370    def __call__(self, data2D): 
    364371        """ 
    365         Perform the sum in the region of interest  
     372        Perform the sum in the region of interest 
    366373          
    367374        :param data2D: Data2D object 
     
    373380         
    374381        # Average the sums 
    375         counts = 0 if y_counts == 0 else y/y_counts 
    376         error  = 0 if y_counts == 0 else math.sqrt(err_y)/y_counts 
     382        counts = 0 if y_counts == 0 else y / y_counts 
     383        error = 0 if y_counts == 0 else math.sqrt(err_y) / y_counts 
    377384         
    378385        return counts, error 
    379386         
     387         
    380388def get_pixel_fraction_square(x, xmin, xmax): 
    381389    """ 
    382     Return the fraction of the length  
     390    Return the fraction of the length 
    383391    from xmin to x.:: 
    384392    
     
    437445        # Get the dq for resolution averaging 
    438446        if data2D.dqx_data != None and data2D.dqy_data != None: 
    439             # The pinholes and det. pix contribution present  
     447            # The pinholes and det. pix contribution present 
    440448            # in both direction of the 2D which must be subtracted when 
    441449            # converting to 1D: dq_overlap should calculated ideally at 
    442             # q = 0. Note This method works on only pinhole geometry.  
     450            # q = 0. Note This method works on only pinhole geometry. 
    443451            # Extrapolate dqx(r) and dqy(phi) at q = 0, and take an average. 
    444452            z_max = max(data2D.q_data) 
    445453            z_min = min(data2D.q_data) 
    446454            x_max = data2D.dqx_data[data2D.q_data[z_max]] 
    447             x_min = data2D.dqx_data[data2D.q_data[z_min]]     
     455            x_min = data2D.dqx_data[data2D.q_data[z_min]] 
    448456            y_max = data2D.dqy_data[data2D.q_data[z_max]] 
    449             y_min = data2D.dqy_data[data2D.q_data[z_min]]           
     457            y_min = data2D.dqy_data[data2D.q_data[z_min]] 
    450458            # Find qdx at q = 0 
    451459            dq_overlap_x = (x_min * z_max - x_max * z_min) / (z_max - z_min) 
     
    453461            if dq_overlap_x > min(data2D.dqx_data): 
    454462                dq_overlap_x = min(data2D.dqx_data) 
    455             dq_overlap_x *= dq_overlap_x  
     463            dq_overlap_x *= dq_overlap_x 
    456464            # Find qdx at q = 0 
    457465            dq_overlap_y = (y_min * z_max - y_max * z_min) / (z_max - z_min) 
     
    462470            dq_overlap_y *= dq_overlap_y 
    463471 
    464             dq_overlap = numpy.sqrt((dq_overlap_x + dq_overlap_y)/2.0) 
     472            dq_overlap = numpy.sqrt((dq_overlap_x + dq_overlap_y) / 2.0) 
    465473            # Final protection of dq 
    466474            if dq_overlap < 0: 
     
    470478            # def; dqx_data = dq_r dqy_data = dq_phi 
    471479            # Convert dq 2D to 1D here 
    472             dqx = dqx_data * dqx_data             
     480            dqx = dqx_data * dqx_data 
    473481            dqy = dqy_data * dqy_data 
    474482            dq_data = numpy.add(dqx, dqy) 
     
    484492        qbins = self.bin_width * numpy.arange(nbins) + self.r_min 
    485493 
    486         x  = numpy.zeros(nbins) 
    487         y  = numpy.zeros(nbins) 
     494        x = numpy.zeros(nbins) 
     495        y = numpy.zeros(nbins) 
    488496        err_y = numpy.zeros(nbins) 
    489497        err_x = numpy.zeros(nbins) 
    490498        y_counts = numpy.zeros(nbins) 
    491499 
    492         for npt in range(len(data)):    
     500        for npt in range(len(data)): 
    493501             
    494502            if ismask and not mask_data[npt]: 
    495                 continue    
     503                continue 
    496504             
    497505            frac = 0 
    498506             
    499507            # q-value at the pixel (j,i) 
    500             q_value = q_data[npt]  
    501             data_n = data[npt]                 
     508            q_value = q_data[npt] 
     509            data_n = data[npt] 
    502510             
    503511            ## No need to calculate the frac when all data are within range 
    504512            if self.r_min >= self.r_max: 
    505                 raise ValueError, "Limit Error: min > max"  
     513                raise ValueError, "Limit Error: min > max" 
    506514             
    507515            if self.r_min <= q_value and q_value <= self.r_max: 
    508                 frac = 1                        
     516                frac = 1 
    509517            if frac == 0: 
    510518                continue  
    511             i_q = int(math.floor((q_value - self.r_min) / self.bin_width))     
    512  
    513             # Take care of the edge case at phi = 2pi.  
    514             if i_q == nbins:   
    515                 i_q = nbins -1               
     519            i_q = int(math.floor((q_value - self.r_min) / self.bin_width)) 
     520 
     521            # Take care of the edge case at phi = 2pi. 
     522            if i_q == nbins: 
     523                i_q = nbins - 1 
    516524            y[i_q] += frac * data_n 
    517525            # Take dqs from data to get the q_average 
     
    524532                err_y[i_q] += frac * frac * err_data[npt] * err_data[npt] 
    525533            if dq_data != None: 
    526                 # To be consistent with dq calculation in 1d reduction,  
    527                 # we need just the averages (not quadratures) because  
    528                 # it should not depend on the number of the q points  
     534                # To be consistent with dq calculation in 1d reduction, 
     535                # we need just the averages (not quadratures) because 
     536                # it should not depend on the number of the q points 
    529537                # in the qr bins. 
    530538                err_x[i_q] += frac * dq_data[npt] 
    531539            else: 
    532540                err_x = None 
    533             y_counts[i_q]  += frac 
    534          
    535         # Average the sums        
     541            y_counts[i_q] += frac 
     542         
     543        # Average the sums 
    536544        for n in range(nbins): 
    537             if err_y[n] < 0: err_y[n] = -err_y[n] 
     545            if err_y[n] < 0: 
     546                err_y[n] = -err_y[n] 
    538547            err_y[n] = math.sqrt(err_y[n]) 
    539548            #if err_x != None: 
     
    541550             
    542551        err_y = err_y / y_counts 
    543         err_y[err_y==0] = numpy.average(err_y) 
    544         y    = y / y_counts 
    545         x    = x / y_counts 
    546         idx = (numpy.isfinite(y)) & (numpy.isfinite(x))  
     552        err_y[err_y == 0] = numpy.average(err_y) 
     553        y = y / y_counts 
     554        x = x / y_counts 
     555        idx = (numpy.isfinite(y)) & (numpy.isfinite(x)) 
    547556         
    548557        if err_x != None: 
     
    551560            d_x = None 
    552561 
    553         if not idx.any():  
    554             msg = "Average Error: No points inside ROI to average..."  
     562        if not idx.any(): 
     563            msg = "Average Error: No points inside ROI to average..." 
    555564            raise ValueError, msg 
    556565         
     
    567576    around the ring as a function of phi. 
    568577     
    569     Phi_min and phi_max should be defined between 0 and 2*pi  
     578    Phi_min and phi_max should be defined between 0 and 2*pi 
    570579    in anti-clockwise starting from the x- axis on the left-hand side 
    571580    """ 
     
    601610        q_data = data2D.q_data[numpy.isfinite(data2D.data)] 
    602611        err_data = data2D.err_data[numpy.isfinite(data2D.data)] 
    603         qx_data = data2D.qx_data[numpy.isfinite(data2D.data)]  
     612        qx_data = data2D.qx_data[numpy.isfinite(data2D.data)] 
    604613        qy_data = data2D.qy_data[numpy.isfinite(data2D.data)] 
    605614         
     
    612621        phi_err    = numpy.zeros(self.nbins_phi) 
    613622         
    614         for npt in range(len(data)):           
     623        for npt in range(len(data)): 
    615624            frac = 0 
    616625            # q-value at the point (npt) 
    617626            q_value = q_data[npt] 
    618             data_n = data[npt]     
     627            data_n = data[npt] 
    619628                         
    620629            # phi-value at the point (npt) 
     
    622631             
    623632            if self.r_min <= q_value and q_value <= self.r_max: 
    624                 frac = 1                        
     633                frac = 1 
    625634            if frac == 0: 
    626635                continue 
     
    628637            i_phi = int(math.floor((self.nbins_phi) * phi_value / (2 * Pi))) 
    629638             
    630             # Take care of the edge case at phi = 2pi.  
    631             if i_phi == self.nbins_phi:   
    632                 i_phi =  self.nbins_phi - 1                             
     639            # Take care of the edge case at phi = 2pi. 
     640            if i_phi == self.nbins_phi: 
     641                i_phi =  self.nbins_phi - 1 
    633642            phi_bins[i_phi] += frac * data[npt] 
    634643             
     
    646655            phi_values[i] = 2.0 * math.pi / self.nbins_phi * (1.0 * i + 0.5) 
    647656             
    648         idx = (numpy.isfinite(phi_bins))  
     657        idx = (numpy.isfinite(phi_bins)) 
    649658 
    650659        if not idx.any(): 
    651             msg = "Average Error: No points inside ROI to average..."  
     660            msg = "Average Error: No points inside ROI to average..." 
    652661            raise ValueError, msg 
    653662        #elif len(phi_bins[idx])!= self.nbins_phi: 
     
    656665        return Data1D(x=phi_values[idx], y=phi_bins[idx], dy=phi_err[idx]) 
    657666     
     667     
    658668def get_pixel_fraction(qmax, q_00, q_01, q_10, q_11): 
    659669    """ 
    660670    Returns the fraction of the pixel defined by 
    661     the four corners (q_00, q_01, q_10, q_11) that  
     671    the four corners (q_00, q_01, q_10, q_11) that 
    662672    has q < qmax.:: 
    663673     
     
    714724    # this pixel and the constant-q ring. We only need 
    715725    # to know if we have to include it or exclude it. 
    716     elif (q_00 + q_01 + q_10 + q_11)/4.0 < qmax: 
     726    elif (q_00 + q_01 + q_10 + q_11) / 4.0 < qmax: 
    717727        frac_max = 1.0 
    718728 
    719729    return frac_max 
     730              
    720731              
    721732def get_intercept(q, q_0, q_1): 
     
    727738     
    728739     
    729             A           B     
     740            A           B 
    730741        +-----------+--------+    <--- pixel size 
    731         0                    1      
     742        0                    1 
    732743        Q_0 -------- Q ----- Q_1   <--- equivalent Q range 
    733744        if Q_1 > Q_0, A is returned 
     
    738749    if q_1 > q_0: 
    739750        if (q > q_0 and q <= q_1): 
    740             return (q - q_0)/(q_1 - q_0)     
     751            return (q - q_0) / (q_1 - q_0) 
    741752    else: 
    742753        if (q > q_1 and q <= q_0): 
    743             return (q - q_1)/(q_0 - q_1) 
     754            return (q - q_1) / (q_0 - q_1) 
    744755    return None 
    745756      
     757      
    746758class _Sector: 
    747759    """ 
    748760    Defines a sector region on a 2D data set. 
    749761    The sector is defined by r_min, r_max, phi_min, phi_max, 
    750     and the position of the center of the ring  
     762    and the position of the center of the ring 
    751763    where phi_min and phi_max are defined by the right 
    752764    and left lines wrt central line 
    753     and phi_max could be less than phi_min.  
     765    and phi_max could be less than phi_min. 
    754766    
    755     Phi is defined between 0 and 2*pi in anti-clockwise  
     767    Phi is defined between 0 and 2*pi in anti-clockwise 
    756768    starting from the x- axis on the left-hand side 
    757769    """ 
     
    763775        self.nbins = nbins 
    764776         
    765      
    766777    def _agv(self, data2D, run='phi'): 
    767778        """ 
     
    781792        q_data = data2D.q_data[numpy.isfinite(data2D.data)] 
    782793        err_data = data2D.err_data[numpy.isfinite(data2D.data)] 
    783         qx_data = data2D.qx_data[numpy.isfinite(data2D.data)]  
     794        qx_data = data2D.qx_data[numpy.isfinite(data2D.data)] 
    784795        qy_data = data2D.qy_data[numpy.isfinite(data2D.data)] 
    785796        dq_data = None 
     
    787798        # Get the dq for resolution averaging 
    788799        if data2D.dqx_data != None and data2D.dqy_data != None: 
    789             # The pinholes and det. pix contribution present  
     800            # The pinholes and det. pix contribution present 
    790801            # in both direction of the 2D which must be subtracted when 
    791802            # converting to 1D: dq_overlap should calculated ideally at 
    792             # q = 0.  
     803            # q = 0. 
    793804            # Extrapolate dqy(perp) at q = 0 
    794805            z_max = max(data2D.q_data) 
    795806            z_min = min(data2D.q_data) 
    796807            x_max = data2D.dqx_data[data2D.q_data[z_max]] 
    797             x_min = data2D.dqx_data[data2D.q_data[z_min]]     
     808            x_min = data2D.dqx_data[data2D.q_data[z_min]] 
    798809            y_max = data2D.dqy_data[data2D.q_data[z_max]] 
    799             y_min = data2D.dqy_data[data2D.q_data[z_min]]           
     810            y_min = data2D.dqy_data[data2D.q_data[z_min]] 
    800811            # Find qdx at q = 0 
    801812            dq_overlap_x = (x_min * z_max - x_max * z_min) / (z_max - z_min) 
     
    803814            if dq_overlap_x > min(data2D.dqx_data): 
    804815                dq_overlap_x = min(data2D.dqx_data) 
    805             dq_overlap_x *= dq_overlap_x  
     816            dq_overlap_x *= dq_overlap_x 
    806817            # Find qdx at q = 0 
    807818            dq_overlap_y = (y_min * z_max - y_max * z_min) / (z_max - z_min) 
     
    812823            dq_overlap_y *= dq_overlap_y 
    813824 
    814             dq_overlap = numpy.sqrt((dq_overlap_x + dq_overlap_y) / 2.0)   
     825            dq_overlap = numpy.sqrt((dq_overlap_x + dq_overlap_y) / 2.0) 
    815826            if dq_overlap < 0: 
    816827                dq_overlap = y_min 
     
    819830            # def; dqx_data = dq_r dqy_data = dq_phi 
    820831            # Convert dq 2D to 1D here 
    821             dqx = dqx_data * dqx_data             
     832            dqx = dqx_data * dqx_data 
    822833            dqy = dqy_data * dqy_data 
    823834            dq_data = numpy.add(dqx, dqy) 
     
    827838        x        = numpy.zeros(self.nbins) 
    828839        y        = numpy.zeros(self.nbins) 
    829         y_err    = numpy.zeros(self.nbins)    
    830         x_err    = numpy.zeros(self.nbins)       
     840        y_err    = numpy.zeros(self.nbins) 
     841        x_err    = numpy.zeros(self.nbins) 
    831842        y_counts = numpy.zeros(self.nbins) 
    832843                       
     
    837848        q_data_max = numpy.max(q_data) 
    838849                       
    839         for n in range(len(data)):      
     850        for n in range(len(data)): 
    840851            frac = 0 
    841852             
     
    848859             
    849860            # phi-value of the pixel (j,i) 
    850             phi_value = math.atan2(qy_data[n], qx_data[n]) + Pi  
     861            phi_value = math.atan2(qy_data[n], qx_data[n]) + Pi 
    851862             
    852863            ## No need to calculate the frac when all data are within range 
    853864            if self.r_min <= q_value and q_value <= self.r_max: 
    854                 frac = 1                        
     865                frac = 1 
    855866            if frac == 0: 
    856867                continue 
    857868            #In case of two ROIs (symmetric major and minor regions)(for 'q2') 
    858             if run.lower()=='q2': 
    859                 ## For minor sector wing  
     869            if run.lower() == 'q2': 
     870                ## For minor sector wing 
    860871                # Calculate the minor wing phis 
    861872                phi_min_minor = flip_phi(phi_min - Pi) 
     
    869880                              phi_value < phi_max_minor) 
    870881 
    871             #For all cases(i.e.,for 'q', 'q2', and 'phi')  
    872             #Find pixels within ROI   
    873             if phi_min > phi_max:  
     882            #For all cases(i.e.,for 'q', 'q2', and 'phi') 
     883            #Find pixels within ROI 
     884            if phi_min > phi_max: 
    874885                is_in = is_in or (phi_value > phi_min or \ 
    875                                    phi_value < phi_max)          
     886                                   phi_value < phi_max) 
    876887            else: 
    877888                is_in = is_in or (phi_value >= phi_min  and \ 
     
    879890             
    880891            if not is_in: 
    881                 frac = 0                                                         
     892                frac = 0 
    882893            if frac == 0: 
    883894                continue 
    884895            # Check which type of averaging we need 
    885             if run.lower() == 'phi':  
     896            if run.lower() == 'phi': 
    886897                temp_x = (self.nbins) * (phi_value - self.phi_min) 
    887898                temp_y = (self.phi_max - self.phi_min) 
     
    892903                i_bin = int(math.floor(temp_x / temp_y)) 
    893904 
    894             # Take care of the edge case at phi = 2pi.  
    895             if i_bin == self.nbins:   
    896                 i_bin =  self.nbins - 1 
     905            # Take care of the edge case at phi = 2pi. 
     906            if i_bin == self.nbins: 
     907                i_bin = self.nbins - 1 
    897908                 
    898             ## Get the total y           
     909            ## Get the total y 
    899910            y[i_bin] += frac * data_n 
    900911            x[i_bin] += frac * q_value 
     
    907918                 
    908919            if dq_data != None: 
    909                 # To be consistent with dq calculation in 1d reduction,  
    910                 # we need just the averages (not quadratures) because  
    911                 # it should not depend on the number of the q points  
     920                # To be consistent with dq calculation in 1d reduction, 
     921                # we need just the averages (not quadratures) because 
     922                # it should not depend on the number of the q points 
    912923                # in the qr bins. 
    913924                x_err[i_bin] += frac * dq_data[n] 
     
    923934            # The type of averaging: phi,q2, or q 
    924935            # Calculate x[i]should be at the center of the bin 
    925             if run.lower()=='phi':                
     936            if run.lower() == 'phi': 
    926937                x[i] = (self.phi_max - self.phi_min) / self.nbins * \ 
    927938                    (1.0 * i + 0.5) + self.phi_min 
    928939            else: 
    929                 # We take the center of ring area, not radius.   
     940                # We take the center of ring area, not radius. 
    930941                # This is more accurate than taking the radial center of ring. 
    931942                #delta_r = (self.r_max - self.r_min) / self.nbins 
     
    934945                #x[i] = math.sqrt((r_inner * r_inner + r_outer * r_outer) / 2) 
    935946                x[i] = x[i] / y_counts[i] 
    936         y_err[y_err==0] = numpy.average(y_err)         
     947        y_err[y_err == 0] = numpy.average(y_err) 
    937948        idx = (numpy.isfinite(y) & numpy.isfinite(y_err)) 
    938949        if x_err != None: 
     
    941952            d_x = None 
    942953        if not idx.any(): 
    943             msg = "Average Error: No points inside sector of ROI to average..."  
     954            msg = "Average Error: No points inside sector of ROI to average..." 
    944955            raise ValueError, msg 
    945956        #elif len(y[idx])!= self.nbins: 
     
    948959        return Data1D(x=x[idx], y=y[idx], dy=y_err[idx], dx=d_x) 
    949960                 
     961                 
    950962class SectorPhi(_Sector): 
    951963    """ 
     
    963975        :return: Data1D object 
    964976        """ 
    965  
    966977        return self._agv(data2D, 'phi') 
     978     
    967979     
    968980class SectorQ(_Sector): 
     
    972984     
    973985    A sector is defined by r_min, r_max, phi_min, phi_max. 
    974     r_min, r_max, phi_min, phi_max >0.   
     986    r_min, r_max, phi_min, phi_max >0. 
    975987    The number of bin in Q also has to be defined. 
    976988    """ 
     
    984996        """ 
    985997        return self._agv(data2D, 'q2') 
     998 
    986999 
    9871000class Ringcut(object): 
     
    9931006    The data returned is the region inside the ring 
    9941007     
    995     Phi_min and phi_max should be defined between 0 and 2*pi  
     1008    Phi_min and phi_max should be defined between 0 and 2*pi 
    9961009    in anti-clockwise starting from the x- axis on the left-hand side 
    9971010    """ 
    998     def __init__(self, r_min=0, r_max=0, center_x=0, center_y=0 ): 
     1011    def __init__(self, r_min=0, r_max=0, center_x=0, center_y=0): 
    9991012        # Minimum radius 
    10001013        self.r_min = r_min 
     
    10061019        self.center_y = center_y 
    10071020 
    1008          
    10091021    def __call__(self, data2D): 
    10101022        """ 
     
    10201032 
    10211033        # Get data 
    1022         qx_data = data2D.qx_data  
     1034        qx_data = data2D.qx_data 
    10231035        qy_data = data2D.qy_data 
    10241036        mask = data2D.mask 
    10251037        q_data = numpy.sqrt(qx_data * qx_data + qy_data * qy_data) 
    1026         #q_data_max = numpy.max(q_data) 
    10271038 
    10281039        # check whether or not the data point is inside ROI 
     
    10511062          
    10521063       :param data2D: Data2D object 
    1053        :return: mask, 1d array (len = len(data))  
     1064       :return: mask, 1d array (len = len(data)) 
    10541065           with Trues where the data points are inside ROI, otherwise False 
    10551066        """ 
     
    10601071    def _find(self, data2D): 
    10611072        """ 
    1062         Find a rectangular 2D region of interest.  
    1063          
    1064         :param data2D: Data2D object 
    1065          
    1066         :return: out, 1d array (length = len(data))  
     1073        Find a rectangular 2D region of interest. 
     1074         
     1075        :param data2D: Data2D object 
     1076         
     1077        :return: out, 1d array (length = len(data)) 
    10671078           with Trues where the data points are inside ROI, otherwise Falses 
    10681079        """ 
    10691080        if data2D.__class__.__name__ not in ["Data2D", "plottable_2D"]: 
    10701081            raise RuntimeError, "Boxcut take only plottable_2D objects" 
    1071         # Get qx_ and qy_data  
     1082        # Get qx_ and qy_data 
    10721083        qx_data = data2D.qx_data 
    10731084        qy_data = data2D.qy_data 
     
    10801091        return (outx & outy) 
    10811092 
     1093 
    10821094class Sectorcut(object): 
    10831095    """ 
    10841096    Defines a sector (major + minor) region on a 2D data set. 
    10851097    The sector is defined by phi_min, phi_max, 
    1086     where phi_min and phi_max are defined by the right  
    1087     and left lines wrt central line.  
     1098    where phi_min and phi_max are defined by the right 
     1099    and left lines wrt central line. 
    10881100    
    1089     Phi_min and phi_max are given in units of radian  
     1101    Phi_min and phi_max are given in units of radian 
    10901102    and (phi_max-phi_min) should not be larger than pi 
    10911103    """ 
     
    11001112        :param data2D: Data2D object 
    11011113         
    1102         :return: mask, 1d array (len = len(data))  
     1114        :return: mask, 1d array (len = len(data)) 
    11031115         
    11041116        with Trues where the data points are inside ROI, otherwise False 
     
    11101122    def _find(self, data2D): 
    11111123        """ 
    1112         Find a rectangular 2D region of interest.  
    1113          
    1114         :param data2D: Data2D object 
    1115          
    1116         :return: out, 1d array (length = len(data))  
     1124        Find a rectangular 2D region of interest. 
     1125         
     1126        :param data2D: Data2D object 
     1127         
     1128        :return: out, 1d array (length = len(data)) 
    11171129         
    11181130        with Trues where the data points are inside ROI, otherwise Falses 
    11191131        """ 
    11201132        if data2D.__class__.__name__ not in ["Data2D", "plottable_2D"]: 
    1121             raise RuntimeError, "Sectorcut take only plottable_2D objects"  
     1133            raise RuntimeError, "Sectorcut take only plottable_2D objects" 
    11221134        Pi = math.pi 
    11231135        # Get data  
    11241136        qx_data = data2D.qx_data 
    1125         qy_data = data2D.qy_data         
     1137        qy_data = data2D.qy_data 
    11261138        phi_data = numpy.zeros(len(qx_data)) 
    11271139 
     
    11311143        # Get the min and max into the region: -pi <= phi < Pi 
    11321144        phi_min_major = flip_phi(self.phi_min + Pi) - Pi 
    1133         phi_max_major = flip_phi(self.phi_max + Pi) - Pi   
     1145        phi_max_major = flip_phi(self.phi_max + Pi) - Pi 
    11341146        # check for major sector 
    11351147        if phi_min_major > phi_max_major: 
     
    11461158        if phi_min_minor > phi_max_minor: 
    11471159            out_minor = (phi_min_minor <= phi_data) + \ 
    1148                             (phi_max_minor >= phi_data)  
     1160                            (phi_max_minor >= phi_data) 
    11491161        else: 
    11501162            out_minor = (phi_min_minor <= phi_data) & \ 
    1151                             (phi_max_minor >= phi_data)  
     1163                            (phi_max_minor >= phi_data) 
    11521164        out = out_major + out_minor 
    11531165         
    11541166        return out 
    1155  
    1156 if __name__ == "__main__":  
    1157  
    1158     from loader import Loader 
    1159      
    1160  
    1161     d = Loader().load('test/MAR07232_rest.ASC') 
    1162     #d = Loader().load('test/MP_New.sans') 
    1163  
    1164      
    1165     r = SectorQ(r_min=.000001, r_max=.01, phi_min=0.0, phi_max=2*math.pi) 
    1166     o = r(d) 
    1167      
    1168     s = Ring(r_min=.000001, r_max=.01)  
    1169     p = s(d) 
    1170      
    1171     for i in range(len(o.x)): 
    1172         print o.x[i], o.y[i], o.dy[i] 
    1173      
    1174   
    1175      
Note: See TracChangeset for help on using the changeset viewer.