[8a20be5] | 1 | #!/usr/bin/env python |
---|
| 2 | # -*- coding: utf-8 -*- |
---|
[caeb06d] | 3 | """ |
---|
| 4 | Program to compare models using different compute engines. |
---|
| 5 | |
---|
| 6 | This program lets you compare results between OpenCL and DLL versions |
---|
| 7 | of the code and between precision (half, fast, single, double, quad), |
---|
| 8 | where fast precision is single precision using native functions for |
---|
| 9 | trig, etc., and may not be completely IEEE 754 compliant. This lets |
---|
| 10 | make sure that the model calculations are stable, or if you need to |
---|
[9cfcac8] | 11 | tag the model as double precision only. |
---|
[caeb06d] | 12 | |
---|
[9cfcac8] | 13 | Run using ./compare.sh (Linux, Mac) or compare.bat (Windows) in the |
---|
[caeb06d] | 14 | sasmodels root to see the command line options. |
---|
| 15 | |
---|
[9cfcac8] | 16 | Note that there is no way within sasmodels to select between an |
---|
| 17 | OpenCL CPU device and a GPU device, but you can do so by setting the |
---|
[caeb06d] | 18 | PYOPENCL_CTX environment variable ahead of time. Start a python |
---|
| 19 | interpreter and enter:: |
---|
| 20 | |
---|
| 21 | import pyopencl as cl |
---|
| 22 | cl.create_some_context() |
---|
| 23 | |
---|
| 24 | This will prompt you to select from the available OpenCL devices |
---|
| 25 | and tell you which string to use for the PYOPENCL_CTX variable. |
---|
| 26 | On Windows you will need to remove the quotes. |
---|
| 27 | """ |
---|
| 28 | |
---|
| 29 | from __future__ import print_function |
---|
| 30 | |
---|
[190fc2b] | 31 | import sys |
---|
| 32 | import math |
---|
| 33 | import datetime |
---|
| 34 | import traceback |
---|
| 35 | |
---|
[7ae2b7f] | 36 | import numpy as np # type: ignore |
---|
[190fc2b] | 37 | |
---|
| 38 | from . import core |
---|
| 39 | from . import kerneldll |
---|
| 40 | from .data import plot_theory, empty_data1D, empty_data2D |
---|
| 41 | from .direct_model import DirectModel |
---|
[f247314] | 42 | from .convert import revert_name, revert_pars, constrain_new_to_old |
---|
[190fc2b] | 43 | |
---|
[dd7fc12] | 44 | try: |
---|
| 45 | from typing import Optional, Dict, Any, Callable, Tuple |
---|
| 46 | except: |
---|
| 47 | pass |
---|
| 48 | else: |
---|
| 49 | from .modelinfo import ModelInfo, Parameter, ParameterSet |
---|
| 50 | from .data import Data |
---|
[8d62008] | 51 | Calculator = Callable[[float], np.ndarray] |
---|
[dd7fc12] | 52 | |
---|
[caeb06d] | 53 | USAGE = """ |
---|
| 54 | usage: compare.py model N1 N2 [options...] [key=val] |
---|
| 55 | |
---|
| 56 | Compare the speed and value for a model between the SasView original and the |
---|
| 57 | sasmodels rewrite. |
---|
| 58 | |
---|
| 59 | model is the name of the model to compare (see below). |
---|
| 60 | N1 is the number of times to run sasmodels (default=1). |
---|
| 61 | N2 is the number times to run sasview (default=1). |
---|
| 62 | |
---|
| 63 | Options (* for default): |
---|
| 64 | |
---|
| 65 | -plot*/-noplot plots or suppress the plot of the model |
---|
| 66 | -lowq*/-midq/-highq/-exq use q values up to 0.05, 0.2, 1.0, 10.0 |
---|
| 67 | -nq=128 sets the number of Q points in the data set |
---|
[e78edc4] | 68 | -zero indicates that q=0 should be included |
---|
[caeb06d] | 69 | -1d*/-2d computes 1d or 2d data |
---|
| 70 | -preset*/-random[=seed] preset or random parameters |
---|
| 71 | -mono/-poly* force monodisperse/polydisperse |
---|
| 72 | -cutoff=1e-5* cutoff value for including a point in polydispersity |
---|
| 73 | -pars/-nopars* prints the parameter set or not |
---|
| 74 | -abs/-rel* plot relative or absolute error |
---|
| 75 | -linear/-log*/-q4 intensity scaling |
---|
| 76 | -hist/-nohist* plot histogram of relative error |
---|
| 77 | -res=0 sets the resolution width dQ/Q if calculating with resolution |
---|
| 78 | -accuracy=Low accuracy of the resolution calculation Low, Mid, High, Xhigh |
---|
| 79 | -edit starts the parameter explorer |
---|
[98d6cfc] | 80 | -default/-demo* use demo vs default parameters |
---|
[caeb06d] | 81 | |
---|
| 82 | Any two calculation engines can be selected for comparison: |
---|
| 83 | |
---|
| 84 | -single/-double/-half/-fast sets an OpenCL calculation engine |
---|
| 85 | -single!/-double!/-quad! sets an OpenMP calculation engine |
---|
| 86 | -sasview sets the sasview calculation engine |
---|
| 87 | |
---|
| 88 | The default is -single -sasview. Note that the interpretation of quad |
---|
| 89 | precision depends on architecture, and may vary from 64-bit to 128-bit, |
---|
| 90 | with 80-bit floats being common (1e-19 precision). |
---|
| 91 | |
---|
| 92 | Key=value pairs allow you to set specific values for the model parameters. |
---|
| 93 | """ |
---|
| 94 | |
---|
| 95 | # Update docs with command line usage string. This is separate from the usual |
---|
| 96 | # doc string so that we can display it at run time if there is an error. |
---|
| 97 | # lin |
---|
[d15a908] | 98 | __doc__ = (__doc__ # pylint: disable=redefined-builtin |
---|
| 99 | + """ |
---|
[caeb06d] | 100 | Program description |
---|
| 101 | ------------------- |
---|
| 102 | |
---|
[d15a908] | 103 | """ |
---|
| 104 | + USAGE) |
---|
[caeb06d] | 105 | |
---|
[750ffa5] | 106 | kerneldll.ALLOW_SINGLE_PRECISION_DLLS = True |
---|
[87985ca] | 107 | |
---|
[7cf2cfd] | 108 | # CRUFT python 2.6 |
---|
| 109 | if not hasattr(datetime.timedelta, 'total_seconds'): |
---|
| 110 | def delay(dt): |
---|
| 111 | """Return number date-time delta as number seconds""" |
---|
| 112 | return dt.days * 86400 + dt.seconds + 1e-6 * dt.microseconds |
---|
| 113 | else: |
---|
| 114 | def delay(dt): |
---|
| 115 | """Return number date-time delta as number seconds""" |
---|
| 116 | return dt.total_seconds() |
---|
| 117 | |
---|
| 118 | |
---|
[4f2478e] | 119 | class push_seed(object): |
---|
| 120 | """ |
---|
| 121 | Set the seed value for the random number generator. |
---|
| 122 | |
---|
| 123 | When used in a with statement, the random number generator state is |
---|
| 124 | restored after the with statement is complete. |
---|
| 125 | |
---|
| 126 | :Parameters: |
---|
| 127 | |
---|
| 128 | *seed* : int or array_like, optional |
---|
| 129 | Seed for RandomState |
---|
| 130 | |
---|
| 131 | :Example: |
---|
| 132 | |
---|
| 133 | Seed can be used directly to set the seed:: |
---|
| 134 | |
---|
| 135 | >>> from numpy.random import randint |
---|
| 136 | >>> push_seed(24) |
---|
| 137 | <...push_seed object at...> |
---|
| 138 | >>> print(randint(0,1000000,3)) |
---|
| 139 | [242082 899 211136] |
---|
| 140 | |
---|
| 141 | Seed can also be used in a with statement, which sets the random |
---|
| 142 | number generator state for the enclosed computations and restores |
---|
| 143 | it to the previous state on completion:: |
---|
| 144 | |
---|
| 145 | >>> with push_seed(24): |
---|
| 146 | ... print(randint(0,1000000,3)) |
---|
| 147 | [242082 899 211136] |
---|
| 148 | |
---|
| 149 | Using nested contexts, we can demonstrate that state is indeed |
---|
| 150 | restored after the block completes:: |
---|
| 151 | |
---|
| 152 | >>> with push_seed(24): |
---|
| 153 | ... print(randint(0,1000000)) |
---|
| 154 | ... with push_seed(24): |
---|
| 155 | ... print(randint(0,1000000,3)) |
---|
| 156 | ... print(randint(0,1000000)) |
---|
| 157 | 242082 |
---|
| 158 | [242082 899 211136] |
---|
| 159 | 899 |
---|
| 160 | |
---|
| 161 | The restore step is protected against exceptions in the block:: |
---|
| 162 | |
---|
| 163 | >>> with push_seed(24): |
---|
| 164 | ... print(randint(0,1000000)) |
---|
| 165 | ... try: |
---|
| 166 | ... with push_seed(24): |
---|
| 167 | ... print(randint(0,1000000,3)) |
---|
| 168 | ... raise Exception() |
---|
[dd7fc12] | 169 | ... except Exception: |
---|
[4f2478e] | 170 | ... print("Exception raised") |
---|
| 171 | ... print(randint(0,1000000)) |
---|
| 172 | 242082 |
---|
| 173 | [242082 899 211136] |
---|
| 174 | Exception raised |
---|
| 175 | 899 |
---|
| 176 | """ |
---|
| 177 | def __init__(self, seed=None): |
---|
[dd7fc12] | 178 | # type: (Optional[int]) -> None |
---|
[4f2478e] | 179 | self._state = np.random.get_state() |
---|
| 180 | np.random.seed(seed) |
---|
| 181 | |
---|
| 182 | def __enter__(self): |
---|
[dd7fc12] | 183 | # type: () -> None |
---|
| 184 | pass |
---|
[4f2478e] | 185 | |
---|
[b32dafd] | 186 | def __exit__(self, exc_type, exc_value, traceback): |
---|
[dd7fc12] | 187 | # type: (Any, BaseException, Any) -> None |
---|
| 188 | # TODO: better typing for __exit__ method |
---|
[4f2478e] | 189 | np.random.set_state(self._state) |
---|
| 190 | |
---|
[7cf2cfd] | 191 | def tic(): |
---|
[dd7fc12] | 192 | # type: () -> Callable[[], float] |
---|
[7cf2cfd] | 193 | """ |
---|
| 194 | Timer function. |
---|
| 195 | |
---|
| 196 | Use "toc=tic()" to start the clock and "toc()" to measure |
---|
| 197 | a time interval. |
---|
| 198 | """ |
---|
| 199 | then = datetime.datetime.now() |
---|
| 200 | return lambda: delay(datetime.datetime.now() - then) |
---|
| 201 | |
---|
| 202 | |
---|
| 203 | def set_beam_stop(data, radius, outer=None): |
---|
[dd7fc12] | 204 | # type: (Data, float, float) -> None |
---|
[7cf2cfd] | 205 | """ |
---|
| 206 | Add a beam stop of the given *radius*. If *outer*, make an annulus. |
---|
| 207 | |
---|
[dd7fc12] | 208 | Note: this function does not require sasview |
---|
[7cf2cfd] | 209 | """ |
---|
| 210 | if hasattr(data, 'qx_data'): |
---|
| 211 | q = np.sqrt(data.qx_data**2 + data.qy_data**2) |
---|
| 212 | data.mask = (q < radius) |
---|
| 213 | if outer is not None: |
---|
| 214 | data.mask |= (q >= outer) |
---|
| 215 | else: |
---|
| 216 | data.mask = (data.x < radius) |
---|
| 217 | if outer is not None: |
---|
| 218 | data.mask |= (data.x >= outer) |
---|
| 219 | |
---|
[8a20be5] | 220 | |
---|
[ec7e360] | 221 | def parameter_range(p, v): |
---|
[dd7fc12] | 222 | # type: (str, float) -> Tuple[float, float] |
---|
[87985ca] | 223 | """ |
---|
[ec7e360] | 224 | Choose a parameter range based on parameter name and initial value. |
---|
[87985ca] | 225 | """ |
---|
[8bd7b77] | 226 | # process the polydispersity options |
---|
[ec7e360] | 227 | if p.endswith('_pd_n'): |
---|
[dd7fc12] | 228 | return 0., 100. |
---|
[ec7e360] | 229 | elif p.endswith('_pd_nsigma'): |
---|
[dd7fc12] | 230 | return 0., 5. |
---|
[ec7e360] | 231 | elif p.endswith('_pd_type'): |
---|
[dd7fc12] | 232 | raise ValueError("Cannot return a range for a string value") |
---|
[caeb06d] | 233 | elif any(s in p for s in ('theta', 'phi', 'psi')): |
---|
[87985ca] | 234 | # orientation in [-180,180], orientation pd in [0,45] |
---|
| 235 | if p.endswith('_pd'): |
---|
[dd7fc12] | 236 | return 0., 45. |
---|
[87985ca] | 237 | else: |
---|
[dd7fc12] | 238 | return -180., 180. |
---|
[87985ca] | 239 | elif p.endswith('_pd'): |
---|
[dd7fc12] | 240 | return 0., 1. |
---|
[8bd7b77] | 241 | elif 'sld' in p: |
---|
[dd7fc12] | 242 | return -0.5, 10. |
---|
[eb46451] | 243 | elif p == 'background': |
---|
[dd7fc12] | 244 | return 0., 10. |
---|
[eb46451] | 245 | elif p == 'scale': |
---|
[dd7fc12] | 246 | return 0., 1.e3 |
---|
| 247 | elif v < 0.: |
---|
| 248 | return 2.*v, -2.*v |
---|
[87985ca] | 249 | else: |
---|
[dd7fc12] | 250 | return 0., (2.*v if v > 0. else 1.) |
---|
[87985ca] | 251 | |
---|
[4f2478e] | 252 | |
---|
[8bd7b77] | 253 | def _randomize_one(model_info, p, v): |
---|
[dd7fc12] | 254 | # type: (ModelInfo, str, float) -> float |
---|
| 255 | # type: (ModelInfo, str, str) -> str |
---|
[ec7e360] | 256 | """ |
---|
[caeb06d] | 257 | Randomize a single parameter. |
---|
[ec7e360] | 258 | """ |
---|
[f3bd37f] | 259 | if any(p.endswith(s) for s in ('_pd', '_pd_n', '_pd_nsigma', '_pd_type')): |
---|
[ec7e360] | 260 | return v |
---|
[8bd7b77] | 261 | |
---|
| 262 | # Find the parameter definition |
---|
[6d6508e] | 263 | for par in model_info.parameters.call_parameters: |
---|
[8bd7b77] | 264 | if par.name == p: |
---|
| 265 | break |
---|
[ec7e360] | 266 | else: |
---|
[8bd7b77] | 267 | raise ValueError("unknown parameter %r"%p) |
---|
| 268 | |
---|
| 269 | if len(par.limits) > 2: # choice list |
---|
| 270 | return np.random.randint(len(par.limits)) |
---|
| 271 | |
---|
| 272 | limits = par.limits |
---|
| 273 | if not np.isfinite(limits).all(): |
---|
| 274 | low, high = parameter_range(p, v) |
---|
| 275 | limits = (max(limits[0], low), min(limits[1], high)) |
---|
| 276 | |
---|
| 277 | return np.random.uniform(*limits) |
---|
[cd3dba0] | 278 | |
---|
[4f2478e] | 279 | |
---|
[8bd7b77] | 280 | def randomize_pars(model_info, pars, seed=None): |
---|
[dd7fc12] | 281 | # type: (ModelInfo, ParameterSet, int) -> ParameterSet |
---|
[caeb06d] | 282 | """ |
---|
| 283 | Generate random values for all of the parameters. |
---|
| 284 | |
---|
| 285 | Valid ranges for the random number generator are guessed from the name of |
---|
| 286 | the parameter; this will not account for constraints such as cap radius |
---|
| 287 | greater than cylinder radius in the capped_cylinder model, so |
---|
| 288 | :func:`constrain_pars` needs to be called afterward.. |
---|
| 289 | """ |
---|
[4f2478e] | 290 | with push_seed(seed): |
---|
| 291 | # Note: the sort guarantees order `of calls to random number generator |
---|
[dd7fc12] | 292 | random_pars = dict((p, _randomize_one(model_info, p, v)) |
---|
| 293 | for p, v in sorted(pars.items())) |
---|
| 294 | return random_pars |
---|
[cd3dba0] | 295 | |
---|
[17bbadd] | 296 | def constrain_pars(model_info, pars): |
---|
[dd7fc12] | 297 | # type: (ModelInfo, ParameterSet) -> None |
---|
[9a66e65] | 298 | """ |
---|
| 299 | Restrict parameters to valid values. |
---|
[caeb06d] | 300 | |
---|
| 301 | This includes model specific code for models such as capped_cylinder |
---|
| 302 | which need to support within model constraints (cap radius more than |
---|
| 303 | cylinder radius in this case). |
---|
[dd7fc12] | 304 | |
---|
| 305 | Warning: this updates the *pars* dictionary in place. |
---|
[9a66e65] | 306 | """ |
---|
[6d6508e] | 307 | name = model_info.id |
---|
[17bbadd] | 308 | # if it is a product model, then just look at the form factor since |
---|
| 309 | # none of the structure factors need any constraints. |
---|
| 310 | if '*' in name: |
---|
| 311 | name = name.split('*')[0] |
---|
| 312 | |
---|
[216a9e1] | 313 | if name == 'capped_cylinder' and pars['cap_radius'] < pars['radius']: |
---|
[caeb06d] | 314 | pars['radius'], pars['cap_radius'] = pars['cap_radius'], pars['radius'] |
---|
[b514adf] | 315 | if name == 'barbell' and pars['bell_radius'] < pars['radius']: |
---|
[caeb06d] | 316 | pars['radius'], pars['bell_radius'] = pars['bell_radius'], pars['radius'] |
---|
[b514adf] | 317 | |
---|
| 318 | # Limit guinier to an Rg such that Iq > 1e-30 (single precision cutoff) |
---|
| 319 | if name == 'guinier': |
---|
| 320 | #q_max = 0.2 # mid q maximum |
---|
| 321 | q_max = 1.0 # high q maximum |
---|
| 322 | rg_max = np.sqrt(90*np.log(10) + 3*np.log(pars['scale']))/q_max |
---|
[caeb06d] | 323 | pars['rg'] = min(pars['rg'], rg_max) |
---|
[cd3dba0] | 324 | |
---|
[82c299f] | 325 | if name == 'rpa': |
---|
| 326 | # Make sure phi sums to 1.0 |
---|
| 327 | if pars['case_num'] < 2: |
---|
[8bd7b77] | 328 | pars['Phi1'] = 0. |
---|
| 329 | pars['Phi2'] = 0. |
---|
[82c299f] | 330 | elif pars['case_num'] < 5: |
---|
[8bd7b77] | 331 | pars['Phi1'] = 0. |
---|
| 332 | total = sum(pars['Phi'+c] for c in '1234') |
---|
| 333 | for c in '1234': |
---|
[82c299f] | 334 | pars['Phi'+c] /= total |
---|
| 335 | |
---|
[d6850fa] | 336 | def parlist(model_info, pars, is2d): |
---|
[dd7fc12] | 337 | # type: (ModelInfo, ParameterSet, bool) -> str |
---|
[caeb06d] | 338 | """ |
---|
| 339 | Format the parameter list for printing. |
---|
| 340 | """ |
---|
[a4a7308] | 341 | lines = [] |
---|
[6d6508e] | 342 | parameters = model_info.parameters |
---|
[d19962c] | 343 | for p in parameters.user_parameters(pars, is2d): |
---|
| 344 | fields = dict( |
---|
| 345 | value=pars.get(p.id, p.default), |
---|
| 346 | pd=pars.get(p.id+"_pd", 0.), |
---|
| 347 | n=int(pars.get(p.id+"_pd_n", 0)), |
---|
| 348 | nsigma=pars.get(p.id+"_pd_nsgima", 3.), |
---|
[dd7fc12] | 349 | pdtype=pars.get(p.id+"_pd_type", 'gaussian'), |
---|
[bd49c79] | 350 | relative_pd=p.relative_pd, |
---|
[dd7fc12] | 351 | ) |
---|
[d19962c] | 352 | lines.append(_format_par(p.name, **fields)) |
---|
[a4a7308] | 353 | return "\n".join(lines) |
---|
| 354 | |
---|
| 355 | #return "\n".join("%s: %s"%(p, v) for p, v in sorted(pars.items())) |
---|
| 356 | |
---|
[bd49c79] | 357 | def _format_par(name, value=0., pd=0., n=0, nsigma=3., pdtype='gaussian', |
---|
| 358 | relative_pd=False): |
---|
[dd7fc12] | 359 | # type: (str, float, float, int, float, str) -> str |
---|
[a4a7308] | 360 | line = "%s: %g"%(name, value) |
---|
| 361 | if pd != 0. and n != 0: |
---|
[bd49c79] | 362 | if relative_pd: |
---|
| 363 | pd *= value |
---|
[a4a7308] | 364 | line += " +/- %g (%d points in [-%g,%g] sigma %s)"\ |
---|
[dd7fc12] | 365 | % (pd, n, nsigma, nsigma, pdtype) |
---|
[a4a7308] | 366 | return line |
---|
[87985ca] | 367 | |
---|
| 368 | def suppress_pd(pars): |
---|
[dd7fc12] | 369 | # type: (ParameterSet) -> ParameterSet |
---|
[87985ca] | 370 | """ |
---|
| 371 | Suppress theta_pd for now until the normalization is resolved. |
---|
| 372 | |
---|
| 373 | May also suppress complete polydispersity of the model to test |
---|
| 374 | models more quickly. |
---|
| 375 | """ |
---|
[f4f3919] | 376 | pars = pars.copy() |
---|
[87985ca] | 377 | for p in pars: |
---|
[8b25ee1] | 378 | if p.endswith("_pd_n"): pars[p] = 0 |
---|
[f4f3919] | 379 | return pars |
---|
[87985ca] | 380 | |
---|
[17bbadd] | 381 | def eval_sasview(model_info, data): |
---|
[dd7fc12] | 382 | # type: (Modelinfo, Data) -> Calculator |
---|
[caeb06d] | 383 | """ |
---|
[f247314] | 384 | Return a model calculator using the pre-4.0 SasView models. |
---|
[caeb06d] | 385 | """ |
---|
[dc056b9] | 386 | # importing sas here so that the error message will be that sas failed to |
---|
| 387 | # import rather than the more obscure smear_selection not imported error |
---|
[2bebe2b] | 388 | import sas |
---|
[dd7fc12] | 389 | import sas.models |
---|
[8d62008] | 390 | from sas.models.qsmearing import smear_selection |
---|
| 391 | from sas.models.MultiplicationModel import MultiplicationModel |
---|
[ec7e360] | 392 | |
---|
[256dfe1] | 393 | def get_model_class(name): |
---|
[dd7fc12] | 394 | # type: (str) -> "sas.models.BaseComponent" |
---|
[17bbadd] | 395 | #print("new",sorted(_pars.items())) |
---|
[dd7fc12] | 396 | __import__('sas.models.' + name) |
---|
[17bbadd] | 397 | ModelClass = getattr(getattr(sas.models, name, None), name, None) |
---|
| 398 | if ModelClass is None: |
---|
| 399 | raise ValueError("could not find model %r in sas.models"%name) |
---|
[256dfe1] | 400 | return ModelClass |
---|
| 401 | |
---|
| 402 | # WARNING: ugly hack when handling model! |
---|
| 403 | # Sasview models with multiplicity need to be created with the target |
---|
| 404 | # multiplicity, so we cannot create the target model ahead of time for |
---|
| 405 | # for multiplicity models. Instead we store the model in a list and |
---|
| 406 | # update the first element of that list with the new multiplicity model |
---|
| 407 | # every time we evaluate. |
---|
[17bbadd] | 408 | |
---|
| 409 | # grab the sasview model, or create it if it is a product model |
---|
[6d6508e] | 410 | if model_info.composition: |
---|
| 411 | composition_type, parts = model_info.composition |
---|
[17bbadd] | 412 | if composition_type == 'product': |
---|
[51ec7e8] | 413 | P, S = [get_model_class(revert_name(p))() for p in parts] |
---|
[256dfe1] | 414 | model = [MultiplicationModel(P, S)] |
---|
[17bbadd] | 415 | else: |
---|
[72a081d] | 416 | raise ValueError("sasview mixture models not supported by compare") |
---|
[17bbadd] | 417 | else: |
---|
[f3bd37f] | 418 | old_name = revert_name(model_info) |
---|
| 419 | if old_name is None: |
---|
| 420 | raise ValueError("model %r does not exist in old sasview" |
---|
| 421 | % model_info.id) |
---|
[256dfe1] | 422 | ModelClass = get_model_class(old_name) |
---|
| 423 | model = [ModelClass()] |
---|
[216a9e1] | 424 | |
---|
[17bbadd] | 425 | # build a smearer with which to call the model, if necessary |
---|
| 426 | smearer = smear_selection(data, model=model) |
---|
[ec7e360] | 427 | if hasattr(data, 'qx_data'): |
---|
| 428 | q = np.sqrt(data.qx_data**2 + data.qy_data**2) |
---|
| 429 | index = ((~data.mask) & (~np.isnan(data.data)) |
---|
| 430 | & (q >= data.qmin) & (q <= data.qmax)) |
---|
| 431 | if smearer is not None: |
---|
| 432 | smearer.model = model # because smear_selection has a bug |
---|
| 433 | smearer.accuracy = data.accuracy |
---|
| 434 | smearer.set_index(index) |
---|
[256dfe1] | 435 | def _call_smearer(): |
---|
| 436 | smearer.model = model[0] |
---|
| 437 | return smearer.get_value() |
---|
[b32dafd] | 438 | theory = _call_smearer |
---|
[ec7e360] | 439 | else: |
---|
[256dfe1] | 440 | theory = lambda: model[0].evalDistribution([data.qx_data[index], |
---|
| 441 | data.qy_data[index]]) |
---|
[ec7e360] | 442 | elif smearer is not None: |
---|
[256dfe1] | 443 | theory = lambda: smearer(model[0].evalDistribution(data.x)) |
---|
[ec7e360] | 444 | else: |
---|
[256dfe1] | 445 | theory = lambda: model[0].evalDistribution(data.x) |
---|
[ec7e360] | 446 | |
---|
| 447 | def calculator(**pars): |
---|
[dd7fc12] | 448 | # type: (float, ...) -> np.ndarray |
---|
[caeb06d] | 449 | """ |
---|
| 450 | Sasview calculator for model. |
---|
| 451 | """ |
---|
[256dfe1] | 452 | oldpars = revert_pars(model_info, pars) |
---|
[bd49c79] | 453 | # For multiplicity models, create a model with the correct multiplicity |
---|
| 454 | control = oldpars.pop("CONTROL", None) |
---|
| 455 | if control is not None: |
---|
| 456 | # sphericalSLD has one fewer multiplicity. This update should |
---|
| 457 | # happen in revert_pars, but it hasn't been called yet. |
---|
| 458 | model[0] = ModelClass(control) |
---|
| 459 | # paying for parameter conversion each time to keep life simple, if not fast |
---|
[f67f26c] | 460 | #print("sasview pars",oldpars) |
---|
[256dfe1] | 461 | for k, v in oldpars.items(): |
---|
[dd7fc12] | 462 | name_attr = k.split('.') # polydispersity components |
---|
| 463 | if len(name_attr) == 2: |
---|
[256dfe1] | 464 | model[0].dispersion[name_attr[0]][name_attr[1]] = v |
---|
[ec7e360] | 465 | else: |
---|
[256dfe1] | 466 | model[0].setParam(k, v) |
---|
[ec7e360] | 467 | return theory() |
---|
| 468 | |
---|
| 469 | calculator.engine = "sasview" |
---|
| 470 | return calculator |
---|
| 471 | |
---|
| 472 | DTYPE_MAP = { |
---|
| 473 | 'half': '16', |
---|
| 474 | 'fast': 'fast', |
---|
| 475 | 'single': '32', |
---|
| 476 | 'double': '64', |
---|
| 477 | 'quad': '128', |
---|
| 478 | 'f16': '16', |
---|
| 479 | 'f32': '32', |
---|
| 480 | 'f64': '64', |
---|
| 481 | 'longdouble': '128', |
---|
| 482 | } |
---|
[17bbadd] | 483 | def eval_opencl(model_info, data, dtype='single', cutoff=0.): |
---|
[dd7fc12] | 484 | # type: (ModelInfo, Data, str, float) -> Calculator |
---|
[caeb06d] | 485 | """ |
---|
| 486 | Return a model calculator using the OpenCL calculation engine. |
---|
| 487 | """ |
---|
[a738209] | 488 | if not core.HAVE_OPENCL: |
---|
| 489 | raise RuntimeError("OpenCL not available") |
---|
| 490 | model = core.build_model(model_info, dtype=dtype, platform="ocl") |
---|
[7cf2cfd] | 491 | calculator = DirectModel(data, model, cutoff=cutoff) |
---|
[ec7e360] | 492 | calculator.engine = "OCL%s"%DTYPE_MAP[dtype] |
---|
| 493 | return calculator |
---|
[216a9e1] | 494 | |
---|
[17bbadd] | 495 | def eval_ctypes(model_info, data, dtype='double', cutoff=0.): |
---|
[dd7fc12] | 496 | # type: (ModelInfo, Data, str, float) -> Calculator |
---|
[9cfcac8] | 497 | """ |
---|
| 498 | Return a model calculator using the DLL calculation engine. |
---|
| 499 | """ |
---|
[72a081d] | 500 | model = core.build_model(model_info, dtype=dtype, platform="dll") |
---|
[7cf2cfd] | 501 | calculator = DirectModel(data, model, cutoff=cutoff) |
---|
[ec7e360] | 502 | calculator.engine = "OMP%s"%DTYPE_MAP[dtype] |
---|
| 503 | return calculator |
---|
| 504 | |
---|
[b32dafd] | 505 | def time_calculation(calculator, pars, evals=1): |
---|
[dd7fc12] | 506 | # type: (Calculator, ParameterSet, int) -> Tuple[np.ndarray, float] |
---|
[caeb06d] | 507 | """ |
---|
| 508 | Compute the average calculation time over N evaluations. |
---|
| 509 | |
---|
| 510 | An additional call is generated without polydispersity in order to |
---|
| 511 | initialize the calculation engine, and make the average more stable. |
---|
| 512 | """ |
---|
[ec7e360] | 513 | # initialize the code so time is more accurate |
---|
[b32dafd] | 514 | if evals > 1: |
---|
[dd7fc12] | 515 | calculator(**suppress_pd(pars)) |
---|
[216a9e1] | 516 | toc = tic() |
---|
[dd7fc12] | 517 | # make sure there is at least one eval |
---|
| 518 | value = calculator(**pars) |
---|
[b32dafd] | 519 | for _ in range(evals-1): |
---|
[7cf2cfd] | 520 | value = calculator(**pars) |
---|
[b32dafd] | 521 | average_time = toc()*1000. / evals |
---|
[f2f67a6] | 522 | #print("I(q)",value) |
---|
[216a9e1] | 523 | return value, average_time |
---|
| 524 | |
---|
[ec7e360] | 525 | def make_data(opts): |
---|
[dd7fc12] | 526 | # type: (Dict[str, Any]) -> Tuple[Data, np.ndarray] |
---|
[caeb06d] | 527 | """ |
---|
| 528 | Generate an empty dataset, used with the model to set Q points |
---|
| 529 | and resolution. |
---|
| 530 | |
---|
| 531 | *opts* contains the options, with 'qmax', 'nq', 'res', |
---|
| 532 | 'accuracy', 'is2d' and 'view' parsed from the command line. |
---|
| 533 | """ |
---|
[ec7e360] | 534 | qmax, nq, res = opts['qmax'], opts['nq'], opts['res'] |
---|
| 535 | if opts['is2d']: |
---|
[dd7fc12] | 536 | q = np.linspace(-qmax, qmax, nq) # type: np.ndarray |
---|
| 537 | data = empty_data2D(q, resolution=res) |
---|
[ec7e360] | 538 | data.accuracy = opts['accuracy'] |
---|
[ea75043] | 539 | set_beam_stop(data, 0.0004) |
---|
[87985ca] | 540 | index = ~data.mask |
---|
[216a9e1] | 541 | else: |
---|
[e78edc4] | 542 | if opts['view'] == 'log' and not opts['zero']: |
---|
[b89f519] | 543 | qmax = math.log10(qmax) |
---|
[ec7e360] | 544 | q = np.logspace(qmax-3, qmax, nq) |
---|
[b89f519] | 545 | else: |
---|
[ec7e360] | 546 | q = np.linspace(0.001*qmax, qmax, nq) |
---|
[e78edc4] | 547 | if opts['zero']: |
---|
| 548 | q = np.hstack((0, q)) |
---|
[ec7e360] | 549 | data = empty_data1D(q, resolution=res) |
---|
[216a9e1] | 550 | index = slice(None, None) |
---|
| 551 | return data, index |
---|
| 552 | |
---|
[17bbadd] | 553 | def make_engine(model_info, data, dtype, cutoff): |
---|
[dd7fc12] | 554 | # type: (ModelInfo, Data, str, float) -> Calculator |
---|
[caeb06d] | 555 | """ |
---|
| 556 | Generate the appropriate calculation engine for the given datatype. |
---|
| 557 | |
---|
| 558 | Datatypes with '!' appended are evaluated using external C DLLs rather |
---|
| 559 | than OpenCL. |
---|
| 560 | """ |
---|
[ec7e360] | 561 | if dtype == 'sasview': |
---|
[17bbadd] | 562 | return eval_sasview(model_info, data) |
---|
[ec7e360] | 563 | elif dtype.endswith('!'): |
---|
[17bbadd] | 564 | return eval_ctypes(model_info, data, dtype=dtype[:-1], cutoff=cutoff) |
---|
[ec7e360] | 565 | else: |
---|
[17bbadd] | 566 | return eval_opencl(model_info, data, dtype=dtype, cutoff=cutoff) |
---|
[87985ca] | 567 | |
---|
[e78edc4] | 568 | def _show_invalid(data, theory): |
---|
[dd7fc12] | 569 | # type: (Data, np.ma.ndarray) -> None |
---|
| 570 | """ |
---|
| 571 | Display a list of the non-finite values in theory. |
---|
| 572 | """ |
---|
[e78edc4] | 573 | if not theory.mask.any(): |
---|
| 574 | return |
---|
| 575 | |
---|
| 576 | if hasattr(data, 'x'): |
---|
| 577 | bad = zip(data.x[theory.mask], theory[theory.mask]) |
---|
[dd7fc12] | 578 | print(" *** ", ", ".join("I(%g)=%g"%(x, y) for x, y in bad)) |
---|
[e78edc4] | 579 | |
---|
| 580 | |
---|
[013adb7] | 581 | def compare(opts, limits=None): |
---|
[dd7fc12] | 582 | # type: (Dict[str, Any], Optional[Tuple[float, float]]) -> Tuple[float, float] |
---|
[caeb06d] | 583 | """ |
---|
| 584 | Preform a comparison using options from the command line. |
---|
| 585 | |
---|
| 586 | *limits* are the limits on the values to use, either to set the y-axis |
---|
| 587 | for 1D or to set the colormap scale for 2D. If None, then they are |
---|
| 588 | inferred from the data and returned. When exploring using Bumps, |
---|
| 589 | the limits are set when the model is initially called, and maintained |
---|
| 590 | as the values are adjusted, making it easier to see the effects of the |
---|
| 591 | parameters. |
---|
| 592 | """ |
---|
[b32dafd] | 593 | n_base, n_comp = opts['n1'], opts['n2'] |
---|
[ec7e360] | 594 | pars = opts['pars'] |
---|
| 595 | data = opts['data'] |
---|
[87985ca] | 596 | |
---|
[dd7fc12] | 597 | # silence the linter |
---|
[b32dafd] | 598 | base = opts['engines'][0] if n_base else None |
---|
| 599 | comp = opts['engines'][1] if n_comp else None |
---|
[dd7fc12] | 600 | base_time = comp_time = None |
---|
| 601 | base_value = comp_value = resid = relerr = None |
---|
| 602 | |
---|
[4b41184] | 603 | # Base calculation |
---|
[b32dafd] | 604 | if n_base > 0: |
---|
[319ab14] | 605 | try: |
---|
[b32dafd] | 606 | base_raw, base_time = time_calculation(base, pars, n_base) |
---|
[dd7fc12] | 607 | base_value = np.ma.masked_invalid(base_raw) |
---|
[af92b73] | 608 | print("%s t=%.2f ms, intensity=%.0f" |
---|
[e78edc4] | 609 | % (base.engine, base_time, base_value.sum())) |
---|
| 610 | _show_invalid(data, base_value) |
---|
[319ab14] | 611 | except ImportError: |
---|
| 612 | traceback.print_exc() |
---|
[b32dafd] | 613 | n_base = 0 |
---|
[4b41184] | 614 | |
---|
| 615 | # Comparison calculation |
---|
[b32dafd] | 616 | if n_comp > 0: |
---|
[7cf2cfd] | 617 | try: |
---|
[b32dafd] | 618 | comp_raw, comp_time = time_calculation(comp, pars, n_comp) |
---|
[dd7fc12] | 619 | comp_value = np.ma.masked_invalid(comp_raw) |
---|
[af92b73] | 620 | print("%s t=%.2f ms, intensity=%.0f" |
---|
[e78edc4] | 621 | % (comp.engine, comp_time, comp_value.sum())) |
---|
| 622 | _show_invalid(data, comp_value) |
---|
[7cf2cfd] | 623 | except ImportError: |
---|
[5753e4e] | 624 | traceback.print_exc() |
---|
[b32dafd] | 625 | n_comp = 0 |
---|
[87985ca] | 626 | |
---|
| 627 | # Compare, but only if computing both forms |
---|
[b32dafd] | 628 | if n_base > 0 and n_comp > 0: |
---|
[ec7e360] | 629 | resid = (base_value - comp_value) |
---|
[b32dafd] | 630 | relerr = resid/np.where(comp_value != 0., abs(comp_value), 1.0) |
---|
[d15a908] | 631 | _print_stats("|%s-%s|" |
---|
| 632 | % (base.engine, comp.engine) + (" "*(3+len(comp.engine))), |
---|
[caeb06d] | 633 | resid) |
---|
[d15a908] | 634 | _print_stats("|(%s-%s)/%s|" |
---|
| 635 | % (base.engine, comp.engine, comp.engine), |
---|
[caeb06d] | 636 | relerr) |
---|
[87985ca] | 637 | |
---|
| 638 | # Plot if requested |
---|
[ec7e360] | 639 | if not opts['plot'] and not opts['explore']: return |
---|
| 640 | view = opts['view'] |
---|
[1726b21] | 641 | import matplotlib.pyplot as plt |
---|
[013adb7] | 642 | if limits is None: |
---|
| 643 | vmin, vmax = np.Inf, -np.Inf |
---|
[b32dafd] | 644 | if n_base > 0: |
---|
[e78edc4] | 645 | vmin = min(vmin, base_value.min()) |
---|
| 646 | vmax = max(vmax, base_value.max()) |
---|
[b32dafd] | 647 | if n_comp > 0: |
---|
[e78edc4] | 648 | vmin = min(vmin, comp_value.min()) |
---|
| 649 | vmax = max(vmax, comp_value.max()) |
---|
[013adb7] | 650 | limits = vmin, vmax |
---|
| 651 | |
---|
[b32dafd] | 652 | if n_base > 0: |
---|
| 653 | if n_comp > 0: plt.subplot(131) |
---|
[841753c] | 654 | plot_theory(data, base_value, view=view, use_data=False, limits=limits) |
---|
[af92b73] | 655 | plt.title("%s t=%.2f ms"%(base.engine, base_time)) |
---|
[ec7e360] | 656 | #cbar_title = "log I" |
---|
[b32dafd] | 657 | if n_comp > 0: |
---|
| 658 | if n_base > 0: plt.subplot(132) |
---|
[841753c] | 659 | plot_theory(data, comp_value, view=view, use_data=False, limits=limits) |
---|
[af92b73] | 660 | plt.title("%s t=%.2f ms"%(comp.engine, comp_time)) |
---|
[7cf2cfd] | 661 | #cbar_title = "log I" |
---|
[b32dafd] | 662 | if n_comp > 0 and n_base > 0: |
---|
[87985ca] | 663 | plt.subplot(133) |
---|
[d5e650d] | 664 | if not opts['rel_err']: |
---|
[caeb06d] | 665 | err, errstr, errview = resid, "abs err", "linear" |
---|
[29f5536] | 666 | else: |
---|
[caeb06d] | 667 | err, errstr, errview = abs(relerr), "rel err", "log" |
---|
[4b41184] | 668 | #err,errstr = base/comp,"ratio" |
---|
[841753c] | 669 | plot_theory(data, None, resid=err, view=errview, use_data=False) |
---|
[d5e650d] | 670 | if view == 'linear': |
---|
| 671 | plt.xscale('linear') |
---|
[e78edc4] | 672 | plt.title("max %s = %.3g"%(errstr, abs(err).max())) |
---|
[7cf2cfd] | 673 | #cbar_title = errstr if errview=="linear" else "log "+errstr |
---|
| 674 | #if is2D: |
---|
| 675 | # h = plt.colorbar() |
---|
| 676 | # h.ax.set_title(cbar_title) |
---|
[0c24a82] | 677 | fig = plt.gcf() |
---|
| 678 | fig.suptitle(opts['name']) |
---|
[ba69383] | 679 | |
---|
[b32dafd] | 680 | if n_comp > 0 and n_base > 0 and '-hist' in opts: |
---|
[ba69383] | 681 | plt.figure() |
---|
[346bc88] | 682 | v = relerr |
---|
[caeb06d] | 683 | v[v == 0] = 0.5*np.min(np.abs(v[v != 0])) |
---|
| 684 | plt.hist(np.log10(np.abs(v)), normed=1, bins=50) |
---|
| 685 | plt.xlabel('log10(err), err = |(%s - %s) / %s|' |
---|
| 686 | % (base.engine, comp.engine, comp.engine)) |
---|
[ba69383] | 687 | plt.ylabel('P(err)') |
---|
[ec7e360] | 688 | plt.title('Distribution of relative error between calculation engines') |
---|
[ba69383] | 689 | |
---|
[ec7e360] | 690 | if not opts['explore']: |
---|
| 691 | plt.show() |
---|
[8a20be5] | 692 | |
---|
[013adb7] | 693 | return limits |
---|
| 694 | |
---|
[0763009] | 695 | def _print_stats(label, err): |
---|
[dd7fc12] | 696 | # type: (str, np.ma.ndarray) -> None |
---|
| 697 | # work with trimmed data, not the full set |
---|
[e78edc4] | 698 | sorted_err = np.sort(abs(err.compressed())) |
---|
[dd7fc12] | 699 | p50 = int((len(sorted_err)-1)*0.50) |
---|
| 700 | p98 = int((len(sorted_err)-1)*0.98) |
---|
[0763009] | 701 | data = [ |
---|
| 702 | "max:%.3e"%sorted_err[-1], |
---|
| 703 | "median:%.3e"%sorted_err[p50], |
---|
| 704 | "98%%:%.3e"%sorted_err[p98], |
---|
[dd7fc12] | 705 | "rms:%.3e"%np.sqrt(np.mean(sorted_err**2)), |
---|
| 706 | "zero-offset:%+.3e"%np.mean(sorted_err), |
---|
[0763009] | 707 | ] |
---|
[caeb06d] | 708 | print(label+" "+" ".join(data)) |
---|
[0763009] | 709 | |
---|
| 710 | |
---|
| 711 | |
---|
[87985ca] | 712 | # =========================================================================== |
---|
| 713 | # |
---|
[216a9e1] | 714 | NAME_OPTIONS = set([ |
---|
[5d316e9] | 715 | 'plot', 'noplot', |
---|
[ec7e360] | 716 | 'half', 'fast', 'single', 'double', |
---|
| 717 | 'single!', 'double!', 'quad!', 'sasview', |
---|
[e78edc4] | 718 | 'lowq', 'midq', 'highq', 'exq', 'zero', |
---|
[5d316e9] | 719 | '2d', '1d', |
---|
| 720 | 'preset', 'random', |
---|
| 721 | 'poly', 'mono', |
---|
| 722 | 'nopars', 'pars', |
---|
| 723 | 'rel', 'abs', |
---|
[b89f519] | 724 | 'linear', 'log', 'q4', |
---|
[5d316e9] | 725 | 'hist', 'nohist', |
---|
[ec7e360] | 726 | 'edit', |
---|
[98d6cfc] | 727 | 'demo', 'default', |
---|
[216a9e1] | 728 | ]) |
---|
| 729 | VALUE_OPTIONS = [ |
---|
| 730 | # Note: random is both a name option and a value option |
---|
[ec7e360] | 731 | 'cutoff', 'random', 'nq', 'res', 'accuracy', |
---|
[87985ca] | 732 | ] |
---|
| 733 | |
---|
[b32dafd] | 734 | def columnize(items, indent="", width=79): |
---|
[dd7fc12] | 735 | # type: (List[str], str, int) -> str |
---|
[caeb06d] | 736 | """ |
---|
[1d4017a] | 737 | Format a list of strings into columns. |
---|
| 738 | |
---|
| 739 | Returns a string with carriage returns ready for printing. |
---|
[caeb06d] | 740 | """ |
---|
[b32dafd] | 741 | column_width = max(len(w) for w in items) + 1 |
---|
[7cf2cfd] | 742 | num_columns = (width - len(indent)) // column_width |
---|
[b32dafd] | 743 | num_rows = len(items) // num_columns |
---|
| 744 | items = items + [""] * (num_rows * num_columns - len(items)) |
---|
| 745 | columns = [items[k*num_rows:(k+1)*num_rows] for k in range(num_columns)] |
---|
[7cf2cfd] | 746 | lines = [" ".join("%-*s"%(column_width, entry) for entry in row) |
---|
| 747 | for row in zip(*columns)] |
---|
| 748 | output = indent + ("\n"+indent).join(lines) |
---|
| 749 | return output |
---|
| 750 | |
---|
| 751 | |
---|
[98d6cfc] | 752 | def get_pars(model_info, use_demo=False): |
---|
[dd7fc12] | 753 | # type: (ModelInfo, bool) -> ParameterSet |
---|
[caeb06d] | 754 | """ |
---|
| 755 | Extract demo parameters from the model definition. |
---|
| 756 | """ |
---|
[ec7e360] | 757 | # Get the default values for the parameters |
---|
[c499331] | 758 | pars = {} |
---|
[6d6508e] | 759 | for p in model_info.parameters.call_parameters: |
---|
[c499331] | 760 | parts = [('', p.default)] |
---|
| 761 | if p.polydisperse: |
---|
| 762 | parts.append(('_pd', 0.0)) |
---|
| 763 | parts.append(('_pd_n', 0)) |
---|
| 764 | parts.append(('_pd_nsigma', 3.0)) |
---|
| 765 | parts.append(('_pd_type', "gaussian")) |
---|
| 766 | for ext, val in parts: |
---|
| 767 | if p.length > 1: |
---|
[b32dafd] | 768 | dict(("%s%d%s" % (p.id, k, ext), val) |
---|
| 769 | for k in range(1, p.length+1)) |
---|
[c499331] | 770 | else: |
---|
[b32dafd] | 771 | pars[p.id + ext] = val |
---|
[ec7e360] | 772 | |
---|
| 773 | # Plug in values given in demo |
---|
[98d6cfc] | 774 | if use_demo: |
---|
[6d6508e] | 775 | pars.update(model_info.demo) |
---|
[373d1b6] | 776 | return pars |
---|
| 777 | |
---|
[17bbadd] | 778 | |
---|
[ec7e360] | 779 | def parse_opts(): |
---|
[dd7fc12] | 780 | # type: () -> Dict[str, Any] |
---|
[caeb06d] | 781 | """ |
---|
| 782 | Parse command line options. |
---|
| 783 | """ |
---|
[fc0fcd0] | 784 | MODELS = core.list_models() |
---|
[caeb06d] | 785 | flags = [arg for arg in sys.argv[1:] |
---|
| 786 | if arg.startswith('-')] |
---|
| 787 | values = [arg for arg in sys.argv[1:] |
---|
| 788 | if not arg.startswith('-') and '=' in arg] |
---|
| 789 | args = [arg for arg in sys.argv[1:] |
---|
| 790 | if not arg.startswith('-') and '=' not in arg] |
---|
[d547f16] | 791 | models = "\n ".join("%-15s"%v for v in MODELS) |
---|
[87985ca] | 792 | if len(args) == 0: |
---|
[7cf2cfd] | 793 | print(USAGE) |
---|
[caeb06d] | 794 | print("\nAvailable models:") |
---|
[7cf2cfd] | 795 | print(columnize(MODELS, indent=" ")) |
---|
[87985ca] | 796 | sys.exit(1) |
---|
[319ab14] | 797 | if len(args) > 3: |
---|
[9cfcac8] | 798 | print("expected parameters: model N1 N2") |
---|
[87985ca] | 799 | |
---|
[17bbadd] | 800 | name = args[0] |
---|
[72a081d] | 801 | try: |
---|
[d19962c] | 802 | model_info = core.load_model_info(name) |
---|
[7ae2b7f] | 803 | except ImportError as exc: |
---|
[72a081d] | 804 | print(str(exc)) |
---|
| 805 | print("Could not find model; use one of:\n " + models) |
---|
| 806 | sys.exit(1) |
---|
[17bbadd] | 807 | |
---|
[ec7e360] | 808 | invalid = [o[1:] for o in flags |
---|
[216a9e1] | 809 | if o[1:] not in NAME_OPTIONS |
---|
[d15a908] | 810 | and not any(o.startswith('-%s='%t) for t in VALUE_OPTIONS)] |
---|
[87985ca] | 811 | if invalid: |
---|
[9404dd3] | 812 | print("Invalid options: %s"%(", ".join(invalid))) |
---|
[87985ca] | 813 | sys.exit(1) |
---|
| 814 | |
---|
[ec7e360] | 815 | |
---|
[d15a908] | 816 | # pylint: disable=bad-whitespace |
---|
[ec7e360] | 817 | # Interpret the flags |
---|
| 818 | opts = { |
---|
| 819 | 'plot' : True, |
---|
| 820 | 'view' : 'log', |
---|
| 821 | 'is2d' : False, |
---|
| 822 | 'qmax' : 0.05, |
---|
| 823 | 'nq' : 128, |
---|
| 824 | 'res' : 0.0, |
---|
| 825 | 'accuracy' : 'Low', |
---|
[72a081d] | 826 | 'cutoff' : 0.0, |
---|
[ec7e360] | 827 | 'seed' : -1, # default to preset |
---|
| 828 | 'mono' : False, |
---|
| 829 | 'show_pars' : False, |
---|
| 830 | 'show_hist' : False, |
---|
| 831 | 'rel_err' : True, |
---|
| 832 | 'explore' : False, |
---|
[98d6cfc] | 833 | 'use_demo' : True, |
---|
[dd7fc12] | 834 | 'zero' : False, |
---|
[ec7e360] | 835 | } |
---|
| 836 | engines = [] |
---|
| 837 | for arg in flags: |
---|
| 838 | if arg == '-noplot': opts['plot'] = False |
---|
| 839 | elif arg == '-plot': opts['plot'] = True |
---|
| 840 | elif arg == '-linear': opts['view'] = 'linear' |
---|
| 841 | elif arg == '-log': opts['view'] = 'log' |
---|
| 842 | elif arg == '-q4': opts['view'] = 'q4' |
---|
| 843 | elif arg == '-1d': opts['is2d'] = False |
---|
| 844 | elif arg == '-2d': opts['is2d'] = True |
---|
| 845 | elif arg == '-exq': opts['qmax'] = 10.0 |
---|
| 846 | elif arg == '-highq': opts['qmax'] = 1.0 |
---|
| 847 | elif arg == '-midq': opts['qmax'] = 0.2 |
---|
[ce0b154] | 848 | elif arg == '-lowq': opts['qmax'] = 0.05 |
---|
[e78edc4] | 849 | elif arg == '-zero': opts['zero'] = True |
---|
[ec7e360] | 850 | elif arg.startswith('-nq='): opts['nq'] = int(arg[4:]) |
---|
| 851 | elif arg.startswith('-res='): opts['res'] = float(arg[5:]) |
---|
| 852 | elif arg.startswith('-accuracy='): opts['accuracy'] = arg[10:] |
---|
| 853 | elif arg.startswith('-cutoff='): opts['cutoff'] = float(arg[8:]) |
---|
| 854 | elif arg.startswith('-random='): opts['seed'] = int(arg[8:]) |
---|
[dd7fc12] | 855 | elif arg == '-random': opts['seed'] = np.random.randint(1000000) |
---|
[ec7e360] | 856 | elif arg == '-preset': opts['seed'] = -1 |
---|
| 857 | elif arg == '-mono': opts['mono'] = True |
---|
| 858 | elif arg == '-poly': opts['mono'] = False |
---|
| 859 | elif arg == '-pars': opts['show_pars'] = True |
---|
| 860 | elif arg == '-nopars': opts['show_pars'] = False |
---|
| 861 | elif arg == '-hist': opts['show_hist'] = True |
---|
| 862 | elif arg == '-nohist': opts['show_hist'] = False |
---|
| 863 | elif arg == '-rel': opts['rel_err'] = True |
---|
| 864 | elif arg == '-abs': opts['rel_err'] = False |
---|
| 865 | elif arg == '-half': engines.append(arg[1:]) |
---|
| 866 | elif arg == '-fast': engines.append(arg[1:]) |
---|
| 867 | elif arg == '-single': engines.append(arg[1:]) |
---|
| 868 | elif arg == '-double': engines.append(arg[1:]) |
---|
| 869 | elif arg == '-single!': engines.append(arg[1:]) |
---|
| 870 | elif arg == '-double!': engines.append(arg[1:]) |
---|
| 871 | elif arg == '-quad!': engines.append(arg[1:]) |
---|
| 872 | elif arg == '-sasview': engines.append(arg[1:]) |
---|
| 873 | elif arg == '-edit': opts['explore'] = True |
---|
[98d6cfc] | 874 | elif arg == '-demo': opts['use_demo'] = True |
---|
| 875 | elif arg == '-default': opts['use_demo'] = False |
---|
[d15a908] | 876 | # pylint: enable=bad-whitespace |
---|
[ec7e360] | 877 | |
---|
| 878 | if len(engines) == 0: |
---|
[c499331] | 879 | engines.extend(['single', 'double']) |
---|
[ec7e360] | 880 | elif len(engines) == 1: |
---|
[def2c1b] | 881 | if engines[0][0] == 'double': |
---|
[ec7e360] | 882 | engines.append('single') |
---|
[def2c1b] | 883 | else: |
---|
| 884 | engines.append('double') |
---|
[ec7e360] | 885 | elif len(engines) > 2: |
---|
| 886 | del engines[2:] |
---|
| 887 | |
---|
[9cfcac8] | 888 | n1 = int(args[1]) if len(args) > 1 else 1 |
---|
| 889 | n2 = int(args[2]) if len(args) > 2 else 1 |
---|
[b32dafd] | 890 | use_sasview = any(engine == 'sasview' and count > 0 |
---|
[fa1582e] | 891 | for engine, count in zip(engines, [n1, n2])) |
---|
[87985ca] | 892 | |
---|
[ec7e360] | 893 | # Get demo parameters from model definition, or use default parameters |
---|
| 894 | # if model does not define demo parameters |
---|
[98d6cfc] | 895 | pars = get_pars(model_info, opts['use_demo']) |
---|
| 896 | |
---|
[87985ca] | 897 | |
---|
| 898 | # Fill in parameters given on the command line |
---|
[ec7e360] | 899 | presets = {} |
---|
| 900 | for arg in values: |
---|
[d15a908] | 901 | k, v = arg.split('=', 1) |
---|
[87985ca] | 902 | if k not in pars: |
---|
[ec7e360] | 903 | # extract base name without polydispersity info |
---|
[87985ca] | 904 | s = set(p.split('_pd')[0] for p in pars) |
---|
[d15a908] | 905 | print("%r invalid; parameters are: %s"%(k, ", ".join(sorted(s)))) |
---|
[87985ca] | 906 | sys.exit(1) |
---|
[ec7e360] | 907 | presets[k] = float(v) if not k.endswith('type') else v |
---|
| 908 | |
---|
| 909 | # randomize parameters |
---|
| 910 | #pars.update(set_pars) # set value before random to control range |
---|
| 911 | if opts['seed'] > -1: |
---|
[8bd7b77] | 912 | pars = randomize_pars(model_info, pars, seed=opts['seed']) |
---|
[ec7e360] | 913 | print("Randomize using -random=%i"%opts['seed']) |
---|
[8b25ee1] | 914 | if opts['mono']: |
---|
| 915 | pars = suppress_pd(pars) |
---|
[ec7e360] | 916 | pars.update(presets) # set value after random to control value |
---|
[fcd7bbd] | 917 | #import pprint; pprint.pprint(model_info) |
---|
[17bbadd] | 918 | constrain_pars(model_info, pars) |
---|
[fa1582e] | 919 | if use_sasview: |
---|
| 920 | constrain_new_to_old(model_info, pars) |
---|
[ec7e360] | 921 | if opts['show_pars']: |
---|
[d6850fa] | 922 | print(str(parlist(model_info, pars, opts['is2d']))) |
---|
[ec7e360] | 923 | |
---|
| 924 | # Create the computational engines |
---|
[d15a908] | 925 | data, _ = make_data(opts) |
---|
[9cfcac8] | 926 | if n1: |
---|
[17bbadd] | 927 | base = make_engine(model_info, data, engines[0], opts['cutoff']) |
---|
[ec7e360] | 928 | else: |
---|
| 929 | base = None |
---|
[9cfcac8] | 930 | if n2: |
---|
[17bbadd] | 931 | comp = make_engine(model_info, data, engines[1], opts['cutoff']) |
---|
[ec7e360] | 932 | else: |
---|
| 933 | comp = None |
---|
| 934 | |
---|
[d15a908] | 935 | # pylint: disable=bad-whitespace |
---|
[ec7e360] | 936 | # Remember it all |
---|
| 937 | opts.update({ |
---|
| 938 | 'name' : name, |
---|
[17bbadd] | 939 | 'def' : model_info, |
---|
[9cfcac8] | 940 | 'n1' : n1, |
---|
| 941 | 'n2' : n2, |
---|
[ec7e360] | 942 | 'presets' : presets, |
---|
| 943 | 'pars' : pars, |
---|
| 944 | 'data' : data, |
---|
| 945 | 'engines' : [base, comp], |
---|
| 946 | }) |
---|
[d15a908] | 947 | # pylint: enable=bad-whitespace |
---|
[ec7e360] | 948 | |
---|
| 949 | return opts |
---|
| 950 | |
---|
| 951 | def explore(opts): |
---|
[dd7fc12] | 952 | # type: (Dict[str, Any]) -> None |
---|
[d15a908] | 953 | """ |
---|
| 954 | Explore the model using the Bumps GUI. |
---|
| 955 | """ |
---|
[7ae2b7f] | 956 | import wx # type: ignore |
---|
| 957 | from bumps.names import FitProblem # type: ignore |
---|
| 958 | from bumps.gui.app_frame import AppFrame # type: ignore |
---|
[ec7e360] | 959 | |
---|
| 960 | problem = FitProblem(Explore(opts)) |
---|
[d15a908] | 961 | is_mac = "cocoa" in wx.version() |
---|
[ec7e360] | 962 | app = wx.App() |
---|
| 963 | frame = AppFrame(parent=None, title="explore") |
---|
[d15a908] | 964 | if not is_mac: frame.Show() |
---|
[ec7e360] | 965 | frame.panel.set_model(model=problem) |
---|
| 966 | frame.panel.Layout() |
---|
| 967 | frame.panel.aui.Split(0, wx.TOP) |
---|
[d15a908] | 968 | if is_mac: frame.Show() |
---|
[ec7e360] | 969 | app.MainLoop() |
---|
| 970 | |
---|
| 971 | class Explore(object): |
---|
| 972 | """ |
---|
[d15a908] | 973 | Bumps wrapper for a SAS model comparison. |
---|
| 974 | |
---|
| 975 | The resulting object can be used as a Bumps fit problem so that |
---|
| 976 | parameters can be adjusted in the GUI, with plots updated on the fly. |
---|
[ec7e360] | 977 | """ |
---|
| 978 | def __init__(self, opts): |
---|
[dd7fc12] | 979 | # type: (Dict[str, Any]) -> None |
---|
[7ae2b7f] | 980 | from bumps.cli import config_matplotlib # type: ignore |
---|
[608e31e] | 981 | from . import bumps_model |
---|
[ec7e360] | 982 | config_matplotlib() |
---|
| 983 | self.opts = opts |
---|
[17bbadd] | 984 | model_info = opts['def'] |
---|
| 985 | pars, pd_types = bumps_model.create_parameters(model_info, **opts['pars']) |
---|
[21b116f] | 986 | # Initialize parameter ranges, fixing the 2D parameters for 1D data. |
---|
[ec7e360] | 987 | if not opts['is2d']: |
---|
[6d6508e] | 988 | for p in model_info.parameters.user_parameters(is2d=False): |
---|
[303d8d6] | 989 | for ext in ['', '_pd', '_pd_n', '_pd_nsigma']: |
---|
[69aa451] | 990 | k = p.name+ext |
---|
[303d8d6] | 991 | v = pars.get(k, None) |
---|
| 992 | if v is not None: |
---|
| 993 | v.range(*parameter_range(k, v.value)) |
---|
[ec7e360] | 994 | else: |
---|
[013adb7] | 995 | for k, v in pars.items(): |
---|
[ec7e360] | 996 | v.range(*parameter_range(k, v.value)) |
---|
| 997 | |
---|
| 998 | self.pars = pars |
---|
| 999 | self.pd_types = pd_types |
---|
[013adb7] | 1000 | self.limits = None |
---|
[ec7e360] | 1001 | |
---|
| 1002 | def numpoints(self): |
---|
[dd7fc12] | 1003 | # type: () -> int |
---|
[ec7e360] | 1004 | """ |
---|
[608e31e] | 1005 | Return the number of points. |
---|
[ec7e360] | 1006 | """ |
---|
| 1007 | return len(self.pars) + 1 # so dof is 1 |
---|
| 1008 | |
---|
| 1009 | def parameters(self): |
---|
[dd7fc12] | 1010 | # type: () -> Any # Dict/List hierarchy of parameters |
---|
[ec7e360] | 1011 | """ |
---|
[608e31e] | 1012 | Return a dictionary of parameters. |
---|
[ec7e360] | 1013 | """ |
---|
| 1014 | return self.pars |
---|
| 1015 | |
---|
| 1016 | def nllf(self): |
---|
[dd7fc12] | 1017 | # type: () -> float |
---|
[608e31e] | 1018 | """ |
---|
| 1019 | Return cost. |
---|
| 1020 | """ |
---|
[d15a908] | 1021 | # pylint: disable=no-self-use |
---|
[ec7e360] | 1022 | return 0. # No nllf |
---|
| 1023 | |
---|
| 1024 | def plot(self, view='log'): |
---|
[dd7fc12] | 1025 | # type: (str) -> None |
---|
[ec7e360] | 1026 | """ |
---|
| 1027 | Plot the data and residuals. |
---|
| 1028 | """ |
---|
[608e31e] | 1029 | pars = dict((k, v.value) for k, v in self.pars.items()) |
---|
[ec7e360] | 1030 | pars.update(self.pd_types) |
---|
| 1031 | self.opts['pars'] = pars |
---|
[013adb7] | 1032 | limits = compare(self.opts, limits=self.limits) |
---|
| 1033 | if self.limits is None: |
---|
| 1034 | vmin, vmax = limits |
---|
[dd7fc12] | 1035 | self.limits = vmax*1e-7, 1.3*vmax |
---|
[87985ca] | 1036 | |
---|
| 1037 | |
---|
[d15a908] | 1038 | def main(): |
---|
[dd7fc12] | 1039 | # type: () -> None |
---|
[d15a908] | 1040 | """ |
---|
| 1041 | Main program. |
---|
| 1042 | """ |
---|
| 1043 | opts = parse_opts() |
---|
| 1044 | if opts['explore']: |
---|
| 1045 | explore(opts) |
---|
| 1046 | else: |
---|
| 1047 | compare(opts) |
---|
| 1048 | |
---|
[8a20be5] | 1049 | if __name__ == "__main__": |
---|
[87985ca] | 1050 | main() |
---|