- Timestamp:
- Sep 10, 2008 3:49:53 PM (16 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 49b7efa
- Parents:
- aa92772
- Location:
- sansview
- Files:
-
- 2 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
sansview/perspectives/fitting/models.py
raa92772 rb30f001 1 1 import wx 2 import os 2 import imp 3 import os,sys 3 4 import os.path 4 5 5 6 (ModelEvent, EVT_MODEL) = wx.lib.newevent.NewEvent() 7 def log(message): 8 print message 9 out = open("plugins.log", 'a') 10 out.write("%10g: %s\n" % (time.clock(), message)) 11 out.close() 12 6 13 def findModels(): 7 14 print "looking for models" 8 try: 9 cwd= os.path.split(__file__)[0] 10 except: 11 cwd= os.getcwd() 12 print "models cwd",cwd 13 dir=os.path.join(cwd,'plugins') 14 print "models: find plugins",dir 15 if os.path.isdir(dir): 16 return _findModels(dir) 17 else: 18 return [] 19 20 15 if os.path.isdir('plugins'): 16 return _findModels('plugins') 17 return [] 21 18 22 19 def _findModels(dir): … … 27 24 list = os.listdir(dir) 28 25 for item in list: 29 print "models: _findModels:",item30 26 toks = os.path.splitext(os.path.basename(item)) 31 print "models: toks:",toks32 27 if toks[1]=='.py' and not toks[0]=='__init__': 33 28 name = toks[0] … … 37 32 try: 38 33 (file, path, info) = imp.find_module(name, path) 34 print "models:module ",file 39 35 module = imp.load_module( name, file, item, info ) 36 print "models:module ",module 40 37 if hasattr(module, "Model"): 41 38 try: … … 44 41 log("Error accessing Model in %s\n %s" % (name, sys.exc_value)) 45 42 except: 46 pass43 log("Error accessing Model in %s\n %s" % (name, sys.exc_value)) 47 44 finally: 48 45 if not file==None: … … 51 48 pass 52 49 return plugins 53 54 55 56 50 class ModelManager: 57 51 … … 59 53 model_list = {} 60 54 model_list_box = {} 55 custom_models={} 56 plugins=[] 61 57 ## Event owner 62 58 event_owner = None … … 80 76 from sans.guitools.LineModel import LineModel 81 77 self.model_list[str(wx.NewId())] = LineModel 82 78 self.plugins =findModels() 79 80 print "models: plugings",self.plugins 83 81 return 0 84 82 … … 96 94 self._getModelList() 97 95 self.event_owner = event_owner 98 96 shape_submenu= wx.Menu() 97 indep_submenu = wx.Menu() 98 added_models = wx.Menu() 99 99 for id_str,value in self.model_list.iteritems(): 100 100 item = self.model_list[id_str] … … 105 105 106 106 self.model_list_box[name] =value 107 108 modelmenu.Append(int(id_str), name, name) 109 wx.EVT_MENU(event_owner, int(id_str), self._on_model) 110 plugings=findModels() 111 print "models: plugings",plugings 107 108 109 shape_submenu.Append(int(id_str), name, name) 110 wx.EVT_MENU(event_owner, int(id_str), self._on_model) 111 modelmenu.AppendMenu(wx.NewId(), "Shapes...", shape_submenu, "List of shape-based models") 112 modelmenu.AppendMenu(wx.NewId(), "Shape-independent...", indep_submenu, "List of shape-independent models") 113 114 id = wx.NewId() 115 if len(self.custom_models) == 0: 116 print self.plugins 117 for item in self.plugins: 118 if item not in self.custom_models.keys(): 119 self.custom_models[str(id)] = item 120 self.model_list[str(id)]=item 121 added_models.Append(id, item.name, item.name) 122 wx.EVT_MENU(event_owner, int(id), self._on_model) 123 id = wx.NewId() 124 125 126 modelmenu.AppendMenu(wx.NewId(),"Added models...", added_models, "List of additional models") 127 128 129 112 130 return 0 113 131 -
sansview/plugins/testmodel.py
raa92772 rb30f001 2 2 Test plug-in model 3 3 """ 4 try: 5 from DataPlugin import * 6 except: 7 import sys, os 8 print os.path.abspath('..') 9 sys.path.insert(1,os.path.abspath('..')) 10 from DataPlugin import * 11 print "Running independently" 4 #try: 5 # from DataPlugin import * 6 #except: 7 # import sys, os 8 # print os.path.abspath('..') 9 # sys.path.insert(1,os.path.abspath('..')) 10 # from DataPlugin import * 11 # print "Running independently" 12 # 13 import math 14 from sans.models.BaseComponent import BaseComponent 15 import math 16 class Model1DPlugin(BaseComponent): 17 ## Name of the model 18 name = "Plugin Model" 12 19 13 import math 20 def __init__(self): 21 """ Initialization """ 22 self.details = {} 23 self.params = {} 24 25 def function(self, x): 26 """ 27 Function to be implemented by the plug-in writer 28 """ 29 return x 30 31 def run(self, x = 0.0): 32 """ Evaluate the model 33 @param x: input x, or [x, phi] [radian] 34 @return: function value 35 """ 36 if x.__class__.__name__ == 'list': 37 x_val = x[0]*math.cos(x[1]) 38 y_val = x[0]*math.sin(x[1]) 39 return self.function(x_val)*self.function(y_val) 40 elif x.__class__.__name__ == 'tuple': 41 raise ValueError, "Tuples are not allowed as input to BaseComponent models" 42 else: 43 return self.function(x) 44 45 def runXY(self, x = 0.0): 46 """ Evaluate the model 47 @param x: input x, or [x, y] 48 @return: function value 49 """ 50 if x.__class__.__name__ == 'list': 51 return self.function(x[0])*self.function(x[1]) 52 elif x.__class__.__name__ == 'tuple': 53 raise ValueError, "Tuples are not allowed as input to BaseComponent models" 54 else: 55 return self.function(x) 14 56 15 57 # Your model HAS to be called Model … … 23 65 def __init__(self): 24 66 """ Initialization """ 25 26 Model1DPlugin.__init__(self) 67 27 68 28 69 ## Parameters definition and defaults
Note: See TracChangeset
for help on using the changeset viewer.