Changeset 6da1d76 in sasmodels for sasmodels/sasview_model.py


Ignore:
Timestamp:
Oct 3, 2018 3:00:10 PM (6 years ago)
Author:
Paul Kienzle <pkienzle@…>
Branches:
master, core_shell_microgels, magnetic_model, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
Children:
17695aa
Parents:
0535624 (diff), a1ec908 (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 branch 'master' into ticket-1157

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/sasview_model.py

    r0535624 r6da1d76  
    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 
Note: See TracChangeset for help on using the changeset viewer.