Changeset 33c8d73 in sasmodels
- Timestamp:
- Mar 13, 2015 4:54:44 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:
- 7954f5c
- Parents:
- 29aa28f
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sasmodels/resolution.py
r29aa28f r33c8d73 28 28 q = None 29 29 q_calc = None 30 def apply(self, Iq_calc):31 """ 32 Smear * Iq_calc* by the resolution function, returning *Iq*.30 def apply(self, theory): 31 """ 32 Smear *theory* by the resolution function, returning *Iq*. 33 33 """ 34 34 raise NotImplementedError("Subclass does not define the apply function") … … 43 43 self.q_calc = self.q = q 44 44 45 def apply(self, Iq_calc):46 return Iq_calc45 def apply(self, theory): 46 return theory 47 47 48 48 class Pinhole1D(Resolution1D): … … 52 52 *q* points at which the data is measured. 53 53 54 * dq* gaussian 1-sigma resolution at each data point.54 *q_width* gaussian 1-sigma resolution at each data point. 55 55 56 56 *q_calc* is the list of points to calculate, or None if this should 57 be autogenerated from the *q, dq*.57 be autogenerated from the *q,q_width*. 58 58 """ 59 59 def __init__(self, q, q_width, q_calc=None): … … 74 74 self.q, np.maximum(q_width, MINIMUM_RESOLUTION)) 75 75 76 def apply(self, Iq_calc):77 return apply_resolution_matrix(self.weight_matrix, Iq_calc)76 def apply(self, theory): 77 return apply_resolution_matrix(self.weight_matrix, theory) 78 78 79 79 class Slit1D(Resolution1D): … … 98 98 slit_resolution(self.q_calc, self.q, self.qx_width, self.qy_width) 99 99 100 def apply(self, Iq_calc):101 return apply_resolution_matrix(self.weight_matrix, Iq_calc)102 103 104 def apply_resolution_matrix(weight_matrix, Iq_calc):100 def apply(self, theory): 101 return apply_resolution_matrix(self.weight_matrix, theory) 102 103 104 def apply_resolution_matrix(weight_matrix, theory): 105 105 """ 106 106 Apply the resolution weight matrix to the computed theory function. 107 107 """ 108 #print "apply shapes", Iq_calc.shape, self.weight_matrix.shape109 Iq = np.dot( Iq_calc[None,:], weight_matrix)108 #print "apply shapes",theory.shape, self.weight_matrix.shape 109 Iq = np.dot(theory[None,:], weight_matrix) 110 110 #print "result shape",Iq.shape 111 111 return Iq.flatten() … … 123 123 edges = bin_edges(q_calc) 124 124 edges[edges<0.0] = 0.0 # clip edges below zero 125 index = (q_width >0.0) # treat perfect resolution differently125 index = (q_width > 0.0) # treat perfect resolution differently 126 126 G = erf( (edges[:,None] - q[None,:]) / (sqrt(2.0)*q_width)[None,:] ) 127 127 weights = G[1:] - G[:-1] … … 168 168 # Make q_calc into a row vector, and q, qx_width, qy_width into columns 169 169 # Make weights match [ q_calc X q ] 170 weights = np.zeros((len(q),len(q_calc)), 'd')170 weights = np.zeros((len(q),len(q_calc)), 'd') 171 171 q_calc = q_calc[None,:] 172 172 q, qx_width, qy_width, edges = [ … … 226 226 227 227 def setUp(self): 228 self.x = 0.001*np.arange(1, 11)228 self.x = 0.001*np.arange(1, 11) 229 229 self.y = self.Iq(self.x) 230 230 … … 238 238 """ 239 239 resolution = Perfect1D(self.x) 240 Iq_calc= self.Iq(resolution.q_calc)241 output = resolution.apply( Iq_calc)240 theory = self.Iq(resolution.q_calc) 241 output = resolution.apply(theory) 242 242 np.testing.assert_equal(output, self.y) 243 243 … … 247 247 """ 248 248 resolution = Slit1D(self.x, 0., 0.) 249 Iq_calc= self.Iq(resolution.q_calc)250 output = resolution.apply( Iq_calc)249 theory = self.Iq(resolution.q_calc) 250 output = resolution.apply(theory) 251 251 np.testing.assert_equal(output, self.y) 252 252 … … 257 257 """ 258 258 resolution = Slit1D(self.x, 0., 0.005) 259 Iq_calc = self.Iq(resolution.q_calc) 260 output = resolution.apply(Iq_calc) 261 # The following commented line was the correct output for even bins [see smearer.cpp for details] 262 #answer = [ 9.666, 9.056, 8.329, 7.494, 6.642, 5.721, 4.774, 3.824, 2.871, 2. ] 263 answer = [ 9.0618, 8.6401, 8.1186, 7.1391, 6.1528, 5.5555, 4.5584, 3.5606, 2.5623, 2. ] 259 theory = self.Iq(resolution.q_calc) 260 output = resolution.apply(theory) 261 answer = [ 9.0618, 8.6401, 8.1186, 7.1391, 6.1528, 262 5.5555, 4.5584, 3.5606, 2.5623, 2. ] 264 263 np.testing.assert_allclose(output, answer, atol=1e-4) 265 264 … … 269 268 """ 270 269 resolution = Pinhole1D(self.x, 0.0*self.x) 271 Iq_calc= self.Iq(resolution.q_calc)272 output = resolution.apply( Iq_calc)270 theory = self.Iq(resolution.q_calc) 271 output = resolution.apply(theory) 273 272 np.testing.assert_equal(output, self.y) 274 273 … … 279 278 resolution = Pinhole1D(self.x, 0.001*np.ones_like(self.x), 280 279 q_calc=self.x) 281 Iq_calc= 12.0-1000.0*resolution.q_calc282 output = resolution.apply( Iq_calc)283 answer = [ 10.44785079, 9.84991299,8.98101708,284 7.99906585, 6.99998311,6.00001689,285 5.00093415, 4.01898292, 3.15008701,2.55214921]280 theory = 12.0-1000.0*resolution.q_calc 281 output = resolution.apply(theory) 282 answer = [ 10.44785079, 9.84991299, 8.98101708, 283 7.99906585, 6.99998311, 6.00001689, 284 5.00093415, 4.01898292, 3.15008701, 2.55214921] 286 285 np.testing.assert_allclose(output, answer, atol=1e-8) 287 286 … … 395 394 396 395 def setUp(self): 397 # sample q, dq, I(q) calculated by NIST Igor SANS package396 # sample q, q_width, I(q) calculated by NIST Igor SANS package 398 397 self.data = np.loadtxt(SPHERE_RESOLUTION_TEST_DATA.split('\n')).T 399 398 self.pars = dict(scale=1.0, background=0.01, radius=60.0, 400 399 solvent_sld=6.3, sld=1) 401 400 402 def Iq(self, q, dq):401 def Iq(self, q, q_width): 403 402 from sasmodels import core 404 403 from sasmodels.models import sphere 405 404 406 405 model = core.load_model(sphere) 407 resolution = Pinhole1D(q, dq)406 resolution = Pinhole1D(q, q_width) 408 407 kernel = core.make_kernel(model, [resolution.q_calc]) 409 Iq_calc= core.call_kernel(kernel, self.pars)410 result = resolution.apply( Iq_calc)408 theory = core.call_kernel(kernel, self.pars) 409 result = resolution.apply(theory) 411 410 return result 412 411 … … 415 414 Compare pinhole resolution smearing with NIST Igor SANS 416 415 """ 417 q, dq, answer = self.data418 output = self.Iq(q, dq)416 q, q_width, answer = self.data 417 output = self.Iq(q, q_width) 419 418 np.testing.assert_allclose(output, answer, rtol=0.006) 420 419 … … 425 424 Compare pinhole resolution smearing with NIST Igor SANS on sparse data 426 425 """ 427 q, dq, answer = self.data[:, ::20] # Take every nth point428 output = self.Iq(q, dq)426 q, q_width, answer = self.data[:, ::20] # Take every nth point 427 output = self.Iq(q, q_width) 429 428 np.testing.assert_allclose(output, answer, rtol=0.006) 430 429 … … 459 458 460 459 kernel = core.make_kernel(model, [resolution.q_calc]) 461 Iq_calc= core.call_kernel(kernel, {'length':210, 'radius':500})462 Iq = resolution.apply( Iq_calc)460 theory = core.call_kernel(kernel, {'length':210, 'radius':500}) 461 Iq = resolution.apply(theory) 463 462 464 463 import matplotlib.pyplot as plt 465 plt.loglog(resolution.q_calc, Iq_calc, label='unsmeared')464 plt.loglog(resolution.q_calc, theory, label='unsmeared') 466 465 plt.loglog(resolution.q, Iq, label='smeared', hold=True) 467 466 plt.legend() … … 471 470 472 471 def demo_pinhole_1d(): 473 q = np.logspace(-3, -1,400)474 dq= 0.1*q475 resolution = Pinhole1D(q, dq)472 q = np.logspace(-3, -1, 400) 473 q_width = 0.1*q 474 resolution = Pinhole1D(q, q_width) 476 475 _eval_demo_1d(resolution, title="10% dQ/Q Pinhole Resolution") 477 476 478 477 def demo_slit_1d(): 479 q = np.logspace(-3, -1,400)478 q = np.logspace(-3, -1, 400) 480 479 qx_width = 0.005 481 480 qy_width = 0.0
Note: See TracChangeset
for help on using the changeset viewer.