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