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