[1b07935d] | 1 | import sys |
---|
| 2 | import wx |
---|
| 3 | import wx.lib |
---|
| 4 | import numpy |
---|
| 5 | import copy |
---|
| 6 | |
---|
[26bf293] | 7 | |
---|
[1b07935d] | 8 | from sans.guicomm.events import StatusEvent |
---|
[26bf293] | 9 | from sans.guiframe.utils import format_number |
---|
[1b07935d] | 10 | (ModelEventbox, EVT_MODEL_BOX) = wx.lib.newevent.NewEvent() |
---|
| 11 | _BOX_WIDTH = 80 |
---|
| 12 | |
---|
| 13 | |
---|
[26bf293] | 14 | |
---|
| 15 | |
---|
[f39511b] | 16 | class ModelPage(wx.ScrolledWindow): |
---|
[1b07935d] | 17 | """ |
---|
| 18 | FitPanel class contains fields allowing to display results when |
---|
| 19 | fitting a model and one data |
---|
| 20 | @note: For Fit to be performed the user should check at least one parameter |
---|
| 21 | on fit Panel window. |
---|
| 22 | |
---|
| 23 | """ |
---|
| 24 | ## Internal name for the AUI manager |
---|
| 25 | window_name = "Fit page" |
---|
| 26 | ## Title to appear on top of the window |
---|
| 27 | window_caption = "Fit Page" |
---|
| 28 | |
---|
| 29 | |
---|
[86c1832] | 30 | def __init__(self, parent,model,name, *args, **kwargs): |
---|
[f39511b] | 31 | wx.ScrolledWindow.__init__(self, parent, *args, **kwargs) |
---|
[1b07935d] | 32 | """ |
---|
| 33 | Initialization of the Panel |
---|
| 34 | """ |
---|
[26bf293] | 35 | # model on which the fit would be performed |
---|
| 36 | self.model=model |
---|
| 37 | #list of dispersion paramaters |
---|
| 38 | self.disp_list=self.model.getDispParamList() |
---|
[1b07935d] | 39 | self.manager = None |
---|
| 40 | self.parent = parent |
---|
[26bf293] | 41 | self.event_owner = None |
---|
| 42 | # this panel does contain data .existing data allow a different drawing |
---|
| 43 | #on set_model parameters |
---|
| 44 | self.data=None |
---|
[1b07935d] | 45 | #panel interface |
---|
| 46 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
[26bf293] | 47 | self.sizer10 = wx.GridBagSizer(5,5) |
---|
| 48 | self.sizer9 = wx.GridBagSizer(5,5) |
---|
| 49 | self.sizer8 = wx.GridBagSizer(5,5) |
---|
| 50 | self.sizer7 = wx.GridBagSizer(5,5) |
---|
| 51 | self.sizer6 = wx.GridBagSizer(5,5) |
---|
[04edd0d] | 52 | self.sizer5 = wx.GridBagSizer(5,5) |
---|
[26bf293] | 53 | self.sizer4 = wx.GridBagSizer(5,5) |
---|
| 54 | |
---|
| 55 | #model selection |
---|
| 56 | self.vbox.Add(wx.StaticLine(self, -1), 0, wx.EXPAND, 0) |
---|
[00561739] | 57 | self.vbox.Add(self.sizer4) |
---|
[26bf293] | 58 | #model paramaters layer |
---|
| 59 | self.vbox.Add(self.sizer5) |
---|
| 60 | #polydispersion selected |
---|
| 61 | self.vbox.Add(wx.StaticLine(self, -1), 0, wx.EXPAND, 0) |
---|
| 62 | self.vbox.Add(self.sizer6) |
---|
| 63 | #combox box for type of dispersion |
---|
| 64 | self.vbox.Add(self.sizer7) |
---|
| 65 | #dispersion parameters layer |
---|
| 66 | self.vbox.Add(self.sizer8) |
---|
| 67 | # plotting range |
---|
| 68 | self.vbox.Add(self.sizer9) |
---|
| 69 | #close layer |
---|
| 70 | self.vbox.Add(wx.StaticLine(self, -1), 0, wx.EXPAND, 0) |
---|
| 71 | self.vbox.Add(wx.StaticLine(self, -1), 0, wx.EXPAND, 0) |
---|
| 72 | self.vbox.Add(self.sizer10) |
---|
[00561739] | 73 | |
---|
[26bf293] | 74 | |
---|
| 75 | #------------------ sizer 4 draw------------------------ |
---|
| 76 | # model on which the fit would be performed |
---|
| 77 | self.model=model |
---|
| 78 | |
---|
| 79 | # define combox box |
---|
| 80 | self.modelbox = wx.ComboBox(self, -1) |
---|
| 81 | # preview selected model name |
---|
| 82 | self.prevmodel_name=name |
---|
| 83 | #print "model view prev_model",name |
---|
| 84 | self.modelbox.SetValue(self.prevmodel_name) |
---|
| 85 | #enable model 2D draw |
---|
| 86 | self.enable2D= False |
---|
| 87 | #filling sizer2 |
---|
| 88 | ix = 0 |
---|
| 89 | iy = 1 |
---|
| 90 | self.sizer4.Add(wx.StaticText(self,-1,'Model'),(iy,ix),(1,1)\ |
---|
| 91 | , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 92 | ix += 1 |
---|
| 93 | self.sizer4.Add(self.modelbox,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 94 | ix += 1 |
---|
[00561739] | 95 | id = wx.NewId() |
---|
[26bf293] | 96 | self.model_view =wx.Button(self,id,'View 2D') |
---|
| 97 | self.model_view.Bind(wx.EVT_BUTTON, self.onModel2D,id=id) |
---|
| 98 | self.model_view.SetToolTipString("View model in 2D") |
---|
| 99 | self.sizer4.Add(self.model_view,(iy,ix),(1,1),\ |
---|
| 100 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 101 | self.model_view.SetFocus() |
---|
| 102 | #----------sizer6------------------------------------------------- |
---|
| 103 | self.disable_disp = wx.RadioButton(self, -1, 'No', (10, 10), style=wx.RB_GROUP) |
---|
| 104 | self.enable_disp = wx.RadioButton(self, -1, 'Yes', (10, 30)) |
---|
| 105 | self.Bind(wx.EVT_RADIOBUTTON, self.Set_DipersParam, id=self.disable_disp.GetId()) |
---|
| 106 | self.Bind(wx.EVT_RADIOBUTTON, self.Set_DipersParam, id=self.enable_disp.GetId()) |
---|
| 107 | ix= 0 |
---|
| 108 | iy=1 |
---|
| 109 | self.sizer6.Add(wx.StaticText(self,-1,'Polydispersity: '),(iy,ix),(1,1)\ |
---|
| 110 | , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[d15c0202] | 111 | ix += 1 |
---|
[26bf293] | 112 | self.sizer6.Add(self.enable_disp ,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[d15c0202] | 113 | ix += 1 |
---|
[26bf293] | 114 | self.sizer6.Add(self.disable_disp ,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 115 | ix =0 |
---|
| 116 | iy+=1 |
---|
| 117 | self.sizer6.Add((20,20),(iy,ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 118 | |
---|
| 119 | |
---|
| 120 | #---------sizer 9 draw---------------------------------------- |
---|
| 121 | |
---|
| 122 | ## Q range |
---|
| 123 | self.qmin_x= 0.001 |
---|
| 124 | self.qmax_x= 0.1 |
---|
[d15c0202] | 125 | self.num_points= 100 |
---|
[24ea33c] | 126 | |
---|
[00561739] | 127 | |
---|
[26bf293] | 128 | self.qmin = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
| 129 | self.qmin.SetValue(format_number(self.qmin_x)) |
---|
| 130 | self.qmin.SetToolTipString("Minimun value of x in linear scale.") |
---|
| 131 | self.qmin.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
| 132 | self.qmin.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
| 133 | |
---|
| 134 | self.qmax = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
| 135 | self.qmax.SetValue(format_number(self.qmax_x)) |
---|
| 136 | self.qmax.SetToolTipString("Maximum value of x in linear scale.") |
---|
| 137 | self.qmax.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
| 138 | self.qmax.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
| 139 | |
---|
| 140 | |
---|
[d15c0202] | 141 | self.npts = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
| 142 | self.npts.SetValue(format_number(self.num_points)) |
---|
| 143 | self.npts.SetToolTipString("Number of point to plot.") |
---|
| 144 | self.npts.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
| 145 | self.npts.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
| 146 | |
---|
[dc317d1] | 147 | ix = 0 |
---|
[26bf293] | 148 | iy = 1 |
---|
| 149 | self.sizer9.Add(wx.StaticText(self, -1, 'Plotting Range'),(iy, ix),(1,1),\ |
---|
| 150 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 151 | ix += 1 |
---|
| 152 | self.sizer9.Add(wx.StaticText(self, -1, 'Min'),(iy, ix),(1,1),\ |
---|
| 153 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 154 | ix += 1 |
---|
| 155 | self.sizer9.Add(wx.StaticText(self, -1, 'Max'),(iy, ix),(1,1),\ |
---|
| 156 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 157 | ix += 1 |
---|
| 158 | self.sizer9.Add(wx.StaticText(self, -1, 'Npts'),(iy, ix),(1,1),\ |
---|
| 159 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[1b07935d] | 160 | ix = 0 |
---|
[26bf293] | 161 | iy += 1 |
---|
| 162 | self.sizer9.Add(wx.StaticText(self, -1, 'x range'),(iy, ix),(1,1),\ |
---|
| 163 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[1b07935d] | 164 | ix += 1 |
---|
[26bf293] | 165 | self.sizer9.Add(self.qmin,(iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[86c1832] | 166 | ix += 1 |
---|
[26bf293] | 167 | self.sizer9.Add(self.qmax,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 168 | ix += 1 |
---|
| 169 | self.sizer9.Add(self.npts,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 170 | |
---|
| 171 | ix =0 |
---|
| 172 | iy+=1 |
---|
| 173 | self.sizer9.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 174 | #----------sizer 10 draw------------------------------------------------------ |
---|
[86c1832] | 175 | id = wx.NewId() |
---|
[26bf293] | 176 | self.btClose =wx.Button(self,id,'Close') |
---|
| 177 | self.btClose.Bind(wx.EVT_BUTTON, self.onClose,id=id) |
---|
| 178 | self.btClose.SetToolTipString("Close page.") |
---|
| 179 | |
---|
| 180 | ix= 3 |
---|
| 181 | iy= 1 |
---|
| 182 | self.sizer10.Add((20,20),(iy,ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 183 | ix +=1 |
---|
| 184 | self.sizer10.Add( self.btClose,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 185 | ix =0 |
---|
| 186 | iy+=1 |
---|
| 187 | self.sizer10.Add((20,20),(iy,ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 188 | |
---|
[1b07935d] | 189 | # contains link between model ,all its parameters, and panel organization |
---|
| 190 | self.parameters=[] |
---|
[26bf293] | 191 | self.fixed_param=[] |
---|
[6b44403] | 192 | self.fittable_param=[] |
---|
[1b07935d] | 193 | #contains link between a model and selected parameters to fit |
---|
| 194 | self.param_toFit=[] |
---|
[26bf293] | 195 | |
---|
[1b07935d] | 196 | #dictionary of model name and model class |
---|
| 197 | self.model_list_box={} |
---|
[26bf293] | 198 | #Draw initial panel |
---|
| 199 | if self.model!=None: |
---|
| 200 | self.set_panel(self.model) |
---|
| 201 | |
---|
| 202 | |
---|
[86c1832] | 203 | |
---|
[1b07935d] | 204 | self.vbox.Layout() |
---|
| 205 | self.vbox.Fit(self) |
---|
| 206 | self.SetSizer(self.vbox) |
---|
[f39511b] | 207 | self.SetScrollbars(20,20,55,40) |
---|
[68dada4] | 208 | |
---|
[1b07935d] | 209 | self.Centre() |
---|
[26bf293] | 210 | self.Layout() |
---|
| 211 | self.parent.GetSizer().Layout() |
---|
| 212 | |
---|
| 213 | def set_owner(self,owner): |
---|
| 214 | """ |
---|
| 215 | set owner of fitpage |
---|
| 216 | @param owner: the class responsible of plotting |
---|
| 217 | """ |
---|
| 218 | self.event_owner=owner |
---|
| 219 | |
---|
| 220 | |
---|
| 221 | def set_manager(self, manager): |
---|
| 222 | """ |
---|
| 223 | set panel manager |
---|
| 224 | @param manager: instance of plugin fitting |
---|
| 225 | """ |
---|
| 226 | self.manager = manager |
---|
| 227 | |
---|
| 228 | def populate_box(self, dict): |
---|
| 229 | """ |
---|
| 230 | Populate each combox box of each page |
---|
| 231 | @param page: the page to populate |
---|
| 232 | """ |
---|
| 233 | id=0 |
---|
| 234 | self.model_list_box=dict |
---|
| 235 | list_name=[] |
---|
| 236 | for item in self.model_list_box.itervalues(): |
---|
| 237 | name = item.__name__ |
---|
| 238 | if hasattr(item, "name"): |
---|
| 239 | name = item.name |
---|
| 240 | list_name.append(name) |
---|
| 241 | list_name.sort() |
---|
| 242 | |
---|
| 243 | for name in list_name: |
---|
| 244 | self.modelbox.Insert(name,int(id)) |
---|
| 245 | id+=1 |
---|
| 246 | wx.EVT_COMBOBOX(self.modelbox,-1, self._on_select_model) |
---|
| 247 | return 0 |
---|
| 248 | |
---|
| 249 | |
---|
| 250 | def Set_DipersParam(self, event): |
---|
| 251 | if self.enable_disp.GetValue(): |
---|
| 252 | if len(self.disp_list)==0: |
---|
| 253 | ix=0 |
---|
| 254 | iy=1 |
---|
[6b44403] | 255 | self.fittable_param=[] |
---|
| 256 | self.fixed_param=[] |
---|
[26bf293] | 257 | self.sizer8.Clear(True) |
---|
| 258 | model_disp = wx.StaticText(self, -1, 'No PolyDispersity for this model') |
---|
| 259 | self.sizer7.Add(model_disp,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 260 | self.vbox.Layout() |
---|
| 261 | self.SetScrollbars(20,20,55,40) |
---|
| 262 | self.Layout() |
---|
| 263 | self.parent.GetSizer().Layout() |
---|
| 264 | return |
---|
| 265 | else: |
---|
[94999eb] | 266 | if self.data !=None: # allow to recognize data panel from model panel |
---|
| 267 | self.cb1.SetValue(False) |
---|
| 268 | self.select_all_param_helper() |
---|
[6b44403] | 269 | self.fittable_param=[] |
---|
| 270 | self.fixed_param=[] |
---|
[26bf293] | 271 | self.sizer8.Clear(True) |
---|
| 272 | self.populate_disp_box() |
---|
| 273 | self.set_panel_dispers(self.disp_list) |
---|
| 274 | self.vbox.Layout() |
---|
| 275 | self.SetScrollbars(20,20,55,40) |
---|
| 276 | self.Layout() |
---|
| 277 | self.parent.GetSizer().Layout() |
---|
| 278 | else: |
---|
[94999eb] | 279 | if self.data !=None: |
---|
| 280 | if self.cb1.GetValue(): |
---|
| 281 | self.select_all_param_helper() |
---|
[6b44403] | 282 | self.fittable_param=[] |
---|
| 283 | self.fixed_param=[] |
---|
[26bf293] | 284 | self.sizer7.Clear(True) |
---|
| 285 | self.sizer8.Clear(True) |
---|
| 286 | self.vbox.Layout() |
---|
| 287 | self.SetScrollbars(20,20,55,40) |
---|
| 288 | self.Layout() |
---|
| 289 | self.parent.GetSizer().Layout() |
---|
| 290 | |
---|
| 291 | def populate_disp_box(self): |
---|
| 292 | self.sizer7.Clear(True) |
---|
| 293 | if len(self.disp_list)>0: |
---|
| 294 | ix=0 |
---|
| 295 | iy=1 |
---|
| 296 | model_disp = wx.StaticText(self, -1, 'Model Disp') |
---|
| 297 | self.sizer7.Add(model_disp,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 298 | ix += 1 |
---|
| 299 | # set up the combox box |
---|
| 300 | id = 0 |
---|
| 301 | self.disp_box = wx.ComboBox(self, -1) |
---|
| 302 | self.disp_box.SetValue("Gaussian") |
---|
| 303 | self.disp_box.Insert("Gaussian",int(id)) |
---|
| 304 | wx.EVT_COMBOBOX(self.disp_box,-1, self._on_select_Disp) |
---|
| 305 | self.sizer7.Add(self.disp_box,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[2dbb681] | 306 | |
---|
[26bf293] | 307 | def set_range(self, qmin_x, qmax_x, npts): |
---|
[cfc68540] | 308 | """ |
---|
| 309 | Set the range for the plotted models |
---|
| 310 | @param qmin: minimum Q |
---|
| 311 | @param qmax: maximum Q |
---|
| 312 | @param npts: number of Q bins |
---|
| 313 | """ |
---|
| 314 | # Set the data members |
---|
[26bf293] | 315 | self.qmin_x = qmin_x |
---|
| 316 | self.qmax_x = qmax_x |
---|
[cfc68540] | 317 | self.num_points = npts |
---|
| 318 | |
---|
| 319 | # Set the controls |
---|
[26bf293] | 320 | self.qmin.SetValue(format_number(self.qmin_x)) |
---|
| 321 | self.qmax.SetValue(format_number(self.qmax_x)) |
---|
[cfc68540] | 322 | self.npts.SetValue(format_number(self.num_points)) |
---|
[26bf293] | 323 | def checkFitRange(self): |
---|
| 324 | """ |
---|
| 325 | Check the validity of fitting range |
---|
| 326 | @note: qmin should always be less than qmax or else each control box |
---|
| 327 | background is colored in pink. |
---|
| 328 | """ |
---|
| 329 | |
---|
| 330 | flag = True |
---|
| 331 | valueMin = self.qmin.GetValue() |
---|
| 332 | valueMax = self.qmax.GetValue() |
---|
| 333 | # Check for possible values entered |
---|
| 334 | #print "fitpage: checkfitrange:",valueMin,valueMax |
---|
| 335 | try: |
---|
| 336 | if (float(valueMax)> float(valueMin)): |
---|
| 337 | self.qmax.SetBackgroundColour(wx.WHITE) |
---|
| 338 | self.qmin.SetBackgroundColour(wx.WHITE) |
---|
| 339 | else: |
---|
| 340 | flag = False |
---|
| 341 | self.qmin.SetBackgroundColour("pink") |
---|
| 342 | self.qmax.SetBackgroundColour("pink") |
---|
| 343 | except: |
---|
| 344 | flag = False |
---|
| 345 | self.qmin.SetBackgroundColour("pink") |
---|
| 346 | self.qmax.SetBackgroundColour("pink") |
---|
| 347 | |
---|
| 348 | self.qmin.Refresh() |
---|
| 349 | self.qmax.Refresh() |
---|
| 350 | return flag |
---|
| 351 | |
---|
| 352 | |
---|
[1b07935d] | 353 | |
---|
[00561739] | 354 | def onClose(self,event): |
---|
| 355 | """ close the page associated with this panel""" |
---|
[26bf293] | 356 | self.parent.onClose() |
---|
[00561739] | 357 | |
---|
[1b07935d] | 358 | |
---|
[dc317d1] | 359 | |
---|
[f39511b] | 360 | def onModel2D(self, event): |
---|
[86c1832] | 361 | """ |
---|
| 362 | call manager to plot model in 2D |
---|
| 363 | """ |
---|
[24ea33c] | 364 | # If the 2D display is not currently enabled, plot the model in 2D |
---|
| 365 | # and set the enable2D flag. |
---|
| 366 | if self.enable2D==False: |
---|
| 367 | self.enable2D=True |
---|
[cfc68540] | 368 | self._draw_model() |
---|
[89032bf] | 369 | |
---|
[32673ac] | 370 | else: |
---|
[89032bf] | 371 | print "enable is true:",self.enable2D |
---|
| 372 | #self.manager.parent. show_panel(147) |
---|
| 373 | self.manager.show_panel2D( id=None ) |
---|
| 374 | #self.manager.menu1.Append(event_id, new_panel.window_caption, |
---|
| 375 | # "Show %s plot panel" % new_panel.window_caption) |
---|
[24ea33c] | 376 | |
---|
[86c1832] | 377 | |
---|
[32d802f] | 378 | def select_model(self, model, name): |
---|
[3dc83be] | 379 | """ |
---|
| 380 | Select a new model |
---|
| 381 | @param model: model object |
---|
| 382 | """ |
---|
| 383 | self.model= model |
---|
[32d802f] | 384 | print "select_model", model.__class__ |
---|
[3dc83be] | 385 | self.set_panel(model) |
---|
[32d802f] | 386 | self._draw_model(name) |
---|
[3dc83be] | 387 | |
---|
| 388 | # Select the model from the combo box |
---|
| 389 | items = self.modelbox.GetItems() |
---|
| 390 | for i in range(len(items)): |
---|
[32d802f] | 391 | print "model name",items[i],model.name, model.__class__.__name__ |
---|
| 392 | #if items[i]==model.__class__.__name__: |
---|
| 393 | if items[i]==name: |
---|
[3dc83be] | 394 | self.modelbox.SetSelection(i) |
---|
[26bf293] | 395 | |
---|
| 396 | |
---|
| 397 | def _on_select_Disp(self,event): |
---|
| 398 | """ |
---|
| 399 | allow selecting different dispersion |
---|
| 400 | self.disp_list should change type later .now only gaussian |
---|
| 401 | """ |
---|
| 402 | type = "Gaussian" |
---|
| 403 | if type ==event.GetString(): |
---|
| 404 | self.set_panel_dispers( self.disp_list,type ) |
---|
| 405 | |
---|
[1b07935d] | 406 | def _on_select_model(self,event): |
---|
| 407 | """ |
---|
| 408 | react when a model is selected from page's combo box |
---|
| 409 | post an event to its owner to draw an appropriate theory |
---|
| 410 | """ |
---|
[26bf293] | 411 | self.disable_disp.SetValue(True) |
---|
| 412 | self.sizer8.Clear(True) |
---|
| 413 | self.sizer7.Clear(True) |
---|
| 414 | self.vbox.Layout() |
---|
| 415 | self.SetScrollbars(20,20,55,40) |
---|
| 416 | self.Layout() |
---|
| 417 | self.parent.GetSizer().Layout() |
---|
[1b07935d] | 418 | for item in self.model_list_box.itervalues(): |
---|
[2dbb681] | 419 | name = item.__name__ |
---|
[86c1832] | 420 | if hasattr(item, "name"): |
---|
| 421 | name = item.name |
---|
[2dbb681] | 422 | if name ==event.GetString(): |
---|
[86c1832] | 423 | model=item() |
---|
[b2c3225] | 424 | self.model= model |
---|
[2dbb681] | 425 | self.set_panel(model) |
---|
[86c1832] | 426 | self.name= name |
---|
[cfc68540] | 427 | #self.manager.draw_model(model, name) |
---|
[89032bf] | 428 | self.enable2D=False |
---|
[32d802f] | 429 | self._draw_model(name) |
---|
[d23544dc] | 430 | |
---|
[1b07935d] | 431 | |
---|
| 432 | def get_model_box(self): |
---|
| 433 | """ return reference to combox box self.model""" |
---|
| 434 | return self.modelbox |
---|
| 435 | |
---|
| 436 | |
---|
| 437 | def get_param_list(self): |
---|
| 438 | """ |
---|
| 439 | @return self.param_toFit: list containing references to TextCtrl |
---|
| 440 | checked.Theses TextCtrl will allow reference to parameters to fit. |
---|
| 441 | @raise: if return an empty list of parameter fit will nnote work |
---|
| 442 | properly so raise ValueError,"missing parameter to fit" |
---|
| 443 | """ |
---|
| 444 | if self.param_toFit !=[]: |
---|
| 445 | return self.param_toFit |
---|
| 446 | else: |
---|
| 447 | raise ValueError,"missing parameter to fit" |
---|
| 448 | |
---|
| 449 | |
---|
[2dbb681] | 450 | def set_panel(self,model): |
---|
[1b07935d] | 451 | """ |
---|
| 452 | Build the panel from the model content |
---|
| 453 | @param model: the model selected in combo box for fitting purpose |
---|
| 454 | """ |
---|
[26bf293] | 455 | print "set_panel", model |
---|
[94999eb] | 456 | |
---|
[04edd0d] | 457 | self.sizer5.Clear(True) |
---|
[1b07935d] | 458 | self.parameters = [] |
---|
| 459 | self.param_toFit=[] |
---|
[26bf293] | 460 | self.fixed_param=[] |
---|
[1b07935d] | 461 | self.model = model |
---|
| 462 | keys = self.model.getParamList() |
---|
[26bf293] | 463 | #list of dispersion paramaters |
---|
| 464 | self.disp_list=self.model.getDispParamList() |
---|
| 465 | |
---|
[1b07935d] | 466 | keys.sort() |
---|
[26bf293] | 467 | ik=0 |
---|
| 468 | im=1 |
---|
[d171299] | 469 | |
---|
[49b7efa] | 470 | iy = 1 |
---|
[26bf293] | 471 | ix = 0 |
---|
| 472 | self.cb1 = wx.CheckBox(self, -1,"Select all", (10, 10)) |
---|
| 473 | if self.data!=None: |
---|
| 474 | wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.select_all_param) |
---|
| 475 | self.cb1.SetValue(False) |
---|
| 476 | else: |
---|
| 477 | self.cb1.Disable() |
---|
| 478 | self.cb1.Hide() |
---|
| 479 | |
---|
| 480 | self.sizer5.Add(self.cb1,(iy, ix),(1,1),\ |
---|
[1b07935d] | 481 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 482 | ix +=1 |
---|
| 483 | self.text2_2 = wx.StaticText(self, -1, 'Values') |
---|
[26bf293] | 484 | self.sizer5.Add(self.text2_2,(iy, ix),(1,1),\ |
---|
[1b07935d] | 485 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[26bf293] | 486 | ix +=2 |
---|
| 487 | self.text2_3 = wx.StaticText(self, -1, 'Errors') |
---|
| 488 | self.sizer5.Add(self.text2_3,(iy, ix),(1,1),\ |
---|
| 489 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 490 | self.text2_3.Hide() |
---|
[1b07935d] | 491 | ix +=1 |
---|
| 492 | self.text2_4 = wx.StaticText(self, -1, 'Units') |
---|
[26bf293] | 493 | self.sizer5.Add(self.text2_4,(iy, ix),(1,1),\ |
---|
[1b07935d] | 494 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 495 | self.text2_4.Hide() |
---|
[26bf293] | 496 | disp_list=self.model.getDispParamList() |
---|
[1b07935d] | 497 | for item in keys: |
---|
[04edd0d] | 498 | if not item in disp_list: |
---|
| 499 | iy += 1 |
---|
| 500 | ix = 0 |
---|
[26bf293] | 501 | |
---|
| 502 | cb = wx.CheckBox(self, -1, item, (10, 10)) |
---|
| 503 | if self.data!=None: |
---|
| 504 | cb.SetValue(False) |
---|
| 505 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
| 506 | else: |
---|
| 507 | cb.Disable() |
---|
| 508 | self.sizer5.Add( cb,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 509 | |
---|
[04edd0d] | 510 | ix += 1 |
---|
| 511 | value= self.model.getParam(item) |
---|
| 512 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
| 513 | ctl1.SetValue(str (format_number(value))) |
---|
| 514 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
[26bf293] | 515 | ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
| 516 | self.sizer5.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
[04edd0d] | 517 | |
---|
[26bf293] | 518 | ix += 1 |
---|
| 519 | text2=wx.StaticText(self, -1, '+/-') |
---|
| 520 | self.sizer5.Add(text2,(iy, ix),(1,1),\ |
---|
| 521 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 522 | text2.Hide() |
---|
| 523 | ix += 1 |
---|
| 524 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
| 525 | self.sizer5.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 526 | ctl2.Hide() |
---|
| 527 | ix +=1 |
---|
[04edd0d] | 528 | # Units |
---|
| 529 | try: |
---|
| 530 | units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
| 531 | except: |
---|
| 532 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
[26bf293] | 533 | self.sizer5.Add(units, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 534 | |
---|
| 535 | self.parameters.append([cb,ctl1,text2,ctl2]) |
---|
[04edd0d] | 536 | |
---|
| 537 | iy+=1 |
---|
[26bf293] | 538 | self.sizer5.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[04edd0d] | 539 | |
---|
[1b07935d] | 540 | #Display units text on panel |
---|
| 541 | for item in keys: |
---|
| 542 | if self.model.details[item][0]!='': |
---|
| 543 | self.text2_4.Show() |
---|
| 544 | break |
---|
| 545 | else: |
---|
| 546 | self.text2_4.Hide() |
---|
[26bf293] | 547 | #Disable or enable fit button |
---|
| 548 | """ |
---|
| 549 | if not (len(self.param_toFit ) >0): |
---|
| 550 | self.qmin.Disable() |
---|
| 551 | self.qmax.Disable() |
---|
| 552 | else: |
---|
| 553 | self.qmin.Enable() |
---|
| 554 | self.qmax.Enable() |
---|
| 555 | """ |
---|
[1b07935d] | 556 | self.vbox.Layout() |
---|
[68dada4] | 557 | self.SetScrollbars(20,20,55,40) |
---|
[26bf293] | 558 | self.Layout() |
---|
| 559 | self.parent.GetSizer().Layout() |
---|
| 560 | |
---|
| 561 | |
---|
| 562 | def set_panel_dispers(self, disp_list, type="Gaussian" ): |
---|
| 563 | |
---|
[68dada4] | 564 | |
---|
[04edd0d] | 565 | |
---|
[26bf293] | 566 | ix=0 |
---|
| 567 | iy=1 |
---|
| 568 | ### this will become a separate method |
---|
| 569 | if type== "Gaussian" : |
---|
| 570 | |
---|
| 571 | disp = wx.StaticText(self, -1, 'Dispersion') |
---|
| 572 | self.sizer8.Add(disp,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 573 | ix += 1 |
---|
| 574 | values = wx.StaticText(self, -1, 'Values') |
---|
| 575 | self.sizer8.Add(values,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 576 | ix +=2 |
---|
| 577 | self.text2_3 = wx.StaticText(self, -1, 'Errors') |
---|
| 578 | self.sizer8.Add(self.text2_3,(iy, ix),(1,1),\ |
---|
| 579 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 580 | self.text2_3.Hide() |
---|
| 581 | |
---|
| 582 | ix += 1 |
---|
| 583 | npts = wx.StaticText(self, -1, 'Npts') |
---|
| 584 | self.sizer8.Add(npts,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 585 | ix += 1 |
---|
| 586 | nsigmas = wx.StaticText(self, -1, 'Nsigmas') |
---|
| 587 | self.sizer8.Add(nsigmas,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 588 | |
---|
| 589 | disp_list.sort() |
---|
| 590 | #print disp_list,self.model.fixed,self.model.dispersion |
---|
| 591 | for item in self.model.dispersion.keys(): |
---|
| 592 | name1=item+".width" |
---|
| 593 | name2=item+".npts" |
---|
| 594 | name3=item+".nsigmas" |
---|
| 595 | iy += 1 |
---|
| 596 | for p in self.model.dispersion[item].keys(): |
---|
| 597 | #print "name 1 2 3", name1, name2, name3 |
---|
| 598 | if p=="width": |
---|
| 599 | ix = 0 |
---|
| 600 | cb = wx.CheckBox(self, -1, name1, (10, 10)) |
---|
| 601 | if self.data !=None: |
---|
| 602 | cb.SetValue(False) |
---|
| 603 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
| 604 | else: |
---|
| 605 | cb.Disable() |
---|
| 606 | self.sizer8.Add( cb,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 607 | ix = 1 |
---|
| 608 | value= self.model.getParam(name1) |
---|
| 609 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
| 610 | ctl1.SetValue(str (format_number(value))) |
---|
| 611 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
| 612 | ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
| 613 | self.sizer8.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
| 614 | |
---|
| 615 | ix = 2 |
---|
| 616 | text2=wx.StaticText(self, -1, '+/-') |
---|
| 617 | self.sizer8.Add(text2,(iy, ix),(1,1),\ |
---|
| 618 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 619 | text2.Hide() |
---|
| 620 | ix = 3 |
---|
| 621 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
| 622 | self.sizer8.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 623 | ctl2.Hide() |
---|
[94999eb] | 624 | self.fittable_param.append([cb,ctl1,text2,ctl2]) |
---|
[26bf293] | 625 | |
---|
| 626 | |
---|
| 627 | elif p=="npts": |
---|
| 628 | ix =4 |
---|
| 629 | value= self.model.getParam(name2) |
---|
| 630 | Tctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER) |
---|
| 631 | Tctl.SetValue(str (format_number(value))) |
---|
| 632 | Tctl.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
| 633 | Tctl.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
| 634 | self.sizer8.Add(Tctl, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 635 | self.fixed_param.append([name2, Tctl]) |
---|
| 636 | elif p=="nsigmas": |
---|
| 637 | ix =5 |
---|
| 638 | value= self.model.getParam(name3) |
---|
| 639 | Tctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER) |
---|
| 640 | Tctl.SetValue(str (format_number(value))) |
---|
| 641 | Tctl.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
| 642 | Tctl.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
| 643 | self.sizer8.Add(Tctl, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 644 | self.fixed_param.append([name3, Tctl]) |
---|
| 645 | |
---|
| 646 | |
---|
| 647 | |
---|
| 648 | |
---|
[1b07935d] | 649 | |
---|
| 650 | def _onparamEnter(self,event): |
---|
| 651 | """ |
---|
| 652 | when enter value on panel redraw model according to changed |
---|
| 653 | """ |
---|
| 654 | self.set_model_parameter() |
---|
| 655 | |
---|
| 656 | def set_model_parameter(self): |
---|
| 657 | if len(self.parameters) !=0 and self.model !=None: |
---|
[24ea33c] | 658 | # Flag to register when a parameter has changed. |
---|
| 659 | is_modified = False |
---|
[26bf293] | 660 | for item in self.fixed_param: |
---|
| 661 | try: |
---|
| 662 | name=str(item[0]) |
---|
| 663 | value= float(item[1].GetValue()) |
---|
| 664 | #print "model para", name,value,self.model.getParam(name) |
---|
| 665 | # If the value of the parameter has changed, |
---|
| 666 | # update the model and set the is_modified flag |
---|
| 667 | if value != self.model.getParam(name): |
---|
| 668 | #print "went hereee" |
---|
| 669 | self.model.setParam(name,value) |
---|
| 670 | is_modified = True |
---|
| 671 | |
---|
| 672 | except: |
---|
| 673 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
| 674 | "Model Drawing Error:wrong value entered : %s"% sys.exc_value)) |
---|
| 675 | |
---|
[db709e4] | 676 | for item in self.parameters: |
---|
[a5aaec9] | 677 | try: |
---|
| 678 | name=str(item[0].GetLabelText()) |
---|
| 679 | value= float(item[1].GetValue()) |
---|
[26bf293] | 680 | #print " fittable model para", name,value |
---|
[a5aaec9] | 681 | # If the value of the parameter has changed, |
---|
| 682 | # update the model and set the is_modified flag |
---|
| 683 | if value != self.model.getParam(name): |
---|
[26bf293] | 684 | #print "went here", name,value |
---|
[a5aaec9] | 685 | self.model.setParam(name,value) |
---|
| 686 | is_modified = True |
---|
[db709e4] | 687 | except: |
---|
[26bf293] | 688 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
[db709e4] | 689 | "Model Drawing Error:wrong value entered : %s"% sys.exc_value)) |
---|
| 690 | |
---|
| 691 | # Here we should check whether the boundaries have been modified. |
---|
| 692 | # If qmin and qmax have been modified, update qmin and qmax and |
---|
| 693 | # set the is_modified flag to True |
---|
[26bf293] | 694 | if float(self.qmin.GetValue()) != self.qmin_x: |
---|
| 695 | self.qmin_x = float(self.qmin.GetValue()) |
---|
[a5aaec9] | 696 | is_modified = True |
---|
[26bf293] | 697 | if float(self.qmax.GetValue()) != self.qmax_x: |
---|
| 698 | self.qmax_x = float(self.qmax.GetValue()) |
---|
[a5aaec9] | 699 | is_modified = True |
---|
[e9b4cc4] | 700 | |
---|
[ea20505] | 701 | if float(self.npts.GetValue()) != self.num_points: |
---|
| 702 | self.num_points = float(self.npts.GetValue()) |
---|
| 703 | is_modified = True |
---|
[e9b4cc4] | 704 | |
---|
[a5aaec9] | 705 | if is_modified: |
---|
[cfc68540] | 706 | self._draw_model() |
---|
[2e10b70] | 707 | |
---|
[32d802f] | 708 | def _draw_model(self, name=None): |
---|
[cfc68540] | 709 | """ |
---|
| 710 | Method to draw or refresh a plotted model. |
---|
| 711 | The method will use the data member from the model page |
---|
| 712 | to build a call to the fitting perspective manager. |
---|
[2e10b70] | 713 | |
---|
[cfc68540] | 714 | [Note to coder: This way future changes will be done in only one place.] |
---|
| 715 | """ |
---|
[26bf293] | 716 | #print "_draw_model",self.model |
---|
[32d802f] | 717 | if name==None: |
---|
| 718 | name= self.model.name |
---|
[26bf293] | 719 | |
---|
| 720 | self.manager.draw_model(self.model, name, data=self.data, |
---|
| 721 | qmin=self.qmin_x, qmax=self.qmax_x, |
---|
[cfc68540] | 722 | qstep= self.num_points, |
---|
| 723 | enable2D=self.enable2D) |
---|
[32d802f] | 724 | """ |
---|
| 725 | self.manager.draw_model(self.model, self.model.name, |
---|
| 726 | qmin=self.qmin, qmax=self.qmax, |
---|
| 727 | qstep= self.num_points, |
---|
| 728 | enable2D=self.enable2D) |
---|
| 729 | """ |
---|
[26bf293] | 730 | def select_param(self,event): |
---|
| 731 | pass |
---|
| 732 | def select_all_param(self,event): |
---|
| 733 | pass |
---|
[94999eb] | 734 | def select_all_param_helper(self): |
---|
| 735 | """ |
---|
| 736 | Allows selecting or delecting button |
---|
| 737 | """ |
---|
| 738 | self.param_toFit=[] |
---|
| 739 | if self.parameters !=[]: |
---|
| 740 | if self.cb1.GetValue()==True: |
---|
| 741 | for item in self.parameters: |
---|
| 742 | item[0].SetValue(True) |
---|
| 743 | list= [item[0],item[1],item[2],item[3]] |
---|
| 744 | self.param_toFit.append(list ) |
---|
| 745 | if len(self.fittable_param)>0: |
---|
| 746 | for item in self.fittable_param: |
---|
| 747 | item[0].SetValue(True) |
---|
| 748 | list= [item[0],item[1],item[2],item[3]] |
---|
| 749 | self.param_toFit.append(list ) |
---|
| 750 | |
---|
| 751 | if not (len(self.param_toFit ) >0): |
---|
| 752 | self.qmin.Disable() |
---|
| 753 | self.qmax.Disable() |
---|
| 754 | else: |
---|
| 755 | self.qmin.Enable() |
---|
| 756 | self.qmax.Enable() |
---|
| 757 | else: |
---|
| 758 | for item in self.parameters: |
---|
| 759 | item[0].SetValue(False) |
---|
| 760 | for item in self.fittable_param: |
---|
| 761 | item[0].SetValue(False) |
---|
| 762 | self.param_toFit=[] |
---|
| 763 | |
---|
| 764 | self.qmin.Disable() |
---|
| 765 | self.qmax.Disable() |
---|
[26bf293] | 766 | |
---|