source: sasview/src/sas/sasgui/perspectives/fitting/models.py @ ae69c690

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalcmagnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since ae69c690 was b682c6a, checked in by Paul Kienzle <pkienzle@…>, 7 years ago

Merge branch 'master' into 4_1_issues

  • Property mode set to 100644
File size: 14.2 KB
Line 
1"""
2    Utilities to manage models
3"""
4from __future__ import print_function
5
6import traceback
7import os
8import sys
9import os.path
10# Time is needed by the log method
11import time
12import datetime
13import logging
14import py_compile
15import shutil
16# Explicitly import from the pluginmodel module so that py2exe
17# places it in the distribution. The Model1DPlugin class is used
18# as the base class of plug-in models.
19from sas.sascalc.fit.pluginmodel import Model1DPlugin
20from sas.sasgui.guiframe.CategoryInstaller import CategoryInstaller
21from sasmodels.sasview_model import load_custom_model, load_standard_models
22from sas.sasgui.perspectives.fitting.fitpage import CUSTOM_MODEL
23
24logger = logging.getLogger(__name__)
25
26
27PLUGIN_DIR = 'plugin_models'
28PLUGIN_LOG = os.path.join(os.path.expanduser("~"), '.sasview', PLUGIN_DIR,
29                          "plugins.log")
30PLUGIN_NAME_BASE = '[plug-in] '
31
32def get_model_python_path():
33    """
34    Returns the python path for a model
35    """
36    return os.path.dirname(__file__)
37
38
39def plugin_log(message):
40    """
41    Log a message in a file located in the user's home directory
42    """
43    out = open(PLUGIN_LOG, 'a')
44    now = time.time()
45    stamp = datetime.datetime.fromtimestamp(now).strftime('%Y-%m-%d %H:%M:%S')
46    out.write("%s: %s\n" % (stamp, message))
47    out.close()
48
49
50def _check_plugin(model, name):
51    """
52    Do some checking before model adding plugins in the list
53
54    :param model: class model to add into the plugin list
55    :param name:name of the module plugin
56
57    :return model: model if valid model or None if not valid
58
59    """
60    #Check if the plugin is of type Model1DPlugin
61    if not issubclass(model, Model1DPlugin):
62        msg = "Plugin %s must be of type Model1DPlugin \n" % str(name)
63        plugin_log(msg)
64        return None
65    if model.__name__ != "Model":
66        msg = "Plugin %s class name must be Model \n" % str(name)
67        plugin_log(msg)
68        return None
69    try:
70        new_instance = model()
71    except:
72        msg = "Plugin %s error in __init__ \n\t: %s %s\n" % (str(name),
73                                                             str(sys.exc_type),
74                                                             sys.exc_info()[1])
75        plugin_log(msg)
76        return None
77
78    if hasattr(new_instance, "function"):
79        try:
80            value = new_instance.function()
81        except:
82            msg = "Plugin %s: error writing function \n\t :%s %s\n " % \
83                    (str(name), str(sys.exc_type), sys.exc_info()[1])
84            plugin_log(msg)
85            return None
86    else:
87        msg = "Plugin  %s needs a method called function \n" % str(name)
88        plugin_log(msg)
89        return None
90    return model
91
92
93def find_plugins_dir():
94    """
95    Find path of the plugins directory.
96    The plugin directory is located in the user's home directory.
97    """
98    dir = os.path.join(os.path.expanduser("~"), '.sasview', PLUGIN_DIR)
99
100    # If the plugin directory doesn't exist, create it
101    if not os.path.isdir(dir):
102        os.makedirs(dir)
103
104    # Find paths needed
105    try:
106        # For source
107        if os.path.isdir(os.path.dirname(__file__)):
108            p_dir = os.path.join(os.path.dirname(__file__), PLUGIN_DIR)
109        else:
110            raise
111    except:
112        # Check for data path next to exe/zip file.
113        #Look for maximum n_dir up of the current dir to find plugins dir
114        n_dir = 12
115        p_dir = None
116        f_dir = os.path.join(os.path.dirname(__file__))
117        for i in range(n_dir):
118            if i > 1:
119                f_dir, _ = os.path.split(f_dir)
120            plugin_path = os.path.join(f_dir, PLUGIN_DIR)
121            if os.path.isdir(plugin_path):
122                p_dir = plugin_path
123                break
124        if not p_dir:
125            raise
126    # Place example user models as needed
127    if os.path.isdir(p_dir):
128        for file in os.listdir(p_dir):
129            file_path = os.path.join(p_dir, file)
130            if os.path.isfile(file_path):
131                if file.split(".")[-1] == 'py' and\
132                    file.split(".")[0] != '__init__':
133                    if not os.path.isfile(os.path.join(dir, file)):
134                        shutil.copy(file_path, dir)
135
136    return dir
137
138
139class ReportProblem:
140    """
141    Class to check for problems with specific values
142    """
143    def __nonzero__(self):
144        type, value, tb = sys.exc_info()
145        if type is not None and issubclass(type, py_compile.PyCompileError):
146            print("Problem with", repr(value))
147            raise type, value, tb
148        return 1
149
150report_problem = ReportProblem()
151
152
153def compile_file(dir):
154    """
155    Compile a py file
156    """
157    try:
158        import compileall
159        compileall.compile_dir(dir=dir, ddir=dir, force=0,
160                               quiet=report_problem)
161    except:
162        return sys.exc_info()[1]
163    return None
164
165
166def _find_models():
167    """
168    Find custom models
169    """
170    # List of plugin objects
171    directory = find_plugins_dir()
172    # Go through files in plug-in directory
173    if not os.path.isdir(directory):
174        msg = "SasView couldn't locate Model plugin folder %r." % directory
175        logger.warning(msg)
176        return {}
177
178    plugin_log("looking for models in: %s" % str(directory))
179    # compile_file(directory)  #always recompile the folder plugin
180    logger.info("plugin model dir: %s" % str(directory))
181
182    plugins = {}
183    for filename in os.listdir(directory):
184        name, ext = os.path.splitext(filename)
185        if ext == '.py' and not name == '__init__':
186            path = os.path.abspath(os.path.join(directory, filename))
187            try:
188                model = load_custom_model(path)
189                if not model.name.count(PLUGIN_NAME_BASE):
190                    model.name = PLUGIN_NAME_BASE + model.name
191                plugins[model.name] = model
192            except Exception:
193                msg = traceback.format_exc()
194                msg += "\nwhile accessing model in %r" % path
195                plugin_log(msg)
196                logger.warning("Failed to load plugin %r. See %s for details"
197                               % (path, PLUGIN_LOG))
198
199    return plugins
200
201
202class ModelList(object):
203    """
204    Contains dictionary of model and their type
205    """
206    def __init__(self):
207        """
208        """
209        self.mydict = {}
210
211    def set_list(self, name, mylist):
212        """
213        :param name: the type of the list
214        :param mylist: the list to add
215
216        """
217        if name not in self.mydict.keys():
218            self.reset_list(name, mylist)
219
220    def reset_list(self, name, mylist):
221        """
222        :param name: the type of the list
223        :param mylist: the list to add
224        """
225        self.mydict[name] = mylist
226
227    def get_list(self):
228        """
229        return all the list stored in a dictionary object
230        """
231        return self.mydict
232
233
234class ModelManagerBase:
235    """
236    Base class for the model manager
237    """
238    ## external dict for models
239    model_combobox = ModelList()
240    ## Dictionary of form factor models
241    form_factor_dict = {}
242    ## dictionary of structure factor models
243    struct_factor_dict = {}
244    ##list of structure factors
245    struct_list = []
246    ##list of model allowing multiplication by a structure factor
247    multiplication_factor = []
248    ##list of multifunctional shapes (i.e. that have user defined number of levels
249    multi_func_list = []
250    ## list of added models -- currently python models found in the plugin dir.
251    plugins = []
252    ## Event owner (guiframe)
253    event_owner = None
254    last_time_dir_modified = 0
255
256    def __init__(self):
257        self.model_dictionary = {}
258        self.stored_plugins = {}
259        self._getModelList()
260
261    def findModels(self):
262        """
263        find  plugin model in directory of plugin .recompile all file
264        in the directory if file were modified
265        """
266        temp = {}
267        if self.is_changed():
268            temp =  _find_models()
269            self.last_time_dir_modified = time.time()
270            return temp
271        logger.info("plugin model : %s" % str(temp))
272        return temp
273
274    def _getModelList(self):
275        """
276        List of models we want to make available by default
277        for this application
278
279        :return: the next free event ID following the new menu events
280
281        """
282
283        # regular model names only
284        self.model_name_list = []
285
286        #Build list automagically from sasmodels package
287        for model in load_standard_models():
288            self.model_dictionary[model.name] = model
289            if model.is_structure_factor:
290                self.struct_list.append(model)
291            if model.is_form_factor:
292                self.multiplication_factor.append(model)
293            if model.is_multiplicity_model:
294                self.multi_func_list.append(model)
295            else:
296                self.model_name_list.append(model.name)
297
298        #Looking for plugins
299        self.stored_plugins = self.findModels()
300        self.plugins = self.stored_plugins.values()
301        for name, plug in self.stored_plugins.iteritems():
302            self.model_dictionary[name] = plug
303
304        self._get_multifunc_models()
305
306        return 0
307
308    def is_changed(self):
309        """
310        check the last time the plugin dir has changed and return true
311        is the directory was modified else return false
312        """
313        is_modified = False
314        plugin_dir = find_plugins_dir()
315        if os.path.isdir(plugin_dir):
316            temp = os.path.getmtime(plugin_dir)
317            if  self.last_time_dir_modified < temp:
318                is_modified = True
319                self.last_time_dir_modified = temp
320
321        return is_modified
322
323    def update(self):
324        """
325        return a dictionary of model if
326        new models were added else return empty dictionary
327        """
328        self.plugins = []
329        new_plugins = self.findModels()
330        if new_plugins:
331            for name, plug in  new_plugins.items():
332                self.stored_plugins[name] = plug
333                self.plugins.append(plug)
334                self.model_dictionary[name] = plug
335            self.model_combobox.set_list(CUSTOM_MODEL, self.plugins)
336            return self.model_combobox.get_list()
337        else:
338            return {}
339
340    def plugins_reset(self):
341        """
342        return a dictionary of model
343        """
344        self.plugins = []
345        new_plugins = _find_models()
346        for name, plug in  new_plugins.iteritems():
347            for stored_name, stored_plug in self.stored_plugins.iteritems():
348                if name == stored_name:
349                    del self.stored_plugins[name]
350                    del self.model_dictionary[name]
351                    break
352            self.stored_plugins[name] = plug
353            self.plugins.append(plug)
354            self.model_dictionary[name] = plug
355
356        self.model_combobox.reset_list("Plugin Models", self.plugins)
357        return self.model_combobox.get_list()
358
359    def _on_model(self, evt):
360        """
361        React to a model menu event
362
363        :param event: wx menu event
364
365        """
366        if int(evt.GetId()) in self.form_factor_dict.keys():
367            from sasmodels.sasview_model import MultiplicationModel
368            self.model_dictionary[MultiplicationModel.__name__] = MultiplicationModel
369            model1, model2 = self.form_factor_dict[int(evt.GetId())]
370            model = MultiplicationModel(model1, model2)
371        else:
372            model = self.struct_factor_dict[str(evt.GetId())]()
373
374
375    def _get_multifunc_models(self):
376        """
377        Get the multifunctional models
378        """
379        items = [item for item in self.plugins if item.is_multiplicity_model]
380        self.multi_func_list = items
381
382    def get_model_list(self):
383        """
384        return dictionary of models for fitpanel use
385
386        """
387        ## Model_list now only contains attribute lists not category list.
388        ## Eventually this should be in one master list -- read in category
389        ## list then pull those models that exist and get attributes then add
390        ## to list ..and if model does not exist remove from list as now
391        ## and update json file.
392        ##
393        ## -PDB   April 26, 2014
394
395#        self.model_combobox.set_list("Shapes", self.shape_list)
396#        self.model_combobox.set_list("Shape-Independent",
397#                                     self.shape_indep_list)
398        self.model_combobox.set_list("Structure Factors", self.struct_list)
399        self.model_combobox.set_list("Plugin Models", self.plugins)
400        self.model_combobox.set_list("P(Q)*S(Q)", self.multiplication_factor)
401        self.model_combobox.set_list("multiplication",
402                                     self.multiplication_factor)
403        self.model_combobox.set_list("Multi-Functions", self.multi_func_list)
404        return self.model_combobox.get_list()
405
406    def get_model_name_list(self):
407        """
408        return regular model name list
409        """
410        return self.model_name_list
411
412    def get_model_dictionary(self):
413        """
414        return dictionary linking model names to objects
415        """
416        return self.model_dictionary
417
418
419class ModelManager(object):
420    """
421    implement model
422    """
423    __modelmanager = ModelManagerBase()
424    cat_model_list = [__modelmanager.model_dictionary[model_name] for model_name \
425                      in __modelmanager.model_dictionary.keys() \
426                      if model_name not in __modelmanager.stored_plugins.keys()]
427
428    CategoryInstaller.check_install(model_list=cat_model_list)
429    def findModels(self):
430        return self.__modelmanager.findModels()
431
432    def _getModelList(self):
433        return self.__modelmanager._getModelList()
434
435    def is_changed(self):
436        return self.__modelmanager.is_changed()
437
438    def update(self):
439        return self.__modelmanager.update()
440
441    def plugins_reset(self):
442        return self.__modelmanager.plugins_reset()
443
444    def populate_menu(self, modelmenu, event_owner):
445        return self.__modelmanager.populate_menu(modelmenu, event_owner)
446
447    def _on_model(self, evt):
448        return self.__modelmanager._on_model(evt)
449
450    def _get_multifunc_models(self):
451        return self.__modelmanager._get_multifunc_models()
452
453    def get_model_list(self):
454        return self.__modelmanager.get_model_list()
455
456    def get_model_name_list(self):
457        return self.__modelmanager.get_model_name_list()
458
459    def get_model_dictionary(self):
460        return self.__modelmanager.get_model_dictionary()
Note: See TracBrowser for help on using the repository browser.