[8fbb73b] | 1 | |
---|
| 2 | import sys,re,string, wx |
---|
| 3 | from sans.guicomm.events import StatusEvent |
---|
| 4 | |
---|
| 5 | class SimultaneousFitPage(wx.ScrolledWindow): |
---|
| 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.ScrolledWindow.__init__(self, parent, *args, **kwargs) |
---|
| 19 | """ |
---|
| 20 | Simultaneous page display |
---|
| 21 | """ |
---|
| 22 | self.parent = parent |
---|
| 23 | #self.page_finder = page_finder |
---|
| 24 | self.page_finder={} |
---|
| 25 | self.sizer3 = wx.GridBagSizer(5,5) |
---|
| 26 | self.sizer1 = wx.GridBagSizer(5,5) |
---|
| 27 | #self.sizer2 = wx.GridBagSizer(5,5) |
---|
| 28 | self.sizer2 = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 29 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
| 30 | self.vbox.Add(self.sizer3) |
---|
| 31 | self.vbox.Add(self.sizer1) |
---|
| 32 | self.vbox.Add(self.sizer2) |
---|
| 33 | id = wx.NewId() |
---|
| 34 | self.btFit =wx.Button(self,id,'Constraint Fit') |
---|
| 35 | self.btFit.Bind(wx.EVT_BUTTON, self.onFit,id=id) |
---|
| 36 | self.btFit.SetToolTipString("Perform fit.") |
---|
| 37 | ix = 0 |
---|
| 38 | iy = 1 |
---|
| 39 | self.cb1 = wx.CheckBox(self, -1,'Models', (10, 10)) |
---|
| 40 | self.sizer3.Add(self.cb1,(iy, ix),(1,1),\ |
---|
| 41 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 42 | wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.select_all_model_name) |
---|
| 43 | |
---|
| 44 | text=wx.StaticText(self, -1, 'Constraint') |
---|
| 45 | self.sizer2.Add(text,0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) |
---|
| 46 | |
---|
| 47 | self.ctl2 = wx.TextCtrl(self, -1, style=wx.TE_MULTILINE) |
---|
| 48 | |
---|
| 49 | self.sizer2.Add(self.ctl2, 0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) |
---|
| 50 | |
---|
| 51 | self.sizer2.Add(self.btFit, 0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) |
---|
| 52 | self.params=[] |
---|
| 53 | self.model_list=[] |
---|
| 54 | self.model_toFit=[] |
---|
| 55 | |
---|
| 56 | #self.add_model(self.page_finder) |
---|
| 57 | self.vbox.Layout() |
---|
| 58 | self.vbox.Fit(self) |
---|
| 59 | self.SetSizer(self.vbox) |
---|
| 60 | self.SetScrollbars(20,20,55,40) |
---|
| 61 | self.Centre() |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | def onFit(self,event): |
---|
| 65 | """ signal for fitting""" |
---|
| 66 | for page in self.page_finder.iterkeys(): |
---|
| 67 | page.set_model_parameter() |
---|
| 68 | if len(self.model_toFit) >= 1 : |
---|
| 69 | self._onTextEnter() |
---|
| 70 | self.set_model() |
---|
| 71 | self.manager._on_simul_fit() |
---|
| 72 | else: |
---|
| 73 | wx.PostEvent(self.parent.Parent, StatusEvent(status=\ |
---|
| 74 | "Select at least one model to fit ")) |
---|
| 75 | |
---|
| 76 | |
---|
| 77 | |
---|
| 78 | def set_manager(self, manager): |
---|
| 79 | """ |
---|
| 80 | set panel manager |
---|
| 81 | @param manager: instance of plugin fitting |
---|
| 82 | """ |
---|
| 83 | self.manager = manager |
---|
| 84 | self.add_model( self.manager.page_finder) |
---|
| 85 | |
---|
| 86 | def select_all_model_name(self,event): |
---|
| 87 | """ |
---|
| 88 | check all models names |
---|
| 89 | """ |
---|
| 90 | self.model_toFit=[] |
---|
| 91 | if self.cb1.GetValue()==True: |
---|
| 92 | for item in self.model_list: |
---|
| 93 | item[0].SetValue(True) |
---|
| 94 | #item[1].schedule_tofit(1) |
---|
| 95 | self.manager.schedule_for_fit( value=1,fitproblem =item[1]) |
---|
| 96 | self.model_toFit.append(item) |
---|
| 97 | else: |
---|
| 98 | #print"simfit: deselected all" |
---|
| 99 | |
---|
| 100 | for item in self.model_list: |
---|
| 101 | item[0].SetValue(False) |
---|
| 102 | self.manager.schedule_for_fit( value=0,fitproblem =item[1]) |
---|
| 103 | |
---|
| 104 | self.model_toFit=[] |
---|
| 105 | |
---|
| 106 | |
---|
| 107 | def add_model(self,page_finder): |
---|
| 108 | """ |
---|
| 109 | Receive a dictionary containing information to display model name |
---|
| 110 | @param page_finder: the dictionary containing models information |
---|
| 111 | """ |
---|
| 112 | if len(self.model_list)>0: |
---|
| 113 | for item in self.model_list: |
---|
| 114 | #print "went here to clear" |
---|
| 115 | item[0].SetValue(False) |
---|
| 116 | self.manager.schedule_for_fit( value=0,fitproblem =item[1]) |
---|
| 117 | self.model_list=[] |
---|
| 118 | self.model_toFit=[] |
---|
| 119 | self.sizer1.Clear(True) |
---|
| 120 | self.page_finder=page_finder |
---|
| 121 | self.cb1.SetValue(False) |
---|
| 122 | ix = 0 |
---|
| 123 | iy = 1 |
---|
| 124 | list=[] |
---|
| 125 | # if len(self.page_finder)>0: |
---|
| 126 | for page, value in page_finder.iteritems(): |
---|
| 127 | try: |
---|
| 128 | list = value.get_model() |
---|
| 129 | model = list[0] |
---|
| 130 | modelname = list[1] |
---|
| 131 | cb = wx.CheckBox(self, -1, modelname, (10, 10)) |
---|
| 132 | cb.SetValue(False) |
---|
| 133 | self.sizer1.Add( cb,( iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 134 | ix = 0 |
---|
| 135 | iy += 1 |
---|
| 136 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_model_name) |
---|
| 137 | self.model_list.append([cb,value,page,modelname]) |
---|
| 138 | except: |
---|
| 139 | #wx.PostEvent(self.parent.GrandParent, StatusEvent(status="Simultaneous fit: %s doesn't have a model selected yet %s" % \ |
---|
| 140 | #(value.get_data().group_id,sys.exc_value))) |
---|
| 141 | pass |
---|
| 142 | iy +=1 |
---|
| 143 | self.sizer1.Add((20,20),( iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 144 | self.sizer1.Layout() |
---|
| 145 | self.vbox.Layout() |
---|
| 146 | |
---|
| 147 | def remove_model(self,delpage): |
---|
| 148 | """ |
---|
| 149 | Remove a checkbox and the name related to a model selected on page delpage |
---|
| 150 | @param delpage: the page removed |
---|
| 151 | """ |
---|
| 152 | #print "self.model_list",self.model_list |
---|
| 153 | |
---|
| 154 | self.model_list=[] |
---|
| 155 | self.model_toFit=[] |
---|
| 156 | self.sizer1.Clear(True) |
---|
| 157 | |
---|
| 158 | self.cb1.SetValue(False) |
---|
| 159 | ix = 0 |
---|
| 160 | iy = 1 |
---|
| 161 | list=[] |
---|
| 162 | for page, value in self.page_finder.iteritems(): |
---|
| 163 | try: |
---|
| 164 | if page!= delpage: |
---|
| 165 | list = value.get_model() |
---|
| 166 | model = list[0] |
---|
| 167 | modelname = list[1] |
---|
| 168 | cb = wx.CheckBox(self, -1, modelname, (10, 10)) |
---|
| 169 | cb.SetValue(False) |
---|
| 170 | self.sizer1.Add( cb,( iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 171 | ix = 0 |
---|
| 172 | iy += 1 |
---|
| 173 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_model_name) |
---|
| 174 | self.model_list.append([cb,value,page,modelname]) |
---|
| 175 | except: |
---|
| 176 | #wx.PostEvent(self.parent.GrandParent, StatusEvent(status="Simultaneous fit: %s doesn't have a model selected yet %s" % \ |
---|
| 177 | #(value.get_data().group_id,sys.exc_value))) |
---|
| 178 | pass |
---|
| 179 | iy +=1 |
---|
| 180 | self.sizer1.Add((20,20),( iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 181 | self.sizer1.Layout() |
---|
| 182 | self.vbox.Layout() |
---|
| 183 | |
---|
| 184 | def select_model_name(self,event): |
---|
| 185 | """ |
---|
| 186 | Save interformation related to checkbox and their states |
---|
| 187 | """ |
---|
| 188 | self.model_toFit=[] |
---|
| 189 | for item in self.model_list: |
---|
| 190 | if item[0].GetValue()==True: |
---|
| 191 | #item[1].schedule_tofit('True') |
---|
| 192 | #item[1].schedule_tofit(1) |
---|
| 193 | self.model_toFit.append(item) |
---|
| 194 | self.manager.schedule_for_fit( value=1,fitproblem =item[1]) |
---|
| 195 | else: |
---|
| 196 | #print"simfit: deselected one" |
---|
| 197 | self.manager.schedule_for_fit( value=0,fitproblem =item[1]) |
---|
| 198 | if item in self.model_toFit: |
---|
| 199 | self.model_toFit.remove(item) |
---|
| 200 | self.cb1.SetValue(False) |
---|
| 201 | |
---|
| 202 | if len(self.model_list)==len(self.model_toFit): |
---|
| 203 | self.cb1.SetValue(True) |
---|
| 204 | else: |
---|
| 205 | self.cb1.SetValue(False) |
---|
| 206 | |
---|
| 207 | |
---|
| 208 | |
---|
| 209 | def set_model(self): |
---|
| 210 | """ |
---|
| 211 | set_model take values in self.params which are the values enters by the user |
---|
| 212 | and try to assign them into the model concerned in self.manager. page.finder |
---|
| 213 | """ |
---|
| 214 | if len(self.params) >0: |
---|
| 215 | for item in self.model_toFit: |
---|
| 216 | #print"simfitpage: self.model_toFit",item[1] |
---|
| 217 | list=item[1].get_model() |
---|
| 218 | #print "simfitpage: list fitpanel2",list,list[0] |
---|
| 219 | model=list[0] |
---|
| 220 | param_list=model.getParamList() |
---|
| 221 | #print "simfitpage: on set_model self.params ",self.params |
---|
| 222 | if self.params !=[]: |
---|
| 223 | for element in self.params: |
---|
| 224 | if model.name == str(element[0]): |
---|
| 225 | for item in param_list: |
---|
| 226 | if item==str(element[1]): |
---|
| 227 | #print "simfitpage: on set_model page 1",param_list |
---|
| 228 | #print "simfitpage: model name",element[0], model.name |
---|
| 229 | #print "simfitpage: param name ,param value",element[1],element[2] |
---|
| 230 | self.manager.set_page_finder(model.name,element[1],\ |
---|
| 231 | str(element[2])) |
---|
| 232 | #print "simfitpage:on set_model page 2",model.params['A'],self.params[2] |
---|
| 233 | |
---|
| 234 | def _onTextEnter(self): |
---|
| 235 | """ |
---|
| 236 | get values from the constrainst textcrtl ,parses them into model name |
---|
| 237 | parameter name and parameters values. |
---|
| 238 | store them in a list self.params .when when params is not empty set_model uses it |
---|
| 239 | to reset the appropriate model and its appropriates parameters |
---|
| 240 | """ |
---|
| 241 | value= self.ctl2.GetValue() |
---|
| 242 | if value: |
---|
| 243 | self.params=[] |
---|
| 244 | #print "simfitpage: value",value |
---|
| 245 | try: |
---|
| 246 | expression='[\s,;]' |
---|
| 247 | if re.search(expression,value) !=None: |
---|
| 248 | word=re.split(expression,value) |
---|
| 249 | #print "simfitpage: when herre",word |
---|
| 250 | for item in word: |
---|
| 251 | self.params.append(self.parser_helper(item)) |
---|
| 252 | else: |
---|
| 253 | self.params.append(self.parser_helper(value)) |
---|
| 254 | except: |
---|
| 255 | #raise |
---|
| 256 | wx.PostEvent(self.parent.Parent, StatusEvent(status="Constraint Error: %s" % sys.exc_value)) |
---|
| 257 | #print "simfitpage: self.params",self.params |
---|
| 258 | def new_parser_helper(self,value): |
---|
| 259 | """ |
---|
| 260 | @return param:a list containing the name of a model ,its parameters |
---|
| 261 | value and name extracted from the constrainst controlbox |
---|
| 262 | """ |
---|
| 263 | from sans.guiframe import utils |
---|
| 264 | mylist=["=","<",">","<=", ">="] |
---|
| 265 | for item in mylist: |
---|
| 266 | if utils.look_for_tag( value,str(item))[0]: |
---|
| 267 | #print "went here" |
---|
| 268 | model_param = utils.split_text(str(item), value,1) |
---|
| 269 | param_name = model_param[0] |
---|
| 270 | param_value = model_param[1] |
---|
| 271 | #print "simpage",utils.look_for_tag(param_name,"\.")[0] |
---|
| 272 | if utils.look_for_tag(param_name,"\.")[0]: |
---|
| 273 | param_names = utils.split_text("\.",param_name,1) |
---|
| 274 | model_name = param_names[0] |
---|
| 275 | param_name = param_names[1] |
---|
| 276 | ##### just added |
---|
| 277 | #print "simfitpage: param name",model_name,param_name |
---|
| 278 | |
---|
| 279 | param=[str(model_name),param_name,str(param_value),"="] |
---|
| 280 | #print "simfitpage: param",param |
---|
| 281 | return param |
---|
| 282 | else: |
---|
| 283 | #raise ValueError,"cannot evaluate this expression" |
---|
| 284 | wx.PostEvent(self.parent.Parent, |
---|
| 285 | StatusEvent(status="cannot evaluate this expression")) |
---|
| 286 | return |
---|
| 287 | elif utils.look_for_tag( value,"<")[0]: |
---|
| 288 | model_param = utils.split_text("<", value,2) |
---|
| 289 | if len(model_param)== 2: |
---|
| 290 | try: |
---|
| 291 | param_name = str(model_param[0]) |
---|
| 292 | param_value = float(model_param[1]) |
---|
| 293 | param=[str(model_name),param_name,param_value,"<"] |
---|
| 294 | #print "simfitpage: param",param |
---|
| 295 | return param |
---|
| 296 | except: |
---|
| 297 | wx.PostEvent(self.parent.Parent, StatusEvent(status="\ |
---|
| 298 | could read this expression%s" % sys.exc_value)) |
---|
| 299 | return |
---|
| 300 | else: |
---|
| 301 | #raise ValueError,"Missing '=' in expression" |
---|
| 302 | wx.PostEvent(self.parent.Parent, StatusEvent(status="Missing '=' in expression")) |
---|
| 303 | |
---|
| 304 | |
---|
| 305 | |
---|
| 306 | def parser_helper(self,value): |
---|
| 307 | """ |
---|
| 308 | @return param:a list containing the name of a model ,its parameters |
---|
| 309 | value and name extracted from the constrainst controlbox |
---|
| 310 | """ |
---|
| 311 | #print "simfitpage: value",value |
---|
| 312 | if string.find(value, "=") !=-1: |
---|
| 313 | model_param= re.split("=",value) |
---|
| 314 | param_name=model_param[0] |
---|
| 315 | param_value=model_param[1] |
---|
| 316 | #print"simfitpage: ", param_name |
---|
| 317 | #print "simfitpage: ",param_value |
---|
| 318 | if string.find(param_name,".")!=-1: |
---|
| 319 | param_names= re.split("\.",param_name) |
---|
| 320 | model_name=param_names[0] |
---|
| 321 | param_name=param_names[1] |
---|
| 322 | ##### just added |
---|
| 323 | #print "simfitpage: param name",model_name,param_name |
---|
| 324 | |
---|
| 325 | param=[str(model_name),param_name,str(param_value)] |
---|
| 326 | #print "simfitpage: param",param |
---|
| 327 | return param |
---|
| 328 | else: |
---|
| 329 | #raise ValueError,"cannot evaluate this expression" |
---|
| 330 | wx.PostEvent(self.parent.Parent, |
---|
| 331 | StatusEvent(status="cannot evaluate this expression")) |
---|
| 332 | return |
---|
| 333 | else: |
---|
| 334 | #raise ValueError,"Missing '=' in expression" |
---|
| 335 | wx.PostEvent(self.parent.Parent, StatusEvent(status="Missing '=' in expression")) |
---|
| 336 | |
---|
| 337 | |
---|
| 338 | |
---|