1 | ##################################################################### |
---|
2 | #This software was developed by the University of Tennessee as part of the |
---|
3 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
4 | #project funded by the US National Science Foundation. |
---|
5 | #See the license text in license.txt |
---|
6 | #copyright 2010, University of Tennessee |
---|
7 | ###################################################################### |
---|
8 | |
---|
9 | """ |
---|
10 | This module implements invariant and its related computations. |
---|
11 | |
---|
12 | :author: Gervaise B. Alina/UTK |
---|
13 | :author: Mathieu Doucet/UTK |
---|
14 | :author: Jae Cho/UTK |
---|
15 | |
---|
16 | """ |
---|
17 | import math |
---|
18 | import numpy |
---|
19 | |
---|
20 | from DataLoader.data_info import Data1D as LoaderData1D |
---|
21 | |
---|
22 | # The minimum q-value to be used when extrapolating |
---|
23 | Q_MINIMUM = 1e-5 |
---|
24 | |
---|
25 | # The maximum q-value to be used when extrapolating |
---|
26 | Q_MAXIMUM = 10 |
---|
27 | |
---|
28 | # Number of steps in the extrapolation |
---|
29 | INTEGRATION_NSTEPS = 1000 |
---|
30 | |
---|
31 | class Transform(object): |
---|
32 | """ |
---|
33 | Define interface that need to compute a function or an inverse |
---|
34 | function given some x, y |
---|
35 | """ |
---|
36 | |
---|
37 | def linearize_data(self, data): |
---|
38 | """ |
---|
39 | Linearize data so that a linear fit can be performed. |
---|
40 | Filter out the data that can't be transformed. |
---|
41 | |
---|
42 | :param data: LoadData1D instance |
---|
43 | |
---|
44 | """ |
---|
45 | # Check that the vector lengths are equal |
---|
46 | assert(len(data.x)==len(data.y)) |
---|
47 | if data.dy is not None: |
---|
48 | assert(len(data.x)==len(data.dy)) |
---|
49 | dy = data.dy |
---|
50 | else: |
---|
51 | dy = numpy.ones(len(data.y)) |
---|
52 | |
---|
53 | # Transform the data |
---|
54 | data_points = zip(data.x, data.y, dy) |
---|
55 | |
---|
56 | output_points = [(self.linearize_q_value(p[0]), |
---|
57 | math.log(p[1]), |
---|
58 | p[2]/p[1]) for p in data_points if p[0]>0 and p[1]>0 and p[2]>0] |
---|
59 | |
---|
60 | x_out, y_out, dy_out = zip(*output_points) |
---|
61 | |
---|
62 | # Create Data1D object |
---|
63 | x_out = numpy.asarray(x_out) |
---|
64 | y_out = numpy.asarray(y_out) |
---|
65 | dy_out = numpy.asarray(dy_out) |
---|
66 | linear_data = LoaderData1D(x=x_out, y=y_out, dy=dy_out) |
---|
67 | |
---|
68 | return linear_data |
---|
69 | |
---|
70 | def get_allowed_bins(self, data): |
---|
71 | """ |
---|
72 | Goes through the data points and returns a list of boolean values |
---|
73 | to indicate whether each points is allowed by the model or not. |
---|
74 | |
---|
75 | :param data: Data1D object |
---|
76 | """ |
---|
77 | return [p[0]>0 and p[1]>0 and p[2]>0 for p in zip(data.x, data.y, data.dy)] |
---|
78 | |
---|
79 | def linearize_q_value(self, value): |
---|
80 | """ |
---|
81 | Transform the input q-value for linearization |
---|
82 | """ |
---|
83 | return NotImplemented |
---|
84 | |
---|
85 | def extract_model_parameters(self, constant, slope, dconstant=0, dslope=0): |
---|
86 | """ |
---|
87 | set private member |
---|
88 | """ |
---|
89 | return NotImplemented |
---|
90 | |
---|
91 | def evaluate_model(self, x): |
---|
92 | """ |
---|
93 | Returns an array f(x) values where f is the Transform function. |
---|
94 | """ |
---|
95 | return NotImplemented |
---|
96 | |
---|
97 | def evaluate_model_errors(self, x): |
---|
98 | """ |
---|
99 | Returns an array of I(q) errors |
---|
100 | """ |
---|
101 | return NotImplemented |
---|
102 | |
---|
103 | class Guinier(Transform): |
---|
104 | """ |
---|
105 | class of type Transform that performs operations related to guinier |
---|
106 | function |
---|
107 | """ |
---|
108 | def __init__(self, scale=1, radius=60): |
---|
109 | Transform.__init__(self) |
---|
110 | self.scale = scale |
---|
111 | self.radius = radius |
---|
112 | ## Uncertainty of scale parameter |
---|
113 | self.dscale = 0 |
---|
114 | ## Unvertainty of radius parameter |
---|
115 | self.dradius = 0 |
---|
116 | |
---|
117 | def linearize_q_value(self, value): |
---|
118 | """ |
---|
119 | Transform the input q-value for linearization |
---|
120 | |
---|
121 | :param value: q-value |
---|
122 | |
---|
123 | :return: q*q |
---|
124 | """ |
---|
125 | return value * value |
---|
126 | |
---|
127 | def extract_model_parameters(self, constant, slope, dconstant=0, dslope=0): |
---|
128 | """ |
---|
129 | assign new value to the scale and the radius |
---|
130 | """ |
---|
131 | self.scale = math.exp(constant) |
---|
132 | self.radius = math.sqrt(-3 * slope) |
---|
133 | |
---|
134 | # Errors |
---|
135 | self.dscale = math.exp(constant)*dconstant |
---|
136 | self.dradius = -3.0/2.0/math.sqrt(-3 * slope)*dslope |
---|
137 | |
---|
138 | return [self.radius, self.scale], [self.dradius, self.dscale] |
---|
139 | |
---|
140 | def evaluate_model(self, x): |
---|
141 | """ |
---|
142 | return F(x)= scale* e-((radius*x)**2/3) |
---|
143 | """ |
---|
144 | return self._guinier(x) |
---|
145 | |
---|
146 | def evaluate_model_errors(self, x): |
---|
147 | """ |
---|
148 | Returns the error on I(q) for the given array of q-values |
---|
149 | |
---|
150 | :param x: array of q-values |
---|
151 | """ |
---|
152 | p1 = numpy.array([self.dscale * math.exp(-((self.radius * q)**2/3)) for q in x]) |
---|
153 | p2 = numpy.array([self.scale * math.exp(-((self.radius * q)**2/3)) * (-(q**2/3)) * 2 * self.radius * self.dradius for q in x]) |
---|
154 | diq2 = p1*p1 + p2*p2 |
---|
155 | return numpy.array( [math.sqrt(err) for err in diq2] ) |
---|
156 | |
---|
157 | def _guinier(self, x): |
---|
158 | """ |
---|
159 | Retrive the guinier function after apply an inverse guinier function |
---|
160 | to x |
---|
161 | Compute a F(x) = scale* e-((radius*x)**2/3). |
---|
162 | |
---|
163 | :param x: a vector of q values |
---|
164 | :param scale: the scale value |
---|
165 | :param radius: the guinier radius value |
---|
166 | |
---|
167 | :return: F(x) |
---|
168 | """ |
---|
169 | # transform the radius of coming from the inverse guinier function to a |
---|
170 | # a radius of a guinier function |
---|
171 | if self.radius <= 0: |
---|
172 | raise ValueError("Rg expected positive value, but got %s"%self.radius) |
---|
173 | value = numpy.array([math.exp(-((self.radius * i)**2/3)) for i in x ]) |
---|
174 | return self.scale * value |
---|
175 | |
---|
176 | class PowerLaw(Transform): |
---|
177 | """ |
---|
178 | class of type transform that perform operation related to power_law |
---|
179 | function |
---|
180 | """ |
---|
181 | def __init__(self, scale=1, power=4): |
---|
182 | Transform.__init__(self) |
---|
183 | self.scale = scale |
---|
184 | self.power = power |
---|
185 | |
---|
186 | def linearize_q_value(self, value): |
---|
187 | """ |
---|
188 | Transform the input q-value for linearization |
---|
189 | |
---|
190 | :param value: q-value |
---|
191 | |
---|
192 | :return: log(q) |
---|
193 | """ |
---|
194 | return math.log(value) |
---|
195 | |
---|
196 | def extract_model_parameters(self, constant, slope, dconstant=0, dslope=0): |
---|
197 | """ |
---|
198 | Assign new value to the scale and the power |
---|
199 | """ |
---|
200 | self.power = -slope |
---|
201 | self.scale = math.exp(constant) |
---|
202 | |
---|
203 | # Errors |
---|
204 | self.dscale = math.exp(constant)*dconstant |
---|
205 | self.dpower = -dslope |
---|
206 | |
---|
207 | return [self.power, self.scale], [self.dpower, self.dscale] |
---|
208 | |
---|
209 | def evaluate_model(self, x): |
---|
210 | """ |
---|
211 | given a scale and a radius transform x, y using a power_law |
---|
212 | function |
---|
213 | """ |
---|
214 | return self._power_law(x) |
---|
215 | |
---|
216 | def evaluate_model_errors(self, x): |
---|
217 | """ |
---|
218 | Returns the error on I(q) for the given array of q-values |
---|
219 | :param x: array of q-values |
---|
220 | """ |
---|
221 | p1 = numpy.array([self.dscale * math.pow(q, -self.power) for q in x]) |
---|
222 | p2 = numpy.array([self.scale * self.power * math.pow(q, -self.power-1) * self.dpower for q in x]) |
---|
223 | diq2 = p1*p1 + p2*p2 |
---|
224 | return numpy.array( [math.sqrt(err) for err in diq2] ) |
---|
225 | |
---|
226 | def _power_law(self, x): |
---|
227 | """ |
---|
228 | F(x) = scale* (x)^(-power) |
---|
229 | when power= 4. the model is porod |
---|
230 | else power_law |
---|
231 | The model has three parameters: :: |
---|
232 | 1. x: a vector of q values |
---|
233 | 2. power: power of the function |
---|
234 | 3. scale : scale factor value |
---|
235 | |
---|
236 | :param x: array |
---|
237 | :return: F(x) |
---|
238 | """ |
---|
239 | if self.power <= 0: |
---|
240 | raise ValueError("Power_law function expected positive power, but got %s"%self.power) |
---|
241 | if self.scale <= 0: |
---|
242 | raise ValueError("scale expected positive value, but got %s"%self.scale) |
---|
243 | |
---|
244 | value = numpy.array([ math.pow(i, -self.power) for i in x ]) |
---|
245 | return self.scale * value |
---|
246 | |
---|
247 | class Extrapolator: |
---|
248 | """ |
---|
249 | Extrapolate I(q) distribution using a given model |
---|
250 | """ |
---|
251 | def __init__(self, data, model=None): |
---|
252 | """ |
---|
253 | Determine a and b given a linear equation y = ax + b |
---|
254 | |
---|
255 | If a model is given, it will be used to linearize the data before |
---|
256 | the extrapolation is performed. If None, a simple linear fit will be done. |
---|
257 | |
---|
258 | :param data: data containing x and y such as y = ax + b |
---|
259 | :param model: optional Transform object |
---|
260 | """ |
---|
261 | self.data = data |
---|
262 | self.model = model |
---|
263 | |
---|
264 | # Set qmin as the lowest non-zero value |
---|
265 | self.qmin = Q_MINIMUM |
---|
266 | for q_value in self.data.x: |
---|
267 | if q_value > 0: |
---|
268 | self.qmin = q_value |
---|
269 | break |
---|
270 | self.qmax = max(self.data.x) |
---|
271 | |
---|
272 | def fit(self, power=None, qmin=None, qmax=None): |
---|
273 | """ |
---|
274 | Fit data for y = ax + b return a and b |
---|
275 | |
---|
276 | :param power: a fixed, otherwise None |
---|
277 | :param qmin: Minimum Q-value |
---|
278 | :param qmax: Maximum Q-value |
---|
279 | """ |
---|
280 | if qmin is None: |
---|
281 | qmin = self.qmin |
---|
282 | if qmax is None: |
---|
283 | qmax = self.qmax |
---|
284 | |
---|
285 | # Identify the bin range for the fit |
---|
286 | idx = (self.data.x >= qmin) & (self.data.x <= qmax) |
---|
287 | |
---|
288 | fx = numpy.zeros(len(self.data.x)) |
---|
289 | |
---|
290 | # Uncertainty |
---|
291 | if type(self.data.dy)==numpy.ndarray and len(self.data.dy)==len(self.data.x): |
---|
292 | sigma = self.data.dy |
---|
293 | else: |
---|
294 | sigma = numpy.ones(len(self.data.x)) |
---|
295 | |
---|
296 | # Compute theory data f(x) |
---|
297 | fx[idx] = self.data.y[idx] |
---|
298 | |
---|
299 | # Linearize the data |
---|
300 | if self.model is not None: |
---|
301 | linearized_data = self.model.linearize_data(LoaderData1D(self.data.x[idx], |
---|
302 | fx[idx], |
---|
303 | dy = sigma[idx])) |
---|
304 | else: |
---|
305 | linearized_data = LoaderData1D(self.data.x[idx], |
---|
306 | fx[idx], |
---|
307 | dy = sigma[idx]) |
---|
308 | |
---|
309 | ##power is given only for function = power_law |
---|
310 | if power != None: |
---|
311 | sigma2 = linearized_data.dy * linearized_data.dy |
---|
312 | a = -(power) |
---|
313 | b = (numpy.sum(linearized_data.y/sigma2) \ |
---|
314 | - a*numpy.sum(linearized_data.x/sigma2))/numpy.sum(1.0/sigma2) |
---|
315 | |
---|
316 | |
---|
317 | deltas = linearized_data.x*a+numpy.ones(len(linearized_data.x))*b-linearized_data.y |
---|
318 | residuals = numpy.sum(deltas*deltas/sigma2) |
---|
319 | |
---|
320 | err = math.fabs(residuals) / numpy.sum(1.0/sigma2) |
---|
321 | return [a, b], [0, math.sqrt(err)] |
---|
322 | else: |
---|
323 | A = numpy.vstack([ linearized_data.x/linearized_data.dy, |
---|
324 | 1.0/linearized_data.dy]).T |
---|
325 | (p, residuals, rank, s) = numpy.linalg.lstsq(A, linearized_data.y/linearized_data.dy) |
---|
326 | |
---|
327 | # Get the covariance matrix, defined as inv_cov = a_transposed * a |
---|
328 | err = numpy.zeros(2) |
---|
329 | try: |
---|
330 | inv_cov = numpy.dot(A.transpose(), A) |
---|
331 | cov = numpy.linalg.pinv(inv_cov) |
---|
332 | err_matrix = math.fabs(residuals) * cov |
---|
333 | err = [math.sqrt(err_matrix[0][0]), math.sqrt(err_matrix[1][1])] |
---|
334 | except: |
---|
335 | err = [-1.0, -1.0] |
---|
336 | |
---|
337 | return p, err |
---|
338 | |
---|
339 | |
---|
340 | class InvariantCalculator(object): |
---|
341 | """ |
---|
342 | Compute invariant if data is given. |
---|
343 | Can provide volume fraction and surface area if the user provides |
---|
344 | Porod constant and contrast values. |
---|
345 | |
---|
346 | :precondition: the user must send a data of type DataLoader.Data1D |
---|
347 | the user provide background and scale values. |
---|
348 | |
---|
349 | :note: Some computations depends on each others. |
---|
350 | """ |
---|
351 | def __init__(self, data, background=0, scale=1 ): |
---|
352 | """ |
---|
353 | Initialize variables. |
---|
354 | |
---|
355 | :param data: data must be of type DataLoader.Data1D |
---|
356 | :param background: Background value. The data will be corrected before processing |
---|
357 | :param scale: Scaling factor for I(q). The data will be corrected before processing |
---|
358 | """ |
---|
359 | # Background and scale should be private data member if the only way to |
---|
360 | # change them are by instantiating a new object. |
---|
361 | self._background = background |
---|
362 | self._scale = scale |
---|
363 | |
---|
364 | # The data should be private |
---|
365 | self._data = self._get_data(data) |
---|
366 | |
---|
367 | # Since there are multiple variants of Q*, you should force the |
---|
368 | # user to use the get method and keep Q* a private data member |
---|
369 | self._qstar = None |
---|
370 | |
---|
371 | # You should keep the error on Q* so you can reuse it without |
---|
372 | # recomputing the whole thing. |
---|
373 | self._qstar_err = 0 |
---|
374 | |
---|
375 | # Extrapolation parameters |
---|
376 | self._low_extrapolation_npts = 4 |
---|
377 | self._low_extrapolation_function = Guinier() |
---|
378 | self._low_extrapolation_power = None |
---|
379 | self._low_extrapolation_power_fitted = None |
---|
380 | |
---|
381 | self._high_extrapolation_npts = 4 |
---|
382 | self._high_extrapolation_function = PowerLaw() |
---|
383 | self._high_extrapolation_power = None |
---|
384 | self._high_extrapolation_power_fitted = None |
---|
385 | |
---|
386 | # Extrapolation range |
---|
387 | self._low_q_limit = Q_MINIMUM |
---|
388 | |
---|
389 | def _get_data(self, data): |
---|
390 | """ |
---|
391 | :note: this function must be call before computing any type |
---|
392 | of invariant |
---|
393 | |
---|
394 | :return: new data = self._scale *data - self._background |
---|
395 | """ |
---|
396 | if not issubclass(data.__class__, LoaderData1D): |
---|
397 | #Process only data that inherited from DataLoader.Data_info.Data1D |
---|
398 | raise ValueError,"Data must be of type DataLoader.Data1D" |
---|
399 | #from copy import deepcopy |
---|
400 | new_data = (self._scale * data) - self._background |
---|
401 | |
---|
402 | # Check that the vector lengths are equal |
---|
403 | assert(len(new_data.x)==len(new_data.y)) |
---|
404 | |
---|
405 | # Verify that the errors are set correctly |
---|
406 | if new_data.dy is None or len(new_data.x) != len(new_data.dy) or \ |
---|
407 | (min(new_data.dy)==0 and max(new_data.dy)==0): |
---|
408 | new_data.dy = numpy.ones(len(new_data.x)) |
---|
409 | |
---|
410 | return new_data |
---|
411 | |
---|
412 | def _fit(self, model, qmin=Q_MINIMUM, qmax=Q_MAXIMUM, power=None): |
---|
413 | """ |
---|
414 | fit data with function using |
---|
415 | data = self._get_data() |
---|
416 | fx = Functor(data , function) |
---|
417 | y = data.y |
---|
418 | slope, constant = linalg.lstsq(y,fx) |
---|
419 | |
---|
420 | :param qmin: data first q value to consider during the fit |
---|
421 | :param qmax: data last q value to consider during the fit |
---|
422 | :param power : power value to consider for power-law |
---|
423 | :param function: the function to use during the fit |
---|
424 | |
---|
425 | :return a: the scale of the function |
---|
426 | :return b: the other parameter of the function for guinier will be radius |
---|
427 | for power_law will be the power value |
---|
428 | """ |
---|
429 | extrapolator = Extrapolator(data=self._data, model=model) |
---|
430 | p, dp = extrapolator.fit(power=power, qmin=qmin, qmax=qmax) |
---|
431 | |
---|
432 | return model.extract_model_parameters(constant=p[1], slope=p[0], dconstant=dp[1], dslope=dp[0]) |
---|
433 | |
---|
434 | def _get_qstar(self, data): |
---|
435 | """ |
---|
436 | Compute invariant for pinhole data. |
---|
437 | This invariant is given by: :: |
---|
438 | |
---|
439 | q_star = x0**2 *y0 *dx0 +x1**2 *y1 *dx1 |
---|
440 | + ..+ xn**2 *yn *dxn |
---|
441 | |
---|
442 | where n >= len(data.x)-1 |
---|
443 | dxi = 1/2*(xi+1 - xi) + (xi - xi-1) |
---|
444 | dx0 = (x1 - x0)/2 |
---|
445 | dxn = (xn - xn-1)/2 |
---|
446 | |
---|
447 | :param data: the data to use to compute invariant. |
---|
448 | |
---|
449 | :return q_star: invariant value for pinhole data. q_star > 0 |
---|
450 | """ |
---|
451 | if len(data.x) <= 1 or len(data.y) <= 1 or len(data.x)!= len(data.y): |
---|
452 | msg = "Length x and y must be equal" |
---|
453 | msg += " and greater than 1; got x=%s, y=%s"%(len(data.x), len(data.y)) |
---|
454 | raise ValueError, msg |
---|
455 | else: |
---|
456 | n = len(data.x)- 1 |
---|
457 | #compute the first delta q |
---|
458 | dx0 = (data.x[1] - data.x[0])/2 |
---|
459 | #compute the last delta q |
---|
460 | dxn = (data.x[n] - data.x[n-1])/2 |
---|
461 | sum = 0 |
---|
462 | sum += data.x[0] * data.x[0] * data.y[0] * dx0 |
---|
463 | sum += data.x[n] * data.x[n] * data.y[n] * dxn |
---|
464 | |
---|
465 | if len(data.x) == 2: |
---|
466 | return sum |
---|
467 | else: |
---|
468 | #iterate between for element different from the first and the last |
---|
469 | for i in xrange(1, n-1): |
---|
470 | dxi = (data.x[i+1] - data.x[i-1])/2 |
---|
471 | sum += data.x[i] * data.x[i] * data.y[i] * dxi |
---|
472 | return sum |
---|
473 | |
---|
474 | def _get_qstar_uncertainty(self, data): |
---|
475 | """ |
---|
476 | Compute invariant uncertainty with with pinhole data. |
---|
477 | This uncertainty is given as follow: :: |
---|
478 | |
---|
479 | dq_star = math.sqrt[(x0**2*(dy0)*dx0)**2 + |
---|
480 | (x1**2 *(dy1)*dx1)**2 + ..+ (xn**2 *(dyn)*dxn)**2 ] |
---|
481 | where n >= len(data.x)-1 |
---|
482 | dxi = 1/2*(xi+1 - xi) + (xi - xi-1) |
---|
483 | dx0 = (x1 - x0)/2 |
---|
484 | dxn = (xn - xn-1)/2 |
---|
485 | dyn: error on dy |
---|
486 | |
---|
487 | :param data: |
---|
488 | :note: if data doesn't contain dy assume dy= math.sqrt(data.y) |
---|
489 | """ |
---|
490 | if len(data.x) <= 1 or len(data.y) <= 1 or \ |
---|
491 | len(data.x) != len(data.y) or \ |
---|
492 | (data.dy is not None and (len(data.dy) != len(data.y))): |
---|
493 | msg = "Length of data.x and data.y must be equal" |
---|
494 | msg += " and greater than 1; got x=%s, y=%s"%(len(data.x), |
---|
495 | len(data.y)) |
---|
496 | raise ValueError, msg |
---|
497 | else: |
---|
498 | #Create error for data without dy error |
---|
499 | if data.dy is None: |
---|
500 | dy = math.sqrt(y) |
---|
501 | else: |
---|
502 | dy = data.dy |
---|
503 | |
---|
504 | n = len(data.x) - 1 |
---|
505 | #compute the first delta |
---|
506 | dx0 = (data.x[1] - data.x[0])/2 |
---|
507 | #compute the last delta |
---|
508 | dxn= (data.x[n] - data.x[n-1])/2 |
---|
509 | sum = 0 |
---|
510 | sum += (data.x[0] * data.x[0] * dy[0] * dx0)**2 |
---|
511 | sum += (data.x[n] * data.x[n] * dy[n] * dxn)**2 |
---|
512 | if len(data.x) == 2: |
---|
513 | return math.sqrt(sum) |
---|
514 | else: |
---|
515 | #iterate between for element different from the first and the last |
---|
516 | for i in xrange(1, n-1): |
---|
517 | dxi = (data.x[i+1] - data.x[i-1])/2 |
---|
518 | sum += (data.x[i] * data.x[i] * dy[i] * dxi)**2 |
---|
519 | return math.sqrt(sum) |
---|
520 | |
---|
521 | def _get_extrapolated_data(self, model, npts=INTEGRATION_NSTEPS, |
---|
522 | q_start=Q_MINIMUM, q_end=Q_MAXIMUM): |
---|
523 | """ |
---|
524 | :return: extrapolate data create from data |
---|
525 | """ |
---|
526 | #create new Data1D to compute the invariant |
---|
527 | q = numpy.linspace(start=q_start, |
---|
528 | stop=q_end, |
---|
529 | num=npts, |
---|
530 | endpoint=True) |
---|
531 | iq = model.evaluate_model(q) |
---|
532 | diq = model.evaluate_model_errors(q) |
---|
533 | |
---|
534 | result_data = LoaderData1D(x=q, y=iq, dy=diq) |
---|
535 | return result_data |
---|
536 | |
---|
537 | def get_data(self): |
---|
538 | """ |
---|
539 | :return: self._data |
---|
540 | """ |
---|
541 | return self._data |
---|
542 | |
---|
543 | def get_extrapolation_power(self, range='high'): |
---|
544 | """ |
---|
545 | :return: the fitted power for power law function for a given extrapolation range |
---|
546 | """ |
---|
547 | if range == 'low': |
---|
548 | return self._low_extrapolation_power_fitted |
---|
549 | return self._high_extrapolation_power_fitted |
---|
550 | |
---|
551 | def get_qstar_low(self): |
---|
552 | """ |
---|
553 | Compute the invariant for extrapolated data at low q range. |
---|
554 | |
---|
555 | Implementation: |
---|
556 | data = self._get_extra_data_low() |
---|
557 | return self._get_qstar() |
---|
558 | |
---|
559 | :return q_star: the invariant for data extrapolated at low q. |
---|
560 | """ |
---|
561 | # Data boundaries for fitting |
---|
562 | qmin = self._data.x[0] |
---|
563 | qmax = self._data.x[self._low_extrapolation_npts - 1] |
---|
564 | |
---|
565 | # Extrapolate the low-Q data |
---|
566 | p, dp = self._fit(model=self._low_extrapolation_function, |
---|
567 | qmin=qmin, |
---|
568 | qmax=qmax, |
---|
569 | power=self._low_extrapolation_power) |
---|
570 | self._low_extrapolation_power_fitted = p[0] |
---|
571 | |
---|
572 | # Distribution starting point |
---|
573 | self._low_q_limit = Q_MINIMUM |
---|
574 | if Q_MINIMUM >= qmin: |
---|
575 | self._low_q_limit = qmin/10 |
---|
576 | |
---|
577 | data = self._get_extrapolated_data(model=self._low_extrapolation_function, |
---|
578 | npts=INTEGRATION_NSTEPS, |
---|
579 | q_start=self._low_q_limit, q_end=qmin) |
---|
580 | |
---|
581 | # Systematic error |
---|
582 | # If we have smearing, the shape of the I(q) distribution at low Q will |
---|
583 | # may not be a Guinier or simple power law. The following is a conservative |
---|
584 | # estimation for the systematic error. |
---|
585 | err = qmin*qmin*math.fabs((qmin-self._low_q_limit)*(data.y[0] - data.y[INTEGRATION_NSTEPS-1])) |
---|
586 | return self._get_qstar(data), self._get_qstar_uncertainty(data)+err |
---|
587 | |
---|
588 | def get_qstar_high(self): |
---|
589 | """ |
---|
590 | Compute the invariant for extrapolated data at high q range. |
---|
591 | |
---|
592 | Implementation: |
---|
593 | data = self._get_extra_data_high() |
---|
594 | return self._get_qstar() |
---|
595 | |
---|
596 | :return q_star: the invariant for data extrapolated at high q. |
---|
597 | """ |
---|
598 | # Data boundaries for fitting |
---|
599 | x_len = len(self._data.x) - 1 |
---|
600 | qmin = self._data.x[x_len - (self._high_extrapolation_npts - 1)] |
---|
601 | qmax = self._data.x[x_len] |
---|
602 | |
---|
603 | # fit the data with a model to get the appropriate parameters |
---|
604 | p, dp = self._fit(model=self._high_extrapolation_function, |
---|
605 | qmin=qmin, |
---|
606 | qmax=qmax, |
---|
607 | power=self._high_extrapolation_power) |
---|
608 | self._high_extrapolation_power_fitted = p[0] |
---|
609 | |
---|
610 | #create new Data1D to compute the invariant |
---|
611 | data = self._get_extrapolated_data(model=self._high_extrapolation_function, |
---|
612 | npts=INTEGRATION_NSTEPS, |
---|
613 | q_start=qmax, q_end=Q_MAXIMUM) |
---|
614 | |
---|
615 | return self._get_qstar(data), self._get_qstar_uncertainty(data) |
---|
616 | |
---|
617 | def get_extra_data_low(self, npts_in=None, q_start=None, npts=20): |
---|
618 | """ |
---|
619 | Returns the extrapolated data used for the loew-Q invariant calculation. |
---|
620 | By default, the distribution will cover the data points used for the |
---|
621 | extrapolation. The number of overlap points is a parameter (npts_in). |
---|
622 | By default, the maximum q-value of the distribution will be |
---|
623 | the minimum q-value used when extrapolating for the purpose of the |
---|
624 | invariant calculation. |
---|
625 | |
---|
626 | :param npts_in: number of data points for which the extrapolated data overlap |
---|
627 | :param q_start: is the minimum value to uses for extrapolated data |
---|
628 | :param npts: the number of points in the extrapolated distribution |
---|
629 | |
---|
630 | """ |
---|
631 | # Get extrapolation range |
---|
632 | if q_start is None: |
---|
633 | q_start = self._low_q_limit |
---|
634 | |
---|
635 | if npts_in is None: |
---|
636 | npts_in = self._low_extrapolation_npts |
---|
637 | q_end = self._data.x[max(0, npts_in-1)] |
---|
638 | |
---|
639 | if q_start >= q_end: |
---|
640 | return numpy.zeros(0), numpy.zeros(0) |
---|
641 | |
---|
642 | return self._get_extrapolated_data(model=self._low_extrapolation_function, |
---|
643 | npts=npts, |
---|
644 | q_start=q_start, q_end=q_end) |
---|
645 | |
---|
646 | def get_extra_data_high(self, npts_in=None, q_end=Q_MAXIMUM, npts=20): |
---|
647 | """ |
---|
648 | Returns the extrapolated data used for the high-Q invariant calculation. |
---|
649 | By default, the distribution will cover the data points used for the |
---|
650 | extrapolation. The number of overlap points is a parameter (npts_in). |
---|
651 | By default, the maximum q-value of the distribution will be Q_MAXIMUM, |
---|
652 | the maximum q-value used when extrapolating for the purpose of the |
---|
653 | invariant calculation. |
---|
654 | |
---|
655 | :param npts_in: number of data points for which the extrapolated data overlap |
---|
656 | :param q_end: is the maximum value to uses for extrapolated data |
---|
657 | :param npts: the number of points in the extrapolated distribution |
---|
658 | """ |
---|
659 | # Get extrapolation range |
---|
660 | if npts_in is None: |
---|
661 | npts_in = self._high_extrapolation_npts |
---|
662 | _npts = len(self._data.x) |
---|
663 | q_start = self._data.x[min(_npts, _npts-npts_in)] |
---|
664 | |
---|
665 | if q_start >= q_end: |
---|
666 | return numpy.zeros(0), numpy.zeros(0) |
---|
667 | |
---|
668 | return self._get_extrapolated_data(model=self._high_extrapolation_function, |
---|
669 | npts=npts, |
---|
670 | q_start=q_start, q_end=q_end) |
---|
671 | |
---|
672 | def set_extrapolation(self, range, npts=4, function=None, power=None): |
---|
673 | """ |
---|
674 | Set the extrapolation parameters for the high or low Q-range. |
---|
675 | Note that this does not turn extrapolation on or off. |
---|
676 | |
---|
677 | :param range: a keyword set the type of extrapolation . type string |
---|
678 | :param npts: the numbers of q points of data to consider for extrapolation |
---|
679 | :param function: a keyword to select the function to use for extrapolation. |
---|
680 | of type string. |
---|
681 | :param power: an power to apply power_low function |
---|
682 | |
---|
683 | """ |
---|
684 | range = range.lower() |
---|
685 | if range not in ['high', 'low']: |
---|
686 | raise ValueError, "Extrapolation range should be 'high' or 'low'" |
---|
687 | function = function.lower() |
---|
688 | if function not in ['power_law', 'guinier']: |
---|
689 | raise ValueError, "Extrapolation function should be 'guinier' or 'power_law'" |
---|
690 | |
---|
691 | if range == 'high': |
---|
692 | if function != 'power_law': |
---|
693 | raise ValueError, "Extrapolation only allows a power law at high Q" |
---|
694 | self._high_extrapolation_npts = npts |
---|
695 | self._high_extrapolation_power = power |
---|
696 | self._high_extrapolation_power_fitted = power |
---|
697 | else: |
---|
698 | if function == 'power_law': |
---|
699 | self._low_extrapolation_function = PowerLaw() |
---|
700 | else: |
---|
701 | self._low_extrapolation_function = Guinier() |
---|
702 | self._low_extrapolation_npts = npts |
---|
703 | self._low_extrapolation_power = power |
---|
704 | self._low_extrapolation_power_fitted = power |
---|
705 | |
---|
706 | def get_qstar(self, extrapolation=None): |
---|
707 | """ |
---|
708 | Compute the invariant of the local copy of data. |
---|
709 | |
---|
710 | :param extrapolation: string to apply optional extrapolation |
---|
711 | |
---|
712 | :return q_star: invariant of the data within data's q range |
---|
713 | |
---|
714 | :warning: When using setting data to Data1D , the user is responsible of |
---|
715 | checking that the scale and the background are properly apply to the data |
---|
716 | |
---|
717 | """ |
---|
718 | self._qstar = self._get_qstar(self._data) |
---|
719 | self._qstar_err = self._get_qstar_uncertainty(self._data) |
---|
720 | |
---|
721 | if extrapolation is None: |
---|
722 | return self._qstar |
---|
723 | |
---|
724 | # Compute invariant plus invariant of extrapolated data |
---|
725 | extrapolation = extrapolation.lower() |
---|
726 | if extrapolation == "low": |
---|
727 | qs_low, dqs_low = self.get_qstar_low() |
---|
728 | qs_hi, dqs_hi = 0, 0 |
---|
729 | |
---|
730 | elif extrapolation == "high": |
---|
731 | qs_low, dqs_low = 0, 0 |
---|
732 | qs_hi, dqs_hi = self.get_qstar_high() |
---|
733 | |
---|
734 | elif extrapolation == "both": |
---|
735 | qs_low, dqs_low = self.get_qstar_low() |
---|
736 | qs_hi, dqs_hi = self.get_qstar_high() |
---|
737 | |
---|
738 | self._qstar += qs_low + qs_hi |
---|
739 | self._qstar_err = math.sqrt(self._qstar_err*self._qstar_err \ |
---|
740 | + dqs_low*dqs_low + dqs_hi*dqs_hi) |
---|
741 | |
---|
742 | return self._qstar |
---|
743 | |
---|
744 | def get_surface(self, contrast, porod_const, extrapolation=None): |
---|
745 | """ |
---|
746 | Compute the specific surface from the data. |
---|
747 | |
---|
748 | Implementation:: |
---|
749 | |
---|
750 | V = self.get_volume_fraction(contrast, extrapolation) |
---|
751 | |
---|
752 | Compute the surface given by: |
---|
753 | surface = (2*pi *V(1- V)*porod_const)/ q_star |
---|
754 | |
---|
755 | :param contrast: contrast value to compute the volume |
---|
756 | :param porod_const: Porod constant to compute the surface |
---|
757 | :param extrapolation: string to apply optional extrapolation |
---|
758 | |
---|
759 | :return: specific surface |
---|
760 | """ |
---|
761 | # Compute the volume |
---|
762 | volume = self.get_volume_fraction(contrast, extrapolation) |
---|
763 | return 2 * math.pi * volume *(1 - volume) * float(porod_const)/self._qstar |
---|
764 | |
---|
765 | def get_volume_fraction(self, contrast, extrapolation=None): |
---|
766 | """ |
---|
767 | Compute volume fraction is deduced as follow: :: |
---|
768 | |
---|
769 | q_star = 2*(pi*contrast)**2* volume( 1- volume) |
---|
770 | for k = 10^(-8)*q_star/(2*(pi*|contrast|)**2) |
---|
771 | we get 2 values of volume: |
---|
772 | with 1 - 4 * k >= 0 |
---|
773 | volume1 = (1- sqrt(1- 4*k))/2 |
---|
774 | volume2 = (1+ sqrt(1- 4*k))/2 |
---|
775 | |
---|
776 | q_star: the invariant value included extrapolation is applied |
---|
777 | unit 1/A^(3)*1/cm |
---|
778 | q_star = self.get_qstar() |
---|
779 | |
---|
780 | the result returned will be 0 <= volume <= 1 |
---|
781 | |
---|
782 | :param contrast: contrast value provides by the user of type float. |
---|
783 | contrast unit is 1/A^(2)= 10^(16)cm^(2) |
---|
784 | :param extrapolation: string to apply optional extrapolation |
---|
785 | |
---|
786 | :return: volume fraction |
---|
787 | |
---|
788 | :note: volume fraction must have no unit |
---|
789 | """ |
---|
790 | if contrast <= 0: |
---|
791 | raise ValueError, "The contrast parameter must be greater than zero" |
---|
792 | |
---|
793 | # Make sure Q star is up to date |
---|
794 | self.get_qstar(extrapolation) |
---|
795 | |
---|
796 | if self._qstar <= 0: |
---|
797 | raise RuntimeError, "Invalid invariant: Invariant Q* must be greater than zero" |
---|
798 | |
---|
799 | # Compute intermediate constant |
---|
800 | k = 1.e-8 * self._qstar/(2 * (math.pi * math.fabs(float(contrast)))**2) |
---|
801 | # Check discriminant value |
---|
802 | discrim = 1 - 4 * k |
---|
803 | |
---|
804 | # Compute volume fraction |
---|
805 | if discrim < 0: |
---|
806 | raise RuntimeError, "Could not compute the volume fraction: negative discriminant" |
---|
807 | elif discrim == 0: |
---|
808 | return 1/2 |
---|
809 | else: |
---|
810 | volume1 = 0.5 * (1 - math.sqrt(discrim)) |
---|
811 | volume2 = 0.5 * (1 + math.sqrt(discrim)) |
---|
812 | |
---|
813 | if 0 <= volume1 and volume1 <= 1: |
---|
814 | return volume1 |
---|
815 | elif 0 <= volume2 and volume2 <= 1: |
---|
816 | return volume2 |
---|
817 | raise RuntimeError, "Could not compute the volume fraction: inconsistent results" |
---|
818 | |
---|
819 | def get_qstar_with_error(self, extrapolation=None): |
---|
820 | """ |
---|
821 | Compute the invariant uncertainty. |
---|
822 | This uncertainty computation depends on whether or not the data is |
---|
823 | smeared. |
---|
824 | |
---|
825 | :param extrapolation: string to apply optional extrapolation |
---|
826 | |
---|
827 | :return: invariant, the invariant uncertainty |
---|
828 | """ |
---|
829 | self.get_qstar(extrapolation) |
---|
830 | return self._qstar, self._qstar_err |
---|
831 | |
---|
832 | def get_volume_fraction_with_error(self, contrast, extrapolation=None): |
---|
833 | """ |
---|
834 | Compute uncertainty on volume value as well as the volume fraction |
---|
835 | This uncertainty is given by the following equation: :: |
---|
836 | |
---|
837 | dV = 0.5 * (4*k* dq_star) /(2* math.sqrt(1-k* q_star)) |
---|
838 | |
---|
839 | for k = 10^(-8)*q_star/(2*(pi*|contrast|)**2) |
---|
840 | |
---|
841 | q_star: the invariant value including extrapolated value if existing |
---|
842 | dq_star: the invariant uncertainty |
---|
843 | dV: the volume uncertainty |
---|
844 | |
---|
845 | The uncertainty will be set to -1 if it can't be computed. |
---|
846 | |
---|
847 | :param contrast: contrast value |
---|
848 | :param extrapolation: string to apply optional extrapolation |
---|
849 | |
---|
850 | :return: V, dV = volume fraction, error on volume fraction |
---|
851 | """ |
---|
852 | volume = self.get_volume_fraction(contrast, extrapolation) |
---|
853 | |
---|
854 | # Compute error |
---|
855 | k = 1.e-8 * self._qstar /(2 * (math.pi* math.fabs(float(contrast)))**2) |
---|
856 | # Check value inside the sqrt function |
---|
857 | value = 1 - k * self._qstar |
---|
858 | if (value) <= 0: |
---|
859 | uncertainty = -1 |
---|
860 | # Compute uncertainty |
---|
861 | uncertainty = math.fabs((0.5 * 4 * k * self._qstar_err)/(2 * math.sqrt(1 - k * self._qstar))) |
---|
862 | |
---|
863 | return volume, uncertainty |
---|
864 | |
---|
865 | def get_surface_with_error(self, contrast, porod_const, extrapolation=None): |
---|
866 | """ |
---|
867 | Compute uncertainty of the surface value as well as the surface value. |
---|
868 | The uncertainty is given as follow: :: |
---|
869 | |
---|
870 | dS = porod_const *2*pi[( dV -2*V*dV)/q_star |
---|
871 | + dq_star(v-v**2) |
---|
872 | |
---|
873 | q_star: the invariant value |
---|
874 | dq_star: the invariant uncertainty |
---|
875 | V: the volume fraction value |
---|
876 | dV: the volume uncertainty |
---|
877 | |
---|
878 | :param contrast: contrast value |
---|
879 | :param porod_const: porod constant value |
---|
880 | :param extrapolation: string to apply optional extrapolation |
---|
881 | |
---|
882 | :return S, dS: the surface, with its uncertainty |
---|
883 | """ |
---|
884 | # We get the volume fraction, with error |
---|
885 | # get_volume_fraction_with_error calls get_volume_fraction |
---|
886 | # get_volume_fraction calls get_qstar |
---|
887 | # which computes Qstar and dQstar |
---|
888 | v, dv = self.get_volume_fraction_with_error(contrast, extrapolation) |
---|
889 | |
---|
890 | s = self.get_surface(contrast=contrast, porod_const=porod_const, |
---|
891 | extrapolation=extrapolation) |
---|
892 | ds = porod_const * 2 * math.pi * (( dv - 2 * v * dv)/ self._qstar\ |
---|
893 | + self._qstar_err * ( v - v**2)) |
---|
894 | |
---|
895 | return s, ds |
---|