[c039589] | 1 | """ |
---|
| 2 | Sector mask interactor |
---|
| 3 | """ |
---|
[c5874f2] | 4 | import math |
---|
| 5 | import wx |
---|
[32c0841] | 6 | #from copy import deepcopy |
---|
[c5874f2] | 7 | from BaseInteractor import _BaseInteractor |
---|
[32c0841] | 8 | from SectorSlicer import SideInteractor |
---|
| 9 | from SectorSlicer import LineInteractor |
---|
[79492222] | 10 | from sas.guiframe.events import SlicerParameterEvent |
---|
[c5874f2] | 11 | |
---|
| 12 | class SectorMask(_BaseInteractor): |
---|
| 13 | """ |
---|
[d955bf19] | 14 | Draw a sector slicer.Allow to find the data 2D inside of the sector lines |
---|
[c5874f2] | 15 | """ |
---|
[32c0841] | 16 | def __init__(self, base, axes, color='gray', zorder=3, side=False): |
---|
| 17 | """ |
---|
| 18 | """ |
---|
[c5874f2] | 19 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
| 20 | ## Class initialization |
---|
| 21 | self.markers = [] |
---|
[c039589] | 22 | self.axes = axes |
---|
[c5874f2] | 23 | self.is_inside = side |
---|
[c039589] | 24 | ## connect the plot to event |
---|
[c5874f2] | 25 | self.connect = self.base.connect |
---|
[c039589] | 26 | |
---|
| 27 | ## compute qmax limit to reset the graph |
---|
| 28 | x = math.pow(max(self.base.data.xmax, |
---|
[32c0841] | 29 | math.fabs(self.base.data.xmin)), 2) |
---|
[c039589] | 30 | y = math.pow(max(self.base.data.ymax, |
---|
[32c0841] | 31 | math.fabs(self.base.data.ymin)), 2) |
---|
| 32 | self.qmax = math.sqrt(x + y) |
---|
[c5874f2] | 33 | ## Number of points on the plot |
---|
| 34 | self.nbins = 20 |
---|
| 35 | ## Angle of the middle line |
---|
[c039589] | 36 | self.theta2 = math.pi / 3 |
---|
[c5874f2] | 37 | ## Absolute value of the Angle between the middle line and any side line |
---|
[c039589] | 38 | self.phi = math.pi / 12 |
---|
| 39 | |
---|
[c5874f2] | 40 | ## Middle line |
---|
[32c0841] | 41 | self.main_line = LineInteractor(self, self.base.subplot, color='blue', |
---|
[c039589] | 42 | zorder=zorder, r=self.qmax, theta=self.theta2) |
---|
[c5874f2] | 43 | self.main_line.qmax = self.qmax |
---|
| 44 | ## Right Side line |
---|
[32c0841] | 45 | self.right_line = SideInteractor(self, self.base.subplot, color='gray', |
---|
[c039589] | 46 | zorder=zorder, r=self.qmax, phi=-1 * self.phi, |
---|
| 47 | theta2=self.theta2) |
---|
[c5874f2] | 48 | self.right_line.qmax = self.qmax |
---|
[c039589] | 49 | ## Left Side line |
---|
| 50 | self.left_line = SideInteractor(self, self.base.subplot, color='gray', |
---|
| 51 | zorder=zorder, r=self.qmax, phi=self.phi, |
---|
| 52 | theta2=self.theta2) |
---|
[c5874f2] | 53 | self.left_line.qmax = self.qmax |
---|
[c039589] | 54 | ## draw the sector |
---|
[c5874f2] | 55 | self.update() |
---|
| 56 | self._post_data() |
---|
[c039589] | 57 | |
---|
[c5874f2] | 58 | def clear(self): |
---|
| 59 | """ |
---|
[d955bf19] | 60 | Clear the slicer and all connected events related to this slicer |
---|
[c5874f2] | 61 | """ |
---|
| 62 | self.clear_markers() |
---|
| 63 | self.main_line.clear() |
---|
| 64 | self.left_line.clear() |
---|
| 65 | self.right_line.clear() |
---|
| 66 | self.base.connect.clearall() |
---|
[c039589] | 67 | |
---|
[c5874f2] | 68 | def update(self): |
---|
| 69 | """ |
---|
[d955bf19] | 70 | Respond to changes in the model by recalculating the profiles and |
---|
| 71 | resetting the widgets. |
---|
[c5874f2] | 72 | """ |
---|
[c039589] | 73 | # Update locations |
---|
| 74 | ## Check if the middle line was dragged and |
---|
| 75 | #update the picture accordingly |
---|
[c5874f2] | 76 | if self.main_line.has_move: |
---|
| 77 | self.main_line.update() |
---|
[c039589] | 78 | self.right_line.update(delta=-self.left_line.phi / 2, |
---|
[32c0841] | 79 | mline=self.main_line.theta) |
---|
[c039589] | 80 | self.left_line.update(delta=self.left_line.phi / 2, |
---|
| 81 | mline=self.main_line.theta) |
---|
| 82 | ## Check if the left side has moved and update the slicer accordingly |
---|
[c5874f2] | 83 | if self.left_line.has_move: |
---|
| 84 | self.main_line.update() |
---|
[c039589] | 85 | self.left_line.update(phi=None, delta=None, mline=self.main_line, |
---|
| 86 | side=True, left=True) |
---|
[32c0841] | 87 | self.right_line.update(phi=self.left_line.phi, delta=None, |
---|
[c039589] | 88 | mline=self.main_line, side=True, |
---|
| 89 | left=False, right=True) |
---|
| 90 | ## Check if the right side line has moved and |
---|
[32c0841] | 91 | #update the slicer accordingly |
---|
[c5874f2] | 92 | if self.right_line.has_move: |
---|
| 93 | self.main_line.update() |
---|
[32c0841] | 94 | self.right_line.update(phi=None, delta=None, mline=self.main_line, |
---|
| 95 | side=True, left=False, right=True) |
---|
| 96 | self.left_line.update(phi=self.right_line.phi, delta=None, |
---|
[c039589] | 97 | mline=self.main_line, side=True, left=False) |
---|
[c5874f2] | 98 | #if self.is_inside != None: |
---|
| 99 | out = self._post_data() |
---|
| 100 | return out |
---|
| 101 | |
---|
| 102 | def save(self, ev): |
---|
| 103 | """ |
---|
| 104 | Remember the roughness for this layer and the next so that we |
---|
| 105 | can restore on Esc. |
---|
| 106 | """ |
---|
| 107 | self.base.freeze_axes() |
---|
| 108 | self.main_line.save(ev) |
---|
| 109 | self.right_line.save(ev) |
---|
| 110 | self.left_line.save(ev) |
---|
| 111 | |
---|
| 112 | def _post_data(self): |
---|
| 113 | """ |
---|
[d955bf19] | 114 | compute sector averaging of data into data1D |
---|
[c5874f2] | 115 | """ |
---|
| 116 | ## get the data to average |
---|
| 117 | data = self.base.data |
---|
| 118 | # If we have no data, just return |
---|
| 119 | if data == None: |
---|
| 120 | return |
---|
| 121 | ## Averaging |
---|
[79492222] | 122 | from sas.dataloader.manipulations import Sectorcut |
---|
[c039589] | 123 | phimin = -self.left_line.phi + self.main_line.theta |
---|
[c5874f2] | 124 | phimax = self.left_line.phi + self.main_line.theta |
---|
[c039589] | 125 | |
---|
[32c0841] | 126 | mask = Sectorcut(phi_min=phimin, phi_max=phimax) |
---|
[c5874f2] | 127 | if self.is_inside: |
---|
[32c0841] | 128 | out = (mask(data) == False) |
---|
[c5874f2] | 129 | else: |
---|
| 130 | out = (mask(data)) |
---|
[c039589] | 131 | return out |
---|
[c5874f2] | 132 | |
---|
| 133 | def moveend(self, ev): |
---|
| 134 | """ |
---|
[c039589] | 135 | Called a dragging motion ends.Get slicer event |
---|
[c5874f2] | 136 | """ |
---|
| 137 | self.base.thaw_axes() |
---|
| 138 | ## Post parameters |
---|
| 139 | event = SlicerParameterEvent() |
---|
| 140 | event.type = self.__class__.__name__ |
---|
| 141 | event.params = self.get_params() |
---|
| 142 | ## Send slicer paramers to plotter2D |
---|
| 143 | wx.PostEvent(self.base, event) |
---|
| 144 | self._post_data() |
---|
[c039589] | 145 | |
---|
[c5874f2] | 146 | def restore(self): |
---|
| 147 | """ |
---|
| 148 | Restore the roughness for this layer. |
---|
| 149 | """ |
---|
| 150 | self.main_line.restore() |
---|
| 151 | self.left_line.restore() |
---|
| 152 | self.right_line.restore() |
---|
| 153 | |
---|
| 154 | def move(self, x, y, ev): |
---|
| 155 | """ |
---|
| 156 | Process move to a new position, making sure that the move is allowed. |
---|
| 157 | """ |
---|
| 158 | pass |
---|
[c039589] | 159 | |
---|
[c5874f2] | 160 | def set_cursor(self, x, y): |
---|
| 161 | pass |
---|
[c039589] | 162 | |
---|
[c5874f2] | 163 | def get_params(self): |
---|
| 164 | """ |
---|
[d955bf19] | 165 | Store a copy of values of parameters of the slicer into a dictionary. |
---|
[c039589] | 166 | |
---|
[d955bf19] | 167 | :return params: the dictionary created |
---|
[c039589] | 168 | |
---|
[c5874f2] | 169 | """ |
---|
| 170 | params = {} |
---|
[c039589] | 171 | ## Always make sure that the left and the right line are at phi |
---|
[c5874f2] | 172 | ## angle of the middle line |
---|
| 173 | if math.fabs(self.left_line.phi) != math.fabs(self.right_line.phi): |
---|
[32c0841] | 174 | msg = "Phi left and phi right are " |
---|
[c039589] | 175 | msg += "different %f, %f" % (self.left_line.phi, |
---|
[32c0841] | 176 | self.right_line.phi) |
---|
| 177 | raise ValueError, msg |
---|
[c5874f2] | 178 | params["Phi"] = self.main_line.theta |
---|
| 179 | params["Delta_Phi"] = math.fabs(self.left_line.phi) |
---|
| 180 | return params |
---|
[c039589] | 181 | |
---|
[c5874f2] | 182 | def set_params(self, params): |
---|
| 183 | """ |
---|
[c039589] | 184 | Receive a dictionary and reset the slicer with values contained |
---|
[d955bf19] | 185 | in the values of the dictionary. |
---|
[c039589] | 186 | |
---|
| 187 | :param params: a dictionary containing name of slicer parameters and |
---|
[c5874f2] | 188 | values the user assigned to the slicer. |
---|
| 189 | """ |
---|
[c039589] | 190 | main = params["Phi"] |
---|
[32c0841] | 191 | phi = math.fabs(params["Delta_Phi"]) |
---|
[c039589] | 192 | |
---|
[32c0841] | 193 | self.main_line.theta = main |
---|
[c5874f2] | 194 | ## Reset the slicer parameters |
---|
| 195 | self.main_line.update() |
---|
[32c0841] | 196 | self.right_line.update(phi=phi, delta=None, mline=self.main_line, |
---|
| 197 | side=True, right=True) |
---|
| 198 | self.left_line.update(phi=phi, delta=None, |
---|
| 199 | mline=self.main_line, side=True) |
---|
[c5874f2] | 200 | ## post the new corresponding data |
---|
| 201 | self._post_data() |
---|
[c039589] | 202 | |
---|
[c5874f2] | 203 | def freeze_axes(self): |
---|
[d955bf19] | 204 | """ |
---|
| 205 | """ |
---|
[c5874f2] | 206 | self.base.freeze_axes() |
---|
[c039589] | 207 | |
---|
[c5874f2] | 208 | def thaw_axes(self): |
---|
[d955bf19] | 209 | """ |
---|
| 210 | """ |
---|
[c5874f2] | 211 | self.base.thaw_axes() |
---|
| 212 | |
---|
| 213 | def draw(self): |
---|
[d955bf19] | 214 | """ |
---|
| 215 | """ |
---|
[c5874f2] | 216 | self.base.update() |
---|