Changeset 87985ca in sasmodels
- Timestamp:
- Sep 2, 2014 2:35:23 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:
- 19dcb933
- Parents:
- f4cf580
- Files:
-
- 36 deleted
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
compare.py
- Property mode changed from 100644 to 100755
ra953943 r87985ca 2 2 # -*- coding: utf-8 -*- 3 3 4 from sasmodel import SasModel, load_data, set_beam_stop, plot_data, tic, toc 4 import sys 5 import math 6 5 7 import numpy as np 6 8 9 from sasmodels.bumps_model import BumpsModel, plot_data, tic 10 from sasmodels import gpu, dll 11 from sasmodels.convert import revert_model 12 7 13 8 14 def sasview_model(modelname, **pars): 9 modelname = modelname+"Model" 15 """ 16 Load a sasview model given the model name. 17 """ 18 # convert model parameters from sasmodel form to sasview form 19 #print "old",sorted(pars.items()) 20 modelname, pars = revert_model(modelname, pars) 21 #print "new",sorted(pars.items()) 10 22 sans = __import__('sans.models.'+modelname) 11 23 ModelClass = getattr(getattr(sans.models,modelname,None),modelname,None) … … 21 33 elif k.endswith("_pd_nsigma"): 22 34 model.dispersion[k[:-10]]['nsigmas'] = v 35 elif k.endswith("_pd_type"): 36 model.dispersion[k[:-8]]['type'] = v 23 37 else: 24 38 model.setParam(k, v) 25 39 return model 26 40 27 28 def sasview_eval(model, data): 29 theory = model.evalDistribution([data.qx_data, data.qy_data]) 30 return theory 31 32 def plot(data, cpumodel, gpumodel, N, pars): 33 34 model = SasModel(data, gpumodel, dtype='f', **pars) 35 tic() 36 for i in range(N): 37 #pars['scale'] = np.random.rand() 38 model.update() 39 gpu = model.theory() 40 gpu_time = toc()*1000./N 41 print "ocl t=%.1f ms"%gpu_time 42 tic() 43 for i in range(N): 44 cpu = sasview_eval(cpumodel, data) 45 cpu_time = toc()*1000./N 46 print "omp t=%.1f ms"%cpu_time 47 41 def load_opencl(modelname, dtype='single'): 42 sasmodels = __import__('sasmodels.models.'+modelname) 43 module = getattr(sasmodels.models, modelname, None) 44 kernel = gpu.load_model(module, dtype=dtype) 45 return kernel 46 47 def load_ctypes(modelname, dtype='single'): 48 sasmodels = __import__('sasmodels.models.'+modelname) 49 module = getattr(sasmodels.models, modelname, None) 50 kernel = dll.load_model(module, dtype=dtype) 51 return kernel 52 53 def randomize(p, v): 54 """ 55 Randomizing parameter. 56 57 Guess the parameter type from name. 58 """ 59 if any(p.endswith(s) for s in ('_pd_n','_pd_nsigma','_pd_type')): 60 return v 61 elif any(s in p for s in ('theta','phi','psi')): 62 # orientation in [-180,180], orientation pd in [0,45] 63 if p.endswith('_pd'): 64 return 45*np.random.rand() 65 else: 66 return 360*np.random.rand() - 180 67 elif 'sld' in p: 68 # sld in in [-0.5,10] 69 return 10.5*np.random.rand() - 0.5 70 elif p.endswith('_pd'): 71 # length pd in [0,1] 72 return np.random.rand() 73 else: 74 # length, scale, background in [0,200] 75 return 200*np.random.rand() 76 77 def parlist(pars): 78 return "\n".join("%s: %s"%(p,v) for p,v in sorted(pars.items())) 79 80 def suppress_pd(pars): 81 """ 82 Suppress theta_pd for now until the normalization is resolved. 83 84 May also suppress complete polydispersity of the model to test 85 models more quickly. 86 """ 87 for p in pars: 88 if p.endswith("_pd"): pars[p] = 0 89 90 def compare(name, pars, Ncpu, Ngpu, opts, set_pars): 91 92 # Sort out data 93 qmax = 1.0 if '-highq' in opts else (0.2 if '-midq' in opts else 0.05) 94 if "-1d" in opts: 95 from sasmodels.bumps_model import empty_data1D 96 qmax = math.log10(qmax) 97 data = empty_data1D(np.logspace(qmax-3, qmax, 128)) 98 index = slice(None, None) 99 else: 100 from sasmodels.bumps_model import empty_data2D, set_beam_stop 101 data = empty_data2D(np.linspace(-qmax, qmax, 128)) 102 set_beam_stop(data, 0.004) 103 index = ~data.mask 104 is2D = hasattr(data, 'qx_data') 105 106 # modelling accuracy is determined by dtype and cutoff 107 dtype = 'double' if '-double' in opts else 'single' 108 cutoff_opts = [s for s in opts if s.startswith('-cutoff')] 109 cutoff = float(cutoff_opts[0].split('=')[1]) if cutoff_opts else 1e-5 110 111 # randomize parameters 112 random_opts = [s for s in opts if s.startswith('-random')] 113 if random_opts: 114 if '=' in random_opts[0]: 115 seed = int(random_opts[0].split('=')[1]) 116 else: 117 seed = int(np.random.rand()*10000) 118 np.random.seed(seed) 119 print "Randomize using -random=%i"%seed 120 # Note: the sort guarantees order of calls to random number generator 121 pars = dict((p,randomize(p,v)) for p,v in sorted(pars.items())) 122 # The capped cylinder model has a constraint on its parameters 123 if name == 'capped_cylinder' and pars['cap_radius'] < pars['radius']: 124 pars['radius'],pars['cap_radius'] = pars['cap_radius'],pars['radius'] 125 pars.update(set_pars) 126 127 # parameter selection 128 if '-mono' in opts: 129 suppress_pd(pars) 130 if '-pars' in opts: 131 print "pars",parlist(pars) 132 133 # OpenCl calculation 134 if Ngpu > 0: 135 try: 136 model = load_opencl(name, dtype=dtype) 137 except Exception,exc: 138 print exc 139 print "... trying again with single precision" 140 model = load_opencl(name, dtype='single') 141 problem = BumpsModel(data, model, cutoff=cutoff, **pars) 142 toc = tic() 143 for _ in range(Ngpu): 144 #pars['scale'] = np.random.rand() 145 problem.update() 146 gpu = problem.theory() 147 gpu_time = toc()*1000./Ngpu 148 print "opencl t=%.1f ms, intensity=%.0f"%(gpu_time, sum(gpu[index])) 149 #print max(gpu), min(gpu) 150 151 # ctypes/sasview calculation 152 if Ncpu > 0 and "-ctypes" in opts: 153 model = load_ctypes(name, dtype=dtype) 154 problem = BumpsModel(data, model, cutoff=cutoff, **pars) 155 toc = tic() 156 for _ in range(Ncpu): 157 problem.update() 158 cpu = problem.theory() 159 cpu_time = toc()*1000./Ncpu 160 comp = "ctypes" 161 print "ctypes t=%.1f ms, intensity=%.0f"%(cpu_time, sum(cpu[index])) 162 elif Ncpu > 0: 163 model = sasview_model(name, **pars) 164 toc = tic() 165 for _ in range(Ncpu): 166 if is2D: 167 cpu = model.evalDistribution([data.qx_data, data.qy_data]) 168 else: 169 cpu = model.evalDistribution(data.x) 170 cpu_time = toc()*1000./Ncpu 171 comp = "sasview" 172 print "sasview t=%.1f ms, intensity=%.0f"%(cpu_time, sum(cpu[index])) 173 174 # Compare, but only if computing both forms 175 if Ngpu > 0 and Ncpu > 0: 176 #print "speedup %.2g"%(cpu_time/gpu_time) 177 #print "max |gpu/cpu|", max(abs(gpu/cpu)), "%.15g"%max(abs(gpu)), "%.15g"%max(abs(cpu)) 178 #cpu *= max(gpu/cpu) 179 resid, relerr = np.zeros_like(gpu), np.zeros_like(gpu) 180 resid[index] = (gpu - cpu)[index] 181 relerr[index] = resid[index]/cpu[index] 182 print "max(|ocl-%s|)"%comp, max(abs(resid[index])) 183 print "max(|(ocl-%s)/ocl|)"%comp, max(abs(relerr[index])) 184 185 # Plot if requested 186 if '-noplot' in opts: return 48 187 import matplotlib.pyplot as plt 49 50 print "gpu/cpu", max(gpu/cpu) 51 cpu *= max(gpu/cpu) 52 relerr = (gpu - cpu)/cpu 53 print "max(|(ocl-omp)/ocl|)", max(abs(relerr)) 54 55 56 plt.subplot(131); plot_data(data, cpu); plt.title("omp t=%.1f ms"%cpu_time) 57 plt.subplot(132); plot_data(data, gpu); plt.title("ocl t=%.1f ms"%gpu_time) 58 plt.subplot(133); plot_data(data, relerr); plt.title("relerr x 10^8"); plt.colorbar() 188 if Ncpu > 0: 189 if Ngpu > 0: plt.subplot(131) 190 plot_data(data, cpu, scale='log') 191 plt.title("%s t=%.1f ms"%(comp,cpu_time)) 192 if Ngpu > 0: 193 if Ncpu > 0: plt.subplot(132) 194 plot_data(data, gpu, scale='log') 195 plt.title("opencl t=%.1f ms"%gpu_time) 196 if Ncpu > 0 and Ngpu > 0: 197 plt.subplot(133) 198 err = resid if '-abs' in opts else relerr 199 errstr = "abs err" if '-abs' in opts else "rel err" 200 #err,errstr = gpu/cpu,"ratio" 201 plot_data(data, err, scale='linear') 202 plt.title("max %s = %.3g"%(errstr, max(abs(err[index])))) 203 if is2D: plt.colorbar() 59 204 plt.show() 60 205 61 def newlen(N): 62 import sys 63 64 if len(sys.argv) > 1: 65 N = int(sys.argv[1]) 66 data = load_data('December/DEC07098.DAT') 67 set_beam_stop(data, 0.004) 68 69 return data, N 70 71 def cyl(N=1): 72 73 dtype = "float" 74 pars = dict( 75 scale=.003, radius=64.1, length=66.96, sldCyl=.291e-6, sldSolv=5.77e-6, background=.1, 76 cyl_theta=90, cyl_phi=0, 77 radius_pd=0.1, radius_pd_n=10, radius_pd_nsigma=3, 78 length_pd=0.1,length_pd_n=5, length_pd_nsigma=3, 79 cyl_theta_pd=0.1, cyl_theta_pd_n=5, cyl_theta_pd_nsigma=3, 80 cyl_phi_pd=0.1, cyl_phi_pd_n=10, cyl_phi_pd_nsigma=3) 81 82 from Models.code_cylinder_f import GpuCylinder as gpumodel 83 model = sasview_model('Cylinder', **pars) 84 data, N = newlen(N) 85 86 plot(data, model, gpumodel, N, pars) 87 88 89 def ellipse(N=1): 90 91 pars = dict(scale=.027, radius_a=60, radius_b=180, sldEll=.297e-6, sldSolv=5.773e-6, background=4.9, 92 axis_theta=0, axis_phi=90, 93 radius_a_pd=0.1, radius_a_pd_n=10, radius_a_pd_nsigma=3, 94 radius_b_pd=0.1, radius_b_pd_n=10, radius_b_pd_nsigma=3, 95 axis_theta_pd=0.1, axis_theta_pd_n=6, axis_theta_pd_nsigma=3, 96 axis_phi_pd=0.1, axis_phi_pd_n=6, axis_phi_pd_nsigma=3,) 97 98 from Models.code_ellipse import GpuEllipse as gpumodel 99 model = sasview_model('Ellipsoid', **pars) 100 101 data, N = newlen(N) 102 plot(data, model, gpumodel, N, pars) 103 104 def coreshell(N=1): 105 106 data, N = newlen(N) 107 108 pars = dict(scale= 1.77881e-06, radius=325, thickness=25, length=34.2709, 109 core_sld=1e-6, shell_sld=.291e-6, solvent_sld=7.105e-6, 110 background=223.827, axis_theta=90, axis_phi=0, 111 axis_theta_pd=15.8, 112 radius_pd=0.1, radius_pd_n=10, radius_pd_nsigma=3, 113 length_pd=0.1, length_pd_n=10, length_pd_nsigma=3, 114 thickness_pd=0.1, thickness_pd_n=5, thickness_pd_nsigma=3, 115 axis_theta_pd_n=20, axis_theta_pd_nsigma=5, 116 axis_phi_pd=0.0008748, axis_phi_pd_n=5, axis_phi_pd_nsigma=3,) 117 118 model = sasview_model('CoreShellCylinder', **pars) 119 from Models.code_coreshellcyl_f import GpuCoreShellCylinder as gpumodel 120 121 plot(data, model, gpumodel, N, pars) 122 123 def trellipse(N=1): 124 125 data, N = newlen(N) 126 127 pars = dict(scale=0.08, semi_axisA=15, semi_axisB=20, semi_axisC=500, 128 sldEll=7.105e-6, sldSolv=.291e-6, 129 background=5, axis_theta=0, axis_phi=0, axis_psi=0, 130 axis_theta_pd=20, axis_theta_pd_n=10, axis_theta_pd_nsigma=3, 131 axis_phi_pd=.1, axis_phi_pd_n=10, axis_phi_pd_nsigma=3, 132 axis_psi_pd=30, axis_psi_pd_n=5, axis_psi_pd_nsigma=3, 133 semi_axisA_pd=.1, semi_axisA_pd_n=5, semi_axisA_pd_nsigma=3, 134 semi_axisB_pd=.1, semi_axisB_pd_n=5, semi_axisB_pd_nsigma=3, 135 semi_axisC_pd=.1, semi_axisC_pd_n=5, semi_axisC_pd_nsigma=3,) 136 137 model = sasview_model('TriaxialEllipsoid', **pars) 138 from Models.code_triaxialellipse import GpuTriEllipse as gpumodel 139 140 plot(data, model, gpumodel, N, pars) 141 142 def lamellar(N=1): 143 144 data, N = newlen(N) 145 146 pars = dict(scale=0.08, 147 bi_thick=19.2946, 148 sld_bi=5.38e-6,sld_sol=7.105e-6, 149 background=0.003, 150 bi_thick_pd= 0.37765, bi_thick_pd_n=40, bi_thick_pd_nsigma=3) 151 152 model = sasview_model('Lamellar', **pars) 153 from Models.code_lamellar import GpuLamellar as gpumodel 154 155 plot(data, model, gpumodel, N, pars) 156 157 def cap(N=1): 158 159 data, N = newlen(N) 160 161 pars = dict(scale=.08, rad_cyl=20, rad_cap=40, len_cyl=400, 162 sld_capcyl=1e-6, sld_solv=6.3e-6, 163 background=0, theta=0, phi=0, 164 rad_cyl_pd=.1, rad_cyl_pd_n=10, rad_cyl_pd_nsigma=3, 165 rad_cap_pd=.1, rad_cap_pd_n=10, rad_cap_pd_nsigma=3, 166 len_cyl_pd=.1, len_cyl_pd_n=3, len_cyl_pd_nsigma=3, 167 theta_pd=.1, theta_pd_n=3, theta_pd_nsigma=3, 168 phi_pd=.1, phi_pd_n=3, phi_pd_nsigma=3) 169 170 171 model = sasview_model('CappedCylinder', **pars) 172 from Models.code_capcyl import GpuCapCylinder as gpumodel 173 174 plot(data, model, gpumodel, N, pars) 175 206 # =========================================================================== 207 # 208 USAGE=""" 209 usage: compare.py model [Nopencl] [Nsasview] [options...] [key=val] 210 211 Compare the speed and value for a model between the SasView original and the 212 OpenCL rewrite. 213 214 model is the name of the model to compare (see below). 215 Nopencl is the number of times to run the OpenCL model (default=5) 216 Nsasview is the number of times to run the Sasview model (default=1) 217 218 Options (* for default): 219 220 -plot*/-noplot plots or suppress the plot of the model 221 -single/-double uses double precision for comparison 222 -lowq/-midq/-highq use q values up to 0.05, 0.2 or 1.0 223 -2d/-1d uses 1d or 2d random data 224 -preset/-random[=seed] preset or random parameters 225 -poly/-mono force monodisperse/polydisperse 226 -sasview/-ctypes whether cpu is tested using sasview or ctypes 227 -cutoff=1e-5/value cutoff for including a point in polydispersity 228 -nopars/-pars prints the parameter set or not 229 -rel/-abs plot relative or absolute error 230 231 Key=value pairs allow you to set specific values to any of the model 232 parameters. 233 234 Available models: 235 236 %s 237 """ 238 239 VALID_OPTIONS = [ 240 'plot','noplot', 241 'single','double', 242 'lowq','midq','highq', 243 '2d','1d', 244 'preset','random', 245 'poly','mono', 246 'sasview','ctypes', 247 'nopars','pars', 248 'rel','abs', 249 ] 250 251 def main(): 252 opts = [arg for arg in sys.argv[1:] if arg.startswith('-')] 253 args = [arg for arg in sys.argv[1:] if not arg.startswith('-')] 254 models = "\n ".join("%-7s: %s"%(k,v.__name__.replace('_',' ')) 255 for k,v in sorted(MODELS.items())) 256 if len(args) == 0: 257 print(USAGE%models) 258 sys.exit(1) 259 if args[0] not in MODELS: 260 print "Model %r not available. Use one of:\n %s"%(args[0],models) 261 sys.exit(1) 262 263 valid_opts = set(VALID_OPTIONS) 264 invalid = [o[1:] for o in opts 265 if o[1:] not in valid_opts 266 and not o.startswith('-cutoff=') 267 and not o.startswith('-random=')] 268 if invalid: 269 print "Invalid options: %s"%(", ".join(invalid)) 270 sys.exit(1) 271 272 name, pars = MODELS[args[0]]() 273 Nopencl = int(args[1]) if len(args) > 1 else 5 274 Nsasview = int(args[2]) if len(args) > 3 else 1 275 276 # Fill in default polydispersity parameters 277 pds = set(p.split('_pd')[0] for p in pars if p.endswith('_pd')) 278 for p in pds: 279 if p+"_pd_nsigma" not in pars: pars[p+"_pd_nsigma"] = 3 280 if p+"_pd_type" not in pars: pars[p+"_pd_type"] = "gaussian" 281 282 # Fill in parameters given on the command line 283 set_pars = {} 284 for arg in args[3:]: 285 k,v = arg.split('=') 286 if k not in pars: 287 # extract base name without distribution 288 s = set(p.split('_pd')[0] for p in pars) 289 print "%r invalid; parameters are: %s"%(k,", ".join(sorted(s))) 290 sys.exit(1) 291 set_pars[k] = float(v) if not v.endswith('type') else v 292 293 compare(name, pars, Nsasview, Nopencl, opts, set_pars) 294 295 # =========================================================================== 296 # 297 298 MODELS = {} 299 def model(name): 300 def gather_function(fn): 301 MODELS[name] = fn 302 return fn 303 return gather_function 304 305 306 @model('cyl') 307 def cylinder(): 308 pars = dict( 309 scale=1, background=0, 310 sld=6, solvent_sld=1, 311 #radius=5, length=20, 312 radius=260, length=290, 313 theta=30, phi=0, 314 radius_pd=.2, radius_pd_n=1, 315 length_pd=.2,length_pd_n=1, 316 theta_pd=15, theta_pd_n=45, 317 phi_pd=15, phi_pd_n=1, 318 ) 319 return 'cylinder', pars 320 321 @model('capcyl') 322 def capped_cylinder(): 323 pars = dict( 324 scale=1, background=0, 325 sld=6, solvent_sld=1, 326 radius=260, cap_radius=290, length=290, 327 theta=30, phi=15, 328 radius_pd=.2, radius_pd_n=1, 329 cap_radius_pd=.2, cap_radius_pd_n=1, 330 length_pd=.2, length_pd_n=1, 331 theta_pd=15, theta_pd_n=45, 332 phi_pd=15, phi_pd_n=1, 333 ) 334 return 'capped_cylinder', pars 335 336 337 @model('cscyl') 338 def core_shell_cylinder(): 339 pars = dict( 340 scale=1, background=0, 341 core_sld=6, shell_sld=8, solvent_sld=1, 342 radius=45, thickness=25, length=340, 343 theta=30, phi=15, 344 radius_pd=.2, radius_pd_n=1, 345 length_pd=.2, length_pd_n=1, 346 thickness_pd=.2, thickness_pd_n=1, 347 theta_pd=15, theta_pd_n=45, 348 phi_pd=15, phi_pd_n=1, 349 ) 350 return 'core_shell_cylinder', pars 351 352 353 @model('ell') 354 def ellipsoid(): 355 pars = dict( 356 scale=1, background=0, 357 sld=6, solvent_sld=1, 358 rpolar=50, requatorial=30, 359 theta=30, phi=15, 360 rpolar_pd=.2, rpolar_pd_n=1, 361 requatorial_pd=.2, requatorial_pd_n=1, 362 theta_pd=15, theta_pd_n=45, 363 phi_pd=15, phi_pd_n=1, 364 ) 365 return 'ellipsoid', pars 366 367 368 @model('ell3') 369 def triaxial_ellipsoid(): 370 pars = dict( 371 scale=1, background=0, 372 sld=6, solvent_sld=1, 373 theta=30, phi=15, psi=5, 374 req_minor=25, req_major=36, rpolar=50, 375 req_minor_pd=0, req_minor_pd_n=1, 376 req_major_pd=0, req_major_pd_n=1, 377 rpolar_pd=.2, rpolar_pd_n=1, 378 theta_pd=15, theta_pd_n=45, 379 phi_pd=15, phi_pd_n=1, 380 psi_pd=15, psi_pd_n=1, 381 ) 382 return 'triaxial_ellipsoid', pars 383 384 @model('sph') 385 def sphere(): 386 pars = dict( 387 scale=1, background=0, 388 sld=6, solvent_sld=1, 389 radius=120, 390 radius_pd=.2, radius_pd_n=45, 391 ) 392 return 'sphere', pars 393 394 @model('lam') 395 def lamellar(): 396 pars = dict( 397 scale=1, background=0, 398 sld=6, solvent_sld=1, 399 thickness=40, 400 thickness_pd= 0.2, thickness_pd_n=40, 401 ) 402 return 'lamellar', pars 176 403 177 404 if __name__ == "__main__": 178 coreshell(1) 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 405 main() -
fit.py
r4001d6e r87985ca 2 2 # -*- coding: utf-8 -*- 3 3 4 import sys 4 5 from bumps.names import * 5 from sasmodel import SasModel, load_data, set_beam_stop, set_half, set_top 6 from Models.code_capcyl import GpuCapCylinder 7 from Models.code_coreshellcyl_f import GpuCoreShellCylinder 8 from Models.code_cylinder_f import GpuCylinder 9 #from Models.code_cylinder import GpuCylinder, OneDGpuCylinder 10 from Models.code_ellipse_f import GpuEllipse 11 from Models.code_lamellar import GpuLamellar 12 from Models.code_triaxialellipse import GpuTriEllipse 6 from sasmodels import bumps_model as sas 13 7 14 8 """ IMPORT THE DATA USED """ 15 #data = load_data('December/Tangential/Sector0/DEC07133.ABS') 16 data = load_data('December/DEC07098.DAT') 9 radial_data = sas.load_data('December/DEC07267.DAT') 10 sas.set_beam_stop(radial_data, 0.00669, outer=0.025) 11 sas.set_top(radial_data, -.0185) 17 12 18 """ SET INNER BEAM STOP, OUTER RING, AND MASK HALF OF THE DATA """ 19 s et_beam_stop(data, 0.004)#, outer=0.025)20 s et_top(data, -.018)21 #s et_half(data, 'right')13 tan_data = sas.load_data('December/DEC07266.DAT') 14 sas.set_beam_stop(tan_data, 0.00669, outer=0.025) 15 sas.set_top(tan_data, -.0185) 16 #sas.set_half(tan_data, 'right') 22 17 18 name = "ellipsoid" if len(sys.argv) < 2 else sys.argv[1] 19 section = "radial" if len(sys.argv) < 3 else sys.argv[2] 20 data = radial_data if section is not "tangent" else tan_data 21 kernel = sas.load_model(name, dtype="single") 23 22 24 if 0: 25 model = SasModel(data, OneDGpuCylinder, 26 scale=0.0013, radius=105, length=1000, 27 background=21, sldCyl=.291e-6,sldSolv=7.105e-6, 28 radius_pd=0.1,radius_pd_n=10,radius_pd_nsigma=0, 29 length_pd=0.1,length_pd_n=5,length_pd_nsigma=0, 30 bolim=0.0, uplim=90) #bottom limit, upper limit of angle integral 31 32 33 if 0: 34 model = SasModel(data, GpuEllipse, 23 if name == "ellipsoid": 24 model = sas.BumpsModel(data, kernel, 35 25 scale=0.08, 36 r adius_a=15, radius_b=800,37 sld Ell=.291e-6, sldSolv=7.105e-6,26 rpolar=15, requatorial=800, 27 sld=.291, solvent_sld=7.105, 38 28 background=0, 39 axis_theta=90, axis_phi=0,40 axis_theta_pd=15, axis_theta_pd_n=40, axis_theta_pd_nsigma=3,41 r adius_a_pd=0.222296, radius_a_pd_n=1, radius_a_pd_nsigma=0,42 r adius_b_pd=.000128, radius_b_pd_n=1, radius_b_pd_nsigma=0,43 axis_phi_pd=2.63698e-05, axis_phi_pd_n=20, axis_phi_pd_nsigma=3,44 dtype='float32')29 theta=90, phi=0, 30 theta_pd=15, theta_pd_n=40, theta_pd_nsigma=3, 31 rpolar_pd=0.222296, rpolar_pd_n=1, rpolar_pd_nsigma=0, 32 requatorial_pd=.000128, requatorial_pd_n=1, requatorial_pd_nsigma=0, 33 phi_pd=0, phi_pd_n=20, phi_pd_nsigma=3, 34 ) 45 35 46 36 47 37 # SET THE FITTING PARAMETERS 48 #model.r adius_a.range(15, 1000)49 #model.r adius_b.range(15, 1000)50 #model. axis_theta_pd.range(0, 360)38 #model.rpolar.range(15, 1000) 39 #model.requatorial.range(15, 1000) 40 #model.theta_pd.range(0, 360) 51 41 #model.background.range(0,1000) 52 42 model.scale.range(0, 1) 53 43 54 44 55 if 0:56 model = SasModel(data, GpuLamellar,45 elif name == "lamellar": 46 model = sas.BumpsModel(data, kernel, 57 47 scale=0.08, 58 bi_thick=19.2946,59 sld _bi=5.38e-6,sld_sol=7.105e-6,48 thickness=19.2946, 49 sld=5.38,sld_sol=7.105, 60 50 background=0.003, 61 bi_thick_pd= 0.37765, bi_thick_pd_n=10, bi_thick_pd_nsigma=3,62 dtype='float32')51 thickness_pd= 0.37765, thickness_pd_n=10, thickness_pd_nsigma=3, 52 ) 63 53 64 54 # SET THE FITTING PARAMETERS 65 #model. bi_thick.range(0, 1000)55 #model.thickness.range(0, 1000) 66 56 #model.scale.range(0, 1) 67 #model. bi_thick_pd.range(0, 1000)57 #model.thickness_pd.range(0, 1000) 68 58 #model.background.range(0, 1000) 69 model.sld _bi.range(0, 1)59 model.sld.range(0, 1) 70 60 71 61 72 if 0:62 elif name == "cylinder": 73 63 """ 74 64 pars = dict(scale=0.0023, radius=92.5, length=798.3, 75 sld Cyl=.29e-6, sldSolv=7.105e-6, background=5,76 cyl_theta=0, cyl_phi=0,77 cyl_theta_pd=22.11, cyl_theta_pd_n=5, cyl_theta_pd_nsigma=3,65 sld=.29, solvent_sld=7.105, background=5, 66 theta=0, phi=0, 67 theta_pd=22.11, theta_pd_n=5, theta_pd_nsigma=3, 78 68 radius_pd=.0084, radius_pd_n=10, radius_pd_nsigma=3, 79 69 length_pd=0.493, length_pd_n=10, length_pd_nsigma=3, 80 cyl_phi_pd=0, cyl_phi_pd_n=5, cyl_phi_pd_nsigma=3,)70 phi_pd=0, phi_pd_n=5, phi_pd_nsigma=3,) 81 71 """ 82 72 pars = dict( 83 scale=.01, radius=64.1, length=66.96, sld Cyl=.291e-6, sldSolv=5.77e-6, background=.1,84 cyl_theta=90, cyl_phi=0,73 scale=.01, radius=64.1, length=66.96, sld=.291, solvent_sld=5.77, background=.1, 74 theta=90, phi=0, 85 75 radius_pd=0.1, radius_pd_n=10, radius_pd_nsigma=3, 86 76 length_pd=0.1,length_pd_n=5, length_pd_nsigma=3, 87 cyl_theta_pd=0.1, cyl_theta_pd_n=5, cyl_theta_pd_nsigma=3,88 cyl_phi_pd=0.1, cyl_phi_pd_n=10, cyl_phi_pd_nsigma=3)89 model = SasModel(data, GpuCylinder, dtype="float32", **pars)77 theta_pd=0.1, theta_pd_n=5, theta_pd_nsigma=3, 78 phi_pd=0.1, phi_pd_n=10, phi_pd_nsigma=3) 79 model = sas.BumpsModel(data, kernel, **pars) 90 80 91 81 … … 94 84 model.radius.range(1, 500) 95 85 model.length.range(1, 5000) 96 #model. cyl_theta.range(-90,100)97 #model. cyl_theta_pd.range(0, 90)98 #model. cyl_theta_pd_n = model.cyl_theta_pd + 586 #model.theta.range(-90,100) 87 #model.theta_pd.range(0, 90) 88 #model.theta_pd_n = model.theta_pd + 5 99 89 #model.radius_pd.range(0, 90) 100 90 #model.length_pd.range(0, 90) 101 91 model.scale.range(0, 1) 102 92 #model.background.range(0, 100) 103 #model.sld Cyl.range(0, 1)93 #model.sld.range(0, 1) 104 94 105 95 106 if 1:107 model = SasModel(data, GpuCoreShellCylinder,96 elif name == "core_shell_cylinder": 97 model = sas.BumpsModel(data, kernel, 108 98 scale= .031, radius=19.5, thickness=30, length=22, 109 core_sld=7.105 e-6, shell_sld=.291e-6, solvent_sld=7.105e-6,110 background=0, axis_theta=0, axis_phi=0,99 core_sld=7.105, shell_sld=.291, solvent_sld=7.105, 100 background=0, theta=0, phi=0, 111 101 112 102 radius_pd=0.26, radius_pd_n=10, radius_pd_nsigma=3, 113 103 length_pd=0.26, length_pd_n=10, length_pd_nsigma=3, 114 104 thickness_pd=1, thickness_pd_n=1, thickness_pd_nsigma=1, 115 axis_theta_pd=1, axis_theta_pd_n=10, axis_theta_pd_nsigma=3,116 axis_phi_pd=0.1, axis_phi_pd_n=1, axis_phi_pd_nsigma=1,117 dtype='float32')105 theta_pd=1, theta_pd_n=10, theta_pd_nsigma=3, 106 phi_pd=0.1, phi_pd_n=1, phi_pd_nsigma=1, 107 ) 118 108 119 109 # SET THE FITTING PARAMETERS … … 122 112 #model.thickness.range(18, 38) 123 113 #model.thickness_pd.range(0, 1) 124 #model. axis_phi.range(0, 90)114 #model.phi.range(0, 90) 125 115 #model.radius_pd.range(0, 1) 126 116 #model.length_pd.range(0, 1) 127 #model. axis_theta_pd.range(0, 360)117 #model.theta_pd.range(0, 360) 128 118 #model.background.range(0,5) 129 119 model.scale.range(0, 1) … … 131 121 132 122 133 if 0:134 model = SasModel(data, GpuCapCylinder,135 scale=.08, rad _cyl=20, rad_cap=40, len_cyl=400,136 sld_capcyl=1 e-6, sld_solv=6.3e-6,123 elif name == "capped_cylinder": 124 model = sas.BumpsModel(data, kernel, 125 scale=.08, radius=20, cap_radius=40, length=400, 126 sld_capcyl=1, sld_solv=6.3, 137 127 background=0, theta=0, phi=0, 138 rad _cyl_pd=.1, rad_cyl_pd_n=5, rad_cyl_pd_nsigma=3,139 rad_cap_pd=.1, rad_cap_pd_n=5, rad_cap_pd_nsigma=3,140 len _cyl_pd=.1, len_cyl_pd_n=1, len_cyl_pd_nsigma=0,128 radius_pd=.1, radius_pd_n=5, radius_pd_nsigma=3, 129 cap_radius_pd=.1, cap_radius_pd_n=5, cap_radius_pd_nsigma=3, 130 length_pd=.1, length_pd_n=1, length_pd_nsigma=0, 141 131 theta_pd=.1, theta_pd_n=1, theta_pd_nsigma=0, 142 132 phi_pd=.1, phi_pd_n=1, phi_pd_nsigma=0, 143 dtype= 'float32')133 dtype=dtype) 144 134 145 135 model.scale.range(0, 1) 146 136 147 137 148 if 0:149 model = SasModel(data, GpuTriEllipse,150 scale=0.08, axisA=15, axisB=20, axisC=500,151 sldEll=7.105 e-6, sldSolv=.291e-6,138 elif name == "triaxial_ellipsoid": 139 model = sas.BumpsModel(data, kernel, 140 scale=0.08, req_minor=15, req_major=20, rpolar=500, 141 sldEll=7.105, solvent_sld=.291, 152 142 background=5, theta=0, phi=0, psi=0, 153 143 theta_pd=20, theta_pd_n=40, theta_pd_nsigma=3, 154 144 phi_pd=.1, phi_pd_n=1, phi_pd_nsigma=0, 155 145 psi_pd=30, psi_pd_n=1, psi_pd_nsigma=0, 156 axisA_pd=.1, axisA_pd_n=1, axisA_pd_nsigma=0,157 axisB_pd=.1, axisB_pd_n=1, axisB_pd_nsigma=0,158 axisC_pd=.1, axisC_pd_n=1, axisC_pd_nsigma=0, dtype='float32')146 req_minor_pd=.1, req_minor_pd_n=1, req_minor_pd_nsigma=0, 147 req_major_pd=.1, req_major_pd_n=1, req_major_pd_nsigma=0, 148 rpolar_pd=.1, rpolar_pd_n=1, rpolar_pd_nsigma=0, dtype=dtype) 159 149 160 150 # SET THE FITTING PARAMETERS 161 model. axisA.range(15, 1000)162 model. axisB.range(15, 1000)163 #model. axisC.range(15, 1000)151 model.req_minor.range(15, 1000) 152 model.req_major.range(15, 1000) 153 #model.rpolar.range(15, 1000) 164 154 #model.background.range(0,1000) 165 155 #model.theta_pd.range(0, 360) … … 167 157 #model.psi_pd.range(0, 360) 168 158 159 else: 160 print "No parameters for %s"%name 161 sys.exit(1) 169 162 163 if section is "both": 164 tan_model = sas.BumpsModel(tan_data, model.model, model.parameters()) 165 tan_model.phi = model.phi - 90 166 problem = FitProblem([model, tan_model]) 167 else: 168 problem = FitProblem(model) 170 169 171 172 problem = FitProblem(model)173 -
sasmodels/bumps_model.py
r5d4777d r87985ca 6 6 7 7 import numpy as np 8 9 try: 10 from .gpu import load_model as _loader 11 except ImportError,exc: 12 warnings.warn(str(exc)) 13 warnings.warn("OpenCL not available --- using ctypes instead") 14 from .dll import load_model as _loader 15 16 def load_model(modelname, dtype='single'): 17 """ 18 Load model by name. 19 """ 20 sasmodels = __import__('sasmodels.models.'+modelname) 21 module = getattr(sasmodels.models, modelname, None) 22 model = _loader(module, dtype=dtype) 23 return model 8 24 9 25 … … 235 251 partype = model.info['partype'] 236 252 253 # remember inputs so we can inspect from outside 254 self.data = data 255 self.model = model 256 237 257 # interpret data 238 self.data = data239 258 if hasattr(data, 'qx_data'): 240 259 self.index = (data.mask==0) & (~np.isnan(data.data)) -
sasmodels/sasview_model.py
r5d4777d r87985ca 1 """ 2 Sasview model constructor. 3 4 Given a module defining an OpenCL kernel such as sasmodels.models.cylinder, 5 create a sasview model class to run that kernel as follows:: 6 7 from sasmodels.sasview_model import make_class 8 from sasmodels.models import cylinder 9 CylinderModel = make_class(cylinder, dtype='single') 10 11 The model parameters for sasmodels are different from those in sasview. 12 When reloading previously saved models, the parameters should be converted 13 using :func:`sasmodels.convert.convert`. 14 """ 15 16 # TODO: add a sasview=>sasmodels parameter translation layer 17 # this will allow us to use the new sasmodels as drop in replacements, and 18 # delay renaming parameters until all models have been converted. 19 1 20 import math 2 21 from copy import deepcopy … … 16 35 """ 17 36 Load the sasview model defined in *kernel_module*. 18 :param kernel_module: 19 :param dtype: 20 :return: 37 38 Returns a class that can be used directly as a sasview model. 21 39 """ 22 40 model = load_model(kernel_module, dtype=dtype) -
setup_py2exe.py
r14de349 r87985ca 40 40 import sys 41 41 42 sys.dont_write_bytecode = True42 #sys.dont_write_bytecode = True 43 43 44 44 # Force build before continuing … … 228 228 # Specify required packages to bundle in the executable image. 229 229 packages = ['numpy', 'scipy', 'matplotlib', 'pytz', 'pyparsing', 230 'periodictable', 'bumps .names', 'dream'230 'periodictable', 'bumps', 'sasmodels', 'pyopencl', 231 231 ] 232 232
Note: See TracChangeset
for help on using the changeset viewer.