[ef0c170] | 1 | #TODO: the line slicer should listen to all 2DREFRESH events, get the data and slice it |
---|
| 2 | # before pushing a new 1D data update. |
---|
| 3 | |
---|
| 4 | # |
---|
| 5 | #TODO: NEED MAJOR REFACTOR |
---|
| 6 | # |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | # Debug printout |
---|
[0d9dae8] | 10 | import math |
---|
| 11 | import wx |
---|
| 12 | from copy import deepcopy |
---|
[ef0c170] | 13 | from BaseInteractor import _BaseInteractor |
---|
[55a0dc1] | 14 | from sans.guiframe.events import NewPlotEvent |
---|
| 15 | from sans.guiframe.events import StatusEvent |
---|
| 16 | from sans.guiframe.events import SlicerParameterEvent |
---|
| 17 | from sans.guiframe.events import EVT_SLICER_PARS |
---|
[ef0c170] | 18 | |
---|
| 19 | |
---|
| 20 | class SectorInteractor(_BaseInteractor): |
---|
| 21 | """ |
---|
[83f4445] | 22 | Select an annulus through a 2D plot |
---|
[ef0c170] | 23 | """ |
---|
[32c0841] | 24 | def __init__(self, base, axes, color='black', zorder=3): |
---|
[83f4445] | 25 | """ |
---|
| 26 | """ |
---|
[ef0c170] | 27 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
| 28 | self.markers = [] |
---|
| 29 | self.axes = axes |
---|
[54cc36a] | 30 | self.qmax = self.base.data2D.xmax |
---|
[ef0c170] | 31 | self.connect = self.base.connect |
---|
| 32 | |
---|
| 33 | ## Number of points on the plot |
---|
| 34 | self.nbins = 20 |
---|
[32c0841] | 35 | theta1 = math.pi/8 |
---|
| 36 | theta2 = math.pi/2 |
---|
| 37 | theta1 = 2 * math.pi/3 |
---|
| 38 | theta2 = -2 * math.pi/3 |
---|
| 39 | r1 = self.qmax/2.0 |
---|
| 40 | r2 = self.qmax/1.8 |
---|
[ef0c170] | 41 | |
---|
| 42 | # Inner circle |
---|
| 43 | from Arc import ArcInteractor |
---|
[32c0841] | 44 | self.inner_circle = ArcInteractor(self, self.base.subplot, |
---|
| 45 | zorder=zorder, |
---|
| 46 | r=self.qmax/2.0, |
---|
| 47 | theta1=theta1, |
---|
[ef0c170] | 48 | theta2=theta2) |
---|
[54cc36a] | 49 | self.inner_circle.qmax = self.qmax |
---|
[32c0841] | 50 | self.outer_circle = ArcInteractor(self, self.base.subplot, |
---|
| 51 | zorder=zorder+1, |
---|
| 52 | r=self.qmax/1.8, |
---|
| 53 | theta1=theta1, |
---|
[ef0c170] | 54 | theta2=theta2) |
---|
[32c0841] | 55 | self.outer_circle.qmax = self.qmax * 1.2 |
---|
[ef0c170] | 56 | #self.outer_circle.set_cursor(self.base.qmax/1.8, 0) |
---|
| 57 | from Edge import RadiusInteractor |
---|
[32c0841] | 58 | self.right_edge= RadiusInteractor(self, self.base.subplot, |
---|
| 59 | zorder=zorder+1, |
---|
[ef0c170] | 60 | arc1=self.inner_circle, |
---|
| 61 | arc2=self.outer_circle, |
---|
| 62 | theta=theta1) |
---|
[32c0841] | 63 | self.left_edge= RadiusInteractor(self, self.base.subplot, |
---|
| 64 | zorder=zorder+1, |
---|
[ef0c170] | 65 | arc1=self.inner_circle, |
---|
| 66 | arc2=self.outer_circle, |
---|
| 67 | theta=theta2) |
---|
| 68 | self.update() |
---|
| 69 | self._post_data() |
---|
| 70 | # Bind to slice parameter events |
---|
[0d9dae8] | 71 | self.base.parent.Bind(EVT_SLICER_PARS, self._onEVT_SLICER_PARS) |
---|
[ef0c170] | 72 | |
---|
| 73 | def _onEVT_SLICER_PARS(self, event): |
---|
[83f4445] | 74 | """ |
---|
| 75 | """ |
---|
[ef0c170] | 76 | #printEVT("AnnulusSlicer._onEVT_SLICER_PARS") |
---|
| 77 | event.Skip() |
---|
| 78 | if event.type == self.__class__.__name__: |
---|
| 79 | self.set_params(event.params) |
---|
| 80 | self.base.update() |
---|
| 81 | |
---|
| 82 | def save_data(self, path, image, x, y): |
---|
[83f4445] | 83 | """ |
---|
| 84 | """ |
---|
[ef0c170] | 85 | output = open(path, 'w') |
---|
| 86 | data_x, data_y = self.get_data(image, x, y) |
---|
| 87 | |
---|
| 88 | output.write("<phi> <average>\n") |
---|
| 89 | for i in range(len(data_x)): |
---|
| 90 | output.write("%g %g\n" % (data_x[i], data_y[i])) |
---|
| 91 | output.close() |
---|
| 92 | |
---|
| 93 | def set_layer(self, n): |
---|
[83f4445] | 94 | """ |
---|
| 95 | """ |
---|
[ef0c170] | 96 | self.layernum = n |
---|
| 97 | self.update() |
---|
| 98 | |
---|
| 99 | def clear(self): |
---|
[83f4445] | 100 | """ |
---|
| 101 | """ |
---|
[ef0c170] | 102 | self.clear_markers() |
---|
| 103 | self.outer_circle.clear() |
---|
| 104 | self.inner_circle.clear() |
---|
| 105 | self.right_edge.clear() |
---|
| 106 | self.left_edge.clear() |
---|
| 107 | #self.base.connect.disconnect() |
---|
[0d9dae8] | 108 | self.base.parent.Unbind(EVT_SLICER_PARS) |
---|
[ef0c170] | 109 | |
---|
| 110 | def update(self): |
---|
| 111 | """ |
---|
| 112 | Respond to changes in the model by recalculating the profiles and |
---|
| 113 | resetting the widgets. |
---|
| 114 | """ |
---|
| 115 | # Update locations |
---|
| 116 | if self.inner_circle.has_move: |
---|
[83f4445] | 117 | #print "inner circle has moved" |
---|
[ef0c170] | 118 | self.inner_circle.update() |
---|
[32c0841] | 119 | r1 = self.inner_circle.get_radius() |
---|
| 120 | r2 = self.outer_circle.get_radius() |
---|
| 121 | self.right_edge.update(r1, r2) |
---|
| 122 | self.left_edge.update(r1, r2) |
---|
[ef0c170] | 123 | if self.outer_circle.has_move: |
---|
[83f4445] | 124 | #print "outer circle has moved" |
---|
[ef0c170] | 125 | self.outer_circle.update() |
---|
[32c0841] | 126 | r1 = self.inner_circle.get_radius() |
---|
| 127 | r2 = self.outer_circle.get_radius() |
---|
| 128 | self.left_edge.update(r1, r2) |
---|
| 129 | self.right_edge.update(r1, r2) |
---|
[ef0c170] | 130 | if self.right_edge.has_move: |
---|
[83f4445] | 131 | #print "right edge has moved" |
---|
[ef0c170] | 132 | self.right_edge.update() |
---|
[32c0841] | 133 | self.inner_circle.update(theta1=self.right_edge.get_angle(), |
---|
| 134 | theta2=None) |
---|
| 135 | self.outer_circle.update(theta1=self.right_edge.get_angle(), |
---|
| 136 | theta2=None) |
---|
[ef0c170] | 137 | if self.left_edge.has_move: |
---|
[83f4445] | 138 | #print "left Edge has moved" |
---|
[ef0c170] | 139 | self.left_edge.update() |
---|
[32c0841] | 140 | self.inner_circle.update(theta1=None, |
---|
| 141 | theta2=self.left_edge.get_angle()) |
---|
| 142 | self.outer_circle.update(theta1=None, |
---|
| 143 | theta2=self.left_edge.get_angle()) |
---|
[ef0c170] | 144 | |
---|
| 145 | def save(self, ev): |
---|
| 146 | """ |
---|
| 147 | Remember the roughness for this layer and the next so that we |
---|
| 148 | can restore on Esc. |
---|
| 149 | """ |
---|
| 150 | self.base.freeze_axes() |
---|
| 151 | self.inner_circle.save(ev) |
---|
| 152 | self.outer_circle.save(ev) |
---|
| 153 | self.right_edge.save(ev) |
---|
| 154 | self.left_edge.save(ev) |
---|
| 155 | |
---|
| 156 | def _post_data(self): |
---|
| 157 | pass |
---|
| 158 | |
---|
| 159 | def post_data(self,new_sector ): |
---|
| 160 | """ post data averaging in Q""" |
---|
| 161 | if self.inner_circle.get_radius() < self.outer_circle.get_radius(): |
---|
[32c0841] | 162 | rmin = self.inner_circle.get_radius() |
---|
| 163 | rmax = self.outer_circle.get_radius() |
---|
[ef0c170] | 164 | else: |
---|
[32c0841] | 165 | rmin = self.outer_circle.get_radius() |
---|
| 166 | rmax = self.inner_circle.get_radius() |
---|
[ef0c170] | 167 | if self.right_edge.get_angle() < self.left_edge.get_angle(): |
---|
[32c0841] | 168 | phimin = self.right_edge.get_angle() |
---|
| 169 | phimax = self.left_edge.get_angle() |
---|
[ef0c170] | 170 | else: |
---|
[32c0841] | 171 | phimin = self.left_edge.get_angle() |
---|
| 172 | phimax = self.right_edge.get_angle() |
---|
| 173 | #print "phimin, phimax, rmin ,rmax",math.degrees(phimin), |
---|
| 174 | # math.degrees(phimax), rmin ,rmax |
---|
[db7a82e] | 175 | #from sans.dataloader.manipulations import SectorQ |
---|
[ef0c170] | 176 | |
---|
[32c0841] | 177 | sect = new_sector(r_min=rmin, r_max=rmax, |
---|
| 178 | phi_min=phimin, phi_max=phimax) |
---|
[ef0c170] | 179 | sector = sect(self.base.data2D) |
---|
| 180 | |
---|
| 181 | from sans.guiframe.dataFitting import Data1D |
---|
[32c0841] | 182 | if hasattr(sector, "dxl"): |
---|
| 183 | dxl = sector.dxl |
---|
[ef0c170] | 184 | else: |
---|
[32c0841] | 185 | dxl = None |
---|
| 186 | if hasattr(sector, "dxw"): |
---|
| 187 | dxw = sector.dxw |
---|
[ef0c170] | 188 | else: |
---|
[32c0841] | 189 | dxw = None |
---|
| 190 | new_plot = Data1D(x=sector.x, y=sector.y, dy=sector.dy, |
---|
| 191 | dxl=dxl, dxw=dxw) |
---|
| 192 | new_plot.name = str(new_sector.__name__) + \ |
---|
| 193 | "("+ self.base.data2D.name+")" |
---|
| 194 | new_plot.source = self.base.data2D.source |
---|
[ef0c170] | 195 | new_plot.interactive = True |
---|
| 196 | #print "loader output.detector",output.source |
---|
[32c0841] | 197 | new_plot.detector = self.base.data2D.detector |
---|
[ef0c170] | 198 | # If the data file does not tell us what the axes are, just assume... |
---|
| 199 | new_plot.xaxis("\\rm{Q}", 'rad') |
---|
[32c0841] | 200 | new_plot.yaxis("\\rm{Intensity} ", "cm^{-1}") |
---|
| 201 | new_plot.group_id = str(new_sector.__name__) + self.base.data2D.name |
---|
| 202 | wx.PostEvent(self.base.parent, |
---|
| 203 | NewPlotEvent(plot=new_plot, title=str(new_sector.__name__))) |
---|
[ef0c170] | 204 | |
---|
| 205 | def moveend(self, ev): |
---|
| 206 | #self.base.thaw_axes() |
---|
| 207 | |
---|
[32c0841] | 208 | # Post paramters |
---|
[0d9dae8] | 209 | #event = SlicerParameterEvent() |
---|
[ef0c170] | 210 | #event.type = self.__class__.__name__ |
---|
| 211 | #event.params = self.get_params() |
---|
| 212 | #print "main moveend ", event.params |
---|
| 213 | #wx.PostEvent(self.base.parent, event) |
---|
[78cae5a] | 214 | #self._post_data() |
---|
| 215 | pass |
---|
[ef0c170] | 216 | |
---|
| 217 | def restore(self): |
---|
| 218 | """ |
---|
| 219 | Restore the roughness for this layer. |
---|
| 220 | """ |
---|
| 221 | self.inner_circle.restore() |
---|
| 222 | self.outer_circle.restore() |
---|
| 223 | self.right_edge.restore() |
---|
| 224 | self.left_edge.restore() |
---|
| 225 | |
---|
| 226 | def move(self, x, y, ev): |
---|
| 227 | """ |
---|
| 228 | Process move to a new position, making sure that the move is allowed. |
---|
| 229 | """ |
---|
| 230 | pass |
---|
| 231 | |
---|
| 232 | def set_cursor(self, x, y): |
---|
[83f4445] | 233 | """ |
---|
| 234 | """ |
---|
[ef0c170] | 235 | pass |
---|
| 236 | |
---|
| 237 | def get_params(self): |
---|
[83f4445] | 238 | """ |
---|
| 239 | """ |
---|
[ef0c170] | 240 | params = {} |
---|
| 241 | params["r_min"] = self.inner_circle.get_radius() |
---|
| 242 | params["r_max"] = self.outer_circle.get_radius() |
---|
| 243 | params["phi_min"] = self.right_edge.get_angle() |
---|
| 244 | params["phi_max"] = self.left_edge.get_angle() |
---|
| 245 | params["nbins"] = self.nbins |
---|
| 246 | return params |
---|
| 247 | |
---|
| 248 | def set_params(self, params): |
---|
[83f4445] | 249 | """ |
---|
| 250 | """ |
---|
| 251 | #print "setparams on main slicer ",params |
---|
[ef0c170] | 252 | inner = params["r_min"] |
---|
| 253 | outer = params["r_max"] |
---|
| 254 | phi_min= params["phi_min"] |
---|
| 255 | phi_max=params["phi_max"] |
---|
| 256 | self.nbins = int(params["nbins"]) |
---|
| 257 | |
---|
| 258 | self.inner_circle.set_cursor(inner, phi_min, phi_max,self.nbins) |
---|
| 259 | self.outer_circle.set_cursor(outer, phi_min, phi_max, self.nbins) |
---|
| 260 | self.right_edge.set_cursor(inner, outer, phi_min) |
---|
| 261 | self.left_edge.set_cursor(inner, outer, phi_max) |
---|
| 262 | self._post_data() |
---|
| 263 | |
---|
| 264 | def freeze_axes(self): |
---|
[83f4445] | 265 | """ |
---|
| 266 | """ |
---|
[ef0c170] | 267 | self.base.freeze_axes() |
---|
| 268 | |
---|
| 269 | def thaw_axes(self): |
---|
[83f4445] | 270 | """ |
---|
| 271 | """ |
---|
[ef0c170] | 272 | self.base.thaw_axes() |
---|
| 273 | |
---|
| 274 | def draw(self): |
---|
[83f4445] | 275 | """ |
---|
| 276 | """ |
---|
[ef0c170] | 277 | self.base.draw() |
---|
| 278 | |
---|
| 279 | class SectorInteractorQ(SectorInteractor): |
---|
[83f4445] | 280 | """ |
---|
| 281 | """ |
---|
[32c0841] | 282 | def __init__(self, base, axes, color='black', zorder=3): |
---|
[83f4445] | 283 | """ |
---|
| 284 | """ |
---|
[ef0c170] | 285 | SectorInteractor.__init__(self, base, axes, color=color) |
---|
| 286 | self.base=base |
---|
| 287 | self._post_data() |
---|
[83f4445] | 288 | |
---|
[ef0c170] | 289 | def _post_data(self): |
---|
[83f4445] | 290 | """ |
---|
| 291 | """ |
---|
[db7a82e] | 292 | from sans.dataloader.manipulations import SectorQ |
---|
[32c0841] | 293 | self.post_data(SectorQ) |
---|
[ef0c170] | 294 | |
---|
| 295 | |
---|
| 296 | class SectorInteractorPhi(SectorInteractor): |
---|
[83f4445] | 297 | """ |
---|
| 298 | """ |
---|
[32c0841] | 299 | def __init__(self, base, axes, color='black', zorder=3): |
---|
[83f4445] | 300 | """ |
---|
| 301 | """ |
---|
[ef0c170] | 302 | SectorInteractor.__init__(self, base, axes, color=color) |
---|
| 303 | self.base=base |
---|
| 304 | self._post_data() |
---|
[83f4445] | 305 | |
---|
[ef0c170] | 306 | def _post_data(self): |
---|
[83f4445] | 307 | """ |
---|
| 308 | """ |
---|
[db7a82e] | 309 | from sans.dataloader.manipulations import SectorPhi |
---|
[ef0c170] | 310 | self.post_data(SectorPhi ) |
---|
| 311 | |
---|
| 312 | |
---|