source: sasview/sansmodels/src/sans/models/MultiplicationModel.py @ 2eaaf1a

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 2eaaf1a was 1affe64, checked in by Jae Cho <jhjcho@…>, 15 years ago

cleaned some codes and added doc.s

  • Property mode set to 100644
File size: 7.5 KB
Line 
1
2from sans.models.BaseComponent import BaseComponent
3import numpy, math
4import copy
5from sans.models.pluginmodel import Model1DPlugin
6class MultiplicationModel(BaseComponent):
7    """
8        Use for P(Q)*S(Q); function call must be in the order of P(Q) and then S(Q):
9        The model parameters are combined from both models, P(Q) and S(Q), except 'effective_radius' of S(Q)
10        which will be calculated from P(Q) via calculate_ER().
11        The polydispersion is applicable only to P(Q), not to S(Q).
12        Note: P(Q) refers to 'form factor' model while S(Q) does to 'structure factor'.
13    """
14    def __init__(self, p_model, s_model ):
15        BaseComponent.__init__(self)
16        """
17            @param p_model: form factor, P(Q)
18            @param s_model: structure factor, S(Q)
19        """
20
21        ## Setting  model name model description
22        self.description=""
23        self.name = p_model.name +" * "+ s_model.name
24        self.description= self.name+"\n"
25        self.fill_description(p_model, s_model)
26                       
27        ##models
28        self.p_model= p_model
29        self.s_model= s_model
30       
31       
32        ## dispersion
33        self._set_dispersion()
34        ## Define parameters
35        self._set_params()
36        ## Parameter details [units, min, max]
37        self._set_details()
38        #list of parameter that can be fitted
39        self._set_fixed_params() 
40        ## parameters with orientation
41        for item in self.p_model.orientation_params:
42            self.orientation_params.append(item)
43           
44        for item in self.s_model.orientation_params:
45            if not item in self.orientation_params:
46                self.orientation_params.append(item)
47               
48       
49    def _clone(self, obj):
50        """
51            Internal utility function to copy the internal
52            data members to a fresh copy.
53        """
54        obj.params     = copy.deepcopy(self.params)
55        obj.description     = copy.deepcopy(self.description)
56        obj.details    = copy.deepcopy(self.details)
57        obj.dispersion = copy.deepcopy(self.dispersion)
58        obj.p_model  = self.p_model.clone()
59        obj.s_model  = self.s_model.clone()
60       
61        return obj
62   
63   
64    def _set_dispersion(self):
65        """
66           combined the two models dispersions
67           Polydispersion should not be applied to s_model
68        """
69        ##set dispersion only from p_model
70        for name , value in self.p_model.dispersion.iteritems():
71            self.dispersion[name]= value                           
72
73    def _set_params(self):
74        """
75            Concatenate the parameters of the two models to create
76            this model parameters
77        """
78
79        for name , value in self.p_model.params.iteritems():
80            self.params[name]= value
81           
82        for name , value in self.s_model.params.iteritems():
83            #Remove the effect_radius from the (P*S) model parameters.
84            if not name in self.params.keys() and name != 'effect_radius':
85                self.params[name]= value
86           
87    def _set_details(self):
88        """
89            Concatenate details of the two models to create
90            this model details
91        """
92        for name ,detail in self.p_model.details.iteritems():
93            self.details[name]= detail
94           
95        for name , detail in self.s_model.details.iteritems():
96            if not name in self.details.keys():
97                self.details[name]= detail
98               
99    def setParam(self, name, value):
100        """
101            Set the value of a model parameter
102       
103            @param name: name of the parameter
104            @param value: value of the parameter
105        """
106
107        self._setParamHelper( name, value)
108
109        if name in self.p_model.getParamList():
110            self.p_model.setParam( name, value)
111
112        if name in self.s_model.getParamList():
113            self.s_model.setParam( name, value)
114
115        self._setParamHelper( name, value)
116       
117    def _setParamHelper(self, name, value):
118        """
119            Helper function to setparam
120        """
121        # Look for dispersion parameters
122        toks = name.split('.')
123        if len(toks)==2:
124            for item in self.dispersion.keys():
125                if item.lower()==toks[0].lower():
126                    for par in self.dispersion[item]:
127                        if par.lower() == toks[1].lower():
128                            self.dispersion[item][par] = value
129                            return
130        else:
131            # Look for standard parameter
132            for item in self.params.keys():
133                if item.lower()==name.lower():
134                    self.params[item] = value
135                    return
136           
137        raise ValueError, "Model does not contain parameter %s" % name
138             
139   
140    def _set_fixed_params(self):
141        """
142             fill the self.fixed list with the p_model fixed list
143        """
144        for item in self.p_model.fixed:
145            self.fixed.append(item)
146
147        self.fixed.sort()
148               
149               
150    def run(self, x = 0.0):
151        """ Evaluate the model
152            @param x: input q-value (float or [float, float] as [r, theta])
153            @return: (DAB value)
154        """
155
156        effective_radius = self.p_model.calculate_ER()
157        #Reset the effective_radius of s_model just before the run
158        if effective_radius != None and effective_radius != NotImplemented:
159            self.s_model.setParam('effect_radius',effective_radius)                       
160        return self.p_model.run(x)*self.s_model.run(x)
161
162    def runXY(self, x = 0.0):
163        """ Evaluate the model
164            @param x: input q-value (float or [float, float] as [qx, qy])
165            @return: DAB value
166        """
167       
168        effective_radius = self.p_model.calculate_ER()
169        #Reset the effective_radius of s_model just before the run
170        if effective_radius != None and effective_radius != NotImplemented:
171            self.s_model.setParam('effect_radius',effective_radius)         
172        return self.p_model.runXY(x)* self.s_model.runXY(x)
173
174    def set_dispersion(self, parameter, dispersion):
175        """
176            Set the dispersion object for a model parameter
177            @param parameter: name of the parameter [string]
178            @dispersion: dispersion object of type DispersionModel
179        """
180        value= None
181        try:
182            if parameter in self.p_model.dispersion.keys():
183                value= self.p_model.set_dispersion(parameter, dispersion)
184            self._set_dispersion()
185            return value
186        except:
187            raise 
188
189    def fill_description(self, p_model, s_model):
190        """
191            Fill the description for P(Q)*S(Q)
192        """
193        description = ""
194        description += "Note:1) The effect_radius (effective radius) of %s \n"% (s_model.name)
195        description +="             is automatically calculated from size parameters (radius...).\n"
196        description += "         2) For non-spherical shape, this approximation is valid \n"
197        description += "            only for limited systems. Thus, use it at your own risk.\n"
198        description +="See %s description and %s description \n"%( p_model.name, s_model.name )
199        description += "        for details of individual models."
200        self.description += description
201   
Note: See TracBrowser for help on using the repository browser.