Ignore:
Timestamp:
Mar 22, 2018 3:02:08 PM (6 years ago)
Author:
krzywon
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:
304e42f
Parents:
47bf906
Message:

Move all GUI updates to a single callable/GUI directed method in P(r)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/qtgui/Perspectives/Inversion/InversionPerspective.py

    r47bf906 re51e078  
    3030MAX_DIST = 140.0 
    3131DICT_KEYS = ["Calculator", "PrPlot", "DataPlot", "DMaxWindow", 
    32              "Logic", "NFunc", "Estimates"] 
    33 ESTIMATE_KEYS = ["nFunc", "??????", "Background"] 
    34  
    35  
    36 # TODO: Modify plot references, don't just send new 
     32             "Logic", "NFunc", "NFuncEst", "BackgroundEst"] 
     33 
     34 
    3735# TODO: Update help with batch capabilities 
    3836# TODO: Method to export results in some meaningful way 
     
    7068        # p(r) calculator for self._data 
    7169        self._calculator = Invertor() 
    72         # Combo box index for self._data 
    73         self._data_index = 0 
    7470        # plots of self._data 
    7571        self.pr_plot = None 
    7672        self.data_plot = None 
     73        # suggested nTerms 
     74        self.nTermsSuggested = NUMBER_OF_TERMS 
    7775 
    7876        # Calculation threads used by all data items 
     
    8179 
    8280        # Mapping for all data items 
    83         # TODO: {QStandardItem -> {params: [], plot: Plot, dmax: dMax, 
    84         # TODO: calculator: Invertor(), estimates: {}, data: Data1D, ...} 
    8581        # list mapping data to all parameters 
    8682        self._data_list = {} 
     
    8985        if data is not None: 
    9086            for datum in data_list: 
    91                 self._data_list[datum] = {DICT_KEYS[0]: self._calculator.clone(), 
    92                                           DICT_KEYS[4]: self.logic, 
    93                                           DICT_KEYS[5]: NUMBER_OF_TERMS, 
    94                                           DICT_KEYS[6]: {}} 
    95  
    96         self.calculateAllButton.setEnabled(False) 
    97         self.calculateThisButton.setEnabled(False) 
     87                self.updateDataList(datum) 
     88 
     89        self.enableButtons() 
    9890 
    9991        self.model = QtGui.QStandardItemModel(self) 
     
    311303        """Switch to another item in the data list""" 
    312304        ref_item = self.dataList.itemData(self.dataList.currentIndex()) 
    313         self.updateDataList(self._data) 
     305        self.updateDataList(ref_item) 
    314306        self.setCurrentData(ref_item) 
    315307 
     
    397389            self.logic = InversionLogic(self._data_set) 
    398390            self.populateDataComboBox(self._data_set.filename, data) 
     391            # Estimate q range 
     392            qmin, qmax = self.logic.computeDataRange() 
     393            self._calculator.set_qmin(qmin) 
     394            self._calculator.set_qmax(qmax) 
     395            self.updateDataList(data) 
    399396            self.setCurrentData(data) 
    400  
    401397            # Estimate initial values from data 
    402398            self.performEstimate() 
    403  
    404             # Estimate q range 
    405             qmin, qmax = self.logic.computeDataRange() 
    406             self.model.setItem(WIDGETS.W_QMIN, QtGui.QStandardItem("{:.4g}".format(qmin))) 
    407             self.model.setItem(WIDGETS.W_QMAX, QtGui.QStandardItem("{:.4g}".format(qmax))) 
    408             self._data_list[data] = {DICT_KEYS[0]: self._calculator.clone(), 
    409                                      DICT_KEYS[1]: self.data_plot, 
    410                                      DICT_KEYS[2]: self.pr_plot, 
    411                                      DICT_KEYS[4]: self.logic} 
    412399        self.enableButtons() 
    413400 
    414401    def updateDataList(self, dataRef): 
    415402        """Save the current data state of the window into self._data_list""" 
     403        if dataRef is None: 
     404            return 
    416405        self._data_list[dataRef] = { 
    417         DICT_KEYS[0]: self._calculator, 
    418         DICT_KEYS[1]: self.pr_plot, 
    419         DICT_KEYS[2]: self.data_plot, 
    420         DICT_KEYS[3]: self.dmaxWindow 
     406            DICT_KEYS[0]: self._calculator, 
     407            DICT_KEYS[1]: self.pr_plot, 
     408            DICT_KEYS[2]: self.data_plot, 
     409            DICT_KEYS[3]: self.dmaxWindow, 
     410            DICT_KEYS[4]: self.logic, 
     411            DICT_KEYS[5]: self.getNFunc(), 
     412            DICT_KEYS[6]: self.nTermsSuggested, 
     413            DICT_KEYS[7]: self.backgroundInput.text() 
    421414        } 
    422415 
     
    433426    def setCurrentData(self, data_ref): 
    434427        """Get the data by reference and display as necessary""" 
    435         if data_ref is None: 
    436             return 
    437428        if not isinstance(data_ref, QtGui.QStandardItem): 
    438429            msg = "Incorrect type passed to the P(r) Perspective" 
    439             raise AttributeError 
     430            raise AttributeError(msg) 
    440431        # Data references 
    441432        self._data = data_ref 
     
    446437        self.dmaxWindow = self._data_list[data_ref].get(DICT_KEYS[3]) 
    447438        self.logic = self._data_list[data_ref].get(DICT_KEYS[4]) 
    448         # TODO: Do the values need to be updated when _calculator is changed? 
     439        self.updateGuiValues() 
     440 
     441    def updateGuiValues(self): 
     442        pr = self._calculator 
     443        out = self._calculator.out 
     444        cov = self._calculator.cov 
     445        elapsed = self._calculator.elapsed 
     446        alpha = self._calculator.suggested_alpha 
     447        nterms = self._calculator.nfunc 
     448        self.model.setItem(WIDGETS.W_QMIN, 
     449                           QtGui.QStandardItem("{:.4g}".format(pr.get_qmin()))) 
     450        self.model.setItem(WIDGETS.W_QMAX, 
     451                           QtGui.QStandardItem("{:.4g}".format(pr.get_qmax()))) 
     452        self.model.setItem(WIDGETS.W_BACKGROUND_INPUT, 
     453                           QtGui.QStandardItem("{:.3f}".format(pr.est_bck))) 
     454        self.model.setItem(WIDGETS.W_BACKGROUND_OUTPUT, 
     455                           QtGui.QStandardItem("{:.3g}".format(pr.background))) 
     456        self.model.setItem(WIDGETS.W_COMP_TIME, 
     457                           QtGui.QStandardItem("{:.4g}".format(elapsed))) 
     458        if alpha != 0: 
     459            self.regConstantSuggestionButton.setText("{:-3.2g}".format(alpha)) 
     460        self.regConstantSuggestionButton.setEnabled(alpha != 0) 
     461        if nterms != self.nTermsSuggested: 
     462            self.noOfTermsSuggestionButton.setText( 
     463                "{:n}".format(self.nTermsSuggested)) 
     464        self.noOfTermsSuggestionButton.setEnabled(nterms != self.nTermsSuggested) 
     465        self.model.setItem(WIDGETS.W_COMP_TIME, 
     466                           QtGui.QStandardItem("{:.2g}".format(elapsed))) 
     467 
     468        if isinstance(pr.chi2, list): 
     469            self.model.setItem(WIDGETS.W_CHI_SQUARED, 
     470                               QtGui.QStandardItem("{:.3g}".format(pr.chi2[0]))) 
     471        if out is not None: 
     472            self.model.setItem(WIDGETS.W_RG, 
     473                               QtGui.QStandardItem("{:.3g}".format(pr.rg(out)))) 
     474            self.model.setItem(WIDGETS.W_I_ZERO, 
     475                               QtGui.QStandardItem( 
     476                                   "{:.3g}".format(pr.iq0(out)))) 
     477            self.model.setItem(WIDGETS.W_OSCILLATION, QtGui.QStandardItem( 
     478                "{:.3g}".format(pr.oscillations(out)))) 
     479            self.model.setItem(WIDGETS.W_POS_FRACTION, QtGui.QStandardItem( 
     480                "{:.3g}".format(pr.get_positive(out)))) 
     481            if cov is not None: 
     482                self.model.setItem(WIDGETS.W_SIGMA_POS_FRACTION, 
     483                                   QtGui.QStandardItem( 
     484                                       "{:.3g}".format( 
     485                                           pr.get_pos_err(out, cov)))) 
    449486 
    450487    def removeData(self): 
     
    453490            self.dmaxWindow = None 
    454491        self.dataList.removeItem(self.dataList.currentIndex()) 
    455         # TODO: Remove plot references from higher level too? 
    456         self.dataList.setCurrentIndex(0) 
     492        self._data_list.pop(self._data) 
    457493        # Last file removed 
    458         self._data_list.pop(self._data) 
    459         if not self._data_list: 
     494        if len(self._data_list) == 0: 
    460495            self._data = None 
    461496            self.pr_plot = None 
    462497            self._data_set = None 
     498            self._calculator = Invertor() 
    463499            self.logic = InversionLogic() 
    464500            self.enableButtons() 
    465         self.displayChange() 
     501        self.dataList.setCurrentIndex(0) 
    466502 
    467503    ###################################################################### 
    468504    # Thread Creators 
    469505    def startThreadAll(self): 
    470         for data_ref, pr in list(self._data_list.items()): 
    471             self._data_set = GuiUtils.dataFromItem(data_ref) 
    472             self._calculator = pr 
     506        for data_ref in self._data_list.keys(): 
     507            self.setCurrentData(data_ref) 
    473508            self.startThread() 
    474509 
     
    556591        alpha, message, elapsed = output_tuple 
    557592        # Save useful info 
    558         self.model.setItem(WIDGETS.W_COMP_TIME, QtGui.QStandardItem("{:.4g}".format(elapsed))) 
    559         self.regConstantSuggestionButton.setText("{:-3.2g}".format(alpha)) 
    560         self.regConstantSuggestionButton.setEnabled(True) 
     593        self.updateGuiValues() 
    561594        if message: 
    562595            logging.info(message) 
     
    576609        :param elapsed: computation time 
    577610        """ 
    578         # TODO: Add estimates to DICT_KEYS 
    579611        nterms, alpha, message, elapsed = output_tuple 
     612        self._calculator.elapsed = elapsed 
     613        self._calculator.suggested_alpha = alpha 
     614        self.nTermsSuggested = nterms 
    580615        # Save useful info 
    581         self.noOfTermsSuggestionButton.setText("{:n}".format(nterms)) 
    582         self.noOfTermsSuggestionButton.setEnabled(True) 
    583         self.regConstantSuggestionButton.setText("{:.3g}".format(alpha)) 
    584         self.regConstantSuggestionButton.setEnabled(True) 
    585         self.model.setItem(WIDGETS.W_COMP_TIME, QtGui.QStandardItem("{:.2g}".format(elapsed))) 
     616        self.updateGuiValues() 
    586617        if message: 
    587618            logging.info(message) 
     
    607638        pr.elapsed = elapsed 
    608639 
    609         # Show result on control panel 
    610         self.model.setItem(WIDGETS.W_RG, QtGui.QStandardItem("{:.3g}".format(pr.rg(out)))) 
    611         self.model.setItem(WIDGETS.W_I_ZERO, QtGui.QStandardItem("{:.3g}".format(pr.iq0(out)))) 
    612         self.model.setItem(WIDGETS.W_BACKGROUND_INPUT, 
    613                            QtGui.QStandardItem("{:.3f}".format(pr.est_bck))) 
    614         self.model.setItem(WIDGETS.W_BACKGROUND_OUTPUT, QtGui.QStandardItem("{:.3g}".format(pr.background))) 
    615         self.model.setItem(WIDGETS.W_CHI_SQUARED, QtGui.QStandardItem("{:.3g}".format(pr.chi2[0]))) 
    616         self.model.setItem(WIDGETS.W_COMP_TIME, QtGui.QStandardItem("{:.2g}".format(elapsed))) 
    617         self.model.setItem(WIDGETS.W_OSCILLATION, QtGui.QStandardItem("{:.3g}".format(pr.oscillations(out)))) 
    618         self.model.setItem(WIDGETS.W_POS_FRACTION, QtGui.QStandardItem("{:.3g}".format(pr.get_positive(out)))) 
    619         self.model.setItem(WIDGETS.W_SIGMA_POS_FRACTION, 
    620                            QtGui.QStandardItem("{:.3g}".format(pr.get_pos_err(out, cov)))) 
    621  
    622640        # Save Pr invertor 
    623641        self._calculator = pr 
    624         # Append data to data list 
    625         self._data_list[self._data][DICT_KEYS[0]] = self._calculator.clone() 
    626642 
    627643        # Create new P(r) and fit plots 
    628644        if self.pr_plot is None: 
    629645            self.pr_plot = self.logic.newPRPlot(out, self._calculator, cov) 
    630             self._data_list[self._data][DICT_KEYS[1]] = self.pr_plot 
    631646        else: 
    632             # FIXME: this should update the existing plot, not create a new one 
    633             self.pr_plot = self.logic.newPRPlot(out, self._calculator, cov) 
    634             self._data_list[self._data][DICT_KEYS[1]] = self.pr_plot 
     647            title = self.pr_plot.name 
     648            GuiUtils.updateModelItemWithPlot(self._data, self.pr_plot, title) 
    635649        if self.data_plot is None: 
    636650            self.data_plot = self.logic.new1DPlot(out, self._calculator) 
    637             self._data_list[self._data][DICT_KEYS[2]] = self.data_plot 
    638651        else: 
    639             # FIXME: this should update the existing plot, not create a new one 
    640             self.data_plot = self.logic.new1DPlot(out, self._calculator) 
    641             self._data_list[self._data][DICT_KEYS[2]] = self.data_plot 
     652            title = self.data_plot.name 
     653            GuiUtils.updateModelItemWithPlot(self._data, self.data_plot, title) 
     654        self.updateDataList(self._data) 
     655        self.updateGuiValues() 
    642656 
    643657    def _threadError(self, error): 
Note: See TracChangeset for help on using the changeset viewer.