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