Changeset 72a081d in sasmodels
- Timestamp:
- Mar 19, 2016 6:52:46 AM (9 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:
- f3d7abd
- Parents:
- ce43de0
- Location:
- sasmodels
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
sasmodels/compare.py
raf92b73 r72a081d 369 369 model = MultiplicationModel(P, S) 370 370 else: 371 raise ValueError(" mixture models not handled yet")371 raise ValueError("sasview mixture models not supported by compare") 372 372 else: 373 373 model = get_model(model_info['oldname']) … … 424 424 Return a model calculator using the OpenCL calculation engine. 425 425 """ 426 def builder(model_info): 427 try: 428 return core.build_model(model_info, dtype=dtype, platform="ocl") 429 except Exception as exc: 430 print(exc) 431 print("... trying again with single precision") 432 return core.build_model(model_info, dtype='single', platform="ocl") 433 if model_info['composition']: 434 composition_type, parts = model_info['composition'] 435 if composition_type == 'product': 436 P, S = [builder(p) for p in parts] 437 model = product.ProductModel(P, S) 438 else: 439 raise ValueError("mixture models not handled yet") 440 else: 441 model = builder(model_info) 426 try: 427 model = core.build_model(model_info, dtype=dtype, platform="ocl") 428 except Exception as exc: 429 print(exc) 430 print("... trying again with single precision") 431 model = core.build_model(model_info, dtype='single', platform="ocl") 442 432 calculator = DirectModel(data, model, cutoff=cutoff) 443 433 calculator.engine = "OCL%s"%DTYPE_MAP[dtype] … … 450 440 if dtype == 'quad': 451 441 dtype = 'longdouble' 452 def builder(model_info): 453 return core.build_model(model_info, dtype=dtype, platform="dll") 454 455 if model_info['composition']: 456 composition_type, parts = model_info['composition'] 457 if composition_type == 'product': 458 P, S = [builder(p) for p in parts] 459 model = product.ProductModel(P, S) 460 else: 461 raise ValueError("mixture models not handled yet") 462 else: 463 model = builder(model_info) 442 model = core.build_model(model_info, dtype=dtype, platform="dll") 464 443 calculator = DirectModel(data, model, cutoff=cutoff) 465 444 calculator.engine = "OMP%s"%DTYPE_MAP[dtype] … … 715 694 print("expected parameters: model N1 N2") 716 695 717 def _get_info(name):718 try:719 model_info = core.load_model_info(name)720 except ImportError, exc:721 print(str(exc))722 print("Use one of:\n " + models)723 sys.exit(1)724 return model_info725 726 696 name = args[0] 727 if '*' in name: 728 parts = [_get_info(k) for k in name.split('*')] 729 model_info = product.make_product_info(*parts) 730 else: 731 model_info = _get_info(name) 697 try: 698 model_info = core.load_model_info(name) 699 except ImportError, exc: 700 print(str(exc)) 701 print("Could not find model; use one of:\n " + models) 702 sys.exit(1) 732 703 733 704 invalid = [o[1:] for o in flags … … 749 720 'res' : 0.0, 750 721 'accuracy' : 'Low', 751 'cutoff' : 1e-5,722 'cutoff' : 0.0, 752 723 'seed' : -1, # default to preset 753 724 'mono' : False, -
sasmodels/core.py
rd5ba841 r72a081d 3 3 """ 4 4 5 from os.path import basename, dirname, join as joinpath 5 from os.path import basename, dirname, join as joinpath, splitext 6 6 from glob import glob 7 import imp 7 8 8 9 import numpy as np … … 11 12 from . import weights 12 13 from . import generate 13 14 # TODO: remove circular references between product and core 15 # product uses call_ER/call_VR, core uses make_product_info/ProductModel 16 #from . import product 17 from . import mixture 14 18 from . import kernelpy 15 19 from . import kerneldll … … 46 50 Load model info and build model. 47 51 """ 52 return build_model(load_model_info(model_name), **kw) 53 54 def load_model_info_from_path(path): 55 # Pull off the last .ext if it exists; there may be others 56 name = basename(splitext(path)[0]) 57 58 # Not cleaning name since don't need to be able to reload this 59 # model later 60 # Should probably turf the model from sys.modules after we are done... 61 62 # Placing the model in the 'sasmodels.custom' name space, even 63 # though it doesn't actually exist. imp.load_source doesn't seem 64 # to care. 65 kernel_module = imp.load_source('sasmodels.custom.'+name, path) 66 67 # Now that we have the module, we can load it as usual 68 model_info = generate.make_model_info(kernel_module) 69 return model_info 70 71 def load_model_info(model_name): 72 """ 73 Load a model definition given the model name. 74 75 This returns a handle to the module defining the model. This can be 76 used with functions in generate to build the docs or extract model info. 77 """ 48 78 parts = model_name.split('+') 49 79 if len(parts) > 1: 50 from .mixture import MixtureModel 51 models = [load_model(p, **kw) for p in parts] 52 return MixtureModel(models) 80 model_info_list = [load_model_info(p) for p in parts] 81 return mixture.make_mixture_info(model_info_list) 53 82 54 83 parts = model_name.split('*') 55 84 if len(parts) > 1: 85 from . import product 56 86 # Note: currently have circular reference 57 from .product import ProductModel58 87 if len(parts) > 2: 59 88 raise ValueError("use P*S to apply structure factor S to model P") 60 P, Q = [load_model(p, **kw) for p in parts] 61 return ProductModel(P, Q) 62 63 return build_model(load_model_info(model_name), **kw) 64 65 def load_model_info(model_name): 66 """ 67 Load a model definition given the model name. 68 69 This returns a handle to the module defining the model. This can be 70 used with functions in generate to build the docs or extract model info. 71 """ 89 P_info, Q_info = [load_model_info(p) for p in parts] 90 return product.make_product_info(P_info, Q_info) 91 72 92 #import sys; print "\n".join(sys.path) 73 93 __import__('sasmodels.models.'+model_name) … … 95 115 otherwise it uses the default "ocl". 96 116 """ 97 source = generate.make_source(model_info) 98 if dtype is None: 99 dtype = 'single' if model_info['single'] else 'double' 100 if callable(model_info.get('Iq', None)): 101 return kernelpy.PyModel(model_info) 117 composition = model_info.get('composition', None) 118 if composition is not None: 119 composition_type, parts = composition 120 models = [build_model(p, dtype=dtype, platform=platform) for p in parts] 121 if composition_type == 'mixture': 122 return mixture.MixtureModel(model_info, models) 123 elif composition_type == 'product': 124 from . import product 125 P, S = parts 126 return product.ProductModel(model_info, P, S) 127 else: 128 raise ValueError('unknown mixture type %s'%composition_type) 102 129 103 130 ## for debugging: … … 109 136 # open(model_info['name']+'.c','w').write(source) 110 137 # source = open(model_info['name']+'.cl','r').read() 111 138 source = generate.make_source(model_info) 139 if dtype is None: 140 dtype = 'single' if model_info['single'] else 'double' 141 if callable(model_info.get('Iq', None)): 142 return kernelpy.PyModel(model_info) 112 143 if (platform == "dll" 113 144 or not HAVE_OPENCL -
sasmodels/product.py
rd5ba841 r72a081d 68 68 69 69 class ProductModel(object): 70 def __init__(self, P, S): 70 def __init__(self, model_info, P, S): 71 self.info = model_info 71 72 self.P = P 72 73 self.S = S 73 self.info = make_product_info(P.info, S.info)74 74 75 75 def __call__(self, q_vectors): -
sasmodels/sasview_model.py
r08376e7 r72a081d 16 16 import math 17 17 from copy import deepcopy 18 import warnings19 18 import collections 20 19 … … 22 21 23 22 from . import core 23 from . import generate 24 from . import custom 24 25 25 26 def standard_models(): 26 27 return [make_class(model_name) for model_name in core.list_models()] 27 28 28 def make_class(model_name, namestyle='name'): 29 # TODO: rename to make_class_from_name and update sasview 30 def make_class(model_name): 29 31 """ 30 32 Load the sasview model defined in *kernel_module*. … … 37 39 """ 38 40 model_info = core.load_model_info(model_name) 41 return make_class_from_info(model_info) 42 43 def make_class_from_file(path): 44 model_info = core.load_model_info_from_path(path) 45 return make_class_from_info(model_info) 46 47 def make_class_from_info(model_info): 39 48 def __init__(self, multfactor=1): 40 49 SasviewModel.__init__(self) 41 50 attrs = dict(__init__=__init__, _model_info=model_info) 42 ConstructedModel = type(model_info[ namestyle], (SasviewModel,), attrs)51 ConstructedModel = type(model_info['name'], (SasviewModel,), attrs) 43 52 return ConstructedModel 44 53
Note: See TracChangeset
for help on using the changeset viewer.