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