Changeset 570a58f9 in sasview for src/sas/qtgui/Plotter.py


Ignore:
Timestamp:
Jan 6, 2017 1:40:05 AM (7 years ago)
Author:
Piotr Rozyczko <rozyczko@…>
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:
fed94a2
Parents:
aadf0af1
git-author:
Piotr Rozyczko <rozyczko@…> (01/06/17 01:31:10)
git-committer:
Piotr Rozyczko <rozyczko@…> (01/06/17 01:40:05)
Message:

Linear fits for 1D charts

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/qtgui/Plotter.py

    raadf0af1 r570a58f9  
    1212from sas.qtgui.AddText import AddText 
    1313from sas.qtgui.SetGraphRange import SetGraphRange 
     14from sas.qtgui.LinearFit import LinearFit 
    1415 
    1516class PlotterWidget(PlotterBase): 
     
    1920    def __init__(self, parent=None, manager=None, quickplot=False): 
    2021        super(PlotterWidget, self).__init__(parent, manager=manager, quickplot=quickplot) 
     22 
    2123        self.parent = parent 
    2224        self.addText = AddText(self) 
     
    2729        # Simple window for data display 
    2830        self.txt_widget = QtGui.QTextEdit(None) 
     31 
     32        self.xLogLabel = "log10(x)" 
     33        self.yLogLabel = "log10(y)" 
     34 
     35        # Data container for the linear fit 
     36        self.fit_result = Data1D(x=[], y=[], dy=None) 
     37        self.fit_result.symbol = 13 
     38        self.fit_result.name = "Fit" 
     39 
     40        # Add a slot for receiving update signal from LinearFit 
     41        # NEW style signals - don't work! 
     42        #self.updatePlot = QtCore.pyqtSignal(tuple) 
     43        #self.updatePlot.connect(self.updateWithData) 
     44        # OLD style signals - work perfectly 
     45        QtCore.QObject.connect(self, QtCore.SIGNAL('updatePlot'), self.onFitDisplay) 
    2946 
    3047    @property 
     
    4865            self.data = data 
    4966        assert(self._data) 
     67 
     68        is_fit = (self._data.id=="fit") 
    5069 
    5170        # Shortcut for an axis 
     
    87106 
    88107        # Current labels for axes 
    89         ax.set_ylabel(self.y_label) 
    90         ax.set_xlabel(self.x_label) 
     108        if self.y_label and not is_fit: 
     109            ax.set_ylabel(self.y_label) 
     110        if self.x_label and not is_fit: 
     111            ax.set_xlabel(self.x_label) 
    91112 
    92113        # Title only for regular charts 
    93         if not self.quickplot: 
     114        if not self.quickplot and not is_fit: 
    94115            ax.set_title(label=self._title) 
    95116 
     
    161182            if plot.id != 'fit': 
    162183                self.actionLinearFit = plot_menu.addAction('&Linear Fit') 
    163                 self.actionLinearFit.triggered.connect(self.onLinearFit) 
     184                self.actionLinearFit.triggered.connect( 
     185                                functools.partial(self.onLinearFit, id)) 
    164186                plot_menu.addSeparator() 
    165187 
     
    209231        """ 
    210232        if self.properties.exec_() == QtGui.QDialog.Accepted: 
    211             xLabel, yLabel = self.properties.getValues() 
    212             self.xyTransform(xLabel, yLabel) 
     233            self.xLogLabel, self.yLogLabel = self.properties.getValues() 
     234            self.xyTransform(self.xLogLabel, self.yLogLabel) 
    213235 
    214236    def onModifyGraph(self): 
     
    319341        GuiUtils.saveData1D(plot_data) 
    320342 
    321     def onLinearFit(self): 
     343    def onLinearFit(self, id): 
    322344        """ 
    323345        Creates and displays a simple linear fit for the selected plot 
    324346        """ 
    325         pass 
     347        selected_plot = self.plot_dict[id] 
     348 
     349        maxrange = (min(selected_plot.x), max(selected_plot.x)) 
     350        fitrange = self.ax.get_xlim() 
     351 
     352        fit_dialog = LinearFit(parent=self, 
     353                    data=selected_plot, 
     354                    max_range=maxrange, 
     355                    fit_range=fitrange, 
     356                    xlabel=self.xLogLabel, 
     357                    ylabel=self.yLogLabel) 
     358        if fit_dialog.exec_() == QtGui.QDialog.Accepted: 
     359            return 
    326360 
    327361    def onRemovePlot(self, id): 
    328362        """ 
     363        Responds to the plot delete action 
     364        """ 
     365        self.removePlot(id) 
     366 
     367        if len(self.plot_dict) == 0: 
     368            # last plot: graph is empty must be the panel must be destroyed 
     369                self.parent.close() 
     370 
     371    def removePlot(self, id): 
     372        """ 
    329373        Deletes the selected plot from the chart 
    330374        """ 
     375        if id not in self.plot_dict: 
     376            return 
     377 
    331378        selected_plot = self.plot_dict[id] 
    332379 
     
    341388            if ids != id: 
    342389                self.plot(data=plot_dict[ids], hide_error=plot_dict[ids].hide_error)                 
    343  
    344         if len(self.plot_dict) == 0: 
    345             # last plot: graph is empty must be the panel must be destroyed 
    346                 self.parent.close() 
    347390 
    348391    def onFreeze(self, id): 
     
    387430        Transforms x and y in View and set the scale 
    388431        """ 
    389         # Clear the plot first 
    390         plt.cla() 
    391         self.ax.cla() 
    392  
    393         new_xlabel, new_ylabel, xscale, yscale = GuiUtils.xyTransform(self.data, xLabel, yLabel) 
    394         self.xscale = xscale 
    395         self.yscale = yscale 
    396         self.xLabel = new_xlabel 
    397         self.yLabel = new_ylabel 
    398  
    399         # Plot the updated chart 
    400         self.plot(marker='o', linestyle='') 
     432        # Transform all the plots on the chart 
     433        for id in self.plot_dict.keys(): 
     434            current_plot = self.plot_dict[id] 
     435            if current_plot.id == "fit": 
     436                self.removePlot(id) 
     437                continue 
     438            new_xlabel, new_ylabel, xscale, yscale =\ 
     439                GuiUtils.xyTransform(current_plot, xLabel, yLabel) 
     440            self.xscale = xscale 
     441            self.yscale = yscale 
     442            self.xLabel = new_xlabel 
     443            self.yLabel = new_ylabel 
     444            # Plot the updated chart 
     445            self.removePlot(id) 
     446            self.plot(data=current_plot, marker='o', linestyle='') 
     447 
     448        pass # debug hook 
     449 
     450    def onFitDisplay(self, fit_data): 
     451        """ 
     452        Add a linear fitting line to the chart 
     453        """ 
     454        # Create new data structure with fitting result 
     455        tempx = fit_data[0] 
     456        tempy = fit_data[1] 
     457        self.fit_result.x = [] 
     458        self.fit_result.y = [] 
     459        self.fit_result.x = tempx 
     460        self.fit_result.y = tempy 
     461        self.fit_result.dx = None 
     462        self.fit_result.dy = None 
     463 
     464        #Remove another Fit, if exists 
     465        self.removePlot("fit") 
     466 
     467        self.fit_result.reset_view() 
     468        #self.offset_graph() 
     469 
     470        # Set plot properties 
     471        self.fit_result.id = 'fit' 
     472        self.fit_result.title = 'Fit' 
     473        self.fit_result.name = 'Fit' 
     474 
     475        # Plot the line 
     476        self.plot(data=self.fit_result, marker='', linestyle='solid', hide_error=True) 
    401477 
    402478 
Note: See TracChangeset for help on using the changeset viewer.