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