Changeset 1e2a1ba in sasmodels for sasmodels/sasview_model.py


Ignore:
Timestamp:
Apr 5, 2016 12:06:36 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:
d2bb604
Parents:
ea05c87 (diff), 4d76711 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge remote-tracking branch 'origin/master' into polydisp

Conflicts:

sasmodels/core.py
sasmodels/custom/init.py
sasmodels/direct_model.py
sasmodels/generate.py
sasmodels/kernelpy.py
sasmodels/sasview_model.py

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/sasview_model.py

    rea05c87 r1e2a1ba  
    1313using :func:`sasmodels.convert.convert`. 
    1414""" 
     15from __future__ import print_function 
    1516 
    1617import math 
    1718from copy import deepcopy 
    1819import collections 
     20import traceback 
     21import logging 
    1922 
    2023import numpy as np 
    2124 
    2225from . import core 
     26from . import custom 
     27from . import generate 
    2328from . import weights 
    2429 
    25 def standard_models(): 
    26     return [make_class(model_name) for model_name in core.list_models()] 
    27  
    28 # TODO: rename to make_class_from_name and update sasview 
    29 def make_class(model_name): 
    30     """ 
    31     Load the sasview model defined in *kernel_module*. 
    32  
    33     Returns a class that can be used directly as a sasview model.t 
    34     """ 
    35     model_info = core.load_model_info(model_name) 
    36     return make_class_from_info(model_info) 
    37  
    38 def make_class_from_file(path): 
    39     model_info = core.load_model_info_from_path(path) 
    40     return make_class_from_info(model_info) 
    41  
    42 def make_class_from_info(model_info): 
     30def load_standard_models(): 
     31    """ 
     32    Load and return the list of predefined models. 
     33 
     34    If there is an error loading a model, then a traceback is logged and the 
     35    model is not returned. 
     36    """ 
     37    models = [] 
     38    for name in core.list_models(): 
     39        try: 
     40            models.append(_make_standard_model(name)) 
     41        except: 
     42            logging.error(traceback.format_exc()) 
     43    return models 
     44 
     45 
     46def load_custom_model(path): 
     47    """ 
     48    Load a custom model given the model path. 
     49    """ 
     50    kernel_module = custom.load_custom_kernel_module(path) 
     51    model_info = generate.make_model_info(kernel_module) 
     52    return _make_model_from_info(model_info) 
     53 
     54 
     55def _make_standard_model(name): 
     56    """ 
     57    Load the sasview model defined by *name*. 
     58 
     59    *name* can be a standard model name or a path to a custom model. 
     60 
     61    Returns a class that can be used directly as a sasview model. 
     62    """ 
     63    kernel_module = generate.load_kernel_module(name) 
     64    model_info = generate.make_model_info(kernel_module) 
     65    return _make_model_from_info(model_info) 
     66 
     67 
     68def _make_model_from_info(model_info): 
     69    """ 
     70    Convert *model_info* into a SasView model wrapper. 
     71    """ 
    4372    def __init__(self, multfactor=1): 
    4473        SasviewModel.__init__(self) 
     
    4776    return ConstructedModel 
    4877 
     78 
    4979class SasviewModel(object): 
    5080    """ 
    5181    Sasview wrapper for opencl/ctypes model. 
    5282    """ 
     83    _model_info = {} 
    5384    def __init__(self): 
    5485        self._model = None 
     
    116147    def __get_state__(self): 
    117148        state = self.__dict__.copy() 
    118         state.pop('_kernel') 
     149        state.pop('_model') 
    119150        # May need to reload model info on set state since it has pointers 
    120151        # to python implementations of Iq, etc. 
     
    408439 
    409440def test_model(): 
    410     Cylinder = make_class('cylinder') 
     441    """ 
     442    Test that a sasview model (cylinder) can be run. 
     443    """ 
     444    Cylinder = _make_standard_model('cylinder') 
    411445    cylinder = Cylinder() 
    412446    return cylinder.evalDistribution([0.1,0.1]) 
    413447 
     448 
     449def test_model_list(): 
     450    """ 
     451    Make sure that all models build as sasview models. 
     452    """ 
     453    from .exception import annotate_exception 
     454    for name in core.list_models(): 
     455        try: 
     456            _make_standard_model(name) 
     457        except: 
     458            annotate_exception("when loading "+name) 
     459            raise 
     460 
    414461if __name__ == "__main__": 
    415462    print("cylinder(0.1,0.1)=%g"%test_model()) 
Note: See TracChangeset for help on using the changeset viewer.