Changeset f60a8c2 in sasview


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

Location:
sansdataloader/src/sans/dataloader
Files:
3 edited

Legend:

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

    re38e335 rf60a8c2  
    11""" 
    2     Module that contains classes to hold information read from  
     2    Module that contains classes to hold information read from 
    33    reduced data files. 
    44     
    5     A good description of the data members can be found in  
     5    A good description of the data members can be found in 
    66    the CanSAS 1D XML data format: 
    77     
    88    http://www.smallangles.net/wgwiki/index.php/cansas1d_documentation 
    99""" 
    10  
    11  
     10##################################################################### 
    1211#This software was developed by the University of Tennessee as part of the 
    1312#Distributed Data Analysis of Neutron Scattering Experiments (DANSE) 
    14 #project funded by the US National Science Foundation.  
    15 #If you use DANSE applications to do scientific research that leads to  
    16 #publication, we ask that you acknowledge the use of the software with the  
    17 #following sentence: 
    18 #This work benefited from DANSE software developed under NSF award DMR-0520547. 
     13#project funded by the US National Science Foundation. 
     14#See the license text in license.txt 
    1915#copyright 2008, University of Tennessee 
     16###################################################################### 
    2017 
    2118 
     
    2825import numpy 
    2926import math 
     27 
    3028 
    3129class plottable_1D: 
     
    5452        self.y = numpy.asarray(y) 
    5553        if dx is not None: 
    56              self.dx = numpy.asarray(dx) 
    57         if dy is not None:  
     54            self.dx = numpy.asarray(dx) 
     55        if dy is not None: 
    5856            self.dy = numpy.asarray(dy) 
    59         if dxl is not None:  
     57        if dxl is not None: 
    6058            self.dxl = numpy.asarray(dxl) 
    6159        if dxw is not None:  
     
    7573        self._yaxis = label 
    7674        self._yunit = unit 
     75 
    7776 
    7877class plottable_2D: 
     
    8584    ymax = None 
    8685    data = None 
    87     qx_data     = None 
    88     qy_data     = None 
    89     q_data      = None 
    90     err_data    = None 
    91     dqx_data    = None 
    92     dqy_data    = None 
    93     mask        = None 
     86    qx_data = None 
     87    qy_data = None 
     88    q_data = None 
     89    err_data = None 
     90    dqx_data = None 
     91    dqy_data = None 
     92    mask = None 
    9493     
    9594    # Units 
     
    102101     
    103102    def __init__(self, data=None, err_data=None, qx_data=None, 
    104                   qy_data=None, q_data=None, mask=None, 
    105                    dqx_data=None, dqy_data=None): 
     103                 qy_data=None, q_data=None, mask=None, 
     104                 dqx_data=None, dqy_data=None): 
    106105        self.data = numpy.asarray(data) 
    107106        self.qx_data = numpy.asarray(qx_data) 
     
    110109        self.mask = numpy.asarray(mask) 
    111110        self.err_data = numpy.asarray(err_data) 
    112         if dqx_data is not None: self.dqx_data = numpy.asarray(dqx_data)  
    113         if dqy_data is not None: self.dqy_data = numpy.asarray(dqy_data)  
     111        if dqx_data is not None: 
     112            self.dqx_data = numpy.asarray(dqx_data)  
     113        if dqy_data is not None: 
     114            self.dqy_data = numpy.asarray(dqy_data)  
    114115                
    115116    def xaxis(self, label, unit): 
     
    174175    distance = None 
    175176    distance_unit = 'mm' 
    176     ## Offset of this detector position in X, Y,  
    177     #(and Z if necessary) [Vector] [mm]  
     177    ## Offset of this detector position in X, Y, 
     178    #(and Z if necessary) [Vector] [mm] 
    178179    offset = None 
    179180    offset_unit = 'm' 
     
    182183    orientation = None 
    183184    orientation_unit = 'degree' 
    184     ## Center of the beam on the detector in X and Y  
     185    ## Center of the beam on the detector in X and Y 
    185186    #(and Z if necessary) [Vector] [mm] 
    186187    beam_center = None 
     
    204205        self.pixel_size  = Vector() 
    205206         
    206      
    207207    def __str__(self): 
    208208        _str  = "Detector:\n" 
     
    222222        return _str 
    223223 
     224 
    224225class Aperture: 
    225226    ## Name 
     
    239240        self.size = Vector() 
    240241     
     242     
    241243class Collimation: 
    242244    """ 
     
    265267        return _str 
    266268 
     269 
    267270class Source: 
    268271    """ 
    269272    Class to hold source information 
    270     """   
     273    """ 
    271274    ## Name 
    272275    name = None 
     
    296299        self.beam_size = Vector() 
    297300         
    298      
    299301    def __str__(self): 
    300302        _str  = "Source:\n" 
     
    314316     
    315317     
    316 """  
     318""" 
    317319Definitions of radiation types 
    318320""" 
     
    321323MUON     = 'muon' 
    322324ELECTRON = 'electron' 
     325     
    323326     
    324327class Sample: 
     
    371374        return _str 
    372375   
     376   
    373377class Process: 
    374378    """ 
     
    405409    the data itself and any other meta data. 
    406410    """ 
    407     ## Title  
     411    ## Title 
    408412    title      = '' 
    409413    ## Run number 
     
    460464        self.meta_data  = {} 
    461465        ## Loading errors 
    462         self.errors = []         
     466        self.errors = [] 
    463467         
    464468    def append_empty_process(self): 
     
    497501    # but should be implemented for each data class inherited from DataInfo 
    498502    # that holds actual data (ex.: Data1D) 
    499     def _perform_operation(self, other, operation):  
     503    def _perform_operation(self, other, operation): 
    500504        """ 
    501505        Private method to perform operation. Not implemented for DataInfo, 
     
    619623        def operation(a, b): 
    620624            return b/a 
    621         return self._perform_operation(other, operation)             
     625        return self._perform_operation(other, operation) 
     626             
    622627             
    623628class Data1D(plottable_1D, DataInfo): 
     
    631636        DataInfo.__init__(self) 
    632637        plottable_1D.__init__(self, x, y, dx, dy) 
    633          
    634638         
    635639    def __str__(self): 
     
    654658         
    655659        """ 
    656         def _check(v):             
     660        def _check(v): 
    657661            if (v.__class__ == list or v.__class__ == numpy.ndarray) \ 
    658662                and len(v) > 0 and min(v) > 0: 
     
    675679         
    676680        if clone is None or not issubclass(clone.__class__, Data1D): 
    677             x  = numpy.zeros(length)  
    678             dx = numpy.zeros(length)  
    679             y  = numpy.zeros(length)  
    680             dy = numpy.zeros(length)  
     681            x  = numpy.zeros(length) 
     682            dx = numpy.zeros(length) 
     683            y  = numpy.zeros(length) 
     684            dy = numpy.zeros(length) 
    681685            clone = Data1D(x, y, dx=dx, dy=dy) 
    682686         
     
    684688        clone.run         = self.run 
    685689        clone.filename    = self.filename 
    686         clone.instrument   = self.instrument 
    687         clone.notes       = deepcopy(self.notes)  
    688         clone.process     = deepcopy(self.process)  
    689         clone.detector    = deepcopy(self.detector)  
    690         clone.sample      = deepcopy(self.sample)  
    691         clone.source      = deepcopy(self.source)  
    692         clone.collimation = deepcopy(self.collimation)  
    693         clone.meta_data   = deepcopy(self.meta_data)  
    694         clone.errors      = deepcopy(self.errors)  
     690        clone.instrument  = self.instrument 
     691        clone.notes       = deepcopy(self.notes) 
     692        clone.process     = deepcopy(self.process) 
     693        clone.detector    = deepcopy(self.detector) 
     694        clone.sample      = deepcopy(self.sample) 
     695        clone.source      = deepcopy(self.source) 
     696        clone.collimation = deepcopy(self.collimation) 
     697        clone.meta_data   = deepcopy(self.meta_data) 
     698        clone.errors      = deepcopy(self.errors) 
    695699         
    696700        return clone 
     
    716720            if len(self.x) != len(other.x) or \ 
    717721                len(self.y) != len(other.y): 
    718                 msg =  "Unable to perform operation: data length are not equal" 
     722                msg = "Unable to perform operation: data length are not equal" 
    719723                raise ValueError, msg 
    720724             
     
    734738        dy = self.dy 
    735739        if self.dy == None or (len(self.dy) != len(self.y)): 
    736             dy = numpy.zeros(len(self.y))             
     740            dy = numpy.zeros(len(self.y)) 
    737741             
    738742        return dy, dy_other 
     
    761765        return result 
    762766         
     767         
    763768class Data2D(plottable_2D, DataInfo): 
    764769    """ 
     
    777782    y_bins = None 
    778783     
    779      
    780784    def __init__(self, data=None, err_data=None, qx_data=None, 
    781                   qy_data=None, q_data=None, mask=None,  
    782                   dqx_data=None, dqy_data=None): 
     785                 qy_data=None, q_data=None, mask=None, 
     786                 dqx_data=None, dqy_data=None): 
    783787        self.y_bins = [] 
    784788        self.x_bins = [] 
    785789        DataInfo.__init__(self) 
    786790        plottable_2D.__init__(self, data, err_data, qx_data, 
    787                               qy_data, q_data,mask, dqx_data, dqy_data) 
     791                              qy_data, q_data, mask, dqx_data, dqy_data) 
    788792        if len(self.detector) > 0: 
    789793            raise RuntimeError, "Data2D: Detector bank already filled at init" 
    790794 
    791795    def __str__(self): 
    792         _str =  "%s\n" % DataInfo.__str__(self) 
     796        _str = "%s\n" % DataInfo.__str__(self) 
    793797         
    794798        _str += "Data:\n" 
     
    814818        from copy import deepcopy 
    815819         
    816         if clone is None or not issubclass(clone.__class__, Data2D):   
    817             data     = numpy.zeros(length)  
    818             err_data = numpy.zeros(length)  
     820        if clone is None or not issubclass(clone.__class__, Data2D): 
     821            data = numpy.zeros(length) 
     822            err_data = numpy.zeros(length) 
    819823            qx_data = numpy.zeros(length) 
    820824            qy_data = numpy.zeros(length) 
     
    824828            dqy_data = None 
    825829            clone = Data2D(data, err_data, qx_data, qy_data, 
    826                             q_data,mask, dqx_data=dqx_data, dqy_data=dqy_data) 
     830                            q_data, mask, dqx_data=dqx_data, dqy_data=dqy_data) 
    827831 
    828832        clone.title       = self.title 
     
    830834        clone.filename    = self.filename 
    831835        clone.instrument  = self.instrument 
    832         clone.notes       = deepcopy(self.notes)  
    833         clone.process     = deepcopy(self.process)  
    834         clone.detector    = deepcopy(self.detector)  
    835         clone.sample      = deepcopy(self.sample)  
    836         clone.source      = deepcopy(self.source)  
    837         clone.collimation = deepcopy(self.collimation)  
    838         clone.meta_data   = deepcopy(self.meta_data)  
    839         clone.errors      = deepcopy(self.errors)  
     836        clone.notes       = deepcopy(self.notes) 
     837        clone.process     = deepcopy(self.process) 
     838        clone.detector    = deepcopy(self.detector) 
     839        clone.sample      = deepcopy(self.sample) 
     840        clone.source      = deepcopy(self.source) 
     841        clone.collimation = deepcopy(self.collimation) 
     842        clone.meta_data   = deepcopy(self.meta_data) 
     843        clone.errors      = deepcopy(self.errors) 
    840844         
    841845        return clone 
    842    
    843846   
    844847    def _validity_check(self, other): 
     
    865868                
    866869            # Check that the scales match 
    867             #TODO: matching scales?      
     870            #TODO: matching scales? 
    868871             
    869872            # Check that the other data set has errors, otherwise 
     
    885888        return err, err_other 
    886889   
    887    
    888890    def _perform_operation(self, other, operation): 
    889891        """ 
     
    897899        dy, dy_other = self._validity_check(other) 
    898900     
    899         result = self.clone_without_data([numpy.size(self.data, 0),  
     901        result = self.clone_without_data([numpy.size(self.data, 0), 
    900902                                          numpy.size(self.data, 1)]) 
    901903         
     
    917919                result.err_data[i][j] = math.sqrt(math.fabs(output.variance)) 
    918920        return result 
    919      
    920    
    921   
  • sansdataloader/src/sans/dataloader/loader.py

    r371cb85 rf60a8c2  
    1  
     1""" 
     2    File handler to support different file extensions. 
     3    Uses reflectometry's registry utility. 
     4     
     5    The default readers are found in the 'readers' sub-module 
     6    and registered by default at initialization time. 
     7     
     8    To add a new default reader, one must register it in 
     9    the register_readers method found in readers/__init__.py. 
     10     
     11    A utility method (find_plugins) is available to inspect 
     12    a directory (for instance, a user plug-in directory) and 
     13    look for new readers/writers. 
     14""" 
    215##################################################################### 
    316#This software was developed by the University of Tennessee as part of the 
    417#Distributed Data Analysis of Neutron Scattering Experiments (DANSE) 
    5 #project funded by the US National Science Foundation.  
     18#project funded by the US National Science Foundation. 
    619#See the license text in license.txt 
    720#copyright 2008, University of Tennessee 
    821###################################################################### 
    922 
    10 """ 
    11     File handler to support different file extensions. 
    12     Uses reflectometry's registry utility. 
    13      
    14     The default readers are found in the 'readers' sub-module 
    15     and registered by default at initialization time. 
    16      
    17     To add a new default reader, one must register it in 
    18     the register_readers method found in readers/__init__.py.  
    19      
    20     A utility method (find_plugins) is available to inspect  
    21     a directory (for instance, a user plug-in directory) and 
    22     look for new readers/writers. 
    23 """ 
    24  
    25 import os  
     23import os 
    2624import sys 
    2725import logging 
     
    5553        readers.read_associations(self) 
    5654         
    57         #TODO: remove the following line when ready to switch to  
     55        #TODO: remove the following line when ready to switch to 
    5856        #the new default readers 
    5957        #readers.register_readers(self._identify_plugin) 
     
    6260        #self.find_plugins('plugins') 
    6361 
    64          
    6562    def load(self, path, format=None): 
    6663        """ 
     
    6865 
    6966        :param path: file path 
    70         :param format: explicit extension, to force the use  
     67        :param format: explicit extension, to force the use 
    7168            of a particular reader 
    7269 
    7370        Defaults to the ascii (multi-column) reader 
    7471        if no reader was registered for the file's 
    75         extension.    
     72        extension. 
    7673        """ 
    7774        try: 
     
    107104        dir = temp_path 
    108105        # Check whether the directory exists 
    109         if not os.path.isdir(dir):  
     106        if not os.path.isdir(dir): 
    110107            msg = "DataLoader couldn't locate DataLoader plugin folder." 
    111108            msg += """ "%s" does not exist""" % dir 
     
    157154                        logging.error(msg) 
    158155                      
    159         return readers_found  
     156        return readers_found 
    160157     
    161158    def associate_file_type(self, ext, module): 
    162159        """ 
    163         Look into a module to find whether it contains a  
     160        Look into a module to find whether it contains a 
    164161        Reader class. If so, APPEND it to readers and (potentially) 
    165162        to the list of writers for the given extension 
     
    226223                type_name = loader.type_name 
    227224                 
    228                 wcard = "%s files (*%s)|*%s" % (type_name, ext.lower(),  
     225                wcard = "%s files (*%s)|*%s" % (type_name, ext.lower(), 
    229226                                                ext.lower()) 
    230227                if wcard not in self.wildcards: 
     
    237234        return reader_found 
    238235 
    239      
    240236    def _identify_plugin(self, module): 
    241237        """ 
     
    268264                                                     ext.lower()) 
    269265                    if wcard not in self.wildcards: 
    270                         self.wildcards.append(wcard) 
     266                        self.wildcards.append(wcard) 
    271267                             
    272268                # Check whether writing is supported 
     
    275271                        if ext not in self.writers: 
    276272                            self.writers[ext] = [] 
    277                         self.writers[ext].insert(0,loader.write) 
     273                        self.writers[ext].insert(0, loader.write) 
    278274                         
    279275            except: 
     
    288284         
    289285        :Raises ValueError: if file type is not known. 
    290         """         
     286        """ 
    291287        # Find matching extensions 
    292288        extlist = [ext for ext in self.extensions() if path.endswith(ext)] 
     
    301297            result = [] 
    302298            for L in writers: 
    303                 if L not in result: result.append(L) 
     299                if L not in result: 
     300                    result.append(L) 
    304301            writers = L 
    305302        # Raise an error if there are no matching extensions 
     
    316313        Raises KeyError if format is not available. 
    317314         
    318         May raise a writer-defined exception if writer fails.         
     315        May raise a writer-defined exception if writer fails. 
    319316        """ 
    320317        if format is None: 
     
    326323                return fn(path, data) 
    327324            except: 
    328                 pass # give other loaders a chance to succeed 
     325                pass  # give other loaders a chance to succeed 
    329326        # If we get here it is because all loaders failed 
    330         raise # reraises last exception 
     327        raise  # reraises last exception 
    331328 
    332329         
     
    340337    def associate_file_type(self, ext, module): 
    341338        """ 
    342         Look into a module to find whether it contains a  
     339        Look into a module to find whether it contains a 
    343340        Reader class. If so, append it to readers and (potentially) 
    344341        to the list of writers for the given extension 
     
    394391    def get_wildcards(self): 
    395392        return self.__registry.wildcards 
    396          
    397 if __name__ == "__main__":  
    398     logging.basicConfig(level=logging.INFO, 
    399                         format='%(asctime)s %(levelname)s %(message)s', 
    400                         filename='loader.log', 
    401                         filemode='w') 
    402     l = Loader() 
    403     test_data = l.load('test/cansas1d.xml') 
    404     l.save('test_file.xml', test_data, '.xml') 
    405      
    406     print l.get_wildcards() 
    407          
    408          
    409      
  • 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.