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