source: sasview/park_integration/ParkFitting.py @ 52d3fd1

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 52d3fd1 was b2f25dc5, checked in by Gervaise Alina <gervyh@…>, 14 years ago

working on pylint

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