Ignore:
Timestamp:
Apr 27, 2012 10:23:08 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:
f60a8c2
Parents:
8a621ac
Message:

Pep-8-ification

File:
1 edited

Legend:

Unmodified
Added
Removed
  • plottools/src/danse/common/plottools/plottables.py

    r82a54b8 r10bfeb3  
    4444import copy 
    4545import numpy 
    46 import math 
    4746import sys 
    4847 
     
    5150    def any(L): 
    5251        for cond in L: 
    53             if cond: return True 
     52            if cond: 
     53                return True 
    5454        return False 
     55     
    5556    def all(L): 
    5657        for cond in L: 
     
    5960        return True 
    6061 
    61 # Graph structure for holding multiple plottables 
     62 
    6263class Graph: 
    6364    """ 
     
    129130        (as opposed to changing the basic properties) 
    130131        """ 
    131         if units != "":  
    132              name = "%s (%s)" % (name, units) 
     132        if units != "": 
     133            name = "%s (%s)" % (name, units) 
    133134        self.prop["xlabel"] = name 
    134135        self.prop["xunit"] = units 
     
    140141        (as opposed to changing the basic properties) 
    141142        """ 
    142         if units != "":  
    143              name = "%s (%s)" % (name, units) 
     143        if units != "": 
     144            name = "%s (%s)" % (name, units) 
    144145        self.prop["ylabel"] = name 
    145146        self.prop["yunit"] = units 
     
    149150        Properties of the x axis. 
    150151        """ 
    151         if units != "":  
    152              name = "%s (%s)" % (name, units) 
     152        if units != "": 
     153            name = "%s (%s)" % (name, units) 
    153154        self.prop["xlabel"] = name 
    154155        self.prop["xunit"] = units 
     
    160161        Properties of the y axis. 
    161162        """ 
    162         if units != "":  
    163              name = "%s (%s)" % (name, units) 
     163        if units != "": 
     164            name = "%s (%s)" % (name, units) 
    164165        self.prop["ylabel"] = name 
    165166        self.prop["yunit"] = units 
     
    200201        if plottable in self.plottables: 
    201202            return True 
    202         return False   
     203        return False 
    203204 
    204205    def add(self, plottable, color=None): 
     
    211212                self.color += plottable.colors() 
    212213                self.plottables[plottable] = self.color 
    213                  
     214 
    214215    def changed(self): 
    215216        """Detect if any graphed plottables have changed""" 
     
    250251        if plottable in self.plottables: 
    251252            del self.plottables[plottable] 
    252             self.color = len(self.plottables)  
     253            self.color = len(self.plottables) 
    253254             
    254255    def reset_scale(self): 
     
    263264        self.color = -1 
    264265        self.symbol = 0 
    265         self.prop = {"xlabel":"", "xunit":None, 
    266                      "ylabel":"","yunit":None, 
    267                      "title":""} 
     266        self.prop = {"xlabel": "", "xunit": None, 
     267                     "ylabel": "", "yunit": None, 
     268                     "title": ""} 
    268269        self.plottables = {} 
    269270     
     
    297298        """ 
    298299        This method returns a dictionary of plottables contained in graph 
    299         It is just by Plotpanel to interact with the complete list of plottables  
     300        It is just by Plotpanel to interact with the complete list of plottables 
    300301        inside the graph. 
    301302        """ 
     
    310311        for p in self.plottables: 
    311312            if p.custom_color is not None: 
    312                 p.render(plot, color=p.custom_color, symbol=0,  
     313                p.render(plot, color=p.custom_color, symbol=0, 
    313314                markersize=p.markersize, label=labels[p]) 
    314315            else: 
    315                 p.render(plot, color=self.plottables[p], symbol=0,  
     316                p.render(plot, color=self.plottables[p], symbol=0, 
    316317                     markersize=p.markersize, label=labels[p]) 
    317318        plot.render() 
     
    346347        menu for the graph. 
    347348         
    348     type: operational axis.  This determines whether the  
     349    type: operational axis.  This determines whether the 
    349350        transform should appear on x,y or z axis context 
    350351        menus, or if it should appear in the context menu for 
     
    353354    inventory: (not implemented)  
    354355        a dictionary of user settable parameter names and 
    355         their associated types.  These should appear as keyword  
    356         arguments to the transform call.  For example, Fresnel  
     356        their associated types.  These should appear as keyword 
     357        arguments to the transform call.  For example, Fresnel 
    357358        reflectivity requires the substrate density: 
    358359             { 'rho': type.Value(10e-6/units.angstrom**2) } 
    359360              
    360         Supply reasonable defaults in the callback so that  
    361         limited plotting clients work even though they cannot  
     361        Supply reasonable defaults in the callback so that 
     362        limited plotting clients work even though they cannot 
    362363        set the inventory. 
    363364         
     
    373374        the standard x,dx,y,dy,z,dz attributes appropriately. 
    374375         
    375         If the call raises a NotImplemented error the dataline  
     376        If the call raises a NotImplemented error the dataline 
    376377        will not be plotted.  The associated string will usually 
    377378        be 'Not a valid transform', though other strings are possible. 
     
    380381         
    381382        """ 
    382         raise NotImplemented,"Not a valid transform" 
     383        raise NotImplemented, "Not a valid transform" 
    383384 
    384385    # Related issues 
     
    387388    # log scale: 
    388389    #    All axes have implicit log/linear scaling options. 
    389     #  
     390    # 
    390391    # normalization: 
    391392    #    Want to display raw counts vs detector efficiency correction 
     
    401402    # 
    402403    # multiline graph: 
    403     #    How do we show/hide data parts.  E.g., data or theory, or  
     404    #    How do we show/hide data parts.  E.g., data or theory, or 
    404405    #    different polarization cross sections?  One way is with 
    405406    #    tags: each plottable has a set of tags and the tags are 
     
    420421    """ 
    421422    """ 
    422     # Short ascii name to refer to the plottable in a menu    
     423    # Short ascii name to refer to the plottable in a menu 
    423424    short_name = None 
    424425    # Fancy name 
     
    434435    interactive = True 
    435436    custom_color = None 
    436     markersize = 5 #default marker size is 'size 5' 
     437    markersize = 5  # default marker size is 'size 5' 
    437438     
    438439    def __init__(self): 
     
    441442        self._xunit = "" 
    442443        self._yaxis = "" 
    443         self._yunit = ""  
     444        self._yunit = "" 
    444445         
    445446    def __setattr__(self, name, value): 
     
    495496    def labels(cls, collection): 
    496497        """ 
    497         Construct a set of unique labels for a collection of plottables of  
     498        Construct a set of unique labels for a collection of plottables of 
    498499        the same type. 
    499500         
     
    509510            else: 
    510511                for i in xrange(len(collection)): 
    511                     map[collection[i]] = "%s %d"%(basename, i) 
     512                    map[collection[i]] = "%s %d" % (basename, i) 
    512513        return map 
     514 
    513515    ##Use the following if @classmethod doesn't work 
    514516    # labels = classmethod(labels) 
     
    526528    def set_View(self, x, y): 
    527529        """Load View""" 
    528         self.x= x 
     530        self.x = x 
    529531        self.y = y 
    530532        self.reset_view() 
     
    541543        """ 
    542544        The base class makes sure the correct units are being used for 
    543         subsequent plottable.   
    544          
    545         For now it is assumed that the graphs are commensurate, and if you  
     545        subsequent plottable. 
     546         
     547        For now it is assumed that the graphs are commensurate, and if you 
    546548        put a Qx object on a Temperature graph then you had better hope  
    547549        that it makes sense. 
     
    560562        return False 
    561563         
    562          
    563564    def colors(self): 
    564565        """Return the number of colors need to render the object""" 
     
    577578        return self.view.returnXview() 
    578579     
    579     def check_data_PlottableX(self):  
    580         """ 
    581         Since no transformation is made for log10(x), check that  
     580    def check_data_PlottableX(self): 
     581        """ 
     582        Since no transformation is made for log10(x), check that 
    582583        no negative values is plot in log scale 
    583584        """ 
    584585        self.view.check_data_logX() 
    585586         
    586     def check_data_PlottableY(self):  
     587    def check_data_PlottableY(self): 
    587588        """ 
    588589        Since no transformation is made for log10(y), check that  
    589590        no negative values is plot in log scale 
    590591        """ 
    591         self.view.check_data_logY()  
     592        self.view.check_data_logY() 
    592593         
    593594    def transformX(self, transx, transdx): 
    594595        """ 
    595         Receive pointers to function that transform x and dx  
     596        Receive pointers to function that transform x and dx 
    596597        and set corresponding View pointers 
    597598         
     
    604605    def transformY(self, transy, transdy): 
    605606        """ 
    606         Receive pointers to function that transform y and dy  
     607        Receive pointers to function that transform y and dy 
    607608        and set corresponding View pointers 
    608609         
     
    628629        """ 
    629630        self.view.onFitRangeView(xmin, xmax) 
    630          
     631     
     632     
    631633class View: 
    632634    """ 
     
    666668        :param x: array of x values 
    667669        :param y: array of y values 
    668         :param dx: array of  errors values on x  
     670        :param dx: array of  errors values on x 
    669671        :param dy: array of error values on y 
    670672         
     
    675677        has_err_y = not (dy == None or len(dy) == 0) 
    676678         
    677         if(x != None) and (y != None):  
    678             if not dx == None and not len(dx) == 0 and not len(x)== len(dx): 
     679        if(x != None) and (y != None): 
     680            if not dx == None and not len(dx) == 0 and not len(x) == len(dx): 
    679681                msg = "Plottable.View: Given x and dx are not" 
    680                 msg += " of the same length"  
     682                msg += " of the same length" 
    681683                raise ValueError, msg 
    682684            # Check length of y array 
     
    686688                raise ValueError, msg 
    687689         
    688             if not dy == None and not len(dy) == 0 and not len(y)==len(dy): 
     690            if not dy == None and not len(dy) == 0 and not len(y) == len(dy): 
    689691                msg = "Plottable.View: Given y and dy are not of the same " 
    690                 msg += "length: len(y)=%s, len(dy)=%s" %(len(y),len(dy)) 
     692                msg += "length: len(y)=%s, len(dy)=%s" % (len(y), len(dy)) 
    691693                raise ValueError, msg 
    692694            self.x = [] 
     
    708710            for i in range(len(x)): 
    709711                try: 
    710                     tempx =self.funcx(x[i],y[i]) 
    711                     tempy =self.funcy(y[i],x[i]) 
     712                    tempx = self.funcx(x[i], y[i]) 
     713                    tempy = self.funcy(y[i], x[i]) 
    712714                    if has_err_x: 
    713715                        tempdx = self.funcdx(x[i], y[i], dx[i], dy[i]) 
     
    727729            if not len(self.x) == len(self.y): 
    728730                msg = "Plottable.View: transformed x " 
    729                 msg += "and y are not of the same length"  
     731                msg += "and y are not of the same length" 
    730732                raise ValueError, msg 
    731733            if has_err_x and not (len(self.x) and len(self.dx)): 
    732734                msg = "Plottable.View: transformed x and dx" 
    733                 msg += " are not of the same length"  
     735                msg += " are not of the same length" 
    734736                raise ValueError, msg 
    735737            if has_err_y and not (len(self.y) and len(self.dy)): 
    736738                msg = "Plottable.View: transformed y" 
    737                 msg += " and dy are not of the same length"  
     739                msg += " and dy are not of the same length" 
    738740                raise ValueError, msg 
    739741            # Check that negative values are not plot on x and y axis for 
     
    750752        """ 
    751753        Reset x,y,dx and y in their full range  and in the initial scale 
    752         in case their previous range has changed   
    753          
     754        in case their previous range has changed 
    754755        """ 
    755756        self.x = self.Xreel 
     
    758759        self.dy = self.DYreel 
    759760         
    760     def setTransformX(self, funcx, funcdx):   
    761         """ 
    762         Receive pointers to function that transform x and dx  
     761    def setTransformX(self, funcx, funcdx): 
     762        """ 
     763        Receive pointers to function that transform x and dx 
    763764        and set corresponding View pointers 
    764765         
    765766        :param transx: pointer to function that transforms x 
    766767        :param transdx: pointer to function that transforms dx 
    767          
    768768        """ 
    769769        self.funcx = funcx 
     
    772772    def setTransformY(self, funcy, funcdy): 
    773773        """ 
    774         Receive pointers to function that transform y and dy  
     774        Receive pointers to function that transform y and dy 
    775775        and set corresponding View pointers 
    776776         
    777777        :param transx: pointer to function that transforms y 
    778778        :param transdx: pointer to function that transforms dy 
    779          
    780         """     
     779        """ 
    781780        self.funcy = funcy 
    782781        self.funcdy = funcdy 
     
    788787        return self.x, self.y, self.dx, self.dy 
    789788     
    790     def check_data_logX(self):  
     789    def check_data_logX(self): 
    791790        """ 
    792791        Remove negative value in x vector to avoid plotting negative 
    793792        value of Log10 
    794          
    795793        """ 
    796794        tempx = [] 
     
    802800        if self.dy == None: 
    803801            self.dy = numpy.zeros(len(self.y)) 
    804         if self.xLabel == "log10(x)" : 
     802        if self.xLabel == "log10(x)": 
    805803            for i in range(len(self.x)): 
    806804                try: 
     
    812810                except: 
    813811                    print "check_data_logX: skipping point x %g" % self.x[i] 
    814                     print sys.exc_value   
     812                    print sys.exc_value 
    815813                    pass  
    816814            self.x = tempx 
     
    819817            self.dy = tempdy 
    820818         
    821     def check_data_logY(self):  
    822         """ 
    823         Remove negative value in y vector  
     819    def check_data_logY(self): 
     820        """ 
     821        Remove negative value in y vector 
    824822        to avoid plotting negative value of Log10 
    825823         
     
    833831        if self.dy == None: 
    834832            self.dy = numpy.zeros(len(self.y)) 
    835         if (self.yLabel == "log10(y)" ): 
     833        if (self.yLabel == "log10(y)"): 
    836834            for i in range(len(self.x)): 
    837                  try: 
     835                try: 
    838836                    if (self.y[i] > 0): 
    839837                        tempx.append(self.x[i]) 
     
    841839                        tempy.append(self.y[i]) 
    842840                        tempdy.append(self.dy[i]) 
    843                  except: 
    844                     print "check_data_logY: skipping point %g" %self.y[i] 
    845                     print sys.exc_value   
     841                except: 
     842                    print "check_data_logY: skipping point %g" % self.y[i] 
     843                    print sys.exc_value 
    846844                    pass 
    847845            self.x = tempx 
     
    865863            self.dx = numpy.zeros(len(self.x)) 
    866864        if self.dy == None: 
    867             self.dy=numpy.zeros(len(self.y)) 
     865            self.dy = numpy.zeros(len(self.y)) 
    868866        if (xmin != None) and (xmax != None): 
    869867            for i in range(len(self.x)): 
     
    876874            self.y = tempy 
    877875            self.dx = tempdx 
    878             self.dy = tempdy  
    879                         
     876            self.dy = tempdy 
     877 
     878               
    880879class Data2D(Plottable): 
    881880    """ 
     
    910909        self._yunit = 'A^{-1}' 
    911910         
    912         ### might remove that later  
     911        ### might remove that later 
    913912        ## Vector of Q-values at the center of each bin in x 
    914913        self.x_bins = [] 
     
    10031002        plot.image(self.data, self.qx_data, self.qy_data, 
    10041003                   self.xmin, self.xmax, self.ymin, 
    1005                    self.ymax, self.zmin, self.zmax, **kw)     
     1004                   self.ymax, self.zmin, self.zmax, **kw) 
    10061005        
    10071006    def changed(self): 
     
    10211020            map[item] = item.label 
    10221021        return map 
     1022 
    10231023 
    10241024class Data1D(Plottable): 
     
    10601060        if self.interactive == True: 
    10611061            kw['symbol'] = self.symbol 
    1062             kw['id'] =  self.id 
     1062            kw['id'] = self.id 
    10631063            kw['hide_error'] = self.hide_error 
    10641064            kw['markersize'] = self.markersize 
    10651065            plot.interactive_points(self.view.x, self.view.y, 
    10661066                                    dx=self.view.dx, dy=self.view.dy, 
    1067               name=self.name, **kw)             
     1067              name=self.name, **kw) 
    10681068        else: 
    10691069            kw['id'] =  self.id 
     
    10721072            kw['color'] = self.custom_color 
    10731073            kw['markersize'] = self.markersize 
    1074             plot.points(self.view.x, self.view.y, dx=self.view.dx,  
     1074            plot.points(self.view.x, self.view.y, dx=self.view.dx, 
    10751075                dy=self.view.dy, marker=self.symbollist[self.symbol], **kw) 
    10761076        
     
    10891089            map[item] = item.label 
    10901090        return map 
     1091     
    10911092     
    10921093class Theory1D(Plottable): 
     
    11211122        """ 
    11221123        """ 
    1123         if self.interactive==True: 
    1124        
    1125             kw['id'] =  self.id 
     1124        if self.interactive == True: 
     1125            kw['id'] = self.id 
    11261126            plot.interactive_curve(self.view.x, self.view.y, 
    11271127                                   dy=self.view.dy, 
    11281128                                   name=self.name, **kw) 
    11291129        else: 
    1130             kw['id'] =  self.id 
     1130            kw['id'] = self.id 
    11311131            plot.curve(self.view.x, self.view.y, dy=self.view.dy, **kw) 
    11321132             
     
    11391139        map = {} 
    11401140        for item in collection: 
    1141             #map[item] = label(item, collection) 
    1142             #map[item] = r"$\rm{%s}$" % item.name 
    11431141            if item.label == "theory": 
    11441142                item.label = item.name 
     
    11751173        return self.data.changed() or self.theory.changed() 
    11761174 
     1175 
    11771176# --------------------------------------------------------------- 
    11781177class Text(Plottable): 
     
    11901189        self.ypos = ypos 
    11911190         
    1192     def render(self,plot,**kw): 
     1191    def render(self, plot, **kw): 
    11931192        """ 
    11941193        """ 
    11951194        from matplotlib import transforms 
    11961195 
    1197         xcoords=transforms.blended_transform_factory(plot.subplot.transAxes, 
    1198                                                      plot.subplot.transAxes) 
     1196        xcoords = transforms.blended_transform_factory(plot.subplot.transAxes, 
     1197                                                       plot.subplot.transAxes) 
    11991198        plot.subplot.text(self.xpos, 
    12001199                          self.ypos, 
     
    12061205    def setText(self, text): 
    12071206        """Set the text string.""" 
    1208         self.text =  text 
     1207        self.text = text 
    12091208 
    12101209    def getText(self, text): 
     
    12301229class Chisq(Plottable): 
    12311230    """ 
    1232     Chisq plottable plots the chisq  
     1231    Chisq plottable plots the chisq 
    12331232    """ 
    12341233    def __init__(self, chisq=None): 
     
    12651264        Set the chisq value. 
    12661265        """ 
    1267         self._chisq =  chisq 
     1266        self._chisq = chisq 
    12681267 
    12691268 
     
    12751274    # Construct a simple graph 
    12761275    if False: 
    1277         x = nx.array([1,2,3,4,5,6],'d') 
    1278         y = nx.array([4,5,6,5,4,5],'d') 
     1276        x = nx.array([1,2,3,4,5,6], 'd') 
     1277        y = nx.array([4,5,6,5,4,5], 'd') 
    12791278        dy = nx.array([0.2, 0.3, 0.1, 0.2, 0.9, 0.3]) 
    12801279    else: 
    1281         x = nx.linspace(0,1.,10000) 
     1280        x = nx.linspace(0, 1., 10000) 
    12821281        y = nx.sin(2 * nx.pi * x * 2.8) 
    12831282        dy = nx.sqrt(100 * nx.abs(y)) / 100 
     
    12891288    graph.add(data) 
    12901289    graph.add(Theory1D(x, y, dy=dy)) 
    1291     return graph  
     1290    return graph 
     1291 
    12921292 
    12931293def demo_plotter(graph): 
     
    13061306     
    13071307    class GraphUpdate: 
    1308         callnum=0 
     1308        callnum = 0 
     1309         
    13091310        def __init__(self, graph, plotter): 
    13101311            self.graph, self.plotter = graph, plotter 
     1312         
    13111313        def __call__(self): 
    1312             if self.graph.changed():  
     1314            if self.graph.changed(): 
    13131315                self.graph.render(self.plotter) 
    13141316                return True 
    13151317            return False 
     1318         
    13161319        def onIdle(self, event): 
    1317             #print "On Idle checker %d"%(self.callnum) 
    13181320            self.callnum = self.callnum + 1 
    13191321            if self.__call__():  
    1320                 pass # event.RequestMore() 
     1322                pass  # event.RequestMore() 
    13211323    update = GraphUpdate(graph, plotter) 
    13221324    frame.Bind(wx.EVT_IDLE, update.onIdle) 
    13231325    app.MainLoop() 
    1324  
    1325 import sys; print sys.version 
    1326 if __name__ == "__main__": 
    1327     demo_plotter(sample_graph()) 
    1328      
Note: See TracChangeset for help on using the changeset viewer.