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