[c77d859] | 1 | |
---|
[340c2b3] | 2 | import sys |
---|
| 3 | import os |
---|
[c77d859] | 4 | import wx |
---|
| 5 | import numpy |
---|
[a074145] | 6 | import time |
---|
[3370922] | 7 | import copy |
---|
[904168e1] | 8 | import math |
---|
[edd166b] | 9 | import string |
---|
[015d109] | 10 | from sans.guiframe.panel_base import PanelBase |
---|
[340c2b3] | 11 | from wx.lib.scrolledpanel import ScrolledPanel |
---|
[85b3971] | 12 | from sans.guiframe.utils import format_number,check_float |
---|
[3cd5806] | 13 | from sans.guiframe.events import PanelOnFocusEvent |
---|
[4ce74917] | 14 | from sans.guiframe.events import StatusEvent |
---|
[20b228a0] | 15 | from sans.guiframe.events import AppendBookmarkEvent |
---|
[cfc0913] | 16 | import pagestate |
---|
| 17 | from pagestate import PageState |
---|
| 18 | (PageInfoEvent, EVT_PAGE_INFO) = wx.lib.newevent.NewEvent() |
---|
[330573d] | 19 | (PreviousStateEvent, EVT_PREVIOUS_STATE) = wx.lib.newevent.NewEvent() |
---|
| 20 | (NextStateEvent, EVT_NEXT_STATE) = wx.lib.newevent.NewEvent() |
---|
[f72333f] | 21 | |
---|
[e7b1ccf] | 22 | _BOX_WIDTH = 76 |
---|
[6bbeacd4] | 23 | _QMIN_DEFAULT = 0.0005 |
---|
| 24 | _QMAX_DEFAULT = 0.5 |
---|
[6d8053c] | 25 | _NPTS_DEFAULT = 50 |
---|
[9170547] | 26 | #Control panel width |
---|
[6e9976b] | 27 | if sys.platform.count("darwin")==0: |
---|
[b421b1a] | 28 | PANEL_WIDTH = 450 |
---|
[f1aa385] | 29 | FONT_VARIANT = 0 |
---|
[35c9d31] | 30 | ON_MAC = False |
---|
[9170547] | 31 | else: |
---|
[c99a6c5] | 32 | PANEL_WIDTH = 500 |
---|
[f1aa385] | 33 | FONT_VARIANT = 1 |
---|
[35c9d31] | 34 | ON_MAC = True |
---|
[6bbeacd4] | 35 | |
---|
| 36 | |
---|
| 37 | |
---|
[015d109] | 38 | class BasicPage(ScrolledPanel, PanelBase): |
---|
[c77d859] | 39 | """ |
---|
[5062bbf] | 40 | This class provide general structure of fitpanel page |
---|
[c77d859] | 41 | """ |
---|
[b787e68c] | 42 | ## Internal name for the AUI manager |
---|
[6bbeacd4] | 43 | window_name = "Fit Page" |
---|
[b787e68c] | 44 | ## Title to appear on top of the window |
---|
[6bbeacd4] | 45 | window_caption = "Fit Page " |
---|
[59a7f2d] | 46 | |
---|
[6bbeacd4] | 47 | def __init__(self, parent,color='blue', **kwargs): |
---|
[5062bbf] | 48 | """ |
---|
| 49 | """ |
---|
[015d109] | 50 | ScrolledPanel.__init__(self, parent, **kwargs) |
---|
[e030625] | 51 | PanelBase.__init__(self, parent) |
---|
[340c2b3] | 52 | self.SetupScrolling() |
---|
[f1aa385] | 53 | #Set window's font size |
---|
| 54 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
[6bbeacd4] | 55 | |
---|
| 56 | self.SetBackgroundColour(color) |
---|
[c77d859] | 57 | ## parent of the page |
---|
| 58 | self.parent = parent |
---|
[77e23a2] | 59 | ## manager is the fitting plugin |
---|
[c77d859] | 60 | ## owner of the page (fitting plugin) |
---|
[4a7ad5f] | 61 | self.event_owner = None |
---|
[cfc0913] | 62 | ## current model |
---|
[ffa69b6] | 63 | self.model = None |
---|
[cfc0913] | 64 | ## data |
---|
[ffa69b6] | 65 | self.data = None |
---|
[e575db9] | 66 | self.mask = None |
---|
[66ff250] | 67 | self.uid = None |
---|
[0b12abb5] | 68 | ## Q range |
---|
[f95301b] | 69 | self.qmin = None |
---|
| 70 | self.qmax = None |
---|
[6bbeacd4] | 71 | self.qmax_x = _QMAX_DEFAULT |
---|
| 72 | self.qmin_x = _QMIN_DEFAULT |
---|
| 73 | self.npts_x = _NPTS_DEFAULT |
---|
[0b12abb5] | 74 | ## total number of point: float |
---|
| 75 | self.npts = None |
---|
| 76 | ## default fitengine type |
---|
[e3d1423] | 77 | self.engine_type = 'scipy' |
---|
[0b12abb5] | 78 | ## smear default |
---|
| 79 | self.smearer = None |
---|
| 80 | self.current_smearer = None |
---|
| 81 | ## 2D smear accuracy default |
---|
| 82 | self.smear2d_accuracy = 'Low' |
---|
| 83 | ## slit smear: |
---|
| 84 | self.dxl = None |
---|
| 85 | self.dxw = None |
---|
| 86 | ## pinhole smear |
---|
| 87 | self.dx_min = None |
---|
| 88 | self.dx_max = None |
---|
| 89 | |
---|
| 90 | self.disp_cb_dict = {} |
---|
| 91 | |
---|
[ffa69b6] | 92 | self.state = PageState(parent=parent) |
---|
[c77d859] | 93 | ## dictionary containing list of models |
---|
[6bbeacd4] | 94 | self.model_list_box = {} |
---|
| 95 | |
---|
[c77d859] | 96 | ## Data member to store the dispersion object created |
---|
| 97 | self._disp_obj_dict = {} |
---|
[cfc0913] | 98 | ## selected parameters to apply dispersion |
---|
[c77d859] | 99 | self.disp_cb_dict ={} |
---|
[b421b1a] | 100 | |
---|
[997131a] | 101 | ## smearer object |
---|
| 102 | self.smearer = None |
---|
[69f9084] | 103 | self.enable2D = False |
---|
[330573d] | 104 | |
---|
[c77d859] | 105 | ##list of model parameters. each item must have same length |
---|
| 106 | ## each item related to a given parameters |
---|
| 107 | ##[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
[4a7ad5f] | 108 | self.parameters = [] |
---|
[fb59ed9] | 109 | # non-fittable parameter whose value is astring |
---|
| 110 | self.str_parameters = [] |
---|
[c77d859] | 111 | ## list of parameters to fit , must be like self.parameters |
---|
[4a7ad5f] | 112 | self.param_toFit = [] |
---|
[c77d859] | 113 | ## list of looking like parameters but with non fittable parameters info |
---|
[4a7ad5f] | 114 | self.fixed_param = [] |
---|
[c77d859] | 115 | ## list of looking like parameters but with fittable parameters info |
---|
[4a7ad5f] | 116 | self.fittable_param = [] |
---|
[c77d859] | 117 | ##list of dispersion parameters |
---|
[4a7ad5f] | 118 | self.disp_list = [] |
---|
| 119 | self.disp_name = "" |
---|
[ffa69b6] | 120 | |
---|
[780d095] | 121 | ## list of orientation parameters |
---|
[4a7ad5f] | 122 | self.orientation_params = [] |
---|
| 123 | self.orientation_params_disp = [] |
---|
| 124 | if self.model != None: |
---|
| 125 | self.disp_list = self.model.getDispParamList() |
---|
[70c57ebf] | 126 | |
---|
[c77d859] | 127 | ##enable model 2D draw |
---|
[4a7ad5f] | 128 | self.enable2D = False |
---|
[c77d859] | 129 | ## check that the fit range is correct to plot the model again |
---|
[4a7ad5f] | 130 | self.fitrange = True |
---|
[1b69256] | 131 | ## Create memento to save the current state |
---|
[4a7ad5f] | 132 | self.state = PageState(parent=self.parent, |
---|
| 133 | model=self.model, data=self.data) |
---|
[240b9966] | 134 | ## flag to determine if state has change |
---|
[4a7ad5f] | 135 | self.state_change = False |
---|
[71f0373] | 136 | ## save customized array |
---|
[4a7ad5f] | 137 | self.values = [] |
---|
| 138 | self.weights = [] |
---|
[a074145] | 139 | ## retrieve saved state |
---|
[4a7ad5f] | 140 | self.number_saved_state = 0 |
---|
[a074145] | 141 | ## dictionary of saved state |
---|
[4a7ad5f] | 142 | self.saved_states = {} |
---|
[1b69256] | 143 | ## Create context menu for page |
---|
| 144 | self.popUpMenu = wx.Menu() |
---|
[6bbeacd4] | 145 | |
---|
[6d91073] | 146 | id = wx.NewId() |
---|
[fadf925] | 147 | self._keep = wx.MenuItem(self.popUpMenu,id,"Add bookmark", |
---|
[4a7ad5f] | 148 | " Keep the panel status to recall it later") |
---|
[6d91073] | 149 | self.popUpMenu.AppendItem(self._keep) |
---|
[2657df9] | 150 | self._keep.Enable(False) |
---|
[20b228a0] | 151 | self._set_bookmark_flag(False) |
---|
| 152 | self._set_save_flag(False) |
---|
[9bc499b6] | 153 | wx.EVT_MENU(self, id, self.on_bookmark) |
---|
[6d91073] | 154 | self.popUpMenu.AppendSeparator() |
---|
| 155 | |
---|
[60132ef] | 156 | ## Default locations |
---|
| 157 | self._default_save_location = os.getcwd() |
---|
[a074145] | 158 | ## save initial state on context menu |
---|
[1b69256] | 159 | #self.onSave(event=None) |
---|
[a074145] | 160 | self.Bind(wx.EVT_CONTEXT_MENU, self.onContextMenu) |
---|
[2296316] | 161 | |
---|
| 162 | # bind key event |
---|
| 163 | self.Bind(wx.EVT_LEFT_DOWN, self.on_left_down) |
---|
[f72333f] | 164 | |
---|
[cfc0913] | 165 | ## create the basic structure of the panel with empty sizer |
---|
| 166 | self.define_page_structure() |
---|
[c77d859] | 167 | ## drawing Initial dispersion parameters sizer |
---|
| 168 | self.set_dispers_sizer() |
---|
[20b228a0] | 169 | |
---|
[c77d859] | 170 | ## layout |
---|
| 171 | self.set_layout() |
---|
[3c44c66] | 172 | |
---|
[6bbeacd4] | 173 | |
---|
| 174 | |
---|
[3cd5806] | 175 | def on_set_focus(self, event): |
---|
| 176 | """ |
---|
| 177 | """ |
---|
| 178 | if self._manager is not None: |
---|
| 179 | wx.PostEvent(self._manager.parent, PanelOnFocusEvent(panel=self)) |
---|
| 180 | |
---|
[6f2c919] | 181 | class ModelTextCtrl(wx.TextCtrl): |
---|
| 182 | """ |
---|
[5062bbf] | 183 | Text control for model and fit parameters. |
---|
| 184 | Binds the appropriate events for user interactions. |
---|
| 185 | Default callback methods can be overwritten on initialization |
---|
| 186 | |
---|
| 187 | :param kill_focus_callback: callback method for EVT_KILL_FOCUS event |
---|
| 188 | :param set_focus_callback: callback method for EVT_SET_FOCUS event |
---|
| 189 | :param mouse_up_callback: callback method for EVT_LEFT_UP event |
---|
| 190 | :param text_enter_callback: callback method for EVT_TEXT_ENTER event |
---|
| 191 | |
---|
[6f2c919] | 192 | """ |
---|
[39b7019] | 193 | ## Set to True when the mouse is clicked while the whole string is selected |
---|
| 194 | full_selection = False |
---|
| 195 | ## Call back for EVT_SET_FOCUS events |
---|
| 196 | _on_set_focus_callback = None |
---|
| 197 | |
---|
[4a7ad5f] | 198 | def __init__(self, parent, id=-1, |
---|
| 199 | value=wx.EmptyString, |
---|
| 200 | pos=wx.DefaultPosition, |
---|
| 201 | size=wx.DefaultSize, |
---|
| 202 | style=0, |
---|
| 203 | validator=wx.DefaultValidator, |
---|
| 204 | name=wx.TextCtrlNameStr, |
---|
| 205 | kill_focus_callback=None, |
---|
| 206 | set_focus_callback=None, |
---|
| 207 | mouse_up_callback=None, |
---|
| 208 | text_enter_callback = None): |
---|
[7975f2b] | 209 | |
---|
[4a7ad5f] | 210 | wx.TextCtrl.__init__(self, parent, id, value, pos, |
---|
| 211 | size, style, validator, name) |
---|
[6f2c919] | 212 | |
---|
[156a0b2] | 213 | # Bind appropriate events |
---|
| 214 | self._on_set_focus_callback = parent.onSetFocus \ |
---|
| 215 | if set_focus_callback is None else set_focus_callback |
---|
| 216 | self.Bind(wx.EVT_SET_FOCUS, self._on_set_focus) |
---|
[7609f1a] | 217 | self.Bind(wx.EVT_KILL_FOCUS, self._silent_kill_focus \ |
---|
[156a0b2] | 218 | if kill_focus_callback is None else kill_focus_callback) |
---|
[6f2c919] | 219 | self.Bind(wx.EVT_TEXT_ENTER, parent._onparamEnter \ |
---|
| 220 | if text_enter_callback is None else text_enter_callback) |
---|
[35c9d31] | 221 | if not ON_MAC : |
---|
| 222 | self.Bind(wx.EVT_LEFT_UP, self._highlight_text \ |
---|
| 223 | if mouse_up_callback is None else mouse_up_callback) |
---|
[39b7019] | 224 | |
---|
| 225 | def _on_set_focus(self, event): |
---|
| 226 | """ |
---|
[5062bbf] | 227 | Catch when the text control is set in focus to highlight the whole |
---|
| 228 | text if necessary |
---|
| 229 | |
---|
| 230 | :param event: mouse event |
---|
| 231 | |
---|
[39b7019] | 232 | """ |
---|
| 233 | event.Skip() |
---|
| 234 | self.full_selection = True |
---|
| 235 | return self._on_set_focus_callback(event) |
---|
| 236 | |
---|
[35c9d31] | 237 | |
---|
| 238 | |
---|
[39b7019] | 239 | def _highlight_text(self, event): |
---|
| 240 | """ |
---|
[5062bbf] | 241 | Highlight text of a TextCtrl only of no text has be selected |
---|
| 242 | |
---|
| 243 | :param event: mouse event |
---|
| 244 | |
---|
[39b7019] | 245 | """ |
---|
| 246 | # Make sure the mouse event is available to other listeners |
---|
| 247 | event.Skip() |
---|
| 248 | control = event.GetEventObject() |
---|
| 249 | if self.full_selection: |
---|
| 250 | self.full_selection = False |
---|
| 251 | # Check that we have a TextCtrl |
---|
| 252 | if issubclass(control.__class__, wx.TextCtrl): |
---|
| 253 | # Check whether text has been selected, |
---|
| 254 | # if not, select the whole string |
---|
| 255 | (start, end) = control.GetSelection() |
---|
| 256 | if start==end: |
---|
| 257 | control.SetSelection(-1,-1) |
---|
[7609f1a] | 258 | |
---|
| 259 | def _silent_kill_focus(self,event): |
---|
| 260 | """ |
---|
[3c44c66] | 261 | Save the state of the page |
---|
[7609f1a] | 262 | """ |
---|
[3c44c66] | 263 | |
---|
[7609f1a] | 264 | event.Skip() |
---|
| 265 | pass |
---|
| 266 | |
---|
[ffa69b6] | 267 | def set_page_info(self, page_info): |
---|
| 268 | """ |
---|
[5062bbf] | 269 | set some page important information at once |
---|
[ffa69b6] | 270 | """ |
---|
| 271 | ##window_name |
---|
| 272 | self.window_name = page_info.window_name |
---|
| 273 | ##window_caption |
---|
| 274 | self.window_caption = page_info.window_caption |
---|
| 275 | ## manager is the fitting plugin |
---|
[3cd5806] | 276 | self._manager= page_info.manager |
---|
[ffa69b6] | 277 | ## owner of the page (fitting plugin) |
---|
| 278 | self.event_owner= page_info.event_owner |
---|
| 279 | ## current model |
---|
| 280 | self.model = page_info.model |
---|
| 281 | ## data |
---|
| 282 | self.data = page_info.data |
---|
| 283 | ## dictionary containing list of models |
---|
| 284 | self.model_list_box = page_info.model_list_box |
---|
| 285 | ## Data member to store the dispersion object created |
---|
| 286 | self.populate_box(dict=self.model_list_box) |
---|
| 287 | |
---|
[a074145] | 288 | def onContextMenu(self, event): |
---|
| 289 | """ |
---|
[5062bbf] | 290 | Retrieve the state selected state |
---|
[a074145] | 291 | """ |
---|
[6d1235b] | 292 | # Skipping the save state functionality for release 0.9.0 |
---|
[dad49a0] | 293 | #return |
---|
[6d1235b] | 294 | |
---|
[a074145] | 295 | pos = event.GetPosition() |
---|
| 296 | pos = self.ScreenToClient(pos) |
---|
[1b69256] | 297 | |
---|
| 298 | self.PopupMenu(self.popUpMenu, pos) |
---|
| 299 | |
---|
[a074145] | 300 | |
---|
[1b69256] | 301 | def onUndo(self, event): |
---|
| 302 | """ |
---|
[5062bbf] | 303 | Cancel the previous action |
---|
[1b69256] | 304 | """ |
---|
[330573d] | 305 | event = PreviousStateEvent(page = self) |
---|
| 306 | wx.PostEvent(self.parent, event) |
---|
| 307 | |
---|
[1b69256] | 308 | def onRedo(self, event): |
---|
| 309 | """ |
---|
[5062bbf] | 310 | Restore the previous action cancelled |
---|
[1b69256] | 311 | """ |
---|
[fe496eeb] | 312 | event = NextStateEvent(page= self) |
---|
[330573d] | 313 | wx.PostEvent(self.parent, event) |
---|
[5062bbf] | 314 | |
---|
[c77d859] | 315 | def define_page_structure(self): |
---|
| 316 | """ |
---|
[5062bbf] | 317 | Create empty sizer for a panel |
---|
[c77d859] | 318 | """ |
---|
| 319 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
| 320 | self.sizer0 = wx.BoxSizer(wx.VERTICAL) |
---|
| 321 | self.sizer1 = wx.BoxSizer(wx.VERTICAL) |
---|
| 322 | self.sizer2 = wx.BoxSizer(wx.VERTICAL) |
---|
| 323 | self.sizer3 = wx.BoxSizer(wx.VERTICAL) |
---|
| 324 | self.sizer4 = wx.BoxSizer(wx.VERTICAL) |
---|
| 325 | self.sizer5 = wx.BoxSizer(wx.VERTICAL) |
---|
[0a518e4c] | 326 | self.sizer6 = wx.BoxSizer(wx.VERTICAL) |
---|
[c77d859] | 327 | |
---|
[9170547] | 328 | self.sizer0.SetMinSize((PANEL_WIDTH,-1)) |
---|
| 329 | self.sizer1.SetMinSize((PANEL_WIDTH,-1)) |
---|
| 330 | self.sizer2.SetMinSize((PANEL_WIDTH,-1)) |
---|
| 331 | self.sizer3.SetMinSize((PANEL_WIDTH,-1)) |
---|
| 332 | self.sizer4.SetMinSize((PANEL_WIDTH,-1)) |
---|
| 333 | self.sizer5.SetMinSize((PANEL_WIDTH,-1)) |
---|
[0b12abb5] | 334 | self.sizer6.SetMinSize((PANEL_WIDTH,-1)) |
---|
[8bd4dc0] | 335 | |
---|
[c77d859] | 336 | self.vbox.Add(self.sizer0) |
---|
| 337 | self.vbox.Add(self.sizer1) |
---|
| 338 | self.vbox.Add(self.sizer2) |
---|
| 339 | self.vbox.Add(self.sizer3) |
---|
| 340 | self.vbox.Add(self.sizer4) |
---|
| 341 | self.vbox.Add(self.sizer5) |
---|
[0b12abb5] | 342 | self.vbox.Add(self.sizer6) |
---|
[a074145] | 343 | |
---|
[c77d859] | 344 | def set_layout(self): |
---|
| 345 | """ |
---|
[5062bbf] | 346 | layout |
---|
[c77d859] | 347 | """ |
---|
| 348 | self.vbox.Layout() |
---|
| 349 | self.vbox.Fit(self) |
---|
| 350 | self.SetSizer(self.vbox) |
---|
| 351 | self.Centre() |
---|
[340c2b3] | 352 | |
---|
[c77d859] | 353 | def set_owner(self,owner): |
---|
| 354 | """ |
---|
[5062bbf] | 355 | set owner of fitpage |
---|
| 356 | |
---|
| 357 | :param owner: the class responsible of plotting |
---|
| 358 | |
---|
[c77d859] | 359 | """ |
---|
| 360 | self.event_owner = owner |
---|
[cfc0913] | 361 | self.state.event_owner = owner |
---|
[7609f1a] | 362 | |
---|
[c8deee5] | 363 | def get_state(self): |
---|
| 364 | """ |
---|
| 365 | """ |
---|
| 366 | return self.state |
---|
[2f189dc] | 367 | def get_data(self): |
---|
| 368 | """ |
---|
[5062bbf] | 369 | return the current data |
---|
[2f189dc] | 370 | """ |
---|
[7609f1a] | 371 | return self.data |
---|
| 372 | |
---|
[c77d859] | 373 | def set_manager(self, manager): |
---|
| 374 | """ |
---|
[5062bbf] | 375 | set panel manager |
---|
| 376 | |
---|
| 377 | :param manager: instance of plugin fitting |
---|
| 378 | |
---|
[c77d859] | 379 | """ |
---|
[3cd5806] | 380 | self._manager = manager |
---|
[cfc0913] | 381 | self.state.manager = manager |
---|
[c77d859] | 382 | |
---|
| 383 | def populate_box(self, dict): |
---|
| 384 | """ |
---|
[5062bbf] | 385 | Store list of model |
---|
| 386 | |
---|
| 387 | :param dict: dictionary containing list of models |
---|
| 388 | |
---|
[c77d859] | 389 | """ |
---|
| 390 | self.model_list_box = dict |
---|
[cfc0913] | 391 | self.state.model_list_box = self.model_list_box |
---|
[6bbeacd4] | 392 | self.initialize_combox() |
---|
[c77d859] | 393 | |
---|
[ffa69b6] | 394 | def initialize_combox(self): |
---|
| 395 | """ |
---|
[5062bbf] | 396 | put default value in the combobox |
---|
[ffa69b6] | 397 | """ |
---|
| 398 | ## fill combox box |
---|
| 399 | if self.model_list_box is None: |
---|
| 400 | return |
---|
[4a7ad5f] | 401 | if len(self.model_list_box) > 0: |
---|
| 402 | self._populate_box(self.formfactorbox, |
---|
| 403 | self.model_list_box["Shapes"]) |
---|
[ffa69b6] | 404 | |
---|
[4a7ad5f] | 405 | if len(self.model_list_box) > 0: |
---|
| 406 | self._populate_box(self.structurebox, |
---|
[ffa69b6] | 407 | self.model_list_box["Structure Factors"]) |
---|
[4a7ad5f] | 408 | self.structurebox.Insert("None", 0, None) |
---|
[ffa69b6] | 409 | self.structurebox.SetSelection(0) |
---|
| 410 | self.structurebox.Hide() |
---|
| 411 | self.text2.Hide() |
---|
| 412 | self.structurebox.Disable() |
---|
| 413 | self.text2.Disable() |
---|
| 414 | |
---|
| 415 | if self.model.__class__ in self.model_list_box["P(Q)*S(Q)"]: |
---|
| 416 | self.structurebox.Show() |
---|
| 417 | self.text2.Show() |
---|
| 418 | self.structurebox.Enable() |
---|
| 419 | self.text2.Enable() |
---|
| 420 | |
---|
[c77d859] | 421 | def set_dispers_sizer(self): |
---|
| 422 | """ |
---|
[5062bbf] | 423 | fill sizer containing dispersity info |
---|
[c77d859] | 424 | """ |
---|
| 425 | self.sizer4.Clear(True) |
---|
[87fbc60] | 426 | name="Polydispersity and Orientational Distribution" |
---|
| 427 | box_description= wx.StaticBox(self, -1,name) |
---|
[c77d859] | 428 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
| 429 | #---------------------------------------------------- |
---|
[4a7ad5f] | 430 | self.disable_disp = wx.RadioButton(self, -1, 'Off', (10, 10), |
---|
| 431 | style=wx.RB_GROUP) |
---|
[fa58441] | 432 | self.enable_disp = wx.RadioButton(self, -1, 'On', (10, 30)) |
---|
[6f23daf] | 433 | # best size for MAC and PC |
---|
| 434 | if ON_MAC: |
---|
| 435 | size_q = (-1, -1) |
---|
| 436 | else: |
---|
| 437 | size_q = (20,15) |
---|
| 438 | self.disp_help_bt = wx.Button(self,wx.NewId(),'?', |
---|
| 439 | style = wx.BU_EXACTFIT, |
---|
| 440 | size=size_q) |
---|
[2296316] | 441 | self.disp_help_bt.Bind(wx.EVT_BUTTON, |
---|
| 442 | self.on_pd_help_clicked,id= self.disp_help_bt.GetId()) |
---|
| 443 | self.disp_help_bt.SetToolTipString("Helps for Polydispersion.") |
---|
[cfc0913] | 444 | |
---|
[4a7ad5f] | 445 | self.Bind(wx.EVT_RADIOBUTTON, self._set_dipers_Param, |
---|
| 446 | id=self.disable_disp.GetId()) |
---|
| 447 | self.Bind(wx.EVT_RADIOBUTTON, self._set_dipers_Param, |
---|
| 448 | id=self.enable_disp.GetId()) |
---|
[ff8f99b] | 449 | #MAC needs SetValue |
---|
| 450 | self.disable_disp.SetValue(True) |
---|
[c77d859] | 451 | sizer_dispersion = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 452 | sizer_dispersion.Add((20,20)) |
---|
[fa58441] | 453 | name=""#Polydispersity and \nOrientational Distribution " |
---|
[a074145] | 454 | sizer_dispersion.Add(wx.StaticText(self,-1,name)) |
---|
[c77d859] | 455 | sizer_dispersion.Add(self.enable_disp ) |
---|
| 456 | sizer_dispersion.Add((20,20)) |
---|
| 457 | sizer_dispersion.Add(self.disable_disp ) |
---|
[2296316] | 458 | sizer_dispersion.Add((25,20)) |
---|
| 459 | sizer_dispersion.Add(self.disp_help_bt) |
---|
[c77d859] | 460 | |
---|
[2296316] | 461 | ## fill a sizer for dispersion |
---|
[997131a] | 462 | boxsizer1.Add( sizer_dispersion,0, |
---|
| 463 | wx.TOP|wx.BOTTOM|wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE,border=5) |
---|
| 464 | #boxsizer1.Add( (10,10) ) |
---|
[2296316] | 465 | #boxsizer1.Add( sizer_select_dispers ) |
---|
| 466 | self.sizer4_4 = wx.GridBagSizer(6,5) |
---|
| 467 | |
---|
[c77d859] | 468 | boxsizer1.Add( self.sizer4_4 ) |
---|
| 469 | #----------------------------------------------------- |
---|
| 470 | self.sizer4.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
[3b605bb] | 471 | self.sizer4_4.Layout() |
---|
[c77d859] | 472 | self.sizer4.Layout() |
---|
[3b605bb] | 473 | self.Layout() |
---|
[340c2b3] | 474 | |
---|
[3b605bb] | 475 | self.Refresh() |
---|
[71f0373] | 476 | ## saving the state of enable dispersity button |
---|
| 477 | self.state.enable_disp= self.enable_disp.GetValue() |
---|
| 478 | self.state.disable_disp= self.disable_disp.GetValue() |
---|
[36cb4d2f] | 479 | self.SetupScrolling() |
---|
[5062bbf] | 480 | |
---|
[a074145] | 481 | |
---|
| 482 | def onResetModel(self, event): |
---|
| 483 | """ |
---|
[5062bbf] | 484 | Reset model state |
---|
[a074145] | 485 | """ |
---|
[20b228a0] | 486 | menu = event.GetEventObject() |
---|
[a074145] | 487 | ## post help message for the selected model |
---|
[20b228a0] | 488 | msg = menu.GetHelpString(event.GetId()) |
---|
[a074145] | 489 | msg +=" reloaded" |
---|
[20b228a0] | 490 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg)) |
---|
[a074145] | 491 | |
---|
[20b228a0] | 492 | name = menu.GetLabel(event.GetId()) |
---|
[f20767b] | 493 | self._on_select_model_helper() |
---|
| 494 | |
---|
[a074145] | 495 | if name in self.saved_states.keys(): |
---|
| 496 | previous_state = self.saved_states[name] |
---|
[f20767b] | 497 | ## reset state of checkbox,textcrtl and regular parameters value |
---|
[2296316] | 498 | self.reset_page(previous_state) |
---|
[fadf925] | 499 | |
---|
[2296316] | 500 | |
---|
[aad74b3] | 501 | def on_preview(self, event): |
---|
[2296316] | 502 | """ |
---|
| 503 | Report the current fit results |
---|
| 504 | """ |
---|
| 505 | # Get plot image from plotpanel |
---|
| 506 | images, canvases = self.get_images() |
---|
| 507 | # get the report dialog |
---|
| 508 | self.state.report(images, canvases) |
---|
| 509 | |
---|
[9bc499b6] | 510 | |
---|
[3cd5806] | 511 | def on_save(self, event): |
---|
[9bc499b6] | 512 | """ |
---|
[5062bbf] | 513 | Save the current state into file |
---|
[bb70474] | 514 | """ |
---|
| 515 | self.save_current_state() |
---|
| 516 | new_state = self.state.clone() |
---|
| 517 | # Ask the user the location of the file to write to. |
---|
| 518 | path = None |
---|
| 519 | dlg = wx.FileDialog(self, "Choose a file", self._default_save_location, |
---|
| 520 | "", "*.fitv", wx.SAVE) |
---|
| 521 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 522 | path = dlg.GetPath() |
---|
| 523 | self._default_save_location = os.path.dirname(path) |
---|
| 524 | else: |
---|
| 525 | return None |
---|
[23cdeab] | 526 | # MAC always needs the extension for saving |
---|
| 527 | extens = ".fitv" |
---|
| 528 | # Make sure the ext included in the file name |
---|
| 529 | fName = os.path.splitext(path)[0] + extens |
---|
[bb70474] | 530 | #the manager write the state into file |
---|
[23cdeab] | 531 | self._manager.save_fit_state(filepath=fName, fitstate=new_state) |
---|
[bb70474] | 532 | return new_state |
---|
[0b12abb5] | 533 | |
---|
[20b228a0] | 534 | def _get_time_stamp(self): |
---|
| 535 | """ |
---|
| 536 | return time and date stings |
---|
| 537 | """ |
---|
| 538 | # date and time |
---|
| 539 | year, month, day,hour,minute,second,tda,ty,tm_isdst= time.localtime() |
---|
| 540 | current_time= str(hour)+":"+str(minute)+":"+str(second) |
---|
| 541 | current_date= str( month)+"/"+str(day)+"/"+str(year) |
---|
| 542 | return current_time, current_date |
---|
| 543 | |
---|
[9bc499b6] | 544 | def on_bookmark(self, event): |
---|
[00c3aac] | 545 | """ |
---|
[5062bbf] | 546 | save history of the data and model |
---|
[00c3aac] | 547 | """ |
---|
[a074145] | 548 | if self.model==None: |
---|
[4470b10] | 549 | msg="Can not bookmark; Please select Data and Model first..." |
---|
| 550 | wx.MessageBox(msg, 'Info') |
---|
[a074145] | 551 | return |
---|
[0b12abb5] | 552 | self.save_current_state() |
---|
| 553 | new_state = self.state.clone() |
---|
| 554 | ##Add model state on context menu |
---|
| 555 | self.number_saved_state += 1 |
---|
[20b228a0] | 556 | current_time, current_date = self._get_time_stamp() |
---|
[0b12abb5] | 557 | #name= self.model.name+"[%g]"%self.number_saved_state |
---|
[20b228a0] | 558 | name = "Fitting: %g]" % self.number_saved_state |
---|
| 559 | name += self.model.__class__.__name__ |
---|
| 560 | name += "bookmarked at %s on %s" % (current_time, current_date) |
---|
[0b12abb5] | 561 | self.saved_states[name]= new_state |
---|
| 562 | |
---|
| 563 | ## Add item in the context menu |
---|
[20b228a0] | 564 | msg = "Model saved at %s on %s"%(current_time, current_date) |
---|
[0b12abb5] | 565 | ## post help message for the selected model |
---|
| 566 | msg +=" Saved! right click on this page to retrieve this model" |
---|
| 567 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
| 568 | |
---|
[1258aa3] | 569 | id = wx.NewId() |
---|
| 570 | self.popUpMenu.Append(id,name,str(msg)) |
---|
| 571 | wx.EVT_MENU(self, id, self.onResetModel) |
---|
[20b228a0] | 572 | wx.PostEvent(self.parent.parent, |
---|
| 573 | AppendBookmarkEvent(title=name, |
---|
[95b2bad] | 574 | hint=str(msg), handler=self._back_to_bookmark)) |
---|
| 575 | def _back_to_bookmark(self, event): |
---|
| 576 | """ |
---|
| 577 | Back to bookmark |
---|
| 578 | """ |
---|
| 579 | self._manager.on_perspective(event) |
---|
| 580 | self.onResetModel(event) |
---|
[0b12abb5] | 581 | def old_on_bookmark(self, event): |
---|
| 582 | """ |
---|
| 583 | save history of the data and model |
---|
| 584 | """ |
---|
| 585 | if self.model==None: |
---|
| 586 | msg="Can not bookmark; Please select Data and Model first..." |
---|
| 587 | wx.MessageBox(msg, 'Info') |
---|
| 588 | return |
---|
[a074145] | 589 | if hasattr(self,"enable_disp"): |
---|
[fc6ea43] | 590 | self.state.enable_disp = copy.deepcopy(self.enable_disp.GetValue()) |
---|
[a074145] | 591 | if hasattr(self, "disp_box"): |
---|
[fc6ea43] | 592 | self.state.disp_box = copy.deepcopy(self.disp_box.GetSelection()) |
---|
[edd166b] | 593 | |
---|
[f20767b] | 594 | self.state.model.name= self.model.name |
---|
[c99a6c5] | 595 | |
---|
| 596 | #Remember fit engine_type for fit panel |
---|
[edd166b] | 597 | if self.engine_type == None: |
---|
| 598 | self.engine_type = "scipy" |
---|
[3cd5806] | 599 | if self._manager !=None: |
---|
| 600 | self._manager._on_change_engine(engine=self.engine_type) |
---|
[c99a6c5] | 601 | |
---|
[edd166b] | 602 | self.state.engine_type = self.engine_type |
---|
[b421b1a] | 603 | |
---|
[a074145] | 604 | new_state = self.state.clone() |
---|
[f20767b] | 605 | new_state.model.name = self.state.model.name |
---|
| 606 | |
---|
[c477b31] | 607 | new_state.enable2D = copy.deepcopy(self.enable2D) |
---|
[a074145] | 608 | ##Add model state on context menu |
---|
| 609 | self.number_saved_state += 1 |
---|
[cd57979a] | 610 | #name= self.model.name+"[%g]"%self.number_saved_state |
---|
| 611 | name= self.model.__class__.__name__+"[%g]"%self.number_saved_state |
---|
[a074145] | 612 | self.saved_states[name]= new_state |
---|
| 613 | |
---|
| 614 | ## Add item in the context menu |
---|
[fc6ea43] | 615 | |
---|
| 616 | year, month, day,hour,minute,second,tda,ty,tm_isdst= time.localtime() |
---|
| 617 | my_time= str(hour)+" : "+str(minute)+" : "+str(second)+" " |
---|
[a074145] | 618 | date= str( month)+"|"+str(day)+"|"+str(year) |
---|
| 619 | msg= "Model saved at %s on %s"%(my_time, date) |
---|
| 620 | ## post help message for the selected model |
---|
| 621 | msg +=" Saved! right click on this page to retrieve this model" |
---|
| 622 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
| 623 | |
---|
| 624 | id = wx.NewId() |
---|
[1b69256] | 625 | self.popUpMenu.Append(id,name,str(msg)) |
---|
[a074145] | 626 | wx.EVT_MENU(self, id, self.onResetModel) |
---|
| 627 | |
---|
[1328e03] | 628 | def onSetFocus(self, evt): |
---|
[77e23a2] | 629 | """ |
---|
[5062bbf] | 630 | highlight the current textcrtl and hide the error text control shown |
---|
| 631 | after fitting |
---|
[77e23a2] | 632 | """ |
---|
[1328e03] | 633 | return |
---|
[c77d859] | 634 | |
---|
| 635 | def read_file(self, path): |
---|
| 636 | """ |
---|
[5062bbf] | 637 | Read two columns file |
---|
| 638 | |
---|
| 639 | :param path: the path to the file to read |
---|
| 640 | |
---|
[c77d859] | 641 | """ |
---|
| 642 | try: |
---|
| 643 | if path==None: |
---|
| 644 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
| 645 | " Selected Distribution was not loaded: %s"%path)) |
---|
| 646 | return None, None |
---|
| 647 | input_f = open(path, 'r') |
---|
| 648 | buff = input_f.read() |
---|
| 649 | lines = buff.split('\n') |
---|
| 650 | |
---|
| 651 | angles = [] |
---|
| 652 | weights=[] |
---|
| 653 | for line in lines: |
---|
| 654 | toks = line.split() |
---|
[502de0a] | 655 | try: |
---|
| 656 | angle = float(toks[0]) |
---|
| 657 | weight = float(toks[1]) |
---|
| 658 | except: |
---|
| 659 | # Skip non-data lines |
---|
| 660 | pass |
---|
| 661 | angles.append(angle) |
---|
| 662 | weights.append(weight) |
---|
[c77d859] | 663 | return numpy.array(angles), numpy.array(weights) |
---|
| 664 | except: |
---|
| 665 | raise |
---|
[5062bbf] | 666 | |
---|
[cfc0913] | 667 | def createMemento(self): |
---|
| 668 | """ |
---|
[5062bbf] | 669 | return the current state of the page |
---|
[cfc0913] | 670 | """ |
---|
| 671 | return self.state.clone() |
---|
| 672 | |
---|
| 673 | |
---|
| 674 | def save_current_state(self): |
---|
| 675 | """ |
---|
[5062bbf] | 676 | Store current state |
---|
[cfc0913] | 677 | """ |
---|
[e3d1423] | 678 | self.state.engine_type = copy.deepcopy(self.engine_type) |
---|
[0aeabc6] | 679 | ## save model option |
---|
[87fbc60] | 680 | if self.model!= None: |
---|
[71f0373] | 681 | self.disp_list= self.model.getDispParamList() |
---|
| 682 | self.state.disp_list= copy.deepcopy(self.disp_list) |
---|
[87fbc60] | 683 | self.state.model = self.model.clone() |
---|
[0b12abb5] | 684 | #save radiobutton state for model selection |
---|
| 685 | self.state.shape_rbutton = self.shape_rbutton.GetValue() |
---|
| 686 | self.state.shape_indep_rbutton = self.shape_indep_rbutton.GetValue() |
---|
| 687 | self.state.struct_rbutton = self.struct_rbutton.GetValue() |
---|
| 688 | self.state.plugin_rbutton = self.plugin_rbutton.GetValue() |
---|
| 689 | #model combobox |
---|
| 690 | self.state.structurebox = self.structurebox.GetSelection() |
---|
| 691 | self.state.formfactorbox = self.formfactorbox.GetSelection() |
---|
| 692 | |
---|
[c477b31] | 693 | self.state.enable2D = copy.deepcopy(self.enable2D) |
---|
[71f0373] | 694 | self.state.values= copy.deepcopy(self.values) |
---|
| 695 | self.state.weights = copy.deepcopy( self.weights) |
---|
[0aeabc6] | 696 | ## save data |
---|
[240b9966] | 697 | self.state.data= copy.deepcopy(self.data) |
---|
[0b12abb5] | 698 | self.state.qmax_x = self.qmax_x |
---|
| 699 | self.state.qmin_x = self.qmin_x |
---|
| 700 | |
---|
[cfc0913] | 701 | if hasattr(self,"enable_disp"): |
---|
| 702 | self.state.enable_disp= self.enable_disp.GetValue() |
---|
[fc6ea43] | 703 | self.state.disable_disp = self.disable_disp.GetValue() |
---|
| 704 | |
---|
[3370922] | 705 | self.state.smearer = copy.deepcopy(self.smearer) |
---|
[cfc0913] | 706 | if hasattr(self,"enable_smearer"): |
---|
[4a7ad5f] | 707 | self.state.enable_smearer = \ |
---|
| 708 | copy.deepcopy(self.enable_smearer.GetValue()) |
---|
| 709 | self.state.disable_smearer = \ |
---|
| 710 | copy.deepcopy(self.disable_smearer.GetValue()) |
---|
[7609f1a] | 711 | |
---|
[4a7ad5f] | 712 | self.state.pinhole_smearer = \ |
---|
| 713 | copy.deepcopy(self.pinhole_smearer.GetValue()) |
---|
[7609f1a] | 714 | self.state.slit_smearer = copy.deepcopy(self.slit_smearer.GetValue()) |
---|
| 715 | |
---|
[2296316] | 716 | if len(self._disp_obj_dict)>0: |
---|
| 717 | for k , v in self._disp_obj_dict.iteritems(): |
---|
| 718 | self.state._disp_obj_dict[k]= v |
---|
[f20767b] | 719 | |
---|
| 720 | |
---|
[71f0373] | 721 | self.state.values = copy.deepcopy(self.values) |
---|
| 722 | self.state.weights = copy.deepcopy(self.weights) |
---|
[0aeabc6] | 723 | ## save plotting range |
---|
[b787e68c] | 724 | self._save_plotting_range() |
---|
[d2d0cfdf] | 725 | |
---|
[2296316] | 726 | self.state.orientation_params = [] |
---|
| 727 | self.state.orientation_params_disp = [] |
---|
| 728 | self.state.parameters = [] |
---|
| 729 | self.state.fittable_param = [] |
---|
| 730 | self.state.fixed_param = [] |
---|
| 731 | self.state.str_parameters = [] |
---|
[f20767b] | 732 | |
---|
[d2d0cfdf] | 733 | |
---|
[cfc0913] | 734 | ## save checkbutton state and txtcrtl values |
---|
[2296316] | 735 | self._copy_parameters_state(self.str_parameters, |
---|
| 736 | self.state.str_parameters) |
---|
[fc6ea43] | 737 | self._copy_parameters_state(self.orientation_params, |
---|
| 738 | self.state.orientation_params) |
---|
| 739 | self._copy_parameters_state(self.orientation_params_disp, |
---|
| 740 | self.state.orientation_params_disp) |
---|
[f20767b] | 741 | |
---|
[d2d0cfdf] | 742 | self._copy_parameters_state(self.parameters, self.state.parameters) |
---|
[4a7ad5f] | 743 | self._copy_parameters_state(self.fittable_param, |
---|
| 744 | self.state.fittable_param) |
---|
[d2d0cfdf] | 745 | self._copy_parameters_state(self.fixed_param, self.state.fixed_param) |
---|
[3c44c66] | 746 | #save chisqr |
---|
| 747 | self.state.tcChi = self.tcChi.GetValue() |
---|
| 748 | |
---|
[52cac46] | 749 | def save_current_state_fit(self): |
---|
| 750 | """ |
---|
[5062bbf] | 751 | Store current state for fit_page |
---|
[52cac46] | 752 | """ |
---|
| 753 | ## save model option |
---|
| 754 | if self.model!= None: |
---|
| 755 | self.disp_list= self.model.getDispParamList() |
---|
| 756 | self.state.disp_list= copy.deepcopy(self.disp_list) |
---|
| 757 | self.state.model = self.model.clone() |
---|
[9bc499b6] | 758 | if hasattr(self, "engine_type"): |
---|
| 759 | self.state.engine_type = copy.deepcopy(self.engine_type) |
---|
| 760 | |
---|
[52cac46] | 761 | self.state.enable2D = copy.deepcopy(self.enable2D) |
---|
| 762 | self.state.values= copy.deepcopy(self.values) |
---|
| 763 | self.state.weights = copy.deepcopy( self.weights) |
---|
| 764 | ## save data |
---|
| 765 | self.state.data= copy.deepcopy(self.data) |
---|
| 766 | |
---|
| 767 | if hasattr(self,"enable_disp"): |
---|
| 768 | self.state.enable_disp= self.enable_disp.GetValue() |
---|
| 769 | self.state.disable_disp = self.disable_disp.GetValue() |
---|
| 770 | |
---|
| 771 | self.state.smearer = copy.deepcopy(self.smearer) |
---|
| 772 | if hasattr(self,"enable_smearer"): |
---|
[4a7ad5f] | 773 | self.state.enable_smearer = \ |
---|
| 774 | copy.deepcopy(self.enable_smearer.GetValue()) |
---|
| 775 | self.state.disable_smearer = \ |
---|
| 776 | copy.deepcopy(self.disable_smearer.GetValue()) |
---|
[52cac46] | 777 | |
---|
[4a7ad5f] | 778 | self.state.pinhole_smearer = \ |
---|
| 779 | copy.deepcopy(self.pinhole_smearer.GetValue()) |
---|
[7609f1a] | 780 | self.state.slit_smearer = copy.deepcopy(self.slit_smearer.GetValue()) |
---|
| 781 | |
---|
[52cac46] | 782 | if hasattr(self,"disp_box"): |
---|
| 783 | self.state.disp_box = self.disp_box.GetCurrentSelection() |
---|
| 784 | |
---|
[4a7ad5f] | 785 | if len(self.disp_cb_dict) > 0: |
---|
| 786 | for k, v in self.disp_cb_dict.iteritems(): |
---|
[52cac46] | 787 | |
---|
[4a7ad5f] | 788 | if v == None : |
---|
| 789 | self.state.disp_cb_dict[k] = v |
---|
[52cac46] | 790 | else: |
---|
| 791 | try: |
---|
[4a7ad5f] | 792 | self.state.disp_cb_dict[k] = v.GetValue() |
---|
[52cac46] | 793 | except: |
---|
[4a7ad5f] | 794 | self.state.disp_cb_dict[k] = None |
---|
[52cac46] | 795 | |
---|
[4a7ad5f] | 796 | if len(self._disp_obj_dict) > 0: |
---|
[52cac46] | 797 | for k , v in self._disp_obj_dict.iteritems(): |
---|
| 798 | |
---|
[4a7ad5f] | 799 | self.state._disp_obj_dict[k] = v |
---|
[52cac46] | 800 | |
---|
| 801 | |
---|
| 802 | self.state.values = copy.deepcopy(self.values) |
---|
| 803 | self.state.weights = copy.deepcopy(self.weights) |
---|
[b421b1a] | 804 | |
---|
[52cac46] | 805 | ## save plotting range |
---|
| 806 | self._save_plotting_range() |
---|
| 807 | |
---|
| 808 | ## save checkbutton state and txtcrtl values |
---|
| 809 | self._copy_parameters_state(self.orientation_params, |
---|
| 810 | self.state.orientation_params) |
---|
| 811 | self._copy_parameters_state(self.orientation_params_disp, |
---|
| 812 | self.state.orientation_params_disp) |
---|
| 813 | self._copy_parameters_state(self.parameters, self.state.parameters) |
---|
[4a7ad5f] | 814 | self._copy_parameters_state(self.fittable_param, |
---|
| 815 | self.state.fittable_param) |
---|
[52cac46] | 816 | self._copy_parameters_state(self.fixed_param, self.state.fixed_param) |
---|
[a074145] | 817 | |
---|
[ffa69b6] | 818 | |
---|
| 819 | def check_invalid_panel(self): |
---|
| 820 | """ |
---|
[5062bbf] | 821 | check if the user can already perform some action with this panel |
---|
[ffa69b6] | 822 | """ |
---|
| 823 | flag = False |
---|
[4470b10] | 824 | if self.data is None: |
---|
| 825 | self.disable_smearer.SetValue(True) |
---|
| 826 | self.disable_disp.SetValue(True) |
---|
| 827 | msg = "Please load Data and select Model to start..." |
---|
[ffa69b6] | 828 | wx.MessageBox(msg, 'Info') |
---|
| 829 | return True |
---|
[5062bbf] | 830 | |
---|
[e88ebfd] | 831 | def set_model_state(self, state): |
---|
| 832 | """ |
---|
| 833 | reset page given a model state |
---|
| 834 | """ |
---|
| 835 | self.disp_cb_dict = state.disp_cb_dict |
---|
| 836 | self.disp_list = state.disp_list |
---|
| 837 | |
---|
| 838 | ## set the state of the radio box |
---|
| 839 | self.shape_rbutton.SetValue(state.shape_rbutton ) |
---|
| 840 | self.shape_indep_rbutton.SetValue(state.shape_indep_rbutton) |
---|
| 841 | self.struct_rbutton.SetValue(state.struct_rbutton) |
---|
| 842 | self.plugin_rbutton.SetValue(state.plugin_rbutton) |
---|
| 843 | |
---|
| 844 | ## fill model combobox |
---|
| 845 | self._show_combox_helper() |
---|
| 846 | #select the current model |
---|
| 847 | self.formfactorbox.Select(int(state.formfactorcombobox)) |
---|
| 848 | self.structurebox.SetSelection(state.structurecombobox ) |
---|
| 849 | if state.multi_factor != None: |
---|
| 850 | self.multifactorbox.SetSelection(state.multi_factor) |
---|
| 851 | |
---|
| 852 | ## reset state of checkbox,textcrtl and regular parameters value |
---|
| 853 | self._reset_parameters_state(self.orientation_params_disp, |
---|
| 854 | state.orientation_params_disp) |
---|
| 855 | self._reset_parameters_state(self.orientation_params, |
---|
| 856 | state.orientation_params) |
---|
| 857 | self._reset_parameters_state(self.str_parameters, |
---|
| 858 | state.str_parameters) |
---|
| 859 | self._reset_parameters_state(self.parameters,state.parameters) |
---|
| 860 | ## display dispersion info layer |
---|
| 861 | self.enable_disp.SetValue(state.enable_disp) |
---|
| 862 | self.disable_disp.SetValue(state.disable_disp) |
---|
| 863 | |
---|
| 864 | if hasattr(self, "disp_box"): |
---|
| 865 | |
---|
| 866 | self.disp_box.SetSelection(state.disp_box) |
---|
| 867 | n= self.disp_box.GetCurrentSelection() |
---|
| 868 | dispersity= self.disp_box.GetClientData(n) |
---|
| 869 | name = dispersity.__name__ |
---|
| 870 | |
---|
| 871 | self._set_dipers_Param(event=None) |
---|
| 872 | |
---|
| 873 | if name == "ArrayDispersion": |
---|
| 874 | |
---|
| 875 | for item in self.disp_cb_dict.keys(): |
---|
| 876 | |
---|
| 877 | if hasattr(self.disp_cb_dict[item], "SetValue") : |
---|
| 878 | self.disp_cb_dict[item].SetValue(\ |
---|
| 879 | state.disp_cb_dict[item]) |
---|
| 880 | # Create the dispersion objects |
---|
| 881 | from sans.models.dispersion_models import ArrayDispersion |
---|
| 882 | disp_model = ArrayDispersion() |
---|
| 883 | if hasattr(state,"values")and\ |
---|
| 884 | self.disp_cb_dict[item].GetValue() == True: |
---|
| 885 | if len(state.values)>0: |
---|
| 886 | self.values=state.values |
---|
| 887 | self.weights=state.weights |
---|
| 888 | disp_model.set_weights(self.values, |
---|
| 889 | state.weights) |
---|
| 890 | else: |
---|
| 891 | self._reset_dispersity() |
---|
| 892 | |
---|
| 893 | self._disp_obj_dict[item] = disp_model |
---|
| 894 | # Set the new model as the dispersion object |
---|
| 895 | #for the selected parameter |
---|
| 896 | self.model.set_dispersion(item, disp_model) |
---|
| 897 | |
---|
| 898 | self.model._persistency_dict[item] = \ |
---|
| 899 | [state.values, state.weights] |
---|
| 900 | |
---|
| 901 | else: |
---|
| 902 | keys = self.model.getParamList() |
---|
| 903 | for item in keys: |
---|
| 904 | if item in self.disp_list and \ |
---|
| 905 | not self.model.details.has_key(item): |
---|
| 906 | self.model.details[item] = ["", None, None] |
---|
| 907 | for k,v in self.state.disp_cb_dict.iteritems(): |
---|
| 908 | self.disp_cb_dict = copy.deepcopy(state.disp_cb_dict) |
---|
| 909 | self.state.disp_cb_dict = copy.deepcopy(state.disp_cb_dict) |
---|
| 910 | ## smearing info restore |
---|
| 911 | if hasattr(self, "enable_smearer"): |
---|
| 912 | ## set smearing value whether or not the data |
---|
| 913 | #contain the smearing info |
---|
| 914 | self.enable_smearer.SetValue(state.enable_smearer) |
---|
| 915 | self.disable_smearer.SetValue(state.disable_smearer) |
---|
| 916 | self.onSmear(event=None) |
---|
| 917 | self.pinhole_smearer.SetValue(state.pinhole_smearer) |
---|
| 918 | self.slit_smearer.SetValue(state.slit_smearer) |
---|
| 919 | ## we have two more options for smearing |
---|
| 920 | if self.pinhole_smearer.GetValue(): self.onPinholeSmear(event=None) |
---|
| 921 | elif self.slit_smearer.GetValue(): self.onSlitSmear(event=None) |
---|
| 922 | |
---|
| 923 | ## reset state of checkbox,textcrtl and dispersity parameters value |
---|
| 924 | self._reset_parameters_state(self.fittable_param,state.fittable_param) |
---|
| 925 | self._reset_parameters_state(self.fixed_param,state.fixed_param) |
---|
| 926 | |
---|
| 927 | ## draw the model with previous parameters value |
---|
| 928 | self._onparamEnter_helper() |
---|
| 929 | self.select_param(event=None) |
---|
| 930 | #Save state_fit |
---|
| 931 | self.save_current_state_fit() |
---|
| 932 | self._lay_out() |
---|
| 933 | self.Refresh() |
---|
| 934 | |
---|
[240b9966] | 935 | def reset_page_helper(self, state): |
---|
[cfc0913] | 936 | """ |
---|
[5062bbf] | 937 | Use page_state and change the state of existing page |
---|
| 938 | |
---|
| 939 | :precondition: the page is already drawn or created |
---|
| 940 | |
---|
| 941 | :postcondition: the state of the underlying data change as well as the |
---|
[71f0373] | 942 | state of the graphic interface |
---|
[cfc0913] | 943 | """ |
---|
[4a7ad5f] | 944 | if state == None: |
---|
[3a37fe0] | 945 | #self._undo.Enable(False) |
---|
[330573d] | 946 | return |
---|
[2296316] | 947 | # set data, etc. from the state |
---|
[2657df9] | 948 | # reset page between theory and fitting from bookmarking |
---|
| 949 | #if state.data == None: |
---|
| 950 | # data = None |
---|
| 951 | #else: |
---|
| 952 | data = state.data |
---|
| 953 | |
---|
| 954 | #if data != None: |
---|
| 955 | |
---|
| 956 | if data == None: |
---|
| 957 | data_min = state.qmin |
---|
| 958 | data_max = state.qmax |
---|
| 959 | self.qmin_x = data_min |
---|
| 960 | self.qmax_x = data_max |
---|
| 961 | self.minimum_q.SetValue(str(data_min)) |
---|
| 962 | self.maximum_q.SetValue(str(data_max)) |
---|
| 963 | self.qmin.SetValue(str(data_min)) |
---|
| 964 | self.qmax.SetValue(str(data_max)) |
---|
| 965 | |
---|
| 966 | self.state.data = data |
---|
| 967 | self.state.qmin = self.qmin_x |
---|
| 968 | self.state.qmax = self.qmax_x |
---|
| 969 | else: |
---|
| 970 | self.set_data(data) |
---|
| 971 | |
---|
[0b12abb5] | 972 | self.enable2D= state.enable2D |
---|
| 973 | self.engine_type = state.engine_type |
---|
| 974 | |
---|
| 975 | self.disp_cb_dict = state.disp_cb_dict |
---|
| 976 | self.disp_list = state.disp_list |
---|
[e3d1423] | 977 | |
---|
[0b12abb5] | 978 | ## set the state of the radio box |
---|
| 979 | self.shape_rbutton.SetValue(state.shape_rbutton ) |
---|
| 980 | self.shape_indep_rbutton.SetValue(state.shape_indep_rbutton) |
---|
[e3d1423] | 981 | self.struct_rbutton.SetValue(state.struct_rbutton) |
---|
[0b12abb5] | 982 | self.plugin_rbutton.SetValue(state.plugin_rbutton) |
---|
| 983 | |
---|
| 984 | ## fill model combobox |
---|
| 985 | self._show_combox_helper() |
---|
| 986 | #select the current model |
---|
[e3d1423] | 987 | self.formfactorbox.Select(int(state.formfactorcombobox)) |
---|
[0b12abb5] | 988 | self.structurebox.SetSelection(state.structurecombobox ) |
---|
[4523b68] | 989 | if state.multi_factor != None: |
---|
[cf6a192] | 990 | self.multifactorbox.SetSelection(state.multi_factor) |
---|
[4523b68] | 991 | |
---|
[0b12abb5] | 992 | #reset the fitting engine type |
---|
| 993 | self.engine_type = state.engine_type |
---|
| 994 | #draw the pnael according to the new model parameter |
---|
| 995 | self._on_select_model(event=None) |
---|
[2657df9] | 996 | # take care of 2D button |
---|
| 997 | if data == None and self.model_view.IsEnabled(): |
---|
| 998 | if self.enable2D: |
---|
| 999 | self.model_view.SetLabel("Switch to 1D") |
---|
| 1000 | else: |
---|
| 1001 | self.model_view.SetLabel("Switch to 2D") |
---|
| 1002 | # else: |
---|
| 1003 | |
---|
[3cd5806] | 1004 | if self._manager !=None: |
---|
| 1005 | self._manager._on_change_engine(engine=self.engine_type) |
---|
[0b12abb5] | 1006 | ## set the select all check box to the a given state |
---|
| 1007 | self.cb1.SetValue(state.cb1) |
---|
| 1008 | |
---|
| 1009 | ## reset state of checkbox,textcrtl and regular parameters value |
---|
| 1010 | self._reset_parameters_state(self.orientation_params_disp, |
---|
| 1011 | state.orientation_params_disp) |
---|
| 1012 | self._reset_parameters_state(self.orientation_params, |
---|
| 1013 | state.orientation_params) |
---|
[fb59ed9] | 1014 | self._reset_parameters_state(self.str_parameters, |
---|
| 1015 | state.str_parameters) |
---|
[0b12abb5] | 1016 | self._reset_parameters_state(self.parameters,state.parameters) |
---|
| 1017 | ## display dispersion info layer |
---|
| 1018 | self.enable_disp.SetValue(state.enable_disp) |
---|
| 1019 | self.disable_disp.SetValue(state.disable_disp) |
---|
[2296316] | 1020 | # If the polydispersion is ON |
---|
| 1021 | if state.enable_disp: |
---|
| 1022 | # reset dispersion according the state |
---|
[0b12abb5] | 1023 | self._set_dipers_Param(event=None) |
---|
[2296316] | 1024 | self._reset_page_disp_helper(state) |
---|
[0b12abb5] | 1025 | ##plotting range restore |
---|
| 1026 | self._reset_plotting_range(state) |
---|
| 1027 | ## smearing info restore |
---|
| 1028 | if hasattr(self, "enable_smearer"): |
---|
[4a7ad5f] | 1029 | ## set smearing value whether or not the data |
---|
| 1030 | #contain the smearing info |
---|
[0b12abb5] | 1031 | self.enable_smearer.SetValue(state.enable_smearer) |
---|
| 1032 | self.disable_smearer.SetValue(state.disable_smearer) |
---|
| 1033 | self.onSmear(event=None) |
---|
| 1034 | self.pinhole_smearer.SetValue(state.pinhole_smearer) |
---|
| 1035 | self.slit_smearer.SetValue(state.slit_smearer) |
---|
| 1036 | ## we have two more options for smearing |
---|
| 1037 | if self.pinhole_smearer.GetValue(): self.onPinholeSmear(event=None) |
---|
| 1038 | elif self.slit_smearer.GetValue(): self.onSlitSmear(event=None) |
---|
| 1039 | |
---|
| 1040 | ## reset state of checkbox,textcrtl and dispersity parameters value |
---|
| 1041 | self._reset_parameters_state(self.fittable_param,state.fittable_param) |
---|
| 1042 | self._reset_parameters_state(self.fixed_param,state.fixed_param) |
---|
| 1043 | |
---|
| 1044 | ## draw the model with previous parameters value |
---|
| 1045 | self._onparamEnter_helper() |
---|
[3c44c66] | 1046 | #reset the value of chisqr when not consistent with the value computed |
---|
| 1047 | self.tcChi.SetValue(str(self.state.tcChi)) |
---|
[0b12abb5] | 1048 | ## reset context menu items |
---|
| 1049 | self._reset_context_menu() |
---|
[3c44c66] | 1050 | |
---|
[0b12abb5] | 1051 | ## set the value of the current state to the state given as parameter |
---|
| 1052 | self.state = state.clone() |
---|
| 1053 | |
---|
[2296316] | 1054 | def _reset_page_disp_helper(self, state): |
---|
| 1055 | """ |
---|
| 1056 | Help to rest page for dispersions |
---|
| 1057 | """ |
---|
| 1058 | keys = self.model.getParamList() |
---|
| 1059 | for item in keys: |
---|
| 1060 | if item in self.disp_list and \ |
---|
| 1061 | not self.model.details.has_key(item): |
---|
| 1062 | self.model.details[item] = ["", None, None] |
---|
| 1063 | #for k,v in self.state.disp_cb_dict.iteritems(): |
---|
| 1064 | self.disp_cb_dict = copy.deepcopy(state.disp_cb_dict) |
---|
| 1065 | self.state.disp_cb_dict = copy.deepcopy(state.disp_cb_dict) |
---|
| 1066 | self.values = copy.deepcopy(state.values) |
---|
| 1067 | self.weights = copy.deepcopy(state.weights) |
---|
| 1068 | |
---|
| 1069 | for key, disp in state._disp_obj_dict.iteritems(): |
---|
| 1070 | # From saved file, disp_model can not be sent in model obj. |
---|
| 1071 | # it will be sent as a string here, then converted to model object. |
---|
| 1072 | if disp.__class__.__name__ == 'str': |
---|
| 1073 | com_str = "from sans.models.dispersion_models " |
---|
| 1074 | com_str += "import %s as disp_func" |
---|
| 1075 | exec com_str % disp |
---|
| 1076 | disp_model = disp_func() |
---|
[f20767b] | 1077 | else: |
---|
[2296316] | 1078 | disp_model = disp |
---|
[edd166b] | 1079 | |
---|
[2296316] | 1080 | self._disp_obj_dict[key] = disp_model |
---|
| 1081 | param_name = key.split('.')[0] |
---|
| 1082 | # Try to set dispersion only when available |
---|
| 1083 | # for eg., pass the orient. angles for 1D Cal |
---|
| 1084 | try: |
---|
| 1085 | self.model.set_dispersion(param_name, disp_model) |
---|
| 1086 | self.model._persistency_dict[key] = \ |
---|
| 1087 | [state.values, state.weights] |
---|
| 1088 | except: |
---|
| 1089 | pass |
---|
| 1090 | selection = self._find_polyfunc_selection(disp_model) |
---|
| 1091 | for list in self.fittable_param: |
---|
| 1092 | if list[1] == key and list[7] != None: |
---|
| 1093 | list[7].SetSelection(selection) |
---|
| 1094 | # For the array disp_model, set the values and weights |
---|
| 1095 | if selection == 1: |
---|
| 1096 | disp_model.set_weights(self.values[key], |
---|
| 1097 | self.weights[key]) |
---|
| 1098 | try: |
---|
| 1099 | # Diables all fittable params for array |
---|
| 1100 | list[0].SetValue(False) |
---|
| 1101 | list[0].Disable() |
---|
| 1102 | list[2].Disable() |
---|
| 1103 | list[5].Disable() |
---|
| 1104 | list[6].Disable() |
---|
| 1105 | except: |
---|
| 1106 | pass |
---|
| 1107 | # For array, disable all fixed params |
---|
| 1108 | if selection == 1: |
---|
| 1109 | for item in self.fixed_param: |
---|
| 1110 | if item[1].split(".")[0] == key.split(".")[0]: |
---|
| 1111 | # try it and pass it for the orientation for 1D |
---|
| 1112 | try: |
---|
| 1113 | item[2].Disable() |
---|
| 1114 | except: |
---|
| 1115 | pass |
---|
[5582a9f1] | 1116 | |
---|
[2296316] | 1117 | # Make sure the check box updated when all checked |
---|
| 1118 | if self.cb1.GetValue(): |
---|
| 1119 | self.select_all_param(None) |
---|
| 1120 | |
---|
[c77d859] | 1121 | def _selectDlg(self): |
---|
| 1122 | """ |
---|
[5062bbf] | 1123 | open a dialog file to selected the customized dispersity |
---|
[c77d859] | 1124 | """ |
---|
| 1125 | import os |
---|
[60132ef] | 1126 | dlg = wx.FileDialog(self, "Choose a weight file", |
---|
[4a7ad5f] | 1127 | self._default_save_location , "", |
---|
| 1128 | "*.*", wx.OPEN) |
---|
[c77d859] | 1129 | path = None |
---|
| 1130 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 1131 | path = dlg.GetPath() |
---|
| 1132 | dlg.Destroy() |
---|
| 1133 | return path |
---|
[5062bbf] | 1134 | |
---|
[a074145] | 1135 | def _reset_context_menu(self): |
---|
| 1136 | """ |
---|
[5062bbf] | 1137 | reset the context menu |
---|
[a074145] | 1138 | """ |
---|
| 1139 | for name, state in self.state.saved_states.iteritems(): |
---|
| 1140 | self.number_saved_state += 1 |
---|
| 1141 | ## Add item in the context menu |
---|
| 1142 | id = wx.NewId() |
---|
[4a7ad5f] | 1143 | msg = 'Save model and state %g' % self.number_saved_state |
---|
| 1144 | self.popUpMenu.Append(id, name, msg) |
---|
[a074145] | 1145 | wx.EVT_MENU(self, id, self.onResetModel) |
---|
| 1146 | |
---|
[0aeabc6] | 1147 | def _reset_plotting_range(self, state): |
---|
[cfc0913] | 1148 | """ |
---|
[5062bbf] | 1149 | Reset the plotting range to a given state |
---|
[cfc0913] | 1150 | """ |
---|
[6bbeacd4] | 1151 | # if self.check_invalid_panel(): |
---|
| 1152 | # return |
---|
[f95301b] | 1153 | self.qmin.SetValue(str(state.qmin)) |
---|
[0aeabc6] | 1154 | self.qmax.SetValue(str(state.qmax)) |
---|
[0b12abb5] | 1155 | |
---|
[240b9966] | 1156 | def _save_typeOfmodel(self): |
---|
| 1157 | """ |
---|
[5062bbf] | 1158 | save radiobutton containing the type model that can be selected |
---|
[240b9966] | 1159 | """ |
---|
| 1160 | self.state.shape_rbutton = self.shape_rbutton.GetValue() |
---|
| 1161 | self.state.shape_indep_rbutton = self.shape_indep_rbutton.GetValue() |
---|
| 1162 | self.state.struct_rbutton = self.struct_rbutton.GetValue() |
---|
| 1163 | self.state.plugin_rbutton = self.plugin_rbutton.GetValue() |
---|
[3595309d] | 1164 | self.state.structurebox= self.structurebox.GetCurrentSelection() |
---|
| 1165 | self.state.formfactorbox = self.formfactorbox.GetCurrentSelection() |
---|
[c99a6c5] | 1166 | |
---|
[3a37fe0] | 1167 | #self._undo.Enable(True) |
---|
[240b9966] | 1168 | ## post state to fit panel |
---|
| 1169 | event = PageInfoEvent(page = self) |
---|
| 1170 | wx.PostEvent(self.parent, event) |
---|
[3595309d] | 1171 | |
---|
[cfc0913] | 1172 | def _save_plotting_range(self ): |
---|
| 1173 | """ |
---|
[5062bbf] | 1174 | save the state of plotting range |
---|
[cfc0913] | 1175 | """ |
---|
[b787e68c] | 1176 | self.state.qmin = self.qmin_x |
---|
| 1177 | self.state.qmax = self.qmax_x |
---|
[6bbeacd4] | 1178 | self.state.npts = self.npts_x |
---|
[cfc0913] | 1179 | |
---|
[c77d859] | 1180 | def _onparamEnter_helper(self): |
---|
| 1181 | """ |
---|
[5062bbf] | 1182 | check if values entered by the user are changed and valid to replot |
---|
| 1183 | model |
---|
[c77d859] | 1184 | """ |
---|
[51a71a3] | 1185 | # Flag to register when a parameter has changed. |
---|
[7975f2b] | 1186 | is_modified = False |
---|
[4a7ad5f] | 1187 | self.fitrange = True |
---|
[51a71a3] | 1188 | is_2Ddata = False |
---|
[3a37fe0] | 1189 | #self._undo.Enable(True) |
---|
[51a71a3] | 1190 | # check if 2d data |
---|
[4a7ad5f] | 1191 | if self.data.__class__.__name__ == "Data2D": |
---|
[51a71a3] | 1192 | is_2Ddata = True |
---|
[c77d859] | 1193 | if self.model !=None: |
---|
[edd166b] | 1194 | try: |
---|
[4a7ad5f] | 1195 | is_modified = self._check_value_enter(self.fittable_param, |
---|
| 1196 | is_modified) |
---|
| 1197 | is_modified = self._check_value_enter(self.fixed_param, |
---|
| 1198 | is_modified) |
---|
| 1199 | is_modified = self._check_value_enter(self.parameters, |
---|
| 1200 | is_modified) |
---|
[edd166b] | 1201 | except: |
---|
| 1202 | pass |
---|
| 1203 | #if is_modified: |
---|
[c99a6c5] | 1204 | |
---|
[c77d859] | 1205 | # Here we should check whether the boundaries have been modified. |
---|
| 1206 | # If qmin and qmax have been modified, update qmin and qmax and |
---|
| 1207 | # set the is_modified flag to True |
---|
[f95301b] | 1208 | if self._validate_qrange(self.qmin, self.qmax): |
---|
| 1209 | tempmin = float(self.qmin.GetValue()) |
---|
[7975f2b] | 1210 | if tempmin != self.qmin_x: |
---|
| 1211 | self.qmin_x = tempmin |
---|
[c77d859] | 1212 | is_modified = True |
---|
[7975f2b] | 1213 | tempmax = float(self.qmax.GetValue()) |
---|
| 1214 | if tempmax != self.qmax_x: |
---|
| 1215 | self.qmax_x = tempmax |
---|
[c77d859] | 1216 | is_modified = True |
---|
[b787e68c] | 1217 | |
---|
[51a71a3] | 1218 | if is_2Ddata: |
---|
| 1219 | # set mask |
---|
| 1220 | is_modified = self._validate_Npts() |
---|
[247cb58] | 1221 | |
---|
[51a71a3] | 1222 | else: |
---|
| 1223 | self.fitrange = False |
---|
[1a74523] | 1224 | |
---|
[c77d859] | 1225 | ## if any value is modify draw model with new value |
---|
[51a71a3] | 1226 | if not self.fitrange: |
---|
[247cb58] | 1227 | #self.btFit.Disable() |
---|
[51a71a3] | 1228 | if is_2Ddata: self.btEditMask.Disable() |
---|
| 1229 | else: |
---|
[247cb58] | 1230 | #self.btFit.Enable(True) |
---|
[51a71a3] | 1231 | if is_2Ddata: self.btEditMask.Enable(True) |
---|
| 1232 | if is_modified and self.fitrange: |
---|
[1a74523] | 1233 | if self.data == None: |
---|
| 1234 | # Theory case: need to get npts value to draw |
---|
| 1235 | self.npts_x = float(self.Npts_total.GetValue()) |
---|
[240b9966] | 1236 | self.state_change= True |
---|
[c77d859] | 1237 | self._draw_model() |
---|
[7975f2b] | 1238 | self.Refresh() |
---|
| 1239 | return is_modified |
---|
| 1240 | |
---|
[35c9d31] | 1241 | def _update_paramv_on_fit(self): |
---|
| 1242 | """ |
---|
[5062bbf] | 1243 | make sure that update param values just before the fitting |
---|
[35c9d31] | 1244 | """ |
---|
| 1245 | #flag for qmin qmax check values |
---|
[7609f1a] | 1246 | flag = True |
---|
[51a71a3] | 1247 | self.fitrange = True |
---|
[35c9d31] | 1248 | is_modified = False |
---|
[51a71a3] | 1249 | |
---|
[66ff250] | 1250 | #wx.PostEvent(self._manager.parent, StatusEvent(status=" \ |
---|
| 1251 | #updating ... ",type="update")) |
---|
[4fbc93e] | 1252 | |
---|
[35c9d31] | 1253 | ##So make sure that update param values on_Fit. |
---|
| 1254 | #self._undo.Enable(True) |
---|
| 1255 | if self.model !=None: |
---|
| 1256 | ##Check the values |
---|
| 1257 | self._check_value_enter( self.fittable_param ,is_modified) |
---|
| 1258 | self._check_value_enter( self.fixed_param ,is_modified) |
---|
[edd166b] | 1259 | self._check_value_enter( self.parameters ,is_modified) |
---|
[35c9d31] | 1260 | |
---|
| 1261 | # If qmin and qmax have been modified, update qmin and qmax and |
---|
[7975f2b] | 1262 | # Here we should check whether the boundaries have been modified. |
---|
| 1263 | # If qmin and qmax have been modified, update qmin and qmax and |
---|
| 1264 | # set the is_modified flag to True |
---|
[f95301b] | 1265 | self.fitrange = self._validate_qrange(self.qmin, self.qmax) |
---|
[51a71a3] | 1266 | if self.fitrange: |
---|
[f95301b] | 1267 | tempmin = float(self.qmin.GetValue()) |
---|
[7975f2b] | 1268 | if tempmin != self.qmin_x: |
---|
| 1269 | self.qmin_x = tempmin |
---|
| 1270 | tempmax = float(self.qmax.GetValue()) |
---|
| 1271 | if tempmax != self.qmax_x: |
---|
| 1272 | self.qmax_x = tempmax |
---|
[7609f1a] | 1273 | if tempmax == tempmin: |
---|
| 1274 | flag = False |
---|
| 1275 | temp_smearer = None |
---|
| 1276 | if not self.disable_smearer.GetValue(): |
---|
| 1277 | temp_smearer= self.current_smearer |
---|
| 1278 | if self.slit_smearer.GetValue(): |
---|
| 1279 | flag = self.update_slit_smear() |
---|
| 1280 | elif self.pinhole_smearer.GetValue(): |
---|
| 1281 | flag = self.update_pinhole_smear() |
---|
| 1282 | else: |
---|
[6bbeacd4] | 1283 | self._manager.set_smearer(smearer=temp_smearer, |
---|
[66ff250] | 1284 | uid=self.uid, |
---|
[4a7ad5f] | 1285 | qmin=float(self.qmin_x), |
---|
[6bbeacd4] | 1286 | qmax=float(self.qmax_x), |
---|
| 1287 | draw=False) |
---|
[4fbc93e] | 1288 | elif not self._is_2D(): |
---|
[3cd5806] | 1289 | self._manager.set_smearer(smearer=temp_smearer, |
---|
[4a7ad5f] | 1290 | qmin=float(self.qmin_x), |
---|
[66ff250] | 1291 | uid=self.uid, |
---|
[7609f1a] | 1292 | qmax= float(self.qmax_x)) |
---|
[0cf97c5] | 1293 | if self.data != None: |
---|
| 1294 | index_data = ((self.qmin_x <= self.data.x)&\ |
---|
| 1295 | (self.data.x <= self.qmax_x)) |
---|
| 1296 | val = str(len(self.data.x[index_data==True])) |
---|
| 1297 | self.Npts_fit.SetValue(val) |
---|
| 1298 | else: |
---|
| 1299 | # No data in the panel |
---|
| 1300 | try: |
---|
| 1301 | self.npts_x = float(self.Npts_total.GetValue()) |
---|
| 1302 | except: |
---|
| 1303 | flag = False |
---|
| 1304 | return flag |
---|
[51a71a3] | 1305 | flag = True |
---|
[4fbc93e] | 1306 | if self._is_2D(): |
---|
[1a74523] | 1307 | # only 2D case set mask |
---|
[51a71a3] | 1308 | flag = self._validate_Npts() |
---|
| 1309 | if not flag: |
---|
| 1310 | return flag |
---|
[7609f1a] | 1311 | else: flag = False |
---|
| 1312 | else: |
---|
[7975f2b] | 1313 | flag = False |
---|
[51a71a3] | 1314 | |
---|
| 1315 | #For invalid q range, disable the mask editor and fit button, vs. |
---|
| 1316 | if not self.fitrange: |
---|
[247cb58] | 1317 | #self.btFit.Disable() |
---|
[4fbc93e] | 1318 | if self._is_2D():self.btEditMask.Disable() |
---|
[51a71a3] | 1319 | else: |
---|
[247cb58] | 1320 | #self.btFit.Enable(True) |
---|
[4fbc93e] | 1321 | if self._is_2D():self.btEditMask.Enable(True) |
---|
[51a71a3] | 1322 | |
---|
[7609f1a] | 1323 | if not flag: |
---|
[4a7ad5f] | 1324 | msg = "Cannot Plot or Fit :Must select a " |
---|
| 1325 | msg += " model or Fitting range is not valid!!! " |
---|
| 1326 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg)) |
---|
[51a71a3] | 1327 | |
---|
[7609f1a] | 1328 | self.save_current_state() |
---|
[4470b10] | 1329 | |
---|
[35c9d31] | 1330 | return flag |
---|
[acd0bda3] | 1331 | |
---|
[c99a6c5] | 1332 | def _is_modified(self, is_modified): |
---|
[acd0bda3] | 1333 | """ |
---|
[5062bbf] | 1334 | return to self._is_modified |
---|
[acd0bda3] | 1335 | """ |
---|
[c99a6c5] | 1336 | return is_modified |
---|
[acd0bda3] | 1337 | |
---|
[4a7ad5f] | 1338 | def _reset_parameters_state(self, listtorestore, statelist): |
---|
[cfc0913] | 1339 | """ |
---|
[5062bbf] | 1340 | Reset the parameters at the given state |
---|
[cfc0913] | 1341 | """ |
---|
[4a7ad5f] | 1342 | if len(statelist) == 0 or len(listtorestore) == 0: |
---|
[6999659] | 1343 | return |
---|
[4a7ad5f] | 1344 | if len(statelist) != len(listtorestore): |
---|
[cfc0913] | 1345 | return |
---|
[c99a6c5] | 1346 | |
---|
[cfc0913] | 1347 | for j in range(len(listtorestore)): |
---|
| 1348 | item_page = listtorestore[j] |
---|
| 1349 | item_page_info = statelist[j] |
---|
| 1350 | ##change the state of the check box for simple parameters |
---|
[0b12abb5] | 1351 | if item_page[0]!=None: |
---|
[cfc0913] | 1352 | item_page[0].SetValue(item_page_info[0]) |
---|
| 1353 | if item_page[2]!=None: |
---|
| 1354 | item_page[2].SetValue(item_page_info[2]) |
---|
[2296316] | 1355 | if item_page[2].__class__.__name__ == "ComboBox": |
---|
| 1356 | if self.model.fun_list.has_key(item_page_info[2]): |
---|
| 1357 | fun_val = self.model.fun_list[item_page_info[2]] |
---|
| 1358 | self.model.setParam(item_page_info[1],fun_val) |
---|
[cfc0913] | 1359 | if item_page[3]!=None: |
---|
| 1360 | ## show or hide text +/- |
---|
| 1361 | if item_page_info[2]: |
---|
| 1362 | item_page[3].Show(True) |
---|
| 1363 | else: |
---|
| 1364 | item_page[3].Hide() |
---|
| 1365 | if item_page[4]!=None: |
---|
| 1366 | ## show of hide the text crtl for fitting error |
---|
| 1367 | if item_page_info[4][0]: |
---|
| 1368 | item_page[4].Show(True) |
---|
| 1369 | item_page[4].SetValue(item_page_info[4][1]) |
---|
| 1370 | else: |
---|
| 1371 | item_page[3].Hide() |
---|
| 1372 | if item_page[5]!=None: |
---|
| 1373 | ## show of hide the text crtl for fitting error |
---|
[2296316] | 1374 | item_page[5].Show(item_page_info[5][0]) |
---|
| 1375 | item_page[5].SetValue(item_page_info[5][1]) |
---|
| 1376 | |
---|
[cfc0913] | 1377 | if item_page[6]!=None: |
---|
| 1378 | ## show of hide the text crtl for fitting error |
---|
[2296316] | 1379 | item_page[6].Show(item_page_info[6][0]) |
---|
| 1380 | item_page[6].SetValue(item_page_info[6][1]) |
---|
| 1381 | |
---|
| 1382 | |
---|
| 1383 | def _reset_strparam_state(self, listtorestore, statelist): |
---|
| 1384 | """ |
---|
| 1385 | Reset the string parameters at the given state |
---|
| 1386 | """ |
---|
| 1387 | if len(statelist) == 0: |
---|
| 1388 | return |
---|
| 1389 | |
---|
| 1390 | listtorestore = copy.deepcopy(statelist) |
---|
| 1391 | |
---|
| 1392 | for j in range(len(listtorestore)): |
---|
| 1393 | item_page = listtorestore[j] |
---|
| 1394 | item_page_info = statelist[j] |
---|
| 1395 | ##change the state of the check box for simple parameters |
---|
| 1396 | |
---|
| 1397 | if item_page[0] != None: |
---|
| 1398 | item_page[0].SetValue(format_number(item_page_info[0], True)) |
---|
| 1399 | |
---|
| 1400 | if item_page[2] != None: |
---|
| 1401 | param_name = item_page_info[1] |
---|
| 1402 | value = item_page_info[2] |
---|
| 1403 | selection = value |
---|
| 1404 | if self.model.fun_list.has_key(value): |
---|
| 1405 | selection = self.model.fun_list[value] |
---|
| 1406 | item_page[2].SetValue(selection) |
---|
| 1407 | self.model.setParam(param_name, selection) |
---|
[5062bbf] | 1408 | |
---|
[cfc0913] | 1409 | def _copy_parameters_state(self, listtocopy, statelist): |
---|
| 1410 | """ |
---|
[5062bbf] | 1411 | copy the state of button |
---|
| 1412 | |
---|
| 1413 | :param listtocopy: the list of check button to copy |
---|
| 1414 | :param statelist: list of state object to store the current state |
---|
| 1415 | |
---|
[cfc0913] | 1416 | """ |
---|
| 1417 | if len(listtocopy)==0: |
---|
| 1418 | return |
---|
[1c1436d] | 1419 | |
---|
[cfc0913] | 1420 | for item in listtocopy: |
---|
[f20767b] | 1421 | |
---|
[cfc0913] | 1422 | checkbox_state = None |
---|
| 1423 | if item[0]!= None: |
---|
| 1424 | checkbox_state= item[0].GetValue() |
---|
| 1425 | parameter_name = item[1] |
---|
| 1426 | parameter_value = None |
---|
| 1427 | if item[2]!=None: |
---|
| 1428 | parameter_value = item[2].GetValue() |
---|
| 1429 | static_text = None |
---|
| 1430 | if item[3]!=None: |
---|
| 1431 | static_text = item[3].IsShown() |
---|
| 1432 | error_value = None |
---|
| 1433 | error_state = None |
---|
| 1434 | if item[4]!= None: |
---|
| 1435 | error_value = item[4].GetValue() |
---|
| 1436 | error_state = item[4].IsShown() |
---|
| 1437 | |
---|
| 1438 | min_value = None |
---|
| 1439 | min_state = None |
---|
| 1440 | if item[5]!= None: |
---|
| 1441 | min_value = item[5].GetValue() |
---|
| 1442 | min_state = item[5].IsShown() |
---|
| 1443 | |
---|
| 1444 | max_value = None |
---|
| 1445 | max_state = None |
---|
| 1446 | if item[6]!= None: |
---|
| 1447 | max_value = item[6].GetValue() |
---|
| 1448 | max_state = item[6].IsShown() |
---|
[240b9966] | 1449 | unit=None |
---|
| 1450 | if item[7]!=None: |
---|
| 1451 | unit = item[7].GetLabel() |
---|
[cfc0913] | 1452 | |
---|
| 1453 | statelist.append([checkbox_state, parameter_name, parameter_value, |
---|
[4a7ad5f] | 1454 | static_text ,[error_state, error_value], |
---|
| 1455 | [min_state, min_value], |
---|
| 1456 | [max_state, max_value], unit]) |
---|
[cfc0913] | 1457 | |
---|
[c77d859] | 1458 | def _set_model_sizer_selection(self, model): |
---|
| 1459 | """ |
---|
[5062bbf] | 1460 | Display the sizer according to the type of the current model |
---|
[c77d859] | 1461 | """ |
---|
[5062bbf] | 1462 | if model == None: |
---|
[70c57ebf] | 1463 | return |
---|
[72f719b] | 1464 | if hasattr(model ,"s_model"): |
---|
[c77d859] | 1465 | |
---|
[4a7ad5f] | 1466 | class_name = model.s_model.__class__ |
---|
| 1467 | name = model.s_model.name |
---|
| 1468 | flag = (name != "NoStructure") |
---|
| 1469 | if flag and \ |
---|
| 1470 | (class_name in self.model_list_box["Structure Factors"]): |
---|
[6dc9ad8] | 1471 | self.structurebox.Show() |
---|
| 1472 | self.text2.Show() |
---|
[3b605bb] | 1473 | self.structurebox.Enable() |
---|
[c097f02] | 1474 | self.text2.Enable() |
---|
[c77d859] | 1475 | items = self.structurebox.GetItems() |
---|
| 1476 | self.sizer1.Layout() |
---|
[340c2b3] | 1477 | |
---|
[c77d859] | 1478 | for i in range(len(items)): |
---|
| 1479 | if items[i]== str(name): |
---|
| 1480 | self.structurebox.SetSelection(i) |
---|
| 1481 | break |
---|
| 1482 | |
---|
[72f719b] | 1483 | if hasattr(model ,"p_model"): |
---|
| 1484 | class_name = model.p_model.__class__ |
---|
| 1485 | name = model.p_model.name |
---|
[c77d859] | 1486 | self.formfactorbox.Clear() |
---|
| 1487 | |
---|
| 1488 | for k, list in self.model_list_box.iteritems(): |
---|
[376916c] | 1489 | if k in["P(Q)*S(Q)","Shapes" ] and class_name in self.model_list_box["Shapes"]: |
---|
[c77d859] | 1490 | self.shape_rbutton.SetValue(True) |
---|
[376916c] | 1491 | ## fill the form factor list with new model |
---|
| 1492 | self._populate_box(self.formfactorbox,self.model_list_box["Shapes"]) |
---|
| 1493 | items = self.formfactorbox.GetItems() |
---|
| 1494 | ## set comboxbox to the selected item |
---|
| 1495 | for i in range(len(items)): |
---|
| 1496 | if items[i]== str(name): |
---|
| 1497 | self.formfactorbox.SetSelection(i) |
---|
| 1498 | break |
---|
| 1499 | return |
---|
| 1500 | elif k == "Shape-Independent": |
---|
[c77d859] | 1501 | self.shape_indep_rbutton.SetValue(True) |
---|
| 1502 | elif k == "Structure Factors": |
---|
| 1503 | self.struct_rbutton.SetValue(True) |
---|
[fb59ed9] | 1504 | elif k == "Multi-Functions": |
---|
| 1505 | continue |
---|
[c77d859] | 1506 | else: |
---|
| 1507 | self.plugin_rbutton.SetValue(True) |
---|
[376916c] | 1508 | |
---|
[c77d859] | 1509 | if class_name in list: |
---|
| 1510 | ## fill the form factor list with new model |
---|
| 1511 | self._populate_box(self.formfactorbox, list) |
---|
| 1512 | items = self.formfactorbox.GetItems() |
---|
| 1513 | ## set comboxbox to the selected item |
---|
| 1514 | for i in range(len(items)): |
---|
| 1515 | if items[i]== str(name): |
---|
| 1516 | self.formfactorbox.SetSelection(i) |
---|
| 1517 | break |
---|
| 1518 | break |
---|
| 1519 | else: |
---|
[cdbe88e] | 1520 | |
---|
| 1521 | ## Select the model from the menu |
---|
[c77d859] | 1522 | class_name = model.__class__ |
---|
| 1523 | name = model.name |
---|
| 1524 | self.formfactorbox.Clear() |
---|
| 1525 | items = self.formfactorbox.GetItems() |
---|
[376916c] | 1526 | |
---|
[cdbe88e] | 1527 | for k, list in self.model_list_box.iteritems(): |
---|
[376916c] | 1528 | if k in["P(Q)*S(Q)","Shapes" ] and class_name in self.model_list_box["Shapes"]: |
---|
[3b605bb] | 1529 | if class_name in self.model_list_box["P(Q)*S(Q)"]: |
---|
[6dc9ad8] | 1530 | self.structurebox.Show() |
---|
| 1531 | self.text2.Show() |
---|
[3b605bb] | 1532 | self.structurebox.Enable() |
---|
[cdbe88e] | 1533 | self.structurebox.SetSelection(0) |
---|
[c097f02] | 1534 | self.text2.Enable() |
---|
[3b605bb] | 1535 | else: |
---|
[6dc9ad8] | 1536 | self.structurebox.Hide() |
---|
| 1537 | self.text2.Hide() |
---|
[3b605bb] | 1538 | self.structurebox.Disable() |
---|
| 1539 | self.structurebox.SetSelection(0) |
---|
[c097f02] | 1540 | self.text2.Disable() |
---|
[3b605bb] | 1541 | |
---|
[c77d859] | 1542 | self.shape_rbutton.SetValue(True) |
---|
[376916c] | 1543 | ## fill the form factor list with new model |
---|
| 1544 | self._populate_box(self.formfactorbox,self.model_list_box["Shapes"]) |
---|
| 1545 | items = self.formfactorbox.GetItems() |
---|
| 1546 | ## set comboxbox to the selected item |
---|
| 1547 | for i in range(len(items)): |
---|
| 1548 | if items[i]== str(name): |
---|
| 1549 | self.formfactorbox.SetSelection(i) |
---|
| 1550 | break |
---|
| 1551 | return |
---|
| 1552 | elif k == "Shape-Independent": |
---|
[c77d859] | 1553 | self.shape_indep_rbutton.SetValue(True) |
---|
| 1554 | elif k == "Structure Factors": |
---|
[cdbe88e] | 1555 | self.struct_rbutton.SetValue(True) |
---|
[fb59ed9] | 1556 | elif k == "Multi-Functions": |
---|
| 1557 | continue |
---|
[c77d859] | 1558 | else: |
---|
| 1559 | self.plugin_rbutton.SetValue(True) |
---|
| 1560 | if class_name in list: |
---|
[3b605bb] | 1561 | self.structurebox.SetSelection(0) |
---|
| 1562 | self.structurebox.Disable() |
---|
[c097f02] | 1563 | self.text2.Disable() |
---|
[376916c] | 1564 | ## fill the form factor list with new model |
---|
[c77d859] | 1565 | self._populate_box(self.formfactorbox, list) |
---|
| 1566 | items = self.formfactorbox.GetItems() |
---|
| 1567 | ## set comboxbox to the selected item |
---|
| 1568 | for i in range(len(items)): |
---|
| 1569 | if items[i]== str(name): |
---|
| 1570 | self.formfactorbox.SetSelection(i) |
---|
| 1571 | break |
---|
| 1572 | break |
---|
[1467e1a6] | 1573 | |
---|
[2296316] | 1574 | def _draw_model(self, update_chisqr=True): |
---|
[c77d859] | 1575 | """ |
---|
[5062bbf] | 1576 | Method to draw or refresh a plotted model. |
---|
| 1577 | The method will use the data member from the model page |
---|
| 1578 | to build a call to the fitting perspective manager. |
---|
[2296316] | 1579 | |
---|
| 1580 | :param chisqr: update chisqr value [bool] |
---|
[c77d859] | 1581 | """ |
---|
[6bbeacd4] | 1582 | #if self.check_invalid_panel(): |
---|
| 1583 | # return |
---|
[c77d859] | 1584 | if self.model !=None: |
---|
[fca9cbd9] | 1585 | temp_smear=None |
---|
| 1586 | if hasattr(self, "enable_smearer"): |
---|
[7609f1a] | 1587 | if not self.disable_smearer.GetValue(): |
---|
| 1588 | temp_smear= self.current_smearer |
---|
[fa65e99] | 1589 | toggle_mode_on = self.model_view.IsEnabled() |
---|
[3cd5806] | 1590 | self._manager.draw_model(self.model, |
---|
[f72333f] | 1591 | data=self.data, |
---|
[c16557c] | 1592 | smearer= temp_smear, |
---|
[484faf7] | 1593 | qmin=float(self.qmin_x), |
---|
| 1594 | qmax=float(self.qmax_x), |
---|
[6bbeacd4] | 1595 | qstep= float(self.npts_x), |
---|
[66ff250] | 1596 | page_id=self.uid, |
---|
[fa65e99] | 1597 | toggle_mode_on=toggle_mode_on, |
---|
[5ef55d2] | 1598 | state = self.state, |
---|
[2296316] | 1599 | enable2D=self.enable2D, |
---|
| 1600 | update_chisqr=update_chisqr) |
---|
[71f0373] | 1601 | |
---|
[340c2b3] | 1602 | |
---|
[a1b2471] | 1603 | def _on_show_sld(self, event=None): |
---|
| 1604 | """ |
---|
| 1605 | Plot SLD profile |
---|
| 1606 | """ |
---|
| 1607 | # get profile data |
---|
| 1608 | x,y=self.model.getProfile() |
---|
| 1609 | |
---|
| 1610 | from danse.common.plottools import Data1D |
---|
[da9ac4e6] | 1611 | #from sans.perspectives.theory.profile_dialog import SLDPanel |
---|
| 1612 | from sans.guiframe.local_perspectives.plotting.profile_dialog \ |
---|
| 1613 | import SLDPanel |
---|
[a1b2471] | 1614 | sld_data = Data1D(x,y) |
---|
| 1615 | sld_data.name = 'SLD' |
---|
[fb59ed9] | 1616 | sld_data.axes = self.sld_axes |
---|
| 1617 | self.panel = SLDPanel(self, data=sld_data,axes =self.sld_axes,id =-1 ) |
---|
[a1b2471] | 1618 | self.panel.ShowModal() |
---|
| 1619 | |
---|
[fb59ed9] | 1620 | def _set_multfactor_combobox(self, multiplicity=10): |
---|
[4523b68] | 1621 | """ |
---|
| 1622 | Set comboBox for muitfactor of CoreMultiShellModel |
---|
[fb59ed9] | 1623 | :param multiplicit: no. of multi-functionality |
---|
[4523b68] | 1624 | """ |
---|
[fb59ed9] | 1625 | # build content of the combobox |
---|
| 1626 | for idx in range(0,multiplicity): |
---|
[4523b68] | 1627 | self.multifactorbox.Append(str(idx),int(idx)) |
---|
[fb59ed9] | 1628 | #self.multifactorbox.SetSelection(1) |
---|
[4523b68] | 1629 | self._hide_multfactor_combobox() |
---|
| 1630 | |
---|
| 1631 | def _show_multfactor_combobox(self): |
---|
| 1632 | """ |
---|
| 1633 | Show the comboBox of muitfactor of CoreMultiShellModel |
---|
| 1634 | """ |
---|
| 1635 | if not self.mutifactor_text.IsShown(): |
---|
| 1636 | self.mutifactor_text.Show(True) |
---|
[fb59ed9] | 1637 | self.mutifactor_text1.Show(True) |
---|
[4523b68] | 1638 | if not self.multifactorbox.IsShown(): |
---|
| 1639 | self.multifactorbox.Show(True) |
---|
| 1640 | |
---|
| 1641 | def _hide_multfactor_combobox(self): |
---|
| 1642 | """ |
---|
| 1643 | Hide the comboBox of muitfactor of CoreMultiShellModel |
---|
| 1644 | """ |
---|
| 1645 | if self.mutifactor_text.IsShown(): |
---|
| 1646 | self.mutifactor_text.Hide() |
---|
[fb59ed9] | 1647 | self.mutifactor_text1.Hide() |
---|
[4523b68] | 1648 | if self.multifactorbox.IsShown(): |
---|
| 1649 | self.multifactorbox.Hide() |
---|
| 1650 | |
---|
| 1651 | |
---|
[0b12abb5] | 1652 | def _show_combox_helper(self): |
---|
[c77d859] | 1653 | """ |
---|
[0b12abb5] | 1654 | Fill panel's combo box according to the type of model selected |
---|
[c77d859] | 1655 | """ |
---|
[0b12abb5] | 1656 | if self.shape_rbutton.GetValue(): |
---|
[376916c] | 1657 | ##fill the combobox with form factor list |
---|
[3b605bb] | 1658 | self.structurebox.SetSelection(0) |
---|
| 1659 | self.structurebox.Disable() |
---|
[c77d859] | 1660 | self.formfactorbox.Clear() |
---|
[376916c] | 1661 | self._populate_box( self.formfactorbox,self.model_list_box["Shapes"]) |
---|
[0b12abb5] | 1662 | if self.shape_indep_rbutton.GetValue(): |
---|
[376916c] | 1663 | ##fill the combobox with shape independent factor list |
---|
[3b605bb] | 1664 | self.structurebox.SetSelection(0) |
---|
| 1665 | self.structurebox.Disable() |
---|
[c77d859] | 1666 | self.formfactorbox.Clear() |
---|
| 1667 | self._populate_box( self.formfactorbox, |
---|
[376916c] | 1668 | self.model_list_box["Shape-Independent"]) |
---|
[0b12abb5] | 1669 | if self.struct_rbutton.GetValue(): |
---|
[376916c] | 1670 | ##fill the combobox with structure factor list |
---|
[3b605bb] | 1671 | self.structurebox.SetSelection(0) |
---|
| 1672 | self.structurebox.Disable() |
---|
[c77d859] | 1673 | self.formfactorbox.Clear() |
---|
| 1674 | self._populate_box( self.formfactorbox, |
---|
| 1675 | self.model_list_box["Structure Factors"]) |
---|
[0b12abb5] | 1676 | if self.plugin_rbutton.GetValue(): |
---|
[376916c] | 1677 | ##fill the combobox with form factor list |
---|
[3b605bb] | 1678 | self.structurebox.Disable() |
---|
[c77d859] | 1679 | self.formfactorbox.Clear() |
---|
| 1680 | self._populate_box( self.formfactorbox, |
---|
[376916c] | 1681 | self.model_list_box["Customized Models"]) |
---|
[3595309d] | 1682 | |
---|
[0b12abb5] | 1683 | def _show_combox(self, event=None): |
---|
| 1684 | """ |
---|
| 1685 | Show combox box associate with type of model selected |
---|
| 1686 | """ |
---|
[6bbeacd4] | 1687 | #if self.check_invalid_panel(): |
---|
| 1688 | # self.shape_rbutton.SetValue(True) |
---|
| 1689 | # return |
---|
[0b12abb5] | 1690 | |
---|
| 1691 | self._show_combox_helper() |
---|
[77e23a2] | 1692 | self._on_select_model(event=None) |
---|
[3595309d] | 1693 | self._save_typeOfmodel() |
---|
[3b605bb] | 1694 | self.sizer4_4.Layout() |
---|
[9853ad0] | 1695 | self.sizer4.Layout() |
---|
[3b605bb] | 1696 | self.Layout() |
---|
| 1697 | self.Refresh() |
---|
[340c2b3] | 1698 | |
---|
[c77d859] | 1699 | def _populate_box(self, combobox, list): |
---|
| 1700 | """ |
---|
[5062bbf] | 1701 | fill combox box with dict item |
---|
| 1702 | |
---|
| 1703 | :param list: contains item to fill the combox |
---|
[c77d859] | 1704 | item must model class |
---|
| 1705 | """ |
---|
| 1706 | for models in list: |
---|
| 1707 | model= models() |
---|
| 1708 | name = model.__class__.__name__ |
---|
[0a518e4c] | 1709 | if models.__name__!="NoStructure": |
---|
| 1710 | if hasattr(model, "name"): |
---|
| 1711 | name = model.name |
---|
| 1712 | combobox.Append(name,models) |
---|
[c77d859] | 1713 | return 0 |
---|
[6bbeacd4] | 1714 | |
---|
[6e9976b] | 1715 | def _onQrangeEnter(self, event): |
---|
| 1716 | """ |
---|
[5062bbf] | 1717 | Check validity of value enter in the Q range field |
---|
[6bbeacd4] | 1718 | |
---|
| 1719 | """ |
---|
| 1720 | tcrtl = event.GetEventObject() |
---|
| 1721 | #Clear msg if previously shown. |
---|
| 1722 | msg = "" |
---|
| 1723 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
| 1724 | # Flag to register when a parameter has changed. |
---|
| 1725 | is_modified = False |
---|
| 1726 | if tcrtl.GetValue().lstrip().rstrip() != "": |
---|
| 1727 | try: |
---|
| 1728 | value = float(tcrtl.GetValue()) |
---|
| 1729 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
| 1730 | # If qmin and qmax have been modified, update qmin and qmax |
---|
[f95301b] | 1731 | if self._validate_qrange(self.qmin, self.qmax): |
---|
| 1732 | tempmin = float(self.qmin.GetValue()) |
---|
[6bbeacd4] | 1733 | if tempmin != self.qmin_x: |
---|
| 1734 | self.qmin_x = tempmin |
---|
| 1735 | tempmax = float(self.qmax.GetValue()) |
---|
| 1736 | if tempmax != self.qmax_x: |
---|
| 1737 | self.qmax_x = tempmax |
---|
| 1738 | else: |
---|
| 1739 | tcrtl.SetBackgroundColour("pink") |
---|
| 1740 | msg = "Model Error:wrong value entered : %s" % sys.exc_value |
---|
| 1741 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
| 1742 | return |
---|
| 1743 | except: |
---|
| 1744 | tcrtl.SetBackgroundColour("pink") |
---|
| 1745 | msg = "Model Error:wrong value entered : %s" % sys.exc_value |
---|
| 1746 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
| 1747 | return |
---|
| 1748 | #Check if # of points for theory model are valid(>0). |
---|
| 1749 | if self.npts != None: |
---|
| 1750 | if check_float(self.npts): |
---|
| 1751 | temp_npts = float(self.npts.GetValue()) |
---|
| 1752 | if temp_npts != self.num_points: |
---|
| 1753 | self.num_points = temp_npts |
---|
| 1754 | is_modified = True |
---|
| 1755 | else: |
---|
| 1756 | msg = "Cannot Plot :No npts in that Qrange!!! " |
---|
| 1757 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
| 1758 | else: |
---|
| 1759 | tcrtl.SetBackgroundColour("pink") |
---|
| 1760 | msg = "Model Error:wrong value entered!!!" |
---|
| 1761 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
| 1762 | #self._undo.Enable(True) |
---|
| 1763 | self.save_current_state() |
---|
| 1764 | event = PageInfoEvent(page=self) |
---|
| 1765 | wx.PostEvent(self.parent, event) |
---|
| 1766 | self.state_change = False |
---|
| 1767 | #Draw the model for a different range |
---|
| 1768 | self._draw_model() |
---|
| 1769 | |
---|
| 1770 | def _theory_qrange_enter(self, event): |
---|
| 1771 | """ |
---|
| 1772 | Check validity of value enter in the Q range field |
---|
[6e9976b] | 1773 | """ |
---|
| 1774 | |
---|
| 1775 | tcrtl= event.GetEventObject() |
---|
| 1776 | #Clear msg if previously shown. |
---|
| 1777 | msg= "" |
---|
| 1778 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
| 1779 | # Flag to register when a parameter has changed. |
---|
| 1780 | is_modified = False |
---|
| 1781 | if tcrtl.GetValue().lstrip().rstrip()!="": |
---|
| 1782 | try: |
---|
| 1783 | value = float(tcrtl.GetValue()) |
---|
| 1784 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
| 1785 | |
---|
| 1786 | # If qmin and qmax have been modified, update qmin and qmax |
---|
[6bbeacd4] | 1787 | if self._validate_qrange(self.theory_qmin, self.theory_qmax): |
---|
| 1788 | tempmin = float(self.theory_qmin.GetValue()) |
---|
| 1789 | if tempmin != self.theory_qmin_x: |
---|
| 1790 | self.theory_qmin_x = tempmin |
---|
| 1791 | tempmax = float(self.theory_qmax.GetValue()) |
---|
[6e9976b] | 1792 | if tempmax != self.qmax_x: |
---|
[6bbeacd4] | 1793 | self.theory_qmax_x = tempmax |
---|
[6e9976b] | 1794 | else: |
---|
| 1795 | tcrtl.SetBackgroundColour("pink") |
---|
| 1796 | msg= "Model Error:wrong value entered : %s"% sys.exc_value |
---|
[6bbeacd4] | 1797 | wx.PostEvent(self._manager.parent, StatusEvent(status = msg )) |
---|
[0b12abb5] | 1798 | return |
---|
[6e9976b] | 1799 | except: |
---|
| 1800 | tcrtl.SetBackgroundColour("pink") |
---|
| 1801 | msg= "Model Error:wrong value entered : %s"% sys.exc_value |
---|
[6bbeacd4] | 1802 | wx.PostEvent(self._manager.parent, StatusEvent(status = msg )) |
---|
[6e9976b] | 1803 | return |
---|
| 1804 | #Check if # of points for theory model are valid(>0). |
---|
[e9875db] | 1805 | if self.Npts_total.IsEditable() : |
---|
[0cf97c5] | 1806 | if check_float(self.Npts_total): |
---|
| 1807 | temp_npts = float(self.Npts_total.GetValue()) |
---|
[6e9976b] | 1808 | if temp_npts != self.num_points: |
---|
| 1809 | self.num_points = temp_npts |
---|
| 1810 | is_modified = True |
---|
| 1811 | else: |
---|
| 1812 | msg= "Cannot Plot :No npts in that Qrange!!! " |
---|
| 1813 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
| 1814 | else: |
---|
| 1815 | tcrtl.SetBackgroundColour("pink") |
---|
[6bbeacd4] | 1816 | msg = "Model Error:wrong value entered!!!" |
---|
[0b12abb5] | 1817 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg)) |
---|
[6e9976b] | 1818 | #self._undo.Enable(True) |
---|
| 1819 | self.save_current_state() |
---|
| 1820 | event = PageInfoEvent(page = self) |
---|
| 1821 | wx.PostEvent(self.parent, event) |
---|
| 1822 | self.state_change= False |
---|
[484faf7] | 1823 | #Draw the model for a different range |
---|
| 1824 | self._draw_model() |
---|
[6e9976b] | 1825 | |
---|
[59a7f2d] | 1826 | def _on_select_model_helper(self): |
---|
[c77d859] | 1827 | """ |
---|
[5062bbf] | 1828 | call back for model selection |
---|
[c77d859] | 1829 | """ |
---|
[376916c] | 1830 | ## reset dictionary containing reference to dispersion |
---|
| 1831 | self._disp_obj_dict = {} |
---|
| 1832 | self.disp_cb_dict ={} |
---|
[b421b1a] | 1833 | |
---|
[c77d859] | 1834 | f_id = self.formfactorbox.GetCurrentSelection() |
---|
[ff8f99b] | 1835 | #For MAC |
---|
| 1836 | form_factor = None |
---|
| 1837 | if f_id >= 0: |
---|
[0b12abb5] | 1838 | form_factor = self.formfactorbox.GetClientData(f_id) |
---|
[9170547] | 1839 | |
---|
[376916c] | 1840 | if not form_factor in self.model_list_box["multiplication"]: |
---|
[6dc9ad8] | 1841 | self.structurebox.Hide() |
---|
| 1842 | self.text2.Hide() |
---|
[3b605bb] | 1843 | self.structurebox.Disable() |
---|
| 1844 | self.structurebox.SetSelection(0) |
---|
[c097f02] | 1845 | self.text2.Disable() |
---|
[87fbc60] | 1846 | else: |
---|
[6dc9ad8] | 1847 | self.structurebox.Show() |
---|
| 1848 | self.text2.Show() |
---|
[87fbc60] | 1849 | self.structurebox.Enable() |
---|
[c097f02] | 1850 | self.text2.Enable() |
---|
[4523b68] | 1851 | |
---|
| 1852 | if form_factor != None: |
---|
| 1853 | # set multifactor for Mutifunctional models |
---|
[e87f9fc] | 1854 | if form_factor().__class__ in self.model_list_box["Multi-Functions"]: |
---|
[4523b68] | 1855 | m_id = self.multifactorbox.GetCurrentSelection() |
---|
[fb59ed9] | 1856 | multiplicity = form_factor().multiplicity_info[0] |
---|
| 1857 | self.multifactorbox.Clear() |
---|
| 1858 | #self.mutifactor_text.SetLabel(form_factor().details[]) |
---|
| 1859 | self._set_multfactor_combobox(multiplicity) |
---|
| 1860 | self._show_multfactor_combobox() |
---|
| 1861 | #ToDo: this info should be called directly from the model |
---|
| 1862 | text = form_factor().multiplicity_info[1]#'No. of Shells: ' |
---|
| 1863 | |
---|
| 1864 | #self.mutifactor_text.Clear() |
---|
| 1865 | self.mutifactor_text.SetLabel(text) |
---|
| 1866 | if m_id > multiplicity -1: |
---|
| 1867 | # default value |
---|
| 1868 | m_id = 1 |
---|
| 1869 | |
---|
[4523b68] | 1870 | self.multi_factor = self.multifactorbox.GetClientData(m_id) |
---|
| 1871 | if self.multi_factor == None: self.multi_factor =0 |
---|
| 1872 | form_factor = form_factor(int(self.multi_factor)) |
---|
[fb59ed9] | 1873 | self.multifactorbox.SetSelection(m_id) |
---|
| 1874 | # Check len of the text1 and max_multiplicity |
---|
| 1875 | text = '' |
---|
| 1876 | if form_factor.multiplicity_info[0] == len(form_factor.multiplicity_info[2]): |
---|
| 1877 | text = form_factor.multiplicity_info[2][self.multi_factor] |
---|
| 1878 | self.mutifactor_text1.SetLabel(text) |
---|
| 1879 | # Check if model has get sld profile. |
---|
| 1880 | if len(form_factor.multiplicity_info[3]) > 0: |
---|
| 1881 | self.sld_axes = form_factor.multiplicity_info[3] |
---|
| 1882 | self.show_sld_button.Show(True) |
---|
| 1883 | else: |
---|
| 1884 | self.sld_axes = "" |
---|
| 1885 | |
---|
[4523b68] | 1886 | else: |
---|
| 1887 | self._hide_multfactor_combobox() |
---|
[a1b2471] | 1888 | self.show_sld_button.Hide() |
---|
[4523b68] | 1889 | form_factor = form_factor() |
---|
| 1890 | self.multi_factor = None |
---|
| 1891 | else: |
---|
| 1892 | self._hide_multfactor_combobox() |
---|
[a1b2471] | 1893 | self.show_sld_button.Hide() |
---|
[4523b68] | 1894 | self.multi_factor = None |
---|
| 1895 | |
---|
[3b605bb] | 1896 | s_id = self.structurebox.GetCurrentSelection() |
---|
| 1897 | struct_factor = self.structurebox.GetClientData( s_id ) |
---|
[4523b68] | 1898 | |
---|
[3b605bb] | 1899 | if struct_factor !=None: |
---|
[9853ad0] | 1900 | from sans.models.MultiplicationModel import MultiplicationModel |
---|
[4523b68] | 1901 | self.model= MultiplicationModel(form_factor,struct_factor()) |
---|
[0aeabc6] | 1902 | |
---|
[c77d859] | 1903 | else: |
---|
[fbf4bf8] | 1904 | if form_factor != None: |
---|
[4523b68] | 1905 | self.model= form_factor |
---|
[fbf4bf8] | 1906 | else: |
---|
| 1907 | self.model = None |
---|
| 1908 | return self.model |
---|
[4523b68] | 1909 | |
---|
| 1910 | |
---|
[cfc0913] | 1911 | ## post state to fit panel |
---|
[f20767b] | 1912 | self.state.parameters =[] |
---|
[fc6ea43] | 1913 | self.state.model =self.model |
---|
[4523b68] | 1914 | self.state.qmin = self.qmin_x |
---|
| 1915 | self.state.multi_factor = self.multi_factor |
---|
[71f0373] | 1916 | self.disp_list =self.model.getDispParamList() |
---|
[f20767b] | 1917 | self.state.disp_list = self.disp_list |
---|
[fadf925] | 1918 | self.on_set_focus(None) |
---|
[7975f2b] | 1919 | self.Layout() |
---|
[c99a6c5] | 1920 | |
---|
[85b3971] | 1921 | def _validate_qrange(self, qmin_ctrl, qmax_ctrl): |
---|
| 1922 | """ |
---|
[5062bbf] | 1923 | Verify that the Q range controls have valid values |
---|
| 1924 | and that Qmin < Qmax. |
---|
| 1925 | |
---|
| 1926 | :param qmin_ctrl: text control for Qmin |
---|
| 1927 | :param qmax_ctrl: text control for Qmax |
---|
| 1928 | |
---|
| 1929 | :return: True is the Q range is value, False otherwise |
---|
| 1930 | |
---|
[85b3971] | 1931 | """ |
---|
| 1932 | qmin_validity = check_float(qmin_ctrl) |
---|
| 1933 | qmax_validity = check_float(qmax_ctrl) |
---|
| 1934 | if not (qmin_validity and qmax_validity): |
---|
| 1935 | return False |
---|
| 1936 | else: |
---|
| 1937 | qmin = float(qmin_ctrl.GetValue()) |
---|
| 1938 | qmax = float(qmax_ctrl.GetValue()) |
---|
[7609f1a] | 1939 | if qmin <= qmax: |
---|
[85b3971] | 1940 | #Make sure to set both colours white. |
---|
| 1941 | qmin_ctrl.SetBackgroundColour(wx.WHITE) |
---|
| 1942 | qmin_ctrl.Refresh() |
---|
| 1943 | qmax_ctrl.SetBackgroundColour(wx.WHITE) |
---|
| 1944 | qmax_ctrl.Refresh() |
---|
| 1945 | else: |
---|
| 1946 | qmin_ctrl.SetBackgroundColour("pink") |
---|
| 1947 | qmin_ctrl.Refresh() |
---|
| 1948 | qmax_ctrl.SetBackgroundColour("pink") |
---|
| 1949 | qmax_ctrl.Refresh() |
---|
| 1950 | msg= "Invalid Q range: Q min must be smaller than Q max" |
---|
| 1951 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg)) |
---|
| 1952 | return False |
---|
| 1953 | return True |
---|
[51a71a3] | 1954 | |
---|
| 1955 | def _validate_Npts(self): |
---|
| 1956 | """ |
---|
[5062bbf] | 1957 | Validate the number of points for fitting is more than 10 points. |
---|
| 1958 | If valid, setvalues Npts_fit otherwise post msg. |
---|
[51a71a3] | 1959 | """ |
---|
| 1960 | #default flag |
---|
| 1961 | flag = True |
---|
[1a74523] | 1962 | # Theory |
---|
| 1963 | if self.data == None and self.enable2D: |
---|
| 1964 | return flag |
---|
[7609f1a] | 1965 | |
---|
[51a71a3] | 1966 | # q value from qx and qy |
---|
[3f6508f] | 1967 | radius= numpy.sqrt( self.data.qx_data * self.data.qx_data + |
---|
| 1968 | self.data.qy_data * self.data.qy_data ) |
---|
[51a71a3] | 1969 | #get unmasked index |
---|
[f95301b] | 1970 | index_data = (float(self.qmin.GetValue()) <= radius) & \ |
---|
[3f6508f] | 1971 | (radius <= float(self.qmax.GetValue())) |
---|
| 1972 | index_data = (index_data) & (self.data.mask) |
---|
| 1973 | index_data = (index_data) & (numpy.isfinite(self.data.data)) |
---|
[51a71a3] | 1974 | |
---|
| 1975 | if len(index_data[index_data]) < 10: |
---|
| 1976 | # change the color pink. |
---|
[f95301b] | 1977 | self.qmin.SetBackgroundColour("pink") |
---|
| 1978 | self.qmin.Refresh() |
---|
[51a71a3] | 1979 | self.qmax.SetBackgroundColour("pink") |
---|
| 1980 | self.qmax.Refresh() |
---|
| 1981 | msg= "Cannot Plot :No or too little npts in that data range!!! " |
---|
| 1982 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
| 1983 | self.fitrange = False |
---|
| 1984 | flag = False |
---|
| 1985 | else: |
---|
| 1986 | self.Npts_fit.SetValue(str(len(self.data.mask[index_data==True]))) |
---|
| 1987 | self.fitrange = True |
---|
| 1988 | |
---|
| 1989 | return flag |
---|
| 1990 | |
---|
[c77d859] | 1991 | def _check_value_enter(self, list, modified): |
---|
| 1992 | """ |
---|
[5062bbf] | 1993 | :param list: model parameter and panel info |
---|
| 1994 | :Note: each item of the list should be as follow: |
---|
| 1995 | item=[check button state, parameter's name, |
---|
| 1996 | paramater's value, string="+/-", |
---|
| 1997 | parameter's error of fit, |
---|
| 1998 | parameter's minimum value, |
---|
| 1999 | parrameter's maximum value , |
---|
| 2000 | parameter's units] |
---|
[c77d859] | 2001 | """ |
---|
[c99a6c5] | 2002 | is_modified = modified |
---|
[c77d859] | 2003 | if len(list)==0: |
---|
[c99a6c5] | 2004 | return is_modified |
---|
[c77d859] | 2005 | for item in list: |
---|
[edd166b] | 2006 | #skip angle parameters for 1D |
---|
[f7e05b0] | 2007 | if not self.enable2D:#self.data.__class__.__name__ !="Data2D": |
---|
[edd166b] | 2008 | if item in self.orientation_params: |
---|
| 2009 | continue |
---|
| 2010 | #try: |
---|
| 2011 | name = str(item[1]) |
---|
| 2012 | |
---|
[2296316] | 2013 | if string.find(name,".npts") ==-1 and \ |
---|
| 2014 | string.find(name,".nsigmas")==-1: |
---|
[c985bef] | 2015 | ## check model parameters range |
---|
[77e23a2] | 2016 | param_min= None |
---|
| 2017 | param_max= None |
---|
[7975f2b] | 2018 | |
---|
[c985bef] | 2019 | ## check minimun value |
---|
[edd166b] | 2020 | if item[5]!= None and item[5]!= "": |
---|
[298b762] | 2021 | if item[5].GetValue().lstrip().rstrip()!="": |
---|
[edd166b] | 2022 | try: |
---|
[7975f2b] | 2023 | |
---|
[edd166b] | 2024 | param_min = float(item[5].GetValue()) |
---|
[918fcb1] | 2025 | if not self._validate_qrange(item[5],item[2]): |
---|
[7975f2b] | 2026 | if numpy.isfinite(param_min): |
---|
| 2027 | item[2].SetValue(format_number(param_min)) |
---|
[6e9976b] | 2028 | |
---|
| 2029 | item[5].SetBackgroundColour(wx.WHITE) |
---|
| 2030 | item[2].SetBackgroundColour(wx.WHITE) |
---|
| 2031 | |
---|
[edd166b] | 2032 | except: |
---|
| 2033 | msg = "Wrong Fit parameter range entered " |
---|
[2296316] | 2034 | wx.PostEvent(self.parent.parent, |
---|
| 2035 | StatusEvent(status = msg)) |
---|
[edd166b] | 2036 | raise ValueError, msg |
---|
[c985bef] | 2037 | is_modified = True |
---|
[77e23a2] | 2038 | ## check maximum value |
---|
[7975f2b] | 2039 | if item[6]!= None and item[6]!= "": |
---|
[298b762] | 2040 | if item[6].GetValue().lstrip().rstrip()!="": |
---|
[7975f2b] | 2041 | try: |
---|
[edd166b] | 2042 | param_max = float(item[6].GetValue()) |
---|
[918fcb1] | 2043 | if not self._validate_qrange(item[2],item[6]): |
---|
[7975f2b] | 2044 | if numpy.isfinite(param_max): |
---|
| 2045 | item[2].SetValue(format_number(param_max)) |
---|
[6e9976b] | 2046 | |
---|
| 2047 | item[6].SetBackgroundColour(wx.WHITE) |
---|
| 2048 | item[2].SetBackgroundColour(wx.WHITE) |
---|
[edd166b] | 2049 | except: |
---|
| 2050 | msg = "Wrong Fit parameter range entered " |
---|
[2296316] | 2051 | wx.PostEvent(self.parent.parent, |
---|
| 2052 | StatusEvent(status = msg)) |
---|
[edd166b] | 2053 | raise ValueError, msg |
---|
[c985bef] | 2054 | is_modified = True |
---|
[7975f2b] | 2055 | |
---|
[edd166b] | 2056 | |
---|
[77e23a2] | 2057 | if param_min != None and param_max !=None: |
---|
[85b3971] | 2058 | if not self._validate_qrange(item[5], item[6]): |
---|
[77e23a2] | 2059 | msg= "Wrong Fit range entered for parameter " |
---|
| 2060 | msg+= "name %s of model %s "%(name, self.model.name) |
---|
[2296316] | 2061 | wx.PostEvent(self.parent.parent, |
---|
| 2062 | StatusEvent(status = msg)) |
---|
[7975f2b] | 2063 | |
---|
[cfc0913] | 2064 | if name in self.model.details.keys(): |
---|
[2296316] | 2065 | self.model.details[name][1:3] = param_min, param_max |
---|
[edd166b] | 2066 | is_modified = True |
---|
[7975f2b] | 2067 | |
---|
[edd166b] | 2068 | else: |
---|
[2296316] | 2069 | self.model.details [name] = ["", param_min, param_max] |
---|
[7975f2b] | 2070 | is_modified = True |
---|
[2296316] | 2071 | try: |
---|
| 2072 | # Check if the textctr is enabled |
---|
| 2073 | if item[2].IsEnabled(): |
---|
| 2074 | value= float(item[2].GetValue()) |
---|
| 2075 | item[2].SetBackgroundColour("white") |
---|
| 2076 | # If the value of the parameter has changed, |
---|
| 2077 | # +update the model and set the is_modified flag |
---|
| 2078 | if value != self.model.getParam(name) and \ |
---|
| 2079 | numpy.isfinite(value): |
---|
| 2080 | self.model.setParam(name, value) |
---|
| 2081 | |
---|
[6e9976b] | 2082 | except: |
---|
[7987962] | 2083 | item[2].SetBackgroundColour("pink") |
---|
[6e9976b] | 2084 | msg = "Wrong Fit parameter value entered " |
---|
| 2085 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg)) |
---|
[7975f2b] | 2086 | |
---|
[c99a6c5] | 2087 | return is_modified |
---|
[c77d859] | 2088 | |
---|
| 2089 | |
---|
| 2090 | def _set_dipers_Param(self, event): |
---|
| 2091 | """ |
---|
[5062bbf] | 2092 | respond to self.enable_disp and self.disable_disp radio box. |
---|
| 2093 | The dispersity object is reset inside the model into Gaussian. |
---|
| 2094 | When the user select yes , this method display a combo box for more selection |
---|
| 2095 | when the user selects No,the combo box disappears. |
---|
| 2096 | Redraw the model with the default dispersity (Gaussian) |
---|
[c77d859] | 2097 | """ |
---|
[6bbeacd4] | 2098 | #if self.check_invalid_panel(): |
---|
| 2099 | # return |
---|
[4470b10] | 2100 | ## On selction if no model exists. |
---|
| 2101 | if self.model ==None: |
---|
| 2102 | self.disable_disp.SetValue(True) |
---|
| 2103 | msg="Please select a Model first..." |
---|
| 2104 | wx.MessageBox(msg, 'Info') |
---|
[3cd5806] | 2105 | wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
[4470b10] | 2106 | "Polydispersion: %s"%msg)) |
---|
| 2107 | return |
---|
| 2108 | |
---|
[1c1436d] | 2109 | self._reset_dispersity() |
---|
[1467e1a6] | 2110 | |
---|
[70c57ebf] | 2111 | if self.model ==None: |
---|
| 2112 | self.model_disp.Hide() |
---|
| 2113 | self.sizer4_4.Clear(True) |
---|
| 2114 | return |
---|
[b421b1a] | 2115 | |
---|
[1c1436d] | 2116 | if self.enable_disp.GetValue(): |
---|
| 2117 | ## layout for model containing no dispersity parameters |
---|
[f20767b] | 2118 | |
---|
[71f0373] | 2119 | self.disp_list= self.model.getDispParamList() |
---|
[f20767b] | 2120 | |
---|
| 2121 | if len(self.disp_list)==0 and len(self.disp_cb_dict)==0: |
---|
[1c1436d] | 2122 | self._layout_sizer_noDipers() |
---|
[c77d859] | 2123 | else: |
---|
[1c1436d] | 2124 | ## set gaussian sizer |
---|
| 2125 | self._on_select_Disp(event=None) |
---|
| 2126 | else: |
---|
| 2127 | self.sizer4_4.Clear(True) |
---|
[f20767b] | 2128 | |
---|
[240b9966] | 2129 | ## post state to fit panel |
---|
[1c1436d] | 2130 | self.save_current_state() |
---|
[240b9966] | 2131 | if event !=None: |
---|
[3a37fe0] | 2132 | #self._undo.Enable(True) |
---|
[240b9966] | 2133 | event = PageInfoEvent(page = self) |
---|
| 2134 | wx.PostEvent(self.parent, event) |
---|
[1467e1a6] | 2135 | #draw the model with the current dispersity |
---|
[f20767b] | 2136 | self._draw_model() |
---|
[f1aa385] | 2137 | self.sizer4_4.Layout() |
---|
| 2138 | self.sizer5.Layout() |
---|
[71f0373] | 2139 | self.Layout() |
---|
[edd166b] | 2140 | self.Refresh() |
---|
[3b605bb] | 2141 | |
---|
[c77d859] | 2142 | |
---|
| 2143 | def _layout_sizer_noDipers(self): |
---|
| 2144 | """ |
---|
[5062bbf] | 2145 | Draw a sizer with no dispersity info |
---|
[c77d859] | 2146 | """ |
---|
| 2147 | ix=0 |
---|
| 2148 | iy=1 |
---|
| 2149 | self.fittable_param=[] |
---|
| 2150 | self.fixed_param=[] |
---|
[1c1436d] | 2151 | self.orientation_params_disp=[] |
---|
| 2152 | |
---|
[c77d859] | 2153 | self.sizer4_4.Clear(True) |
---|
[01b6bd0] | 2154 | text = "No polydispersity available for this model" |
---|
[2296316] | 2155 | text = "No polydispersity available for this model" |
---|
[01b6bd0] | 2156 | model_disp = wx.StaticText(self, -1, text) |
---|
[2296316] | 2157 | self.sizer4_4.Add(model_disp,( iy, ix),(1,1), |
---|
| 2158 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 10) |
---|
[dcf29d7] | 2159 | self.sizer4_4.Layout() |
---|
[c77d859] | 2160 | self.sizer4.Layout() |
---|
[340c2b3] | 2161 | |
---|
[c77d859] | 2162 | def _reset_dispersity(self): |
---|
| 2163 | """ |
---|
[5062bbf] | 2164 | put gaussian dispersity into current model |
---|
[c77d859] | 2165 | """ |
---|
[1c1436d] | 2166 | if len(self.param_toFit)>0: |
---|
| 2167 | for item in self.fittable_param: |
---|
[513115c] | 2168 | if item in self.param_toFit: |
---|
[1c1436d] | 2169 | self.param_toFit.remove(item) |
---|
[edd166b] | 2170 | |
---|
[1c1436d] | 2171 | for item in self.orientation_params_disp: |
---|
[513115c] | 2172 | if item in self.param_toFit: |
---|
[1c1436d] | 2173 | self.param_toFit.remove(item) |
---|
[513115c] | 2174 | |
---|
[c77d859] | 2175 | self.fittable_param=[] |
---|
| 2176 | self.fixed_param=[] |
---|
[5812a55] | 2177 | self.orientation_params_disp=[] |
---|
[2296316] | 2178 | self.values={} |
---|
| 2179 | self.weights={} |
---|
[f20767b] | 2180 | |
---|
| 2181 | from sans.models.dispersion_models import GaussianDispersion, ArrayDispersion |
---|
[c77d859] | 2182 | if len(self.disp_cb_dict)==0: |
---|
[e2f7b92] | 2183 | self.save_current_state() |
---|
[3b605bb] | 2184 | self.sizer4_4.Clear(True) |
---|
| 2185 | self.Layout() |
---|
[7975f2b] | 2186 | |
---|
[c77d859] | 2187 | return |
---|
[f20767b] | 2188 | if (len(self.disp_cb_dict)>0) : |
---|
| 2189 | for p in self.disp_cb_dict: |
---|
| 2190 | # The parameter was un-selected. Go back to Gaussian model (with 0 pts) |
---|
| 2191 | disp_model = GaussianDispersion() |
---|
| 2192 | |
---|
[ff8f99b] | 2193 | self._disp_obj_dict[p] = disp_model |
---|
| 2194 | # Set the new model as the dispersion object for the selected parameter |
---|
[f20767b] | 2195 | try: |
---|
[ff8f99b] | 2196 | self.model.set_dispersion(p, disp_model) |
---|
[f20767b] | 2197 | except: |
---|
[c985bef] | 2198 | |
---|
[f20767b] | 2199 | pass |
---|
[7975f2b] | 2200 | |
---|
[240b9966] | 2201 | ## save state into |
---|
| 2202 | self.save_current_state() |
---|
[7975f2b] | 2203 | self.Layout() |
---|
[c985bef] | 2204 | self.Refresh() |
---|
[7975f2b] | 2205 | |
---|
[c77d859] | 2206 | def _on_select_Disp(self,event): |
---|
| 2207 | """ |
---|
[5062bbf] | 2208 | allow selecting different dispersion |
---|
| 2209 | self.disp_list should change type later .now only gaussian |
---|
[c77d859] | 2210 | """ |
---|
[2296316] | 2211 | self._set_sizer_dispersion() |
---|
| 2212 | |
---|
[6e9150d] | 2213 | ## Redraw the model |
---|
| 2214 | self._draw_model() |
---|
[3a37fe0] | 2215 | #self._undo.Enable(True) |
---|
[3b9e023] | 2216 | event = PageInfoEvent(page = self) |
---|
| 2217 | wx.PostEvent(self.parent, event) |
---|
| 2218 | |
---|
[7975f2b] | 2219 | self.sizer4_4.Layout() |
---|
| 2220 | self.sizer4.Layout() |
---|
[36cb4d2f] | 2221 | self.SetupScrolling() |
---|
[340c2b3] | 2222 | |
---|
[2296316] | 2223 | def _on_disp_func(self, event=None): |
---|
[c77d859] | 2224 | """ |
---|
[2296316] | 2225 | Select a distribution function for the polydispersion |
---|
[f20767b] | 2226 | |
---|
[2296316] | 2227 | :Param event: ComboBox event |
---|
| 2228 | """ |
---|
| 2229 | # get ready for new event |
---|
| 2230 | if event != None: |
---|
| 2231 | event.Skip() |
---|
| 2232 | # Get event object |
---|
| 2233 | disp_box = event.GetEventObject() |
---|
| 2234 | |
---|
| 2235 | # Try to select a Distr. function |
---|
| 2236 | try: |
---|
| 2237 | disp_box.SetBackgroundColour("white") |
---|
| 2238 | selection = disp_box.GetCurrentSelection() |
---|
| 2239 | param_name = disp_box.Name.split('.')[0] |
---|
| 2240 | disp_name = disp_box.GetValue() |
---|
| 2241 | dispersity= disp_box.GetClientData(selection) |
---|
| 2242 | |
---|
| 2243 | #disp_model = GaussianDispersion() |
---|
| 2244 | disp_model = dispersity() |
---|
| 2245 | # Get param names to reset the values of the param |
---|
| 2246 | name1 = param_name + ".width" |
---|
| 2247 | name2 = param_name + ".npts" |
---|
| 2248 | name3 = param_name + ".nsigmas" |
---|
| 2249 | # Check Disp. function whether or not it is 'array' |
---|
| 2250 | if disp_name.lower() == "array": |
---|
| 2251 | value2= "" |
---|
| 2252 | value3= "" |
---|
| 2253 | value1 = self._set_array_disp(name=name1, disp=disp_model) |
---|
| 2254 | else: |
---|
| 2255 | self._del_array_values(name1) |
---|
| 2256 | #self._reset_array_disp(param_name) |
---|
| 2257 | self._disp_obj_dict[name1] = disp_model |
---|
| 2258 | self.model.set_dispersion(param_name, disp_model) |
---|
| 2259 | self.state._disp_obj_dict[name1]= disp_model |
---|
| 2260 | |
---|
| 2261 | value1= str(format_number(self.model.getParam(name1), True)) |
---|
| 2262 | value2= str(format_number(self.model.getParam(name2))) |
---|
| 2263 | value3= str(format_number(self.model.getParam(name3))) |
---|
| 2264 | # Reset fittable polydispersin parameter value |
---|
[513115c] | 2265 | for item in self.fittable_param: |
---|
[2296316] | 2266 | if item[1] == name1: |
---|
| 2267 | item[2].SetValue(value1) |
---|
| 2268 | item[5].SetValue("") |
---|
| 2269 | item[6].SetValue("") |
---|
| 2270 | # Disable for array |
---|
| 2271 | if disp_name.lower() == "array": |
---|
| 2272 | item[0].SetValue(False) |
---|
| 2273 | item[0].Disable() |
---|
| 2274 | item[2].Disable() |
---|
| 2275 | item[5].Disable() |
---|
| 2276 | item[6].Disable() |
---|
| 2277 | else: |
---|
| 2278 | item[0].Enable() |
---|
| 2279 | item[2].Enable() |
---|
| 2280 | item[5].Enable() |
---|
| 2281 | item[6].Enable() |
---|
| 2282 | break |
---|
| 2283 | # Reset fixed polydispersion params |
---|
| 2284 | for item in self.fixed_param: |
---|
| 2285 | if item[1] == name2: |
---|
| 2286 | item[2].SetValue(value2) |
---|
| 2287 | # Disable Npts for array |
---|
| 2288 | if disp_name.lower() == "array": |
---|
| 2289 | item[2].Disable() |
---|
| 2290 | else: |
---|
| 2291 | item[2].Enable() |
---|
| 2292 | if item[1] == name3: |
---|
| 2293 | item[2].SetValue(value3) |
---|
| 2294 | # Disable Nsigs for array |
---|
| 2295 | if disp_name.lower() == "array": |
---|
| 2296 | item[2].Disable() |
---|
| 2297 | else: |
---|
| 2298 | item[2].Enable() |
---|
| 2299 | |
---|
| 2300 | # Make sure the check box updated when all checked |
---|
| 2301 | if self.cb1.GetValue(): |
---|
| 2302 | self.select_all_param(None) |
---|
| 2303 | |
---|
| 2304 | # update params |
---|
| 2305 | self._update_paramv_on_fit() |
---|
| 2306 | # draw |
---|
| 2307 | self._draw_model() |
---|
| 2308 | self.Refresh() |
---|
| 2309 | except: |
---|
| 2310 | # Error msg |
---|
| 2311 | msg = "Error occurred:" |
---|
| 2312 | msg += " Could not select the distribution function..." |
---|
| 2313 | msg += " Please select another distribution function." |
---|
| 2314 | disp_box.SetBackgroundColour("pink") |
---|
| 2315 | # Focus on Fit button so that users can see the pinky box |
---|
| 2316 | self.btFit.SetFocus() |
---|
| 2317 | wx.PostEvent(self.parent.parent, |
---|
| 2318 | StatusEvent(status=msg, info="error")) |
---|
| 2319 | |
---|
| 2320 | |
---|
| 2321 | def _set_array_disp(self, name=None, disp=None): |
---|
| 2322 | """ |
---|
| 2323 | Set array dispersion |
---|
| 2324 | |
---|
| 2325 | :param name: name of the parameter for the dispersion to be set |
---|
| 2326 | :param disp: the polydisperion object |
---|
| 2327 | """ |
---|
| 2328 | # The user wants this parameter to be averaged. |
---|
| 2329 | # Pop up the file selection dialog. |
---|
| 2330 | path = self._selectDlg() |
---|
| 2331 | # Array data |
---|
| 2332 | values = [] |
---|
| 2333 | weights = [] |
---|
| 2334 | # If nothing was selected, just return |
---|
| 2335 | if path is None: |
---|
| 2336 | self.disp_cb_dict[name].SetValue(False) |
---|
| 2337 | #self.noDisper_rbox.SetValue(True) |
---|
| 2338 | return |
---|
| 2339 | self._default_save_location = os.path.dirname(path) |
---|
[f20767b] | 2340 | |
---|
[2296316] | 2341 | basename = os.path.basename(path) |
---|
| 2342 | values,weights = self.read_file(path) |
---|
[3b9e023] | 2343 | |
---|
[2296316] | 2344 | # If any of the two arrays is empty, notify the user that we won't |
---|
| 2345 | # proceed |
---|
| 2346 | if len(self.param_toFit)>0: |
---|
| 2347 | if name in self.param_toFit: |
---|
| 2348 | self.param_toFit.remove(name) |
---|
| 2349 | |
---|
| 2350 | # Tell the user that we are about to apply the distribution |
---|
| 2351 | msg = "Applying loaded %s distribution: %s" % (name, path) |
---|
| 2352 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg)) |
---|
| 2353 | |
---|
| 2354 | disp.set_weights(values, weights) |
---|
| 2355 | self._disp_obj_dict[name] = disp |
---|
| 2356 | self.model.set_dispersion(name.split('.')[0], disp) |
---|
| 2357 | self.state._disp_obj_dict[name]= disp |
---|
| 2358 | self.values[name] = values |
---|
| 2359 | self.weights[name] = weights |
---|
| 2360 | # Store the object to make it persist outside the |
---|
| 2361 | # scope of this method |
---|
| 2362 | #TODO: refactor model to clean this up? |
---|
| 2363 | self.state.values = {} |
---|
| 2364 | self.state.weights = {} |
---|
| 2365 | self.state.values = copy.deepcopy(self.values) |
---|
| 2366 | self.state.weights = copy.deepcopy(self.weights) |
---|
| 2367 | |
---|
| 2368 | # Set the new model as the dispersion object for the |
---|
| 2369 | #selected parameter |
---|
| 2370 | #self.model.set_dispersion(p, disp_model) |
---|
| 2371 | # Store a reference to the weights in the model object |
---|
| 2372 | #so that |
---|
| 2373 | # it's not lost when we use the model within another thread. |
---|
| 2374 | #TODO: total hack - fix this |
---|
| 2375 | self.state.model= self.model.clone() |
---|
| 2376 | self.model._persistency_dict[name.split('.')[0]] = \ |
---|
| 2377 | [values, weights] |
---|
| 2378 | self.state.model._persistency_dict[name.split('.')[0]] = \ |
---|
| 2379 | [values,weights] |
---|
| 2380 | return basename |
---|
| 2381 | |
---|
| 2382 | def _del_array_values(self, name=None): |
---|
| 2383 | """ |
---|
| 2384 | Reset array dispersion |
---|
[f20767b] | 2385 | |
---|
[2296316] | 2386 | :param name: name of the parameter for the dispersion to be set |
---|
| 2387 | """ |
---|
| 2388 | # Try to delete values and weight of the names array dic if exists |
---|
| 2389 | try: |
---|
| 2390 | del self.values[name] |
---|
| 2391 | del self.weights[name] |
---|
| 2392 | # delete all other dic |
---|
| 2393 | del self.state.values[name] |
---|
| 2394 | del self.state.weights[name] |
---|
| 2395 | del self.model._persistency_dict[name.split('.')[0]] |
---|
| 2396 | del self.state.model._persistency_dict[name.split('.')[0]] |
---|
| 2397 | except: |
---|
| 2398 | pass |
---|
| 2399 | |
---|
[7975f2b] | 2400 | def _lay_out(self): |
---|
| 2401 | """ |
---|
[5062bbf] | 2402 | returns self.Layout |
---|
| 2403 | |
---|
| 2404 | :Note: Mac seems to like this better when self. |
---|
| 2405 | Layout is called after fitting. |
---|
[7975f2b] | 2406 | """ |
---|
| 2407 | self._sleep4sec() |
---|
| 2408 | self.Layout() |
---|
| 2409 | return |
---|
| 2410 | |
---|
| 2411 | def _sleep4sec(self): |
---|
| 2412 | """ |
---|
[12eac73] | 2413 | sleep for 1 sec only applied on Mac |
---|
| 2414 | Note: This 1sec helps for Mac not to crash on self.:ayout after self._draw_model |
---|
[7975f2b] | 2415 | """ |
---|
| 2416 | if ON_MAC == True: |
---|
[12eac73] | 2417 | time.sleep(1) |
---|
[f72333f] | 2418 | |
---|
[2296316] | 2419 | def _find_polyfunc_selection(self, disp_func = None): |
---|
| 2420 | """ |
---|
| 2421 | FInd Comboox selection from disp_func |
---|
| 2422 | |
---|
| 2423 | :param disp_function: dispersion distr. function |
---|
| 2424 | """ |
---|
| 2425 | # List of the poly_model name in the combobox |
---|
| 2426 | list = ["RectangleDispersion", "ArrayDispersion", |
---|
| 2427 | "LogNormalDispersion", "GaussianDispersion", |
---|
| 2428 | "SchulzDispersion"] |
---|
| 2429 | |
---|
| 2430 | # Find the selection |
---|
| 2431 | try: |
---|
| 2432 | selection = list.index(disp_func.__class__.__name__) |
---|
| 2433 | return selection |
---|
| 2434 | except: |
---|
| 2435 | return 3 |
---|
| 2436 | |
---|
[904168e1] | 2437 | def on_reset_clicked(self,event): |
---|
| 2438 | """ |
---|
[5062bbf] | 2439 | On 'Reset' button for Q range clicked |
---|
[904168e1] | 2440 | """ |
---|
[7609f1a] | 2441 | flag = True |
---|
[6bbeacd4] | 2442 | #if self.check_invalid_panel(): |
---|
| 2443 | # return |
---|
[904168e1] | 2444 | ##For 3 different cases: Data2D, Data1D, and theory |
---|
[06aa2eeb] | 2445 | if self.model == None: |
---|
| 2446 | msg="Please select a model first..." |
---|
| 2447 | wx.MessageBox(msg, 'Info') |
---|
| 2448 | flag = False |
---|
| 2449 | return |
---|
| 2450 | |
---|
| 2451 | elif self.data.__class__.__name__ == "Data2D": |
---|
[904168e1] | 2452 | data_min= 0 |
---|
| 2453 | x= max(math.fabs(self.data.xmin), math.fabs(self.data.xmax)) |
---|
| 2454 | y= max(math.fabs(self.data.ymin), math.fabs(self.data.ymax)) |
---|
| 2455 | self.qmin_x = data_min |
---|
| 2456 | self.qmax_x = math.sqrt(x*x + y*y) |
---|
[f72333f] | 2457 | # check smearing |
---|
| 2458 | if not self.disable_smearer.GetValue(): |
---|
| 2459 | temp_smearer= self.current_smearer |
---|
| 2460 | ## set smearing value whether or not the data contain the smearing info |
---|
| 2461 | if self.pinhole_smearer.GetValue(): |
---|
| 2462 | flag = self.update_pinhole_smear() |
---|
| 2463 | else: |
---|
| 2464 | flag = True |
---|
[06aa2eeb] | 2465 | |
---|
| 2466 | elif self.data == None: |
---|
| 2467 | self.qmin_x = _QMIN_DEFAULT |
---|
| 2468 | self.qmax_x = _QMAX_DEFAULT |
---|
| 2469 | self.num_points = _NPTS_DEFAULT |
---|
| 2470 | self.state.npts = self.num_points |
---|
| 2471 | |
---|
[6318298] | 2472 | elif self.data.__class__.__name__ != "Data2D": |
---|
[904168e1] | 2473 | self.qmin_x = min(self.data.x) |
---|
| 2474 | self.qmax_x = max(self.data.x) |
---|
[7609f1a] | 2475 | # check smearing |
---|
| 2476 | if not self.disable_smearer.GetValue(): |
---|
| 2477 | temp_smearer= self.current_smearer |
---|
| 2478 | ## set smearing value whether or not the data contain the smearing info |
---|
| 2479 | if self.slit_smearer.GetValue(): |
---|
| 2480 | flag = self.update_slit_smear() |
---|
| 2481 | elif self.pinhole_smearer.GetValue(): |
---|
| 2482 | flag = self.update_pinhole_smear() |
---|
| 2483 | else: |
---|
| 2484 | flag = True |
---|
[904168e1] | 2485 | else: |
---|
[06aa2eeb] | 2486 | flag = False |
---|
[7975f2b] | 2487 | |
---|
[7609f1a] | 2488 | if flag == False: |
---|
| 2489 | msg= "Cannot Plot :Must enter a number!!! " |
---|
| 2490 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
[51a71a3] | 2491 | else: |
---|
| 2492 | # set relative text ctrs. |
---|
[f95301b] | 2493 | self.qmin.SetValue(str(self.qmin_x)) |
---|
[51a71a3] | 2494 | self.qmax.SetValue(str(self.qmax_x)) |
---|
| 2495 | self.set_npts2fit() |
---|
| 2496 | # At this point, some button and variables satatus (disabled?) should be checked |
---|
| 2497 | # such as color that should be reset to white in case that it was pink. |
---|
| 2498 | self._onparamEnter_helper() |
---|
| 2499 | |
---|
[7609f1a] | 2500 | self.save_current_state() |
---|
[904168e1] | 2501 | self.state.qmin = self.qmin_x |
---|
| 2502 | self.state.qmax = self.qmax_x |
---|
[00c3aac] | 2503 | |
---|
[904168e1] | 2504 | #reset the q range values |
---|
| 2505 | self._reset_plotting_range(self.state) |
---|
[f72333f] | 2506 | #self.compute_chisqr(smearer=self.current_smearer) |
---|
[904168e1] | 2507 | #Re draw plot |
---|
| 2508 | self._draw_model() |
---|
[2296316] | 2509 | |
---|
| 2510 | def get_images(self): |
---|
| 2511 | """ |
---|
| 2512 | Get the images of the plots corresponding this panel for report |
---|
| 2513 | |
---|
| 2514 | : return graphs: list of figures |
---|
| 2515 | : TODO: Move to guiframe |
---|
| 2516 | """ |
---|
| 2517 | # set list of graphs |
---|
| 2518 | graphs = [] |
---|
| 2519 | canvases = [] |
---|
| 2520 | # call gui_manager |
---|
| 2521 | gui_manager = self.parent.parent |
---|
| 2522 | # loops through the panels [dic] |
---|
| 2523 | for item1, item2 in gui_manager.panels.iteritems(): |
---|
| 2524 | data_title = self.data.group_id |
---|
[69f9084] | 2525 | data_name = str(self.data.name).split(" [")[0] |
---|
[2296316] | 2526 | # try to get all plots belonging to this control panel |
---|
| 2527 | try: |
---|
[818589a] | 2528 | title = '' |
---|
[2296316] | 2529 | # check titles (main plot) |
---|
[818589a] | 2530 | if hasattr(item2,"data2D"): |
---|
| 2531 | title = item2.data2D.title |
---|
[2296316] | 2532 | # and data_names (model plot[2D], and residuals) |
---|
| 2533 | if item2.group_id == data_title or \ |
---|
[818589a] | 2534 | title.count(data_name) or \ |
---|
| 2535 | item2.window_name.count(data_name) or \ |
---|
[aad74b3] | 2536 | item2.window_caption.count(data_name): |
---|
[2296316] | 2537 | #panel = gui_manager._mgr.GetPane(item2.window_name) |
---|
| 2538 | # append to the list |
---|
| 2539 | graphs.append(item2.figure) |
---|
| 2540 | canvases.append(item2.canvas) |
---|
| 2541 | except: |
---|
| 2542 | # Not for control panels |
---|
| 2543 | pass |
---|
| 2544 | # return the list of graphs |
---|
| 2545 | return graphs, canvases |
---|
[904168e1] | 2546 | |
---|
[7ad6ff5] | 2547 | def on_model_help_clicked(self,event): |
---|
| 2548 | """ |
---|
[5062bbf] | 2549 | on 'More details' button |
---|
[7ad6ff5] | 2550 | """ |
---|
[484faf7] | 2551 | from help_panel import HelpWindow |
---|
[2296316] | 2552 | import sans.models as models |
---|
[c77d859] | 2553 | |
---|
[2296316] | 2554 | # Get models help model_function path |
---|
| 2555 | path = models.get_data_path(media='media') |
---|
| 2556 | model_path = os.path.join(path,"model_functions.html") |
---|
[7ad6ff5] | 2557 | if self.model == None: |
---|
| 2558 | name = 'FuncHelp' |
---|
| 2559 | else: |
---|
[500f9f2] | 2560 | name = self.formfactorbox.GetValue() |
---|
| 2561 | #name = self.model.__class__.__name__ |
---|
[2296316] | 2562 | frame = HelpWindow(None, -1, pageToOpen=model_path) |
---|
[7ad6ff5] | 2563 | frame.Show(True) |
---|
| 2564 | if frame.rhelp.HasAnchor(name): |
---|
| 2565 | frame.rhelp.ScrollToAnchor(name) |
---|
| 2566 | else: |
---|
| 2567 | msg= "Model does not contains an available description " |
---|
| 2568 | msg +="Please try searching in the Help window" |
---|
[2296316] | 2569 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
| 2570 | |
---|
| 2571 | def on_pd_help_clicked(self, event): |
---|
| 2572 | """ |
---|
| 2573 | Button event for PD help |
---|
| 2574 | """ |
---|
| 2575 | from help_panel import HelpWindow |
---|
| 2576 | import sans.models as models |
---|
| 2577 | |
---|
| 2578 | # Get models help model_function path |
---|
| 2579 | path = models.get_data_path(media='media') |
---|
| 2580 | pd_path = os.path.join(path,"pd_help.html") |
---|
| 2581 | |
---|
| 2582 | frame = HelpWindow(None, -1, pageToOpen=pd_path) |
---|
| 2583 | frame.Show(True) |
---|
| 2584 | |
---|
| 2585 | def on_left_down(self, event): |
---|
| 2586 | """ |
---|
| 2587 | Get key stroke event |
---|
| 2588 | """ |
---|
| 2589 | # Figuring out key combo: Cmd for copy, Alt for paste |
---|
| 2590 | if event.CmdDown() and event.ShiftDown(): |
---|
| 2591 | flag = self.get_paste() |
---|
| 2592 | elif event.CmdDown(): |
---|
| 2593 | flag = self.get_copy() |
---|
| 2594 | else: |
---|
| 2595 | event.Skip() |
---|
| 2596 | return |
---|
| 2597 | # make event free |
---|
| 2598 | event.Skip() |
---|
| 2599 | # messages depending on the flag |
---|
| 2600 | if flag == None: |
---|
| 2601 | msg = " Parameter values are copied to the clipboard..." |
---|
| 2602 | infor = 'warning' |
---|
| 2603 | elif flag: |
---|
| 2604 | msg = " Parameter values are pasted from the clipboad..." |
---|
| 2605 | infor = "warning" |
---|
| 2606 | else: |
---|
| 2607 | msg = "Error was occured during pasting the parameter values " |
---|
| 2608 | msg += "from the clipboard..." |
---|
| 2609 | infor = "error" |
---|
| 2610 | # inform msg to wx |
---|
| 2611 | wx.PostEvent( self.parent.parent, |
---|
| 2612 | StatusEvent(status= msg, info=infor)) |
---|
| 2613 | |
---|
| 2614 | |
---|
| 2615 | def get_copy(self): |
---|
| 2616 | """ |
---|
| 2617 | Get the string copies of the param names and values in the tap |
---|
| 2618 | """ |
---|
| 2619 | content = 'sansview_parameter_values:' |
---|
| 2620 | # Do it if params exist |
---|
| 2621 | if self.parameters !=[]: |
---|
| 2622 | |
---|
| 2623 | # go through the parameters |
---|
| 2624 | string = self._get_copy_helper(self.parameters, |
---|
| 2625 | self.orientation_params) |
---|
| 2626 | content += string |
---|
| 2627 | |
---|
| 2628 | # go through the fittables |
---|
| 2629 | string = self._get_copy_helper(self.fittable_param, |
---|
| 2630 | self.orientation_params_disp) |
---|
| 2631 | content += string |
---|
| 2632 | |
---|
| 2633 | # go through the fixed params |
---|
| 2634 | string = self._get_copy_helper(self.fixed_param, |
---|
| 2635 | self.orientation_params_disp) |
---|
| 2636 | content += string |
---|
| 2637 | |
---|
| 2638 | # go through the str params |
---|
| 2639 | string = self._get_copy_helper(self.str_parameters, |
---|
| 2640 | self.orientation_params) |
---|
| 2641 | content += string |
---|
| 2642 | |
---|
| 2643 | if wx.TheClipboard.Open(): |
---|
| 2644 | wx.TheClipboard.SetData(wx.TextDataObject(str(content))) |
---|
| 2645 | data = wx.TextDataObject() |
---|
| 2646 | success = wx.TheClipboard.GetData(data) |
---|
| 2647 | text = data.GetText() |
---|
| 2648 | wx.TheClipboard.Close() |
---|
| 2649 | |
---|
| 2650 | return None |
---|
| 2651 | |
---|
| 2652 | def _get_copy_helper(self, param, orient_param): |
---|
| 2653 | """ |
---|
| 2654 | Helping get value and name of the params |
---|
| 2655 | |
---|
| 2656 | : param param: parameters |
---|
| 2657 | : param orient_param = oritational params |
---|
| 2658 | : return content: strings [list] [name,value:....] |
---|
| 2659 | """ |
---|
| 2660 | content = '' |
---|
| 2661 | # go through the str params |
---|
| 2662 | for item in param: |
---|
| 2663 | # 2D |
---|
| 2664 | if self.data.__class__.__name__== "Data2D": |
---|
| 2665 | name = item[1] |
---|
| 2666 | value = item[2].GetValue() |
---|
| 2667 | # 1D |
---|
| 2668 | else: |
---|
| 2669 | ## for 1D all parameters except orientation |
---|
| 2670 | if not item[1] in orient_param: |
---|
| 2671 | name = item[1] |
---|
| 2672 | value = item[2].GetValue() |
---|
| 2673 | # add to the content |
---|
| 2674 | content += name + ',' + value + ':' |
---|
| 2675 | |
---|
| 2676 | return content |
---|
| 2677 | |
---|
| 2678 | def get_paste(self): |
---|
| 2679 | """ |
---|
| 2680 | Get the string copies of the param names and values in the tap |
---|
| 2681 | """ |
---|
| 2682 | context = {} |
---|
| 2683 | text = "" |
---|
| 2684 | |
---|
| 2685 | # Get text from the clip board |
---|
| 2686 | if wx.TheClipboard.Open(): |
---|
| 2687 | if wx.TheClipboard.IsSupported(wx.DataFormat(wx.DF_TEXT)): |
---|
| 2688 | data = wx.TextDataObject() |
---|
| 2689 | # get wx dataobject |
---|
| 2690 | success = wx.TheClipboard.GetData(data) |
---|
| 2691 | # get text |
---|
| 2692 | text = data.GetText() |
---|
| 2693 | # close clipboard |
---|
| 2694 | wx.TheClipboard.Close() |
---|
| 2695 | |
---|
| 2696 | # put the text into dictionary |
---|
| 2697 | lines = text.split(':') |
---|
| 2698 | if lines[0] != 'sansview_parameter_values': |
---|
| 2699 | return False |
---|
| 2700 | for line in lines[1:-1]: |
---|
| 2701 | if len(line) != 0: |
---|
| 2702 | item =line.split(',') |
---|
| 2703 | name = item[0] |
---|
| 2704 | value = item[1] |
---|
| 2705 | # Transfer the text to content[dictionary] |
---|
| 2706 | context[name] = value |
---|
| 2707 | |
---|
| 2708 | # Do it if params exist |
---|
| 2709 | if self.parameters != []: |
---|
| 2710 | # go through the parameters |
---|
| 2711 | self._get_paste_helper(self.parameters, |
---|
| 2712 | self.orientation_params, context) |
---|
| 2713 | |
---|
| 2714 | # go through the fittables |
---|
| 2715 | self._get_paste_helper(self.fittable_param, |
---|
| 2716 | self.orientation_params_disp, context) |
---|
| 2717 | |
---|
| 2718 | # go through the fixed params |
---|
| 2719 | self._get_paste_helper(self.fixed_param, |
---|
| 2720 | self.orientation_params_disp, context) |
---|
| 2721 | |
---|
| 2722 | # go through the str params |
---|
| 2723 | self._get_paste_helper(self.str_parameters, |
---|
| 2724 | self.orientation_params, context) |
---|
| 2725 | |
---|
| 2726 | return True |
---|
| 2727 | |
---|
| 2728 | def _get_paste_helper(self, param, orient_param, content): |
---|
| 2729 | """ |
---|
| 2730 | Helping set values of the params |
---|
| 2731 | |
---|
| 2732 | : param param: parameters |
---|
| 2733 | : param orient_param: oritational params |
---|
| 2734 | : param content: dictionary [ name, value: name1.value1,...] |
---|
| 2735 | """ |
---|
| 2736 | # go through the str params |
---|
| 2737 | for item in param: |
---|
| 2738 | # 2D |
---|
| 2739 | if self.data.__class__.__name__== "Data2D": |
---|
| 2740 | name = item[1] |
---|
| 2741 | if name in content.keys(): |
---|
| 2742 | item[2].SetValue(content[name]) |
---|
| 2743 | # 1D |
---|
| 2744 | else: |
---|
| 2745 | ## for 1D all parameters except orientation |
---|
| 2746 | if not item[1] in orient_param: |
---|
| 2747 | name = item[1] |
---|
| 2748 | if name in content.keys(): |
---|
| 2749 | # Avoid changing combox content which needs special care |
---|
| 2750 | if item[2].__class__.__name__ != "ComboBox": |
---|
| 2751 | item[2].SetValue(content[name]) |
---|
| 2752 | |
---|
[c77d859] | 2753 | |
---|