def find_plugin_models(): """ Find custom models """ # List of plugin objects plugins_dir = find_plugins_dir() # Go through files in plug-in directory if not os.path.isdir(plugins_dir): msg = "SasView couldn't locate Model plugin folder %r." % plugins_dir logger.warning(msg) return {} plugin_log("looking for models in: %s" % plugins_dir) # compile_file(plugins_dir) #always recompile the folder plugin logger.info("plugin model dir: %s", plugins_dir) plugins = {} for filename in os.listdir(plugins_dir): name, ext = os.path.splitext(filename) if ext == '.py' and not name == '__init__': path = os.path.abspath(os.path.join(plugins_dir, filename)) try: model = load_custom_model(path) # TODO: add [plug-in] tag to model name in sasview_model if not model.name.startswith(PLUGIN_NAME_BASE): model.name = PLUGIN_NAME_BASE + model.name plugins[model.name] = model except Exception as exc: msg = traceback.format_exc() msg += "\nwhile accessing model in %r" % path plugin_log(msg) logger.warning("Failed to load plugin %r. See %s for details", path, PLUGIN_LOG) return plugins
def _find_models(): """ Find custom models """ # List of plugin objects directory = find_plugins_dir() # Go through files in plug-in directory if not os.path.isdir(directory): msg = "SasView couldn't locate Model plugin folder %r." % directory logger.warning(msg) return {} plugin_log("looking for models in: %s" % str(directory)) # compile_file(directory) #always recompile the folder plugin logger.info("plugin model dir: %s" % str(directory)) plugins = {} for filename in os.listdir(directory): name, ext = os.path.splitext(filename) if ext == '.py' and not name == '__init__': path = os.path.abspath(os.path.join(directory, filename)) try: model = load_custom_model(path) if not model.name.count(PLUGIN_NAME_BASE): model.name = PLUGIN_NAME_BASE + model.name plugins[model.name] = model except Exception: msg = traceback.format_exc() msg += "\nwhile accessing model in %r" % path plugin_log(msg) logger.warning("Failed to load plugin %r. See %s for details" % (path, PLUGIN_LOG)) return plugins
def _findModels(dir): """ Find custom models """ # List of plugin objects dir = find_plugins_dir() # Go through files in plug-in directory if not os.path.isdir(dir): msg = "SasView couldn't locate Model plugin folder %r." % dir logging.warning(msg) return {} plugin_log("looking for models in: %s" % str(dir)) #compile_file(dir) #always recompile the folder plugin logging.info("plugin model dir: %s" % str(dir)) plugins = {} for filename in os.listdir(dir): name, ext = os.path.splitext(filename) if ext == '.py' and not name == '__init__': path = os.path.abspath(os.path.join(dir, filename)) try: model = load_custom_model(path) model.name = "[plug-in] " + model.name plugins[model.name] = model except Exception: msg = traceback.format_exc() msg += "\nwhile accessing model in %r" % path plugin_log(msg) logging.warning( "Failed to load plugin %r. See %s for details" % (path, PLUGIN_LOG)) return plugins
def load_model(name): # type: (str) -> Optional["SasviewModel"] """ Given a model name load the Sasview shim model from sasmodels. If name starts with "[Plug-in]" then load it as a custom model from the plugins directory. This code does not go through the Sasview model manager interface since that loads all available models rather than just those needed. """ # Remember the models that are loaded so they are only loaded once. While # not strictly necessary (the models will use identical but different model # info structure) it saves a little time and memory for the usual case # where models are reused for simultaneous and batch fitting. if name in _MODEL_CACHE: return _MODEL_CACHE[name] if name.startswith(PLUGIN_NAME_BASE): name = name[len(PLUGIN_NAME_BASE):] plugins_dir = find_plugins_dir() path = os.path.abspath(os.path.join(plugins_dir, name + ".py")) #print("loading custom", path) model = load_custom_model(path) _MODEL_CACHE[name] = model elif name != "" and name.lower() != "none": #print("loading standard", name) model = _make_standard_model(name) _MODEL_CACHE[name] = model else: model = None return model
def check_model(path): """ Check that the model on the path can run. """ # TODO: fix model caching # model_test.run_one() is directly forcing a reload of the module, but # sasview_model is caching models that have already been loaded. # If the sasview load happens before the test, then the module is # reloaded out from under it, which causes the global variables in # the model function definitions to be cleared (at least in python 2.7). # To fix the proximal problem of models failing on test, perform the # run_one() tests first. To fix the deeper problem we should either # remove caching from sasmodels.sasview_model.load_custom_model() or # add caching to sasmodels.custom.load_custom_kernel_module(). Another # option is to add a runTests method to SasviewModel which runs the # test suite directly from the model info structure. Probably some # combination of options: # (1) have this function (check_model) operate on a loaded model # so that caching isn't needed in sasview_models.load_custom_model # (2) add the runTests method to SasviewModel so that tests can # be run on a loaded module. # # Also, note that the model test suite runs the equivalent of the # "try running the model" block below, and doesn't need to be run # twice. The reason for duplicating the block here is to generate # an exception that show_model_output can catch. Need to write the # runTests method so that it returns success flag as well as output # string so that the extra test is not necessary. # check the model's unit tests run from sasmodels.model_test import run_one result = run_one(path) # remove cached version of the model, if any from sasmodels import sasview_model sasview_model.MODEL_BY_PATH.pop(path, None) # try running the model Model = sasview_model.load_custom_model(path) model = Model() q = np.array([0.01, 0.1]) Iq = model.evalDistribution(q) qx, qy = np.array([0.01, 0.01]), np.array([0.1, 0.1]) Iqxy = model.evalDistribution([qx, qy]) return result
def check_model(path): """ Check that the model on the path can run. """ # try running the model from sasmodels.sasview_model import load_custom_model Model = load_custom_model(path) model = Model() q = np.array([0.01, 0.1]) Iq = model.evalDistribution(q) qx, qy = np.array([0.01, 0.01]), np.array([0.1, 0.1]) Iqxy = model.evalDistribution([qx, qy]) # check the model's unit tests run from sasmodels.model_test import run_one result = run_one(path) return result
def check_model(path): """ Check that the model on the path can run. """ # try running the model from sasmodels.sasview_model import load_custom_model Model = load_custom_model(path) model = Model() q = np.array([0.01, 0.1]) Iq = model.evalDistribution(q) qx, qy = np.array([0.01, 0.01]), np.array([0.1, 0.1]) Iqxy = model.evalDistribution([qx, qy]) result = """ Iq(%s) = %s Iqxy(%s, %s) = %s """%(q, Iq, qx, qy, Iqxy) return result