[803f835] | 1 | """ |
---|
| 2 | Class interface to the model calculator. |
---|
| 3 | |
---|
| 4 | Calling a model is somewhat non-trivial since the functions called depend |
---|
| 5 | on the data type. For 1D data the *Iq* kernel needs to be called, for |
---|
| 6 | 2D data the *Iqxy* kernel needs to be called, and for SESANS data the |
---|
| 7 | *Iq* kernel needs to be called followed by a Hankel transform. Before |
---|
| 8 | the kernel is called an appropriate *q* calculation vector needs to be |
---|
| 9 | constructed. This is not the simple *q* vector where you have measured |
---|
| 10 | the data since the resolution calculation will require values beyond the |
---|
| 11 | range of the measured data. After the calculation the resolution calculator |
---|
| 12 | must be called to return the predicted value for each measured data point. |
---|
| 13 | |
---|
| 14 | :class:`DirectModel` is a callable object that takes *parameter=value* |
---|
| 15 | keyword arguments and returns the appropriate theory values for the data. |
---|
| 16 | |
---|
| 17 | :class:`DataMixin` does the real work of interpreting the data and calling |
---|
| 18 | the model calculator. This is used by :class:`DirectModel`, which uses |
---|
| 19 | direct parameter values and by :class:`bumps_model.Experiment` which wraps |
---|
| 20 | the parameter values in boxes so that the user can set fitting ranges, etc. |
---|
| 21 | on the individual parameters and send the model to the Bumps optimizers. |
---|
| 22 | """ |
---|
| 23 | from __future__ import print_function |
---|
[ae7b97b] | 24 | |
---|
[7ae2b7f] | 25 | import numpy as np # type: ignore |
---|
[ae7b97b] | 26 | |
---|
[7ae2b7f] | 27 | # TODO: fix sesans module |
---|
| 28 | from . import sesans # type: ignore |
---|
[6d6508e] | 29 | from . import weights |
---|
[7cf2cfd] | 30 | from . import resolution |
---|
| 31 | from . import resolution2d |
---|
[bde38b5] | 32 | from .details import make_kernel_args, dispersion_mesh |
---|
[7b9e4dd] | 33 | from .modelinfo import DEFAULT_BACKGROUND |
---|
[6d6508e] | 34 | |
---|
[2d81cfe] | 35 | # pylint: disable=unused-import |
---|
[a5b8477] | 36 | try: |
---|
| 37 | from typing import Optional, Dict, Tuple |
---|
| 38 | except ImportError: |
---|
| 39 | pass |
---|
| 40 | else: |
---|
| 41 | from .data import Data |
---|
| 42 | from .kernel import Kernel, KernelModel |
---|
| 43 | from .modelinfo import Parameter, ParameterSet |
---|
[2d81cfe] | 44 | # pylint: enable=unused-import |
---|
[a5b8477] | 45 | |
---|
[0ff62d4] | 46 | def call_kernel(calculator, pars, cutoff=0., mono=False): |
---|
[a5b8477] | 47 | # type: (Kernel, ParameterSet, float, bool) -> np.ndarray |
---|
[6d6508e] | 48 | """ |
---|
| 49 | Call *kernel* returned from *model.make_kernel* with parameters *pars*. |
---|
| 50 | |
---|
| 51 | *cutoff* is the limiting value for the product of dispersion weights used |
---|
| 52 | to perform the multidimensional dispersion calculation more quickly at a |
---|
| 53 | slight cost to accuracy. The default value of *cutoff=0* integrates over |
---|
| 54 | the entire dispersion cube. Using *cutoff=1e-5* can be 50% faster, but |
---|
| 55 | with an error of about 1%, which is usually less than the measurement |
---|
| 56 | uncertainty. |
---|
| 57 | |
---|
| 58 | *mono* is True if polydispersity should be set to none on all parameters. |
---|
| 59 | """ |
---|
[3c24ccd] | 60 | mesh = get_mesh(calculator.info, pars, dim=calculator.dim, mono=mono) |
---|
[9e771a3] | 61 | #print("pars", list(zip(*mesh))[0]) |
---|
[8698a0d] | 62 | call_details, values, is_magnetic = make_kernel_args(calculator, mesh) |
---|
[32e3c9b] | 63 | #print("values:", values) |
---|
[9eb3632] | 64 | return calculator(call_details, values, cutoff, is_magnetic) |
---|
[6d6508e] | 65 | |
---|
[304c775] | 66 | def call_Fq(calculator, pars, cutoff=0., mono=False): |
---|
| 67 | # type: (Kernel, ParameterSet, float, bool) -> np.ndarray |
---|
[40a87fa] | 68 | """ |
---|
[304c775] | 69 | Like :func:`call_kernel`, but returning F, F^2, R_eff, V, V_form/V_shell. |
---|
[40a87fa] | 70 | """ |
---|
[304c775] | 71 | R_eff_type = int(pars.pop('radius_effective_type', 1.0)) |
---|
| 72 | mesh = get_mesh(calculator.info, pars, dim=calculator.dim, mono=mono) |
---|
| 73 | #print("pars", list(zip(*mesh))[0]) |
---|
| 74 | call_details, values, is_magnetic = make_kernel_args(calculator, mesh) |
---|
| 75 | #print("values:", values) |
---|
| 76 | return calculator.Fq(call_details, values, cutoff, is_magnetic, R_eff_type) |
---|
[40a87fa] | 77 | |
---|
| 78 | def call_profile(model_info, **pars): |
---|
| 79 | # type: (ModelInfo, ...) -> Tuple[np.ndarray, np.ndarray, Tuple[str, str]] |
---|
| 80 | """ |
---|
| 81 | Returns the profile *x, y, (xlabel, ylabel)* representing the model. |
---|
| 82 | """ |
---|
| 83 | args = {} |
---|
| 84 | for p in model_info.parameters.kernel_parameters: |
---|
| 85 | if p.length > 1: |
---|
| 86 | value = np.array([pars.get(p.id+str(j), p.default) |
---|
| 87 | for j in range(1, p.length+1)]) |
---|
| 88 | else: |
---|
| 89 | value = pars.get(p.id, p.default) |
---|
| 90 | args[p.id] = value |
---|
| 91 | x, y = model_info.profile(**args) |
---|
| 92 | return x, y, model_info.profile_axes |
---|
| 93 | |
---|
[3c24ccd] | 94 | def get_mesh(model_info, values, dim='1d', mono=False): |
---|
| 95 | # type: (ModelInfo, Dict[str, float], str, bool) -> List[Tuple[float, np.ndarray, np.ndarry]] |
---|
| 96 | """ |
---|
| 97 | Retrieve the dispersity mesh described by the parameter set. |
---|
| 98 | |
---|
| 99 | Returns a list of *(value, dispersity, weights)* with one tuple for each |
---|
| 100 | parameter in the model call parameters. Inactive parameters return the |
---|
| 101 | default value with a weight of 1.0. |
---|
| 102 | """ |
---|
| 103 | parameters = model_info.parameters |
---|
| 104 | if mono: |
---|
| 105 | active = lambda name: False |
---|
| 106 | elif dim == '1d': |
---|
| 107 | active = lambda name: name in parameters.pd_1d |
---|
| 108 | elif dim == '2d': |
---|
| 109 | active = lambda name: name in parameters.pd_2d |
---|
| 110 | else: |
---|
| 111 | active = lambda name: True |
---|
| 112 | |
---|
| 113 | #print("pars",[p.id for p in parameters.call_parameters]) |
---|
| 114 | mesh = [_get_par_weights(p, values, active(p.name)) |
---|
| 115 | for p in parameters.call_parameters] |
---|
| 116 | return mesh |
---|
| 117 | |
---|
[40a87fa] | 118 | |
---|
[3c24ccd] | 119 | def _get_par_weights(parameter, values, active=True): |
---|
| 120 | # type: (Parameter, Dict[str, float]) -> Tuple[float, np.ndarray, np.ndarray] |
---|
[6d6508e] | 121 | """ |
---|
| 122 | Generate the distribution for parameter *name* given the parameter values |
---|
| 123 | in *pars*. |
---|
| 124 | |
---|
| 125 | Uses "name", "name_pd", "name_pd_type", "name_pd_n", "name_pd_sigma" |
---|
| 126 | from the *pars* dictionary for parameter value and parameter dispersion. |
---|
| 127 | """ |
---|
| 128 | value = float(values.get(parameter.name, parameter.default)) |
---|
| 129 | npts = values.get(parameter.name+'_pd_n', 0) |
---|
| 130 | width = values.get(parameter.name+'_pd', 0.0) |
---|
[32f87a5] | 131 | relative = parameter.relative_pd |
---|
[9e771a3] | 132 | if npts == 0 or width == 0.0 or not active: |
---|
[8698a0d] | 133 | # Note: orientation parameters have the viewing angle as the parameter |
---|
| 134 | # value and the jitter in the distribution, so be sure to set the |
---|
| 135 | # empty pd for orientation parameters to 0. |
---|
[9e771a3] | 136 | pd = [value if relative or not parameter.polydisperse else 0.0], [1.0] |
---|
[8698a0d] | 137 | else: |
---|
| 138 | limits = parameter.limits |
---|
| 139 | disperser = values.get(parameter.name+'_pd_type', 'gaussian') |
---|
| 140 | nsigma = values.get(parameter.name+'_pd_nsigma', 3.0) |
---|
| 141 | pd = weights.get_weights(disperser, npts, width, nsigma, |
---|
[2d81cfe] | 142 | value, limits, relative) |
---|
[8698a0d] | 143 | return value, pd[0], pd[1] |
---|
[ae7b97b] | 144 | |
---|
[745b7bb] | 145 | |
---|
[9e771a3] | 146 | def _vol_pars(model_info, values): |
---|
[40a87fa] | 147 | # type: (ModelInfo, ParameterSet) -> Tuple[np.ndarray, np.ndarray] |
---|
[9e771a3] | 148 | vol_pars = [_get_par_weights(p, values) |
---|
[40a87fa] | 149 | for p in model_info.parameters.call_parameters |
---|
| 150 | if p.type == 'volume'] |
---|
[4cc161e] | 151 | #import pylab; pylab.plot(vol_pars[0][0],vol_pars[0][1]); pylab.show() |
---|
[9e771a3] | 152 | dispersity, weight = dispersion_mesh(model_info, vol_pars) |
---|
| 153 | return dispersity, weight |
---|
[745b7bb] | 154 | |
---|
| 155 | |
---|
[fa79f5c] | 156 | def _make_sesans_transform(data): |
---|
| 157 | from sas.sascalc.data_util.nxsunit import Converter |
---|
| 158 | |
---|
| 159 | # Pre-compute the Hankel matrix (H) |
---|
| 160 | SElength = Converter(data._xunit)(data.x, "A") |
---|
| 161 | |
---|
| 162 | theta_max = Converter("radians")(data.sample.zacceptance)[0] |
---|
| 163 | q_max = 2 * np.pi / np.max(data.source.wavelength) * np.sin(theta_max) |
---|
| 164 | zaccept = Converter("1/A")(q_max, "1/" + data.source.wavelength_unit), |
---|
| 165 | |
---|
| 166 | Rmax = 10000000 |
---|
| 167 | hankel = sesans.SesansTransform(data.x, SElength, |
---|
| 168 | data.source.wavelength, |
---|
| 169 | zaccept, Rmax) |
---|
| 170 | return hankel |
---|
| 171 | |
---|
| 172 | |
---|
[7cf2cfd] | 173 | class DataMixin(object): |
---|
| 174 | """ |
---|
| 175 | DataMixin captures the common aspects of evaluating a SAS model for a |
---|
| 176 | particular data set, including calculating Iq and evaluating the |
---|
| 177 | resolution function. It is used in particular by :class:`DirectModel`, |
---|
| 178 | which evaluates a SAS model parameters as key word arguments to the |
---|
| 179 | calculator method, and by :class:`bumps_model.Experiment`, which wraps the |
---|
| 180 | model and data for use with the Bumps fitting engine. It is not |
---|
| 181 | currently used by :class:`sasview_model.SasviewModel` since this will |
---|
| 182 | require a number of changes to SasView before we can do it. |
---|
[803f835] | 183 | |
---|
| 184 | :meth:`_interpret_data` initializes the data structures necessary |
---|
| 185 | to manage the calculations. This sets attributes in the child class |
---|
| 186 | such as *data_type* and *resolution*. |
---|
| 187 | |
---|
| 188 | :meth:`_calc_theory` evaluates the model at the given control values. |
---|
| 189 | |
---|
| 190 | :meth:`_set_data` sets the intensity data in the data object, |
---|
| 191 | possibly with random noise added. This is useful for simulating a |
---|
| 192 | dataset with the results from :meth:`_calc_theory`. |
---|
[7cf2cfd] | 193 | """ |
---|
| 194 | def _interpret_data(self, data, model): |
---|
[a5b8477] | 195 | # type: (Data, KernelModel) -> None |
---|
[803f835] | 196 | # pylint: disable=attribute-defined-outside-init |
---|
| 197 | |
---|
[7cf2cfd] | 198 | self._data = data |
---|
| 199 | self._model = model |
---|
| 200 | |
---|
| 201 | # interpret data |
---|
[a769b54] | 202 | if hasattr(data, 'isSesans') and data.isSesans: |
---|
[7cf2cfd] | 203 | self.data_type = 'sesans' |
---|
| 204 | elif hasattr(data, 'qx_data'): |
---|
| 205 | self.data_type = 'Iqxy' |
---|
[ea75043] | 206 | elif getattr(data, 'oriented', False): |
---|
| 207 | self.data_type = 'Iq-oriented' |
---|
[7cf2cfd] | 208 | else: |
---|
| 209 | self.data_type = 'Iq' |
---|
| 210 | |
---|
| 211 | if self.data_type == 'sesans': |
---|
[fa79f5c] | 212 | res = _make_sesans_transform(data) |
---|
[803f835] | 213 | index = slice(None, None) |
---|
[7cf2cfd] | 214 | if data.y is not None: |
---|
[803f835] | 215 | Iq, dIq = data.y, data.dy |
---|
| 216 | else: |
---|
| 217 | Iq, dIq = None, None |
---|
[7cf2cfd] | 218 | #self._theory = np.zeros_like(q) |
---|
[fa79f5c] | 219 | q_vectors = [res.q_calc] |
---|
[7cf2cfd] | 220 | elif self.data_type == 'Iqxy': |
---|
[6d6508e] | 221 | #if not model.info.parameters.has_2d: |
---|
[60eab2a] | 222 | # raise ValueError("not 2D without orientation or magnetic parameters") |
---|
[7cf2cfd] | 223 | q = np.sqrt(data.qx_data**2 + data.qy_data**2) |
---|
| 224 | qmin = getattr(data, 'qmin', 1e-16) |
---|
| 225 | qmax = getattr(data, 'qmax', np.inf) |
---|
| 226 | accuracy = getattr(data, 'accuracy', 'Low') |
---|
[1a8c11c] | 227 | index = (data.mask == 0) & (q >= qmin) & (q <= qmax) |
---|
[7cf2cfd] | 228 | if data.data is not None: |
---|
[803f835] | 229 | index &= ~np.isnan(data.data) |
---|
| 230 | Iq = data.data[index] |
---|
| 231 | dIq = data.err_data[index] |
---|
| 232 | else: |
---|
| 233 | Iq, dIq = None, None |
---|
| 234 | res = resolution2d.Pinhole2D(data=data, index=index, |
---|
| 235 | nsigma=3.0, accuracy=accuracy) |
---|
[7cf2cfd] | 236 | #self._theory = np.zeros_like(self.Iq) |
---|
[803f835] | 237 | q_vectors = res.q_calc |
---|
[7cf2cfd] | 238 | elif self.data_type == 'Iq': |
---|
[803f835] | 239 | index = (data.x >= data.qmin) & (data.x <= data.qmax) |
---|
[b9c19aa2] | 240 | mask = getattr(data, 'mask', None) |
---|
| 241 | if mask is not None: |
---|
[1a8c11c] | 242 | index &= (mask == 0) |
---|
[7cf2cfd] | 243 | if data.y is not None: |
---|
[803f835] | 244 | index &= ~np.isnan(data.y) |
---|
| 245 | Iq = data.y[index] |
---|
| 246 | dIq = data.dy[index] |
---|
| 247 | else: |
---|
| 248 | Iq, dIq = None, None |
---|
[7cf2cfd] | 249 | if getattr(data, 'dx', None) is not None: |
---|
[803f835] | 250 | q, dq = data.x[index], data.dx[index] |
---|
| 251 | if (dq > 0).any(): |
---|
| 252 | res = resolution.Pinhole1D(q, dq) |
---|
[7cf2cfd] | 253 | else: |
---|
[803f835] | 254 | res = resolution.Perfect1D(q) |
---|
| 255 | elif (getattr(data, 'dxl', None) is not None |
---|
| 256 | and getattr(data, 'dxw', None) is not None): |
---|
| 257 | res = resolution.Slit1D(data.x[index], |
---|
[4d8e0bb] | 258 | qx_width=data.dxl[index], |
---|
| 259 | qy_width=data.dxw[index]) |
---|
[7cf2cfd] | 260 | else: |
---|
[803f835] | 261 | res = resolution.Perfect1D(data.x[index]) |
---|
[7cf2cfd] | 262 | |
---|
| 263 | #self._theory = np.zeros_like(self.Iq) |
---|
[803f835] | 264 | q_vectors = [res.q_calc] |
---|
[ea75043] | 265 | elif self.data_type == 'Iq-oriented': |
---|
| 266 | index = (data.x >= data.qmin) & (data.x <= data.qmax) |
---|
| 267 | if data.y is not None: |
---|
| 268 | index &= ~np.isnan(data.y) |
---|
| 269 | Iq = data.y[index] |
---|
| 270 | dIq = data.dy[index] |
---|
| 271 | else: |
---|
| 272 | Iq, dIq = None, None |
---|
| 273 | if (getattr(data, 'dxl', None) is None |
---|
[40a87fa] | 274 | or getattr(data, 'dxw', None) is None): |
---|
[ea75043] | 275 | raise ValueError("oriented sample with 1D data needs slit resolution") |
---|
| 276 | |
---|
| 277 | res = resolution2d.Slit2D(data.x[index], |
---|
| 278 | qx_width=data.dxw[index], |
---|
| 279 | qy_width=data.dxl[index]) |
---|
| 280 | q_vectors = res.q_calc |
---|
[7cf2cfd] | 281 | else: |
---|
| 282 | raise ValueError("Unknown data type") # never gets here |
---|
| 283 | |
---|
| 284 | # Remember function inputs so we can delay loading the function and |
---|
| 285 | # so we can save/restore state |
---|
[02e70ff] | 286 | self._kernel_inputs = q_vectors |
---|
[7cf2cfd] | 287 | self._kernel = None |
---|
[803f835] | 288 | self.Iq, self.dIq, self.index = Iq, dIq, index |
---|
| 289 | self.resolution = res |
---|
[7cf2cfd] | 290 | |
---|
| 291 | def _set_data(self, Iq, noise=None): |
---|
[a5b8477] | 292 | # type: (np.ndarray, Optional[float]) -> None |
---|
[803f835] | 293 | # pylint: disable=attribute-defined-outside-init |
---|
[7cf2cfd] | 294 | if noise is not None: |
---|
| 295 | self.dIq = Iq*noise*0.01 |
---|
| 296 | dy = self.dIq |
---|
| 297 | y = Iq + np.random.randn(*dy.shape) * dy |
---|
| 298 | self.Iq = y |
---|
[ea75043] | 299 | if self.data_type in ('Iq', 'Iq-oriented'): |
---|
[d1ff3a5] | 300 | if self._data.y is None: |
---|
| 301 | self._data.y = np.empty(len(self._data.x), 'd') |
---|
| 302 | if self._data.dy is None: |
---|
| 303 | self._data.dy = np.empty(len(self._data.x), 'd') |
---|
[7cf2cfd] | 304 | self._data.dy[self.index] = dy |
---|
| 305 | self._data.y[self.index] = y |
---|
| 306 | elif self.data_type == 'Iqxy': |
---|
[d1ff3a5] | 307 | if self._data.data is None: |
---|
| 308 | self._data.data = np.empty_like(self._data.qx_data, 'd') |
---|
| 309 | if self._data.err_data is None: |
---|
| 310 | self._data.err_data = np.empty_like(self._data.qx_data, 'd') |
---|
[7cf2cfd] | 311 | self._data.data[self.index] = y |
---|
[d1ff3a5] | 312 | self._data.err_data[self.index] = dy |
---|
[7cf2cfd] | 313 | elif self.data_type == 'sesans': |
---|
[d1ff3a5] | 314 | if self._data.y is None: |
---|
| 315 | self._data.y = np.empty(len(self._data.x), 'd') |
---|
[7cf2cfd] | 316 | self._data.y[self.index] = y |
---|
| 317 | else: |
---|
| 318 | raise ValueError("Unknown model") |
---|
| 319 | |
---|
| 320 | def _calc_theory(self, pars, cutoff=0.0): |
---|
[a5b8477] | 321 | # type: (ParameterSet, float) -> np.ndarray |
---|
[7cf2cfd] | 322 | if self._kernel is None: |
---|
[68e7f9d] | 323 | self._kernel = self._model.make_kernel(self._kernel_inputs) |
---|
[7cf2cfd] | 324 | |
---|
[d18d6dd] | 325 | # Need to pull background out of resolution for multiple scattering |
---|
[7b9e4dd] | 326 | background = pars.get('background', DEFAULT_BACKGROUND) |
---|
[d18d6dd] | 327 | pars = pars.copy() |
---|
| 328 | pars['background'] = 0. |
---|
| 329 | |
---|
[7cf2cfd] | 330 | Iq_calc = call_kernel(self._kernel, pars, cutoff=cutoff) |
---|
[40a87fa] | 331 | # Storing the calculated Iq values so that they can be plotted. |
---|
| 332 | # Only applies to oriented USANS data for now. |
---|
| 333 | # TODO: extend plotting of calculate Iq to other measurement types |
---|
| 334 | # TODO: refactor so we don't store the result in the model |
---|
[d1ff3a5] | 335 | self.Iq_calc = Iq_calc |
---|
[fa79f5c] | 336 | result = self.resolution.apply(Iq_calc) |
---|
| 337 | if hasattr(self.resolution, 'nx'): |
---|
| 338 | self.Iq_calc = ( |
---|
| 339 | self.resolution.qx_calc, self.resolution.qy_calc, |
---|
| 340 | np.reshape(Iq_calc, (self.resolution.ny, self.resolution.nx)) |
---|
| 341 | ) |
---|
[d18d6dd] | 342 | return result + background |
---|
[7cf2cfd] | 343 | |
---|
| 344 | |
---|
| 345 | class DirectModel(DataMixin): |
---|
[803f835] | 346 | """ |
---|
| 347 | Create a calculator object for a model. |
---|
| 348 | |
---|
| 349 | *data* is 1D SAS, 2D SAS or SESANS data |
---|
| 350 | |
---|
| 351 | *model* is a model calculator return from :func:`generate.load_model` |
---|
| 352 | |
---|
| 353 | *cutoff* is the polydispersity weight cutoff. |
---|
| 354 | """ |
---|
[7cf2cfd] | 355 | def __init__(self, data, model, cutoff=1e-5): |
---|
[a5b8477] | 356 | # type: (Data, KernelModel, float) -> None |
---|
[7cf2cfd] | 357 | self.model = model |
---|
| 358 | self.cutoff = cutoff |
---|
[803f835] | 359 | # Note: _interpret_data defines the model attributes |
---|
[7cf2cfd] | 360 | self._interpret_data(data, model) |
---|
[803f835] | 361 | |
---|
[16bc3fc] | 362 | def __call__(self, **pars): |
---|
[a5b8477] | 363 | # type: (**float) -> np.ndarray |
---|
[7cf2cfd] | 364 | return self._calc_theory(pars, cutoff=self.cutoff) |
---|
[803f835] | 365 | |
---|
[7cf2cfd] | 366 | def simulate_data(self, noise=None, **pars): |
---|
[a5b8477] | 367 | # type: (Optional[float], **float) -> None |
---|
[803f835] | 368 | """ |
---|
| 369 | Generate simulated data for the model. |
---|
| 370 | """ |
---|
[7cf2cfd] | 371 | Iq = self.__call__(**pars) |
---|
| 372 | self._set_data(Iq, noise=noise) |
---|
[ae7b97b] | 373 | |
---|
[745b7bb] | 374 | def profile(self, **pars): |
---|
| 375 | # type: (**float) -> None |
---|
| 376 | """ |
---|
| 377 | Generate a plottable profile. |
---|
| 378 | """ |
---|
| 379 | return call_profile(self.model.info, **pars) |
---|
| 380 | |
---|
[803f835] | 381 | def main(): |
---|
[a5b8477] | 382 | # type: () -> None |
---|
[803f835] | 383 | """ |
---|
| 384 | Program to evaluate a particular model at a set of q values. |
---|
| 385 | """ |
---|
[ae7b97b] | 386 | import sys |
---|
[7cf2cfd] | 387 | from .data import empty_data1D, empty_data2D |
---|
[17bbadd] | 388 | from .core import load_model_info, build_model |
---|
[7cf2cfd] | 389 | |
---|
[ae7b97b] | 390 | if len(sys.argv) < 3: |
---|
[9404dd3] | 391 | print("usage: python -m sasmodels.direct_model modelname (q|qx,qy) par=val ...") |
---|
[ae7b97b] | 392 | sys.exit(1) |
---|
| 393 | model_name = sys.argv[1] |
---|
[aa4946b] | 394 | call = sys.argv[2].upper() |
---|
[304c775] | 395 | try: |
---|
| 396 | values = [float(v) for v in call.split(',')] |
---|
| 397 | except ValueError: |
---|
| 398 | values = [] |
---|
| 399 | if len(values) == 1: |
---|
| 400 | q, = values |
---|
| 401 | data = empty_data1D([q]) |
---|
| 402 | elif len(values) == 2: |
---|
| 403 | qx, qy = values |
---|
| 404 | data = empty_data2D([qx], [qy]) |
---|
[7cf2cfd] | 405 | else: |
---|
[304c775] | 406 | print("use q or qx,qy") |
---|
| 407 | sys.exit(1) |
---|
[7cf2cfd] | 408 | |
---|
[17bbadd] | 409 | model_info = load_model_info(model_name) |
---|
| 410 | model = build_model(model_info) |
---|
[7cf2cfd] | 411 | calculator = DirectModel(data, model) |
---|
[4cc161e] | 412 | pars = dict((k, (float(v) if not k.endswith("_pd_type") else v)) |
---|
[ae7b97b] | 413 | for pair in sys.argv[3:] |
---|
[803f835] | 414 | for k, v in [pair.split('=')]) |
---|
[304c775] | 415 | Iq = calculator(**pars) |
---|
| 416 | print(Iq[0]) |
---|
[ae7b97b] | 417 | |
---|
| 418 | if __name__ == "__main__": |
---|
[803f835] | 419 | main() |
---|