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