Changeset 216a9e1 in sasmodels for compare.py
- Timestamp:
- Dec 3, 2014 11:38:49 AM (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:
- 8fc26f8
- Parents:
- 31819c5
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
compare.py
rba69383 r216a9e1 75 75 return 200*np.random.rand() 76 76 77 def randomize_model(name, pars, seed=None): 78 if seed is None: 79 seed = np.random.randint(1e9) 80 np.random.seed(seed) 81 # Note: the sort guarantees order of calls to random number generator 82 pars = dict((p,randomize(p,v)) for p,v in sorted(pars.items())) 83 # The capped cylinder model has a constraint on its parameters 84 if name == 'capped_cylinder' and pars['cap_radius'] < pars['radius']: 85 pars['radius'],pars['cap_radius'] = pars['cap_radius'],pars['radius'] 86 return pars, seed 87 77 88 def parlist(pars): 78 89 return "\n".join("%s: %s"%(p,v) for p,v in sorted(pars.items())) … … 88 99 if p.endswith("_pd"): pars[p] = 0 89 100 101 def eval_sasview(name, pars, data, Nevals=1): 102 model = sasview_model(name, **pars) 103 toc = tic() 104 for _ in range(Nevals): 105 if hasattr(data, 'qx_data'): 106 value = model.evalDistribution([data.qx_data, data.qy_data]) 107 else: 108 value = model.evalDistribution(data.x) 109 average_time = toc()*1000./Nevals 110 return value, average_time 111 112 def eval_opencl(name, pars, data, dtype='single', Nevals=1, cutoff=0): 113 try: 114 model = load_opencl(name, dtype=dtype) 115 except Exception,exc: 116 print exc 117 print "... trying again with single precision" 118 model = load_opencl(name, dtype='single') 119 problem = BumpsModel(data, model, cutoff=cutoff, **pars) 120 toc = tic() 121 for _ in range(Nevals): 122 #pars['scale'] = np.random.rand() 123 problem.update() 124 value = problem.theory() 125 average_time = toc()*1000./Nevals 126 return value, average_time 127 128 def eval_ctypes(name, pars, data, dtype='double', Nevals=1, cutoff=0): 129 model = load_ctypes(name, dtype=dtype) 130 problem = BumpsModel(data, model, cutoff=cutoff, **pars) 131 toc = tic() 132 for _ in range(Nevals): 133 problem.update() 134 value = problem.theory() 135 average_time = toc()*1000./Nevals 136 return value, average_time 137 138 def make_data(qmax, is2D, Nq=128): 139 if is2D: 140 from sasmodels.bumps_model import empty_data2D, set_beam_stop 141 data = empty_data2D(np.linspace(-qmax, qmax, Nq)) 142 set_beam_stop(data, 0.004) 143 index = ~data.mask 144 else: 145 from sasmodels.bumps_model import empty_data1D 146 qmax = math.log10(qmax) 147 data = empty_data1D(np.logspace(qmax-3, qmax, Nq)) 148 index = slice(None, None) 149 return data, index 150 90 151 def compare(name, pars, Ncpu, Ngpu, opts, set_pars): 152 opt_values = dict(split 153 for s in opts for split in ((s.split('='),)) 154 if len(split) == 2) 91 155 # Sort out data 92 156 qmax = 1.0 if '-highq' in opts else (0.2 if '-midq' in opts else 0.05) 93 if "-1d" in opts: 94 from sasmodels.bumps_model import empty_data1D 95 qmax = math.log10(qmax) 96 data = empty_data1D(np.logspace(qmax-3, qmax, 128)) 97 index = slice(None, None) 98 else: 99 from sasmodels.bumps_model import empty_data2D, set_beam_stop 100 data = empty_data2D(np.linspace(-qmax, qmax, 128)) 101 set_beam_stop(data, 0.004) 102 index = ~data.mask 103 is2D = hasattr(data, 'qx_data') 157 Nq = int(opt_values.get('-Nq', '128')) 158 is2D = not "-1d" in opts 159 data, index = make_data(qmax, is2D, Nq) 160 104 161 105 162 # modelling accuracy is determined by dtype and cutoff 106 163 dtype = 'double' if '-double' in opts else 'single' 107 cutoff_opts = [s for s in opts if s.startswith('-cutoff')] 108 cutoff = float(cutoff_opts[0].split('=')[1]) if cutoff_opts else 1e-5 164 cutoff = float(opt_values.get('-cutoff','1e-5')) 109 165 110 166 # randomize parameters 111 random_opts = [s for s in opts if s.startswith('-random')] 112 if random_opts: 113 if '=' in random_opts[0]: 114 seed = int(random_opts[0].split('=')[1]) 115 else: 116 seed = int(np.random.rand()*10000) 117 np.random.seed(seed) 167 if '-random' in opts or '-random' in opt_values: 168 seed = int(opt_values['-random']) if '-random' in opt_values else None 169 pars, seed = randomize_model(name, pars, seed=seed) 118 170 print "Randomize using -random=%i"%seed 119 # Note: the sort guarantees order of calls to random number generator120 pars = dict((p,randomize(p,v)) for p,v in sorted(pars.items()))121 # The capped cylinder model has a constraint on its parameters122 if name == 'capped_cylinder' and pars['cap_radius'] < pars['radius']:123 pars['radius'],pars['cap_radius'] = pars['cap_radius'],pars['radius']124 171 pars.update(set_pars) 125 172 … … 132 179 # OpenCl calculation 133 180 if Ngpu > 0: 134 try: 135 model = load_opencl(name, dtype=dtype) 136 except Exception,exc: 137 print exc 138 print "... trying again with single precision" 139 model = load_opencl(name, dtype='single') 140 problem = BumpsModel(data, model, cutoff=cutoff, **pars) 141 toc = tic() 142 for _ in range(Ngpu): 143 #pars['scale'] = np.random.rand() 144 problem.update() 145 gpu = problem.theory() 146 gpu_time = toc()*1000./Ngpu 181 gpu, gpu_time = eval_opencl(name, pars, data, dtype, Ngpu) 147 182 print "opencl t=%.1f ms, intensity=%.0f"%(gpu_time, sum(gpu[index])) 148 183 #print max(gpu), min(gpu) … … 150 185 # ctypes/sasview calculation 151 186 if Ncpu > 0 and "-ctypes" in opts: 152 model = load_ctypes(name, dtype=dtype) 153 problem = BumpsModel(data, model, cutoff=cutoff, **pars) 154 toc = tic() 155 for _ in range(Ncpu): 156 problem.update() 157 cpu = problem.theory() 158 cpu_time = toc()*1000./Ncpu 187 cpu, cpu_time = eval_ctypes(name, pars, data, dtype=dtype, cutoff=cutoff, Nevals=Ncpu) 159 188 comp = "ctypes" 160 189 print "ctypes t=%.1f ms, intensity=%.0f"%(cpu_time, sum(cpu[index])) 161 190 elif Ncpu > 0: 162 model = sasview_model(name, **pars) 163 toc = tic() 164 for _ in range(Ncpu): 165 if is2D: 166 cpu = model.evalDistribution([data.qx_data, data.qy_data]) 167 else: 168 cpu = model.evalDistribution(data.x) 169 cpu_time = toc()*1000./Ncpu 191 cpu, cpu_time = eval_sasview(name, pars, data, Ncpu) 170 192 comp = "sasview" 171 193 print "sasview t=%.1f ms, intensity=%.0f"%(cpu_time, sum(cpu[index])) … … 235 257 -single*/-double uses double precision for comparison 236 258 -lowq*/-midq/-highq use q values up to 0.05, 0.2 or 1.0 237 -1d/-2d* uses 1d or 2d random data 259 -Nq=128 sets the number of Q points in the data set 260 -1d/-2d* computes 1d or 2d data 238 261 -preset*/-random[=seed] preset or random parameters 239 262 -mono/-poly* force monodisperse/polydisperse … … 252 275 """ 253 276 254 VALID_OPTIONS =[277 NAME_OPTIONS = set([ 255 278 'plot','noplot', 256 279 'single','double', … … 263 286 'rel','abs', 264 287 'hist','nohist', 288 ]) 289 VALUE_OPTIONS = [ 290 # Note: random is both a name option and a value option 291 'cutoff', 'random', 'Nq', 265 292 ] 266 293 … … 277 304 sys.exit(1) 278 305 279 valid_opts = set(VALID_OPTIONS)280 306 invalid = [o[1:] for o in opts 281 if o[1:] not in valid_opts 282 and not o.startswith('-cutoff=') 283 and not o.startswith('-random=')] 307 if o[1:] not in NAME_OPTIONS 308 and not any(o.startswith('-%s='%t) for t in VALUE_OPTIONS)] 284 309 if invalid: 285 310 print "Invalid options: %s"%(", ".join(invalid)) … … 328 353 radius=260, length=290, 329 354 theta=30, phi=0, 330 radius_pd=.2, radius_pd_n= 1,331 length_pd=.2,length_pd_n=1 ,355 radius_pd=.2, radius_pd_n=9, 356 length_pd=.2,length_pd_n=10, 332 357 theta_pd=15, theta_pd_n=45, 333 358 phi_pd=15, phi_pd_n=1, … … 344 369 radius_pd=.2, radius_pd_n=1, 345 370 cap_radius_pd=.2, cap_radius_pd_n=1, 346 length_pd=.2, length_pd_n=1 ,371 length_pd=.2, length_pd_n=10, 347 372 theta_pd=15, theta_pd_n=45, 348 373 phi_pd=15, phi_pd_n=1, … … 359 384 theta=30, phi=15, 360 385 radius_pd=.2, radius_pd_n=1, 361 length_pd=.2, length_pd_n=1 ,362 thickness_pd=.2, thickness_pd_n=1 ,386 length_pd=.2, length_pd_n=10, 387 thickness_pd=.2, thickness_pd_n=10, 363 388 theta_pd=15, theta_pd_n=45, 364 389 phi_pd=15, phi_pd_n=1, … … 374 399 rpolar=50, requatorial=30, 375 400 theta=30, phi=15, 376 rpolar_pd=.2, rpolar_pd_n=1 ,377 requatorial_pd=.2, requatorial_pd_n=1 ,401 rpolar_pd=.2, rpolar_pd_n=15, 402 requatorial_pd=.2, requatorial_pd_n=15, 378 403 theta_pd=15, theta_pd_n=45, 379 404 phi_pd=15, phi_pd_n=1, … … 391 416 req_minor_pd=0, req_minor_pd_n=1, 392 417 req_major_pd=0, req_major_pd_n=1, 393 rpolar_pd=.2, rpolar_pd_n= 1,418 rpolar_pd=.2, rpolar_pd_n=30, 394 419 theta_pd=15, theta_pd_n=45, 395 420 phi_pd=15, phi_pd_n=1,
Note: See TracChangeset
for help on using the changeset viewer.