Changeset aa4946b in sasmodels for compare.py


Ignore:
Timestamp:
Mar 11, 2015 9:15:40 PM (10 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:
af1d68c
Parents:
49d1d42f
Message:

refactor so kernels are loaded via core.load_model

File:
1 edited

Legend:

Unmodified
Added
Removed
  • compare.py

    r750ffa5 raa4946b  
    1010 
    1111from sasmodels.bumps_model import BumpsModel, plot_data, tic 
    12 try: from sasmodels import kernelcl 
    13 except: from sasmodels import kerneldll as kernelcl 
     12from sasmodels import core 
    1413from sasmodels import kerneldll 
    1514from sasmodels.convert import revert_model 
     
    2221 
    2322 
    24 def sasview_model(modelname, **pars): 
     23def sasview_model(model_definition, **pars): 
    2524    """ 
    2625    Load a sasview model given the model name. 
     
    2827    # convert model parameters from sasmodel form to sasview form 
    2928    #print "old",sorted(pars.items()) 
    30     modelname, pars = revert_model(modelname, pars) 
     29    modelname, pars = revert_model(model_definition, pars) 
    3130    #print "new",sorted(pars.items()) 
    3231    sas = __import__('sas.models.'+modelname) 
     
    4847            model.setParam(k, v) 
    4948    return model 
    50  
    51 def load_opencl(modelname, dtype='single'): 
    52     sasmodels = __import__('sasmodels.models.'+modelname) 
    53     module = getattr(sasmodels.models, modelname, None) 
    54     kernel = kernelcl.load_model(module, dtype=dtype) 
    55     return kernel 
    56  
    57 def load_ctypes(modelname, dtype='single'): 
    58     sasmodels = __import__('sasmodels.models.'+modelname) 
    59     module = getattr(sasmodels.models, modelname, None) 
    60     kernel = kerneldll.load_model(module, dtype=dtype) 
    61     return kernel 
    6249 
    6350def randomize(p, v): 
     
    120107    return value, average_time 
    121108 
    122 def eval_opencl(name, pars, data, dtype='single', Nevals=1, cutoff=0): 
     109def eval_opencl(model_definition, pars, data, dtype='single', Nevals=1, cutoff=0): 
    123110    try: 
    124         model = load_opencl(name, dtype=dtype) 
     111        model = core.load_model(model_definition, dtype=dtype, platform="ocl") 
    125112    except Exception,exc: 
    126113        print exc 
    127114        print "... trying again with single precision" 
    128         model = load_opencl(name, dtype='single') 
     115        model = core.load_model(model_definition, dtype='single', platform="ocl") 
    129116    problem = BumpsModel(data, model, cutoff=cutoff, **pars) 
    130117    toc = tic() 
     
    136123    return value, average_time 
    137124 
    138 def eval_ctypes(name, pars, data, dtype='double', Nevals=1, cutoff=0): 
    139     model = load_ctypes(name, dtype=dtype) 
     125def eval_ctypes(model_definition, pars, data, dtype='double', Nevals=1, cutoff=0): 
     126    model = core.load_model(model_definition, dtype=dtype, platform="dll") 
    140127    problem = BumpsModel(data, model, cutoff=cutoff, **pars) 
    141128    toc = tic() 
     
    193180        print "pars",parlist(pars) 
    194181 
     182    model_definition = core.load_model_definition(name) 
    195183    # OpenCl calculation 
    196184    if Nocl > 0: 
    197         ocl, ocl_time = eval_opencl(name, pars, data, dtype, Nocl) 
     185        ocl, ocl_time = eval_opencl(model_definition, pars, data, 
     186                                    dtype=dtype, cutoff=cutoff, Nevals=Nocl) 
    198187        print "opencl t=%.1f ms, intensity=%.0f"%(ocl_time, sum(ocl[index])) 
    199188        #print max(ocl), min(ocl) 
     
    201190    # ctypes/sasview calculation 
    202191    if Ncpu > 0 and "-ctypes" in opts: 
    203         cpu, cpu_time = eval_ctypes(name, pars, data, dtype=dtype, cutoff=cutoff, Nevals=Ncpu) 
     192        cpu, cpu_time = eval_ctypes(model_definition, pars, data, 
     193                                    dtype=dtype, cutoff=cutoff, Nevals=Ncpu) 
    204194        comp = "ctypes" 
    205195        print "ctypes t=%.1f ms, intensity=%.0f"%(cpu_time, sum(cpu[index])) 
    206196    elif Ncpu > 0: 
    207         cpu, cpu_time = eval_sasview(name, pars, data, Ncpu) 
     197        cpu, cpu_time = eval_sasview(model_definition, pars, data, Ncpu) 
    208198        comp = "sasview" 
    209199        print "sasview t=%.1f ms, intensity=%.0f"%(cpu_time, sum(cpu[index])) 
     
    219209        #bad = (relerr>1e-4) 
    220210        #print relerr[bad],cpu[bad],ocl[bad],data.qx_data[bad],data.qy_data[bad] 
    221         print "max(|ocl-%s|)"%comp, max(abs(resid[index])) 
    222         print "max(|(ocl-%s)/%s|)"%(comp,comp), max(abs(relerr[index])) 
    223         p98 = int(len(relerr[index])*0.98) 
    224         print "98%% (|(ocl-%s)/%s|) <"%(comp,comp), np.sort(abs(relerr[index]))[p98] 
    225  
     211        def stats(label,err): 
     212            sorted_err = np.sort(abs(err)) 
     213            p50 = int((len(err)-1)*0.50) 
     214            p98 = int((len(err)-1)*0.98) 
     215            data = [ 
     216                "max:%.3e"%sorted_err[-1], 
     217                "median:%.3e"%sorted_err[p50], 
     218                "98%%:%.3e"%sorted_err[p98], 
     219                "rms:%.3e"%np.sqrt(np.mean(err**2)), 
     220                "zero-offset:%+.3e"%np.mean(err), 
     221                ] 
     222            print label,"  ".join(data) 
     223        stats("|ocl-%s|"%comp+(" "*(3+len(comp))), resid[index]) 
     224        stats("|(ocl-%s)/%s|"%(comp,comp), relerr[index]) 
    226225 
    227226    # Plot if requested 
Note: See TracChangeset for help on using the changeset viewer.