[1b07935d] | 1 | import sys |
---|
| 2 | import wx |
---|
| 3 | import wx.lib |
---|
| 4 | import numpy |
---|
| 5 | import copy |
---|
| 6 | |
---|
| 7 | from sans.guicomm.events import StatusEvent |
---|
| 8 | (ModelEventbox, EVT_MODEL_BOX) = wx.lib.newevent.NewEvent() |
---|
| 9 | _BOX_WIDTH = 80 |
---|
| 10 | |
---|
| 11 | def format_number(value, high=False): |
---|
| 12 | """ |
---|
| 13 | Return a float in a standardized, human-readable formatted string |
---|
| 14 | """ |
---|
| 15 | try: |
---|
| 16 | value = float(value) |
---|
| 17 | except: |
---|
| 18 | print "returning 0" |
---|
| 19 | return "0" |
---|
| 20 | |
---|
| 21 | if high: |
---|
| 22 | return "%-6.4g" % value |
---|
| 23 | else: |
---|
| 24 | return "%-5.3g" % value |
---|
| 25 | |
---|
| 26 | |
---|
| 27 | class ModelPage(wx.Panel): |
---|
| 28 | """ |
---|
| 29 | FitPanel class contains fields allowing to display results when |
---|
| 30 | fitting a model and one data |
---|
| 31 | @note: For Fit to be performed the user should check at least one parameter |
---|
| 32 | on fit Panel window. |
---|
| 33 | |
---|
| 34 | """ |
---|
| 35 | ## Internal name for the AUI manager |
---|
| 36 | window_name = "Fit page" |
---|
| 37 | ## Title to appear on top of the window |
---|
| 38 | window_caption = "Fit Page" |
---|
| 39 | |
---|
| 40 | |
---|
[2dbb681] | 41 | def __init__(self, parent,model, *args, **kwargs): |
---|
[1b07935d] | 42 | wx.Panel.__init__(self, parent, *args, **kwargs) |
---|
| 43 | """ |
---|
| 44 | Initialization of the Panel |
---|
| 45 | """ |
---|
| 46 | self.manager = None |
---|
| 47 | self.parent = parent |
---|
| 48 | self.event_owner=None |
---|
| 49 | #panel interface |
---|
| 50 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
| 51 | self.sizer3 = wx.GridBagSizer(5,5) |
---|
[49b7efa] | 52 | self.sizer1 = wx.GridBagSizer(5,5) |
---|
[1b07935d] | 53 | self.sizer2 = wx.GridBagSizer(5,5) |
---|
[00561739] | 54 | self.sizer4 = wx.GridBagSizer(5,5) |
---|
[04edd0d] | 55 | self.sizer5 = wx.GridBagSizer(5,5) |
---|
| 56 | self.static_line_1 = wx.StaticLine(self, -1) |
---|
[1b07935d] | 57 | self.modelbox = wx.ComboBox(self, -1) |
---|
| 58 | id = wx.NewId() |
---|
| 59 | self.vbox.Add(self.sizer3) |
---|
[49b7efa] | 60 | self.vbox.Add(self.sizer1) |
---|
[1b07935d] | 61 | self.vbox.Add(self.sizer2) |
---|
[04edd0d] | 62 | self.vbox.Add(self.static_line_1, 0, wx.EXPAND, 0) |
---|
| 63 | self.vbox.Add(self.sizer5) |
---|
[00561739] | 64 | self.vbox.Add(self.sizer4) |
---|
| 65 | |
---|
| 66 | id = wx.NewId() |
---|
| 67 | self.btClose =wx.Button(self,id,'Close') |
---|
| 68 | self.btClose.Bind(wx.EVT_BUTTON, self.onClose,id=id) |
---|
| 69 | self.btClose.SetToolTipString("Close page.") |
---|
| 70 | |
---|
| 71 | ix = 12 |
---|
| 72 | iy = 1 |
---|
| 73 | self.sizer4.Add( self.btClose,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[1b07935d] | 74 | ix = 0 |
---|
| 75 | iy = 1 |
---|
| 76 | self.sizer3.Add(wx.StaticText(self,-1,'Model'),(iy,ix),(1,1)\ |
---|
| 77 | , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 78 | ix += 1 |
---|
| 79 | self.sizer3.Add(self.modelbox,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 80 | # contains link between model ,all its parameters, and panel organization |
---|
| 81 | self.parameters=[] |
---|
| 82 | #contains link between a model and selected parameters to fit |
---|
| 83 | self.param_toFit=[] |
---|
| 84 | # model on which the fit would be performed |
---|
| 85 | self.model=model |
---|
[2dbb681] | 86 | try: |
---|
| 87 | print"init modelpage",model.name |
---|
| 88 | self.set_panel(model) |
---|
| 89 | except: |
---|
| 90 | raise |
---|
[1b07935d] | 91 | # preview selected model name |
---|
| 92 | self.prevmodel_name=model.__class__.__name__ |
---|
| 93 | self.modelbox.SetValue(self.prevmodel_name) |
---|
| 94 | # flag to check if the user has selected a new model in the combox box |
---|
| 95 | self.model_hasChanged=False |
---|
| 96 | #dictionary of model name and model class |
---|
| 97 | self.model_list_box={} |
---|
| 98 | # Data1D to make a deep comparison between 2 Data1D for checking data |
---|
| 99 | #change |
---|
| 100 | self.vbox.Layout() |
---|
| 101 | self.vbox.Fit(self) |
---|
| 102 | self.SetSizer(self.vbox) |
---|
| 103 | self.Centre() |
---|
[2dbb681] | 104 | |
---|
[1b07935d] | 105 | |
---|
[00561739] | 106 | def onClose(self,event): |
---|
| 107 | """ close the page associated with this panel""" |
---|
| 108 | self.GrandParent.onClose() |
---|
| 109 | |
---|
[1b07935d] | 110 | def set_owner(self,owner): |
---|
| 111 | """ |
---|
| 112 | set owner of fitpage |
---|
| 113 | @param owner: the class responsible of plotting |
---|
| 114 | """ |
---|
| 115 | self.event_owner=owner |
---|
| 116 | |
---|
| 117 | |
---|
| 118 | def set_manager(self, manager): |
---|
| 119 | """ |
---|
| 120 | set panel manager |
---|
| 121 | @param manager: instance of plugin fitting |
---|
| 122 | """ |
---|
| 123 | self.manager = manager |
---|
| 124 | |
---|
| 125 | def populate_box(self, dict): |
---|
| 126 | """ |
---|
| 127 | Populate each combox box of each page |
---|
| 128 | @param page: the page to populate |
---|
| 129 | """ |
---|
| 130 | id=0 |
---|
| 131 | self.model_list_box=dict |
---|
[2dbb681] | 132 | list_name=[] |
---|
| 133 | for item in self.model_list_box.itervalues(): |
---|
| 134 | name = item.__name__ |
---|
| 135 | model=item() |
---|
| 136 | if hasattr(model, "name"): |
---|
| 137 | name = model.name |
---|
| 138 | list_name.append(name) |
---|
| 139 | list_name.sort() |
---|
| 140 | for name in list_name: |
---|
[1b07935d] | 141 | self.modelbox.Insert(name,int(id)) |
---|
| 142 | id+=1 |
---|
[2dbb681] | 143 | wx.EVT_COMBOBOX(self.modelbox,-1, self._on_select_model) |
---|
[1b07935d] | 144 | return 0 |
---|
[060b857] | 145 | |
---|
[2dbb681] | 146 | def set_page(self, model,description=None): |
---|
[00561739] | 147 | #print " modelpage: set_page was called",model |
---|
[060b857] | 148 | self.model=model |
---|
[2dbb681] | 149 | name = self.model.__class__.__name__ |
---|
[060b857] | 150 | if hasattr(self.model, "name"): |
---|
| 151 | name = self.model.name |
---|
[2dbb681] | 152 | |
---|
| 153 | |
---|
[060b857] | 154 | self.modelbox.SetValue(name) |
---|
[2dbb681] | 155 | self.set_panel(self.model) |
---|
[060b857] | 156 | self.manager.draw_model(self.model) |
---|
[2dbb681] | 157 | |
---|
[1b07935d] | 158 | def _on_select_model(self,event): |
---|
| 159 | """ |
---|
| 160 | react when a model is selected from page's combo box |
---|
| 161 | post an event to its owner to draw an appropriate theory |
---|
| 162 | """ |
---|
[00561739] | 163 | #print "modelpage: self.model_list_box ",self.model_list_box |
---|
[1b07935d] | 164 | for item in self.model_list_box.itervalues(): |
---|
[2dbb681] | 165 | name = item.__name__ |
---|
| 166 | items=item() |
---|
| 167 | if hasattr(items, "name"): |
---|
| 168 | name = items.name |
---|
| 169 | print "fitpage: _on_select_model model name",name ,event.GetString() |
---|
| 170 | if name ==event.GetString(): |
---|
| 171 | model=items |
---|
| 172 | print "fitpage: _on_select_model model name",name ,event.GetString() |
---|
[b2c3225] | 173 | self.model= model |
---|
[2dbb681] | 174 | self.manager.draw_model(model) |
---|
| 175 | self.set_panel(model) |
---|
[1b07935d] | 176 | def set_model_name(self,name): |
---|
| 177 | """ |
---|
| 178 | set model name. set also self.model_hasChanged to true is the model |
---|
| 179 | type has changed or false if it didn't |
---|
| 180 | @param name: model 's name |
---|
| 181 | """ |
---|
| 182 | self.model_hasChanged=False |
---|
| 183 | if (name != self.prevmodel_name): |
---|
| 184 | self.model_hasChanged=True |
---|
| 185 | |
---|
| 186 | self.prevmodel_name=self.modelbox.GetValue() |
---|
| 187 | |
---|
| 188 | |
---|
| 189 | def get_model_box(self): |
---|
| 190 | """ return reference to combox box self.model""" |
---|
| 191 | return self.modelbox |
---|
| 192 | |
---|
| 193 | |
---|
| 194 | def get_param_list(self): |
---|
| 195 | """ |
---|
| 196 | @return self.param_toFit: list containing references to TextCtrl |
---|
| 197 | checked.Theses TextCtrl will allow reference to parameters to fit. |
---|
| 198 | @raise: if return an empty list of parameter fit will nnote work |
---|
| 199 | properly so raise ValueError,"missing parameter to fit" |
---|
| 200 | """ |
---|
| 201 | if self.param_toFit !=[]: |
---|
| 202 | return self.param_toFit |
---|
| 203 | else: |
---|
| 204 | raise ValueError,"missing parameter to fit" |
---|
| 205 | |
---|
| 206 | |
---|
[2dbb681] | 207 | def set_panel(self,model): |
---|
[1b07935d] | 208 | """ |
---|
| 209 | Build the panel from the model content |
---|
| 210 | @param model: the model selected in combo box for fitting purpose |
---|
| 211 | """ |
---|
[2dbb681] | 212 | |
---|
[1b07935d] | 213 | self.sizer2.Clear(True) |
---|
[49b7efa] | 214 | self.sizer1.Clear(True) |
---|
[04edd0d] | 215 | self.sizer5.Clear(True) |
---|
[1b07935d] | 216 | self.parameters = [] |
---|
| 217 | self.param_toFit=[] |
---|
| 218 | self.model = model |
---|
| 219 | keys = self.model.getParamList() |
---|
| 220 | keys.sort() |
---|
[5cab7d3] | 221 | description=None |
---|
| 222 | if hasattr(self.model,'description'): |
---|
| 223 | description =model.description |
---|
[04edd0d] | 224 | disp_list=self.model.getDispParamList() |
---|
| 225 | ip=0 |
---|
| 226 | iq=1 |
---|
| 227 | if len(disp_list)>0: |
---|
| 228 | disp = wx.StaticText(self, -1, 'Dispersion') |
---|
| 229 | self.sizer5.Add(disp,( iq, ip),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 230 | ip += 1 |
---|
| 231 | values = wx.StaticText(self, -1, 'Values') |
---|
| 232 | self.sizer5.Add(values,( iq, ip),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 233 | |
---|
| 234 | disp_list.sort() |
---|
[2dbb681] | 235 | print "went here",self.model.name,model.description |
---|
[1b07935d] | 236 | iy = 1 |
---|
| 237 | ix = 0 |
---|
[49b7efa] | 238 | self.cb0 = wx.StaticText(self, -1,'Model Description:') |
---|
| 239 | self.sizer1.Add(self.cb0,(iy, ix),(1,1),\ |
---|
| 240 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[d171299] | 241 | iy += 1 |
---|
| 242 | |
---|
| 243 | self.cb01 = wx.StaticText(self, -1,str(description),style=wx.ALIGN_LEFT) |
---|
| 244 | self.cb01.Wrap(400) |
---|
| 245 | #self.cb01 = wx.StaticText(self, -1,str(description),(45, 25),style=wx.ALIGN_LEFT) |
---|
| 246 | |
---|
[49b7efa] | 247 | self.sizer1.Add(self.cb01,(iy, ix),(1,1),\ |
---|
| 248 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 249 | ix = 0 |
---|
| 250 | iy = 1 |
---|
[1b07935d] | 251 | self.cb1 = wx.StaticText(self, -1,'Parameters') |
---|
| 252 | self.sizer2.Add(self.cb1,(iy, ix),(1,1),\ |
---|
| 253 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 254 | ix +=1 |
---|
| 255 | self.text2_2 = wx.StaticText(self, -1, 'Values') |
---|
| 256 | self.sizer2.Add(self.text2_2,(iy, ix),(1,1),\ |
---|
| 257 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 258 | ix +=1 |
---|
| 259 | self.text2_4 = wx.StaticText(self, -1, 'Units') |
---|
| 260 | self.sizer2.Add(self.text2_4,(iy, ix),(1,1),\ |
---|
| 261 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 262 | self.text2_4.Hide() |
---|
| 263 | for item in keys: |
---|
[04edd0d] | 264 | if not item in disp_list: |
---|
| 265 | iy += 1 |
---|
| 266 | ix = 0 |
---|
| 267 | cb=wx.StaticText(self, -1, item) |
---|
| 268 | self.sizer2.Add( cb,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 269 | ix += 1 |
---|
| 270 | value= self.model.getParam(item) |
---|
| 271 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
| 272 | ctl1.SetValue(str (format_number(value))) |
---|
| 273 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
| 274 | ctl1.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
| 275 | self.sizer2.Add(ctl1, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 276 | ix +=1 |
---|
| 277 | |
---|
| 278 | # Units |
---|
| 279 | try: |
---|
| 280 | units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
| 281 | except: |
---|
| 282 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
| 283 | self.sizer2.Add(units, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 284 | else: |
---|
| 285 | ip = 0 |
---|
| 286 | iq += 1 |
---|
| 287 | cb = wx.CheckBox(self, -1, item, (10, 10)) |
---|
| 288 | cb.SetValue(False) |
---|
| 289 | self.sizer5.Add( cb,( iq, ip),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 290 | wx.EVT_CHECKBOX(self, cb.GetId(), self._on_select_model) |
---|
| 291 | |
---|
| 292 | ip += 1 |
---|
| 293 | value= self.model.getParam(item) |
---|
| 294 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
| 295 | ctl1.SetValue(str (format_number(value))) |
---|
| 296 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
| 297 | ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
| 298 | self.sizer5.Add(ctl1, (iq,ip),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[1b07935d] | 299 | #save data |
---|
| 300 | self.parameters.append([cb,ctl1]) |
---|
[04edd0d] | 301 | iy+=1 |
---|
| 302 | self.sizer2.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 303 | |
---|
[1b07935d] | 304 | #Display units text on panel |
---|
| 305 | for item in keys: |
---|
| 306 | if self.model.details[item][0]!='': |
---|
| 307 | self.text2_4.Show() |
---|
| 308 | break |
---|
| 309 | else: |
---|
| 310 | self.text2_4.Hide() |
---|
| 311 | self.vbox.Layout() |
---|
| 312 | self.GrandParent.GetSizer().Layout() |
---|
[04edd0d] | 313 | |
---|
[1b07935d] | 314 | |
---|
| 315 | def _onparamEnter(self,event): |
---|
| 316 | """ |
---|
| 317 | when enter value on panel redraw model according to changed |
---|
| 318 | """ |
---|
| 319 | self.set_model_parameter() |
---|
| 320 | |
---|
| 321 | def set_model_parameter(self): |
---|
| 322 | if len(self.parameters) !=0 and self.model !=None: |
---|
| 323 | for item in self.parameters: |
---|
| 324 | try: |
---|
[060b857] | 325 | name=str(item[0].GetLabelText()) |
---|
[1b07935d] | 326 | value= float(item[1].GetValue()) |
---|
| 327 | self.model.setParam(name,value) |
---|
| 328 | except: |
---|
| 329 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
[442895f] | 330 | "Model Drawing Error:wrong value entered : %s"% sys.exc_value)) |
---|
[2dbb681] | 331 | self.manager.draw_model(self.model) |
---|
[1b07935d] | 332 | |
---|