source: sasmodels/sasmodels/compare.py @ da63656

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since da63656 was f2f67a6, checked in by Paul Kienzle <pkienzle@…>, 8 years ago

reenable opencl; works on cpu but not gpu

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