[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 | |
---|
[0d9dae8] | 8 | import math |
---|
| 9 | import wx |
---|
| 10 | from copy import deepcopy |
---|
[ef0c170] | 11 | # Debug printout |
---|
[0d9dae8] | 12 | from sans.guicomm.events import NewPlotEvent, StatusEvent,SlicerParameterEvent,EVT_SLICER_PARS |
---|
[ef0c170] | 13 | from BaseInteractor import _BaseInteractor |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | class AnnulusInteractor(_BaseInteractor): |
---|
| 17 | """ |
---|
| 18 | Select an annulus through a 2D plot |
---|
| 19 | """ |
---|
| 20 | def __init__(self,base,axes,color='black', zorder=3): |
---|
| 21 | |
---|
| 22 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
| 23 | self.markers = [] |
---|
| 24 | self.axes = axes |
---|
[a2c38de] | 25 | self.base= base |
---|
| 26 | self.qmax = min(math.fabs(self.base.data2D.xmax),math.fabs(self.base.data2D.xmin)) #must be positive |
---|
[ef0c170] | 27 | self.connect = self.base.connect |
---|
| 28 | |
---|
[a2c38de] | 29 | |
---|
[ef0c170] | 30 | ## Number of points on the plot |
---|
| 31 | self.nbins = 20 |
---|
[a2c38de] | 32 | |
---|
| 33 | #Cursor position of Rings (Left(-1) or Right(1)) |
---|
| 34 | self.xmaxd=self.base.data2D.xmax |
---|
| 35 | self.xmind=self.base.data2D.xmin |
---|
[ef0c170] | 36 | |
---|
[a2c38de] | 37 | #self.sign=1 |
---|
| 38 | |
---|
| 39 | if (self.xmaxd+self.xmind)>0: |
---|
| 40 | self.sign=1 |
---|
| 41 | else: |
---|
| 42 | self.sign=-1 |
---|
| 43 | |
---|
[ef0c170] | 44 | # Inner circle |
---|
[a2c38de] | 45 | self.inner_circle = RingInteractor(self, self.base.subplot, zorder=zorder, r=self.qmax/2.0,sign=self.sign) |
---|
[bd1d9d9] | 46 | self.inner_circle.qmax = self.qmax |
---|
[a2c38de] | 47 | self.outer_circle = RingInteractor(self, self.base.subplot, zorder=zorder+1, r=self.qmax/1.8,sign=self.sign) |
---|
[bd1d9d9] | 48 | self.outer_circle.qmax = self.qmax*1.2 |
---|
[ca88b2e] | 49 | |
---|
[ef0c170] | 50 | self.update() |
---|
[7ab9241] | 51 | self._post_data() |
---|
[ef0c170] | 52 | |
---|
| 53 | # Bind to slice parameter events |
---|
[0d9dae8] | 54 | self.base.parent.Bind(EVT_SLICER_PARS, self._onEVT_SLICER_PARS) |
---|
[a2c38de] | 55 | |
---|
[d468daa] | 56 | |
---|
[ef0c170] | 57 | |
---|
| 58 | def _onEVT_SLICER_PARS(self, event): |
---|
| 59 | wx.PostEvent(self.base.parent, StatusEvent(status="AnnulusSlicer._onEVT_SLICER_PARS")) |
---|
| 60 | event.Skip() |
---|
| 61 | if event.type == self.__class__.__name__: |
---|
| 62 | self.set_params(event.params) |
---|
| 63 | self.base.update() |
---|
| 64 | |
---|
| 65 | def update_and_post(self): |
---|
| 66 | self.update() |
---|
| 67 | self._post_data() |
---|
| 68 | |
---|
| 69 | def save_data(self, path, image, x, y): |
---|
| 70 | output = open(path, 'w') |
---|
| 71 | |
---|
| 72 | data_x, data_y = self.get_data(image, x, y) |
---|
| 73 | |
---|
| 74 | output.write("<phi> <average>\n") |
---|
| 75 | for i in range(len(data_x)): |
---|
| 76 | output.write("%g %g\n" % (data_x[i], data_y[i])) |
---|
| 77 | output.close() |
---|
| 78 | |
---|
| 79 | def set_layer(self, n): |
---|
| 80 | self.layernum = n |
---|
| 81 | self.update() |
---|
| 82 | |
---|
| 83 | def clear(self): |
---|
| 84 | self.clear_markers() |
---|
| 85 | self.outer_circle.clear() |
---|
| 86 | self.inner_circle.clear() |
---|
| 87 | #self.base.connect.disconnect() |
---|
[d468daa] | 88 | #self.base.parent.Unbind(EVT_SLICER_PARS) |
---|
| 89 | self.base.Unbind(EVT_SLICER_PARS) |
---|
[ef0c170] | 90 | |
---|
| 91 | def update(self): |
---|
| 92 | """ |
---|
| 93 | Respond to changes in the model by recalculating the profiles and |
---|
| 94 | resetting the widgets. |
---|
| 95 | """ |
---|
| 96 | # Update locations |
---|
| 97 | self.inner_circle.update() |
---|
| 98 | self.outer_circle.update() |
---|
| 99 | |
---|
| 100 | |
---|
| 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.inner_circle.save(ev) |
---|
| 109 | self.outer_circle.save(ev) |
---|
| 110 | |
---|
[5554566] | 111 | def _post_data(self,nbins=None): |
---|
[ef0c170] | 112 | # Compute data |
---|
| 113 | data = self.base.data2D |
---|
| 114 | # If we have no data, just return |
---|
| 115 | if data == None: |
---|
| 116 | return |
---|
| 117 | |
---|
| 118 | from DataLoader.manipulations import SectorPhi |
---|
[a2c38de] | 119 | radius = self.qmax#math.sqrt(math.pow(self.qmax,2)+math.pow(self.qmax,2)) |
---|
| 120 | rmin= math.fabs(self.inner_circle.get_radius()) |
---|
| 121 | rmax = math.fabs(self.outer_circle.get_radius()) |
---|
[e4032d64] | 122 | phi_min=-math.pi |
---|
| 123 | phi_max=math.pi |
---|
| 124 | phimi=phi_min+math.pi |
---|
| 125 | phima=phi_max+math.pi |
---|
| 126 | sect = SectorPhi(r_min=rmin , r_max= rmax, phi_min=phimi, phi_max=phima) |
---|
[ef0c170] | 127 | if nbins!=None: |
---|
| 128 | sect.nbins = nbins |
---|
| 129 | |
---|
| 130 | sector = sect(self.base.data2D) |
---|
| 131 | |
---|
| 132 | from sans.guiframe.dataFitting import Data1D |
---|
| 133 | if hasattr(sector,"dxl"): |
---|
| 134 | dxl= sector.dxl |
---|
| 135 | else: |
---|
| 136 | dxl= None |
---|
| 137 | if hasattr(sector,"dxw"): |
---|
| 138 | dxw= sector.dxw |
---|
| 139 | else: |
---|
| 140 | dxw= None |
---|
| 141 | |
---|
| 142 | new_plot = Data1D(x=sector.x,y=sector.y,dy=sector.dy,dxl=dxl,dxw=dxw) |
---|
| 143 | new_plot.name = "SectorPhi" +"("+ self.base.data2D.name+")" |
---|
| 144 | |
---|
| 145 | |
---|
[e4032d64] | 146 | |
---|
[ef0c170] | 147 | new_plot.source=self.base.data2D.source |
---|
[ac9a5f6] | 148 | #new_plot.info=self.base.data2D.info |
---|
[ef0c170] | 149 | new_plot.interactive = True |
---|
| 150 | #print "loader output.detector",output.source |
---|
| 151 | new_plot.detector =self.base.data2D.detector |
---|
| 152 | # If the data file does not tell us what the axes are, just assume... |
---|
[0d9dae8] | 153 | new_plot.xaxis("\\rm{\phi}", 'rad') |
---|
[ef0c170] | 154 | new_plot.yaxis("\\rm{Intensity} ","cm^{-1}") |
---|
| 155 | new_plot.group_id = "SectorPhi"+self.base.data2D.name |
---|
[ffd23b5] | 156 | new_plot.xtransform="x" |
---|
| 157 | new_plot.ytransform="y" |
---|
[ef0c170] | 158 | wx.PostEvent(self.base.parent, NewPlotEvent(plot=new_plot, |
---|
| 159 | title="SectorPhi" )) |
---|
| 160 | |
---|
| 161 | |
---|
| 162 | |
---|
| 163 | |
---|
| 164 | def moveend(self, ev): |
---|
| 165 | self.base.thaw_axes() |
---|
| 166 | |
---|
| 167 | # Post paramters |
---|
[0d9dae8] | 168 | event = SlicerParameterEvent() |
---|
[ef0c170] | 169 | event.type = self.__class__.__name__ |
---|
| 170 | event.params = self.get_params() |
---|
[d468daa] | 171 | #wx.PostEvent(self.base.parent, event) |
---|
| 172 | wx.PostEvent(self.base, event) |
---|
[ef0c170] | 173 | self._post_data() |
---|
| 174 | |
---|
| 175 | def restore(self): |
---|
| 176 | """ |
---|
| 177 | Restore the roughness for this layer. |
---|
| 178 | """ |
---|
| 179 | self.inner_circle.restore() |
---|
| 180 | self.outer_circle.restore() |
---|
| 181 | |
---|
| 182 | def move(self, x, y, ev): |
---|
| 183 | """ |
---|
| 184 | Process move to a new position, making sure that the move is allowed. |
---|
| 185 | """ |
---|
| 186 | pass |
---|
| 187 | |
---|
| 188 | def set_cursor(self, x, y): |
---|
| 189 | pass |
---|
| 190 | |
---|
| 191 | def get_params(self): |
---|
| 192 | params = {} |
---|
| 193 | params["inner_radius"] = self.inner_circle._inner_mouse_x |
---|
| 194 | params["outer_radius"] = self.outer_circle._inner_mouse_x |
---|
| 195 | params["nbins"] = self.nbins |
---|
| 196 | return params |
---|
| 197 | |
---|
| 198 | def set_params(self, params): |
---|
| 199 | |
---|
| 200 | inner = params["inner_radius"] |
---|
| 201 | outer = params["outer_radius"] |
---|
| 202 | self.nbins = int(params["nbins"]) |
---|
| 203 | self.inner_circle.set_cursor(inner, self.inner_circle._inner_mouse_y) |
---|
| 204 | self.outer_circle.set_cursor(outer, self.outer_circle._inner_mouse_y) |
---|
[5554566] | 205 | self._post_data(self.nbins) |
---|
[ef0c170] | 206 | |
---|
| 207 | def freeze_axes(self): |
---|
| 208 | self.base.freeze_axes() |
---|
| 209 | |
---|
| 210 | def thaw_axes(self): |
---|
| 211 | self.base.thaw_axes() |
---|
| 212 | |
---|
| 213 | def draw(self): |
---|
| 214 | self.base.draw() |
---|
| 215 | |
---|
| 216 | |
---|
| 217 | class RingInteractor(_BaseInteractor): |
---|
| 218 | """ |
---|
| 219 | Select an annulus through a 2D plot |
---|
| 220 | """ |
---|
[a2c38de] | 221 | def __init__(self,base,axes,color='black', zorder=5, r=1.0,sign=1): |
---|
[ef0c170] | 222 | |
---|
| 223 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
| 224 | self.markers = [] |
---|
| 225 | self.axes = axes |
---|
| 226 | self._inner_mouse_x = r |
---|
| 227 | self._inner_mouse_y = 0 |
---|
| 228 | self._inner_save_x = r |
---|
| 229 | self._inner_save_y = 0 |
---|
| 230 | self.scale = 10.0 |
---|
[a2c38de] | 231 | self.base= base |
---|
| 232 | self.sign=sign |
---|
| 233 | |
---|
[ef0c170] | 234 | |
---|
[d468daa] | 235 | #print "sign",self.sign,self.sign*math.fabs(self._inner_mouse_x) |
---|
[a2c38de] | 236 | |
---|
[ef0c170] | 237 | try: |
---|
| 238 | # Inner circle marker |
---|
[a2c38de] | 239 | self.inner_marker = self.axes.plot([self.sign*math.fabs(self._inner_mouse_x)],[0], linestyle='', |
---|
[ef0c170] | 240 | marker='s', markersize=10, |
---|
| 241 | color=self.color, alpha=0.6, |
---|
| 242 | pickradius=5, label="pick", |
---|
| 243 | zorder=zorder, # Prefer this to other lines |
---|
| 244 | visible=True)[0] |
---|
| 245 | except: |
---|
[a2c38de] | 246 | self.inner_marker = self.axes.plot([self.sign*math.fabs(self._inner_mouse_x)],[0], linestyle='', |
---|
[ef0c170] | 247 | marker='s', markersize=10, |
---|
| 248 | color=self.color, alpha=0.6, |
---|
| 249 | label="pick", |
---|
| 250 | visible=True)[0] |
---|
| 251 | message = "\nTHIS PROTOTYPE NEEDS THE LATEST VERSION OF MATPLOTLIB\n" |
---|
| 252 | message += "Get the SVN version that is at least as recent as June 1, 2007" |
---|
| 253 | |
---|
| 254 | #raise "Version error", message |
---|
| 255 | |
---|
| 256 | # Inner circle |
---|
| 257 | |
---|
| 258 | |
---|
| 259 | [self.inner_circle] = self.axes.plot([],[], |
---|
| 260 | linestyle='-', marker='', |
---|
| 261 | color=self.color) |
---|
[e4032d64] | 262 | self.npts = 40 |
---|
[ef0c170] | 263 | |
---|
| 264 | self.connect_markers([self.inner_marker]) |
---|
| 265 | self.update() |
---|
| 266 | |
---|
| 267 | def set_layer(self, n): |
---|
| 268 | self.layernum = n |
---|
| 269 | self.update() |
---|
| 270 | |
---|
| 271 | def clear(self): |
---|
| 272 | self.clear_markers() |
---|
| 273 | try: |
---|
| 274 | self.inner_marker.remove() |
---|
| 275 | self.inner_circle.remove() |
---|
| 276 | except: |
---|
| 277 | # Old version of matplotlib |
---|
| 278 | for item in range(len(self.axes.lines)): |
---|
| 279 | del self.axes.lines[0] |
---|
| 280 | |
---|
| 281 | |
---|
| 282 | |
---|
| 283 | def get_radius(self): |
---|
| 284 | return self._inner_mouse_x |
---|
| 285 | |
---|
| 286 | def update(self): |
---|
| 287 | """ |
---|
| 288 | Draw the new roughness on the graph. |
---|
| 289 | """ |
---|
| 290 | # Plot inner circle |
---|
| 291 | x = [] |
---|
| 292 | y = [] |
---|
| 293 | for i in range(self.npts): |
---|
| 294 | phi = 2.0*math.pi/(self.npts-1)*i |
---|
| 295 | |
---|
| 296 | xval = 1.0*self._inner_mouse_x*math.cos(phi) |
---|
| 297 | yval = 1.0*self._inner_mouse_x*math.sin(phi) |
---|
| 298 | |
---|
| 299 | x.append(xval) |
---|
| 300 | y.append(yval) |
---|
[a2c38de] | 301 | |
---|
| 302 | self.inner_marker.set(xdata=[self.sign*math.fabs(self._inner_mouse_x)],ydata=[0]) |
---|
[ef0c170] | 303 | self.inner_circle.set_data(x, y) |
---|
| 304 | |
---|
| 305 | def save(self, ev): |
---|
| 306 | """ |
---|
| 307 | Remember the roughness for this layer and the next so that we |
---|
| 308 | can restore on Esc. |
---|
| 309 | """ |
---|
| 310 | self._inner_save_x = self._inner_mouse_x |
---|
| 311 | self._inner_save_y = self._inner_mouse_y |
---|
| 312 | self.base.freeze_axes() |
---|
| 313 | |
---|
| 314 | def moveend(self, ev): |
---|
| 315 | self.base.moveend(ev) |
---|
| 316 | |
---|
| 317 | def restore(self): |
---|
| 318 | """ |
---|
| 319 | Restore the roughness for this layer. |
---|
| 320 | """ |
---|
| 321 | self._inner_mouse_x = self._inner_save_x |
---|
| 322 | self._inner_mouse_y = self._inner_save_y |
---|
| 323 | |
---|
| 324 | def move(self, x, y, ev): |
---|
| 325 | """ |
---|
| 326 | Process move to a new position, making sure that the move is allowed. |
---|
| 327 | """ |
---|
| 328 | self._inner_mouse_x = x |
---|
| 329 | self._inner_mouse_y = y |
---|
| 330 | self.base.base.update() |
---|
| 331 | |
---|
| 332 | def set_cursor(self, x, y): |
---|
| 333 | self.move(x, y, None) |
---|
| 334 | self.update() |
---|
| 335 | |
---|
| 336 | |
---|
| 337 | def get_params(self): |
---|
| 338 | params = {} |
---|
[a2c38de] | 339 | params["radius"] = math.fabs(self._inner_mouse_x) |
---|
[ef0c170] | 340 | return params |
---|
| 341 | |
---|
| 342 | def set_params(self, params): |
---|
| 343 | |
---|
| 344 | x = params["radius"] |
---|
| 345 | self.set_cursor(x, self._inner_mouse_y) |
---|
| 346 | |
---|
| 347 | |
---|