[49e124c] | 1 | import copy |
---|
| 2 | import numpy |
---|
| 3 | import pylab |
---|
[092a3d9] | 4 | import functools |
---|
[49e124c] | 5 | |
---|
[4992ff2] | 6 | from PyQt5 import QtCore |
---|
| 7 | from PyQt5 import QtGui |
---|
| 8 | from PyQt5 import QtWidgets |
---|
[49e124c] | 9 | |
---|
[31c5b58] | 10 | DEFAULT_CMAP = pylab.cm.jet |
---|
[4992ff2] | 11 | |
---|
| 12 | #import sys |
---|
| 13 | #print("SYS.PATH = ", sys.path) |
---|
| 14 | import matplotlib as mpl |
---|
| 15 | mpl.use("Qt5Agg") |
---|
| 16 | |
---|
[9290b1a] | 17 | from mpl_toolkits.mplot3d import Axes3D |
---|
[31c5b58] | 18 | |
---|
[dc5ef15] | 19 | from sas.sascalc.dataloader.manipulations import CircularAverage |
---|
| 20 | |
---|
| 21 | from sas.qtgui.Plotting.PlotterData import Data1D |
---|
| 22 | from sas.qtgui.Plotting.PlotterData import Data2D |
---|
| 23 | |
---|
[83eb5208] | 24 | import sas.qtgui.Plotting.PlotUtilities as PlotUtilities |
---|
| 25 | import sas.qtgui.Utilities.GuiUtils as GuiUtils |
---|
| 26 | from sas.qtgui.Plotting.PlotterBase import PlotterBase |
---|
| 27 | from sas.qtgui.Plotting.ColorMap import ColorMap |
---|
| 28 | from sas.qtgui.Plotting.BoxSum import BoxSum |
---|
| 29 | from sas.qtgui.Plotting.SlicerParameters import SlicerParameters |
---|
[dc5ef15] | 30 | from sas.qtgui.Plotting.Binder import BindArtist |
---|
| 31 | |
---|
| 32 | # TODO: move to sas.qtgui namespace |
---|
| 33 | from sas.qtgui.Plotting.Slicers.BoxSlicer import BoxInteractorX |
---|
| 34 | from sas.qtgui.Plotting.Slicers.BoxSlicer import BoxInteractorY |
---|
| 35 | from sas.qtgui.Plotting.Slicers.AnnulusSlicer import AnnulusInteractor |
---|
| 36 | from sas.qtgui.Plotting.Slicers.SectorSlicer import SectorInteractor |
---|
| 37 | from sas.qtgui.Plotting.Slicers.BoxSum import BoxSumCalculator |
---|
[49e124c] | 38 | |
---|
[fecfe28] | 39 | # Minimum value of Z for which we will present data. |
---|
[3bdbfcc] | 40 | MIN_Z = -32 |
---|
[fecfe28] | 41 | |
---|
[416fa8f] | 42 | class Plotter2DWidget(PlotterBase): |
---|
[c4e5400] | 43 | """ |
---|
| 44 | 2D Plot widget for use with a QDialog |
---|
[fecfe28] | 45 | """ |
---|
[416fa8f] | 46 | def __init__(self, parent=None, manager=None, quickplot=False, dimension=2): |
---|
[55d89f8] | 47 | self.dimension = dimension |
---|
[416fa8f] | 48 | super(Plotter2DWidget, self).__init__(parent, manager=manager, quickplot=quickplot) |
---|
[49e124c] | 49 | |
---|
[092a3d9] | 50 | self.cmap = DEFAULT_CMAP.name |
---|
| 51 | # Default scale |
---|
| 52 | self.scale = 'log_{10}' |
---|
[3bdbfcc] | 53 | # to set the order of lines drawn first. |
---|
| 54 | self.slicer_z = 5 |
---|
| 55 | # Reference to the current slicer |
---|
| 56 | self.slicer = None |
---|
[57b7ee2] | 57 | self.slicer_widget = None |
---|
[3bdbfcc] | 58 | # Create Artist and bind it |
---|
| 59 | self.connect = BindArtist(self.figure) |
---|
[5d89f43] | 60 | self.vmin = None |
---|
| 61 | self.vmax = None |
---|
[092a3d9] | 62 | |
---|
[116260a] | 63 | self.manager = manager |
---|
| 64 | |
---|
[31c5b58] | 65 | @property |
---|
| 66 | def data(self): |
---|
| 67 | return self._data |
---|
| 68 | |
---|
| 69 | @data.setter |
---|
[49e124c] | 70 | def data(self, data=None): |
---|
| 71 | """ data setter """ |
---|
[14d9c7b] | 72 | self._data = data |
---|
[fecfe28] | 73 | self.qx_data = data.qx_data |
---|
| 74 | self.qy_data = data.qy_data |
---|
| 75 | self.xmin = data.xmin |
---|
| 76 | self.xmax = data.xmax |
---|
| 77 | self.ymin = data.ymin |
---|
| 78 | self.ymax = data.ymax |
---|
| 79 | self.zmin = data.zmin |
---|
| 80 | self.zmax = data.zmax |
---|
| 81 | self.label = data.name |
---|
| 82 | self.xLabel = "%s(%s)"%(data._xaxis, data._xunit) |
---|
| 83 | self.yLabel = "%s(%s)"%(data._yaxis, data._yunit) |
---|
[49e124c] | 84 | self.title(title=data.title) |
---|
| 85 | |
---|
[3bdbfcc] | 86 | @property |
---|
| 87 | def item(self): |
---|
| 88 | ''' getter for this plot's QStandardItem ''' |
---|
| 89 | return self._item |
---|
| 90 | |
---|
| 91 | @item.setter |
---|
| 92 | def item(self, item=None): |
---|
| 93 | ''' setter for this plot's QStandardItem ''' |
---|
| 94 | self._item = item |
---|
| 95 | |
---|
[d5c5d3d] | 96 | def plot(self, data=None, marker=None, show_colorbar=True): |
---|
[49e124c] | 97 | """ |
---|
| 98 | Plot 2D self._data |
---|
[5236449] | 99 | marker - unused |
---|
[49e124c] | 100 | """ |
---|
[9290b1a] | 101 | # Assing data |
---|
| 102 | if isinstance(data, Data2D): |
---|
| 103 | self.data = data |
---|
| 104 | |
---|
[3bdbfcc] | 105 | assert self._data |
---|
[9290b1a] | 106 | |
---|
[64f1e93] | 107 | # Toggle the scale |
---|
[092a3d9] | 108 | zmin_2D_temp, zmax_2D_temp = self.calculateDepth() |
---|
[31c5b58] | 109 | |
---|
[64f1e93] | 110 | # Prepare and show the plot |
---|
| 111 | self.showPlot(data=self.data.data, |
---|
| 112 | qx_data=self.qx_data, |
---|
| 113 | qy_data=self.qy_data, |
---|
| 114 | xmin=self.xmin, |
---|
| 115 | xmax=self.xmax, |
---|
| 116 | ymin=self.ymin, ymax=self.ymax, |
---|
| 117 | cmap=self.cmap, zmin=zmin_2D_temp, |
---|
[d5c5d3d] | 118 | zmax=zmax_2D_temp, show_colorbar=show_colorbar) |
---|
[6d05e1d] | 119 | |
---|
[092a3d9] | 120 | def calculateDepth(self): |
---|
| 121 | """ |
---|
| 122 | Re-calculate the plot depth parameters depending on the scale |
---|
| 123 | """ |
---|
| 124 | # Toggle the scale |
---|
[fbfc488] | 125 | zmin_temp = self.zmin if self.zmin else MIN_Z |
---|
[092a3d9] | 126 | zmax_temp = self.zmax |
---|
| 127 | # self.scale predefined in the baseclass |
---|
| 128 | if self.scale == 'log_{10}': |
---|
| 129 | if self.zmin is not None: |
---|
| 130 | zmin_temp = numpy.power(10, self.zmin) |
---|
| 131 | if self.zmax is not None: |
---|
| 132 | zmax_temp = numpy.power(10, self.zmax) |
---|
| 133 | else: |
---|
| 134 | if self.zmin is not None: |
---|
| 135 | # min log value: no log(negative) |
---|
| 136 | zmin_temp = MIN_Z if self.zmin <= 0 else numpy.log10(self.zmin) |
---|
| 137 | if self.zmax is not None: |
---|
| 138 | zmax_temp = numpy.log10(self.zmax) |
---|
| 139 | |
---|
| 140 | return (zmin_temp, zmax_temp) |
---|
| 141 | |
---|
| 142 | |
---|
[b46f285] | 143 | def createContextMenu(self): |
---|
[c4e5400] | 144 | """ |
---|
| 145 | Define common context menu and associated actions for the MPL widget |
---|
| 146 | """ |
---|
| 147 | self.defaultContextMenu() |
---|
| 148 | |
---|
[092a3d9] | 149 | self.contextMenu.addSeparator() |
---|
| 150 | self.actionDataInfo = self.contextMenu.addAction("&DataInfo") |
---|
| 151 | self.actionDataInfo.triggered.connect( |
---|
[161713c] | 152 | functools.partial(self.onDataInfo, self.data)) |
---|
[092a3d9] | 153 | |
---|
| 154 | self.actionSavePointsAsFile = self.contextMenu.addAction("&Save Points as a File") |
---|
| 155 | self.actionSavePointsAsFile.triggered.connect( |
---|
[161713c] | 156 | functools.partial(self.onSavePoints, self.data)) |
---|
[092a3d9] | 157 | self.contextMenu.addSeparator() |
---|
| 158 | |
---|
| 159 | self.actionCircularAverage = self.contextMenu.addAction("&Perform Circular Average") |
---|
| 160 | self.actionCircularAverage.triggered.connect(self.onCircularAverage) |
---|
| 161 | |
---|
| 162 | self.actionSectorView = self.contextMenu.addAction("&Sector [Q View]") |
---|
| 163 | self.actionSectorView.triggered.connect(self.onSectorView) |
---|
| 164 | self.actionAnnulusView = self.contextMenu.addAction("&Annulus [Phi View]") |
---|
| 165 | self.actionAnnulusView.triggered.connect(self.onAnnulusView) |
---|
| 166 | self.actionBoxSum = self.contextMenu.addAction("&Box Sum") |
---|
| 167 | self.actionBoxSum.triggered.connect(self.onBoxSum) |
---|
| 168 | self.actionBoxAveragingX = self.contextMenu.addAction("&Box Averaging in Qx") |
---|
| 169 | self.actionBoxAveragingX.triggered.connect(self.onBoxAveragingX) |
---|
| 170 | self.actionBoxAveragingY = self.contextMenu.addAction("&Box Averaging in Qy") |
---|
| 171 | self.actionBoxAveragingY.triggered.connect(self.onBoxAveragingY) |
---|
[3bdbfcc] | 172 | # Additional items for slicer interaction |
---|
| 173 | if self.slicer: |
---|
| 174 | self.actionClearSlicer = self.contextMenu.addAction("&Clear Slicer") |
---|
| 175 | self.actionClearSlicer.triggered.connect(self.onClearSlicer) |
---|
[57b7ee2] | 176 | if self.slicer.__class__.__name__ != "BoxSumCalculator": |
---|
| 177 | self.actionEditSlicer = self.contextMenu.addAction("&Edit Slicer Parameters") |
---|
| 178 | self.actionEditSlicer.triggered.connect(self.onEditSlicer) |
---|
[092a3d9] | 179 | self.contextMenu.addSeparator() |
---|
| 180 | self.actionColorMap = self.contextMenu.addAction("&2D Color Map") |
---|
| 181 | self.actionColorMap.triggered.connect(self.onColorMap) |
---|
| 182 | self.contextMenu.addSeparator() |
---|
| 183 | self.actionChangeScale = self.contextMenu.addAction("Toggle Linear/Log Scale") |
---|
| 184 | self.actionChangeScale.triggered.connect(self.onToggleScale) |
---|
| 185 | |
---|
[b46f285] | 186 | def createContextMenuQuick(self): |
---|
[6d05e1d] | 187 | """ |
---|
| 188 | Define context menu and associated actions for the quickplot MPL widget |
---|
| 189 | """ |
---|
[c4e5400] | 190 | self.defaultContextMenu() |
---|
| 191 | |
---|
[55d89f8] | 192 | if self.dimension == 2: |
---|
| 193 | self.actionToggleGrid = self.contextMenu.addAction("Toggle Grid On/Off") |
---|
| 194 | self.contextMenu.addSeparator() |
---|
[6d05e1d] | 195 | self.actionChangeScale = self.contextMenu.addAction("Toggle Linear/Log Scale") |
---|
| 196 | |
---|
| 197 | # Define the callbacks |
---|
[c4e5400] | 198 | self.actionChangeScale.triggered.connect(self.onToggleScale) |
---|
[55d89f8] | 199 | if self.dimension == 2: |
---|
| 200 | self.actionToggleGrid.triggered.connect(self.onGridToggle) |
---|
[6d05e1d] | 201 | |
---|
| 202 | def onToggleScale(self, event): |
---|
| 203 | """ |
---|
| 204 | Toggle axis and replot image |
---|
| 205 | """ |
---|
[092a3d9] | 206 | # self.scale predefined in the baseclass |
---|
| 207 | if self.scale == 'log_{10}': |
---|
| 208 | self.scale = 'linear' |
---|
| 209 | else: |
---|
| 210 | self.scale = 'log_{10}' |
---|
| 211 | |
---|
[55d89f8] | 212 | self.plot() |
---|
[6d05e1d] | 213 | |
---|
[3bdbfcc] | 214 | def onClearSlicer(self): |
---|
| 215 | """ |
---|
| 216 | Remove all sclicers from the chart |
---|
| 217 | """ |
---|
[b789967] | 218 | if self.slicer is None: |
---|
| 219 | return |
---|
| 220 | |
---|
| 221 | self.slicer.clear() |
---|
| 222 | self.canvas.draw() |
---|
| 223 | self.slicer = None |
---|
[3bdbfcc] | 224 | |
---|
| 225 | def onEditSlicer(self): |
---|
| 226 | """ |
---|
| 227 | Present a small dialog for manipulating the current slicer |
---|
| 228 | """ |
---|
| 229 | assert self.slicer |
---|
[57b7ee2] | 230 | # Only show the dialog if not currently shown |
---|
| 231 | if self.slicer_widget: |
---|
| 232 | return |
---|
| 233 | def slicer_closed(): |
---|
| 234 | # Need to disconnect the signal!! |
---|
| 235 | self.slicer_widget.close_signal.disconnect() |
---|
| 236 | # reset slicer_widget on "Edit Slicer Parameters" window close |
---|
| 237 | self.slicer_widget = None |
---|
[3bdbfcc] | 238 | |
---|
| 239 | self.param_model = self.slicer.model() |
---|
[57b7ee2] | 240 | # Pass the model to the Slicer Parameters widget |
---|
[161713c] | 241 | self.slicer_widget = SlicerParameters(model=self.param_model, |
---|
| 242 | validate_method=self.slicer.validate) |
---|
[57b7ee2] | 243 | self.slicer_widget.close_signal.connect(slicer_closed) |
---|
[9a05a8d5] | 244 | # Add the plot to the workspace |
---|
[7969b9c] | 245 | self.manager.parent.workspace().addSubWindow(self.slicer_widget) |
---|
[b789967] | 246 | |
---|
[3bdbfcc] | 247 | self.slicer_widget.show() |
---|
| 248 | |
---|
[092a3d9] | 249 | def onCircularAverage(self): |
---|
| 250 | """ |
---|
[3bdbfcc] | 251 | Perform circular averaging on Data2D |
---|
[092a3d9] | 252 | """ |
---|
[3bdbfcc] | 253 | # Find the best number of bins |
---|
| 254 | npt = numpy.sqrt(len(self.data.data[numpy.isfinite(self.data.data)])) |
---|
| 255 | npt = numpy.floor(npt) |
---|
| 256 | # compute the maximum radius of data2D |
---|
| 257 | self.qmax = max(numpy.fabs(self.data.xmax), |
---|
| 258 | numpy.fabs(self.data.xmin)) |
---|
| 259 | self.ymax = max(numpy.fabs(self.data.ymax), |
---|
| 260 | numpy.fabs(self.data.ymin)) |
---|
| 261 | self.radius = numpy.sqrt(numpy.power(self.qmax, 2) + numpy.power(self.ymax, 2)) |
---|
| 262 | #Compute beam width |
---|
| 263 | bin_width = (self.qmax + self.qmax) / npt |
---|
| 264 | # Create data1D circular average of data2D |
---|
| 265 | circle = CircularAverage(r_min=0, r_max=self.radius, bin_width=bin_width) |
---|
| 266 | circ = circle(self.data) |
---|
| 267 | dxl = circ.dxl if hasattr(circ, "dxl") else None |
---|
| 268 | dxw = circ.dxw if hasattr(circ, "dxw") else None |
---|
| 269 | |
---|
| 270 | new_plot = Data1D(x=circ.x, y=circ.y, dy=circ.dy, dx=circ.dx) |
---|
| 271 | new_plot.dxl = dxl |
---|
| 272 | new_plot.dxw = dxw |
---|
| 273 | new_plot.name = new_plot.title = "Circ avg " + self.data.name |
---|
| 274 | new_plot.source = self.data.source |
---|
| 275 | new_plot.interactive = True |
---|
| 276 | new_plot.detector = self.data.detector |
---|
| 277 | |
---|
| 278 | # Define axes if not done yet. |
---|
| 279 | new_plot.xaxis("\\rm{Q}", "A^{-1}") |
---|
| 280 | if hasattr(self.data, "scale") and \ |
---|
| 281 | self.data.scale == 'linear': |
---|
| 282 | new_plot.ytransform = 'y' |
---|
| 283 | new_plot.yaxis("\\rm{Residuals} ", "normalized") |
---|
| 284 | else: |
---|
| 285 | new_plot.yaxis("\\rm{Intensity} ", "cm^{-1}") |
---|
| 286 | |
---|
| 287 | new_plot.group_id = "2daverage" + self.data.name |
---|
| 288 | new_plot.id = "Circ avg " + self.data.name |
---|
| 289 | new_plot.is_data = True |
---|
[cee5c78] | 290 | GuiUtils.updateModelItemWithPlot(self._item, new_plot, new_plot.id) |
---|
[116260a] | 291 | self.manager.communicator.plotUpdateSignal.emit([new_plot]) |
---|
[3bdbfcc] | 292 | |
---|
| 293 | def setSlicer(self, slicer): |
---|
| 294 | """ |
---|
| 295 | Clear the previous slicer and create a new one. |
---|
| 296 | slicer: slicer class to create |
---|
| 297 | """ |
---|
| 298 | # Clear current slicer |
---|
| 299 | if self.slicer is not None: |
---|
| 300 | self.slicer.clear() |
---|
| 301 | # Create a new slicer |
---|
| 302 | self.slicer_z += 1 |
---|
| 303 | self.slicer = slicer(self, self.ax, item=self._item, zorder=self.slicer_z) |
---|
| 304 | self.ax.set_ylim(self.data.ymin, self.data.ymax) |
---|
| 305 | self.ax.set_xlim(self.data.xmin, self.data.xmax) |
---|
| 306 | # Draw slicer |
---|
| 307 | self.figure.canvas.draw() |
---|
| 308 | self.slicer.update() |
---|
[092a3d9] | 309 | |
---|
[57b7ee2] | 310 | # Reset the model on the Edit slicer parameters widget |
---|
| 311 | self.param_model = self.slicer.model() |
---|
| 312 | if self.slicer_widget: |
---|
| 313 | self.slicer_widget.setModel(self.param_model) |
---|
| 314 | |
---|
[092a3d9] | 315 | def onSectorView(self): |
---|
| 316 | """ |
---|
[3bdbfcc] | 317 | Perform sector averaging on Q and draw sector slicer |
---|
[092a3d9] | 318 | """ |
---|
[3bdbfcc] | 319 | self.setSlicer(slicer=SectorInteractor) |
---|
[092a3d9] | 320 | |
---|
| 321 | def onAnnulusView(self): |
---|
| 322 | """ |
---|
[3bdbfcc] | 323 | Perform sector averaging on Phi and draw annulus slicer |
---|
[092a3d9] | 324 | """ |
---|
[3bdbfcc] | 325 | self.setSlicer(slicer=AnnulusInteractor) |
---|
[092a3d9] | 326 | |
---|
| 327 | def onBoxSum(self): |
---|
| 328 | """ |
---|
[3bdbfcc] | 329 | Perform 2D Data averaging Qx and Qy. |
---|
| 330 | Display box slicer details. |
---|
[092a3d9] | 331 | """ |
---|
[3bdbfcc] | 332 | self.onClearSlicer() |
---|
| 333 | self.slicer_z += 1 |
---|
| 334 | self.slicer = BoxSumCalculator(self, self.ax, zorder=self.slicer_z) |
---|
| 335 | |
---|
| 336 | self.ax.set_ylim(self.data.ymin, self.data.ymax) |
---|
| 337 | self.ax.set_xlim(self.data.xmin, self.data.xmax) |
---|
| 338 | self.figure.canvas.draw() |
---|
| 339 | self.slicer.update() |
---|
| 340 | |
---|
| 341 | # Get the BoxSumCalculator model. |
---|
| 342 | self.box_sum_model = self.slicer.model() |
---|
| 343 | # Pass the BoxSumCalculator model to the BoxSum widget |
---|
| 344 | self.boxwidget = BoxSum(self, model=self.box_sum_model) |
---|
| 345 | # Add the plot to the workspace |
---|
[7969b9c] | 346 | self.manager.parent.workspace().addSubWindow(self.boxwidget) |
---|
[3bdbfcc] | 347 | self.boxwidget.show() |
---|
[092a3d9] | 348 | |
---|
| 349 | def onBoxAveragingX(self): |
---|
| 350 | """ |
---|
[3bdbfcc] | 351 | Perform 2D data averaging on Qx |
---|
| 352 | Create a new slicer. |
---|
[092a3d9] | 353 | """ |
---|
[3bdbfcc] | 354 | self.setSlicer(slicer=BoxInteractorX) |
---|
[092a3d9] | 355 | |
---|
| 356 | def onBoxAveragingY(self): |
---|
| 357 | """ |
---|
[3bdbfcc] | 358 | Perform 2D data averaging on Qy |
---|
| 359 | Create a new slicer . |
---|
[092a3d9] | 360 | """ |
---|
[3bdbfcc] | 361 | self.setSlicer(slicer=BoxInteractorY) |
---|
[092a3d9] | 362 | |
---|
| 363 | def onColorMap(self): |
---|
| 364 | """ |
---|
| 365 | Display the color map dialog and modify the plot's map accordingly |
---|
| 366 | """ |
---|
| 367 | color_map_dialog = ColorMap(self, cmap=self.cmap, |
---|
[03c372d] | 368 | vmin=self.vmin, |
---|
| 369 | vmax=self.vmax, |
---|
[092a3d9] | 370 | data=self.data) |
---|
| 371 | |
---|
[5d89f43] | 372 | color_map_dialog.apply_signal.connect(self.onApplyMap) |
---|
| 373 | |
---|
[4992ff2] | 374 | if color_map_dialog.exec_() == QtWidgets.QDialog.Accepted: |
---|
[5d89f43] | 375 | self.onApplyMap(color_map_dialog.norm(), color_map_dialog.cmap()) |
---|
| 376 | |
---|
| 377 | def onApplyMap(self, v_values, cmap): |
---|
| 378 | """ |
---|
| 379 | Update the chart color map based on data passed from the widget |
---|
| 380 | """ |
---|
| 381 | self.cmap = str(cmap) |
---|
| 382 | self.vmin, self.vmax = v_values |
---|
| 383 | # Redraw the chart with new cmap |
---|
| 384 | self.plot() |
---|
[092a3d9] | 385 | |
---|
[64f1e93] | 386 | def showPlot(self, data, qx_data, qy_data, xmin, xmax, ymin, ymax, |
---|
[d5c5d3d] | 387 | zmin, zmax, label='data2D', cmap=DEFAULT_CMAP, show_colorbar=True): |
---|
[6d05e1d] | 388 | """ |
---|
[64f1e93] | 389 | Render and show the current data |
---|
[6d05e1d] | 390 | """ |
---|
| 391 | self.qx_data = qx_data |
---|
| 392 | self.qy_data = qy_data |
---|
| 393 | self.xmin = xmin |
---|
| 394 | self.xmax = xmax |
---|
| 395 | self.ymin = ymin |
---|
| 396 | self.ymax = ymax |
---|
| 397 | self.zmin = zmin |
---|
| 398 | self.zmax = zmax |
---|
| 399 | # If we don't have any data, skip. |
---|
[fecfe28] | 400 | if data is None: |
---|
[6d05e1d] | 401 | return |
---|
[f4a1433] | 402 | if data.ndim == 0: |
---|
| 403 | return |
---|
| 404 | elif data.ndim == 1: |
---|
[6d05e1d] | 405 | output = PlotUtilities.build_matrix(data, self.qx_data, self.qy_data) |
---|
| 406 | else: |
---|
| 407 | output = copy.deepcopy(data) |
---|
| 408 | |
---|
| 409 | zmin_temp = self.zmin |
---|
| 410 | # check scale |
---|
| 411 | if self.scale == 'log_{10}': |
---|
| 412 | try: |
---|
| 413 | if self.zmin <= 0 and len(output[output > 0]) > 0: |
---|
[092a3d9] | 414 | zmin_temp = self.zmin |
---|
[6d05e1d] | 415 | output[output > 0] = numpy.log10(output[output > 0]) |
---|
| 416 | elif self.zmin <= 0: |
---|
| 417 | zmin_temp = self.zmin |
---|
| 418 | output[output > 0] = numpy.zeros(len(output)) |
---|
[fecfe28] | 419 | output[output <= 0] = MIN_Z |
---|
[6d05e1d] | 420 | else: |
---|
| 421 | zmin_temp = self.zmin |
---|
| 422 | output[output > 0] = numpy.log10(output[output > 0]) |
---|
| 423 | except: |
---|
| 424 | #Too many problems in 2D plot with scale |
---|
| 425 | output[output > 0] = numpy.log10(output[output > 0]) |
---|
| 426 | pass |
---|
| 427 | |
---|
| 428 | self.cmap = cmap |
---|
| 429 | if self.dimension != 3: |
---|
| 430 | #Re-adjust colorbar |
---|
| 431 | self.figure.subplots_adjust(left=0.2, right=.8, bottom=.2) |
---|
| 432 | |
---|
[5d89f43] | 433 | zmax_temp = self.zmax |
---|
| 434 | if self.vmin is not None: |
---|
| 435 | zmin_temp = self.vmin |
---|
| 436 | zmax_temp = self.vmax |
---|
| 437 | |
---|
[6d05e1d] | 438 | im = self.ax.imshow(output, interpolation='nearest', |
---|
[01cda57] | 439 | # origin='lower', |
---|
[5d89f43] | 440 | vmin=zmin_temp, vmax=zmax_temp, |
---|
[6d05e1d] | 441 | cmap=self.cmap, |
---|
| 442 | extent=(self.xmin, self.xmax, |
---|
| 443 | self.ymin, self.ymax)) |
---|
| 444 | |
---|
| 445 | cbax = self.figure.add_axes([0.84, 0.2, 0.02, 0.7]) |
---|
[b4b8589] | 446 | |
---|
| 447 | # Current labels for axes |
---|
| 448 | self.ax.set_ylabel(self.y_label) |
---|
| 449 | self.ax.set_xlabel(self.x_label) |
---|
| 450 | |
---|
| 451 | # Title only for regular charts |
---|
| 452 | if not self.quickplot: |
---|
| 453 | self.ax.set_title(label=self._title) |
---|
| 454 | |
---|
[fecfe28] | 455 | if cbax is None: |
---|
| 456 | ax.set_frame_on(False) |
---|
| 457 | cb = self.figure.colorbar(im, shrink=0.8, aspect=20) |
---|
| 458 | else: |
---|
| 459 | cb = self.figure.colorbar(im, cax=cbax) |
---|
| 460 | |
---|
| 461 | cb.update_bruteforce(im) |
---|
| 462 | cb.set_label('$' + self.scale + '$') |
---|
[b4b8589] | 463 | |
---|
[092a3d9] | 464 | self.vmin = cb.vmin |
---|
| 465 | self.vmax = cb.vmax |
---|
| 466 | |
---|
[d5c5d3d] | 467 | if show_colorbar is False: |
---|
| 468 | cb.remove() |
---|
| 469 | |
---|
[6d05e1d] | 470 | else: |
---|
| 471 | # clear the previous 2D from memory |
---|
| 472 | self.figure.clear() |
---|
| 473 | |
---|
| 474 | self.figure.subplots_adjust(left=0.1, right=.8, bottom=.1) |
---|
| 475 | |
---|
[3bdbfcc] | 476 | data_x, data_y = numpy.meshgrid(self._data.x_bins[0:-1], |
---|
| 477 | self._data.y_bins[0:-1]) |
---|
[6d05e1d] | 478 | |
---|
[64f1e93] | 479 | ax = Axes3D(self.figure) |
---|
[b4b8589] | 480 | |
---|
[64f1e93] | 481 | # Disable rotation for large sets. |
---|
| 482 | # TODO: Define "large" for a dataset |
---|
| 483 | SET_TOO_LARGE = 500 |
---|
[3bdbfcc] | 484 | if len(data_x) > SET_TOO_LARGE: |
---|
[64f1e93] | 485 | ax.disable_mouse_rotation() |
---|
| 486 | |
---|
[6d05e1d] | 487 | self.figure.canvas.resizing = False |
---|
[3bdbfcc] | 488 | im = ax.plot_surface(data_x, data_y, output, rstride=1, |
---|
| 489 | cstride=1, cmap=cmap, |
---|
[6d05e1d] | 490 | linewidth=0, antialiased=False) |
---|
[55d89f8] | 491 | self.ax.set_axis_off() |
---|
[6d05e1d] | 492 | |
---|
| 493 | if self.dimension != 3: |
---|
| 494 | self.figure.canvas.draw_idle() |
---|
| 495 | else: |
---|
| 496 | self.figure.canvas.draw() |
---|
[416fa8f] | 497 | |
---|
[3bdbfcc] | 498 | def update(self): |
---|
| 499 | self.figure.canvas.draw() |
---|
| 500 | |
---|
| 501 | def draw(self): |
---|
| 502 | self.figure.canvas.draw() |
---|
| 503 | |
---|
[0274aea] | 504 | def replacePlot(self, id, new_plot): |
---|
| 505 | """ |
---|
| 506 | Replace data in current chart. |
---|
[01cda57] | 507 | This effectively refreshes the chart with changes to one of its plots |
---|
[0274aea] | 508 | """ |
---|
| 509 | self.plot(data=new_plot) |
---|
| 510 | |
---|
[3bdbfcc] | 511 | |
---|
[4992ff2] | 512 | class Plotter2D(QtWidgets.QDialog, Plotter2DWidget): |
---|
[3bdbfcc] | 513 | """ |
---|
| 514 | Plotter widget implementation |
---|
| 515 | """ |
---|
[416fa8f] | 516 | def __init__(self, parent=None, quickplot=False, dimension=2): |
---|
[4992ff2] | 517 | QtWidgets.QDialog.__init__(self) |
---|
[cad617b] | 518 | Plotter2DWidget.__init__(self, manager=parent, quickplot=quickplot, dimension=dimension) |
---|
[c4e5400] | 519 | icon = QtGui.QIcon() |
---|
| 520 | icon.addPixmap(QtGui.QPixmap(":/res/ball.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off) |
---|
| 521 | self.setWindowIcon(icon) |
---|