Changeset d970df9 in sasview


Ignore:
Timestamp:
Jun 15, 2012 3:23:58 PM (12 years ago)
Author:
Jae Cho <jhjcho@…>
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:
4a47244
Parents:
d7b49576
Message:

Added custom models in the list

File:
1 edited

Legend:

Unmodified
Added
Removed
  • calculatorview/src/sans/perspectives/calculator/model_editor.py

    rfdef956 rd970df9  
    1  
    21################################################################################ 
    32#This software was developed by the University of Tennessee as part of the 
     
    98#copyright 2009, University of Tennessee 
    109################################################################################ 
    11  
    12  
    1310import wx 
    1411import sys 
     
    4037        return '' 
    4138    except: 
    42         type, value, traceback = sys.exc_info() 
     39        _, value, _ = sys.exc_info() 
    4340        return value 
    4441     
     
    5754    Dialog for easy custom sum models   
    5855    """ 
    59     def __init__(self, parent=None, base=None, id=None, title='', model_list=[], plugin_dir=None): 
    60         """ 
    61         Dialog window popup when selecting 'Easy Custom Sum/Multiply' on the menu 
     56    def __init__(self, parent=None, base=None, id=None, title='',  
     57                 model_list=[], plugin_dir=None): 
     58        """ 
     59        Dialog window popup when selecting 'Easy Custom Sum/Multiply'  
     60        on the menu 
    6261        """ 
    6362        wx.Dialog.__init__(self, parent=parent, id=id,  
     
    7574        self.model2_string = "CylinderModel" 
    7675        self.name = 'Sum' + M_NAME 
     76        self.factor = 'scale_factor' 
    7777        self._notes = '' 
    7878        self.operator = '+' 
     
    8181        self.explanationctr = None 
    8282        self.sizer = None 
     83        self.name_sizer = None 
     84        self.name_hsizer = None 
     85        self.desc_sizer = None 
     86        self.desc_tcl = None 
     87        self.model1 = None 
     88        self.model2 = None 
     89        self.static_line_1 = None 
     90        self.okButton = None 
     91        self.closeButton = None 
    8392        self._msg_box = None 
    8493        self.msg_sizer = None 
     94        self.fname = None 
     95        self.cm_list = None 
     96        self.is_p1_custom = False 
     97        self.is_p2_custom = False 
    8598        self._build_sizer() 
    8699        self.model1_name = str(self.model1.GetValue()) 
     
    123136        self.desc_tcl.SetToolTipString(hint_desc) 
    124137        self.desc_sizer.AddMany([(desc_txt, 0, wx.LEFT|wx.TOP, 10), 
    125                                     (self.desc_tcl, -1,  
    126                                     wx.EXPAND|wx.RIGHT|wx.TOP|wx.BOTTOM, 10)])      
     138                                (self.desc_tcl, -1,  
     139                                wx.EXPAND|wx.RIGHT|wx.TOP|wx.BOTTOM, 10)])      
    127140   
    128141    def _build_sizer(self): 
     
    137150         
    138151         
    139         sum_description= wx.StaticBox(self, -1, 'Select',  
     152        sum_description = wx.StaticBox(self, -1, 'Select',  
    140153                                       size=(PNL_WIDTH-30, 70)) 
    141154        sum_box = wx.StaticBoxSizer(sum_description, wx.VERTICAL) 
     
    167180                                     size=(box_width/2, 25)) 
    168181        # Intro 
    169         self.explanation  = "  custom model = scale_factor * (model1 %s model2)\n"\ 
    170                             % self.operator 
     182        self.explanation  = "  custom model = %s %s "% (self.factor, '*') 
     183        self.explanation  += "(model1 %s model2)\n"% self.operator 
    171184        #explanation  += "  Note: This will overwrite the previous sum model.\n" 
    172185        model_string = " Model%s (p%s):" 
     
    183196        self.sizer.Add(self.explanationctr , (iy, ix), 
    184197                 (1, 1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 
    185         model1_box.Add(wx.StaticText(self,-1, model_string% (1, 1)), -1, 0) 
    186         model1_box.Add((box_width-15,10)) 
     198        model1_box.Add(wx.StaticText(self, -1, model_string% (1, 1)), -1, 0) 
     199        model1_box.Add((box_width-15, 10)) 
    187200        model1_box.Add(wx.StaticText(self, -1, model_string% (2, 2)), -1, 0) 
    188201        model2_box.Add(self.model1, -1, 0) 
    189         model2_box.Add((15,10)) 
     202        model2_box.Add((15, 10)) 
    190203        model2_box.Add(self.operator_cbox, 0, 0) 
    191         model2_box.Add((15,10)) 
     204        model2_box.Add((15, 10)) 
    192205        model2_box.Add(self.model2, -1, 0) 
    193206        model_vbox.Add(model1_box, -1, 0) 
     
    198211        self.sizer.Add(sum_box, (iy, ix), 
    199212                  (1, 1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 
    200         vbox.Add((10,10)) 
     213        vbox.Add((10, 10)) 
    201214        vbox.Add(self.static_line_1, 0, wx.EXPAND, 10) 
    202215        vbox.Add(self.msg_sizer, 0,  
     
    226239        Check name if exist already 
    227240        """ 
     241        mname = M_NAME 
    228242        self.on_change_name(None) 
    229243        list_fnames = os.listdir(self.plugin_dir) 
    230  
     244        # fake existing regular model name list 
     245        m_list = [model + ".py" for model in self.model_list] 
     246        list_fnames.append(m_list) 
    231247        # function/file name 
    232248        title = self.name_tcl.GetValue().lstrip().rstrip() 
     
    234250            text = self.operator_cbox.GetLabel().strip() 
    235251            if text == '+': 
    236                 name = 'Sum' 
     252                mname = 'Sum' 
    237253            else: 
    238                 name = 'Multi' 
    239             self.name = name + M_NAME 
    240             title = self.name 
     254                mname = 'Multi' 
     255            mname += M_NAME 
     256            title = mname 
    241257        self.name = title 
    242258        t_fname = title + '.py' 
    243259        if not self.overwrite_name: 
    244             if t_fname in list_fnames and title != self.name: 
     260            if t_fname in list_fnames and title != mname: 
    245261                self.name_tcl.SetBackgroundColour('pink') 
    246262                self.good_name = False 
     
    295311        """ 
    296312        # list of model names 
     313        cm_list = [] 
     314        # models 
    297315        list = self.model_list 
     316        # custom models 
     317        al_list = os.listdir(self.plugin_dir) 
     318        for c_name in al_list: 
     319            if c_name.split('.')[-1] == 'py' and \ 
     320                    c_name.split('.')[0] != '__init__': 
     321                name = str(c_name.split('.')[0]) 
     322                cm_list.append(name) 
     323                if name not in list: 
     324                    list.append(name) 
     325        self.cm_list = cm_list 
    298326        if len(list) > 1: 
    299327            list.sort() 
    300328        for idx in range(len(list)): 
    301             self.model1.Append(list[idx],idx)  
    302             self.model2.Append(list[idx],idx) 
     329            self.model1.Append(str(list[idx]), idx)  
     330            self.model2.Append(str(list[idx]), idx) 
    303331        self.model1.SetStringSelection(self.model1_string) 
    304332        self.model2.SetStringSelection(self.model2_string) 
    305             
     333     
     334    def update_cm_list(self): 
     335        """ 
     336        Update custom model list 
     337        """ 
     338        cm_list = [] 
     339        al_list = os.listdir(self.plugin_dir) 
     340        for c_name in al_list: 
     341            if c_name.split('.')[-1] == 'py' and \ 
     342                    c_name.split('.')[0] != '__init__': 
     343                name = str(c_name.split('.')[0]) 
     344                cm_list.append(name) 
     345        self.cm_list = cm_list  
     346               
    306347    def on_model1(self, event): 
    307348        """ 
     
    309350        """ 
    310351        event.Skip() 
     352        self.update_cm_list() 
    311353        self.model1_name = str(self.model1.GetValue()) 
    312354        self.model1_string = self.model1_name 
     355        if self.model1_name in self.cm_list: 
     356            self.is_p1_custom = True 
     357        else: 
     358            self.is_p1_custom = False 
    313359             
    314360    def on_model2(self, event): 
     
    317363        """ 
    318364        event.Skip() 
     365        self.update_cm_list() 
    319366        self.model2_name = str(self.model2.GetValue()) 
    320367        self.model2_string = self.model2_name 
     368        if self.model2_name in self.cm_list: 
     369            self.is_p2_custom = True 
     370        else: 
     371            self.is_p2_custom = False 
    321372         
    322373    def on_select_operator(self, event=None): 
     
    328379        if text == '+': 
    329380            name = 'Sum' 
     381            factor = 'scale_factor' 
     382            f_oper = '*' 
    330383        else: 
    331384            name = 'Multi' 
     385            factor = 'BackGround' 
     386            f_oper = '+' 
     387        self.factor = str(factor) 
    332388        self.operator = str(text) 
    333         self.explanation = "  custom model = scale_factor * " 
     389        self.explanation = "  custom model = %s %s "% (self.factor,  
     390                                                       f_oper) 
    334391        self.explanation += "(model1 %s model2)\n"% self.operator 
    335392        self.explanationctr.SetLabel(self.explanation) 
     
    362419            description = name1 + self.operator + name2 
    363420        name = self.name_tcl.GetValue().lstrip().rstrip() 
    364         if name == '': 
    365             text = self.operator_cbox.GetLabel().strip() 
    366             if text == '+': 
    367                 name = 'Sum' 
    368             else: 
    369                 name = 'Multi' 
    370             self.name = name + M_NAME 
     421        text = self.operator_cbox.GetLabel().strip() 
     422        if text == '+': 
     423            factor = 'scale_factor' 
     424            f_oper = '*' 
     425            default_val = '1.0' 
     426        else: 
     427            factor = 'BackGround' 
     428            f_oper = '+' 
     429            default_val = '0.0' 
    371430        path = self.fname 
    372431        try: 
     
    376435        lines = SUM_TEMPLATE.split('\n') 
    377436        for line in lines: 
    378             if line.count("import %s as P1"): 
    379                 out_f.write(line % (name1, name1) + "\n") 
    380             elif line.count("import %s as P2"): 
    381                 out_f.write(line % (name2, name2) + "\n") 
    382             elif line.count("self.description = '%s'"): 
    383                 out_f.write(line % description + "\n") 
    384             elif line.count("run") and line.count("%s"): 
    385                 out_f.write(line % self.operator) 
    386             elif line.count("evalDistribution") and line.count("%s"): 
    387                 out_f.write(line % self.operator) 
    388             else: 
    389                 out_f.write(line + "\n") 
     437            try: 
     438                if line.count("scale_factor"): 
     439                    line = line.replace('scale_factor', factor) 
     440                    #print "scale_factor", line 
     441                if line.count("= %s"): 
     442                    out_f.write(line % (default_val) + "\n") 
     443                elif line.count("import Model as P1"): 
     444                    if self.is_p1_custom: 
     445                        line = line.replace('#', '') 
     446                        out_f.write(line % name1 + "\n") 
     447                    else: 
     448                        out_f.write(line + "\n") 
     449                elif line.count("import %s as P1"): 
     450                    if not self.is_p1_custom: 
     451                        line = line.replace('#', '') 
     452                        out_f.write(line % (name1, name1) + "\n") 
     453                    else: 
     454                        out_f.write(line + "\n") 
     455                elif line.count("import Model as P2"): 
     456                    if self.is_p2_custom: 
     457                        line = line.replace('#', '') 
     458                        out_f.write(line % name2 + "\n") 
     459                    else: 
     460                        out_f.write(line + "\n") 
     461                elif line.count("import %s as P2"): 
     462                    if not self.is_p2_custom: 
     463                        line = line.replace('#', '') 
     464                        out_f.write(line % (name2, name2) + "\n") 
     465                    else: 
     466                        out_f.write(line + "\n") 
     467                elif line.count("self.description = '%s'"): 
     468                    out_f.write(line % description + "\n") 
     469                #elif line.count("run") and line.count("%s"): 
     470                #    out_f.write(line % self.operator + "\n") 
     471                #elif line.count("evalDistribution") and line.count("%s"): 
     472                #    out_f.write(line % self.operator + "\n") 
     473                elif line.count("return") and line.count("%s") == 2: 
     474                    #print "line return", line 
     475                    out_f.write(line % (f_oper, self.operator) + "\n") 
     476                elif line.count("out2")and line.count("%s"): 
     477                    out_f.write(line % self.operator + "\n") 
     478                else: 
     479                    out_f.write(line + "\n") 
     480            except: 
     481                raise 
    390482        out_f.close() 
    391483        #else: 
     
    413505        kwds['name'] = title 
    414506        kwds["size"] = (EDITOR_WIDTH, EDITOR_HEIGTH) 
    415         kwds["style"]= wx.FULL_REPAINT_ON_RESIZE 
     507        kwds["style"] = wx.FULL_REPAINT_ON_RESIZE 
    416508        wx.ScrolledWindow.__init__(self, parent, *args, **kwds) 
    417509        #self.SetupScrolling() 
     
    814906         
    815907class EditorWindow(wx.Frame): 
     908    """ 
     909    Editor Window 
     910    """ 
    816911    def __init__(self, parent, base, path, title,  
    817912                 size=(EDITOR_WIDTH, EDITOR_HEIGTH), *args, **kwds): 
     913        """ 
     914        Init 
     915        """ 
    818916        kwds["title"] = title 
    819917        kwds["size"] = size 
     
    9191017# User can change the name of the model (only with single functional model) 
    9201018#P1_model:  
    921 from sans.models.%s import %s as P1 
     1019#from sans.models.%s import %s as P1 
     1020#from %s import Model as P1  
    9221021 
    9231022#P2_model:  
    924 from sans.models.%s import %s as P2 
     1023#from sans.models.%s import %s as P2 
     1024#from %s import Model as P2  
    9251025import os 
    9261026import sys 
     
    9611061        ## Define parameters 
    9621062        self._set_params() 
    963         ## New parameter:Scaling factor 
    964         self.params['scale_factor'] = 1 
     1063        ## New parameter:scaling_factor 
     1064        self.params['scale_factor'] = %s 
    9651065         
    9661066        ## Parameter details [units, min, max] 
     
    11521252    def run(self, x = 0.0): 
    11531253        self._set_scale_factor() 
    1154         return self.params['scale_factor'] * \ 
     1254        return self.params['scale_factor'] %s \ 
    11551255(self.p_model1.run(x) %s self.p_model2.run(x)) 
    11561256     
    11571257    def runXY(self, x = 0.0): 
    11581258        self._set_scale_factor() 
    1159         return self.params['scale_factor'] * \ 
     1259        return self.params['scale_factor'] %s \ 
    11601260(self.p_model1.runXY(x) %s self.p_model2.runXY(x)) 
    11611261     
     
    11641264    def evalDistribution(self, x = []): 
    11651265        self._set_scale_factor() 
    1166         return self.params['scale_factor'] * \ 
     1266        return self.params['scale_factor'] %s \ 
    11671267(self.p_model1.evalDistribution(x) %s \ 
    11681268self.p_model2.evalDistribution(x)) 
Note: See TracChangeset for help on using the changeset viewer.