[d89f09b] | 1 | |
---|
| 2 | import sys,re,string, wx |
---|
| 3 | from sans.guicomm.events import StatusEvent |
---|
| 4 | |
---|
| 5 | class SimultaneousFitPage(wx.Panel): |
---|
| 6 | """ |
---|
| 7 | Simultaneous fitting panel |
---|
| 8 | All that needs to be defined are the |
---|
| 9 | two data members window_name and window_caption |
---|
| 10 | """ |
---|
| 11 | ## Internal name for the AUI manager |
---|
| 12 | window_name = "simultaneous Fit page" |
---|
| 13 | ## Title to appear on top of the window |
---|
| 14 | window_caption = "Simultaneous Fit Page" |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | def __init__(self, parent, *args, **kwargs): |
---|
| 18 | wx.Panel.__init__(self, parent, *args, **kwargs) |
---|
| 19 | """ |
---|
| 20 | Simultaneous page display |
---|
| 21 | """ |
---|
| 22 | self.parent = parent |
---|
| 23 | self.sizer3 = wx.GridBagSizer(5,5) |
---|
| 24 | self.sizer1 = wx.GridBagSizer(5,5) |
---|
| 25 | self.sizer2 = wx.GridBagSizer(5,5) |
---|
| 26 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
| 27 | self.vbox.Add(self.sizer3) |
---|
| 28 | self.vbox.Add(self.sizer1) |
---|
| 29 | self.vbox.Add(self.sizer2) |
---|
| 30 | id = wx.NewId() |
---|
| 31 | self.btFit =wx.Button(self,id,'Constraint Fit') |
---|
| 32 | self.btFit.Bind(wx.EVT_BUTTON, self.onFit,id=id) |
---|
| 33 | self.btFit.SetToolTipString("Perform fit.") |
---|
| 34 | ix = 0 |
---|
| 35 | iy = 1 |
---|
| 36 | self.cb1 = wx.CheckBox(self, -1,'Models', (10, 10)) |
---|
| 37 | self.sizer3.Add(self.cb1,(iy, ix),(1,1),\ |
---|
| 38 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 39 | wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.select_all_model_name) |
---|
| 40 | wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.select_all_model_name) |
---|
| 41 | ix = 0 |
---|
| 42 | iy = 1 |
---|
| 43 | text=wx.StaticText(self, -1, 'Constraint') |
---|
| 44 | self.sizer2.Add(text,(iy, ix),(1,1),\ |
---|
| 45 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 46 | ix = 0 |
---|
| 47 | iy += 1 |
---|
| 48 | self.ctl2 = wx.TextCtrl(self, -1, style=wx.TE_MULTILINE) |
---|
| 49 | self.ctl2.Bind(wx.EVT_KILL_FOCUS, self._onTextEnter) |
---|
| 50 | self.sizer2.Add(self.ctl2, (iy, ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 51 | ix +=2 |
---|
| 52 | self.sizer2.Add(self.btFit,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 53 | self.params=[] |
---|
| 54 | self.model_list=[] |
---|
| 55 | self.model_toFit=[] |
---|
[1b07935d] | 56 | self.page_finder={} |
---|
[d89f09b] | 57 | iy +=1 |
---|
| 58 | self.sizer2.Add((20,20),(iy, ix)) |
---|
| 59 | self.vbox.Layout() |
---|
| 60 | self.vbox.Fit(self) |
---|
| 61 | self.SetSizer(self.vbox) |
---|
| 62 | self.Centre() |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | def onFit(self,event): |
---|
[1b07935d] | 66 | """ signal for fitting""" |
---|
| 67 | if len(self.model_toFit) >0 : |
---|
| 68 | if len(self.params)>0: |
---|
| 69 | self.set_model() |
---|
| 70 | else: |
---|
| 71 | for page in self.page_finder.iterkeys(): |
---|
| 72 | page.set_model_parameter() |
---|
| 73 | self.manager._on_simul_fit() |
---|
| 74 | else: |
---|
| 75 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
[d89f09b] | 76 | "Select at least on model to fit ")) |
---|
| 77 | def set_manager(self, manager): |
---|
| 78 | """ |
---|
| 79 | set panel manager |
---|
| 80 | @param manager: instance of plugin fitting |
---|
| 81 | """ |
---|
| 82 | self.manager = manager |
---|
| 83 | |
---|
| 84 | |
---|
| 85 | def select_all_model_name(self,event): |
---|
| 86 | """ |
---|
| 87 | check all models names |
---|
| 88 | """ |
---|
| 89 | self.model_toFit=[] |
---|
| 90 | if self.cb1.GetValue()==True: |
---|
| 91 | for item in self.model_list: |
---|
| 92 | item[0].SetValue(True) |
---|
[3b19ac9] | 93 | item[1].schedule_tofit('True') |
---|
[d89f09b] | 94 | self.model_toFit.append(item) |
---|
| 95 | else: |
---|
| 96 | for item in self.model_list: |
---|
| 97 | item[0].SetValue(False) |
---|
[3b19ac9] | 98 | item[1].schedule_tofit('False') |
---|
[d89f09b] | 99 | self.model_toFit=[] |
---|
| 100 | |
---|
| 101 | |
---|
| 102 | def add_model(self,page_finder): |
---|
| 103 | """ |
---|
| 104 | Receive a dictionary containing information to display model name |
---|
| 105 | @param page_finder: the dictionary containing models information |
---|
| 106 | """ |
---|
[1b07935d] | 107 | import copy |
---|
[d89f09b] | 108 | self.model_list=[] |
---|
| 109 | self.model_toFit=[] |
---|
| 110 | self.sizer1.Clear(True) |
---|
[1b07935d] | 111 | self.page_finder=page_finder |
---|
[d89f09b] | 112 | ix = 0 |
---|
| 113 | iy = 1 |
---|
| 114 | list=[] |
---|
| 115 | for page, value in page_finder.iteritems(): |
---|
| 116 | try: |
---|
| 117 | list=value.get_model() |
---|
| 118 | model=list[0] |
---|
| 119 | modelname=list[1] |
---|
| 120 | cb = wx.CheckBox(self, -1, modelname, (10, 10)) |
---|
| 121 | cb.SetValue(False) |
---|
| 122 | self.sizer1.Add( cb,( iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 123 | ix = 0 |
---|
| 124 | iy += 1 |
---|
| 125 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_model_name) |
---|
| 126 | self.model_list.append([cb,value,page,modelname]) |
---|
| 127 | except: |
---|
| 128 | #wx.PostEvent(self.parent.GrandParent, StatusEvent(status="Simultaneous fit: %s doesn't have a model selected yet %s" % \ |
---|
| 129 | #(value.get_data().group_id,sys.exc_value))) |
---|
| 130 | pass |
---|
| 131 | self.sizer1.Layout() |
---|
| 132 | self.vbox.Layout() |
---|
| 133 | |
---|
| 134 | def remove_model(self,delpage): |
---|
| 135 | """ |
---|
| 136 | Remove a checkbox and the name realted to a model selected on page delpage |
---|
| 137 | @param delpage: the page removed |
---|
| 138 | """ |
---|
| 139 | self.sizer1.Clear(True) |
---|
| 140 | ix = 0 |
---|
| 141 | iy = 1 |
---|
| 142 | for item in self.model_list: |
---|
| 143 | try: |
---|
| 144 | if not delpage in item: |
---|
| 145 | #print "simfitpage: redraw modelname",item[3] |
---|
| 146 | cb = wx.CheckBox(self, -1, item[3], (10, 10)) |
---|
| 147 | cb.SetValue(False) |
---|
| 148 | self.sizer1.Add( cb,( iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 149 | ix = 0 |
---|
| 150 | iy += 1 |
---|
| 151 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_model_name) |
---|
| 152 | else: |
---|
| 153 | self.model_list.remove(item) |
---|
| 154 | except: |
---|
| 155 | raise |
---|
| 156 | self.sizer1.Layout() |
---|
| 157 | self.vbox.Layout() |
---|
| 158 | |
---|
| 159 | |
---|
| 160 | def select_model_name(self,event): |
---|
| 161 | """ |
---|
| 162 | Save interformation related to checkbox and their states |
---|
| 163 | """ |
---|
| 164 | self.model_toFit=[] |
---|
| 165 | for item in self.model_list: |
---|
| 166 | if item[0].GetValue()==True: |
---|
[3b19ac9] | 167 | item[1].schedule_tofit('True') |
---|
[d89f09b] | 168 | self.model_toFit.append(item) |
---|
| 169 | else: |
---|
| 170 | if item in self.model_toFit: |
---|
| 171 | self.model_toFit.remove(item) |
---|
[3b19ac9] | 172 | item[1].schedule_tofit('False') |
---|
[d89f09b] | 173 | self.cb1.SetValue(False) |
---|
| 174 | if len(self.model_list)==len(self.model_toFit): |
---|
| 175 | self.cb1.SetValue(True) |
---|
| 176 | else: |
---|
| 177 | self.cb1.SetValue(False) |
---|
| 178 | |
---|
| 179 | |
---|
| 180 | def set_model(self): |
---|
| 181 | """ |
---|
| 182 | set_model take values in self.params which are the values enters by the user |
---|
| 183 | and try to assign them into the model concerned in self.manager. page.finder |
---|
| 184 | """ |
---|
| 185 | if len(self.params) >0: |
---|
| 186 | for item in self.model_toFit: |
---|
| 187 | #print"simfitpage: self.model_toFit",item[1] |
---|
| 188 | list=item[1].get_model() |
---|
| 189 | #print "simfitpage: list fitpanel2",list,list[0] |
---|
| 190 | model=list[0] |
---|
[1b07935d] | 191 | param_list=model.getParamList() |
---|
[d89f09b] | 192 | #print "simfitpage: on set_model ",self.params |
---|
| 193 | if self.params !=[]: |
---|
| 194 | for element in self.params: |
---|
[1b07935d] | 195 | if model.name == str(element[0]): |
---|
[d89f09b] | 196 | for item in param_list: |
---|
| 197 | if item==str(element[1]): |
---|
| 198 | #print "simfitpage: on set_model page 1",param_list |
---|
| 199 | #print "simfitpage: model name",element[0], model.name |
---|
| 200 | #print "simfitpage: param name ,param value",element[1],element[2] |
---|
[1b07935d] | 201 | self.manager.set_page_finder(model.name,element[1],\ |
---|
[d89f09b] | 202 | str(element[2])) |
---|
| 203 | #print "simfitpage:on set_model page 2",model.params['A'],self.params[2] |
---|
[1b07935d] | 204 | |
---|
[d89f09b] | 205 | |
---|
| 206 | def _onTextEnter(self,event): |
---|
| 207 | """ |
---|
| 208 | get values from the constrainst textcrtl ,parses them into model name |
---|
| 209 | parameter name and parameters values. |
---|
| 210 | store them in a list self.params .when when params is not empty set_model uses it |
---|
| 211 | to reset the appropriate model and its appropriates parameters |
---|
| 212 | """ |
---|
| 213 | value= self.ctl2.GetValue() |
---|
| 214 | self.params=[] |
---|
| 215 | #print "simfitpage: value",value |
---|
| 216 | try: |
---|
| 217 | expression='[\s,;]' |
---|
| 218 | if re.search(expression,value) !=None: |
---|
| 219 | word=re.split(expression,value) |
---|
| 220 | #print "simfitpage: when herre",word |
---|
| 221 | for item in word: |
---|
| 222 | try: |
---|
| 223 | self.params.append(self.parser_helper(item)) |
---|
| 224 | except: |
---|
| 225 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status="Loading Error: %s" % sys.exc_value)) |
---|
| 226 | else: |
---|
| 227 | try: |
---|
| 228 | self.params.append(self.parser_helper(value)) |
---|
| 229 | except: |
---|
| 230 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status="Loading Error: %s" % sys.exc_value)) |
---|
| 231 | except: |
---|
| 232 | raise |
---|
| 233 | #print "simfitpage: self.params",self.params |
---|
| 234 | |
---|
| 235 | |
---|
| 236 | |
---|
| 237 | def parser_helper(self,value): |
---|
| 238 | """ |
---|
| 239 | @return param:a list containing the name of a model ,its parameters |
---|
| 240 | value and name extracted from the constrainst controlbox |
---|
| 241 | """ |
---|
| 242 | #print "simfitpage: value",value |
---|
| 243 | if string.find(value, "=") !=-1: |
---|
| 244 | model_param= re.split("=",value) |
---|
| 245 | param_name=model_param[0] |
---|
| 246 | param_value=model_param[1] |
---|
| 247 | #print"simfitpage: ", param_name |
---|
| 248 | #print "simfitpage: ",param_value |
---|
| 249 | if string.find(param_name,".")!=-1: |
---|
| 250 | param_names= re.split("\.",param_name) |
---|
| 251 | model_name=param_names[0] |
---|
| 252 | param_name=param_names[1] |
---|
| 253 | param=[str(model_name),str(param_name),str(param_value)] |
---|
| 254 | #print "simfitpage: param",param |
---|
| 255 | return param |
---|
| 256 | else: |
---|
| 257 | raise ValueError,"cannot evaluate this expression" |
---|
| 258 | return |
---|
| 259 | else: |
---|
| 260 | raise ValueError,"Missing '=' in expression" |
---|
| 261 | |
---|
| 262 | |
---|
| 263 | |
---|