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

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 0efe791 was 0efe791, checked in by wojciech, 4 years ago

Complied with naming convention of widgets

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