Changeset aa4946b in sasmodels for compare.py
- Timestamp:
- Mar 11, 2015 9:15:40 PM (10 years ago)
- 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
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
compare.py
r750ffa5 raa4946b 10 10 11 11 from sasmodels.bumps_model import BumpsModel, plot_data, tic 12 try: from sasmodels import kernelcl 13 except: from sasmodels import kerneldll as kernelcl 12 from sasmodels import core 14 13 from sasmodels import kerneldll 15 14 from sasmodels.convert import revert_model … … 22 21 23 22 24 def sasview_model(model name, **pars):23 def sasview_model(model_definition, **pars): 25 24 """ 26 25 Load a sasview model given the model name. … … 28 27 # convert model parameters from sasmodel form to sasview form 29 28 #print "old",sorted(pars.items()) 30 modelname, pars = revert_model(model name, pars)29 modelname, pars = revert_model(model_definition, pars) 31 30 #print "new",sorted(pars.items()) 32 31 sas = __import__('sas.models.'+modelname) … … 48 47 model.setParam(k, v) 49 48 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 kernel56 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 kernel62 49 63 50 def randomize(p, v): … … 120 107 return value, average_time 121 108 122 def eval_opencl( name, pars, data, dtype='single', Nevals=1, cutoff=0):109 def eval_opencl(model_definition, pars, data, dtype='single', Nevals=1, cutoff=0): 123 110 try: 124 model = load_opencl(name, dtype=dtype)111 model = core.load_model(model_definition, dtype=dtype, platform="ocl") 125 112 except Exception,exc: 126 113 print exc 127 114 print "... trying again with single precision" 128 model = load_opencl(name, dtype='single')115 model = core.load_model(model_definition, dtype='single', platform="ocl") 129 116 problem = BumpsModel(data, model, cutoff=cutoff, **pars) 130 117 toc = tic() … … 136 123 return value, average_time 137 124 138 def eval_ctypes( name, pars, data, dtype='double', Nevals=1, cutoff=0):139 model = load_ctypes(name, dtype=dtype)125 def eval_ctypes(model_definition, pars, data, dtype='double', Nevals=1, cutoff=0): 126 model = core.load_model(model_definition, dtype=dtype, platform="dll") 140 127 problem = BumpsModel(data, model, cutoff=cutoff, **pars) 141 128 toc = tic() … … 193 180 print "pars",parlist(pars) 194 181 182 model_definition = core.load_model_definition(name) 195 183 # OpenCl calculation 196 184 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) 198 187 print "opencl t=%.1f ms, intensity=%.0f"%(ocl_time, sum(ocl[index])) 199 188 #print max(ocl), min(ocl) … … 201 190 # ctypes/sasview calculation 202 191 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) 204 194 comp = "ctypes" 205 195 print "ctypes t=%.1f ms, intensity=%.0f"%(cpu_time, sum(cpu[index])) 206 196 elif Ncpu > 0: 207 cpu, cpu_time = eval_sasview( name, pars, data, Ncpu)197 cpu, cpu_time = eval_sasview(model_definition, pars, data, Ncpu) 208 198 comp = "sasview" 209 199 print "sasview t=%.1f ms, intensity=%.0f"%(cpu_time, sum(cpu[index])) … … 219 209 #bad = (relerr>1e-4) 220 210 #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]) 226 225 227 226 # Plot if requested
Note: See TracChangeset
for help on using the changeset viewer.