source: sasview/theoryview/perspectives/theory/models.py @ 0b12abb5

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 0b12abb5 was 74755ff, checked in by Gervaise Alina <gervyh@…>, 15 years ago

working on documentation theory view

  • Property mode set to 100644
File size: 12.1 KB
RevLine 
[0277d084]1
[74755ff]2"""
3This module collects models from sans.models packages and orders theses models
4by categories that GUI application understands and uses.
[0277d084]5
[74755ff]6"""
[0277d084]7import wx
8import wx.lib.newevent
9import imp
10import os,sys,math
11import os.path
12
13(ModelEvent, EVT_MODEL) = wx.lib.newevent.NewEvent()
14from sans.guicomm.events import StatusEvent 
15# Time is needed by the log method
16import time
17
18# Explicitly import from the pluginmodel module so that py2exe
19# places it in the distribution. The Model1DPlugin class is used
20# as the base class of plug-in models.
21from sans.models.pluginmodel import Model1DPlugin
22   
23def log(message):
[74755ff]24    """
25    """
[0277d084]26    out = open("plugins.log", 'a')
27    out.write("%10g%s\n" % (time.clock(), message))
28    out.close()
29
30def findModels():
[74755ff]31    """
32    """
[0277d084]33    log("looking for models in: %s/plugins" % os.getcwd())
34    if os.path.isdir('plugins'):
35        return _findModels('plugins')
36    return []
37   
38def _check_plugin(model, name):
39    """
[74755ff]40    Do some checking before model adding plugins in the list
41   
42    :param model: class model to add into the plugin list
43    :param name: name of the module plugin
44   
45    :return model: model if valid model or None if not valid
46   
[0277d084]47    """
48    #Check is the plugin is of type Model1DPlugin
49    if not issubclass(model, Model1DPlugin):
50        msg= "Plugin %s must be of type Model1DPlugin \n"%str(name)
51        log(msg)
52        return None
53    if model.__name__!="Model":
54        msg= "Plugin %s class name must be Model \n"%str(name)
55        log(msg)
56        return None
57    try:
58        new_instance= model()
59    except:
60        msg="Plugin %s error in __init__ \n\t: %s %s\n"%(str(name),
61                                    str(sys.exc_type),sys.exc_value)
62        log(msg)
63        return None
64   
65    new_instance= model() 
66    if hasattr(new_instance,"function"):
67        try:
68           value=new_instance.function()
69        except:
70           msg="Plugin %s: error writing function \n\t :%s %s\n "%(str(name),
71                                    str(sys.exc_type),sys.exc_value)
72           log(msg)
73           return None
74    else:
75       msg="Plugin  %s needs a method called function \n"%str(name)
76       log(msg)
77       return None
78    return model
79 
80 
81def _findModels(dir):
[74755ff]82    """
83    """
[0277d084]84    # List of plugin objects
85    plugins = []
86    # Go through files in plug-in directory
87    try:
88        list = os.listdir(dir)
89        for item in list:
90            toks = os.path.splitext(os.path.basename(item))
91            if toks[1]=='.py' and not toks[0]=='__init__':
92                name = toks[0]
93           
94                path = [os.path.abspath(dir)]
95                file = None
96                try:
97                    (file, path, info) = imp.find_module(name, path)
98                    module = imp.load_module( name, file, item, info )
99                    if hasattr(module, "Model"):
100                        try:
101                            if _check_plugin(module.Model, name)!=None:
102                                plugins.append(module.Model)
103                        except:
104                            msg="Error accessing Model"
105                            msg+="in %s\n  %s %s\n" % (name,
106                                    str(sys.exc_type), sys.exc_value)
107                            log(msg)
108                except:
109                    msg="Error accessing Model"
110                    msg +=" in %s\n  %s %s \n" %(name,
111                                    str(sys.exc_type), sys.exc_value)
112                    log(msg)
113                finally:
114             
115                    if not file==None:
116                        file.close()
117    except:
118        # Don't deal with bad plug-in imports. Just skip.
119        pass
120    return plugins
121
122class ModelList(object):
123    """
[74755ff]124    Contains dictionary of model and their type
125   
[0277d084]126    """
127    def __init__(self):
128        self.mydict={}
129       
130    def set_list(self, name, mylist):
131        """
[74755ff]132       
133        :param name: the type of the list
134        :param mylist: the list to add
135       
[0277d084]136        """
137        if name not in self.mydict.keys():
138            self.mydict[name] = mylist
139           
140           
141    def get_list(self):
142        """
[74755ff]143        return all the list stored in a dictionary object
[0277d084]144        """
145        return self.mydict
146       
147class ModelManager:
[74755ff]148    """
149    ModelManager collected model classes object of available into a
150    dictionary of models' names and models' classes.
151   
152    """
[0277d084]153    ## external dict for models
154    model_combobox = ModelList()
155    ## Dictionary of form models
156    form_factor_dict = {}
157    ## dictionary of other
158    struct_factor_dict = {}
159    ##list of form factors
160    shape_list =[]
161    ## independent shape model list
162    shape_indep_list = []
163    ##list of structure factors
164    struct_list= []
165    ##list of model allowing multiplication
166    multiplication_factor=[]
167    ## list of added models
168    plugins=[]
169    ## Event owner (guiframe)
170    event_owner = None
171   
172    def _getModelList(self):
173        """
[74755ff]174        Fill up lists of models available by default
175        for the current application
176   
177        :return: the next free event ID following the new menu events
[0277d084]178       
179        """
180        ## form factor
181        from sans.models.SphereModel import SphereModel
182        self.shape_list.append(SphereModel)
183        self.multiplication_factor.append(SphereModel)
184       
[ce07fa8]185        from sans.models.FuzzySphereModel import FuzzySphereModel
186        self.shape_list.append(FuzzySphereModel)
187        self.multiplication_factor.append(FuzzySphereModel)
188           
[0277d084]189        from sans.models.CoreShellModel import CoreShellModel
190        self.shape_list.append(CoreShellModel)
191        self.multiplication_factor.append(CoreShellModel)
[ce07fa8]192
193        from sans.models.CoreFourShellModel import CoreFourShellModel
194        self.shape_list.append(CoreFourShellModel)
195        self.multiplication_factor.append(CoreFourShellModel)
[0277d084]196       
197        from sans.models.VesicleModel import VesicleModel
198        self.shape_list.append(VesicleModel)
199        self.multiplication_factor.append(VesicleModel)
200       
201        from sans.models.MultiShellModel import MultiShellModel
202        self.shape_list.append(MultiShellModel)
203        self.multiplication_factor.append(MultiShellModel)
204       
205        from sans.models.BinaryHSModel import BinaryHSModel
206        self.shape_list.append(BinaryHSModel)
207       
208        from sans.models.CylinderModel import CylinderModel
209        self.shape_list.append(CylinderModel)
210        self.multiplication_factor.append(CylinderModel)
211       
212        from sans.models.CoreShellCylinderModel import CoreShellCylinderModel
213        self.shape_list.append(CoreShellCylinderModel)
214        self.multiplication_factor.append(CoreShellCylinderModel)
215       
216        from sans.models.HollowCylinderModel import HollowCylinderModel
217        self.shape_list.append(HollowCylinderModel)
218        self.multiplication_factor.append(HollowCylinderModel)
219             
220        from sans.models.FlexibleCylinderModel import FlexibleCylinderModel
221        self.shape_list.append(FlexibleCylinderModel)
222
[ce07fa8]223        from sans.models.FlexCylEllipXModel import FlexCylEllipXModel
224        self.shape_list.append(FlexCylEllipXModel)
[0277d084]225       
226        from sans.models.StackedDisksModel import StackedDisksModel
227        self.shape_list.append(StackedDisksModel)
228        self.multiplication_factor.append(StackedDisksModel)
229       
230        from sans.models.ParallelepipedModel import ParallelepipedModel
231        self.shape_list.append(ParallelepipedModel)
232        self.multiplication_factor.append(ParallelepipedModel)
233       
234        from sans.models.EllipticalCylinderModel import EllipticalCylinderModel
235        self.shape_list.append(EllipticalCylinderModel)
236        self.multiplication_factor.append(EllipticalCylinderModel)
237               
238        from sans.models.EllipsoidModel import EllipsoidModel
239        self.shape_list.append(EllipsoidModel)
240        self.multiplication_factor.append(EllipsoidModel)
241     
242        from sans.models.CoreShellEllipsoidModel import CoreShellEllipsoidModel
243        self.shape_list.append(CoreShellEllipsoidModel)
244        self.multiplication_factor.append(CoreShellEllipsoidModel)
245         
246        from sans.models.TriaxialEllipsoidModel import TriaxialEllipsoidModel
247        self.shape_list.append(TriaxialEllipsoidModel)
248        self.multiplication_factor.append(TriaxialEllipsoidModel)
249       
250        from sans.models.LamellarModel import LamellarModel
251        self.shape_list.append(LamellarModel)
252       
253        from sans.models.LamellarFFHGModel import LamellarFFHGModel
254        self.shape_list.append(LamellarFFHGModel)
255       
256        from sans.models.LamellarPSModel import LamellarPSModel
257        self.shape_list.append(LamellarPSModel)
258     
259        from sans.models.LamellarPSHGModel import LamellarPSHGModel
260        self.shape_list.append(LamellarPSHGModel)
261     
262        ## Structure factor
263        from sans.models.SquareWellStructure import SquareWellStructure
264        self.struct_list.append(SquareWellStructure)
265       
266        from sans.models.HardsphereStructure import HardsphereStructure
267        self.struct_list.append(HardsphereStructure)
268         
269        from sans.models.StickyHSStructure import StickyHSStructure
270        self.struct_list.append(StickyHSStructure)
271       
272        from sans.models.HayterMSAStructure import HayterMSAStructure
273        self.struct_list.append(HayterMSAStructure)
274       
275       
276        ##shape-independent models
[ce07fa8]277           
278        from sans.models.PowerLawAbsModel import PowerLawAbsModel
279        self.shape_indep_list.append( PowerLawAbsModel )
280       
[0277d084]281        from sans.models.BEPolyelectrolyte import BEPolyelectrolyte
282        self.shape_indep_list.append(BEPolyelectrolyte )
283        self.form_factor_dict[str(wx.NewId())] =  [SphereModel]
[ce07fa8]284
[0277d084]285        from sans.models.DABModel import DABModel
286        self.shape_indep_list.append(DABModel )
287       
[ce07fa8]288        from sans.models.DebyeModel import DebyeModel
289        self.shape_indep_list.append(DebyeModel )
290       
[0277d084]291        from sans.models.GuinierModel import GuinierModel
292        self.shape_indep_list.append(GuinierModel )
293       
[ce07fa8]294        from sans.models.FractalModel import FractalModel
295        self.shape_indep_list.append(FractalModel )
[0277d084]296       
[ce07fa8]297        from sans.models.LorentzModel import LorentzModel
298        self.shape_indep_list.append( LorentzModel) 
[0277d084]299       
300        from sans.models.PeakGaussModel import PeakGaussModel
301        self.shape_indep_list.append(PeakGaussModel)
302       
303        from sans.models.PeakLorentzModel import PeakLorentzModel
304        self.shape_indep_list.append(PeakLorentzModel)
305       
[ce07fa8]306        from sans.models.Poly_GaussCoil import Poly_GaussCoil
307        self.shape_indep_list.append(Poly_GaussCoil)
308                 
309        from sans.models.PorodModel import PorodModel
310        self.shape_indep_list.append(PorodModel )
[0277d084]311       
[ce07fa8]312        #FractalModel (a c-model)will be used.
313        #from sans.models.FractalAbsModel import FractalAbsModel
314        #self.shape_indep_list.append(FractalAbsModel)
[0277d084]315       
316        from sans.models.TeubnerStreyModel import TeubnerStreyModel
317        self.shape_indep_list.append(TeubnerStreyModel )
318       
319        from sans.models.LineModel import LineModel
320        self.shape_indep_list.append(LineModel)
321   
322        #Looking for plugins
323        self.plugins = findModels()
324       
325        return 0
326
327    def get_model_list(self):   
[74755ff]328        """
329        :return: dictionary of models for fitpanel use
330       
331        """
[0277d084]332        self._getModelList()
333        self.model_combobox.set_list("Shapes", self.shape_list)
334        self.model_combobox.set_list("Shape-Independent", self.shape_indep_list)
335        self.model_combobox.set_list("Structure Factors", self.struct_list)
336        self.model_combobox.set_list("Customized Models", self.plugins)
337        self.model_combobox.set_list("P(Q)*S(Q)", self.multiplication_factor)
338        self.model_combobox.set_list("multiplication", self.multiplication_factor)
339        return self.model_combobox
[74755ff]340 
Note: See TracBrowser for help on using the repository browser.