source: sasview/src/sas/qtgui/Utilities/GridPanel.py @ 3b3b40b

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 3b3b40b was 3b3b40b, checked in by Piotr Rozyczko <rozyczko@…>, 6 years ago

Merging feature branches

  • Property mode set to 100644
File size: 10.0 KB
RevLine 
[3b3b40b]1import os
2import time
3import logging
4import webbrowser
5
6from PyQt5 import QtCore, QtGui, QtWidgets
7
8import sas.qtgui.Utilities.GuiUtils as GuiUtils
9
10from sas.qtgui.Utilities.UI.GridPanelUI import Ui_GridPanelUI
11
12
13class BatchOutputPanel(QtWidgets.QMainWindow, Ui_GridPanelUI):
14    """
15    Class for stateless grid-like printout of model parameters for mutiple models
16    """
17    def __init__(self, parent = None, output_data=None):
18
19        super(BatchOutputPanel, self).__init__()
20        self.setupUi(self)
21
22        self.data = output_data
23        self.parent = parent
24        if hasattr(self.parent, "communicate"):
25            self.communicate = parent.communicate
26
27        self.addToolbarActions()
28
29        # file name for the dataset
30        self.grid_filename = ""
31
32        # context menu on the table
33        self.tblParams.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
34        self.tblParams.customContextMenuRequested.connect(self.showContextMenu)
35
36        # Fill in the table from input data
37        self.setupTable(output_data)
38
39        # Command buttons
40        self.cmdHelp.clicked.connect(self.onHelp)
41
42    def addToolbarActions(self):
43        """
44        Assing actions and callbacks to the File menu items
45        """
46        self.actionOpen.triggered.connect(self.actionLoadData)
47        self.actionOpen_with_Excel.triggered.connect(self.actionSendToExcel)
48        self.actionSave.triggered.connect(self.actionSaveFile)
49
50    def actionLoadData(self):
51        """
52        Open file load dialog and load a .csv file
53        """
54        datafile = QtWidgets.QFileDialog.getOpenFileName(
55            self, "Choose a file with results", "", "CSV files (*.csv)", None,
56            QtWidgets.QFileDialog.DontUseNativeDialog)[0]
57
58        if not datafile:
59            logging.info("No data file chosen.")
60            return
61
62        with open(datafile, 'r') as csv_file:
63            lines = csv_file.readlines()
64
65        self.setupTableFromCSV(lines)
66
67    def showContextMenu(self, position):
68        """
69        Show context specific menu in the tab table widget.
70        """
71        menu = QtWidgets.QMenu()
72        rows = [s.row() for s in self.tblParams.selectionModel().selectedRows()]
73        num_rows = len(rows)
74        if num_rows <= 0:
75            return
76        # Find out which items got selected and in which row
77        # Select for fitting
78
79        self.actionPlotResults = QtWidgets.QAction(self)
80        self.actionPlotResults.setObjectName("actionPlot")
81        self.actionPlotResults.setText(QtCore.QCoreApplication.translate("self", "Plot selected fits."))
82
83        menu.addAction(self.actionPlotResults)
84
85        # Define the callbacks
86        self.actionPlotResults.triggered.connect(self.plotFits)
87        try:
88            menu.exec_(self.tblParams.viewport().mapToGlobal(position))
89        except AttributeError as ex:
90            logging.error("Error generating context menu: %s" % ex)
91        return
92
93    def onHelp(self):
94        """
95        Open a local url in the default browser
96        """
97        location = GuiUtils.HELP_DIRECTORY_LOCATION
98        url = "/user/sasgui/perspectives/fitting/fitting_help.html#batch-fit-mode"
99        try:
100            webbrowser.open('file://' + os.path.realpath(location+url))
101        except webbrowser.Error as ex:
102            logging.warning("Cannot display help. %s" % ex)
103
104    def plotFits(self):
105        """
106        Plot selected fits by sending signal to the parent
107        """
108        rows = [s.row() for s in self.tblParams.selectionModel().selectedRows()]
109        data = self.dataFromTable(self.tblParams)
110        # data['Data'] -> ['filename1', 'filename2', ...]
111        # look for the 'Data' column and extract the filename
112        for row in rows:
113            try:
114                filename = data['Data'][row]
115                # emit a signal so the plots are being shown
116                self.communicate.plotFromFilenameSignal.emit(filename)
117            except (IndexError, AttributeError):
118                # data messed up.
119                return
120
121    def dataFromTable(self, table):
122        """
123        Creates a dictionary {<parameter>:[list of values]} from the parameter table
124        """
125        assert(isinstance(table, QtWidgets.QTableWidget))
126        params = {}
127        #return {"sld_solvent":[1.2, 2.0, 0.0], "scale":[1.0, 2.0, 3.0]}
128        for column in range(table.columnCount()):
129            #pass
130            value = [table.item(row, column).data(0) for row in range(table.rowCount())]
131            key = table.horizontalHeaderItem(column).data(0)
132            params[key] = value
133        return params
134
135    def actionSendToExcel(self):
136        """
137        Generates a .csv file and opens the default CSV reader
138        """
139        if not self.grid_filename:
140            import tempfile
141            tmpfile = tempfile.NamedTemporaryFile(delete=False, mode="w+", suffix=".csv")
142            self.grid_filename = tmpfile.name
143            data = self.dataFromTable(self.tblParams)
144            t = time.localtime(time.time())
145            time_str = time.strftime("%b %d %H:%M of %Y", t)
146            details = "File Generated by SasView " 
147            details += "on %s.\n" % time_str
148            self.writeBatchToFile(data=data, tmpfile=tmpfile, details=details)
149            tmpfile.close()
150
151        try:
152            from win32com.client import Dispatch
153            excel_app = Dispatch('Excel.Application')
154            excel_app.Workbooks.Open(self.grid_filename)
155            excel_app.Visible = 1
156        except Exception as ex:
157            msg = "Error occured when calling Excel.\n"
158            msg += ex
159            self.parent.communicate.statusBarUpdateSignal.emit(msg)
160
161    def actionSaveFile(self):
162        """
163        Generate a .csv file and dump it do disk
164        """
165        t = time.localtime(time.time())
166        time_str = time.strftime("%b %d %H %M of %Y", t)
167        default_name = "Batch_Fitting_"+time_str+".csv"
168
169        wildcard = "CSV files (*.csv);;"
170        kwargs = {
171            'caption'   : 'Save As',
172            'directory' : default_name,
173            'filter'    : wildcard,
174            'parent'    : None,
175        }
176        # Query user for filename.
177        filename_tuple = QtWidgets.QFileDialog.getSaveFileName(**kwargs)
178        filename = filename_tuple[0]
179
180        # User cancelled.
181        if not filename:
182            return
183        data = self.dataFromTable(self.tblParams)
184        details = "File generated by SasView\n"
185        with open(filename, 'w') as csv_file:
186            self.writeBatchToFile(data=data, tmpfile=csv_file, details=details)
187
188    def setupTableFromCSV(self, csv_data):
189        """
190        Create tablewidget items and show them, based on params
191        """
192        # Clear existing display
193        self.tblParams.clear()
194        # headers
195        param_list = csv_data[1].rstrip().split(',')
196        for i, param in enumerate(param_list):
197            self.tblParams.setHorizontalHeaderItem(i, QtWidgets.QTableWidgetItem(param))
198
199        # first - Chi2 and data filename
200        for i_row, row in enumerate(csv_data[2:]):
201            for i_col, col in enumerate(row.rstrip().split(',')):
202                self.tblParams.setItem(i_row, i_col, QtWidgets.QTableWidgetItem(col))
203
204        self.tblParams.resizeColumnsToContents()
205
206    def setupTable(self, data):
207        """
208        Create tablewidget items and show them, based on params
209        """
210        # headers
211        model = data[0][0]
212        param_list = [m for m in model.model.params.keys() if ":" not in m]
213
214        # Check if 2D model. If not, remove theta/phi
215
216        rows = len(data)
217        columns = len(param_list)
218        self.tblParams.setColumnCount(columns+2)
219        self.tblParams.setRowCount(rows)
220
221        param_list.insert(0, "Data")
222        param_list.insert(0, "Chi2")
223        for i, param in enumerate(param_list):
224            self.tblParams.setHorizontalHeaderItem(i, QtWidgets.QTableWidgetItem(param))
225
226        # first - Chi2 and data filename
227        for i_row, row in enumerate(data):
228            # each row corresponds to a single fit
229            chi2 = row[0].fitness
230            filename = ""
231            if hasattr(row[0].data, "sas_data"):
232                filename = row[0].data.sas_data.filename
233            self.tblParams.setItem(i_row, 0, QtWidgets.QTableWidgetItem(GuiUtils.formatNumber(chi2, high=True)))
234            self.tblParams.setItem(i_row, 1, QtWidgets.QTableWidgetItem(str(filename)))
235            # Now, all the parameters
236            for i_col, param in enumerate(param_list[2:]):
237                if param in row[0].param_list:
238                    # parameter is on the to-optimize list - get the optimized value
239                    par_value = row[0].pvec[row[0].param_list.index(param)]
240                    # should we parse out errors here and store them?
241                else:
242                    # parameter was not varied
243                    par_value = row[0].model.params[param]
244                self.tblParams.setItem(i_row, i_col+2, QtWidgets.QTableWidgetItem(
245                    GuiUtils.formatNumber(par_value, high=True)))
246
247        self.tblParams.resizeColumnsToContents()
248
249    def writeBatchToFile(self, data, tmpfile, details=""):
250        """
251        Helper to write result from batch into cvs file
252        """
253        name = tmpfile.name
254        if data is None or name is None or name.strip() == "":
255            return
256        _, ext = os.path.splitext(name)
257        separator = "\t"
258        if ext.lower() == ".csv":
259            separator = ","
260        tmpfile.write(details)
261        for col_name in data.keys():
262            tmpfile.write(col_name)
263            tmpfile.write(separator)
264        tmpfile.write('\n')
265        max_list = [len(value) for value in data.values()]
266        if len(max_list) == 0:
267            return
268        max_index = max(max_list)
269        index = 0
270        while index < max_index:
271            for value_list in data.values():
272                if index < len(value_list):
273                    tmpfile.write(str(value_list[index]))
274                    tmpfile.write(separator)
275                else:
276                    tmpfile.write('')
277                    tmpfile.write(separator)
278            tmpfile.write('\n')
279            index += 1
280
Note: See TracBrowser for help on using the repository browser.