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

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 3f4c12f was 3f4c12f, checked in by wojciech, 3 years ago

Set Polydispersity parameters from sasmodels

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