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