source: sasview/src/sas/qtgui/Perspectives/Fitting/ModelUtilities.py @ 5d14851a

ESS_GUIESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_opencl
Last change on this file since 5d14851a was d4dac80, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

Merge branch 'ESS_GUI' into ESS_GUI_better_batch

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