Changeset 60335cc in sasmodels


Ignore:
Timestamp:
Sep 5, 2017 9:00:45 AM (7 years ago)
Author:
lewis
Branches:
master, core_shell_microgels, costrafo411, magnetic_model, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
Children:
31ae428
Parents:
6a5ccfb
Message:

Allow mixtures to use custom models

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/core.py

    r481ff64 r60335cc  
    1010 
    1111import os 
     12import re 
    1213from os.path import basename, dirname, join as joinpath 
    1314from glob import glob 
     
    2122from . import kernelpy 
    2223from . import kerneldll 
     24from . import custom 
    2325 
    2426if os.environ.get("SAS_OPENCL", "").lower() == "none": 
     
    3032    except Exception: 
    3133        HAVE_OPENCL = False 
     34 
     35CUSTOM_MODEL_PATH = os.environ.get('SAS_MODELPATH', "") 
     36if CUSTOM_MODEL_PATH == "": 
     37    path = joinpath(os.path.expanduser("~"), ".sasmodels", "custom_models") 
     38    if not os.path.isdir(path): 
     39        os.makedirs(path) 
     40    CUSTOM_MODEL_PATH = path 
    3241 
    3342try: 
     
    132141    *model_string* is the name of the model, or perhaps a model expression 
    133142    such as sphere*cylinder or sphere+cylinder. Use '@' for a structure 
    134     factor product, eg sphere@hardsphere. 
     143    factor product, e.g. sphere@hardsphere. Custom models can be specified by 
     144    prefixing the model name with 'custom.', e.g. 'custom.MyModel+sphere'. 
    135145 
    136146    This returns a handle to the module defining the model.  This can be 
     
    155165            product_parts = [load_model_info(part) for part in product_parts_names] 
    156166        elif len(product_parts_names) == 1: 
     167            if "custom." in product_parts_names[0]: 
     168                # Extract ModelName from "custom.ModelName" 
     169                pattern = "custom.([A-Za-z0-9_-]+)" 
     170                result = re.match(pattern, product_parts_names[0]) 
     171                if result is None: 
     172                    raise ValueError("Model name in invalid format: " + product_parts_names[0]) 
     173                model_name = result.group(1) 
     174                # Use ModelName to find the path to the custom model file 
     175                model_path = joinpath(CUSTOM_MODEL_PATH, model_name + ".py") 
     176                if not os.path.isfile(model_path): 
     177                    raise ValueError("The model file {} doesn't exist".format(model_path)) 
     178                kernel_module = custom.load_custom_kernel_module(model_path) 
     179                return modelinfo.make_model_info(kernel_module) 
     180            # Model is a core model 
    157181            kernel_module = generate.load_kernel_module(product_parts_names[0]) 
    158182            return modelinfo.make_model_info(kernel_module) 
Note: See TracChangeset for help on using the changeset viewer.