source: sasmodels/sasmodels/compare.py @ 5d316e9

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

support fast and loose single precision and half precision

  • Property mode set to 100755
File size: 18.2 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4import sys
5import math
6from os.path import basename, dirname, join as joinpath
7import glob
8import datetime
9import traceback
10
11import numpy as np
12
13ROOT = dirname(__file__)
14sys.path.insert(0, ROOT)  # Make sure sasmodels is first on the path
15
16
17from . import core
18from . import kerneldll
19from . import generate
20from .data import plot_theory, empty_data1D, empty_data2D
21from .direct_model import DirectModel
22from .convert import revert_model
23kerneldll.ALLOW_SINGLE_PRECISION_DLLS = True
24
25# List of available models
26MODELS = [basename(f)[:-3]
27          for f in sorted(glob.glob(joinpath(ROOT,"models","[a-zA-Z]*.py")))]
28
29# CRUFT python 2.6
30if not hasattr(datetime.timedelta, 'total_seconds'):
31    def delay(dt):
32        """Return number date-time delta as number seconds"""
33        return dt.days * 86400 + dt.seconds + 1e-6 * dt.microseconds
34else:
35    def delay(dt):
36        """Return number date-time delta as number seconds"""
37        return dt.total_seconds()
38
39
40def tic():
41    """
42    Timer function.
43
44    Use "toc=tic()" to start the clock and "toc()" to measure
45    a time interval.
46    """
47    then = datetime.datetime.now()
48    return lambda: delay(datetime.datetime.now() - then)
49
50
51def set_beam_stop(data, radius, outer=None):
52    """
53    Add a beam stop of the given *radius*.  If *outer*, make an annulus.
54
55    Note: this function does not use the sasview package
56    """
57    if hasattr(data, 'qx_data'):
58        q = np.sqrt(data.qx_data**2 + data.qy_data**2)
59        data.mask = (q < radius)
60        if outer is not None:
61            data.mask |= (q >= outer)
62    else:
63        data.mask = (data.x < radius)
64        if outer is not None:
65            data.mask |= (data.x >= outer)
66
67
68def sasview_model(model_definition, **pars):
69    """
70    Load a sasview model given the model name.
71    """
72    # convert model parameters from sasmodel form to sasview form
73    #print("old",sorted(pars.items()))
74    modelname, pars = revert_model(model_definition, pars)
75    #print("new",sorted(pars.items()))
76    sas = __import__('sas.models.'+modelname)
77    ModelClass = getattr(getattr(sas.models,modelname,None),modelname,None)
78    if ModelClass is None:
79        raise ValueError("could not find model %r in sas.models"%modelname)
80    model = ModelClass()
81
82    for k,v in pars.items():
83        if k.endswith("_pd"):
84            model.dispersion[k[:-3]]['width'] = v
85        elif k.endswith("_pd_n"):
86            model.dispersion[k[:-5]]['npts'] = v
87        elif k.endswith("_pd_nsigma"):
88            model.dispersion[k[:-10]]['nsigmas'] = v
89        elif k.endswith("_pd_type"):
90            model.dispersion[k[:-8]]['type'] = v
91        else:
92            model.setParam(k, v)
93    return model
94
95def randomize(p, v):
96    """
97    Randomizing parameter.
98
99    Guess the parameter type from name.
100    """
101    if any(p.endswith(s) for s in ('_pd_n','_pd_nsigma','_pd_type')):
102        return v
103    elif any(s in p for s in ('theta','phi','psi')):
104        # orientation in [-180,180], orientation pd in [0,45]
105        if p.endswith('_pd'):
106            return 45*np.random.rand()
107        else:
108            return 360*np.random.rand() - 180
109    elif 'sld' in p:
110        # sld in in [-0.5,10]
111        return 10.5*np.random.rand() - 0.5
112    elif p.endswith('_pd'):
113        # length pd in [0,1]
114        return np.random.rand()
115    else:
116        # values from 0 to 2*x for all other parameters
117        return 2*np.random.rand()*(v if v != 0 else 1)
118
119def randomize_model(pars, seed=None):
120    if seed is None:
121        seed = np.random.randint(1e9)
122    np.random.seed(seed)
123    # Note: the sort guarantees order of calls to random number generator
124    pars = dict((p,randomize(p,v)) for p,v in sorted(pars.items()))
125
126    return pars, seed
127
128def constrain_pars(model_definition, pars):
129    name = model_definition.name
130    if name == 'capped_cylinder' and pars['cap_radius'] < pars['radius']:
131        pars['radius'],pars['cap_radius'] = pars['cap_radius'],pars['radius']
132    if name == 'barbell' and pars['bell_radius'] < pars['radius']:
133        pars['radius'],pars['bell_radius'] = pars['bell_radius'],pars['radius']
134
135    # Limit guinier to an Rg such that Iq > 1e-30 (single precision cutoff)
136    if name == 'guinier':
137        #q_max = 0.2  # mid q maximum
138        q_max = 1.0  # high q maximum
139        rg_max = np.sqrt(90*np.log(10) + 3*np.log(pars['scale']))/q_max
140        pars['rg'] = min(pars['rg'],rg_max)
141
142    # These constraints are only needed for comparison to sasview
143    if name in ('teubner_strey','broad_peak'):
144        del pars['scale']
145    if name in ('guinier',):
146        del pars['background']
147    if getattr(model_definition, 'category', None) == 'structure-factor':
148        del pars['scale'], pars['background']
149
150
151def parlist(pars):
152    return "\n".join("%s: %s"%(p,v) for p,v in sorted(pars.items()))
153
154def suppress_pd(pars):
155    """
156    Suppress theta_pd for now until the normalization is resolved.
157
158    May also suppress complete polydispersity of the model to test
159    models more quickly.
160    """
161    for p in pars:
162        if p.endswith("_pd"): pars[p] = 0
163
164def eval_sasview(model_definition, pars, data, Nevals=1):
165    # importing sas here so that the error message will be that sas failed to
166    # import rather than the more obscure smear_selection not imported error
167    import sas
168    from sas.models.qsmearing import smear_selection
169    model = sasview_model(model_definition, **pars)
170    smearer = smear_selection(data, model=model)
171    value = None  # silence the linter
172    toc = tic()
173    for _ in range(max(Nevals, 1)):  # make sure there is at least one eval
174        if hasattr(data, 'qx_data'):
175            q = np.sqrt(data.qx_data**2 + data.qy_data**2)
176            index = ((~data.mask) & (~np.isnan(data.data))
177                     & (q >= data.qmin) & (q <= data.qmax))
178            if smearer is not None:
179                smearer.model = model  # because smear_selection has a bug
180                smearer.accuracy = data.accuracy
181                smearer.set_index(index)
182                value = smearer.get_value()
183            else:
184                value = model.evalDistribution([data.qx_data[index], data.qy_data[index]])
185        else:
186            value = model.evalDistribution(data.x)
187            if smearer is not None:
188                value = smearer(value)
189    average_time = toc()*1000./Nevals
190    return value, average_time
191
192def eval_opencl(model_definition, pars, data, dtype='single', Nevals=1,
193                cutoff=0., fast=False):
194    try:
195        model = core.load_model(model_definition, dtype=dtype,
196                                platform="ocl", fast=fast)
197    except Exception as exc:
198        print(exc)
199        print("... trying again with single precision")
200        model = core.load_model(model_definition, dtype='single',
201                                platform="ocl", fast=fast)
202    calculator = DirectModel(data, model, cutoff=cutoff)
203    value = None  # silence the linter
204    toc = tic()
205    for _ in range(max(Nevals, 1)):  # force at least one eval
206        value = calculator(**pars)
207    average_time = toc()*1000./Nevals
208    return value, average_time
209
210
211def eval_ctypes(model_definition, pars, data, dtype='double', Nevals=1, cutoff=0.):
212    model = core.load_model(model_definition, dtype=dtype, platform="dll")
213    calculator = DirectModel(data, model, cutoff=cutoff)
214    value = None  # silence the linter
215    toc = tic()
216    for _ in range(max(Nevals, 1)):  # force at least one eval
217        value = calculator(**pars)
218    average_time = toc()*1000./Nevals
219    return value, average_time
220
221
222def make_data(qmax, is2D, Nq=128, resolution=0.0, accuracy='Low', view='log'):
223    if is2D:
224        data = empty_data2D(np.linspace(-qmax, qmax, Nq), resolution=resolution)
225        data.accuracy = accuracy
226        set_beam_stop(data, 0.004)
227        index = ~data.mask
228    else:
229        if view == 'log':
230            qmax = math.log10(qmax)
231            q = np.logspace(qmax-3, qmax, Nq)
232        else:
233            q = np.linspace(0.001*qmax, qmax, Nq)
234        data = empty_data1D(q, resolution=resolution)
235        index = slice(None, None)
236    return data, index
237
238def compare(name, pars, Ncomp, Nbase, opts, set_pars):
239    model_definition = core.load_model_definition(name)
240
241    view = ('linear' if '-linear' in opts
242            else 'log' if '-log' in opts
243            else 'q4' if '-q4' in opts
244            else 'log')
245
246    opt_values = dict(split
247                      for s in opts for split in ((s.split('='),))
248                      if len(split) == 2)
249    # Sort out data
250    qmax = (10.0 if '-exq' in opts
251            else 1.0 if '-highq' in opts
252            else 0.2 if '-midq' in opts
253            else 0.05)
254    Nq = int(opt_values.get('-Nq', '128'))
255    res = float(opt_values.get('-res', '0'))
256    accuracy = opt_values.get('-accuracy', 'Low')
257    is2D = "-2d" in opts
258    data, index = make_data(qmax, is2D, Nq, res, accuracy, view=view)
259
260
261    # modelling accuracy is determined by dtype and cutoff
262    dtype = ('longdouble' if '-quad' in opts
263             else 'double' if '-double' in opts
264             else 'half' if '-half' in opts
265             else 'single')
266    cutoff = float(opt_values.get('-cutoff','1e-5'))
267    fast = "-fast" in opts and dtype is 'single'
268
269    # randomize parameters
270    #pars.update(set_pars)  # set value before random to control range
271    if '-random' in opts or '-random' in opt_values:
272        seed = int(opt_values['-random']) if '-random' in opt_values else None
273        pars, seed = randomize_model(pars, seed=seed)
274        print("Randomize using -random=%i"%seed)
275    pars.update(set_pars)  # set value after random to control value
276    constrain_pars(model_definition, pars)
277
278    # parameter selection
279    if '-mono' in opts:
280        suppress_pd(pars)
281    if '-pars' in opts:
282        print("pars "+str(parlist(pars)))
283
284    # Base calculation
285    if 0:
286        from sasmodels.models import sphere as target
287        base_name = target.name
288        base, base_time = eval_ctypes(target, pars, data,
289                dtype='longdouble', cutoff=0., Nevals=Ncomp)
290    elif Nbase > 0 and "-ctypes" in opts and "-sasview" in opts:
291        try:
292            base, base_time = eval_sasview(model_definition, pars, data, Ncomp)
293            base_name = "sasview"
294            #print("base/sasview", (base-pars['background'])/(comp-pars['background']))
295            print("sasview t=%.1f ms, intensity=%.0f"%(base_time, sum(base)))
296            #print("sasview",comp)
297        except ImportError:
298            traceback.print_exc()
299            Nbase = 0
300    elif Nbase > 0:
301        base, base_time = eval_opencl(model_definition, pars, data,
302                dtype=dtype, cutoff=cutoff, Nevals=Nbase, fast=fast)
303        base_name = "ocl"
304        print("opencl t=%.1f ms, intensity=%.0f"%(base_time, sum(base)))
305        #print("base " + base)
306        #print(max(base), min(base))
307
308    # Comparison calculation
309    if Ncomp > 0 and "-ctypes" in opts:
310        comp, comp_time = eval_ctypes(model_definition, pars, data,
311                dtype=dtype, cutoff=cutoff, Nevals=Ncomp)
312        comp_name = "ctypes"
313        print("ctypes t=%.1f ms, intensity=%.0f"%(comp_time, sum(comp)))
314    elif Ncomp > 0:
315        try:
316            comp, comp_time = eval_sasview(model_definition, pars, data, Ncomp)
317            comp_name = "sasview"
318            #print("base/sasview", (base-pars['background'])/(comp-pars['background']))
319            print("sasview t=%.1f ms, intensity=%.0f"%(comp_time, sum(comp)))
320            #print("sasview",comp)
321        except ImportError:
322            traceback.print_exc()
323            Ncomp = 0
324
325    # Compare, but only if computing both forms
326    if Nbase > 0 and Ncomp > 0:
327        #print("speedup %.2g"%(comp_time/base_time))
328        #print("max |base/comp|", max(abs(base/comp)), "%.15g"%max(abs(base)), "%.15g"%max(abs(comp)))
329        #comp *= max(base/comp)
330        resid = (base - comp)
331        relerr = resid/comp
332        #bad = (relerr>1e-4)
333        #print(relerr[bad],comp[bad],base[bad],data.qx_data[bad],data.qy_data[bad])
334        _print_stats("|%s-%s|"%(base_name,comp_name)+(" "*(3+len(comp_name))), resid)
335        _print_stats("|(%s-%s)/%s|"%(base_name,comp_name,comp_name), relerr)
336
337    # Plot if requested
338    if '-noplot' in opts: return
339    import matplotlib.pyplot as plt
340    if Ncomp > 0:
341        if Nbase > 0: plt.subplot(131)
342        plot_theory(data, comp, view=view, plot_data=False)
343        plt.title("%s t=%.1f ms"%(comp_name,comp_time))
344        #cbar_title = "log I"
345    if Nbase > 0:
346        if Ncomp > 0: plt.subplot(132)
347        plot_theory(data, base, view=view, plot_data=False)
348        plt.title("%s t=%.1f ms"%(base_name,base_time))
349        #cbar_title = "log I"
350    if Ncomp > 0 and Nbase > 0:
351        plt.subplot(133)
352        if '-abs' in opts:
353            err,errstr,errview = resid, "abs err", "linear"
354        else:
355            err,errstr,errview = abs(relerr), "rel err", "log"
356        #err,errstr = base/comp,"ratio"
357        plot_theory(data, None, resid=err, view=errview, plot_data=False)
358        plt.title("max %s = %.3g"%(errstr, max(abs(err))))
359        #cbar_title = errstr if errview=="linear" else "log "+errstr
360    #if is2D:
361    #    h = plt.colorbar()
362    #    h.ax.set_title(cbar_title)
363
364    if Ncomp > 0 and Nbase > 0 and '-hist' in opts:
365        plt.figure()
366        v = relerr
367        v[v==0] = 0.5*np.min(np.abs(v[v!=0]))
368        plt.hist(np.log10(np.abs(v)), normed=1, bins=50);
369        plt.xlabel('log10(err), err = | F(q) single - F(q) double| / | F(q) double |');
370        plt.ylabel('P(err)')
371        plt.title('Comparison of single and double precision models for %s'%name)
372
373    plt.show()
374
375def _print_stats(label, err):
376    sorted_err = np.sort(abs(err))
377    p50 = int((len(err)-1)*0.50)
378    p98 = int((len(err)-1)*0.98)
379    data = [
380        "max:%.3e"%sorted_err[-1],
381        "median:%.3e"%sorted_err[p50],
382        "98%%:%.3e"%sorted_err[p98],
383        "rms:%.3e"%np.sqrt(np.mean(err**2)),
384        "zero-offset:%+.3e"%np.mean(err),
385        ]
386    print(label+"  ".join(data))
387
388
389
390# ===========================================================================
391#
392USAGE="""
393usage: compare.py model [Nopencl] [Nsasview] [options...] [key=val]
394
395Compare the speed and value for a model between the SasView original and the
396OpenCL rewrite.
397
398model is the name of the model to compare (see below).
399Nopencl is the number of times to run the OpenCL model (default=5)
400Nsasview is the number of times to run the Sasview model (default=1)
401
402Options (* for default):
403
404    -plot*/-noplot plots or suppress the plot of the model
405    -half/-single*/-double/-quad/-fast sets the calculation precision
406    -lowq*/-midq/-highq/-exq use q values up to 0.05, 0.2, 1.0, 10.0
407    -Nq=128 sets the number of Q points in the data set
408    -1d*/-2d computes 1d or 2d data
409    -preset*/-random[=seed] preset or random parameters
410    -mono/-poly* force monodisperse/polydisperse
411    -ctypes/-sasview* selects gpu:cpu, gpu:sasview, or sasview:cpu if both
412    -cutoff=1e-5* cutoff value for including a point in polydispersity
413    -pars/-nopars* prints the parameter set or not
414    -abs/-rel* plot relative or absolute error
415    -linear/-log/-q4 intensity scaling
416    -hist/-nohist* plot histogram of relative error
417    -res=0 sets the resolution width dQ/Q if calculating with resolution
418    -accuracy=Low accuracy of the resolution calculation Low, Mid, High, Xhigh
419
420Key=value pairs allow you to set specific values to any of the model
421parameters.
422
423Available models:
424"""
425
426
427NAME_OPTIONS = set([
428    'plot', 'noplot',
429    'half', 'single', 'double', 'quad', 'fast',
430    'lowq', 'midq', 'highq', 'exq',
431    '2d', '1d',
432    'preset', 'random',
433    'poly', 'mono',
434    'sasview', 'ctypes',
435    'nopars', 'pars',
436    'rel', 'abs',
437    'linear', 'log', 'q4',
438    'hist', 'nohist',
439    ])
440VALUE_OPTIONS = [
441    # Note: random is both a name option and a value option
442    'cutoff', 'random', 'Nq', 'res', 'accuracy',
443    ]
444
445def columnize(L, indent="", width=79):
446    column_width = max(len(w) for w in L) + 1
447    num_columns = (width - len(indent)) // column_width
448    num_rows = len(L) // num_columns
449    L = L + [""] * (num_rows*num_columns - len(L))
450    columns = [L[k*num_rows:(k+1)*num_rows] for k in range(num_columns)]
451    lines = [" ".join("%-*s"%(column_width, entry) for entry in row)
452             for row in zip(*columns)]
453    output = indent + ("\n"+indent).join(lines)
454    return output
455
456
457def get_demo_pars(model_definition):
458    info = generate.make_info(model_definition)
459    pars = dict((p[0],p[2]) for p in info['parameters'])
460    pars.update(info['demo'])
461    return pars
462
463def main():
464    opts = [arg for arg in sys.argv[1:] if arg.startswith('-')]
465    popts = [arg for arg in sys.argv[1:] if not arg.startswith('-') and '=' in arg]
466    args = [arg for arg in sys.argv[1:] if not arg.startswith('-') and '=' not in arg]
467    models = "\n    ".join("%-15s"%v for v in MODELS)
468    if len(args) == 0:
469        print(USAGE)
470        print(columnize(MODELS, indent="  "))
471        sys.exit(1)
472    if args[0] not in MODELS:
473        print("Model %r not available. Use one of:\n    %s"%(args[0],models))
474        sys.exit(1)
475    if len(args) > 3:
476        print("expected parameters: model Nopencl Nsasview")
477
478    invalid = [o[1:] for o in opts
479               if o[1:] not in NAME_OPTIONS
480                  and not any(o.startswith('-%s='%t) for t in VALUE_OPTIONS)]
481    if invalid:
482        print("Invalid options: %s"%(", ".join(invalid)))
483        sys.exit(1)
484
485    # Get demo parameters from model definition, or use default parameters
486    # if model does not define demo parameters
487    name = args[0]
488    model_definition = core.load_model_definition(name)
489    pars = get_demo_pars(model_definition)
490
491    Ncomp = int(args[1]) if len(args) > 1 else 5
492    Nbase = int(args[2]) if len(args) > 2 else 1
493
494    # Fill in default polydispersity parameters
495    pds = set(p.split('_pd')[0] for p in pars if p.endswith('_pd'))
496    for p in pds:
497        if p+"_pd_nsigma" not in pars: pars[p+"_pd_nsigma"] = 3
498        if p+"_pd_type" not in pars: pars[p+"_pd_type"] = "gaussian"
499
500    # Fill in parameters given on the command line
501    set_pars = {}
502    for arg in popts:
503        k,v = arg.split('=',1)
504        if k not in pars:
505            # extract base name without distribution
506            s = set(p.split('_pd')[0] for p in pars)
507            print("%r invalid; parameters are: %s"%(k,", ".join(sorted(s))))
508            sys.exit(1)
509        set_pars[k] = float(v) if not v.endswith('type') else v
510
511    compare(name, pars, Ncomp, Nbase, opts, set_pars)
512
513if __name__ == "__main__":
514    main()
Note: See TracBrowser for help on using the repository browser.