[aa36f96] | 1 | |
---|
| 2 | |
---|
| 3 | |
---|
[792db7d5] | 4 | """ |
---|
[aa36f96] | 5 | ParkFitting module contains SansParameter,Model,Data |
---|
| 6 | FitArrange, ParkFit,Parameter classes.All listed classes work together to perform a |
---|
| 7 | simple fit with park optimizer. |
---|
[792db7d5] | 8 | """ |
---|
[89f3b66] | 9 | #import time |
---|
| 10 | #import numpy |
---|
[7705306] | 11 | import park |
---|
[89f3b66] | 12 | from park import fit, fitresult |
---|
[7705306] | 13 | from park import assembly |
---|
[cf3b781] | 14 | from park.fitmc import FitSimplex, FitMC |
---|
[7d0c1a8] | 15 | |
---|
[61cb28d] | 16 | #from Loader import Load |
---|
[7d0c1a8] | 17 | from AbstractFitEngine import FitEngine |
---|
[d4b0687] | 18 | |
---|
[fadea71] | 19 | |
---|
[4c718654] | 20 | class ParkFit(FitEngine): |
---|
[7705306] | 21 | """ |
---|
[aa36f96] | 22 | ParkFit performs the Fit.This class can be used as follow: |
---|
| 23 | #Do the fit Park |
---|
| 24 | create an engine: engine = ParkFit() |
---|
| 25 | Use data must be of type plottable |
---|
| 26 | Use a sans model |
---|
| 27 | |
---|
| 28 | Add data with a dictionnary of FitArrangeList where Uid is a key and data |
---|
| 29 | is saved in FitArrange object. |
---|
| 30 | engine.set_data(data,Uid) |
---|
| 31 | |
---|
| 32 | Set model parameter "M1"= model.name add {model.parameter.name:value}. |
---|
| 33 | |
---|
| 34 | :note: Set_param() if used must always preceded set_model() |
---|
| 35 | for the fit to be performed. |
---|
| 36 | engine.set_param( model,"M1", {'A':2,'B':4}) |
---|
| 37 | |
---|
| 38 | Add model with a dictionnary of FitArrangeList{} where Uid is a key and model |
---|
| 39 | is save in FitArrange object. |
---|
| 40 | engine.set_model(model,Uid) |
---|
| 41 | |
---|
| 42 | engine.fit return chisqr,[model.parameter 1,2,..],[[err1....][..err2...]] |
---|
| 43 | chisqr1, out1, cov1=engine.fit({model.parameter.name:value},qmin,qmax) |
---|
| 44 | |
---|
| 45 | :note: {model.parameter.name:value} is ignored in fit function since |
---|
[792db7d5] | 46 | the user should make sure to call set_param himself. |
---|
[aa36f96] | 47 | |
---|
[7705306] | 48 | """ |
---|
[916a15f] | 49 | def __init__(self): |
---|
[792db7d5] | 50 | """ |
---|
[aa36f96] | 51 | Creates a dictionary (self.fitArrangeList={})of FitArrange elements |
---|
| 52 | with Uid as keys |
---|
[792db7d5] | 53 | """ |
---|
[89f3b66] | 54 | self.fitArrangeDict = {} |
---|
| 55 | self.paramList = [] |
---|
[37d9521] | 56 | |
---|
[ca6d914] | 57 | def createAssembly(self): |
---|
[7705306] | 58 | """ |
---|
[792db7d5] | 59 | Extract sansmodel and sansdata from self.FitArrangelist ={Uid:FitArrange} |
---|
| 60 | Create parkmodel and park data ,form a list couple of parkmodel and parkdata |
---|
| 61 | create an assembly self.problem= park.Assembly([(parkmodel,parkdata)]) |
---|
[7705306] | 62 | """ |
---|
[89f3b66] | 63 | mylist = [] |
---|
| 64 | listmodel = [] |
---|
| 65 | i = 0 |
---|
| 66 | fitproblems = [] |
---|
| 67 | for id,fproblem in self.fitArrangeDict.iteritems(): |
---|
| 68 | if fproblem.get_to_fit() == 1: |
---|
[a9e04aa] | 69 | fitproblems.append(fproblem) |
---|
[89f3b66] | 70 | if len(fitproblems) == 0: |
---|
[a9e04aa] | 71 | raise RuntimeError, "No Assembly scheduled for Park fitting." |
---|
| 72 | return |
---|
| 73 | for item in fitproblems: |
---|
| 74 | parkmodel = item.get_model() |
---|
[9e85792] | 75 | for p in parkmodel.parameterset: |
---|
[fe886ee] | 76 | ## does not allow status change for constraint parameters |
---|
[89f3b66] | 77 | if p.status != 'computed': |
---|
[aed7c57] | 78 | if p._getname()in item.pars: |
---|
[fe886ee] | 79 | ## make parameters selected for fit will be between boundaries |
---|
[89f3b66] | 80 | p.set(p.range) |
---|
[aed7c57] | 81 | else: |
---|
[89f3b66] | 82 | p.status = 'fixed' |
---|
| 83 | i += 1 |
---|
| 84 | Ldata = item.get_data() |
---|
[7d0c1a8] | 85 | #parkdata=self._concatenateData(Ldata) |
---|
[89f3b66] | 86 | parkdata = Ldata |
---|
| 87 | fitness = (parkmodel, parkdata) |
---|
[ca6d914] | 88 | mylist.append(fitness) |
---|
[89f3b66] | 89 | self.problem = park.Assembly(mylist) |
---|
[126a761] | 90 | |
---|
[89f3b66] | 91 | def fit(self, q=None, handler=None, curr_thread=None): |
---|
[7705306] | 92 | """ |
---|
[aa36f96] | 93 | Performs fit with park.fit module.It can perform fit with one model |
---|
| 94 | and a set of data, more than two fit of one model and sets of data or |
---|
| 95 | fit with more than two model associated with their set of data and constraints |
---|
| 96 | |
---|
| 97 | :param pars: Dictionary of parameter names for the model and their values. |
---|
| 98 | :param qmin: The minimum value of data's range to be fit |
---|
| 99 | :param qmax: The maximum value of data's range to be fit |
---|
| 100 | |
---|
| 101 | :note: all parameter are ignored most of the time.Are just there to keep ScipyFit |
---|
[792db7d5] | 102 | and ParkFit interface the same. |
---|
[aa36f96] | 103 | |
---|
| 104 | :return: result.fitness Value of the goodness of fit metric |
---|
| 105 | :return: result.pvec list of parameter with the best value found during fitting |
---|
| 106 | :return: result.cov Covariance matrix |
---|
| 107 | |
---|
[7705306] | 108 | """ |
---|
[ca6d914] | 109 | self.createAssembly() |
---|
[cf3b781] | 110 | localfit = FitSimplex() |
---|
| 111 | localfit.ftol = 1e-8 |
---|
[681f0dc] | 112 | |
---|
[916a15f] | 113 | # See `park.fitresult.FitHandler` for details. |
---|
[9c648c7] | 114 | fitter = FitMC(localfit=localfit, start_points=1) |
---|
[681f0dc] | 115 | if handler == None: |
---|
[89f3b66] | 116 | handler = fitresult.ConsoleUpdate(improvement_delta=0.1) |
---|
[ee5b04c] | 117 | result = fit.fit(self.problem, |
---|
[fadea71] | 118 | fitter=fitter, |
---|
[89f3b66] | 119 | handler=handler) |
---|
[8296ff5] | 120 | self.problem.all_results(result) |
---|
[89f3b66] | 121 | if result != None: |
---|
| 122 | if q != None: |
---|
[fd6b789] | 123 | q.put(result) |
---|
| 124 | return q |
---|
[48882d1] | 125 | return result |
---|
[ee5b04c] | 126 | else: |
---|
| 127 | raise ValueError, "SVD did not converge" |
---|
[aa36f96] | 128 | |
---|