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