Changeset f32d144 in sasview for fittingview/src/sans/perspectives/fitting/models.py
- Timestamp:
- Apr 27, 2012 2:20:33 PM (13 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:
- f992a06
- Parents:
- 9e07bc4
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
fittingview/src/sans/perspectives/fitting/models.py
r279e371 rf32d144 3 3 """ 4 4 import wx 5 import wx.lib.newevent6 5 import imp 7 6 import os … … 14 13 import py_compile 15 14 import shutil 16 from sans.guiframe.events import StatusEvent 15 from sans.guiframe.events import StatusEvent 17 16 # Explicitly import from the pluginmodel module so that py2exe 18 17 # places it in the distribution. The Model1DPlugin class is used … … 20 19 from sans.models.pluginmodel import Model1DPlugin 21 20 22 PLUGIN_DIR = 'plugin_models' 21 PLUGIN_DIR = 'plugin_models' 22 23 23 24 24 def log(message): … … 47 47 log(msg) 48 48 return None 49 if model.__name__ !="Model":50 msg = "Plugin %s class name must be Model \n" % str(name)49 if model.__name__ != "Model": 50 msg = "Plugin %s class name must be Model \n" % str(name) 51 51 log(msg) 52 52 return None 53 53 try: 54 new_instance = model()54 new_instance = model() 55 55 except: 56 msg ="Plugin %s error in __init__ \n\t: %s %s\n" % (str(name),56 msg = "Plugin %s error in __init__ \n\t: %s %s\n" % (str(name), 57 57 str(sys.exc_type), sys.exc_value) 58 58 log(msg) 59 59 return None 60 60 61 new_instance= model() 62 if hasattr(new_instance,"function"): 61 if hasattr(new_instance, "function"): 63 62 try: 64 value=new_instance.function()63 value = new_instance.function() 65 64 except: 66 msg="Plugin %s: error writing function \n\t :%s %s\n " % (str(name),65 msg = "Plugin %s: error writing function \n\t :%s %s\n " % (str(name), 67 66 str(sys.exc_type), sys.exc_value) 68 log(msg)69 return None67 log(msg) 68 return None 70 69 else: 71 msg="Plugin %s needs a method called function \n" % str(name)72 log(msg)73 return None70 msg = "Plugin %s needs a method called function \n" % str(name) 71 log(msg) 72 return None 74 73 return model 74 75 75 76 76 def find_plugins_dir(): … … 89 89 # For source 90 90 if os.path.isdir(os.path.dirname(__file__)): 91 p_dir = 91 p_dir = os.path.join(os.path.dirname(__file__), PLUGIN_DIR) 92 92 else: 93 93 raise … … 109 109 110 110 # Place example user models as needed 111 for file in os.listdir(p_dir): 111 for file in os.listdir(p_dir): 112 112 file_path = os.path.join(p_dir, file) 113 113 if os.path.isfile(file_path): … … 118 118 return dir 119 119 120 120 121 class ReportProblem: 121 122 def __nonzero__(self): … … 128 129 report_problem = ReportProblem() 129 130 131 130 132 def compile_file(dir): 131 133 """ … … 134 136 try: 135 137 import compileall 136 compileall.compile_dir(dir=dir, ddir=dir, force=1, quiet=report_problem) 138 compileall.compile_dir(dir=dir, ddir=dir, force=1, 139 quiet=report_problem) 137 140 except: 138 141 type, value, traceback = sys.exc_info() 139 142 return value 140 143 return None 144 141 145 142 146 def _findModels(dir): … … 161 165 for item in list: 162 166 toks = os.path.splitext(os.path.basename(item)) 163 if toks[1] =='.py' and not toks[0]=='__init__':167 if toks[1] == '.py' and not toks[0] == '__init__': 164 168 name = toks[0] 165 169 … … 168 172 try: 169 173 (file, path, info) = imp.find_module(name, path) 170 module = imp.load_module( name, file, item, info)174 module = imp.load_module(name, file, item, info) 171 175 if hasattr(module, "Model"): 172 176 try: 173 if _check_plugin(module.Model, name) !=None:177 if _check_plugin(module.Model, name) != None: 174 178 plugins[name] = module.Model 175 179 except: 176 msg ="Error accessing Model"177 msg +="in %s\n %s %s\n" % (name,180 msg = "Error accessing Model" 181 msg += "in %s\n %s %s\n" % (name, 178 182 str(sys.exc_type), sys.exc_value) 179 183 log(msg) 180 184 except: 181 msg ="Error accessing Model"182 msg += " in %s\n %s %s \n" %(name,185 msg = "Error accessing Model" 186 msg += " in %s\n %s %s \n" % (name, 183 187 str(sys.exc_type), sys.exc_value) 184 188 log(msg) 185 189 finally: 186 190 187 if not file ==None:191 if not file == None: 188 192 file.close() 189 193 except: … … 194 198 return plugins 195 199 200 196 201 class ModelList(object): 197 202 """ … … 217 222 :param mylist: the list to add 218 223 """ 219 self.mydict[name] = mylist 224 self.mydict[name] = mylist 220 225 221 226 def get_list(self): … … 224 229 """ 225 230 return self.mydict 231 226 232 227 233 class ModelManagerBase: … … 239 245 ## independent shape model list 240 246 shape_indep_list = [] 241 ##list of structure factors 247 ##list of structure factors 242 248 struct_list = [] 243 249 ##list of model allowing multiplication … … 250 256 event_owner = None 251 257 last_time_dir_modified = 0 258 252 259 def __init__(self): 253 260 """ … … 441 448 self.model_name_list.append(BCCrystalModel.__name__) 442 449 443 ## Structure factor 450 ## Structure factor 444 451 from sans.models.SquareWellStructure import SquareWellStructure 445 452 self.struct_list.append(SquareWellStructure) … … 460 467 ##shape-independent models 461 468 from sans.models.PowerLawAbsModel import PowerLawAbsModel 462 self.shape_indep_list.append( PowerLawAbsModel)469 self.shape_indep_list.append(PowerLawAbsModel) 463 470 self.model_name_list.append(PowerLawAbsModel.__name__) 464 471 465 472 from sans.models.BEPolyelectrolyte import BEPolyelectrolyte 466 self.shape_indep_list.append(BEPolyelectrolyte 473 self.shape_indep_list.append(BEPolyelectrolyte) 467 474 self.model_name_list.append(BEPolyelectrolyte.__name__) 468 475 self.form_factor_dict[str(wx.NewId())] = [SphereModel] … … 477 484 478 485 from sans.models.DABModel import DABModel 479 self.shape_indep_list.append(DABModel 486 self.shape_indep_list.append(DABModel) 480 487 self.model_name_list.append(DABModel.__name__) 481 488 482 489 from sans.models.DebyeModel import DebyeModel 483 self.shape_indep_list.append(DebyeModel 490 self.shape_indep_list.append(DebyeModel) 484 491 self.model_name_list.append(DebyeModel.__name__) 485 492 486 493 from sans.models.FractalModel import FractalModel 487 self.shape_indep_list.append(FractalModel 494 self.shape_indep_list.append(FractalModel) 488 495 self.model_name_list.append(FractalModel.__name__) 489 496 490 497 from sans.models.FractalCoreShellModel import FractalCoreShellModel 491 self.shape_indep_list.append(FractalCoreShellModel 498 self.shape_indep_list.append(FractalCoreShellModel) 492 499 self.model_name_list.append(FractalCoreShellModel.__name__) 493 500 494 501 from sans.models.GaussLorentzGelModel import GaussLorentzGelModel 495 self.shape_indep_list.append(GaussLorentzGelModel) 502 self.shape_indep_list.append(GaussLorentzGelModel) 496 503 self.model_name_list.append(GaussLorentzGelModel.__name__) 497 504 498 505 from sans.models.GuinierModel import GuinierModel 499 self.shape_indep_list.append(GuinierModel 506 self.shape_indep_list.append(GuinierModel) 500 507 self.model_name_list.append(GuinierModel.__name__) 501 508 502 509 from sans.models.GuinierPorodModel import GuinierPorodModel 503 self.shape_indep_list.append(GuinierPorodModel 510 self.shape_indep_list.append(GuinierPorodModel) 504 511 self.model_name_list.append(GuinierPorodModel.__name__) 505 512 506 513 from sans.models.LorentzModel import LorentzModel 507 self.shape_indep_list.append( LorentzModel)514 self.shape_indep_list.append(LorentzModel) 508 515 self.model_name_list.append(LorentzModel.__name__) 509 516 … … 522 529 from sans.models.PeakLorentzModel import PeakLorentzModel 523 530 self.shape_indep_list.append(PeakLorentzModel) 524 self.model_name_list.append( 531 self.model_name_list.append(PeakLorentzModel.__name__) 525 532 526 533 from sans.models.Poly_GaussCoil import Poly_GaussCoil … … 533 540 534 541 from sans.models.PorodModel import PorodModel 535 self.shape_indep_list.append(PorodModel )536 self.model_name_list.append(PorodModel.__name__) 542 self.shape_indep_list.append(PorodModel) 543 self.model_name_list.append(PorodModel.__name__) 537 544 538 545 from sans.models.RPA10Model import RPA10Model … … 545 552 546 553 from sans.models.TeubnerStreyModel import TeubnerStreyModel 547 self.shape_indep_list.append(TeubnerStreyModel 554 self.shape_indep_list.append(TeubnerStreyModel) 548 555 self.model_name_list.append(TeubnerStreyModel.__name__) 549 556 550 557 from sans.models.TwoLorentzianModel import TwoLorentzianModel 551 self.shape_indep_list.append(TwoLorentzianModel 558 self.shape_indep_list.append(TwoLorentzianModel) 552 559 self.model_name_list.append(TwoLorentzianModel.__name__) 553 560 554 561 from sans.models.TwoPowerLawModel import TwoPowerLawModel 555 self.shape_indep_list.append(TwoPowerLawModel 562 self.shape_indep_list.append(TwoPowerLawModel) 556 563 self.model_name_list.append(TwoPowerLawModel.__name__) 557 564 558 565 from sans.models.UnifiedPowerRgModel import UnifiedPowerRgModel 559 self.shape_indep_list.append(UnifiedPowerRgModel 566 self.shape_indep_list.append(UnifiedPowerRgModel) 560 567 self.multi_func_list.append(UnifiedPowerRgModel) 561 568 … … 587 594 plugin_dir = find_plugins_dir() 588 595 if os.path.isdir(plugin_dir): 589 temp = 596 temp = os.path.getmtime(plugin_dir) 590 597 if self.last_time_dir_modified != temp: 591 598 is_modified = True … … 596 603 def update(self): 597 604 """ 598 return a dictionary of model if 605 return a dictionary of model if 599 606 new models were added else return empty dictionary 600 607 """ … … 654 661 multip_models = wx.Menu() 655 662 ## create menu with shape 656 self._fill_simple_menu(menuinfo=["Shapes",shape_submenu," simple shape"], 663 self._fill_simple_menu(menuinfo=["Shapes", 664 shape_submenu, 665 " simple shape"], 657 666 list1=self.shape_list) 658 667 659 self._fill_simple_menu(menuinfo=["Shape-Independent",shape_indep_submenu, 660 "List of shape-independent models"], 661 list1=self.shape_indep_list ) 662 663 self._fill_simple_menu(menuinfo=["Structure Factors",structure_factor, 664 "List of Structure factors models" ], 668 self._fill_simple_menu(menuinfo=["Shape-Independent", 669 shape_indep_submenu, 670 "List of shape-independent models"], 671 list1=self.shape_indep_list) 672 673 self._fill_simple_menu(menuinfo=["Structure Factors", 674 structure_factor, 675 "List of Structure factors models"], 665 676 list1=self.struct_list) 666 677 … … 669 680 list1=self.plugins) 670 681 671 self._fill_menu(menuinfo=["P(Q)*S(Q)", multip_models,682 self._fill_menu(menuinfo=["P(Q)*S(Q)", multip_models, 672 683 "mulplication of 2 models"], 673 list1=self.multiplication_factor 674 list2= 684 list1=self.multiplication_factor, 685 list2=self.struct_list) 675 686 return 0 676 687 … … 679 690 fill the plugin menu with costumized models 680 691 """ 681 if len(list1) ==0:682 id = wx.NewId() 683 msg = "No model available check plugins.log for errors to fix problem"684 menuinfo[1].Append(int(id), "Empty",msg)685 self._fill_simple_menu( menuinfo,list1)692 if len(list1) == 0: 693 id = wx.NewId() 694 msg = "No model available check plugins.log for errors to fix problem" 695 menuinfo[1].Append(int(id), "Empty", msg) 696 self._fill_simple_menu(menuinfo, list1) 686 697 687 698 def _fill_simple_menu(self, menuinfo, list1): … … 696 707 697 708 """ 698 if len(list1) >0:699 self.model_combobox.set_list(menuinfo[0], list1)709 if len(list1) > 0: 710 self.model_combobox.set_list(menuinfo[0], list1) 700 711 701 712 for item in list1: 702 713 try: 703 id = wx.NewId() 704 struct_factor =item()714 id = wx.NewId() 715 struct_factor = item() 705 716 struct_name = struct_factor.__class__.__name__ 706 717 if hasattr(struct_factor, "name"): 707 718 struct_name = struct_factor.name 708 719 709 menuinfo[1].Append(int(id), struct_name,struct_name)720 menuinfo[1].Append(int(id), struct_name, struct_name) 710 721 if not item in self.struct_factor_dict.itervalues(): 711 self.struct_factor_dict[str(id)] = item722 self.struct_factor_dict[str(id)] = item 712 723 wx.EVT_MENU(self.event_owner, int(id), self._on_model) 713 724 except: 714 msg = "Error Occured: %s"%sys.exc_value725 msg = "Error Occured: %s" % sys.exc_value 715 726 wx.PostEvent(self.event_owner, StatusEvent(status=msg)) 716 727 717 id = wx.NewId() 718 self.modelmenu.AppendMenu(id, menuinfo[0], menuinfo[1],menuinfo[2])728 id = wx.NewId() 729 self.modelmenu.AppendMenu(id, menuinfo[0], menuinfo[1], menuinfo[2]) 719 730 720 731 def _fill_menu(self, menuinfo, list1, list2): … … 726 737 [name(string) , menu(wx.menu), help(string)] 727 738 :param list1: contains item (form factor )to fill modelmenu second column 728 :param list2: contains item (Structure factor )to fill modelmenu 739 :param list2: contains item (Structure factor )to fill modelmenu 729 740 third column 730 741 731 742 """ 732 if len(list1) >0:733 self.model_combobox.set_list(menuinfo[0], list1)743 if len(list1) > 0: 744 self.model_combobox.set_list(menuinfo[0], list1) 734 745 735 for item in list1: 736 form_factor = item()746 for item in list1: 747 form_factor = item() 737 748 form_name = form_factor.__class__.__name__ 738 749 if hasattr(form_factor, "name"): 739 750 form_name = form_factor.name 740 ### store form factor to return to other users 741 newmenu = wx.Menu()742 if len(list2) >0:751 ### store form factor to return to other users 752 newmenu = wx.Menu() 753 if len(list2) > 0: 743 754 for model in list2: 744 755 id = wx.NewId() … … 747 758 if hasattr(struct_factor, "name"): 748 759 name = struct_factor.name 749 newmenu.Append(id, name, name)760 newmenu.Append(id, name, name) 750 761 wx.EVT_MENU(self.event_owner, int(id), self._on_model) 751 762 ## save form_fact and struct_fact 752 self.form_factor_dict[int(id)] = [form_factor,struct_factor] 763 self.form_factor_dict[int(id)] = [form_factor, 764 struct_factor] 753 765 754 form_id= wx.NewId() 755 menuinfo[1].AppendMenu(int(form_id), form_name,newmenu,menuinfo[2]) 756 id=wx.NewId() 757 self.modelmenu.AppendMenu(id,menuinfo[0],menuinfo[1], menuinfo[2]) 766 form_id = wx.NewId() 767 menuinfo[1].AppendMenu(int(form_id), form_name, 768 newmenu, menuinfo[2]) 769 id = wx.NewId() 770 self.modelmenu.AppendMenu(id, menuinfo[0], menuinfo[1], menuinfo[2]) 758 771 759 772 def _on_model(self, evt): … … 767 780 from sans.models.MultiplicationModel import MultiplicationModel 768 781 model1, model2 = self.form_factor_dict[int(evt.GetId())] 769 model = MultiplicationModel(model1, model2) 782 model = MultiplicationModel(model1, model2) 770 783 else: 771 model = self.struct_factor_dict[str(evt.GetId())]()784 model = self.struct_factor_dict[str(evt.GetId())]() 772 785 773 786 #TODO: investigate why the following two lines were left in the code … … 789 802 pass 790 803 791 def get_model_list(self): 792 """ 793 return dictionary of models for fitpanel use 804 def get_model_list(self): 805 """ 806 return dictionary of models for fitpanel use 794 807 795 808 """ 796 809 self.model_combobox.set_list("Shapes", self.shape_list) 797 self.model_combobox.set_list("Shape-Independent", self.shape_indep_list) 810 self.model_combobox.set_list("Shape-Independent", 811 self.shape_indep_list) 798 812 self.model_combobox.set_list("Structure Factors", self.struct_list) 799 813 self.model_combobox.set_list("Customized Models", self.plugins) 800 814 self.model_combobox.set_list("P(Q)*S(Q)", self.multiplication_factor) 801 self.model_combobox.set_list("multiplication", self.multiplication_factor) 815 self.model_combobox.set_list("multiplication", 816 self.multiplication_factor) 802 817 self.model_combobox.set_list("Multi-Functions", self.multi_func_list) 803 818 return self.model_combobox.get_list() … … 812 827 class ModelManager(object): 813 828 """ 814 implement model 829 implement model 815 830 """ 816 831 __modelmanager = ModelManagerBase() … … 840 855 return self.__modelmanager._get_multifunc_models() 841 856 842 def get_model_list(self): 857 def get_model_list(self): 843 858 return self.__modelmanager.get_model_list() 844 859 845 860 def get_model_name_list(self): 846 861 return self.__modelmanager.get_model_name_list() 847 848 849
Note: See TracChangeset
for help on using the changeset viewer.