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