[49e124c] | 1 | import copy |
---|
| 2 | import numpy |
---|
| 3 | import pylab |
---|
| 4 | |
---|
| 5 | from PyQt4 import QtGui |
---|
| 6 | |
---|
[31c5b58] | 7 | DEFAULT_CMAP = pylab.cm.jet |
---|
[9290b1a] | 8 | from mpl_toolkits.mplot3d import Axes3D |
---|
[31c5b58] | 9 | |
---|
| 10 | import sas.qtgui.PlotUtilities as PlotUtilities |
---|
[ef01be4] | 11 | from sas.qtgui.PlotterBase import PlotterBase |
---|
[9290b1a] | 12 | from sas.sasgui.guiframe.dataFitting import Data2D |
---|
[49e124c] | 13 | |
---|
[fecfe28] | 14 | # Minimum value of Z for which we will present data. |
---|
| 15 | MIN_Z=-32 |
---|
| 16 | |
---|
[416fa8f] | 17 | class Plotter2DWidget(PlotterBase): |
---|
[c4e5400] | 18 | """ |
---|
| 19 | 2D Plot widget for use with a QDialog |
---|
[fecfe28] | 20 | """ |
---|
[416fa8f] | 21 | def __init__(self, parent=None, manager=None, quickplot=False, dimension=2): |
---|
[55d89f8] | 22 | self.dimension = dimension |
---|
[416fa8f] | 23 | super(Plotter2DWidget, self).__init__(parent, manager=manager, quickplot=quickplot) |
---|
[49e124c] | 24 | |
---|
[31c5b58] | 25 | @property |
---|
| 26 | def data(self): |
---|
| 27 | return self._data |
---|
| 28 | |
---|
| 29 | @data.setter |
---|
[49e124c] | 30 | def data(self, data=None): |
---|
| 31 | """ data setter """ |
---|
[14d9c7b] | 32 | self._data = data |
---|
[fecfe28] | 33 | self.qx_data = data.qx_data |
---|
| 34 | self.qy_data = data.qy_data |
---|
| 35 | self.xmin = data.xmin |
---|
| 36 | self.xmax = data.xmax |
---|
| 37 | self.ymin = data.ymin |
---|
| 38 | self.ymax = data.ymax |
---|
| 39 | self.zmin = data.zmin |
---|
| 40 | self.zmax = data.zmax |
---|
| 41 | self.label = data.name |
---|
| 42 | self.xLabel = "%s(%s)"%(data._xaxis, data._xunit) |
---|
| 43 | self.yLabel = "%s(%s)"%(data._yaxis, data._yunit) |
---|
[49e124c] | 44 | self.title(title=data.title) |
---|
| 45 | |
---|
[9290b1a] | 46 | def plot(self, data=None, marker=None, linestyle=None): |
---|
[49e124c] | 47 | """ |
---|
| 48 | Plot 2D self._data |
---|
| 49 | """ |
---|
[9290b1a] | 50 | # Assing data |
---|
| 51 | if isinstance(data, Data2D): |
---|
| 52 | self.data = data |
---|
| 53 | |
---|
| 54 | assert(self._data) |
---|
| 55 | |
---|
[64f1e93] | 56 | # Toggle the scale |
---|
[55d89f8] | 57 | zmin_2D_temp = self.zmin |
---|
| 58 | zmax_2D_temp = self.zmax |
---|
[fecfe28] | 59 | # self.scale predefined in the baseclass |
---|
[55d89f8] | 60 | if self.scale == 'log_{10}': |
---|
| 61 | self.scale = 'linear' |
---|
| 62 | if not self.zmin is None: |
---|
[cad617b] | 63 | zmin_2D_temp = numpy.power(10, self.zmin) |
---|
[55d89f8] | 64 | if not self.zmax is None: |
---|
[cad617b] | 65 | zmax_2D_temp = numpy.power(10, self.zmax) |
---|
[55d89f8] | 66 | else: |
---|
| 67 | self.scale = 'log_{10}' |
---|
| 68 | if not self.zmin is None: |
---|
| 69 | # min log value: no log(negative) |
---|
| 70 | if self.zmin <= 0: |
---|
[fecfe28] | 71 | zmin_2D_temp = MIN_Z |
---|
[55d89f8] | 72 | else: |
---|
| 73 | zmin_2D_temp = numpy.log10(self.zmin) |
---|
| 74 | if not self.zmax is None: |
---|
| 75 | zmax_2D_temp = numpy.log10(self.zmax) |
---|
[31c5b58] | 76 | |
---|
[64f1e93] | 77 | # Prepare and show the plot |
---|
| 78 | self.showPlot(data=self.data.data, |
---|
| 79 | qx_data=self.qx_data, |
---|
| 80 | qy_data=self.qy_data, |
---|
| 81 | xmin=self.xmin, |
---|
| 82 | xmax=self.xmax, |
---|
| 83 | ymin=self.ymin, ymax=self.ymax, |
---|
| 84 | cmap=self.cmap, zmin=zmin_2D_temp, |
---|
| 85 | zmax=zmax_2D_temp) |
---|
[6d05e1d] | 86 | |
---|
[b46f285] | 87 | def createContextMenu(self): |
---|
[c4e5400] | 88 | """ |
---|
| 89 | Define common context menu and associated actions for the MPL widget |
---|
| 90 | """ |
---|
| 91 | self.defaultContextMenu() |
---|
| 92 | |
---|
[b46f285] | 93 | def createContextMenuQuick(self): |
---|
[6d05e1d] | 94 | """ |
---|
| 95 | Define context menu and associated actions for the quickplot MPL widget |
---|
| 96 | """ |
---|
[c4e5400] | 97 | self.defaultContextMenu() |
---|
| 98 | |
---|
[55d89f8] | 99 | if self.dimension == 2: |
---|
| 100 | self.actionToggleGrid = self.contextMenu.addAction("Toggle Grid On/Off") |
---|
| 101 | self.contextMenu.addSeparator() |
---|
[6d05e1d] | 102 | self.actionChangeScale = self.contextMenu.addAction("Toggle Linear/Log Scale") |
---|
| 103 | |
---|
| 104 | # Define the callbacks |
---|
[c4e5400] | 105 | self.actionChangeScale.triggered.connect(self.onToggleScale) |
---|
[55d89f8] | 106 | if self.dimension == 2: |
---|
| 107 | self.actionToggleGrid.triggered.connect(self.onGridToggle) |
---|
[6d05e1d] | 108 | |
---|
| 109 | def onToggleScale(self, event): |
---|
| 110 | """ |
---|
| 111 | Toggle axis and replot image |
---|
| 112 | """ |
---|
[55d89f8] | 113 | self.plot() |
---|
[6d05e1d] | 114 | |
---|
[64f1e93] | 115 | def showPlot(self, data, qx_data, qy_data, xmin, xmax, ymin, ymax, |
---|
| 116 | zmin, zmax, color=0, symbol=0, markersize=0, |
---|
| 117 | label='data2D', cmap=DEFAULT_CMAP): |
---|
[6d05e1d] | 118 | """ |
---|
[64f1e93] | 119 | Render and show the current data |
---|
[6d05e1d] | 120 | """ |
---|
| 121 | self.qx_data = qx_data |
---|
| 122 | self.qy_data = qy_data |
---|
| 123 | self.xmin = xmin |
---|
| 124 | self.xmax = xmax |
---|
| 125 | self.ymin = ymin |
---|
| 126 | self.ymax = ymax |
---|
| 127 | self.zmin = zmin |
---|
| 128 | self.zmax = zmax |
---|
| 129 | # If we don't have any data, skip. |
---|
[fecfe28] | 130 | if data is None: |
---|
[6d05e1d] | 131 | return |
---|
| 132 | if data.ndim == 1: |
---|
| 133 | output = PlotUtilities.build_matrix(data, self.qx_data, self.qy_data) |
---|
| 134 | else: |
---|
| 135 | output = copy.deepcopy(data) |
---|
| 136 | |
---|
| 137 | zmin_temp = self.zmin |
---|
| 138 | # check scale |
---|
| 139 | if self.scale == 'log_{10}': |
---|
| 140 | try: |
---|
| 141 | if self.zmin <= 0 and len(output[output > 0]) > 0: |
---|
| 142 | zmin_temp = self.zmin_2D |
---|
| 143 | output[output > 0] = numpy.log10(output[output > 0]) |
---|
| 144 | elif self.zmin <= 0: |
---|
| 145 | zmin_temp = self.zmin |
---|
| 146 | output[output > 0] = numpy.zeros(len(output)) |
---|
[fecfe28] | 147 | output[output <= 0] = MIN_Z |
---|
[6d05e1d] | 148 | else: |
---|
| 149 | zmin_temp = self.zmin |
---|
| 150 | output[output > 0] = numpy.log10(output[output > 0]) |
---|
| 151 | except: |
---|
| 152 | #Too many problems in 2D plot with scale |
---|
| 153 | output[output > 0] = numpy.log10(output[output > 0]) |
---|
| 154 | pass |
---|
| 155 | |
---|
| 156 | self.cmap = cmap |
---|
| 157 | if self.dimension != 3: |
---|
| 158 | #Re-adjust colorbar |
---|
| 159 | self.figure.subplots_adjust(left=0.2, right=.8, bottom=.2) |
---|
| 160 | |
---|
| 161 | im = self.ax.imshow(output, interpolation='nearest', |
---|
| 162 | origin='lower', |
---|
| 163 | vmin=zmin_temp, vmax=self.zmax, |
---|
| 164 | cmap=self.cmap, |
---|
| 165 | extent=(self.xmin, self.xmax, |
---|
| 166 | self.ymin, self.ymax)) |
---|
| 167 | |
---|
| 168 | cbax = self.figure.add_axes([0.84, 0.2, 0.02, 0.7]) |
---|
[b4b8589] | 169 | |
---|
| 170 | # Current labels for axes |
---|
| 171 | self.ax.set_ylabel(self.y_label) |
---|
| 172 | self.ax.set_xlabel(self.x_label) |
---|
| 173 | |
---|
| 174 | # Title only for regular charts |
---|
| 175 | if not self.quickplot: |
---|
| 176 | self.ax.set_title(label=self._title) |
---|
| 177 | |
---|
[fecfe28] | 178 | if cbax is None: |
---|
| 179 | ax.set_frame_on(False) |
---|
| 180 | cb = self.figure.colorbar(im, shrink=0.8, aspect=20) |
---|
| 181 | else: |
---|
| 182 | cb = self.figure.colorbar(im, cax=cbax) |
---|
| 183 | |
---|
| 184 | cb.update_bruteforce(im) |
---|
| 185 | cb.set_label('$' + self.scale + '$') |
---|
[b4b8589] | 186 | |
---|
[6d05e1d] | 187 | else: |
---|
| 188 | # clear the previous 2D from memory |
---|
| 189 | self.figure.clear() |
---|
| 190 | |
---|
| 191 | self.figure.subplots_adjust(left=0.1, right=.8, bottom=.1) |
---|
| 192 | |
---|
[55d89f8] | 193 | X = self._data.x_bins[0:-1] |
---|
| 194 | Y = self._data.y_bins[0:-1] |
---|
[6d05e1d] | 195 | X, Y = numpy.meshgrid(X, Y) |
---|
| 196 | |
---|
[64f1e93] | 197 | ax = Axes3D(self.figure) |
---|
[fecfe28] | 198 | #cbax = self.figure.add_axes([0.84, 0.1, 0.02, 0.8]) |
---|
[b4b8589] | 199 | |
---|
[64f1e93] | 200 | # Disable rotation for large sets. |
---|
| 201 | # TODO: Define "large" for a dataset |
---|
| 202 | SET_TOO_LARGE = 500 |
---|
| 203 | if len(X) > SET_TOO_LARGE: |
---|
| 204 | ax.disable_mouse_rotation() |
---|
| 205 | |
---|
[6d05e1d] | 206 | self.figure.canvas.resizing = False |
---|
| 207 | im = ax.plot_surface(X, Y, output, rstride=1, cstride=1, cmap=cmap, |
---|
| 208 | linewidth=0, antialiased=False) |
---|
[55d89f8] | 209 | self.ax.set_axis_off() |
---|
[6d05e1d] | 210 | |
---|
| 211 | if self.dimension != 3: |
---|
| 212 | self.figure.canvas.draw_idle() |
---|
| 213 | else: |
---|
| 214 | self.figure.canvas.draw() |
---|
[416fa8f] | 215 | |
---|
| 216 | class Plotter2D(QtGui.QDialog, Plotter2DWidget): |
---|
| 217 | def __init__(self, parent=None, quickplot=False, dimension=2): |
---|
| 218 | |
---|
| 219 | QtGui.QDialog.__init__(self) |
---|
[cad617b] | 220 | Plotter2DWidget.__init__(self, manager=parent, quickplot=quickplot, dimension=dimension) |
---|
[c4e5400] | 221 | icon = QtGui.QIcon() |
---|
| 222 | icon.addPixmap(QtGui.QPixmap(":/res/ball.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off) |
---|
| 223 | self.setWindowIcon(icon) |
---|