- Timestamp:
- Jan 15, 2010 2:31:50 PM (15 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:
- aafa962
- Parents:
- 59a41066
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Invariant/invariant.py
r59a41066 r82703a1 9 9 from DataLoader.data_info import Data1D as LoaderData1D 10 10 from DataLoader.qsmearing import smear_selection 11 12 11 13 12 # The minimum q-value to be used when extrapolating … … 25 24 function given some x, y 26 25 """ 27 def set_value(self, a, b):26 def transform_values(self, a, b): 28 27 """ 29 28 set private member 30 29 """ 30 return NotImplemented 31 31 32 def transform_value(self, value): 32 33 """ 33 Can use one of the function transform_x2 or transform_logx34 """35 def transform_x2(self, value):36 """37 34 @param value: float 38 """ 39 return value **2 40 41 def transform_to_logx(self, value): 42 """ 43 @param value: float 44 """ 45 return math.log(value) 46 35 return f(value) 36 """ 37 return NotImplemented 38 47 39 def inverse_transform_value(self, value): 48 40 """ … … 51 43 @param value : float 52 44 """ 45 return NotImplemented 46 53 47 def get_extrapolated_data(self, data, q_start=Q_MINIMUM, q_end=Q_MAXIMUM): 54 48 """ 55 49 @return extrapolate data create from data 56 50 """ 51 return NotImplemented 52 57 53 def transform_data(self, x): 58 54 """ … … 61 57 return x, y transform given a specific function 62 58 """ 59 return NotImplemented 60 63 61 def inverse_transform_data(self, x, y): 64 62 """ 65 63 reverse transform x, y given a specific function 66 64 """ 65 return NotImplemented 66 67 67 def transform_error(self, y , dy=None): 68 68 if dy is None: … … 96 96 result_data.dxl = dxl 97 97 result_data.dxw = dxw 98 99 100 98 return result_data 101 99 … … 111 109 112 110 def transform_value(self, value): 113 return self.transform_x2(value) 114 115 def set_value(self, a, b): 116 # b is the radius value of the guinier function 117 if b >=0 : 118 raise ValueError("Guinier fit was not converged") 119 else: 120 b = math.sqrt(-3 * b) 111 """ 112 Square input value 113 @param value: of type float 114 """ 115 return value * value 116 117 def transform_values(self, a, b): 118 """ 119 assign new value to the scale and the radius 120 """ 121 b = math.sqrt(-3 * b) 121 122 a = math.exp(a) 122 123 123 self.scale = a 124 124 self.radius = b 125 return a, b 125 126 126 127 def transform_data(self, x): 127 128 """ 128 given a scale and a radius transform x, y using a guinier 129 function 129 return F(x)= scale* e-((radius*x)**2/3) 130 130 """ 131 131 return self._guinier(x) … … 133 133 def inverse_transform_data(self, x, y): 134 134 """ 135 given a scale and a radius transform x, y using a inverse guinier 136 function 135 this function finds x, y given equation1: y = ax + b 136 and equation2: y1 = scale* e-((radius*x1)**2/3). 137 where equation1, equation2 are equivalent 138 imply: x = x1*x1 139 y = ln(y1) 140 b = math.exp(scale) 141 a = -radius**2/3 142 @return x, y 137 143 """ 138 144 result_x = numpy.array([i * i for i in x]) 139 145 result_y = numpy.array([math.log(j) for j in y]) 140 141 146 return result_x, result_y 142 147 … … 155 160 if self.radius <= 0: 156 161 raise ValueError("Rg expected positive value, but got %s"%self.radius) 157 158 162 value = numpy.array([math.exp(-((self.radius * i)**2/3)) for i in x ]) 159 160 163 return self.scale * value 161 164 … … 170 173 self.power = power 171 174 172 def set_value(self, a, b): 175 def transform_values(self, a, b): 176 """ 177 Assign new value to the scale and the power 178 """ 173 179 b = -1 * b 174 if b <= 0:175 raise ValueError("Power_law fit expected posive power, but got %s"%power)176 180 a = math.exp(a) 181 self.power = b 177 182 self.scale = a 178 self.power = b 179 180 def transform_data(self, x, a=None, b=None): 183 return a, b 184 185 def transform_value(self, value): 186 """ 187 compute log(value) 188 """ 189 return math.log(value) 190 191 def transform_data(self, x): 181 192 """ 182 193 given a scale and a radius transform x, y using a power_law 183 194 function 184 195 """ 185 if a is not None:186 self.scale = a187 if b is not None:188 self.power = b189 190 196 return self._power_law(x) 191 197 … … 195 201 function 196 202 """ 197 result_x = numpy.array([ i * ifor i in x])203 result_x = numpy.array([math.log(i) for i in x]) 198 204 result_y = numpy.array([math.log(j) for j in y]) 199 205 … … 212 218 """ 213 219 if self.power <= 0: 214 raise ValueError("Power_law function expected positive power, but got %s"% power)220 raise ValueError("Power_law function expected positive power, but got %s"%self.power) 215 221 if self.scale <= 0: 216 222 raise ValueError("scale expected positive value, but got %s"%self.scale) … … 297 303 298 304 a, b = numpy.linalg.lstsq(A, fx[self.idx])[0] 305 299 306 return a, b 300 307 … … 352 359 #Process only data that inherited from DataLoader.Data_info.Data1D 353 360 raise ValueError,"Data must be of type DataLoader.Data1D" 354 355 new_data = (self._scale * data) - self._background 356 357 # Copy data that is not copied by the operations 358 #TODO: fix this in DataLoader 359 #new_data.dxl = data.dxl 360 #new_data.dxw = data.dxw 361 362 return new_data 363 361 new_data = (self._scale * data) - self._background 362 new_data.dxl = data.dxl 363 new_data.dxw = data.dxw 364 return new_data 365 364 366 def _fit(self, function, qmin=Q_MINIMUM, qmax=Q_MAXIMUM, power=None): 365 367 """ … … 386 388 fit_data.dxl = self._data.dxl 387 389 fit_data.dxw = self._data.dxw 388 functor = Extrapolator(data=fit_data) 389 functor.set_fit_range(qmin=qmin, qmax=qmax) 390 b, a = functor.fit(power=power) 391 return b, a 390 extrapolator = Extrapolator(data=fit_data) 391 extrapolator.set_fit_range(qmin=qmin, qmax=qmax) 392 b, a = extrapolator.fit(power=power) 393 394 return function.transform_values(a=a, b=b) 392 395 393 396 def _get_qstar(self, data): … … 502 505 """ 503 506 if data is None: 504 data = self.data 505 507 data = self._data 506 508 if data.is_slit_smeared(): 507 509 return self._get_qstar_smear_uncertainty(data) … … 509 511 return self._get_qstar_unsmear_uncertainty(data) 510 512 511 def _get_qstar_unsmear_uncertainty(self, data =None):513 def _get_qstar_unsmear_uncertainty(self, data): 512 514 """ 513 515 Compute invariant uncertainty with with pinhole data. … … 554 556 return math.sqrt(sum) 555 557 556 def _get_qstar_smear_uncertainty(self ):558 def _get_qstar_smear_uncertainty(self, data): 557 559 """ 558 560 Compute invariant uncertainty with slit smeared data. … … 571 573 note: if data doesn't contain dy assume dy= math.sqrt(data.y) 572 574 """ 573 #if data is None:574 # data = self._data575 576 575 if not data.is_slit_smeared(): 577 576 msg = "_get_qstar_smear_uncertainty need slit smear data " … … 659 658 qmin = self._data.x[0] 660 659 qmax = self._data.x[self._low_extrapolation_npts - 1] 661 662 660 # Extrapolate the low-Q data 663 661 #TODO: this fit fails. Fix it. 664 b, a= self._fit(function=self._low_extrapolation_function,662 a, b = self._fit(function=self._low_extrapolation_function, 665 663 qmin=qmin, 666 664 qmax=qmax, 667 665 power=self._low_extrapolation_power) 668 669 666 #q_start point 670 667 q_start = Q_MINIMUM 671 668 if Q_MINIMUM >= qmin: 672 669 q_start = qmin/10 673 674 self._low_extrapolation_function.set_value(a= a, b=b) 670 675 671 data_min = self._low_extrapolation_function.get_extrapolated_data(data=self._data, 676 672 npts=INTEGRATION_NSTEPS, 677 673 q_start=q_start, q_end=qmin, smear_indice=0) 678 679 674 return data_min 680 675 … … 693 688 @return: a new data of type Data1D 694 689 """ 695 # Data boundaries for fi iting690 # Data boundaries for fitting 696 691 x_len = len(self._data.x) - 1 697 q _start= self._data.x[x_len - (self._high_extrapolation_npts - 1)]692 qmin = self._data.x[x_len - (self._high_extrapolation_npts - 1)] 698 693 qmax = self._data.x[x_len] 699 smear_indice = x_len 694 q_end = Q_MAXIMUM 695 smear_indice = 0 696 if self._data.dxl is not None: 697 smear_indice = len(self._data.dxl) - 1 700 698 701 699 # fit the data with a model to get the appropriate parameters 702 b, a= self._fit(function=self._high_extrapolation_function,703 qmin=q _start,700 a, b = self._fit(function=self._high_extrapolation_function, 701 qmin=qmin, 704 702 qmax=qmax, 705 703 power=self._high_extrapolation_power) 706 707 704 #create new Data1D to compute the invariant 708 self._high_extrapolation_function.set_value(a=a, b=b)709 705 data_max = self._high_extrapolation_function.get_extrapolated_data(data=self._data, 710 706 npts=INTEGRATION_NSTEPS, 711 q_start=q _start, q_end=qmax,707 q_start=qmax, q_end=q_end, 712 708 smear_indice=smear_indice) 713 709 return data_max
Note: See TracChangeset
for help on using the changeset viewer.