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