[e20870bc] | 1 | import numpy |
---|
| 2 | |
---|
| 3 | from sas.qtgui.Plotting.Slicers.BaseInteractor import BaseInteractor |
---|
| 4 | from sas.qtgui.Plotting.Slicers.AnnulusSlicer import RingInteractor |
---|
| 5 | |
---|
| 6 | class CircularMask(BaseInteractor): |
---|
| 7 | """ |
---|
| 8 | Draw a ring Given a radius |
---|
| 9 | """ |
---|
| 10 | def __init__(self, base, axes, color='grey', zorder=3, side=None): |
---|
| 11 | """ |
---|
| 12 | :param: the color of the line that defined the ring |
---|
| 13 | :param r: the radius of the ring |
---|
| 14 | :param sign: the direction of motion the the marker |
---|
| 15 | """ |
---|
| 16 | BaseInteractor.__init__(self, base, axes, color=color) |
---|
| 17 | self.markers = [] |
---|
| 18 | self.axes = axes |
---|
| 19 | self.base = base |
---|
| 20 | self.is_inside = side |
---|
| 21 | self.qmax = min(numpy.fabs(self.base.data.xmax), |
---|
| 22 | numpy.fabs(self.base.data.xmin)) # must be positive |
---|
| 23 | self.connect = self.base.connect |
---|
| 24 | |
---|
| 25 | # Cursor position of Rings (Left(-1) or Right(1)) |
---|
| 26 | self.xmaxd = self.base.data.xmax |
---|
| 27 | self.xmind = self.base.data.xmin |
---|
| 28 | |
---|
| 29 | if (self.xmaxd + self.xmind) > 0: |
---|
| 30 | self.sign = 1 |
---|
| 31 | else: |
---|
| 32 | self.sign = -1 |
---|
| 33 | # Inner circle |
---|
| 34 | self.outer_circle = RingInteractor(self, self.axes, 'blue', |
---|
| 35 | zorder=zorder + 1, r=self.qmax / 1.8, |
---|
| 36 | sign=self.sign) |
---|
| 37 | self.outer_circle.qmax = self.qmax * 1.2 |
---|
| 38 | self.update() |
---|
| 39 | self._post_data() |
---|
| 40 | |
---|
| 41 | def set_layer(self, n): |
---|
| 42 | """ |
---|
| 43 | Allow adding plot to the same panel |
---|
| 44 | :param n: the number of layer |
---|
| 45 | """ |
---|
| 46 | self.layernum = n |
---|
| 47 | self.update() |
---|
| 48 | |
---|
| 49 | def clear(self): |
---|
| 50 | """ |
---|
| 51 | Clear the slicer and all connected events related to this slicer |
---|
| 52 | """ |
---|
| 53 | self.clear_markers() |
---|
| 54 | self.outer_circle.clear() |
---|
| 55 | |
---|
| 56 | self.base.connect.clearall() |
---|
| 57 | |
---|
| 58 | def update(self): |
---|
| 59 | """ |
---|
| 60 | Respond to changes in the model by recalculating the profiles and |
---|
| 61 | resetting the widgets. |
---|
| 62 | """ |
---|
| 63 | # Update locations |
---|
| 64 | self.outer_circle.update() |
---|
| 65 | self._post_data() |
---|
| 66 | out = self._post_data() |
---|
| 67 | return out |
---|
| 68 | |
---|
| 69 | def save(self, ev): |
---|
| 70 | """ |
---|
| 71 | Remember the roughness for this layer and the next so that we |
---|
| 72 | can restore on Esc. |
---|
| 73 | """ |
---|
| 74 | self.outer_circle.save(ev) |
---|
| 75 | |
---|
| 76 | def _post_data(self): |
---|
| 77 | """ |
---|
| 78 | Uses annulus parameters to plot averaged data into 1D data. |
---|
| 79 | |
---|
| 80 | :param nbins: the number of points to plot |
---|
| 81 | |
---|
| 82 | """ |
---|
| 83 | # Data to average |
---|
| 84 | data = self.base.data |
---|
| 85 | |
---|
| 86 | # If we have no data, just return |
---|
| 87 | if data is None: |
---|
| 88 | return |
---|
| 89 | mask = data.mask |
---|
| 90 | from sas.sascalc.dataloader.manipulations import Ringcut |
---|
| 91 | |
---|
| 92 | rmin = 0 |
---|
| 93 | rmax = numpy.fabs(self.outer_circle.get_radius()) |
---|
| 94 | |
---|
| 95 | # Create the data1D Q average of data2D |
---|
| 96 | mask = Ringcut(r_min=rmin, r_max=rmax) |
---|
| 97 | |
---|
| 98 | if self.is_inside: |
---|
| 99 | out = (mask(data) == False) |
---|
| 100 | else: |
---|
| 101 | out = (mask(data)) |
---|
| 102 | return out |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | def moveend(self, ev): |
---|
| 106 | """ |
---|
| 107 | Called when any dragging motion ends. |
---|
| 108 | Post an event (type =SlicerParameterEvent) |
---|
| 109 | to plotter 2D with a copy slicer parameters |
---|
| 110 | Call _post_data method |
---|
| 111 | """ |
---|
| 112 | #self.base.thaw_axes() |
---|
| 113 | # create a 1D data plot |
---|
| 114 | self._post_data() |
---|
| 115 | |
---|
| 116 | def restore(self): |
---|
| 117 | """ |
---|
| 118 | Restore the roughness for this layer. |
---|
| 119 | """ |
---|
| 120 | self.outer_circle.restore() |
---|
| 121 | |
---|
| 122 | def move(self, x, y, ev): |
---|
| 123 | """ |
---|
| 124 | Process move to a new position, making sure that the move is allowed. |
---|
| 125 | """ |
---|
| 126 | pass |
---|
| 127 | |
---|
| 128 | def set_cursor(self, x, y): |
---|
| 129 | pass |
---|
| 130 | |
---|
| 131 | def getParams(self): |
---|
| 132 | """ |
---|
| 133 | Store a copy of values of parameters of the slicer into a dictionary. |
---|
| 134 | |
---|
| 135 | :return params: the dictionary created |
---|
| 136 | |
---|
| 137 | """ |
---|
| 138 | params = {} |
---|
| 139 | params["outer_radius"] = numpy.fabs(self.outer_circle._inner_mouse_x) |
---|
| 140 | return params |
---|
| 141 | |
---|
| 142 | def setParams(self, params): |
---|
| 143 | """ |
---|
| 144 | Receive a dictionary and reset the slicer with values contained |
---|
| 145 | in the values of the dictionary. |
---|
| 146 | |
---|
| 147 | :param params: a dictionary containing name of slicer parameters and |
---|
| 148 | values the user assigned to the slicer. |
---|
| 149 | """ |
---|
| 150 | outer = numpy.fabs(params["outer_radius"]) |
---|
| 151 | # Update the picture |
---|
| 152 | self.outer_circle.set_cursor(outer, self.outer_circle._inner_mouse_y) |
---|
| 153 | # Post the data given the nbins entered by the user |
---|
| 154 | self._post_data() |
---|
| 155 | |
---|
| 156 | def draw(self): |
---|
| 157 | self.base.update() |
---|
| 158 | |
---|