[75047cf] | 1 | """ |
---|
| 2 | This module implements invariant and its related computations. |
---|
| 3 | @author: Gervaise B. Alina/UTK |
---|
| 4 | """ |
---|
| 5 | |
---|
| 6 | import math |
---|
[ef9ed58] | 7 | import numpy |
---|
[75047cf] | 8 | |
---|
| 9 | from DataLoader.data_info import Data1D as LoaderData1D |
---|
| 10 | from DataLoader.qsmearing import smear_selection |
---|
| 11 | |
---|
| 12 | |
---|
[b6666d4] | 13 | # The minimum q-value to be used when extrapolating |
---|
| 14 | Q_MINIMUM = 1e-5 |
---|
| 15 | |
---|
| 16 | # The maximum q-value to be used when extrapolating |
---|
| 17 | Q_MAXIMUM = 10 |
---|
| 18 | |
---|
| 19 | # Number of steps in the extrapolation |
---|
| 20 | INTEGRATION_NSTEPS = 1000 |
---|
| 21 | |
---|
[9b6497bb] | 22 | def guinier(x, scale=1, radius=60): |
---|
[ef9ed58] | 23 | """ |
---|
| 24 | Compute a F(x) = scale* e-((radius*x)**2/3). |
---|
| 25 | @param x: a vector of q values |
---|
| 26 | @param scale: the scale value |
---|
| 27 | @param radius: the guinier radius value |
---|
| 28 | @return F(x) |
---|
| 29 | """ |
---|
[9b6497bb] | 30 | if radius <= 0: |
---|
| 31 | raise ValueError("Rg expected positive value, but got %s"%radius) |
---|
[ef9ed58] | 32 | value = numpy.array([math.exp(-((radius * i)**2/3)) for i in x ]) |
---|
[9b6497bb] | 33 | scale = numpy.sqrt(scale*scale) |
---|
| 34 | if scale == 0: |
---|
| 35 | raise ValueError("scale expected positive value, but got %s"%scale) |
---|
[ef9ed58] | 36 | return scale * value |
---|
| 37 | |
---|
[9b6497bb] | 38 | def power_law(x, scale=1, power=4): |
---|
[ef9ed58] | 39 | """ |
---|
| 40 | F(x) = scale* (x)^(-power) |
---|
| 41 | when power= 4. the model is porod |
---|
| 42 | else power_law |
---|
| 43 | The model has three parameters: |
---|
| 44 | @param x: a vector of q values |
---|
| 45 | @param power: power of the function |
---|
| 46 | @param scale : scale factor value |
---|
| 47 | @param F(x) |
---|
| 48 | """ |
---|
[4e80ae0] | 49 | if power <=0: |
---|
[9b6497bb] | 50 | raise ValueError("Power_law function expected positive power, but got %s"%power) |
---|
[4e80ae0] | 51 | |
---|
[ef9ed58] | 52 | value = numpy.array([ math.pow(i, -power) for i in x ]) |
---|
[9b6497bb] | 53 | scale = numpy.sqrt(scale*scale) |
---|
| 54 | if scale == 0: |
---|
| 55 | raise ValueError("scale expected positive value, but got %s"%scale) |
---|
[ef9ed58] | 56 | return scale * value |
---|
| 57 | |
---|
[b6666d4] | 58 | class FitFunctor: |
---|
[75047cf] | 59 | """ |
---|
| 60 | compute f(x) |
---|
| 61 | """ |
---|
[f521f61] | 62 | def __init__(self, data): |
---|
[75047cf] | 63 | """ |
---|
[437a9f0] | 64 | Determine a and b given a linear equation y = ax + b |
---|
| 65 | @param Data: data containing x and y such as y = ax + b |
---|
[75047cf] | 66 | """ |
---|
| 67 | self.data = data |
---|
| 68 | x_len = len(self.data.x) -1 |
---|
| 69 | #fitting range |
---|
[ef9ed58] | 70 | self.qmin = self.data.x[0] |
---|
[75047cf] | 71 | if self.qmin == 0: |
---|
[669aaf9] | 72 | self.qmin = Q_MINIMUM |
---|
[75047cf] | 73 | |
---|
[ef9ed58] | 74 | self.qmax = self.data.x[x_len] |
---|
[75047cf] | 75 | #Unsmeared q range |
---|
| 76 | self._qmin_unsmeared = 0 |
---|
[ef9ed58] | 77 | self._qmax_unsmeared = self.data.x[x_len] |
---|
[75047cf] | 78 | |
---|
| 79 | #bin for smear data |
---|
| 80 | self._first_unsmeared_bin = 0 |
---|
| 81 | self._last_unsmeared_bin = x_len |
---|
| 82 | |
---|
| 83 | # Identify the bin range for the unsmeared and smeared spaces |
---|
[ef9ed58] | 84 | self.idx = (self.data.x >= self.qmin) & (self.data.x <= self.qmax) |
---|
| 85 | self.idx_unsmeared = (self.data.x >= self._qmin_unsmeared) \ |
---|
| 86 | & (self.data.x <= self._qmax_unsmeared) |
---|
[75047cf] | 87 | |
---|
| 88 | #get the smear object of data |
---|
| 89 | self.smearer = smear_selection( self.data ) |
---|
[ef9ed58] | 90 | |
---|
| 91 | def set_fit_range(self ,qmin=None, qmax=None): |
---|
[75047cf] | 92 | """ to set the fit range""" |
---|
[ef9ed58] | 93 | if qmin is not None: |
---|
| 94 | self.qmin = qmin |
---|
| 95 | if qmax is not None: |
---|
| 96 | self.qmax = qmax |
---|
[75047cf] | 97 | |
---|
| 98 | # Determine the range needed in unsmeared-Q to cover |
---|
| 99 | # the smeared Q range |
---|
| 100 | self._qmin_unsmeared = self.qmin |
---|
| 101 | self._qmax_unsmeared = self.qmax |
---|
| 102 | |
---|
| 103 | self._first_unsmeared_bin = 0 |
---|
| 104 | self._last_unsmeared_bin = len(self.data.x)-1 |
---|
| 105 | |
---|
| 106 | if self.smearer!=None: |
---|
| 107 | self._first_unsmeared_bin, self._last_unsmeared_bin = self.smearer.get_bin_range(self.qmin, self.qmax) |
---|
| 108 | self._qmin_unsmeared = self.data.x[self._first_unsmeared_bin] |
---|
| 109 | self._qmax_unsmeared = self.data.x[self._last_unsmeared_bin] |
---|
| 110 | |
---|
| 111 | # Identify the bin range for the unsmeared and smeared spaces |
---|
[ef9ed58] | 112 | self.idx = (self.data.x >= self.qmin) & (self.data.x <= self.qmax) |
---|
| 113 | self.idx_unsmeared = (self.data.x >= self._qmin_unsmeared) \ |
---|
| 114 | & (self.data.x <= self._qmax_unsmeared) |
---|
[75047cf] | 115 | |
---|
[472b11c] | 116 | def fit(self, power =None): |
---|
[75047cf] | 117 | """ |
---|
[ef9ed58] | 118 | Fit data for y = ax + b return a and b |
---|
[472b11c] | 119 | @param power = a fixed, otherwise None |
---|
[75047cf] | 120 | """ |
---|
[472b11c] | 121 | power = power |
---|
[437a9f0] | 122 | fx = numpy.zeros(len(self.data.x)) |
---|
[25a92d1] | 123 | one = numpy.ones(len(self.data.x)) |
---|
[9b6497bb] | 124 | |
---|
| 125 | #define dy^2 |
---|
[25a92d1] | 126 | try: |
---|
| 127 | sigma = self.data.dy[self.idx_unsmeared ] |
---|
| 128 | except: |
---|
| 129 | print "The dy data for Invariant calculation should be prepared before get to FitFunctor.fit()..." |
---|
| 130 | sigma = one[self.idx_unsmeared ] |
---|
[9b6497bb] | 131 | sigma2 = sigma*sigma |
---|
| 132 | |
---|
| 133 | # Compute theory data f(x) |
---|
[eb62193] | 134 | fx = self.data.y[self.idx_unsmeared ]/sigma |
---|
[75047cf] | 135 | ## Smear theory data |
---|
| 136 | if self.smearer is not None: |
---|
[437a9f0] | 137 | fx = self.smearer(fx, self._first_unsmeared_bin,self._last_unsmeared_bin) |
---|
[472b11c] | 138 | |
---|
| 139 | ##power is given only for function = power_law |
---|
| 140 | if power != None: |
---|
[eb62193] | 141 | a = -(power) |
---|
| 142 | b = (numpy.sum(fx/sigma) - a*numpy.sum(self.data.x[self.idx]/sigma2))/numpy.sum(numpy.ones(len(sigma2))/sigma2) |
---|
[472b11c] | 143 | return a, b |
---|
| 144 | else: |
---|
[eb62193] | 145 | A = numpy.vstack([ self.data.x[self.idx]/sigma, |
---|
| 146 | numpy.ones(len(self.data.x[self.idx]))/sigma]).T |
---|
[437a9f0] | 147 | |
---|
[472b11c] | 148 | a, b = numpy.linalg.lstsq(A, fx)[0] |
---|
| 149 | return a, b |
---|
[b6666d4] | 150 | |
---|
[75047cf] | 151 | class InvariantCalculator(object): |
---|
| 152 | """ |
---|
| 153 | Compute invariant if data is given. |
---|
| 154 | Can provide volume fraction and surface area if the user provides |
---|
| 155 | Porod constant and contrast values. |
---|
| 156 | @precondition: the user must send a data of type DataLoader.Data1D |
---|
| 157 | the user provide background and scale values. |
---|
| 158 | |
---|
| 159 | @note: Some computations depends on each others. |
---|
| 160 | """ |
---|
[ef9ed58] | 161 | |
---|
| 162 | |
---|
[75047cf] | 163 | def __init__(self, data, background=0, scale=1 ): |
---|
| 164 | """ |
---|
| 165 | Initialize variables |
---|
| 166 | @param data: data must be of type DataLoader.Data1D |
---|
| 167 | @param contrast: contrast value of type float |
---|
| 168 | @param pConst: Porod Constant of type float |
---|
| 169 | """ |
---|
[b6666d4] | 170 | # Background and scale should be private data member if the only way to |
---|
| 171 | # change them are by instantiating a new object. |
---|
| 172 | self._background = background |
---|
| 173 | self._scale = scale |
---|
[75047cf] | 174 | |
---|
[b6666d4] | 175 | # The data should be private |
---|
| 176 | self._data = self._get_data(data) |
---|
[f31ab59] | 177 | |
---|
[b6666d4] | 178 | # Since there are multiple variants of Q*, you should force the |
---|
| 179 | # user to use the get method and keep Q* a private data member |
---|
| 180 | self._qstar = None |
---|
| 181 | |
---|
| 182 | # You should keep the error on Q* so you can reuse it without |
---|
| 183 | # recomputing the whole thing. |
---|
| 184 | self._qstar_err = 0 |
---|
[75047cf] | 185 | |
---|
[b6666d4] | 186 | # Extrapolation parameters |
---|
| 187 | self._low_extrapolation_npts = 4 |
---|
| 188 | self._low_extrapolation_function = guinier |
---|
[4e80ae0] | 189 | self._low_extrapolation_power = None |
---|
[ef9ed58] | 190 | |
---|
[b6666d4] | 191 | self._high_extrapolation_npts = 4 |
---|
| 192 | self._high_extrapolation_function = power_law |
---|
[4e80ae0] | 193 | self._high_extrapolation_power = None |
---|
[75047cf] | 194 | |
---|
[b6666d4] | 195 | def _get_data(self, data): |
---|
[75047cf] | 196 | """ |
---|
| 197 | @note this function must be call before computing any type |
---|
| 198 | of invariant |
---|
[b6666d4] | 199 | @return data= self._scale *data - self._background |
---|
[75047cf] | 200 | """ |
---|
[b6666d4] | 201 | if not issubclass(data.__class__, LoaderData1D): |
---|
| 202 | #Process only data that inherited from DataLoader.Data_info.Data1D |
---|
| 203 | raise ValueError,"Data must be of type DataLoader.Data1D" |
---|
[f31ab59] | 204 | new_data = self._scale * data - self._background |
---|
[25a92d1] | 205 | try: |
---|
| 206 | #All data should pass here. |
---|
| 207 | new_data.dy = data.dy |
---|
| 208 | new_data.dxl = data.dxl |
---|
| 209 | new_data.dxw = data.dxw |
---|
| 210 | except: |
---|
| 211 | #in case... |
---|
| 212 | new_data.dy = numpy.ones(len(data.x)) |
---|
| 213 | new_data.dxl = numpy.zeros(len(data.x)) |
---|
| 214 | new_data.dxw = numpy.zeros(len(data.x)) |
---|
| 215 | |
---|
[f31ab59] | 216 | return new_data |
---|
[b6666d4] | 217 | |
---|
[4e80ae0] | 218 | def _fit(self, function, qmin=Q_MINIMUM, qmax=Q_MAXIMUM, power=None): |
---|
[75047cf] | 219 | """ |
---|
| 220 | fit data with function using |
---|
| 221 | data= self._get_data() |
---|
| 222 | fx= Functor(data , function) |
---|
| 223 | y = data.y |
---|
[bad9ae2] | 224 | slope, constant = linalg.lstsq(y,fx) |
---|
[ef9ed58] | 225 | @param qmin: data first q value to consider during the fit |
---|
| 226 | @param qmax: data last q value to consider during the fit |
---|
[4e80ae0] | 227 | @param power : power value to consider for power-law |
---|
[75047cf] | 228 | @param function: the function to use during the fit |
---|
[ef9ed58] | 229 | @return a: the scale of the function |
---|
| 230 | @return b: the other parameter of the function for guinier will be radius |
---|
| 231 | for power_law will be the power value |
---|
| 232 | """ |
---|
[472b11c] | 233 | fit_x = numpy.array([math.log(x) for x in self._data.x]) |
---|
| 234 | if function.__name__ == "guinier": |
---|
[ef9ed58] | 235 | qmin = qmin**2 |
---|
| 236 | qmax = qmax**2 |
---|
[437a9f0] | 237 | fit_y = numpy.array([math.log(y) for y in self._data.y]) |
---|
[9b6497bb] | 238 | fit_dy = numpy.array([y for y in self._data.y]) |
---|
| 239 | fit_dy = numpy.array([dy for dy in self._data.dy])/fit_dy |
---|
| 240 | |
---|
[ef9ed58] | 241 | elif function.__name__ == "power_law": |
---|
[472b11c] | 242 | qmin = math.log(qmin) |
---|
| 243 | qmax = math.log(qmax) |
---|
[9b6497bb] | 244 | fit_y = numpy.array([math.log(y) for y in self._data.y]) |
---|
| 245 | fit_dy = numpy.array([y for y in self._data.y]) |
---|
| 246 | fit_dy = numpy.array([dy for dy in self._data.dy])/fit_dy |
---|
| 247 | |
---|
[ef9ed58] | 248 | else: |
---|
| 249 | raise ValueError("Unknown function used to fit %s"%function.__name__) |
---|
[472b11c] | 250 | |
---|
[ef9ed58] | 251 | |
---|
[472b11c] | 252 | #else: |
---|
| 253 | fit_data = LoaderData1D(x=fit_x, y=fit_y, dy=fit_dy) |
---|
| 254 | fit_data.dxl = self._data.dxl |
---|
| 255 | fit_data.dxw = self._data.dxw |
---|
| 256 | functor = FitFunctor(data=fit_data) |
---|
| 257 | functor.set_fit_range(qmin=qmin, qmax=qmax) |
---|
| 258 | b, a = functor.fit(power=power) |
---|
| 259 | |
---|
[9b6497bb] | 260 | |
---|
[ef9ed58] | 261 | if function.__name__ == "guinier": |
---|
| 262 | # b is the radius value of the guinier function |
---|
[9b6497bb] | 263 | if b>=0: |
---|
| 264 | raise ValueError("Guinier fit was not converged") |
---|
| 265 | else: |
---|
| 266 | b = math.sqrt(-3 * b) |
---|
| 267 | |
---|
| 268 | |
---|
[437a9f0] | 269 | if function.__name__ == "power_law": |
---|
[472b11c] | 270 | b = -1 * b |
---|
[4e80ae0] | 271 | if b <= 0: |
---|
| 272 | raise ValueError("Power_law fit expected posive power, but got %s"%power) |
---|
[ef9ed58] | 273 | # a is the scale of the guinier function |
---|
| 274 | a = math.exp(a) |
---|
[472b11c] | 275 | |
---|
[437a9f0] | 276 | return a, b |
---|
[ef9ed58] | 277 | |
---|
| 278 | def _get_qstar(self, data): |
---|
[75047cf] | 279 | """ |
---|
[ef9ed58] | 280 | Compute invariant for data |
---|
| 281 | @param data: data to use to compute invariant. |
---|
| 282 | |
---|
| 283 | """ |
---|
| 284 | if data is None: |
---|
| 285 | return 0 |
---|
| 286 | if data.is_slit_smeared(): |
---|
| 287 | return self._get_qstar_smear(data) |
---|
| 288 | else: |
---|
| 289 | return self._get_qstar_unsmear(data) |
---|
[75047cf] | 290 | |
---|
[ef9ed58] | 291 | def _get_qstar_unsmear(self, data): |
---|
[75047cf] | 292 | """ |
---|
| 293 | Compute invariant for pinhole data. |
---|
| 294 | This invariant is given by: |
---|
| 295 | |
---|
| 296 | q_star = x0**2 *y0 *dx0 +x1**2 *y1 *dx1 |
---|
| 297 | + ..+ xn**2 *yn *dxn |
---|
[b6666d4] | 298 | |
---|
[669aaf9] | 299 | where n >= len(data.x)-1 |
---|
[75047cf] | 300 | dxi = 1/2*(xi+1 - xi) + (xi - xi-1) |
---|
[3bb37ef] | 301 | dx0 = (x1 - x0)/2 |
---|
| 302 | dxn = (xn - xn-1)/2 |
---|
[ef9ed58] | 303 | @param data: the data to use to compute invariant. |
---|
| 304 | @return q_star: invariant value for pinhole data. q_star > 0 |
---|
[75047cf] | 305 | """ |
---|
[ef9ed58] | 306 | if len(data.x) <= 1 or len(data.y) <= 1 or len(data.x)!= len(data.y): |
---|
| 307 | msg = "Length x and y must be equal" |
---|
| 308 | msg += " and greater than 1; got x=%s, y=%s"%(len(data.x), len(data.y)) |
---|
| 309 | raise ValueError, msg |
---|
| 310 | else: |
---|
| 311 | n = len(data.x)- 1 |
---|
| 312 | #compute the first delta q |
---|
[3bb37ef] | 313 | dx0 = (data.x[1] - data.x[0])/2 |
---|
[ef9ed58] | 314 | #compute the last delta q |
---|
[3bb37ef] | 315 | dxn = (data.x[n] - data.x[n-1])/2 |
---|
[ef9ed58] | 316 | sum = 0 |
---|
| 317 | sum += data.x[0] * data.x[0] * data.y[0] * dx0 |
---|
| 318 | sum += data.x[n] * data.x[n] * data.y[n] * dxn |
---|
| 319 | |
---|
| 320 | if len(data.x) == 2: |
---|
| 321 | return sum |
---|
| 322 | else: |
---|
| 323 | #iterate between for element different from the first and the last |
---|
| 324 | for i in xrange(1, n-1): |
---|
| 325 | dxi = (data.x[i+1] - data.x[i-1])/2 |
---|
| 326 | sum += data.x[i] * data.x[i] * data.y[i] * dxi |
---|
| 327 | return sum |
---|
| 328 | |
---|
| 329 | def _get_qstar_smear(self, data): |
---|
[75047cf] | 330 | """ |
---|
| 331 | Compute invariant for slit-smeared data. |
---|
| 332 | This invariant is given by: |
---|
| 333 | q_star = x0*dxl *y0*dx0 + x1*dxl *y1 *dx1 |
---|
| 334 | + ..+ xn*dxl *yn *dxn |
---|
[669aaf9] | 335 | where n >= len(data.x)-1 |
---|
[75047cf] | 336 | dxi = 1/2*(xi+1 - xi) + (xi - xi-1) |
---|
[3bb37ef] | 337 | dx0 = (x1 - x0)/2 |
---|
| 338 | dxn = (xn - xn-1)/2 |
---|
[75047cf] | 339 | dxl: slit smear value |
---|
[b6666d4] | 340 | |
---|
[75047cf] | 341 | @return q_star: invariant value for slit smeared data. |
---|
| 342 | """ |
---|
[ef9ed58] | 343 | if not data.is_slit_smeared(): |
---|
| 344 | msg = "_get_qstar_smear need slit smear data " |
---|
| 345 | msg += "Hint :dxl= %s , dxw= %s"%(str(data.dxl), str(data.dxw)) |
---|
| 346 | raise ValueError, msg |
---|
| 347 | |
---|
| 348 | if len(data.x) <= 1 or len(data.y) <= 1 or len(data.x) != len(data.y)\ |
---|
| 349 | or len(data.x)!= len(data.dxl): |
---|
| 350 | msg = "x, dxl, and y must be have the same length and greater than 1" |
---|
| 351 | raise ValueError, msg |
---|
| 352 | else: |
---|
| 353 | n = len(data.x)-1 |
---|
| 354 | #compute the first delta |
---|
[3bb37ef] | 355 | dx0 = (data.x[1] - data.x[0])/2 |
---|
[ef9ed58] | 356 | #compute the last delta |
---|
[3bb37ef] | 357 | dxn = (data.x[n] - data.x[n-1])/2 |
---|
[ef9ed58] | 358 | sum = 0 |
---|
| 359 | sum += data.x[0] * data.dxl[0] * data.y[0] * dx0 |
---|
| 360 | sum += data.x[n] * data.dxl[n] * data.y[n] * dxn |
---|
| 361 | |
---|
| 362 | if len(data.x)==2: |
---|
| 363 | return sum |
---|
| 364 | else: |
---|
| 365 | #iterate between for element different from the first and the last |
---|
| 366 | for i in xrange(1, n-1): |
---|
| 367 | dxi = (data.x[i+1] - data.x[i-1])/2 |
---|
| 368 | sum += data.x[i] * data.dxl[i] * data.y[i] * dxi |
---|
| 369 | return sum |
---|
[75047cf] | 370 | |
---|
[ef9ed58] | 371 | def _get_qstar_uncertainty(self, data=None): |
---|
| 372 | """ |
---|
| 373 | Compute uncertainty of invariant value |
---|
| 374 | Implementation: |
---|
| 375 | if data is None: |
---|
| 376 | data = self.data |
---|
| 377 | |
---|
| 378 | if data.slit smear: |
---|
| 379 | return self._get_qstar_smear_uncertainty(data) |
---|
| 380 | else: |
---|
| 381 | return self._get_qstar_unsmear_uncertainty(data) |
---|
| 382 | |
---|
| 383 | @param: data use to compute the invariant which allow uncertainty |
---|
| 384 | computation. |
---|
| 385 | @return: uncertainty |
---|
| 386 | """ |
---|
| 387 | if data is None: |
---|
| 388 | data = self.data |
---|
| 389 | |
---|
| 390 | if data.is_slit_smeared(): |
---|
| 391 | return self._get_qstar_smear_uncertainty(data) |
---|
| 392 | else: |
---|
| 393 | return self._get_qstar_unsmear_uncertainty(data) |
---|
| 394 | |
---|
| 395 | def _get_qstar_unsmear_uncertainty(self, data=None): |
---|
[75047cf] | 396 | """ |
---|
| 397 | Compute invariant uncertainty with with pinhole data. |
---|
| 398 | This uncertainty is given as follow: |
---|
| 399 | dq_star = math.sqrt[(x0**2*(dy0)*dx0)**2 + |
---|
| 400 | (x1**2 *(dy1)*dx1)**2 + ..+ (xn**2 *(dyn)*dxn)**2 ] |
---|
[669aaf9] | 401 | where n >= len(data.x)-1 |
---|
[75047cf] | 402 | dxi = 1/2*(xi+1 - xi) + (xi - xi-1) |
---|
[3bb37ef] | 403 | dx0 = (x1 - x0)/2 |
---|
| 404 | dxn = (xn - xn-1)/2 |
---|
[75047cf] | 405 | dyn: error on dy |
---|
[ef9ed58] | 406 | |
---|
| 407 | @param data: |
---|
[75047cf] | 408 | note: if data doesn't contain dy assume dy= math.sqrt(data.y) |
---|
| 409 | """ |
---|
[ef9ed58] | 410 | if data is None: |
---|
| 411 | data = self.data |
---|
| 412 | |
---|
| 413 | if len(data.x) <= 1 or len(data.y) <= 1 or \ |
---|
| 414 | len(self.data.x) != len(self.data.y): |
---|
| 415 | msg = "Length of data.x and data.y must be equal" |
---|
| 416 | msg += " and greater than 1; got x=%s, y=%s"%(len(data.x), |
---|
| 417 | len(data.y)) |
---|
| 418 | raise ValueError, msg |
---|
| 419 | else: |
---|
| 420 | #Create error for data without dy error |
---|
| 421 | if (data.dy is None) or (not data.dy): |
---|
| 422 | dy = math.sqrt(y) |
---|
| 423 | else: |
---|
| 424 | dy = data.dy |
---|
| 425 | |
---|
| 426 | n = len(data.x) - 1 |
---|
| 427 | #compute the first delta |
---|
[3bb37ef] | 428 | dx0 = (data.x[1] - data.x[0])/2 |
---|
[ef9ed58] | 429 | #compute the last delta |
---|
[3bb37ef] | 430 | dxn= (data.x[n] - data.x[n-1])/2 |
---|
[ef9ed58] | 431 | sum = 0 |
---|
| 432 | sum += (data.x[0] * data.x[0] * dy[0] * dx0)**2 |
---|
| 433 | sum += (data.x[n] * data.x[n] * dy[n] * dxn)**2 |
---|
| 434 | if len(data.x) == 2: |
---|
| 435 | return math.sqrt(sum) |
---|
| 436 | else: |
---|
| 437 | #iterate between for element different from the first and the last |
---|
| 438 | for i in xrange(1, n-1): |
---|
| 439 | dxi = (data.x[i+1] - data.x[i-1])/2 |
---|
| 440 | sum += (data.x[i] * data.x[i] * dy[i] * dxi)**2 |
---|
| 441 | return math.sqrt(sum) |
---|
[b6666d4] | 442 | |
---|
| 443 | def _get_qstar_smear_uncertainty(self): |
---|
[75047cf] | 444 | """ |
---|
| 445 | Compute invariant uncertainty with slit smeared data. |
---|
| 446 | This uncertainty is given as follow: |
---|
| 447 | dq_star = x0*dxl *dy0 *dx0 + x1*dxl *dy1 *dx1 |
---|
| 448 | + ..+ xn*dxl *dyn *dxn |
---|
[669aaf9] | 449 | where n >= len(data.x)-1 |
---|
[75047cf] | 450 | dxi = 1/2*(xi+1 - xi) + (xi - xi-1) |
---|
[3bb37ef] | 451 | dx0 = (x1 - x0)/2 |
---|
| 452 | dxn = (xn - xn-1)/2 |
---|
[75047cf] | 453 | dxl: slit smearing value |
---|
| 454 | dyn : error on dy |
---|
| 455 | @param data: data of type Data1D where the scale is applied |
---|
| 456 | and the background is subtracted. |
---|
| 457 | |
---|
| 458 | note: if data doesn't contain dy assume dy= math.sqrt(data.y) |
---|
| 459 | """ |
---|
[ef9ed58] | 460 | if data is None: |
---|
| 461 | data = self._data |
---|
| 462 | |
---|
| 463 | if not data.is_slit_smeared(): |
---|
| 464 | msg = "_get_qstar_smear_uncertainty need slit smear data " |
---|
| 465 | msg += "Hint :dxl= %s , dxw= %s"%(str(data.dxl), str(data.dxw)) |
---|
| 466 | raise ValueError, msg |
---|
| 467 | |
---|
| 468 | if len(data.x) <= 1 or len(data.y) <= 1 or len(data.x) != len(data.y)\ |
---|
| 469 | or len(data.x) != len(data.dxl): |
---|
| 470 | msg = "x, dxl, and y must be have the same length and greater than 1" |
---|
| 471 | raise ValueError, msg |
---|
| 472 | else: |
---|
| 473 | #Create error for data without dy error |
---|
| 474 | if (data.dy is None) or (not data.dy): |
---|
| 475 | dy = math.sqrt(y) |
---|
| 476 | else: |
---|
| 477 | dy = data.dy |
---|
| 478 | |
---|
| 479 | n = len(data.x) - 1 |
---|
| 480 | #compute the first delta |
---|
[3bb37ef] | 481 | dx0 = (data.x[1] - data.x[0])/2 |
---|
[ef9ed58] | 482 | #compute the last delta |
---|
[3bb37ef] | 483 | dxn = (data.x[n] - data.x[n-1])/2 |
---|
[ef9ed58] | 484 | sum = 0 |
---|
| 485 | sum += (data.x[0] * data.dxl[0] * dy[0] * dx0)**2 |
---|
| 486 | sum += (data.x[n] * data.dxl[n] * dy[n] * dxn)**2 |
---|
| 487 | |
---|
| 488 | if len(data.x) == 2: |
---|
| 489 | return math.sqrt(sum) |
---|
| 490 | else: |
---|
| 491 | #iterate between for element different from the first and the last |
---|
| 492 | for i in xrange(1, n-1): |
---|
| 493 | dxi = (data.x[i+1] - data.x[i-1])/2 |
---|
| 494 | sum += (data.x[i] * data.dxl[i] * dy[i] * dxi)**2 |
---|
| 495 | return math.sqrt(sum) |
---|
[4e80ae0] | 496 | |
---|
[b6666d4] | 497 | def _get_qstar_low(self): |
---|
[75047cf] | 498 | """ |
---|
| 499 | Compute the invariant for extrapolated data at low q range. |
---|
| 500 | |
---|
| 501 | Implementation: |
---|
[b6666d4] | 502 | data = self.get_extra_data_low() |
---|
[75047cf] | 503 | return self._get_qstar() |
---|
| 504 | |
---|
| 505 | @return q_star: the invariant for data extrapolated at low q. |
---|
| 506 | """ |
---|
[ef9ed58] | 507 | data = self._get_extra_data_low() |
---|
| 508 | return self._get_qstar(data=data) |
---|
[75047cf] | 509 | |
---|
[b6666d4] | 510 | def _get_qstar_high(self): |
---|
[75047cf] | 511 | """ |
---|
| 512 | Compute the invariant for extrapolated data at high q range. |
---|
| 513 | |
---|
| 514 | Implementation: |
---|
[b6666d4] | 515 | data = self.get_extra_data_high() |
---|
[75047cf] | 516 | return self._get_qstar() |
---|
| 517 | |
---|
| 518 | @return q_star: the invariant for data extrapolated at high q. |
---|
| 519 | """ |
---|
[ef9ed58] | 520 | data = self._get_extra_data_high() |
---|
[f31ab59] | 521 | return self._get_qstar(data=data) |
---|
[75047cf] | 522 | |
---|
[b6666d4] | 523 | def _get_extra_data_low(self): |
---|
[75047cf] | 524 | """ |
---|
[b6666d4] | 525 | This method creates a new data set from the invariant calculator. |
---|
| 526 | |
---|
| 527 | It will use the extrapolation parameters kept as private data members. |
---|
| 528 | |
---|
| 529 | self._low_extrapolation_npts is the number of data points to use in to fit. |
---|
| 530 | self._low_extrapolation_function will be used as the fit function. |
---|
| 531 | |
---|
| 532 | |
---|
[75047cf] | 533 | |
---|
[b6666d4] | 534 | It takes npts first points of data, fits them with a given model |
---|
| 535 | then uses the new parameters resulting from the fit to create a new data set. |
---|
[75047cf] | 536 | |
---|
[b6666d4] | 537 | The new data first point is Q_MINIMUM. |
---|
| 538 | |
---|
| 539 | The last point of the new data is the first point of the original data. |
---|
| 540 | the number of q points of this data is INTEGRATION_NSTEPS. |
---|
| 541 | |
---|
| 542 | @return: a new data of type Data1D |
---|
[75047cf] | 543 | """ |
---|
[9b6497bb] | 544 | |
---|
[ef9ed58] | 545 | # Data boundaries for fiiting |
---|
| 546 | qmin = self._data.x[0] |
---|
[437a9f0] | 547 | qmax = self._data.x[self._low_extrapolation_npts - 1] |
---|
[ef9ed58] | 548 | |
---|
| 549 | try: |
---|
| 550 | # fit the data with a model to get the appropriate parameters |
---|
| 551 | a, b = self._fit(function=self._low_extrapolation_function, |
---|
[4e80ae0] | 552 | qmin=qmin, |
---|
| 553 | qmax=qmax, |
---|
| 554 | power=self._low_extrapolation_power) |
---|
[ef9ed58] | 555 | except: |
---|
[7a108dd] | 556 | return None |
---|
[9b6497bb] | 557 | |
---|
[3bb37ef] | 558 | #q_start point |
---|
| 559 | q_start = Q_MINIMUM |
---|
| 560 | if Q_MINIMUM >= qmin: |
---|
| 561 | q_start = qmin/10 |
---|
| 562 | |
---|
[ef9ed58] | 563 | #create new Data1D to compute the invariant |
---|
[3bb37ef] | 564 | new_x = numpy.linspace(start=q_start, |
---|
[ef9ed58] | 565 | stop=qmin, |
---|
| 566 | num=INTEGRATION_NSTEPS, |
---|
| 567 | endpoint=True) |
---|
[437a9f0] | 568 | new_y = self._low_extrapolation_function(x=new_x, scale=a, radius=b) |
---|
[ef9ed58] | 569 | dxl = None |
---|
| 570 | dxw = None |
---|
| 571 | if self._data.dxl is not None: |
---|
[f31ab59] | 572 | dxl = numpy.ones(INTEGRATION_NSTEPS) |
---|
[ef9ed58] | 573 | dxl = dxl * self._data.dxl[0] |
---|
| 574 | if self._data.dxw is not None: |
---|
[f31ab59] | 575 | dxw = numpy.ones(INTEGRATION_NSTEPS) |
---|
[437a9f0] | 576 | dxw = dxw * self._data.dxw[0] |
---|
[9b6497bb] | 577 | |
---|
[ef9ed58] | 578 | data_min = LoaderData1D(x=new_x, y=new_y) |
---|
| 579 | data_min.dxl = dxl |
---|
| 580 | data_min.dxw = dxw |
---|
| 581 | self._data.clone_without_data( clone= data_min) |
---|
[9b6497bb] | 582 | |
---|
[ef9ed58] | 583 | return data_min |
---|
| 584 | |
---|
[b6666d4] | 585 | def _get_extra_data_high(self): |
---|
[75047cf] | 586 | """ |
---|
[b6666d4] | 587 | This method creates a new data from the invariant calculator. |
---|
| 588 | |
---|
| 589 | It takes npts last points of data, fits them with a given model |
---|
[75047cf] | 590 | (for this function only power_law will be use), then uses |
---|
[b6666d4] | 591 | the new parameters resulting from the fit to create a new data set. |
---|
| 592 | The first point is the last point of data. |
---|
| 593 | The last point of the new data is Q_MAXIMUM. |
---|
| 594 | The number of q points of this data is INTEGRATION_NSTEPS. |
---|
| 595 | |
---|
[75047cf] | 596 | |
---|
[b6666d4] | 597 | @return: a new data of type Data1D |
---|
[75047cf] | 598 | """ |
---|
[ef9ed58] | 599 | # Data boundaries for fiiting |
---|
| 600 | x_len = len(self._data.x) - 1 |
---|
[bad9ae2] | 601 | qmin = self._data.x[x_len - (self._high_extrapolation_npts - 1)] |
---|
[ef9ed58] | 602 | qmax = self._data.x[x_len] |
---|
| 603 | |
---|
| 604 | try: |
---|
| 605 | # fit the data with a model to get the appropriate parameters |
---|
| 606 | a, b = self._fit(function=self._high_extrapolation_function, |
---|
[4e80ae0] | 607 | qmin=qmin, |
---|
| 608 | qmax=qmax, |
---|
| 609 | power=self._high_extrapolation_power) |
---|
[ef9ed58] | 610 | except: |
---|
[7a108dd] | 611 | return None |
---|
[ef9ed58] | 612 | |
---|
| 613 | #create new Data1D to compute the invariant |
---|
| 614 | new_x = numpy.linspace(start=qmax, |
---|
| 615 | stop=Q_MAXIMUM, |
---|
| 616 | num=INTEGRATION_NSTEPS, |
---|
| 617 | endpoint=True) |
---|
[437a9f0] | 618 | |
---|
| 619 | new_y = self._high_extrapolation_function(x=new_x, scale=a, power=b) |
---|
| 620 | |
---|
[ef9ed58] | 621 | dxl = None |
---|
| 622 | dxw = None |
---|
| 623 | if self._data.dxl is not None: |
---|
[f31ab59] | 624 | dxl = numpy.ones(INTEGRATION_NSTEPS) |
---|
[ef9ed58] | 625 | dxl = dxl * self._data.dxl[0] |
---|
| 626 | if self._data.dxw is not None: |
---|
[f31ab59] | 627 | dxw = numpy.ones(INTEGRATION_NSTEPS) |
---|
[437a9f0] | 628 | dxw = dxw * self._data.dxw[0] |
---|
[ef9ed58] | 629 | |
---|
| 630 | data_max = LoaderData1D(x=new_x, y=new_y) |
---|
| 631 | data_max.dxl = dxl |
---|
| 632 | data_max.dxw = dxw |
---|
[7a108dd] | 633 | self._data.clone_without_data(clone=data_max) |
---|
[f31ab59] | 634 | |
---|
[ef9ed58] | 635 | return data_max |
---|
[4e80ae0] | 636 | |
---|
| 637 | def set_extrapolation(self, range, npts=4, function=None, power=None): |
---|
| 638 | """ |
---|
| 639 | Set the extrapolation parameters for the high or low Q-range. |
---|
| 640 | Note that this does not turn extrapolation on or off. |
---|
| 641 | @param range: a keyword set the type of extrapolation . type string |
---|
| 642 | @param npts: the numbers of q points of data to consider for extrapolation |
---|
| 643 | @param function: a keyword to select the function to use for extrapolation. |
---|
| 644 | of type string. |
---|
| 645 | @param power: an power to apply power_low function |
---|
| 646 | |
---|
| 647 | """ |
---|
| 648 | range = range.lower() |
---|
| 649 | if range not in ['high', 'low']: |
---|
| 650 | raise ValueError, "Extrapolation range should be 'high' or 'low'" |
---|
| 651 | function = function.lower() |
---|
| 652 | if function not in ['power_law', 'guinier']: |
---|
| 653 | raise ValueError, "Extrapolation function should be 'guinier' or 'power_law'" |
---|
| 654 | |
---|
| 655 | if range == 'high': |
---|
| 656 | if function != 'power_law': |
---|
| 657 | raise ValueError, "Extrapolation only allows a power law at high Q" |
---|
| 658 | self._high_extrapolation_npts = npts |
---|
| 659 | self._high_extrapolation_power = power |
---|
| 660 | else: |
---|
| 661 | if function == 'power_law': |
---|
| 662 | self._low_extrapolation_function = power_law |
---|
| 663 | else: |
---|
| 664 | self._low_extrapolation_function = guinier |
---|
| 665 | self._low_extrapolation_npts = npts |
---|
| 666 | self._low_extrapolation_power = power |
---|
| 667 | |
---|
| 668 | def get_qstar(self, extrapolation=None): |
---|
| 669 | """ |
---|
| 670 | Compute the invariant of the local copy of data. |
---|
| 671 | Implementation: |
---|
| 672 | if slit smear: |
---|
| 673 | qstar_0 = self._get_qstar_smear() |
---|
| 674 | else: |
---|
| 675 | qstar_0 = self._get_qstar_unsmear() |
---|
| 676 | if extrapolation is None: |
---|
| 677 | return qstar_0 |
---|
| 678 | if extrapolation==low: |
---|
| 679 | return qstar_0 + self._get_qstar_low() |
---|
| 680 | elif extrapolation==high: |
---|
| 681 | return qstar_0 + self._get_qstar_high() |
---|
| 682 | elif extrapolation==both: |
---|
| 683 | return qstar_0 + self._get_qstar_low() + self._get_qstar_high() |
---|
| 684 | |
---|
| 685 | @param extrapolation: string to apply optional extrapolation |
---|
| 686 | @return q_star: invariant of the data within data's q range |
---|
| 687 | |
---|
| 688 | @warning: When using setting data to Data1D , the user is responsible of |
---|
| 689 | checking that the scale and the background are properly apply to the data |
---|
| 690 | |
---|
| 691 | @warning: if error occur self._get_qstar_low() or self._get_qstar_low() |
---|
| 692 | their returned value will be ignored |
---|
| 693 | """ |
---|
| 694 | qstar_0 = self._get_qstar(data=self._data) |
---|
| 695 | |
---|
| 696 | if extrapolation is None: |
---|
| 697 | self._qstar = qstar_0 |
---|
| 698 | return self._qstar |
---|
| 699 | # Compute invariant plus invaraint of extrapolated data |
---|
| 700 | extrapolation = extrapolation.lower() |
---|
| 701 | if extrapolation == "low": |
---|
| 702 | self._qstar = qstar_0 + self._get_qstar_low() |
---|
| 703 | return self._qstar |
---|
| 704 | elif extrapolation == "high": |
---|
| 705 | self._qstar = qstar_0 + self._get_qstar_high() |
---|
| 706 | return self._qstar |
---|
| 707 | elif extrapolation == "both": |
---|
| 708 | self._qstar = qstar_0 + self._get_qstar_low() + self._get_qstar_high() |
---|
| 709 | return self._qstar |
---|
| 710 | |
---|
| 711 | def get_surface(self,contrast, porod_const): |
---|
| 712 | """ |
---|
| 713 | Compute the surface of the data. |
---|
| 714 | |
---|
| 715 | Implementation: |
---|
| 716 | V= self.get_volume_fraction(contrast) |
---|
| 717 | |
---|
| 718 | Compute the surface given by: |
---|
| 719 | surface = (2*pi *V(1- V)*porod_const)/ q_star |
---|
| 720 | |
---|
| 721 | @param contrast: contrast value to compute the volume |
---|
| 722 | @param porod_const: Porod constant to compute the surface |
---|
| 723 | @return: specific surface |
---|
| 724 | """ |
---|
| 725 | #Check whether we have Q star |
---|
| 726 | if self._qstar is None: |
---|
| 727 | self._qstar = self.get_star() |
---|
| 728 | if self._qstar == 0: |
---|
| 729 | raise RuntimeError("Cannot compute surface, invariant value is zero") |
---|
| 730 | # Compute the volume |
---|
| 731 | volume = self.get_volume_fraction(contrast) |
---|
| 732 | return 2 * math.pi * volume *(1 - volume) * float(porod_const)/self._qstar |
---|
| 733 | |
---|
| 734 | def get_volume_fraction(self, contrast): |
---|
| 735 | """ |
---|
| 736 | Compute volume fraction is deduced as follow: |
---|
| 737 | |
---|
| 738 | q_star = 2*(pi*contrast)**2* volume( 1- volume) |
---|
| 739 | for k = 10^(-8)*q_star/(2*(pi*|contrast|)**2) |
---|
| 740 | we get 2 values of volume: |
---|
| 741 | with 1 - 4 * k >= 0 |
---|
| 742 | volume1 = (1- sqrt(1- 4*k))/2 |
---|
| 743 | volume2 = (1+ sqrt(1- 4*k))/2 |
---|
| 744 | |
---|
| 745 | q_star: the invariant value included extrapolation is applied |
---|
| 746 | unit 1/A^(3)*1/cm |
---|
| 747 | q_star = self.get_qstar() |
---|
| 748 | |
---|
| 749 | the result returned will be 0<= volume <= 1 |
---|
| 750 | |
---|
| 751 | @param contrast: contrast value provides by the user of type float. |
---|
| 752 | contrast unit is 1/A^(2)= 10^(16)cm^(2) |
---|
| 753 | @return: volume fraction |
---|
| 754 | @note: volume fraction must have no unit |
---|
| 755 | """ |
---|
| 756 | if contrast < 0: |
---|
| 757 | raise ValueError, "contrast must be greater than zero" |
---|
| 758 | |
---|
| 759 | if self._qstar is None: |
---|
| 760 | self._qstar = self.get_qstar() |
---|
| 761 | |
---|
| 762 | if self._qstar < 0: |
---|
| 763 | raise RuntimeError, "invariant must be greater than zero" |
---|
| 764 | |
---|
| 765 | # Compute intermediate constant |
---|
| 766 | k = 1.e-8 * self._qstar/(2 * (math.pi * math.fabs(float(contrast)))**2) |
---|
| 767 | #Check discriminant value |
---|
| 768 | discrim = 1 - 4 * k |
---|
| 769 | |
---|
| 770 | # Compute volume fraction |
---|
| 771 | if discrim < 0: |
---|
| 772 | raise RuntimeError, "could not compute the volume fraction: negative discriminant" |
---|
| 773 | elif discrim == 0: |
---|
| 774 | return 1/2 |
---|
| 775 | else: |
---|
| 776 | volume1 = 0.5 * (1 - math.sqrt(discrim)) |
---|
| 777 | volume2 = 0.5 * (1 + math.sqrt(discrim)) |
---|
| 778 | |
---|
| 779 | if 0 <= volume1 and volume1 <= 1: |
---|
| 780 | return volume1 |
---|
| 781 | elif 0 <= volume2 and volume2 <= 1: |
---|
| 782 | return volume2 |
---|
| 783 | raise RuntimeError, "could not compute the volume fraction: inconsistent results" |
---|
[ef9ed58] | 784 | |
---|
| 785 | def get_qstar_with_error(self, extrapolation=None): |
---|
[75047cf] | 786 | """ |
---|
| 787 | Compute the invariant uncertainty. |
---|
| 788 | This uncertainty computation depends on whether or not the data is |
---|
| 789 | smeared. |
---|
[ef9ed58] | 790 | @return: invariant, the invariant uncertainty |
---|
| 791 | return self._get_qstar(), self._get_qstar_smear_uncertainty() |
---|
[75047cf] | 792 | """ |
---|
[ef9ed58] | 793 | if self._qstar is None: |
---|
| 794 | self._qstar = self.get_qstar(extrapolation=extrapolation) |
---|
| 795 | if self._qstar_err is None: |
---|
| 796 | self._qstar_err = self._get_qstar_smear_uncertainty() |
---|
| 797 | |
---|
| 798 | return self._qstar, self._qstar_err |
---|
| 799 | |
---|
[b6666d4] | 800 | def get_volume_fraction_with_error(self, contrast): |
---|
[75047cf] | 801 | """ |
---|
[2cce133] | 802 | Compute uncertainty on volume value as well as the volume fraction |
---|
[75047cf] | 803 | This uncertainty is given by the following equation: |
---|
| 804 | dV = 0.5 * (4*k* dq_star) /(2* math.sqrt(1-k* q_star)) |
---|
| 805 | |
---|
[437a9f0] | 806 | for k = 10^(-8)*q_star/(2*(pi*|contrast|)**2) |
---|
[2cce133] | 807 | |
---|
[75047cf] | 808 | q_star: the invariant value including extrapolated value if existing |
---|
| 809 | dq_star: the invariant uncertainty |
---|
| 810 | dV: the volume uncertainty |
---|
| 811 | @param contrast: contrast value |
---|
[2cce133] | 812 | @return: V, dV = self.get_volume_fraction_with_error(contrast), dV |
---|
[75047cf] | 813 | """ |
---|
[ef9ed58] | 814 | self._qstar, self._qstar_err = self.get_qstar_with_error() |
---|
| 815 | |
---|
| 816 | volume = self.get_volume_fraction(contrast) |
---|
| 817 | if self._qstar < 0: |
---|
| 818 | raise ValueError, "invariant must be greater than zero" |
---|
[75047cf] | 819 | |
---|
[437a9f0] | 820 | k = 1.e-8 * self._qstar /(2 * (math.pi* math.fabs(float(contrast)))**2) |
---|
[ef9ed58] | 821 | #check value inside the sqrt function |
---|
| 822 | value = 1 - k * self._qstar |
---|
[bad9ae2] | 823 | if (value) <= 0: |
---|
[ef9ed58] | 824 | raise ValueError, "Cannot compute incertainty on volume" |
---|
| 825 | # Compute uncertainty |
---|
[437a9f0] | 826 | uncertainty = (0.5 * 4 * k * self._qstar_err)/(2 * math.sqrt(1 - k * self._qstar)) |
---|
[ef9ed58] | 827 | |
---|
| 828 | return volume, math.fabs(uncertainty) |
---|
| 829 | |
---|
[b6666d4] | 830 | def get_surface_with_error(self, contrast, porod_const): |
---|
[75047cf] | 831 | """ |
---|
[2cce133] | 832 | Compute uncertainty of the surface value as well as thesurface value |
---|
[75047cf] | 833 | this uncertainty is given as follow: |
---|
| 834 | |
---|
| 835 | dS = porod_const *2*pi[( dV -2*V*dV)/q_star |
---|
| 836 | + dq_star(v-v**2) |
---|
| 837 | |
---|
| 838 | q_star: the invariant value including extrapolated value if existing |
---|
| 839 | dq_star: the invariant uncertainty |
---|
| 840 | V: the volume fraction value |
---|
| 841 | dV: the volume uncertainty |
---|
| 842 | |
---|
| 843 | @param contrast: contrast value |
---|
| 844 | @param porod_const: porod constant value |
---|
[b6666d4] | 845 | @return S, dS: the surface, with its uncertainty |
---|
[75047cf] | 846 | """ |
---|
[ef9ed58] | 847 | v, dv = self.get_volume_fraction_with_error(contrast) |
---|
| 848 | self._qstar, self._qstar_err = self.get_qstar_with_error() |
---|
| 849 | if self._qstar <= 0: |
---|
| 850 | raise ValueError, "invariant must be greater than zero" |
---|
| 851 | ds = porod_const * 2 * math.pi * (( dv - 2 * v * dv)/ self._qstar\ |
---|
| 852 | + self._qstar_err * ( v - v**2)) |
---|
| 853 | s = self.get_surface(contrast=contrast, porod_const=porod_const) |
---|
| 854 | return s, ds |
---|