Changeset 1e2a1ba in sasmodels for sasmodels/sasview_model.py
- Timestamp:
- Apr 5, 2016 12:06:36 PM (8 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:
- 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. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sasmodels/sasview_model.py
rea05c87 r1e2a1ba 13 13 using :func:`sasmodels.convert.convert`. 14 14 """ 15 from __future__ import print_function 15 16 16 17 import math 17 18 from copy import deepcopy 18 19 import collections 20 import traceback 21 import logging 19 22 20 23 import numpy as np 21 24 22 25 from . import core 26 from . import custom 27 from . import generate 23 28 from . import weights 24 29 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): 30 def 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 46 def 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 55 def _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 68 def _make_model_from_info(model_info): 69 """ 70 Convert *model_info* into a SasView model wrapper. 71 """ 43 72 def __init__(self, multfactor=1): 44 73 SasviewModel.__init__(self) … … 47 76 return ConstructedModel 48 77 78 49 79 class SasviewModel(object): 50 80 """ 51 81 Sasview wrapper for opencl/ctypes model. 52 82 """ 83 _model_info = {} 53 84 def __init__(self): 54 85 self._model = None … … 116 147 def __get_state__(self): 117 148 state = self.__dict__.copy() 118 state.pop('_ kernel')149 state.pop('_model') 119 150 # May need to reload model info on set state since it has pointers 120 151 # to python implementations of Iq, etc. … … 408 439 409 440 def 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') 411 445 cylinder = Cylinder() 412 446 return cylinder.evalDistribution([0.1,0.1]) 413 447 448 449 def 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 414 461 if __name__ == "__main__": 415 462 print("cylinder(0.1,0.1)=%g"%test_model())
Note: See TracChangeset
for help on using the changeset viewer.