[0277d084] | 1 | |
---|
[2f60121] | 2 | import sys |
---|
| 3 | import os |
---|
[0277d084] | 4 | import wx |
---|
| 5 | import numpy |
---|
| 6 | import time |
---|
| 7 | import copy |
---|
| 8 | import math |
---|
| 9 | import string |
---|
[fc292d3] | 10 | from wx.lib.scrolledpanel import ScrolledPanel |
---|
[2f60121] | 11 | from sans.guiframe.utils import format_number |
---|
| 12 | from sans.guiframe.utils import check_float |
---|
[f53444be] | 13 | from sans.guiframe.events import StatusEvent |
---|
[0277d084] | 14 | import pagestate |
---|
| 15 | from pagestate import PageState |
---|
[2f60121] | 16 | |
---|
| 17 | (PageInfoEvent, EVT_PAGE_INFO) = wx.lib.newevent.NewEvent() |
---|
| 18 | (PreviousStateEvent, EVT_PREVIOUS_STATE)= wx.lib.newevent.NewEvent() |
---|
| 19 | (NextStateEvent, EVT_NEXT_STATE) = wx.lib.newevent.NewEvent() |
---|
| 20 | |
---|
[0277d084] | 21 | _BOX_WIDTH = 76 |
---|
| 22 | _QMIN_DEFAULT = 0.001 |
---|
| 23 | _QMAX_DEFAULT = 0.13 |
---|
| 24 | _NPTS_DEFAULT = 50 |
---|
| 25 | #Control panel width |
---|
[2f60121] | 26 | if sys.platform.count("darwin") == 0: |
---|
[0277d084] | 27 | PANEL_WIDTH = 450 |
---|
| 28 | FONT_VARIANT = 0 |
---|
| 29 | ON_MAC = False |
---|
| 30 | else: |
---|
| 31 | PANEL_WIDTH = 500 |
---|
| 32 | FONT_VARIANT = 1 |
---|
| 33 | ON_MAC = True |
---|
| 34 | |
---|
[9299455] | 35 | class BasicPage(ScrolledPanel): |
---|
[0277d084] | 36 | """ |
---|
[74755ff] | 37 | This class provide general structure of fitpanel page |
---|
| 38 | |
---|
[0277d084] | 39 | """ |
---|
| 40 | ## Internal name for the AUI manager |
---|
| 41 | window_name = "Basic Page" |
---|
| 42 | ## Title to appear on top of the window |
---|
| 43 | window_caption = "Basic page " |
---|
| 44 | |
---|
[2f60121] | 45 | def __init__(self,parent, page_info=None, model_list_box=None): |
---|
[9299455] | 46 | ScrolledPanel.__init__(self, parent) |
---|
[fc292d3] | 47 | self.SetupScrolling() |
---|
[0277d084] | 48 | #Set window's font size |
---|
| 49 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
| 50 | ## parent of the page |
---|
| 51 | self.parent = parent |
---|
| 52 | self.model_list_box = model_list_box |
---|
| 53 | self.model = None |
---|
| 54 | self.manager = None |
---|
| 55 | self.data = None |
---|
| 56 | ## owner of the page (fitting plugin) |
---|
| 57 | self.event_owner = None |
---|
| 58 | if page_info is not None: |
---|
| 59 | ## manager is the fitting plugin |
---|
[2f60121] | 60 | self.manager = page_info.manager |
---|
[0277d084] | 61 | ## owner of the page (fitting plugin) |
---|
[2f60121] | 62 | self.event_owner = page_info.event_owner |
---|
[0277d084] | 63 | ## current model |
---|
| 64 | self.model = page_info.model |
---|
| 65 | ## data |
---|
| 66 | self.data = page_info.data |
---|
| 67 | ## dictionary containing list of models |
---|
| 68 | self.model_list_box = page_info.model_list_box |
---|
| 69 | ##window_name |
---|
| 70 | self.window_name = page_info.window_name |
---|
| 71 | ##window_caption |
---|
| 72 | self.window_caption = page_info.window_caption |
---|
| 73 | ## Data member to store the dispersion object created |
---|
| 74 | self._disp_obj_dict = {} |
---|
| 75 | ## selected parameters to apply dispersion |
---|
[2f60121] | 76 | self.disp_cb_dict = {} |
---|
[0277d084] | 77 | ## smearer object |
---|
| 78 | self.smearer = None |
---|
| 79 | ##list of model parameters. each item must have same length |
---|
| 80 | ## each item related to a given parameters |
---|
| 81 | ##[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
[2f60121] | 82 | self.parameters = [] |
---|
[fb59ed9] | 83 | # non-fittable parameter whose value is astring |
---|
| 84 | self.str_parameters = [] |
---|
[0277d084] | 85 | ## list of parameters to fit , must be like self.parameters |
---|
[2f60121] | 86 | self.param_toFit = [] |
---|
[0277d084] | 87 | ## list of looking like parameters but with non fittable parameters info |
---|
[2f60121] | 88 | self.fixed_param = [] |
---|
[0277d084] | 89 | ## list of looking like parameters but with fittable parameters info |
---|
[2f60121] | 90 | self.fittable_param = [] |
---|
[0277d084] | 91 | ##list of dispersion parameters |
---|
[2f60121] | 92 | self.disp_list = [] |
---|
| 93 | self.disp_name = "" |
---|
[0277d084] | 94 | ## list of orientation parameters |
---|
[2f60121] | 95 | self.orientation_params = [] |
---|
| 96 | self.orientation_params_disp = [] |
---|
[0277d084] | 97 | if self.model != None: |
---|
[2f60121] | 98 | self.disp_list = self.model.getDispParamList() |
---|
[4523b68] | 99 | self.multi_factor = None |
---|
[0277d084] | 100 | ##enable model 2D draw |
---|
[2f60121] | 101 | self.enable2D = False |
---|
[0277d084] | 102 | ## check that the fit range is correct to plot the model again |
---|
[2f60121] | 103 | self.fitrange = True |
---|
[0277d084] | 104 | ## Q range |
---|
[2f60121] | 105 | self.qmin_x = _QMIN_DEFAULT |
---|
| 106 | self.qmax_x = _QMAX_DEFAULT |
---|
| 107 | self.num_points = _NPTS_DEFAULT |
---|
[0277d084] | 108 | ## Create memento to save the current state |
---|
[2f60121] | 109 | self.state = PageState(parent=self.parent, |
---|
| 110 | model=self.model, data=self.data) |
---|
[0277d084] | 111 | ## flag to determine if state has change |
---|
[2f60121] | 112 | self.state_change = False |
---|
[0277d084] | 113 | ## save customized array |
---|
[2f60121] | 114 | self.values = [] |
---|
| 115 | self.weights = [] |
---|
[0277d084] | 116 | ## retrieve saved state |
---|
[2f60121] | 117 | self.number_saved_state = 0 |
---|
[0277d084] | 118 | ## dictionary of saved state |
---|
[2f60121] | 119 | self.saved_states = {} |
---|
[0277d084] | 120 | |
---|
| 121 | ## Create context menu for page |
---|
| 122 | self.popUpMenu = wx.Menu() |
---|
| 123 | #id = wx.NewId() |
---|
[2f60121] | 124 | #self._undo = wx.MenuItem(self.popUpMenu,id, "Undo" |
---|
| 125 | #,"cancel the previous action") |
---|
[0277d084] | 126 | #self.popUpMenu.AppendItem(self._undo) |
---|
| 127 | #self._undo.Enable(False) |
---|
| 128 | #wx.EVT_MENU(self, id, self.onUndo) |
---|
| 129 | |
---|
| 130 | #id = wx.NewId() |
---|
[2f60121] | 131 | #self._redo = wx.MenuItem(self.popUpMenu,id,"Redo", |
---|
| 132 | #" Restore the previous action") |
---|
[0277d084] | 133 | #self.popUpMenu.AppendItem(self._redo) |
---|
| 134 | #self._redo.Enable(False) |
---|
| 135 | #wx.EVT_MENU(self, id, self.onRedo) |
---|
| 136 | #self.popUpMenu.AppendSeparator() |
---|
| 137 | #if sys.platform.count("win32")>0: |
---|
| 138 | id = wx.NewId() |
---|
[2f60121] | 139 | self._keep = wx.MenuItem(self.popUpMenu, id, "BookMark", |
---|
| 140 | " Keep the panel status to recall it later") |
---|
[0277d084] | 141 | self.popUpMenu.AppendItem(self._keep) |
---|
| 142 | self._keep.Enable(True) |
---|
| 143 | wx.EVT_MENU(self, id, self.onSave) |
---|
| 144 | self.popUpMenu.AppendSeparator() |
---|
| 145 | ## Default locations |
---|
| 146 | self._default_save_location = os.getcwd() |
---|
| 147 | ## save initial state on context menu |
---|
| 148 | #self.onSave(event=None) |
---|
| 149 | self.Bind(wx.EVT_CONTEXT_MENU, self.onContextMenu) |
---|
| 150 | |
---|
| 151 | ## create the basic structure of the panel with empty sizer |
---|
| 152 | self.define_page_structure() |
---|
| 153 | ## drawing Initial dispersion parameters sizer |
---|
| 154 | self.set_dispers_sizer() |
---|
| 155 | self._fill_save_sizer() |
---|
| 156 | ## layout |
---|
| 157 | self.set_layout() |
---|
| 158 | |
---|
| 159 | class ModelTextCtrl(wx.TextCtrl): |
---|
| 160 | """ |
---|
[74755ff] | 161 | Text control for model and fit parameters. |
---|
| 162 | Binds the appropriate events for user interactions. |
---|
| 163 | Default callback methods can be overwritten on initialization |
---|
| 164 | |
---|
| 165 | :param kill_focus_callback: callback method for EVT_KILL_FOCUS event |
---|
| 166 | :param set_focus_callback: callback method for EVT_SET_FOCUS event |
---|
| 167 | :param mouse_up_callback: callback method for EVT_LEFT_UP event |
---|
| 168 | :param text_enter_callback: callback method for EVT_TEXT_ENTER event |
---|
| 169 | |
---|
[0277d084] | 170 | """ |
---|
[2f60121] | 171 | ## Set to True when the mouse is clicked |
---|
| 172 | #while the whole string is selected |
---|
[0277d084] | 173 | full_selection = False |
---|
| 174 | ## Call back for EVT_SET_FOCUS events |
---|
| 175 | _on_set_focus_callback = None |
---|
| 176 | |
---|
[2f60121] | 177 | def __init__(self, parent, id=-1, value=wx.EmptyString, |
---|
| 178 | pos=wx.DefaultPosition, |
---|
| 179 | size=wx.DefaultSize, style=0, |
---|
| 180 | validator=wx.DefaultValidator, name=wx.TextCtrlNameStr, |
---|
| 181 | kill_focus_callback=None, set_focus_callback=None, |
---|
| 182 | mouse_up_callback=None, text_enter_callback=None): |
---|
[0277d084] | 183 | |
---|
[2f60121] | 184 | wx.TextCtrl.__init__(self, parent, id, value, pos, size, style, |
---|
| 185 | validator, name) |
---|
[0277d084] | 186 | |
---|
| 187 | # Bind appropriate events |
---|
| 188 | self._on_set_focus_callback = parent.onSetFocus \ |
---|
| 189 | if set_focus_callback is None else set_focus_callback |
---|
| 190 | self.Bind(wx.EVT_SET_FOCUS, self._on_set_focus) |
---|
| 191 | self.Bind(wx.EVT_KILL_FOCUS, parent._onparamEnter \ |
---|
| 192 | if kill_focus_callback is None else kill_focus_callback) |
---|
| 193 | self.Bind(wx.EVT_TEXT_ENTER, parent._onparamEnter \ |
---|
| 194 | if text_enter_callback is None else text_enter_callback) |
---|
| 195 | if not ON_MAC : |
---|
| 196 | self.Bind(wx.EVT_LEFT_UP, self._highlight_text \ |
---|
| 197 | if mouse_up_callback is None else mouse_up_callback) |
---|
| 198 | |
---|
| 199 | def _on_set_focus(self, event): |
---|
| 200 | """ |
---|
[74755ff] | 201 | Catch when the text control is set in focus to highlight the whole |
---|
| 202 | text if necessary |
---|
| 203 | |
---|
| 204 | :param event: mouse event |
---|
[0277d084] | 205 | """ |
---|
| 206 | event.Skip() |
---|
| 207 | self.full_selection = True |
---|
| 208 | return self._on_set_focus_callback(event) |
---|
[2f60121] | 209 | |
---|
[0277d084] | 210 | def _highlight_text(self, event): |
---|
| 211 | """ |
---|
[74755ff] | 212 | Highlight text of a TextCtrl only of no text has be selected |
---|
| 213 | |
---|
| 214 | :param event: mouse event |
---|
| 215 | |
---|
[0277d084] | 216 | """ |
---|
| 217 | # Make sure the mouse event is available to other listeners |
---|
| 218 | event.Skip() |
---|
| 219 | control = event.GetEventObject() |
---|
| 220 | if self.full_selection: |
---|
| 221 | self.full_selection = False |
---|
| 222 | # Check that we have a TextCtrl |
---|
| 223 | if issubclass(control.__class__, wx.TextCtrl): |
---|
| 224 | # Check whether text has been selected, |
---|
| 225 | # if not, select the whole string |
---|
| 226 | (start, end) = control.GetSelection() |
---|
| 227 | if start==end: |
---|
[2f60121] | 228 | control.SetSelection(-1, -1) |
---|
[0277d084] | 229 | |
---|
| 230 | def onContextMenu(self, event): |
---|
| 231 | """ |
---|
[74755ff] | 232 | Retrieve the state selected state |
---|
| 233 | |
---|
[0277d084] | 234 | """ |
---|
| 235 | pos = event.GetPosition() |
---|
| 236 | pos = self.ScreenToClient(pos) |
---|
| 237 | self.PopupMenu(self.popUpMenu, pos) |
---|
| 238 | |
---|
| 239 | def onUndo(self, event): |
---|
| 240 | """ |
---|
[74755ff] | 241 | Cancel the previous action |
---|
| 242 | |
---|
[0277d084] | 243 | """ |
---|
[2f60121] | 244 | event = PreviousStateEvent(page=self) |
---|
[0277d084] | 245 | wx.PostEvent(self.parent, event) |
---|
[74755ff] | 246 | |
---|
[0277d084] | 247 | def onRedo(self, event): |
---|
| 248 | """ |
---|
[74755ff] | 249 | Restore the previous action canceled |
---|
| 250 | |
---|
[0277d084] | 251 | """ |
---|
[2f60121] | 252 | event = NextStateEvent(page=self) |
---|
[0277d084] | 253 | wx.PostEvent(self.parent, event) |
---|
[74755ff] | 254 | |
---|
[0277d084] | 255 | def define_page_structure(self): |
---|
| 256 | """ |
---|
[74755ff] | 257 | Create empty sizer for a panel |
---|
| 258 | |
---|
[0277d084] | 259 | """ |
---|
| 260 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
| 261 | self.sizer0 = wx.BoxSizer(wx.VERTICAL) |
---|
| 262 | self.sizer1 = wx.BoxSizer(wx.VERTICAL) |
---|
| 263 | self.sizer2 = wx.BoxSizer(wx.VERTICAL) |
---|
| 264 | self.sizer3 = wx.BoxSizer(wx.VERTICAL) |
---|
| 265 | self.sizer4 = wx.BoxSizer(wx.VERTICAL) |
---|
| 266 | self.sizer5 = wx.BoxSizer(wx.VERTICAL) |
---|
| 267 | self.sizer6 = wx.BoxSizer(wx.VERTICAL) |
---|
| 268 | self.sizer0.SetMinSize((PANEL_WIDTH,-1)) |
---|
| 269 | self.sizer1.SetMinSize((PANEL_WIDTH,-1)) |
---|
| 270 | self.sizer2.SetMinSize((PANEL_WIDTH,-1)) |
---|
| 271 | self.sizer3.SetMinSize((PANEL_WIDTH,-1)) |
---|
| 272 | self.sizer4.SetMinSize((PANEL_WIDTH,-1)) |
---|
| 273 | self.sizer5.SetMinSize((PANEL_WIDTH,-1)) |
---|
| 274 | #self.sizer6.SetMinSize((375,-1)) |
---|
| 275 | self.vbox.Add(self.sizer0) |
---|
| 276 | self.vbox.Add(self.sizer1) |
---|
| 277 | self.vbox.Add(self.sizer2) |
---|
| 278 | self.vbox.Add(self.sizer3) |
---|
| 279 | self.vbox.Add(self.sizer4) |
---|
| 280 | self.vbox.Add(self.sizer5) |
---|
| 281 | #self.vbox.Add(self.sizer6) |
---|
| 282 | |
---|
| 283 | def set_layout(self): |
---|
| 284 | """ |
---|
[74755ff] | 285 | layout |
---|
| 286 | |
---|
[0277d084] | 287 | """ |
---|
| 288 | self.vbox.Layout() |
---|
| 289 | self.vbox.Fit(self) |
---|
| 290 | self.SetSizer(self.vbox) |
---|
[fc292d3] | 291 | |
---|
[0277d084] | 292 | self.Centre() |
---|
[fc292d3] | 293 | |
---|
[74755ff] | 294 | def set_owner(self, owner): |
---|
[0277d084] | 295 | """ |
---|
[74755ff] | 296 | set owner of fitpage |
---|
| 297 | @param owner: the class responsible of plotting |
---|
[0277d084] | 298 | """ |
---|
| 299 | self.event_owner = owner |
---|
| 300 | self.state.event_owner = owner |
---|
| 301 | |
---|
| 302 | def set_manager(self, manager): |
---|
| 303 | """ |
---|
[74755ff] | 304 | set panel manager |
---|
| 305 | |
---|
| 306 | :param manager: instance of plugin fitting |
---|
[0277d084] | 307 | """ |
---|
| 308 | self.manager = manager |
---|
| 309 | self.state.manager = manager |
---|
| 310 | |
---|
| 311 | def populate_box(self, dict): |
---|
| 312 | """ |
---|
[74755ff] | 313 | Store list of model |
---|
| 314 | |
---|
| 315 | :param dict: dictionary containing list of models |
---|
| 316 | |
---|
[0277d084] | 317 | """ |
---|
| 318 | self.model_list_box = dict |
---|
| 319 | self.state.model_list_box = self.model_list_box |
---|
| 320 | |
---|
| 321 | def set_dispers_sizer(self): |
---|
| 322 | """ |
---|
[74755ff] | 323 | fill sizer containing dispersity info |
---|
| 324 | |
---|
[0277d084] | 325 | """ |
---|
| 326 | self.sizer4.Clear(True) |
---|
[74755ff] | 327 | name = "Polydispersity and Orientational Distribution" |
---|
[2f60121] | 328 | box_description= wx.StaticBox(self, -1, name) |
---|
[0277d084] | 329 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
| 330 | #---------------------------------------------------- |
---|
[2f60121] | 331 | self.disable_disp = wx.RadioButton(self, -1, 'Off', (10, 10), |
---|
| 332 | style=wx.RB_GROUP) |
---|
[0277d084] | 333 | self.enable_disp = wx.RadioButton(self, -1, 'On', (10, 30)) |
---|
| 334 | |
---|
[2f60121] | 335 | self.Bind(wx.EVT_RADIOBUTTON, self._set_dipers_Param, |
---|
| 336 | id=self.disable_disp.GetId()) |
---|
| 337 | self.Bind(wx.EVT_RADIOBUTTON, self._set_dipers_Param, |
---|
| 338 | id=self.enable_disp.GetId()) |
---|
[0277d084] | 339 | #MAC needs SetValue |
---|
| 340 | self.disable_disp.SetValue(True) |
---|
| 341 | sizer_dispersion = wx.BoxSizer(wx.HORIZONTAL) |
---|
[2f60121] | 342 | sizer_dispersion.Add((20, 20)) |
---|
[0277d084] | 343 | name=""#Polydispersity and \nOrientational Distribution " |
---|
[2f60121] | 344 | sizer_dispersion.Add(wx.StaticText(self, -1, name)) |
---|
[0277d084] | 345 | sizer_dispersion.Add(self.enable_disp ) |
---|
[2f60121] | 346 | sizer_dispersion.Add((20, 20)) |
---|
| 347 | sizer_dispersion.Add(self.disable_disp) |
---|
| 348 | sizer_dispersion.Add((10, 10)) |
---|
[0277d084] | 349 | |
---|
| 350 | ## fill a sizer with the combobox to select dispersion type |
---|
| 351 | sizer_select_dispers = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 352 | self.model_disp = wx.StaticText(self, -1, 'Distribution Function ') |
---|
| 353 | |
---|
| 354 | import sans.models.dispersion_models |
---|
| 355 | self.polydisp= sans.models.dispersion_models.models |
---|
| 356 | self.disp_box = wx.ComboBox(self, -1) |
---|
| 357 | |
---|
| 358 | for key, value in self.polydisp.iteritems(): |
---|
| 359 | name = str(key) |
---|
| 360 | self.disp_box.Append(name,value) |
---|
| 361 | self.disp_box.SetStringSelection("gaussian") |
---|
[2f60121] | 362 | wx.EVT_COMBOBOX(self.disp_box, -1, self._on_select_Disp) |
---|
[0277d084] | 363 | |
---|
[2f60121] | 364 | sizer_select_dispers.Add((10, 10)) |
---|
[0277d084] | 365 | sizer_select_dispers.Add(self.model_disp) |
---|
[2f60121] | 366 | sizer_select_dispers.Add(self.disp_box, 0, |
---|
| 367 | wx.TOP|wx.BOTTOM|wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, border=5) |
---|
[0277d084] | 368 | self.model_disp.Hide() |
---|
| 369 | self.disp_box.Hide() |
---|
[2f60121] | 370 | boxsizer1.Add(sizer_dispersion, 0, |
---|
| 371 | wx.TOP|wx.BOTTOM|wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, border=5) |
---|
| 372 | #boxsizer1.Add((10, 10)) |
---|
| 373 | boxsizer1.Add(sizer_select_dispers) |
---|
| 374 | self.sizer4_4 = wx.GridBagSizer(5, 5) |
---|
| 375 | boxsizer1.Add(self.sizer4_4) |
---|
[0277d084] | 376 | #----------------------------------------------------- |
---|
[2f60121] | 377 | self.sizer4.Add(boxsizer1, 0, wx.EXPAND|wx.ALL, 10) |
---|
[0277d084] | 378 | self.sizer4_4.Layout() |
---|
| 379 | self.sizer4.Layout() |
---|
| 380 | self.Layout() |
---|
[fc292d3] | 381 | self.SetupScrolling() |
---|
[0277d084] | 382 | self.Refresh() |
---|
| 383 | ## saving the state of enable dispersity button |
---|
| 384 | self.state.enable_disp= self.enable_disp.GetValue() |
---|
| 385 | self.state.disable_disp= self.disable_disp.GetValue() |
---|
| 386 | |
---|
| 387 | def select_disp_angle(self, event): |
---|
| 388 | """ |
---|
[74755ff] | 389 | Event for when a user select a parameter to average over. |
---|
| 390 | |
---|
| 391 | :param event: radiobutton event |
---|
[0277d084] | 392 | """ |
---|
[2f60121] | 393 | self.values = [] |
---|
| 394 | self.weights = [] |
---|
| 395 | if event.GetEventObject() == self.noDisper_rbox: |
---|
[0277d084] | 396 | if self.noDisper_rbox.GetValue(): |
---|
| 397 | #No array dispersity apply yet |
---|
| 398 | self._reset_dispersity() |
---|
| 399 | ## Redraw the model ??? |
---|
| 400 | self._draw_model() |
---|
[2f60121] | 401 | # Go through the list of dispersion check boxes to |
---|
| 402 | #identify which one has changed |
---|
[0277d084] | 403 | for p in self.disp_cb_dict: |
---|
[2f60121] | 404 | self.state.disp_cb_dict[p] = self.disp_cb_dict[p].GetValue() |
---|
[0277d084] | 405 | # Catch which one of the box was just checked or unchecked. |
---|
| 406 | if event.GetEventObject() == self.disp_cb_dict[p]: |
---|
| 407 | if self.disp_cb_dict[p].GetValue() == True: |
---|
[2f60121] | 408 | ##Temp. FIX for V1.0 regarding changing |
---|
| 409 | #checkbox to radiobutton. |
---|
| 410 | ##This (self._reset_dispersity) should be removed |
---|
| 411 | #when the array dispersion is fixed. |
---|
[0277d084] | 412 | self._reset_dispersity() |
---|
| 413 | # The user wants this parameter to be averaged. |
---|
| 414 | # Pop up the file selection dialog. |
---|
| 415 | path = self._selectDlg() |
---|
| 416 | # If nothing was selected, just return |
---|
| 417 | if path is None: |
---|
| 418 | self.disp_cb_dict[p].SetValue(False) |
---|
[4cf7443] | 419 | self.noDisper_rbox.SetValue(True) |
---|
[0277d084] | 420 | return |
---|
| 421 | try: |
---|
| 422 | self._default_save_location = os.path.dirname(path) |
---|
| 423 | except: |
---|
| 424 | pass |
---|
| 425 | try: |
---|
[2f60121] | 426 | self.values, self.weights = self.read_file(path) |
---|
[0277d084] | 427 | except: |
---|
[2f60121] | 428 | msg = "Could not read input file" |
---|
| 429 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
[0277d084] | 430 | return |
---|
[2f60121] | 431 | # If any of the two arrays is empty, notify the user |
---|
| 432 | # that we won't proceed |
---|
[0277d084] | 433 | if self.values is None or self.weights is None or \ |
---|
[2f60121] | 434 | self.values == [] or self.weights == []: |
---|
| 435 | msg = "The loaded %s distrubtion is corrupted" |
---|
| 436 | msg += " or empty" % p |
---|
| 437 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
[0277d084] | 438 | return |
---|
| 439 | # Tell the user that we are about to apply the distribution |
---|
[2f60121] | 440 | msg = "Applying loaded %s distribution: %s" % (p, path) |
---|
| 441 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
[0277d084] | 442 | |
---|
| 443 | # Create the dispersion objects |
---|
| 444 | from sans.models.dispersion_models import ArrayDispersion |
---|
| 445 | disp_model = ArrayDispersion() |
---|
| 446 | disp_model.set_weights(self.values, self.weights) |
---|
| 447 | |
---|
[2f60121] | 448 | # Store the object to make it persist outside the scope |
---|
| 449 | #of this method |
---|
[0277d084] | 450 | #TODO: refactor model to clean this up? |
---|
| 451 | self._disp_obj_dict[p] = disp_model |
---|
[2f60121] | 452 | self.state._disp_obj_dict [p] = disp_model |
---|
| 453 | self.state.values = [] |
---|
| 454 | self.state.weights = [] |
---|
[0277d084] | 455 | self.state.values = copy.deepcopy(self.values) |
---|
| 456 | self.state.weights = copy.deepcopy(self.weights) |
---|
[2f60121] | 457 | # Set the new model as the dispersion object for the |
---|
| 458 | #selected parameter |
---|
[0277d084] | 459 | self.model.set_dispersion(p, disp_model) |
---|
[2f60121] | 460 | # Store a reference to the weights in the model object |
---|
| 461 | #so that |
---|
[0277d084] | 462 | # it's not lost when we use the model within another thread. |
---|
| 463 | #TODO: total hack - fix this |
---|
| 464 | self.state.model= self.model.clone() |
---|
| 465 | |
---|
| 466 | self.model._persistency_dict = {} |
---|
[2f60121] | 467 | self.model._persistency_dict[p] = [self.values, |
---|
| 468 | self.weights] |
---|
| 469 | self.state.model._persistency_dict[p] = [self.values, |
---|
| 470 | self.weights] |
---|
[0277d084] | 471 | else: |
---|
| 472 | self._reset_dispersity() |
---|
| 473 | |
---|
| 474 | ## Redraw the model |
---|
| 475 | self._draw_model() |
---|
| 476 | |
---|
| 477 | ## post state to fit panel |
---|
[2f60121] | 478 | event = PageInfoEvent(page=self) |
---|
[0277d084] | 479 | wx.PostEvent(self.parent, event) |
---|
| 480 | |
---|
| 481 | def onResetModel(self, event): |
---|
| 482 | """ |
---|
[74755ff] | 483 | Reset model state |
---|
| 484 | |
---|
[0277d084] | 485 | """ |
---|
| 486 | ## post help message for the selected model |
---|
| 487 | msg = self.popUpMenu.GetHelpString(event.GetId()) |
---|
[2f60121] | 488 | msg += " reloaded" |
---|
| 489 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
[0277d084] | 490 | |
---|
[2f60121] | 491 | name = self.popUpMenu.GetLabel(event.GetId()) |
---|
[0277d084] | 492 | self._on_select_model_helper() |
---|
| 493 | |
---|
| 494 | if name in self.saved_states.keys(): |
---|
| 495 | previous_state = self.saved_states[name] |
---|
| 496 | ## reset state of checkbox,textcrtl and regular parameters value |
---|
| 497 | self.reset_page(previous_state) |
---|
[4523b68] | 498 | |
---|
[0277d084] | 499 | def onSave(self, event): |
---|
| 500 | """ |
---|
[74755ff] | 501 | save history of the data and model |
---|
| 502 | |
---|
[0277d084] | 503 | """ |
---|
[2f60121] | 504 | if self.model == None: |
---|
[0277d084] | 505 | return |
---|
[4523b68] | 506 | self.save_current_state() |
---|
| 507 | """ |
---|
[0277d084] | 508 | if hasattr(self,"enable_disp"): |
---|
| 509 | self.state.enable_disp = copy.deepcopy(self.enable_disp.GetValue()) |
---|
| 510 | if hasattr(self, "disp_box"): |
---|
| 511 | self.state.disp_box = copy.deepcopy(self.disp_box.GetSelection()) |
---|
| 512 | |
---|
| 513 | self.state.model.name= self.model.name |
---|
[4523b68] | 514 | """ |
---|
[0277d084] | 515 | new_state = self.state.clone() |
---|
| 516 | new_state.model.name = self.state.model.name |
---|
| 517 | |
---|
| 518 | new_state.enable2D = copy.deepcopy(self.enable2D) |
---|
| 519 | ##Add model state on context menu |
---|
| 520 | self.number_saved_state += 1 |
---|
| 521 | #name= self.model.name+"[%g]"%self.number_saved_state |
---|
[2f60121] | 522 | name = self.model.__class__.__name__ + "[%g]" % self.number_saved_state |
---|
| 523 | self.saved_states[name] = new_state |
---|
[0277d084] | 524 | ## Add item in the context menu |
---|
[2f60121] | 525 | year, month, day, hour, minute, second, _, _, _ = time.localtime() |
---|
| 526 | my_time = str(hour) + " : " + str(minute) + " : " + str(second) + " " |
---|
| 527 | date = str( month) + "|" + str(day) + "|" + str(year) |
---|
| 528 | msg = "Model saved at %s on %s" % (my_time, date) |
---|
[0277d084] | 529 | ## post help message for the selected model |
---|
[2f60121] | 530 | msg += " Saved! right click on this page to retrieve this model" |
---|
| 531 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
[0277d084] | 532 | |
---|
| 533 | id = wx.NewId() |
---|
[2f60121] | 534 | self.popUpMenu.Append(id, name, str(msg)) |
---|
[0277d084] | 535 | wx.EVT_MENU(self, id, self.onResetModel) |
---|
| 536 | |
---|
| 537 | def onSetFocus(self, evt): |
---|
| 538 | """ |
---|
[74755ff] | 539 | highlight the current textcrtl and hide the error text control shown |
---|
| 540 | after fitting |
---|
| 541 | :Not implemented. |
---|
[0277d084] | 542 | """ |
---|
| 543 | return |
---|
| 544 | |
---|
| 545 | def read_file(self, path): |
---|
| 546 | """ |
---|
[74755ff] | 547 | Read two columns file |
---|
| 548 | |
---|
| 549 | :param path: the path to the file to read |
---|
[0277d084] | 550 | """ |
---|
| 551 | try: |
---|
[2f60121] | 552 | if path == None: |
---|
| 553 | msg = " Selected Distribution was not loaded: %s" % path |
---|
| 554 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
[0277d084] | 555 | return None, None |
---|
| 556 | input_f = open(path, 'r') |
---|
| 557 | buff = input_f.read() |
---|
| 558 | lines = buff.split('\n') |
---|
| 559 | angles = [] |
---|
[2f60121] | 560 | weights = [] |
---|
[0277d084] | 561 | for line in lines: |
---|
| 562 | toks = line.split() |
---|
| 563 | try: |
---|
| 564 | angle = float(toks[0]) |
---|
| 565 | weight = float(toks[1]) |
---|
| 566 | except: |
---|
| 567 | # Skip non-data lines |
---|
| 568 | pass |
---|
| 569 | angles.append(angle) |
---|
| 570 | weights.append(weight) |
---|
| 571 | return numpy.array(angles), numpy.array(weights) |
---|
| 572 | except: |
---|
| 573 | raise |
---|
| 574 | |
---|
| 575 | def createMemento(self): |
---|
| 576 | """ |
---|
[74755ff] | 577 | return the current state of the page |
---|
[0277d084] | 578 | """ |
---|
| 579 | return self.state.clone() |
---|
| 580 | |
---|
| 581 | def save_current_state(self): |
---|
| 582 | """ |
---|
[74755ff] | 583 | Store current state |
---|
| 584 | |
---|
[0277d084] | 585 | """ |
---|
| 586 | ## save model option |
---|
[2f60121] | 587 | if self.model != None: |
---|
| 588 | self.disp_list = self.model.getDispParamList() |
---|
| 589 | self.state.disp_list = copy.deepcopy(self.disp_list) |
---|
[0277d084] | 590 | self.state.model = self.model.clone() |
---|
| 591 | self.state.enable2D = copy.deepcopy(self.enable2D) |
---|
[2f60121] | 592 | self.state.values = copy.deepcopy(self.values) |
---|
[0277d084] | 593 | self.state.weights = copy.deepcopy( self.weights) |
---|
| 594 | ## save data |
---|
[2f60121] | 595 | self.state.data = copy.deepcopy(self.data) |
---|
[0277d084] | 596 | try: |
---|
| 597 | n = self.disp_box.GetCurrentSelection() |
---|
| 598 | dispersity= self.disp_box.GetClientData(n) |
---|
[2f60121] | 599 | name = dispersity.__name__ |
---|
[0277d084] | 600 | self.disp_name = name |
---|
[2f60121] | 601 | if name == "GaussianDispersion": |
---|
[0277d084] | 602 | if hasattr(self,"cb1"): |
---|
[2f60121] | 603 | self.state.cb1 = self.cb1.GetValue() |
---|
[0277d084] | 604 | except: |
---|
| 605 | pass |
---|
[2f60121] | 606 | if hasattr(self, "enable_disp"): |
---|
| 607 | self.state.enable_disp = self.enable_disp.GetValue() |
---|
[0277d084] | 608 | self.state.disable_disp = self.disable_disp.GetValue() |
---|
| 609 | self.state.smearer = copy.deepcopy(self.smearer) |
---|
[2f60121] | 610 | if hasattr(self, "enable_smearer"): |
---|
[0277d084] | 611 | self.state.enable_smearer = copy.deepcopy(self.enable_smearer.GetValue()) |
---|
| 612 | self.state.disable_smearer = copy.deepcopy(self.disable_smearer.GetValue()) |
---|
[2f60121] | 613 | if hasattr(self, "disp_box"): |
---|
[0277d084] | 614 | self.state.disp_box = self.disp_box.GetCurrentSelection() |
---|
[2f60121] | 615 | if len(self.disp_cb_dict) > 0: |
---|
| 616 | for k, v in self.disp_cb_dict.iteritems(): |
---|
| 617 | if v == None : |
---|
| 618 | self.state.disp_cb_dict[k] = v |
---|
[0277d084] | 619 | else: |
---|
| 620 | try: |
---|
[2f60121] | 621 | self.state.disp_cb_dict[k] = v.GetValue() |
---|
[0277d084] | 622 | except: |
---|
[2f60121] | 623 | self.state.disp_cb_dict[k] = None |
---|
| 624 | if len(self._disp_obj_dict) > 0: |
---|
| 625 | for k, v in self._disp_obj_dict.iteritems(): |
---|
| 626 | self.state._disp_obj_dict[k] = v |
---|
[0277d084] | 627 | self.state.values = copy.deepcopy(self.values) |
---|
| 628 | self.state.weights = copy.deepcopy(self.weights) |
---|
| 629 | ## save plotting range |
---|
| 630 | self._save_plotting_range() |
---|
[2f60121] | 631 | self.state.orientation_params = [] |
---|
| 632 | self.state.orientation_params_disp = [] |
---|
| 633 | self.state.parameters = [] |
---|
| 634 | self.state.fittable_param = [] |
---|
| 635 | self.state.fixed_param = [] |
---|
[0277d084] | 636 | ## save checkbutton state and txtcrtl values |
---|
| 637 | self._copy_parameters_state(self.orientation_params, |
---|
| 638 | self.state.orientation_params) |
---|
| 639 | self._copy_parameters_state(self.orientation_params_disp, |
---|
| 640 | self.state.orientation_params_disp) |
---|
| 641 | self._copy_parameters_state(self.parameters, self.state.parameters) |
---|
[2f60121] | 642 | self._copy_parameters_state(self.fittable_param, |
---|
| 643 | self.state.fittable_param) |
---|
[0277d084] | 644 | self._copy_parameters_state(self.fixed_param, self.state.fixed_param) |
---|
[74755ff] | 645 | |
---|
| 646 | def reset_page_helper(self, state): |
---|
[0277d084] | 647 | """ |
---|
[74755ff] | 648 | Use page_state and change the state of existing page |
---|
[0277d084] | 649 | |
---|
[74755ff] | 650 | :precondition: the page is already drawn or created |
---|
[0277d084] | 651 | |
---|
[74755ff] | 652 | :postcondition: the state of the underlying data change as well as the |
---|
[0277d084] | 653 | state of the graphic interface |
---|
| 654 | """ |
---|
[2f60121] | 655 | if state == None: |
---|
[0277d084] | 656 | #self._undo.Enable(False) |
---|
| 657 | return |
---|
[2f60121] | 658 | self.model = state.model |
---|
[0277d084] | 659 | self.data = state.data |
---|
[2f60121] | 660 | if self.data != None: |
---|
[0277d084] | 661 | from DataLoader.qsmearing import smear_selection |
---|
[2f60121] | 662 | self.smearer= smear_selection(self.data) |
---|
| 663 | self.enable2D = state.enable2D |
---|
[0277d084] | 664 | self.engine_type = state.engine_type |
---|
| 665 | self.disp_cb_dict = state.disp_cb_dict |
---|
| 666 | self.disp_list = state.disp_list |
---|
| 667 | ## set the state of the radio box |
---|
[2f60121] | 668 | self.shape_rbutton.SetValue(state.shape_rbutton) |
---|
[0277d084] | 669 | self.shape_indep_rbutton.SetValue(state.shape_indep_rbutton) |
---|
[2f60121] | 670 | self.struct_rbutton.SetValue(state.struct_rbutton) |
---|
[0277d084] | 671 | self.plugin_rbutton.SetValue(state.plugin_rbutton) |
---|
| 672 | ##draw sizer containing model parameters value for the current model |
---|
[2f60121] | 673 | self._set_model_sizer_selection(self.model) |
---|
[0277d084] | 674 | self.set_model_param_sizer(self.model) |
---|
| 675 | ## reset value of combox box |
---|
[2f60121] | 676 | self.structurebox.SetSelection(state.structurecombobox) |
---|
[0277d084] | 677 | self.formfactorbox.SetSelection(state.formfactorcombobox) |
---|
[4523b68] | 678 | if state.multi_factor != None: |
---|
[a706353b] | 679 | self.multifactorbox.SetSelection(state.multi_factor) |
---|
[4523b68] | 680 | #draw the pnael according to the new model parameter |
---|
| 681 | self._on_select_model(event=None) |
---|
[0277d084] | 682 | ## enable the view 2d button if this is a modelpage type |
---|
[2f60121] | 683 | if hasattr(self, "model_view"): |
---|
[0277d084] | 684 | if self.enable2D: |
---|
| 685 | self.model_view.Disable() |
---|
| 686 | else: |
---|
| 687 | self.model_view.Enable() |
---|
| 688 | ## set the select all check box to the a given state |
---|
| 689 | if hasattr(self, "cb1"): |
---|
| 690 | self.cb1.SetValue(state.cb1) |
---|
| 691 | ## reset state of checkbox,textcrtl and regular parameters value |
---|
| 692 | self._reset_parameters_state(self.orientation_params_disp, |
---|
| 693 | state.orientation_params_disp) |
---|
| 694 | self._reset_parameters_state(self.orientation_params, |
---|
| 695 | state.orientation_params) |
---|
[fb59ed9] | 696 | self._reset_parameters_state(self.str_parameters, |
---|
| 697 | state.str_parameters) |
---|
[2f60121] | 698 | self._reset_parameters_state(self.parameters, state.parameters) |
---|
[0277d084] | 699 | ## display dispersion info layer |
---|
| 700 | self.enable_disp.SetValue(state.enable_disp) |
---|
| 701 | self.disable_disp.SetValue(state.disable_disp) |
---|
| 702 | if hasattr(self, "disp_box"): |
---|
| 703 | self.disp_box.SetSelection(state.disp_box) |
---|
[2f60121] | 704 | n = self.disp_box.GetCurrentSelection() |
---|
| 705 | dispersity = self.disp_box.GetClientData(n) |
---|
| 706 | name = dispersity.__name__ |
---|
[0277d084] | 707 | self._set_dipers_Param(event=None) |
---|
[2f60121] | 708 | if name == "ArrayDispersion": |
---|
[0277d084] | 709 | for item in self.disp_cb_dict.keys(): |
---|
[2f60121] | 710 | if hasattr(self.disp_cb_dict[item], "SetValue") : |
---|
[0277d084] | 711 | self.disp_cb_dict[item].SetValue(state.disp_cb_dict[item]) |
---|
| 712 | # Create the dispersion objects |
---|
| 713 | from sans.models.dispersion_models import ArrayDispersion |
---|
| 714 | disp_model = ArrayDispersion() |
---|
[2f60121] | 715 | if hasattr(state, "values") and \ |
---|
| 716 | self.disp_cb_dict[item].GetValue() == True: |
---|
| 717 | if len(state.values) > 0: |
---|
| 718 | self.values = state.values |
---|
| 719 | self.weights = state.weights |
---|
| 720 | disp_model.set_weights(self.values, |
---|
| 721 | state.weights) |
---|
[0277d084] | 722 | else: |
---|
| 723 | self._reset_dispersity() |
---|
| 724 | self._disp_obj_dict[item] = disp_model |
---|
[2f60121] | 725 | # Set the new model as the dispersion object for |
---|
| 726 | #the selected parameter |
---|
[0277d084] | 727 | self.model.set_dispersion(item, disp_model) |
---|
[2f60121] | 728 | self.model._persistency_dict[item] = [state.values, |
---|
| 729 | state.weights] |
---|
[0277d084] | 730 | else: |
---|
| 731 | keys = self.model.getParamList() |
---|
| 732 | for item in keys: |
---|
[2f60121] | 733 | if item in self.disp_list and \ |
---|
| 734 | not self.model.details.has_key(item): |
---|
| 735 | self.model.details[item]=["", None, None] |
---|
| 736 | for k, v in self.state.disp_cb_dict.iteritems(): |
---|
[0277d084] | 737 | self.disp_cb_dict = copy.deepcopy(state.disp_cb_dict) |
---|
| 738 | self.state.disp_cb_dict = copy.deepcopy(state.disp_cb_dict) |
---|
| 739 | ##plotting range restore |
---|
| 740 | self._reset_plotting_range(state) |
---|
| 741 | ## smearing info restore |
---|
[2f60121] | 742 | if hasattr(self, "enable_smearer"): |
---|
| 743 | ## set smearing value whether or not the data contain |
---|
| 744 | #the smearing info |
---|
[0277d084] | 745 | self.enable_smearer.SetValue(state.enable_smearer) |
---|
| 746 | self.disable_smearer.SetValue(state.disable_smearer) |
---|
| 747 | self.onSmear(event=None) |
---|
| 748 | ## reset state of checkbox,textcrtl and dispersity parameters value |
---|
[2f60121] | 749 | self._reset_parameters_state(self.fittable_param, state.fittable_param) |
---|
| 750 | self._reset_parameters_state(self.fixed_param, state.fixed_param) |
---|
[0277d084] | 751 | ## draw the model with previous parameters value |
---|
| 752 | self._onparamEnter_helper() |
---|
| 753 | ## reset context menu items |
---|
| 754 | self._reset_context_menu() |
---|
| 755 | ## set the value of the current state to the state given as parameter |
---|
| 756 | self.state = state.clone() |
---|
| 757 | self._draw_model() |
---|
| 758 | |
---|
| 759 | def _selectDlg(self): |
---|
| 760 | """ |
---|
[74755ff] | 761 | open a dialog file to selected the customized dispersity |
---|
| 762 | |
---|
[0277d084] | 763 | """ |
---|
[2f60121] | 764 | #import os |
---|
[0277d084] | 765 | dlg = wx.FileDialog(self, "Choose a weight file", |
---|
[2f60121] | 766 | self._default_save_location, "", "*.*", wx.OPEN) |
---|
[0277d084] | 767 | path = None |
---|
| 768 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 769 | path = dlg.GetPath() |
---|
| 770 | dlg.Destroy() |
---|
| 771 | return path |
---|
| 772 | |
---|
| 773 | def _reset_context_menu(self): |
---|
| 774 | """ |
---|
[74755ff] | 775 | reset the context menu |
---|
| 776 | |
---|
[0277d084] | 777 | """ |
---|
| 778 | for name, state in self.state.saved_states.iteritems(): |
---|
| 779 | self.number_saved_state += 1 |
---|
| 780 | ## Add item in the context menu |
---|
| 781 | id = wx.NewId() |
---|
[2f60121] | 782 | hint_msg = 'Save model and state %g' % self.number_saved_state |
---|
| 783 | self.popUpMenu.Append(id, name, hint_msg) |
---|
[0277d084] | 784 | wx.EVT_MENU(self, id, self.onResetModel) |
---|
[2f60121] | 785 | |
---|
[0277d084] | 786 | def _reset_plotting_range(self, state): |
---|
| 787 | """ |
---|
[74755ff] | 788 | Reset the plotting range to a given state |
---|
| 789 | |
---|
[0277d084] | 790 | """ |
---|
| 791 | self.qmin.SetValue(str(state.qmin)) |
---|
| 792 | self.qmax.SetValue(str(state.qmax)) |
---|
[2f60121] | 793 | if self.state.npts != None: |
---|
[0277d084] | 794 | self.npts.SetValue(str(state.npts)) |
---|
| 795 | |
---|
| 796 | def _save_typeOfmodel(self): |
---|
| 797 | """ |
---|
[74755ff] | 798 | save radiobutton containing the type model that can be selected |
---|
| 799 | |
---|
[0277d084] | 800 | """ |
---|
| 801 | self.state.shape_rbutton = self.shape_rbutton.GetValue() |
---|
| 802 | self.state.shape_indep_rbutton = self.shape_indep_rbutton.GetValue() |
---|
| 803 | self.state.struct_rbutton = self.struct_rbutton.GetValue() |
---|
| 804 | self.state.plugin_rbutton = self.plugin_rbutton.GetValue() |
---|
[2f60121] | 805 | self.state.structurebox = self.structurebox.GetCurrentSelection() |
---|
[0277d084] | 806 | self.state.formfactorbox = self.formfactorbox.GetCurrentSelection() |
---|
| 807 | #self._undo.Enable(True) |
---|
| 808 | ## post state to fit panel |
---|
[2f60121] | 809 | event = PageInfoEvent(page=self) |
---|
[0277d084] | 810 | wx.PostEvent(self.parent, event) |
---|
| 811 | |
---|
[2f60121] | 812 | def _save_plotting_range(self): |
---|
[0277d084] | 813 | """ |
---|
[74755ff] | 814 | save the state of plotting range |
---|
| 815 | |
---|
[0277d084] | 816 | """ |
---|
| 817 | self.state.qmin = self.qmin_x |
---|
| 818 | self.state.qmax = self.qmax_x |
---|
[2f60121] | 819 | if self.npts != None: |
---|
| 820 | self.state.npts = self.num_points |
---|
[0277d084] | 821 | |
---|
| 822 | def _onparamEnter_helper(self): |
---|
| 823 | """ |
---|
[74755ff] | 824 | check if values entered by the user are changed and valid to replot |
---|
| 825 | model |
---|
| 826 | use : _check_value_enter |
---|
| 827 | |
---|
[0277d084] | 828 | """ |
---|
| 829 | # Flag to register when a parameter has changed. |
---|
| 830 | is_modified = False |
---|
| 831 | #self._undo.Enable(True) |
---|
[2f60121] | 832 | if self.model != None: |
---|
[0277d084] | 833 | try: |
---|
[2f60121] | 834 | is_modified = self._check_value_enter(self.fittable_param, |
---|
| 835 | is_modified) |
---|
| 836 | is_modified = self._check_value_enter(self.fixed_param, |
---|
| 837 | is_modified) |
---|
| 838 | is_modified = self._check_value_enter(self.parameters, |
---|
| 839 | is_modified) |
---|
[0277d084] | 840 | except: |
---|
| 841 | pass |
---|
| 842 | #if is_modified: |
---|
| 843 | # Here we should check whether the boundaries have been modified. |
---|
| 844 | # If qmin and qmax have been modified, update qmin and qmax and |
---|
| 845 | # set the is_modified flag to True |
---|
| 846 | if self._validate_qrange(self.qmin, self.qmax): |
---|
| 847 | tempmin = float(self.qmin.GetValue()) |
---|
| 848 | if tempmin != self.qmin_x: |
---|
| 849 | self.qmin_x = tempmin |
---|
| 850 | is_modified = True |
---|
| 851 | tempmax = float(self.qmax.GetValue()) |
---|
| 852 | if tempmax != self.qmax_x: |
---|
| 853 | self.qmax_x = tempmax |
---|
| 854 | is_modified = True |
---|
| 855 | self.fitrange = True |
---|
| 856 | else: |
---|
| 857 | self.fitrange = False |
---|
| 858 | if self.npts != None: |
---|
| 859 | if check_float(self.npts): |
---|
| 860 | temp_npts = float(self.npts.GetValue()) |
---|
| 861 | if temp_npts != self.num_points: |
---|
| 862 | self.num_points = temp_npts |
---|
| 863 | is_modified = True |
---|
| 864 | else: |
---|
[2f60121] | 865 | msg = "Cannot Plot :Must enter a number!!! " |
---|
| 866 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
[0277d084] | 867 | ## if any value is modify draw model with new value |
---|
| 868 | if is_modified: |
---|
[2f60121] | 869 | self.state_change = True |
---|
[0277d084] | 870 | self._draw_model() |
---|
| 871 | self.Refresh() |
---|
| 872 | return is_modified |
---|
[885857e] | 873 | |
---|
[0277d084] | 874 | def _is_modified(self, is_modified): |
---|
| 875 | """ |
---|
[74755ff] | 876 | return to self._is_modified |
---|
| 877 | |
---|
[0277d084] | 878 | """ |
---|
| 879 | return is_modified |
---|
| 880 | |
---|
[2f60121] | 881 | def _reset_parameters_state(self, listtorestore, statelist): |
---|
[0277d084] | 882 | """ |
---|
[74755ff] | 883 | Reset the parameters at the given state |
---|
| 884 | |
---|
[0277d084] | 885 | """ |
---|
[2f60121] | 886 | if len(statelist) == 0 or len(listtorestore) == 0: |
---|
[0277d084] | 887 | return |
---|
[2f60121] | 888 | if len(statelist) != len(listtorestore): |
---|
[0277d084] | 889 | return |
---|
| 890 | |
---|
| 891 | for j in range(len(listtorestore)): |
---|
| 892 | item_page = listtorestore[j] |
---|
| 893 | item_page_info = statelist[j] |
---|
| 894 | ##change the state of the check box for simple parameters |
---|
[2f60121] | 895 | if item_page[0] != None: |
---|
[0277d084] | 896 | item_page[0].SetValue(item_page_info[0]) |
---|
[2f60121] | 897 | if item_page[2] != None: |
---|
[0277d084] | 898 | item_page[2].SetValue(item_page_info[2]) |
---|
[2f60121] | 899 | if item_page[3] != None: |
---|
[0277d084] | 900 | ## show or hide text +/- |
---|
| 901 | if item_page_info[2]: |
---|
| 902 | item_page[3].Show(True) |
---|
| 903 | else: |
---|
| 904 | item_page[3].Hide() |
---|
[2f60121] | 905 | if item_page[4] != None: |
---|
[0277d084] | 906 | ## show of hide the text crtl for fitting error |
---|
| 907 | if item_page_info[4][0]: |
---|
| 908 | item_page[4].Show(True) |
---|
| 909 | item_page[4].SetValue(item_page_info[4][1]) |
---|
| 910 | else: |
---|
| 911 | item_page[3].Hide() |
---|
[2f60121] | 912 | if item_page[5] != None: |
---|
[0277d084] | 913 | ## show of hide the text crtl for fitting error |
---|
| 914 | if item_page_info[5][0]: |
---|
| 915 | item_page[5].Show(True) |
---|
| 916 | item_page[5].SetValue(item_page_info[5][1]) |
---|
| 917 | else: |
---|
| 918 | item_page[5].Hide() |
---|
[2f60121] | 919 | if item_page[6] != None: |
---|
[0277d084] | 920 | ## show of hide the text crtl for fitting error |
---|
| 921 | if item_page_info[6][0]: |
---|
| 922 | item_page[6].Show(True) |
---|
| 923 | item_page[6].SetValue(item_page_info[6][1]) |
---|
| 924 | else: |
---|
| 925 | item_page[6].Hide() |
---|
[74755ff] | 926 | |
---|
[0277d084] | 927 | def _copy_parameters_state(self, listtocopy, statelist): |
---|
| 928 | """ |
---|
[74755ff] | 929 | copy the state of button |
---|
| 930 | |
---|
| 931 | :param listtocopy: the list of widgets 's state to copy |
---|
| 932 | :param statelist: container for the state of widgets that must be copied |
---|
| 933 | |
---|
[0277d084] | 934 | """ |
---|
[2f60121] | 935 | if len(listtocopy) == 0: |
---|
[0277d084] | 936 | return |
---|
| 937 | for item in listtocopy: |
---|
| 938 | checkbox_state = None |
---|
[2f60121] | 939 | if item[0] != None: |
---|
| 940 | checkbox_state = item[0].GetValue() |
---|
[0277d084] | 941 | parameter_name = item[1] |
---|
| 942 | parameter_value = None |
---|
[2f60121] | 943 | if item[2] != None: |
---|
[0277d084] | 944 | parameter_value = item[2].GetValue() |
---|
| 945 | static_text = None |
---|
[2f60121] | 946 | if item[3] != None: |
---|
[0277d084] | 947 | static_text = item[3].IsShown() |
---|
| 948 | error_value = None |
---|
| 949 | error_state = None |
---|
[2f60121] | 950 | if item[4] != None: |
---|
[0277d084] | 951 | error_value = item[4].GetValue() |
---|
| 952 | error_state = item[4].IsShown() |
---|
| 953 | min_value = None |
---|
| 954 | min_state = None |
---|
[2f60121] | 955 | if item[5] != None: |
---|
[0277d084] | 956 | min_value = item[5].GetValue() |
---|
| 957 | min_state = item[5].IsShown() |
---|
| 958 | max_value = None |
---|
| 959 | max_state = None |
---|
[2f60121] | 960 | if item[6] != None: |
---|
[0277d084] | 961 | max_value = item[6].GetValue() |
---|
| 962 | max_state = item[6].IsShown() |
---|
[2f60121] | 963 | unit = None |
---|
| 964 | if item[7] != None: |
---|
[0277d084] | 965 | unit = item[7].GetLabel() |
---|
| 966 | statelist.append([checkbox_state, parameter_name, parameter_value, |
---|
[2f60121] | 967 | static_text , [error_state, error_value], |
---|
| 968 | [min_state, min_value], [max_state, max_value], unit]) |
---|
[0277d084] | 969 | |
---|
| 970 | def _set_model_sizer_selection(self, model): |
---|
| 971 | """ |
---|
[74755ff] | 972 | Display the sizer according to the type of the current model |
---|
| 973 | |
---|
[0277d084] | 974 | """ |
---|
[2f60121] | 975 | if model == None: |
---|
[0277d084] | 976 | return |
---|
[2f60121] | 977 | if hasattr(model, "s_model"): |
---|
| 978 | class_name = model.s_model.__class__ |
---|
| 979 | name = model.s_model.name |
---|
| 980 | flag = (name != "NoStructure") |
---|
[0277d084] | 981 | if flag and (class_name in self.model_list_box["Structure Factors"]): |
---|
| 982 | self.structurebox.Show() |
---|
| 983 | self.text2.Show() |
---|
| 984 | self.structurebox.Enable() |
---|
| 985 | self.text2.Enable() |
---|
| 986 | items = self.structurebox.GetItems() |
---|
| 987 | self.sizer1.Layout() |
---|
[fc292d3] | 988 | self.SetupScrolling() |
---|
[0277d084] | 989 | for i in range(len(items)): |
---|
[2f60121] | 990 | if items[i] == str(name): |
---|
[0277d084] | 991 | self.structurebox.SetSelection(i) |
---|
| 992 | break |
---|
[2f60121] | 993 | if hasattr(model, "p_model"): |
---|
[0277d084] | 994 | class_name = model.p_model.__class__ |
---|
| 995 | name = model.p_model.name |
---|
| 996 | self.formfactorbox.Clear() |
---|
| 997 | for k, list in self.model_list_box.iteritems(): |
---|
[2f60121] | 998 | if k in ["P(Q)*S(Q)", "Shapes" ] and \ |
---|
| 999 | class_name in self.model_list_box["Shapes"]: |
---|
[0277d084] | 1000 | self.shape_rbutton.SetValue(True) |
---|
| 1001 | ## fill the form factor list with new model |
---|
[2f60121] | 1002 | self._populate_box(self.formfactorbox, |
---|
| 1003 | self.model_list_box["Shapes"]) |
---|
[0277d084] | 1004 | items = self.formfactorbox.GetItems() |
---|
| 1005 | ## set comboxbox to the selected item |
---|
| 1006 | for i in range(len(items)): |
---|
[2f60121] | 1007 | if items[i] == str(name): |
---|
[0277d084] | 1008 | self.formfactorbox.SetSelection(i) |
---|
| 1009 | break |
---|
| 1010 | return |
---|
| 1011 | elif k == "Shape-Independent": |
---|
| 1012 | self.shape_indep_rbutton.SetValue(True) |
---|
| 1013 | elif k == "Structure Factors": |
---|
| 1014 | self.struct_rbutton.SetValue(True) |
---|
[fb59ed9] | 1015 | elif k == "Multi-Functions": |
---|
| 1016 | continue |
---|
[0277d084] | 1017 | else: |
---|
| 1018 | self.plugin_rbutton.SetValue(True) |
---|
| 1019 | if class_name in list: |
---|
| 1020 | ## fill the form factor list with new model |
---|
| 1021 | self._populate_box(self.formfactorbox, list) |
---|
| 1022 | items = self.formfactorbox.GetItems() |
---|
| 1023 | ## set comboxbox to the selected item |
---|
| 1024 | for i in range(len(items)): |
---|
[2f60121] | 1025 | if items[i] == str(name): |
---|
[0277d084] | 1026 | self.formfactorbox.SetSelection(i) |
---|
| 1027 | break |
---|
| 1028 | break |
---|
| 1029 | else: |
---|
| 1030 | |
---|
| 1031 | ## Select the model from the menu |
---|
| 1032 | class_name = model.__class__ |
---|
| 1033 | name = model.name |
---|
| 1034 | self.formfactorbox.Clear() |
---|
| 1035 | items = self.formfactorbox.GetItems() |
---|
| 1036 | |
---|
| 1037 | for k, list in self.model_list_box.iteritems(): |
---|
[2f60121] | 1038 | if k in["P(Q)*S(Q)","Shapes" ] and \ |
---|
| 1039 | class_name in self.model_list_box["Shapes"]: |
---|
[0277d084] | 1040 | if class_name in self.model_list_box["P(Q)*S(Q)"]: |
---|
| 1041 | self.structurebox.Show() |
---|
| 1042 | self.text2.Show() |
---|
| 1043 | self.structurebox.Enable() |
---|
| 1044 | self.structurebox.SetSelection(0) |
---|
| 1045 | self.text2.Enable() |
---|
| 1046 | else: |
---|
| 1047 | self.structurebox.Hide() |
---|
| 1048 | self.text2.Hide() |
---|
| 1049 | self.structurebox.Disable() |
---|
| 1050 | self.structurebox.SetSelection(0) |
---|
| 1051 | self.text2.Disable() |
---|
| 1052 | |
---|
| 1053 | self.shape_rbutton.SetValue(True) |
---|
| 1054 | ## fill the form factor list with new model |
---|
[2f60121] | 1055 | self._populate_box(self.formfactorbox, |
---|
| 1056 | self.model_list_box["Shapes"]) |
---|
[0277d084] | 1057 | items = self.formfactorbox.GetItems() |
---|
| 1058 | ## set comboxbox to the selected item |
---|
| 1059 | for i in range(len(items)): |
---|
[2f60121] | 1060 | if items[i] == str(name): |
---|
[0277d084] | 1061 | self.formfactorbox.SetSelection(i) |
---|
| 1062 | break |
---|
| 1063 | return |
---|
| 1064 | elif k == "Shape-Independent": |
---|
| 1065 | self.shape_indep_rbutton.SetValue(True) |
---|
| 1066 | elif k == "Structure Factors": |
---|
| 1067 | self.struct_rbutton.SetValue(True) |
---|
[fb59ed9] | 1068 | elif k == "Multi-Functions": |
---|
| 1069 | continue |
---|
[0277d084] | 1070 | else: |
---|
| 1071 | self.plugin_rbutton.SetValue(True) |
---|
| 1072 | if class_name in list: |
---|
| 1073 | self.structurebox.SetSelection(0) |
---|
| 1074 | self.structurebox.Disable() |
---|
| 1075 | self.text2.Disable() |
---|
| 1076 | ## fill the form factor list with new model |
---|
| 1077 | self._populate_box(self.formfactorbox, list) |
---|
| 1078 | items = self.formfactorbox.GetItems() |
---|
| 1079 | ## set comboxbox to the selected item |
---|
| 1080 | for i in range(len(items)): |
---|
[2f60121] | 1081 | if items[i] == str(name): |
---|
[0277d084] | 1082 | self.formfactorbox.SetSelection(i) |
---|
| 1083 | break |
---|
| 1084 | break |
---|
| 1085 | |
---|
| 1086 | def _draw_model(self): |
---|
| 1087 | """ |
---|
[74755ff] | 1088 | Method to draw or refresh a plotted model. |
---|
| 1089 | The method will use the data member from the model page |
---|
| 1090 | to build a call to the fitting perspective manager. |
---|
| 1091 | |
---|
| 1092 | :Note to coder: This way future changes will be done in only one place. |
---|
| 1093 | |
---|
[0277d084] | 1094 | """ |
---|
[2f60121] | 1095 | if self.model != None: |
---|
| 1096 | temp_smear = None |
---|
[0277d084] | 1097 | if hasattr(self, "enable_smearer"): |
---|
| 1098 | if self.enable_smearer.GetValue(): |
---|
| 1099 | temp_smear= self.smearer |
---|
| 1100 | |
---|
| 1101 | self.manager.draw_model(self.model, data=self.data, |
---|
| 1102 | smearer= temp_smear, |
---|
| 1103 | qmin=float(self.qmin_x), |
---|
| 1104 | qmax=float(self.qmax_x), |
---|
| 1105 | qstep= float(self.num_points), |
---|
| 1106 | enable2D=self.enable2D) |
---|
| 1107 | |
---|
| 1108 | def _set_model_sizer(self,sizer, box_sizer, title="", object=None): |
---|
| 1109 | """ |
---|
[74755ff] | 1110 | Use lists to fill a sizer for model info |
---|
| 1111 | |
---|
[0277d084] | 1112 | """ |
---|
| 1113 | |
---|
| 1114 | sizer.Clear(True) |
---|
| 1115 | ##For MAC, this should defined here. |
---|
| 1116 | if box_sizer == None: |
---|
[2f60121] | 1117 | box_description = wx.StaticBox(self, -1, str(title)) |
---|
[0277d084] | 1118 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
| 1119 | else: |
---|
| 1120 | boxsizer1 = box_sizer |
---|
[4523b68] | 1121 | sizer_buttons = wx.BoxSizer(wx.HORIZONTAL) |
---|
[0277d084] | 1122 | #-------------------------------------------------------- |
---|
[2f60121] | 1123 | self.shape_rbutton = wx.RadioButton(self, -1, 'Shapes', |
---|
| 1124 | style=wx.RB_GROUP) |
---|
| 1125 | self.shape_indep_rbutton = wx.RadioButton(self, -1, |
---|
| 1126 | "Shape-Independent") |
---|
[0277d084] | 1127 | self.struct_rbutton = wx.RadioButton(self, -1, "Structure Factor ") |
---|
| 1128 | self.plugin_rbutton = wx.RadioButton(self, -1, "Customized Models") |
---|
[2f60121] | 1129 | self.Bind(wx.EVT_RADIOBUTTON, self._show_combox, |
---|
| 1130 | id=self.shape_rbutton.GetId()) |
---|
| 1131 | self.Bind(wx.EVT_RADIOBUTTON, self._show_combox, |
---|
| 1132 | id=self.shape_indep_rbutton.GetId()) |
---|
| 1133 | self.Bind(wx.EVT_RADIOBUTTON, self._show_combox, |
---|
| 1134 | id=self.struct_rbutton.GetId()) |
---|
| 1135 | self.Bind(wx.EVT_RADIOBUTTON, self._show_combox, |
---|
| 1136 | id=self.plugin_rbutton.GetId()) |
---|
[0277d084] | 1137 | #MAC needs SetValue |
---|
| 1138 | self.shape_rbutton.SetValue(True) |
---|
[2f60121] | 1139 | sizer_radiobutton = wx.GridSizer(2, 2, 5, 5) |
---|
[0277d084] | 1140 | sizer_radiobutton.Add(self.shape_rbutton) |
---|
| 1141 | sizer_radiobutton.Add(self.shape_indep_rbutton) |
---|
| 1142 | sizer_radiobutton.Add(self.plugin_rbutton) |
---|
| 1143 | sizer_radiobutton.Add(self.struct_rbutton) |
---|
[4523b68] | 1144 | sizer_buttons.Add(sizer_radiobutton) |
---|
| 1145 | # detail button |
---|
[2f60121] | 1146 | if object != None: |
---|
| 1147 | sizer_buttons.Add((50, 0)) |
---|
[4523b68] | 1148 | sizer_buttons.Add(object) |
---|
[0277d084] | 1149 | sizer_selection = wx.BoxSizer(wx.HORIZONTAL) |
---|
[4523b68] | 1150 | mutifactor_selection = wx.BoxSizer(wx.HORIZONTAL) |
---|
[2f60121] | 1151 | self.text1 = wx.StaticText(self, -1, "") |
---|
| 1152 | self.text2 = wx.StaticText(self, -1, "P(Q)*S(Q)") |
---|
| 1153 | self.mutifactor_text = wx.StaticText(self, -1, "No. of Shells: ") |
---|
| 1154 | self.mutifactor_text1 = wx.StaticText(self, -1, "") |
---|
| 1155 | self.show_sld_button = wx.Button(self, -1, "Show SLD Profile") |
---|
[a1b2471] | 1156 | self.show_sld_button.Bind(wx.EVT_BUTTON,self._on_show_sld) |
---|
[2f60121] | 1157 | self.formfactorbox = wx.ComboBox(self, -1, style=wx.CB_READONLY) |
---|
| 1158 | if self.model!= None: |
---|
[0277d084] | 1159 | self.formfactorbox.SetValue(self.model.name) |
---|
[2f60121] | 1160 | self.structurebox = wx.ComboBox(self, -1, style=wx.CB_READONLY) |
---|
| 1161 | self.multifactorbox = wx.ComboBox(self, -1, style=wx.CB_READONLY) |
---|
| 1162 | wx.EVT_COMBOBOX(self.formfactorbox, -1, self._on_select_model) |
---|
| 1163 | wx.EVT_COMBOBOX(self.structurebox, -1, self._on_select_model) |
---|
| 1164 | wx.EVT_COMBOBOX(self.multifactorbox, -1, self._on_select_model) |
---|
[0277d084] | 1165 | ## fill combox box |
---|
[2f60121] | 1166 | if len(self.model_list_box) > 0: |
---|
| 1167 | self._populate_box(self.formfactorbox, |
---|
| 1168 | self.model_list_box["Shapes"]) |
---|
| 1169 | if len(self.model_list_box) > 0: |
---|
| 1170 | self._populate_box(self.structurebox, |
---|
[0277d084] | 1171 | self.model_list_box["Structure Factors"]) |
---|
[2f60121] | 1172 | self.structurebox.Insert("None", 0, None) |
---|
[0277d084] | 1173 | self.structurebox.SetSelection(0) |
---|
| 1174 | self.structurebox.Hide() |
---|
| 1175 | self.text2.Hide() |
---|
| 1176 | self.structurebox.Disable() |
---|
| 1177 | self.text2.Disable() |
---|
| 1178 | if self.model.__class__ in self.model_list_box["P(Q)*S(Q)"]: |
---|
| 1179 | self.structurebox.Show() |
---|
| 1180 | self.text2.Show() |
---|
| 1181 | self.structurebox.Enable() |
---|
| 1182 | self.text2.Enable() |
---|
| 1183 | ## check model type to show sizer |
---|
[2f60121] | 1184 | if self.model != None: |
---|
| 1185 | self._set_model_sizer_selection(self.model) |
---|
[0277d084] | 1186 | sizer_selection.Add(self.text1) |
---|
[2f60121] | 1187 | sizer_selection.Add((5, 5)) |
---|
[0277d084] | 1188 | sizer_selection.Add(self.formfactorbox) |
---|
[2f60121] | 1189 | sizer_selection.Add((5, 5)) |
---|
[0277d084] | 1190 | sizer_selection.Add(self.text2) |
---|
[2f60121] | 1191 | sizer_selection.Add((5, 5)) |
---|
[0277d084] | 1192 | sizer_selection.Add(self.structurebox) |
---|
[2f60121] | 1193 | #sizer_selection.Add((5, 5)) |
---|
| 1194 | mutifactor_selection.Add((10, 5)) |
---|
[4523b68] | 1195 | mutifactor_selection.Add(self.mutifactor_text) |
---|
| 1196 | mutifactor_selection.Add(self.multifactorbox) |
---|
[2f60121] | 1197 | mutifactor_selection.Add((5, 5)) |
---|
[fb59ed9] | 1198 | mutifactor_selection.Add(self.mutifactor_text1) |
---|
[2f60121] | 1199 | mutifactor_selection.Add((10, 5)) |
---|
[a1b2471] | 1200 | mutifactor_selection.Add(self.show_sld_button) |
---|
[2f60121] | 1201 | boxsizer1.Add(sizer_buttons) |
---|
| 1202 | boxsizer1.Add((15, 15)) |
---|
| 1203 | boxsizer1.Add(sizer_selection) |
---|
| 1204 | boxsizer1.Add((10,10)) |
---|
[4523b68] | 1205 | boxsizer1.Add(mutifactor_selection) |
---|
| 1206 | self._set_multfactor_combobox() |
---|
[fb59ed9] | 1207 | self.multifactorbox.SetSelection(1) |
---|
[a1b2471] | 1208 | self.show_sld_button.Hide() |
---|
[0277d084] | 1209 | #-------------------------------------------------------- |
---|
[2f60121] | 1210 | sizer.Add(boxsizer1, 0, wx.EXPAND|wx.ALL, 10) |
---|
[0277d084] | 1211 | sizer.Layout() |
---|
[fc292d3] | 1212 | self.SetupScrolling() |
---|
[0277d084] | 1213 | |
---|
[a1b2471] | 1214 | def _on_show_sld(self, event=None): |
---|
| 1215 | """ |
---|
| 1216 | Plot SLD profile |
---|
| 1217 | """ |
---|
| 1218 | # get profile data |
---|
[2f60121] | 1219 | x, y = self.model.getProfile() |
---|
[a1b2471] | 1220 | from danse.common.plottools import Data1D |
---|
[da9ac4e6] | 1221 | #from profile_dialog import SLDPanel |
---|
| 1222 | from sans.guiframe.local_perspectives.plotting.profile_dialog \ |
---|
| 1223 | import SLDPanel |
---|
[2f60121] | 1224 | sld_data = Data1D(x, y) |
---|
[a1b2471] | 1225 | sld_data.name = 'SLD' |
---|
[fb59ed9] | 1226 | sld_data.axes = self.sld_axes |
---|
[2f60121] | 1227 | self.panel = SLDPanel(self, data=sld_data, axes=self.sld_axes, id=-1) |
---|
[a1b2471] | 1228 | self.panel.ShowModal() |
---|
| 1229 | |
---|
[fb59ed9] | 1230 | def _set_multfactor_combobox(self, multiplicity=10): |
---|
[4523b68] | 1231 | """ |
---|
| 1232 | Set comboBox for muitfactor of CoreMultiShellModel |
---|
[fb59ed9] | 1233 | :param multiplicit: no. of multi-functionality |
---|
[4523b68] | 1234 | """ |
---|
[fb59ed9] | 1235 | # build content of the combobox |
---|
[2f60121] | 1236 | for idx in range(0, multiplicity): |
---|
| 1237 | self.multifactorbox.Append(str(idx), int(idx)) |
---|
[fb59ed9] | 1238 | #self.multifactorbox.SetSelection(1) |
---|
[4523b68] | 1239 | self._hide_multfactor_combobox() |
---|
| 1240 | |
---|
| 1241 | def _show_multfactor_combobox(self): |
---|
| 1242 | """ |
---|
| 1243 | Show the comboBox of muitfactor of CoreMultiShellModel |
---|
| 1244 | """ |
---|
| 1245 | if not self.mutifactor_text.IsShown(): |
---|
| 1246 | self.mutifactor_text.Show(True) |
---|
[fb59ed9] | 1247 | self.mutifactor_text1.Show(True) |
---|
[4523b68] | 1248 | if not self.multifactorbox.IsShown(): |
---|
| 1249 | self.multifactorbox.Show(True) |
---|
| 1250 | |
---|
| 1251 | def _hide_multfactor_combobox(self): |
---|
| 1252 | """ |
---|
| 1253 | Hide the comboBox of muitfactor of CoreMultiShellModel |
---|
| 1254 | """ |
---|
| 1255 | if self.mutifactor_text.IsShown(): |
---|
| 1256 | self.mutifactor_text.Hide() |
---|
[fb59ed9] | 1257 | self.mutifactor_text1.Hide() |
---|
[4523b68] | 1258 | if self.multifactorbox.IsShown(): |
---|
| 1259 | self.multifactorbox.Hide() |
---|
| 1260 | |
---|
[0277d084] | 1261 | def _show_combox(self, event): |
---|
| 1262 | """ |
---|
[74755ff] | 1263 | Show combox box associate with type of model selected |
---|
| 1264 | |
---|
[0277d084] | 1265 | """ |
---|
| 1266 | ## Don't want to populate combo box again if the event comes from check box |
---|
[2f60121] | 1267 | if self.shape_rbutton.GetValue() and \ |
---|
| 1268 | event.GetEventObject() == self.shape_rbutton: |
---|
[0277d084] | 1269 | ##fill the combobox with form factor list |
---|
| 1270 | self.structurebox.SetSelection(0) |
---|
| 1271 | self.structurebox.Disable() |
---|
| 1272 | self.formfactorbox.Clear() |
---|
[2f60121] | 1273 | self._populate_box(self.formfactorbox, |
---|
| 1274 | self.model_list_box["Shapes"]) |
---|
| 1275 | if self.shape_indep_rbutton.GetValue() and \ |
---|
| 1276 | event.GetEventObject() == self.shape_indep_rbutton: |
---|
[0277d084] | 1277 | ##fill the combobox with shape independent factor list |
---|
| 1278 | self.structurebox.SetSelection(0) |
---|
| 1279 | self.structurebox.Disable() |
---|
| 1280 | self.formfactorbox.Clear() |
---|
[2f60121] | 1281 | self._populate_box(self.formfactorbox, |
---|
[0277d084] | 1282 | self.model_list_box["Shape-Independent"]) |
---|
[2f60121] | 1283 | if self.struct_rbutton.GetValue() and \ |
---|
| 1284 | event.GetEventObject() == self.struct_rbutton: |
---|
[0277d084] | 1285 | ##fill the combobox with structure factor list |
---|
| 1286 | self.structurebox.SetSelection(0) |
---|
| 1287 | self.structurebox.Disable() |
---|
| 1288 | self.formfactorbox.Clear() |
---|
[2f60121] | 1289 | self._populate_box(self.formfactorbox, |
---|
[0277d084] | 1290 | self.model_list_box["Structure Factors"]) |
---|
[2f60121] | 1291 | if self.plugin_rbutton.GetValue() and \ |
---|
| 1292 | event.GetEventObject() == self.plugin_rbutton: |
---|
[0277d084] | 1293 | ##fill the combobox with form factor list |
---|
| 1294 | self.structurebox.Disable() |
---|
| 1295 | self.formfactorbox.Clear() |
---|
[2f60121] | 1296 | self._populate_box(self.formfactorbox, |
---|
[0277d084] | 1297 | self.model_list_box["Customized Models"]) |
---|
| 1298 | self._on_select_model(event=None) |
---|
| 1299 | self._save_typeOfmodel() |
---|
| 1300 | self.sizer4_4.Layout() |
---|
| 1301 | self.sizer4.Layout() |
---|
| 1302 | self.Layout() |
---|
| 1303 | self.Refresh() |
---|
[fc292d3] | 1304 | self.SetupScrolling() |
---|
[0277d084] | 1305 | |
---|
| 1306 | def _populate_box(self, combobox, list): |
---|
| 1307 | """ |
---|
[74755ff] | 1308 | fill combox box with dict item |
---|
| 1309 | |
---|
| 1310 | :param list: contains item to fill the combox |
---|
[0277d084] | 1311 | item must model class |
---|
| 1312 | """ |
---|
| 1313 | for models in list: |
---|
[2f60121] | 1314 | model = models() |
---|
[0277d084] | 1315 | name = model.__class__.__name__ |
---|
| 1316 | if models.__name__!="NoStructure": |
---|
| 1317 | if hasattr(model, "name"): |
---|
| 1318 | name = model.name |
---|
[2f60121] | 1319 | combobox.Append(name, models) |
---|
[0277d084] | 1320 | return 0 |
---|
| 1321 | |
---|
| 1322 | def _onparamEnter(self,event): |
---|
| 1323 | """ |
---|
[74755ff] | 1324 | when enter value on panel redraw model according to changed |
---|
| 1325 | |
---|
[0277d084] | 1326 | """ |
---|
[2f60121] | 1327 | tcrtl = event.GetEventObject() |
---|
[0277d084] | 1328 | #Clear msg if previously shown. |
---|
[2f60121] | 1329 | msg = "" |
---|
| 1330 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
[0277d084] | 1331 | ## save current state |
---|
| 1332 | self.save_current_state() |
---|
[2f60121] | 1333 | if event != None: |
---|
[0277d084] | 1334 | #self._undo.Enable(True) |
---|
[2f60121] | 1335 | event = PageInfoEvent(page=self) |
---|
[0277d084] | 1336 | wx.PostEvent(self.parent, event) |
---|
| 1337 | if check_float(tcrtl): |
---|
| 1338 | self._onparamEnter_helper() |
---|
| 1339 | #event.Skip() |
---|
| 1340 | else: |
---|
[2f60121] | 1341 | msg = "Cannot Plot :Must enter a number!!! " |
---|
| 1342 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
[0277d084] | 1343 | #event.Skip() |
---|
| 1344 | return |
---|
| 1345 | |
---|
| 1346 | def _onQrangeEnter(self, event): |
---|
| 1347 | """ |
---|
[74755ff] | 1348 | Check validity of value enter in the Q range field |
---|
| 1349 | |
---|
[0277d084] | 1350 | """ |
---|
[2f60121] | 1351 | tcrtl = event.GetEventObject() |
---|
[0277d084] | 1352 | #Clear msg if previously shown. |
---|
[2f60121] | 1353 | msg = "" |
---|
| 1354 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
[0277d084] | 1355 | # Flag to register when a parameter has changed. |
---|
| 1356 | is_modified = False |
---|
[2f60121] | 1357 | if tcrtl.GetValue().lstrip().rstrip() != "": |
---|
[0277d084] | 1358 | try: |
---|
| 1359 | value = float(tcrtl.GetValue()) |
---|
| 1360 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
| 1361 | # If qmin and qmax have been modified, update qmin and qmax |
---|
[2f60121] | 1362 | if self._validate_qrange(self.qmin, self.qmax): |
---|
[0277d084] | 1363 | tempmin = float(self.qmin.GetValue()) |
---|
| 1364 | if tempmin != self.qmin_x: |
---|
| 1365 | self.qmin_x = tempmin |
---|
| 1366 | tempmax = float(self.qmax.GetValue()) |
---|
| 1367 | if tempmax != self.qmax_x: |
---|
| 1368 | self.qmax_x = tempmax |
---|
| 1369 | else: |
---|
| 1370 | tcrtl.SetBackgroundColour("pink") |
---|
[2f60121] | 1371 | msg = "Model Error:wrong value entered : %s" % sys.exc_value |
---|
| 1372 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
[0277d084] | 1373 | return |
---|
| 1374 | except: |
---|
| 1375 | tcrtl.SetBackgroundColour("pink") |
---|
[2f60121] | 1376 | msg = "Model Error:wrong value entered : %s" % sys.exc_value |
---|
| 1377 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
[0277d084] | 1378 | return |
---|
| 1379 | #Check if # of points for theory model are valid(>0). |
---|
| 1380 | if self.npts != None: |
---|
| 1381 | if check_float(self.npts): |
---|
| 1382 | temp_npts = float(self.npts.GetValue()) |
---|
| 1383 | if temp_npts != self.num_points: |
---|
| 1384 | self.num_points = temp_npts |
---|
| 1385 | is_modified = True |
---|
| 1386 | else: |
---|
[2f60121] | 1387 | msg = "Cannot Plot :No npts in that Qrange!!! " |
---|
| 1388 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
[0277d084] | 1389 | else: |
---|
| 1390 | tcrtl.SetBackgroundColour("pink") |
---|
[2f60121] | 1391 | msg = "Model Error:wrong value entered!!!" |
---|
| 1392 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
[0277d084] | 1393 | #self._undo.Enable(True) |
---|
| 1394 | self.save_current_state() |
---|
[2f60121] | 1395 | event = PageInfoEvent(page=self) |
---|
[0277d084] | 1396 | wx.PostEvent(self.parent, event) |
---|
[2f60121] | 1397 | self.state_change = False |
---|
[0277d084] | 1398 | #Draw the model for a different range |
---|
| 1399 | self._draw_model() |
---|
| 1400 | |
---|
| 1401 | def _on_select_model_helper(self): |
---|
| 1402 | """ |
---|
[74755ff] | 1403 | call back for model selection |
---|
| 1404 | |
---|
[0277d084] | 1405 | """ |
---|
| 1406 | ## reset dictionary containing reference to dispersion |
---|
| 1407 | self._disp_obj_dict = {} |
---|
[2f60121] | 1408 | self.disp_cb_dict = {} |
---|
[0277d084] | 1409 | f_id = self.formfactorbox.GetCurrentSelection() |
---|
| 1410 | #For MAC |
---|
| 1411 | form_factor = None |
---|
| 1412 | if f_id >= 0: |
---|
[2f60121] | 1413 | form_factor = self.formfactorbox.GetClientData(f_id) |
---|
[0277d084] | 1414 | if not form_factor in self.model_list_box["multiplication"]: |
---|
| 1415 | self.structurebox.Hide() |
---|
| 1416 | self.text2.Hide() |
---|
| 1417 | self.structurebox.Disable() |
---|
| 1418 | self.structurebox.SetSelection(0) |
---|
| 1419 | self.text2.Disable() |
---|
| 1420 | else: |
---|
| 1421 | self.structurebox.Show() |
---|
| 1422 | self.text2.Show() |
---|
| 1423 | self.structurebox.Enable() |
---|
| 1424 | self.text2.Enable() |
---|
[4523b68] | 1425 | if form_factor != None: |
---|
| 1426 | # set multifactor for Mutifunctional models |
---|
[e87f9fc] | 1427 | if form_factor().__class__ in self.model_list_box["Multi-Functions"]: |
---|
[4523b68] | 1428 | m_id = self.multifactorbox.GetCurrentSelection() |
---|
[fb59ed9] | 1429 | multiplicity = form_factor().multiplicity_info[0] |
---|
| 1430 | self.multifactorbox.Clear() |
---|
| 1431 | #self.mutifactor_text.SetLabel(form_factor().details[]) |
---|
| 1432 | self._set_multfactor_combobox(multiplicity) |
---|
| 1433 | self._show_multfactor_combobox() |
---|
| 1434 | text = form_factor().multiplicity_info[1]#'No. of Shells: ' |
---|
| 1435 | |
---|
| 1436 | #self.mutifactor_text.Clear() |
---|
| 1437 | self.mutifactor_text.SetLabel(text) |
---|
| 1438 | |
---|
| 1439 | # Check max value |
---|
[2f60121] | 1440 | if m_id > multiplicity - 1: |
---|
[fb59ed9] | 1441 | # default value |
---|
| 1442 | m_id = 1 |
---|
[4523b68] | 1443 | self.multi_factor = self.multifactorbox.GetClientData(m_id) |
---|
[2f60121] | 1444 | if self.multi_factor == None: |
---|
| 1445 | self.multi_factor =1 |
---|
[4523b68] | 1446 | form_factor = form_factor(int(self.multi_factor)) |
---|
[fb59ed9] | 1447 | self.multifactorbox.SetSelection(m_id) |
---|
| 1448 | # Check len of the text1 and max_multiplicity |
---|
| 1449 | text = '' |
---|
| 1450 | if form_factor.multiplicity_info[0] == len(form_factor.multiplicity_info[2]): |
---|
| 1451 | text = form_factor.multiplicity_info[2][self.multi_factor] |
---|
| 1452 | self.mutifactor_text1.SetLabel(text) |
---|
| 1453 | # Check if model has get sld profile. |
---|
| 1454 | if len(form_factor.multiplicity_info[3]) > 0: |
---|
| 1455 | self.sld_axes = form_factor.multiplicity_info[3] |
---|
| 1456 | self.show_sld_button.Show(True) |
---|
| 1457 | else: |
---|
| 1458 | self.sld_axes = "" |
---|
[4523b68] | 1459 | else: |
---|
| 1460 | self._hide_multfactor_combobox() |
---|
[a1b2471] | 1461 | self.show_sld_button.Hide() |
---|
[4523b68] | 1462 | form_factor = form_factor() |
---|
| 1463 | self.multi_factor = None |
---|
| 1464 | else: |
---|
| 1465 | self._hide_multfactor_combobox() |
---|
[a1b2471] | 1466 | self.show_sld_button.Hide() |
---|
[4523b68] | 1467 | self.multi_factor = None |
---|
[0277d084] | 1468 | s_id = self.structurebox.GetCurrentSelection() |
---|
[2f60121] | 1469 | struct_factor = self.structurebox.GetClientData(s_id) |
---|
| 1470 | if struct_factor != None: |
---|
[0277d084] | 1471 | from sans.models.MultiplicationModel import MultiplicationModel |
---|
[2f60121] | 1472 | self.model = MultiplicationModel(form_factor, struct_factor()) |
---|
[0277d084] | 1473 | else: |
---|
| 1474 | if form_factor != None: |
---|
[2f60121] | 1475 | self.model = form_factor |
---|
[0277d084] | 1476 | else: |
---|
| 1477 | self.model = None |
---|
| 1478 | return self.model |
---|
| 1479 | ## post state to fit panel |
---|
[2f60121] | 1480 | self.state.parameters = [] |
---|
| 1481 | self.state.model = self.model |
---|
[4523b68] | 1482 | self.state.qmin = self.qmin_x |
---|
| 1483 | self.state.multi_factor = self.multi_factor |
---|
[2f60121] | 1484 | self.disp_list = self.model.getDispParamList() |
---|
[0277d084] | 1485 | self.state.disp_list = self.disp_list |
---|
| 1486 | self.Layout() |
---|
| 1487 | |
---|
| 1488 | def _validate_qrange(self, qmin_ctrl, qmax_ctrl): |
---|
| 1489 | """ |
---|
[74755ff] | 1490 | Verify that the Q range controls have valid values |
---|
| 1491 | and that Qmin < Qmax. |
---|
| 1492 | |
---|
| 1493 | :param qmin_ctrl: text control for Qmin |
---|
| 1494 | :param qmax_ctrl: text control for Qmax |
---|
| 1495 | |
---|
| 1496 | :return: True is the Q range is value, False otherwise |
---|
| 1497 | |
---|
[0277d084] | 1498 | """ |
---|
| 1499 | qmin_validity = check_float(qmin_ctrl) |
---|
| 1500 | qmax_validity = check_float(qmax_ctrl) |
---|
| 1501 | if not (qmin_validity and qmax_validity): |
---|
| 1502 | return False |
---|
| 1503 | else: |
---|
| 1504 | qmin = float(qmin_ctrl.GetValue()) |
---|
| 1505 | qmax = float(qmax_ctrl.GetValue()) |
---|
| 1506 | if qmin < qmax: |
---|
| 1507 | #Make sure to set both colours white. |
---|
| 1508 | qmin_ctrl.SetBackgroundColour(wx.WHITE) |
---|
| 1509 | qmin_ctrl.Refresh() |
---|
| 1510 | qmax_ctrl.SetBackgroundColour(wx.WHITE) |
---|
| 1511 | qmax_ctrl.Refresh() |
---|
| 1512 | else: |
---|
| 1513 | qmin_ctrl.SetBackgroundColour("pink") |
---|
| 1514 | qmin_ctrl.Refresh() |
---|
| 1515 | qmax_ctrl.SetBackgroundColour("pink") |
---|
| 1516 | qmax_ctrl.Refresh() |
---|
| 1517 | msg= "Invalid Q range: Q min must be smaller than Q max" |
---|
[2f60121] | 1518 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
[0277d084] | 1519 | return False |
---|
| 1520 | return True |
---|
| 1521 | |
---|
| 1522 | def _check_value_enter(self, list, modified): |
---|
| 1523 | """ |
---|
[74755ff] | 1524 | :param list: model parameter and panel info |
---|
| 1525 | each item of the list should be as follow: :: |
---|
| 1526 | |
---|
| 1527 | item = [cb state, name, value, "+/-", error of fit, min, max , units] |
---|
| 1528 | |
---|
[0277d084] | 1529 | """ |
---|
| 1530 | is_modified = modified |
---|
[2f60121] | 1531 | if len(list) == 0: |
---|
[0277d084] | 1532 | return is_modified |
---|
| 1533 | for item in list: |
---|
| 1534 | #skip angle parameters for 1D |
---|
[885857e] | 1535 | if not self.enable2D: |
---|
[0277d084] | 1536 | if item in self.orientation_params: |
---|
| 1537 | continue |
---|
| 1538 | #try: |
---|
| 1539 | name = str(item[1]) |
---|
| 1540 | try: |
---|
[2f60121] | 1541 | value = float(item[2].GetValue()) |
---|
[0277d084] | 1542 | # If the value of the parameter has changed, |
---|
| 1543 | # +update the model and set the is_modified flag |
---|
| 1544 | if value != self.model.getParam(name) and numpy.isfinite(value): |
---|
| 1545 | self.model.setParam(name,value) |
---|
| 1546 | is_modified = True |
---|
| 1547 | except: |
---|
[885857e] | 1548 | msg = "Wrong parameter value entered " |
---|
[2f60121] | 1549 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
[0277d084] | 1550 | return is_modified |
---|
[74755ff] | 1551 | |
---|
[0277d084] | 1552 | def _set_dipers_Param(self, event): |
---|
| 1553 | """ |
---|
[74755ff] | 1554 | respond to self.enable_disp and self.disable_disp radio box. |
---|
| 1555 | The dispersity object is reset inside the model into Gaussian. |
---|
[2f60121] | 1556 | When the user select yes , this method display a combo box |
---|
| 1557 | for more selection |
---|
[74755ff] | 1558 | when the user selects No,the combo box disappears. |
---|
| 1559 | Redraw the model with the default dispersity (Gaussian) |
---|
| 1560 | |
---|
[0277d084] | 1561 | """ |
---|
| 1562 | self._reset_dispersity() |
---|
[2f60121] | 1563 | if self.model == None: |
---|
[0277d084] | 1564 | self.model_disp.Hide() |
---|
| 1565 | self.disp_box.Hide() |
---|
| 1566 | self.sizer4_4.Clear(True) |
---|
| 1567 | return |
---|
| 1568 | if self.enable_disp.GetValue(): |
---|
| 1569 | self.model_disp.Show(True) |
---|
| 1570 | self.disp_box.Show(True) |
---|
| 1571 | ## layout for model containing no dispersity parameters |
---|
[2f60121] | 1572 | self.disp_list = self.model.getDispParamList() |
---|
| 1573 | if len(self.disp_list) == 0 and len(self.disp_cb_dict) == 0: |
---|
[0277d084] | 1574 | self._layout_sizer_noDipers() |
---|
| 1575 | else: |
---|
| 1576 | ## set gaussian sizer |
---|
| 1577 | self._on_select_Disp(event=None) |
---|
| 1578 | else: |
---|
| 1579 | self.model_disp.Hide() |
---|
| 1580 | self.disp_box.Hide() |
---|
| 1581 | self.disp_box.SetSelection(0) |
---|
| 1582 | self.sizer4_4.Clear(True) |
---|
| 1583 | ## post state to fit panel |
---|
| 1584 | self.save_current_state() |
---|
[2f60121] | 1585 | if event != None: |
---|
[0277d084] | 1586 | #self._undo.Enable(True) |
---|
[2f60121] | 1587 | event = PageInfoEvent(page=self) |
---|
[0277d084] | 1588 | wx.PostEvent(self.parent, event) |
---|
| 1589 | #draw the model with the current dispersity |
---|
| 1590 | self._draw_model() |
---|
| 1591 | self.sizer4_4.Layout() |
---|
| 1592 | self.sizer5.Layout() |
---|
| 1593 | self.Layout() |
---|
| 1594 | self.Refresh() |
---|
| 1595 | |
---|
| 1596 | def _layout_sizer_noDipers(self): |
---|
| 1597 | """ |
---|
[74755ff] | 1598 | Draw a sizer with no dispersity info |
---|
| 1599 | |
---|
[0277d084] | 1600 | """ |
---|
[2f60121] | 1601 | ix = 0 |
---|
| 1602 | iy = 1 |
---|
| 1603 | self.fittable_param = [] |
---|
| 1604 | self.fixed_param = [] |
---|
| 1605 | self.orientation_params_disp = [] |
---|
[0277d084] | 1606 | self.model_disp.Hide() |
---|
| 1607 | self.disp_box.Hide() |
---|
| 1608 | self.sizer4_4.Clear(True) |
---|
[01b6bd0] | 1609 | text = "No polydispersity available for this model" |
---|
| 1610 | model_disp = wx.StaticText(self, -1, text) |
---|
[2f60121] | 1611 | self.sizer4_4.Add(model_disp,(iy, ix), (1,1), |
---|
| 1612 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[0277d084] | 1613 | self.sizer4_4.Layout() |
---|
| 1614 | self.sizer4.Layout() |
---|
[fc292d3] | 1615 | self.SetupScrolling() |
---|
[0277d084] | 1616 | |
---|
| 1617 | def _reset_dispersity(self): |
---|
| 1618 | """ |
---|
[74755ff] | 1619 | put gaussian dispersity into current model |
---|
| 1620 | |
---|
[0277d084] | 1621 | """ |
---|
[2f60121] | 1622 | if len(self.param_toFit) > 0: |
---|
[0277d084] | 1623 | for item in self.fittable_param: |
---|
| 1624 | if item in self.param_toFit: |
---|
| 1625 | self.param_toFit.remove(item) |
---|
| 1626 | for item in self.orientation_params_disp: |
---|
| 1627 | if item in self.param_toFit: |
---|
| 1628 | self.param_toFit.remove(item) |
---|
[2f60121] | 1629 | self.fittable_param = [] |
---|
| 1630 | self.fixed_param = [] |
---|
| 1631 | self.orientation_params_disp = [] |
---|
| 1632 | self.values = [] |
---|
| 1633 | self.weights = [] |
---|
| 1634 | from sans.models.dispersion_models import GaussianDispersion |
---|
| 1635 | from sans.models.dispersion_models import ArrayDispersion |
---|
| 1636 | if len(self.disp_cb_dict) == 0: |
---|
[0277d084] | 1637 | self.save_current_state() |
---|
| 1638 | self.sizer4_4.Clear(True) |
---|
| 1639 | self.Layout() |
---|
| 1640 | return |
---|
[2f60121] | 1641 | if (len(self.disp_cb_dict) > 0) : |
---|
[0277d084] | 1642 | for p in self.disp_cb_dict: |
---|
[2f60121] | 1643 | # The parameter was un-selected. |
---|
| 1644 | # Go back to Gaussian model (with 0 pts) |
---|
[0277d084] | 1645 | disp_model = GaussianDispersion() |
---|
| 1646 | self._disp_obj_dict[p] = disp_model |
---|
[2f60121] | 1647 | # Set the new model as the dispersion object |
---|
| 1648 | # for the selected parameter |
---|
[0277d084] | 1649 | try: |
---|
| 1650 | self.model.set_dispersion(p, disp_model) |
---|
| 1651 | except: |
---|
| 1652 | pass |
---|
| 1653 | ## save state into |
---|
| 1654 | self.save_current_state() |
---|
| 1655 | self.Layout() |
---|
| 1656 | self.Refresh() |
---|
| 1657 | |
---|
[2f60121] | 1658 | def _on_select_Disp(self, event): |
---|
[0277d084] | 1659 | """ |
---|
[74755ff] | 1660 | allow selecting different dispersion |
---|
| 1661 | self.disp_list should change type later .now only gaussian |
---|
| 1662 | |
---|
[0277d084] | 1663 | """ |
---|
| 1664 | n = self.disp_box.GetCurrentSelection() |
---|
| 1665 | name = self.disp_box.GetValue() |
---|
[2f60121] | 1666 | dispersity = self.disp_box.GetClientData(n) |
---|
[0277d084] | 1667 | self.disp_name = name |
---|
| 1668 | if name.lower() == "array": |
---|
| 1669 | self._set_sizer_arraydispersion() |
---|
| 1670 | else: |
---|
[2f60121] | 1671 | self._set_sizer_dispersion(dispersity=dispersity) |
---|
| 1672 | self.state.disp_box = n |
---|
[0277d084] | 1673 | ## Redraw the model |
---|
| 1674 | self._draw_model() |
---|
| 1675 | #self._undo.Enable(True) |
---|
[2f60121] | 1676 | event = PageInfoEvent(page=self) |
---|
[0277d084] | 1677 | wx.PostEvent(self.parent, event) |
---|
| 1678 | self.sizer4_4.Layout() |
---|
| 1679 | self.sizer4.Layout() |
---|
[fc292d3] | 1680 | self.SetupScrolling() |
---|
[0277d084] | 1681 | |
---|
| 1682 | def _set_sizer_arraydispersion(self): |
---|
| 1683 | """ |
---|
[74755ff] | 1684 | draw sizer with array dispersity parameters |
---|
[0277d084] | 1685 | """ |
---|
[2f60121] | 1686 | if len(self.param_toFit) > 0: |
---|
[0277d084] | 1687 | for item in self.fittable_param: |
---|
| 1688 | if item in self.param_toFit: |
---|
| 1689 | self.param_toFit.remove(item) |
---|
| 1690 | for item in self.orientation_params_disp: |
---|
| 1691 | if item in self.param_toFit: |
---|
| 1692 | self.param_toFit.remove(item) |
---|
| 1693 | for item in self.model.details.keys(): |
---|
| 1694 | if item in self.model.fixed: |
---|
| 1695 | del self.model.details [item] |
---|
[2f60121] | 1696 | self.fittable_param = [] |
---|
| 1697 | self.fixed_param = [] |
---|
| 1698 | self.orientation_params_disp = [] |
---|
[0277d084] | 1699 | self.sizer4_4.Clear(True) |
---|
| 1700 | self._reset_dispersity() |
---|
[2f60121] | 1701 | ix = 0 |
---|
| 1702 | iy = 1 |
---|
[0277d084] | 1703 | disp1 = wx.StaticText(self, -1, 'Array Dispersion') |
---|
[2f60121] | 1704 | self.sizer4_4.Add(disp1, (iy, ix), (1, 1), |
---|
| 1705 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 1706 | # Look for model parameters to which we can apply |
---|
| 1707 | # an ArrayDispersion model |
---|
[0277d084] | 1708 | # Add a check box for each parameter. |
---|
| 1709 | self.disp_cb_dict = {} |
---|
| 1710 | ix+=1 |
---|
[2f60121] | 1711 | self.noDisper_rbox = wx.RadioButton(self, -1, "None", |
---|
| 1712 | (10, 10), style=wx.RB_GROUP) |
---|
| 1713 | self.Bind(wx.EVT_RADIOBUTTON, self.select_disp_angle, |
---|
| 1714 | id=self.noDisper_rbox.GetId()) |
---|
[0277d084] | 1715 | #MAC needs SetValue |
---|
| 1716 | self.noDisper_rbox.SetValue(True) |
---|
| 1717 | self.sizer4_4.Add(self.noDisper_rbox, (iy, ix), |
---|
[2f60121] | 1718 | (1, 1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[0277d084] | 1719 | for p in self.model.dispersion.keys(): |
---|
| 1720 | if not p in self.model.orientation_params: |
---|
[2f60121] | 1721 | ix += 1 |
---|
[0277d084] | 1722 | self.disp_cb_dict[p] = wx.RadioButton(self, -1, p, (10, 10)) |
---|
[2f60121] | 1723 | self.state.disp_cb_dict[p] = self.disp_cb_dict[p].GetValue() |
---|
| 1724 | self.Bind(wx.EVT_RADIOBUTTON, self.select_disp_angle, |
---|
| 1725 | id=self.disp_cb_dict[p].GetId()) |
---|
| 1726 | self.sizer4_4.Add(self.disp_cb_dict[p], (iy, ix), (1,1), |
---|
| 1727 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[0277d084] | 1728 | for p in self.model.dispersion.keys(): |
---|
| 1729 | if p in self.model.orientation_params: |
---|
[2f60121] | 1730 | ix += 1 |
---|
[0277d084] | 1731 | self.disp_cb_dict[p] = wx.RadioButton(self, -1, p, (10, 10)) |
---|
[2f60121] | 1732 | self.state.disp_cb_dict[p] = self.disp_cb_dict[p].GetValue() |
---|
| 1733 | if not (self.enable2D or \ |
---|
| 1734 | self.data.__class__.__name__ == "Data2D"): |
---|
[0277d084] | 1735 | self.disp_cb_dict[p].Hide() |
---|
| 1736 | else: |
---|
| 1737 | self.disp_cb_dict[p].Show(True) |
---|
[2f60121] | 1738 | self.Bind(wx.EVT_RADIOBUTTON, self.select_disp_angle, |
---|
| 1739 | id=self.disp_cb_dict[p].GetId()) |
---|
| 1740 | self.sizer4_4.Add(self.disp_cb_dict[p], (iy, ix), (1,1), |
---|
| 1741 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 1742 | ix = 0 |
---|
| 1743 | iy += 1 |
---|
| 1744 | self.sizer4_4.Add((20, 20), (iy, ix), (1, 1), |
---|
| 1745 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[0277d084] | 1746 | self.Layout() |
---|
[2f60121] | 1747 | self.state.orientation_params = [] |
---|
| 1748 | self.state.orientation_params_disp = [] |
---|
| 1749 | self.state.parameters = [] |
---|
| 1750 | self.state.fittable_param = [] |
---|
| 1751 | self.state.fixed_param = [] |
---|
[0277d084] | 1752 | ## save checkbutton state and txtcrtl values |
---|
| 1753 | self._copy_parameters_state(self.orientation_params, |
---|
| 1754 | self.state.orientation_params) |
---|
| 1755 | self._copy_parameters_state(self.orientation_params_disp, |
---|
| 1756 | self.state.orientation_params_disp) |
---|
| 1757 | self._copy_parameters_state(self.parameters, self.state.parameters) |
---|
[2f60121] | 1758 | self._copy_parameters_state(self.fittable_param, |
---|
| 1759 | self.state.fittable_param) |
---|
[0277d084] | 1760 | self._copy_parameters_state(self.fixed_param, self.state.fixed_param) |
---|
| 1761 | ## post state to fit panel |
---|
[2f60121] | 1762 | event = PageInfoEvent(page=self) |
---|
[0277d084] | 1763 | wx.PostEvent(self.parent, event) |
---|
| 1764 | |
---|
| 1765 | def _set_range_sizer(self, title, box_sizer=None, object1=None,object=None): |
---|
| 1766 | """ |
---|
[74755ff] | 1767 | Fill the Q range sizer |
---|
| 1768 | |
---|
[0277d084] | 1769 | """ |
---|
| 1770 | self.sizer5.Clear(True) |
---|
| 1771 | #-------------------------------------------------------------- |
---|
| 1772 | if box_sizer == None: |
---|
[2f60121] | 1773 | box_description= wx.StaticBox(self, -1, str(title)) |
---|
[0277d084] | 1774 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
| 1775 | else: |
---|
| 1776 | #for MAC |
---|
| 1777 | boxsizer1 = box_sizer |
---|
| 1778 | |
---|
[2f60121] | 1779 | self.qmin = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH, 20), |
---|
| 1780 | style=wx. TE_PROCESS_ENTER, |
---|
| 1781 | kill_focus_callback=self._onQrangeEnter, |
---|
| 1782 | text_enter_callback=self._onQrangeEnter) |
---|
[0277d084] | 1783 | self.qmin.SetValue(str(self.qmin_x)) |
---|
| 1784 | self.qmin.SetToolTipString("Minimun value of Q in linear scale.") |
---|
| 1785 | |
---|
[2f60121] | 1786 | self.qmax = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH, 20), |
---|
| 1787 | style=wx.TE_PROCESS_ENTER, |
---|
| 1788 | kill_focus_callback=self._onQrangeEnter, |
---|
| 1789 | text_enter_callback=self._onQrangeEnter) |
---|
[0277d084] | 1790 | self.qmax.SetValue(str(self.qmax_x)) |
---|
| 1791 | self.qmax.SetToolTipString("Maximum value of Q in linear scale.") |
---|
| 1792 | id = wx.NewId() |
---|
[2f60121] | 1793 | self.reset_qrange = wx.Button(self, id, 'Reset', size=(70, 23)) |
---|
| 1794 | self.reset_qrange.Bind(wx.EVT_BUTTON, self.on_reset_clicked, id=id) |
---|
[0277d084] | 1795 | self.reset_qrange.SetToolTipString("Reset Q range to the default values") |
---|
[2f60121] | 1796 | sizer_horizontal = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 1797 | sizer = wx.GridSizer(3, 3, 2, 5) |
---|
[0277d084] | 1798 | sizer.Add(wx.StaticText(self, -1, ' Q range')) |
---|
| 1799 | sizer.Add(wx.StaticText(self, -1, ' Min')) |
---|
| 1800 | sizer.Add(wx.StaticText(self, -1, ' Max')) |
---|
| 1801 | sizer.Add(self.reset_qrange) |
---|
| 1802 | sizer.Add(self.qmin) |
---|
| 1803 | sizer.Add(self.qmax) |
---|
| 1804 | sizer_horizontal.Add(sizer) |
---|
[2f60121] | 1805 | if object != None: |
---|
[0277d084] | 1806 | sizer_horizontal.Add(object) |
---|
[2f60121] | 1807 | if object1 != None: |
---|
[0277d084] | 1808 | boxsizer1.Add(object1) |
---|
[2f60121] | 1809 | boxsizer1.Add((10, 10)) |
---|
[0277d084] | 1810 | boxsizer1.Add(sizer_horizontal) |
---|
| 1811 | ## save state |
---|
| 1812 | self.save_current_state() |
---|
| 1813 | #---------------------------------------------------------------- |
---|
[2f60121] | 1814 | self.sizer5.Add(boxsizer1, 0, wx.EXPAND|wx.ALL, 10) |
---|
[0277d084] | 1815 | self.sizer5.Layout() |
---|
| 1816 | |
---|
| 1817 | def _fill_save_sizer(self): |
---|
| 1818 | """ |
---|
[74755ff] | 1819 | Draw the layout for saving option |
---|
| 1820 | |
---|
[0277d084] | 1821 | """ |
---|
| 1822 | # Skipping save state functionality for release 0.9.0 |
---|
| 1823 | return |
---|
| 1824 | self.sizer6.Clear(True) |
---|
[2f60121] | 1825 | box_description= wx.StaticBox(self, -1, "Save Model") |
---|
[0277d084] | 1826 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
| 1827 | sizer_save = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 1828 | |
---|
| 1829 | self.btSave_title = wx.StaticText(self, -1, 'Save the current Model') |
---|
| 1830 | self.btSave = wx.Button(self,wx.NewId(),'Save') |
---|
[2f60121] | 1831 | self.btSave.Bind(wx.EVT_BUTTON, self.onSave,id=self.btSave.GetId()) |
---|
[0277d084] | 1832 | self.btSave.SetToolTipString("Save the current Model") |
---|
| 1833 | sizer_save.Add(self.btSave_title) |
---|
[2f60121] | 1834 | sizer_save.Add((20, 20), 0, wx.LEFT|wx.RIGHT|wx.EXPAND, 45) |
---|
[0277d084] | 1835 | sizer_save.Add(self.btSave) |
---|
| 1836 | boxsizer1.Add(sizer_save) |
---|
[2f60121] | 1837 | self.sizer6.Add(boxsizer1, 0, wx.EXPAND|wx.ALL, 10) |
---|
[0277d084] | 1838 | self.sizer6.Layout() |
---|
[fc292d3] | 1839 | self.SetupScrolling() |
---|
[0277d084] | 1840 | |
---|
| 1841 | def _lay_out(self): |
---|
| 1842 | """ |
---|
[74755ff] | 1843 | returns self.Layout |
---|
| 1844 | |
---|
| 1845 | :Note: Mac seems to like this better when self.Layout |
---|
| 1846 | is called after fitting. |
---|
| 1847 | |
---|
[0277d084] | 1848 | """ |
---|
| 1849 | self._sleep4sec() |
---|
| 1850 | self.Layout() |
---|
| 1851 | #self._sleep4sec() |
---|
| 1852 | return |
---|
| 1853 | |
---|
| 1854 | def _sleep4sec(self): |
---|
| 1855 | """ |
---|
[74755ff] | 1856 | sleep for 1 sec only applied on Mac |
---|
| 1857 | |
---|
| 1858 | :Note: This 1sec helps for Mac not to crash on |
---|
| 1859 | self.layout after self._draw_model |
---|
| 1860 | |
---|
[0277d084] | 1861 | """ |
---|
| 1862 | if ON_MAC == True: |
---|
| 1863 | time.sleep(1) |
---|
| 1864 | |
---|
[2f60121] | 1865 | def on_reset_clicked(self, event): |
---|
[0277d084] | 1866 | """ |
---|
[74755ff] | 1867 | On 'Reset' button for Q range clicked |
---|
| 1868 | |
---|
[0277d084] | 1869 | """ |
---|
| 1870 | ##For 3 different cases: Data2D, Data1D, and theory |
---|
| 1871 | if self.data.__class__.__name__ == "Data2D": |
---|
[2f60121] | 1872 | data_min = 0 |
---|
| 1873 | x = max(math.fabs(self.data.xmin), math.fabs(self.data.xmax)) |
---|
| 1874 | y = max(math.fabs(self.data.ymin), math.fabs(self.data.ymax)) |
---|
[0277d084] | 1875 | self.qmin_x = data_min |
---|
[2f60121] | 1876 | self.qmax_x = math.sqrt(x * x + y * y) |
---|
[0277d084] | 1877 | elif self.data.__class__.__name__ == "Data1D": |
---|
| 1878 | self.qmin_x = min(self.data.x) |
---|
| 1879 | self.qmax_x = max(self.data.x) |
---|
| 1880 | else: |
---|
| 1881 | self.qmin_x = _QMIN_DEFAULT |
---|
| 1882 | self.qmax_x = _QMAX_DEFAULT |
---|
| 1883 | self.num_points = _NPTS_DEFAULT |
---|
| 1884 | self.state.npts = self.num_points |
---|
| 1885 | self.state.qmin = self.qmin_x |
---|
| 1886 | self.state.qmax = self.qmax_x |
---|
| 1887 | #reset the q range values |
---|
| 1888 | self._reset_plotting_range(self.state) |
---|
| 1889 | #Re draw plot |
---|
| 1890 | self._draw_model() |
---|