Changeset 0ff62d4 in sasmodels
- Timestamp:
- Apr 15, 2016 10:31:35 AM (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:
- f2f67a6
- Parents:
- 8f6817d
- Location:
- sasmodels
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
sasmodels/details.py
r8d62008 r0ff62d4 83 83 print("theta par", self.buffer[-1]) 84 84 85 def build_details(kernel, pairs):86 values, weights = zip(*pairs)87 if max([len(w) for w in weights]) > 1:88 call_details = poly_details(kernel.info, weights)89 else:90 call_details = mono_details(kernel.info)91 weights, values = [np.hstack(v) for v in (weights, values)]92 weights = weights.astype(dtype=kernel.dtype)93 values = values.astype(dtype=kernel.dtype)94 return call_details, weights, values95 96 85 def mono_details(model_info): 97 86 call_details = CallDetails(model_info) … … 139 128 raise NotImplementedError("Can't handle constraints yet") 140 129 141 142 try:143 np.meshgrid([])144 meshgrid = np.meshgrid145 except ValueError:146 # CRUFT: np.meshgrid requires multiple vectors147 def meshgrid(*args):148 if len(args) > 1:149 return np.meshgrid(*args)150 else:151 return [np.asarray(v) for v in args]152 153 def dispersion_mesh(model_info, pars):154 """155 Create a mesh grid of dispersion parameters and weights.156 157 Returns [p1,p2,...],w where pj is a vector of values for parameter j158 and w is a vector containing the products for weights for each159 parameter set in the vector.160 """161 value, weight = zip(*pars)162 weight = [w if w else [1.] for w in weight]163 weight = np.vstack([v.flatten() for v in meshgrid(*weight)])164 weight = np.prod(weight, axis=0)165 value = [v.flatten() for v in meshgrid(*value)]166 lengths = [par.length for par in model_info.parameters.kernel_parameters167 if par.type == 'volume']168 if any(n > 1 for n in lengths):169 pars = []170 offset = 0171 for n in lengths:172 pars.append(np.vstack(value[offset:offset+n]) if n > 1 else value[offset])173 offset += n174 value = pars175 return value, weight176 177 -
sasmodels/direct_model.py
ra5b8477 r0ff62d4 30 30 from . import resolution 31 31 from . import resolution2d 32 from . import details32 from . import kernel 33 33 34 34 try: … … 41 41 from .modelinfo import Parameter, ParameterSet 42 42 43 def call_kernel( kernel, pars, cutoff=0., mono=False):43 def call_kernel(calculator, pars, cutoff=0., mono=False): 44 44 # type: (Kernel, ParameterSet, float, bool) -> np.ndarray 45 45 """ … … 55 55 *mono* is True if polydispersity should be set to none on all parameters. 56 56 """ 57 parameters = kernel.info.parameters57 parameters = calculator.info.parameters 58 58 if mono: 59 59 active = lambda name: False 60 elif kernel.dim == '1d':60 elif calculator.dim == '1d': 61 61 active = lambda name: name in parameters.pd_1d 62 elif kernel.dim == '2d':62 elif calculator.dim == '2d': 63 63 active = lambda name: name in parameters.pd_2d 64 64 else: … … 69 69 for p in parameters.call_parameters] 70 70 71 call_details, weights, values = details.build_details(kernel, vw_pairs)72 return kernel(call_details, weights, values, cutoff)71 call_details, weights, values = kernel.build_details(calculator, vw_pairs) 72 return calculator(call_details, weights, values, cutoff) 73 73 74 74 def get_weights(parameter, values): -
sasmodels/kernel.py
ra5b8477 r0ff62d4 10 10 """ 11 11 12 import numpy as np 13 from .details import mono_details, poly_details 14 12 15 try: 13 16 from typing import List 17 except ImportError: 18 pass 19 else: 14 20 from .details import CallDetails 15 21 from .modelinfo import ModelInfo 16 22 import numpy as np # type: ignore 17 except ImportError:18 pass19 23 20 24 class KernelModel(object): … … 42 46 # type: () -> None 43 47 pass 48 49 try: 50 np.meshgrid([]) 51 meshgrid = np.meshgrid 52 except ValueError: 53 # CRUFT: np.meshgrid requires multiple vectors 54 def meshgrid(*args): 55 if len(args) > 1: 56 return np.meshgrid(*args) 57 else: 58 return [np.asarray(v) for v in args] 59 60 def dispersion_mesh(model_info, pars): 61 """ 62 Create a mesh grid of dispersion parameters and weights. 63 64 Returns [p1,p2,...],w where pj is a vector of values for parameter j 65 and w is a vector containing the products for weights for each 66 parameter set in the vector. 67 """ 68 value, weight = zip(*pars) 69 weight = [w if w else [1.] for w in weight] 70 weight = np.vstack([v.flatten() for v in meshgrid(*weight)]) 71 weight = np.prod(weight, axis=0) 72 value = [v.flatten() for v in meshgrid(*value)] 73 lengths = [par.length for par in model_info.parameters.kernel_parameters 74 if par.type == 'volume'] 75 if any(n > 1 for n in lengths): 76 pars = [] 77 offset = 0 78 for n in lengths: 79 pars.append(np.vstack(value[offset:offset+n]) if n > 1 else value[offset]) 80 offset += n 81 value = pars 82 return value, weight 83 84 85 86 def build_details(kernel, pairs): 87 # type: (Kernel, Tuple[List[np.ndarray], List[np.ndarray]]) -> Tuple[CallDetails, np.ndarray, np.ndarray] 88 """ 89 Construct the kernel call details object for calling the particular kernel. 90 """ 91 values, weights = zip(*pairs) 92 if max([len(w) for w in weights]) > 1: 93 call_details = poly_details(kernel.info, weights) 94 else: 95 call_details = mono_details(kernel.info) 96 weights, values = [np.hstack(v) for v in (weights, values)] 97 weights = weights.astype(dtype=kernel.dtype) 98 values = values.astype(dtype=kernel.dtype) 99 return call_details, weights, values 100 -
sasmodels/model_test.py
rb151003 r0ff62d4 51 51 52 52 from .core import list_models, load_model_info, build_model, HAVE_OPENCL 53 from . detailsimport dispersion_mesh53 from .kernel import dispersion_mesh 54 54 from .direct_model import call_kernel, get_weights 55 55 from .exception import annotate_exception … … 62 62 else: 63 63 from .modelinfo import ParameterTable, ParameterSet, TestCondition, ModelInfo 64 from .kernel py import PyModel, PyInput, PyKernel, DType64 from .kernel import KernelModel 65 65 66 66 def call_ER(model_info, pars): … … 226 226 227 227 def run_one(self, model, test): 228 # type: ( PyModel, TestCondition) -> None228 # type: (KernelModel, TestCondition) -> None 229 229 user_pars, x, y = test 230 230 pars = expand_pars(self.info.parameters, user_pars) -
sasmodels/product.py
r7ae2b7f r0ff62d4 13 13 import numpy as np # type: ignore 14 14 15 from .details import dispersion_mesh16 15 from .modelinfo import suffix_parameter, ParameterTable, ModelInfo 17 from .kernel import KernelModel, Kernel 16 from .kernel import KernelModel, Kernel, dispersion_mesh 18 17 19 18 try: -
sasmodels/sasview_model.py
r60f03de r0ff62d4 27 27 from . import generate 28 28 from . import weights 29 from . import details30 29 from . import modelinfo 30 from . import kernel 31 31 32 32 try: … … 502 502 pairs = [self._get_weights(p) 503 503 for p in self._model_info.parameters.call_parameters] 504 call_details, weight, value = details.build_details(kernel, pairs)504 call_details, weight, value = kernel.build_details(kernel, pairs) 505 505 result = kernel(call_details, weight, value, cutoff=self.cutoff) 506 506 kernel.release() … … 577 577 for p in self._model_info.parameters.call_parameters 578 578 if p.type == 'volume'] 579 return details.dispersion_mesh(self._model_info, pars)579 return kernel.dispersion_mesh(self._model_info, pars) 580 580 581 581 def _get_weights(self, par):
Note: See TracChangeset
for help on using the changeset viewer.