[ff7119b] | 1 | """ |
---|
| 2 | Sasmodels core. |
---|
| 3 | """ |
---|
[14de349] | 4 | import datetime |
---|
| 5 | |
---|
[c97724e] | 6 | from sasmodels import sesans |
---|
| 7 | |
---|
[31819c5] | 8 | # CRUFT python 2.6 |
---|
| 9 | if not hasattr(datetime.timedelta, 'total_seconds'): |
---|
[7e224c2] | 10 | def delay(dt): return dt.days * 86400 + dt.seconds + 1e-6 * dt.microseconds |
---|
[31819c5] | 11 | else: |
---|
| 12 | def delay(dt): return dt.total_seconds() |
---|
| 13 | |
---|
[14de349] | 14 | import numpy as np |
---|
| 15 | |
---|
[87985ca] | 16 | try: |
---|
[f786ff3] | 17 | from .kernelcl import load_model as _loader |
---|
[7e224c2] | 18 | except RuntimeError, exc: |
---|
[78356b31] | 19 | import warnings |
---|
[87985ca] | 20 | warnings.warn(str(exc)) |
---|
| 21 | warnings.warn("OpenCL not available --- using ctypes instead") |
---|
[f786ff3] | 22 | from .kerneldll import load_model as _loader |
---|
[87985ca] | 23 | |
---|
| 24 | def load_model(modelname, dtype='single'): |
---|
| 25 | """ |
---|
| 26 | Load model by name. |
---|
| 27 | """ |
---|
[7e224c2] | 28 | sasmodels = __import__('sasmodels.models.' + modelname) |
---|
[87985ca] | 29 | module = getattr(sasmodels.models, modelname, None) |
---|
| 30 | model = _loader(module, dtype=dtype) |
---|
| 31 | return model |
---|
| 32 | |
---|
[14de349] | 33 | |
---|
| 34 | def tic(): |
---|
[ff7119b] | 35 | """ |
---|
| 36 | Timer function. |
---|
[ce27e21] | 37 | |
---|
[ff7119b] | 38 | Use "toc=tic()" to start the clock and "toc()" to measure |
---|
| 39 | a time interval. |
---|
| 40 | """ |
---|
| 41 | then = datetime.datetime.now() |
---|
[7e224c2] | 42 | return lambda: delay(datetime.datetime.now() - then) |
---|
[14de349] | 43 | |
---|
[ce27e21] | 44 | |
---|
[14de349] | 45 | def load_data(filename): |
---|
[ff7119b] | 46 | """ |
---|
| 47 | Load data using a sasview loader. |
---|
| 48 | """ |
---|
[87c722e] | 49 | from sas.dataloader.loader import Loader |
---|
[14de349] | 50 | loader = Loader() |
---|
| 51 | data = loader.load(filename) |
---|
| 52 | if data is None: |
---|
[7e224c2] | 53 | raise IOError("Data %r could not be loaded" % filename) |
---|
[14de349] | 54 | return data |
---|
| 55 | |
---|
[ce27e21] | 56 | |
---|
[ff7119b] | 57 | def empty_data1D(q): |
---|
| 58 | """ |
---|
| 59 | Create empty 1D data using the given *q* as the x value. |
---|
| 60 | |
---|
| 61 | Resolutions dq/q is 5%. |
---|
| 62 | """ |
---|
| 63 | |
---|
[87c722e] | 64 | from sas.dataloader.data_info import Data1D |
---|
[ff7119b] | 65 | |
---|
[7e224c2] | 66 | Iq = 100 * np.ones_like(q) |
---|
[ff7119b] | 67 | dIq = np.sqrt(Iq) |
---|
[7e224c2] | 68 | data = Data1D(q, Iq, dx=0.05 * q, dy=dIq) |
---|
[ff7119b] | 69 | data.filename = "fake data" |
---|
| 70 | data.qmin, data.qmax = q.min(), q.max() |
---|
| 71 | return data |
---|
| 72 | |
---|
| 73 | |
---|
[13d86bc] | 74 | def empty_data2D(qx, qy=None): |
---|
[ff7119b] | 75 | """ |
---|
| 76 | Create empty 2D data using the given mesh. |
---|
| 77 | |
---|
| 78 | If *qy* is missing, create a square mesh with *qy=qx*. |
---|
| 79 | |
---|
| 80 | Resolution dq/q is 5%. |
---|
| 81 | """ |
---|
[87c722e] | 82 | from sas.dataloader.data_info import Data2D, Detector |
---|
[14de349] | 83 | |
---|
| 84 | if qy is None: |
---|
| 85 | qy = qx |
---|
[7e224c2] | 86 | Qx, Qy = np.meshgrid(qx, qy) |
---|
| 87 | Qx, Qy = Qx.flatten(), Qy.flatten() |
---|
| 88 | Iq = 100 * np.ones_like(Qx) |
---|
[14de349] | 89 | dIq = np.sqrt(Iq) |
---|
| 90 | mask = np.ones(len(Iq), dtype='bool') |
---|
| 91 | |
---|
| 92 | data = Data2D() |
---|
| 93 | data.filename = "fake data" |
---|
| 94 | data.qx_data = Qx |
---|
| 95 | data.qy_data = Qy |
---|
| 96 | data.data = Iq |
---|
| 97 | data.err_data = dIq |
---|
| 98 | data.mask = mask |
---|
| 99 | |
---|
| 100 | # 5% dQ/Q resolution |
---|
[7e224c2] | 101 | data.dqx_data = 0.05 * Qx |
---|
| 102 | data.dqy_data = 0.05 * Qy |
---|
[14de349] | 103 | |
---|
| 104 | detector = Detector() |
---|
| 105 | detector.pixel_size.x = 5 # mm |
---|
| 106 | detector.pixel_size.y = 5 # mm |
---|
| 107 | detector.distance = 4 # m |
---|
| 108 | data.detector.append(detector) |
---|
| 109 | data.xbins = qx |
---|
| 110 | data.ybins = qy |
---|
| 111 | data.source.wavelength = 5 # angstroms |
---|
| 112 | data.source.wavelength_unit = "A" |
---|
| 113 | data.Q_unit = "1/A" |
---|
| 114 | data.I_unit = "1/cm" |
---|
[7e224c2] | 115 | data.q_data = np.sqrt(Qx ** 2 + Qy ** 2) |
---|
[14de349] | 116 | data.xaxis("Q_x", "A^{-1}") |
---|
| 117 | data.yaxis("Q_y", "A^{-1}") |
---|
| 118 | data.zaxis("Intensity", r"\text{cm}^{-1}") |
---|
| 119 | return data |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | def set_beam_stop(data, radius, outer=None): |
---|
[ff7119b] | 123 | """ |
---|
| 124 | Add a beam stop of the given *radius*. If *outer*, make an annulus. |
---|
| 125 | """ |
---|
[87c722e] | 126 | from sas.dataloader.manipulations import Ringcut |
---|
[14de349] | 127 | if hasattr(data, 'qx_data'): |
---|
| 128 | data.mask = Ringcut(0, radius)(data) |
---|
| 129 | if outer is not None: |
---|
[7e224c2] | 130 | data.mask += Ringcut(outer, np.inf)(data) |
---|
[14de349] | 131 | else: |
---|
[7e224c2] | 132 | data.mask = (data.x >= radius) |
---|
[14de349] | 133 | if outer is not None: |
---|
[7e224c2] | 134 | data.mask &= (data.x < outer) |
---|
[14de349] | 135 | |
---|
[ce27e21] | 136 | |
---|
[14de349] | 137 | def set_half(data, half): |
---|
[ff7119b] | 138 | """ |
---|
| 139 | Select half of the data, either "right" or "left". |
---|
| 140 | """ |
---|
[87c722e] | 141 | from sas.dataloader.manipulations import Boxcut |
---|
[14de349] | 142 | if half == 'right': |
---|
| 143 | data.mask += Boxcut(x_min=-np.inf, x_max=0.0, y_min=-np.inf, y_max=np.inf)(data) |
---|
| 144 | if half == 'left': |
---|
| 145 | data.mask += Boxcut(x_min=0.0, x_max=np.inf, y_min=-np.inf, y_max=np.inf)(data) |
---|
| 146 | |
---|
[ce27e21] | 147 | |
---|
[14de349] | 148 | def set_top(data, max): |
---|
[ff7119b] | 149 | """ |
---|
| 150 | Chop the top off the data, above *max*. |
---|
| 151 | """ |
---|
[87c722e] | 152 | from sas.dataloader.manipulations import Boxcut |
---|
[14de349] | 153 | data.mask += Boxcut(x_min=-np.inf, x_max=np.inf, y_min=-np.inf, y_max=max)(data) |
---|
| 154 | |
---|
[ce27e21] | 155 | |
---|
[13d86bc] | 156 | def plot_data(data, iq, vmin=None, vmax=None, scale='log'): |
---|
[ff7119b] | 157 | """ |
---|
| 158 | Plot the target value for the data. This could be the data itself, |
---|
| 159 | the theory calculation, or the residuals. |
---|
| 160 | |
---|
| 161 | *scale* can be 'log' for log scale data, or 'linear'. |
---|
| 162 | """ |
---|
[13d86bc] | 163 | from numpy.ma import masked_array, masked |
---|
[14de349] | 164 | import matplotlib.pyplot as plt |
---|
[13d86bc] | 165 | if hasattr(data, 'qx_data'): |
---|
[7e224c2] | 166 | iq = iq + 0 |
---|
[5d4777d] | 167 | valid = np.isfinite(iq) |
---|
[13d86bc] | 168 | if scale == 'log': |
---|
[5d4777d] | 169 | valid[valid] = (iq[valid] > 0) |
---|
| 170 | iq[valid] = np.log10(iq[valid]) |
---|
[7e224c2] | 171 | iq[~valid | data.mask] = 0 |
---|
[5d4777d] | 172 | #plottable = iq |
---|
[7e224c2] | 173 | plottable = masked_array(iq, ~valid | data.mask) |
---|
[13d86bc] | 174 | xmin, xmax = min(data.qx_data), max(data.qx_data) |
---|
| 175 | ymin, ymax = min(data.qy_data), max(data.qy_data) |
---|
[5134b2c] | 176 | try: |
---|
[7e224c2] | 177 | if vmin is None: vmin = iq[valid & ~data.mask].min() |
---|
| 178 | if vmax is None: vmax = iq[valid & ~data.mask].max() |
---|
[5134b2c] | 179 | except: |
---|
| 180 | vmin, vmax = 0, 1 |
---|
[7e224c2] | 181 | plt.imshow(plottable.reshape(128, 128), |
---|
[13d86bc] | 182 | interpolation='nearest', aspect=1, origin='upper', |
---|
| 183 | extent=[xmin, xmax, ymin, ymax], vmin=vmin, vmax=vmax) |
---|
| 184 | else: # 1D data |
---|
| 185 | if scale == 'linear': |
---|
| 186 | idx = np.isfinite(iq) |
---|
| 187 | plt.plot(data.x[idx], iq[idx]) |
---|
| 188 | else: |
---|
[5d4777d] | 189 | idx = np.isfinite(iq) |
---|
[7e224c2] | 190 | idx[idx] = (iq[idx] > 0) |
---|
[13d86bc] | 191 | plt.loglog(data.x[idx], iq[idx]) |
---|
[14de349] | 192 | |
---|
[ce27e21] | 193 | |
---|
[ff7119b] | 194 | def _plot_result1D(data, theory, view): |
---|
| 195 | """ |
---|
| 196 | Plot the data and residuals for 1D data. |
---|
| 197 | """ |
---|
[14de349] | 198 | import matplotlib.pyplot as plt |
---|
| 199 | from numpy.ma import masked_array, masked |
---|
| 200 | #print "not a number",sum(np.isnan(data.y)) |
---|
| 201 | #data.y[data.y<0.05] = 0.5 |
---|
| 202 | mdata = masked_array(data.y, data.mask) |
---|
| 203 | mdata[np.isnan(mdata)] = masked |
---|
| 204 | if view is 'log': |
---|
| 205 | mdata[mdata <= 0] = masked |
---|
| 206 | mtheory = masked_array(theory, mdata.mask) |
---|
[7e224c2] | 207 | mresid = masked_array((theory - data.y) / data.dy, mdata.mask) |
---|
[14de349] | 208 | |
---|
[ce27e21] | 209 | plt.subplot(121) |
---|
[14de349] | 210 | plt.errorbar(data.x, mdata, yerr=data.dy) |
---|
| 211 | plt.plot(data.x, mtheory, '-', hold=True) |
---|
| 212 | plt.yscale(view) |
---|
[ce27e21] | 213 | plt.subplot(122) |
---|
[14de349] | 214 | plt.plot(data.x, mresid, 'x') |
---|
| 215 | |
---|
[c97724e] | 216 | def _plot_sesans(data, theory, view): |
---|
| 217 | import matplotlib.pyplot as plt |
---|
[7e224c2] | 218 | resid = (theory - data.y) / data.dy |
---|
[c97724e] | 219 | plt.subplot(121) |
---|
[0ac3db5] | 220 | plt.errorbar(data.x, data.y, yerr=data.dy) |
---|
| 221 | plt.plot(data.x, theory, '-', hold=True) |
---|
[c97724e] | 222 | plt.xlabel('spin echo length (A)') |
---|
| 223 | plt.ylabel('polarization') |
---|
| 224 | plt.subplot(122) |
---|
[0ac3db5] | 225 | plt.plot(data.x, resid, 'x') |
---|
[c97724e] | 226 | plt.xlabel('spin echo length (A)') |
---|
| 227 | plt.ylabel('residuals') |
---|
[14de349] | 228 | |
---|
[ff7119b] | 229 | def _plot_result2D(data, theory, view): |
---|
| 230 | """ |
---|
| 231 | Plot the data and residuals for 2D data. |
---|
| 232 | """ |
---|
| 233 | import matplotlib.pyplot as plt |
---|
[7e224c2] | 234 | resid = (theory - data.data) / data.err_data |
---|
[ff7119b] | 235 | plt.subplot(131) |
---|
| 236 | plot_data(data, data.data, scale=view) |
---|
| 237 | plt.colorbar() |
---|
| 238 | plt.subplot(132) |
---|
| 239 | plot_data(data, theory, scale=view) |
---|
| 240 | plt.colorbar() |
---|
| 241 | plt.subplot(133) |
---|
| 242 | plot_data(data, resid, scale='linear') |
---|
| 243 | plt.colorbar() |
---|
| 244 | |
---|
[14de349] | 245 | class BumpsModel(object): |
---|
[ff7119b] | 246 | """ |
---|
| 247 | Return a bumps wrapper for a SAS model. |
---|
| 248 | |
---|
| 249 | *data* is the data to be fitted. |
---|
| 250 | |
---|
| 251 | *model* is the SAS model, e.g., from :func:`gen.opencl_model`. |
---|
| 252 | |
---|
| 253 | *cutoff* is the integration cutoff, which avoids computing the |
---|
| 254 | the SAS model where the polydispersity weight is low. |
---|
| 255 | |
---|
| 256 | Model parameters can be initialized with additional keyword |
---|
| 257 | arguments, or by assigning to model.parameter_name.value. |
---|
| 258 | |
---|
| 259 | The resulting bumps model can be used directly in a FitProblem call. |
---|
| 260 | """ |
---|
[14de349] | 261 | def __init__(self, data, model, cutoff=1e-5, **kw): |
---|
| 262 | from bumps.names import Parameter |
---|
| 263 | |
---|
[87985ca] | 264 | # remember inputs so we can inspect from outside |
---|
[14de349] | 265 | self.data = data |
---|
[87985ca] | 266 | self.model = model |
---|
[abb22f4] | 267 | self.cutoff = cutoff |
---|
[7e224c2] | 268 | # TODO if isinstance(data,SESANSData1D) |
---|
[0ac3db5] | 269 | if hasattr(data, 'lam'): |
---|
[c97724e] | 270 | self.data_type = 'sesans' |
---|
| 271 | elif hasattr(data, 'qx_data'): |
---|
| 272 | self.data_type = 'Iqxy' |
---|
| 273 | else: |
---|
| 274 | self.data_type = 'Iq' |
---|
[abb22f4] | 275 | |
---|
| 276 | partype = model.info['partype'] |
---|
[87985ca] | 277 | |
---|
| 278 | # interpret data |
---|
[c97724e] | 279 | if self.data_type == 'sesans': |
---|
[0ac3db5] | 280 | q = sesans.make_q(data.sample.zacceptance, data.Rmax) |
---|
[7e224c2] | 281 | self.index = slice(None, None) |
---|
[0ac3db5] | 282 | self.iq = data.y |
---|
| 283 | self.diq = data.dy |
---|
[c97724e] | 284 | self._theory = np.zeros_like(q) |
---|
| 285 | q_vectors = [q] |
---|
| 286 | elif self.data_type == 'Iqxy': |
---|
[7e224c2] | 287 | self.index = (data.mask == 0) & (~np.isnan(data.data)) |
---|
[14de349] | 288 | self.iq = data.data[self.index] |
---|
| 289 | self.diq = data.err_data[self.index] |
---|
| 290 | self._theory = np.zeros_like(data.data) |
---|
[5d4777d] | 291 | if not partype['orientation'] and not partype['magnetic']: |
---|
[7e224c2] | 292 | q_vectors = [np.sqrt(data.qx_data ** 2 + data.qy_data ** 2)] |
---|
[5d4777d] | 293 | else: |
---|
| 294 | q_vectors = [data.qx_data, data.qy_data] |
---|
[c97724e] | 295 | elif self.data_type == 'Iq': |
---|
[7e224c2] | 296 | self.index = (data.x >= data.qmin) & (data.x <= data.qmax) & ~np.isnan(data.y) |
---|
[14de349] | 297 | self.iq = data.y[self.index] |
---|
| 298 | self.diq = data.dy[self.index] |
---|
| 299 | self._theory = np.zeros_like(data.y) |
---|
| 300 | q_vectors = [data.x] |
---|
[c97724e] | 301 | else: |
---|
| 302 | raise ValueError("Unknown data type") # never gets here |
---|
[14de349] | 303 | |
---|
[abb22f4] | 304 | # Remember function inputs so we can delay loading the function and |
---|
| 305 | # so we can save/restore state |
---|
| 306 | self._fn_inputs = [v[self.index] for v in q_vectors] |
---|
| 307 | self._fn = None |
---|
[14de349] | 308 | |
---|
| 309 | # define bumps parameters |
---|
| 310 | pars = [] |
---|
[ce27e21] | 311 | for p in model.info['parameters']: |
---|
[14de349] | 312 | name, default, limits, ptype = p[0], p[2], p[3], p[4] |
---|
| 313 | value = kw.pop(name, default) |
---|
| 314 | setattr(self, name, Parameter.default(value, name=name, limits=limits)) |
---|
| 315 | pars.append(name) |
---|
[5d4777d] | 316 | for name in partype['pd-2d']: |
---|
[7e224c2] | 317 | for xpart, xdefault, xlimits in [ |
---|
[ce27e21] | 318 | ('_pd', 0, limits), |
---|
[7e224c2] | 319 | ('_pd_n', 35, (0, 1000)), |
---|
[ce27e21] | 320 | ('_pd_nsigma', 3, (0, 10)), |
---|
| 321 | ('_pd_type', 'gaussian', None), |
---|
| 322 | ]: |
---|
[7e224c2] | 323 | xname = name + xpart |
---|
[ce27e21] | 324 | xvalue = kw.pop(xname, xdefault) |
---|
| 325 | if xlimits is not None: |
---|
| 326 | xvalue = Parameter.default(xvalue, name=xname, limits=xlimits) |
---|
[14de349] | 327 | pars.append(xname) |
---|
[ce27e21] | 328 | setattr(self, xname, xvalue) |
---|
| 329 | self._parameter_names = pars |
---|
[14de349] | 330 | if kw: |
---|
[7e224c2] | 331 | raise TypeError("unexpected parameters: %s" % (", ".join(sorted(kw.keys())))) |
---|
[14de349] | 332 | self.update() |
---|
| 333 | |
---|
| 334 | def update(self): |
---|
| 335 | self._cache = {} |
---|
| 336 | |
---|
| 337 | def numpoints(self): |
---|
[7e224c2] | 338 | """ |
---|
| 339 | Return the number of points |
---|
| 340 | """ |
---|
[14de349] | 341 | return len(self.iq) |
---|
| 342 | |
---|
| 343 | def parameters(self): |
---|
[7e224c2] | 344 | """ |
---|
| 345 | Return a dictionary of parameters |
---|
| 346 | """ |
---|
| 347 | return dict((k, getattr(self, k)) for k in self._parameter_names) |
---|
[14de349] | 348 | |
---|
| 349 | def theory(self): |
---|
| 350 | if 'theory' not in self._cache: |
---|
[abb22f4] | 351 | if self._fn is None: |
---|
[7e224c2] | 352 | input_value = self.model.make_input(self._fn_inputs) |
---|
| 353 | self._fn = self.model(input_value) |
---|
[abb22f4] | 354 | |
---|
[7e224c2] | 355 | fixed_pars = [getattr(self, p).value for p in self._fn.fixed_pars] |
---|
[abb22f4] | 356 | pd_pars = [self._get_weights(p) for p in self._fn.pd_pars] |
---|
[f1ecfa92] | 357 | #print fixed_pars,pd_pars |
---|
| 358 | self._theory[self.index] = self._fn(fixed_pars, pd_pars, self.cutoff) |
---|
[abb22f4] | 359 | #self._theory[:] = self._fn.eval(pars, pd_pars) |
---|
[c97724e] | 360 | if self.data_type == 'sesans': |
---|
[7e224c2] | 361 | P = sesans.hankel(self.data.x, self.data.lam * 1e-9, |
---|
| 362 | self.data.sample.thickness / 10, self._fn_inputs[0], |
---|
[c97724e] | 363 | self._theory) |
---|
| 364 | self._cache['theory'] = P |
---|
| 365 | else: |
---|
| 366 | self._cache['theory'] = self._theory |
---|
[14de349] | 367 | return self._cache['theory'] |
---|
| 368 | |
---|
| 369 | def residuals(self): |
---|
| 370 | #if np.any(self.err ==0): print "zeros in err" |
---|
[7e224c2] | 371 | return (self.theory()[self.index] - self.iq) / self.diq |
---|
[14de349] | 372 | |
---|
| 373 | def nllf(self): |
---|
| 374 | R = self.residuals() |
---|
| 375 | #if np.any(np.isnan(R)): print "NaN in residuals" |
---|
[7e224c2] | 376 | return 0.5 * np.sum(R ** 2) |
---|
[14de349] | 377 | |
---|
| 378 | def __call__(self): |
---|
[7e224c2] | 379 | return 2 * self.nllf() / self.dof |
---|
[14de349] | 380 | |
---|
| 381 | def plot(self, view='log'): |
---|
[c97724e] | 382 | """ |
---|
| 383 | Plot the data and residuals. |
---|
| 384 | """ |
---|
| 385 | data, theory = self.data, self.theory() |
---|
| 386 | if self.data_type == 'Iq': |
---|
| 387 | _plot_result1D(data, theory, view) |
---|
| 388 | elif self.data_type == 'Iqxy': |
---|
| 389 | _plot_result2D(data, theory, view) |
---|
| 390 | elif self.data_type == 'sesans': |
---|
| 391 | _plot_sesans(data, theory, view) |
---|
| 392 | else: |
---|
| 393 | raise ValueError("Unknown data type") |
---|
| 394 | |
---|
| 395 | def simulate_data(self, noise=None): |
---|
| 396 | print "noise", noise |
---|
| 397 | if noise is None: |
---|
| 398 | noise = self.diq[self.index] |
---|
| 399 | else: |
---|
[7e224c2] | 400 | noise = 0.01 * noise |
---|
[c97724e] | 401 | self.diq[self.index] = noise |
---|
| 402 | y = self.theory() |
---|
[7e224c2] | 403 | y += y * np.random.randn(*y.shape) * noise |
---|
[c97724e] | 404 | if self.data_type == 'Iq': |
---|
| 405 | self.data.y[self.index] = y |
---|
| 406 | elif self.data_type == 'Iqxy': |
---|
| 407 | self.data.data[self.index] = y |
---|
| 408 | elif self.data_type == 'sesans': |
---|
[0ac3db5] | 409 | self.data.y[self.index] = y |
---|
[c97724e] | 410 | else: |
---|
| 411 | raise ValueError("Unknown model") |
---|
[14de349] | 412 | |
---|
| 413 | def save(self, basename): |
---|
| 414 | pass |
---|
| 415 | |
---|
[ce27e21] | 416 | def _get_weights(self, par): |
---|
[7e224c2] | 417 | """ |
---|
| 418 | Get parameter dispersion weights |
---|
| 419 | """ |
---|
[ce27e21] | 420 | from . import weights |
---|
| 421 | |
---|
[abb22f4] | 422 | relative = self.model.info['partype']['pd-rel'] |
---|
| 423 | limits = self.model.info['limits'] |
---|
[7e224c2] | 424 | disperser, value, npts, width, nsigma = \ |
---|
| 425 | [getattr(self, par + ext) for ext in ('_pd_type', '', '_pd_n', '_pd', '_pd_nsigma')] |
---|
| 426 | v, w = weights.get_weights( |
---|
[ce27e21] | 427 | disperser, int(npts.value), width.value, nsigma.value, |
---|
| 428 | value.value, limits[par], par in relative) |
---|
[7e224c2] | 429 | return v, w / w.max() |
---|
[ce27e21] | 430 | |
---|
[abb22f4] | 431 | def __getstate__(self): |
---|
| 432 | # Can't pickle gpu functions, so instead make them lazy |
---|
| 433 | state = self.__dict__.copy() |
---|
| 434 | state['_fn'] = None |
---|
| 435 | return state |
---|
| 436 | |
---|
| 437 | def __setstate__(self, state): |
---|
| 438 | self.__dict__ = state |
---|
| 439 | |
---|
[ce27e21] | 440 | |
---|
[14de349] | 441 | def demo(): |
---|
| 442 | data = load_data('DEC07086.DAT') |
---|
| 443 | set_beam_stop(data, 0.004) |
---|
[9890053] | 444 | plot_data(data, data.data) |
---|
[14de349] | 445 | import matplotlib.pyplot as plt; plt.show() |
---|
| 446 | |
---|
[ce27e21] | 447 | |
---|
[14de349] | 448 | if __name__ == "__main__": |
---|
| 449 | demo() |
---|