[c77d859] | 1 | |
---|
| 2 | import sys |
---|
| 3 | import wx |
---|
| 4 | import numpy |
---|
| 5 | |
---|
| 6 | from sans.guiframe.utils import format_number |
---|
| 7 | from sans.guicomm.events import StatusEvent |
---|
[cfc0913] | 8 | import pagestate |
---|
| 9 | from pagestate import PageState |
---|
| 10 | (PageInfoEvent, EVT_PAGE_INFO) = wx.lib.newevent.NewEvent() |
---|
[c77d859] | 11 | _BOX_WIDTH = 80 |
---|
| 12 | |
---|
| 13 | class BasicPage(wx.ScrolledWindow): |
---|
| 14 | """ |
---|
| 15 | This class provide general structure of fitpanel page |
---|
| 16 | """ |
---|
[b787e68c] | 17 | ## Internal name for the AUI manager |
---|
| 18 | window_name = "Basic Page" |
---|
| 19 | ## Title to appear on top of the window |
---|
| 20 | window_caption = "Basic page " |
---|
[59a7f2d] | 21 | |
---|
[cfc0913] | 22 | def __init__(self,parent, page_info): |
---|
[c77d859] | 23 | wx.ScrolledWindow.__init__(self, parent) |
---|
[cfc0913] | 24 | ##window_name |
---|
| 25 | self.window_name = page_info.window_name |
---|
| 26 | ##window_caption |
---|
| 27 | self.window_caption = page_info.window_caption |
---|
[c77d859] | 28 | ## parent of the page |
---|
| 29 | self.parent = parent |
---|
[77e23a2] | 30 | ## manager is the fitting plugin |
---|
[cfc0913] | 31 | self.manager= page_info.manager |
---|
[c77d859] | 32 | ## owner of the page (fitting plugin) |
---|
[cfc0913] | 33 | self.event_owner= page_info.event_owner |
---|
| 34 | ## current model |
---|
| 35 | self.model = page_info.model |
---|
| 36 | ## data |
---|
| 37 | self.data = page_info.data |
---|
[c77d859] | 38 | ## dictionary containing list of models |
---|
[cfc0913] | 39 | self.model_list_box = page_info.model_list_box |
---|
[c77d859] | 40 | ## Data member to store the dispersion object created |
---|
| 41 | self._disp_obj_dict = {} |
---|
[cfc0913] | 42 | ## selected parameters to apply dispersion |
---|
[c77d859] | 43 | self.disp_cb_dict ={} |
---|
| 44 | ##list of model parameters. each item must have same length |
---|
| 45 | ## each item related to a given parameters |
---|
| 46 | ##[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
| 47 | self.parameters=[] |
---|
| 48 | ## list of parameters to fit , must be like self.parameters |
---|
| 49 | self.param_toFit=[] |
---|
| 50 | ## list of looking like parameters but with non fittable parameters info |
---|
| 51 | self.fixed_param=[] |
---|
| 52 | ## list of looking like parameters but with fittable parameters info |
---|
| 53 | self.fittable_param=[] |
---|
| 54 | ##list of dispersion parameters |
---|
| 55 | self.disp_list=[] |
---|
[cfc0913] | 56 | if self.model !=None: |
---|
| 57 | self.disp_list= self.model.getDispParamList() |
---|
[c77d859] | 58 | ##enable model 2D draw |
---|
| 59 | self.enable2D= False |
---|
| 60 | ## check that the fit range is correct to plot the model again |
---|
| 61 | self.fitrange= True |
---|
| 62 | ## Q range |
---|
| 63 | self.qmin_x= 0.001 |
---|
| 64 | self.qmax_x= 0.1 |
---|
| 65 | self.num_points= 100 |
---|
[cfc0913] | 66 | ## Create memento to save the current state |
---|
| 67 | |
---|
| 68 | self.state= PageState(parent= self.parent,model=self.model, data=self.data) |
---|
| 69 | ## create the basic structure of the panel with empty sizer |
---|
| 70 | self.define_page_structure() |
---|
[c77d859] | 71 | ## drawing Initial dispersion parameters sizer |
---|
| 72 | self.set_dispers_sizer() |
---|
[00c3aac] | 73 | self._fill_save_sizer() |
---|
[cfc0913] | 74 | |
---|
[c77d859] | 75 | ## layout |
---|
| 76 | self.set_layout() |
---|
| 77 | |
---|
| 78 | |
---|
| 79 | def define_page_structure(self): |
---|
| 80 | """ |
---|
| 81 | Create empty sizer for a panel |
---|
| 82 | """ |
---|
| 83 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
| 84 | self.sizer0 = wx.BoxSizer(wx.VERTICAL) |
---|
| 85 | self.sizer1 = wx.BoxSizer(wx.VERTICAL) |
---|
| 86 | self.sizer2 = wx.BoxSizer(wx.VERTICAL) |
---|
| 87 | self.sizer3 = wx.BoxSizer(wx.VERTICAL) |
---|
| 88 | self.sizer4 = wx.BoxSizer(wx.VERTICAL) |
---|
| 89 | self.sizer5 = wx.BoxSizer(wx.VERTICAL) |
---|
[0a518e4c] | 90 | self.sizer6 = wx.BoxSizer(wx.VERTICAL) |
---|
[c77d859] | 91 | |
---|
[8bd4dc0] | 92 | self.sizer0.SetMinSize((375,-1)) |
---|
| 93 | self.sizer1.SetMinSize((375,-1)) |
---|
| 94 | self.sizer2.SetMinSize((375,-1)) |
---|
| 95 | self.sizer3.SetMinSize((375,-1)) |
---|
| 96 | self.sizer4.SetMinSize((375,-1)) |
---|
| 97 | self.sizer5.SetMinSize((375,-1)) |
---|
[0a518e4c] | 98 | self.sizer6.SetMinSize((375,-1)) |
---|
[8bd4dc0] | 99 | |
---|
[c77d859] | 100 | self.vbox.Add(self.sizer0) |
---|
| 101 | self.vbox.Add(self.sizer1) |
---|
| 102 | self.vbox.Add(self.sizer2) |
---|
| 103 | self.vbox.Add(self.sizer3) |
---|
| 104 | self.vbox.Add(self.sizer4) |
---|
| 105 | self.vbox.Add(self.sizer5) |
---|
[0a518e4c] | 106 | self.vbox.Add(self.sizer6) |
---|
[c77d859] | 107 | |
---|
| 108 | def set_layout(self): |
---|
| 109 | """ |
---|
| 110 | layout |
---|
| 111 | """ |
---|
| 112 | self.vbox.Layout() |
---|
| 113 | self.vbox.Fit(self) |
---|
| 114 | self.SetSizer(self.vbox) |
---|
| 115 | |
---|
| 116 | self.set_scroll() |
---|
| 117 | self.Centre() |
---|
| 118 | |
---|
| 119 | def set_scroll(self): |
---|
| 120 | self.SetScrollbars(20,20,200,100) |
---|
| 121 | self.Layout() |
---|
[0a518e4c] | 122 | self.SetAutoLayout(True) |
---|
[c77d859] | 123 | |
---|
| 124 | def set_owner(self,owner): |
---|
| 125 | """ |
---|
| 126 | set owner of fitpage |
---|
| 127 | @param owner: the class responsible of plotting |
---|
| 128 | """ |
---|
| 129 | self.event_owner = owner |
---|
[cfc0913] | 130 | self.state.event_owner = owner |
---|
[c77d859] | 131 | |
---|
| 132 | def set_manager(self, manager): |
---|
| 133 | """ |
---|
| 134 | set panel manager |
---|
| 135 | @param manager: instance of plugin fitting |
---|
| 136 | """ |
---|
| 137 | self.manager = manager |
---|
[cfc0913] | 138 | self.state.manager = manager |
---|
[c77d859] | 139 | |
---|
| 140 | def populate_box(self, dict): |
---|
| 141 | """ |
---|
| 142 | Store list of model |
---|
| 143 | @param dict: dictionary containing list of models |
---|
| 144 | """ |
---|
| 145 | self.model_list_box = dict |
---|
[cfc0913] | 146 | self.state.model_list_box = self.model_list_box |
---|
[c77d859] | 147 | |
---|
| 148 | |
---|
| 149 | |
---|
| 150 | def set_dispers_sizer(self): |
---|
| 151 | """ |
---|
| 152 | fill sizer containing dispersity info |
---|
| 153 | """ |
---|
| 154 | self.sizer4.Clear(True) |
---|
| 155 | box_description= wx.StaticBox(self, -1,"PolyDispersity") |
---|
| 156 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
| 157 | #---------------------------------------------------- |
---|
| 158 | self.disable_disp = wx.RadioButton(self, -1, 'No', (10, 10), style=wx.RB_GROUP) |
---|
| 159 | self.enable_disp = wx.RadioButton(self, -1, 'Yes', (10, 30)) |
---|
[cfc0913] | 160 | ## saving the state of enable dispersity button |
---|
| 161 | self.state.enable_disp= self.enable_disp.GetValue() |
---|
| 162 | |
---|
[c77d859] | 163 | self.Bind(wx.EVT_RADIOBUTTON, self._set_dipers_Param, id=self.disable_disp.GetId()) |
---|
| 164 | self.Bind(wx.EVT_RADIOBUTTON, self._set_dipers_Param, id=self.enable_disp.GetId()) |
---|
| 165 | |
---|
| 166 | sizer_dispersion = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 167 | sizer_dispersion.Add((20,20)) |
---|
| 168 | sizer_dispersion.Add(wx.StaticText(self,-1,'Polydispersity: ')) |
---|
| 169 | sizer_dispersion.Add(self.enable_disp ) |
---|
| 170 | sizer_dispersion.Add((20,20)) |
---|
| 171 | sizer_dispersion.Add(self.disable_disp ) |
---|
| 172 | sizer_dispersion.Add((10,10)) |
---|
| 173 | |
---|
| 174 | ## fill a sizer with the combobox to select dispersion type |
---|
| 175 | sizer_select_dispers = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 176 | self.model_disp = wx.StaticText(self, -1, 'Model Disp') |
---|
| 177 | |
---|
| 178 | import sans.models.dispersion_models |
---|
| 179 | self.polydisp= sans.models.dispersion_models.models |
---|
[0a518e4c] | 180 | self.disp_box = wx.ComboBox(self, -1) |
---|
[b787e68c] | 181 | |
---|
[c77d859] | 182 | for key in self.polydisp.iterkeys(): |
---|
| 183 | name = str(key.__name__) |
---|
| 184 | if name=="ArrayDispersion": |
---|
| 185 | # Remove the option until the rest of the code is ready for it |
---|
| 186 | self.disp_box.Append("Select customized Model",key) |
---|
[b787e68c] | 187 | |
---|
[c77d859] | 188 | else: |
---|
[b787e68c] | 189 | self.disp_box.Append(name,key) |
---|
| 190 | self.disp_box.SetSelection(0) |
---|
| 191 | |
---|
[c77d859] | 192 | wx.EVT_COMBOBOX(self.disp_box,-1, self._on_select_Disp) |
---|
| 193 | |
---|
| 194 | sizer_select_dispers.Add((10,10)) |
---|
| 195 | sizer_select_dispers.Add(self.model_disp) |
---|
| 196 | sizer_select_dispers.Add(self.disp_box) |
---|
| 197 | sizer_select_dispers.Add((10,10)) |
---|
| 198 | self.model_disp.Hide() |
---|
| 199 | self.disp_box.Hide() |
---|
| 200 | |
---|
| 201 | boxsizer1.Add( sizer_dispersion ) |
---|
| 202 | boxsizer1.Add( (10,10) ) |
---|
| 203 | boxsizer1.Add( sizer_select_dispers ) |
---|
| 204 | self.sizer4_4 = wx.GridBagSizer(5,5) |
---|
| 205 | boxsizer1.Add( self.sizer4_4 ) |
---|
| 206 | #----------------------------------------------------- |
---|
| 207 | self.sizer4.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
[3b605bb] | 208 | self.sizer4_4.Layout() |
---|
[c77d859] | 209 | self.sizer4.Layout() |
---|
[3b605bb] | 210 | self.Layout() |
---|
[c77d859] | 211 | self.SetScrollbars(20,20,200,100) |
---|
[3b605bb] | 212 | self.Refresh() |
---|
[0a518e4c] | 213 | |
---|
[c77d859] | 214 | |
---|
| 215 | def select_disp_angle(self, event): |
---|
| 216 | """ |
---|
| 217 | Event for when a user select a parameter to average over. |
---|
| 218 | @param event: check box event |
---|
| 219 | """ |
---|
| 220 | |
---|
| 221 | |
---|
| 222 | # Go through the list of dispersion check boxes to identify which one has changed |
---|
| 223 | for p in self.disp_cb_dict: |
---|
| 224 | # Catch which one of the box was just checked or unchecked. |
---|
| 225 | if event.GetEventObject() == self.disp_cb_dict[p]: |
---|
| 226 | |
---|
| 227 | |
---|
| 228 | if self.disp_cb_dict[p].GetValue() == True: |
---|
| 229 | # The user wants this parameter to be averaged. |
---|
| 230 | # Pop up the file selection dialog. |
---|
| 231 | path = self._selectDlg() |
---|
| 232 | |
---|
| 233 | # If nothing was selected, just return |
---|
| 234 | if path is None: |
---|
| 235 | self.disp_cb_dict[p].SetValue(False) |
---|
| 236 | return |
---|
| 237 | |
---|
| 238 | try: |
---|
| 239 | values,weights = self.read_file(path) |
---|
| 240 | except: |
---|
| 241 | msg="Could not read input file" |
---|
| 242 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg)) |
---|
| 243 | return |
---|
| 244 | |
---|
| 245 | # If any of the two arrays is empty, notify the user that we won't |
---|
| 246 | # proceed |
---|
| 247 | if values is None or weights is None: |
---|
| 248 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
| 249 | "The loaded %s distrubtion is corrupted or empty" % p)) |
---|
| 250 | return |
---|
| 251 | |
---|
| 252 | # Tell the user that we are about to apply the distribution |
---|
| 253 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
| 254 | "Applying loaded %s distribution: %s" % (p, path))) |
---|
| 255 | |
---|
| 256 | # Create the dispersion objects |
---|
| 257 | from sans.models.dispersion_models import ArrayDispersion |
---|
| 258 | disp_model = ArrayDispersion() |
---|
| 259 | disp_model.set_weights(values, weights) |
---|
| 260 | # Store the object to make it persist outside the scope of this method |
---|
| 261 | #TODO: refactor model to clean this up? |
---|
| 262 | self._disp_obj_dict[p] = disp_model |
---|
| 263 | |
---|
| 264 | # Set the new model as the dispersion object for the selected parameter |
---|
| 265 | self.model.set_dispersion(p, disp_model) |
---|
| 266 | |
---|
| 267 | else: |
---|
| 268 | # The parameter was un-selected. Go back to Gaussian model (with 0 pts) |
---|
| 269 | self._reset_dispersity() |
---|
| 270 | |
---|
| 271 | ## Redraw the model |
---|
| 272 | self._draw_model() |
---|
| 273 | return |
---|
| 274 | |
---|
[00c3aac] | 275 | def onSave(self, event): |
---|
| 276 | """ |
---|
| 277 | save history of the data and model |
---|
| 278 | """ |
---|
[cfc0913] | 279 | self.state.model= self.model |
---|
| 280 | self.state.data = self.data |
---|
[00c3aac] | 281 | import os |
---|
| 282 | |
---|
| 283 | |
---|
| 284 | path = None |
---|
| 285 | dlg = wx.FileDialog(self, "Choose a file", os.getcwd(), "", "*.txt", wx.SAVE) |
---|
| 286 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 287 | path = dlg.GetPath() |
---|
| 288 | mypath = os.path.basename(path) |
---|
| 289 | |
---|
| 290 | dlg.Destroy() |
---|
| 291 | |
---|
| 292 | if not path == None: |
---|
| 293 | out = open(path, 'w') |
---|
| 294 | has_data = self.data !=None |
---|
| 295 | has_model = self.model!=None |
---|
| 296 | import time |
---|
| 297 | year, month, day,hour,minute,second,tda,ty,tm_isdst= time.gmtime() |
---|
| 298 | import os.path |
---|
| 299 | out.write("Title: State for %s\n"%os.path.basename(path)) |
---|
| 300 | out.write("Date: %g\%g\%g \n"%(year, month, day)) |
---|
| 301 | out.write("GMT Time: %g:%g:%g \n\n"%(hour,minute, second)) |
---|
| 302 | # Sanity check |
---|
| 303 | if has_data: |
---|
| 304 | out.write("Fit Data name: %s\n\n"%self.data.name) |
---|
| 305 | |
---|
| 306 | if has_model: |
---|
| 307 | out.write("Model name: %s\n"%self.model.name) |
---|
| 308 | if len(self.parameters)>0: |
---|
| 309 | out.write("Parameters Info:\n") |
---|
| 310 | msg= "Name\t\tValues\t\t+\-\tErrors\t\tMin\t\tMax\t\tUnits\t\tstate\n" |
---|
| 311 | out.write(msg) |
---|
| 312 | ##self.parameters= |
---|
| 313 | ##[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
| 314 | for item in self.parameters: |
---|
| 315 | if item[1]!=None: |
---|
| 316 | name= item[1] |
---|
| 317 | else: |
---|
| 318 | name="" |
---|
| 319 | if item[2]!=None: |
---|
| 320 | value= str(item[2].GetValue().rstrip().lstrip()) |
---|
| 321 | else: |
---|
| 322 | value="" |
---|
| 323 | if item[3]!=None: |
---|
| 324 | sign= str(item[3].GetLabelText().rstrip().lstrip()) |
---|
| 325 | else: |
---|
| 326 | sign="" |
---|
| 327 | if item[4]!=None: |
---|
| 328 | error= str(item[4].GetValue().rstrip().lstrip()) |
---|
| 329 | else: |
---|
| 330 | error="" |
---|
| 331 | if item[5]!=None: |
---|
| 332 | min= str(item[5].GetValue().rstrip().lstrip()) |
---|
| 333 | else: |
---|
| 334 | min="" |
---|
| 335 | if item[6]!=None: |
---|
| 336 | max= str(item[6].GetValue().rstrip().lstrip()) |
---|
| 337 | else: |
---|
| 338 | max="" |
---|
| 339 | if item[7]!=None: |
---|
| 340 | unit= str(item[7].GetLabelText().rstrip().lstrip()) |
---|
| 341 | else: |
---|
| 342 | unit="" |
---|
| 343 | if item[0]!=None: |
---|
| 344 | if item[0].GetValue(): |
---|
| 345 | state= "Fitted" |
---|
| 346 | else: |
---|
| 347 | state= "Not Fitted" |
---|
| 348 | else: |
---|
| 349 | state="Not Fitted" |
---|
| 350 | |
---|
| 351 | msg= "%s\t\t%s\t\t%s\t%s\t\t%s\t\t%s\t\t%s\t\t%s\n"\ |
---|
| 352 | %(name,value,sign,error,min,max,unit,state) |
---|
| 353 | out.write(msg) |
---|
| 354 | |
---|
| 355 | |
---|
| 356 | out.close() |
---|
| 357 | |
---|
[1328e03] | 358 | |
---|
| 359 | def onSetFocus(self, evt): |
---|
[77e23a2] | 360 | """ |
---|
| 361 | highlight the current textcrtl and hide the error text control shown |
---|
| 362 | after fitting |
---|
| 363 | """ |
---|
[cfc0913] | 364 | |
---|
[77e23a2] | 365 | if hasattr(self,"text2_3"): |
---|
| 366 | self.text2_3.Hide() |
---|
| 367 | if len(self.parameters)>0: |
---|
| 368 | for item in self.parameters: |
---|
| 369 | ## hide statictext +/- |
---|
| 370 | if item[3]!=None: |
---|
| 371 | item[3].Hide() |
---|
| 372 | ## hide textcrtl for error after fit |
---|
| 373 | if item[4]!=None: |
---|
| 374 | item[4].Clear() |
---|
| 375 | item[4].Hide() |
---|
| 376 | if len(self.fittable_param)>0: |
---|
| 377 | for item in self.fittable_param: |
---|
| 378 | ## hide statictext +/- |
---|
| 379 | if item[3]!=None: |
---|
| 380 | item[3].Hide() |
---|
| 381 | ## hide textcrtl for error after fit |
---|
| 382 | if item[4]!=None: |
---|
| 383 | item[4].Clear() |
---|
| 384 | item[4].Hide() |
---|
| 385 | self.Layout() |
---|
[1328e03] | 386 | # Get a handle to the TextCtrl |
---|
| 387 | widget = evt.GetEventObject() |
---|
| 388 | # Select the whole control, after this event resolves |
---|
[77e23a2] | 389 | wx.CallAfter(widget.SetSelection, -1,-1) |
---|
[1328e03] | 390 | return |
---|
[c77d859] | 391 | |
---|
| 392 | def read_file(self, path): |
---|
| 393 | """ |
---|
| 394 | Read two columns file |
---|
| 395 | @param path: the path to the file to read |
---|
| 396 | """ |
---|
| 397 | try: |
---|
| 398 | if path==None: |
---|
| 399 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
| 400 | " Selected Distribution was not loaded: %s"%path)) |
---|
| 401 | return None, None |
---|
| 402 | input_f = open(path, 'r') |
---|
| 403 | buff = input_f.read() |
---|
| 404 | lines = buff.split('\n') |
---|
| 405 | |
---|
| 406 | angles = [] |
---|
| 407 | weights=[] |
---|
| 408 | for line in lines: |
---|
| 409 | toks = line.split() |
---|
| 410 | if len(toks)==2: |
---|
| 411 | try: |
---|
| 412 | angle = float(toks[0]) |
---|
| 413 | weight = float(toks[1]) |
---|
| 414 | except: |
---|
| 415 | # Skip non-data lines |
---|
| 416 | pass |
---|
| 417 | angles.append(angle) |
---|
| 418 | weights.append(weight) |
---|
| 419 | return numpy.array(angles), numpy.array(weights) |
---|
| 420 | except: |
---|
| 421 | raise |
---|
[00c3aac] | 422 | |
---|
[cfc0913] | 423 | |
---|
| 424 | def createMemento(self): |
---|
| 425 | """ |
---|
| 426 | return the current state of the page |
---|
| 427 | """ |
---|
| 428 | return self.state.clone() |
---|
| 429 | |
---|
| 430 | |
---|
| 431 | def save_current_state(self): |
---|
| 432 | """ |
---|
| 433 | Store current state |
---|
| 434 | """ |
---|
| 435 | self.state.model = self.model.clone() |
---|
| 436 | self.state.save_data(self.data) |
---|
| 437 | |
---|
| 438 | if hasattr(self,"cb1"): |
---|
| 439 | self.state.cb1= self.cb1.GetValue() |
---|
| 440 | |
---|
| 441 | if hasattr(self,"enable_disp"): |
---|
| 442 | self.state.enable_disp= self.enable_disp.GetValue() |
---|
| 443 | |
---|
| 444 | if hasattr(self,"enable_smearer"): |
---|
| 445 | self.state.enable_smearer = self.enable_smearer.GetValue() |
---|
[b787e68c] | 446 | |
---|
| 447 | if hasattr(self,"disp_box"): |
---|
| 448 | self.state.disp_box = self.disp_box.GetCurrentSelection() |
---|
| 449 | self._save_plotting_range() |
---|
[cfc0913] | 450 | |
---|
| 451 | ## save checkbutton state and txtcrtl values |
---|
| 452 | self.state.parameters=[] |
---|
| 453 | self.state.fittable_param=[] |
---|
| 454 | self.state.fixed_param=[] |
---|
| 455 | self._copy_parameters_state(self.parameters, self.state.parameters) |
---|
| 456 | self._copy_parameters_state(self.fittable_param, self.state.fittable_param) |
---|
| 457 | self._copy_parameters_state(self.fixed_param, self.state.fixed_param) |
---|
| 458 | |
---|
| 459 | ## post state to fit panel |
---|
| 460 | event = PageInfoEvent(page = self) |
---|
| 461 | wx.PostEvent(self.parent, event) |
---|
| 462 | |
---|
[7c845cb] | 463 | def reset_page_helper(self, state): |
---|
[cfc0913] | 464 | """ |
---|
[b787e68c] | 465 | Use page_state and change the state of existing page |
---|
[cfc0913] | 466 | """ |
---|
| 467 | self.state = state.clone() |
---|
| 468 | self.model= self.state.model |
---|
| 469 | self.data = self.state.data |
---|
[b787e68c] | 470 | |
---|
| 471 | ##model parameter values restore |
---|
[cfc0913] | 472 | self._set_model_sizer_selection( self.model ) |
---|
| 473 | self.set_model_param_sizer(self.model) |
---|
| 474 | |
---|
[b787e68c] | 475 | ## display dispersion info layer |
---|
[cfc0913] | 476 | self.enable_disp.SetValue(self.state.enable_disp) |
---|
[b787e68c] | 477 | if hasattr(self, "disp_box"): |
---|
| 478 | self.disp_box.SetSelection(self.state.disp_box) |
---|
[cfc0913] | 479 | self._set_dipers_Param(event=None) |
---|
[b787e68c] | 480 | |
---|
| 481 | ## smearing info restore |
---|
[cfc0913] | 482 | if hasattr(self,"enable_smearer"): |
---|
[b787e68c] | 483 | self.enable_smearer.SetValue(state.enable_smearer) |
---|
| 484 | |
---|
| 485 | ##plotting range restore |
---|
| 486 | self._reset_plotting_range() |
---|
| 487 | |
---|
| 488 | ## reset state of checkbox,textcrtl and parameters value |
---|
[cfc0913] | 489 | if hasattr(self, "cb1"): |
---|
| 490 | self.cb1.SetValue(self.state.cb1) |
---|
| 491 | self._reset_parameters_state(self.parameters,state.parameters) |
---|
| 492 | self._reset_parameters_state(self.fittable_param,state.fittable_param) |
---|
| 493 | self._reset_parameters_state(self.fixed_param,state.fixed_param) |
---|
[b787e68c] | 494 | |
---|
| 495 | ## draw the model with previous parameters value |
---|
[cfc0913] | 496 | self._draw_model() |
---|
| 497 | |
---|
| 498 | |
---|
[c77d859] | 499 | def _selectDlg(self): |
---|
| 500 | """ |
---|
| 501 | open a dialog file to selected the customized dispersity |
---|
| 502 | """ |
---|
| 503 | import os |
---|
| 504 | dlg = wx.FileDialog(self, "Choose a weight file", os.getcwd(), "", "*.*", wx.OPEN) |
---|
| 505 | path = None |
---|
| 506 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 507 | path = dlg.GetPath() |
---|
| 508 | dlg.Destroy() |
---|
| 509 | return path |
---|
| 510 | |
---|
[cfc0913] | 511 | |
---|
| 512 | def _reset_plotting_range(self): |
---|
| 513 | """ |
---|
| 514 | Reset the plotting range to a given state |
---|
| 515 | """ |
---|
| 516 | |
---|
| 517 | self.qmin.SetValue(format_number(self.state.qmin)) |
---|
| 518 | self.qmax.SetValue(format_number(self.state.qmax)) |
---|
| 519 | if self.state.npts!=None: |
---|
[b787e68c] | 520 | self.npts.SetValue(format_number(self.state.npts)) |
---|
| 521 | self.num_points = float(self.state.npts) |
---|
[cfc0913] | 522 | |
---|
| 523 | self.qmin_x = float(self.qmin.GetValue()) |
---|
| 524 | self.qmax_x = float(self.qmax.GetValue()) |
---|
| 525 | |
---|
| 526 | |
---|
| 527 | def _save_plotting_range(self ): |
---|
| 528 | """ |
---|
| 529 | save the state of plotting range |
---|
| 530 | """ |
---|
[b787e68c] | 531 | self.state.qmin = self.qmin_x |
---|
| 532 | self.state.qmax = self.qmax_x |
---|
[cfc0913] | 533 | if self.npts!=None: |
---|
[b787e68c] | 534 | self.state.npts= self.num_points |
---|
[cfc0913] | 535 | |
---|
| 536 | |
---|
[c77d859] | 537 | def _onparamEnter_helper(self): |
---|
| 538 | """ |
---|
| 539 | check if values entered by the user are changed and valid to replot |
---|
| 540 | model |
---|
| 541 | use : _check_value_enter |
---|
| 542 | """ |
---|
| 543 | if self.model !=None: |
---|
[b787e68c] | 544 | |
---|
[c77d859] | 545 | # Flag to register when a parameter has changed. |
---|
| 546 | is_modified = False |
---|
| 547 | is_modified =self._check_value_enter( self.fittable_param ,is_modified) |
---|
| 548 | is_modified =self._check_value_enter( self.fixed_param ,is_modified) |
---|
| 549 | is_modified =self._check_value_enter( self.parameters ,is_modified) |
---|
| 550 | |
---|
[cfc0913] | 551 | |
---|
[0a518e4c] | 552 | self.Layout() |
---|
[c77d859] | 553 | # Here we should check whether the boundaries have been modified. |
---|
| 554 | # If qmin and qmax have been modified, update qmin and qmax and |
---|
| 555 | # set the is_modified flag to True |
---|
| 556 | from sans.guiframe.utils import check_value |
---|
| 557 | if check_value( self.qmin, self.qmax): |
---|
| 558 | if float(self.qmin.GetValue()) != self.qmin_x: |
---|
| 559 | self.qmin_x = float(self.qmin.GetValue()) |
---|
| 560 | is_modified = True |
---|
| 561 | if float(self.qmax.GetValue()) != self.qmax_x: |
---|
| 562 | self.qmax_x = float(self.qmax.GetValue()) |
---|
| 563 | is_modified = True |
---|
| 564 | self.fitrange = True |
---|
| 565 | else: |
---|
| 566 | self.fitrange = False |
---|
| 567 | if self.npts != None: |
---|
| 568 | if float(self.npts.GetValue()) != self.num_points: |
---|
| 569 | self.num_points = float(self.npts.GetValue()) |
---|
| 570 | is_modified = True |
---|
[b787e68c] | 571 | ## save current state |
---|
| 572 | self.save_current_state() |
---|
| 573 | |
---|
[c77d859] | 574 | ## if any value is modify draw model with new value |
---|
| 575 | if is_modified: |
---|
| 576 | self._draw_model() |
---|
| 577 | |
---|
| 578 | |
---|
[cfc0913] | 579 | def _reset_parameters_state(self, listtorestore,statelist): |
---|
| 580 | """ |
---|
| 581 | Reset the parameters at the given state |
---|
| 582 | """ |
---|
[6999659] | 583 | if len(statelist)==0 or len(listtorestore)==0 : |
---|
| 584 | return |
---|
| 585 | if len(statelist)!= len(listtorestore) : |
---|
[cfc0913] | 586 | return |
---|
| 587 | for j in range(len(listtorestore)): |
---|
| 588 | item_page = listtorestore[j] |
---|
| 589 | item_page_info = statelist[j] |
---|
| 590 | ##change the state of the check box for simple parameters |
---|
| 591 | if item_page[0]!=None: |
---|
| 592 | item_page[0].SetValue(item_page_info[0]) |
---|
| 593 | |
---|
| 594 | if item_page[2]!=None: |
---|
| 595 | item_page[2].SetValue(item_page_info[2]) |
---|
| 596 | |
---|
| 597 | if item_page[3]!=None: |
---|
| 598 | ## show or hide text +/- |
---|
| 599 | if item_page_info[2]: |
---|
| 600 | item_page[3].Show(True) |
---|
| 601 | else: |
---|
| 602 | item_page[3].Hide() |
---|
| 603 | if item_page[4]!=None: |
---|
| 604 | ## show of hide the text crtl for fitting error |
---|
| 605 | if item_page_info[4][0]: |
---|
| 606 | item_page[4].Show(True) |
---|
| 607 | item_page[4].SetValue(item_page_info[4][1]) |
---|
| 608 | else: |
---|
| 609 | item_page[3].Hide() |
---|
| 610 | if item_page[5]!=None: |
---|
| 611 | ## show of hide the text crtl for fitting error |
---|
| 612 | if item_page_info[5][0]: |
---|
| 613 | item_page[5].Show(True) |
---|
| 614 | item_page[5].SetValue(item_page_info[4][1]) |
---|
| 615 | else: |
---|
| 616 | item_page[5].Hide() |
---|
| 617 | |
---|
| 618 | if item_page[6]!=None: |
---|
| 619 | ## show of hide the text crtl for fitting error |
---|
| 620 | if item_page_info[6][0]: |
---|
| 621 | item_page[6].Show(True) |
---|
| 622 | item_page[6].SetValue(item_page_info[6][1]) |
---|
| 623 | else: |
---|
| 624 | item_page[6].Hide() |
---|
| 625 | |
---|
[b787e68c] | 626 | |
---|
[cfc0913] | 627 | def _copy_parameters_state(self, listtocopy, statelist): |
---|
| 628 | """ |
---|
| 629 | copy the state of button |
---|
| 630 | @param listtocopy: the list of check button to copy |
---|
| 631 | @param statelist: list of state object to store the current state |
---|
| 632 | """ |
---|
| 633 | if len(listtocopy)==0: |
---|
| 634 | return |
---|
| 635 | |
---|
| 636 | for item in listtocopy: |
---|
| 637 | checkbox_state = None |
---|
| 638 | if item[0]!= None: |
---|
| 639 | checkbox_state= item[0].GetValue() |
---|
| 640 | parameter_name = item[1] |
---|
| 641 | parameter_value = None |
---|
| 642 | if item[2]!=None: |
---|
| 643 | parameter_value = item[2].GetValue() |
---|
| 644 | static_text = None |
---|
| 645 | if item[3]!=None: |
---|
| 646 | static_text = item[3].IsShown() |
---|
| 647 | error_value = None |
---|
| 648 | error_state = None |
---|
| 649 | if item[4]!= None: |
---|
| 650 | error_value = item[4].GetValue() |
---|
| 651 | error_state = item[4].IsShown() |
---|
| 652 | |
---|
| 653 | min_value = None |
---|
| 654 | min_state = None |
---|
| 655 | if item[5]!= None: |
---|
| 656 | min_value = item[5].GetValue() |
---|
| 657 | min_state = item[5].IsShown() |
---|
| 658 | |
---|
| 659 | max_value = None |
---|
| 660 | max_state = None |
---|
| 661 | if item[6]!= None: |
---|
| 662 | max_value = item[6].GetValue() |
---|
| 663 | max_state = item[6].IsShown() |
---|
| 664 | |
---|
| 665 | statelist.append([checkbox_state, parameter_name, parameter_value, |
---|
| 666 | static_text ,[error_state,error_value], |
---|
| 667 | [min_state,min_value],[max_state , max_value],None]) |
---|
| 668 | |
---|
| 669 | |
---|
| 670 | |
---|
[c77d859] | 671 | def _set_model_sizer_selection(self, model): |
---|
| 672 | """ |
---|
| 673 | Display the sizer according to the type of the current model |
---|
| 674 | """ |
---|
| 675 | if hasattr(model ,"model2"): |
---|
| 676 | |
---|
| 677 | class_name= model.model2.__class__ |
---|
| 678 | name= model.model2.name |
---|
| 679 | flag= name != "NoStructure" |
---|
| 680 | if flag and (class_name in self.model_list_box["Structure Factors"]): |
---|
[3b605bb] | 681 | self.structurebox.Enable() |
---|
[c77d859] | 682 | items = self.structurebox.GetItems() |
---|
| 683 | self.sizer1.Layout() |
---|
| 684 | self.SetScrollbars(20,20,200,100) |
---|
| 685 | for i in range(len(items)): |
---|
| 686 | if items[i]== str(name): |
---|
| 687 | self.structurebox.SetSelection(i) |
---|
| 688 | break |
---|
| 689 | |
---|
| 690 | if hasattr(model ,"model1"): |
---|
| 691 | class_name = model.model1.__class__ |
---|
| 692 | name = model.model1.name |
---|
| 693 | self.formfactorbox.Clear() |
---|
| 694 | |
---|
| 695 | for k, list in self.model_list_box.iteritems(): |
---|
[376916c] | 696 | if k in["P(Q)*S(Q)","Shapes" ] and class_name in self.model_list_box["Shapes"]: |
---|
[c77d859] | 697 | self.shape_rbutton.SetValue(True) |
---|
[376916c] | 698 | ## fill the form factor list with new model |
---|
| 699 | self._populate_box(self.formfactorbox,self.model_list_box["Shapes"]) |
---|
| 700 | items = self.formfactorbox.GetItems() |
---|
| 701 | ## set comboxbox to the selected item |
---|
| 702 | for i in range(len(items)): |
---|
| 703 | if items[i]== str(name): |
---|
| 704 | self.formfactorbox.SetSelection(i) |
---|
| 705 | break |
---|
| 706 | return |
---|
| 707 | elif k == "Shape-Independent": |
---|
[c77d859] | 708 | self.shape_indep_rbutton.SetValue(True) |
---|
| 709 | elif k == "Structure Factors": |
---|
| 710 | self.struct_rbutton.SetValue(True) |
---|
| 711 | else: |
---|
| 712 | self.plugin_rbutton.SetValue(True) |
---|
[376916c] | 713 | |
---|
[c77d859] | 714 | if class_name in list: |
---|
| 715 | ## fill the form factor list with new model |
---|
| 716 | self._populate_box(self.formfactorbox, list) |
---|
| 717 | items = self.formfactorbox.GetItems() |
---|
| 718 | ## set comboxbox to the selected item |
---|
| 719 | for i in range(len(items)): |
---|
| 720 | if items[i]== str(name): |
---|
| 721 | self.formfactorbox.SetSelection(i) |
---|
| 722 | break |
---|
| 723 | break |
---|
| 724 | else: |
---|
[376916c] | 725 | ## Select the model from the combobox |
---|
[c77d859] | 726 | class_name = model.__class__ |
---|
| 727 | name = model.name |
---|
| 728 | self.formfactorbox.Clear() |
---|
| 729 | items = self.formfactorbox.GetItems() |
---|
[376916c] | 730 | |
---|
[c77d859] | 731 | for k, list in self.model_list_box.iteritems(): |
---|
[376916c] | 732 | if k in["P(Q)*S(Q)","Shapes" ] and class_name in self.model_list_box["Shapes"]: |
---|
[3b605bb] | 733 | |
---|
| 734 | if class_name in self.model_list_box["P(Q)*S(Q)"]: |
---|
| 735 | self.structurebox.Enable() |
---|
| 736 | else: |
---|
| 737 | self.structurebox.Disable() |
---|
| 738 | self.structurebox.SetSelection(0) |
---|
| 739 | |
---|
[c77d859] | 740 | self.shape_rbutton.SetValue(True) |
---|
[376916c] | 741 | ## fill the form factor list with new model |
---|
| 742 | self._populate_box(self.formfactorbox,self.model_list_box["Shapes"]) |
---|
| 743 | items = self.formfactorbox.GetItems() |
---|
| 744 | ## set comboxbox to the selected item |
---|
| 745 | for i in range(len(items)): |
---|
| 746 | if items[i]== str(name): |
---|
| 747 | self.formfactorbox.SetSelection(i) |
---|
| 748 | break |
---|
| 749 | return |
---|
| 750 | elif k == "Shape-Independent": |
---|
[c77d859] | 751 | self.shape_indep_rbutton.SetValue(True) |
---|
| 752 | elif k == "Structure Factors": |
---|
| 753 | self.struct_rbutton.SetValue(True) |
---|
| 754 | else: |
---|
| 755 | self.plugin_rbutton.SetValue(True) |
---|
| 756 | if class_name in list: |
---|
[3b605bb] | 757 | self.structurebox.SetSelection(0) |
---|
| 758 | self.structurebox.Disable() |
---|
[376916c] | 759 | ## fill the form factor list with new model |
---|
[c77d859] | 760 | self._populate_box(self.formfactorbox, list) |
---|
| 761 | items = self.formfactorbox.GetItems() |
---|
| 762 | ## set comboxbox to the selected item |
---|
| 763 | for i in range(len(items)): |
---|
| 764 | if items[i]== str(name): |
---|
| 765 | self.formfactorbox.SetSelection(i) |
---|
| 766 | break |
---|
| 767 | break |
---|
| 768 | |
---|
| 769 | |
---|
| 770 | def _draw_model(self): |
---|
| 771 | """ |
---|
| 772 | Method to draw or refresh a plotted model. |
---|
| 773 | The method will use the data member from the model page |
---|
| 774 | to build a call to the fitting perspective manager. |
---|
| 775 | |
---|
| 776 | [Note to coder: This way future changes will be done in only one place.] |
---|
| 777 | """ |
---|
| 778 | if self.model !=None: |
---|
| 779 | self.manager.draw_model(self.model, data=self.data, |
---|
[4e6b5939] | 780 | qmin=float(self.qmin_x), qmax=float(self.qmax_x), |
---|
| 781 | qstep= float(self.num_points), |
---|
[c77d859] | 782 | enable2D=self.enable2D) |
---|
| 783 | |
---|
| 784 | def _set_model_sizer(self, sizer, title="", object=None): |
---|
| 785 | """ |
---|
| 786 | Use lists to fill a sizer for model info |
---|
| 787 | """ |
---|
| 788 | |
---|
| 789 | sizer.Clear(True) |
---|
| 790 | box_description= wx.StaticBox(self, -1,str(title)) |
---|
| 791 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
| 792 | #-------------------------------------------------------- |
---|
[376916c] | 793 | self.shape_rbutton = wx.RadioButton(self, -1, 'Shapes', style=wx.RB_GROUP) |
---|
| 794 | self.shape_indep_rbutton = wx.RadioButton(self, -1, "Shape-Independent") |
---|
| 795 | self.struct_rbutton = wx.RadioButton(self, -1, "Structure Factor ") |
---|
| 796 | self.plugin_rbutton = wx.RadioButton(self, -1, "Customized Models") |
---|
[c77d859] | 797 | |
---|
| 798 | self.Bind( wx.EVT_RADIOBUTTON, self._show_combox, |
---|
| 799 | id= self.shape_rbutton.GetId() ) |
---|
| 800 | self.Bind( wx.EVT_RADIOBUTTON, self._show_combox, |
---|
| 801 | id= self.shape_indep_rbutton.GetId() ) |
---|
| 802 | self.Bind( wx.EVT_RADIOBUTTON, self._show_combox, |
---|
| 803 | id= self.struct_rbutton.GetId() ) |
---|
| 804 | self.Bind( wx.EVT_RADIOBUTTON, self._show_combox, |
---|
| 805 | id= self.plugin_rbutton.GetId() ) |
---|
[3b605bb] | 806 | |
---|
[cfc0913] | 807 | |
---|
[3b605bb] | 808 | sizer_radiobutton = wx.GridSizer(2, 2,5, 5) |
---|
[c77d859] | 809 | sizer_radiobutton.Add(self.shape_rbutton) |
---|
| 810 | sizer_radiobutton.Add(self.shape_indep_rbutton) |
---|
| 811 | sizer_radiobutton.Add(self.plugin_rbutton) |
---|
| 812 | sizer_radiobutton.Add(self.struct_rbutton) |
---|
| 813 | |
---|
| 814 | sizer_selection = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 815 | |
---|
| 816 | self.text1 = wx.StaticText( self,-1,"P(Q)" ) |
---|
| 817 | self.text2 = wx.StaticText( self,-1,"* S(Q)" ) |
---|
| 818 | |
---|
| 819 | |
---|
[8bd4dc0] | 820 | self.formfactorbox = wx.ComboBox(self, -1,style=wx.CB_READONLY) |
---|
[c77d859] | 821 | if self.model!=None: |
---|
| 822 | self.formfactorbox.SetValue(self.model.name) |
---|
[59a7f2d] | 823 | |
---|
| 824 | |
---|
[8bd4dc0] | 825 | self.structurebox = wx.ComboBox(self, -1,style=wx.CB_READONLY) |
---|
[c77d859] | 826 | wx.EVT_COMBOBOX(self.formfactorbox,-1, self._on_select_model) |
---|
| 827 | wx.EVT_COMBOBOX(self.structurebox,-1, self._on_select_model) |
---|
[3b605bb] | 828 | |
---|
| 829 | |
---|
[c77d859] | 830 | |
---|
| 831 | ## fill combox box |
---|
| 832 | if len(self.model_list_box)>0: |
---|
[376916c] | 833 | self._populate_box( self.formfactorbox,self.model_list_box["Shapes"]) |
---|
[c77d859] | 834 | |
---|
| 835 | if len(self.model_list_box)>0: |
---|
| 836 | self._populate_box( self.structurebox, |
---|
| 837 | self.model_list_box["Structure Factors"]) |
---|
[3b605bb] | 838 | self.structurebox.Insert("None", 0,None) |
---|
| 839 | self.structurebox.SetSelection(0) |
---|
| 840 | self.structurebox.Disable() |
---|
| 841 | |
---|
| 842 | if self.model.__class__ in self.model_list_box["P(Q)*S(Q)"]: |
---|
| 843 | self.structurebox.Enable() |
---|
| 844 | |
---|
[c77d859] | 845 | |
---|
[59a7f2d] | 846 | ## check model type to show sizer |
---|
[c77d859] | 847 | if self.model !=None: |
---|
| 848 | self._set_model_sizer_selection( self.model ) |
---|
| 849 | |
---|
| 850 | sizer_selection.Add(self.text1) |
---|
| 851 | sizer_selection.Add((5,5)) |
---|
| 852 | sizer_selection.Add(self.formfactorbox) |
---|
| 853 | sizer_selection.Add((5,5)) |
---|
| 854 | sizer_selection.Add(self.text2) |
---|
| 855 | sizer_selection.Add((5,5)) |
---|
| 856 | sizer_selection.Add(self.structurebox) |
---|
| 857 | sizer_selection.Add((5,5)) |
---|
| 858 | |
---|
| 859 | boxsizer1.Add( sizer_radiobutton ) |
---|
| 860 | boxsizer1.Add( (20,20)) |
---|
| 861 | boxsizer1.Add( sizer_selection ) |
---|
| 862 | if object !=None: |
---|
| 863 | boxsizer1.Add( (20,20)) |
---|
| 864 | boxsizer1.Add( object ) |
---|
| 865 | #-------------------------------------------------------- |
---|
| 866 | sizer.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
| 867 | sizer.Layout() |
---|
| 868 | self.SetScrollbars(20,20,200,100) |
---|
| 869 | |
---|
| 870 | |
---|
| 871 | def _show_combox(self, event): |
---|
| 872 | """ |
---|
| 873 | Show combox box associate with type of model selected |
---|
| 874 | """ |
---|
[376916c] | 875 | ## Don't want to populate combo box again if the event comes from check box |
---|
| 876 | if self.shape_rbutton.GetValue()and\ |
---|
| 877 | event.GetEventObject()==self.shape_rbutton: |
---|
| 878 | ##fill the combobox with form factor list |
---|
[3b605bb] | 879 | self.structurebox.SetSelection(0) |
---|
| 880 | self.structurebox.Disable() |
---|
[c77d859] | 881 | self.formfactorbox.Clear() |
---|
[376916c] | 882 | self._populate_box( self.formfactorbox,self.model_list_box["Shapes"]) |
---|
[c77d859] | 883 | |
---|
[376916c] | 884 | if self.shape_indep_rbutton.GetValue()and\ |
---|
| 885 | event.GetEventObject()==self.shape_indep_rbutton: |
---|
| 886 | ##fill the combobox with shape independent factor list |
---|
[3b605bb] | 887 | self.structurebox.SetSelection(0) |
---|
| 888 | self.structurebox.Disable() |
---|
[c77d859] | 889 | self.formfactorbox.Clear() |
---|
| 890 | self._populate_box( self.formfactorbox, |
---|
[376916c] | 891 | self.model_list_box["Shape-Independent"]) |
---|
[c77d859] | 892 | |
---|
[376916c] | 893 | if self.struct_rbutton.GetValue() and\ |
---|
| 894 | event.GetEventObject()==self.struct_rbutton: |
---|
| 895 | ##fill the combobox with structure factor list |
---|
[3b605bb] | 896 | self.structurebox.SetSelection(0) |
---|
| 897 | self.structurebox.Disable() |
---|
[c77d859] | 898 | self.formfactorbox.Clear() |
---|
| 899 | self._populate_box( self.formfactorbox, |
---|
| 900 | self.model_list_box["Structure Factors"]) |
---|
| 901 | |
---|
[376916c] | 902 | if self.plugin_rbutton.GetValue()and\ |
---|
| 903 | event.GetEventObject()==self.plugin_rbutton: |
---|
| 904 | |
---|
| 905 | ##fill the combobox with form factor list |
---|
[3b605bb] | 906 | self.structurebox.Disable() |
---|
[c77d859] | 907 | self.formfactorbox.Clear() |
---|
| 908 | self._populate_box( self.formfactorbox, |
---|
[376916c] | 909 | self.model_list_box["Customized Models"]) |
---|
| 910 | |
---|
[77e23a2] | 911 | self._on_select_model(event=None) |
---|
[3b605bb] | 912 | self.sizer4_4.Layout() |
---|
[9853ad0] | 913 | self.sizer4.Layout() |
---|
[3b605bb] | 914 | self.Layout() |
---|
| 915 | self.Refresh() |
---|
| 916 | self.SetScrollbars(20,20,200,100) |
---|
[c77d859] | 917 | |
---|
| 918 | def _populate_box(self, combobox, list): |
---|
| 919 | """ |
---|
| 920 | fill combox box with dict item |
---|
| 921 | @param list: contains item to fill the combox |
---|
| 922 | item must model class |
---|
| 923 | """ |
---|
| 924 | for models in list: |
---|
| 925 | model= models() |
---|
| 926 | name = model.__class__.__name__ |
---|
[0a518e4c] | 927 | if models.__name__!="NoStructure": |
---|
| 928 | if hasattr(model, "name"): |
---|
| 929 | name = model.name |
---|
| 930 | combobox.Append(name,models) |
---|
[9853ad0] | 931 | try: |
---|
[376916c] | 932 | |
---|
[9853ad0] | 933 | combobox.SetSelection(0) |
---|
[59a7f2d] | 934 | |
---|
[9853ad0] | 935 | except: |
---|
| 936 | pass |
---|
[cfc0913] | 937 | |
---|
[c77d859] | 938 | return 0 |
---|
| 939 | |
---|
| 940 | |
---|
[59a7f2d] | 941 | def _on_select_model_helper(self): |
---|
[c77d859] | 942 | """ |
---|
| 943 | call back for model selection |
---|
| 944 | """ |
---|
[376916c] | 945 | ## reset dictionary containing reference to dispersion |
---|
| 946 | self._disp_obj_dict = {} |
---|
| 947 | self.disp_cb_dict ={} |
---|
[c77d859] | 948 | f_id = self.formfactorbox.GetCurrentSelection() |
---|
| 949 | form_factor = self.formfactorbox.GetClientData( f_id ) |
---|
[376916c] | 950 | if not form_factor in self.model_list_box["multiplication"]: |
---|
[3b605bb] | 951 | self.structurebox.Disable() |
---|
| 952 | self.structurebox.SetSelection(0) |
---|
[376916c] | 953 | |
---|
[3b605bb] | 954 | |
---|
| 955 | s_id = self.structurebox.GetCurrentSelection() |
---|
| 956 | struct_factor = self.structurebox.GetClientData( s_id ) |
---|
| 957 | |
---|
| 958 | if struct_factor !=None: |
---|
[9853ad0] | 959 | from sans.models.MultiplicationModel import MultiplicationModel |
---|
| 960 | self.model= MultiplicationModel(form_factor(),struct_factor()) |
---|
[c77d859] | 961 | else: |
---|
[9853ad0] | 962 | self.model= form_factor() |
---|
[cfc0913] | 963 | |
---|
| 964 | ## post state to fit panel |
---|
| 965 | self.save_current_state() |
---|
[3b605bb] | 966 | self.sizer4_4.Layout() |
---|
| 967 | self.sizer4.Layout() |
---|
| 968 | self.Layout() |
---|
| 969 | self.SetScrollbars(20,20,200,100) |
---|
| 970 | self.Refresh() |
---|
| 971 | |
---|
[c77d859] | 972 | |
---|
| 973 | |
---|
| 974 | def _onparamEnter(self,event): |
---|
| 975 | """ |
---|
| 976 | when enter value on panel redraw model according to changed |
---|
| 977 | """ |
---|
| 978 | self._onparamEnter_helper() |
---|
| 979 | |
---|
| 980 | |
---|
| 981 | def _check_value_enter(self, list, modified): |
---|
| 982 | """ |
---|
| 983 | @param list: model parameter and panel info |
---|
| 984 | each item of the list should be as follow: |
---|
| 985 | item=[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
| 986 | """ |
---|
| 987 | is_modified = modified |
---|
| 988 | if len(list)==0: |
---|
| 989 | return is_modified |
---|
| 990 | for item in list: |
---|
| 991 | try: |
---|
[77e23a2] | 992 | name = str(item[1]) |
---|
[0a518e4c] | 993 | if hasattr(self,"text2_3"): |
---|
| 994 | self.text2_3.Hide() |
---|
[77e23a2] | 995 | ## check model parameters range |
---|
| 996 | ## check minimun value |
---|
| 997 | param_min= None |
---|
| 998 | param_max= None |
---|
| 999 | if item[5]!= None: |
---|
| 1000 | if format_number(item[5].GetValue())!="NaN": |
---|
| 1001 | param_min = float(item[5].GetValue()) |
---|
[cfc0913] | 1002 | |
---|
[77e23a2] | 1003 | ## check maximum value |
---|
| 1004 | if item[6]!= None: |
---|
| 1005 | if format_number(item[6].GetValue())!="NaN": |
---|
| 1006 | param_max = float(item[6].GetValue()) |
---|
| 1007 | |
---|
| 1008 | from sans.guiframe.utils import check_value |
---|
| 1009 | if param_min != None and param_max !=None: |
---|
[87a43fe] | 1010 | if not check_value(item[5], item[6]): |
---|
[77e23a2] | 1011 | msg= "Wrong Fit range entered for parameter " |
---|
| 1012 | msg+= "name %s of model %s "%(name, self.model.name) |
---|
| 1013 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
[cfc0913] | 1014 | if name in self.model.details.keys(): |
---|
| 1015 | self.model.details[name][1:]= param_min,param_max |
---|
[87a43fe] | 1016 | |
---|
[77e23a2] | 1017 | ## hide statictext +/- |
---|
[0a518e4c] | 1018 | if item[3]!=None: |
---|
| 1019 | item[3].Hide() |
---|
[77e23a2] | 1020 | ## hide textcrtl for error after fit |
---|
[0a518e4c] | 1021 | if item[4]!=None: |
---|
| 1022 | item[4].Clear() |
---|
| 1023 | item[4].Hide() |
---|
[77e23a2] | 1024 | |
---|
[0a518e4c] | 1025 | value= float(item[2].GetValue()) |
---|
| 1026 | # If the value of the parameter has changed, |
---|
[1328e03] | 1027 | # +update the model and set the is_modified flag |
---|
[0a518e4c] | 1028 | if value != self.model.getParam(name): |
---|
| 1029 | self.model.setParam(name,value) |
---|
| 1030 | is_modified = True |
---|
[c77d859] | 1031 | except: |
---|
[1328e03] | 1032 | msg= "Model Drawing Error:wrong value entered : %s"% sys.exc_value |
---|
| 1033 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
| 1034 | return |
---|
[0a518e4c] | 1035 | |
---|
[c77d859] | 1036 | return is_modified |
---|
| 1037 | |
---|
| 1038 | |
---|
| 1039 | def _set_dipers_Param(self, event): |
---|
| 1040 | """ |
---|
| 1041 | Add more item to select user dispersity |
---|
| 1042 | """ |
---|
| 1043 | if self.model == None: |
---|
| 1044 | msg= " Select non - model value:%s !"%self.model |
---|
| 1045 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg)) |
---|
| 1046 | return |
---|
| 1047 | else: |
---|
[cfc0913] | 1048 | |
---|
[c77d859] | 1049 | if self.enable_disp.GetValue(): |
---|
| 1050 | self.model_disp.Show(True) |
---|
| 1051 | self.disp_box.Show(True) |
---|
| 1052 | ## layout for model containing no dispersity parameters |
---|
| 1053 | if len(self.disp_list)==0: |
---|
| 1054 | self._layout_sizer_noDipers() |
---|
| 1055 | else: |
---|
| 1056 | ## set gaussian sizer |
---|
[b787e68c] | 1057 | self._on_select_Disp(event=None) |
---|
[c77d859] | 1058 | else: |
---|
| 1059 | self.model_disp.Hide() |
---|
| 1060 | self.disp_box.Hide() |
---|
| 1061 | self.sizer4_4.Clear(True) |
---|
| 1062 | self._reset_dispersity() |
---|
[cfc0913] | 1063 | |
---|
| 1064 | |
---|
| 1065 | ## post state to fit panel |
---|
| 1066 | self.save_current_state() |
---|
[c77d859] | 1067 | |
---|
[3b605bb] | 1068 | |
---|
[0a518e4c] | 1069 | |
---|
[c77d859] | 1070 | |
---|
| 1071 | def _layout_sizer_noDipers(self): |
---|
| 1072 | """ |
---|
| 1073 | Draw a sizer with no dispersity info |
---|
| 1074 | """ |
---|
| 1075 | ix=0 |
---|
| 1076 | iy=1 |
---|
| 1077 | self.fittable_param=[] |
---|
| 1078 | self.fixed_param=[] |
---|
| 1079 | self.model_disp.Hide() |
---|
| 1080 | self.disp_box.Hide() |
---|
| 1081 | self.sizer4_4.Clear(True) |
---|
| 1082 | model_disp = wx.StaticText(self, -1, 'No PolyDispersity for this model') |
---|
| 1083 | self.sizer4_4.Add(model_disp,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[dcf29d7] | 1084 | self.sizer4_4.Layout() |
---|
[c77d859] | 1085 | self.sizer4.Layout() |
---|
| 1086 | self.SetScrollbars(20,20,200,100) |
---|
[3b605bb] | 1087 | |
---|
[c77d859] | 1088 | |
---|
| 1089 | def _reset_dispersity(self): |
---|
| 1090 | """ |
---|
| 1091 | put gaussian dispersity into current model |
---|
| 1092 | """ |
---|
| 1093 | self.fittable_param=[] |
---|
| 1094 | self.fixed_param=[] |
---|
[376916c] | 1095 | |
---|
[c77d859] | 1096 | from sans.models.dispersion_models import GaussianDispersion |
---|
| 1097 | if len(self.disp_cb_dict)==0: |
---|
[3b605bb] | 1098 | self.sizer4_4.Clear(True) |
---|
| 1099 | self.sizer4_4.Layout() |
---|
| 1100 | self.sizer4.Layout() |
---|
| 1101 | self.Layout() |
---|
| 1102 | self.Refresh() |
---|
| 1103 | self.SetScrollbars(20,20,200,100) |
---|
[c77d859] | 1104 | return |
---|
| 1105 | |
---|
| 1106 | for p in self.disp_cb_dict: |
---|
| 1107 | # The parameter was un-selected. Go back to Gaussian model (with 0 pts) |
---|
| 1108 | disp_model = GaussianDispersion() |
---|
| 1109 | # Store the object to make it persist outside the scope of this method |
---|
| 1110 | #TODO: refactor model to clean this up? |
---|
| 1111 | self._disp_obj_dict[p] = disp_model |
---|
| 1112 | # Set the new model as the dispersion object for the selected parameter |
---|
| 1113 | self.model.set_dispersion(p, disp_model) |
---|
| 1114 | # Redraw the model |
---|
[376916c] | 1115 | self._draw_model() |
---|
[c77d859] | 1116 | |
---|
[3b605bb] | 1117 | self.sizer4_4.Layout() |
---|
| 1118 | self.sizer4.Layout() |
---|
| 1119 | self.Layout() |
---|
| 1120 | self.SetScrollbars(20,20,200,100) |
---|
| 1121 | self.Refresh() |
---|
| 1122 | |
---|
| 1123 | |
---|
[c77d859] | 1124 | |
---|
| 1125 | def _on_select_Disp(self,event): |
---|
| 1126 | """ |
---|
| 1127 | allow selecting different dispersion |
---|
| 1128 | self.disp_list should change type later .now only gaussian |
---|
| 1129 | """ |
---|
[b787e68c] | 1130 | |
---|
| 1131 | n = self.disp_box.GetCurrentSelection() |
---|
| 1132 | dispersity= self.disp_box.GetClientData(n) |
---|
[c77d859] | 1133 | name= dispersity.__name__ |
---|
[376916c] | 1134 | if name == "GaussianDispersion": |
---|
[c77d859] | 1135 | self._set_sizer_gaussian() |
---|
| 1136 | |
---|
| 1137 | if name=="ArrayDispersion": |
---|
| 1138 | self._set_sizer_arraydispersion() |
---|
[3b605bb] | 1139 | |
---|
[b787e68c] | 1140 | self.state.disp_box= n |
---|
| 1141 | ## post state to fit panel |
---|
| 1142 | event = PageInfoEvent(page = self) |
---|
| 1143 | wx.PostEvent(self.parent, event) |
---|
| 1144 | |
---|
[3b605bb] | 1145 | self.sizer4_4.Layout() |
---|
| 1146 | self.sizer4.Layout() |
---|
| 1147 | self.Layout() |
---|
| 1148 | self.SetScrollbars(20,20,200,100) |
---|
| 1149 | self.Refresh() |
---|
| 1150 | |
---|
| 1151 | |
---|
| 1152 | |
---|
[b787e68c] | 1153 | |
---|
[c77d859] | 1154 | def _set_sizer_arraydispersion(self): |
---|
| 1155 | """ |
---|
| 1156 | draw sizer with array dispersity parameters |
---|
| 1157 | """ |
---|
| 1158 | self.sizer4_4.Clear(True) |
---|
| 1159 | ix=0 |
---|
| 1160 | iy=1 |
---|
| 1161 | disp1 = wx.StaticText(self, -1, 'Array Dispersion') |
---|
| 1162 | self.sizer4_4.Add(disp1,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 1163 | |
---|
| 1164 | # Look for model parameters to which we can apply an ArrayDispersion model |
---|
| 1165 | # Add a check box for each parameter. |
---|
| 1166 | self.disp_cb_dict = {} |
---|
| 1167 | for p in self.model.dispersion.keys(): |
---|
| 1168 | ix+=1 |
---|
| 1169 | self.disp_cb_dict[p] = wx.CheckBox(self, -1, p, (10, 10)) |
---|
| 1170 | |
---|
| 1171 | wx.EVT_CHECKBOX(self, self.disp_cb_dict[p].GetId(), self.select_disp_angle) |
---|
| 1172 | self.sizer4_4.Add(self.disp_cb_dict[p], (iy, ix), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 1173 | |
---|
| 1174 | ix =0 |
---|
| 1175 | iy +=1 |
---|
| 1176 | self.sizer4_4.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 1177 | self.sizer4_4.Layout() |
---|
| 1178 | self.sizer4.Layout() |
---|
| 1179 | self.SetScrollbars(20,20,200,100) |
---|
| 1180 | |
---|
| 1181 | |
---|
| 1182 | def _set_range_sizer(self, title, object1=None,object=None): |
---|
| 1183 | """ |
---|
| 1184 | Fill the |
---|
| 1185 | """ |
---|
| 1186 | self.sizer5.Clear(True) |
---|
| 1187 | box_description= wx.StaticBox(self, -1,str(title)) |
---|
| 1188 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
| 1189 | #-------------------------------------------------------------- |
---|
| 1190 | self.qmin = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
| 1191 | self.qmin.SetValue(format_number(self.qmin_x)) |
---|
| 1192 | self.qmin.SetToolTipString("Minimun value of Q in linear scale.") |
---|
[1328e03] | 1193 | self.qmin.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
[dcf29d7] | 1194 | self.qmin.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
[c77d859] | 1195 | self.qmin.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
| 1196 | |
---|
| 1197 | self.qmax = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
| 1198 | self.qmax.SetValue(format_number(self.qmax_x)) |
---|
| 1199 | self.qmax.SetToolTipString("Maximum value of Q in linear scale.") |
---|
[1328e03] | 1200 | self.qmax.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
[dcf29d7] | 1201 | self.qmax.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
[c77d859] | 1202 | self.qmax.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
| 1203 | |
---|
| 1204 | sizer_horizontal=wx.BoxSizer(wx.HORIZONTAL) |
---|
| 1205 | sizer= wx.GridSizer(3, 3,5, 5) |
---|
| 1206 | |
---|
| 1207 | sizer.Add((5,5)) |
---|
| 1208 | sizer.Add(wx.StaticText(self, -1, 'Min')) |
---|
| 1209 | sizer.Add(wx.StaticText(self, -1, 'Max')) |
---|
| 1210 | sizer.Add(wx.StaticText(self, -1, 'Q range')) |
---|
| 1211 | |
---|
| 1212 | sizer.Add(self.qmin) |
---|
| 1213 | sizer.Add(self.qmax) |
---|
| 1214 | sizer_horizontal.Add(sizer) |
---|
| 1215 | if object!=None: |
---|
| 1216 | sizer_horizontal.Add(object) |
---|
| 1217 | |
---|
| 1218 | if object1!=None: |
---|
| 1219 | boxsizer1.Add(object1) |
---|
| 1220 | boxsizer1.Add((10,10)) |
---|
| 1221 | boxsizer1.Add(sizer_horizontal) |
---|
[cfc0913] | 1222 | ## save state |
---|
| 1223 | self.save_current_state() |
---|
[c77d859] | 1224 | #---------------------------------------------------------------- |
---|
| 1225 | self.sizer5.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
| 1226 | self.sizer5.Layout() |
---|
[dcf29d7] | 1227 | self.Layout() |
---|
[c77d859] | 1228 | self.SetScrollbars(20,20,200,100) |
---|
| 1229 | |
---|
[cfc0913] | 1230 | |
---|
[00c3aac] | 1231 | def _fill_save_sizer(self): |
---|
| 1232 | """ |
---|
| 1233 | Draw the layout for saving option |
---|
| 1234 | """ |
---|
| 1235 | self.sizer6.Clear(True) |
---|
| 1236 | box_description= wx.StaticBox(self, -1,"Save Options") |
---|
| 1237 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
| 1238 | sizer_save = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 1239 | |
---|
| 1240 | self.btSave = wx.Button(self,wx.NewId(),'Save') |
---|
| 1241 | self.btSave.Bind(wx.EVT_BUTTON, self.onSave,id= self.btSave.GetId()) |
---|
| 1242 | self.btSave.SetToolTipString("Save current state") |
---|
| 1243 | |
---|
| 1244 | sizer_save.Add((20,20),0, wx.LEFT|wx.RIGHT|wx.EXPAND,120) |
---|
| 1245 | sizer_save.Add(self.btSave) |
---|
| 1246 | |
---|
| 1247 | boxsizer1.Add(sizer_save) |
---|
| 1248 | self.sizer6.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
| 1249 | self.sizer6.Layout() |
---|
| 1250 | self.SetScrollbars(20,20,200,100) |
---|
| 1251 | |
---|
[c77d859] | 1252 | |
---|
| 1253 | |
---|