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