source: sasview/src/sas/qtgui/Plotting/Slicers/SectorSlicer.py @ d6b8a1d

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 d6b8a1d was d6b8a1d, checked in by Piotr Rozyczko <rozyczko@…>, 6 years ago

More Qt5 related fixes

  • Property mode set to 100644
File size: 19.2 KB
RevLine 
[dc5ef15]1"""
2    Sector interactor
3"""
4import numpy
[d6b8a1d]5import logging
[dc5ef15]6
[b3e8629]7from .BaseInteractor import BaseInteractor
[dc5ef15]8from sas.qtgui.Plotting.PlotterData import Data1D
9import sas.qtgui.Utilities.GuiUtils as GuiUtils
10from sas.qtgui.Plotting.SlicerModel import SlicerModel
11
12MIN_PHI = 0.05
13
14class SectorInteractor(BaseInteractor, SlicerModel):
15    """
16    Draw a sector slicer.Allow to performQ averaging on data 2D
17    """
18    def __init__(self, base, axes, item=None, color='black', zorder=3):
19
20        BaseInteractor.__init__(self, base, axes, color=color)
21        SlicerModel.__init__(self)
22        # Class initialization
23        self.markers = []
24        self.axes = axes
25        self._item = item
26        # Connect the plot to event
27        self.connect = self.base.connect
28
29        # Compute qmax limit to reset the graph
30        x = numpy.power(max(self.base.data.xmax,
31                         numpy.fabs(self.base.data.xmin)), 2)
32        y = numpy.power(max(self.base.data.ymax,
33                         numpy.fabs(self.base.data.ymin)), 2)
34        self.qmax = numpy.sqrt(x + y)
35        # Number of points on the plot
36        self.nbins = 20
37        # Angle of the middle line
38        self.theta2 = numpy.pi / 3
39        # Absolute value of the Angle between the middle line and any side line
40        self.phi = numpy.pi / 12
41        # Middle line
42        self.main_line = LineInteractor(self, self.axes, color='blue',
43                                        zorder=zorder, r=self.qmax,
44                                        theta=self.theta2)
45        self.main_line.qmax = self.qmax
46        # Right Side line
47        self.right_line = SideInteractor(self, self.axes, color='black',
48                                         zorder=zorder, r=self.qmax,
49                                         phi=-1 * self.phi, theta2=self.theta2)
50        self.right_line.qmax = self.qmax
51        # Left Side line
52        self.left_line = SideInteractor(self, self.axes, color='black',
53                                        zorder=zorder, r=self.qmax,
54                                        phi=self.phi, theta2=self.theta2)
55        self.left_line.qmax = self.qmax
56        # draw the sector
57        self.update()
58        self._post_data()
59        self.setModelFromParams()
60
61    def set_layer(self, n):
62        """
63         Allow adding plot to the same panel
64        :param n: the number of layer
65        """
66        self.layernum = n
67        self.update()
68
69    def clear(self):
70        """
71        Clear the slicer and all connected events related to this slicer
72        """
73        self.clear_markers()
74        self.main_line.clear()
75        self.left_line.clear()
76        self.right_line.clear()
77        self.base.connect.clearall()
78
79    def update(self):
80        """
81        Respond to changes in the model by recalculating the profiles and
82        resetting the widgets.
83        """
84        # Update locations
85        # Check if the middle line was dragged and
86        # update the picture accordingly
87        if self.main_line.has_move:
88            self.main_line.update()
89            self.right_line.update(delta=-self.left_line.phi / 2,
90                                   mline=self.main_line.theta)
91            self.left_line.update(delta=self.left_line.phi / 2,
92                                  mline=self.main_line.theta)
93        # Check if the left side has moved and update the slicer accordingly
94        if self.left_line.has_move:
95            self.main_line.update()
96            self.left_line.update(phi=None, delta=None, mline=self.main_line,
97                                  side=True, left=True)
98            self.right_line.update(phi=self.left_line.phi, delta=None,
99                                   mline=self.main_line, side=True,
100                                   left=False, right=True)
101        # Check if the right side line has moved and update the slicer accordingly
102        if self.right_line.has_move:
103            self.main_line.update()
104            self.right_line.update(phi=None, delta=None, mline=self.main_line,
105                                   side=True, left=False, right=True)
106            self.left_line.update(phi=self.right_line.phi, delta=None,
107                                  mline=self.main_line, side=True, left=False)
108
109    def save(self, ev):
110        """
111        Remember the roughness for this layer and the next so that we
112        can restore on Esc.
113        """
114        self.main_line.save(ev)
115        self.right_line.save(ev)
116        self.left_line.save(ev)
117
118    def _post_data(self, nbins=None):
119        """
120        compute sector averaging of data2D into data1D
121        :param nbins: the number of point to plot for the average 1D data
122        """
123        # Get the data2D to average
124        data = self.base.data
125        # If we have no data, just return
[cee5c78]126        if data is None:
[dc5ef15]127            return
128        # Averaging
129        from sas.sascalc.dataloader.manipulations import SectorQ
130        radius = self.qmax
131        phimin = -self.left_line.phi + self.main_line.theta
132        phimax = self.left_line.phi + self.main_line.theta
[cee5c78]133        if nbins is None:
[dc5ef15]134            nbins = 20
135        sect = SectorQ(r_min=0.0, r_max=radius,
136                       phi_min=phimin + numpy.pi,
137                       phi_max=phimax + numpy.pi, nbins=nbins)
138
139        sector = sect(self.base.data)
140        # Create 1D data resulting from average
141
142        if hasattr(sector, "dxl"):
143            dxl = sector.dxl
144        else:
145            dxl = None
146        if hasattr(sector, "dxw"):
147            dxw = sector.dxw
148        else:
149            dxw = None
150        new_plot = Data1D(x=sector.x, y=sector.y, dy=sector.dy, dx=sector.dx)
151        new_plot.dxl = dxl
152        new_plot.dxw = dxw
153        new_plot.name = "SectorQ" + "(" + self.base.data.name + ")"
154        new_plot.title = "SectorQ" + "(" + self.base.data.name + ")"
155        new_plot.source = self.base.data.source
156        new_plot.interactive = True
157        new_plot.detector = self.base.data.detector
158        # If the data file does not tell us what the axes are, just assume them.
159        new_plot.xaxis("\\rm{Q}", "A^{-1}")
160        new_plot.yaxis("\\rm{Intensity}", "cm^{-1}")
161        if hasattr(data, "scale") and data.scale == 'linear' and \
162                self.base.data.name.count("Residuals") > 0:
163            new_plot.ytransform = 'y'
164            new_plot.yaxis("\\rm{Residuals} ", "/")
165
166        new_plot.group_id = "2daverage" + self.base.data.name
167        new_plot.id = "SectorQ" + self.base.data.name
168        new_plot.is_data = True
[cee5c78]169        GuiUtils.updateModelItemWithPlot(self._item, new_plot, new_plot.id)
[d6b8a1d]170
[dc5ef15]171        self.base.manager.communicator.plotUpdateSignal.emit([new_plot])
172
173        if self.update_model:
174            self.setModelFromParams()
175        self.draw()
176
177    def validate(self, param_name, param_value):
178        """
179        Test the proposed new value "value" for row "row" of parameters
180        """
181        MIN_DIFFERENCE = 0.01
182        isValid = True
183
184        if param_name == 'Delta_Phi [deg]':
185            # First, check the closeness
186            if numpy.fabs(param_value) < MIN_DIFFERENCE:
187                print("Sector angles too close. Please adjust.")
188                isValid = False
189        elif param_name == 'nbins':
190            # Can't be 0
191            if param_value < 1:
192                print("Number of bins cannot be less than or equal to 0. Please adjust.")
193                isValid = False
194        return isValid
195
196    def moveend(self, ev):
197        """
198        Called a dragging motion ends.Get slicer event
199        """
200        # Post parameters
201        self._post_data(self.nbins)
202
203    def restore(self):
204        """
205        Restore the roughness for this layer.
206        """
207        self.main_line.restore()
208        self.left_line.restore()
209        self.right_line.restore()
210
211    def move(self, x, y, ev):
212        """
213        Process move to a new position, making sure that the move is allowed.
214        """
215        pass
216
217    def set_cursor(self, x, y):
218        pass
219
220    def getParams(self):
221        """
222        Store a copy of values of parameters of the slicer into a dictionary.
223        :return params: the dictionary created
224        """
225        params = {}
226        # Always make sure that the left and the right line are at phi
227        # angle of the middle line
228        if numpy.fabs(self.left_line.phi) != numpy.fabs(self.right_line.phi):
229            msg = "Phi left and phi right are different"
230            msg += " %f, %f" % (self.left_line.phi, self.right_line.phi)
[b3e8629]231            raise ValueError(msg)
[dc5ef15]232        params["Phi [deg]"] = self.main_line.theta * 180 / numpy.pi
233        params["Delta_Phi [deg]"] = numpy.fabs(self.left_line.phi * 180 / numpy.pi)
234        params["nbins"] = self.nbins
235        return params
236
237    def setParams(self, params):
238        """
239        Receive a dictionary and reset the slicer with values contained
240        in the values of the dictionary.
241
242        :param params: a dictionary containing name of slicer parameters and
243            values the user assigned to the slicer.
244        """
245        main = params["Phi [deg]"] * numpy.pi / 180
246        phi = numpy.fabs(params["Delta_Phi [deg]"] * numpy.pi / 180)
247
248        # phi should not be too close.
249        if numpy.fabs(phi) < MIN_PHI:
250            phi = MIN_PHI
251            params["Delta_Phi [deg]"] = MIN_PHI
252
253        self.nbins = int(params["nbins"])
254        self.main_line.theta = main
255        # Reset the slicer parameters
256        self.main_line.update()
257        self.right_line.update(phi=phi, delta=None, mline=self.main_line,
258                               side=True, right=True)
259        self.left_line.update(phi=phi, delta=None,
260                              mline=self.main_line, side=True)
261        # Post the new corresponding data
262        self._post_data(nbins=self.nbins)
263
264    def draw(self):
265        """
266        Redraw canvas
267        """
268        self.base.draw()
269
270
271class SideInteractor(BaseInteractor):
272    """
273    Draw an oblique line
274
275    :param phi: the phase between the middle line and one side line
276    :param theta2: the angle between the middle line and x- axis
277
278    """
279    def __init__(self, base, axes, color='black', zorder=5, r=1.0,
280                 phi=numpy.pi / 4, theta2=numpy.pi / 3):
281        BaseInteractor.__init__(self, base, axes, color=color)
282        # Initialize the class
283        self.markers = []
284        self.axes = axes
285        # compute the value of the angle between the current line and
286        # the x-axis
287        self.save_theta = theta2 + phi
288        self.theta = theta2 + phi
289        # the value of the middle line angle with respect to the x-axis
290        self.theta2 = theta2
291        # Radius to find polar coordinates this line's endpoints
292        self.radius = r
293        # phi is the phase between the current line and the middle line
294        self.phi = phi
295        # End points polar coordinates
296        x1 = self.radius * numpy.cos(self.theta)
297        y1 = self.radius * numpy.sin(self.theta)
298        x2 = -1 * self.radius * numpy.cos(self.theta)
299        y2 = -1 * self.radius * numpy.sin(self.theta)
300        # Defining a new marker
301        self.inner_marker = self.axes.plot([x1 / 2.5], [y1 / 2.5], linestyle='',
302                                           marker='s', markersize=10,
303                                           color=self.color, alpha=0.6,
304                                           pickradius=5, label="pick",
305                                           zorder=zorder, visible=True)[0]
306
307        # Defining the current line
308        self.line = self.axes.plot([x1, x2], [y1, y2],
309                                   linestyle='-', marker='',
310                                   color=self.color, visible=True)[0]
311        # Flag to differentiate the left line from the right line motion
312        self.left_moving = False
313        # Flag to define a motion
314        self.has_move = False
315        # connecting markers and draw the picture
316        self.connect_markers([self.inner_marker, self.line])
317
318    def set_layer(self, n):
319        """
320        Allow adding plot to the same panel
321        :param n: the number of layer
322        """
323        self.layernum = n
324        self.update()
325
326    def clear(self):
327        """
328        Clear the slicer and all connected events related to this slicer
329        """
330        self.clear_markers()
331        try:
332            self.line.remove()
333            self.inner_marker.remove()
334        except:
335            # Old version of matplotlib
336            for item in range(len(self.axes.lines)):
337                del self.axes.lines[0]
338
339    def update(self, phi=None, delta=None, mline=None,
340               side=False, left=False, right=False):
341        """
342        Draw oblique line
343
344        :param phi: the phase between the middle line and the current line
345        :param delta: phi/2 applied only when the mline was moved
346
347        """
348        self.left_moving = left
349        theta3 = 0
[cee5c78]350        if phi is not None:
[dc5ef15]351            self.phi = phi
[cee5c78]352        if delta is None:
[dc5ef15]353            delta = 0
354        if  right:
355            self.phi = -1 * numpy.fabs(self.phi)
356            #delta=-delta
357        else:
358            self.phi = numpy.fabs(self.phi)
359        if side:
360            self.theta = mline.theta + self.phi
361
[cee5c78]362        if mline is not None:
[dc5ef15]363            if delta != 0:
364                self.theta2 = mline + delta
365            else:
366                self.theta2 = mline.theta
367        if delta == 0:
368            theta3 = self.theta + delta
369        else:
370            theta3 = self.theta2 + delta
371        x1 = self.radius * numpy.cos(theta3)
372        y1 = self.radius * numpy.sin(theta3)
373        x2 = -1 * self.radius * numpy.cos(theta3)
374        y2 = -1 * self.radius * numpy.sin(theta3)
375        self.inner_marker.set(xdata=[x1 / 2.5], ydata=[y1 / 2.5])
376        self.line.set(xdata=[x1, x2], ydata=[y1, y2])
377
378    def save(self, ev):
379        """
380        Remember the roughness for this layer and the next so that we
381        can restore on Esc.
382        """
383        self.save_theta = self.theta
384
385    def moveend(self, ev):
386        self.has_move = False
387        self.base.moveend(ev)
388
389    def restore(self):
390        """
391        Restore the roughness for this layer.
392        """
393        self.theta = self.save_theta
394
395    def move(self, x, y, ev):
396        """
397        Process move to a new position, making sure that the move is allowed.
398        """
399        self.theta = numpy.arctan2(y, x)
400        self.has_move = True
401        if not self.left_moving:
402            if  self.theta2 - self.theta <= 0 and self.theta2 > 0:
403                self.restore()
404                return
405            elif self.theta2 < 0 and self.theta < 0 and \
406                self.theta - self.theta2 >= 0:
407                self.restore()
408                return
409            elif  self.theta2 < 0 and self.theta > 0 and \
410                (self.theta2 + 2 * numpy.pi - self.theta) >= numpy.pi / 2:
411                self.restore()
412                return
413            elif  self.theta2 < 0 and self.theta < 0 and \
414                (self.theta2 - self.theta) >= numpy.pi / 2:
415                self.restore()
416                return
417            elif self.theta2 > 0 and (self.theta2 - self.theta >= numpy.pi / 2 or \
418                (self.theta2 - self.theta >= numpy.pi / 2)):
419                self.restore()
420                return
421        else:
422            if  self.theta < 0 and (self.theta + numpy.pi * 2 - self.theta2) <= 0:
423                self.restore()
424                return
425            elif self.theta2 < 0 and (self.theta - self.theta2) <= 0:
426                self.restore()
427                return
428            elif  self.theta > 0 and self.theta - self.theta2 <= 0:
429                self.restore()
430                return
431            elif self.theta - self.theta2 >= numpy.pi / 2 or  \
432                ((self.theta + numpy.pi * 2 - self.theta2) >= numpy.pi / 2 and \
433                 self.theta < 0 and self.theta2 > 0):
434                self.restore()
435                return
436
437        self.phi = numpy.fabs(self.theta2 - self.theta)
438        if self.phi > numpy.pi:
439            self.phi = 2 * numpy.pi - numpy.fabs(self.theta2 - self.theta)
440        self.base.base.update()
441
442    def set_cursor(self, x, y):
443        self.move(x, y, None)
444        self.update()
445
446    def getParams(self):
447        params = {}
448        params["radius"] = self.radius
449        params["theta"] = self.theta
450        return params
451
452    def setParams(self, params):
453        x = params["radius"]
454        self.set_cursor(x, None)
455
456
457class LineInteractor(BaseInteractor):
458    """
459    Select an annulus through a 2D plot
460    """
461    def __init__(self, base, axes, color='black',
462                 zorder=5, r=1.0, theta=numpy.pi / 4):
463        BaseInteractor.__init__(self, base, axes, color=color)
464
465        self.markers = []
466        self.axes = axes
467        self.save_theta = theta
468        self.theta = theta
469        self.radius = r
470        self.scale = 10.0
471        # Inner circle
472        x1 = self.radius * numpy.cos(self.theta)
473        y1 = self.radius * numpy.sin(self.theta)
474        x2 = -1 * self.radius * numpy.cos(self.theta)
475        y2 = -1 * self.radius * numpy.sin(self.theta)
476        # Inner circle marker
477        self.inner_marker = self.axes.plot([x1 / 2.5], [y1 / 2.5], linestyle='',
478                                           marker='s', markersize=10,
479                                           color=self.color, alpha=0.6,
480                                           pickradius=5, label="pick",
481                                           zorder=zorder,
482                                           visible=True)[0]
483        self.line = self.axes.plot([x1, x2], [y1, y2],
484                                   linestyle='-', marker='',
485                                   color=self.color, visible=True)[0]
486        self.npts = 20
487        self.has_move = False
488        self.connect_markers([self.inner_marker, self.line])
489        self.update()
490
491    def set_layer(self, n):
492        self.layernum = n
493        self.update()
494
495    def clear(self):
496        self.clear_markers()
497        try:
498            self.inner_marker.remove()
499            self.line.remove()
500        except:
501            # Old version of matplotlib
502            for item in range(len(self.axes.lines)):
503                del self.axes.lines[0]
504
505    def update(self, theta=None):
506        """
507        Draw the new roughness on the graph.
508        """
509
[cee5c78]510        if theta is not None:
[dc5ef15]511            self.theta = theta
512        x1 = self.radius * numpy.cos(self.theta)
513        y1 = self.radius * numpy.sin(self.theta)
514        x2 = -1 * self.radius * numpy.cos(self.theta)
515        y2 = -1 * self.radius * numpy.sin(self.theta)
516
517        self.inner_marker.set(xdata=[x1 / 2.5], ydata=[y1 / 2.5])
518        self.line.set(xdata=[x1, x2], ydata=[y1, y2])
519
520    def save(self, ev):
521        """
522        Remember the roughness for this layer and the next so that we
523        can restore on Esc.
524        """
525        self.save_theta = self.theta
526
527    def moveend(self, ev):
528        self.has_move = False
529        self.base.moveend(ev)
530
531    def restore(self):
532        """
533        Restore the roughness for this layer.
534        """
535        self.theta = self.save_theta
536
537    def move(self, x, y, ev):
538        """
539        Process move to a new position, making sure that the move is allowed.
540        """
541        self.theta = numpy.arctan2(y, x)
542        self.has_move = True
543        self.base.base.update()
544
545    def set_cursor(self, x, y):
546        self.move(x, y, None)
547        self.update()
548
549    def getParams(self):
550        params = {}
551        params["radius"] = self.radius
552        params["theta"] = self.theta
553        return params
554
555    def setParams(self, params):
556        x = params["radius"]
557        self.set_cursor(x, None)
Note: See TracBrowser for help on using the repository browser.