Changeset c85db69 in sasmodels for sasmodels/kernelpy.py
- Timestamp:
- Mar 3, 2015 2:07:28 PM (9 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:
- e930946
- Parents:
- a156419
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sasmodels/kernelpy.py
rf734e7d rc85db69 1 1 import numpy as np 2 from numpy import pi, sin, cos, sqrt3 4 from .generate import F 32, F642 from numpy import pi, cos 3 4 from .generate import F64 5 5 6 6 class PyModel(object): 7 7 def __init__(self, info): 8 8 self.info = info 9 def __call__(self, input ):10 kernel = self.info['Iqxy'] if input .is_2D else self.info['Iq']11 return PyKernel(kernel, self.info, input )9 def __call__(self, input_value): 10 kernel = self.info['Iqxy'] if input_value.is_2D else self.info['Iq'] 11 return PyKernel(kernel, self.info, input_value) 12 12 def make_input(self, q_vectors): 13 13 return PyInput(q_vectors, dtype=F64) … … 38 38 self.dtype = dtype 39 39 self.is_2D = (len(q_vectors) == 2) 40 self.q_vectors = [np.ascontiguousarray(q, self.dtype) for q in q_vectors]40 self.q_vectors = [np.ascontiguousarray(q, self.dtype) for q in q_vectors] 41 41 self.q_pointers = [q.ctypes.data for q in q_vectors] 42 42 … … 73 73 if dim == '2d': 74 74 def vector_kernel(qx, qy, *args): 75 return np.array([kernel(qxi, qyi,*args) for qxi,qyi in zip(qx,qy)])75 return np.array([kernel(qxi, qyi, *args) for qxi, qyi in zip(qx, qy)]) 76 76 else: 77 77 def vector_kernel(q, *args): 78 return np.array([kernel(qi, *args) for qi in q])78 return np.array([kernel(qi, *args) for qi in q]) 79 79 self.kernel = vector_kernel 80 80 else: 81 81 self.kernel = kernel 82 fixed_pars = info['partype']['fixed-' +dim]83 pd_pars = info['partype']['pd-' +dim]82 fixed_pars = info['partype']['fixed-' + dim] 83 pd_pars = info['partype']['pd-' + dim] 84 84 vol_pars = info['partype']['volume'] 85 85 … … 87 87 pars = [p[0] for p in info['parameters'][2:]] 88 88 offset = len(self.input.q_vectors) 89 self.args = self.input.q_vectors + [None] *len(pars)90 self.fixed_index = np.array([pars.index(p) +offset for p in fixed_pars[2:]])91 self.pd_index = np.array([pars.index(p) +offset for p in pd_pars])92 self.vol_index = np.array([pars.index(p) +offset for p in vol_pars])93 try: self.theta_index = pars.index('theta') +offset89 self.args = self.input.q_vectors + [None] * len(pars) 90 self.fixed_index = np.array([pars.index(p) + offset for p in fixed_pars[2:]]) 91 self.pd_index = np.array([pars.index(p) + offset for p in pd_pars]) 92 self.vol_index = np.array([pars.index(p) + offset for p in vol_pars]) 93 try: self.theta_index = pars.index('theta') + offset 94 94 except ValueError: self.theta_index = -1 95 95 … … 105 105 # First two fixed 106 106 scale, background = fixed[:2] 107 for index, value in zip(self.fixed_index, fixed[2:]):107 for index, value in zip(self.fixed_index, fixed[2:]): 108 108 args[index] = float(value) 109 res = _loops(form, form_volume, cutoff, scale, background, 109 res = _loops(form, form_volume, cutoff, scale, background, args, 110 110 pd, self.pd_index, self.vol_index, self.theta_index) 111 111 … … 185 185 for k in range(stride[-1]): 186 186 # update polydispersity parameter values 187 fast_index = k %stride[0]187 fast_index = k % stride[0] 188 188 if fast_index == 0: # bottom loop complete ... check all other loops 189 189 if weight.size > 0: 190 for i, index, in enumerate(k%stride):190 for i, index, in enumerate(k % stride): 191 191 args[pd_index[i]] = pd[i][0][index] 192 192 weight[i] = pd[i][1][index] … … 202 202 if w > cutoff: 203 203 I = form(*args) 204 positive = (I >=0.0)204 positive = (I >= 0.0) 205 205 206 206 # Note: can precompute spherical correction if theta_index is not the fast index 207 207 # Correction factor for spherical integration p(theta) I(q) sin(theta) dtheta 208 208 #spherical_correction = abs(sin(pi*args[theta_index])) if theta_index>=0 else 1.0 209 spherical_correction = abs(cos(pi *args[theta_index]))*pi/2 if theta_index>=0 else 1.0209 spherical_correction = abs(cos(pi * args[theta_index])) * pi / 2 if theta_index >= 0 else 1.0 210 210 #spherical_correction = 1.0 211 ret += w *I*spherical_correction*positive212 norm += w *positive211 ret += w * I * spherical_correction * positive 212 norm += w * positive 213 213 214 214 # Volume normalization. … … 220 220 vol_args = [args[index] for index in vol_index] 221 221 vol_weight = np.prod(weight[vol_weight_index]) 222 vol += vol_weight *form_volume(*vol_args)*positive223 vol_norm += vol_weight *positive224 225 positive = (vol *vol_norm != 0.0)222 vol += vol_weight * form_volume(*vol_args) * positive 223 vol_norm += vol_weight * positive 224 225 positive = (vol * vol_norm != 0.0) 226 226 ret[positive] *= vol_norm[positive] / vol[positive] 227 result = scale *ret/norm+background227 result = scale * ret / norm + background 228 228 return result
Note: See TracChangeset
for help on using the changeset viewer.