Changeset 37a7252 in sasmodels
- Timestamp:
- Jan 27, 2016 3:42:24 PM (9 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:
- d15a908
- Parents:
- 87edabf
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
extra/pylint.rc
r5ef0633 r37a7252 6 6 # Python code to execute, usually for sys.path manipulation such as 7 7 # pygtk.require(). 8 #init-hook= 8 #init-hook='import os, sys; sys.path.extend([os.getcwd(),os.path.join(os.getcwd(),"extra")])' 9 9 10 10 # Profiled execution. -
sasmodels/__init__.py
r9890053 r37a7252 1 """ 2 sasmodels 3 ========= 4 5 **sasmodels** is a package containing models for small angle neutron and xray 6 scattering. Models supported are the one dimensional circular average and 7 two dimensional oriented patterns. As well as the form factor calculations 8 for the individual shapes **sasmodels** also provides automatic shape 9 polydispersity, angular dispersion and resolution convolution. SESANS 10 patterns can be computed for any model. 11 12 Models can be written in python or in C. C models can run on the GPU if 13 OpenCL drivers are available. See :mod:`generate` for details on 14 defining new models. 15 """ 16 1 17 __version__ = "0.9" -
sasmodels/bumps_model.py
rec7e360 r37a7252 6 6 arguments to set the initial value for each parameter. 7 7 8 :class:`Experiment` combines the *Model* function with a data file loaded by the9 sasview data loader. *Experiment* takes a *cutoff* parameter controlling8 :class:`Experiment` combines the *Model* function with a data file loaded by 9 the sasview data loader. *Experiment* takes a *cutoff* parameter controlling 10 10 how far the polydispersity integral extends. 11 11 12 12 """ 13 14 __all__ = [ 15 "Model", "Experiment", 16 ] 13 17 14 18 import warnings … … 20 24 21 25 # CRUFT: old style bumps wrapper which doesn't separate data and model 26 # pylint: disable=invalid-name 22 27 def BumpsModel(data, model, cutoff=1e-5, **kw): 28 r""" 29 Bind a model to data, along with a polydispersity cutoff. 30 31 *data* is a :class:`data.Data1D`, :class:`data.Data2D` or 32 :class:`data.Sesans` object. Use :func:`data.empty_data1D` or 33 :func:`data.empty_data2D` to define $q, \Delta q$ calculation 34 points for displaying the SANS curve when there is no measured data. 35 36 *model* is a runnable module as returned from :func:`core.load_model`. 37 38 *cutoff* is the polydispersity weight cutoff. 39 40 Any additional *key=value* pairs are model dependent parameters. 41 42 Returns an :class:`Experiment` object. 43 44 Note that the usual Bumps semantics is not fully supported, since 45 assigning *M.name = parameter* on the returned experiment object 46 does not set that parameter in the model. Range setting will still 47 work as expected though. 48 49 .. deprecated:: 0.1 50 Use :class:`Experiment` instead. 51 """ 23 52 warnings.warn("Use of BumpsModel is deprecated. Use bumps_model.Experiment instead.") 53 54 # Create the model and experiment 24 55 model = Model(model, **kw) 25 56 experiment = Experiment(data=data, model=model, cutoff=cutoff) 26 for k in model._parameter_names: 27 setattr(experiment, k, getattr(model, k)) 57 58 # Copy the model parameters up to the experiment object. 59 for k, v in model.parameters().items(): 60 setattr(experiment, k, v) 28 61 return experiment 29 62 63 30 64 def create_parameters(model_info, **kwargs): 65 """ 66 Generate Bumps parameters from the model info. 67 68 *model_info* is returned from :func:`generate.model_info` on the 69 model definition module. 70 71 Any additional *key=value* pairs are initial values for the parameters 72 to the models. Uninitialized parameters will use the model default 73 value. 74 75 Returns a dictionary of *{name: Parameter}* containing the bumps 76 parameters for each model parameter, and a dictionary of 77 *{name: str}* containing the polydispersity distribution types. 78 """ 31 79 # lazy import; this allows the doc builder and nosetests to run even 32 80 # when bumps is not on the path. 33 81 from bumps.names import Parameter 34 35 partype = model_info['partype']36 82 37 83 pars = {} … … 40 86 value = kwargs.pop(name, default) 41 87 pars[name] = Parameter.default(value, name=name, limits=limits) 42 for name in partype['pd-2d']:88 for name in model_info['partype']['pd-2d']: 43 89 for xpart, xdefault, xlimits in [ 44 ('_pd', 0., limits),45 ('_pd_n', 35., (0, 1000)),46 ('_pd_nsigma', 3., (0, 10)),47 ]:90 ('_pd', 0., limits), 91 ('_pd_n', 35., (0, 1000)), 92 ('_pd_nsigma', 3., (0, 10)), 93 ]: 48 94 xname = name + xpart 49 95 xvalue = kwargs.pop(xname, xdefault) … … 51 97 52 98 pd_types = {} 53 for name in partype['pd-2d']:99 for name in model_info['partype']['pd-2d']: 54 100 xname = name + '_pd_type' 55 101 xvalue = kwargs.pop(xname, 'gaussian') … … 63 109 64 110 class Model(object): 111 """ 112 Bumps wrapper for a SAS model. 113 114 *model* is a runnable module as returned from :func:`core.load_model`. 115 116 *cutoff* is the polydispersity weight cutoff. 117 118 Any additional *key=value* pairs are model dependent parameters. 119 """ 65 120 def __init__(self, model, **kwargs): 66 121 self._sasmodel = model 67 122 pars, pd_types = create_parameters(model.info, **kwargs) 68 for k, v in pars.items():123 for k, v in pars.items(): 69 124 setattr(self, k, v) 70 for k, v in pd_types.items():125 for k, v in pd_types.items(): 71 126 setattr(self, k, v) 72 127 self._parameter_names = list(pars.keys()) … … 75 130 def parameters(self): 76 131 """ 77 Return a dictionary of parameters 132 Return a dictionary of parameters objects for the parameters, 133 excluding polydispersity distribution type. 78 134 """ 79 135 return dict((k, getattr(self, k)) for k in self._parameter_names) 80 136 81 137 def state(self): 138 """ 139 Return a dictionary of current values for all the parameters, 140 including polydispersity distribution type. 141 """ 82 142 pars = dict((k, getattr(self, k).value) for k in self._parameter_names) 83 143 pars.update((k, getattr(self, k)) for k in self._pd_type_names) … … 85 145 86 146 class Experiment(DataMixin): 87 """ 88 Return a bumps wrapper for a SAS model. 89 90 *data* is the data to be fitted. 91 92 *model* is the SAS model from :func:`core.load_model`. 147 r""" 148 Bumps wrapper for a SAS experiment. 149 150 *data* is a :class:`data.Data1D`, :class:`data.Data2D` or 151 :class:`data.Sesans` object. Use :func:`data.empty_data1D` or 152 :func:`data.empty_data2D` to define $q, \Delta q$ calculation 153 points for displaying the SANS curve when there is no measured data. 154 155 *model* is a :class:`Model` object. 93 156 94 157 *cutoff* is the integration cutoff, which avoids computing the 95 158 the SAS model where the polydispersity weight is low. 96 159 97 Model parameters can be initialized with additional keyword 98 arguments, or by assigning to model.parameter_name.value. 99 100 The resulting bumps model can be used directly in a FitProblem call. 160 The resulting model can be used directly in a Bumps FitProblem call. 101 161 """ 102 162 def __init__(self, data, model, cutoff=1e-5): … … 109 169 110 170 def update(self): 171 """ 172 Call when model parameters have changed and theory needs to be 173 recalculated. 174 """ 111 175 self._cache = {} 112 176 113 177 def numpoints(self): 114 178 """ 115 Return the number ofpoints179 Return the number of data points 116 180 """ 117 181 return len(self.Iq) … … 124 188 125 189 def theory(self): 190 """ 191 Return the theory corresponding to the model parameters. 192 193 This method uses lazy evaluation, and requires model.update() to be 194 called when the parameters have changed. 195 """ 126 196 if 'theory' not in self._cache: 127 197 pars = self.model.state() … … 130 200 131 201 def residuals(self): 202 """ 203 Return theory minus data normalized by uncertainty. 204 """ 132 205 #if np.any(self.err ==0): print("zeros in err") 133 206 return (self.theory() - self.Iq) / self.dIq 134 207 135 208 def nllf(self): 209 """ 210 Return the negative log likelihood of seeing data given the model 211 parameters, up to a normalizing constant which depends on the data 212 uncertainty. 213 """ 136 214 delta = self.residuals() 137 215 #if np.any(np.isnan(R)): print("NaN in residuals") … … 149 227 150 228 def simulate_data(self, noise=None): 229 """ 230 Generate simulated data. 231 """ 151 232 Iq = self.theory() 152 233 self._set_data(Iq, noise) 153 234 154 235 def save(self, basename): 236 """ 237 Save the model parameters and data into a file. 238 239 Not Implemented. 240 """ 155 241 pass 156 242
Note: See TracChangeset
for help on using the changeset viewer.