Changeset 17bbadd in sasmodels for sasmodels/core.py
- Timestamp:
- Mar 15, 2016 12:47:12 PM (8 years ago)
- Branches:
- master, core_shell_microgels, costrafo411, magnetic_model, release_v0.94, release_v0.95, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
- Children:
- 754e27b
- Parents:
- 5ceb7d0
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sasmodels/core.py
rd18582e r17bbadd 21 21 22 22 __all__ = [ 23 "list_models", "load_model_ definition", "precompile_dll",24 " load_model", "make_kernel", "call_kernel", "call_ER", "call_VR",23 "list_models", "load_model_info", "precompile_dll", 24 "build_model", "make_kernel", "call_kernel", "call_ER_VR", 25 25 ] 26 26 … … 35 35 36 36 37 def load_model_ definition(model_name):37 def load_model_info(model_name): 38 38 """ 39 39 Load a model definition given the model name. … … 43 43 """ 44 44 __import__('sasmodels.models.'+model_name) 45 model_definition= getattr(models, model_name, None)46 return model_definition45 kernel_module = getattr(models, model_name, None) 46 return generate.make_model_info(kernel_module) 47 47 48 48 … … 51 51 Precompile the dll for a model. 52 52 53 Returns the path to the compiled model. 53 Returns the path to the compiled model, or None if the model is a pure 54 python model. 54 55 55 56 This can be used when build the windows distribution of sasmodels … … 60 61 dll path and the allowed floating point precision. 61 62 """ 62 model_ definition = load_model_definition(model_name)63 source , info = generate.make(model_definition)64 return kerneldll.make_dll(source, info, dtype=dtype)63 model_info = load_model_info(model_name) 64 source = generate.make_source(model_info) 65 return kerneldll.make_dll(source, model_info, dtype=dtype) if source else None 65 66 66 67 … … 73 74 return True 74 75 75 def load_model(model_definition, dtype=None, platform="ocl"):76 def build_model(model_info, dtype=None, platform="ocl"): 76 77 """ 77 78 Prepare the model for the default execution platform. … … 80 81 on the model and the computing platform. 81 82 82 *model_definition* is the python module which defines the model. If the 83 model name is given instead, then :func:`load_model_definition` will be 84 called with the model name. 83 *model_info* is the model definition structure returned from 84 :func:`load_model_info`. 85 85 86 86 *dtype* indicates whether the model should use single or double precision … … 93 93 otherwise it uses the default "ocl". 94 94 """ 95 if isstr(model_definition): 96 model_definition = load_model_definition(model_definition) 95 source = generate.make_source(model_info) 97 96 if dtype is None: 98 dtype = 'single' if getattr(model_definition, 'single', True) else 'double' 99 source, info = generate.make(model_definition) 100 if callable(info.get('Iq', None)): 101 return kernelpy.PyModel(info) 97 dtype = 'single' if model_info['single'] else 'double' 98 if callable(model_info.get('Iq', None)): 99 return kernelpy.PyModel(model_info) 102 100 103 101 ## for debugging: … … 107 105 ## 4. rerun "python -m sasmodels.direct_model $MODELNAME" 108 106 ## 5. uncomment open().read() so that source will be regenerated from model 109 # open( info['name']+'.c','w').write(source)110 # source = open( info['name']+'.cl','r').read()107 # open(model_info['name']+'.c','w').write(source) 108 # source = open(model_info['name']+'.cl','r').read() 111 109 112 110 if (platform == "dll" 113 111 or not HAVE_OPENCL 114 112 or not kernelcl.environment().has_type(dtype)): 115 return kerneldll.load_dll(source, info, dtype)113 return kerneldll.load_dll(source, model_info, dtype) 116 114 else: 117 return kernelcl.GpuModel(source, info, dtype)115 return kernelcl.GpuModel(source, model_info, dtype) 118 116 119 117 def make_kernel(model, q_vectors): … … 123 121 return model(q_vectors) 124 122 125 def get_weights( info, pars, name):123 def get_weights(model_info, pars, name): 126 124 """ 127 125 Generate the distribution for parameter *name* given the parameter values … … 131 129 from the *pars* dictionary for parameter value and parameter dispersion. 132 130 """ 133 relative = name in info['partype']['pd-rel']134 limits = info['limits'][name]131 relative = name in model_info['partype']['pd-rel'] 132 limits = model_info['limits'][name] 135 133 disperser = pars.get(name+'_pd_type', 'gaussian') 136 value = pars.get(name, info['defaults'][name])134 value = pars.get(name, model_info['defaults'][name]) 137 135 npts = pars.get(name+'_pd_n', 0) 138 136 width = pars.get(name+'_pd', 0.0) … … 173 171 return kernel(fixed_pars, pd_pars, cutoff=cutoff) 174 172 173 def call_ER_VR(model_info, vol_pars): 174 """ 175 Return effect radius and volume ratio for the model. 176 177 *info* is either *kernel.info* for *kernel=make_kernel(model,q)* 178 or *model.info*. 179 180 *pars* are the parameters as expected by :func:`call_kernel`. 181 """ 182 ER = model_info.get('ER', None) 183 VR = model_info.get('VR', None) 184 value, weight = dispersion_mesh(vol_pars) 185 186 individual_radii = ER(*value) if ER else 1.0 187 whole, part = VR(*value) if VR else (1.0, 1.0) 188 189 effect_radius = np.sum(weight*individual_radii) / np.sum(weight) 190 volume_ratio = np.sum(weight*part)/np.sum(weight*whole) 191 return effect_radius, volume_ratio 192 193 175 194 def call_ER(info, pars): 176 195 """ 177 196 Call the model ER function using *pars*. 178 179 197 *info* is either *model.info* if you have a loaded model, or *kernel.info* 180 198 if you have a model kernel prepared for evaluation. … … 194 212 """ 195 213 Call the model VR function using *pars*. 196 197 214 *info* is either *model.info* if you have a loaded model, or *kernel.info* 198 215 if you have a model kernel prepared for evaluation. … … 208 225 return np.sum(weight*part)/np.sum(weight*whole) 209 226 227 # TODO: remove call_ER, call_VR 228
Note: See TracChangeset
for help on using the changeset viewer.