[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 | |
---|
| 25 | import numpy as np |
---|
| 26 | |
---|
[17bbadd] | 27 | from .core import make_kernel |
---|
| 28 | from .core import call_kernel, call_ER_VR |
---|
[7cf2cfd] | 29 | from . import sesans |
---|
| 30 | from . import resolution |
---|
| 31 | from . import resolution2d |
---|
[ae7b97b] | 32 | |
---|
[7cf2cfd] | 33 | class DataMixin(object): |
---|
| 34 | """ |
---|
| 35 | DataMixin captures the common aspects of evaluating a SAS model for a |
---|
| 36 | particular data set, including calculating Iq and evaluating the |
---|
| 37 | resolution function. It is used in particular by :class:`DirectModel`, |
---|
| 38 | which evaluates a SAS model parameters as key word arguments to the |
---|
| 39 | calculator method, and by :class:`bumps_model.Experiment`, which wraps the |
---|
| 40 | model and data for use with the Bumps fitting engine. It is not |
---|
| 41 | currently used by :class:`sasview_model.SasviewModel` since this will |
---|
| 42 | require a number of changes to SasView before we can do it. |
---|
[803f835] | 43 | |
---|
| 44 | :meth:`_interpret_data` initializes the data structures necessary |
---|
| 45 | to manage the calculations. This sets attributes in the child class |
---|
| 46 | such as *data_type* and *resolution*. |
---|
| 47 | |
---|
| 48 | :meth:`_calc_theory` evaluates the model at the given control values. |
---|
| 49 | |
---|
| 50 | :meth:`_set_data` sets the intensity data in the data object, |
---|
| 51 | possibly with random noise added. This is useful for simulating a |
---|
| 52 | dataset with the results from :meth:`_calc_theory`. |
---|
[7cf2cfd] | 53 | """ |
---|
| 54 | def _interpret_data(self, data, model): |
---|
[803f835] | 55 | # pylint: disable=attribute-defined-outside-init |
---|
| 56 | |
---|
[7cf2cfd] | 57 | self._data = data |
---|
| 58 | self._model = model |
---|
| 59 | |
---|
| 60 | # interpret data |
---|
| 61 | if hasattr(data, 'lam'): |
---|
| 62 | self.data_type = 'sesans' |
---|
| 63 | elif hasattr(data, 'qx_data'): |
---|
| 64 | self.data_type = 'Iqxy' |
---|
| 65 | else: |
---|
| 66 | self.data_type = 'Iq' |
---|
| 67 | |
---|
| 68 | if self.data_type == 'sesans': |
---|
[02e70ff] | 69 | |
---|
[7cf2cfd] | 70 | q = sesans.make_q(data.sample.zacceptance, data.Rmax) |
---|
[803f835] | 71 | index = slice(None, None) |
---|
| 72 | res = None |
---|
[7cf2cfd] | 73 | if data.y is not None: |
---|
[803f835] | 74 | Iq, dIq = data.y, data.dy |
---|
| 75 | else: |
---|
| 76 | Iq, dIq = None, None |
---|
[7cf2cfd] | 77 | #self._theory = np.zeros_like(q) |
---|
[02e70ff] | 78 | q_vectors = [q] |
---|
| 79 | q_mono = sesans.make_all_q(data) |
---|
[7cf2cfd] | 80 | elif self.data_type == 'Iqxy': |
---|
[303d8d6] | 81 | partype = model.info['par_type'] |
---|
[7cf2cfd] | 82 | if not partype['orientation'] and not partype['magnetic']: |
---|
| 83 | raise ValueError("not 2D without orientation or magnetic parameters") |
---|
| 84 | q = np.sqrt(data.qx_data**2 + data.qy_data**2) |
---|
| 85 | qmin = getattr(data, 'qmin', 1e-16) |
---|
| 86 | qmax = getattr(data, 'qmax', np.inf) |
---|
| 87 | accuracy = getattr(data, 'accuracy', 'Low') |
---|
[803f835] | 88 | index = ~data.mask & (q >= qmin) & (q <= qmax) |
---|
[7cf2cfd] | 89 | if data.data is not None: |
---|
[803f835] | 90 | index &= ~np.isnan(data.data) |
---|
| 91 | Iq = data.data[index] |
---|
| 92 | dIq = data.err_data[index] |
---|
| 93 | else: |
---|
| 94 | Iq, dIq = None, None |
---|
| 95 | res = resolution2d.Pinhole2D(data=data, index=index, |
---|
| 96 | nsigma=3.0, accuracy=accuracy) |
---|
[7cf2cfd] | 97 | #self._theory = np.zeros_like(self.Iq) |
---|
[803f835] | 98 | q_vectors = res.q_calc |
---|
[02e70ff] | 99 | q_mono = [] |
---|
[7cf2cfd] | 100 | elif self.data_type == 'Iq': |
---|
[803f835] | 101 | index = (data.x >= data.qmin) & (data.x <= data.qmax) |
---|
[7cf2cfd] | 102 | if data.y is not None: |
---|
[803f835] | 103 | index &= ~np.isnan(data.y) |
---|
| 104 | Iq = data.y[index] |
---|
| 105 | dIq = data.dy[index] |
---|
| 106 | else: |
---|
| 107 | Iq, dIq = None, None |
---|
[7cf2cfd] | 108 | if getattr(data, 'dx', None) is not None: |
---|
[803f835] | 109 | q, dq = data.x[index], data.dx[index] |
---|
| 110 | if (dq > 0).any(): |
---|
| 111 | res = resolution.Pinhole1D(q, dq) |
---|
[7cf2cfd] | 112 | else: |
---|
[803f835] | 113 | res = resolution.Perfect1D(q) |
---|
| 114 | elif (getattr(data, 'dxl', None) is not None |
---|
| 115 | and getattr(data, 'dxw', None) is not None): |
---|
| 116 | res = resolution.Slit1D(data.x[index], |
---|
| 117 | width=data.dxh[index], |
---|
| 118 | height=data.dxw[index]) |
---|
[7cf2cfd] | 119 | else: |
---|
[803f835] | 120 | res = resolution.Perfect1D(data.x[index]) |
---|
[7cf2cfd] | 121 | |
---|
| 122 | #self._theory = np.zeros_like(self.Iq) |
---|
[803f835] | 123 | q_vectors = [res.q_calc] |
---|
[02e70ff] | 124 | q_mono = [] |
---|
[7cf2cfd] | 125 | else: |
---|
| 126 | raise ValueError("Unknown data type") # never gets here |
---|
| 127 | |
---|
| 128 | # Remember function inputs so we can delay loading the function and |
---|
| 129 | # so we can save/restore state |
---|
[02e70ff] | 130 | self._kernel_inputs = q_vectors |
---|
| 131 | self._kernel_mono_inputs = q_mono |
---|
[7cf2cfd] | 132 | self._kernel = None |
---|
[803f835] | 133 | self.Iq, self.dIq, self.index = Iq, dIq, index |
---|
| 134 | self.resolution = res |
---|
[7cf2cfd] | 135 | |
---|
| 136 | def _set_data(self, Iq, noise=None): |
---|
[803f835] | 137 | # pylint: disable=attribute-defined-outside-init |
---|
[7cf2cfd] | 138 | if noise is not None: |
---|
| 139 | self.dIq = Iq*noise*0.01 |
---|
| 140 | dy = self.dIq |
---|
| 141 | y = Iq + np.random.randn(*dy.shape) * dy |
---|
| 142 | self.Iq = y |
---|
| 143 | if self.data_type == 'Iq': |
---|
| 144 | self._data.dy[self.index] = dy |
---|
| 145 | self._data.y[self.index] = y |
---|
| 146 | elif self.data_type == 'Iqxy': |
---|
| 147 | self._data.data[self.index] = y |
---|
| 148 | elif self.data_type == 'sesans': |
---|
| 149 | self._data.y[self.index] = y |
---|
| 150 | else: |
---|
| 151 | raise ValueError("Unknown model") |
---|
| 152 | |
---|
| 153 | def _calc_theory(self, pars, cutoff=0.0): |
---|
| 154 | if self._kernel is None: |
---|
[02e70ff] | 155 | self._kernel = make_kernel(self._model, self._kernel_inputs) # pylint: disable=attribute-dedata_type |
---|
| 156 | self._kernel_mono = make_kernel(self._model, self._kernel_mono_inputs) if self._kernel_mono_inputs else None |
---|
[7cf2cfd] | 157 | |
---|
| 158 | Iq_calc = call_kernel(self._kernel, pars, cutoff=cutoff) |
---|
[02e70ff] | 159 | Iq_mono = call_kernel(self._kernel_mono, pars, mono=True) if self._kernel_mono_inputs else None |
---|
[7cf2cfd] | 160 | if self.data_type == 'sesans': |
---|
[02e70ff] | 161 | result = sesans.transform(self._data, |
---|
| 162 | self._kernel_inputs[0], Iq_calc, |
---|
| 163 | self._kernel_mono_inputs, Iq_mono) |
---|
[7cf2cfd] | 164 | else: |
---|
| 165 | result = self.resolution.apply(Iq_calc) |
---|
[02e70ff] | 166 | return result |
---|
[7cf2cfd] | 167 | |
---|
| 168 | |
---|
| 169 | class DirectModel(DataMixin): |
---|
[803f835] | 170 | """ |
---|
| 171 | Create a calculator object for a model. |
---|
| 172 | |
---|
| 173 | *data* is 1D SAS, 2D SAS or SESANS data |
---|
| 174 | |
---|
| 175 | *model* is a model calculator return from :func:`generate.load_model` |
---|
| 176 | |
---|
| 177 | *cutoff* is the polydispersity weight cutoff. |
---|
| 178 | """ |
---|
[7cf2cfd] | 179 | def __init__(self, data, model, cutoff=1e-5): |
---|
| 180 | self.model = model |
---|
| 181 | self.cutoff = cutoff |
---|
[803f835] | 182 | # Note: _interpret_data defines the model attributes |
---|
[7cf2cfd] | 183 | self._interpret_data(data, model) |
---|
[803f835] | 184 | |
---|
[16bc3fc] | 185 | def __call__(self, **pars): |
---|
[7cf2cfd] | 186 | return self._calc_theory(pars, cutoff=self.cutoff) |
---|
[803f835] | 187 | |
---|
[17bbadd] | 188 | def ER_VR(self, **pars): |
---|
[803f835] | 189 | """ |
---|
[17bbadd] | 190 | Compute the equivalent radius and volume ratio for the model. |
---|
[803f835] | 191 | """ |
---|
[17bbadd] | 192 | return call_ER_VR(self.model.info, pars) |
---|
[803f835] | 193 | |
---|
[7cf2cfd] | 194 | def simulate_data(self, noise=None, **pars): |
---|
[803f835] | 195 | """ |
---|
| 196 | Generate simulated data for the model. |
---|
| 197 | """ |
---|
[7cf2cfd] | 198 | Iq = self.__call__(**pars) |
---|
| 199 | self._set_data(Iq, noise=noise) |
---|
[ae7b97b] | 200 | |
---|
[803f835] | 201 | def main(): |
---|
| 202 | """ |
---|
| 203 | Program to evaluate a particular model at a set of q values. |
---|
| 204 | """ |
---|
[ae7b97b] | 205 | import sys |
---|
[7cf2cfd] | 206 | from .data import empty_data1D, empty_data2D |
---|
[17bbadd] | 207 | from .core import load_model_info, build_model |
---|
[7cf2cfd] | 208 | |
---|
[ae7b97b] | 209 | if len(sys.argv) < 3: |
---|
[9404dd3] | 210 | print("usage: python -m sasmodels.direct_model modelname (q|qx,qy) par=val ...") |
---|
[ae7b97b] | 211 | sys.exit(1) |
---|
| 212 | model_name = sys.argv[1] |
---|
[aa4946b] | 213 | call = sys.argv[2].upper() |
---|
[17bbadd] | 214 | if call != "ER_VR": |
---|
[7cf2cfd] | 215 | try: |
---|
| 216 | values = [float(v) for v in call.split(',')] |
---|
| 217 | except: |
---|
| 218 | values = [] |
---|
[aa4946b] | 219 | if len(values) == 1: |
---|
[7cf2cfd] | 220 | q, = values |
---|
| 221 | data = empty_data1D([q]) |
---|
[aa4946b] | 222 | elif len(values) == 2: |
---|
[803f835] | 223 | qx, qy = values |
---|
| 224 | data = empty_data2D([qx], [qy]) |
---|
[aa4946b] | 225 | else: |
---|
[9404dd3] | 226 | print("use q or qx,qy or ER or VR") |
---|
[aa4946b] | 227 | sys.exit(1) |
---|
[7cf2cfd] | 228 | else: |
---|
| 229 | data = empty_data1D([0.001]) # Data not used in ER/VR |
---|
| 230 | |
---|
[17bbadd] | 231 | model_info = load_model_info(model_name) |
---|
| 232 | model = build_model(model_info) |
---|
[7cf2cfd] | 233 | calculator = DirectModel(data, model) |
---|
[803f835] | 234 | pars = dict((k, float(v)) |
---|
[ae7b97b] | 235 | for pair in sys.argv[3:] |
---|
[803f835] | 236 | for k, v in [pair.split('=')]) |
---|
[17bbadd] | 237 | if call == "ER_VR": |
---|
| 238 | print(calculator.ER_VR(**pars)) |
---|
[aa4946b] | 239 | else: |
---|
[7cf2cfd] | 240 | Iq = calculator(**pars) |
---|
[9404dd3] | 241 | print(Iq[0]) |
---|
[ae7b97b] | 242 | |
---|
| 243 | if __name__ == "__main__": |
---|
[803f835] | 244 | main() |
---|