Changeset 4d76711 in sasmodels for sasmodels/core.py


Ignore:
Timestamp:
Apr 5, 2016 10:33:44 AM (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:
3a45c2c, c4e7a5f
Parents:
cd0a808
Message:

adjust interface to sasview

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/core.py

    rb7172bb r4d76711  
    77    ] 
    88 
    9  
    109from os.path import basename, dirname, join as joinpath, splitext 
    1110from glob import glob 
    12 import imp 
    1311 
    1412import numpy as np 
     
    4038            return [np.asarray(v) for v in args] 
    4139 
     40# TODO: refactor composite model support 
     41# The current load_model_info/build_model does not reuse existing model 
     42# definitions when loading a composite model, instead reloading and 
     43# rebuilding the kernel for each component model in the expression.  This 
     44# is fine in a scripting environment where the model is built when the script 
     45# starts and is thrown away when the script ends, but may not be the best 
     46# solution in a long-lived application.  This affects the following functions: 
     47# 
     48#    load_model 
     49#    load_model_info 
     50#    build_model 
    4251 
    4352def list_models(): 
     
    6473    return build_model(load_model_info(model_name), **kw) 
    6574 
    66 def load_model_info_from_path(path): 
    67     # Pull off the last .ext if it exists; there may be others 
    68     name = basename(splitext(path)[0]) 
    69  
    70     # Not cleaning name since don't need to be able to reload this 
    71     # model later 
    72     # Should probably turf the model from sys.modules after we are done... 
    73  
    74     # Placing the model in the 'sasmodels.custom' name space, even 
    75     # though it doesn't actually exist.  imp.load_source doesn't seem 
    76     # to care. 
    77     kernel_module = imp.load_source('sasmodels.custom.'+name, path) 
    78  
    79     # Now that we have the module, we can load it as usual 
    80     model_info = generate.make_model_info(kernel_module) 
    81     return model_info 
    8275 
    8376def load_model_info(model_name): 
     
    10295        return product.make_product_info(P_info, Q_info) 
    10396 
    104     #import sys; print "\n".join(sys.path) 
    105     __import__('sasmodels.models.'+model_name) 
    106     kernel_module = getattr(models, model_name, None) 
     97    kernel_module = generate.load_kernel_module(model_name) 
    10798    return generate.make_model_info(kernel_module) 
    10899 
     
    179170 
    180171 
    181 def make_kernel(model, q_vectors): 
    182     """ 
    183     Return a computation kernel from the model definition and the q input. 
    184     """ 
    185     return model(q_vectors) 
    186  
    187172def get_weights(model_info, pars, name): 
    188173    """ 
     
    220205def call_kernel(kernel, pars, cutoff=0, mono=False): 
    221206    """ 
    222     Call *kernel* returned from :func:`make_kernel` with parameters *pars*. 
     207    Call *kernel* returned from *model.make_kernel* with parameters *pars*. 
    223208 
    224209    *cutoff* is the limiting value for the product of dispersion weights used 
     
    228213    with an error of about 1%, which is usually less than the measurement 
    229214    uncertainty. 
     215 
     216    *mono* is True if polydispersity should be set to none on all parameters. 
    230217    """ 
    231218    fixed_pars = [pars.get(name, kernel.info['defaults'][name]) 
     
    259246 
    260247 
    261 def call_ER(info, pars): 
    262     """ 
    263     Call the model ER function using *pars*. 
    264     *info* is either *model.info* if you have a loaded model, or *kernel.info* 
    265     if you have a model kernel prepared for evaluation. 
    266     """ 
    267     ER = info.get('ER', None) 
     248def call_ER(model_info, values): 
     249    """ 
     250    Call the model ER function using *values*. *model_info* is either 
     251    *model.info* if you have a loaded model, or *kernel.info* if you 
     252    have a model kernel prepared for evaluation. 
     253    """ 
     254    ER = model_info.get('ER', None) 
    268255    if ER is None: 
    269256        return 1.0 
    270257    else: 
    271         vol_pars = [get_weights(info, pars, name) 
    272                     for name in info['partype']['volume']] 
     258        vol_pars = [get_weights(model_info, values, name) 
     259                    for name in model_info['partype']['volume']] 
    273260        value, weight = dispersion_mesh(vol_pars) 
    274261        individual_radii = ER(*value) 
     
    276263        return np.sum(weight*individual_radii) / np.sum(weight) 
    277264 
    278 def call_VR(info, pars): 
     265def call_VR(model_info, values): 
    279266    """ 
    280267    Call the model VR function using *pars*. 
     
    282269    if you have a model kernel prepared for evaluation. 
    283270    """ 
    284     VR = info.get('VR', None) 
     271    VR = model_info.get('VR', None) 
    285272    if VR is None: 
    286273        return 1.0 
    287274    else: 
    288         vol_pars = [get_weights(info, pars, name) 
    289                     for name in info['partype']['volume']] 
     275        vol_pars = [get_weights(model_info, values, name) 
     276                    for name in model_info['partype']['volume']] 
    290277        value, weight = dispersion_mesh(vol_pars) 
    291278        whole, part = VR(*value) 
Note: See TracChangeset for help on using the changeset viewer.