Changeset 7fdb332 in sasview for sansmodels/src
- Timestamp:
- Jul 26, 2012 11:47:53 AM (12 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 48c547b
- Parents:
- f1e0c1eb
- Location:
- sansmodels/src/sans/models
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
sansmodels/src/sans/models/BaseComponent.py
r4ce74b44 r7fdb332 32 32 # string containing information about the model such as the equation 33 33 #of the given model, exception or possible use 34 self.description =''34 self.description = '' 35 35 #list of parameter that can be fitted 36 36 self.fixed = [] … … 66 66 #return self.params[str(par_name)].is_fittable() 67 67 68 def run(self, x): return NotImplemented 69 def runXY(self, x): return NotImplemented 70 def calculate_ER(self): return NotImplemented 71 def calculate_VR(self): return NotImplemented 68 def run(self, x): 69 """ 70 run 1d 71 """ 72 return NotImplemented 73 74 def runXY(self, x): 75 """ 76 run 2d 77 """ 78 return NotImplemented 79 80 def calculate_ER(self): 81 """ 82 Calculate effective radius 83 """ 84 return NotImplemented 85 86 def calculate_VR(self): 87 """ 88 Calculate volume fraction ratio 89 """ 90 return NotImplemented 91 72 92 def evalDistribution(self, qdist): 73 93 """ … … 112 132 qdist[0].__class__.__name__ != 'ndarray' or \ 113 133 qdist[1].__class__.__name__ != 'ndarray': 114 raise RuntimeError, "evalDistribution expects a list of 2 ndarrays" 134 msg = "evalDistribution expects a list of 2 ndarrays" 135 raise RuntimeError, msg 115 136 116 137 # Extract qx and qy for code clarity … … 121 142 q = numpy.sqrt(qx**2+qy**2) 122 143 # vectorize the model function runXY 123 v_model = numpy.vectorize(self.runXY, otypes=[float])144 v_model = numpy.vectorize(self.runXY, otypes=[float]) 124 145 # calculate the scattering 125 146 iq_array = v_model(q) … … 128 149 129 150 elif qdist.__class__.__name__ == 'ndarray': 130 # We have a simple 1D distribution of q-values 131 v_model = numpy.vectorize(self.runXY,otypes=[float]) 132 iq_array = v_model(qdist) 133 134 return iq_array 151 # We have a simple 1D distribution of q-values 152 v_model = numpy.vectorize(self.runXY, otypes=[float]) 153 iq_array = v_model(qdist) 154 return iq_array 135 155 136 156 else: … … 246 266 247 267 # Old-style methods that are no longer used 248 def setParamWithToken(self, name, value, token, member): return NotImplemented 249 def getParamWithToken(self, name, token, member): return NotImplemented 250 def getParamListWithToken(self, token, member): return NotImplemented 251 def __add__(self, other): raise ValueError, "Model operation are no longer supported" 252 def __sub__(self, other): raise ValueError, "Model operation are no longer supported" 253 def __mul__(self, other): raise ValueError, "Model operation are no longer supported" 254 def __div__(self, other): raise ValueError, "Model operation are no longer supported" 255 268 def setParamWithToken(self, name, value, token, member): 269 """ 270 set Param With Token 271 """ 272 return NotImplemented 273 def getParamWithToken(self, name, token, member): 274 """ 275 get Param With Token 276 """ 277 return NotImplemented 278 279 def getParamListWithToken(self, token, member): 280 """ 281 get Param List With Token 282 """ 283 return NotImplemented 284 def __add__(self, other): 285 """ 286 add 287 """ 288 raise ValueError, "Model operation are no longer supported" 289 def __sub__(self, other): 290 """ 291 sub 292 """ 293 raise ValueError, "Model operation are no longer supported" 294 def __mul__(self, other): 295 """ 296 mul 297 """ 298 raise ValueError, "Model operation are no longer supported" 299 def __div__(self, other): 300 """ 301 div 302 """ 303 raise ValueError, "Model operation are no longer supported" 304 -
sansmodels/src/sans/models/MultiplicationModel.py
ra8a55f2 r7fdb332 1 1 2 2 from sans.models.BaseComponent import BaseComponent 3 import numpy, math3 #import numpy, math 4 4 import copy 5 from sans.models.pluginmodel import Model1DPlugin5 #from sans.models.pluginmodel import Model1DPlugin 6 6 class MultiplicationModel(BaseComponent): 7 7 """ … … 17 17 BaseComponent.__init__(self) 18 18 """ 19 @param p_model: form factor, P(Q)20 @param s_model: structure factor, S(Q)19 :param p_model: form factor, P(Q) 20 :param s_model: structure factor, S(Q) 21 21 """ 22 22 23 23 ## Setting model name model description 24 self.description =""24 self.description = "" 25 25 self.name = p_model.name +" * "+ s_model.name 26 self.description= self.name +"\n"26 self.description= self.name + "\n" 27 27 self.fill_description(p_model, s_model) 28 28 … … 34 34 35 35 ##models 36 self.p_model = p_model37 self.s_model = s_model36 self.p_model = p_model 37 self.s_model = s_model 38 38 39 39 ## dispersion … … 100 100 ##set dispersion only from p_model 101 101 for name , value in self.p_model.dispersion.iteritems(): 102 self.dispersion[name] = value102 self.dispersion[name] = value 103 103 104 104 def getProfile(self): … … 111 111 """ 112 112 try: 113 x, y = self.p_model.getProfile()113 x, y = self.p_model.getProfile() 114 114 except: 115 115 x = None … … 126 126 for name , value in self.p_model.params.iteritems(): 127 127 if not name in self.params.keys() and name != 'scale': 128 self.params[name] = value128 self.params[name] = value 129 129 130 130 for name , value in self.s_model.params.iteritems(): 131 131 #Remove the effect_radius from the (P*S) model parameters. 132 132 if not name in self.params.keys() and name != 'effect_radius': 133 self.params[name] = value133 self.params[name] = value 134 134 135 135 # Set "scale and effec_radius to P and S model as initializing … … 143 143 this model details 144 144 """ 145 for name ,detail in self.p_model.details.iteritems():145 for name, detail in self.p_model.details.iteritems(): 146 146 if name != 'scale': 147 self.details[name] = detail147 self.details[name] = detail 148 148 149 149 for name , detail in self.s_model.details.iteritems(): 150 150 if not name in self.details.keys() or name != 'effect_radius': 151 self.details[name] = detail151 self.details[name] = detail 152 152 153 153 def _set_scale_factor(self): … … 159 159 factor = self.p_model.calculate_VR() 160 160 if factor == None or factor == NotImplemented or factor == 0.0: 161 val = value161 val = value 162 162 else: 163 163 val = value / factor … … 174 174 #Reset the effective_radius of s_model just before the run 175 175 if effective_radius != None and effective_radius != NotImplemented: 176 self.s_model.setParam('effect_radius', effective_radius)176 self.s_model.setParam('effect_radius', effective_radius) 177 177 178 178 def setParam(self, name, value): 179 179 """ 180 181 182 @param name: name of the parameter183 @param value: value of the parameter180 Set the value of a model parameter 181 182 :param name: name of the parameter 183 :param value: value of the parameter 184 184 """ 185 185 # set param to P*S model … … 237 237 238 238 def run(self, x = 0.0): 239 """ Evaluate the model 240 @param x: input q-value (float or [float, float] as [r, theta]) 241 @return: (scattering function value) 239 """ 240 Evaluate the model 241 :param x: input q-value (float or [float, float] as [r, theta]) 242 :return: (scattering function value) 242 243 """ 243 244 # set effective radius and scaling factor before run 244 245 self._set_effect_radius() 245 246 self._set_scale_factor() 246 return self.params['scale_factor']*self.p_model.run(x)*self.s_model.run(x) 247 return self.params['scale_factor'] * self.p_model.run(x) * \ 248 self.s_model.run(x) 247 249 248 250 def runXY(self, x = 0.0): … … 254 256 self._set_effect_radius() 255 257 self._set_scale_factor() 256 return self.params['scale_factor']*self.p_model.runXY(x)* self.s_model.runXY(x) 258 out = self.params['scale_factor'] * self.p_model.runXY(x) * \ 259 self.s_model.runXY(x) 260 return out 257 261 258 262 ## Now (May27,10) directly uses the model eval function 259 263 ## instead of the for-loop in Base Component. 260 264 def evalDistribution(self, x = []): 261 """ Evaluate the model in cartesian coordinates 262 @param x: input q[], or [qx[], qy[]] 263 @return: scattering function P(q[]) 265 """ 266 Evaluate the model in cartesian coordinates 267 :param x: input q[], or [qx[], qy[]] 268 :return: scattering function P(q[]) 264 269 """ 265 270 # set effective radius and scaling factor before run 266 271 self._set_effect_radius() 267 272 self._set_scale_factor() 268 return self.params['scale_factor']*self.p_model.evalDistribution(x)* self.s_model.evalDistribution(x) 273 out = self.params['scale_factor'] * self.p_model.evalDistribution(x) * \ 274 self.s_model.evalDistribution(x) 275 return out 269 276 270 277 def set_dispersion(self, parameter, dispersion): 271 278 """ 272 273 @param parameter: name of the parameter [string]274 @dispersion: dispersion object of type DispersionModel275 """ 276 value = None279 Set the dispersion object for a model parameter 280 :param parameter: name of the parameter [string] 281 :dispersion: dispersion object of type DispersionModel 282 """ 283 value = None 277 284 try: 278 285 if parameter in self.p_model.dispersion.keys(): 279 value = self.p_model.set_dispersion(parameter, dispersion)286 value = self.p_model.set_dispersion(parameter, dispersion) 280 287 self._set_dispersion() 281 288 return value … … 288 295 """ 289 296 description = "" 290 description += "Note:1) The effect_radius (effective radius) of %s \n"% (s_model.name) 291 description +=" is automatically calculated from size parameters (radius...).\n" 292 description += " 2) For non-spherical shape, this approximation is valid \n" 293 description += " only for limited systems. Thus, use it at your own risk.\n" 294 description +="See %s description and %s description \n"%( p_model.name, s_model.name ) 297 description += "Note:1) The effect_radius (effective radius) of %s \n"%\ 298 (s_model.name) 299 description += " is automatically calculated " 300 description += "from size parameters (radius...).\n" 301 description += " 2) For non-spherical shape, " 302 description += "this approximation is valid \n" 303 description += " only for limited systems. " 304 description += "Thus, use it at your own risk.\n" 305 description += "See %s description and %s description \n"% \ 306 ( p_model.name, s_model.name ) 295 307 description += " for details of individual models." 296 308 self.description += description -
sansmodels/src/sans/models/UnifiedPowerRgModel.py
reace08e r7fdb332 1 2 1 from sans.models.BaseComponent import BaseComponent 3 2 from math import exp, sqrt … … 13 12 BaseComponent.__init__(self) 14 13 """ 15 :param multfactor: number of levels in the model, assumes 0<= level# <=5. 14 :param multfactor: number of levels in the model, 15 assumes 0<= level# <=5. 16 16 """ 17 17 18 18 ## Setting model name model description 19 19 self.name = "UnifiedPowerRg" 20 self.description="""Multiple Levels of Unified Exponential/Power-law Method. 20 self.description = """ 21 Multiple Levels of Unified Exponential/Power-law Method. 21 22 Up to Level 6 is provided. 22 23 Note; the additional Level 0 is an inverse linear function, … … 53 54 54 55 ## functional multiplicity of the model 55 self.multiplicity_info = [max_level_n, "Level No.:",[],[]]56 57 def _unifiedpowerrg(self, x):56 self.multiplicity_info = [max_level_n, "Level No.:", [], []] 57 58 def _unifiedpowerrg(self, x): 58 59 """ 59 60 Scattering function … … 73 74 return answer 74 75 # rearrange the parameters for the given label no. 75 for ind in range(1, l_num+1):76 for ind in range(1, l_num+1): 76 77 # get exp term 77 exp_now = exp(-power(x*self.params['Rg%s'% ind], 2)/3.0)78 exp_now = exp(-power(x*self.params['Rg%s'% ind], 2)/3.0) 78 79 # get erf term 79 80 erf_now = erf(x*self.params['Rg%s'% ind]/sqrt(6.0)) 80 81 # get power term 81 pow_now = power((erf_now*erf_now*erf_now/x),self.params['power%s'% ind]) 82 pow_now = power((erf_now*erf_now*erf_now/x), 83 self.params['power%s'% ind]) 82 84 # get next exp term only if it exists 83 85 try: 84 exp_next = exp(-power(x*self.params['Rg%s'% (ind+1)], 2)/3.0)86 exp_next = exp(-power(x*self.params['Rg%s'% (ind+1)], 2)/3.0) 85 87 except: 86 88 exp_next = 1.0 87 89 # get to the calculation 88 answer += self.params['G%s'% ind]*exp_now + self.params['B%s'% ind]*\89 exp_next * pow_now90 answer += self.params['G%s'% ind]*exp_now + \ 91 self.params['B%s'% ind] * exp_next * pow_now 90 92 # take care of the singular point 91 93 if x == 0.0: 92 94 answer = 0.0 93 for ind in range(1, l_num+1):95 for ind in range(1, l_num+1): 94 96 answer += self.params['G%s'% ind] 95 97 # get scaled … … 117 119 l_num = self.level_num 118 120 # rearrange the parameters for the given label no. 119 for ind in range(0, l_num+1):121 for ind in range(0, l_num+1): 120 122 if ind == 0: 121 123 continue … … 128 130 # Set reasonably define default values that consistent 129 131 # w/NIST for label #1 130 self.params['G%s'% ind] = 0.3 *mult*pow(10, \132 self.params['G%s'% ind] = 0.3 * mult * pow(10, \ 131 133 (l_num+1 - float('%s'% ind))) 132 self.params['Rg%s'% ind] = 21.0 /mult*pow(10, \134 self.params['Rg%s'% ind] = 21.0 / mult * pow(10, \ 133 135 (l_num - float('%s'% ind))) 134 self.params['B%s'% ind] = 6e-03/mult *pow(10, \136 self.params['B%s'% ind] = 6e-03/mult * pow(10, \ 135 137 -(l_num+1 - float('%s'% ind))) 136 138 self.params['power%s'% ind] = 2.0 * mul_pow … … 146 148 self.details['scale'] = ['', None, None] 147 149 # rearrange the parameters for the given label no. 148 for ind in range(0, self.level_num+1):150 for ind in range(0, self.level_num+1): 149 151 if ind == 0: 150 152 continue … … 215 217 return self._unifiedpowerrg(x) 216 218 elif x.__class__.__name__ == 'tuple': 217 raise ValueError, "Tuples are not allowed as input to BaseComponent models" 219 msg = "Tuples are not allowed as input to BaseComponent models" 220 raise ValueError, msg 218 221 else: 219 222 return self._unifiedpowerrg(x) … … 233 236 return self._unifiedpowerrg(x) 234 237 elif x.__class__.__name__ == 'tuple': 235 raise ValueError, "Tuples are not allowed as input to BaseComponent models" 238 msg = "Tuples are not allowed as input to BaseComponent models" 239 raise ValueError, msg 236 240 else: 237 241 return self._unifiedpowerrg(x)
Note: See TracChangeset
for help on using the changeset viewer.