source: sasview/src/sas/qtgui/Perspectives/Fitting/FittingPerspective.py @ b9c4c1e1

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since b9c4c1e1 was b9c4c1e1, checked in by wojciech, 3 years ago

Unit tests added for selecting models, categories and structure factors

  • Property mode set to 100755
File size: 9.0 KB
Line 
1import sys
2import json
3import  os
4from collections import defaultdict
5
6from PyQt4 import QtGui
7from PyQt4 import QtCore
8
9from UI.FittingUI import Ui_FittingUI
10
11from sasmodels import generate
12from sasmodels import modelinfo
13from sas.sasgui.guiframe.CategoryInstaller import CategoryInstaller
14
15class FittingWindow(QtGui.QDialog, Ui_FittingUI):
16    """
17    Main window for selecting form and structure factor models
18    """
19    def __init__(self, manager=None, parent=None):
20        """
21
22        :param manager:
23        :param parent:
24        :return:
25        """
26        super(FittingWindow, self).__init__()
27        self.setupUi(self)
28
29        self.setWindowTitle("Fitting")
30        self._model_model = QtGui.QStandardItemModel()
31        self._poly_model = QtGui.QStandardItemModel()
32        self.tableView.setModel(self._model_model)
33
34        self._readCategoryInfo()
35
36        structure_factor_list = self.master_category_dict.pop('Structure Factor')
37        for (structure_factor, enabled) in structure_factor_list:
38            self.cbStructureFactor.addItem(structure_factor)
39        self.cbStructureFactor.currentIndexChanged.connect(self.selectStructureFactor)
40
41        category_list = sorted(self.master_category_dict.keys())
42        self.cbCategory.addItems(category_list)
43        self.cbCategory.currentIndexChanged.connect(self.selectCategory)
44
45        category = self.cbCategory.currentText()
46        model_list = self.master_category_dict[str(category)]
47        for (model, enabled) in model_list:
48            self.cbModel.addItem(model)
49        self.cbModel.currentIndexChanged.connect(self.selectModel)
50
51        self.pushButton.setEnabled(False)
52        self.chkPolydispersity.setEnabled(False)
53        self.chkSmearing.setEnabled(False)
54
55        self.lblMinRangeDef.setText("---")
56        self.lblMaxRangeDef.setText("---")
57        self.lblChi2Value.setText("---")
58
59        #self.setTableProperties(self.tableView)
60
61        self.tableView_2.setModel(self._poly_model)
62        self.setPolyModel()
63        self.setTableProperties(self.tableView_2)
64
65        for row in range(2):
66            c = QtGui.QComboBox()
67            c.addItems(['rectangle','array','lognormal','gaussian','schulz',])
68            i = self.tableView_2.model().index(row,6)
69            self.tableView_2.setIndexWidget(i,c)
70
71    def selectCategory(self):
72        """
73        Select Category from list
74        :return:
75        """
76        self.cbModel.clear()
77        category = self.cbCategory.currentText()
78        model_list = self.master_category_dict[str(category)]
79        for (model, enabled) in model_list:
80            self.cbModel.addItem(model)
81
82    def selectModel(self):
83        """
84        Select Model from list
85        :return:
86        """
87        model = self.cbModel.currentText()
88        self.setModelModel(model)
89
90    def selectStructureFactor(self):
91        """
92        Select Structure Factor from list
93        :param:
94        :return:
95        """
96
97
98    def _readCategoryInfo(self):
99        """
100        Reads the categories in from file
101        """
102        self.master_category_dict = defaultdict(list)
103        self.by_model_dict = defaultdict(list)
104        self.model_enabled_dict = defaultdict(bool)
105
106        try:
107            categorization_file = CategoryInstaller.get_user_file()
108            if not os.path.isfile(categorization_file):
109                categorization_file = CategoryInstaller.get_default_file()
110            cat_file = open(categorization_file, 'rb')
111            self.master_category_dict = json.load(cat_file)
112            self._regenerate_model_dict()
113            cat_file.close()
114        except IOError:
115            raise
116            print 'Problem reading in category file.'
117            print 'We even looked for it, made sure it was there.'
118            print 'An existential crisis if there ever was one.'
119
120    def _regenerate_model_dict(self):
121        """
122        regenerates self.by_model_dict which has each model name as the
123        key and the list of categories belonging to that model
124        along with the enabled mapping
125        """
126        self.by_model_dict = defaultdict(list)
127        for category in self.master_category_dict:
128            for (model, enabled) in self.master_category_dict[category]:
129                self.by_model_dict[model].append(category)
130                self.model_enabled_dict[model] = enabled
131
132       
133    def setModelModel(self, model_name):
134        """
135        Setting model parameters into table based on selected
136        :param model_name:
137        :return:
138        """
139        # Crete/overwrite model items
140        self._model_model.clear()
141        model_name = str(model_name)
142        kernel_module = generate.load_kernel_module(model_name)
143        parameters = modelinfo.make_parameter_table(getattr(kernel_module, 'parameters', []))
144
145        #TODO: scaale and background are implicit in sasmodels and needs to be added
146        item1 = QtGui.QStandardItem('scale')
147        item1.setCheckable(True)
148        item2 = QtGui.QStandardItem('1.0')
149        item3 = QtGui.QStandardItem('0.0')
150        item4 = QtGui.QStandardItem('inf')
151        item5 = QtGui.QStandardItem('')
152        self._model_model.appendRow([item1, item2, item3, item4, item5])
153
154        item1 = QtGui.QStandardItem('background')
155        item1.setCheckable(True)
156        item2 = QtGui.QStandardItem('0.001')
157        item3 = QtGui.QStandardItem('-inf')
158        item4 = QtGui.QStandardItem('inf')
159        item5 = QtGui.QStandardItem('1/cm')
160        self._model_model.appendRow([item1, item2, item3, item4, item5])
161
162        #TODO: iq_parameters are used here. If orientation paramateres or magnetic are needed kernel_paramters should be used instead
163        #For orientation and magentic parameters param.type needs to be checked
164        for param in parameters.iq_parameters:
165            item1 = QtGui.QStandardItem(param.name)
166            item1.setCheckable(True)
167            item2 = QtGui.QStandardItem(str(param.default))
168            item3 = QtGui.QStandardItem(str(param.limits[0]))
169            item4 = QtGui.QStandardItem(str(param.limits[1]))
170            item5 = QtGui.QStandardItem(param.units)
171            self._model_model.appendRow([item1, item2, item3, item4, item5])
172
173        self._model_model.setHeaderData(0, QtCore.Qt.Horizontal, QtCore.QVariant("Parameter"))
174        self._model_model.setHeaderData(1, QtCore.Qt.Horizontal, QtCore.QVariant("Value"))
175        self._model_model.setHeaderData(2, QtCore.Qt.Horizontal, QtCore.QVariant("Min"))
176        self._model_model.setHeaderData(3, QtCore.Qt.Horizontal, QtCore.QVariant("Max"))
177        self._model_model.setHeaderData(4, QtCore.Qt.Horizontal, QtCore.QVariant("[Units]"))
178
179    def setTableProperties(self, table):
180        """
181        Setting table properties
182        :param table:
183        :return:
184        """
185        table.setStyleSheet("background-image: url(model.png);")
186
187        # Table properties
188        table.verticalHeader().setVisible(False)
189        table.setAlternatingRowColors(True)
190        table.setSizePolicy(QtGui.QSizePolicy.MinimumExpanding, QtGui.QSizePolicy.Expanding)
191        table.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
192        # Header
193        header = table.horizontalHeader()
194        header.setResizeMode(QtGui.QHeaderView.Stretch)
195        header.setStretchLastSection(True)
196
197    def setPolyModel(self):
198        """
199        Set polydispersity values
200        :return:
201        """
202        item1 = QtGui.QStandardItem("Distribution of radius")
203        item1.setCheckable(True)
204        item2 = QtGui.QStandardItem("0")
205        item3 = QtGui.QStandardItem("")
206        item4 = QtGui.QStandardItem("")
207        item5 = QtGui.QStandardItem("35")
208        item6 = QtGui.QStandardItem("3")
209        item7 = QtGui.QStandardItem("")
210        self._poly_model.appendRow([item1, item2, item3, item4, item5, item6, item7])
211        item1 = QtGui.QStandardItem("Distribution of thickness")
212        item1.setCheckable(True)
213        item2 = QtGui.QStandardItem("0")
214        item3 = QtGui.QStandardItem("")
215        item4 = QtGui.QStandardItem("")
216        item5 = QtGui.QStandardItem("35")
217        item6 = QtGui.QStandardItem("3")
218        item7 = QtGui.QStandardItem("")
219        self._poly_model.appendRow([item1, item2, item3, item4, item5, item6, item7])
220
221        self._poly_model.setHeaderData(0, QtCore.Qt.Horizontal, QtCore.QVariant("Parameter"))
222        self._poly_model.setHeaderData(1, QtCore.Qt.Horizontal, QtCore.QVariant("PD[ratio]"))
223        self._poly_model.setHeaderData(2, QtCore.Qt.Horizontal, QtCore.QVariant("Min"))
224        self._poly_model.setHeaderData(3, QtCore.Qt.Horizontal, QtCore.QVariant("Max"))
225        self._poly_model.setHeaderData(4, QtCore.Qt.Horizontal, QtCore.QVariant("Npts"))
226        self._poly_model.setHeaderData(5, QtCore.Qt.Horizontal, QtCore.QVariant("Nsigs"))
227        self._poly_model.setHeaderData(6, QtCore.Qt.Horizontal, QtCore.QVariant("Function"))
228
229        self.tableView_2.resizeColumnsToContents()
230        header = self.tableView_2.horizontalHeader()
231        header.ResizeMode(QtGui.QHeaderView.Stretch)
232        header.setStretchLastSection(True)
233
234
235if __name__ == "__main__":
236    app = QtGui.QApplication([])
237    dlg = FittingWindow()
238    dlg.show()
239    sys.exit(app.exec_())
Note: See TracBrowser for help on using the repository browser.