Changeset 346bc88 in sasmodels for compare.py


Ignore:
Timestamp:
Aug 31, 2015 10:24:45 PM (9 years ago)
Author:
Paul Kienzle <pkienzle@…>
Branches:
master, core_shell_microgels, costrafo411, magnetic_model, release_v0.94, release_v0.95, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
Children:
3e6aaad
Parents:
f224873
git-author:
Paul Kienzle <pkienzle@…> (08/31/15 22:12:39)
git-committer:
Paul Kienzle <pkienzle@…> (08/31/15 22:24:45)
Message:

Add 2D resolution smearing. Split BumpsModel? into Experiment and Model and fix up examples.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • compare.py

    raa4946b r346bc88  
    99import numpy as np 
    1010 
    11 from sasmodels.bumps_model import BumpsModel, plot_data, tic 
     11from sasmodels.bumps_model import Model, Experiment, plot_theory, tic 
    1212from sasmodels import core 
    1313from sasmodels import kerneldll 
     
    9797 
    9898def eval_sasview(name, pars, data, Nevals=1): 
     99    from sas.models.qsmearing import smear_selection 
    99100    model = sasview_model(name, **pars) 
     101    smearer = smear_selection(data, model=model) 
    100102    toc = tic() 
    101103    for _ in range(Nevals): 
    102104        if hasattr(data, 'qx_data'): 
    103             value = model.evalDistribution([data.qx_data, data.qy_data]) 
     105            q = np.sqrt(data.qx_data**2 + data.qy_data**2) 
     106            index = ((~data.mask) & (~np.isnan(data.data)) 
     107                     & (q >= data.qmin) & (q <= data.qmax)) 
     108            if smearer is not None: 
     109                smearer.model = model  # because smear_selection has a bug 
     110                smearer.set_index(index) 
     111                value = smearer.get_value() 
     112            else: 
     113                value = model.evalDistribution([data.qx_data[index], data.qy_data[index]]) 
    104114        else: 
    105115            value = model.evalDistribution(data.x) 
     116            if smearer is not None: 
     117                value = smearer(value) 
    106118    average_time = toc()*1000./Nevals 
    107119    return value, average_time 
     
    114126        print "... trying again with single precision" 
    115127        model = core.load_model(model_definition, dtype='single', platform="ocl") 
    116     problem = BumpsModel(data, model, cutoff=cutoff, **pars) 
     128    problem = Experiment(data, Model(model, **pars), cutoff=cutoff) 
    117129    toc = tic() 
    118130    for _ in range(Nevals): 
     
    125137def eval_ctypes(model_definition, pars, data, dtype='double', Nevals=1, cutoff=0): 
    126138    model = core.load_model(model_definition, dtype=dtype, platform="dll") 
    127     problem = BumpsModel(data, model, cutoff=cutoff, **pars) 
     139    problem = Experiment(data, Model(model, **pars), cutoff=cutoff) 
    128140    toc = tic() 
    129141    for _ in range(Nevals): 
     
    133145    return value, average_time 
    134146 
    135 def make_data(qmax, is2D, Nq=128, view='log'): 
     147def make_data(qmax, is2D, Nq=128, resolution=0.0, view='log'): 
    136148    if is2D: 
    137149        from sasmodels.bumps_model import empty_data2D, set_beam_stop 
    138         data = empty_data2D(np.linspace(-qmax, qmax, Nq)) 
     150        data = empty_data2D(np.linspace(-qmax, qmax, Nq), resolution=resolution) 
    139151        set_beam_stop(data, 0.004) 
    140152        index = ~data.mask 
     
    146158        else: 
    147159            q = np.linspace(0.001*qmax, qmax, Nq) 
    148         data = empty_data1D(q) 
     160        data = empty_data1D(q, resolution=resolution) 
    149161        index = slice(None, None) 
    150162    return data, index 
     
    159171    qmax = 10.0 if '-exq' in opts else 1.0 if '-highq' in opts else 0.2 if '-midq' in opts else 0.05 
    160172    Nq = int(opt_values.get('-Nq', '128')) 
     173    res = float(opt_values.get('-res', '0')) 
    161174    is2D = not "-1d" in opts 
    162     data, index = make_data(qmax, is2D, Nq, view=view) 
     175    data, index = make_data(qmax, is2D, Nq, res, view=view) 
    163176 
    164177 
     
    185198        ocl, ocl_time = eval_opencl(model_definition, pars, data, 
    186199                                    dtype=dtype, cutoff=cutoff, Nevals=Nocl) 
    187         print "opencl t=%.1f ms, intensity=%.0f"%(ocl_time, sum(ocl[index])) 
     200        print "opencl t=%.1f ms, intensity=%.0f"%(ocl_time, sum(ocl)) 
    188201        #print max(ocl), min(ocl) 
    189202 
     
    193206                                    dtype=dtype, cutoff=cutoff, Nevals=Ncpu) 
    194207        comp = "ctypes" 
    195         print "ctypes t=%.1f ms, intensity=%.0f"%(cpu_time, sum(cpu[index])) 
     208        print "ctypes t=%.1f ms, intensity=%.0f"%(cpu_time, sum(cpu)) 
    196209    elif Ncpu > 0: 
    197210        cpu, cpu_time = eval_sasview(model_definition, pars, data, Ncpu) 
    198211        comp = "sasview" 
    199         print "sasview t=%.1f ms, intensity=%.0f"%(cpu_time, sum(cpu[index])) 
     212        print "sasview t=%.1f ms, intensity=%.0f"%(cpu_time, sum(cpu)) 
    200213 
    201214    # Compare, but only if computing both forms 
     
    204217        #print "max |ocl/cpu|", max(abs(ocl/cpu)), "%.15g"%max(abs(ocl)), "%.15g"%max(abs(cpu)) 
    205218        #cpu *= max(ocl/cpu) 
    206         resid, relerr = np.zeros_like(ocl), np.zeros_like(ocl) 
    207         resid[index] = (ocl - cpu)[index] 
    208         relerr[index] = resid[index]/cpu[index] 
     219        resid = (ocl - cpu) 
     220        relerr = resid/cpu 
    209221        #bad = (relerr>1e-4) 
    210222        #print relerr[bad],cpu[bad],ocl[bad],data.qx_data[bad],data.qy_data[bad] 
     
    221233                ] 
    222234            print label,"  ".join(data) 
    223         stats("|ocl-%s|"%comp+(" "*(3+len(comp))), resid[index]) 
    224         stats("|(ocl-%s)/%s|"%(comp,comp), relerr[index]) 
     235        stats("|ocl-%s|"%comp+(" "*(3+len(comp))), resid) 
     236        stats("|(ocl-%s)/%s|"%(comp,comp), relerr) 
    225237 
    226238    # Plot if requested 
     
    229241    if Ncpu > 0: 
    230242        if Nocl > 0: plt.subplot(131) 
    231         plot_data(data, cpu, view=view) 
     243        plot_theory(data, cpu, view=view) 
    232244        plt.title("%s t=%.1f ms"%(comp,cpu_time)) 
    233245        cbar_title = "log I" 
    234246    if Nocl > 0: 
    235247        if Ncpu > 0: plt.subplot(132) 
    236         plot_data(data, ocl, view=view) 
     248        plot_theory(data, ocl, view=view) 
    237249        plt.title("opencl t=%.1f ms"%ocl_time) 
    238250        cbar_title = "log I" 
     
    244256            err,errstr,errview = abs(relerr), "rel err", "log" 
    245257        #err,errstr = ocl/cpu,"ratio" 
    246         plot_data(data, err, view=errview) 
    247         plt.title("max %s = %.3g"%(errstr, max(abs(err[index])))) 
     258        plot_theory(data, err, view=errview) 
     259        plt.title("max %s = %.3g"%(errstr, max(abs(err)))) 
    248260        cbar_title = errstr if errview=="linear" else "log "+errstr 
    249261    if is2D: 
     
    253265    if Ncpu > 0 and Nocl > 0 and '-hist' in opts: 
    254266        plt.figure() 
    255         v = relerr[index] 
     267        v = relerr 
    256268        v[v==0] = 0.5*np.min(np.abs(v[v!=0])) 
    257269        plt.hist(np.log10(np.abs(v)), normed=1, bins=50); 
     
    289301    -linear/-log/-q4 intensity scaling 
    290302    -hist/-nohist* plot histogram of relative error 
     303    -res=0 sets the resolution width dQ/Q if calculating with resolution 
    291304 
    292305Key=value pairs allow you to set specific values to any of the model 
     
    313326VALUE_OPTIONS = [ 
    314327    # Note: random is both a name option and a value option 
    315     'cutoff', 'random', 'Nq', 
     328    'cutoff', 'random', 'Nq', 'res', 
    316329    ] 
    317330 
Note: See TracChangeset for help on using the changeset viewer.