[f60a8c2] | 1 | """ |
---|
| 2 | Data manipulations for 2D data sets. |
---|
| 3 | Using the meta data information, various types of averaging |
---|
| 4 | are performed in Q-space |
---|
| 5 | """ |
---|
[0997158f] | 6 | ##################################################################### |
---|
| 7 | #This software was developed by the University of Tennessee as part of the |
---|
| 8 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
[f60a8c2] | 9 | #project funded by the US National Science Foundation. |
---|
[0997158f] | 10 | #See the license text in license.txt |
---|
| 11 | #copyright 2008, University of Tennessee |
---|
| 12 | ###################################################################### |
---|
| 13 | |
---|
[76e2369] | 14 | #TODO: copy the meta data from the 2D object to the resulting 1D object |
---|
| 15 | import math |
---|
| 16 | import numpy |
---|
| 17 | |
---|
[a7a5886] | 18 | #from data_info import plottable_2D |
---|
| 19 | from data_info import Data1D |
---|
| 20 | |
---|
| 21 | |
---|
[76e2369] | 22 | def get_q(dx, dy, det_dist, wavelength): |
---|
| 23 | """ |
---|
[0997158f] | 24 | :param dx: x-distance from beam center [mm] |
---|
| 25 | :param dy: y-distance from beam center [mm] |
---|
| 26 | |
---|
| 27 | :return: q-value at the given position |
---|
[76e2369] | 28 | """ |
---|
| 29 | # Distance from beam center in the plane of detector |
---|
| 30 | plane_dist = math.sqrt(dx*dx + dy*dy) |
---|
| 31 | # Half of the scattering angle |
---|
[a7a5886] | 32 | theta = 0.5 * math.atan(plane_dist/det_dist) |
---|
| 33 | return (4.0 * math.pi/wavelength) * math.sin(theta) |
---|
[acb37d9] | 34 | |
---|
[f60a8c2] | 35 | |
---|
[a7a5886] | 36 | def get_q_compo(dx, dy, det_dist, wavelength, compo=None): |
---|
[0997158f] | 37 | """ |
---|
| 38 | This reduces tiny error at very large q. |
---|
| 39 | Implementation of this func is not started yet.<--ToDo |
---|
| 40 | """ |
---|
[a7a5886] | 41 | if dy == 0: |
---|
| 42 | if dx >= 0: |
---|
| 43 | angle_xy = 0 |
---|
[acb37d9] | 44 | else: |
---|
[a7a5886] | 45 | angle_xy = math.pi |
---|
[acb37d9] | 46 | else: |
---|
[a7a5886] | 47 | angle_xy = math.atan(dx/dy) |
---|
[acb37d9] | 48 | |
---|
[a7a5886] | 49 | if compo == "x": |
---|
| 50 | out = get_q(dx, dy, det_dist, wavelength) * math.cos(angle_xy) |
---|
| 51 | elif compo == "y": |
---|
| 52 | out = get_q(dx, dy, det_dist, wavelength) * math.sin(angle_xy) |
---|
[acb37d9] | 53 | else: |
---|
[a7a5886] | 54 | out = get_q(dx, dy, det_dist, wavelength) |
---|
[acb37d9] | 55 | return out |
---|
[095ab1b] | 56 | |
---|
[f60a8c2] | 57 | |
---|
[095ab1b] | 58 | def flip_phi(phi): |
---|
| 59 | """ |
---|
[0997158f] | 60 | Correct phi to within the 0 <= to <= 2pi range |
---|
| 61 | |
---|
| 62 | :return: phi in >=0 and <=2Pi |
---|
[095ab1b] | 63 | """ |
---|
| 64 | Pi = math.pi |
---|
| 65 | if phi < 0: |
---|
[f60a8c2] | 66 | phi_out = phi + (2 * Pi) |
---|
[a7a5886] | 67 | elif phi > (2 * Pi): |
---|
[f60a8c2] | 68 | phi_out = phi - (2 * Pi) |
---|
[095ab1b] | 69 | else: |
---|
[f60a8c2] | 70 | phi_out = phi |
---|
[095ab1b] | 71 | return phi_out |
---|
| 72 | |
---|
[f60a8c2] | 73 | |
---|
[095ab1b] | 74 | def reader2D_converter(data2d=None): |
---|
| 75 | """ |
---|
[a7a5886] | 76 | convert old 2d format opened by IhorReader or danse_reader |
---|
| 77 | to new Data2D format |
---|
[0997158f] | 78 | |
---|
| 79 | :param data2d: 2d array of Data2D object |
---|
| 80 | |
---|
| 81 | :return: 1d arrays of Data2D object |
---|
| 82 | |
---|
[095ab1b] | 83 | """ |
---|
[a7a5886] | 84 | if data2d.data == None or data2d.x_bins == None or data2d.y_bins == None: |
---|
| 85 | raise ValueError, "Can't convert this data: data=None..." |
---|
[76e2369] | 86 | |
---|
[47045e6] | 87 | from sans.dataloader.data_info import Data2D |
---|
[095ab1b] | 88 | |
---|
[a7a5886] | 89 | new_x = numpy.tile(data2d.x_bins, (len(data2d.y_bins), 1)) |
---|
| 90 | new_y = numpy.tile(data2d.y_bins, (len(data2d.x_bins), 1)) |
---|
| 91 | new_y = new_y.swapaxes(0, 1) |
---|
[095ab1b] | 92 | |
---|
| 93 | new_data = data2d.data.flatten() |
---|
| 94 | qx_data = new_x.flatten() |
---|
| 95 | qy_data = new_y.flatten() |
---|
[a7a5886] | 96 | q_data = numpy.sqrt(qx_data*qx_data + qy_data*qy_data) |
---|
[f60a8c2] | 97 | if data2d.err_data == None or numpy.any(data2d.err_data <= 0): |
---|
[f265927] | 98 | new_err_data = numpy.sqrt(numpy.abs(new_data)) |
---|
[dde2d44] | 99 | else: |
---|
| 100 | new_err_data = data2d.err_data.flatten() |
---|
[f60a8c2] | 101 | mask = numpy.ones(len(new_data), dtype=bool) |
---|
[095ab1b] | 102 | |
---|
[f60a8c2] | 103 | #TODO: make sense of the following two lines... |
---|
[095ab1b] | 104 | output = Data2D() |
---|
| 105 | output = data2d |
---|
| 106 | output.data = new_data |
---|
| 107 | output.err_data = new_err_data |
---|
| 108 | output.qx_data = qx_data |
---|
| 109 | output.qy_data = qy_data |
---|
| 110 | output.q_data = q_data |
---|
| 111 | output.mask = mask |
---|
| 112 | |
---|
| 113 | return output |
---|
| 114 | |
---|
[f60a8c2] | 115 | |
---|
[70975f3] | 116 | class _Slab(object): |
---|
| 117 | """ |
---|
[0997158f] | 118 | Compute average I(Q) for a region of interest |
---|
[70975f3] | 119 | """ |
---|
[a7a5886] | 120 | def __init__(self, x_min=0.0, x_max=0.0, y_min=0.0, |
---|
| 121 | y_max=0.0, bin_width=0.001): |
---|
[70975f3] | 122 | # Minimum Qx value [A-1] |
---|
| 123 | self.x_min = x_min |
---|
| 124 | # Maximum Qx value [A-1] |
---|
| 125 | self.x_max = x_max |
---|
| 126 | # Minimum Qy value [A-1] |
---|
| 127 | self.y_min = y_min |
---|
| 128 | # Maximum Qy value [A-1] |
---|
| 129 | self.y_max = y_max |
---|
| 130 | # Bin width (step size) [A-1] |
---|
| 131 | self.bin_width = bin_width |
---|
[a7a5886] | 132 | # If True, I(|Q|) will be return, otherwise, |
---|
| 133 | # negative q-values are allowed |
---|
[70975f3] | 134 | self.fold = False |
---|
| 135 | |
---|
[a7a5886] | 136 | def __call__(self, data2D): |
---|
| 137 | return NotImplemented |
---|
[70975f3] | 138 | |
---|
| 139 | def _avg(self, data2D, maj): |
---|
| 140 | """ |
---|
[0997158f] | 141 | Compute average I(Q_maj) for a region of interest. |
---|
| 142 | The major axis is defined as the axis of Q_maj. |
---|
| 143 | The minor axis is the axis that we average over. |
---|
| 144 | |
---|
| 145 | :param data2D: Data2D object |
---|
| 146 | :param maj_min: min value on the major axis |
---|
| 147 | |
---|
| 148 | :return: Data1D object |
---|
[70975f3] | 149 | """ |
---|
| 150 | if len(data2D.detector) != 1: |
---|
[a7a5886] | 151 | msg = "_Slab._avg: invalid number of " |
---|
| 152 | msg += " detectors: %g" % len(data2D.detector) |
---|
| 153 | raise RuntimeError, msg |
---|
[70975f3] | 154 | |
---|
[f60a8c2] | 155 | # Get data |
---|
[c6f95bb] | 156 | data = data2D.data[numpy.isfinite(data2D.data)] |
---|
| 157 | q_data = data2D.q_data[numpy.isfinite(data2D.data)] |
---|
| 158 | err_data = data2D.err_data[numpy.isfinite(data2D.data)] |
---|
[f60a8c2] | 159 | qx_data = data2D.qx_data[numpy.isfinite(data2D.data)] |
---|
[c6f95bb] | 160 | qy_data = data2D.qy_data[numpy.isfinite(data2D.data)] |
---|
[095ab1b] | 161 | |
---|
[70975f3] | 162 | # Build array of Q intervals |
---|
[a7a5886] | 163 | if maj == 'x': |
---|
| 164 | if self.fold: |
---|
[f60a8c2] | 165 | x_min = 0 |
---|
| 166 | else: |
---|
| 167 | x_min = self.x_min |
---|
| 168 | nbins = int(math.ceil((self.x_max - x_min) / self.bin_width)) |
---|
[a7a5886] | 169 | qbins = self.bin_width * numpy.arange(nbins) + x_min |
---|
| 170 | elif maj == 'y': |
---|
[f60a8c2] | 171 | if self.fold: |
---|
| 172 | y_min = 0 |
---|
| 173 | else: |
---|
| 174 | y_min = self.y_min |
---|
[a7a5886] | 175 | nbins = int(math.ceil((self.y_max - y_min)/self.bin_width)) |
---|
[f60a8c2] | 176 | qbins = self.bin_width * numpy.arange(nbins) + y_min |
---|
[70975f3] | 177 | else: |
---|
| 178 | raise RuntimeError, "_Slab._avg: unrecognized axis %s" % str(maj) |
---|
| 179 | |
---|
[f60a8c2] | 180 | x = numpy.zeros(nbins) |
---|
| 181 | y = numpy.zeros(nbins) |
---|
[70975f3] | 182 | err_y = numpy.zeros(nbins) |
---|
| 183 | y_counts = numpy.zeros(nbins) |
---|
| 184 | |
---|
[f60a8c2] | 185 | # Average pixelsize in q space |
---|
| 186 | for npts in range(len(data)): |
---|
| 187 | # default frac |
---|
[095ab1b] | 188 | frac_x = 0 |
---|
| 189 | frac_y = 0 |
---|
| 190 | # get ROI |
---|
| 191 | if self.x_min <= qx_data[npts] and self.x_max > qx_data[npts]: |
---|
| 192 | frac_x = 1 |
---|
| 193 | if self.y_min <= qy_data[npts] and self.y_max > qy_data[npts]: |
---|
| 194 | frac_y = 1 |
---|
| 195 | frac = frac_x * frac_y |
---|
| 196 | |
---|
[a7a5886] | 197 | if frac == 0: |
---|
| 198 | continue |
---|
[095ab1b] | 199 | # binning: find axis of q |
---|
[f60a8c2] | 200 | if maj == 'x': |
---|
[095ab1b] | 201 | q_value = qx_data[npts] |
---|
[f60a8c2] | 202 | min = x_min |
---|
| 203 | if maj == 'y': |
---|
| 204 | q_value = qy_data[npts] |
---|
[095ab1b] | 205 | min = y_min |
---|
[a7a5886] | 206 | if self.fold and q_value < 0: |
---|
[f60a8c2] | 207 | q_value = -q_value |
---|
[095ab1b] | 208 | # bin |
---|
[f60a8c2] | 209 | i_q = int(math.ceil((q_value - min) / self.bin_width)) - 1 |
---|
[095ab1b] | 210 | |
---|
| 211 | # skip outside of max bins |
---|
[a7a5886] | 212 | if i_q < 0 or i_q >= nbins: |
---|
| 213 | continue |
---|
[095ab1b] | 214 | |
---|
| 215 | #TODO: find better definition of x[i_q] based on q_data |
---|
[f60a8c2] | 216 | x[i_q] += frac * q_value # min + (i_q + 1) * self.bin_width / 2.0 |
---|
[a7a5886] | 217 | y[i_q] += frac * data[npts] |
---|
[095ab1b] | 218 | |
---|
[a7a5886] | 219 | if err_data == None or err_data[npts] == 0.0: |
---|
[f60a8c2] | 220 | if data[npts] < 0: |
---|
| 221 | data[npts] = -data[npts] |
---|
[c6f95bb] | 222 | err_y[i_q] += frac * frac * data[npts] |
---|
[095ab1b] | 223 | else: |
---|
| 224 | err_y[i_q] += frac * frac * err_data[npts] * err_data[npts] |
---|
[f60a8c2] | 225 | y_counts[i_q] += frac |
---|
[8ba103f] | 226 | |
---|
[f60a8c2] | 227 | # Average the sums |
---|
[095ab1b] | 228 | for n in range(nbins): |
---|
| 229 | err_y[n] = math.sqrt(err_y[n]) |
---|
| 230 | |
---|
[a7a5886] | 231 | err_y = err_y / y_counts |
---|
[f60a8c2] | 232 | y = y / y_counts |
---|
| 233 | x = x / y_counts |
---|
| 234 | idx = (numpy.isfinite(y) & numpy.isfinite(x)) |
---|
[095ab1b] | 235 | |
---|
| 236 | if not idx.any(): |
---|
[f60a8c2] | 237 | msg = "Average Error: No points inside ROI to average..." |
---|
[a7a5886] | 238 | raise ValueError, msg |
---|
| 239 | #elif len(y[idx])!= nbins: |
---|
| 240 | # msg = "empty bin(s) due to tight binning..." |
---|
| 241 | # print "resulted",nbins- len(y[idx]), msg |
---|
[095ab1b] | 242 | return Data1D(x=x[idx], y=y[idx], dy=err_y[idx]) |
---|
[70975f3] | 243 | |
---|
[f60a8c2] | 244 | |
---|
[70975f3] | 245 | class SlabY(_Slab): |
---|
| 246 | """ |
---|
[0997158f] | 247 | Compute average I(Qy) for a region of interest |
---|
[70975f3] | 248 | """ |
---|
| 249 | def __call__(self, data2D): |
---|
| 250 | """ |
---|
[0997158f] | 251 | Compute average I(Qy) for a region of interest |
---|
| 252 | |
---|
| 253 | :param data2D: Data2D object |
---|
| 254 | |
---|
| 255 | :return: Data1D object |
---|
[70975f3] | 256 | """ |
---|
| 257 | return self._avg(data2D, 'y') |
---|
| 258 | |
---|
[f60a8c2] | 259 | |
---|
[70975f3] | 260 | class SlabX(_Slab): |
---|
| 261 | """ |
---|
[0997158f] | 262 | Compute average I(Qx) for a region of interest |
---|
[70975f3] | 263 | """ |
---|
| 264 | def __call__(self, data2D): |
---|
| 265 | """ |
---|
[0997158f] | 266 | Compute average I(Qx) for a region of interest |
---|
| 267 | |
---|
| 268 | :param data2D: Data2D object |
---|
| 269 | |
---|
| 270 | :return: Data1D object |
---|
| 271 | |
---|
[70975f3] | 272 | """ |
---|
[f60a8c2] | 273 | return self._avg(data2D, 'x') |
---|
| 274 | |
---|
| 275 | |
---|
[f8d0ee7] | 276 | class Boxsum(object): |
---|
| 277 | """ |
---|
[0997158f] | 278 | Perform the sum of counts in a 2D region of interest. |
---|
[f8d0ee7] | 279 | """ |
---|
| 280 | def __init__(self, x_min=0.0, x_max=0.0, y_min=0.0, y_max=0.0): |
---|
| 281 | # Minimum Qx value [A-1] |
---|
| 282 | self.x_min = x_min |
---|
| 283 | # Maximum Qx value [A-1] |
---|
| 284 | self.x_max = x_max |
---|
| 285 | # Minimum Qy value [A-1] |
---|
| 286 | self.y_min = y_min |
---|
| 287 | # Maximum Qy value [A-1] |
---|
| 288 | self.y_max = y_max |
---|
| 289 | |
---|
| 290 | def __call__(self, data2D): |
---|
| 291 | """ |
---|
[f60a8c2] | 292 | Perform the sum in the region of interest |
---|
[0997158f] | 293 | |
---|
| 294 | :param data2D: Data2D object |
---|
| 295 | |
---|
[d555416] | 296 | :return: number of counts, error on number of counts, |
---|
| 297 | number of points summed |
---|
[0997158f] | 298 | |
---|
[f8d0ee7] | 299 | """ |
---|
| 300 | y, err_y, y_counts = self._sum(data2D) |
---|
| 301 | |
---|
| 302 | # Average the sums |
---|
[a7a5886] | 303 | counts = 0 if y_counts == 0 else y |
---|
[f60a8c2] | 304 | error = 0 if y_counts == 0 else math.sqrt(err_y) |
---|
[f8d0ee7] | 305 | |
---|
[d555416] | 306 | # Added y_counts to return, SMK & PDB, 04/03/2013 |
---|
| 307 | return counts, error, y_counts |
---|
[f8d0ee7] | 308 | |
---|
| 309 | def _sum(self, data2D): |
---|
| 310 | """ |
---|
[f60a8c2] | 311 | Perform the sum in the region of interest |
---|
[0997158f] | 312 | |
---|
| 313 | :param data2D: Data2D object |
---|
| 314 | |
---|
[f60a8c2] | 315 | :return: number of counts, |
---|
[a7a5886] | 316 | error on number of counts, number of entries summed |
---|
[0997158f] | 317 | |
---|
[f8d0ee7] | 318 | """ |
---|
| 319 | if len(data2D.detector) != 1: |
---|
[a7a5886] | 320 | msg = "Circular averaging: invalid number " |
---|
| 321 | msg += "of detectors: %g" % len(data2D.detector) |
---|
| 322 | raise RuntimeError, msg |
---|
[f60a8c2] | 323 | # Get data |
---|
[c6f95bb] | 324 | data = data2D.data[numpy.isfinite(data2D.data)] |
---|
| 325 | q_data = data2D.q_data[numpy.isfinite(data2D.data)] |
---|
| 326 | err_data = data2D.err_data[numpy.isfinite(data2D.data)] |
---|
[f60a8c2] | 327 | qx_data = data2D.qx_data[numpy.isfinite(data2D.data)] |
---|
[c6f95bb] | 328 | qy_data = data2D.qy_data[numpy.isfinite(data2D.data)] |
---|
[095ab1b] | 329 | |
---|
[f60a8c2] | 330 | y = 0.0 |
---|
[f8d0ee7] | 331 | err_y = 0.0 |
---|
| 332 | y_counts = 0.0 |
---|
| 333 | |
---|
[f60a8c2] | 334 | # Average pixelsize in q space |
---|
| 335 | for npts in range(len(data)): |
---|
| 336 | # default frac |
---|
| 337 | frac_x = 0 |
---|
| 338 | frac_y = 0 |
---|
[095ab1b] | 339 | |
---|
| 340 | # get min and max at each points |
---|
| 341 | qx = qx_data[npts] |
---|
| 342 | qy = qy_data[npts] |
---|
| 343 | |
---|
| 344 | # get the ROI |
---|
| 345 | if self.x_min <= qx and self.x_max > qx: |
---|
| 346 | frac_x = 1 |
---|
| 347 | if self.y_min <= qy and self.y_max > qy: |
---|
| 348 | frac_y = 1 |
---|
[f60a8c2] | 349 | #Find the fraction along each directions |
---|
[095ab1b] | 350 | frac = frac_x * frac_y |
---|
[a7a5886] | 351 | if frac == 0: |
---|
| 352 | continue |
---|
[095ab1b] | 353 | y += frac * data[npts] |
---|
[a7a5886] | 354 | if err_data == None or err_data[npts] == 0.0: |
---|
| 355 | if data[npts] < 0: |
---|
| 356 | data[npts] = -data[npts] |
---|
[c6f95bb] | 357 | err_y += frac * frac * data[npts] |
---|
[095ab1b] | 358 | else: |
---|
| 359 | err_y += frac * frac * err_data[npts] * err_data[npts] |
---|
[f60a8c2] | 360 | y_counts += frac |
---|
[f8d0ee7] | 361 | return y, err_y, y_counts |
---|
[095ab1b] | 362 | |
---|
| 363 | |
---|
[f8d0ee7] | 364 | class Boxavg(Boxsum): |
---|
| 365 | """ |
---|
[0997158f] | 366 | Perform the average of counts in a 2D region of interest. |
---|
[f8d0ee7] | 367 | """ |
---|
| 368 | def __init__(self, x_min=0.0, x_max=0.0, y_min=0.0, y_max=0.0): |
---|
[a7a5886] | 369 | super(Boxavg, self).__init__(x_min=x_min, x_max=x_max, |
---|
| 370 | y_min=y_min, y_max=y_max) |
---|
[f8d0ee7] | 371 | |
---|
| 372 | def __call__(self, data2D): |
---|
| 373 | """ |
---|
[f60a8c2] | 374 | Perform the sum in the region of interest |
---|
[0997158f] | 375 | |
---|
| 376 | :param data2D: Data2D object |
---|
| 377 | |
---|
| 378 | :return: average counts, error on average counts |
---|
| 379 | |
---|
[f8d0ee7] | 380 | """ |
---|
| 381 | y, err_y, y_counts = self._sum(data2D) |
---|
| 382 | |
---|
| 383 | # Average the sums |
---|
[f60a8c2] | 384 | counts = 0 if y_counts == 0 else y / y_counts |
---|
| 385 | error = 0 if y_counts == 0 else math.sqrt(err_y) / y_counts |
---|
[f8d0ee7] | 386 | |
---|
| 387 | return counts, error |
---|
| 388 | |
---|
[f60a8c2] | 389 | |
---|
[f8d0ee7] | 390 | def get_pixel_fraction_square(x, xmin, xmax): |
---|
| 391 | """ |
---|
[f60a8c2] | 392 | Return the fraction of the length |
---|
[0997158f] | 393 | from xmin to x.:: |
---|
| 394 | |
---|
| 395 | |
---|
| 396 | A B |
---|
| 397 | +-----------+---------+ |
---|
| 398 | xmin x xmax |
---|
| 399 | |
---|
| 400 | :param x: x-value |
---|
| 401 | :param xmin: minimum x for the length considered |
---|
| 402 | :param xmax: minimum x for the length considered |
---|
| 403 | |
---|
| 404 | :return: (x-xmin)/(xmax-xmin) when xmin < x < xmax |
---|
| 405 | |
---|
[f8d0ee7] | 406 | """ |
---|
[a7a5886] | 407 | if x <= xmin: |
---|
[f8d0ee7] | 408 | return 0.0 |
---|
[a7a5886] | 409 | if x > xmin and x < xmax: |
---|
| 410 | return (x - xmin) / (xmax - xmin) |
---|
[f8d0ee7] | 411 | else: |
---|
| 412 | return 1.0 |
---|
| 413 | |
---|
[76e2369] | 414 | |
---|
| 415 | class CircularAverage(object): |
---|
| 416 | """ |
---|
[0997158f] | 417 | Perform circular averaging on 2D data |
---|
| 418 | |
---|
| 419 | The data returned is the distribution of counts |
---|
| 420 | as a function of Q |
---|
[76e2369] | 421 | """ |
---|
[095ab1b] | 422 | def __init__(self, r_min=0.0, r_max=0.0, bin_width=0.0005): |
---|
[76e2369] | 423 | # Minimum radius included in the average [A-1] |
---|
| 424 | self.r_min = r_min |
---|
| 425 | # Maximum radius included in the average [A-1] |
---|
| 426 | self.r_max = r_max |
---|
| 427 | # Bin width (step size) [A-1] |
---|
| 428 | self.bin_width = bin_width |
---|
| 429 | |
---|
[8f12385] | 430 | def __call__(self, data2D, ismask=False): |
---|
[76e2369] | 431 | """ |
---|
[0997158f] | 432 | Perform circular averaging on the data |
---|
| 433 | |
---|
| 434 | :param data2D: Data2D object |
---|
| 435 | |
---|
| 436 | :return: Data1D object |
---|
[76e2369] | 437 | """ |
---|
[729bcf6] | 438 | # Get data W/ finite values |
---|
[c6f95bb] | 439 | data = data2D.data[numpy.isfinite(data2D.data)] |
---|
| 440 | q_data = data2D.q_data[numpy.isfinite(data2D.data)] |
---|
[729bcf6] | 441 | qx_data = data2D.qx_data[numpy.isfinite(data2D.data)] |
---|
[c6f95bb] | 442 | err_data = data2D.err_data[numpy.isfinite(data2D.data)] |
---|
[8f12385] | 443 | mask_data = data2D.mask[numpy.isfinite(data2D.data)] |
---|
[729bcf6] | 444 | |
---|
[342a506] | 445 | dq_data = None |
---|
[729bcf6] | 446 | |
---|
| 447 | # Get the dq for resolution averaging |
---|
[342a506] | 448 | if data2D.dqx_data != None and data2D.dqy_data != None: |
---|
[f60a8c2] | 449 | # The pinholes and det. pix contribution present |
---|
[729bcf6] | 450 | # in both direction of the 2D which must be subtracted when |
---|
| 451 | # converting to 1D: dq_overlap should calculated ideally at |
---|
[f60a8c2] | 452 | # q = 0. Note This method works on only pinhole geometry. |
---|
[729bcf6] | 453 | # Extrapolate dqx(r) and dqy(phi) at q = 0, and take an average. |
---|
| 454 | z_max = max(data2D.q_data) |
---|
| 455 | z_min = min(data2D.q_data) |
---|
| 456 | x_max = data2D.dqx_data[data2D.q_data[z_max]] |
---|
[f60a8c2] | 457 | x_min = data2D.dqx_data[data2D.q_data[z_min]] |
---|
[729bcf6] | 458 | y_max = data2D.dqy_data[data2D.q_data[z_max]] |
---|
[f60a8c2] | 459 | y_min = data2D.dqy_data[data2D.q_data[z_min]] |
---|
[729bcf6] | 460 | # Find qdx at q = 0 |
---|
| 461 | dq_overlap_x = (x_min * z_max - x_max * z_min) / (z_max - z_min) |
---|
| 462 | # when extrapolation goes wrong |
---|
| 463 | if dq_overlap_x > min(data2D.dqx_data): |
---|
| 464 | dq_overlap_x = min(data2D.dqx_data) |
---|
[f60a8c2] | 465 | dq_overlap_x *= dq_overlap_x |
---|
[729bcf6] | 466 | # Find qdx at q = 0 |
---|
| 467 | dq_overlap_y = (y_min * z_max - y_max * z_min) / (z_max - z_min) |
---|
| 468 | # when extrapolation goes wrong |
---|
| 469 | if dq_overlap_y > min(data2D.dqy_data): |
---|
| 470 | dq_overlap_y = min(data2D.dqy_data) |
---|
| 471 | # get dq at q=0. |
---|
| 472 | dq_overlap_y *= dq_overlap_y |
---|
| 473 | |
---|
[f60a8c2] | 474 | dq_overlap = numpy.sqrt((dq_overlap_x + dq_overlap_y) / 2.0) |
---|
[729bcf6] | 475 | # Final protection of dq |
---|
| 476 | if dq_overlap < 0: |
---|
| 477 | dq_overlap = y_min |
---|
| 478 | dqx_data = data2D.dqx_data[numpy.isfinite(data2D.data)] |
---|
| 479 | dqy_data = data2D.dqy_data[numpy.isfinite(data2D.data)] - dq_overlap |
---|
| 480 | # def; dqx_data = dq_r dqy_data = dq_phi |
---|
| 481 | # Convert dq 2D to 1D here |
---|
[f60a8c2] | 482 | dqx = dqx_data * dqx_data |
---|
[729bcf6] | 483 | dqy = dqy_data * dqy_data |
---|
| 484 | dq_data = numpy.add(dqx, dqy) |
---|
[342a506] | 485 | dq_data = numpy.sqrt(dq_data) |
---|
| 486 | |
---|
[729bcf6] | 487 | #q_data_max = numpy.max(q_data) |
---|
[095ab1b] | 488 | if len(data2D.q_data) == None: |
---|
[a7a5886] | 489 | msg = "Circular averaging: invalid q_data: %g" % data2D.q_data |
---|
| 490 | raise RuntimeError, msg |
---|
[095ab1b] | 491 | |
---|
[76e2369] | 492 | # Build array of Q intervals |
---|
[a7a5886] | 493 | nbins = int(math.ceil((self.r_max - self.r_min) / self.bin_width)) |
---|
| 494 | qbins = self.bin_width * numpy.arange(nbins) + self.r_min |
---|
[095ab1b] | 495 | |
---|
[f60a8c2] | 496 | x = numpy.zeros(nbins) |
---|
| 497 | y = numpy.zeros(nbins) |
---|
[76e2369] | 498 | err_y = numpy.zeros(nbins) |
---|
[342a506] | 499 | err_x = numpy.zeros(nbins) |
---|
[76e2369] | 500 | y_counts = numpy.zeros(nbins) |
---|
[095ab1b] | 501 | |
---|
[f60a8c2] | 502 | for npt in range(len(data)): |
---|
[8f12385] | 503 | |
---|
| 504 | if ismask and not mask_data[npt]: |
---|
[f60a8c2] | 505 | continue |
---|
[8f12385] | 506 | |
---|
[095ab1b] | 507 | frac = 0 |
---|
[76e2369] | 508 | |
---|
[095ab1b] | 509 | # q-value at the pixel (j,i) |
---|
[f60a8c2] | 510 | q_value = q_data[npt] |
---|
| 511 | data_n = data[npt] |
---|
[3c67340] | 512 | |
---|
[095ab1b] | 513 | ## No need to calculate the frac when all data are within range |
---|
| 514 | if self.r_min >= self.r_max: |
---|
[f60a8c2] | 515 | raise ValueError, "Limit Error: min > max" |
---|
[76e2369] | 516 | |
---|
[a7a5886] | 517 | if self.r_min <= q_value and q_value <= self.r_max: |
---|
[f60a8c2] | 518 | frac = 1 |
---|
[a7a5886] | 519 | if frac == 0: |
---|
| 520 | continue |
---|
[f60a8c2] | 521 | i_q = int(math.floor((q_value - self.r_min) / self.bin_width)) |
---|
[095ab1b] | 522 | |
---|
[f60a8c2] | 523 | # Take care of the edge case at phi = 2pi. |
---|
| 524 | if i_q == nbins: |
---|
| 525 | i_q = nbins - 1 |
---|
[095ab1b] | 526 | y[i_q] += frac * data_n |
---|
[729bcf6] | 527 | # Take dqs from data to get the q_average |
---|
| 528 | x[i_q] += frac * q_value |
---|
[a7a5886] | 529 | if err_data == None or err_data[npt] == 0.0: |
---|
| 530 | if data_n < 0: |
---|
| 531 | data_n = -data_n |
---|
[c6f95bb] | 532 | err_y[i_q] += frac * frac * data_n |
---|
[8ba103f] | 533 | else: |
---|
[095ab1b] | 534 | err_y[i_q] += frac * frac * err_data[npt] * err_data[npt] |
---|
[342a506] | 535 | if dq_data != None: |
---|
[f60a8c2] | 536 | # To be consistent with dq calculation in 1d reduction, |
---|
| 537 | # we need just the averages (not quadratures) because |
---|
| 538 | # it should not depend on the number of the q points |
---|
[729bcf6] | 539 | # in the qr bins. |
---|
| 540 | err_x[i_q] += frac * dq_data[npt] |
---|
[342a506] | 541 | else: |
---|
| 542 | err_x = None |
---|
[f60a8c2] | 543 | y_counts[i_q] += frac |
---|
[095ab1b] | 544 | |
---|
[f60a8c2] | 545 | # Average the sums |
---|
[095ab1b] | 546 | for n in range(nbins): |
---|
[f60a8c2] | 547 | if err_y[n] < 0: |
---|
| 548 | err_y[n] = -err_y[n] |
---|
[095ab1b] | 549 | err_y[n] = math.sqrt(err_y[n]) |
---|
[729bcf6] | 550 | #if err_x != None: |
---|
| 551 | # err_x[n] = math.sqrt(err_x[n]) |
---|
[342a506] | 552 | |
---|
[a7a5886] | 553 | err_y = err_y / y_counts |
---|
[f60a8c2] | 554 | err_y[err_y == 0] = numpy.average(err_y) |
---|
| 555 | y = y / y_counts |
---|
| 556 | x = x / y_counts |
---|
| 557 | idx = (numpy.isfinite(y)) & (numpy.isfinite(x)) |
---|
[8f12385] | 558 | |
---|
[342a506] | 559 | if err_x != None: |
---|
| 560 | d_x = err_x[idx] / y_counts[idx] |
---|
| 561 | else: |
---|
| 562 | d_x = None |
---|
| 563 | |
---|
[f60a8c2] | 564 | if not idx.any(): |
---|
| 565 | msg = "Average Error: No points inside ROI to average..." |
---|
[a7a5886] | 566 | raise ValueError, msg |
---|
[342a506] | 567 | |
---|
| 568 | return Data1D(x=x[idx], y=y[idx], dy=err_y[idx], dx=d_x) |
---|
[76e2369] | 569 | |
---|
| 570 | |
---|
| 571 | class Ring(object): |
---|
| 572 | """ |
---|
[0997158f] | 573 | Defines a ring on a 2D data set. |
---|
| 574 | The ring is defined by r_min, r_max, and |
---|
| 575 | the position of the center of the ring. |
---|
| 576 | |
---|
| 577 | The data returned is the distribution of counts |
---|
| 578 | around the ring as a function of phi. |
---|
| 579 | |
---|
[f60a8c2] | 580 | Phi_min and phi_max should be defined between 0 and 2*pi |
---|
[0997158f] | 581 | in anti-clockwise starting from the x- axis on the left-hand side |
---|
[76e2369] | 582 | """ |
---|
[095ab1b] | 583 | #Todo: remove center. |
---|
[a7a5886] | 584 | def __init__(self, r_min=0, r_max=0, center_x=0, center_y=0, nbins=20): |
---|
[76e2369] | 585 | # Minimum radius |
---|
| 586 | self.r_min = r_min |
---|
| 587 | # Maximum radius |
---|
| 588 | self.r_max = r_max |
---|
| 589 | # Center of the ring in x |
---|
| 590 | self.center_x = center_x |
---|
| 591 | # Center of the ring in y |
---|
| 592 | self.center_y = center_y |
---|
| 593 | # Number of angular bins |
---|
[8ba103f] | 594 | self.nbins_phi = nbins |
---|
[76e2369] | 595 | |
---|
| 596 | def __call__(self, data2D): |
---|
| 597 | """ |
---|
[0997158f] | 598 | Apply the ring to the data set. |
---|
| 599 | Returns the angular distribution for a given q range |
---|
| 600 | |
---|
| 601 | :param data2D: Data2D object |
---|
| 602 | |
---|
| 603 | :return: Data1D object |
---|
[76e2369] | 604 | """ |
---|
| 605 | if data2D.__class__.__name__ not in ["Data2D", "plottable_2D"]: |
---|
| 606 | raise RuntimeError, "Ring averaging only take plottable_2D objects" |
---|
| 607 | |
---|
[095ab1b] | 608 | Pi = math.pi |
---|
| 609 | |
---|
| 610 | # Get data |
---|
[c6f95bb] | 611 | data = data2D.data[numpy.isfinite(data2D.data)] |
---|
| 612 | q_data = data2D.q_data[numpy.isfinite(data2D.data)] |
---|
| 613 | err_data = data2D.err_data[numpy.isfinite(data2D.data)] |
---|
[f60a8c2] | 614 | qx_data = data2D.qx_data[numpy.isfinite(data2D.data)] |
---|
[c6f95bb] | 615 | qy_data = data2D.qy_data[numpy.isfinite(data2D.data)] |
---|
| 616 | |
---|
[095ab1b] | 617 | q_data_max = numpy.max(q_data) |
---|
| 618 | |
---|
| 619 | # Set space for 1d outputs |
---|
[76e2369] | 620 | phi_bins = numpy.zeros(self.nbins_phi) |
---|
| 621 | phi_counts = numpy.zeros(self.nbins_phi) |
---|
| 622 | phi_values = numpy.zeros(self.nbins_phi) |
---|
| 623 | phi_err = numpy.zeros(self.nbins_phi) |
---|
| 624 | |
---|
[f60a8c2] | 625 | for npt in range(len(data)): |
---|
[095ab1b] | 626 | frac = 0 |
---|
| 627 | # q-value at the point (npt) |
---|
| 628 | q_value = q_data[npt] |
---|
[f60a8c2] | 629 | data_n = data[npt] |
---|
[095ab1b] | 630 | |
---|
| 631 | # phi-value at the point (npt) |
---|
[a7a5886] | 632 | phi_value = math.atan2(qy_data[npt], qx_data[npt]) + Pi |
---|
[76e2369] | 633 | |
---|
[a7a5886] | 634 | if self.r_min <= q_value and q_value <= self.r_max: |
---|
[f60a8c2] | 635 | frac = 1 |
---|
[a7a5886] | 636 | if frac == 0: |
---|
| 637 | continue |
---|
[095ab1b] | 638 | # binning |
---|
[a7a5886] | 639 | i_phi = int(math.floor((self.nbins_phi) * phi_value / (2 * Pi))) |
---|
[76e2369] | 640 | |
---|
[f60a8c2] | 641 | # Take care of the edge case at phi = 2pi. |
---|
| 642 | if i_phi == self.nbins_phi: |
---|
| 643 | i_phi = self.nbins_phi - 1 |
---|
[095ab1b] | 644 | phi_bins[i_phi] += frac * data[npt] |
---|
[76e2369] | 645 | |
---|
[a7a5886] | 646 | if err_data == None or err_data[npt] == 0.0: |
---|
| 647 | if data_n < 0: |
---|
| 648 | data_n = -data_n |
---|
[095ab1b] | 649 | phi_err[i_phi] += frac * frac * math.fabs(data_n) |
---|
| 650 | else: |
---|
[a7a5886] | 651 | phi_err[i_phi] += frac * frac * err_data[npt] * err_data[npt] |
---|
[095ab1b] | 652 | phi_counts[i_phi] += frac |
---|
| 653 | |
---|
[76e2369] | 654 | for i in range(self.nbins_phi): |
---|
| 655 | phi_bins[i] = phi_bins[i] / phi_counts[i] |
---|
| 656 | phi_err[i] = math.sqrt(phi_err[i]) / phi_counts[i] |
---|
[a7a5886] | 657 | phi_values[i] = 2.0 * math.pi / self.nbins_phi * (1.0 * i + 0.5) |
---|
[76e2369] | 658 | |
---|
[f60a8c2] | 659 | idx = (numpy.isfinite(phi_bins)) |
---|
[095ab1b] | 660 | |
---|
[a7a5886] | 661 | if not idx.any(): |
---|
[f60a8c2] | 662 | msg = "Average Error: No points inside ROI to average..." |
---|
[a7a5886] | 663 | raise ValueError, msg |
---|
| 664 | #elif len(phi_bins[idx])!= self.nbins_phi: |
---|
| 665 | # print "resulted",self.nbins_phi- len(phi_bins[idx]) |
---|
| 666 | #,"empty bin(s) due to tight binning..." |
---|
[095ab1b] | 667 | return Data1D(x=phi_values[idx], y=phi_bins[idx], dy=phi_err[idx]) |
---|
[76e2369] | 668 | |
---|
[f60a8c2] | 669 | |
---|
[76e2369] | 670 | def get_pixel_fraction(qmax, q_00, q_01, q_10, q_11): |
---|
| 671 | """ |
---|
[0997158f] | 672 | Returns the fraction of the pixel defined by |
---|
[f60a8c2] | 673 | the four corners (q_00, q_01, q_10, q_11) that |
---|
[0997158f] | 674 | has q < qmax.:: |
---|
| 675 | |
---|
[76e2369] | 676 | q_01 q_11 |
---|
| 677 | y=1 +--------------+ |
---|
| 678 | | | |
---|
| 679 | | | |
---|
| 680 | | | |
---|
| 681 | y=0 +--------------+ |
---|
[bb0b12c] | 682 | q_00 q_10 |
---|
[76e2369] | 683 | |
---|
| 684 | x=0 x=1 |
---|
[0997158f] | 685 | |
---|
[76e2369] | 686 | """ |
---|
| 687 | # y side for x = minx |
---|
| 688 | x_0 = get_intercept(qmax, q_00, q_01) |
---|
| 689 | # y side for x = maxx |
---|
| 690 | x_1 = get_intercept(qmax, q_10, q_11) |
---|
| 691 | |
---|
| 692 | # x side for y = miny |
---|
| 693 | y_0 = get_intercept(qmax, q_00, q_10) |
---|
| 694 | # x side for y = maxy |
---|
| 695 | y_1 = get_intercept(qmax, q_01, q_11) |
---|
| 696 | |
---|
| 697 | # surface fraction for a 1x1 pixel |
---|
| 698 | frac_max = 0 |
---|
| 699 | |
---|
| 700 | if x_0 and x_1: |
---|
[a7a5886] | 701 | frac_max = (x_0 + x_1) / 2.0 |
---|
[76e2369] | 702 | elif y_0 and y_1: |
---|
[a7a5886] | 703 | frac_max = (y_0 + y_1) / 2.0 |
---|
[76e2369] | 704 | elif x_0 and y_0: |
---|
| 705 | if q_00 < q_10: |
---|
[a7a5886] | 706 | frac_max = x_0 * y_0 / 2.0 |
---|
[76e2369] | 707 | else: |
---|
[a7a5886] | 708 | frac_max = 1.0 - x_0 * y_0 / 2.0 |
---|
[76e2369] | 709 | elif x_0 and y_1: |
---|
| 710 | if q_00 < q_10: |
---|
[a7a5886] | 711 | frac_max = x_0 * y_1 / 2.0 |
---|
[76e2369] | 712 | else: |
---|
[a7a5886] | 713 | frac_max = 1.0 - x_0 * y_1 / 2.0 |
---|
[76e2369] | 714 | elif x_1 and y_0: |
---|
| 715 | if q_00 > q_10: |
---|
[a7a5886] | 716 | frac_max = x_1 * y_0 / 2.0 |
---|
[76e2369] | 717 | else: |
---|
[a7a5886] | 718 | frac_max = 1.0 - x_1 * y_0 / 2.0 |
---|
[76e2369] | 719 | elif x_1 and y_1: |
---|
| 720 | if q_00 < q_10: |
---|
[a7a5886] | 721 | frac_max = 1.0 - (1.0 - x_1) * (1.0 - y_1) / 2.0 |
---|
[76e2369] | 722 | else: |
---|
[a7a5886] | 723 | frac_max = (1.0 - x_1) * (1.0 - y_1) / 2.0 |
---|
[76e2369] | 724 | |
---|
| 725 | # If we make it here, there is no intercept between |
---|
| 726 | # this pixel and the constant-q ring. We only need |
---|
| 727 | # to know if we have to include it or exclude it. |
---|
[f60a8c2] | 728 | elif (q_00 + q_01 + q_10 + q_11) / 4.0 < qmax: |
---|
[76e2369] | 729 | frac_max = 1.0 |
---|
[095ab1b] | 730 | |
---|
[76e2369] | 731 | return frac_max |
---|
| 732 | |
---|
[f60a8c2] | 733 | |
---|
[76e2369] | 734 | def get_intercept(q, q_0, q_1): |
---|
| 735 | """ |
---|
[0997158f] | 736 | Returns the fraction of the side at which the |
---|
| 737 | q-value intercept the pixel, None otherwise. |
---|
| 738 | The values returned is the fraction ON THE SIDE |
---|
| 739 | OF THE LOWEST Q. :: |
---|
| 740 | |
---|
| 741 | |
---|
[f60a8c2] | 742 | A B |
---|
[0997158f] | 743 | +-----------+--------+ <--- pixel size |
---|
[f60a8c2] | 744 | 0 1 |
---|
[0997158f] | 745 | Q_0 -------- Q ----- Q_1 <--- equivalent Q range |
---|
[76e2369] | 746 | if Q_1 > Q_0, A is returned |
---|
| 747 | if Q_1 < Q_0, B is returned |
---|
| 748 | if Q is outside the range of [Q_0, Q_1], None is returned |
---|
| 749 | |
---|
| 750 | """ |
---|
| 751 | if q_1 > q_0: |
---|
| 752 | if (q > q_0 and q <= q_1): |
---|
[f60a8c2] | 753 | return (q - q_0) / (q_1 - q_0) |
---|
[76e2369] | 754 | else: |
---|
| 755 | if (q > q_1 and q <= q_0): |
---|
[f60a8c2] | 756 | return (q - q_1) / (q_0 - q_1) |
---|
[76e2369] | 757 | return None |
---|
[095ab1b] | 758 | |
---|
[f60a8c2] | 759 | |
---|
[fb198a9] | 760 | class _Sector: |
---|
| 761 | """ |
---|
[0997158f] | 762 | Defines a sector region on a 2D data set. |
---|
| 763 | The sector is defined by r_min, r_max, phi_min, phi_max, |
---|
[f60a8c2] | 764 | and the position of the center of the ring |
---|
[a7a5886] | 765 | where phi_min and phi_max are defined by the right |
---|
| 766 | and left lines wrt central line |
---|
[f60a8c2] | 767 | and phi_max could be less than phi_min. |
---|
[0997158f] | 768 | |
---|
[f60a8c2] | 769 | Phi is defined between 0 and 2*pi in anti-clockwise |
---|
[a7a5886] | 770 | starting from the x- axis on the left-hand side |
---|
[fb198a9] | 771 | """ |
---|
[a7a5886] | 772 | def __init__(self, r_min, r_max, phi_min=0, phi_max=2*math.pi, nbins=20): |
---|
[fb198a9] | 773 | self.r_min = r_min |
---|
| 774 | self.r_max = r_max |
---|
| 775 | self.phi_min = phi_min |
---|
| 776 | self.phi_max = phi_max |
---|
| 777 | self.nbins = nbins |
---|
| 778 | |
---|
| 779 | def _agv(self, data2D, run='phi'): |
---|
| 780 | """ |
---|
[0997158f] | 781 | Perform sector averaging. |
---|
| 782 | |
---|
| 783 | :param data2D: Data2D object |
---|
| 784 | :param run: define the varying parameter ('phi' , 'q' , or 'q2') |
---|
| 785 | |
---|
| 786 | :return: Data1D object |
---|
[fb198a9] | 787 | """ |
---|
| 788 | if data2D.__class__.__name__ not in ["Data2D", "plottable_2D"]: |
---|
| 789 | raise RuntimeError, "Ring averaging only take plottable_2D objects" |
---|
[095ab1b] | 790 | Pi = math.pi |
---|
[c6f95bb] | 791 | |
---|
[095ab1b] | 792 | # Get the all data & info |
---|
[c6f95bb] | 793 | data = data2D.data[numpy.isfinite(data2D.data)] |
---|
| 794 | q_data = data2D.q_data[numpy.isfinite(data2D.data)] |
---|
| 795 | err_data = data2D.err_data[numpy.isfinite(data2D.data)] |
---|
[f60a8c2] | 796 | qx_data = data2D.qx_data[numpy.isfinite(data2D.data)] |
---|
[c6f95bb] | 797 | qy_data = data2D.qy_data[numpy.isfinite(data2D.data)] |
---|
[342a506] | 798 | dq_data = None |
---|
[729bcf6] | 799 | |
---|
| 800 | # Get the dq for resolution averaging |
---|
[342a506] | 801 | if data2D.dqx_data != None and data2D.dqy_data != None: |
---|
[f60a8c2] | 802 | # The pinholes and det. pix contribution present |
---|
[729bcf6] | 803 | # in both direction of the 2D which must be subtracted when |
---|
| 804 | # converting to 1D: dq_overlap should calculated ideally at |
---|
[f60a8c2] | 805 | # q = 0. |
---|
[729bcf6] | 806 | # Extrapolate dqy(perp) at q = 0 |
---|
| 807 | z_max = max(data2D.q_data) |
---|
| 808 | z_min = min(data2D.q_data) |
---|
| 809 | x_max = data2D.dqx_data[data2D.q_data[z_max]] |
---|
[f60a8c2] | 810 | x_min = data2D.dqx_data[data2D.q_data[z_min]] |
---|
[729bcf6] | 811 | y_max = data2D.dqy_data[data2D.q_data[z_max]] |
---|
[f60a8c2] | 812 | y_min = data2D.dqy_data[data2D.q_data[z_min]] |
---|
[729bcf6] | 813 | # Find qdx at q = 0 |
---|
| 814 | dq_overlap_x = (x_min * z_max - x_max * z_min) / (z_max - z_min) |
---|
| 815 | # when extrapolation goes wrong |
---|
| 816 | if dq_overlap_x > min(data2D.dqx_data): |
---|
| 817 | dq_overlap_x = min(data2D.dqx_data) |
---|
[f60a8c2] | 818 | dq_overlap_x *= dq_overlap_x |
---|
[729bcf6] | 819 | # Find qdx at q = 0 |
---|
| 820 | dq_overlap_y = (y_min * z_max - y_max * z_min) / (z_max - z_min) |
---|
| 821 | # when extrapolation goes wrong |
---|
| 822 | if dq_overlap_y > min(data2D.dqy_data): |
---|
| 823 | dq_overlap_y = min(data2D.dqy_data) |
---|
| 824 | # get dq at q=0. |
---|
| 825 | dq_overlap_y *= dq_overlap_y |
---|
| 826 | |
---|
[f60a8c2] | 827 | dq_overlap = numpy.sqrt((dq_overlap_x + dq_overlap_y) / 2.0) |
---|
[729bcf6] | 828 | if dq_overlap < 0: |
---|
| 829 | dq_overlap = y_min |
---|
| 830 | dqx_data = data2D.dqx_data[numpy.isfinite(data2D.data)] |
---|
| 831 | dqy_data = data2D.dqy_data[numpy.isfinite(data2D.data)] - dq_overlap |
---|
| 832 | # def; dqx_data = dq_r dqy_data = dq_phi |
---|
| 833 | # Convert dq 2D to 1D here |
---|
[f60a8c2] | 834 | dqx = dqx_data * dqx_data |
---|
[729bcf6] | 835 | dqy = dqy_data * dqy_data |
---|
| 836 | dq_data = numpy.add(dqx, dqy) |
---|
[342a506] | 837 | dq_data = numpy.sqrt(dq_data) |
---|
| 838 | |
---|
[095ab1b] | 839 | #set space for 1d outputs |
---|
| 840 | x = numpy.zeros(self.nbins) |
---|
[fb198a9] | 841 | y = numpy.zeros(self.nbins) |
---|
[f60a8c2] | 842 | y_err = numpy.zeros(self.nbins) |
---|
| 843 | x_err = numpy.zeros(self.nbins) |
---|
[fb198a9] | 844 | y_counts = numpy.zeros(self.nbins) |
---|
[095ab1b] | 845 | |
---|
| 846 | # Get the min and max into the region: 0 <= phi < 2Pi |
---|
| 847 | phi_min = flip_phi(self.phi_min) |
---|
| 848 | phi_max = flip_phi(self.phi_max) |
---|
[bb0b12c] | 849 | |
---|
[095ab1b] | 850 | q_data_max = numpy.max(q_data) |
---|
| 851 | |
---|
[f60a8c2] | 852 | for n in range(len(data)): |
---|
[a7a5886] | 853 | frac = 0 |
---|
| 854 | |
---|
| 855 | # q-value at the pixel (j,i) |
---|
| 856 | q_value = q_data[n] |
---|
| 857 | data_n = data[n] |
---|
| 858 | |
---|
| 859 | # Is pixel within range? |
---|
| 860 | is_in = False |
---|
| 861 | |
---|
| 862 | # phi-value of the pixel (j,i) |
---|
[f60a8c2] | 863 | phi_value = math.atan2(qy_data[n], qx_data[n]) + Pi |
---|
[a7a5886] | 864 | |
---|
| 865 | ## No need to calculate the frac when all data are within range |
---|
| 866 | if self.r_min <= q_value and q_value <= self.r_max: |
---|
[f60a8c2] | 867 | frac = 1 |
---|
[a7a5886] | 868 | if frac == 0: |
---|
| 869 | continue |
---|
| 870 | #In case of two ROIs (symmetric major and minor regions)(for 'q2') |
---|
[f60a8c2] | 871 | if run.lower() == 'q2': |
---|
| 872 | ## For minor sector wing |
---|
[a7a5886] | 873 | # Calculate the minor wing phis |
---|
| 874 | phi_min_minor = flip_phi(phi_min - Pi) |
---|
| 875 | phi_max_minor = flip_phi(phi_max - Pi) |
---|
| 876 | # Check if phis of the minor ring is within 0 to 2pi |
---|
| 877 | if phi_min_minor > phi_max_minor: |
---|
| 878 | is_in = (phi_value > phi_min_minor or \ |
---|
| 879 | phi_value < phi_max_minor) |
---|
| 880 | else: |
---|
| 881 | is_in = (phi_value > phi_min_minor and \ |
---|
| 882 | phi_value < phi_max_minor) |
---|
[3c67340] | 883 | |
---|
[f60a8c2] | 884 | #For all cases(i.e.,for 'q', 'q2', and 'phi') |
---|
| 885 | #Find pixels within ROI |
---|
| 886 | if phi_min > phi_max: |
---|
[a7a5886] | 887 | is_in = is_in or (phi_value > phi_min or \ |
---|
[f60a8c2] | 888 | phi_value < phi_max) |
---|
[a7a5886] | 889 | else: |
---|
| 890 | is_in = is_in or (phi_value >= phi_min and \ |
---|
| 891 | phi_value < phi_max) |
---|
| 892 | |
---|
| 893 | if not is_in: |
---|
[f60a8c2] | 894 | frac = 0 |
---|
[a7a5886] | 895 | if frac == 0: |
---|
| 896 | continue |
---|
| 897 | # Check which type of averaging we need |
---|
[f60a8c2] | 898 | if run.lower() == 'phi': |
---|
[a7a5886] | 899 | temp_x = (self.nbins) * (phi_value - self.phi_min) |
---|
| 900 | temp_y = (self.phi_max - self.phi_min) |
---|
| 901 | i_bin = int(math.floor(temp_x / temp_y)) |
---|
| 902 | else: |
---|
| 903 | temp_x = (self.nbins) * (q_value - self.r_min) |
---|
[ec3959ab] | 904 | temp_y = (self.r_max - self.r_min) |
---|
[a7a5886] | 905 | i_bin = int(math.floor(temp_x / temp_y)) |
---|
[bb0b12c] | 906 | |
---|
[f60a8c2] | 907 | # Take care of the edge case at phi = 2pi. |
---|
| 908 | if i_bin == self.nbins: |
---|
| 909 | i_bin = self.nbins - 1 |
---|
[095ab1b] | 910 | |
---|
[f60a8c2] | 911 | ## Get the total y |
---|
[a7a5886] | 912 | y[i_bin] += frac * data_n |
---|
[729bcf6] | 913 | x[i_bin] += frac * q_value |
---|
[342a506] | 914 | if err_data[n] == None or err_data[n] == 0.0: |
---|
[a7a5886] | 915 | if data_n < 0: |
---|
| 916 | data_n = -data_n |
---|
| 917 | y_err[i_bin] += frac * frac * data_n |
---|
| 918 | else: |
---|
| 919 | y_err[i_bin] += frac * frac * err_data[n] * err_data[n] |
---|
[342a506] | 920 | |
---|
| 921 | if dq_data != None: |
---|
[f60a8c2] | 922 | # To be consistent with dq calculation in 1d reduction, |
---|
| 923 | # we need just the averages (not quadratures) because |
---|
| 924 | # it should not depend on the number of the q points |
---|
[729bcf6] | 925 | # in the qr bins. |
---|
| 926 | x_err[i_bin] += frac * dq_data[n] |
---|
[342a506] | 927 | else: |
---|
| 928 | x_err = None |
---|
[a7a5886] | 929 | y_counts[i_bin] += frac |
---|
| 930 | |
---|
[095ab1b] | 931 | # Organize the results |
---|
[fb198a9] | 932 | for i in range(self.nbins): |
---|
| 933 | y[i] = y[i] / y_counts[i] |
---|
| 934 | y_err[i] = math.sqrt(y_err[i]) / y_counts[i] |
---|
[729bcf6] | 935 | |
---|
[095ab1b] | 936 | # The type of averaging: phi,q2, or q |
---|
| 937 | # Calculate x[i]should be at the center of the bin |
---|
[f60a8c2] | 938 | if run.lower() == 'phi': |
---|
[12c5b87] | 939 | x[i] = (self.phi_max - self.phi_min) / self.nbins * \ |
---|
| 940 | (1.0 * i + 0.5) + self.phi_min |
---|
[095ab1b] | 941 | else: |
---|
[f60a8c2] | 942 | # We take the center of ring area, not radius. |
---|
[342a506] | 943 | # This is more accurate than taking the radial center of ring. |
---|
[729bcf6] | 944 | #delta_r = (self.r_max - self.r_min) / self.nbins |
---|
| 945 | #r_inner = self.r_min + delta_r * i |
---|
| 946 | #r_outer = r_inner + delta_r |
---|
| 947 | #x[i] = math.sqrt((r_inner * r_inner + r_outer * r_outer) / 2) |
---|
| 948 | x[i] = x[i] / y_counts[i] |
---|
[f60a8c2] | 949 | y_err[y_err == 0] = numpy.average(y_err) |
---|
[a7a5886] | 950 | idx = (numpy.isfinite(y) & numpy.isfinite(y_err)) |
---|
[342a506] | 951 | if x_err != None: |
---|
[729bcf6] | 952 | d_x = x_err[idx] / y_counts[idx] |
---|
[342a506] | 953 | else: |
---|
| 954 | d_x = None |
---|
[a7a5886] | 955 | if not idx.any(): |
---|
[f60a8c2] | 956 | msg = "Average Error: No points inside sector of ROI to average..." |
---|
[a7a5886] | 957 | raise ValueError, msg |
---|
| 958 | #elif len(y[idx])!= self.nbins: |
---|
| 959 | # print "resulted",self.nbins- len(y[idx]), |
---|
| 960 | #"empty bin(s) due to tight binning..." |
---|
[342a506] | 961 | return Data1D(x=x[idx], y=y[idx], dy=y_err[idx], dx=d_x) |
---|
[fb198a9] | 962 | |
---|
[f60a8c2] | 963 | |
---|
[2e83ff3] | 964 | class SectorPhi(_Sector): |
---|
| 965 | """ |
---|
[0997158f] | 966 | Sector average as a function of phi. |
---|
| 967 | I(phi) is return and the data is averaged over Q. |
---|
| 968 | |
---|
| 969 | A sector is defined by r_min, r_max, phi_min, phi_max. |
---|
| 970 | The number of bin in phi also has to be defined. |
---|
[2e83ff3] | 971 | """ |
---|
| 972 | def __call__(self, data2D): |
---|
| 973 | """ |
---|
[0997158f] | 974 | Perform sector average and return I(phi). |
---|
| 975 | |
---|
| 976 | :param data2D: Data2D object |
---|
| 977 | :return: Data1D object |
---|
[2e83ff3] | 978 | """ |
---|
| 979 | return self._agv(data2D, 'phi') |
---|
[fb198a9] | 980 | |
---|
[f60a8c2] | 981 | |
---|
[fb198a9] | 982 | class SectorQ(_Sector): |
---|
| 983 | """ |
---|
[0997158f] | 984 | Sector average as a function of Q for both symatric wings. |
---|
| 985 | I(Q) is return and the data is averaged over phi. |
---|
| 986 | |
---|
| 987 | A sector is defined by r_min, r_max, phi_min, phi_max. |
---|
[f60a8c2] | 988 | r_min, r_max, phi_min, phi_max >0. |
---|
[0997158f] | 989 | The number of bin in Q also has to be defined. |
---|
[fb198a9] | 990 | """ |
---|
| 991 | def __call__(self, data2D): |
---|
| 992 | """ |
---|
[0997158f] | 993 | Perform sector average and return I(Q). |
---|
| 994 | |
---|
| 995 | :param data2D: Data2D object |
---|
| 996 | |
---|
| 997 | :return: Data1D object |
---|
[fb198a9] | 998 | """ |
---|
| 999 | return self._agv(data2D, 'q2') |
---|
[c6f95bb] | 1000 | |
---|
[f60a8c2] | 1001 | |
---|
[f265927] | 1002 | class Ringcut(object): |
---|
| 1003 | """ |
---|
[0997158f] | 1004 | Defines a ring on a 2D data set. |
---|
| 1005 | The ring is defined by r_min, r_max, and |
---|
| 1006 | the position of the center of the ring. |
---|
| 1007 | |
---|
| 1008 | The data returned is the region inside the ring |
---|
| 1009 | |
---|
[f60a8c2] | 1010 | Phi_min and phi_max should be defined between 0 and 2*pi |
---|
[0997158f] | 1011 | in anti-clockwise starting from the x- axis on the left-hand side |
---|
[f265927] | 1012 | """ |
---|
[f60a8c2] | 1013 | def __init__(self, r_min=0, r_max=0, center_x=0, center_y=0): |
---|
[f265927] | 1014 | # Minimum radius |
---|
| 1015 | self.r_min = r_min |
---|
| 1016 | # Maximum radius |
---|
| 1017 | self.r_max = r_max |
---|
| 1018 | # Center of the ring in x |
---|
| 1019 | self.center_x = center_x |
---|
| 1020 | # Center of the ring in y |
---|
| 1021 | self.center_y = center_y |
---|
| 1022 | |
---|
| 1023 | def __call__(self, data2D): |
---|
| 1024 | """ |
---|
[0997158f] | 1025 | Apply the ring to the data set. |
---|
| 1026 | Returns the angular distribution for a given q range |
---|
| 1027 | |
---|
| 1028 | :param data2D: Data2D object |
---|
| 1029 | |
---|
| 1030 | :return: index array in the range |
---|
[f265927] | 1031 | """ |
---|
| 1032 | if data2D.__class__.__name__ not in ["Data2D", "plottable_2D"]: |
---|
| 1033 | raise RuntimeError, "Ring cut only take plottable_2D objects" |
---|
| 1034 | |
---|
| 1035 | # Get data |
---|
[f60a8c2] | 1036 | qx_data = data2D.qx_data |
---|
[f265927] | 1037 | qy_data = data2D.qy_data |
---|
| 1038 | mask = data2D.mask |
---|
[a7a5886] | 1039 | q_data = numpy.sqrt(qx_data * qx_data + qy_data * qy_data) |
---|
[f265927] | 1040 | |
---|
| 1041 | # check whether or not the data point is inside ROI |
---|
| 1042 | out = (self.r_min <= q_data) & (self.r_max >= q_data) |
---|
| 1043 | |
---|
| 1044 | return (out) |
---|
| 1045 | |
---|
| 1046 | |
---|
[c6f95bb] | 1047 | class Boxcut(object): |
---|
| 1048 | """ |
---|
[0997158f] | 1049 | Find a rectangular 2D region of interest. |
---|
[c6f95bb] | 1050 | """ |
---|
| 1051 | def __init__(self, x_min=0.0, x_max=0.0, y_min=0.0, y_max=0.0): |
---|
| 1052 | # Minimum Qx value [A-1] |
---|
| 1053 | self.x_min = x_min |
---|
| 1054 | # Maximum Qx value [A-1] |
---|
| 1055 | self.x_max = x_max |
---|
| 1056 | # Minimum Qy value [A-1] |
---|
| 1057 | self.y_min = y_min |
---|
| 1058 | # Maximum Qy value [A-1] |
---|
| 1059 | self.y_max = y_max |
---|
| 1060 | |
---|
| 1061 | def __call__(self, data2D): |
---|
| 1062 | """ |
---|
[0997158f] | 1063 | Find a rectangular 2D region of interest. |
---|
| 1064 | |
---|
| 1065 | :param data2D: Data2D object |
---|
[f60a8c2] | 1066 | :return: mask, 1d array (len = len(data)) |
---|
[0997158f] | 1067 | with Trues where the data points are inside ROI, otherwise False |
---|
[c6f95bb] | 1068 | """ |
---|
| 1069 | mask = self._find(data2D) |
---|
| 1070 | |
---|
| 1071 | return mask |
---|
| 1072 | |
---|
| 1073 | def _find(self, data2D): |
---|
| 1074 | """ |
---|
[f60a8c2] | 1075 | Find a rectangular 2D region of interest. |
---|
[0997158f] | 1076 | |
---|
| 1077 | :param data2D: Data2D object |
---|
| 1078 | |
---|
[f60a8c2] | 1079 | :return: out, 1d array (length = len(data)) |
---|
[0997158f] | 1080 | with Trues where the data points are inside ROI, otherwise Falses |
---|
[c6f95bb] | 1081 | """ |
---|
| 1082 | if data2D.__class__.__name__ not in ["Data2D", "plottable_2D"]: |
---|
| 1083 | raise RuntimeError, "Boxcut take only plottable_2D objects" |
---|
[f60a8c2] | 1084 | # Get qx_ and qy_data |
---|
[c6f95bb] | 1085 | qx_data = data2D.qx_data |
---|
| 1086 | qy_data = data2D.qy_data |
---|
[f265927] | 1087 | mask = data2D.mask |
---|
[c6f95bb] | 1088 | |
---|
| 1089 | # check whether or not the data point is inside ROI |
---|
[f265927] | 1090 | outx = (self.x_min <= qx_data) & (self.x_max > qx_data) |
---|
| 1091 | outy = (self.y_min <= qy_data) & (self.y_max > qy_data) |
---|
[c6f95bb] | 1092 | |
---|
| 1093 | return (outx & outy) |
---|
| 1094 | |
---|
[f60a8c2] | 1095 | |
---|
[c6f95bb] | 1096 | class Sectorcut(object): |
---|
| 1097 | """ |
---|
[0997158f] | 1098 | Defines a sector (major + minor) region on a 2D data set. |
---|
| 1099 | The sector is defined by phi_min, phi_max, |
---|
[f60a8c2] | 1100 | where phi_min and phi_max are defined by the right |
---|
| 1101 | and left lines wrt central line. |
---|
[0997158f] | 1102 | |
---|
[f60a8c2] | 1103 | Phi_min and phi_max are given in units of radian |
---|
[0997158f] | 1104 | and (phi_max-phi_min) should not be larger than pi |
---|
[c6f95bb] | 1105 | """ |
---|
[a7a5886] | 1106 | def __init__(self, phi_min=0, phi_max=math.pi): |
---|
[c6f95bb] | 1107 | self.phi_min = phi_min |
---|
| 1108 | self.phi_max = phi_max |
---|
| 1109 | |
---|
| 1110 | def __call__(self, data2D): |
---|
| 1111 | """ |
---|
[0997158f] | 1112 | Find a rectangular 2D region of interest. |
---|
| 1113 | |
---|
| 1114 | :param data2D: Data2D object |
---|
| 1115 | |
---|
[f60a8c2] | 1116 | :return: mask, 1d array (len = len(data)) |
---|
[0997158f] | 1117 | |
---|
| 1118 | with Trues where the data points are inside ROI, otherwise False |
---|
[c6f95bb] | 1119 | """ |
---|
| 1120 | mask = self._find(data2D) |
---|
| 1121 | |
---|
| 1122 | return mask |
---|
| 1123 | |
---|
| 1124 | def _find(self, data2D): |
---|
| 1125 | """ |
---|
[f60a8c2] | 1126 | Find a rectangular 2D region of interest. |
---|
[0997158f] | 1127 | |
---|
| 1128 | :param data2D: Data2D object |
---|
| 1129 | |
---|
[f60a8c2] | 1130 | :return: out, 1d array (length = len(data)) |
---|
[0997158f] | 1131 | |
---|
| 1132 | with Trues where the data points are inside ROI, otherwise Falses |
---|
[c6f95bb] | 1133 | """ |
---|
| 1134 | if data2D.__class__.__name__ not in ["Data2D", "plottable_2D"]: |
---|
[f60a8c2] | 1135 | raise RuntimeError, "Sectorcut take only plottable_2D objects" |
---|
[c6f95bb] | 1136 | Pi = math.pi |
---|
| 1137 | # Get data |
---|
| 1138 | qx_data = data2D.qx_data |
---|
[f60a8c2] | 1139 | qy_data = data2D.qy_data |
---|
[c6f95bb] | 1140 | phi_data = numpy.zeros(len(qx_data)) |
---|
| 1141 | |
---|
| 1142 | # get phi from data |
---|
[f265927] | 1143 | phi_data = numpy.arctan2(qy_data, qx_data) |
---|
| 1144 | |
---|
| 1145 | # Get the min and max into the region: -pi <= phi < Pi |
---|
[a7a5886] | 1146 | phi_min_major = flip_phi(self.phi_min + Pi) - Pi |
---|
[f60a8c2] | 1147 | phi_max_major = flip_phi(self.phi_max + Pi) - Pi |
---|
[c6f95bb] | 1148 | # check for major sector |
---|
[f265927] | 1149 | if phi_min_major > phi_max_major: |
---|
| 1150 | out_major = (phi_min_major <= phi_data) + (phi_max_major > phi_data) |
---|
[c6f95bb] | 1151 | else: |
---|
[f265927] | 1152 | out_major = (phi_min_major <= phi_data) & (phi_max_major > phi_data) |
---|
| 1153 | |
---|
[c6f95bb] | 1154 | # minor sector |
---|
| 1155 | # Get the min and max into the region: -pi <= phi < Pi |
---|
[a7a5886] | 1156 | phi_min_minor = flip_phi(self.phi_min) - Pi |
---|
| 1157 | phi_max_minor = flip_phi(self.phi_max) - Pi |
---|
[c6f95bb] | 1158 | |
---|
| 1159 | # check for minor sector |
---|
| 1160 | if phi_min_minor > phi_max_minor: |
---|
[a7a5886] | 1161 | out_minor = (phi_min_minor <= phi_data) + \ |
---|
[f60a8c2] | 1162 | (phi_max_minor >= phi_data) |
---|
[c6f95bb] | 1163 | else: |
---|
[a7a5886] | 1164 | out_minor = (phi_min_minor <= phi_data) & \ |
---|
[f60a8c2] | 1165 | (phi_max_minor >= phi_data) |
---|
[c6f95bb] | 1166 | out = out_major + out_minor |
---|
[f265927] | 1167 | |
---|
[c6f95bb] | 1168 | return out |
---|