Changeset ff7119b in sasmodels for sasmodels/bumps_model.py
- Timestamp:
- Aug 26, 2014 8:27:06 PM (10 years ago)
- Branches:
- master, core_shell_microgels, costrafo411, magnetic_model, release_v0.94, release_v0.95, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
- Children:
- 5d4777d
- Parents:
- a7684e5
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
sasmodels/bumps_model.py
r13d86bc rff7119b 1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 1 """ 2 Sasmodels core. 3 """ 4 4 import sys, os 5 5 import datetime 6 import warnings7 6 8 7 import numpy as np 9 8 10 9 11 def opencl_model(kernel_module, dtype="single"):12 from sasmodels import gen, gpu13 14 source, info = gen.make(kernel_module)15 ## for debugging, save source to a .cl file, edit it, and reload as model16 #open(modelname+'.cl','w').write(source)17 #source = open(modelname+'.cl','r').read()18 return gpu.GpuModel(source, info, dtype)19 20 21 if sys.platform == 'darwin':22 COMPILE = "gcc-mp-4.7 -shared -fPIC -std=c99 -fopenmp -O2 -Wall %s -o %s -lm -lgomp"23 elif os.name == 'nt':24 COMPILE = "gcc -shared -fPIC -std=c99 -fopenmp -O2 -Wall %s -o %s -lm"25 else:26 COMPILE = "cc -shared -fPIC -std=c99 -fopenmp -O2 -Wall %s -o %s -lm"27 DLL_PATH = "/tmp"28 29 30 def dll_path(info):31 from os.path import join as joinpath, split as splitpath, splitext32 basename = splitext(splitpath(info['filename'])[1])[0]33 return joinpath(DLL_PATH, basename+'.so')34 35 36 def dll_model(kernel_module):37 import os38 import tempfile39 from sasmodels import gen, dll40 41 source, info = gen.make(kernel_module)42 dllpath = dll_path(info)43 if not os.path.exists(dllpath) \44 or (os.path.getmtime(dllpath) < os.path.getmtime(info['filename'])):45 # Replace with a proper temp file46 srcfile = tempfile.mkstemp(suffix=".c",prefix="sas_"+info['name'])47 open(srcfile, 'w').write(source)48 os.system(COMPILE%(srcfile, dllpath))49 ## comment the following to keep the generated c file50 #os.unlink(srcfile)51 return dll.DllModel(dllpath, info)52 53 54 TIC = None55 10 def tic(): 56 global TIC 11 """ 12 Timer function. 13 14 Use "toc=tic()" to start the clock and "toc()" to measure 15 a time interval. 16 """ 57 17 then = datetime.datetime.now() 58 TIC = lambda: (datetime.datetime.now()-then).total_seconds() 59 return TIC 60 61 62 def toc(): 63 return TIC() 18 return lambda: (datetime.datetime.now()-then).total_seconds() 64 19 65 20 66 21 def load_data(filename): 22 """ 23 Load data using a sasview loader. 24 """ 67 25 from sans.dataloader.loader import Loader 68 26 loader = Loader() … … 73 31 74 32 33 def empty_data1D(q): 34 """ 35 Create empty 1D data using the given *q* as the x value. 36 37 Resolutions dq/q is 5%. 38 """ 39 40 from sans.dataloader.data_info import Data1D 41 42 Iq = 100*np.ones_like(q) 43 dIq = np.sqrt(Iq) 44 data = Data1D(q, Iq, dx=0.05*q, dy=dIq) 45 data.filename = "fake data" 46 data.qmin, data.qmax = q.min(), q.max() 47 return data 48 49 75 50 def empty_data2D(qx, qy=None): 51 """ 52 Create empty 2D data using the given mesh. 53 54 If *qy* is missing, create a square mesh with *qy=qx*. 55 56 Resolution dq/q is 5%. 57 """ 76 58 from sans.dataloader.data_info import Data2D, Detector 77 59 … … 114 96 115 97 116 def empty_data1D(q):117 from sans.dataloader.data_info import Data1D118 119 Iq = 100*np.ones_like(q)120 dIq = np.sqrt(Iq)121 data = Data1D(q, Iq, dx=0.05*q, dy=dIq)122 data.filename = "fake data"123 data.qmin, data.qmax = q.min(), q.max()124 return data125 126 127 98 def set_beam_stop(data, radius, outer=None): 99 """ 100 Add a beam stop of the given *radius*. If *outer*, make an annulus. 101 """ 128 102 from sans.dataloader.manipulations import Ringcut 129 103 if hasattr(data, 'qx_data'): … … 138 112 139 113 def set_half(data, half): 114 """ 115 Select half of the data, either "right" or "left". 116 """ 140 117 from sans.dataloader.manipulations import Boxcut 141 118 if half == 'right': … … 146 123 147 124 def set_top(data, max): 125 """ 126 Chop the top off the data, above *max*. 127 """ 148 128 from sans.dataloader.manipulations import Boxcut 149 129 data.mask += Boxcut(x_min=-np.inf, x_max=np.inf, y_min=-np.inf, y_max=max)(data) … … 151 131 152 132 def plot_data(data, iq, vmin=None, vmax=None, scale='log'): 133 """ 134 Plot the target value for the data. This could be the data itself, 135 the theory calculation, or the residuals. 136 137 *scale* can be 'log' for log scale data, or 'linear'. 138 """ 153 139 from numpy.ma import masked_array, masked 154 140 import matplotlib.pyplot as plt … … 172 158 173 159 174 def plot_result2D(data, theory, view='log'): 160 def _plot_result1D(data, theory, view): 161 """ 162 Plot the data and residuals for 1D data. 163 """ 164 import matplotlib.pyplot as plt 165 from numpy.ma import masked_array, masked 166 #print "not a number",sum(np.isnan(data.y)) 167 #data.y[data.y<0.05] = 0.5 168 mdata = masked_array(data.y, data.mask) 169 mdata[np.isnan(mdata)] = masked 170 if view is 'log': 171 mdata[mdata <= 0] = masked 172 mtheory = masked_array(theory, mdata.mask) 173 mresid = masked_array((theory-data.y)/data.dy, mdata.mask) 174 175 plt.subplot(121) 176 plt.errorbar(data.x, mdata, yerr=data.dy) 177 plt.plot(data.x, mtheory, '-', hold=True) 178 plt.yscale(view) 179 plt.subplot(122) 180 plt.plot(data.x, mresid, 'x') 181 #plt.axhline(1, color='black', ls='--',lw=1, hold=True) 182 #plt.axhline(0, color='black', lw=1, hold=True) 183 #plt.axhline(-1, color='black', ls='--',lw=1, hold=True) 184 185 186 def _plot_result2D(data, theory, view): 187 """ 188 Plot the data and residuals for 2D data. 189 """ 175 190 import matplotlib.pyplot as plt 176 191 resid = (theory-data.data)/data.err_data … … 185 200 plt.colorbar() 186 201 187 188 def plot_result1D(data, theory, view='log'): 189 import matplotlib.pyplot as plt 190 from numpy.ma import masked_array, masked 191 #print "not a number",sum(np.isnan(data.y)) 192 #data.y[data.y<0.05] = 0.5 193 mdata = masked_array(data.y, data.mask) 194 mdata[np.isnan(mdata)] = masked 195 if view is 'log': 196 mdata[mdata <= 0] = masked 197 mtheory = masked_array(theory, mdata.mask) 198 mresid = masked_array((theory-data.y)/data.dy, mdata.mask) 199 200 plt.subplot(121) 201 plt.errorbar(data.x, mdata, yerr=data.dy) 202 plt.plot(data.x, mtheory, '-', hold=True) 203 plt.yscale(view) 204 plt.subplot(122) 205 plt.plot(data.x, mresid, 'x') 206 #plt.axhline(1, color='black', ls='--',lw=1, hold=True) 207 #plt.axhline(0, color='black', lw=1, hold=True) 208 #plt.axhline(-1, color='black', ls='--',lw=1, hold=True) 202 def plot_result(data, theory, view='log'): 203 """ 204 Plot the data and residuals. 205 """ 206 if hasattr(data, 'qx_data'): 207 _plot_result2D(data, theory, view) 208 else: 209 _plot_result1D(data, theory, view) 209 210 210 211 211 212 class BumpsModel(object): 213 """ 214 Return a bumps wrapper for a SAS model. 215 216 *data* is the data to be fitted. 217 218 *model* is the SAS model, e.g., from :func:`gen.opencl_model`. 219 220 *cutoff* is the integration cutoff, which avoids computing the 221 the SAS model where the polydispersity weight is low. 222 223 Model parameters can be initialized with additional keyword 224 arguments, or by assigning to model.parameter_name.value. 225 226 The resulting bumps model can be used directly in a FitProblem call. 227 """ 212 228 def __init__(self, data, model, cutoff=1e-5, **kw): 213 229 from bumps.names import Parameter 214 from . import gpu215 230 216 231 # interpret data 217 self.is2D = hasattr(data,'qx_data')218 232 self.data = data 219 if self.is2D:233 if hasattr(data, 'qx_data'): 220 234 self.index = (data.mask==0) & (~np.isnan(data.data)) 221 235 self.iq = data.data[self.index] … … 294 308 295 309 def plot(self, view='log'): 296 if self.is2D: 297 plot_result2D(self.data, self.theory(), view=view) 298 else: 299 plot_result1D(self.data, self.theory(), view=view) 310 plot_result(self.data, self.theory(), view=view) 300 311 301 312 def save(self, basename):
Note: See TracChangeset
for help on using the changeset viewer.