source: sasview/sansmodels/src/sans/models/MultiplicationModel.py @ 52b8b74

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 52b8b74 was 35aface, checked in by Jae Cho <jhjcho@…>, 14 years ago

addede new models and attr. non_fittable

  • Property mode set to 100644
File size: 10.4 KB
RevLine 
[c9636f7]1
2from sans.models.BaseComponent import BaseComponent
3import numpy, math
[a68efd1]4import copy
[3740b11]5from sans.models.pluginmodel import Model1DPlugin
[c9636f7]6class MultiplicationModel(BaseComponent):
7    """
[1affe64]8        Use for P(Q)*S(Q); function call must be in the order of P(Q) and then S(Q):
[c52f66f]9        The model parameters are combined from both models, P(Q) and S(Q), except 1) 'effect_radius' of S(Q)
10        which will be calculated from P(Q) via calculate_ER(),
11        and 2) 'scale' in P model which is synchronized w/ volfraction in S
12        then P*S is multiplied by a new param, 'scale_factor'.
[1affe64]13        The polydispersion is applicable only to P(Q), not to S(Q).
14        Note: P(Q) refers to 'form factor' model while S(Q) does to 'structure factor'.
[c9636f7]15    """
[1affe64]16    def __init__(self, p_model, s_model ):
[c9636f7]17        BaseComponent.__init__(self)
[1affe64]18        """
19            @param p_model: form factor, P(Q)
20            @param s_model: structure factor, S(Q)
21        """
[c9636f7]22
[8cfdd5e]23        ## Setting  model name model description
[996fd35]24        self.description=""
[1affe64]25        self.name = p_model.name +" * "+ s_model.name
26        self.description= self.name+"\n"
27        self.fill_description(p_model, s_model)
[c52f66f]28
29        ## Define parameters
30        self.params = {}
31
32        ## Parameter details [units, min, max]
33        self.details = {}
[35aface]34       
35        # non-fittable parameters
36        self.non_fittable = p_model.non_fittable   
37           
[1affe64]38        ##models
39        self.p_model= p_model
40        self.s_model= s_model
[5eb9154]41       
42       
[c9636f7]43        ## dispersion
44        self._set_dispersion()
45        ## Define parameters
46        self._set_params()
[c52f66f]47        ## New parameter:Scaling factor
48        self.params['scale_factor'] = 1
49       
[c9636f7]50        ## Parameter details [units, min, max]
51        self._set_details()
[c52f66f]52        self.details['scale_factor'] = ['',     None, None]
53       
[c9636f7]54        #list of parameter that can be fitted
55        self._set_fixed_params() 
[5fc8e22]56        ## parameters with orientation
[1affe64]57        for item in self.p_model.orientation_params:
[5fc8e22]58            self.orientation_params.append(item)
59           
[1affe64]60        for item in self.s_model.orientation_params:
[5fc8e22]61            if not item in self.orientation_params:
[8b677ec]62                self.orientation_params.append(item)
[35aface]63        # get multiplicity if model provide it, else 1.
64        try:
65            multiplicity = p_model.multiplicity
66        except:
67            multiplicity = 1
68        ## functional multiplicity of the model
69        self.multiplicity = multiplicity     
[5eb9154]70       
[a68efd1]71    def _clone(self, obj):
72        """
73            Internal utility function to copy the internal
74            data members to a fresh copy.
75        """
76        obj.params     = copy.deepcopy(self.params)
77        obj.description     = copy.deepcopy(self.description)
78        obj.details    = copy.deepcopy(self.details)
79        obj.dispersion = copy.deepcopy(self.dispersion)
[1affe64]80        obj.p_model  = self.p_model.clone()
81        obj.s_model  = self.s_model.clone()
[fe9c19b4]82        #obj = copy.deepcopy(self)
[a68efd1]83        return obj
84   
85   
[c9636f7]86    def _set_dispersion(self):
87        """
88           combined the two models dispersions
[1affe64]89           Polydispersion should not be applied to s_model
[c9636f7]90        """
[1affe64]91        ##set dispersion only from p_model
92        for name , value in self.p_model.dispersion.iteritems():
[a1b2471]93            self.dispersion[name]= value
94                                     
95    def getProfile(self):
96        """
97        Get SLD profile of p_model if exists
98       
99        : return: (r, beta) where r is a list of radius of the transition points
100                beta is a list of the corresponding SLD values
101        : Note: This works only for func_shell# = 2 (exp function).
102        """
103        try:
104            x,y = self.p_model.getProfile()
105        except:
106            x = None
107            y = None
108           
109        return x, y
110   
[c9636f7]111    def _set_params(self):
112        """
113            Concatenate the parameters of the two models to create
114            this model parameters
115        """
[1affe64]116
117        for name , value in self.p_model.params.iteritems():
[c52f66f]118            if not name in self.params.keys() and name != 'scale':
119                self.params[name]= value
[3740b11]120           
[1affe64]121        for name , value in self.s_model.params.iteritems():
122            #Remove the effect_radius from the (P*S) model parameters.
123            if not name in self.params.keys() and name != 'effect_radius':
124                self.params[name]= value
[c52f66f]125               
126        # Set "scale and effec_radius to P and S model as initializing
127        # since run P*S comes from P and S separately.
128        self._set_scale_factor()
129        self._set_effect_radius()       
[c9636f7]130           
131    def _set_details(self):
132        """
133            Concatenate details of the two models to create
134            this model details
135        """
[1affe64]136        for name ,detail in self.p_model.details.iteritems():
[c52f66f]137            if name != 'scale':
138                self.details[name]= detail
[c9636f7]139           
[1affe64]140        for name , detail in self.s_model.details.iteritems():
[c52f66f]141            if not name in self.details.keys() or name != 'effect_radius':
[1affe64]142                self.details[name]= detail
[c52f66f]143   
144    def _set_scale_factor(self):
145        """
146            Set scale=volfraction to P model
147        """
148        value = self.params['volfraction']
149        if value != None: 
150            self.p_model.setParam( 'scale', value)
151           
152           
153    def _set_effect_radius(self):
154        """
155            Set effective radius to S(Q) model
156        """
157        effective_radius = self.p_model.calculate_ER()
158        #Reset the effective_radius of s_model just before the run
159        if effective_radius != None and effective_radius != NotImplemented:
160            self.s_model.setParam('effect_radius',effective_radius)
[c9636f7]161               
[8cfdd5e]162    def setParam(self, name, value):
163        """
164            Set the value of a model parameter
165       
166            @param name: name of the parameter
167            @param value: value of the parameter
168        """
[c52f66f]169        # set param to P*S model
[3740b11]170        self._setParamHelper( name, value)
[c52f66f]171       
172        ## setParam to p model
173        # set 'scale' in P(Q) equal to volfraction
174        if name == 'volfraction':
175            self._set_scale_factor()
176        elif name in self.p_model.getParamList():
[1affe64]177            self.p_model.setParam( name, value)
[c52f66f]178       
179        ## setParam to s model
180        # This is a little bit abundant: Todo: find better way         
181        self._set_effect_radius()
[1affe64]182        if name in self.s_model.getParamList():
183            self.s_model.setParam( name, value)
[c52f66f]184           
[5eb9154]185
[c52f66f]186        #self._setParamHelper( name, value)
[8cfdd5e]187       
188    def _setParamHelper(self, name, value):
189        """
190            Helper function to setparam
191        """
192        # Look for dispersion parameters
193        toks = name.split('.')
194        if len(toks)==2:
195            for item in self.dispersion.keys():
196                if item.lower()==toks[0].lower():
197                    for par in self.dispersion[item]:
198                        if par.lower() == toks[1].lower():
199                            self.dispersion[item][par] = value
200                            return
201        else:
202            # Look for standard parameter
203            for item in self.params.keys():
204                if item.lower()==name.lower():
205                    self.params[item] = value
206                    return
207           
208        raise ValueError, "Model does not contain parameter %s" % name
209             
210   
[c9636f7]211    def _set_fixed_params(self):
212        """
[1affe64]213             fill the self.fixed list with the p_model fixed list
[c9636f7]214        """
[1affe64]215        for item in self.p_model.fixed:
[c9636f7]216            self.fixed.append(item)
[8b677ec]217
[c9636f7]218        self.fixed.sort()
[5eb9154]219               
220               
[c9636f7]221    def run(self, x = 0.0):
222        """ Evaluate the model
223            @param x: input q-value (float or [float, float] as [r, theta])
[35aface]224            @return: (scattering function value)
[c9636f7]225        """
[c52f66f]226        # set effective radius and scaling factor before run
227        self._set_effect_radius()
228        self._set_scale_factor()
229        return self.params['scale_factor']*self.p_model.run(x)*self.s_model.run(x)
[1affe64]230
[c9636f7]231    def runXY(self, x = 0.0):
232        """ Evaluate the model
233            @param x: input q-value (float or [float, float] as [qx, qy])
[35aface]234            @return: scattering function value
[c52f66f]235        """ 
236        # set effective radius and scaling factor before run
237        self._set_effect_radius()
238        self._set_scale_factor()
239        return self.params['scale_factor']*self.p_model.runXY(x)* self.s_model.runXY(x)
[06c7fcc]240   
241    ## Now (May27,10) directly uses the model eval function
242    ## instead of the for-loop in Base Component.
243    def evalDistribution(self, x = []):
244        """ Evaluate the model in cartesian coordinates
245            @param x: input q[], or [qx[], qy[]]
246            @return: scattering function P(q[])
247        """
248        # set effective radius and scaling factor before run
249        self._set_effect_radius()
250        self._set_scale_factor()
251        return self.params['scale_factor']*self.p_model.evalDistribution(x)* self.s_model.evalDistribution(x)
[5eb9154]252
[c9636f7]253    def set_dispersion(self, parameter, dispersion):
254        """
255            Set the dispersion object for a model parameter
256            @param parameter: name of the parameter [string]
257            @dispersion: dispersion object of type DispersionModel
258        """
[db39b2a]259        value= None
260        try:
[1affe64]261            if parameter in self.p_model.dispersion.keys():
262                value= self.p_model.set_dispersion(parameter, dispersion)
[8077fc4]263            self._set_dispersion()
[db39b2a]264            return value
265        except:
266            raise 
[c9636f7]267
[1affe64]268    def fill_description(self, p_model, s_model):
[8b677ec]269        """
270            Fill the description for P(Q)*S(Q)
271        """
272        description = ""
[1affe64]273        description += "Note:1) The effect_radius (effective radius) of %s \n"% (s_model.name)
[8b677ec]274        description +="             is automatically calculated from size parameters (radius...).\n"
275        description += "         2) For non-spherical shape, this approximation is valid \n"
[1affe64]276        description += "            only for limited systems. Thus, use it at your own risk.\n"
277        description +="See %s description and %s description \n"%( p_model.name, s_model.name )
278        description += "        for details of individual models."
[8b677ec]279        self.description += description
[c9636f7]280   
Note: See TracBrowser for help on using the repository browser.