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