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