Changeset 17bbadd in sasmodels for sasmodels/core.py


Ignore:
Timestamp:
Mar 15, 2016 12:47:12 PM (8 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:
754e27b
Parents:
5ceb7d0
Message:

refactor so all model defintion queries use model_info; better documentation of model_info structure; initial implementation of product model (broken)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/core.py

    rd18582e r17bbadd  
    2121 
    2222__all__ = [ 
    23     "list_models", "load_model_definition", "precompile_dll", 
    24     "load_model", "make_kernel", "call_kernel", "call_ER", "call_VR", 
     23    "list_models", "load_model_info", "precompile_dll", 
     24    "build_model", "make_kernel", "call_kernel", "call_ER_VR", 
    2525] 
    2626 
     
    3535 
    3636 
    37 def load_model_definition(model_name): 
     37def load_model_info(model_name): 
    3838    """ 
    3939    Load a model definition given the model name. 
     
    4343    """ 
    4444    __import__('sasmodels.models.'+model_name) 
    45     model_definition = getattr(models, model_name, None) 
    46     return model_definition 
     45    kernel_module = getattr(models, model_name, None) 
     46    return generate.make_model_info(kernel_module) 
    4747 
    4848 
     
    5151    Precompile the dll for a model. 
    5252 
    53     Returns the path to the compiled model. 
     53    Returns the path to the compiled model, or None if the model is a pure 
     54    python model. 
    5455 
    5556    This can be used when build the windows distribution of sasmodels 
     
    6061    dll path and the allowed floating point precision. 
    6162    """ 
    62     model_definition = load_model_definition(model_name) 
    63     source, info = generate.make(model_definition) 
    64     return kerneldll.make_dll(source, info, dtype=dtype) 
     63    model_info = load_model_info(model_name) 
     64    source = generate.make_source(model_info) 
     65    return kerneldll.make_dll(source, model_info, dtype=dtype) if source else None 
    6566 
    6667 
     
    7374    return True 
    7475 
    75 def load_model(model_definition, dtype=None, platform="ocl"): 
     76def build_model(model_info, dtype=None, platform="ocl"): 
    7677    """ 
    7778    Prepare the model for the default execution platform. 
     
    8081    on the model and the computing platform. 
    8182 
    82     *model_definition* is the python module which defines the model.  If the 
    83     model name is given instead, then :func:`load_model_definition` will be 
    84     called with the model name. 
     83    *model_info* is the model definition structure returned from 
     84    :func:`load_model_info`. 
    8585 
    8686    *dtype* indicates whether the model should use single or double precision 
     
    9393    otherwise it uses the default "ocl". 
    9494    """ 
    95     if isstr(model_definition): 
    96         model_definition = load_model_definition(model_definition) 
     95    source = generate.make_source(model_info) 
    9796    if dtype is None: 
    98         dtype = 'single' if getattr(model_definition, 'single', True) else 'double' 
    99     source, info = generate.make(model_definition) 
    100     if callable(info.get('Iq', None)): 
    101         return kernelpy.PyModel(info) 
     97        dtype = 'single' if model_info['single'] else 'double' 
     98    if callable(model_info.get('Iq', None)): 
     99        return kernelpy.PyModel(model_info) 
    102100 
    103101    ## for debugging: 
     
    107105    ##  4. rerun "python -m sasmodels.direct_model $MODELNAME" 
    108106    ##  5. uncomment open().read() so that source will be regenerated from model 
    109     # open(info['name']+'.c','w').write(source) 
    110     # source = open(info['name']+'.cl','r').read() 
     107    # open(model_info['name']+'.c','w').write(source) 
     108    # source = open(model_info['name']+'.cl','r').read() 
    111109 
    112110    if (platform == "dll" 
    113111            or not HAVE_OPENCL 
    114112            or not kernelcl.environment().has_type(dtype)): 
    115         return kerneldll.load_dll(source, info, dtype) 
     113        return kerneldll.load_dll(source, model_info, dtype) 
    116114    else: 
    117         return kernelcl.GpuModel(source, info, dtype) 
     115        return kernelcl.GpuModel(source, model_info, dtype) 
    118116 
    119117def make_kernel(model, q_vectors): 
     
    123121    return model(q_vectors) 
    124122 
    125 def get_weights(info, pars, name): 
     123def get_weights(model_info, pars, name): 
    126124    """ 
    127125    Generate the distribution for parameter *name* given the parameter values 
     
    131129    from the *pars* dictionary for parameter value and parameter dispersion. 
    132130    """ 
    133     relative = name in info['partype']['pd-rel'] 
    134     limits = info['limits'][name] 
     131    relative = name in model_info['partype']['pd-rel'] 
     132    limits = model_info['limits'][name] 
    135133    disperser = pars.get(name+'_pd_type', 'gaussian') 
    136     value = pars.get(name, info['defaults'][name]) 
     134    value = pars.get(name, model_info['defaults'][name]) 
    137135    npts = pars.get(name+'_pd_n', 0) 
    138136    width = pars.get(name+'_pd', 0.0) 
     
    173171    return kernel(fixed_pars, pd_pars, cutoff=cutoff) 
    174172 
     173def call_ER_VR(model_info, vol_pars): 
     174    """ 
     175    Return effect radius and volume ratio for the model. 
     176 
     177    *info* is either *kernel.info* for *kernel=make_kernel(model,q)* 
     178    or *model.info*. 
     179 
     180    *pars* are the parameters as expected by :func:`call_kernel`. 
     181    """ 
     182    ER = model_info.get('ER', None) 
     183    VR = model_info.get('VR', None) 
     184    value, weight = dispersion_mesh(vol_pars) 
     185 
     186    individual_radii = ER(*value) if ER else 1.0 
     187    whole, part = VR(*value) if VR else (1.0, 1.0) 
     188 
     189    effect_radius = np.sum(weight*individual_radii) / np.sum(weight) 
     190    volume_ratio = np.sum(weight*part)/np.sum(weight*whole) 
     191    return effect_radius, volume_ratio 
     192 
     193 
    175194def call_ER(info, pars): 
    176195    """ 
    177196    Call the model ER function using *pars*. 
    178  
    179197    *info* is either *model.info* if you have a loaded model, or *kernel.info* 
    180198    if you have a model kernel prepared for evaluation. 
     
    194212    """ 
    195213    Call the model VR function using *pars*. 
    196  
    197214    *info* is either *model.info* if you have a loaded model, or *kernel.info* 
    198215    if you have a model kernel prepared for evaluation. 
     
    208225        return np.sum(weight*part)/np.sum(weight*whole) 
    209226 
     227# TODO: remove call_ER, call_VR 
     228 
Note: See TracChangeset for help on using the changeset viewer.