Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/sasview_model.py

    rd533590 rbd547d0  
    6262#: set of defined models (standard and custom) 
    6363MODELS = {}  # type: Dict[str, SasviewModelType] 
     64# TODO: remove unused MODEL_BY_PATH cache once sasview no longer references it 
    6465#: custom model {path: model} mapping so we can check timestamps 
    6566MODEL_BY_PATH = {}  # type: Dict[str, SasviewModelType] 
     67#: Track modules that we have loaded so we can determine whether the model 
     68#: has changed since we last reloaded. 
     69_CACHED_MODULE = {}  # type: Dict[str, "module"] 
    6670 
    6771def find_model(modelname): 
     
    106110    Load a custom model given the model path. 
    107111    """ 
    108     model = MODEL_BY_PATH.get(path, None) 
    109     if model is not None and model.timestamp == getmtime(path): 
    110         #logger.info("Model already loaded %s", path) 
    111         return model 
    112  
    113112    #logger.info("Loading model %s", path) 
     113 
     114    # Load the kernel module.  This may already be cached by the loader, so 
     115    # only requires checking the timestamps of the dependents. 
    114116    kernel_module = custom.load_custom_kernel_module(path) 
    115     if hasattr(kernel_module, 'Model'): 
    116         model = kernel_module.Model 
     117 
     118    # Check if the module has changed since we last looked. 
     119    reloaded = kernel_module != _CACHED_MODULE.get(path, None) 
     120    _CACHED_MODULE[path] = kernel_module 
     121 
     122    # Turn the module into a model.  We need to do this in even if the 
     123    # model has already been loaded so that we can determine the model 
     124    # name and retrieve it from the MODELS cache. 
     125    model = getattr(kernel_module, 'Model', None) 
     126    if model is not None: 
    117127        # Old style models do not set the name in the class attributes, so 
    118128        # set it here; this name will be overridden when the object is created 
     
    127137        model_info = modelinfo.make_model_info(kernel_module) 
    128138        model = make_model_from_info(model_info) 
    129     model.timestamp = getmtime(path) 
    130139 
    131140    # If a model name already exists and we are loading a different model, 
     
    143152                    _previous_name, model.name, model.filename) 
    144153 
    145     MODELS[model.name] = model 
    146     MODEL_BY_PATH[path] = model 
    147     return model 
     154    # Only update the model if the module has changed 
     155    if reloaded or model.name not in MODELS: 
     156        MODELS[model.name] = model 
     157 
     158    return MODELS[model.name] 
    148159 
    149160 
     
    372383            hidden.add('background') 
    373384            self._model_info.parameters.defaults['background'] = 0. 
     385 
     386        # Update the parameter lists to exclude any hidden parameters 
     387        self.magnetic_params = tuple(pname for pname in self.magnetic_params 
     388                                     if pname not in hidden) 
     389        self.orientation_params = tuple(pname for pname in self.orientation_params 
     390                                        if pname not in hidden) 
    374391 
    375392        self._persistency_dict = {} 
     
    873890    Model = _make_standard_model('sphere') 
    874891    model = Model() 
    875     model.setParam('M0:sld', 8) 
     892    model.setParam('sld_M0', 8) 
    876893    q = np.linspace(-0.35, 0.35, 500) 
    877894    qx, qy = np.meshgrid(q, q) 
Note: See TracChangeset for help on using the changeset viewer.