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