source: sasview/src/sas/sasgui/guiframe/local_perspectives/plotting/AnnulusSlicer.py @ 3bdbfcc

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 3bdbfcc was 3bdbfcc, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

Reimplementation of the slicer functionality

  • Property mode set to 100644
File size: 15.4 KB
RevLine 
[3bdbfcc]1import numpy
2from PyQt4 import QtGui
3from PyQt4 import QtCore
4
[ef0c170]5from BaseInteractor import _BaseInteractor
[d85c194]6from sas.sasgui.guiframe.dataFitting import Data1D
[3bdbfcc]7import sas.qtgui.GuiUtils as GuiUtils
8from sas.qtgui.GuiUtils import formatNumber
9from sas.qtgui.SlicerModel import SlicerModel
[ef0c170]10
[3bdbfcc]11class AnnulusInteractor(_BaseInteractor, SlicerModel):
[ef0c170]12    """
[83f4445]13    Select an annulus through a 2D plot.
[824e488]14    This interactor is used to average 2D data  with the region
[83f4445]15    defined by 2 radius.
16    this class is defined by 2 Ringinterators.
[ef0c170]17    """
[3bdbfcc]18    def __init__(self, base, axes, item=None, color='black', zorder=3):
[824e488]19
[ef0c170]20        _BaseInteractor.__init__(self, base, axes, color=color)
[3bdbfcc]21        SlicerModel.__init__(self)
22
[ef0c170]23        self.markers = []
24        self.axes = axes
[32c0841]25        self.base = base
[3bdbfcc]26        self._item = item
27        self.qmax = min(numpy.fabs(self.base.data.xmax),
28                        numpy.fabs(self.base.data.xmin))  # must be positive
[ef0c170]29        self.connect = self.base.connect
[824e488]30
[3bdbfcc]31        # Number of points on the plot
[400155b]32        self.nbins = 36
[824e488]33        # Cursor position of Rings (Left(-1) or Right(1))
[3bdbfcc]34        self.xmaxd = self.base.data.xmax
35        self.xmind = self.base.data.xmin
[ef0c170]36
[32c0841]37        if (self.xmaxd + self.xmind) > 0:
38            self.sign = 1
[a2c38de]39        else:
[32c0841]40            self.sign = -1
[ef0c170]41        # Inner circle
[3bdbfcc]42        self.inner_circle = RingInteractor(self, self.axes,
[824e488]43                                           zorder=zorder,
44                                           r=self.qmax / 2.0, sign=self.sign)
[bd1d9d9]45        self.inner_circle.qmax = self.qmax
[3bdbfcc]46        self.outer_circle = RingInteractor(self, self.axes,
[824e488]47                                           zorder=zorder + 1, r=self.qmax / 1.8,
[32c0841]48                                           sign=self.sign)
49        self.outer_circle.qmax = self.qmax * 1.2
[ef0c170]50        self.update()
[7ab9241]51        self._post_data()
[824e488]52
[3bdbfcc]53        self.setModelFromParams()
[ef0c170]54
55    def set_layer(self, n):
[eba08f1a]56        """
[83f4445]57        Allow adding plot to the same panel
[824e488]58
[83f4445]59        :param n: the number of layer
[824e488]60
[eba08f1a]61        """
[ef0c170]62        self.layernum = n
63        self.update()
[824e488]64
[ef0c170]65    def clear(self):
[eba08f1a]66        """
[83f4445]67        Clear the slicer and all connected events related to this slicer
[eba08f1a]68        """
[ef0c170]69        self.clear_markers()
70        self.outer_circle.clear()
71        self.inner_circle.clear()
[18eba35]72        self.base.connect.clearall()
[824e488]73
[ef0c170]74    def update(self):
75        """
[83f4445]76        Respond to changes in the model by recalculating the profiles and
77        resetting the widgets.
[ef0c170]78        """
[824e488]79        # Update locations
[ef0c170]80        self.inner_circle.update()
81        self.outer_circle.update()
[824e488]82
[ef0c170]83    def save(self, ev):
84        """
[83f4445]85        Remember the roughness for this layer and the next so that we
86        can restore on Esc.
[ef0c170]87        """
88        self.inner_circle.save(ev)
89        self.outer_circle.save(ev)
90
[32c0841]91    def _post_data(self, nbins=None):
[eba08f1a]92        """
[83f4445]93        Uses annulus parameters to plot averaged data into 1D data.
[824e488]94
95        :param nbins: the number of points to plot
96
[eba08f1a]97        """
[824e488]98        # Data to average
[3bdbfcc]99        data = self.base.data
100        if data is None:
[ef0c170]101            return
[824e488]102
[b699768]103        from sas.sascalc.dataloader.manipulations import Ring
[3bdbfcc]104        rmin = min(numpy.fabs(self.inner_circle.get_radius()),
105                   numpy.fabs(self.outer_circle.get_radius()))
106        rmax = max(numpy.fabs(self.inner_circle.get_radius()),
107                   numpy.fabs(self.outer_circle.get_radius()))
108        # If the user does not specify the numbers of points to plot
[400155b]109        # the default number will be nbins= 36
[3bdbfcc]110        if nbins is None:
[400155b]111            self.nbins = 36
[eba08f1a]112        else:
113            self.nbins = nbins
[3bdbfcc]114        # Create the data1D Q average of data2D
[32c0841]115        sect = Ring(r_min=rmin, r_max=rmax, nbins=self.nbins)
[3bdbfcc]116        sector = sect(self.base.data)
[824e488]117
[32c0841]118        if hasattr(sector, "dxl"):
119            dxl = sector.dxl
[ef0c170]120        else:
[32c0841]121            dxl = None
122        if hasattr(sector, "dxw"):
123            dxw = sector.dxw
[ef0c170]124        else:
[32c0841]125            dxw = None
[3bdbfcc]126        new_plot = Data1D(x=(sector.x - numpy.pi) * 180 / numpy.pi,
[32c0841]127                          y=sector.y, dy=sector.dy)
[4ac8556]128        new_plot.dxl = dxl
129        new_plot.dxw = dxw
[3bdbfcc]130        new_plot.name = "AnnulusPhi" + "(" + self.base.data.name + ")"
131        new_plot.title = "AnnulusPhi" + "(" + self.base.data.name + ")"
[824e488]132
[3bdbfcc]133        new_plot.source = self.base.data.source
[ef0c170]134        new_plot.interactive = True
[3bdbfcc]135        new_plot.detector = self.base.data.detector
[ef0c170]136        # If the data file does not tell us what the axes are, just assume...
[14d3495]137        new_plot.xaxis("\\rm{\phi}", 'degrees')
[fcf072d]138        new_plot.yaxis("\\rm{Intensity} ", "cm^{-1}")
139        if hasattr(data, "scale") and data.scale == 'linear' and \
[3bdbfcc]140                self.base.data.name.count("Residuals") > 0:
[fcf072d]141            new_plot.ytransform = 'y'
142            new_plot.yaxis("\\rm{Residuals} ", "/")
[21969a4a]143
[3bdbfcc]144        new_plot.group_id = "AnnulusPhi" + self.base.data.name
145        new_plot.id = "AnnulusPhi" + self.base.data.name
[824e488]146        new_plot.is_data = True
[32c0841]147        new_plot.xtransform = "x"
148        new_plot.ytransform = "y"
[3bdbfcc]149        variant_plot = QtCore.QVariant(new_plot)
150        GuiUtils.updateModelItemWithPlot(self._item, variant_plot, new_plot.id)
151
152        if self.update_model:
153            self.setModelFromParams()
154        self.draw()
155
[824e488]156
[ef0c170]157    def moveend(self, ev):
[eba08f1a]158        """
[83f4445]159        Called when any dragging motion ends.
[3bdbfcc]160        Redraw the plot with new parameters.
[eba08f1a]161        """
[3bdbfcc]162        self._post_data(self.nbins)
[824e488]163
[ef0c170]164    def restore(self):
165        """
166        Restore the roughness for this layer.
167        """
168        self.inner_circle.restore()
169        self.outer_circle.restore()
170
171    def move(self, x, y, ev):
172        """
173        Process move to a new position, making sure that the move is allowed.
174        """
175        pass
[824e488]176
[ef0c170]177    def set_cursor(self, x, y):
178        pass
[824e488]179
[3bdbfcc]180    def getParams(self):
[eba08f1a]181        """
[83f4445]182        Store a copy of values of parameters of the slicer into a dictionary.
183        :return params: the dictionary created
[eba08f1a]184        """
[ef0c170]185        params = {}
[3bdbfcc]186        params["inner_radius"] = numpy.fabs(self.inner_circle._inner_mouse_x)
187        params["outer_radius"] = numpy.fabs(self.outer_circle._inner_mouse_x)
[ef0c170]188        params["nbins"] = self.nbins
189        return params
[824e488]190
[3bdbfcc]191    def setParams(self, params):
[eba08f1a]192        """
[824e488]193        Receive a dictionary and reset the slicer with values contained
[83f4445]194        in the values of the dictionary.
[824e488]195
196        :param params: a dictionary containing name of slicer parameters and
[eba08f1a]197            values the user assigned to the slicer.
198        """
[3bdbfcc]199        inner = numpy.fabs(params["inner_radius"])
200        outer = numpy.fabs(params["outer_radius"])
[ef0c170]201        self.nbins = int(params["nbins"])
[3bdbfcc]202        # Update the picture
[ef0c170]203        self.inner_circle.set_cursor(inner, self.inner_circle._inner_mouse_y)
204        self.outer_circle.set_cursor(outer, self.outer_circle._inner_mouse_y)
[3bdbfcc]205        # Post the data given the nbins entered by the user
[5554566]206        self._post_data(self.nbins)
[824e488]207
[ef0c170]208    def draw(self):
[83f4445]209        """
210        """
[ef0c170]211        self.base.draw()
212
[824e488]213
[ef0c170]214class RingInteractor(_BaseInteractor):
215    """
[824e488]216     Draw a ring Given a radius
[ef0c170]217    """
[32c0841]218    def __init__(self, base, axes, color='black', zorder=5, r=1.0, sign=1):
[83f4445]219        """
220        :param: the color of the line that defined the ring
221        :param r: the radius of the ring
[824e488]222        :param sign: the direction of motion the the marker
223
[83f4445]224        """
[ef0c170]225        _BaseInteractor.__init__(self, base, axes, color=color)
226        self.markers = []
227        self.axes = axes
[eba08f1a]228        # Current radius of the ring
[ef0c170]229        self._inner_mouse_x = r
[824e488]230        # Value of the center of the ring
[ef0c170]231        self._inner_mouse_y = 0
[eba08f1a]232        # previous value of that radius
[824e488]233        self._inner_save_x = r
234        # Save value of the center of the ring
235        self._inner_save_y = 0
236        # Class instantiating RingIterator class
[32c0841]237        self.base = base
[824e488]238        # the direction of the motion of the marker
[32c0841]239        self.sign = sign
[824e488]240        # # Create a marker
[3bdbfcc]241        # Inner circle marker
242        x_value = [self.sign * numpy.fabs(self._inner_mouse_x)]
243        self.inner_marker = self.axes.plot(x_value, [0], linestyle='',
244                                           marker='s', markersize=10,
245                                           color=self.color, alpha=0.6,
246                                           pickradius=5, label="pick",
247                                           zorder=zorder,
248                                           visible=True)[0]
[824e488]249        # Draw a circle
250        [self.inner_circle] = self.axes.plot([], [], linestyle='-', marker='', color=self.color)
[3bdbfcc]251        # The number of points that make the ring line
[e4032d64]252        self.npts = 40
[824e488]253
[ef0c170]254        self.connect_markers([self.inner_marker])
255        self.update()
256
257    def set_layer(self, n):
[eba08f1a]258        """
[83f4445]259        Allow adding plot to the same panel
[824e488]260
[83f4445]261        :param n: the number of layer
[824e488]262
[eba08f1a]263        """
[ef0c170]264        self.layernum = n
265        self.update()
[824e488]266
[ef0c170]267    def clear(self):
[eba08f1a]268        """
[83f4445]269        Clear the slicer and all connected events related to this slicer
[eba08f1a]270        """
[ef0c170]271        self.clear_markers()
[3bdbfcc]272        self.inner_marker.remove()
273        self.inner_circle.remove()
[824e488]274
[ef0c170]275    def get_radius(self):
[eba08f1a]276        """
[83f4445]277        :return self._inner_mouse_x: the current radius of the ring
[eba08f1a]278        """
[ef0c170]279        return self._inner_mouse_x
[824e488]280
[ef0c170]281    def update(self):
282        """
[83f4445]283        Draw the new roughness on the graph.
[ef0c170]284        """
285        # Plot inner circle
286        x = []
287        y = []
288        for i in range(self.npts):
[3bdbfcc]289            phi = 2.0 * numpy.pi / (self.npts - 1) * i
[824e488]290
[3bdbfcc]291            xval = 1.0 * self._inner_mouse_x * numpy.cos(phi)
292            yval = 1.0 * self._inner_mouse_x * numpy.sin(phi)
[824e488]293
[ef0c170]294            x.append(xval)
295            y.append(yval)
[824e488]296
[3bdbfcc]297        self.inner_marker.set(xdata=[self.sign * numpy.fabs(self._inner_mouse_x)],
[32c0841]298                              ydata=[0])
[824e488]299        self.inner_circle.set_data(x, y)
[ef0c170]300
301    def save(self, ev):
302        """
303        Remember the roughness for this layer and the next so that we
304        can restore on Esc.
305        """
306        self._inner_save_x = self._inner_mouse_x
307        self._inner_save_y = self._inner_mouse_y
308
309    def moveend(self, ev):
[eba08f1a]310        """
[83f4445]311        Called after a dragging motion
[eba08f1a]312        """
[ef0c170]313        self.base.moveend(ev)
[824e488]314
[ef0c170]315    def restore(self):
316        """
317        Restore the roughness for this layer.
318        """
319        self._inner_mouse_x = self._inner_save_x
320        self._inner_mouse_y = self._inner_save_y
321
322    def move(self, x, y, ev):
323        """
324        Process move to a new position, making sure that the move is allowed.
325        """
326        self._inner_mouse_x = x
327        self._inner_mouse_y = y
328        self.base.base.update()
[824e488]329
[ef0c170]330    def set_cursor(self, x, y):
[eba08f1a]331        """
[824e488]332        draw the ring given x, y value
[eba08f1a]333        """
[ef0c170]334        self.move(x, y, None)
335        self.update()
[824e488]336
[3bdbfcc]337    def getParams(self):
[eba08f1a]338        """
[83f4445]339        Store a copy of values of parameters of the slicer into a dictionary.
340        :return params: the dictionary created
[eba08f1a]341        """
[ef0c170]342        params = {}
[3bdbfcc]343        params["radius"] = numpy.fabs(self._inner_mouse_x)
[ef0c170]344        return params
[824e488]345
[3bdbfcc]346    def setParams(self, params):
[eba08f1a]347        """
[824e488]348        Receive a dictionary and reset the slicer with values contained
[83f4445]349        in the values of the dictionary.
[824e488]350
351        :param params: a dictionary containing name of slicer parameters and
[eba08f1a]352            values the user assigned to the slicer.
[824e488]353
[eba08f1a]354        """
[824e488]355        x = params["radius"]
[ef0c170]356        self.set_cursor(x, self._inner_mouse_y)
[824e488]357
[c5874f2]358class CircularMask(_BaseInteractor):
359    """
[824e488]360     Draw a ring Given a radius
[c5874f2]361    """
[32c0841]362    def __init__(self, base, axes, color='grey', zorder=3, side=None):
[83f4445]363        """
364        :param: the color of the line that defined the ring
365        :param r: the radius of the ring
[824e488]366        :param sign: the direction of motion the the marker
[83f4445]367        """
[c5874f2]368        _BaseInteractor.__init__(self, base, axes, color=color)
369        self.markers = []
370        self.axes = axes
[32c0841]371        self.base = base
[c5874f2]372        self.is_inside = side
[3bdbfcc]373        self.qmax = min(numpy.fabs(self.base.data.xmax),
374                        numpy.fabs(self.base.data.xmin))  # must be positive
[c5874f2]375        self.connect = self.base.connect
[824e488]376
377        # Cursor position of Rings (Left(-1) or Right(1))
[32c0841]378        self.xmaxd = self.base.data.xmax
379        self.xmind = self.base.data.xmin
[c5874f2]380
[32c0841]381        if (self.xmaxd + self.xmind) > 0:
382            self.sign = 1
[c5874f2]383        else:
[32c0841]384            self.sign = -1
[c5874f2]385        # Inner circle
[3bdbfcc]386        self.outer_circle = RingInteractor(self, self.axes, 'blue',
[824e488]387                                           zorder=zorder + 1, r=self.qmax / 1.8,
388                                           sign=self.sign)
[32c0841]389        self.outer_circle.qmax = self.qmax * 1.2
[c5874f2]390        self.update()
391        self._post_data()
[824e488]392
[c5874f2]393    def set_layer(self, n):
394        """
[83f4445]395        Allow adding plot to the same panel
396        :param n: the number of layer
[c5874f2]397        """
398        self.layernum = n
399        self.update()
[824e488]400
[c5874f2]401    def clear(self):
402        """
[83f4445]403        Clear the slicer and all connected events related to this slicer
[c5874f2]404        """
405        self.clear_markers()
406        self.outer_circle.clear()
407        self.base.connect.clearall()
[824e488]408
[c5874f2]409    def update(self):
410        """
[83f4445]411        Respond to changes in the model by recalculating the profiles and
412        resetting the widgets.
[c5874f2]413        """
[824e488]414        # Update locations
[c5874f2]415        self.outer_circle.update()
[3bdbfcc]416        self._post_data()
[c5874f2]417        out = self._post_data()
418        return out
419
420    def save(self, ev):
421        """
[83f4445]422        Remember the roughness for this layer and the next so that we
423        can restore on Esc.
[c5874f2]424        """
425        self.outer_circle.save(ev)
426
427    def _post_data(self):
428        """
[83f4445]429        Uses annulus parameters to plot averaged data into 1D data.
[824e488]430
431        :param nbins: the number of points to plot
432
[c5874f2]433        """
[824e488]434        # Data to average
[c5874f2]435        data = self.base.data
[824e488]436
[c5874f2]437        # If we have no data, just return
[3bdbfcc]438        if data is None:
[c5874f2]439            return
[824e488]440        mask = data.mask
[b699768]441        from sas.sascalc.dataloader.manipulations import Ringcut
[824e488]442
[32c0841]443        rmin = 0
[3bdbfcc]444        rmax = numpy.fabs(self.outer_circle.get_radius())
[c5874f2]445
[3bdbfcc]446        # Create the data1D Q average of data2D
[824e488]447        mask = Ringcut(r_min=rmin, r_max=rmax)
[c5874f2]448
449        if self.is_inside:
[32c0841]450            out = (mask(data) == False)
[c5874f2]451        else:
452            out = (mask(data))
[824e488]453        return out
454
[c5874f2]455
456    def moveend(self, ev):
457        """
[83f4445]458        Called when any dragging motion ends.
459        Post an event (type =SlicerParameterEvent)
460        to plotter 2D with a copy  slicer parameters
461        Call  _post_data method
[c5874f2]462        """
463        self.base.thaw_axes()
464        # create a 1D data plot
465        self._post_data()
[824e488]466
[c5874f2]467    def restore(self):
468        """
469        Restore the roughness for this layer.
470        """
471        self.outer_circle.restore()
472
473    def move(self, x, y, ev):
474        """
475        Process move to a new position, making sure that the move is allowed.
476        """
477        pass
[824e488]478
[c5874f2]479    def set_cursor(self, x, y):
480        pass
[824e488]481
[3bdbfcc]482    def getParams(self):
[c5874f2]483        """
[83f4445]484        Store a copy of values of parameters of the slicer into a dictionary.
[824e488]485
[83f4445]486        :return params: the dictionary created
[824e488]487
[c5874f2]488        """
489        params = {}
[3bdbfcc]490        params["outer_radius"] = numpy.fabs(self.outer_circle._inner_mouse_x)
[c5874f2]491        return params
[824e488]492
[3bdbfcc]493    def setParams(self, params):
[c5874f2]494        """
[824e488]495        Receive a dictionary and reset the slicer with values contained
[83f4445]496        in the values of the dictionary.
[824e488]497
498        :param params: a dictionary containing name of slicer parameters and
[c5874f2]499            values the user assigned to the slicer.
500        """
[3bdbfcc]501        outer = numpy.fabs(params["outer_radius"])
502        # Update the picture
[c5874f2]503        self.outer_circle.set_cursor(outer, self.outer_circle._inner_mouse_y)
[3bdbfcc]504        # Post the data given the nbins entered by the user
[c5874f2]505        self._post_data()
[824e488]506
[c5874f2]507    def draw(self):
508        self.base.update()
[824e488]509
Note: See TracBrowser for help on using the repository browser.