Changeset be6e99a in sasview for calculator
- Timestamp:
- Apr 8, 2011 1:19:10 PM (14 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:
- ef73618
- Parents:
- cdb3a9a
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
calculator/resolution_calculator.py
r19637b1 rbe6e99a 11 11 from math import pi 12 12 from math import sqrt 13 from math import cos 14 from math import sin 15 from math import atan 16 from math import atan2 17 from math import pow 18 from math import asin 19 from math import tan 20 13 import math 14 import scipy 21 15 import numpy 22 16 … … 43 37 self.image = [] 44 38 # resolutions 39 # lamda in r-direction 40 self.sigma_lamda = 0 41 # x-dir (no lamda) 45 42 self.sigma_1 = 0 43 #y-dir (no lamda) 46 44 self.sigma_2 = 0 45 # 1D total 47 46 self.sigma_1d = 0 48 47 # q min and max … … 74 73 75 74 def compute_and_plot(self, qx_value, qy_value, qx_min, qx_max, 76 qy_min, qy_max, coord = ' polar'):75 qy_min, qy_max, coord = 'cartesian'): 77 76 """ 78 77 Compute the resolution … … 81 80 """ 82 81 # compute 2d resolution 83 _, _, sigma_1, sigma_2 = self.compute(qx_value, qy_value, coord) 82 _, _, sigma_1, sigma_2, sigma_r = \ 83 self.compute(qx_value, qy_value, coord) 84 84 # make image 85 image = self.get_image(qx_value, qy_value, sigma_1, sigma_2, 85 image = self.get_image(qx_value, qy_value, sigma_1, sigma_2, sigma_r, 86 86 qx_min, qx_max, qy_min, qy_max, coord) 87 87 # plot image 88 88 return self.plot_image(image) 89 89 90 def compute(self, qx_value, qy_value, coord = ' polar'):90 def compute(self, qx_value, qy_value, coord = 'cartesian'): 91 91 """ 92 92 Compute the Q resoltuion in || and + direction of 2D … … 94 94 : qy_value: y component of q 95 95 """ 96 coord = 'cartesian' 96 97 # make sure to update all the variables need. 97 98 self.get_all_instrument_params() … … 113 114 theta = pi/2 114 115 else: 115 theta = asin(qr_value/knot)116 theta = math.asin(qr_value/knot) 116 117 # source aperture size 117 118 rone = self.source_aperture_size … … 135 136 lp_cor = (l_ssa * l_two) / (l_one + l_two) 136 137 # the radial distance to the pixel from the center of the detector 137 radius = tan(theta)*l_two138 radius = math.tan(theta)*l_two 138 139 #Lp = l_one*l_two/(l_one+l_two) 139 140 # default polar coordinate … … 158 159 # reserve for 1d calculation 159 160 sigma_wave_1 = self.get_variance_wave(radius, l_two, lamb_spread, 160 phi, comp1, 'on')161 phi, 'radial', 'on') 161 162 # for 1d 162 163 variance_1d_1 = sigma_1/2 +sigma_wave_1 … … 165 166 166 167 # for 2d 167 sigma_1 += sigma_wave_1168 #sigma_1 += sigma_wave_1 168 169 # normalize 169 170 sigma_1 = knot*sqrt(sigma_1/12) 170 171 sigma_r = knot*sqrt(sigma_wave_1/12) 171 172 # sigma in the phi/y direction 172 173 # for source apperture 173 174 sigma_2 = self.get_variance(rone, l1_cor, phi, comp2) 175 174 176 # for sample apperture 175 177 sigma_2 += self.get_variance(rtwo, lp_cor, phi, comp2) 178 176 179 # for detector pix 177 180 sigma_2 += self.get_variance(rthree, l_two, phi, comp2) 181 178 182 # for gravity term 179 183 sigma_2 += self.get_variance_gravity(l_ssa, l_sad, lamb, lamb_spread, 180 184 phi, comp2, 'on') 185 186 181 187 # for wavelength spread 182 188 # reserve for 1d calculation 183 189 sigma_wave_2 = self.get_variance_wave(radius, l_two, lamb_spread, 184 phi, comp2, 'on')190 phi, 'phi', 'on') 185 191 # for 1d 186 192 variance_1d_2 = sigma_2/2 +sigma_wave_2 … … 189 195 190 196 # for 2d 191 sigma_2 += sigma_wave_2 197 #sigma_2 = knot*sqrt(sigma_2/12) 198 #sigma_2 += sigma_wave_2 192 199 # normalize 193 200 sigma_2 = knot*sqrt(sigma_2/12) … … 195 202 # set sigmas 196 203 self.sigma_1 = sigma_1 204 self.sigma_lamd = sigma_r 197 205 self.sigma_2 = sigma_2 198 206 199 207 self.sigma_1d = sqrt(variance_1d_1 + variance_1d_2) 200 return qr_value, phi, sigma_1, sigma_2 201 202 def get_image(self, qx_value, qy_value, sigma_1, sigma_2, 203 qx_min, qx_max, qy_min, qy_max, coord = ' polar'):208 return qr_value, phi, sigma_1, sigma_2, sigma_r 209 210 def get_image(self, qx_value, qy_value, sigma_1, sigma_2, sigma_r, 211 qx_min, qx_max, qy_min, qy_max, coord = 'cartesian'): 204 212 """ 205 213 Get the resolution in polar coordinate ready to plot … … 217 225 #qy_min = self.qy_min 218 226 #qy_max = self.qy_max 219 220 # Find polar values221 227 qr_value, phi = self._get_polar_value(qx_value, qy_value) 222 228 223 229 # Check whether the q value is within the detector range 224 230 msg = "Invalid input: Q value out of the detector range..." … … 242 248 y_val = numpy.arange(self.qy_max, self.qy_min, -dy_size) 243 249 q_1, q_2 = numpy.meshgrid(x_val, y_val) 244 250 #q_phi = numpy.arctan(q_1,q_2) 245 251 # check whether polar or cartesian 246 252 if coord == 'polar': 253 # Find polar values 254 qr_value, phi = self._get_polar_value(qx_value, qy_value) 247 255 q_1, q_2 = self._rotate_z(q_1, q_2, phi) 248 256 qc_1 = qr_value 249 257 qc_2 = 0.0 258 # Calculate the 2D Gaussian distribution image 259 image = self._gaussian2d_polar(q_1, q_2, qc_1, qc_2, 260 sigma_1, sigma_2, sigma_r) 250 261 else: 251 262 # catesian coordinate … … 255 266 qc_2 = qy_value 256 267 257 # Calculate the 2D Gaussian distribution image 258 image = self._gaussian2d(q_1, q_2, qc_1, qc_2, sigma_1, sigma_2) 268 # Calculate the 2D Gaussian distribution image 269 image = self._gaussian2d(q_1, q_2, qc_1, qc_2, 270 sigma_1, sigma_2, sigma_r) 259 271 # Add it if there are more than one inputs. 260 272 if len(self.image) > 0: … … 309 321 # define sigma component direction 310 322 if comp == 'radial': 311 phi_x = cos(phi)312 phi_y = sin(phi)323 phi_x = math.cos(phi) 324 phi_y = math.sin(phi) 313 325 elif comp == 'phi': 314 phi_x = sin(phi)315 phi_y = cos(phi)326 phi_x = math.sin(phi) 327 phi_y = math.cos(phi) 316 328 elif comp == 'x': 317 329 phi_x = 1 … … 362 374 else: 363 375 # calculate sigma^2 364 sigma = 2 * pow(radius/distance*spread, 2)376 sigma = 2 * math.pow(radius/distance*spread, 2) 365 377 if comp == 'x': 366 sigma *= ( cos(phi)*cos(phi))378 sigma *= (math.cos(phi)*math.cos(phi)) 367 379 elif comp == 'y': 368 sigma *= ( sin(phi)*sin(phi))380 sigma *= (math.sin(phi)*math.sin(phi)) 369 381 else: 370 382 sigma *= 1 … … 403 415 # A value 404 416 a_value = d_distance * (s_distance + d_distance) 405 a_value *= pow(m_over_h / 2, 2)417 a_value *= math.pow(m_over_h / 2, 2) 406 418 a_value *= gravy 407 419 # unit correction (1/cm to 1/A) for A and d_distance below … … 409 421 410 422 # calculate sigma^2 411 sigma = pow(a_value / d_distance, 2)412 sigma *= pow(wavelength, 4)413 sigma *= pow(spread, 2)423 sigma = math.pow(a_value / d_distance, 2) 424 sigma *= math.pow(wavelength, 4) 425 sigma *= math.pow(spread, 2) 414 426 sigma *= 8 415 427 416 428 # only for the polar coordinate 417 if comp == 'radial':418 sigma *= (sin(phi) *sin(phi))419 elif comp == 'phi':420 sigma *= (cos(phi) *cos(phi))429 #if comp == 'radial': 430 # sigma *= (math.sin(phi) * math.sin(phi)) 431 #elif comp == 'phi': 432 # sigma *= (math.cos(phi) * math.cos(phi)) 421 433 422 434 return sigma … … 521 533 """ 522 534 self.wave.set_mass(mass) 535 self.mass = mass 523 536 524 537 def set_sample_aperture_size(self, size): … … 603 616 """ 604 617 # rotate by theta 605 x_prime = x_value * cos(theta) + y_value *sin(theta)606 y_prime = -x_value * sin(theta) + y_value *cos(theta)618 x_prime = x_value * math.cos(theta) + y_value * math.sin(theta) 619 y_prime = -x_value * math.sin(theta) + y_value * math.cos(theta) 607 620 608 621 return x_prime, y_prime 609 622 610 def _gaussian2d(self, x_val, y_val, x0_val, y0_val, sigma_x, sigma_y): 623 def _gaussian2d(self, x_val, y_val, x0_val, y0_val, 624 sigma_x, sigma_y, sigma_r): 611 625 """ 612 626 Calculate 2D Gaussian distribution … … 620 634 : return: gaussian (value) 621 635 """ 636 # phi values at each points (not at the center) 637 x_value = x_val - x0_val 638 y_value = y_val - y0_val 639 phi_i = numpy.arctan2(y_val, x_val) 640 641 sin_phi = numpy.sin(phi_i) 642 cos_phi = numpy.cos(phi_i) 643 644 x_p = x_value * cos_phi + y_value * sin_phi 645 y_p = -x_value * sin_phi + y_value * cos_phi 646 647 new_x = x_p * cos_phi / (sigma_r / sigma_x + 1) - y_p * sin_phi 648 new_x /= sigma_x 649 new_y = x_p * sin_phi / (sigma_r / sigma_y + 1) + y_p * cos_phi 650 new_y /= sigma_y 651 652 nu_value = -0.5 *(new_x * new_x + new_y * new_y) 653 654 gaussian = numpy.exp(nu_value) 655 # normalizing factor correction 656 gaussian /= gaussian.sum() 657 658 return gaussian 659 660 def _gaussian2d_polar(self, x_val, y_val, x0_val, y0_val, 661 sigma_x, sigma_y, sigma_r): 662 """ 663 Calculate 2D Gaussian distribution for polar coodinate 664 : x_val: x value 665 : y_val: y value 666 : x0_val: mean value in x-axis 667 : y0_val: mean value in y-axis 668 : sigma_x: variance in r-direction 669 : sigma_y: variance in phi-direction 670 : sigma_r: wavelength variance in r-direction 671 672 : return: gaussian (value) 673 """ 674 sigma_x = sqrt(sigma_x * sigma_x + sigma_r * sigma_r) 622 675 # call gaussian1d 623 676 gaussian = self._gaussian1d(x_val, x0_val, sigma_x) … … 628 681 gaussian *= sqrt(2 * pi) 629 682 return gaussian 630 683 631 684 def _gaussian1d(self, value, mean, sigma): 632 685 """ … … 660 713 : return phi: the azimuthal angle of q on x-y plane 661 714 """ 715 phi = math.atan2(qy_value, qx_value) 716 return phi 662 717 # default 663 718 phi = 0 … … 674 729 else: 675 730 # the angle 676 phi = atan2(qy_value, qx_value)731 phi = math.atan2(qy_value, qx_value) 677 732 678 733 return phi … … 811 866 plane_dist = dx_size 812 867 # full scattering angle on the x-axis 813 theta = atan(plane_dist / det_dist)814 qx_value = (2.0 * pi / wavelength) * sin(theta)868 theta = math.atan(plane_dist / det_dist) 869 qx_value = (2.0 * pi / wavelength) * math.sin(theta) 815 870 return qx_value 816 871
Note: See TracChangeset
for help on using the changeset viewer.