1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | |
---|
4 | import sys |
---|
5 | import numpy as np |
---|
6 | import pyopencl as cl |
---|
7 | from bumps.names import Parameter |
---|
8 | from sans.dataloader.loader import Loader |
---|
9 | from sans.dataloader.manipulations import Ringcut, Boxcut |
---|
10 | |
---|
11 | |
---|
12 | def load_data(filename): |
---|
13 | loader = Loader() |
---|
14 | data = loader.load(filename) |
---|
15 | if data is None: |
---|
16 | raise IOError("Data %r could not be loaded"%filename) |
---|
17 | return data |
---|
18 | |
---|
19 | |
---|
20 | def set_beam_stop(data, radius, outer=None): |
---|
21 | if hasattr(data, 'qx_data'): |
---|
22 | data.mask = Ringcut(0, radius)(data) |
---|
23 | if outer is not None: |
---|
24 | data.mask += Ringcut(outer,np.inf)(data) |
---|
25 | else: |
---|
26 | data.mask = (data.x>=radius) |
---|
27 | if outer is not None: |
---|
28 | data.mask &= (data.x<outer) |
---|
29 | |
---|
30 | def set_half(data, half): |
---|
31 | if half == 'left': |
---|
32 | data.mask += Boxcut(x_min=-np.inf, x_max=0.0, y_min=-np.inf, y_max=np.inf)(data) |
---|
33 | if half == 'right': |
---|
34 | data.mask += Boxcut(x_min=0.0, x_max=np.inf, y_min=-np.inf, y_max=np.inf)(data) |
---|
35 | |
---|
36 | |
---|
37 | |
---|
38 | def plot_data(data, iq, vmin=None, vmax=None): |
---|
39 | from numpy.ma import masked_array |
---|
40 | import matplotlib.pyplot as plt |
---|
41 | img = masked_array(iq, data.mask) |
---|
42 | xmin, xmax = min(data.qx_data), max(data.qx_data) |
---|
43 | ymin, ymax = min(data.qy_data), max(data.qy_data) |
---|
44 | plt.imshow(img.reshape(128,128), |
---|
45 | interpolation='nearest', aspect=1, origin='upper', |
---|
46 | extent=[xmin, xmax, ymin, ymax], vmin=vmin, vmax=vmax) |
---|
47 | |
---|
48 | def plot_result2D(data, theory, view='linear'): |
---|
49 | import matplotlib.pyplot as plt |
---|
50 | from numpy.ma import masked_array, masked |
---|
51 | #print "not a number",sum(np.isnan(data.data)) |
---|
52 | #data.data[data.data<0.05] = 0.5 |
---|
53 | mdata = masked_array(data.data, data.mask) |
---|
54 | mdata[np.isnan(mdata)] = masked |
---|
55 | if view is 'log': |
---|
56 | mdata[mdata <= 0] = masked |
---|
57 | mdata = np.log10(mdata) |
---|
58 | mtheory = masked_array(np.log10(theory), mdata.mask) |
---|
59 | else: |
---|
60 | mtheory = masked_array(theory, mdata.mask) |
---|
61 | mresid = masked_array((theory-data.data)/data.err_data, data.mask) |
---|
62 | vmin = min(mdata.min(), mtheory.min()) |
---|
63 | vmax = max(mdata.max(), mtheory.max()) |
---|
64 | |
---|
65 | plt.subplot(1, 3, 1) |
---|
66 | plot_data(data, mdata, vmin=vmin, vmax=vmax) |
---|
67 | plt.colorbar() |
---|
68 | plt.subplot(1, 3, 2) |
---|
69 | plot_data(data, mtheory, vmin=vmin, vmax=vmax) |
---|
70 | plt.colorbar() |
---|
71 | plt.subplot(1, 3, 3) |
---|
72 | plot_data(data, mresid) |
---|
73 | plt.colorbar() |
---|
74 | |
---|
75 | |
---|
76 | def plot_result1D(data, theory, view='linear'): |
---|
77 | import matplotlib.pyplot as plt |
---|
78 | from numpy.ma import masked_array, masked |
---|
79 | #print "not a number",sum(np.isnan(data.y)) |
---|
80 | #data.y[data.y<0.05] = 0.5 |
---|
81 | mdata = masked_array(data.y, data.mask) |
---|
82 | mdata[np.isnan(mdata)] = masked |
---|
83 | if view is 'log': |
---|
84 | mdata[mdata <= 0] = masked |
---|
85 | mtheory = masked_array(theory, mdata.mask) |
---|
86 | mresid = masked_array((theory-data.y)/data.dy, mdata.mask) |
---|
87 | |
---|
88 | plt.subplot(1,2,1) |
---|
89 | plt.errorbar(data.x, mdata, yerr=data.dy) |
---|
90 | plt.plot(data.x, mtheory, '-', hold=True) |
---|
91 | plt.yscale(view) |
---|
92 | plt.subplot(1, 2, 2) |
---|
93 | plt.plot(data.x, mresid, 'x') |
---|
94 | #plt.axhline(1, color='black', ls='--',lw=1, hold=True) |
---|
95 | #plt.axhline(0, color='black', lw=1, hold=True) |
---|
96 | #plt.axhline(-1, color='black', ls='--',lw=1, hold=True) |
---|
97 | |
---|
98 | |
---|
99 | |
---|
100 | GPU_CONTEXT = None |
---|
101 | GPU_QUEUE = None |
---|
102 | def card(): |
---|
103 | global GPU_CONTEXT, GPU_QUEUE |
---|
104 | if GPU_CONTEXT is None: |
---|
105 | GPU_CONTEXT = cl.create_some_context() |
---|
106 | GPU_QUEUE = cl.CommandQueue(GPU_CONTEXT) |
---|
107 | return GPU_CONTEXT, GPU_QUEUE |
---|
108 | |
---|
109 | |
---|
110 | class SasModel(object): |
---|
111 | def __init__(self, data, model, dtype='float32', **kw): |
---|
112 | self.__dict__['_parameters'] = {} |
---|
113 | self.is2D = hasattr(data,'qx_data') |
---|
114 | self.data = data |
---|
115 | if self.is2D: |
---|
116 | self.index = (data.mask==0) & (~np.isnan(data.data)) |
---|
117 | self.iq = data.data[self.index] |
---|
118 | self.diq = data.err_data[self.index] |
---|
119 | self.qx = data.qx_data |
---|
120 | self.qy = data.qy_data |
---|
121 | self.gpu = model(self.qx, self.qy, dtype=dtype) |
---|
122 | else: |
---|
123 | self.index = (data.mask==0) & (~np.isnan(data.y)) |
---|
124 | self.iq = data.y[self.index] |
---|
125 | self.diq = data.dy[self.index] |
---|
126 | self.q = data.x |
---|
127 | self.gpu = model(self.q, dtype=dtype) |
---|
128 | pd_pars = set(base+attr for base in model.PD_PARS for attr in ('_pd','_pd_n','_pd_nsigma')) |
---|
129 | total_pars = set(model.PARS.keys()) | pd_pars |
---|
130 | extra_pars = set(kw.keys()) - total_pars |
---|
131 | if extra_pars: |
---|
132 | raise TypeError("unexpected parameters %s"%(str(extra_pars,))) |
---|
133 | pars = model.PARS.copy() |
---|
134 | pars.update((base+'_pd', 0) for base in model.PD_PARS) |
---|
135 | pars.update((base+'_pd_n', 35) for base in model.PD_PARS) |
---|
136 | pars.update((base+'_pd_nsigma', 3) for base in model.PD_PARS) |
---|
137 | pars.update(kw) |
---|
138 | self._parameters = dict((k, Parameter.default(v, name=k)) for k, v in pars.items()) |
---|
139 | |
---|
140 | def numpoints(self): |
---|
141 | return len(self.iq) |
---|
142 | |
---|
143 | def parameters(self): |
---|
144 | return self._parameters |
---|
145 | |
---|
146 | def __getattr__(self, par): |
---|
147 | return self._parameters[par] |
---|
148 | |
---|
149 | def __setattr__(self, par, val): |
---|
150 | if par in self._parameters: |
---|
151 | self._parameters[par] = val |
---|
152 | else: |
---|
153 | self.__dict__[par] = val |
---|
154 | |
---|
155 | def theory(self): |
---|
156 | pars = dict((k,v.value) for k,v in self._parameters.items()) |
---|
157 | result = self.gpu.eval(pars) |
---|
158 | return result |
---|
159 | |
---|
160 | def residuals(self): |
---|
161 | #if np.any(self.err ==0): print "zeros in err" |
---|
162 | return (self.theory()[self.index]-self.iq)/self.diq |
---|
163 | |
---|
164 | def nllf(self): |
---|
165 | R = self.residuals() |
---|
166 | #if np.any(np.isnan(R)): print "NaN in residuals" |
---|
167 | return 0.5*np.sum(R**2) |
---|
168 | |
---|
169 | def __call__(self): |
---|
170 | return 2*self.nllf()/self.dof |
---|
171 | |
---|
172 | def plot(self, view='log'): |
---|
173 | if self.is2D: |
---|
174 | plot_result2D(self.data, self.theory(), view=view) |
---|
175 | else: |
---|
176 | plot_result1D(self.data, self.theory(), view=view) |
---|
177 | |
---|
178 | def save(self, basename): |
---|
179 | pass |
---|
180 | |
---|
181 | def update(self): |
---|
182 | pass |
---|
183 | |
---|
184 | |
---|
185 | def demo(): |
---|
186 | data = load_data('DEC07086.DAT') |
---|
187 | set_beam_stop(data, 0.004) |
---|
188 | plot_data(data) |
---|
189 | import matplotlib.pyplot as plt; plt.show() |
---|
190 | |
---|
191 | if __name__ == "__main__": |
---|
192 | demo() |
---|