Changeset dd16e07 in sasmodels


Ignore:
Timestamp:
Sep 25, 2018 7:15:47 PM (6 years ago)
Author:
GitHub <noreply@…>
Branches:
master, core_shell_microgels, magnetic_model, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
Children:
284bdd4, da7b26b
Parents:
2015f02 (diff), 91bd550 (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.
git-author:
Paul Butler <butlerpd@…> (09/25/18 19:15:47)
git-committer:
GitHub <noreply@…> (09/25/18 19:15:47)
Message:

Merge pull request #80 from SasView?/ticket-1173-cache-loadinfo

Ticket 1173: cache load info

Closes #1173

Files:
4 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/custom/__init__.py

    r0f48f1e r91bd550  
    3737        return module 
    3838 
     39_MODULE_CACHE = {} 
     40_MODULE_DEPENDS = {} 
     41_MODULE_DEPENDS_STACK = [] 
    3942def load_custom_kernel_module(path): 
    4043    """load SAS kernel from *path* as *sasmodels.custom.modelname*""" 
    4144    # Pull off the last .ext if it exists; there may be others 
    4245    name = basename(splitext(path)[0]) 
    43     # Placing the model in the 'sasmodels.custom' name space. 
    44     kernel_module = load_module_from_path('sasmodels.custom.'+name, 
    45                                           os.path.expanduser(path)) 
    46     return kernel_module 
     46    path = os.path.expanduser(path) 
     47 
     48    # reload module if necessary 
     49    if need_reload(path): 
     50        # Push to the next dependency level 
     51        _MODULE_DEPENDS_STACK.append(path) 
     52        _MODULE_DEPENDS[path] = set([path]) 
     53 
     54        # Load module into the 'sasmodels.custom' name space. 
     55        # If this triggers any submodule loads then they will be added 
     56        # as dependencies below when _MODULE_DEPENDS_STACK is not empty. 
     57        module = load_module_from_path('sasmodels.custom.'+name, path) 
     58 
     59        # Pop the dependency level 
     60        _MODULE_DEPENDS_STACK.pop() 
     61 
     62        # TODO: include external C code in the dependencies 
     63        # If we had the model info structure we could do the following: 
     64        #    _MODEL_DEPENDS[path].extend(generate.model_sources(info)) 
     65        # but at this point all we have is the module.  Don't want to 
     66        # repeat the logic in modelinfo.make_model_info. 
     67 
     68        # Cache the module with the newest timestamp 
     69        timestamp = max(os.path.getmtime(f) for f in _MODULE_DEPENDS[path]) 
     70        _MODULE_CACHE[path] = module, timestamp 
     71 
     72        #print("loading", os.path.basename(path), _MODULE_CACHE[path][1], 
     73        #    [os.path.basename(p) for p in _MODULE_DEPENDS[path]]) 
     74 
     75    if _MODULE_DEPENDS_STACK: 
     76        # Add child and all its dependence to the parent module 
     77        working_on = _MODULE_DEPENDS_STACK[-1] 
     78        _MODULE_DEPENDS[working_on].update(_MODULE_DEPENDS[path]) 
     79 
     80    return _MODULE_CACHE[path][0] 
     81 
     82def need_reload(path): 
     83    # TODO: fails if a dependency has a modification time in the future 
     84    # If the newest dependency has a time stamp in the future, then this 
     85    # will be recorded as the cached time.  When a second dependency 
     86    # is updated to the current time stamp, it will still be considered 
     87    # older than the current build and the reload will not be triggered. 
     88    # Could instead treat all future times as 0 here and in the code above 
     89    # which records the newest timestamp.  This will force a reload when 
     90    # the future time is reached, but other than that should perform 
     91    # correctly.  Probably not worth the extra code... 
     92    _, cache_time = _MODULE_CACHE.get(path, (None, -1)) 
     93    depends = _MODULE_DEPENDS.get(path, [path]) 
     94    return any(cache_time < os.path.getmtime(p) for p in depends) 
  • sasmodels/kernelpy.py

    r108e70e r91bd550  
    3737        self.info = model_info 
    3838        self.dtype = np.dtype('d') 
    39         logger.info("load python model " + self.info.name) 
    4039 
    4140    def make_kernel(self, q_vectors): 
  • sasmodels/sasview_model.py

    rd533590 r839fd68  
    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] 
     
    106107    Load a custom model given the model path. 
    107108    """ 
    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  
    113109    #logger.info("Loading model %s", path) 
    114110    kernel_module = custom.load_custom_kernel_module(path) 
     
    127123        model_info = modelinfo.make_model_info(kernel_module) 
    128124        model = make_model_from_info(model_info) 
    129     model.timestamp = getmtime(path) 
    130125 
    131126    # If a model name already exists and we are loading a different model, 
     
    144139 
    145140    MODELS[model.name] = model 
    146     MODEL_BY_PATH[path] = model 
    147141    return model 
    148142 
  • doc/guide/plugin.rst

    rf796469 r2015f02  
    423423calculations, but instead rely on numerical integration to compute the 
    424424appropriately smeared pattern. 
     425 
     426Each .py file also contains a function:: 
     427 
     428        def random(): 
     429        ... 
     430         
     431This function provides a model-specific random parameter set which shows model  
     432features in the USANS to SANS range.  For example, core-shell sphere sets the  
     433outer radius of the sphere logarithmically in `[20, 20,000]`, which sets the Q  
     434value for the transition from flat to falling.  It then uses a beta distribution  
     435to set the percentage of the shape which is shell, giving a preference for very  
     436thin or very thick shells (but never 0% or 100%).  Using `-sets=10` in sascomp  
     437should show a reasonable variety of curves over the default sascomp q range.   
     438The parameter set is returned as a dictionary of `{parameter: value, ...}`.   
     439Any model parameters not included in the dictionary will default according to  
     440the code in the `_randomize_one()` function from sasmodels/compare.py. 
    425441 
    426442Python Models 
Note: See TracChangeset for help on using the changeset viewer.