[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 |
---|
| 8 | |
---|
| 9 | _BOX_WIDTH = 80 |
---|
| 10 | |
---|
| 11 | class BasicPage(wx.ScrolledWindow): |
---|
| 12 | """ |
---|
| 13 | This class provide general structure of fitpanel page |
---|
| 14 | """ |
---|
| 15 | ## Internal name for the AUI manager |
---|
| 16 | window_name = "Fit page" |
---|
| 17 | ## Title to appear on top of the window |
---|
| 18 | window_caption = "Fit Page" |
---|
| 19 | name="" |
---|
| 20 | def __init__(self,parent, pageinfo, name): |
---|
| 21 | wx.ScrolledWindow.__init__(self, parent) |
---|
| 22 | ## store name |
---|
| 23 | self.name = name |
---|
| 24 | ## store info of panel |
---|
| 25 | self.page_info= pageinfo |
---|
| 26 | self.page_info.page_name = name |
---|
| 27 | ## parent of the page |
---|
| 28 | self.parent = parent |
---|
| 29 | ## owner of the page (fitting plugin) |
---|
| 30 | self.event_owner= None |
---|
| 31 | ## create the basic structure of the panel with empty sizer |
---|
| 32 | self.define_page_structure() |
---|
| 33 | ## dictionary containing list of models |
---|
| 34 | self.model_list_box={} |
---|
| 35 | ## Data member to store the dispersion object created |
---|
| 36 | self._disp_obj_dict = {} |
---|
| 37 | self.disp_cb_dict ={} |
---|
| 38 | ##list of model parameters. each item must have same length |
---|
| 39 | ## each item related to a given parameters |
---|
| 40 | ##[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
| 41 | self.parameters=[] |
---|
| 42 | ## list of parameters to fit , must be like self.parameters |
---|
| 43 | self.param_toFit=[] |
---|
| 44 | ## list of looking like parameters but with non fittable parameters info |
---|
| 45 | self.fixed_param=[] |
---|
| 46 | ## list of looking like parameters but with fittable parameters info |
---|
| 47 | self.fittable_param=[] |
---|
| 48 | ##list of dispersion parameters |
---|
| 49 | self.disp_list=[] |
---|
| 50 | |
---|
| 51 | ##enable model 2D draw |
---|
| 52 | self.enable2D= False |
---|
| 53 | ## check that the fit range is correct to plot the model again |
---|
| 54 | self.fitrange= True |
---|
| 55 | ## current model |
---|
| 56 | self.model= None |
---|
| 57 | ## data |
---|
| 58 | self.data=None |
---|
| 59 | ## Q range |
---|
| 60 | self.qmin_x= 0.001 |
---|
| 61 | self.qmax_x= 0.1 |
---|
| 62 | self.num_points= 100 |
---|
| 63 | ## changing initial values given page_info value |
---|
| 64 | if self.page_info !=None and len(self.page_info.model_list_box) >0: |
---|
| 65 | self.model_list_box = self.page_info.model_list_box |
---|
| 66 | ## current model |
---|
| 67 | self.model=self.page_info.model |
---|
| 68 | self.data = self.page_info.data |
---|
| 69 | self.event_owner = self.page_info.event_owner |
---|
| 70 | if self.model !=None: |
---|
| 71 | self.disp_list= self.model.getDispParamList() |
---|
| 72 | ## drawing Initial dispersion parameters sizer |
---|
| 73 | self.set_dispers_sizer() |
---|
| 74 | ## layout |
---|
| 75 | self.set_layout() |
---|
| 76 | |
---|
| 77 | |
---|
| 78 | def define_page_structure(self): |
---|
| 79 | """ |
---|
| 80 | Create empty sizer for a panel |
---|
| 81 | """ |
---|
| 82 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
| 83 | self.sizer0 = wx.BoxSizer(wx.VERTICAL) |
---|
| 84 | self.sizer1 = wx.BoxSizer(wx.VERTICAL) |
---|
| 85 | self.sizer2 = wx.BoxSizer(wx.VERTICAL) |
---|
| 86 | self.sizer3 = wx.BoxSizer(wx.VERTICAL) |
---|
| 87 | self.sizer4 = wx.BoxSizer(wx.VERTICAL) |
---|
| 88 | self.sizer5 = wx.BoxSizer(wx.VERTICAL) |
---|
[0a518e4c] | 89 | self.sizer6 = wx.BoxSizer(wx.VERTICAL) |
---|
[c77d859] | 90 | |
---|
[8bd4dc0] | 91 | self.sizer0.SetMinSize((375,-1)) |
---|
| 92 | self.sizer1.SetMinSize((375,-1)) |
---|
| 93 | self.sizer2.SetMinSize((375,-1)) |
---|
| 94 | self.sizer3.SetMinSize((375,-1)) |
---|
| 95 | self.sizer4.SetMinSize((375,-1)) |
---|
| 96 | self.sizer5.SetMinSize((375,-1)) |
---|
[0a518e4c] | 97 | self.sizer6.SetMinSize((375,-1)) |
---|
[8bd4dc0] | 98 | |
---|
[c77d859] | 99 | self.vbox.Add(self.sizer0) |
---|
| 100 | self.vbox.Add(self.sizer1) |
---|
| 101 | self.vbox.Add(self.sizer2) |
---|
| 102 | self.vbox.Add(self.sizer3) |
---|
| 103 | self.vbox.Add(self.sizer4) |
---|
| 104 | self.vbox.Add(self.sizer5) |
---|
[0a518e4c] | 105 | self.vbox.Add(self.sizer6) |
---|
[c77d859] | 106 | |
---|
| 107 | def set_layout(self): |
---|
| 108 | """ |
---|
| 109 | layout |
---|
| 110 | """ |
---|
| 111 | self.vbox.Layout() |
---|
| 112 | self.vbox.Fit(self) |
---|
| 113 | self.SetSizer(self.vbox) |
---|
| 114 | |
---|
| 115 | self.set_scroll() |
---|
| 116 | self.Centre() |
---|
| 117 | |
---|
| 118 | def set_scroll(self): |
---|
| 119 | self.SetScrollbars(20,20,200,100) |
---|
| 120 | self.Layout() |
---|
[0a518e4c] | 121 | self.SetAutoLayout(True) |
---|
[c77d859] | 122 | |
---|
| 123 | def set_owner(self,owner): |
---|
| 124 | """ |
---|
| 125 | set owner of fitpage |
---|
| 126 | @param owner: the class responsible of plotting |
---|
| 127 | """ |
---|
| 128 | self.event_owner = owner |
---|
| 129 | self.page_info.event_owner = owner |
---|
| 130 | |
---|
| 131 | def set_manager(self, manager): |
---|
| 132 | """ |
---|
| 133 | set panel manager |
---|
| 134 | @param manager: instance of plugin fitting |
---|
| 135 | """ |
---|
| 136 | self.manager = manager |
---|
| 137 | self.page_info.manager = manager |
---|
| 138 | |
---|
| 139 | def populate_box(self, dict): |
---|
| 140 | """ |
---|
| 141 | Store list of model |
---|
| 142 | @param dict: dictionary containing list of models |
---|
| 143 | """ |
---|
| 144 | self.model_list_box = dict |
---|
| 145 | if self.page_info!=None: |
---|
| 146 | self.page_info.model_list_box = self.model_list_box |
---|
| 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)) |
---|
| 160 | self.Bind(wx.EVT_RADIOBUTTON, self._set_dipers_Param, id=self.disable_disp.GetId()) |
---|
| 161 | self.Bind(wx.EVT_RADIOBUTTON, self._set_dipers_Param, id=self.enable_disp.GetId()) |
---|
| 162 | |
---|
| 163 | sizer_dispersion = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 164 | sizer_dispersion.Add((20,20)) |
---|
| 165 | sizer_dispersion.Add(wx.StaticText(self,-1,'Polydispersity: ')) |
---|
| 166 | sizer_dispersion.Add(self.enable_disp ) |
---|
| 167 | sizer_dispersion.Add((20,20)) |
---|
| 168 | sizer_dispersion.Add(self.disable_disp ) |
---|
| 169 | sizer_dispersion.Add((10,10)) |
---|
| 170 | |
---|
| 171 | ## fill a sizer with the combobox to select dispersion type |
---|
| 172 | sizer_select_dispers = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 173 | self.model_disp = wx.StaticText(self, -1, 'Model Disp') |
---|
| 174 | |
---|
| 175 | import sans.models.dispersion_models |
---|
| 176 | self.polydisp= sans.models.dispersion_models.models |
---|
[0a518e4c] | 177 | self.disp_box = wx.ComboBox(self, -1) |
---|
[c77d859] | 178 | self.disp_box.SetValue("GaussianModel") |
---|
| 179 | |
---|
| 180 | for key in self.polydisp.iterkeys(): |
---|
| 181 | name = str(key.__name__) |
---|
| 182 | if name=="ArrayDispersion": |
---|
| 183 | # Remove the option until the rest of the code is ready for it |
---|
| 184 | self.disp_box.Append("Select customized Model",key) |
---|
| 185 | pass |
---|
| 186 | else: |
---|
| 187 | self.disp_box.Append(name,key) |
---|
| 188 | wx.EVT_COMBOBOX(self.disp_box,-1, self._on_select_Disp) |
---|
| 189 | |
---|
| 190 | sizer_select_dispers.Add((10,10)) |
---|
| 191 | sizer_select_dispers.Add(self.model_disp) |
---|
| 192 | sizer_select_dispers.Add(self.disp_box) |
---|
| 193 | sizer_select_dispers.Add((10,10)) |
---|
| 194 | self.model_disp.Hide() |
---|
| 195 | self.disp_box.Hide() |
---|
| 196 | |
---|
| 197 | boxsizer1.Add( sizer_dispersion ) |
---|
| 198 | boxsizer1.Add( (10,10) ) |
---|
| 199 | boxsizer1.Add( sizer_select_dispers ) |
---|
| 200 | self.sizer4_4 = wx.GridBagSizer(5,5) |
---|
| 201 | boxsizer1.Add( self.sizer4_4 ) |
---|
| 202 | #----------------------------------------------------- |
---|
| 203 | self.sizer4.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
| 204 | self.sizer4.Layout() |
---|
[0a518e4c] | 205 | |
---|
[c77d859] | 206 | self.SetScrollbars(20,20,200,100) |
---|
[0a518e4c] | 207 | |
---|
[c77d859] | 208 | |
---|
| 209 | def select_disp_angle(self, event): |
---|
| 210 | """ |
---|
| 211 | Event for when a user select a parameter to average over. |
---|
| 212 | @param event: check box event |
---|
| 213 | """ |
---|
| 214 | |
---|
| 215 | |
---|
| 216 | # Go through the list of dispersion check boxes to identify which one has changed |
---|
| 217 | for p in self.disp_cb_dict: |
---|
| 218 | # Catch which one of the box was just checked or unchecked. |
---|
| 219 | if event.GetEventObject() == self.disp_cb_dict[p]: |
---|
| 220 | |
---|
| 221 | |
---|
| 222 | if self.disp_cb_dict[p].GetValue() == True: |
---|
| 223 | # The user wants this parameter to be averaged. |
---|
| 224 | # Pop up the file selection dialog. |
---|
| 225 | path = self._selectDlg() |
---|
| 226 | |
---|
| 227 | # If nothing was selected, just return |
---|
| 228 | if path is None: |
---|
| 229 | self.disp_cb_dict[p].SetValue(False) |
---|
| 230 | return |
---|
| 231 | |
---|
| 232 | try: |
---|
| 233 | values,weights = self.read_file(path) |
---|
| 234 | except: |
---|
| 235 | msg="Could not read input file" |
---|
| 236 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg)) |
---|
| 237 | return |
---|
| 238 | |
---|
| 239 | # If any of the two arrays is empty, notify the user that we won't |
---|
| 240 | # proceed |
---|
| 241 | if values is None or weights is None: |
---|
| 242 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
| 243 | "The loaded %s distrubtion is corrupted or empty" % p)) |
---|
| 244 | return |
---|
| 245 | |
---|
| 246 | # Tell the user that we are about to apply the distribution |
---|
| 247 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
| 248 | "Applying loaded %s distribution: %s" % (p, path))) |
---|
| 249 | |
---|
| 250 | # Create the dispersion objects |
---|
| 251 | from sans.models.dispersion_models import ArrayDispersion |
---|
| 252 | disp_model = ArrayDispersion() |
---|
| 253 | disp_model.set_weights(values, weights) |
---|
| 254 | # Store the object to make it persist outside the scope of this method |
---|
| 255 | #TODO: refactor model to clean this up? |
---|
| 256 | self._disp_obj_dict[p] = disp_model |
---|
| 257 | |
---|
| 258 | # Set the new model as the dispersion object for the selected parameter |
---|
| 259 | self.model.set_dispersion(p, disp_model) |
---|
| 260 | |
---|
| 261 | else: |
---|
| 262 | # The parameter was un-selected. Go back to Gaussian model (with 0 pts) |
---|
| 263 | self._reset_dispersity() |
---|
| 264 | |
---|
| 265 | ## Redraw the model |
---|
| 266 | self._draw_model() |
---|
| 267 | return |
---|
| 268 | |
---|
| 269 | |
---|
| 270 | |
---|
| 271 | |
---|
| 272 | def read_file(self, path): |
---|
| 273 | """ |
---|
| 274 | Read two columns file |
---|
| 275 | @param path: the path to the file to read |
---|
| 276 | """ |
---|
| 277 | try: |
---|
| 278 | if path==None: |
---|
| 279 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
| 280 | " Selected Distribution was not loaded: %s"%path)) |
---|
| 281 | return None, None |
---|
| 282 | input_f = open(path, 'r') |
---|
| 283 | buff = input_f.read() |
---|
| 284 | lines = buff.split('\n') |
---|
| 285 | |
---|
| 286 | angles = [] |
---|
| 287 | weights=[] |
---|
| 288 | for line in lines: |
---|
| 289 | toks = line.split() |
---|
| 290 | if len(toks)==2: |
---|
| 291 | try: |
---|
| 292 | angle = float(toks[0]) |
---|
| 293 | weight = float(toks[1]) |
---|
| 294 | except: |
---|
| 295 | # Skip non-data lines |
---|
| 296 | pass |
---|
| 297 | angles.append(angle) |
---|
| 298 | weights.append(weight) |
---|
| 299 | return numpy.array(angles), numpy.array(weights) |
---|
| 300 | except: |
---|
| 301 | raise |
---|
| 302 | |
---|
| 303 | |
---|
| 304 | def _selectDlg(self): |
---|
| 305 | """ |
---|
| 306 | open a dialog file to selected the customized dispersity |
---|
| 307 | """ |
---|
| 308 | import os |
---|
| 309 | dlg = wx.FileDialog(self, "Choose a weight file", os.getcwd(), "", "*.*", wx.OPEN) |
---|
| 310 | path = None |
---|
| 311 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 312 | path = dlg.GetPath() |
---|
| 313 | dlg.Destroy() |
---|
| 314 | return path |
---|
| 315 | |
---|
| 316 | |
---|
| 317 | def _onparamEnter_helper(self): |
---|
| 318 | """ |
---|
| 319 | check if values entered by the user are changed and valid to replot |
---|
| 320 | model |
---|
| 321 | use : _check_value_enter |
---|
| 322 | """ |
---|
| 323 | if self.model !=None: |
---|
| 324 | # Flag to register when a parameter has changed. |
---|
| 325 | is_modified = False |
---|
| 326 | is_modified =self._check_value_enter( self.fittable_param ,is_modified) |
---|
| 327 | is_modified =self._check_value_enter( self.fixed_param ,is_modified) |
---|
| 328 | is_modified =self._check_value_enter( self.parameters ,is_modified) |
---|
| 329 | |
---|
[0a518e4c] | 330 | self.Layout() |
---|
[c77d859] | 331 | # Here we should check whether the boundaries have been modified. |
---|
| 332 | # If qmin and qmax have been modified, update qmin and qmax and |
---|
| 333 | # set the is_modified flag to True |
---|
| 334 | from sans.guiframe.utils import check_value |
---|
| 335 | if check_value( self.qmin, self.qmax): |
---|
| 336 | if float(self.qmin.GetValue()) != self.qmin_x: |
---|
| 337 | self.qmin_x = float(self.qmin.GetValue()) |
---|
| 338 | is_modified = True |
---|
| 339 | if float(self.qmax.GetValue()) != self.qmax_x: |
---|
| 340 | self.qmax_x = float(self.qmax.GetValue()) |
---|
| 341 | is_modified = True |
---|
| 342 | self.fitrange = True |
---|
| 343 | else: |
---|
| 344 | self.fitrange = False |
---|
| 345 | if self.npts != None: |
---|
| 346 | if float(self.npts.GetValue()) != self.num_points: |
---|
| 347 | self.num_points = float(self.npts.GetValue()) |
---|
| 348 | is_modified = True |
---|
| 349 | ## if any value is modify draw model with new value |
---|
| 350 | if is_modified: |
---|
| 351 | self._draw_model() |
---|
| 352 | |
---|
| 353 | |
---|
| 354 | def _set_model_sizer_selection(self, model): |
---|
| 355 | """ |
---|
| 356 | Display the sizer according to the type of the current model |
---|
| 357 | """ |
---|
| 358 | if hasattr(model ,"model2"): |
---|
| 359 | |
---|
| 360 | class_name= model.model2.__class__ |
---|
| 361 | name= model.model2.name |
---|
| 362 | flag= name != "NoStructure" |
---|
| 363 | if flag and (class_name in self.model_list_box["Structure Factors"]): |
---|
| 364 | self.structurebox.Show(True) |
---|
| 365 | self.text2.Show(True) |
---|
| 366 | self.multip_cb.SetValue(True) |
---|
| 367 | items = self.structurebox.GetItems() |
---|
| 368 | self.sizer1.Layout() |
---|
| 369 | self.SetScrollbars(20,20,200,100) |
---|
| 370 | for i in range(len(items)): |
---|
| 371 | if items[i]== str(name): |
---|
| 372 | self.structurebox.SetSelection(i) |
---|
| 373 | break |
---|
| 374 | |
---|
| 375 | if hasattr(model ,"model1"): |
---|
| 376 | class_name = model.model1.__class__ |
---|
| 377 | name = model.model1.name |
---|
| 378 | self.formfactorbox.Clear() |
---|
| 379 | |
---|
| 380 | for k, list in self.model_list_box.iteritems(): |
---|
| 381 | if k == "shapes": |
---|
| 382 | self.shape_rbutton.SetValue(True) |
---|
| 383 | elif k == "Shape-independent": |
---|
| 384 | self.shape_indep_rbutton.SetValue(True) |
---|
| 385 | elif k == "Structure Factors": |
---|
| 386 | self.struct_rbutton.SetValue(True) |
---|
| 387 | else: |
---|
| 388 | self.plugin_rbutton.SetValue(True) |
---|
| 389 | |
---|
| 390 | if class_name in list: |
---|
| 391 | ## fill the form factor list with new model |
---|
| 392 | self._populate_box(self.formfactorbox, list) |
---|
| 393 | items = self.formfactorbox.GetItems() |
---|
| 394 | ## set comboxbox to the selected item |
---|
| 395 | for i in range(len(items)): |
---|
| 396 | if items[i]== str(name): |
---|
| 397 | self.formfactorbox.SetSelection(i) |
---|
| 398 | break |
---|
| 399 | break |
---|
| 400 | else: |
---|
| 401 | ## Select the model from the combo box |
---|
| 402 | class_name = model.__class__ |
---|
| 403 | name = model.name |
---|
| 404 | self.formfactorbox.Clear() |
---|
| 405 | items = self.formfactorbox.GetItems() |
---|
| 406 | for k, list in self.model_list_box.iteritems(): |
---|
| 407 | if k == "shapes": |
---|
| 408 | self.shape_rbutton.SetValue(True) |
---|
| 409 | elif k == "Shape-independent": |
---|
| 410 | self.shape_indep_rbutton.SetValue(True) |
---|
| 411 | elif k == "Structure Factors": |
---|
| 412 | self.struct_rbutton.SetValue(True) |
---|
| 413 | else: |
---|
| 414 | self.plugin_rbutton.SetValue(True) |
---|
| 415 | if class_name in list: |
---|
| 416 | ## fill the form factor list with new model |
---|
| 417 | self._populate_box(self.formfactorbox, list) |
---|
| 418 | items = self.formfactorbox.GetItems() |
---|
| 419 | ## set comboxbox to the selected item |
---|
| 420 | for i in range(len(items)): |
---|
| 421 | if items[i]== str(name): |
---|
| 422 | self.formfactorbox.SetSelection(i) |
---|
| 423 | break |
---|
| 424 | break |
---|
| 425 | |
---|
| 426 | |
---|
| 427 | |
---|
| 428 | def _draw_model(self): |
---|
| 429 | """ |
---|
| 430 | Method to draw or refresh a plotted model. |
---|
| 431 | The method will use the data member from the model page |
---|
| 432 | to build a call to the fitting perspective manager. |
---|
| 433 | |
---|
| 434 | [Note to coder: This way future changes will be done in only one place.] |
---|
| 435 | """ |
---|
| 436 | if self.model !=None: |
---|
| 437 | self.manager.draw_model(self.model, data=self.data, |
---|
| 438 | qmin=self.qmin_x, qmax=self.qmax_x, |
---|
| 439 | qstep= self.num_points, |
---|
| 440 | enable2D=self.enable2D) |
---|
| 441 | |
---|
| 442 | def _set_model_sizer(self, sizer, title="", object=None): |
---|
| 443 | """ |
---|
| 444 | Use lists to fill a sizer for model info |
---|
| 445 | """ |
---|
| 446 | |
---|
| 447 | sizer.Clear(True) |
---|
| 448 | box_description= wx.StaticBox(self, -1,str(title)) |
---|
| 449 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
| 450 | #-------------------------------------------------------- |
---|
| 451 | self.shape_rbutton = wx.RadioButton(self, -1, 'Shape', style=wx.RB_GROUP) |
---|
| 452 | self.shape_indep_rbutton = wx.RadioButton(self, -1, "Shape Independent") |
---|
| 453 | self.struct_rbutton = wx.RadioButton(self, -1, "Structure ") |
---|
| 454 | self.plugin_rbutton = wx.RadioButton(self, -1, "customized Model") |
---|
| 455 | self.multip_cb = wx.CheckBox(self, -1,"P(Q)*S(Q)") |
---|
| 456 | |
---|
| 457 | |
---|
| 458 | |
---|
| 459 | self.Bind( wx.EVT_RADIOBUTTON, self._show_combox, |
---|
| 460 | id= self.shape_rbutton.GetId() ) |
---|
| 461 | self.Bind( wx.EVT_RADIOBUTTON, self._show_combox, |
---|
| 462 | id= self.shape_indep_rbutton.GetId() ) |
---|
| 463 | self.Bind( wx.EVT_RADIOBUTTON, self._show_combox, |
---|
| 464 | id= self.struct_rbutton.GetId() ) |
---|
| 465 | self.Bind( wx.EVT_RADIOBUTTON, self._show_combox, |
---|
| 466 | id= self.plugin_rbutton.GetId() ) |
---|
| 467 | wx.EVT_CHECKBOX(self, self.multip_cb.GetId() , self._show_combox ) |
---|
| 468 | ## store state |
---|
| 469 | self.page_info.save_radiobox_state( self.shape_rbutton ) |
---|
| 470 | self.page_info.save_radiobox_state( self.shape_indep_rbutton ) |
---|
| 471 | self.page_info.save_radiobox_state( self.struct_rbutton ) |
---|
| 472 | self.page_info.save_radiobox_state( self.plugin_rbutton ) |
---|
| 473 | |
---|
| 474 | |
---|
| 475 | sizer_radiobutton = wx.GridSizer(3, 3,5, 5) |
---|
| 476 | sizer_radiobutton.Add(self.shape_rbutton) |
---|
| 477 | sizer_radiobutton.Add(self.shape_indep_rbutton) |
---|
| 478 | sizer_radiobutton.Add(self.multip_cb) |
---|
| 479 | sizer_radiobutton.Add(self.plugin_rbutton) |
---|
| 480 | sizer_radiobutton.Add(self.struct_rbutton) |
---|
| 481 | |
---|
| 482 | sizer_selection = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 483 | |
---|
| 484 | self.text1 = wx.StaticText( self,-1,"P(Q)" ) |
---|
| 485 | self.text2 = wx.StaticText( self,-1,"* S(Q)" ) |
---|
| 486 | |
---|
| 487 | self.text2.Hide() |
---|
| 488 | |
---|
[8bd4dc0] | 489 | self.formfactorbox = wx.ComboBox(self, -1,style=wx.CB_READONLY) |
---|
[c77d859] | 490 | if self.model!=None: |
---|
| 491 | self.formfactorbox.SetValue(self.model.name) |
---|
| 492 | |
---|
[8bd4dc0] | 493 | self.structurebox = wx.ComboBox(self, -1,style=wx.CB_READONLY) |
---|
[c77d859] | 494 | wx.EVT_COMBOBOX(self.formfactorbox,-1, self._on_select_model) |
---|
| 495 | wx.EVT_COMBOBOX(self.structurebox,-1, self._on_select_model) |
---|
| 496 | self.structurebox.Hide() |
---|
| 497 | |
---|
| 498 | ## fill combox box |
---|
| 499 | if len(self.model_list_box)>0: |
---|
| 500 | self._populate_box( self.formfactorbox,self.model_list_box["shapes"]) |
---|
| 501 | |
---|
| 502 | if len(self.model_list_box)>0: |
---|
| 503 | self._populate_box( self.structurebox, |
---|
| 504 | self.model_list_box["Structure Factors"]) |
---|
| 505 | |
---|
| 506 | ## check model type to show sizer |
---|
| 507 | if self.model !=None: |
---|
| 508 | self._set_model_sizer_selection( self.model ) |
---|
| 509 | |
---|
| 510 | sizer_selection.Add(self.text1) |
---|
| 511 | sizer_selection.Add((5,5)) |
---|
| 512 | sizer_selection.Add(self.formfactorbox) |
---|
| 513 | sizer_selection.Add((5,5)) |
---|
| 514 | sizer_selection.Add(self.text2) |
---|
| 515 | sizer_selection.Add((5,5)) |
---|
| 516 | sizer_selection.Add(self.structurebox) |
---|
| 517 | sizer_selection.Add((5,5)) |
---|
| 518 | |
---|
| 519 | boxsizer1.Add( sizer_radiobutton ) |
---|
| 520 | boxsizer1.Add( (20,20)) |
---|
| 521 | boxsizer1.Add( sizer_selection ) |
---|
| 522 | if object !=None: |
---|
| 523 | boxsizer1.Add( (20,20)) |
---|
| 524 | boxsizer1.Add( object ) |
---|
| 525 | #-------------------------------------------------------- |
---|
| 526 | sizer.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
| 527 | sizer.Layout() |
---|
| 528 | self.SetScrollbars(20,20,200,100) |
---|
| 529 | |
---|
| 530 | |
---|
| 531 | def _show_combox(self, event): |
---|
| 532 | """ |
---|
| 533 | Show combox box associate with type of model selected |
---|
| 534 | """ |
---|
| 535 | self.page_info.save_radiobox_state( self.shape_rbutton ) |
---|
| 536 | self.page_info.save_radiobox_state( self.shape_indep_rbutton ) |
---|
| 537 | self.page_info.save_radiobox_state( self.struct_rbutton ) |
---|
| 538 | self.page_info.save_radiobox_state( self.plugin_rbutton ) |
---|
| 539 | |
---|
| 540 | |
---|
| 541 | |
---|
| 542 | if self.shape_rbutton.GetValue(): |
---|
| 543 | ##fill the comboxbox with form factor list |
---|
| 544 | self.structurebox.Hide() |
---|
| 545 | self.text2.Hide() |
---|
| 546 | self.formfactorbox.Clear() |
---|
| 547 | self._populate_box( self.formfactorbox,self.model_list_box["shapes"]) |
---|
| 548 | |
---|
| 549 | if self.shape_indep_rbutton.GetValue(): |
---|
| 550 | ##fill the comboxbox with shape independent factor list |
---|
| 551 | self.structurebox.Hide() |
---|
| 552 | self.text2.Hide() |
---|
| 553 | self.formfactorbox.Clear() |
---|
| 554 | self._populate_box( self.formfactorbox, |
---|
| 555 | self.model_list_box["Shape-independent"]) |
---|
| 556 | |
---|
| 557 | if self.struct_rbutton.GetValue(): |
---|
| 558 | ##fill the comboxbox with structure factor list |
---|
| 559 | self.structurebox.Hide() |
---|
| 560 | self.text2.Hide() |
---|
| 561 | self.formfactorbox.Clear() |
---|
| 562 | self._populate_box( self.formfactorbox, |
---|
| 563 | self.model_list_box["Structure Factors"]) |
---|
| 564 | |
---|
| 565 | if self.plugin_rbutton.GetValue(): |
---|
| 566 | ##fill the comboxbox with form factor list |
---|
| 567 | self.structurebox.Hide() |
---|
| 568 | self.text2.Hide() |
---|
| 569 | self.formfactorbox.Clear() |
---|
| 570 | self._populate_box( self.formfactorbox, |
---|
| 571 | self.model_list_box["Added models"]) |
---|
| 572 | |
---|
| 573 | |
---|
| 574 | if self.multip_cb.GetValue(): |
---|
| 575 | ## multplication not available for structure factor |
---|
| 576 | if self.struct_rbutton.GetValue(): |
---|
| 577 | self.multip_cb.SetValue(False) |
---|
| 578 | self.structurebox.Hide() |
---|
| 579 | return |
---|
| 580 | ##fill the comboxbox with form factor list |
---|
| 581 | self.structurebox.Show(True) |
---|
| 582 | self.text2.Show(True) |
---|
[0a518e4c] | 583 | ## draw empty model |
---|
| 584 | from sans.models.NullModel import NullModel |
---|
| 585 | self.model = NullModel() |
---|
| 586 | self.model.name = "Select P(Q) and S(Q)" |
---|
| 587 | self.set_model_param_sizer(self.model) |
---|
| 588 | self.sizer4_4.Clear() |
---|
| 589 | self.sizer4.Layout() |
---|
| 590 | self._draw_model() |
---|
| 591 | |
---|
[c77d859] | 592 | self.set_scroll() |
---|
| 593 | |
---|
| 594 | |
---|
| 595 | |
---|
| 596 | |
---|
| 597 | def _populate_box(self, combobox, list): |
---|
| 598 | """ |
---|
| 599 | fill combox box with dict item |
---|
| 600 | @param list: contains item to fill the combox |
---|
| 601 | item must model class |
---|
| 602 | """ |
---|
| 603 | for models in list: |
---|
| 604 | model= models() |
---|
| 605 | name = model.__class__.__name__ |
---|
[0a518e4c] | 606 | if models.__name__!="NoStructure": |
---|
| 607 | if hasattr(model, "name"): |
---|
| 608 | name = model.name |
---|
| 609 | combobox.Append(name,models) |
---|
[c77d859] | 610 | |
---|
| 611 | wx.EVT_COMBOBOX(combobox,-1, self._on_select_model) |
---|
| 612 | return 0 |
---|
| 613 | |
---|
| 614 | |
---|
| 615 | def _on_select_model_helper(self,event): |
---|
| 616 | """ |
---|
| 617 | call back for model selection |
---|
| 618 | """ |
---|
| 619 | f_id = self.formfactorbox.GetCurrentSelection() |
---|
| 620 | s_id = self.structurebox.GetCurrentSelection() |
---|
| 621 | form_factor = self.formfactorbox.GetClientData( f_id ) |
---|
| 622 | struct_factor = self.structurebox.GetClientData( s_id ) |
---|
| 623 | |
---|
| 624 | if self.multip_cb.GetValue(): |
---|
[0a518e4c] | 625 | if struct_factor != None and form_factor != None: |
---|
[c77d859] | 626 | from sans.models.MultiplicationModel import MultiplicationModel |
---|
[0a518e4c] | 627 | self.model= MultiplicationModel(form_factor(),struct_factor()) |
---|
| 628 | else: |
---|
| 629 | from sans.models.NullModel import NullModel |
---|
| 630 | self.model = NullModel() |
---|
| 631 | self.model.name = "Select P(Q) and S(Q)" |
---|
| 632 | msg= "select one P(Q) and one S(Q) to plot" |
---|
| 633 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg)) |
---|
[c77d859] | 634 | else: |
---|
| 635 | if form_factor != None: |
---|
| 636 | self.model= form_factor() |
---|
| 637 | |
---|
| 638 | |
---|
| 639 | def _onparamEnter(self,event): |
---|
| 640 | """ |
---|
| 641 | when enter value on panel redraw model according to changed |
---|
| 642 | """ |
---|
| 643 | self._onparamEnter_helper() |
---|
| 644 | |
---|
| 645 | |
---|
| 646 | def _check_value_enter(self, list, modified): |
---|
| 647 | """ |
---|
| 648 | @param list: model parameter and panel info |
---|
| 649 | each item of the list should be as follow: |
---|
| 650 | item=[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
| 651 | """ |
---|
| 652 | is_modified = modified |
---|
| 653 | if len(list)==0: |
---|
| 654 | return is_modified |
---|
| 655 | for item in list: |
---|
| 656 | try: |
---|
[0a518e4c] | 657 | if hasattr(self,"text2_3"): |
---|
| 658 | self.text2_3.Hide() |
---|
| 659 | if item[3]!=None: |
---|
| 660 | item[3].Hide() |
---|
| 661 | if item[4]!=None: |
---|
| 662 | item[4].Clear() |
---|
| 663 | item[4].Hide() |
---|
| 664 | name = str(item[1]) |
---|
| 665 | value= float(item[2].GetValue()) |
---|
| 666 | # If the value of the parameter has changed, |
---|
| 667 | # update the model and set the is_modified flag |
---|
| 668 | if value != self.model.getParam(name): |
---|
| 669 | self.model.setParam(name,value) |
---|
| 670 | is_modified = True |
---|
[c77d859] | 671 | except: |
---|
| 672 | msg= "Model Drawing Error:wrong value entered : %s"% sys.exc_value |
---|
| 673 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
| 674 | return |
---|
[0a518e4c] | 675 | |
---|
[c77d859] | 676 | return is_modified |
---|
| 677 | |
---|
| 678 | |
---|
| 679 | |
---|
| 680 | def _set_dipers_Param(self, event): |
---|
| 681 | """ |
---|
| 682 | Add more item to select user dispersity |
---|
| 683 | """ |
---|
| 684 | if self.model == None: |
---|
| 685 | msg= " Select non - model value:%s !"%self.model |
---|
| 686 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg)) |
---|
| 687 | return |
---|
| 688 | else: |
---|
| 689 | if self.enable_disp.GetValue(): |
---|
| 690 | self.model_disp.Show(True) |
---|
| 691 | self.disp_box.Show(True) |
---|
| 692 | ## layout for model containing no dispersity parameters |
---|
| 693 | if len(self.disp_list)==0: |
---|
| 694 | self._layout_sizer_noDipers() |
---|
| 695 | else: |
---|
| 696 | ## set gaussian sizer |
---|
| 697 | self._set_sizer_gaussian() |
---|
| 698 | else: |
---|
| 699 | self.model_disp.Hide() |
---|
| 700 | self.disp_box.Hide() |
---|
| 701 | self.sizer4_4.Clear(True) |
---|
| 702 | self._reset_dispersity() |
---|
| 703 | self._draw_model() |
---|
| 704 | |
---|
| 705 | self.sizer4.Layout() |
---|
[0a518e4c] | 706 | self.Layout() |
---|
[c77d859] | 707 | self.SetScrollbars(20,20,200,100) |
---|
[0a518e4c] | 708 | |
---|
[c77d859] | 709 | |
---|
| 710 | |
---|
| 711 | def _layout_sizer_noDipers(self): |
---|
| 712 | """ |
---|
| 713 | Draw a sizer with no dispersity info |
---|
| 714 | """ |
---|
| 715 | ix=0 |
---|
| 716 | iy=1 |
---|
| 717 | self.fittable_param=[] |
---|
| 718 | self.fixed_param=[] |
---|
| 719 | self.model_disp.Hide() |
---|
| 720 | self.disp_box.Hide() |
---|
| 721 | self.sizer4_4.Clear(True) |
---|
| 722 | model_disp = wx.StaticText(self, -1, 'No PolyDispersity for this model') |
---|
| 723 | self.sizer4_4.Add(model_disp,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[0a518e4c] | 724 | #self.sizer4_4.Layout() |
---|
[c77d859] | 725 | self.sizer4.Layout() |
---|
| 726 | self.SetScrollbars(20,20,200,100) |
---|
| 727 | return |
---|
| 728 | |
---|
| 729 | def _reset_gaussian_dispers(self): |
---|
| 730 | """ |
---|
| 731 | reset model paramaters to gaussian |
---|
| 732 | TO IMPLEMENT |
---|
| 733 | """ |
---|
| 734 | |
---|
| 735 | return |
---|
| 736 | from sans.models.dispersion_models import GaussianDispersion |
---|
| 737 | if self.model==None: |
---|
| 738 | return |
---|
| 739 | list= self.model.dispersion.keys() |
---|
| 740 | for key in list: |
---|
| 741 | |
---|
| 742 | ## The parameter was un-selected. Go back to Gaussian model (with 0 pts) |
---|
| 743 | disp_model = GaussianDispersion() |
---|
| 744 | ## Set the new model as the dispersion object for the selected parameter |
---|
| 745 | self.model.set_dispersion(key,disp_model) |
---|
| 746 | self._draw_model() |
---|
| 747 | |
---|
| 748 | |
---|
| 749 | def _reset_dispersity(self): |
---|
| 750 | """ |
---|
| 751 | put gaussian dispersity into current model |
---|
| 752 | """ |
---|
| 753 | self.fittable_param=[] |
---|
| 754 | self.fixed_param=[] |
---|
| 755 | from sans.models.dispersion_models import GaussianDispersion |
---|
| 756 | if len(self.disp_cb_dict)==0: |
---|
| 757 | return |
---|
| 758 | |
---|
| 759 | for p in self.disp_cb_dict: |
---|
| 760 | # The parameter was un-selected. Go back to Gaussian model (with 0 pts) |
---|
| 761 | disp_model = GaussianDispersion() |
---|
| 762 | # Store the object to make it persist outside the scope of this method |
---|
| 763 | #TODO: refactor model to clean this up? |
---|
| 764 | self._disp_obj_dict[p] = disp_model |
---|
| 765 | |
---|
| 766 | # Set the new model as the dispersion object for the selected parameter |
---|
| 767 | self.model.set_dispersion(p, disp_model) |
---|
| 768 | |
---|
| 769 | # Redraw the model |
---|
| 770 | |
---|
| 771 | |
---|
| 772 | |
---|
| 773 | def _on_select_Disp(self,event): |
---|
| 774 | """ |
---|
| 775 | allow selecting different dispersion |
---|
| 776 | self.disp_list should change type later .now only gaussian |
---|
| 777 | """ |
---|
| 778 | dispersity =event.GetClientData() |
---|
| 779 | name= dispersity.__name__ |
---|
| 780 | |
---|
| 781 | if name == "GaussianModel": |
---|
| 782 | self._set_sizer_gaussian() |
---|
| 783 | |
---|
| 784 | if name=="ArrayDispersion": |
---|
| 785 | self._set_sizer_arraydispersion() |
---|
| 786 | |
---|
| 787 | |
---|
| 788 | |
---|
| 789 | |
---|
| 790 | def _set_sizer_arraydispersion(self): |
---|
| 791 | """ |
---|
| 792 | draw sizer with array dispersity parameters |
---|
| 793 | """ |
---|
| 794 | self.sizer4_4.Clear(True) |
---|
| 795 | ix=0 |
---|
| 796 | iy=1 |
---|
| 797 | disp1 = wx.StaticText(self, -1, 'Array Dispersion') |
---|
| 798 | self.sizer4_4.Add(disp1,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 799 | |
---|
| 800 | # Look for model parameters to which we can apply an ArrayDispersion model |
---|
| 801 | # Add a check box for each parameter. |
---|
| 802 | self.disp_cb_dict = {} |
---|
| 803 | for p in self.model.dispersion.keys(): |
---|
| 804 | ix+=1 |
---|
| 805 | self.disp_cb_dict[p] = wx.CheckBox(self, -1, p, (10, 10)) |
---|
| 806 | |
---|
| 807 | wx.EVT_CHECKBOX(self, self.disp_cb_dict[p].GetId(), self.select_disp_angle) |
---|
| 808 | self.sizer4_4.Add(self.disp_cb_dict[p], (iy, ix), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 809 | |
---|
| 810 | ix =0 |
---|
| 811 | iy +=1 |
---|
| 812 | self.sizer4_4.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 813 | self.sizer4_4.Layout() |
---|
| 814 | self.sizer4.Layout() |
---|
| 815 | self.SetScrollbars(20,20,200,100) |
---|
| 816 | |
---|
| 817 | |
---|
| 818 | def _set_range_sizer(self, title, object1=None,object=None): |
---|
| 819 | """ |
---|
| 820 | Fill the |
---|
| 821 | """ |
---|
| 822 | self.sizer5.Clear(True) |
---|
| 823 | box_description= wx.StaticBox(self, -1,str(title)) |
---|
| 824 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
| 825 | #-------------------------------------------------------------- |
---|
| 826 | self.qmin = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
| 827 | self.qmin.SetValue(format_number(self.qmin_x)) |
---|
| 828 | self.qmin.SetToolTipString("Minimun value of Q in linear scale.") |
---|
| 829 | self.qmin.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
| 830 | self.qmin.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
| 831 | |
---|
| 832 | self.qmax = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
| 833 | self.qmax.SetValue(format_number(self.qmax_x)) |
---|
| 834 | self.qmax.SetToolTipString("Maximum value of Q in linear scale.") |
---|
| 835 | self.qmax.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
| 836 | self.qmax.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
| 837 | |
---|
| 838 | sizer_horizontal=wx.BoxSizer(wx.HORIZONTAL) |
---|
| 839 | sizer= wx.GridSizer(3, 3,5, 5) |
---|
| 840 | |
---|
| 841 | sizer.Add((5,5)) |
---|
| 842 | sizer.Add(wx.StaticText(self, -1, 'Min')) |
---|
| 843 | sizer.Add(wx.StaticText(self, -1, 'Max')) |
---|
| 844 | sizer.Add(wx.StaticText(self, -1, 'Q range')) |
---|
| 845 | |
---|
| 846 | sizer.Add(self.qmin) |
---|
| 847 | sizer.Add(self.qmax) |
---|
| 848 | sizer_horizontal.Add(sizer) |
---|
| 849 | if object!=None: |
---|
| 850 | sizer_horizontal.Add(object) |
---|
| 851 | |
---|
| 852 | if object1!=None: |
---|
| 853 | boxsizer1.Add(object1) |
---|
| 854 | boxsizer1.Add((10,10)) |
---|
| 855 | boxsizer1.Add(sizer_horizontal) |
---|
| 856 | |
---|
| 857 | #---------------------------------------------------------------- |
---|
| 858 | self.sizer5.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
| 859 | self.sizer5.Layout() |
---|
| 860 | self.SetScrollbars(20,20,200,100) |
---|
| 861 | |
---|
| 862 | |
---|
| 863 | |
---|