Changeset 346bc88 in sasmodels for compare.py
- Timestamp:
- Aug 31, 2015 10:24:45 PM (9 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:
- 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)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
compare.py
raa4946b r346bc88 9 9 import numpy as np 10 10 11 from sasmodels.bumps_model import BumpsModel, plot_data, tic11 from sasmodels.bumps_model import Model, Experiment, plot_theory, tic 12 12 from sasmodels import core 13 13 from sasmodels import kerneldll … … 97 97 98 98 def eval_sasview(name, pars, data, Nevals=1): 99 from sas.models.qsmearing import smear_selection 99 100 model = sasview_model(name, **pars) 101 smearer = smear_selection(data, model=model) 100 102 toc = tic() 101 103 for _ in range(Nevals): 102 104 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]]) 104 114 else: 105 115 value = model.evalDistribution(data.x) 116 if smearer is not None: 117 value = smearer(value) 106 118 average_time = toc()*1000./Nevals 107 119 return value, average_time … … 114 126 print "... trying again with single precision" 115 127 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) 117 129 toc = tic() 118 130 for _ in range(Nevals): … … 125 137 def eval_ctypes(model_definition, pars, data, dtype='double', Nevals=1, cutoff=0): 126 138 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) 128 140 toc = tic() 129 141 for _ in range(Nevals): … … 133 145 return value, average_time 134 146 135 def make_data(qmax, is2D, Nq=128, view='log'):147 def make_data(qmax, is2D, Nq=128, resolution=0.0, view='log'): 136 148 if is2D: 137 149 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) 139 151 set_beam_stop(data, 0.004) 140 152 index = ~data.mask … … 146 158 else: 147 159 q = np.linspace(0.001*qmax, qmax, Nq) 148 data = empty_data1D(q )160 data = empty_data1D(q, resolution=resolution) 149 161 index = slice(None, None) 150 162 return data, index … … 159 171 qmax = 10.0 if '-exq' in opts else 1.0 if '-highq' in opts else 0.2 if '-midq' in opts else 0.05 160 172 Nq = int(opt_values.get('-Nq', '128')) 173 res = float(opt_values.get('-res', '0')) 161 174 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) 163 176 164 177 … … 185 198 ocl, ocl_time = eval_opencl(model_definition, pars, data, 186 199 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)) 188 201 #print max(ocl), min(ocl) 189 202 … … 193 206 dtype=dtype, cutoff=cutoff, Nevals=Ncpu) 194 207 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)) 196 209 elif Ncpu > 0: 197 210 cpu, cpu_time = eval_sasview(model_definition, pars, data, Ncpu) 198 211 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)) 200 213 201 214 # Compare, but only if computing both forms … … 204 217 #print "max |ocl/cpu|", max(abs(ocl/cpu)), "%.15g"%max(abs(ocl)), "%.15g"%max(abs(cpu)) 205 218 #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 209 221 #bad = (relerr>1e-4) 210 222 #print relerr[bad],cpu[bad],ocl[bad],data.qx_data[bad],data.qy_data[bad] … … 221 233 ] 222 234 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) 225 237 226 238 # Plot if requested … … 229 241 if Ncpu > 0: 230 242 if Nocl > 0: plt.subplot(131) 231 plot_ data(data, cpu, view=view)243 plot_theory(data, cpu, view=view) 232 244 plt.title("%s t=%.1f ms"%(comp,cpu_time)) 233 245 cbar_title = "log I" 234 246 if Nocl > 0: 235 247 if Ncpu > 0: plt.subplot(132) 236 plot_ data(data, ocl, view=view)248 plot_theory(data, ocl, view=view) 237 249 plt.title("opencl t=%.1f ms"%ocl_time) 238 250 cbar_title = "log I" … … 244 256 err,errstr,errview = abs(relerr), "rel err", "log" 245 257 #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)))) 248 260 cbar_title = errstr if errview=="linear" else "log "+errstr 249 261 if is2D: … … 253 265 if Ncpu > 0 and Nocl > 0 and '-hist' in opts: 254 266 plt.figure() 255 v = relerr [index]267 v = relerr 256 268 v[v==0] = 0.5*np.min(np.abs(v[v!=0])) 257 269 plt.hist(np.log10(np.abs(v)), normed=1, bins=50); … … 289 301 -linear/-log/-q4 intensity scaling 290 302 -hist/-nohist* plot histogram of relative error 303 -res=0 sets the resolution width dQ/Q if calculating with resolution 291 304 292 305 Key=value pairs allow you to set specific values to any of the model … … 313 326 VALUE_OPTIONS = [ 314 327 # Note: random is both a name option and a value option 315 'cutoff', 'random', 'Nq', 328 'cutoff', 'random', 'Nq', 'res', 316 329 ] 317 330
Note: See TracChangeset
for help on using the changeset viewer.