[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 | |
---|
[f39511b] | 27 | class ModelPage(wx.ScrolledWindow): |
---|
[1b07935d] | 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): |
---|
[f39511b] | 42 | wx.ScrolledWindow.__init__(self, parent, *args, **kwargs) |
---|
[1b07935d] | 43 | """ |
---|
| 44 | Initialization of the Panel |
---|
| 45 | """ |
---|
[f39511b] | 46 | #self.scroll = wx.ScrolledWindow(self) |
---|
[1b07935d] | 47 | self.manager = None |
---|
| 48 | self.parent = parent |
---|
| 49 | self.event_owner=None |
---|
| 50 | #panel interface |
---|
| 51 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
| 52 | self.sizer3 = wx.GridBagSizer(5,5) |
---|
[49b7efa] | 53 | self.sizer1 = wx.GridBagSizer(5,5) |
---|
[1b07935d] | 54 | self.sizer2 = wx.GridBagSizer(5,5) |
---|
[00561739] | 55 | self.sizer4 = wx.GridBagSizer(5,5) |
---|
[04edd0d] | 56 | self.sizer5 = wx.GridBagSizer(5,5) |
---|
| 57 | self.static_line_1 = wx.StaticLine(self, -1) |
---|
[1b07935d] | 58 | self.modelbox = wx.ComboBox(self, -1) |
---|
| 59 | id = wx.NewId() |
---|
| 60 | self.vbox.Add(self.sizer3) |
---|
[49b7efa] | 61 | self.vbox.Add(self.sizer1) |
---|
[1b07935d] | 62 | self.vbox.Add(self.sizer2) |
---|
[04edd0d] | 63 | self.vbox.Add(self.static_line_1, 0, wx.EXPAND, 0) |
---|
| 64 | self.vbox.Add(self.sizer5) |
---|
[00561739] | 65 | self.vbox.Add(self.sizer4) |
---|
| 66 | |
---|
| 67 | id = wx.NewId() |
---|
| 68 | self.btClose =wx.Button(self,id,'Close') |
---|
| 69 | self.btClose.Bind(wx.EVT_BUTTON, self.onClose,id=id) |
---|
| 70 | self.btClose.SetToolTipString("Close page.") |
---|
[dc317d1] | 71 | ix = 0 |
---|
| 72 | iy = 1 |
---|
| 73 | self.sizer4.Add(wx.StaticText(self, -1, 'Min'),(iy, ix),(1,1),\ |
---|
| 74 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 75 | ix += 2 |
---|
| 76 | self.sizer4.Add(wx.StaticText(self, -1, 'Max'),(iy, ix),(1,1),\ |
---|
| 77 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 78 | ix = 0 |
---|
| 79 | iy += 1 |
---|
| 80 | self.sizer4.Add(wx.StaticText(self, -1, 'x range'),(iy, ix),(1,1),\ |
---|
| 81 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[2e10b70] | 82 | |
---|
| 83 | qmin= 0.001 |
---|
| 84 | qmax= 1.0 |
---|
[dc317d1] | 85 | ix += 1 |
---|
| 86 | self.xmin = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
[2e10b70] | 87 | self.xmin.SetValue(format_number(qmin)) |
---|
[dc317d1] | 88 | self.xmin.SetToolTipString("Minimun value of x in linear scale.") |
---|
| 89 | self.xmin.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
| 90 | self.xmin.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
| 91 | self.sizer4.Add(self.xmin,(iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[00561739] | 92 | |
---|
[dc317d1] | 93 | |
---|
| 94 | ix += 2 |
---|
| 95 | self.xmax = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
[2e10b70] | 96 | self.xmax.SetValue(format_number(qmax)) |
---|
[dc317d1] | 97 | self.xmax.SetToolTipString("Maximum value of x in linear scale.") |
---|
| 98 | self.xmax.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
| 99 | self.xmax.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
| 100 | |
---|
| 101 | self.sizer4.Add(self.xmax,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 102 | ix = 0 |
---|
| 103 | iy += 1 |
---|
| 104 | self.sizer4.Add((20,20),(iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 105 | ix +=3 |
---|
[00561739] | 106 | self.sizer4.Add( self.btClose,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[1b07935d] | 107 | ix = 0 |
---|
| 108 | iy = 1 |
---|
| 109 | self.sizer3.Add(wx.StaticText(self,-1,'Model'),(iy,ix),(1,1)\ |
---|
| 110 | , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 111 | ix += 1 |
---|
| 112 | self.sizer3.Add(self.modelbox,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[f39511b] | 113 | ix = 0 |
---|
| 114 | iy += 1 |
---|
| 115 | self.model_view= wx.CheckBox(self, -1, "View in 2D", (10, 10)) |
---|
| 116 | wx.EVT_CHECKBOX(self, self.model_view.GetId(), self.onModel2D) |
---|
| 117 | self.sizer3.Add(self.model_view,(iy,ix),(1,1),\ |
---|
| 118 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[1b07935d] | 119 | # contains link between model ,all its parameters, and panel organization |
---|
| 120 | self.parameters=[] |
---|
| 121 | #contains link between a model and selected parameters to fit |
---|
| 122 | self.param_toFit=[] |
---|
| 123 | # model on which the fit would be performed |
---|
| 124 | self.model=model |
---|
[2dbb681] | 125 | try: |
---|
[f39511b] | 126 | #print"init modelpage",model.name |
---|
[2dbb681] | 127 | self.set_panel(model) |
---|
| 128 | except: |
---|
| 129 | raise |
---|
[1b07935d] | 130 | # preview selected model name |
---|
| 131 | self.prevmodel_name=model.__class__.__name__ |
---|
| 132 | self.modelbox.SetValue(self.prevmodel_name) |
---|
| 133 | # flag to check if the user has selected a new model in the combox box |
---|
| 134 | self.model_hasChanged=False |
---|
| 135 | #dictionary of model name and model class |
---|
| 136 | self.model_list_box={} |
---|
| 137 | # Data1D to make a deep comparison between 2 Data1D for checking data |
---|
| 138 | #change |
---|
| 139 | self.vbox.Layout() |
---|
| 140 | self.vbox.Fit(self) |
---|
[f39511b] | 141 | |
---|
[1b07935d] | 142 | self.SetSizer(self.vbox) |
---|
[f39511b] | 143 | self.SetScrollbars(20,20,55,40) |
---|
[1b07935d] | 144 | self.Centre() |
---|
[2dbb681] | 145 | |
---|
[1b07935d] | 146 | |
---|
[00561739] | 147 | def onClose(self,event): |
---|
| 148 | """ close the page associated with this panel""" |
---|
| 149 | self.GrandParent.onClose() |
---|
| 150 | |
---|
[1b07935d] | 151 | def set_owner(self,owner): |
---|
| 152 | """ |
---|
| 153 | set owner of fitpage |
---|
| 154 | @param owner: the class responsible of plotting |
---|
| 155 | """ |
---|
| 156 | self.event_owner=owner |
---|
| 157 | |
---|
| 158 | |
---|
| 159 | def set_manager(self, manager): |
---|
| 160 | """ |
---|
| 161 | set panel manager |
---|
| 162 | @param manager: instance of plugin fitting |
---|
| 163 | """ |
---|
| 164 | self.manager = manager |
---|
[dc317d1] | 165 | |
---|
[f39511b] | 166 | def onModel2D(self, event): |
---|
| 167 | |
---|
| 168 | if self.model_view.GetValue()==True: |
---|
| 169 | print "2D model" |
---|
| 170 | self.manager.draw_model(self.model, |
---|
| 171 | description=None, enable2D=True,qmin=None, qmax=None) |
---|
[1b07935d] | 172 | def populate_box(self, dict): |
---|
| 173 | """ |
---|
| 174 | Populate each combox box of each page |
---|
| 175 | @param page: the page to populate |
---|
| 176 | """ |
---|
| 177 | id=0 |
---|
| 178 | self.model_list_box=dict |
---|
[2dbb681] | 179 | list_name=[] |
---|
| 180 | for item in self.model_list_box.itervalues(): |
---|
| 181 | name = item.__name__ |
---|
| 182 | model=item() |
---|
| 183 | if hasattr(model, "name"): |
---|
| 184 | name = model.name |
---|
| 185 | list_name.append(name) |
---|
| 186 | list_name.sort() |
---|
| 187 | for name in list_name: |
---|
[1b07935d] | 188 | self.modelbox.Insert(name,int(id)) |
---|
| 189 | id+=1 |
---|
[2dbb681] | 190 | wx.EVT_COMBOBOX(self.modelbox,-1, self._on_select_model) |
---|
[1b07935d] | 191 | return 0 |
---|
[060b857] | 192 | |
---|
[2dbb681] | 193 | def set_page(self, model,description=None): |
---|
[00561739] | 194 | #print " modelpage: set_page was called",model |
---|
[060b857] | 195 | self.model=model |
---|
[2dbb681] | 196 | name = self.model.__class__.__name__ |
---|
[060b857] | 197 | if hasattr(self.model, "name"): |
---|
| 198 | name = self.model.name |
---|
[2dbb681] | 199 | |
---|
| 200 | |
---|
[060b857] | 201 | self.modelbox.SetValue(name) |
---|
[2dbb681] | 202 | self.set_panel(self.model) |
---|
[060b857] | 203 | self.manager.draw_model(self.model) |
---|
[2dbb681] | 204 | |
---|
[1b07935d] | 205 | def _on_select_model(self,event): |
---|
| 206 | """ |
---|
| 207 | react when a model is selected from page's combo box |
---|
| 208 | post an event to its owner to draw an appropriate theory |
---|
| 209 | """ |
---|
[00561739] | 210 | #print "modelpage: self.model_list_box ",self.model_list_box |
---|
[1b07935d] | 211 | for item in self.model_list_box.itervalues(): |
---|
[2dbb681] | 212 | name = item.__name__ |
---|
| 213 | items=item() |
---|
| 214 | if hasattr(items, "name"): |
---|
| 215 | name = items.name |
---|
[f39511b] | 216 | #print "fitpage: _on_select_model model name",name ,event.GetString() |
---|
[2dbb681] | 217 | if name ==event.GetString(): |
---|
| 218 | model=items |
---|
[f39511b] | 219 | #print "fitpage: _on_select_model model name",name ,event.GetString() |
---|
[b2c3225] | 220 | self.model= model |
---|
[2dbb681] | 221 | self.set_panel(model) |
---|
[f39511b] | 222 | self.manager.draw_model(model) |
---|
| 223 | |
---|
[1b07935d] | 224 | def set_model_name(self,name): |
---|
| 225 | """ |
---|
| 226 | set model name. set also self.model_hasChanged to true is the model |
---|
| 227 | type has changed or false if it didn't |
---|
| 228 | @param name: model 's name |
---|
| 229 | """ |
---|
| 230 | self.model_hasChanged=False |
---|
| 231 | if (name != self.prevmodel_name): |
---|
| 232 | self.model_hasChanged=True |
---|
| 233 | |
---|
| 234 | self.prevmodel_name=self.modelbox.GetValue() |
---|
| 235 | |
---|
| 236 | |
---|
| 237 | def get_model_box(self): |
---|
| 238 | """ return reference to combox box self.model""" |
---|
| 239 | return self.modelbox |
---|
| 240 | |
---|
| 241 | |
---|
| 242 | def get_param_list(self): |
---|
| 243 | """ |
---|
| 244 | @return self.param_toFit: list containing references to TextCtrl |
---|
| 245 | checked.Theses TextCtrl will allow reference to parameters to fit. |
---|
| 246 | @raise: if return an empty list of parameter fit will nnote work |
---|
| 247 | properly so raise ValueError,"missing parameter to fit" |
---|
| 248 | """ |
---|
| 249 | if self.param_toFit !=[]: |
---|
| 250 | return self.param_toFit |
---|
| 251 | else: |
---|
| 252 | raise ValueError,"missing parameter to fit" |
---|
| 253 | |
---|
| 254 | |
---|
[2dbb681] | 255 | def set_panel(self,model): |
---|
[1b07935d] | 256 | """ |
---|
| 257 | Build the panel from the model content |
---|
| 258 | @param model: the model selected in combo box for fitting purpose |
---|
| 259 | """ |
---|
[2dbb681] | 260 | |
---|
[1b07935d] | 261 | self.sizer2.Clear(True) |
---|
[49b7efa] | 262 | self.sizer1.Clear(True) |
---|
[04edd0d] | 263 | self.sizer5.Clear(True) |
---|
[1b07935d] | 264 | self.parameters = [] |
---|
| 265 | self.param_toFit=[] |
---|
| 266 | self.model = model |
---|
| 267 | keys = self.model.getParamList() |
---|
| 268 | keys.sort() |
---|
[5cab7d3] | 269 | description=None |
---|
| 270 | if hasattr(self.model,'description'): |
---|
| 271 | description =model.description |
---|
[04edd0d] | 272 | disp_list=self.model.getDispParamList() |
---|
| 273 | ip=0 |
---|
| 274 | iq=1 |
---|
| 275 | if len(disp_list)>0: |
---|
| 276 | disp = wx.StaticText(self, -1, 'Dispersion') |
---|
| 277 | self.sizer5.Add(disp,( iq, ip),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 278 | ip += 1 |
---|
| 279 | values = wx.StaticText(self, -1, 'Values') |
---|
| 280 | self.sizer5.Add(values,( iq, ip),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 281 | |
---|
| 282 | disp_list.sort() |
---|
[f39511b] | 283 | #print "went here",self.model.name,model.description |
---|
[1b07935d] | 284 | iy = 1 |
---|
| 285 | ix = 0 |
---|
[49b7efa] | 286 | self.cb0 = wx.StaticText(self, -1,'Model Description:') |
---|
| 287 | self.sizer1.Add(self.cb0,(iy, ix),(1,1),\ |
---|
| 288 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[d171299] | 289 | iy += 1 |
---|
| 290 | |
---|
| 291 | self.cb01 = wx.StaticText(self, -1,str(description),style=wx.ALIGN_LEFT) |
---|
| 292 | self.cb01.Wrap(400) |
---|
| 293 | #self.cb01 = wx.StaticText(self, -1,str(description),(45, 25),style=wx.ALIGN_LEFT) |
---|
| 294 | |
---|
[49b7efa] | 295 | self.sizer1.Add(self.cb01,(iy, ix),(1,1),\ |
---|
| 296 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 297 | ix = 0 |
---|
| 298 | iy = 1 |
---|
[1b07935d] | 299 | self.cb1 = wx.StaticText(self, -1,'Parameters') |
---|
| 300 | self.sizer2.Add(self.cb1,(iy, ix),(1,1),\ |
---|
| 301 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 302 | ix +=1 |
---|
| 303 | self.text2_2 = wx.StaticText(self, -1, 'Values') |
---|
| 304 | self.sizer2.Add(self.text2_2,(iy, ix),(1,1),\ |
---|
| 305 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 306 | ix +=1 |
---|
| 307 | self.text2_4 = wx.StaticText(self, -1, 'Units') |
---|
| 308 | self.sizer2.Add(self.text2_4,(iy, ix),(1,1),\ |
---|
| 309 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 310 | self.text2_4.Hide() |
---|
| 311 | for item in keys: |
---|
[04edd0d] | 312 | if not item in disp_list: |
---|
| 313 | iy += 1 |
---|
| 314 | ix = 0 |
---|
| 315 | cb=wx.StaticText(self, -1, item) |
---|
| 316 | self.sizer2.Add( cb,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 317 | ix += 1 |
---|
| 318 | value= self.model.getParam(item) |
---|
| 319 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
| 320 | ctl1.SetValue(str (format_number(value))) |
---|
| 321 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
| 322 | ctl1.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
| 323 | self.sizer2.Add(ctl1, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 324 | ix +=1 |
---|
| 325 | |
---|
| 326 | # Units |
---|
| 327 | try: |
---|
| 328 | units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
| 329 | except: |
---|
| 330 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
| 331 | self.sizer2.Add(units, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 332 | else: |
---|
| 333 | ip = 0 |
---|
| 334 | iq += 1 |
---|
| 335 | cb = wx.CheckBox(self, -1, item, (10, 10)) |
---|
| 336 | cb.SetValue(False) |
---|
| 337 | self.sizer5.Add( cb,( iq, ip),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 338 | wx.EVT_CHECKBOX(self, cb.GetId(), self._on_select_model) |
---|
| 339 | |
---|
| 340 | ip += 1 |
---|
| 341 | value= self.model.getParam(item) |
---|
| 342 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
| 343 | ctl1.SetValue(str (format_number(value))) |
---|
| 344 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
| 345 | ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
| 346 | self.sizer5.Add(ctl1, (iq,ip),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[1b07935d] | 347 | #save data |
---|
| 348 | self.parameters.append([cb,ctl1]) |
---|
[04edd0d] | 349 | iy+=1 |
---|
| 350 | self.sizer2.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 351 | |
---|
[1b07935d] | 352 | #Display units text on panel |
---|
| 353 | for item in keys: |
---|
| 354 | if self.model.details[item][0]!='': |
---|
| 355 | self.text2_4.Show() |
---|
| 356 | break |
---|
| 357 | else: |
---|
| 358 | self.text2_4.Hide() |
---|
| 359 | self.vbox.Layout() |
---|
| 360 | self.GrandParent.GetSizer().Layout() |
---|
[04edd0d] | 361 | |
---|
[1b07935d] | 362 | |
---|
| 363 | def _onparamEnter(self,event): |
---|
| 364 | """ |
---|
| 365 | when enter value on panel redraw model according to changed |
---|
| 366 | """ |
---|
| 367 | self.set_model_parameter() |
---|
| 368 | |
---|
| 369 | def set_model_parameter(self): |
---|
| 370 | if len(self.parameters) !=0 and self.model !=None: |
---|
| 371 | for item in self.parameters: |
---|
| 372 | try: |
---|
[060b857] | 373 | name=str(item[0].GetLabelText()) |
---|
[1b07935d] | 374 | value= float(item[1].GetValue()) |
---|
| 375 | self.model.setParam(name,value) |
---|
| 376 | except: |
---|
| 377 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
[442895f] | 378 | "Model Drawing Error:wrong value entered : %s"% sys.exc_value)) |
---|
[2e10b70] | 379 | self.manager.draw_model(self.model,qmin=float(self.xmin.GetValue()), |
---|
| 380 | qmax=float(self.xmax.GetValue()), |
---|
| 381 | enable2D=self.model_view.GetValue()) |
---|
| 382 | #self.manager.draw_model(self,model,description=None, |
---|
| 383 | # enable1D=True,qmin=None,qmax=None, qstep=None) |
---|
| 384 | |
---|
| 385 | |
---|
| 386 | |
---|
| 387 | |
---|
| 388 | |
---|