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