[a9d5684] | 1 | """ |
---|
[dd5bf63] | 2 | This module is used to fit a set of x,y data to a model passed to it. It is |
---|
| 3 | used to calculate the slope and intercepts for the linearized fits. Two things |
---|
| 4 | should be noted: |
---|
| 5 | |
---|
| 6 | First, this fitting module uses the NLLSQ module of SciPy rather than a linear |
---|
| 7 | fit. This along with a few other modules could probably be removed if we |
---|
| 8 | move to a linear regression approach. |
---|
| 9 | |
---|
| 10 | Second, this infrastructure does not allow for resolution smearing of the |
---|
| 11 | the models. Hence the results are not that accurate even for pinhole |
---|
| 12 | collimation of SANS but may be good for SAXS. It is completely wrong for |
---|
| 13 | slit smeared data. |
---|
| 14 | |
---|
[a9d5684] | 15 | """ |
---|
| 16 | from scipy import optimize |
---|
| 17 | |
---|
| 18 | |
---|
[2df0b74] | 19 | class Parameter(object): |
---|
[a9d5684] | 20 | """ |
---|
[dd5bf63] | 21 | Class to handle model parameters - sets the parameters and their |
---|
| 22 | initial value from the model based to it. |
---|
[a9d5684] | 23 | """ |
---|
| 24 | def __init__(self, model, name, value=None): |
---|
[2df0b74] | 25 | self.model = model |
---|
| 26 | self.name = name |
---|
| 27 | if not value == None: |
---|
| 28 | self.model.setParam(self.name, value) |
---|
| 29 | |
---|
[a9d5684] | 30 | def set(self, value): |
---|
| 31 | """ |
---|
| 32 | Set the value of the parameter |
---|
| 33 | """ |
---|
| 34 | self.model.setParam(self.name, value) |
---|
| 35 | |
---|
| 36 | def __call__(self): |
---|
| 37 | """ |
---|
| 38 | Return the current value of the parameter |
---|
| 39 | """ |
---|
| 40 | return self.model.getParam(self.name) |
---|
[2df0b74] | 41 | |
---|
| 42 | |
---|
| 43 | def sasfit(model, pars, x, y, err_y, qmin=None, qmax=None): |
---|
[a9d5684] | 44 | """ |
---|
| 45 | Fit function |
---|
[2df0b74] | 46 | |
---|
[79492222] | 47 | :param model: sas model object |
---|
[a9d5684] | 48 | :param pars: list of parameters |
---|
| 49 | :param x: vector of x data |
---|
| 50 | :param y: vector of y data |
---|
| 51 | :param err_y: vector of y errors |
---|
| 52 | """ |
---|
| 53 | def f(params): |
---|
| 54 | """ |
---|
| 55 | Calculates the vector of residuals for each point |
---|
| 56 | in y for a given set of input parameters. |
---|
[2df0b74] | 57 | |
---|
[a9d5684] | 58 | :param params: list of parameter values |
---|
| 59 | :return: vector of residuals |
---|
| 60 | """ |
---|
| 61 | i = 0 |
---|
| 62 | for p in pars: |
---|
| 63 | p.set(params[i]) |
---|
| 64 | i += 1 |
---|
[2df0b74] | 65 | |
---|
[a9d5684] | 66 | residuals = [] |
---|
| 67 | for j in range(len(x)): |
---|
| 68 | if x[j] >= qmin and x[j] <= qmax: |
---|
| 69 | residuals.append((y[j] - model.runXY(x[j])) / err_y[j]) |
---|
| 70 | return residuals |
---|
[2df0b74] | 71 | |
---|
[a9d5684] | 72 | def chi2(params): |
---|
| 73 | """ |
---|
| 74 | Calculates chi^2 |
---|
[2df0b74] | 75 | |
---|
[a9d5684] | 76 | :param params: list of parameter values |
---|
[2df0b74] | 77 | |
---|
[a9d5684] | 78 | :return: chi^2 |
---|
[2df0b74] | 79 | |
---|
[a9d5684] | 80 | """ |
---|
| 81 | sum = 0 |
---|
| 82 | res = f(params) |
---|
| 83 | for item in res: |
---|
| 84 | sum += item * item |
---|
| 85 | return sum |
---|
[2df0b74] | 86 | |
---|
[a9d5684] | 87 | p = [param() for param in pars] |
---|
| 88 | out, cov_x, info, mesg, success = optimize.leastsq(f, p, full_output=1) |
---|
| 89 | # Calculate chi squared |
---|
| 90 | if len(pars) > 1: |
---|
| 91 | chisqr = chi2(out) |
---|
| 92 | elif len(pars) == 1: |
---|
| 93 | chisqr = chi2([out]) |
---|
[2df0b74] | 94 | |
---|
[a9d5684] | 95 | return chisqr, out, cov_x |
---|
| 96 | |
---|
| 97 | |
---|
| 98 | def calcCommandline(event): |
---|
[2df0b74] | 99 | # Testing implementation |
---|
[a9d5684] | 100 | # Fit a Line model |
---|
| 101 | from LineModel import LineModel |
---|
| 102 | line = LineModel() |
---|
| 103 | cstA = Parameter(line, 'A', event.cstA) |
---|
| 104 | cstB = Parameter(line, 'B', event.cstB) |
---|
| 105 | y = line.run() |
---|
[b9a5f0e] | 106 | chisqr, out, cov = sasfit(line, [cstA, cstB], event.x, y, 0) |
---|
[a9d5684] | 107 | # print "Output parameters:", out |
---|
| 108 | print "The right answer is [70.0, 1.0]" |
---|
| 109 | print chisqr, out, cov |
---|