Changeset 60af928 in sasview for src/sas/qtgui/Perspectives/Fitting
- Timestamp:
- Feb 27, 2017 5:56:13 AM (8 years ago)
- Branches:
- ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc
- Children:
- f46f6dc
- Parents:
- 6ee82dd
- Location:
- src/sas/qtgui/Perspectives/Fitting
- Files:
-
- 4 added
- 1 deleted
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/qtgui/Perspectives/Fitting/FittingPerspective.py
r3f4c12f r60af928 1 1 import sys 2 import json3 import os4 from collections import defaultdict5 2 3 from PyQt4 import QtCore 6 4 from PyQt4 import QtGui 7 from PyQt4 import QtCore8 5 9 from UI.FittingUI import Ui_FittingUI6 from FittingWidget import FittingWidget 10 7 11 from sasmodels import generate 12 from sasmodels import modelinfo 13 from sas.sasgui.guiframe.CategoryInstaller import CategoryInstaller 8 class FittingWindow(QtGui.QTabWidget): 9 """ 10 """ 11 name = "Fitting" # For displaying in the combo box in DataExplorer 12 def __init__(self, manager=None, parent=None, data=None): 13 super(FittingWindow, self).__init__() 14 14 15 class 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): 15 self.manager = manager 16 self.parent = parent 17 self._data = data 18 19 #r = raw_input("A") 20 #r = 1 21 22 # List of active fits 23 self.tabs = [] 24 25 # Max index for adding new, non-clashing tab names 26 self.maxIndex = 0 27 28 # Index of the current tab 29 self.currentTab = 0 30 31 # The current optimizer 32 self.optimizer = 'DREAM' 33 34 # The tabs need to be closeable 35 self.setTabsClosable(True) 36 37 # Initialize the first tab 38 self.addFit(None) 39 40 # Deal with signals 41 self.tabCloseRequested.connect(self.tabCloses) 42 43 self.setWindowTitle('Fit panel - Active Fitting Optimizer: %s' % self.optimizer) 44 45 def addFit(self, data): 20 46 """ 47 Add a new tab for passed data 48 """ 49 tab = FittingWidget(manager=self.manager, parent=self.parent, data=data) 50 self.tabs.append(tab) 51 self.maxIndex += 1 52 self.addTab(tab, self.tabName()) 21 53 22 :param manager: 23 :param parent: 24 :return: 54 def tabName(self): 25 55 """ 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): 56 Get the new tab name, based on the number of fitting tabs so far 66 57 """ 67 Select Category from list 68 :return: 58 page_name = "FitPage" + str(self.maxIndex) 59 return page_name 60 61 def tabCloses(self, index): 69 62 """ 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): 63 Update local bookkeeping on tab close 77 64 """ 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) 65 assert(len(self.tabs) >= index) 66 # don't remove the last tab 67 if len(self.tabs) <= 1: 68 return 69 del self.tabs[index] 70 self.removeTab(index) 230 71 231 72 if __name__ == "__main__":
Note: See TracChangeset
for help on using the changeset viewer.