Changeset 6d05e1d in sasview for src/sas/qtgui/PlotterBase.py


Ignore:
Timestamp:
Dec 6, 2016 5:39:24 AM (7 years ago)
Author:
Piotr Rozyczko <rozyczko@…>
Branches:
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
Children:
55d89f8
Parents:
b94889a
git-author:
Piotr Rozyczko <rozyczko@…> (12/06/16 05:38:55)
git-committer:
Piotr Rozyczko <rozyczko@…> (12/06/16 05:39:24)
Message:

More functionality for quick plots + unit tests

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/qtgui/PlotterBase.py

    ref01be4 r6d05e1d  
    1 import logging 
    2 import copy 
    3 import numpy 
    41import pylab 
    52 
    63from PyQt4 import QtGui 
    7 from PyQt4 import QtCore 
    84 
    95# TODO: Replace the qt4agg calls below with qt5 equivalent. 
     
    139from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas 
    1410from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar 
    15 from matplotlib.backend_bases import NavigationToolbar2 
    1611 
    1712import matplotlib.pyplot as plt 
     
    4540        # set the layout 
    4641        layout = QtGui.QVBoxLayout() 
     42        layout.setMargin(0) 
    4743        layout.addWidget(self.canvas) 
    4844 
    4945        # defaults 
    5046        self.current_plot = 111 
    51         self._data = [] 
     47        self._data = [] # Original 1D/2D object 
     48        self._xscale = 'log' 
     49        self._yscale = 'log' 
    5250        self.qx_data = [] 
    5351        self.qy_data = [] 
    5452        self.color=0 
    5553        self.symbol=0 
     54        self.dimension=1 
    5655        self.grid_on = False 
    5756        self.scale = 'linear' 
     57        self.x_label = "log10(x)" 
     58        self.y_label = "log10(y)" 
    5859 
    5960        # default color map 
     
    6465 
    6566        if not quickplot: 
     67            # set the layout 
    6668            layout.addWidget(self.toolbar) 
    6769            # Notify the helper 
    6870            PlotHelper.addPlot(self) 
     71            # Add the context menu 
     72            self.contextMenu() 
    6973            # Notify the listeners 
    7074            self.parent.communicator.activeGraphsSignal.emit(PlotHelper.currentPlots()) 
     
    8084    @data.setter 
    8185    def data(self, data=None): 
    82         """ data setter """ 
    83         pass 
     86        """ virtual data setter """ 
     87        raise ImportError("Data setter must be implemented in derived class.") 
    8488 
    8589    def title(self, title=""): 
    8690        """ title setter """ 
    87         self.title = title 
    88  
    89     def id(self, id=""): 
    90         """ id setter """ 
    91         self.id = id 
    92  
     91        self._title = title 
     92 
     93    #def id(self, id=""): 
     94    #    """ id setter """ 
     95    #    self.id = id 
     96 
     97    @property 
     98    def xLabel(self, xlabel=""): 
     99        """ x-label setter """ 
     100        return self.x_label 
     101 
     102    @xLabel.setter 
    93103    def xLabel(self, xlabel=""): 
    94104        """ x-label setter """ 
    95105        self.x_label = r'$%s$'% xlabel 
    96106 
     107    @property 
     108    def yLabel(self, ylabel=""): 
     109        """ y-label setter """ 
     110        return self.y_label 
     111 
     112    @yLabel.setter 
    97113    def yLabel(self, ylabel=""): 
    98114        """ y-label setter """ 
     
    102118    def yscale(self): 
    103119        """ Y-axis scale getter """ 
    104         return self.yscale 
     120        return self._yscale 
    105121 
    106122    @yscale.setter 
    107123    def yscale(self, scale='linear'): 
    108124        """ Y-axis scale setter """ 
    109         self.subplot.set_yscale(scale, nonposy='clip') 
    110         self.yscale = scale 
     125        self.ax.set_yscale(scale, nonposy='clip') 
     126        self._yscale = scale 
    111127 
    112128    @property 
    113129    def xscale(self): 
    114130        """ X-axis scale getter """ 
    115         return self.xscale 
     131        return self._xscale 
    116132 
    117133    @xscale.setter 
    118134    def xscale(self, scale='linear'): 
    119135        """ X-axis scale setter """ 
    120         self.subplot.set_xscale(scale) 
    121         self.xscale = scale 
    122  
    123     def contextMenuQuickPlot(self): 
     136        self.ax.set_xscale(scale) 
     137        self._xscale = scale 
     138 
     139    def contextMenu(self): 
    124140        """ 
    125141        Define context menu and associated actions for the MPL widget 
     
    131147        self.actionCopyToClipboard = self.contextMenu.addAction("Copy to Clipboard") 
    132148        self.contextMenu.addSeparator() 
    133         self.actionToggleGrid = self.contextMenu.addAction("Toggle Grid On/Off") 
    134         self.contextMenu.addSeparator() 
    135         self.actionChangeScale = self.contextMenu.addAction("Change Scale") 
    136149 
    137150        # Define the callbacks 
     
    139152        self.actionPrintImage.triggered.connect(self.onImagePrint) 
    140153        self.actionCopyToClipboard.triggered.connect(self.onClipboardCopy) 
    141         self.actionToggleGrid.triggered.connect(self.onGridToggle) 
    142         self.actionChangeScale.triggered.connect(self.onScaleChange) 
     154 
     155    def contextMenuQuickPlot(self): 
     156        """ 
     157        Define context menu and associated actions for the quickplot MPL widget 
     158        """ 
     159        raise ImportError("Context menu method must be implemented in derived class.") 
    143160 
    144161    def contextMenuEvent(self, event): 
     
    217234        self.ax.grid(self.grid_on) 
    218235        self.canvas.draw_idle() 
    219  
    220     def onScaleChange(self): 
    221         """ 
    222         Show a dialog allowing axes rescaling 
    223         """ 
    224         if self.properties.exec_() == QtGui.QDialog.Accepted: 
    225             xLabel, yLabel = self.properties.getValues() 
    226             #self.xyTransform(xLabel, yLabel) 
    227  
    228     def xyTransform(self, xLabel="", yLabel=""): 
    229         """ 
    230         Transforms x and y in View and set the scale 
    231         """ 
    232         # The logic should be in the right order 
    233         self.ly = None 
    234         self.q_ctrl = None 
    235         # Changing the scale might be incompatible with 
    236         # currently displayed data (for instance, going 
    237         # from ln to log when all plotted values have 
    238         # negative natural logs). 
    239         # Go linear and only change the scale at the end. 
    240         self.set_xscale("linear") 
    241         self.set_yscale("linear") 
    242         _xscale = 'linear' 
    243         _yscale = 'linear' 
    244         # Local data is either 1D or 2D 
    245         #for item in list: 
    246         #if item.id == 'fit': 
    247         #    continue 
    248         item.setLabel(self.xLabel, self.yLabel) 
    249         # control axis labels from the panel itself 
    250         yname, yunits = item.get_yaxis() 
    251         if self.yaxis_label != None: 
    252             yname = self.yaxis_label 
    253             yunits = self.yaxis_unit 
    254         else: 
    255             self.yaxis_label = yname 
    256             self.yaxis_unit = yunits 
    257         xname, xunits = item.get_xaxis() 
    258         if self.xaxis_label != None: 
    259             xname = self.xaxis_label 
    260             xunits = self.xaxis_unit 
    261         else: 
    262             self.xaxis_label = xname 
    263             self.xaxis_unit = xunits 
    264         # Goes through all possible scales 
    265         if self.xLabel == "x": 
    266             item.transformX(transform.toX, transform.errToX) 
    267             self.graph._xaxis_transformed("%s" % xname, "%s" % xunits) 
    268         if self.xLabel == "x^(2)": 
    269             item.transformX(transform.toX2, transform.errToX2) 
    270             xunits = convert_unit(2, xunits) 
    271             self.graph._xaxis_transformed("%s^{2}" % xname, "%s" % xunits) 
    272         if self.xLabel == "x^(4)": 
    273             item.transformX(transform.toX4, transform.errToX4) 
    274             xunits = convert_unit(4, xunits) 
    275             self.graph._xaxis_transformed("%s^{4}" % xname, "%s" % xunits) 
    276         if self.xLabel == "ln(x)": 
    277             item.transformX(transform.toLogX, transform.errToLogX) 
    278             self.graph._xaxis_transformed("\ln{(%s)}" % xname, "%s" % xunits) 
    279         if self.xLabel == "log10(x)": 
    280             item.transformX(transform.toX_pos, transform.errToX_pos) 
    281             _xscale = 'log' 
    282             self.graph._xaxis_transformed("%s" % xname, "%s" % xunits) 
    283         if self.xLabel == "log10(x^(4))": 
    284             item.transformX(transform.toX4, transform.errToX4) 
    285             xunits = convert_unit(4, xunits) 
    286             self.graph._xaxis_transformed("%s^{4}" % xname, "%s" % xunits) 
    287             _xscale = 'log' 
    288         if self.yLabel == "ln(y)": 
    289             item.transformY(transform.toLogX, transform.errToLogX) 
    290             self.graph._yaxis_transformed("\ln{(%s)}" % yname, "%s" % yunits) 
    291         if self.yLabel == "y": 
    292             item.transformY(transform.toX, transform.errToX) 
    293             self.graph._yaxis_transformed("%s" % yname, "%s" % yunits) 
    294         if self.yLabel == "log10(y)": 
    295             item.transformY(transform.toX_pos, transform.errToX_pos) 
    296             _yscale = 'log' 
    297             self.graph._yaxis_transformed("%s" % yname, "%s" % yunits) 
    298         if self.yLabel == "y^(2)": 
    299             item.transformY(transform.toX2, transform.errToX2) 
    300             yunits = convert_unit(2, yunits) 
    301             self.graph._yaxis_transformed("%s^{2}" % yname, "%s" % yunits) 
    302         if self.yLabel == "1/y": 
    303             item.transformY(transform.toOneOverX, transform.errOneOverX) 
    304             yunits = convert_unit(-1, yunits) 
    305             self.graph._yaxis_transformed("1/%s" % yname, "%s" % yunits) 
    306         if self.yLabel == "y*x^(2)": 
    307             item.transformY(transform.toYX2, transform.errToYX2) 
    308             xunits = convert_unit(2, self.xaxis_unit) 
    309             self.graph._yaxis_transformed("%s \ \ %s^{2}" % (yname, xname), 
    310                                             "%s%s" % (yunits, xunits)) 
    311         if self.yLabel == "y*x^(4)": 
    312             item.transformY(transform.toYX4, transform.errToYX4) 
    313             xunits = convert_unit(4, self.xaxis_unit) 
    314             self.graph._yaxis_transformed("%s \ \ %s^{4}" % (yname, xname), 
    315                                             "%s%s" % (yunits, xunits)) 
    316         if self.yLabel == "1/sqrt(y)": 
    317             item.transformY(transform.toOneOverSqrtX, 
    318                             transform.errOneOverSqrtX) 
    319             yunits = convert_unit(-0.5, yunits) 
    320             self.graph._yaxis_transformed("1/\sqrt{%s}" % yname, 
    321                                             "%s" % yunits) 
    322         if self.yLabel == "ln(y*x)": 
    323             item.transformY(transform.toLogXY, transform.errToLogXY) 
    324             self.graph._yaxis_transformed("\ln{(%s \ \ %s)}" % (yname, xname), 
    325                                             "%s%s" % (yunits, self.xaxis_unit)) 
    326         if self.yLabel == "ln(y*x^(2))": 
    327             item.transformY(transform.toLogYX2, transform.errToLogYX2) 
    328             xunits = convert_unit(2, self.xaxis_unit) 
    329             self.graph._yaxis_transformed("\ln (%s \ \ %s^{2})" % (yname, xname), 
    330                                             "%s%s" % (yunits, xunits)) 
    331         if self.yLabel == "ln(y*x^(4))": 
    332             item.transformY(transform.toLogYX4, transform.errToLogYX4) 
    333             xunits = convert_unit(4, self.xaxis_unit) 
    334             self.graph._yaxis_transformed("\ln (%s \ \ %s^{4})" % (yname, xname), 
    335                                             "%s%s" % (yunits, xunits)) 
    336         if self.yLabel == "log10(y*x^(4))": 
    337             item.transformY(transform.toYX4, transform.errToYX4) 
    338             xunits = convert_unit(4, self.xaxis_unit) 
    339             _yscale = 'log' 
    340             self.graph._yaxis_transformed("%s \ \ %s^{4}" % (yname, xname), 
    341                                             "%s%s" % (yunits, xunits)) 
    342             item.transformView() 
    343  
    344         # set new label and units 
    345         yname = self.graph.prop["ylabel"] 
    346         yunits = '' 
    347         xname = self.graph.prop["xlabel"] 
    348         xunits = '' 
    349  
    350         self.resetFitView() 
    351         self.prevXtrans = self.xLabel 
    352         self.prevYtrans = self.yLabel 
    353         self.graph.render(self) 
    354         self.set_xscale(_xscale) 
    355         self.set_yscale(_yscale) 
    356  
    357         self.xaxis(xname, xunits, self.xaxis_font, 
    358                    self.xaxis_color, self.xaxis_tick) 
    359         self.yaxis(yname, yunits, self.yaxis_font, 
    360                    self.yaxis_color, self.yaxis_tick) 
    361         self.subplot.texts = self.textList 
    362  
    363         self.canvas.draw_idle() 
Note: See TracChangeset for help on using the changeset viewer.