Changeset f8d0ee7 in sasview
- Timestamp:
- Sep 17, 2008 4:34:32 PM (16 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 1f8accb
- Parents:
- 76e2369
- Location:
- DataLoader
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
DataLoader/manipulations.py
r76e2369 rf8d0ee7 1 1 """ 2 2 Data manipulations for 2D data sets. 3 Using the meta data information, various types of averaging 4 are performed in Q-space 3 5 """ 4 6 … … 36 38 pass 37 39 38 class Boxsum: 39 pass 40 41 class Boxsum(object): 42 """ 43 Perform the sum of counts in a 2D region of interest. 44 """ 45 def __init__(self, x_min=0.0, x_max=0.0, y_min=0.0, y_max=0.0): 46 # Minimum Qx value [A-1] 47 self.x_min = x_min 48 # Maximum Qx value [A-1] 49 self.x_max = x_max 50 # Minimum Qy value [A-1] 51 self.y_min = y_min 52 # Maximum Qy value [A-1] 53 self.y_max = y_max 54 55 def __call__(self, data2D): 56 """ 57 Perform the sum in the region of interest 58 59 @param data2D: Data2D object 60 @return: number of counts, error on number of counts 61 """ 62 y, err_y, y_counts = self._sum(data2D) 63 64 # Average the sums 65 counts = 0 if y_counts==0 else y 66 error = 0 if y_counts==0 else math.sqrt(err_y) 67 68 return counts, error 69 70 def _sum(self, data2D): 71 """ 72 Perform the sum in the region of interest 73 @param data2D: Data2D object 74 @return: number of counts, error on number of counts, number of entries summed 75 """ 76 if len(data2D.detector) != 1: 77 raise RuntimeError, "Circular averaging: invalid number of detectors: %g" % len(data2D.detector) 78 79 pixel_width = data2D.detector[0].pixel_size.x 80 det_dist = data2D.detector[0].distance 81 wavelength = data2D.source.wavelength 82 center_x = data2D.detector[0].beam_center.x/pixel_width 83 center_y = data2D.detector[0].beam_center.y/pixel_width 84 85 y = 0.0 86 err_y = 0.0 87 y_counts = 0.0 88 89 for i in range(len(data2D.data)): 90 # Min and max x-value for the pixel 91 minx = pixel_width*(i - center_x) 92 maxx = pixel_width*(i+1.0 - center_x) 93 94 qxmin = get_q(minx, 0.0, det_dist, wavelength) 95 qxmax = get_q(maxx, 0.0, det_dist, wavelength) 96 97 # Get the count fraction in x for that pixel 98 frac_min = get_pixel_fraction_square(self.x_min, qxmin, qxmax) 99 frac_max = get_pixel_fraction_square(self.x_max, qxmin, qxmax) 100 frac_x = frac_max - frac_min 101 102 for j in range(len(data2D.data)): 103 # Min and max y-value for the pixel 104 miny = pixel_width*(j - center_y) 105 maxy = pixel_width*(j+1.0 - center_y) 106 107 qymin = get_q(0.0, miny, det_dist, wavelength) 108 qymax = get_q(0.0, maxy, det_dist, wavelength) 109 110 # Get the count fraction in x for that pixel 111 frac_min = get_pixel_fraction_square(self.y_min, qymin, qymax) 112 frac_max = get_pixel_fraction_square(self.y_max, qymin, qymax) 113 frac_y = frac_max - frac_min 114 115 frac = frac_x * frac_y 116 117 y += frac * data2D.data[j][i] 118 if data2D.err_data == None or data2D.err_data[j][i]==0.0: 119 err_y += frac * frac * math.fabs(data2D.data[j][i]) 120 else: 121 err_y += frac * frac * data2D.err_data[j][i] * data2D.err_data[j][i] 122 y_counts += frac 123 124 return y, err_y, y_counts 125 # Average the sums 126 counts = 0 if y_counts==0 else y/y_counts 127 error = 0 if y_counts==0 else math.sqrt(err_y)/y_counts 128 129 return counts, error 130 131 class Boxavg(Boxsum): 132 """ 133 Perform the average of counts in a 2D region of interest. 134 """ 135 def __init__(self, x_min=0.0, x_max=0.0, y_min=0.0, y_max=0.0): 136 super(Boxavg, self).__init__(x_min=x_min, x_max=x_max, y_min=y_min, y_max=y_max) 137 138 def __call__(self, data2D): 139 """ 140 Perform the sum in the region of interest 141 142 @param data2D: Data2D object 143 @return: average counts, error on average counts 144 """ 145 y, err_y, y_counts = self._sum(data2D) 146 147 # Average the sums 148 counts = 0 if y_counts==0 else y/y_counts 149 error = 0 if y_counts==0 else math.sqrt(err_y)/y_counts 150 151 return counts, error 152 153 def get_pixel_fraction_square(x, xmin, xmax): 154 """ 155 Return the fraction of the length 156 from xmin to x. 157 158 A B 159 +-----------+---------+ 160 xmin x xmax 161 162 @param x: x-value 163 @param xmin: minimum x for the length considered 164 @param xmax: minimum x for the length considered 165 @return: (x-xmin)/(xmax-xmin) when xmin < x < xmax 166 167 """ 168 if x<=xmin: 169 return 0.0 170 if x>xmin and x<xmax: 171 return (x-xmin)/(xmax-xmin) 172 else: 173 return 1.0 174 40 175 41 176 class CircularAverage(object): … … 367 502 368 503 369 #d = Loader().load('test/MAR07232_rest.ASC') 370 d = Loader().load('test/MP_New.sans') 371 372 373 r = CircularAverage(r_min=.0, r_max=.075,bin_width=0.0003) 504 d = Loader().load('test/MAR07232_rest.ASC') 505 #d = Loader().load('test/MP_New.sans') 506 507 508 #r = Boxsum(x_min=.2, x_max=.4, y_min=0.2, y_max=0.4) 509 r = Boxsum(x_min=.01, x_max=.015, y_min=0.01, y_max=0.015) 374 510 o = r(d) 375 for i in range(len(o.x)): 376 print o.x[i], o.y[i], o.dy[i] 511 print o 512 513 r = Boxavg(x_min=.01, x_max=.015, y_min=0.01, y_max=0.015) 514 o = r(d) 515 print o 377 516 378 517 -
DataLoader/release_notes.txt
rbee885e rf8d0ee7 11 11 - HFIR 1D 4-col reader available (*.d1d) 12 12 - Allows plugins through python source or zip files 13 - Added ring averaging and circular averaging 13 14 14 15 Version 0.1 … … 23 24 24 25 2.2- Installing: 25 - Get the code from svn://danse.us/sans/ releases/DataLoader-0.126 - Get the code from svn://danse.us/sans/trunk/DataLoader 26 27 - The following modules are required: 27 28 * numpy 28 * data_util from svn://danse.us/common/ releases/data_util-0.129 * data_util from svn://danse.us/common/util 29 30 * The CanSAS reader needs PyXML installed 30 31 -
DataLoader/test/utest_averaging.py
r76e2369 rf8d0ee7 44 44 self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) 45 45 46 def test_box(self): 47 """ 48 Test circular averaging 49 The test data was not generated by IGOR. 50 """ 51 from DataLoader.manipulations import Boxsum, Boxavg 52 53 r = Boxsum(x_min=.01, x_max=.015, y_min=0.01, y_max=0.015) 54 s, ds = r(self.data) 55 self.assertAlmostEqual(s, 151.81809601016641, 4) 56 self.assertAlmostEqual(ds, 16.245399156009537, 4) 57 58 r = Boxavg(x_min=.01, x_max=.015, y_min=0.01, y_max=0.015) 59 s, ds = r(self.data) 60 self.assertAlmostEqual(s, 0.11195555855955155, 4) 61 self.assertAlmostEqual(ds, 0.011979881083557541, 4) 62 46 63 47 64 if __name__ == '__main__':
Note: See TracChangeset
for help on using the changeset viewer.