Changeset ce27e21 in sasmodels for sasmodels/core.py
- Timestamp:
- Aug 24, 2014 7:18:14 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:
- 1780d59
- Parents:
- 14de349
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sasmodels/core.py
r14de349 rce27e21 4 4 import sys, os 5 5 import datetime 6 import warnings 6 7 7 8 import numpy as np … … 14 15 return gen.make(modelpath) 15 16 17 18 16 19 def opencl_model(modelname, dtype="single"): 17 20 from sasmodels import gpu 18 21 19 source, meta, _ = load_model(modelname)22 source, info, _ = load_model(modelname) 20 23 # for debugging, save source to a .cl file, edit it, and reload as model 21 24 #open(modelname+'.cl','w').write(source) 22 25 #source = open(modelname+'.cl','r').read() 23 return gpu.GpuModel(source, meta, dtype)26 return gpu.GpuModel(source, info, dtype) 24 27 25 28 … … 31 34 COMPILE = "cc -shared -fPIC -std=c99 -fopenmp -O2 -Wall %s -o %s -lm" 32 35 DLL_PATH = "/tmp" 33 def dll_path(meta): 36 37 38 def dll_path(info): 34 39 from os.path import join as joinpath, split as splitpath, splitext 35 basename = splitext(splitpath( meta['filename'])[1])[0]40 basename = splitext(splitpath(info['filename'])[1])[0] 36 41 return joinpath(DLL_PATH, basename+'.so') 42 37 43 38 44 def dll_model(modelname): … … 40 46 from sasmodels import dll 41 47 42 source, meta, _ = load_model(modelname)43 dllpath = dll_path( meta)48 source, info, _ = load_model(modelname) 49 dllpath = dll_path(info) 44 50 if not os.path.exists(dllpath) \ 45 or (os.path.getmtime(dllpath) < os.path.getmtime( meta['filename'])):51 or (os.path.getmtime(dllpath) < os.path.getmtime(info['filename'])): 46 52 # Replace with a proper temp file 47 53 srcfile = '/tmp/%s.c'%modelname 48 54 open(srcfile, 'w').write(source) 49 55 os.system(COMPILE%(srcfile, dllpath)) 50 return dll.DllModel(dllpath, meta) 56 return dll.DllModel(dllpath, info) 57 51 58 52 59 TIC = None … … 57 64 return TIC 58 65 66 59 67 def toc(): 60 68 return TIC() 69 61 70 62 71 def load_data(filename): … … 68 77 return data 69 78 79 70 80 def fake_data2D(qx, qy=None): 71 81 from sans.dataloader.data_info import Data2D, Detector 72 73 82 74 83 if qy is None: … … 121 130 data.mask &= (data.x<outer) 122 131 132 123 133 def set_half(data, half): 124 134 from sans.dataloader.manipulations import Boxcut … … 128 138 data.mask += Boxcut(x_min=0.0, x_max=np.inf, y_min=-np.inf, y_max=np.inf)(data) 129 139 140 130 141 def set_top(data, max): 131 142 from sans.dataloader.manipulations import Boxcut 132 143 data.mask += Boxcut(x_min=-np.inf, x_max=np.inf, y_min=-np.inf, y_max=max)(data) 144 133 145 134 146 def plot_data(data, iq, vmin=None, vmax=None): … … 141 153 interpolation='nearest', aspect=1, origin='upper', 142 154 extent=[xmin, xmax, ymin, ymax], vmin=vmin, vmax=vmax) 155 143 156 144 157 def plot_result2D(data, theory, view='linear'): … … 184 197 mresid = masked_array((theory-data.y)/data.dy, mdata.mask) 185 198 186 plt.subplot(1 ,2,1)199 plt.subplot(121) 187 200 plt.errorbar(data.x, mdata, yerr=data.dy) 188 201 plt.plot(data.x, mtheory, '-', hold=True) 189 202 plt.yscale(view) 190 plt.subplot(1 , 2,2)203 plt.subplot(122) 191 204 plt.plot(data.x, mresid, 'x') 192 205 #plt.axhline(1, color='black', ls='--',lw=1, hold=True) … … 219 232 220 233 # create model 221 self.fn = model(input, cutoff=cutoff) 234 self.fn = model(input) 235 self.cutoff = cutoff 222 236 223 237 # define bumps parameters 224 238 pars = [] 225 for p in model.meta['parameters']: 239 extras = [] 240 for p in model.info['parameters']: 226 241 name, default, limits, ptype = p[0], p[2], p[3], p[4] 227 242 value = kw.pop(name, default) 228 243 setattr(self, name, Parameter.default(value, name=name, limits=limits)) 229 244 pars.append(name) 230 if ptype != "": 231 for xpart,xdefault,xlimits in [ 232 ('_pd', 0, limits), 233 ('_pd_n', 35, (0,1000)), 234 ('_pd_nsigma', 3, (0,10)), 235 ]: 236 xname = name+xpart 237 xvalue = kw.pop(xname, xdefault) 238 setattr(self, xname, Parameter.default(xvalue, name=xname)) 245 for name in model.info['partype']['pd-2d']: 246 for xpart,xdefault,xlimits in [ 247 ('_pd', 0, limits), 248 ('_pd_n', 35, (0,1000)), 249 ('_pd_nsigma', 3, (0, 10)), 250 ('_pd_type', 'gaussian', None), 251 ]: 252 xname = name+xpart 253 xvalue = kw.pop(xname, xdefault) 254 if xlimits is not None: 255 xvalue = Parameter.default(xvalue, name=xname, limits=xlimits) 239 256 pars.append(xname) 257 setattr(self, xname, xvalue) 258 self._parameter_names = pars 240 259 if kw: 241 260 raise TypeError("unexpected parameters: %s"%(", ".join(sorted(kw.keys())))) 242 self._parameter_names = pars243 261 self.update() 244 262 … … 254 272 def theory(self): 255 273 if 'theory' not in self._cache: 256 pars = dict((k,getattr(self,k).value) for k in self._parameter_names) 274 pars = [getattr(self,p).value for p in self.fn.fixed_pars] 275 pd_pars = [self._get_weights(p) for p in self.fn.pd_pars] 257 276 #print pars 258 self._theory[self.index] = self.fn .eval(pars)259 #self._theory[:] = self.fn.eval(pars )277 self._theory[self.index] = self.fn(pars, pd_pars, self.cutoff) 278 #self._theory[:] = self.fn.eval(pars, pd_pars) 260 279 self._cache['theory'] = self._theory 261 280 return self._cache['theory'] … … 282 301 pass 283 302 303 def _get_weights(self, par): 304 from . import weights 305 306 relative = self.fn.info['partype']['pd-rel'] 307 limits = self.fn.info['limits'] 308 disperser,value,npts,width,nsigma = [getattr(self, par+ext) 309 for ext in ('_pd_type','','_pd_n','_pd','_pd_nsigma')] 310 v,w = weights.get_weights( 311 disperser, int(npts.value), width.value, nsigma.value, 312 value.value, limits[par], par in relative) 313 return v,w/w.max() 314 315 284 316 def demo(): 285 317 data = load_data('DEC07086.DAT') … … 288 320 import matplotlib.pyplot as plt; plt.show() 289 321 322 290 323 if __name__ == "__main__": 291 324 demo()
Note: See TracChangeset
for help on using the changeset viewer.