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