Changeset 0ff62d4 in sasmodels


Ignore:
Timestamp:
Apr 15, 2016 10:31:35 AM (9 years ago)
Author:
Paul Kienzle <pkienzle@…>
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
Message:

refactor: move dispersion_mesh alongside build_details in kernel.py

Location:
sasmodels
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/details.py

    r8d62008 r0ff62d4  
    8383        print("theta par", self.buffer[-1]) 
    8484 
    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, values 
    95  
    9685def mono_details(model_info): 
    9786    call_details = CallDetails(model_info) 
     
    139128    raise NotImplementedError("Can't handle constraints yet") 
    140129 
    141  
    142 try: 
    143     np.meshgrid([]) 
    144     meshgrid = np.meshgrid 
    145 except ValueError: 
    146     # CRUFT: np.meshgrid requires multiple vectors 
    147     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 j 
    158     and w is a vector containing the products for weights for each 
    159     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_parameters 
    167                if par.type == 'volume'] 
    168     if any(n > 1 for n in lengths): 
    169         pars = [] 
    170         offset = 0 
    171         for n in lengths: 
    172             pars.append(np.vstack(value[offset:offset+n]) if n > 1 else value[offset]) 
    173             offset += n 
    174         value = pars 
    175     return value, weight 
    176  
    177  
  • sasmodels/direct_model.py

    ra5b8477 r0ff62d4  
    3030from . import resolution 
    3131from . import resolution2d 
    32 from . import details 
     32from . import kernel 
    3333 
    3434try: 
     
    4141    from .modelinfo import Parameter, ParameterSet 
    4242 
    43 def call_kernel(kernel, pars, cutoff=0., mono=False): 
     43def call_kernel(calculator, pars, cutoff=0., mono=False): 
    4444    # type: (Kernel, ParameterSet, float, bool) -> np.ndarray 
    4545    """ 
     
    5555    *mono* is True if polydispersity should be set to none on all parameters. 
    5656    """ 
    57     parameters = kernel.info.parameters 
     57    parameters = calculator.info.parameters 
    5858    if mono: 
    5959        active = lambda name: False 
    60     elif kernel.dim == '1d': 
     60    elif calculator.dim == '1d': 
    6161        active = lambda name: name in parameters.pd_1d 
    62     elif kernel.dim == '2d': 
     62    elif calculator.dim == '2d': 
    6363        active = lambda name: name in parameters.pd_2d 
    6464    else: 
     
    6969                for p in parameters.call_parameters] 
    7070 
    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) 
    7373 
    7474def get_weights(parameter, values): 
  • sasmodels/kernel.py

    ra5b8477 r0ff62d4  
    1010""" 
    1111 
     12import numpy as np 
     13from .details import mono_details, poly_details 
     14 
    1215try: 
    1316    from typing import List 
     17except ImportError: 
     18    pass 
     19else: 
    1420    from .details import CallDetails 
    1521    from .modelinfo import ModelInfo 
    1622    import numpy as np  # type: ignore 
    17 except ImportError: 
    18     pass 
    1923 
    2024class KernelModel(object): 
     
    4246        # type: () -> None 
    4347        pass 
     48 
     49try: 
     50    np.meshgrid([]) 
     51    meshgrid = np.meshgrid 
     52except 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 
     60def 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 
     86def 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  
    5151 
    5252from .core import list_models, load_model_info, build_model, HAVE_OPENCL 
    53 from .details import dispersion_mesh 
     53from .kernel import dispersion_mesh 
    5454from .direct_model import call_kernel, get_weights 
    5555from .exception import annotate_exception 
     
    6262else: 
    6363    from .modelinfo import ParameterTable, ParameterSet, TestCondition, ModelInfo 
    64     from .kernelpy import PyModel, PyInput, PyKernel, DType 
     64    from .kernel import KernelModel 
    6565 
    6666def call_ER(model_info, pars): 
     
    226226 
    227227        def run_one(self, model, test): 
    228             # type: (PyModel, TestCondition) -> None 
     228            # type: (KernelModel, TestCondition) -> None 
    229229            user_pars, x, y = test 
    230230            pars = expand_pars(self.info.parameters, user_pars) 
  • sasmodels/product.py

    r7ae2b7f r0ff62d4  
    1313import numpy as np  # type: ignore 
    1414 
    15 from .details import dispersion_mesh 
    1615from .modelinfo import suffix_parameter, ParameterTable, ModelInfo 
    17 from .kernel import KernelModel, Kernel 
     16from .kernel import KernelModel, Kernel, dispersion_mesh 
    1817 
    1918try: 
  • sasmodels/sasview_model.py

    r60f03de r0ff62d4  
    2727from . import generate 
    2828from . import weights 
    29 from . import details 
    3029from . import modelinfo 
     30from . import kernel 
    3131 
    3232try: 
     
    502502        pairs = [self._get_weights(p) 
    503503                 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) 
    505505        result = kernel(call_details, weight, value, cutoff=self.cutoff) 
    506506        kernel.release() 
     
    577577                for p in self._model_info.parameters.call_parameters 
    578578                if p.type == 'volume'] 
    579         return details.dispersion_mesh(self._model_info, pars) 
     579        return kernel.dispersion_mesh(self._model_info, pars) 
    580580 
    581581    def _get_weights(self, par): 
Note: See TracChangeset for help on using the changeset viewer.