1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | |
---|
4 | import sys, os |
---|
5 | import datetime |
---|
6 | import warnings |
---|
7 | |
---|
8 | import numpy as np |
---|
9 | |
---|
10 | |
---|
11 | def opencl_model(kernel_module, dtype="single"): |
---|
12 | from sasmodels import gen, gpu |
---|
13 | |
---|
14 | source, info = gen.make(kernel_module) |
---|
15 | ## for debugging, save source to a .cl file, edit it, and reload as model |
---|
16 | #open(modelname+'.cl','w').write(source) |
---|
17 | #source = open(modelname+'.cl','r').read() |
---|
18 | return gpu.GpuModel(source, info, dtype) |
---|
19 | |
---|
20 | |
---|
21 | if sys.platform == 'darwin': |
---|
22 | COMPILE = "gcc-mp-4.7 -shared -fPIC -std=c99 -fopenmp -O2 -Wall %s -o %s -lm -lgomp" |
---|
23 | elif os.name == 'nt': |
---|
24 | COMPILE = "gcc -shared -fPIC -std=c99 -fopenmp -O2 -Wall %s -o %s -lm" |
---|
25 | else: |
---|
26 | COMPILE = "cc -shared -fPIC -std=c99 -fopenmp -O2 -Wall %s -o %s -lm" |
---|
27 | DLL_PATH = "/tmp" |
---|
28 | |
---|
29 | |
---|
30 | def dll_path(info): |
---|
31 | from os.path import join as joinpath, split as splitpath, splitext |
---|
32 | basename = splitext(splitpath(info['filename'])[1])[0] |
---|
33 | return joinpath(DLL_PATH, basename+'.so') |
---|
34 | |
---|
35 | |
---|
36 | def dll_model(kernel_module): |
---|
37 | import os |
---|
38 | import tempfile |
---|
39 | from sasmodels import gen, dll |
---|
40 | |
---|
41 | source, info = gen.make(kernel_module) |
---|
42 | dllpath = dll_path(info) |
---|
43 | if not os.path.exists(dllpath) \ |
---|
44 | or (os.path.getmtime(dllpath) < os.path.getmtime(info['filename'])): |
---|
45 | # Replace with a proper temp file |
---|
46 | srcfile = tempfile.mkstemp(suffix=".c",prefix="sas_"+info['name']) |
---|
47 | open(srcfile, 'w').write(source) |
---|
48 | os.system(COMPILE%(srcfile, dllpath)) |
---|
49 | ## comment the following to keep the generated c file |
---|
50 | #os.unlink(srcfile) |
---|
51 | return dll.DllModel(dllpath, info) |
---|
52 | |
---|
53 | |
---|
54 | TIC = None |
---|
55 | def tic(): |
---|
56 | global TIC |
---|
57 | then = datetime.datetime.now() |
---|
58 | TIC = lambda: (datetime.datetime.now()-then).total_seconds() |
---|
59 | return TIC |
---|
60 | |
---|
61 | |
---|
62 | def toc(): |
---|
63 | return TIC() |
---|
64 | |
---|
65 | |
---|
66 | def load_data(filename): |
---|
67 | from sans.dataloader.loader import Loader |
---|
68 | loader = Loader() |
---|
69 | data = loader.load(filename) |
---|
70 | if data is None: |
---|
71 | raise IOError("Data %r could not be loaded"%filename) |
---|
72 | return data |
---|
73 | |
---|
74 | |
---|
75 | def empty_data2D(qx, qy=None): |
---|
76 | from sans.dataloader.data_info import Data2D, Detector |
---|
77 | |
---|
78 | if qy is None: |
---|
79 | qy = qx |
---|
80 | Qx,Qy = np.meshgrid(qx,qy) |
---|
81 | Qx,Qy = Qx.flatten(), Qy.flatten() |
---|
82 | Iq = 100*np.ones_like(Qx) |
---|
83 | dIq = np.sqrt(Iq) |
---|
84 | mask = np.ones(len(Iq), dtype='bool') |
---|
85 | |
---|
86 | data = Data2D() |
---|
87 | data.filename = "fake data" |
---|
88 | data.qx_data = Qx |
---|
89 | data.qy_data = Qy |
---|
90 | data.data = Iq |
---|
91 | data.err_data = dIq |
---|
92 | data.mask = mask |
---|
93 | |
---|
94 | # 5% dQ/Q resolution |
---|
95 | data.dqx_data = 0.05*Qx |
---|
96 | data.dqy_data = 0.05*Qy |
---|
97 | |
---|
98 | detector = Detector() |
---|
99 | detector.pixel_size.x = 5 # mm |
---|
100 | detector.pixel_size.y = 5 # mm |
---|
101 | detector.distance = 4 # m |
---|
102 | data.detector.append(detector) |
---|
103 | data.xbins = qx |
---|
104 | data.ybins = qy |
---|
105 | data.source.wavelength = 5 # angstroms |
---|
106 | data.source.wavelength_unit = "A" |
---|
107 | data.Q_unit = "1/A" |
---|
108 | data.I_unit = "1/cm" |
---|
109 | data.q_data = np.sqrt(Qx**2 + Qy**2) |
---|
110 | data.xaxis("Q_x", "A^{-1}") |
---|
111 | data.yaxis("Q_y", "A^{-1}") |
---|
112 | data.zaxis("Intensity", r"\text{cm}^{-1}") |
---|
113 | return data |
---|
114 | |
---|
115 | |
---|
116 | def empty_data1D(q): |
---|
117 | from sans.dataloader.data_info import Data1D |
---|
118 | |
---|
119 | Iq = 100*np.ones_like(q) |
---|
120 | dIq = np.sqrt(Iq) |
---|
121 | data = Data1D(q, Iq, dx=0.05*q, dy=dIq) |
---|
122 | data.filename = "fake data" |
---|
123 | data.qmin, data.qmax = q.min(), q.max() |
---|
124 | return data |
---|
125 | |
---|
126 | |
---|
127 | def set_beam_stop(data, radius, outer=None): |
---|
128 | from sans.dataloader.manipulations import Ringcut |
---|
129 | if hasattr(data, 'qx_data'): |
---|
130 | data.mask = Ringcut(0, radius)(data) |
---|
131 | if outer is not None: |
---|
132 | data.mask += Ringcut(outer,np.inf)(data) |
---|
133 | else: |
---|
134 | data.mask = (data.x>=radius) |
---|
135 | if outer is not None: |
---|
136 | data.mask &= (data.x<outer) |
---|
137 | |
---|
138 | |
---|
139 | def set_half(data, half): |
---|
140 | from sans.dataloader.manipulations import Boxcut |
---|
141 | if half == 'right': |
---|
142 | data.mask += Boxcut(x_min=-np.inf, x_max=0.0, y_min=-np.inf, y_max=np.inf)(data) |
---|
143 | if half == 'left': |
---|
144 | data.mask += Boxcut(x_min=0.0, x_max=np.inf, y_min=-np.inf, y_max=np.inf)(data) |
---|
145 | |
---|
146 | |
---|
147 | def set_top(data, max): |
---|
148 | from sans.dataloader.manipulations import Boxcut |
---|
149 | data.mask += Boxcut(x_min=-np.inf, x_max=np.inf, y_min=-np.inf, y_max=max)(data) |
---|
150 | |
---|
151 | |
---|
152 | def plot_data(data, iq, vmin=None, vmax=None, scale='log'): |
---|
153 | from numpy.ma import masked_array, masked |
---|
154 | import matplotlib.pyplot as plt |
---|
155 | if hasattr(data, 'qx_data'): |
---|
156 | img = masked_array(iq, data.mask) |
---|
157 | if scale == 'log': |
---|
158 | img[(img <= 0) | ~np.isfinite(img)] = masked |
---|
159 | img = np.log10(img) |
---|
160 | xmin, xmax = min(data.qx_data), max(data.qx_data) |
---|
161 | ymin, ymax = min(data.qy_data), max(data.qy_data) |
---|
162 | plt.imshow(img.reshape(128,128), |
---|
163 | interpolation='nearest', aspect=1, origin='upper', |
---|
164 | extent=[xmin, xmax, ymin, ymax], vmin=vmin, vmax=vmax) |
---|
165 | else: # 1D data |
---|
166 | if scale == 'linear': |
---|
167 | idx = np.isfinite(iq) |
---|
168 | plt.plot(data.x[idx], iq[idx]) |
---|
169 | else: |
---|
170 | idx = np.isfinite(iq) & (iq>0) |
---|
171 | plt.loglog(data.x[idx], iq[idx]) |
---|
172 | |
---|
173 | |
---|
174 | def plot_result2D(data, theory, view='log'): |
---|
175 | import matplotlib.pyplot as plt |
---|
176 | resid = (theory-data.data)/data.err_data |
---|
177 | plt.subplot(131) |
---|
178 | plot_data(data, data.data, scale=view) |
---|
179 | plt.colorbar() |
---|
180 | plt.subplot(132) |
---|
181 | plot_data(data, theory, scale=view) |
---|
182 | plt.colorbar() |
---|
183 | plt.subplot(133) |
---|
184 | plot_data(data, resid, scale='linear') |
---|
185 | plt.colorbar() |
---|
186 | |
---|
187 | |
---|
188 | def plot_result1D(data, theory, view='log'): |
---|
189 | import matplotlib.pyplot as plt |
---|
190 | from numpy.ma import masked_array, masked |
---|
191 | #print "not a number",sum(np.isnan(data.y)) |
---|
192 | #data.y[data.y<0.05] = 0.5 |
---|
193 | mdata = masked_array(data.y, data.mask) |
---|
194 | mdata[np.isnan(mdata)] = masked |
---|
195 | if view is 'log': |
---|
196 | mdata[mdata <= 0] = masked |
---|
197 | mtheory = masked_array(theory, mdata.mask) |
---|
198 | mresid = masked_array((theory-data.y)/data.dy, mdata.mask) |
---|
199 | |
---|
200 | plt.subplot(121) |
---|
201 | plt.errorbar(data.x, mdata, yerr=data.dy) |
---|
202 | plt.plot(data.x, mtheory, '-', hold=True) |
---|
203 | plt.yscale(view) |
---|
204 | plt.subplot(122) |
---|
205 | plt.plot(data.x, mresid, 'x') |
---|
206 | #plt.axhline(1, color='black', ls='--',lw=1, hold=True) |
---|
207 | #plt.axhline(0, color='black', lw=1, hold=True) |
---|
208 | #plt.axhline(-1, color='black', ls='--',lw=1, hold=True) |
---|
209 | |
---|
210 | |
---|
211 | class BumpsModel(object): |
---|
212 | def __init__(self, data, model, cutoff=1e-5, **kw): |
---|
213 | from bumps.names import Parameter |
---|
214 | from . import gpu |
---|
215 | |
---|
216 | # interpret data |
---|
217 | self.is2D = hasattr(data,'qx_data') |
---|
218 | self.data = data |
---|
219 | if self.is2D: |
---|
220 | self.index = (data.mask==0) & (~np.isnan(data.data)) |
---|
221 | self.iq = data.data[self.index] |
---|
222 | self.diq = data.err_data[self.index] |
---|
223 | self._theory = np.zeros_like(data.data) |
---|
224 | q_vectors = [data.qx_data, data.qy_data] |
---|
225 | else: |
---|
226 | self.index = (data.x>=data.qmin) & (data.x<=data.qmax) & ~np.isnan(data.y) |
---|
227 | self.iq = data.y[self.index] |
---|
228 | self.diq = data.dy[self.index] |
---|
229 | self._theory = np.zeros_like(data.y) |
---|
230 | q_vectors = [data.x] |
---|
231 | #input = model.make_input(q_vectors) |
---|
232 | input = model.make_input([v[self.index] for v in q_vectors]) |
---|
233 | |
---|
234 | # create model |
---|
235 | self.fn = model(input) |
---|
236 | self.cutoff = cutoff |
---|
237 | |
---|
238 | # define bumps parameters |
---|
239 | pars = [] |
---|
240 | extras = [] |
---|
241 | for p in model.info['parameters']: |
---|
242 | name, default, limits, ptype = p[0], p[2], p[3], p[4] |
---|
243 | value = kw.pop(name, default) |
---|
244 | setattr(self, name, Parameter.default(value, name=name, limits=limits)) |
---|
245 | pars.append(name) |
---|
246 | for name in model.info['partype']['pd-2d']: |
---|
247 | for xpart,xdefault,xlimits in [ |
---|
248 | ('_pd', 0, limits), |
---|
249 | ('_pd_n', 35, (0,1000)), |
---|
250 | ('_pd_nsigma', 3, (0, 10)), |
---|
251 | ('_pd_type', 'gaussian', None), |
---|
252 | ]: |
---|
253 | xname = name+xpart |
---|
254 | xvalue = kw.pop(xname, xdefault) |
---|
255 | if xlimits is not None: |
---|
256 | xvalue = Parameter.default(xvalue, name=xname, limits=xlimits) |
---|
257 | pars.append(xname) |
---|
258 | setattr(self, xname, xvalue) |
---|
259 | self._parameter_names = pars |
---|
260 | if kw: |
---|
261 | raise TypeError("unexpected parameters: %s"%(", ".join(sorted(kw.keys())))) |
---|
262 | self.update() |
---|
263 | |
---|
264 | def update(self): |
---|
265 | self._cache = {} |
---|
266 | |
---|
267 | def numpoints(self): |
---|
268 | return len(self.iq) |
---|
269 | |
---|
270 | def parameters(self): |
---|
271 | return dict((k,getattr(self,k)) for k in self._parameter_names) |
---|
272 | |
---|
273 | def theory(self): |
---|
274 | if 'theory' not in self._cache: |
---|
275 | pars = [getattr(self,p).value for p in self.fn.fixed_pars] |
---|
276 | pd_pars = [self._get_weights(p) for p in self.fn.pd_pars] |
---|
277 | #print pars |
---|
278 | self._theory[self.index] = self.fn(pars, pd_pars, self.cutoff) |
---|
279 | #self._theory[:] = self.fn.eval(pars, pd_pars) |
---|
280 | self._cache['theory'] = self._theory |
---|
281 | return self._cache['theory'] |
---|
282 | |
---|
283 | def residuals(self): |
---|
284 | #if np.any(self.err ==0): print "zeros in err" |
---|
285 | return (self.theory()[self.index]-self.iq)/self.diq |
---|
286 | |
---|
287 | def nllf(self): |
---|
288 | R = self.residuals() |
---|
289 | #if np.any(np.isnan(R)): print "NaN in residuals" |
---|
290 | return 0.5*np.sum(R**2) |
---|
291 | |
---|
292 | def __call__(self): |
---|
293 | return 2*self.nllf()/self.dof |
---|
294 | |
---|
295 | def plot(self, view='log'): |
---|
296 | if self.is2D: |
---|
297 | plot_result2D(self.data, self.theory(), view=view) |
---|
298 | else: |
---|
299 | plot_result1D(self.data, self.theory(), view=view) |
---|
300 | |
---|
301 | def save(self, basename): |
---|
302 | pass |
---|
303 | |
---|
304 | def _get_weights(self, par): |
---|
305 | from . import weights |
---|
306 | |
---|
307 | relative = self.fn.info['partype']['pd-rel'] |
---|
308 | limits = self.fn.info['limits'] |
---|
309 | disperser,value,npts,width,nsigma = [getattr(self, par+ext) |
---|
310 | for ext in ('_pd_type','','_pd_n','_pd','_pd_nsigma')] |
---|
311 | v,w = weights.get_weights( |
---|
312 | disperser, int(npts.value), width.value, nsigma.value, |
---|
313 | value.value, limits[par], par in relative) |
---|
314 | return v,w/w.max() |
---|
315 | |
---|
316 | |
---|
317 | def demo(): |
---|
318 | data = load_data('DEC07086.DAT') |
---|
319 | set_beam_stop(data, 0.004) |
---|
320 | plot_data(data) |
---|
321 | import matplotlib.pyplot as plt; plt.show() |
---|
322 | |
---|
323 | |
---|
324 | if __name__ == "__main__": |
---|
325 | demo() |
---|