Changeset 64f1e93 in sasview


Ignore:
Timestamp:
Dec 7, 2016 5:47:22 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:
416fa8f
Parents:
55d89f8
Message:

2D plots: more unit tests + minor cleanup

Location:
src/sas/qtgui
Files:
6 edited

Legend:

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

    r0fc37fea r64f1e93  
    99from UnitTesting import MainWindowTest 
    1010from UnitTesting import TestUtilsTest 
     11from UnitTesting import PlotHelperTest 
     12from UnitTesting import PlotterTest 
     13from UnitTesting import Plotter2DTest 
     14from UnitTesting import SasviewLoggerTest 
     15from UnitTesting import ScalePropertiesTest 
     16from UnitTesting import KiessigCalculatorTest 
     17from UnitTesting import DensityCalculatorTest 
    1118 
    1219def suite(): 
    1320    suites = ( 
    14         unittest.makeSuite(AboutBoxTest.AboutBoxTest,         'test'), 
    15         unittest.makeSuite(DataExplorerTest.DataExplorerTest, 'test'), 
    16         unittest.makeSuite(WelcomePanelTest.WelcomePanelTest, 'test'), 
     21        unittest.makeSuite(AboutBoxTest.AboutBoxTest,          'test'), 
     22        unittest.makeSuite(DataExplorerTest.DataExplorerTest,  'test'), 
     23        unittest.makeSuite(WelcomePanelTest.WelcomePanelTest,  'test'), 
    1724        unittest.makeSuite(DroppableDataLoadWidgetTest.DroppableDataLoadWidgetTest, 'test'), 
    18         unittest.makeSuite(GuiManagerTest.GuiManagerTest,     'test'), 
    19         unittest.makeSuite(GuiUtilsTest.GuiUtilsTest,         'test'), 
    20         unittest.makeSuite(MainWindowTest.MainWindowTest,     'test'), 
    21         unittest.makeSuite(TestUtilsTest.TestUtilsTest,       'test'), 
     25        unittest.makeSuite(GuiManagerTest.GuiManagerTest,      'test'), 
     26        unittest.makeSuite(GuiUtilsTest.GuiUtilsTest,          'test'), 
     27        unittest.makeSuite(MainWindowTest.MainWindowTest,      'test'), 
     28        unittest.makeSuite(TestUtilsTest.TestUtilsTest,        'test'), 
     29        unittest.makeSuite(PlotHelperTest.PlotHelperTest,       'test'), 
     30        unittest.makeSuite(PlotterTest.PlotterTest,          'test'), 
     31        unittest.makeSuite(Plotter2DTest.Plotter2DTest,        'test'), 
     32        unittest.makeSuite(SasviewLoggerTest.SasviewLoggerTest,    'test'), 
     33        unittest.makeSuite(ScalePropertiesTest.ScalePropertiesTest,  'test'), 
     34        unittest.makeSuite(KiessigCalculatorTest.KiessigCalculatorTest, 'test'), 
     35        unittest.makeSuite(DensityCalculatorTest.DensityCalculatorTest, 'test'), 
    2236    ) 
    2337    return unittest.TestSuite(suites) 
  • src/sas/qtgui/Plotter2D.py

    r55d89f8 r64f1e93  
    99import sas.qtgui.PlotUtilities as PlotUtilities 
    1010from sas.qtgui.PlotterBase import PlotterBase 
     11from mpl_toolkits.mplot3d import Axes3D 
    1112 
    1213class Plotter2D(PlotterBase): 
     
    4041        Plot 2D self._data 
    4142        """ 
    42         # create an axis object 
     43        # Toggle the scale 
    4344        zmin_2D_temp = self.zmin 
    4445        zmax_2D_temp = self.zmax 
     
    6061                zmax_2D_temp = numpy.log10(self.zmax) 
    6162 
    62         self.image(data=self.data.data, qx_data=self.qx_data, 
    63                    qy_data=self.qy_data, xmin=self.xmin, 
    64                    xmax=self.xmax, 
    65                    ymin=self.ymin, ymax=self.ymax, 
    66                    cmap=self.cmap, zmin=zmin_2D_temp, 
    67                    zmax=zmax_2D_temp) 
     63        # Prepare and show the plot 
     64        self.showPlot(data=self.data.data, 
     65                      qx_data=self.qx_data, 
     66                      qy_data=self.qy_data, 
     67                      xmin=self.xmin, 
     68                      xmax=self.xmax, 
     69                      ymin=self.ymin, ymax=self.ymax, 
     70                      cmap=self.cmap, zmin=zmin_2D_temp, 
     71                      zmax=zmax_2D_temp) 
    6872 
    6973    def contextMenuQuickPlot(self): 
     
    96100        self.plot() 
    97101 
    98     def image(self, data, qx_data, qy_data, xmin, xmax, ymin, ymax, 
    99               zmin, zmax, color=0, symbol=0, markersize=0, 
    100               label='data2D', cmap=DEFAULT_CMAP): 
     102    def showPlot(self, data, qx_data, qy_data, xmin, xmax, ymin, ymax, 
     103                 zmin, zmax, color=0, symbol=0, markersize=0, 
     104                 label='data2D', cmap=DEFAULT_CMAP): 
    101105        """ 
    102         Render the current data 
     106        Render and show the current data 
    103107        """ 
    104108        self.qx_data = qx_data 
     
    152156        else: 
    153157            # clear the previous 2D from memory 
    154             # mpl is not clf, so we do 
    155158            self.figure.clear() 
    156159 
     
    161164            X, Y = numpy.meshgrid(X, Y) 
    162165 
    163             try: 
    164                 # mpl >= 1.0.0 
    165                 ax = self.figure.gca(projection='3d') 
    166                 cbax = self.figure.add_axes([0.84, 0.1, 0.02, 0.8]) 
    167                 if len(X) > 60: 
    168                     ax.disable_mouse_rotation() 
    169             except: 
    170                 # mpl < 1.0.0 
    171                 try: 
    172                     from mpl_toolkits.mplot3d import Axes3D 
    173                 except: 
    174                     logging.error("PlotPanel could not import Axes3D") 
    175                 self.figure.clear() 
    176                 ax = Axes3D(self.figure) 
    177                 if len(X) > 60: 
    178                     ax.cla() 
    179                 cbax = None 
     166            ax = Axes3D(self.figure) 
     167            cbax = self.figure.add_axes([0.84, 0.1, 0.02, 0.8]) 
     168            # Disable rotation for large sets. 
     169            # TODO: Define "large" for a dataset 
     170            SET_TOO_LARGE = 500 
     171            if len(X) > SET_TOO_LARGE: 
     172                ax.disable_mouse_rotation() 
     173 
    180174            self.figure.canvas.resizing = False 
    181175            im = ax.plot_surface(X, Y, output, rstride=1, cstride=1, cmap=cmap, 
     
    190184        cb.update_bruteforce(im) 
    191185        cb.set_label('$' + self.scale + '$') 
     186 
    192187        if self.dimension != 3: 
    193188            self.figure.canvas.draw_idle() 
  • src/sas/qtgui/PlotterBase.py

    r55d89f8 r64f1e93  
    9090        self._title = title 
    9191 
    92     #def id(self, id=""): 
    93     #    """ id setter """ 
    94     #    self.id = id 
    95  
    9692    @property 
    9793    def xLabel(self, xlabel=""): 
  • src/sas/qtgui/UnitTesting/GuiManagerTest.py

    r31c5b58 r64f1e93  
    119119        Test that the custom exit method is called on shutdown 
    120120        """ 
     121        self.manager._workspace.show() 
     122 
    121123        # Must mask sys.exit, otherwise the whole testing process stops. 
    122124        sys.exit = MagicMock() 
  • src/sas/qtgui/UnitTesting/Plotter2DTest.py

    r55d89f8 r64f1e93  
    66from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas 
    77from mock import MagicMock 
     8from mpl_toolkits.mplot3d import Axes3D 
    89 
    910####### TEMP 
     
    5455        self.plotter.data = self.data 
    5556        self.plotter.show() 
    56         FigureCanvas.draw = MagicMock() 
     57        FigureCanvas.draw_idle = MagicMock() 
    5758 
    5859        self.plotter.plot() 
    5960 
    60         #self.assertTrue(FigureCanvas.draw.called) 
     61        self.assertTrue(FigureCanvas.draw_idle.called) 
     62 
     63    def testOnToggleScale(self): 
     64        """ Respond to the event by replotting """ 
     65        self.plotter.data = self.data 
     66        self.plotter.show() 
     67        FigureCanvas.draw_idle = MagicMock() 
     68 
     69        self.plotter.onToggleScale(None) 
     70 
     71        self.assertTrue(FigureCanvas.draw_idle.called) 
    6172 
    6273    def testContextMenuQuickPlot(self): 
     
    99110        # Trigger Change Scale and make sure the method is called 
    100111        self.assertEqual(actions[6].text(), "Toggle Linear/Log Scale") 
     112        FigureCanvas.draw_idle = MagicMock() 
     113        actions[6].trigger() 
     114        self.assertTrue(FigureCanvas.draw_idle.called) 
     115 
     116    def testShowNoPlot(self): 
     117        """ Test the plot rendering and generation """ 
     118 
     119        FigureCanvas.draw_idle = MagicMock() 
    101120        FigureCanvas.draw = MagicMock() 
    102         actions[6].trigger() 
    103         #self.assertTrue(FigureCanvas.draw.called) 
     121 
     122        # Test early return on no data 
     123        self.plotter.showPlot(data=None, 
     124                              qx_data=self.data.qx_data, 
     125                              qy_data=self.data.qy_data, 
     126                              xmin=self.data.xmin, 
     127                              xmax=self.data.xmax, 
     128                              ymin=self.data.ymin, ymax=self.data.ymax, 
     129                              cmap=None, zmin=None, 
     130                              zmax=None) 
     131 
     132        self.assertFalse(FigureCanvas.draw_idle.called) 
     133        self.assertFalse(FigureCanvas.draw.called) 
     134 
     135    def testShow3DPlot(self): 
     136        """ Test the 3Dplot rendering and generation """ 
     137        # Test 3D printout 
     138        FigureCanvas.draw = MagicMock() 
     139        Axes3D.plot_surface = MagicMock() 
     140        self.plotter.figure.colorbar = MagicMock() 
     141 
     142        self.plotter.dimension = 3 
     143        self.plotter.data = self.data 
     144        self.plotter.showPlot(data=self.plotter.data.data, 
     145                              qx_data=self.data.qx_data, 
     146                              qy_data=self.data.qy_data, 
     147                              xmin=self.data.xmin, 
     148                              xmax=self.data.xmax, 
     149                              ymin=self.data.ymin, ymax=self.data.ymax, 
     150                              cmap=None, zmin=None, 
     151                              zmax=None) 
     152        self.assertTrue(Axes3D.plot_surface.called) 
     153        self.assertTrue(FigureCanvas.draw.called) 
     154 
     155    def testShow2DPlot(self): 
     156        """ Test the 2Dplot rendering and generation """ 
     157        # Test 2D printout 
     158        FigureCanvas.draw_idle = MagicMock() 
     159        self.plotter.figure.colorbar = MagicMock() 
     160 
     161        self.plotter.dimension = 2 
     162        self.plotter.data = self.data 
     163        self.plotter.showPlot(data=self.plotter.data.data, 
     164                              qx_data=self.data.qx_data, 
     165                              qy_data=self.data.qy_data, 
     166                              xmin=self.data.xmin, 
     167                              xmax=self.data.xmax, 
     168                              ymin=self.data.ymin, ymax=self.data.ymax, 
     169                              cmap=None, zmin=None, 
     170                              zmax=None) 
     171        self.assertTrue(FigureCanvas.draw_idle.called) 
    104172 
    105173 
  • src/sas/qtgui/UnitTesting/PlotterTest.py

    r6d05e1d r64f1e93  
    100100        self.plotter.data = self.data 
    101101 
    102         self.plotter.xyTransform(xLabel="ln(x)", yLabel="ln(y)") 
    103         self.assertEqual(self.plotter.ax.get_xlabel(), "$\\ln{()}()$") 
     102        self.plotter.xyTransform(xLabel="x", yLabel="y") 
     103        self.assertEqual(self.plotter.ax.get_xlabel(), "$()$") 
     104        self.assertEqual(self.plotter.ax.get_ylabel(), "$()$") 
     105 
     106        self.plotter.xyTransform(xLabel="x^(2)", yLabel="1/y") 
     107        self.assertEqual(self.plotter.ax.get_xlabel(), "$^{2}(()^{2})$") 
     108        self.assertEqual(self.plotter.ax.get_ylabel(), "$1/(()^{-1})$") 
     109 
     110        self.plotter.xyTransform(xLabel="x^(4)", yLabel="ln(y)") 
     111        self.assertEqual(self.plotter.ax.get_xlabel(), "$^{4}(()^{4})$") 
    104112        self.assertEqual(self.plotter.ax.get_ylabel(), "$\\ln{()}()$") 
    105113 
    106         self.plotter.xyTransform(xLabel="ln(x)", yLabel="ln(y)") 
     114        self.plotter.xyTransform(xLabel="ln(x)", yLabel="y^(2)") 
    107115        self.assertEqual(self.plotter.ax.get_xlabel(), "$\\ln{()}()$") 
    108         self.assertEqual(self.plotter.ax.get_ylabel(), "$\\ln{()}()$") 
     116        self.assertEqual(self.plotter.ax.get_ylabel(), "$^{2}(()^{2})$") 
    109117 
    110         self.plotter.xyTransform(xLabel="x^(2)", yLabel="1/sqrt(y)") 
    111         self.assertEqual(self.plotter.ax.get_xlabel(), "$^{2}(()^{2})$") 
     118        self.plotter.xyTransform(xLabel="log10(x)", yLabel="y*x^(2)") 
     119        self.assertEqual(self.plotter.ax.get_xlabel(), "$()$") 
     120        self.assertEqual(self.plotter.ax.get_ylabel(), "$ \\ \\ ^{2}(()^{2})$") 
     121 
     122        self.plotter.xyTransform(xLabel="log10(x^(4))", yLabel="y*x^(4)") 
     123        self.assertEqual(self.plotter.ax.get_xlabel(), "$^{4}(()^{4})$") 
     124        self.assertEqual(self.plotter.ax.get_ylabel(), "$ \\ \\ ^{4}(()^{16})$") 
     125 
     126        self.plotter.xyTransform(xLabel="x", yLabel="1/sqrt(y)") 
    112127        self.assertEqual(self.plotter.ax.get_ylabel(), "$1/\\sqrt{}(()^{-0.5})$") 
     128 
     129        self.plotter.xyTransform(xLabel="x", yLabel="log10(y)") 
     130        self.assertEqual(self.plotter.ax.get_ylabel(), "$()$") 
     131 
     132        self.plotter.xyTransform(xLabel="x", yLabel="ln(y*x)") 
     133        self.assertEqual(self.plotter.ax.get_ylabel(), "$\\ln{( \\ \\ )}()$") 
     134 
     135        self.plotter.xyTransform(xLabel="x", yLabel="ln(y*x^(2))") 
     136        self.assertEqual(self.plotter.ax.get_ylabel(), "$\\ln ( \\ \\ ^{2})(()^{2})$") 
     137 
     138        self.plotter.xyTransform(xLabel="x", yLabel="ln(y*x^(4))") 
     139        self.assertEqual(self.plotter.ax.get_ylabel(), "$\\ln ( \\ \\ ^{4})(()^{4})$") 
     140 
     141        self.plotter.xyTransform(xLabel="x", yLabel="log10(y*x^(4))") 
     142        self.assertEqual(self.plotter.ax.get_ylabel(), "$ \\ \\ ^{4}(()^{4})$") 
    113143 
    114144if __name__ == "__main__": 
Note: See TracChangeset for help on using the changeset viewer.