Changeset bb18ef1 in sasview for sansview/perspectives/fitting/models.py
- Timestamp:
- Mar 18, 2009 11:41:18 AM (15 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:
- 35eeea8
- Parents:
- 8cfdd5e
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sansview/perspectives/fitting/models.py
r43eb424 rbb18ef1 10 10 11 11 import wx 12 import wx.lib.newevent 12 13 import imp 13 14 import os,sys,math … … 66 67 pass 67 68 return plugins 69 70 class ModelList(object): 71 """ 72 Contains dictionary of model and their type 73 """ 74 def __init__(self): 75 self.mydict={} 76 77 def set_list(self, name, mylist): 78 """ 79 @param name: the type of the list 80 @param mylist: the list to add 81 """ 82 if name not in self.mydict.keys(): 83 self.mydict[name] = mylist 84 85 86 def get_list(self): 87 """ 88 return all the list stored in a dictionary object 89 """ 90 return self.mydict 91 68 92 class ModelManager: 69 70 ## Dictionary of models 71 model_list = {} 72 indep_model_list = {} 73 model_list_box = {} 74 custom_models={} 93 ## external dict for models 94 model_combobox = ModelList() 95 ## Dictionary of form models 96 form_factor_dict = {} 97 ## dictionary of other 98 struct_factor_dict = {} 99 ##list of form factors 100 shape_list =[] 101 ## independent shape model list 102 shape_indep_list = [] 103 ##list of structure factors 104 struct_list= [] 105 ## list of added models 75 106 plugins=[] 76 indep_model=[] 77 ## Event owner 107 ## Event owner (guiframe) 78 108 event_owner = None 79 109 … … 86 116 @return: the next free event ID following the new menu events 87 117 """ 88 self.model_list = {} 89 self.model_list_box = {} 90 91 118 ## form factor 92 119 from sans.models.SphereModel import SphereModel 93 self. model_list[str(wx.NewId())] = SphereModel120 self.shape_list.append(SphereModel) 94 121 95 122 from sans.models.CylinderModel import CylinderModel 96 self. model_list[str(wx.NewId())] =CylinderModel123 self.shape_list.append(CylinderModel) 97 124 98 125 from sans.models.CoreShellModel import CoreShellModel 99 self. model_list[str(wx.NewId())] = CoreShellModel126 self.shape_list.append(CoreShellModel) 100 127 101 128 from sans.models.CoreShellCylinderModel import CoreShellCylinderModel 102 self. model_list[str(wx.NewId())] =CoreShellCylinderModel129 self.shape_list.append(CoreShellCylinderModel) 103 130 104 131 from sans.models.EllipticalCylinderModel import EllipticalCylinderModel 105 self. model_list[str(wx.NewId())] =EllipticalCylinderModel132 self.shape_list.append(EllipticalCylinderModel) 106 133 107 134 from sans.models.EllipsoidModel import EllipsoidModel 108 self.model_list[str(wx.NewId())] = EllipsoidModel 135 self.shape_list.append(EllipsoidModel) 136 137 from sans.models.LineModel import LineModel 138 self.shape_list.append(LineModel) 139 140 ## Structure factor 141 from sans.models.NoStructure import NoStructure 142 self.struct_list.append(NoStructure) 109 143 110 144 from sans.models.SquareWellStructure import SquareWellStructure 111 self. model_list[str(wx.NewId())] = SquareWellStructure145 self.struct_list.append(SquareWellStructure) 112 146 113 147 from sans.models.HardsphereStructure import HardsphereStructure 114 self. model_list[str(wx.NewId())] = HardsphereStructure115 148 self.struct_list.append(HardsphereStructure) 149 116 150 from sans.models.StickyHSStructure import StickyHSStructure 117 self. model_list[str(wx.NewId())] = StickyHSStructure151 self.struct_list.append(StickyHSStructure) 118 152 119 153 from sans.models.HayterMSAStructure import HayterMSAStructure 120 self.model_list[str(wx.NewId())] = HayterMSAStructure 121 122 from sans.models.LineModel import LineModel 123 self.model_list[str(wx.NewId())] = LineModel 124 125 126 model_info="shape-independent models" 127 154 self.struct_list.append(HayterMSAStructure) 155 156 157 ##shape-independent models 128 158 from sans.models.BEPolyelectrolyte import BEPolyelectrolyte 129 self. indep_model.append(BEPolyelectrolyte )130 159 self.shape_indep_list.append(BEPolyelectrolyte ) 160 self.form_factor_dict[str(wx.NewId())] = [SphereModel] 131 161 from sans.models.DABModel import DABModel 132 self. indep_model.append(DABModel )162 self.shape_indep_list.append(DABModel ) 133 163 134 164 from sans.models.GuinierModel import GuinierModel 135 self. indep_model.append(GuinierModel )165 self.shape_indep_list.append(GuinierModel ) 136 166 137 167 from sans.models.DebyeModel import DebyeModel 138 self.indep_model.append(DebyeModel ) 168 self.shape_indep_list.append(DebyeModel ) 169 170 from sans.models.PorodModel import PorodModel 171 self.shape_indep_list.append(PorodModel ) 139 172 140 173 from sans.models.FractalModel import FractalModel … … 142 175 def _Fractal(self, x): 143 176 return FractalModel._Fractal(self, math.fabs(x)) 144 self. indep_model.append(FractalAbsModel)177 self.shape_indep_list.append(FractalAbsModel) 145 178 146 179 from sans.models.LorentzModel import LorentzModel 147 self. indep_model.append( LorentzModel)180 self.shape_indep_list.append( LorentzModel) 148 181 149 182 from sans.models.PowerLawModel import PowerLawModel … … 154 187 except: 155 188 print sys.exc_value 156 self. indep_model.append( PowerLawAbsModel )189 self.shape_indep_list.append( PowerLawAbsModel ) 157 190 from sans.models.TeubnerStreyModel import TeubnerStreyModel 158 self. indep_model.append(TeubnerStreyModel )191 self.shape_indep_list.append(TeubnerStreyModel ) 159 192 160 193 #Looking for plugins … … 173 206 @return: the next free event ID following the new menu events 174 207 """ 208 ## Fill model lists 175 209 self._getModelList() 210 ## store reference to model menu of guiframe 211 self.modelmenu = modelmenu 212 ## guiframe reference 176 213 self.event_owner = event_owner 177 shape_submenu= wx.Menu() 178 indep_submenu = wx.Menu() 214 215 216 shape_submenu = wx.Menu() 217 shape_indep_submenu = wx.Menu() 218 structure_factor = wx.Menu() 179 219 added_models = wx.Menu() 180 for id_str,value in self.model_list.iteritems(): 181 item = self.model_list[id_str]() 182 name = item.__class__.__name__ 183 if hasattr(item, "name"): 184 name = item.name 185 self.model_list_box[name] =value 186 shape_submenu.Append(int(id_str), name, name) 187 wx.EVT_MENU(event_owner, int(id_str), self._on_model) 188 modelmenu.AppendMenu(wx.NewId(), "Shapes...", shape_submenu, "List of shape-based models") 189 id = wx.NewId() 190 if len(self.indep_model_list) == 0: 191 for items in self.indep_model: 192 #if item not in self.indep_model_list.values(): 193 #self.indep_model_list[str(id)] = item 194 self.model_list[str(id)]=items 195 item=items() 196 name = item.__class__.__name__ 197 if hasattr(item, "name"): 198 name = item.name 199 indep_submenu.Append(id,name, name) 200 self.model_list_box[name] =items 201 wx.EVT_MENU(event_owner, int(id), self._on_model) 202 id = wx.NewId() 203 modelmenu.AppendMenu(wx.NewId(), "Shape-independent...", indep_submenu, "List of shape-independent models") 204 id = wx.NewId() 205 if len(self.custom_models) == 0: 206 for items in self.plugins: 207 #if item not in self.custom_models.values(): 208 #self.custom_models[str(id)] = item 209 self.model_list[str(id)]=items 210 name = items.__name__ 211 if hasattr(items, "name"): 212 name = items.name 213 added_models.Append(id, name, name) 214 self.model_list_box[name] =items 215 wx.EVT_MENU(event_owner, int(id), self._on_model) 216 id = wx.NewId() 217 modelmenu.AppendMenu(wx.NewId(),"Added models...", added_models, "List of additional models") 220 ## create menu with shape 221 self._fill_menu( menuinfo = ["shapes",shape_submenu," simple shape"], 222 list1 = self.shape_list, 223 list2 = self.struct_list ) 224 self._fill_menu( menuinfo = ["Shape-independent",shape_indep_submenu, 225 "List of shape-independent models"], 226 list1 = self.shape_indep_list, 227 list2 = self.struct_list ) 228 229 self._fill_simple_menu( menuinfo= ["Structure Factors",structure_factor, 230 "List of Structure factors models" ], 231 list1= self.struct_list ) 232 233 self._fill_simple_menu( menuinfo = ["Added models", added_models, 234 "List of additional models"], 235 list1= self.plugins) 218 236 return 0 219 237 238 def _fill_simple_menu(self,menuinfo, list1): 239 """ 240 Fill the menu with list item 241 @param modelmenu: the menu to fill 242 @param menuinfo: submenu item for the first column of this modelmenu 243 with info.Should be a list : 244 [name(string) , menu(wx.menu), help(string)] 245 @param list1: contains item (form factor )to fill modelmenu second column 246 """ 247 if len(list1)>0: 248 self.model_combobox.set_list(menuinfo[0],list1) 249 250 for item in list1: 251 id = wx.NewId() 252 struct_factor=item() 253 struct_name = struct_factor.__class__.__name__ 254 if hasattr(struct_factor, "name"): 255 struct_name = struct_factor.name 256 257 menuinfo[1].Append(int(id),struct_name,struct_name) 258 if not item in self.struct_factor_dict.itervalues(): 259 self.struct_factor_dict[str(id)]= item 260 wx.EVT_MENU(self.event_owner, int(id), self._on_model) 261 262 id = wx.NewId() 263 self.modelmenu.AppendMenu(id, menuinfo[0],menuinfo[1],menuinfo[2]) 264 265 266 267 def _fill_menu(self,menuinfo, list1,list2 ): 268 """ 269 Fill the menu with list item 270 @param menuinfo: submenu item for the first column of this modelmenu 271 with info.Should be a list : 272 [name(string) , menu(wx.menu), help(string)] 273 @param list1: contains item (form factor )to fill modelmenu second column 274 @param list2: contains item (Structure factor )to fill modelmenu third column 275 """ 276 if len(list1)>0: 277 self.model_combobox.set_list(menuinfo[0],list1) 278 279 for item in list1: 280 form_factor= item() 281 form_name = form_factor.__class__.__name__ 282 if hasattr(form_factor, "name"): 283 form_name = form_factor.name 284 ### store form factor to return to other users 285 newmenu= wx.Menu() 286 if len(list2)>0: 287 for model in list2: 288 id = wx.NewId() 289 struct_factor = model() 290 name = struct_factor.__class__.__name__ 291 if hasattr(struct_factor, "name"): 292 name = struct_factor.name 293 newmenu.Append(id,name, name) 294 wx.EVT_MENU(self.event_owner, int(id), self._on_model) 295 ## save form_fact and struct_fact 296 self.form_factor_dict[int(id)] = [form_factor,struct_factor] 297 298 form_id= wx.NewId() 299 menuinfo[1].AppendMenu(int(form_id), form_name,newmenu,menuinfo[2]) 300 id=wx.NewId() 301 self.modelmenu.AppendMenu(id,menuinfo[0],menuinfo[1], menuinfo[2]) 302 303 304 305 220 306 def _on_model(self, evt): 221 307 """ … … 223 309 @param event: wx menu event 224 310 """ 225 if str(evt.GetId()) in self.model_list.keys():226 # Notify the application manager that a new model has been set227 #self.app_manager.set_model(self.model_list[str(evt.GetId())]())228 229 #TODO: post a model event to update all panels that need230 #evt = ModelEvent(model=self.model_list[str(evt.GetId())]())231 232 model = self.model_list[str(evt.GetId())]233 evt = ModelEvent(model= model )234 311 if int(evt.GetId()) in self.form_factor_dict.keys(): 312 from sans.models.MultiplicationModel import MultiplicationModel 313 model1, model2 = self.form_factor_dict[int(evt.GetId())] 314 model = MultiplicationModel(model1, model2) 315 316 else: 317 model= self.struct_factor_dict[str(evt.GetId())]() 318 319 evt = ModelEvent( model= model ) 320 wx.PostEvent(self.event_owner, evt) 235 321 236 322 def get_model_list(self): 237 323 """ @ return dictionary of models for fitpanel use """ 238 return self.model_list_box 239 240 241 242 324 return self.model_combobox 325 326 327 def get_form_struct(self): 328 """ retunr list of form structures""" 329 return self.struct_list 330 331 332 333
Note: See TracChangeset
for help on using the changeset viewer.