Changeset 811bec1 in sasview for src


Ignore:
Timestamp:
Mar 17, 2017 4:22:23 AM (8 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:
4d457df
Parents:
cd31251
Message:

Started with unit tests for fitting widget SASVIEW-499

Location:
src/sas
Files:
1 added
6 edited

Legend:

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

    r57b7ee2 r811bec1  
    2828from UnitTesting import SlicerParametersTest 
    2929 
     30# Perspectives 
     31import path_prepare 
     32from Perspectives.Fitting.UnitTesting import FittingWidgetTest 
     33from Perspectives.Fitting.UnitTesting import FittingPerspectiveTest 
     34 
     35 
    3036def suite(): 
    3137    suites = ( 
     
    5662        unittest.makeSuite(SlicerModelTest.SlicerModelTest, 'test'), 
    5763        unittest.makeSuite(SlicerParametersTest.SlicerParametersTest, 'test'), 
     64        unittest.makeSuite(FittingPerspectiveTest.FittingPerspectiveTest, 'test'), 
     65        unittest.makeSuite(FittingWidgetTest.FittingWidgetTest, 'test'), 
    5866    ) 
    5967    return unittest.TestSuite(suites) 
  • src/sas/qtgui/GuiManager.py

    rcbcdd2c r811bec1  
    182182            self._current_perspective.close() 
    183183        # Default perspective 
    184         self._current_perspective = Perspectives.PERSPECTIVES[str(perspective_name)](manager=self, parent=self) 
     184        self._current_perspective = Perspectives.PERSPECTIVES[str(perspective_name)](parent=self) 
    185185        self._workspace.workspace.addWindow(self._current_perspective) 
    186186        self._current_perspective.show() 
  • src/sas/qtgui/Perspectives/Fitting/FittingPerspective.py

    rcbcdd2c r811bec1  
    1313    """ 
    1414    name = "Fitting" # For displaying in the combo box in DataExplorer 
    15     def __init__(self, manager=None, parent=None, data=None): 
     15    def __init__(self, parent=None, data=None): 
    1616        super(FittingWindow, self).__init__() 
    1717 
    18         self.manager = manager 
    1918        self.parent = parent 
    2019        self._data = data 
     
    3534        self.setTabsClosable(True) 
    3635 
     36        self.communicate = self.parent.communicator() 
     37 
    3738        # Initialize the first tab 
    3839        self.addFit(None) 
     
    4344        self.setWindowTitle('Fit panel - Active Fitting Optimizer: %s' % self.optimizer) 
    4445 
    45         #self.communicate = GuiUtils.Communicate() 
    46         self.communicate = self.parent.communicator() 
    47  
    4846    def addFit(self, data): 
    4947        """ 
    5048        Add a new tab for passed data 
    5149        """ 
    52         tab     = FittingWidget(manager=self.manager, parent=self.parent, data=data, id=self.maxIndex+1) 
     50        tab     = FittingWidget(parent=self.parent, data=data, id=self.maxIndex+1) 
    5351        self.tabs.append(tab) 
    5452        self.maxIndex += 1 
  • src/sas/qtgui/Perspectives/Fitting/FittingWidget.py

    rcd31251 r811bec1  
    3838    Main widget for selecting form and structure factor models 
    3939    """ 
    40     def __init__(self, manager=None, parent=None, data=None, id=1): 
    41         """ 
    42  
    43         :param manager: 
    44         :param parent: 
    45         :return: 
    46         """ 
     40    def __init__(self, parent=None, data=None, id=1): 
     41 
    4742        super(FittingWidget, self).__init__() 
    4843 
     
    6964        self.tab_id = id 
    7065 
    71         # Parameters 
     66        # Range parameters 
    7267        self.q_range_min = QMIN_DEFAULT 
    7368        self.q_range_max = QMAX_DEFAULT 
    7469        self.npts = NPTS_DEFAULT 
     70 
    7571        # Main Data[12]D holder 
    7672        self._data = None 
     
    8884        self._magnet_model = QtGui.QStandardItemModel() 
    8985 
    90         # Set the proxy models for display 
    91         #   Main display 
    92         self._model_proxy = QtGui.QSortFilterProxyModel() 
    93         self._model_proxy.setSourceModel(self._model_model) 
    94         #self._model_proxy.setFilterRegExp(r"[^()]") 
    95  
    96         #   Proxy display 
    97         self._poly_proxy = QtGui.QSortFilterProxyModel() 
    98         self._poly_proxy.setSourceModel(self._poly_model) 
    99         self._poly_proxy.setFilterRegExp(r"[^()]") 
    100  
    101         #   Magnetism display 
    102         self._magnet_proxy = QtGui.QSortFilterProxyModel() 
    103         self._magnet_proxy.setSourceModel(self._magnet_model) 
    104         #self._magnet_proxy.setFilterRegExp(r"[^()]") 
    105  
    10686        # Param model displayed in param list 
    10787        self.lstParams.setModel(self._model_model) 
    108         #self.lstParams.setModel(self._model_proxy) 
    10988        self.readCategoryInfo() 
    11089        self.model_parameters = None 
     
    11291 
    11392        # Poly model displayed in poly list 
    114         self.lstPoly.setModel(self._poly_proxy) 
     93        self.lstPoly.setModel(self._poly_model) 
    11594        self.setPolyModel() 
    11695        self.setTableProperties(self.lstPoly) 
     
    251230        structure_factors = ["None"] 
    252231        self.cbStructureFactor.clear() 
    253         for (structure_factor, _) in structure_factor_list: 
    254             structure_factors.append(structure_factor) 
     232        structure_factors = [factor[0] for factor in structure_factor_list] 
    255233        self.cbStructureFactor.addItems(sorted(structure_factors)) 
    256234 
     
    365343        If so, returns the name of the counter parameter and the number of shells 
    366344        """ 
    367         iter_param = "" 
    368         iter_length = 0 
    369  
    370345        iter_params = self.getIterParams(model) 
    371         # pull out the iterator parameter name and length 
    372         if iter_params: 
    373             iter_length = iter_params[0].length 
    374             iter_param = iter_params[0].length_control 
    375         return (iter_param, iter_length) 
     346        # return the iterator parameter name and length 
     347        return (iter_params[0].length_control if iter_params else "", 
     348                iter_params[0].length if iter_params else 0) 
    376349 
    377350    def addBackgroundToModel(self, model): 
     
    392365 
    393366    def addCheckedListToModel(self, model, param_list): 
     367        """ 
     368        Add a QItem to model. Makes the QItem checkable 
     369        """ 
    394370        assert isinstance(model, QtGui.QStandardItemModel) 
    395371        item_list = [QtGui.QStandardItem(item) for item in param_list] 
  • src/sas/qtgui/Perspectives/Fitting/UnitTesting/FittingPerspectiveTest.py

    • Property mode changed from 100644 to 100755
    r6ee82dd r811bec1  
    11import sys 
    22import unittest 
     3import webbrowser 
    34 
    45from PyQt4 import QtGui 
     
    78from mock import MagicMock 
    89 
     10# set up import paths 
     11import sas.qtgui.path_prepare 
     12 
    913# Local 
    10 from FittingPerspective import FittingWindow 
     14import sas.qtgui.GuiUtils as GuiUtils 
     15from sas.sasgui.guiframe.dataFitting import Data1D 
     16 
     17from sas.qtgui.Perspectives.Fitting.FittingPerspective import FittingWindow 
    1118 
    1219app = QtGui.QApplication(sys.argv) 
    1320 
    1421class FittingPerspectiveTest(unittest.TestCase): 
    15     """Test the Main Window GUI""" 
     22    '''Test the Fitting Perspective''' 
    1623    def setUp(self): 
    17         """Create the GUI""" 
     24        class dummy_manager(object): 
     25            def communicator(self): 
     26                return GuiUtils.Communicate() 
     27            def communicate(self): 
     28                return GuiUtils.Communicate() 
    1829 
    19         self.widget = FittingWindow(None) 
     30        '''Create the perspective''' 
     31        self.widget = FittingWindow(dummy_manager()) 
    2032 
    2133    def tearDown(self): 
    22         """Destroy the GUI""" 
     34        '''Destroy the perspective''' 
    2335        self.widget.close() 
    2436        self.widget = None 
    2537 
    2638    def testDefaults(self): 
    27         """Test the GUI in its default state""" 
     39        '''Test the GUI in its default state''' 
    2840        self.assertIsInstance(self.widget, QtGui.QWidget) 
    29         self.assertEqual(self.widget.windowTitle(), "Fitting") 
    30         self.assertEqual(self.widget.sizePolicy().Policy(), QtGui.QSizePolicy.Fixed) 
     41        self.assertIn("Fit panel", self.widget.windowTitle()) 
     42        self.assertEqual(self.widget.optimizer, "DREAM") 
     43        self.assertEqual(len(self.widget.tabs), 1) 
     44        self.assertEqual(self.widget.maxIndex, 1) 
     45        self.assertEqual(self.widget.tabName(), "FitPage1") 
    3146 
    32     def testSelectCategory(self): 
    33         """ 
    34         Test if categories have been load properly 
    35         :return: 
    36         """ 
    37         fittingWindow =  FittingWindow(None) 
     47    def testAddTab(self): 
     48        '''Add a tab and test it''' 
    3849 
    39         #Test loading from json categories 
    40         category_list = fittingWindow.master_category_dict.keys() 
    41         self.assertTrue("Cylinder" in category_list) 
    42         self.assertTrue("Ellipsoid" in category_list) 
    43         self.assertTrue("Lamellae" in category_list) 
    44         self.assertTrue("Paracrystal" in category_list) 
    45         self.assertTrue("Parallelepiped" in category_list) 
    46         self.assertTrue("Shape Independent" in category_list) 
    47         self.assertTrue("Sphere" in category_list) 
     50        # Add an empty tab 
     51        self.widget.addFit(None) 
     52        self.assertEqual(len(self.widget.tabs), 2) 
     53        self.assertEqual(self.widget.tabName(), "FitPage2") 
     54        self.assertEqual(self.widget.maxIndex, 2) 
    4855 
    49         #Test for existence in combobox 
    50         self.assertNotEqual(fittingWindow.cbCategory.findText("Cylinder"),-1) 
    51         self.assertNotEqual(fittingWindow.cbCategory.findText("Ellipsoid"),-1) 
    52         self.assertNotEqual(fittingWindow.cbCategory.findText("Lamellae"),-1) 
    53         self.assertNotEqual(fittingWindow.cbCategory.findText("Paracrystal"),-1) 
    54         self.assertNotEqual(fittingWindow.cbCategory.findText("Parallelepiped"),-1) 
    55         self.assertNotEqual(fittingWindow.cbCategory.findText("Shape Independent"),-1) 
    56         self.assertNotEqual(fittingWindow.cbCategory.findText("Sphere"),-1) 
     56    def testCloseTab(self): 
     57        '''Delete a tab and test''' 
     58        # Add an empty tab 
     59        self.widget.addFit(None) 
    5760 
    58         #Test what is current text in the combobox 
    59         self.assertTrue(fittingWindow.cbCategory.currentText(), "Cylinder") 
     61        # Remove the original tab 
     62        self.widget.tabCloses(1) 
     63        self.assertEqual(len(self.widget.tabs), 1) 
     64        self.assertEqual(self.widget.maxIndex, 2) 
     65        self.assertEqual(self.widget.tabName(), "FitPage2") 
    6066 
    61     def testSelectModel(self): 
    62         """ 
    63         Test if models have been loaded properly 
    64         :return: 
    65         """ 
    66         fittingWindow =  FittingWindow(None) 
     67        # Attemtp to remove the last tab 
     68        self.widget.tabCloses(1) 
     69        # The tab should still be there 
     70        self.assertEqual(len(self.widget.tabs), 1) 
     71        self.assertEqual(self.widget.maxIndex, 2) 
     72        self.assertEqual(self.widget.tabName(), "FitPage2") 
    6773 
    68         #Test loading from json categories 
    69         model_list = fittingWindow.master_category_dict["Cylinder"] 
    70         self.assertTrue(['cylinder', True] in model_list) 
    71         self.assertTrue(['core_shell_cylinder', True] in model_list) 
    72         self.assertTrue(['barbell', True] in model_list) 
    73         self.assertTrue(['core_shell_bicelle', True] in model_list) 
    74         self.assertTrue(['flexible_cylinder', True] in model_list) 
    75         self.assertTrue(['flexible_cylinder_elliptical', True] in model_list) 
    76         self.assertTrue(['pearl_necklace', True] in model_list) 
    77         self.assertTrue(['capped_cylinder', True] in model_list) 
    78         self.assertTrue(['elliptical_cylinder', True] in model_list) 
    79         self.assertTrue(['pringle', True] in model_list) 
    80         self.assertTrue(['hollow_cylinder', True] in model_list) 
    81         self.assertTrue(['core_shell_bicelle_elliptical', True] in model_list) 
    82         self.assertTrue(['stacked_disks', True] in model_list) 
     74    def testAllowBatch(self): 
     75        '''Assure the perspective allows multiple datasets''' 
     76        self.assertTrue(self.widget.allowBatch()) 
    8377 
    84         #Test for existence in combobox 
    85         self.assertNotEqual(fittingWindow.cbModel.findText("cylinder"),-1) 
    86         self.assertNotEqual(fittingWindow.cbModel.findText("core_shell_cylinder"),-1) 
    87         self.assertNotEqual(fittingWindow.cbModel.findText("barbell"),-1) 
    88         self.assertNotEqual(fittingWindow.cbModel.findText("core_shell_bicelle"),-1) 
    89         self.assertNotEqual(fittingWindow.cbModel.findText("flexible_cylinder"),-1) 
    90         self.assertNotEqual(fittingWindow.cbModel.findText("flexible_cylinder_elliptical"),-1) 
    91         self.assertNotEqual(fittingWindow.cbModel.findText("pearl_necklace"),-1) 
    92         self.assertNotEqual(fittingWindow.cbModel.findText("capped_cylinder"),-1) 
    93         self.assertNotEqual(fittingWindow.cbModel.findText("elliptical_cylinder"),-1) 
    94         self.assertNotEqual(fittingWindow.cbModel.findText("pringle"),-1) 
    95         self.assertNotEqual(fittingWindow.cbModel.findText("hollow_cylinder"),-1) 
    96         self.assertNotEqual(fittingWindow.cbModel.findText("core_shell_bicelle_elliptical"),-1) 
    97         self.assertNotEqual(fittingWindow.cbModel.findText("stacked_disks"),-1) 
     78    def testSetData(self): 
     79        ''' Assure that setting data is correct''' 
     80        with self.assertRaises(AssertionError): 
     81            self.widget.setData(None) 
    9882 
     83        with self.assertRaises(AttributeError): 
     84            self.widget.setData("BOOP") 
    9985 
    100     def testSelectPolydispersity(self): 
    101         """ 
    102         Test if models have been loaded properly 
    103         :return: 
    104         """ 
    105         fittingWindow =  FittingWindow(None) 
     86        # Mock the datafromitem() call from FittingWidget 
     87        data = Data1D(x=[1,2], y=[1,2]) 
     88        GuiUtils.dataFromItem = MagicMock(return_value=data) 
    10689 
    107         #Test loading from json categories 
    108         fittingWindow.setModelModel("cylinder") 
    109         pd_index = fittingWindow.tableView_2.model().index(0,0) 
    110         self.assertEqual(str(pd_index.data().toString()), "Distribution of radius") 
    111         pd_index = fittingWindow.tableView_2.model().index(1,0) 
    112         self.assertEqual(str(pd_index.data().toString()), "Distribution of length") 
     90        item = QtGui.QStandardItem("test") 
     91        self.widget.setData([item]) 
    11392 
    114     def testSelectStructureFactor(self): 
    115         """ 
    116         Test if structure factors have been loaded properly 
    117         :return: 
    118         """ 
    119         fittingWindow =  FittingWindow(None) 
     93        # Look at the data in tab 
     94        self.assertEqual(self.widget._model_item, item) 
    12095 
    121         #Test for existence in combobox 
    122         self.assertNotEqual(fittingWindow.cbStructureFactor.findText("stickyhardsphere"),-1) 
    123         self.assertNotEqual(fittingWindow.cbStructureFactor.findText("hayter_msa"),-1) 
    124         self.assertNotEqual(fittingWindow.cbStructureFactor.findText("squarewell"),-1) 
    125         self.assertNotEqual(fittingWindow.cbStructureFactor.findText("hardsphere"),-1) 
     96        # First tab should accept data 
     97        self.assertEqual(len(self.widget.tabs), 1) 
    12698 
    127         #Test what is current text in the combobox 
    128         self.assertTrue(fittingWindow.cbCategory.currentText(), "None") 
     99        # Add another set of data 
     100        self.widget.setData([item]) 
     101 
     102        # Now we should have two tabs 
     103        self.assertEqual(len(self.widget.tabs), 2) 
     104 
    129105 
    130106if __name__ == "__main__": 
  • src/sas/sasgui/perspectives/fitting/fitting.py

    • Property mode changed from 100755 to 100644
    r7673ecd r811bec1  
    4141from sas.sasgui.perspectives.fitting.pagestate import Reader 
    4242from sas.sasgui.perspectives.fitting.fitpage import Chi2UpdateEvent 
    43 from sas.sasgui.perspectives.calculator.model_editor import TextDialog 
    44 from sas.sasgui.perspectives.calculator.model_editor import EditorWindow 
     43#from sas.sasgui.perspectives.calculator.model_editor import TextDialog 
     44#from sas.sasgui.perspectives.calculator.model_editor import EditorWindow 
    4545from sas.sasgui.guiframe.gui_manager import MDIFrame 
    4646from sas.sasgui.guiframe.documentation_window import DocumentationWindow 
Note: See TracChangeset for help on using the changeset viewer.