[f0d720b] | 1 | """ |
---|
| 2 | Base Page for fitting |
---|
| 3 | """ |
---|
| 4 | import sys |
---|
| 5 | import os |
---|
| 6 | import wx |
---|
[9a5097c] | 7 | import numpy as np |
---|
[f0d720b] | 8 | import time |
---|
| 9 | import copy |
---|
| 10 | import math |
---|
| 11 | import json |
---|
[5ce7f17] | 12 | import logging |
---|
[7673ecd] | 13 | import traceback |
---|
| 14 | |
---|
[c5c247e] | 15 | from time import time |
---|
[9c9fae1] | 16 | from Queue import Queue |
---|
| 17 | from threading import Thread |
---|
[f0d720b] | 18 | from collections import defaultdict |
---|
| 19 | from wx.lib.scrolledpanel import ScrolledPanel |
---|
[7673ecd] | 20 | |
---|
[a0373d5] | 21 | from sasmodels.weights import MODELS as POLYDISPERSITY_MODELS |
---|
| 22 | |
---|
[d85c194] | 23 | from sas.sasgui.guiframe.panel_base import PanelBase |
---|
[c8e1996] | 24 | from sas.sasgui.guiframe.utils import format_number, check_float, IdList, \ |
---|
| 25 | check_int |
---|
[d85c194] | 26 | from sas.sasgui.guiframe.events import PanelOnFocusEvent |
---|
| 27 | from sas.sasgui.guiframe.events import StatusEvent |
---|
| 28 | from sas.sasgui.guiframe.events import AppendBookmarkEvent |
---|
| 29 | from sas.sasgui.guiframe.dataFitting import Data2D |
---|
| 30 | from sas.sasgui.guiframe.dataFitting import Data1D |
---|
| 31 | from sas.sasgui.guiframe.dataFitting import check_data_validity |
---|
| 32 | from sas.sasgui.guiframe.gui_style import GUIFRAME_ID |
---|
[b699768] | 33 | from sas.sascalc.dataloader.data_info import Detector |
---|
| 34 | from sas.sascalc.dataloader.data_info import Source |
---|
[d85c194] | 35 | from sas.sasgui.perspectives.fitting.pagestate import PageState |
---|
| 36 | from sas.sasgui.guiframe.CategoryInstaller import CategoryInstaller |
---|
| 37 | from sas.sasgui.guiframe.documentation_window import DocumentationWindow |
---|
[5ce7f17] | 38 | |
---|
[463e7ffc] | 39 | logger = logging.getLogger(__name__) |
---|
[f0d720b] | 40 | |
---|
| 41 | (PageInfoEvent, EVT_PAGE_INFO) = wx.lib.newevent.NewEvent() |
---|
| 42 | (PreviousStateEvent, EVT_PREVIOUS_STATE) = wx.lib.newevent.NewEvent() |
---|
| 43 | (NextStateEvent, EVT_NEXT_STATE) = wx.lib.newevent.NewEvent() |
---|
| 44 | |
---|
| 45 | _BOX_WIDTH = 76 |
---|
| 46 | _QMIN_DEFAULT = 0.0005 |
---|
| 47 | _QMAX_DEFAULT = 0.5 |
---|
| 48 | _NPTS_DEFAULT = 50 |
---|
[c8e1996] | 49 | # Control panel width |
---|
[f0d720b] | 50 | if sys.platform.count("win32") > 0: |
---|
| 51 | PANEL_WIDTH = 450 |
---|
| 52 | FONT_VARIANT = 0 |
---|
| 53 | ON_MAC = False |
---|
| 54 | else: |
---|
| 55 | PANEL_WIDTH = 500 |
---|
| 56 | FONT_VARIANT = 1 |
---|
| 57 | ON_MAC = True |
---|
| 58 | |
---|
[4387385] | 59 | CUSTOM_MODEL = 'Plugin Models' |
---|
| 60 | |
---|
[f0d720b] | 61 | class BasicPage(ScrolledPanel, PanelBase): |
---|
| 62 | """ |
---|
[e92a352] | 63 | This class provide general structure of the fitpanel page |
---|
[f0d720b] | 64 | """ |
---|
[c8e1996] | 65 | # Internal name for the AUI manager |
---|
[f0d720b] | 66 | window_name = "Fit Page" |
---|
[c8e1996] | 67 | # Title to appear on top of the window |
---|
[f0d720b] | 68 | window_caption = "Fit Page " |
---|
[02098e3] | 69 | |
---|
[6f16e25] | 70 | # These two buttons have specific IDs since they seem to be created more |
---|
| 71 | # frequently than they need to. In particular, set_dispers_sizer() is |
---|
| 72 | # called by _on_select_model |
---|
| 73 | ID_BOOKMARK = wx.NewId() |
---|
| 74 | ID_DISPERSER_HELP = wx.NewId() |
---|
| 75 | _id_pool = IdList() |
---|
[5ce7f17] | 76 | |
---|
[f0d720b] | 77 | def __init__(self, parent, color='blue', **kwargs): |
---|
| 78 | """ |
---|
| 79 | """ |
---|
| 80 | ScrolledPanel.__init__(self, parent, **kwargs) |
---|
| 81 | PanelBase.__init__(self, parent) |
---|
| 82 | self.SetupScrolling() |
---|
[c8e1996] | 83 | # Set window's font size |
---|
[f0d720b] | 84 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
| 85 | self.SetBackgroundColour(color) |
---|
[6f16e25] | 86 | |
---|
| 87 | self._ids = iter(self._id_pool) |
---|
[c8e1996] | 88 | # parent of the page |
---|
[f0d720b] | 89 | self.parent = parent |
---|
[c8e1996] | 90 | # manager is the fitting plugin |
---|
| 91 | # owner of the page (fitting plugin) |
---|
[f0d720b] | 92 | self.event_owner = None |
---|
[c8e1996] | 93 | # current model |
---|
[f0d720b] | 94 | self.model = None |
---|
| 95 | self.m_name = None |
---|
| 96 | self.index_model = None |
---|
| 97 | self.panel = None |
---|
[c8e1996] | 98 | # data |
---|
[f0d720b] | 99 | self.data = None |
---|
[c8e1996] | 100 | # list of available data |
---|
[f0d720b] | 101 | self.data_list = [] |
---|
| 102 | self.mask = None |
---|
| 103 | self.uid = wx.NewId() |
---|
| 104 | self.graph_id = None |
---|
[c8e1996] | 105 | # Q range for data set |
---|
[9a5097c] | 106 | self.qmin_data_set = np.inf |
---|
[f0d720b] | 107 | self.qmax_data_set = None |
---|
| 108 | self.npts_data_set = 0 |
---|
[c8e1996] | 109 | # Q range |
---|
[f0d720b] | 110 | self.qmin = None |
---|
| 111 | self.qmax = None |
---|
| 112 | self.qmax_x = _QMAX_DEFAULT |
---|
| 113 | self.qmin_x = _QMIN_DEFAULT |
---|
| 114 | self.npts_x = _NPTS_DEFAULT |
---|
[c8e1996] | 115 | # total number of point: float |
---|
[f0d720b] | 116 | self.npts = None |
---|
| 117 | self.num_points = None |
---|
[c8e1996] | 118 | # smear default |
---|
[f0d720b] | 119 | self.current_smearer = None |
---|
[c8e1996] | 120 | # 2D smear accuracy default |
---|
[f0d720b] | 121 | self.smear2d_accuracy = 'Low' |
---|
[c8e1996] | 122 | # slit smear: |
---|
[f0d720b] | 123 | self.dxl = None |
---|
| 124 | self.dxw = None |
---|
[c8e1996] | 125 | # pinhole smear |
---|
[7e98655] | 126 | self.dx_percent = None |
---|
[c8e1996] | 127 | # smear attrbs |
---|
[f0d720b] | 128 | self.enable_smearer = None |
---|
| 129 | self.disable_smearer = None |
---|
| 130 | self.pinhole_smearer = None |
---|
| 131 | self.slit_smearer = None |
---|
[c8e1996] | 132 | # weight attrbs |
---|
[f0d720b] | 133 | self.dI_noweight = None |
---|
| 134 | self.dI_didata = None |
---|
| 135 | self.dI_sqrdata = None |
---|
| 136 | self.dI_idata = None |
---|
[c8e1996] | 137 | # other attrbs |
---|
[f0d720b] | 138 | self.dq_l = None |
---|
| 139 | self.dq_r = None |
---|
| 140 | self.tcChi = None |
---|
| 141 | self.disp_box = None |
---|
| 142 | self.model_disp = None |
---|
| 143 | self.Npts_fit = None |
---|
| 144 | self.Npts_total = None |
---|
[5ce7f17] | 145 | self.theory_qmin = None |
---|
[f0d720b] | 146 | self.theory_qmax = None |
---|
| 147 | self.theory_qmin_x = None |
---|
| 148 | self.theory_qmax_x = None |
---|
| 149 | self.btEditMask = None |
---|
| 150 | self.btFit = None |
---|
| 151 | self.sld_axes = None |
---|
| 152 | self.multi_factor = None |
---|
[5ce7f17] | 153 | |
---|
[f0d720b] | 154 | self.disp_cb_dict = {} |
---|
[5ce7f17] | 155 | |
---|
[c8e1996] | 156 | # self.state = PageState(parent=parent) |
---|
| 157 | # dictionary containing list of models |
---|
[f0d720b] | 158 | self.model_list_box = {} |
---|
[5ce7f17] | 159 | |
---|
[c8e1996] | 160 | # Data member to store the dispersion object created |
---|
[f0d720b] | 161 | self._disp_obj_dict = {} |
---|
[c8e1996] | 162 | # selected parameters to apply dispersion |
---|
[f0d720b] | 163 | self.disp_cb_dict = {} |
---|
[c8e1996] | 164 | # smearer object |
---|
[f0d720b] | 165 | self.enable2D = False |
---|
| 166 | self._has_magnetic = False |
---|
| 167 | self.magnetic_on = False |
---|
| 168 | self.is_mac = ON_MAC |
---|
| 169 | self.formfactorbox = None |
---|
| 170 | self.structurebox = None |
---|
| 171 | self.categorybox = None |
---|
[c8e1996] | 172 | # list of model parameters. each item must have same length |
---|
| 173 | # each item related to a given parameters |
---|
| 174 | # [cb state, name, value, "+/-", error of fit, min, max , units] |
---|
[f0d720b] | 175 | self.parameters = [] |
---|
| 176 | # non-fittable parameter whose value is astring |
---|
| 177 | self.str_parameters = [] |
---|
[c8e1996] | 178 | # list of parameters to fit , must be like self.parameters |
---|
[f0d720b] | 179 | self.param_toFit = [] |
---|
[c8e1996] | 180 | # list of looking like parameters but with non fittable parameters info |
---|
[f0d720b] | 181 | self.fixed_param = [] |
---|
[c8e1996] | 182 | # list of looking like parameters but with fittable parameters info |
---|
[f0d720b] | 183 | self.fittable_param = [] |
---|
[c8e1996] | 184 | # list of dispersion parameters |
---|
[f0d720b] | 185 | self.disp_list = [] |
---|
| 186 | self.disp_name = "" |
---|
[5ce7f17] | 187 | |
---|
[c8e1996] | 188 | # list of orientation parameters |
---|
[f0d720b] | 189 | self.orientation_params = [] |
---|
| 190 | self.orientation_params_disp = [] |
---|
[5ce7f17] | 191 | # Self.model should ALWAYS be None here. It was set to none above in |
---|
[f0d720b] | 192 | # this long init setting. no obvious function call in between setting |
---|
[5ce7f17] | 193 | # and this - commenting out on 4/8/2014 by PDB. Remove once clear |
---|
[f0d720b] | 194 | # it is pointless. |
---|
[c8e1996] | 195 | # if self.model is not None: |
---|
[f0d720b] | 196 | # self.disp_list = self.model.getDispParamList() |
---|
| 197 | self.temp_multi_functional = False |
---|
[c8e1996] | 198 | # enable model 2D draw |
---|
[f0d720b] | 199 | self.enable2D = False |
---|
[c8e1996] | 200 | # check that the fit range is correct to plot the model again |
---|
[f0d720b] | 201 | self.fitrange = True |
---|
[c8e1996] | 202 | # Create memento to save the current state |
---|
[f0d720b] | 203 | self.state = PageState(parent=self.parent, |
---|
| 204 | model=self.model, data=self.data) |
---|
[c8e1996] | 205 | # flag to determine if state has change |
---|
[f0d720b] | 206 | self.state_change = False |
---|
[c8e1996] | 207 | # save customized array |
---|
[6ed67db] | 208 | self.values = {} # type: Dict[str, List[float, ...]] |
---|
| 209 | self.weights = {} # type: Dict[str, List[float, ...]] |
---|
[c8e1996] | 210 | # retrieve saved state |
---|
[f0d720b] | 211 | self.number_saved_state = 0 |
---|
[c8e1996] | 212 | # dictionary of saved state |
---|
[f0d720b] | 213 | self.saved_states = {} |
---|
[c8e1996] | 214 | # Create context menu for page |
---|
[f0d720b] | 215 | self.popUpMenu = wx.Menu() |
---|
[5ce7f17] | 216 | |
---|
[6f16e25] | 217 | wx_id = self._ids.next() |
---|
| 218 | self._keep = wx.MenuItem(self.popUpMenu, wx_id, "Add bookmark", |
---|
[f0d720b] | 219 | " Keep the panel status to recall it later") |
---|
| 220 | self.popUpMenu.AppendItem(self._keep) |
---|
| 221 | self._keep.Enable(False) |
---|
| 222 | self._set_bookmark_flag(False) |
---|
| 223 | self._set_save_flag(False) |
---|
[6f16e25] | 224 | wx.EVT_MENU(self, wx_id, self.on_bookmark) |
---|
[f0d720b] | 225 | self.popUpMenu.AppendSeparator() |
---|
[5ce7f17] | 226 | |
---|
[c8e1996] | 227 | # Default locations |
---|
[f0d720b] | 228 | self._default_save_location = os.getcwd() |
---|
[c8e1996] | 229 | # save initial state on context menu |
---|
| 230 | # self.onSave(event=None) |
---|
[f0d720b] | 231 | self.Bind(wx.EVT_CONTEXT_MENU, self.onContextMenu) |
---|
[5ce7f17] | 232 | |
---|
[f0d720b] | 233 | # bind key event |
---|
| 234 | self.Bind(wx.EVT_LEFT_DOWN, self.on_left_down) |
---|
[5ce7f17] | 235 | |
---|
[c8e1996] | 236 | # create the basic structure of the panel with empty sizer |
---|
[f0d720b] | 237 | self.define_page_structure() |
---|
[c8e1996] | 238 | # drawing Initial dispersion parameters sizer |
---|
[f0d720b] | 239 | self.set_dispers_sizer() |
---|
[5ce7f17] | 240 | |
---|
[c8e1996] | 241 | # layout |
---|
[f0d720b] | 242 | self.set_layout() |
---|
[5ce7f17] | 243 | |
---|
[c5c247e] | 244 | # Setting up a thread for the fitting |
---|
[9c9fae1] | 245 | self.threadedDrawQueue = Queue() |
---|
| 246 | |
---|
[4b402a1] | 247 | self.threadedDrawWorker = Thread(target = self._threaded_draw_worker, |
---|
| 248 | args = (self.threadedDrawQueue,)) |
---|
[9c9fae1] | 249 | self.threadedDrawWorker.setDaemon(True) |
---|
| 250 | self.threadedDrawWorker.start() |
---|
| 251 | |
---|
[c5c247e] | 252 | # And a home for the thread submission times |
---|
[5c6002a] | 253 | self.lastTimeFitSubmitted = 0.00 |
---|
[9c9fae1] | 254 | |
---|
[f0d720b] | 255 | def set_index_model(self, index): |
---|
| 256 | """ |
---|
| 257 | Index related to this page |
---|
| 258 | """ |
---|
| 259 | self.index_model = index |
---|
[5ce7f17] | 260 | |
---|
[f0d720b] | 261 | def create_default_data(self): |
---|
| 262 | """ |
---|
| 263 | Given the user selection, creates a 1D or 2D data |
---|
| 264 | Only when the page is on theory mode. |
---|
| 265 | """ |
---|
| 266 | if not hasattr(self, "model_view"): |
---|
| 267 | return |
---|
| 268 | toggle_mode_on = self.model_view.IsEnabled() |
---|
| 269 | if toggle_mode_on: |
---|
| 270 | if self.enable2D and not check_data_validity(self.data): |
---|
| 271 | self._create_default_2d_data() |
---|
| 272 | else: |
---|
| 273 | if self.pointsbox.GetValue(): |
---|
| 274 | self._create_log_1d_data() |
---|
| 275 | else: |
---|
| 276 | self._create_default_1d_data() |
---|
[5ce7f17] | 277 | |
---|
[c8e1996] | 278 | if self.model is not None: |
---|
[f0d720b] | 279 | if not self.data.is_data: |
---|
[c8e1996] | 280 | self._manager.page_finder[self.uid].set_fit_data( |
---|
| 281 | data=[self.data]) |
---|
[f0d720b] | 282 | self.on_smear_helper(update=True) |
---|
| 283 | self.state.enable_smearer = self.enable_smearer.GetValue() |
---|
| 284 | self.state.disable_smearer = self.disable_smearer.GetValue() |
---|
| 285 | self.state.pinhole_smearer = self.pinhole_smearer.GetValue() |
---|
| 286 | self.state.slit_smearer = self.slit_smearer.GetValue() |
---|
[5ce7f17] | 287 | |
---|
[f0d720b] | 288 | def _create_default_1d_data(self): |
---|
| 289 | """ |
---|
| 290 | Create default data for fitting perspective |
---|
| 291 | Only when the page is on theory mode. |
---|
| 292 | :warning: This data is never plotted. |
---|
[5ce7f17] | 293 | |
---|
[f0d720b] | 294 | """ |
---|
[9a5097c] | 295 | x = np.linspace(start=self.qmin_x, stop=self.qmax_x, |
---|
[f0d720b] | 296 | num=self.npts_x, endpoint=True) |
---|
| 297 | self.data = Data1D(x=x) |
---|
| 298 | self.data.xaxis('\\rm{Q}', "A^{-1}") |
---|
| 299 | self.data.yaxis('\\rm{Intensity}', "cm^{-1}") |
---|
| 300 | self.data.is_data = False |
---|
| 301 | self.data.id = str(self.uid) + " data" |
---|
| 302 | self.data.group_id = str(self.uid) + " Model1D" |
---|
[5ce7f17] | 303 | |
---|
[f0d720b] | 304 | def _create_log_1d_data(self): |
---|
| 305 | """ |
---|
| 306 | Create log-spaced data for fitting perspective |
---|
| 307 | Only when the page is on theory mode. |
---|
| 308 | :warning: This data is never plotted. |
---|
[5ce7f17] | 309 | |
---|
[f0d720b] | 310 | """ |
---|
| 311 | if self.qmin_x >= 1.e-10: |
---|
[9a5097c] | 312 | qmin = np.log10(self.qmin_x) |
---|
[f0d720b] | 313 | else: |
---|
[5ce7f17] | 314 | qmin = -10. |
---|
| 315 | |
---|
[f0d720b] | 316 | if self.qmax_x <= 1.e10: |
---|
[9a5097c] | 317 | qmax = np.log10(self.qmax_x) |
---|
[f0d720b] | 318 | else: |
---|
[5ce7f17] | 319 | qmax = 10. |
---|
| 320 | |
---|
[9a5097c] | 321 | x = np.logspace(start=qmin, stop=qmax, |
---|
[f0d720b] | 322 | num=self.npts_x, endpoint=True, base=10.0) |
---|
| 323 | self.data = Data1D(x=x) |
---|
| 324 | self.data.xaxis('\\rm{Q}', "A^{-1}") |
---|
| 325 | self.data.yaxis('\\rm{Intensity}', "cm^{-1}") |
---|
| 326 | self.data.is_data = False |
---|
| 327 | self.data.id = str(self.uid) + " data" |
---|
| 328 | self.data.group_id = str(self.uid) + " Model1D" |
---|
[5ce7f17] | 329 | |
---|
[f0d720b] | 330 | def _create_default_2d_data(self): |
---|
| 331 | """ |
---|
| 332 | Create 2D data by default |
---|
| 333 | Only when the page is on theory mode. |
---|
| 334 | :warning: This data is never plotted. |
---|
| 335 | """ |
---|
| 336 | self.data = Data2D() |
---|
| 337 | qmax = self.qmax_x / math.sqrt(2) |
---|
| 338 | self.data.xaxis('\\rm{Q_{x}}', 'A^{-1}') |
---|
| 339 | self.data.yaxis('\\rm{Q_{y}}', 'A^{-1}') |
---|
| 340 | self.data.is_data = False |
---|
| 341 | self.data.id = str(self.uid) + " data" |
---|
| 342 | self.data.group_id = str(self.uid) + " Model2D" |
---|
[c8e1996] | 343 | # Default values |
---|
[f0d720b] | 344 | self.data.detector.append(Detector()) |
---|
| 345 | index = len(self.data.detector) - 1 |
---|
| 346 | self.data.detector[index].distance = 8000 # mm |
---|
| 347 | self.data.source.wavelength = 6 # A |
---|
| 348 | self.data.detector[index].pixel_size.x = 5 # mm |
---|
| 349 | self.data.detector[index].pixel_size.y = 5 # mm |
---|
| 350 | self.data.detector[index].beam_center.x = qmax |
---|
| 351 | self.data.detector[index].beam_center.y = qmax |
---|
| 352 | xmax = qmax |
---|
| 353 | xmin = -qmax |
---|
| 354 | ymax = qmax |
---|
| 355 | ymin = -qmax |
---|
| 356 | qstep = self.npts_x |
---|
| 357 | |
---|
[9a5097c] | 358 | x = np.linspace(start=xmin, stop=xmax, num=qstep, endpoint=True) |
---|
| 359 | y = np.linspace(start=ymin, stop=ymax, num=qstep, endpoint=True) |
---|
[c8e1996] | 360 | # use data info instead |
---|
[9a5097c] | 361 | new_x = np.tile(x, (len(y), 1)) |
---|
| 362 | new_y = np.tile(y, (len(x), 1)) |
---|
[f0d720b] | 363 | new_y = new_y.swapaxes(0, 1) |
---|
| 364 | # all data reuire now in 1d array |
---|
| 365 | qx_data = new_x.flatten() |
---|
| 366 | qy_data = new_y.flatten() |
---|
[9a5097c] | 367 | q_data = np.sqrt(qx_data * qx_data + qy_data * qy_data) |
---|
[f0d720b] | 368 | # set all True (standing for unmasked) as default |
---|
[9a5097c] | 369 | mask = np.ones(len(qx_data), dtype=bool) |
---|
[f0d720b] | 370 | # store x and y bin centers in q space |
---|
| 371 | x_bins = x |
---|
| 372 | y_bins = y |
---|
[5ce7f17] | 373 | |
---|
[f0d720b] | 374 | self.data.source = Source() |
---|
[9a5097c] | 375 | self.data.data = np.ones(len(mask)) |
---|
| 376 | self.data.err_data = np.ones(len(mask)) |
---|
[f0d720b] | 377 | self.data.qx_data = qx_data |
---|
| 378 | self.data.qy_data = qy_data |
---|
| 379 | self.data.q_data = q_data |
---|
| 380 | self.data.mask = mask |
---|
| 381 | self.data.x_bins = x_bins |
---|
| 382 | self.data.y_bins = y_bins |
---|
| 383 | # max and min taking account of the bin sizes |
---|
| 384 | self.data.xmin = xmin |
---|
| 385 | self.data.xmax = xmax |
---|
| 386 | self.data.ymin = ymin |
---|
| 387 | self.data.ymax = ymax |
---|
| 388 | |
---|
| 389 | def on_set_focus(self, event): |
---|
| 390 | """ |
---|
| 391 | On Set Focus, update guimanger and menu |
---|
| 392 | """ |
---|
| 393 | if self._manager is not None: |
---|
| 394 | wx.PostEvent(self._manager.parent, PanelOnFocusEvent(panel=self)) |
---|
| 395 | self.on_tap_focus() |
---|
[5ce7f17] | 396 | |
---|
[f0d720b] | 397 | def on_tap_focus(self): |
---|
| 398 | """ |
---|
| 399 | Update menu1 on cliking the page tap |
---|
| 400 | """ |
---|
[c8e1996] | 401 | if self._manager.menu1 is not None: |
---|
| 402 | chain_menu = self._manager.menu1.FindItemById( |
---|
[f0d720b] | 403 | self._manager.id_reset_flag) |
---|
| 404 | chain_menu.Enable(self.batch_on) |
---|
| 405 | sim_menu = self._manager.menu1.FindItemById(self._manager.id_simfit) |
---|
| 406 | flag = self.data.is_data\ |
---|
[c8e1996] | 407 | and (self.model is not None) |
---|
[f0d720b] | 408 | sim_menu.Enable(not self.batch_on and flag) |
---|
| 409 | batch_menu = \ |
---|
| 410 | self._manager.menu1.FindItemById(self._manager.id_batchfit) |
---|
| 411 | batch_menu.Enable(self.batch_on and flag) |
---|
[5ce7f17] | 412 | |
---|
[f0d720b] | 413 | def onContextMenu(self, event): |
---|
| 414 | """ |
---|
| 415 | Retrieve the state selected state |
---|
| 416 | """ |
---|
| 417 | pos = event.GetPosition() |
---|
| 418 | pos = self.ScreenToClient(pos) |
---|
| 419 | self.PopupMenu(self.popUpMenu, pos) |
---|
[5ce7f17] | 420 | |
---|
[f0d720b] | 421 | def onUndo(self, event): |
---|
| 422 | """ |
---|
| 423 | Cancel the previous action |
---|
| 424 | """ |
---|
| 425 | event = PreviousStateEvent(page=self) |
---|
| 426 | wx.PostEvent(self.parent, event) |
---|
[5ce7f17] | 427 | |
---|
[f0d720b] | 428 | def onRedo(self, event): |
---|
| 429 | """ |
---|
| 430 | Restore the previous action cancelled |
---|
| 431 | """ |
---|
| 432 | event = NextStateEvent(page=self) |
---|
| 433 | wx.PostEvent(self.parent, event) |
---|
[5ce7f17] | 434 | |
---|
[f0d720b] | 435 | def define_page_structure(self): |
---|
| 436 | """ |
---|
| 437 | Create empty sizer for a panel |
---|
| 438 | """ |
---|
| 439 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
| 440 | self.sizer0 = wx.BoxSizer(wx.VERTICAL) |
---|
| 441 | self.sizer1 = wx.BoxSizer(wx.VERTICAL) |
---|
| 442 | self.sizer2 = wx.BoxSizer(wx.VERTICAL) |
---|
| 443 | self.sizer3 = wx.BoxSizer(wx.VERTICAL) |
---|
| 444 | self.sizer4 = wx.BoxSizer(wx.VERTICAL) |
---|
| 445 | self.sizer5 = wx.BoxSizer(wx.VERTICAL) |
---|
| 446 | self.sizer6 = wx.BoxSizer(wx.VERTICAL) |
---|
[5ce7f17] | 447 | |
---|
[f0d720b] | 448 | self.sizer0.SetMinSize((PANEL_WIDTH, -1)) |
---|
| 449 | self.sizer1.SetMinSize((PANEL_WIDTH, -1)) |
---|
| 450 | self.sizer2.SetMinSize((PANEL_WIDTH, -1)) |
---|
| 451 | self.sizer3.SetMinSize((PANEL_WIDTH, -1)) |
---|
| 452 | self.sizer4.SetMinSize((PANEL_WIDTH, -1)) |
---|
| 453 | self.sizer5.SetMinSize((PANEL_WIDTH, -1)) |
---|
| 454 | self.sizer6.SetMinSize((PANEL_WIDTH, -1)) |
---|
[5ce7f17] | 455 | |
---|
[f0d720b] | 456 | self.vbox.Add(self.sizer0) |
---|
| 457 | self.vbox.Add(self.sizer1) |
---|
| 458 | self.vbox.Add(self.sizer2) |
---|
| 459 | self.vbox.Add(self.sizer3) |
---|
| 460 | self.vbox.Add(self.sizer4) |
---|
| 461 | self.vbox.Add(self.sizer5) |
---|
| 462 | self.vbox.Add(self.sizer6) |
---|
[5ce7f17] | 463 | |
---|
[f0d720b] | 464 | def set_layout(self): |
---|
| 465 | """ |
---|
| 466 | layout |
---|
| 467 | """ |
---|
| 468 | self.vbox.Layout() |
---|
| 469 | self.vbox.Fit(self) |
---|
| 470 | self.SetSizer(self.vbox) |
---|
| 471 | self.Centre() |
---|
[5ce7f17] | 472 | |
---|
[f0d720b] | 473 | def set_owner(self, owner): |
---|
| 474 | """ |
---|
| 475 | set owner of fitpage |
---|
[5ce7f17] | 476 | |
---|
[f0d720b] | 477 | :param owner: the class responsible of plotting |
---|
[5ce7f17] | 478 | |
---|
[f0d720b] | 479 | """ |
---|
| 480 | self.event_owner = owner |
---|
| 481 | self.state.event_owner = owner |
---|
[5ce7f17] | 482 | |
---|
[f0d720b] | 483 | def get_state(self): |
---|
| 484 | """ |
---|
[5ce7f17] | 485 | return the current page state |
---|
[f0d720b] | 486 | """ |
---|
| 487 | return self.state |
---|
[5ce7f17] | 488 | |
---|
[f0d720b] | 489 | def get_data(self): |
---|
| 490 | """ |
---|
| 491 | return the current data |
---|
| 492 | """ |
---|
| 493 | return self.data |
---|
[5ce7f17] | 494 | |
---|
[f0d720b] | 495 | def get_data_list(self): |
---|
| 496 | """ |
---|
| 497 | return the current data |
---|
| 498 | """ |
---|
| 499 | return self.data_list |
---|
[5ce7f17] | 500 | |
---|
[f0d720b] | 501 | def set_manager(self, manager): |
---|
| 502 | """ |
---|
| 503 | set panel manager |
---|
[5ce7f17] | 504 | |
---|
[f0d720b] | 505 | :param manager: instance of plugin fitting |
---|
[5ce7f17] | 506 | |
---|
[f0d720b] | 507 | """ |
---|
| 508 | self._manager = manager |
---|
| 509 | self.state.manager = manager |
---|
[5ce7f17] | 510 | |
---|
[f0d720b] | 511 | def populate_box(self, model_dict): |
---|
| 512 | """ |
---|
| 513 | Store list of model |
---|
[5ce7f17] | 514 | |
---|
[f0d720b] | 515 | :param model_dict: dictionary containing list of models |
---|
[5ce7f17] | 516 | |
---|
[f0d720b] | 517 | """ |
---|
| 518 | self.model_list_box = model_dict |
---|
| 519 | self.state.model_list_box = self.model_list_box |
---|
| 520 | self.initialize_combox() |
---|
[5ce7f17] | 521 | |
---|
[f0d720b] | 522 | def set_model_dictionary(self, model_dict): |
---|
| 523 | """ |
---|
| 524 | Store a dictionary linking model name -> model object |
---|
| 525 | |
---|
| 526 | :param model_dict: dictionary containing list of models |
---|
| 527 | """ |
---|
| 528 | self.model_dict = model_dict |
---|
| 529 | |
---|
| 530 | def initialize_combox(self): |
---|
| 531 | """ |
---|
[e28f34d] | 532 | put default value in the combo box |
---|
[5ce7f17] | 533 | """ |
---|
[e28f34d] | 534 | if self.model_list_box is not None and len(self.model_list_box) > 0: |
---|
[f0d720b] | 535 | self._populate_box(self.structurebox, |
---|
[5ce7f17] | 536 | self.model_list_box["Structure Factors"]) |
---|
[f0d720b] | 537 | self.structurebox.Insert("None", 0, None) |
---|
| 538 | self.structurebox.SetSelection(0) |
---|
| 539 | self.structurebox.Hide() |
---|
| 540 | self.text2.Hide() |
---|
| 541 | self.structurebox.Disable() |
---|
| 542 | self.text2.Disable() |
---|
[5ce7f17] | 543 | |
---|
[f0d720b] | 544 | def set_dispers_sizer(self): |
---|
| 545 | """ |
---|
| 546 | fill sizer containing dispersity info |
---|
| 547 | """ |
---|
[c8e1996] | 548 | # print "==== entering set_dispers_sizer ===" |
---|
[f0d720b] | 549 | self.sizer4.Clear(True) |
---|
| 550 | name = "Polydispersity and Orientational Distribution" |
---|
[6f16e25] | 551 | box_description = wx.StaticBox(self, wx.ID_ANY, name) |
---|
[f0d720b] | 552 | box_description.SetForegroundColour(wx.BLUE) |
---|
| 553 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
[c8e1996] | 554 | # ---------------------------------------------------- |
---|
[6f16e25] | 555 | self.disable_disp = wx.RadioButton(self, wx.ID_ANY, 'Off', (10, 10), |
---|
[5ce7f17] | 556 | style=wx.RB_GROUP) |
---|
[6f16e25] | 557 | self.enable_disp = wx.RadioButton(self, wx.ID_ANY, 'On', (10, 30)) |
---|
[f0d720b] | 558 | # best size for MAC and PC |
---|
| 559 | if ON_MAC: |
---|
| 560 | size_q = (30, 20) |
---|
| 561 | else: |
---|
| 562 | size_q = (20, 15) |
---|
[6f16e25] | 563 | self.disp_help_bt = wx.Button(self, self.ID_DISPERSER_HELP, '?', |
---|
[f0d720b] | 564 | style=wx.BU_EXACTFIT, |
---|
| 565 | size=size_q) |
---|
[5ce7f17] | 566 | self.disp_help_bt.Bind(wx.EVT_BUTTON, self.on_pd_help_clicked, |
---|
| 567 | id=self.disp_help_bt.GetId()) |
---|
[a0373d5] | 568 | self.disp_help_bt.SetToolTipString("Help for polydispersion.") |
---|
[5ce7f17] | 569 | |
---|
[f0d720b] | 570 | self.Bind(wx.EVT_RADIOBUTTON, self._set_dipers_Param, |
---|
[5ce7f17] | 571 | id=self.disable_disp.GetId()) |
---|
[f0d720b] | 572 | self.Bind(wx.EVT_RADIOBUTTON, self._set_dipers_Param, |
---|
[5ce7f17] | 573 | id=self.enable_disp.GetId()) |
---|
[c8e1996] | 574 | # MAC needs SetValue |
---|
[f0d720b] | 575 | self.disable_disp.SetValue(True) |
---|
| 576 | sizer_dispersion = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 577 | sizer_dispersion.Add((20, 20)) |
---|
| 578 | name = "" # Polydispersity and \nOrientational Distribution " |
---|
[6f16e25] | 579 | sizer_dispersion.Add(wx.StaticText(self, wx.ID_ANY, name)) |
---|
[f0d720b] | 580 | sizer_dispersion.Add(self.enable_disp) |
---|
| 581 | sizer_dispersion.Add((20, 20)) |
---|
| 582 | sizer_dispersion.Add(self.disable_disp) |
---|
| 583 | sizer_dispersion.Add((25, 20)) |
---|
| 584 | sizer_dispersion.Add(self.disp_help_bt) |
---|
[5ce7f17] | 585 | |
---|
[c8e1996] | 586 | # fill a sizer for dispersion |
---|
[f0d720b] | 587 | boxsizer1.Add(sizer_dispersion, 0, |
---|
[5ce7f17] | 588 | wx.TOP|wx.BOTTOM|wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, |
---|
| 589 | border=5) |
---|
[f0d720b] | 590 | self.sizer4_4 = wx.GridBagSizer(6, 5) |
---|
| 591 | |
---|
| 592 | boxsizer1.Add(self.sizer4_4) |
---|
[c8e1996] | 593 | # ----------------------------------------------------- |
---|
[f0d720b] | 594 | self.sizer4.Add(boxsizer1, 0, wx.EXPAND | wx.ALL, 10) |
---|
| 595 | self.sizer4_4.Layout() |
---|
| 596 | self.sizer4.Layout() |
---|
| 597 | self.Layout() |
---|
[5ce7f17] | 598 | |
---|
[f0d720b] | 599 | self.Refresh() |
---|
[c8e1996] | 600 | # saving the state of enable dispersity button |
---|
[f0d720b] | 601 | self.state.enable_disp = self.enable_disp.GetValue() |
---|
| 602 | self.state.disable_disp = self.disable_disp.GetValue() |
---|
| 603 | self.SetupScrolling() |
---|
[5ce7f17] | 604 | |
---|
[f0d720b] | 605 | def onResetModel(self, event): |
---|
| 606 | """ |
---|
| 607 | Reset model state |
---|
| 608 | """ |
---|
| 609 | menu = event.GetEventObject() |
---|
[c8e1996] | 610 | # post help message for the selected model |
---|
[f0d720b] | 611 | msg = menu.GetHelpString(event.GetId()) |
---|
| 612 | msg += " reloaded" |
---|
| 613 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
| 614 | self.Show(False) |
---|
| 615 | name = menu.GetLabel(event.GetId()) |
---|
| 616 | self._on_select_model_helper() |
---|
[c8e1996] | 617 | if self.model is not None: |
---|
[f0d720b] | 618 | self.m_name = self.model.name |
---|
| 619 | if name in self.saved_states.keys(): |
---|
| 620 | previous_state = self.saved_states[name] |
---|
[c8e1996] | 621 | # reset state of checkbox,textcrtl and regular parameters value |
---|
[5ce7f17] | 622 | |
---|
[f0d720b] | 623 | self.reset_page(previous_state) |
---|
| 624 | self.state.m_name = self.m_name |
---|
| 625 | self.Show(True) |
---|
[5ce7f17] | 626 | |
---|
[f0d720b] | 627 | def on_preview(self, event): |
---|
| 628 | """ |
---|
| 629 | Report the current fit results |
---|
| 630 | """ |
---|
| 631 | # Get plot image from plotpanel |
---|
| 632 | images, canvases = self.get_images() |
---|
| 633 | # get the report dialog |
---|
| 634 | self.state.report(images, canvases) |
---|
[5ce7f17] | 635 | |
---|
[f0d720b] | 636 | def on_save(self, event): |
---|
| 637 | """ |
---|
| 638 | Save the current state into file |
---|
| 639 | """ |
---|
| 640 | self.save_current_state() |
---|
| 641 | new_state = self.state.clone() |
---|
| 642 | # Ask the user the location of the file to write to. |
---|
| 643 | path = None |
---|
[c8e1996] | 644 | if self.parent is not None: |
---|
[f0d720b] | 645 | self._default_save_location = \ |
---|
| 646 | self._manager.parent._default_save_location |
---|
| 647 | dlg = wx.FileDialog(self, "Choose a file", self._default_save_location, |
---|
[5ce7f17] | 648 | self.window_caption, "*.fitv", wx.SAVE) |
---|
[f0d720b] | 649 | |
---|
| 650 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 651 | path = dlg.GetPath() |
---|
| 652 | self._default_save_location = os.path.dirname(path) |
---|
[5ce7f17] | 653 | self._manager.parent._default_save_location = \ |
---|
[c8e1996] | 654 | self._default_save_location |
---|
[f0d720b] | 655 | else: |
---|
| 656 | return None |
---|
| 657 | # MAC always needs the extension for saving |
---|
| 658 | extens = ".fitv" |
---|
| 659 | # Make sure the ext included in the file name |
---|
| 660 | fName = os.path.splitext(path)[0] + extens |
---|
[c8e1996] | 661 | # the manager write the state into file |
---|
[f0d720b] | 662 | self._manager.save_fit_state(filepath=fName, fitstate=new_state) |
---|
| 663 | return new_state |
---|
[5ce7f17] | 664 | |
---|
[f0d720b] | 665 | def on_copy(self, event): |
---|
| 666 | """ |
---|
| 667 | Copy Parameter values to the clipboad |
---|
| 668 | """ |
---|
[c8e1996] | 669 | if event is not None: |
---|
[f0d720b] | 670 | event.Skip() |
---|
| 671 | # It seems MAC needs wxCallAfter |
---|
| 672 | if event.GetId() == GUIFRAME_ID.COPYEX_ID: |
---|
| 673 | print "copy excel" |
---|
| 674 | wx.CallAfter(self.get_copy_excel) |
---|
| 675 | elif event.GetId() == GUIFRAME_ID.COPYLAT_ID: |
---|
| 676 | print "copy latex" |
---|
| 677 | wx.CallAfter(self.get_copy_latex) |
---|
| 678 | else: |
---|
| 679 | wx.CallAfter(self.get_copy) |
---|
| 680 | |
---|
| 681 | def on_paste(self, event): |
---|
| 682 | """ |
---|
| 683 | Paste Parameter values to the panel if possible |
---|
| 684 | """ |
---|
[c8e1996] | 685 | # if event is not None: |
---|
[f0d720b] | 686 | # event.Skip() |
---|
| 687 | # It seems MAC needs wxCallAfter for the setvalues |
---|
| 688 | # for multiple textctrl items, otherwise it tends to crash once a while |
---|
| 689 | wx.CallAfter(self.get_paste) |
---|
| 690 | # messages depending on the flag |
---|
[c8e1996] | 691 | # self._copy_info(True) |
---|
[5ce7f17] | 692 | |
---|
[f0d720b] | 693 | def _copy_info(self, flag): |
---|
| 694 | """ |
---|
[e92a352] | 695 | Send event depending on flag |
---|
[5ce7f17] | 696 | |
---|
[e92a352] | 697 | : Param flag: flag that distinguishes the event |
---|
[f0d720b] | 698 | """ |
---|
| 699 | # messages depending on the flag |
---|
[c8e1996] | 700 | if flag is None: |
---|
[f0d720b] | 701 | msg = " Parameter values are copied to the clipboard..." |
---|
| 702 | infor = 'warning' |
---|
| 703 | elif flag: |
---|
| 704 | msg = " Parameter values are pasted from the clipboard..." |
---|
| 705 | infor = "warning" |
---|
| 706 | else: |
---|
| 707 | msg = "Error occurred: " |
---|
| 708 | msg += "No valid parameter values to paste from the clipboard..." |
---|
| 709 | infor = "warning" |
---|
| 710 | # inform msg to wx |
---|
| 711 | wx.PostEvent(self._manager.parent, |
---|
[5ce7f17] | 712 | StatusEvent(status=msg, info=infor)) |
---|
| 713 | |
---|
[f0d720b] | 714 | def _get_time_stamp(self): |
---|
| 715 | """ |
---|
| 716 | return time and date stings |
---|
| 717 | """ |
---|
| 718 | # date and time |
---|
| 719 | year, month, day, hour, minute, second, _, _, _ = time.localtime() |
---|
| 720 | current_time = str(hour) + ":" + str(minute) + ":" + str(second) |
---|
| 721 | current_date = str(month) + "/" + str(day) + "/" + str(year) |
---|
| 722 | return current_time, current_date |
---|
[5ce7f17] | 723 | |
---|
[f0d720b] | 724 | def on_bookmark(self, event): |
---|
| 725 | """ |
---|
| 726 | save history of the data and model |
---|
| 727 | """ |
---|
[c8e1996] | 728 | if self.model is None: |
---|
[f0d720b] | 729 | msg = "Can not bookmark; Please select Data and Model first..." |
---|
| 730 | wx.MessageBox(msg, 'Info') |
---|
| 731 | return |
---|
| 732 | self.save_current_state() |
---|
| 733 | new_state = self.state.clone() |
---|
[c8e1996] | 734 | # Add model state on context menu |
---|
[f0d720b] | 735 | self.number_saved_state += 1 |
---|
| 736 | current_time, current_date = self._get_time_stamp() |
---|
[c8e1996] | 737 | # name= self.model.name+"[%g]"%self.number_saved_state |
---|
[f0d720b] | 738 | name = "Fitting: %g]" % self.number_saved_state |
---|
| 739 | name += self.model.__class__.__name__ |
---|
| 740 | name += "bookmarked at %s on %s" % (current_time, current_date) |
---|
| 741 | self.saved_states[name] = new_state |
---|
[5ce7f17] | 742 | |
---|
[c8e1996] | 743 | # Add item in the context menu |
---|
[f0d720b] | 744 | msg = "Model saved at %s on %s" % (current_time, current_date) |
---|
[c8e1996] | 745 | # post help message for the selected model |
---|
[f0d720b] | 746 | msg += " Saved! right click on this page to retrieve this model" |
---|
| 747 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
[5ce7f17] | 748 | |
---|
[6f16e25] | 749 | self.popUpMenu.Append(self.ID_BOOKMARK, name, str(msg)) |
---|
| 750 | wx.EVT_MENU(self, self.ID_BOOKMARK, self.onResetModel) |
---|
[f0d720b] | 751 | wx.PostEvent(self._manager.parent, |
---|
| 752 | AppendBookmarkEvent(title=name, |
---|
| 753 | hint=str(msg), |
---|
| 754 | handler=self._back_to_bookmark)) |
---|
[5ce7f17] | 755 | |
---|
[f0d720b] | 756 | def _back_to_bookmark(self, event): |
---|
| 757 | """ |
---|
| 758 | Back to bookmark |
---|
| 759 | """ |
---|
| 760 | self._manager.on_perspective(event) |
---|
| 761 | self.onResetModel(event) |
---|
| 762 | self._draw_model() |
---|
[5ce7f17] | 763 | |
---|
[f0d720b] | 764 | def onSetFocus(self, evt): |
---|
| 765 | """ |
---|
| 766 | highlight the current textcrtl and hide the error text control shown |
---|
| 767 | after fitting |
---|
| 768 | """ |
---|
| 769 | return |
---|
[5ce7f17] | 770 | |
---|
[f0d720b] | 771 | def read_file(self, path): |
---|
| 772 | """ |
---|
| 773 | Read two columns file |
---|
[5ce7f17] | 774 | |
---|
[f0d720b] | 775 | :param path: the path to the file to read |
---|
[5ce7f17] | 776 | |
---|
[f0d720b] | 777 | """ |
---|
| 778 | try: |
---|
[c8e1996] | 779 | if path is None: |
---|
[5ce7f17] | 780 | status = " Selected Distribution was not loaded: %s" % path |
---|
[f0d720b] | 781 | wx.PostEvent(self._manager.parent, |
---|
[5ce7f17] | 782 | StatusEvent(status=status)) |
---|
[f0d720b] | 783 | return None, None |
---|
| 784 | input_f = open(path, 'r') |
---|
| 785 | buff = input_f.read() |
---|
| 786 | lines = buff.split('\n') |
---|
| 787 | input_f.close() |
---|
| 788 | angles = [] |
---|
| 789 | weights = [] |
---|
| 790 | for line in lines: |
---|
| 791 | toks = line.split() |
---|
| 792 | try: |
---|
| 793 | angle = float(toks[0]) |
---|
| 794 | weight = float(toks[1]) |
---|
| 795 | angles.append(angle) |
---|
| 796 | weights.append(weight) |
---|
[7673ecd] | 797 | except Exception: |
---|
[f0d720b] | 798 | # Skip non-data lines |
---|
[c155a16] | 799 | logger.error(traceback.format_exc()) |
---|
[9a5097c] | 800 | return np.array(angles), np.array(weights) |
---|
[f0d720b] | 801 | except: |
---|
| 802 | raise |
---|
| 803 | |
---|
| 804 | def createMemento(self): |
---|
| 805 | """ |
---|
| 806 | return the current state of the page |
---|
| 807 | """ |
---|
| 808 | return self.state.clone() |
---|
[5ce7f17] | 809 | |
---|
[f0d720b] | 810 | def save_current_state(self): |
---|
| 811 | """ |
---|
| 812 | Store current state |
---|
| 813 | """ |
---|
[c8e1996] | 814 | # save model option |
---|
| 815 | if self.model is not None: |
---|
[f0d720b] | 816 | self.disp_list = self.model.getDispParamList() |
---|
| 817 | self.state.disp_list = copy.deepcopy(self.disp_list) |
---|
| 818 | self.state.model = self.model.clone() |
---|
[5ce7f17] | 819 | |
---|
[c8e1996] | 820 | # model combobox: complex code because of mac's silent error |
---|
| 821 | if self.structurebox is not None: |
---|
[f0d720b] | 822 | if self.structurebox.IsShown(): |
---|
| 823 | self.state.structurecombobox = 'None' |
---|
| 824 | s_select = self.structurebox.GetSelection() |
---|
| 825 | if s_select > 0: |
---|
[c8e1996] | 826 | self.state.structurecombobox = \ |
---|
| 827 | self.structurebox.GetString(s_select) |
---|
| 828 | if self.formfactorbox is not None: |
---|
[f0d720b] | 829 | f_select = self.formfactorbox.GetSelection() |
---|
| 830 | if f_select > 0: |
---|
[c8e1996] | 831 | self.state.formfactorcombobox = \ |
---|
| 832 | self.formfactorbox.GetString(f_select) |
---|
| 833 | if self.categorybox is not None: |
---|
[f0d720b] | 834 | cb_select = self.categorybox.GetSelection() |
---|
| 835 | if cb_select > 0: |
---|
[c8e1996] | 836 | self.state.categorycombobox = \ |
---|
| 837 | self.categorybox.GetString(cb_select) |
---|
[5ce7f17] | 838 | |
---|
[f0d720b] | 839 | self.state.enable2D = copy.deepcopy(self.enable2D) |
---|
| 840 | self.state.values = copy.deepcopy(self.values) |
---|
| 841 | self.state.weights = copy.deepcopy(self.weights) |
---|
[c8e1996] | 842 | # save data |
---|
[f0d720b] | 843 | self.state.data = copy.deepcopy(self.data) |
---|
| 844 | self.state.qmax_x = self.qmax_x |
---|
| 845 | self.state.qmin_x = self.qmin_x |
---|
| 846 | self.state.dI_noweight = copy.deepcopy(self.dI_noweight.GetValue()) |
---|
| 847 | self.state.dI_didata = copy.deepcopy(self.dI_didata.GetValue()) |
---|
| 848 | self.state.dI_sqrdata = copy.deepcopy(self.dI_sqrdata.GetValue()) |
---|
| 849 | self.state.dI_idata = copy.deepcopy(self.dI_idata.GetValue()) |
---|
| 850 | self.state.dq_l = self.dq_l |
---|
| 851 | self.state.dq_r = self.dq_r |
---|
| 852 | if hasattr(self, "enable_disp"): |
---|
| 853 | self.state.enable_disp = self.enable_disp.GetValue() |
---|
| 854 | self.state.disable_disp = self.disable_disp.GetValue() |
---|
[5ce7f17] | 855 | |
---|
[f0d720b] | 856 | self.state.smearer = copy.deepcopy(self.current_smearer) |
---|
| 857 | if hasattr(self, "enable_smearer"): |
---|
| 858 | self.state.enable_smearer = \ |
---|
| 859 | copy.deepcopy(self.enable_smearer.GetValue()) |
---|
| 860 | self.state.disable_smearer = \ |
---|
| 861 | copy.deepcopy(self.disable_smearer.GetValue()) |
---|
| 862 | |
---|
| 863 | self.state.pinhole_smearer = \ |
---|
| 864 | copy.deepcopy(self.pinhole_smearer.GetValue()) |
---|
[7e98655] | 865 | self.state.dx_percent = copy.deepcopy(self.dx_percent) |
---|
[f0d720b] | 866 | self.state.dxl = copy.deepcopy(self.dxl) |
---|
| 867 | self.state.dxw = copy.deepcopy(self.dxw) |
---|
| 868 | self.state.slit_smearer = copy.deepcopy(self.slit_smearer.GetValue()) |
---|
[5ce7f17] | 869 | |
---|
[f0d720b] | 870 | if len(self._disp_obj_dict) > 0: |
---|
| 871 | for k, v in self._disp_obj_dict.iteritems(): |
---|
[6c382da] | 872 | self.state._disp_obj_dict[k] = v.type |
---|
[f0d720b] | 873 | |
---|
| 874 | self.state.values = copy.deepcopy(self.values) |
---|
| 875 | self.state.weights = copy.deepcopy(self.weights) |
---|
[c8e1996] | 876 | # save plotting range |
---|
[f0d720b] | 877 | self._save_plotting_range() |
---|
[5ce7f17] | 878 | |
---|
[f0d720b] | 879 | self.state.orientation_params = [] |
---|
| 880 | self.state.orientation_params_disp = [] |
---|
| 881 | self.state.parameters = [] |
---|
| 882 | self.state.fittable_param = [] |
---|
| 883 | self.state.fixed_param = [] |
---|
| 884 | self.state.str_parameters = [] |
---|
| 885 | |
---|
[c8e1996] | 886 | # save checkbutton state and txtcrtl values |
---|
[f0d720b] | 887 | self._copy_parameters_state(self.str_parameters, |
---|
| 888 | self.state.str_parameters) |
---|
| 889 | self._copy_parameters_state(self.orientation_params, |
---|
| 890 | self.state.orientation_params) |
---|
| 891 | self._copy_parameters_state(self.orientation_params_disp, |
---|
[2abe6bf] | 892 | self.state.orientation_params_disp) |
---|
[5ce7f17] | 893 | |
---|
[f0d720b] | 894 | self._copy_parameters_state(self.parameters, self.state.parameters) |
---|
| 895 | self._copy_parameters_state(self.fittable_param, |
---|
[2abe6bf] | 896 | self.state.fittable_param) |
---|
[f0d720b] | 897 | self._copy_parameters_state(self.fixed_param, self.state.fixed_param) |
---|
[c8e1996] | 898 | # save chisqr |
---|
[f0d720b] | 899 | self.state.tcChi = self.tcChi.GetValue() |
---|
[5ce7f17] | 900 | |
---|
[f0d720b] | 901 | def save_current_state_fit(self): |
---|
| 902 | """ |
---|
| 903 | Store current state for fit_page |
---|
| 904 | """ |
---|
[c8e1996] | 905 | # save model option |
---|
| 906 | if self.model is not None: |
---|
[f0d720b] | 907 | self.disp_list = self.model.getDispParamList() |
---|
| 908 | self.state.disp_list = copy.deepcopy(self.disp_list) |
---|
| 909 | self.state.model = self.model.clone() |
---|
[5ce7f17] | 910 | |
---|
[f0d720b] | 911 | self.state.enable2D = copy.deepcopy(self.enable2D) |
---|
| 912 | self.state.values = copy.deepcopy(self.values) |
---|
| 913 | self.state.weights = copy.deepcopy(self.weights) |
---|
[c8e1996] | 914 | # save data |
---|
[f0d720b] | 915 | self.state.data = copy.deepcopy(self.data) |
---|
[5ce7f17] | 916 | |
---|
[f0d720b] | 917 | if hasattr(self, "enable_disp"): |
---|
| 918 | self.state.enable_disp = self.enable_disp.GetValue() |
---|
| 919 | self.state.disable_disp = self.disable_disp.GetValue() |
---|
[5ce7f17] | 920 | |
---|
[f0d720b] | 921 | self.state.smearer = copy.deepcopy(self.current_smearer) |
---|
| 922 | if hasattr(self, "enable_smearer"): |
---|
| 923 | self.state.enable_smearer = \ |
---|
| 924 | copy.deepcopy(self.enable_smearer.GetValue()) |
---|
| 925 | self.state.disable_smearer = \ |
---|
| 926 | copy.deepcopy(self.disable_smearer.GetValue()) |
---|
[5ce7f17] | 927 | |
---|
[f0d720b] | 928 | self.state.pinhole_smearer = \ |
---|
| 929 | copy.deepcopy(self.pinhole_smearer.GetValue()) |
---|
| 930 | self.state.slit_smearer = copy.deepcopy(self.slit_smearer.GetValue()) |
---|
| 931 | self.state.dI_noweight = copy.deepcopy(self.dI_noweight.GetValue()) |
---|
| 932 | self.state.dI_didata = copy.deepcopy(self.dI_didata.GetValue()) |
---|
| 933 | self.state.dI_sqrdata = copy.deepcopy(self.dI_sqrdata.GetValue()) |
---|
| 934 | self.state.dI_idata = copy.deepcopy(self.dI_idata.GetValue()) |
---|
[c8e1996] | 935 | if hasattr(self, "disp_box") and self.disp_box is not None: |
---|
[f0d720b] | 936 | self.state.disp_box = self.disp_box.GetCurrentSelection() |
---|
| 937 | |
---|
| 938 | if len(self.disp_cb_dict) > 0: |
---|
| 939 | for k, v in self.disp_cb_dict.iteritems(): |
---|
[c8e1996] | 940 | if v is None: |
---|
[f0d720b] | 941 | self.state.disp_cb_dict[k] = v |
---|
| 942 | else: |
---|
| 943 | try: |
---|
| 944 | self.state.disp_cb_dict[k] = v.GetValue() |
---|
| 945 | except: |
---|
| 946 | self.state.disp_cb_dict[k] = None |
---|
| 947 | if len(self._disp_obj_dict) > 0: |
---|
| 948 | for k, v in self._disp_obj_dict.iteritems(): |
---|
[6c382da] | 949 | self.state._disp_obj_dict[k] = v.type |
---|
[5ce7f17] | 950 | |
---|
[f0d720b] | 951 | self.state.values = copy.deepcopy(self.values) |
---|
| 952 | self.state.weights = copy.deepcopy(self.weights) |
---|
[5ce7f17] | 953 | |
---|
[c8e1996] | 954 | # save plotting range |
---|
[f0d720b] | 955 | self._save_plotting_range() |
---|
[5ce7f17] | 956 | |
---|
[c8e1996] | 957 | # save checkbutton state and txtcrtl values |
---|
[f0d720b] | 958 | self._copy_parameters_state(self.orientation_params, |
---|
[5ce7f17] | 959 | self.state.orientation_params) |
---|
[f0d720b] | 960 | self._copy_parameters_state(self.orientation_params_disp, |
---|
[5ce7f17] | 961 | self.state.orientation_params_disp) |
---|
[f0d720b] | 962 | self._copy_parameters_state(self.parameters, self.state.parameters) |
---|
| 963 | self._copy_parameters_state(self.fittable_param, |
---|
[5ce7f17] | 964 | self.state.fittable_param) |
---|
[f0d720b] | 965 | self._copy_parameters_state(self.fixed_param, self.state.fixed_param) |
---|
[5ce7f17] | 966 | |
---|
[f0d720b] | 967 | def check_invalid_panel(self): |
---|
| 968 | """ |
---|
| 969 | check if the user can already perform some action with this panel |
---|
| 970 | """ |
---|
| 971 | if self.data is None: |
---|
| 972 | self.disable_smearer.SetValue(True) |
---|
| 973 | self.disable_disp.SetValue(True) |
---|
| 974 | msg = "Please load Data and select Model to start..." |
---|
| 975 | wx.MessageBox(msg, 'Info') |
---|
[c8e1996] | 976 | return True |
---|
[5ce7f17] | 977 | |
---|
[f0d720b] | 978 | def set_model_state(self, state): |
---|
| 979 | """ |
---|
| 980 | reset page given a model state |
---|
| 981 | """ |
---|
| 982 | self.disp_cb_dict = state.disp_cb_dict |
---|
| 983 | self.disp_list = state.disp_list |
---|
[5ce7f17] | 984 | |
---|
[c8e1996] | 985 | # fill model combobox |
---|
[f0d720b] | 986 | self._show_combox_helper() |
---|
[c8e1996] | 987 | # select the current model |
---|
[f0d720b] | 988 | try: |
---|
| 989 | # to support older version |
---|
| 990 | category_pos = int(state.categorycombobox) |
---|
| 991 | except: |
---|
| 992 | category_pos = 0 |
---|
| 993 | for ind_cat in range(self.categorybox.GetCount()): |
---|
[5ce7f17] | 994 | if self.categorycombobox.GetString(ind_cat) == \ |
---|
[f0d720b] | 995 | state.categorycombobox: |
---|
| 996 | category_pos = int(ind_cat) |
---|
| 997 | break |
---|
[5ce7f17] | 998 | |
---|
[f0d720b] | 999 | self.categorybox.Select(category_pos) |
---|
| 1000 | try: |
---|
| 1001 | # to support older version |
---|
| 1002 | formfactor_pos = int(state.formfactorcombobox) |
---|
| 1003 | except: |
---|
| 1004 | formfactor_pos = 0 |
---|
| 1005 | for ind_form in range(self.formfactorbox.GetCount()): |
---|
| 1006 | if self.formfactorbox.GetString(ind_form) == \ |
---|
| 1007 | state.formfactorcombobox: |
---|
| 1008 | formfactor_pos = int(ind_form) |
---|
| 1009 | break |
---|
[5ce7f17] | 1010 | |
---|
[f0d720b] | 1011 | self.formfactorbox.Select(formfactor_pos) |
---|
[5ce7f17] | 1012 | |
---|
[f0d720b] | 1013 | try: |
---|
| 1014 | # to support older version |
---|
| 1015 | structfactor_pos = int(state.structurecombobox) |
---|
| 1016 | except: |
---|
| 1017 | structfactor_pos = 0 |
---|
| 1018 | for ind_struct in range(self.structurebox.GetCount()): |
---|
| 1019 | if self.structurebox.GetString(ind_struct) == \ |
---|
| 1020 | state.structurecombobox: |
---|
| 1021 | structfactor_pos = int(ind_struct) |
---|
| 1022 | break |
---|
[5ce7f17] | 1023 | |
---|
[f0d720b] | 1024 | self.structurebox.SetSelection(structfactor_pos) |
---|
[5ce7f17] | 1025 | |
---|
[c8e1996] | 1026 | if state.multi_factor is not None: |
---|
[f0d720b] | 1027 | self.multifactorbox.SetSelection(state.multi_factor) |
---|
[5ce7f17] | 1028 | |
---|
[c8e1996] | 1029 | # reset state of checkbox,textcrtl and regular parameters value |
---|
[f0d720b] | 1030 | self._reset_parameters_state(self.orientation_params_disp, |
---|
| 1031 | state.orientation_params_disp) |
---|
| 1032 | self._reset_parameters_state(self.orientation_params, |
---|
| 1033 | state.orientation_params) |
---|
| 1034 | self._reset_parameters_state(self.str_parameters, |
---|
| 1035 | state.str_parameters) |
---|
| 1036 | self._reset_parameters_state(self.parameters, state.parameters) |
---|
[c8e1996] | 1037 | # display dispersion info layer |
---|
[f0d720b] | 1038 | self.enable_disp.SetValue(state.enable_disp) |
---|
| 1039 | self.disable_disp.SetValue(state.disable_disp) |
---|
[5ce7f17] | 1040 | |
---|
[c8e1996] | 1041 | if hasattr(self, "disp_box") and self.disp_box is not None: |
---|
[f0d720b] | 1042 | self.disp_box.SetSelection(state.disp_box) |
---|
| 1043 | n = self.disp_box.GetCurrentSelection() |
---|
| 1044 | dispersity = self.disp_box.GetClientData(n) |
---|
| 1045 | name = dispersity.__name__ |
---|
| 1046 | self._set_dipers_Param(event=None) |
---|
[5ce7f17] | 1047 | |
---|
[f0d720b] | 1048 | if name == "ArrayDispersion": |
---|
[5ce7f17] | 1049 | |
---|
[f0d720b] | 1050 | for item in self.disp_cb_dict.keys(): |
---|
[5ce7f17] | 1051 | |
---|
[f0d720b] | 1052 | if hasattr(self.disp_cb_dict[item], "SetValue"): |
---|
[c8e1996] | 1053 | self.disp_cb_dict[item].SetValue( |
---|
[f0d720b] | 1054 | state.disp_cb_dict[item]) |
---|
| 1055 | # Create the dispersion objects |
---|
[a0373d5] | 1056 | disp_model = POLYDISPERSITY_MODELS['array']() |
---|
[f0d720b] | 1057 | if hasattr(state, "values") and \ |
---|
[505706a] | 1058 | self.disp_cb_dict[item].GetValue(): |
---|
[f0d720b] | 1059 | if len(state.values) > 0: |
---|
| 1060 | self.values = state.values |
---|
| 1061 | self.weights = state.weights |
---|
| 1062 | disp_model.set_weights(self.values, |
---|
| 1063 | state.weights) |
---|
| 1064 | else: |
---|
| 1065 | self._reset_dispersity() |
---|
[5ce7f17] | 1066 | |
---|
[f0d720b] | 1067 | self._disp_obj_dict[item] = disp_model |
---|
| 1068 | # Set the new model as the dispersion object |
---|
[c8e1996] | 1069 | # for the selected parameter |
---|
[f0d720b] | 1070 | self.model.set_dispersion(item, disp_model) |
---|
[5ce7f17] | 1071 | |
---|
[f0d720b] | 1072 | self.model._persistency_dict[item] = \ |
---|
| 1073 | [state.values, state.weights] |
---|
[5ce7f17] | 1074 | |
---|
[f0d720b] | 1075 | else: |
---|
| 1076 | keys = self.model.getParamList() |
---|
| 1077 | for item in keys: |
---|
| 1078 | if item in self.disp_list and \ |
---|
[c8e1996] | 1079 | item not in self.model.details: |
---|
[f0d720b] | 1080 | self.model.details[item] = ["", None, None] |
---|
| 1081 | self.disp_cb_dict = copy.deepcopy(state.disp_cb_dict) |
---|
| 1082 | self.state.disp_cb_dict = copy.deepcopy(state.disp_cb_dict) |
---|
[c8e1996] | 1083 | # smearing info restore |
---|
[f0d720b] | 1084 | if hasattr(self, "enable_smearer"): |
---|
[c8e1996] | 1085 | # set smearing value whether or not the data |
---|
| 1086 | # contain the smearing info |
---|
[f0d720b] | 1087 | self.enable_smearer.SetValue(state.enable_smearer) |
---|
| 1088 | self.disable_smearer.SetValue(state.disable_smearer) |
---|
| 1089 | self.onSmear(event=None) |
---|
| 1090 | self.pinhole_smearer.SetValue(state.pinhole_smearer) |
---|
| 1091 | self.slit_smearer.SetValue(state.slit_smearer) |
---|
[5ce7f17] | 1092 | |
---|
[f0d720b] | 1093 | self.dI_noweight.SetValue(state.dI_noweight) |
---|
| 1094 | self.dI_didata.SetValue(state.dI_didata) |
---|
| 1095 | self.dI_sqrdata.SetValue(state.dI_sqrdata) |
---|
| 1096 | self.dI_idata.SetValue(state.dI_idata) |
---|
[5ce7f17] | 1097 | |
---|
[c8e1996] | 1098 | # we have two more options for smearing |
---|
[f0d720b] | 1099 | if self.pinhole_smearer.GetValue(): |
---|
| 1100 | self.onPinholeSmear(event=None) |
---|
| 1101 | elif self.slit_smearer.GetValue(): |
---|
| 1102 | self.onSlitSmear(event=None) |
---|
[5ce7f17] | 1103 | |
---|
[c8e1996] | 1104 | # reset state of checkbox,textcrtl and dispersity parameters value |
---|
[f0d720b] | 1105 | self._reset_parameters_state(self.fittable_param, state.fittable_param) |
---|
| 1106 | self._reset_parameters_state(self.fixed_param, state.fixed_param) |
---|
[5ce7f17] | 1107 | |
---|
[c8e1996] | 1108 | # draw the model with previous parameters value |
---|
[f0d720b] | 1109 | self._onparamEnter_helper() |
---|
| 1110 | self.select_param(event=None) |
---|
[c8e1996] | 1111 | # Save state_fit |
---|
[f0d720b] | 1112 | self.save_current_state_fit() |
---|
| 1113 | self._lay_out() |
---|
| 1114 | self.Refresh() |
---|
[5ce7f17] | 1115 | |
---|
[f22b43c] | 1116 | def get_cat_combo_box_pos(self, state): |
---|
| 1117 | """ |
---|
| 1118 | Iterate through the categories to find the structurefactor |
---|
| 1119 | :return: combo_box_position |
---|
| 1120 | """ |
---|
| 1121 | for key, value in self.master_category_dict.iteritems(): |
---|
[db5294e] | 1122 | formfactor = state.formfactorcombobox.split(":") |
---|
| 1123 | if isinstance(formfactor, list): |
---|
| 1124 | formfactor = formfactor[0] |
---|
[f22b43c] | 1125 | for list_item in value: |
---|
[db5294e] | 1126 | if formfactor in list_item: |
---|
[f22b43c] | 1127 | return self.categorybox.Items.index(key) |
---|
[c8e1996] | 1128 | return 0 |
---|
[f22b43c] | 1129 | |
---|
[f0d720b] | 1130 | def reset_page_helper(self, state): |
---|
| 1131 | """ |
---|
| 1132 | Use page_state and change the state of existing page |
---|
[5ce7f17] | 1133 | |
---|
[f0d720b] | 1134 | :precondition: the page is already drawn or created |
---|
[5ce7f17] | 1135 | |
---|
[e92a352] | 1136 | :postcondition: the state of the underlying data changes as well as the |
---|
[f0d720b] | 1137 | state of the graphic interface |
---|
| 1138 | """ |
---|
[c8e1996] | 1139 | if state is None: |
---|
[f0d720b] | 1140 | return |
---|
| 1141 | # set data, etc. from the state |
---|
| 1142 | # reset page between theory and fitting from bookmarking |
---|
| 1143 | data = state.data |
---|
| 1144 | |
---|
[c8e1996] | 1145 | if data is None: |
---|
[f0d720b] | 1146 | data_min = state.qmin |
---|
| 1147 | data_max = state.qmax |
---|
| 1148 | self.qmin_x = data_min |
---|
| 1149 | self.qmax_x = data_max |
---|
| 1150 | self.qmin.SetValue(str(data_min)) |
---|
| 1151 | self.qmax.SetValue(str(data_max)) |
---|
| 1152 | |
---|
| 1153 | self.state.data = data |
---|
| 1154 | self.state.qmin = self.qmin_x |
---|
| 1155 | self.state.qmax = self.qmax_x |
---|
| 1156 | else: |
---|
| 1157 | self.set_data(data) |
---|
[5ce7f17] | 1158 | |
---|
[f0d720b] | 1159 | self.enable2D = state.enable2D |
---|
| 1160 | try: |
---|
| 1161 | self.magnetic_on = state.magnetic_on |
---|
| 1162 | except: |
---|
| 1163 | # Backward compatibility (for older state files) |
---|
| 1164 | self.magnetic_on = False |
---|
| 1165 | |
---|
| 1166 | self.disp_cb_dict = state.disp_cb_dict |
---|
| 1167 | self.disp_list = state.disp_list |
---|
[5ce7f17] | 1168 | |
---|
[c8e1996] | 1169 | # fill model combobox |
---|
[f0d720b] | 1170 | self._show_combox_helper() |
---|
[c8e1996] | 1171 | # select the current model |
---|
[0633048] | 1172 | state._convert_to_sasmodels() |
---|
| 1173 | state.categorycombobox = unicode(state.categorycombobox) |
---|
| 1174 | if state.categorycombobox in self.categorybox.Items: |
---|
| 1175 | category_pos = self.categorybox.Items.index( |
---|
| 1176 | state.categorycombobox) |
---|
| 1177 | else: |
---|
| 1178 | # Look in master list for model name (model.lower) |
---|
| 1179 | category_pos = self.get_cat_combo_box_pos(state) |
---|
[5ce7f17] | 1180 | |
---|
[f0d720b] | 1181 | self.categorybox.Select(category_pos) |
---|
| 1182 | self._show_combox(None) |
---|
[0de74af] | 1183 | from models import PLUGIN_NAME_BASE |
---|
[4387385] | 1184 | if self.categorybox.GetValue() == CUSTOM_MODEL \ |
---|
[0de74af] | 1185 | and PLUGIN_NAME_BASE not in state.formfactorcombobox: |
---|
[0633048] | 1186 | state.formfactorcombobox = \ |
---|
[0de74af] | 1187 | PLUGIN_NAME_BASE + state.formfactorcombobox |
---|
[0633048] | 1188 | formfactor_pos = 0 |
---|
| 1189 | for ind_form in range(self.formfactorbox.GetCount()): |
---|
| 1190 | if self.formfactorbox.GetString(ind_form) == \ |
---|
| 1191 | (state.formfactorcombobox): |
---|
| 1192 | formfactor_pos = int(ind_form) |
---|
| 1193 | break |
---|
[5ce7f17] | 1194 | |
---|
[f0d720b] | 1195 | self.formfactorbox.Select(formfactor_pos) |
---|
[5ce7f17] | 1196 | |
---|
[c8e1996] | 1197 | structfactor_pos = 0 |
---|
[0633048] | 1198 | if state.structurecombobox is not None: |
---|
| 1199 | state.structurecombobox = unicode(state.structurecombobox) |
---|
| 1200 | for ind_struct in range(self.structurebox.GetCount()): |
---|
| 1201 | if self.structurebox.GetString(ind_struct) == \ |
---|
| 1202 | (state.structurecombobox): |
---|
| 1203 | structfactor_pos = int(ind_struct) |
---|
| 1204 | break |
---|
[5ce7f17] | 1205 | |
---|
[f0d720b] | 1206 | self.structurebox.SetSelection(structfactor_pos) |
---|
| 1207 | |
---|
[c8e1996] | 1208 | if state.multi_factor is not None: |
---|
[f0d720b] | 1209 | self.multifactorbox.SetSelection(state.multi_factor) |
---|
| 1210 | |
---|
[c8e1996] | 1211 | # draw the panel according to the new model parameter |
---|
[f0d720b] | 1212 | self._on_select_model(event=None) |
---|
[5ce7f17] | 1213 | |
---|
[f0d720b] | 1214 | # take care of 2D button |
---|
[c8e1996] | 1215 | if data is None and self.model_view.IsEnabled(): |
---|
[f0d720b] | 1216 | if self.enable2D: |
---|
| 1217 | self.model_view.SetLabel("2D Mode") |
---|
| 1218 | else: |
---|
| 1219 | self.model_view.SetLabel("1D Mode") |
---|
[bac3988] | 1220 | |
---|
[c8e1996] | 1221 | # reset state of checkbox,textcrtl and regular parameters value |
---|
[f0d720b] | 1222 | self._reset_parameters_state(self.orientation_params_disp, |
---|
| 1223 | state.orientation_params_disp) |
---|
| 1224 | self._reset_parameters_state(self.orientation_params, |
---|
| 1225 | state.orientation_params) |
---|
| 1226 | self._reset_parameters_state(self.str_parameters, |
---|
| 1227 | state.str_parameters) |
---|
| 1228 | self._reset_parameters_state(self.parameters, state.parameters) |
---|
[c8e1996] | 1229 | # display dispersion info layer |
---|
[f0d720b] | 1230 | self.enable_disp.SetValue(state.enable_disp) |
---|
| 1231 | self.disable_disp.SetValue(state.disable_disp) |
---|
| 1232 | # If the polydispersion is ON |
---|
| 1233 | if state.enable_disp: |
---|
| 1234 | # reset dispersion according the state |
---|
| 1235 | self._set_dipers_Param(event=None) |
---|
| 1236 | self._reset_page_disp_helper(state) |
---|
[c8e1996] | 1237 | # plotting range restore |
---|
[f0d720b] | 1238 | self._reset_plotting_range(state) |
---|
[c8e1996] | 1239 | # smearing info restore |
---|
[f0d720b] | 1240 | if hasattr(self, "enable_smearer"): |
---|
[c8e1996] | 1241 | # set smearing value whether or not the data |
---|
| 1242 | # contain the smearing info |
---|
[f0d720b] | 1243 | self.enable_smearer.SetValue(state.enable_smearer) |
---|
| 1244 | self.disable_smearer.SetValue(state.disable_smearer) |
---|
| 1245 | self.onSmear(event=None) |
---|
| 1246 | self.pinhole_smearer.SetValue(state.pinhole_smearer) |
---|
| 1247 | self.slit_smearer.SetValue(state.slit_smearer) |
---|
| 1248 | try: |
---|
| 1249 | self.dI_noweight.SetValue(state.dI_noweight) |
---|
| 1250 | self.dI_didata.SetValue(state.dI_didata) |
---|
| 1251 | self.dI_sqrdata.SetValue(state.dI_sqrdata) |
---|
| 1252 | self.dI_idata.SetValue(state.dI_idata) |
---|
| 1253 | except: |
---|
| 1254 | # to support older state file formats |
---|
| 1255 | self.dI_noweight.SetValue(False) |
---|
| 1256 | self.dI_didata.SetValue(True) |
---|
| 1257 | self.dI_sqrdata.SetValue(False) |
---|
| 1258 | self.dI_idata.SetValue(False) |
---|
[5ce7f17] | 1259 | |
---|
[c8e1996] | 1260 | # we have two more options for smearing |
---|
[f0d720b] | 1261 | if self.pinhole_smearer.GetValue(): |
---|
[d85f1d8a] | 1262 | self.dx_percent = state.dx_percent |
---|
[7e98655] | 1263 | if self.dx_percent is not None: |
---|
[096181d] | 1264 | if state.dx_old: |
---|
[b301db9] | 1265 | self.dx_percent = 100 * (self.dx_percent / self.data.x[0]) |
---|
[096181d] | 1266 | self.smear_pinhole_percent.SetValue("%.2f" % self.dx_percent) |
---|
[f0d720b] | 1267 | self.onPinholeSmear(event=None) |
---|
| 1268 | elif self.slit_smearer.GetValue(): |
---|
| 1269 | self.dxl = state.dxl |
---|
| 1270 | self.dxw = state.dxw |
---|
[c8e1996] | 1271 | if self.dxl is not None: |
---|
[f0d720b] | 1272 | self.smear_slit_height.SetValue(str(self.dxl)) |
---|
[c8e1996] | 1273 | if self.dxw is not None: |
---|
[5ce7f17] | 1274 | self.smear_slit_width.SetValue(str(self.dxw)) |
---|
[f0d720b] | 1275 | else: |
---|
[5ce7f17] | 1276 | self.smear_slit_width.SetValue('') |
---|
[f0d720b] | 1277 | self.onSlitSmear(event=None) |
---|
[5ce7f17] | 1278 | |
---|
[c8e1996] | 1279 | # reset state of checkbox,textcrtl and dispersity parameters value |
---|
[f0d720b] | 1280 | self._reset_parameters_state(self.fittable_param, state.fittable_param) |
---|
| 1281 | self._reset_parameters_state(self.fixed_param, state.fixed_param) |
---|
[5ce7f17] | 1282 | |
---|
[c8e1996] | 1283 | # draw the model with previous parameters value |
---|
[f0d720b] | 1284 | self._onparamEnter_helper() |
---|
[c8e1996] | 1285 | # reset the value of chisqr when not consistent with the value computed |
---|
[f0d720b] | 1286 | self.tcChi.SetValue(str(self.state.tcChi)) |
---|
[c8e1996] | 1287 | # reset context menu items |
---|
[f0d720b] | 1288 | self._reset_context_menu() |
---|
[5ce7f17] | 1289 | |
---|
[c8e1996] | 1290 | # set the value of the current state to the state given as parameter |
---|
[f0d720b] | 1291 | self.state = state.clone() |
---|
| 1292 | self.state.m_name = self.m_name |
---|
[5ce7f17] | 1293 | |
---|
[f0d720b] | 1294 | def _reset_page_disp_helper(self, state): |
---|
| 1295 | """ |
---|
| 1296 | Help to rest page for dispersions |
---|
| 1297 | """ |
---|
| 1298 | keys = self.model.getParamList() |
---|
| 1299 | for item in keys: |
---|
| 1300 | if item in self.disp_list and \ |
---|
[c8e1996] | 1301 | item not in self.model.details: |
---|
[f0d720b] | 1302 | self.model.details[item] = ["", None, None] |
---|
[c8e1996] | 1303 | # for k,v in self.state.disp_cb_dict.iteritems(): |
---|
[f0d720b] | 1304 | self.disp_cb_dict = copy.deepcopy(state.disp_cb_dict) |
---|
| 1305 | self.state.disp_cb_dict = copy.deepcopy(state.disp_cb_dict) |
---|
| 1306 | self.values = copy.deepcopy(state.values) |
---|
| 1307 | self.weights = copy.deepcopy(state.weights) |
---|
[5ce7f17] | 1308 | |
---|
[6c382da] | 1309 | for key, disp_type in state._disp_obj_dict.iteritems(): |
---|
[c8e1996] | 1310 | # disp_model = disp |
---|
[6c382da] | 1311 | disp_model = POLYDISPERSITY_MODELS[disp_type]() |
---|
[f0d720b] | 1312 | self._disp_obj_dict[key] = disp_model |
---|
| 1313 | param_name = key.split('.')[0] |
---|
| 1314 | # Try to set dispersion only when available |
---|
| 1315 | # for eg., pass the orient. angles for 1D Cal |
---|
| 1316 | try: |
---|
| 1317 | self.model.set_dispersion(param_name, disp_model) |
---|
| 1318 | self.model._persistency_dict[key] = \ |
---|
[c8e1996] | 1319 | [state.values, state.weights] |
---|
[7673ecd] | 1320 | except Exception: |
---|
[c155a16] | 1321 | logger.error(traceback.format_exc()) |
---|
[f0d720b] | 1322 | selection = self._find_polyfunc_selection(disp_model) |
---|
| 1323 | for list in self.fittable_param: |
---|
[c8e1996] | 1324 | if list[1] == key and list[7] is not None: |
---|
[f0d720b] | 1325 | list[7].SetSelection(selection) |
---|
| 1326 | # For the array disp_model, set the values and weights |
---|
| 1327 | if selection == 1: |
---|
| 1328 | disp_model.set_weights(self.values[key], |
---|
| 1329 | self.weights[key]) |
---|
| 1330 | try: |
---|
| 1331 | # Diables all fittable params for array |
---|
| 1332 | list[0].SetValue(False) |
---|
| 1333 | list[0].Disable() |
---|
| 1334 | list[2].Disable() |
---|
| 1335 | list[5].Disable() |
---|
| 1336 | list[6].Disable() |
---|
[7673ecd] | 1337 | except Exception: |
---|
[c155a16] | 1338 | logger.error(traceback.format_exc()) |
---|
[f0d720b] | 1339 | # For array, disable all fixed params |
---|
| 1340 | if selection == 1: |
---|
| 1341 | for item in self.fixed_param: |
---|
| 1342 | if item[1].split(".")[0] == key.split(".")[0]: |
---|
| 1343 | # try it and pass it for the orientation for 1D |
---|
| 1344 | try: |
---|
| 1345 | item[2].Disable() |
---|
[7673ecd] | 1346 | except Exception: |
---|
[c155a16] | 1347 | logger.error(traceback.format_exc()) |
---|
[5ce7f17] | 1348 | |
---|
[f0d720b] | 1349 | def _selectDlg(self): |
---|
| 1350 | """ |
---|
[e92a352] | 1351 | open a dialog file to select the customized polydispersity function |
---|
[f0d720b] | 1352 | """ |
---|
[c8e1996] | 1353 | if self.parent is not None: |
---|
[f0d720b] | 1354 | self._default_save_location = \ |
---|
| 1355 | self._manager.parent.get_save_location() |
---|
| 1356 | dlg = wx.FileDialog(self, "Choose a weight file", |
---|
[5ce7f17] | 1357 | self._default_save_location, "", |
---|
| 1358 | "*.*", wx.OPEN) |
---|
[f0d720b] | 1359 | path = None |
---|
| 1360 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 1361 | path = dlg.GetPath() |
---|
| 1362 | dlg.Destroy() |
---|
| 1363 | return path |
---|
| 1364 | |
---|
| 1365 | def _reset_context_menu(self): |
---|
| 1366 | """ |
---|
| 1367 | reset the context menu |
---|
| 1368 | """ |
---|
[6f16e25] | 1369 | ids = iter(self._id_pool) # Reusing ids for context menu |
---|
[f0d720b] | 1370 | for name, _ in self.state.saved_states.iteritems(): |
---|
| 1371 | self.number_saved_state += 1 |
---|
[c8e1996] | 1372 | # Add item in the context menu |
---|
[6f16e25] | 1373 | wx_id = ids.next() |
---|
[f0d720b] | 1374 | msg = 'Save model and state %g' % self.number_saved_state |
---|
[6f16e25] | 1375 | self.popUpMenu.Append(wx_id, name, msg) |
---|
| 1376 | wx.EVT_MENU(self, wx_id, self.onResetModel) |
---|
[5ce7f17] | 1377 | |
---|
[f0d720b] | 1378 | def _reset_plotting_range(self, state): |
---|
| 1379 | """ |
---|
| 1380 | Reset the plotting range to a given state |
---|
| 1381 | """ |
---|
| 1382 | self.qmin.SetValue(str(state.qmin)) |
---|
| 1383 | self.qmax.SetValue(str(state.qmax)) |
---|
| 1384 | |
---|
| 1385 | def _save_typeOfmodel(self): |
---|
| 1386 | """ |
---|
| 1387 | save radiobutton containing the type model that can be selected |
---|
| 1388 | """ |
---|
[c8e1996] | 1389 | # self.state.shape_rbutton = self.shape_rbutton.GetValue() |
---|
| 1390 | # self.state.shape_indep_rbutton = self.shape_indep_rbutton.GetValue() |
---|
| 1391 | # self.state.struct_rbutton = self.struct_rbutton.GetValue() |
---|
| 1392 | # self.state.plugin_rbutton = self.plugin_rbutton.GetValue() |
---|
[77910cf] | 1393 | self.state.structurecombobox = self.structurebox.GetValue() |
---|
| 1394 | self.state.formfactorcombobox = self.formfactorbox.GetValue() |
---|
| 1395 | self.state.categorycombobox = self.categorybox.GetValue() |
---|
[5ce7f17] | 1396 | |
---|
[c8e1996] | 1397 | # post state to fit panel |
---|
[f0d720b] | 1398 | event = PageInfoEvent(page=self) |
---|
| 1399 | wx.PostEvent(self.parent, event) |
---|
[5ce7f17] | 1400 | |
---|
[f0d720b] | 1401 | def _save_plotting_range(self): |
---|
| 1402 | """ |
---|
| 1403 | save the state of plotting range |
---|
| 1404 | """ |
---|
| 1405 | self.state.qmin = self.qmin_x |
---|
| 1406 | self.state.qmax = self.qmax_x |
---|
| 1407 | self.state.npts = self.npts_x |
---|
[5ce7f17] | 1408 | |
---|
[c8e1996] | 1409 | def _onparamEnter_helper(self, is_modified=False): |
---|
[f0d720b] | 1410 | """ |
---|
| 1411 | check if values entered by the user are changed and valid to replot |
---|
| 1412 | model |
---|
| 1413 | """ |
---|
| 1414 | # Flag to register when a parameter has changed. |
---|
[c8e1996] | 1415 | # is_modified = False |
---|
[f0d720b] | 1416 | self.fitrange = True |
---|
| 1417 | is_2Ddata = False |
---|
[c8e1996] | 1418 | # self._undo.Enable(True) |
---|
[f0d720b] | 1419 | # check if 2d data |
---|
| 1420 | if self.data.__class__.__name__ == "Data2D": |
---|
| 1421 | is_2Ddata = True |
---|
[c8e1996] | 1422 | if self.model is not None: |
---|
| 1423 | # Either we get a is_modified = True passed in because |
---|
| 1424 | # _update_paramv_on_fit() has been called already or |
---|
[8662a58] | 1425 | # we need to check here ourselves. |
---|
| 1426 | if not is_modified: |
---|
| 1427 | is_modified = (self._check_value_enter(self.fittable_param) |
---|
| 1428 | or self._check_value_enter(self.fixed_param) |
---|
| 1429 | or self._check_value_enter(self.parameters)) |
---|
[f0d720b] | 1430 | |
---|
| 1431 | # Here we should check whether the boundaries have been modified. |
---|
| 1432 | # If qmin and qmax have been modified, update qmin and qmax and |
---|
| 1433 | # set the is_modified flag to True |
---|
| 1434 | if self._validate_qrange(self.qmin, self.qmax): |
---|
| 1435 | tempmin = float(self.qmin.GetValue()) |
---|
| 1436 | if tempmin != self.qmin_x: |
---|
| 1437 | self.qmin_x = tempmin |
---|
| 1438 | is_modified = True |
---|
| 1439 | tempmax = float(self.qmax.GetValue()) |
---|
| 1440 | if tempmax != self.qmax_x: |
---|
| 1441 | self.qmax_x = tempmax |
---|
| 1442 | is_modified = True |
---|
| 1443 | if is_2Ddata: |
---|
| 1444 | is_modified = self._validate_Npts() |
---|
[99f3e053] | 1445 | else: |
---|
| 1446 | is_modified = self._validate_Npts_1D() |
---|
[f0d720b] | 1447 | else: |
---|
| 1448 | self.fitrange = False |
---|
[5ce7f17] | 1449 | |
---|
[c8e1996] | 1450 | # if any value is modify draw model with new value |
---|
[f0d720b] | 1451 | if not self.fitrange: |
---|
[c8e1996] | 1452 | # self.btFit.Disable() |
---|
[f0d720b] | 1453 | if is_2Ddata: |
---|
| 1454 | self.btEditMask.Disable() |
---|
| 1455 | else: |
---|
| 1456 | if is_2Ddata and self.data.is_data and not self.batch_on: |
---|
| 1457 | self.btEditMask.Enable(True) |
---|
| 1458 | if is_modified and self.fitrange: |
---|
| 1459 | # Theory case: need to get npts value to draw |
---|
| 1460 | self.npts_x = float(self.Npts_total.GetValue()) |
---|
[3f8c7bb] | 1461 | self.Npts_fit.SetValue(str(self.Npts_total.GetValue())) |
---|
| 1462 | self._save_plotting_range() |
---|
[f0d720b] | 1463 | self.create_default_data() |
---|
| 1464 | self.state_change = True |
---|
| 1465 | self._draw_model() |
---|
| 1466 | self.Refresh() |
---|
[c65a265] | 1467 | |
---|
[c155a16] | 1468 | # logger.info("is_modified flag set to %g",is_modified) |
---|
[f0d720b] | 1469 | return is_modified |
---|
[5ce7f17] | 1470 | |
---|
[f0d720b] | 1471 | def _update_paramv_on_fit(self): |
---|
| 1472 | """ |
---|
| 1473 | make sure that update param values just before the fitting |
---|
| 1474 | """ |
---|
[c8e1996] | 1475 | # flag for qmin qmax check values |
---|
[f0d720b] | 1476 | flag = True |
---|
| 1477 | self.fitrange = True |
---|
[8662a58] | 1478 | is_modified = False |
---|
[f0d720b] | 1479 | |
---|
[c8e1996] | 1480 | # wx.PostEvent(self._manager.parent, StatusEvent(status=" \ |
---|
| 1481 | # updating ... ",type="update")) |
---|
[f0d720b] | 1482 | |
---|
[c8e1996] | 1483 | # So make sure that update param values on_Fit. |
---|
| 1484 | # self._undo.Enable(True) |
---|
| 1485 | if self.model is not None: |
---|
[f0d720b] | 1486 | if self.Npts_total.GetValue() != self.Npts_fit.GetValue(): |
---|
| 1487 | if not self.data.is_data: |
---|
[c8e1996] | 1488 | self._manager.page_finder[self.uid].set_fit_data( |
---|
| 1489 | data=[self.data]) |
---|
| 1490 | # Check the values |
---|
[8662a58] | 1491 | is_modified = (self._check_value_enter(self.fittable_param) |
---|
[c8e1996] | 1492 | or self._check_value_enter(self.fixed_param) |
---|
| 1493 | or self._check_value_enter(self.parameters)) |
---|
[f0d720b] | 1494 | |
---|
[5ce7f17] | 1495 | # If qmin and qmax have been modified, update qmin and qmax and |
---|
[f0d720b] | 1496 | # Here we should check whether the boundaries have been modified. |
---|
[5ce7f17] | 1497 | # If qmin and qmax have been modified, update qmin and qmax and |
---|
[f0d720b] | 1498 | # set the is_modified flag to True |
---|
| 1499 | self.fitrange = self._validate_qrange(self.qmin, self.qmax) |
---|
| 1500 | if self.fitrange: |
---|
| 1501 | tempmin = float(self.qmin.GetValue()) |
---|
| 1502 | if tempmin != self.qmin_x: |
---|
| 1503 | self.qmin_x = tempmin |
---|
| 1504 | tempmax = float(self.qmax.GetValue()) |
---|
| 1505 | if tempmax != self.qmax_x: |
---|
| 1506 | self.qmax_x = tempmax |
---|
| 1507 | if tempmax == tempmin: |
---|
| 1508 | flag = False |
---|
| 1509 | temp_smearer = None |
---|
| 1510 | if not self.disable_smearer.GetValue(): |
---|
| 1511 | temp_smearer = self.current_smearer |
---|
| 1512 | if self.slit_smearer.GetValue(): |
---|
| 1513 | flag = self.update_slit_smear() |
---|
| 1514 | elif self.pinhole_smearer.GetValue(): |
---|
| 1515 | flag = self.update_pinhole_smear() |
---|
| 1516 | else: |
---|
[5ce7f17] | 1517 | enable_smearer = not self.disable_smearer.GetValue() |
---|
[f0d720b] | 1518 | self._manager.set_smearer(smearer=temp_smearer, |
---|
| 1519 | uid=self.uid, |
---|
| 1520 | fid=self.data.id, |
---|
| 1521 | qmin=float(self.qmin_x), |
---|
| 1522 | qmax=float(self.qmax_x), |
---|
[cd5e29b] | 1523 | enable_smearer=enable_smearer, |
---|
[5ce7f17] | 1524 | draw=False) |
---|
[f0d720b] | 1525 | elif not self._is_2D(): |
---|
[cd5e29b] | 1526 | enable_smearer = not self.disable_smearer.GetValue() |
---|
[f0d720b] | 1527 | self._manager.set_smearer(smearer=temp_smearer, |
---|
| 1528 | qmin=float(self.qmin_x), |
---|
| 1529 | uid=self.uid, |
---|
| 1530 | fid=self.data.id, |
---|
| 1531 | qmax=float(self.qmax_x), |
---|
[cd5e29b] | 1532 | enable_smearer=enable_smearer, |
---|
[373d4ee] | 1533 | draw=False) |
---|
[c8e1996] | 1534 | if self.data is not None: |
---|
| 1535 | index_data = ((self.qmin_x <= self.data.x) & |
---|
[f0d720b] | 1536 | (self.data.x <= self.qmax_x)) |
---|
[d3911e3] | 1537 | val = str(len(self.data.x[index_data])) |
---|
[f0d720b] | 1538 | self.Npts_fit.SetValue(val) |
---|
| 1539 | else: |
---|
| 1540 | # No data in the panel |
---|
| 1541 | try: |
---|
| 1542 | self.npts_x = float(self.Npts_total.GetValue()) |
---|
| 1543 | except: |
---|
| 1544 | flag = False |
---|
| 1545 | return flag |
---|
| 1546 | flag = True |
---|
| 1547 | if self._is_2D(): |
---|
| 1548 | # only 2D case set mask |
---|
| 1549 | flag = self._validate_Npts() |
---|
| 1550 | if not flag: |
---|
| 1551 | return flag |
---|
| 1552 | else: |
---|
| 1553 | flag = False |
---|
| 1554 | else: |
---|
| 1555 | flag = False |
---|
| 1556 | |
---|
[c8e1996] | 1557 | # For invalid q range, disable the mask editor and fit button, vs. |
---|
[f0d720b] | 1558 | if not self.fitrange: |
---|
| 1559 | if self._is_2D(): |
---|
| 1560 | self.btEditMask.Disable() |
---|
| 1561 | else: |
---|
[c8e1996] | 1562 | if self._is_2D() and self.data.is_data and not self.batch_on: |
---|
[f0d720b] | 1563 | self.btEditMask.Enable(True) |
---|
| 1564 | |
---|
| 1565 | if not flag: |
---|
| 1566 | msg = "Cannot Plot or Fit :Must select a " |
---|
| 1567 | msg += " model or Fitting range is not valid!!! " |
---|
| 1568 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
[5ce7f17] | 1569 | |
---|
[f0d720b] | 1570 | try: |
---|
| 1571 | self.save_current_state() |
---|
[7673ecd] | 1572 | except Exception: |
---|
[c155a16] | 1573 | logger.error(traceback.format_exc()) |
---|
[5ce7f17] | 1574 | |
---|
[c8e1996] | 1575 | return flag, is_modified |
---|
[5ce7f17] | 1576 | |
---|
[f0d720b] | 1577 | def _reset_parameters_state(self, listtorestore, statelist): |
---|
| 1578 | """ |
---|
| 1579 | Reset the parameters at the given state |
---|
| 1580 | """ |
---|
| 1581 | if len(statelist) == 0 or len(listtorestore) == 0: |
---|
| 1582 | return |
---|
| 1583 | |
---|
| 1584 | for j in range(len(listtorestore)): |
---|
[64c7f39] | 1585 | for param in statelist: |
---|
[7602675] | 1586 | if param[1] == listtorestore[j][1]: |
---|
| 1587 | item_page = listtorestore[j] |
---|
[0b1d7d3] | 1588 | item_page_info = param |
---|
[7602675] | 1589 | if (item_page_info[1] == "theta" or item_page_info[1] == |
---|
| 1590 | "phi") and not self._is_2D(): |
---|
| 1591 | break |
---|
| 1592 | # change the state of the check box for simple parameters |
---|
| 1593 | if item_page[0] is not None: |
---|
| 1594 | item_page[0].SetValue(item_page_info[0]) |
---|
| 1595 | if item_page[2] is not None: |
---|
| 1596 | item_page[2].SetValue(item_page_info[2]) |
---|
| 1597 | if item_page[2].__class__.__name__ == "ComboBox": |
---|
| 1598 | if item_page_info[2] in self.model.fun_list: |
---|
| 1599 | fun_val = self.model.fun_list[item_page_info[2]] |
---|
| 1600 | self.model.setParam(item_page_info[1], fun_val) |
---|
| 1601 | if item_page[3] is not None: |
---|
| 1602 | # show or hide text +/- |
---|
| 1603 | if item_page_info[2]: |
---|
| 1604 | item_page[3].Show(True) |
---|
| 1605 | else: |
---|
| 1606 | item_page[3].Hide() |
---|
| 1607 | if item_page[4] is not None: |
---|
| 1608 | # show of hide the text crtl for fitting error |
---|
| 1609 | if item_page_info[4][0]: |
---|
| 1610 | item_page[4].Show(True) |
---|
[a321c52] | 1611 | item_page[4].SetValue(str(item_page_info[4][1])) |
---|
[7602675] | 1612 | else: |
---|
| 1613 | item_page[3].Hide() |
---|
| 1614 | if item_page[5] is not None: |
---|
| 1615 | # show of hide the text crtl for fitting error |
---|
[a321c52] | 1616 | item_page[5].Show(True) |
---|
| 1617 | item_page[5].SetValue(str(item_page_info[5][1])) |
---|
[7602675] | 1618 | if item_page[6] is not None: |
---|
| 1619 | # show of hide the text crtl for fitting error |
---|
[a321c52] | 1620 | item_page[6].Show(True) |
---|
| 1621 | item_page[6].SetValue(str(item_page_info[6][1])) |
---|
[7602675] | 1622 | break |
---|
[5ce7f17] | 1623 | |
---|
[f0d720b] | 1624 | def _reset_strparam_state(self, listtorestore, statelist): |
---|
| 1625 | """ |
---|
| 1626 | Reset the string parameters at the given state |
---|
| 1627 | """ |
---|
| 1628 | if len(statelist) == 0: |
---|
| 1629 | return |
---|
| 1630 | |
---|
| 1631 | listtorestore = copy.deepcopy(statelist) |
---|
[5ce7f17] | 1632 | |
---|
[f0d720b] | 1633 | for j in range(len(listtorestore)): |
---|
| 1634 | item_page = listtorestore[j] |
---|
| 1635 | item_page_info = statelist[j] |
---|
[c8e1996] | 1636 | # change the state of the check box for simple parameters |
---|
[5ce7f17] | 1637 | |
---|
[c8e1996] | 1638 | if item_page[0] is not None: |
---|
[f0d720b] | 1639 | item_page[0].SetValue(format_number(item_page_info[0], True)) |
---|
| 1640 | |
---|
[c8e1996] | 1641 | if item_page[2] is not None: |
---|
[f0d720b] | 1642 | param_name = item_page_info[1] |
---|
| 1643 | value = item_page_info[2] |
---|
| 1644 | selection = value |
---|
| 1645 | if value in self.model.fun_list: |
---|
| 1646 | selection = self.model.fun_list[value] |
---|
| 1647 | item_page[2].SetValue(selection) |
---|
| 1648 | self.model.setParam(param_name, selection) |
---|
[5ce7f17] | 1649 | |
---|
[f0d720b] | 1650 | def _copy_parameters_state(self, listtocopy, statelist): |
---|
| 1651 | """ |
---|
| 1652 | copy the state of button |
---|
[5ce7f17] | 1653 | |
---|
[f0d720b] | 1654 | :param listtocopy: the list of check button to copy |
---|
| 1655 | :param statelist: list of state object to store the current state |
---|
[5ce7f17] | 1656 | |
---|
[f0d720b] | 1657 | """ |
---|
| 1658 | if len(listtocopy) == 0: |
---|
| 1659 | return |
---|
[5ce7f17] | 1660 | |
---|
[f0d720b] | 1661 | for item in listtocopy: |
---|
[5ce7f17] | 1662 | |
---|
[f0d720b] | 1663 | checkbox_state = None |
---|
[c8e1996] | 1664 | if item[0] is not None: |
---|
[f0d720b] | 1665 | checkbox_state = item[0].GetValue() |
---|
| 1666 | parameter_name = item[1] |
---|
| 1667 | parameter_value = None |
---|
[c8e1996] | 1668 | if item[2] is not None: |
---|
[f0d720b] | 1669 | parameter_value = item[2].GetValue() |
---|
| 1670 | static_text = None |
---|
[c8e1996] | 1671 | if item[3] is not None: |
---|
[f0d720b] | 1672 | static_text = item[3].IsShown() |
---|
| 1673 | error_value = None |
---|
| 1674 | error_state = None |
---|
[c8e1996] | 1675 | if item[4] is not None: |
---|
[f0d720b] | 1676 | error_value = item[4].GetValue() |
---|
| 1677 | error_state = item[4].IsShown() |
---|
[5ce7f17] | 1678 | |
---|
[f0d720b] | 1679 | min_value = None |
---|
| 1680 | min_state = None |
---|
[c8e1996] | 1681 | if item[5] is not None: |
---|
[f0d720b] | 1682 | min_value = item[5].GetValue() |
---|
| 1683 | min_state = item[5].IsShown() |
---|
[5ce7f17] | 1684 | |
---|
[f0d720b] | 1685 | max_value = None |
---|
| 1686 | max_state = None |
---|
[c8e1996] | 1687 | if item[6] is not None: |
---|
[f0d720b] | 1688 | max_value = item[6].GetValue() |
---|
| 1689 | max_state = item[6].IsShown() |
---|
| 1690 | unit = None |
---|
[c8e1996] | 1691 | if item[7] is not None: |
---|
[f0d720b] | 1692 | unit = item[7].GetLabel() |
---|
[5ce7f17] | 1693 | |
---|
[f0d720b] | 1694 | statelist.append([checkbox_state, parameter_name, parameter_value, |
---|
| 1695 | static_text, [error_state, error_value], |
---|
| 1696 | [min_state, min_value], |
---|
| 1697 | [max_state, max_value], unit]) |
---|
[5ce7f17] | 1698 | |
---|
[f0d720b] | 1699 | def _draw_model(self, update_chisqr=True, source='model'): |
---|
| 1700 | """ |
---|
| 1701 | Method to draw or refresh a plotted model. |
---|
| 1702 | The method will use the data member from the model page |
---|
| 1703 | to build a call to the fitting perspective manager. |
---|
[5ce7f17] | 1704 | |
---|
[f0d720b] | 1705 | :param chisqr: update chisqr value [bool] |
---|
| 1706 | """ |
---|
[922ef55] | 1707 | # Get the time |
---|
[c5c247e] | 1708 | currentTime = time() |
---|
[922ef55] | 1709 | |
---|
[5c6002a] | 1710 | # When loading we slam a number of fits through here |
---|
| 1711 | # let's filter these out to start with |
---|
| 1712 | if currentTime > (self.lastTimeFitSubmitted + 0.1): |
---|
| 1713 | # Submitting the rest |
---|
[c5c247e] | 1714 | self.threadedDrawQueue.put([update_chisqr, source]) |
---|
[5c6002a] | 1715 | print 'submitted!' |
---|
| 1716 | else: |
---|
| 1717 | pass |
---|
[c5c247e] | 1718 | |
---|
| 1719 | self.lastTimeFitSubmitted = currentTime |
---|
[9c9fae1] | 1720 | |
---|
| 1721 | def _threaded_draw_worker(self, threadedDrawQueue): |
---|
| 1722 | while True: |
---|
[d6353fb] | 1723 | # Check to see is a manager is running and a calc is running |
---|
[4b402a1] | 1724 | if ((self._manager.calc_1D is not None) and (self._manager.calc_1D.\ |
---|
| 1725 | isrunning() == True)) or ((self._manager.calc_2D is not None)\ |
---|
| 1726 | and (self._manager.calc_2D.isrunning() == True)): |
---|
[5c6002a] | 1727 | # If a manager is running a calculation |
---|
| 1728 | # then trim the queue |
---|
| 1729 | if self.threadedDrawQueue.qsize() > 1: |
---|
| 1730 | print '2' |
---|
| 1731 | for loopIter in range(threadedDrawQueue.qsize() - 1): |
---|
| 1732 | dump = self.threadedDrawQueue.get() |
---|
| 1733 | self.threadedDrawQueue.task_done() |
---|
| 1734 | print 'bounced' |
---|
| 1735 | print 'one element left' |
---|
[f2cc00b] | 1736 | else: |
---|
[5c6002a] | 1737 | # Otherwise, just run |
---|
[f2cc00b] | 1738 | inputVariables = threadedDrawQueue.get() |
---|
| 1739 | self._draw_model_after(inputVariables[0], inputVariables[1]) |
---|
[4b402a1] | 1740 | wx.PostEvent(self._manager.parent, StatusEvent(status = |
---|
| 1741 | "Computation is in progress...", type = "progress")) |
---|
[f2cc00b] | 1742 | threadedDrawQueue.task_done() |
---|
[5c6002a] | 1743 | print 'run' |
---|
[5ce7f17] | 1744 | |
---|
[f0d720b] | 1745 | def _draw_model_after(self, update_chisqr=True, source='model'): |
---|
| 1746 | """ |
---|
| 1747 | Method to draw or refresh a plotted model. |
---|
| 1748 | The method will use the data member from the model page |
---|
| 1749 | to build a call to the fitting perspective manager. |
---|
[5ce7f17] | 1750 | |
---|
[f0d720b] | 1751 | :param chisqr: update chisqr value [bool] |
---|
| 1752 | """ |
---|
[c8e1996] | 1753 | # if self.check_invalid_panel(): |
---|
[f0d720b] | 1754 | # return |
---|
[c8e1996] | 1755 | if self.model is not None: |
---|
[f0d720b] | 1756 | temp_smear = None |
---|
| 1757 | if hasattr(self, "enable_smearer"): |
---|
| 1758 | if not self.disable_smearer.GetValue(): |
---|
| 1759 | temp_smear = self.current_smearer |
---|
| 1760 | # compute weight for the current data |
---|
[d85c194] | 1761 | from sas.sasgui.perspectives.fitting.utils import get_weight |
---|
[f0d720b] | 1762 | flag = self.get_weight_flag() |
---|
| 1763 | weight = get_weight(data=self.data, is2d=self._is_2D(), flag=flag) |
---|
| 1764 | toggle_mode_on = self.model_view.IsEnabled() |
---|
| 1765 | is_2d = self._is_2D() |
---|
[c5c247e] | 1766 | |
---|
[f0d720b] | 1767 | self._manager.draw_model(self.model, |
---|
[c8e1996] | 1768 | data=self.data, |
---|
| 1769 | smearer=temp_smear, |
---|
| 1770 | qmin=float(self.qmin_x), |
---|
| 1771 | qmax=float(self.qmax_x), |
---|
| 1772 | page_id=self.uid, |
---|
| 1773 | toggle_mode_on=toggle_mode_on, |
---|
| 1774 | state=self.state, |
---|
| 1775 | enable2D=is_2d, |
---|
| 1776 | update_chisqr=update_chisqr, |
---|
| 1777 | source='model', |
---|
| 1778 | weight=weight) |
---|
[5ce7f17] | 1779 | |
---|
[f0d720b] | 1780 | def _on_show_sld(self, event=None): |
---|
| 1781 | """ |
---|
| 1782 | Plot SLD profile |
---|
| 1783 | """ |
---|
| 1784 | # get profile data |
---|
| 1785 | x, y = self.model.getProfile() |
---|
| 1786 | |
---|
[d7bb526] | 1787 | from sas.sasgui.plottools import Data1D as pf_data1d |
---|
[c8e1996] | 1788 | # from sas.sasgui.perspectives.theory.profile_dialog import SLDPanel |
---|
[d85c194] | 1789 | from sas.sasgui.guiframe.local_perspectives.plotting.profile_dialog \ |
---|
[c8e1996] | 1790 | import SLDPanel |
---|
[f0d720b] | 1791 | sld_data = pf_data1d(x, y) |
---|
| 1792 | sld_data.name = 'SLD' |
---|
| 1793 | sld_data.axes = self.sld_axes |
---|
[6f16e25] | 1794 | self.panel = SLDPanel(self, data=sld_data, axes=self.sld_axes, |
---|
| 1795 | id=wx.ID_ANY) |
---|
[f0d720b] | 1796 | self.panel.ShowModal() |
---|
[5ce7f17] | 1797 | |
---|
[f0d720b] | 1798 | def _set_multfactor_combobox(self, multiplicity=10): |
---|
| 1799 | """ |
---|
[e92a352] | 1800 | Set comboBox for multitfactor of CoreMultiShellModel |
---|
[f0d720b] | 1801 | :param multiplicit: no. of multi-functionality |
---|
| 1802 | """ |
---|
| 1803 | # build content of the combobox |
---|
| 1804 | for idx in range(0, multiplicity): |
---|
| 1805 | self.multifactorbox.Append(str(idx), int(idx)) |
---|
| 1806 | self._hide_multfactor_combobox() |
---|
[5ce7f17] | 1807 | |
---|
[f0d720b] | 1808 | def _show_multfactor_combobox(self): |
---|
| 1809 | """ |
---|
| 1810 | Show the comboBox of muitfactor of CoreMultiShellModel |
---|
| 1811 | """ |
---|
| 1812 | if not self.mutifactor_text.IsShown(): |
---|
| 1813 | self.mutifactor_text.Show(True) |
---|
| 1814 | self.mutifactor_text1.Show(True) |
---|
| 1815 | if not self.multifactorbox.IsShown(): |
---|
| 1816 | self.multifactorbox.Show(True) |
---|
[5ce7f17] | 1817 | |
---|
[f0d720b] | 1818 | def _hide_multfactor_combobox(self): |
---|
| 1819 | """ |
---|
| 1820 | Hide the comboBox of muitfactor of CoreMultiShellModel |
---|
| 1821 | """ |
---|
| 1822 | if self.mutifactor_text.IsShown(): |
---|
| 1823 | self.mutifactor_text.Hide() |
---|
| 1824 | self.mutifactor_text1.Hide() |
---|
| 1825 | if self.multifactorbox.IsShown(): |
---|
| 1826 | self.multifactorbox.Hide() |
---|
[5ce7f17] | 1827 | |
---|
[f0d720b] | 1828 | def formfactor_combo_init(self): |
---|
| 1829 | """ |
---|
| 1830 | First time calls _show_combox_helper |
---|
| 1831 | """ |
---|
| 1832 | self._show_combox(None) |
---|
[5ce7f17] | 1833 | |
---|
[f0d720b] | 1834 | def _show_combox_helper(self): |
---|
| 1835 | """ |
---|
| 1836 | Fill panel's combo box according to the type of model selected |
---|
| 1837 | """ |
---|
[4387385] | 1838 | |
---|
[f0d720b] | 1839 | mod_cat = self.categorybox.GetStringSelection() |
---|
| 1840 | self.structurebox.SetSelection(0) |
---|
| 1841 | self.structurebox.Disable() |
---|
| 1842 | self.formfactorbox.Clear() |
---|
[c8e1996] | 1843 | if mod_cat is None: |
---|
[f0d720b] | 1844 | return |
---|
| 1845 | m_list = [] |
---|
| 1846 | try: |
---|
[4387385] | 1847 | if mod_cat == CUSTOM_MODEL: |
---|
[f0d720b] | 1848 | for model in self.model_list_box[mod_cat]: |
---|
[2abe6bf] | 1849 | m_list.append(self.model_dict[model.name]) |
---|
[f0d720b] | 1850 | else: |
---|
| 1851 | cat_dic = self.master_category_dict[mod_cat] |
---|
| 1852 | for (model, enabled) in cat_dic: |
---|
| 1853 | if enabled: |
---|
| 1854 | m_list.append(self.model_dict[model]) |
---|
[7673ecd] | 1855 | except Exception: |
---|
| 1856 | msg = traceback.format_exc() |
---|
[f0d720b] | 1857 | wx.PostEvent(self._manager.parent, |
---|
| 1858 | StatusEvent(status=msg, info="error")) |
---|
| 1859 | self._populate_box(self.formfactorbox, m_list) |
---|
[5ce7f17] | 1860 | |
---|
| 1861 | def _on_modify_cat(self, event=None): |
---|
| 1862 | """ |
---|
[cd5e29b] | 1863 | Called when category manager is opened |
---|
[5ce7f17] | 1864 | """ |
---|
| 1865 | self._manager.parent.on_category_panel(event) |
---|
| 1866 | |
---|
[f0d720b] | 1867 | def _show_combox(self, event=None): |
---|
| 1868 | """ |
---|
| 1869 | Show combox box associate with type of model selected |
---|
| 1870 | """ |
---|
| 1871 | self.Show(False) |
---|
| 1872 | self._show_combox_helper() |
---|
| 1873 | self._on_select_model(event=None) |
---|
| 1874 | self.Show(True) |
---|
| 1875 | self._save_typeOfmodel() |
---|
| 1876 | self.sizer4_4.Layout() |
---|
| 1877 | self.sizer4.Layout() |
---|
| 1878 | self.Layout() |
---|
| 1879 | self.Refresh() |
---|
[5ce7f17] | 1880 | |
---|
[f0d720b] | 1881 | def _populate_box(self, combobox, list): |
---|
| 1882 | """ |
---|
| 1883 | fill combox box with dict item |
---|
[5ce7f17] | 1884 | |
---|
[f0d720b] | 1885 | :param list: contains item to fill the combox |
---|
| 1886 | item must model class |
---|
| 1887 | """ |
---|
| 1888 | mlist = [] |
---|
| 1889 | for models in list: |
---|
[cb4ef58] | 1890 | if models.name != "NoStructure": |
---|
| 1891 | mlist.append((models.name, models)) |
---|
[5ce7f17] | 1892 | |
---|
[f0d720b] | 1893 | # Sort the models |
---|
| 1894 | mlist_sorted = sorted(mlist) |
---|
| 1895 | for item in mlist_sorted: |
---|
| 1896 | combobox.Append(item[0], item[1]) |
---|
| 1897 | return 0 |
---|
[5ce7f17] | 1898 | |
---|
[f0d720b] | 1899 | def _onQrangeEnter(self, event): |
---|
| 1900 | """ |
---|
| 1901 | Check validity of value enter in the Q range field |
---|
[5ce7f17] | 1902 | |
---|
[f0d720b] | 1903 | """ |
---|
| 1904 | tcrtl = event.GetEventObject() |
---|
[c8e1996] | 1905 | # Clear msg if previously shown. |
---|
[f0d720b] | 1906 | msg = "" |
---|
| 1907 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
| 1908 | # Flag to register when a parameter has changed. |
---|
| 1909 | if tcrtl.GetValue().lstrip().rstrip() != "": |
---|
| 1910 | try: |
---|
| 1911 | float(tcrtl.GetValue()) |
---|
| 1912 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
| 1913 | # If qmin and qmax have been modified, update qmin and qmax |
---|
| 1914 | if self._validate_qrange(self.qmin, self.qmax): |
---|
| 1915 | tempmin = float(self.qmin.GetValue()) |
---|
| 1916 | if tempmin != self.qmin_x: |
---|
| 1917 | self.qmin_x = tempmin |
---|
| 1918 | tempmax = float(self.qmax.GetValue()) |
---|
| 1919 | if tempmax != self.qmax_x: |
---|
| 1920 | self.qmax_x = tempmax |
---|
| 1921 | else: |
---|
| 1922 | tcrtl.SetBackgroundColour("pink") |
---|
[cd5e29b] | 1923 | msg = "Model Error: wrong value entered: %s" % \ |
---|
[c8e1996] | 1924 | sys.exc_info()[1] |
---|
[f0d720b] | 1925 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
| 1926 | return |
---|
| 1927 | except: |
---|
| 1928 | tcrtl.SetBackgroundColour("pink") |
---|
[cd5e29b] | 1929 | msg = "Model Error: wrong value entered: %s" % sys.exc_info()[1] |
---|
[f0d720b] | 1930 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
| 1931 | return |
---|
[c8e1996] | 1932 | # Check if # of points for theory model are valid(>0). |
---|
| 1933 | if self.npts is not None: |
---|
[f0d720b] | 1934 | if check_float(self.npts): |
---|
| 1935 | temp_npts = float(self.npts.GetValue()) |
---|
| 1936 | if temp_npts != self.num_points: |
---|
| 1937 | self.num_points = temp_npts |
---|
| 1938 | else: |
---|
| 1939 | msg = "Cannot plot: No points in Q range!!! " |
---|
| 1940 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
| 1941 | else: |
---|
| 1942 | tcrtl.SetBackgroundColour("pink") |
---|
| 1943 | msg = "Model Error: wrong value entered!!!" |
---|
| 1944 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
| 1945 | self.save_current_state() |
---|
| 1946 | event = PageInfoEvent(page=self) |
---|
| 1947 | wx.PostEvent(self.parent, event) |
---|
| 1948 | self.state_change = False |
---|
[c8e1996] | 1949 | # Draw the model for a different range |
---|
[f0d720b] | 1950 | if not self.data.is_data: |
---|
| 1951 | self.create_default_data() |
---|
| 1952 | self._draw_model() |
---|
[5ce7f17] | 1953 | |
---|
[f0d720b] | 1954 | def _theory_qrange_enter(self, event): |
---|
| 1955 | """ |
---|
| 1956 | Check validity of value enter in the Q range field |
---|
| 1957 | """ |
---|
[5ce7f17] | 1958 | |
---|
[f0d720b] | 1959 | tcrtl = event.GetEventObject() |
---|
[c8e1996] | 1960 | # Clear msg if previously shown. |
---|
[f0d720b] | 1961 | msg = "" |
---|
| 1962 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
| 1963 | # Flag to register when a parameter has changed. |
---|
| 1964 | is_modified = False |
---|
| 1965 | if tcrtl.GetValue().lstrip().rstrip() != "": |
---|
| 1966 | try: |
---|
| 1967 | value = float(tcrtl.GetValue()) |
---|
| 1968 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
| 1969 | |
---|
| 1970 | # If qmin and qmax have been modified, update qmin and qmax |
---|
| 1971 | if self._validate_qrange(self.theory_qmin, self.theory_qmax): |
---|
| 1972 | tempmin = float(self.theory_qmin.GetValue()) |
---|
| 1973 | if tempmin != self.theory_qmin_x: |
---|
| 1974 | self.theory_qmin_x = tempmin |
---|
| 1975 | tempmax = float(self.theory_qmax.GetValue()) |
---|
| 1976 | if tempmax != self.qmax_x: |
---|
| 1977 | self.theory_qmax_x = tempmax |
---|
| 1978 | else: |
---|
| 1979 | tcrtl.SetBackgroundColour("pink") |
---|
[cd5e29b] | 1980 | msg = "Model Error: wrong value entered: %s" % \ |
---|
[c8e1996] | 1981 | sys.exc_info()[1] |
---|
[f0d720b] | 1982 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
| 1983 | return |
---|
| 1984 | except: |
---|
| 1985 | tcrtl.SetBackgroundColour("pink") |
---|
[cd5e29b] | 1986 | msg = "Model Error: wrong value entered: %s" % sys.exc_info()[1] |
---|
[f0d720b] | 1987 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
| 1988 | return |
---|
[c8e1996] | 1989 | # Check if # of points for theory model are valid(>0). |
---|
[f0d720b] | 1990 | if self.Npts_total.IsEditable(): |
---|
| 1991 | if check_float(self.Npts_total): |
---|
| 1992 | temp_npts = float(self.Npts_total.GetValue()) |
---|
| 1993 | if temp_npts != self.num_points: |
---|
| 1994 | self.num_points = temp_npts |
---|
| 1995 | is_modified = True |
---|
| 1996 | else: |
---|
| 1997 | msg = "Cannot Plot: No points in Q range!!! " |
---|
| 1998 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
| 1999 | else: |
---|
| 2000 | tcrtl.SetBackgroundColour("pink") |
---|
| 2001 | msg = "Model Error: wrong value entered!!!" |
---|
| 2002 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
| 2003 | self.save_current_state() |
---|
| 2004 | event = PageInfoEvent(page=self) |
---|
| 2005 | wx.PostEvent(self.parent, event) |
---|
| 2006 | self.state_change = False |
---|
[c8e1996] | 2007 | # Draw the model for a different range |
---|
[f0d720b] | 2008 | self.create_default_data() |
---|
| 2009 | self._draw_model() |
---|
[5ce7f17] | 2010 | |
---|
[f0d720b] | 2011 | def _on_select_model_helper(self): |
---|
| 2012 | """ |
---|
| 2013 | call back for model selection |
---|
| 2014 | """ |
---|
[c8e1996] | 2015 | # reset dictionary containing reference to dispersion |
---|
[f0d720b] | 2016 | self._disp_obj_dict = {} |
---|
| 2017 | self.disp_cb_dict = {} |
---|
| 2018 | self.temp_multi_functional = False |
---|
| 2019 | f_id = self.formfactorbox.GetCurrentSelection() |
---|
[c8e1996] | 2020 | # For MAC |
---|
[f0d720b] | 2021 | form_factor = None |
---|
| 2022 | if f_id >= 0: |
---|
| 2023 | form_factor = self.formfactorbox.GetClientData(f_id) |
---|
| 2024 | |
---|
[4109bd5] | 2025 | if form_factor is None or \ |
---|
| 2026 | not hasattr(form_factor, 'is_form_factor') or \ |
---|
[c8e1996] | 2027 | not form_factor.is_form_factor: |
---|
[f0d720b] | 2028 | self.structurebox.Hide() |
---|
| 2029 | self.text2.Hide() |
---|
| 2030 | self.structurebox.Disable() |
---|
| 2031 | self.structurebox.SetSelection(0) |
---|
| 2032 | self.text2.Disable() |
---|
| 2033 | else: |
---|
| 2034 | self.structurebox.Show() |
---|
| 2035 | self.text2.Show() |
---|
| 2036 | self.structurebox.Enable() |
---|
| 2037 | self.text2.Enable() |
---|
[5ce7f17] | 2038 | |
---|
[c8e1996] | 2039 | if form_factor is not None: |
---|
[f0d720b] | 2040 | # set multifactor for Mutifunctional models |
---|
[cb4ef58] | 2041 | if form_factor.is_multiplicity_model: |
---|
[f0d720b] | 2042 | m_id = self.multifactorbox.GetCurrentSelection() |
---|
[cb4ef58] | 2043 | multiplicity = form_factor.multiplicity_info[0] |
---|
[f0d720b] | 2044 | self.multifactorbox.Clear() |
---|
| 2045 | self._set_multfactor_combobox(multiplicity) |
---|
| 2046 | self._show_multfactor_combobox() |
---|
[c8e1996] | 2047 | # ToDo: this info should be called directly from the model |
---|
[cb4ef58] | 2048 | text = form_factor.multiplicity_info[1] # 'No. of Shells: ' |
---|
[f0d720b] | 2049 | |
---|
| 2050 | self.mutifactor_text.SetLabel(text) |
---|
| 2051 | if m_id > multiplicity - 1: |
---|
| 2052 | # default value |
---|
| 2053 | m_id = 1 |
---|
[5ce7f17] | 2054 | |
---|
[f0d720b] | 2055 | self.multi_factor = self.multifactorbox.GetClientData(m_id) |
---|
[c8e1996] | 2056 | if self.multi_factor is None: |
---|
[f0d720b] | 2057 | self.multi_factor = 0 |
---|
| 2058 | self.multifactorbox.SetSelection(m_id) |
---|
| 2059 | # Check len of the text1 and max_multiplicity |
---|
| 2060 | text = '' |
---|
| 2061 | if form_factor.multiplicity_info[0] == \ |
---|
[c8e1996] | 2062 | len(form_factor.multiplicity_info[2]): |
---|
[f0d720b] | 2063 | text = form_factor.multiplicity_info[2][self.multi_factor] |
---|
| 2064 | self.mutifactor_text1.SetLabel(text) |
---|
| 2065 | # Check if model has get sld profile. |
---|
| 2066 | if len(form_factor.multiplicity_info[3]) > 0: |
---|
| 2067 | self.sld_axes = form_factor.multiplicity_info[3] |
---|
| 2068 | self.show_sld_button.Show(True) |
---|
| 2069 | else: |
---|
| 2070 | self.sld_axes = "" |
---|
| 2071 | else: |
---|
| 2072 | self._hide_multfactor_combobox() |
---|
| 2073 | self.show_sld_button.Hide() |
---|
| 2074 | self.multi_factor = None |
---|
| 2075 | else: |
---|
| 2076 | self._hide_multfactor_combobox() |
---|
| 2077 | self.show_sld_button.Hide() |
---|
| 2078 | self.multi_factor = None |
---|
[5ce7f17] | 2079 | |
---|
[f0d720b] | 2080 | s_id = self.structurebox.GetCurrentSelection() |
---|
| 2081 | struct_factor = self.structurebox.GetClientData(s_id) |
---|
[5ce7f17] | 2082 | |
---|
[c8e1996] | 2083 | if struct_factor is not None: |
---|
[313c5c9] | 2084 | from sasmodels.sasview_model import MultiplicationModel |
---|
[cb4ef58] | 2085 | self.model = MultiplicationModel(form_factor(self.multi_factor), |
---|
| 2086 | struct_factor()) |
---|
[f0d720b] | 2087 | # multifunctional form factor |
---|
| 2088 | if len(form_factor.non_fittable) > 0: |
---|
| 2089 | self.temp_multi_functional = True |
---|
[c8e1996] | 2090 | elif form_factor is not None: |
---|
[5213d22] | 2091 | if self.multi_factor is not None: |
---|
| 2092 | self.model = form_factor(self.multi_factor) |
---|
| 2093 | else: |
---|
| 2094 | # old style plugin models do not accept a multiplicity argument |
---|
| 2095 | self.model = form_factor() |
---|
[f0d720b] | 2096 | else: |
---|
[cb4ef58] | 2097 | self.model = None |
---|
| 2098 | return |
---|
| 2099 | |
---|
[f0d720b] | 2100 | # check if model has magnetic parameters |
---|
| 2101 | if len(self.model.magnetic_params) > 0: |
---|
[5ce7f17] | 2102 | self._has_magnetic = True |
---|
[f0d720b] | 2103 | else: |
---|
[5ce7f17] | 2104 | self._has_magnetic = False |
---|
[c8e1996] | 2105 | # post state to fit panel |
---|
[f0d720b] | 2106 | self.state.parameters = [] |
---|
| 2107 | self.state.model = self.model |
---|
| 2108 | self.state.qmin = self.qmin_x |
---|
| 2109 | self.state.multi_factor = self.multi_factor |
---|
| 2110 | self.disp_list = self.model.getDispParamList() |
---|
| 2111 | self.state.disp_list = self.disp_list |
---|
| 2112 | self.on_set_focus(None) |
---|
| 2113 | self.Layout() |
---|
[5ce7f17] | 2114 | |
---|
[f0d720b] | 2115 | def _validate_qrange(self, qmin_ctrl, qmax_ctrl): |
---|
| 2116 | """ |
---|
| 2117 | Verify that the Q range controls have valid values |
---|
| 2118 | and that Qmin < Qmax. |
---|
[5ce7f17] | 2119 | |
---|
[f0d720b] | 2120 | :param qmin_ctrl: text control for Qmin |
---|
| 2121 | :param qmax_ctrl: text control for Qmax |
---|
[5ce7f17] | 2122 | |
---|
[f0d720b] | 2123 | :return: True is the Q range is value, False otherwise |
---|
[5ce7f17] | 2124 | |
---|
[f0d720b] | 2125 | """ |
---|
| 2126 | qmin_validity = check_float(qmin_ctrl) |
---|
| 2127 | qmax_validity = check_float(qmax_ctrl) |
---|
| 2128 | if not (qmin_validity and qmax_validity): |
---|
| 2129 | return False |
---|
| 2130 | else: |
---|
| 2131 | qmin = float(qmin_ctrl.GetValue()) |
---|
| 2132 | qmax = float(qmax_ctrl.GetValue()) |
---|
| 2133 | if qmin < qmax: |
---|
[c8e1996] | 2134 | # Make sure to set both colours white. |
---|
[f0d720b] | 2135 | qmin_ctrl.SetBackgroundColour(wx.WHITE) |
---|
| 2136 | qmin_ctrl.Refresh() |
---|
| 2137 | qmax_ctrl.SetBackgroundColour(wx.WHITE) |
---|
| 2138 | qmax_ctrl.Refresh() |
---|
| 2139 | else: |
---|
| 2140 | qmin_ctrl.SetBackgroundColour("pink") |
---|
| 2141 | qmin_ctrl.Refresh() |
---|
| 2142 | qmax_ctrl.SetBackgroundColour("pink") |
---|
| 2143 | qmax_ctrl.Refresh() |
---|
| 2144 | msg = "Invalid Q range: Q min must be smaller than Q max" |
---|
| 2145 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
| 2146 | return False |
---|
| 2147 | return True |
---|
[5ce7f17] | 2148 | |
---|
[f0d720b] | 2149 | def _validate_Npts(self): |
---|
| 2150 | """ |
---|
| 2151 | Validate the number of points for fitting is more than 10 points. |
---|
| 2152 | If valid, setvalues Npts_fit otherwise post msg. |
---|
| 2153 | """ |
---|
[c8e1996] | 2154 | # default flag |
---|
[f0d720b] | 2155 | flag = True |
---|
| 2156 | # Theory |
---|
[c8e1996] | 2157 | if self.data is None and self.enable2D: |
---|
[f0d720b] | 2158 | return flag |
---|
| 2159 | for data in self.data_list: |
---|
| 2160 | # q value from qx and qy |
---|
[9a5097c] | 2161 | radius = np.sqrt(data.qx_data * data.qx_data + |
---|
[f0d720b] | 2162 | data.qy_data * data.qy_data) |
---|
[c8e1996] | 2163 | # get unmasked index |
---|
[f0d720b] | 2164 | index_data = (float(self.qmin.GetValue()) <= radius) & \ |
---|
[c8e1996] | 2165 | (radius <= float(self.qmax.GetValue())) |
---|
[f0d720b] | 2166 | index_data = (index_data) & (data.mask) |
---|
[9a5097c] | 2167 | index_data = (index_data) & (np.isfinite(data.data)) |
---|
[f0d720b] | 2168 | |
---|
| 2169 | if len(index_data[index_data]) < 10: |
---|
| 2170 | # change the color pink. |
---|
| 2171 | self.qmin.SetBackgroundColour("pink") |
---|
| 2172 | self.qmin.Refresh() |
---|
| 2173 | self.qmax.SetBackgroundColour("pink") |
---|
| 2174 | self.qmax.Refresh() |
---|
| 2175 | msg = "Data Error: " |
---|
| 2176 | msg += "Too few points in %s." % data.name |
---|
| 2177 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
| 2178 | self.fitrange = False |
---|
| 2179 | flag = False |
---|
| 2180 | else: |
---|
[d7d0182] | 2181 | self.Npts_fit.SetValue(str(len(index_data[index_data]))) |
---|
[f0d720b] | 2182 | self.fitrange = True |
---|
[5ce7f17] | 2183 | |
---|
[f0d720b] | 2184 | return flag |
---|
| 2185 | |
---|
| 2186 | def _validate_Npts_1D(self): |
---|
| 2187 | """ |
---|
| 2188 | Validate the number of points for fitting is more than 5 points. |
---|
| 2189 | If valid, setvalues Npts_fit otherwise post msg. |
---|
| 2190 | """ |
---|
[c8e1996] | 2191 | # default flag |
---|
[f0d720b] | 2192 | flag = True |
---|
| 2193 | # Theory |
---|
[c8e1996] | 2194 | if self.data is None: |
---|
[f0d720b] | 2195 | return flag |
---|
| 2196 | for data in self.data_list: |
---|
| 2197 | # q value from qx and qy |
---|
| 2198 | radius = data.x |
---|
[c8e1996] | 2199 | # get unmasked index |
---|
[f0d720b] | 2200 | index_data = (float(self.qmin.GetValue()) <= radius) & \ |
---|
[c8e1996] | 2201 | (radius <= float(self.qmax.GetValue())) |
---|
[9a5097c] | 2202 | index_data = (index_data) & (np.isfinite(data.y)) |
---|
[f0d720b] | 2203 | |
---|
| 2204 | if len(index_data[index_data]) < 5: |
---|
| 2205 | # change the color pink. |
---|
| 2206 | self.qmin.SetBackgroundColour("pink") |
---|
| 2207 | self.qmin.Refresh() |
---|
| 2208 | self.qmax.SetBackgroundColour("pink") |
---|
| 2209 | self.qmax.Refresh() |
---|
| 2210 | msg = "Data Error: " |
---|
| 2211 | msg += "Too few points in %s." % data.name |
---|
| 2212 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
| 2213 | self.fitrange = False |
---|
| 2214 | flag = False |
---|
| 2215 | else: |
---|
[505706a] | 2216 | self.Npts_fit.SetValue(str(len(index_data[index_data]))) |
---|
[f0d720b] | 2217 | self.fitrange = True |
---|
[5ce7f17] | 2218 | |
---|
[f0d720b] | 2219 | return flag |
---|
[5ce7f17] | 2220 | |
---|
[ee4b3cb] | 2221 | def _check_value_enter(self, list): |
---|
[f0d720b] | 2222 | """ |
---|
| 2223 | :param list: model parameter and panel info |
---|
| 2224 | :Note: each item of the list should be as follow: |
---|
| 2225 | item=[check button state, parameter's name, |
---|
| 2226 | paramater's value, string="+/-", |
---|
| 2227 | parameter's error of fit, |
---|
| 2228 | parameter's minimum value, |
---|
[cb4ef58] | 2229 | parameter's maximum value , |
---|
[f0d720b] | 2230 | parameter's units] |
---|
[ee4b3cb] | 2231 | |
---|
| 2232 | Returns True if the model parameters have changed. |
---|
[f0d720b] | 2233 | """ |
---|
[ee4b3cb] | 2234 | is_modified = False |
---|
[f0d720b] | 2235 | for item in list: |
---|
[c8e1996] | 2236 | # skip angle parameters for 1D |
---|
[ee4b3cb] | 2237 | if not self.enable2D and item in self.orientation_params: |
---|
| 2238 | continue |
---|
[5ce7f17] | 2239 | |
---|
[a0373d5] | 2240 | value_ctrl = item[2] |
---|
| 2241 | if not value_ctrl.IsEnabled(): |
---|
| 2242 | # ArrayDispersion disables PD, Min, Max, Npts, Nsigs |
---|
[ee4b3cb] | 2243 | continue |
---|
| 2244 | |
---|
[a0373d5] | 2245 | name = item[1] |
---|
[ee4b3cb] | 2246 | value_str = value_ctrl.GetValue().strip() |
---|
[a0373d5] | 2247 | if name.endswith(".npts"): |
---|
| 2248 | validity = check_int(value_ctrl) |
---|
| 2249 | if not validity: |
---|
| 2250 | continue |
---|
| 2251 | value = int(value_str) |
---|
| 2252 | |
---|
| 2253 | elif name.endswith(".nsigmas"): |
---|
| 2254 | validity = check_float(value_ctrl) |
---|
| 2255 | if not validity: |
---|
| 2256 | continue |
---|
| 2257 | value = float(value_str) |
---|
| 2258 | |
---|
| 2259 | else: # value or polydispersity |
---|
| 2260 | |
---|
| 2261 | # Check that min, max and value are floats |
---|
| 2262 | min_ctrl, max_ctrl = item[5], item[6] |
---|
| 2263 | min_str = min_ctrl.GetValue().strip() |
---|
| 2264 | max_str = max_ctrl.GetValue().strip() |
---|
| 2265 | validity = check_float(value_ctrl) |
---|
| 2266 | if min_str != "": |
---|
| 2267 | validity = validity and check_float(min_ctrl) |
---|
| 2268 | if max_str != "": |
---|
| 2269 | validity = validity and check_float(max_ctrl) |
---|
| 2270 | if not validity: |
---|
| 2271 | continue |
---|
| 2272 | |
---|
| 2273 | # Check that min is less than max |
---|
[9a5097c] | 2274 | low = -np.inf if min_str == "" else float(min_str) |
---|
| 2275 | high = np.inf if max_str == "" else float(max_str) |
---|
[a0373d5] | 2276 | if high < low: |
---|
| 2277 | min_ctrl.SetBackgroundColour("pink") |
---|
| 2278 | min_ctrl.Refresh() |
---|
| 2279 | max_ctrl.SetBackgroundColour("pink") |
---|
| 2280 | max_ctrl.Refresh() |
---|
[c8e1996] | 2281 | # msg = "Invalid fit range for %s: min must be smaller |
---|
| 2282 | # than max"%name |
---|
| 2283 | # wx.PostEvent(self._manager.parent, |
---|
| 2284 | # StatusEvent(status=msg)) |
---|
[a0373d5] | 2285 | continue |
---|
| 2286 | |
---|
| 2287 | # Force value between min and max |
---|
| 2288 | value = float(value_str) |
---|
| 2289 | if value < low: |
---|
| 2290 | value = low |
---|
| 2291 | value_ctrl.SetValue(format_number(value)) |
---|
| 2292 | elif value > high: |
---|
| 2293 | value = high |
---|
| 2294 | value_ctrl.SetValue(format_number(value)) |
---|
| 2295 | |
---|
| 2296 | if name not in self.model.details.keys(): |
---|
| 2297 | self.model.details[name] = ["", None, None] |
---|
| 2298 | old_low, old_high = self.model.details[name][1:3] |
---|
| 2299 | if old_low != low or old_high != high: |
---|
| 2300 | # The configuration has changed but it won't change the |
---|
| 2301 | # computed curve so no need to set is_modified to True |
---|
[c8e1996] | 2302 | # is_modified = True |
---|
[a0373d5] | 2303 | self.model.details[name][1:3] = low, high |
---|
[ee4b3cb] | 2304 | |
---|
| 2305 | # Update value in model if it has changed |
---|
| 2306 | if value != self.model.getParam(name): |
---|
| 2307 | self.model.setParam(name, value) |
---|
| 2308 | is_modified = True |
---|
[5ce7f17] | 2309 | |
---|
[f0d720b] | 2310 | return is_modified |
---|
[5ce7f17] | 2311 | |
---|
[f0d720b] | 2312 | def _set_dipers_Param(self, event): |
---|
| 2313 | """ |
---|
| 2314 | respond to self.enable_disp and self.disable_disp radio box. |
---|
| 2315 | The dispersity object is reset inside the model into Gaussian. |
---|
| 2316 | When the user select yes , this method display a combo box for |
---|
| 2317 | more selection when the user selects No,the combo box disappears. |
---|
| 2318 | Redraw the model with the default dispersity (Gaussian) |
---|
| 2319 | """ |
---|
[c8e1996] | 2320 | # On selction if no model exists. |
---|
| 2321 | if self.model is None: |
---|
[f0d720b] | 2322 | self.disable_disp.SetValue(True) |
---|
| 2323 | msg = "Please select a Model first..." |
---|
| 2324 | wx.MessageBox(msg, 'Info') |
---|
| 2325 | wx.PostEvent(self._manager.parent, |
---|
| 2326 | StatusEvent(status="Polydispersion: %s" % msg)) |
---|
| 2327 | return |
---|
| 2328 | |
---|
| 2329 | self._reset_dispersity() |
---|
[5ce7f17] | 2330 | |
---|
[c8e1996] | 2331 | if self.model is None: |
---|
[f0d720b] | 2332 | self.model_disp.Hide() |
---|
| 2333 | self.sizer4_4.Clear(True) |
---|
| 2334 | return |
---|
| 2335 | |
---|
| 2336 | if self.enable_disp.GetValue(): |
---|
[c8e1996] | 2337 | # layout for model containing no dispersity parameters |
---|
[5ce7f17] | 2338 | |
---|
[f0d720b] | 2339 | self.disp_list = self.model.getDispParamList() |
---|
[5ce7f17] | 2340 | |
---|
[f0d720b] | 2341 | if len(self.disp_list) == 0 and len(self.disp_cb_dict) == 0: |
---|
| 2342 | self._layout_sizer_noDipers() |
---|
| 2343 | else: |
---|
[c8e1996] | 2344 | # set gaussian sizer |
---|
[f0d720b] | 2345 | self._on_select_Disp(event=None) |
---|
| 2346 | else: |
---|
| 2347 | self.sizer4_4.Clear(True) |
---|
[5ce7f17] | 2348 | |
---|
[c8e1996] | 2349 | # post state to fit panel |
---|
[f0d720b] | 2350 | self.save_current_state() |
---|
[c8e1996] | 2351 | if event is not None: |
---|
[f0d720b] | 2352 | event = PageInfoEvent(page=self) |
---|
| 2353 | wx.PostEvent(self.parent, event) |
---|
[c8e1996] | 2354 | # draw the model with the current dispersity |
---|
[47f2b5d] | 2355 | |
---|
[c8e1996] | 2356 | # Wojtek P, Oct 8, 2016: Calling draw_model seems to be unessecary. |
---|
| 2357 | # By comenting it we save an extra Iq calculation |
---|
| 2358 | # self._draw_model() |
---|
[47f2b5d] | 2359 | |
---|
[c8e1996] | 2360 | # Need to use FitInside again here to replace the next four lines. |
---|
| 2361 | # Otherwised polydispersity off does not resize the scrollwindow. |
---|
| 2362 | # PDB Nov 28, 2015 |
---|
[1c2bf90] | 2363 | self.FitInside() |
---|
| 2364 | # self.sizer4_4.Layout() |
---|
| 2365 | # self.sizer5.Layout() |
---|
| 2366 | # self.Layout() |
---|
| 2367 | # self.Refresh() |
---|
[5ce7f17] | 2368 | |
---|
[f0d720b] | 2369 | def _layout_sizer_noDipers(self): |
---|
| 2370 | """ |
---|
| 2371 | Draw a sizer with no dispersity info |
---|
| 2372 | """ |
---|
| 2373 | ix = 0 |
---|
| 2374 | iy = 1 |
---|
| 2375 | self.fittable_param = [] |
---|
| 2376 | self.fixed_param = [] |
---|
| 2377 | self.orientation_params_disp = [] |
---|
[5ce7f17] | 2378 | |
---|
[f0d720b] | 2379 | self.sizer4_4.Clear(True) |
---|
| 2380 | text = "No polydispersity available for this model" |
---|
[6f16e25] | 2381 | model_disp = wx.StaticText(self, wx.ID_ANY, text) |
---|
[f0d720b] | 2382 | self.sizer4_4.Add(model_disp, (iy, ix), (1, 1), |
---|
| 2383 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 10) |
---|
| 2384 | self.sizer4_4.Layout() |
---|
| 2385 | self.sizer4.Layout() |
---|
[5ce7f17] | 2386 | |
---|
[f0d720b] | 2387 | def _reset_dispersity(self): |
---|
| 2388 | """ |
---|
| 2389 | put gaussian dispersity into current model |
---|
| 2390 | """ |
---|
| 2391 | if len(self.param_toFit) > 0: |
---|
| 2392 | for item in self.fittable_param: |
---|
| 2393 | if item in self.param_toFit: |
---|
| 2394 | self.param_toFit.remove(item) |
---|
| 2395 | |
---|
| 2396 | for item in self.orientation_params_disp: |
---|
| 2397 | if item in self.param_toFit: |
---|
| 2398 | self.param_toFit.remove(item) |
---|
[5ce7f17] | 2399 | |
---|
[f0d720b] | 2400 | self.fittable_param = [] |
---|
| 2401 | self.fixed_param = [] |
---|
| 2402 | self.orientation_params_disp = [] |
---|
| 2403 | self.values = {} |
---|
| 2404 | self.weights = {} |
---|
[bac3988] | 2405 | |
---|
[c8e1996] | 2406 | # from sas.models.dispersion_models import GaussianDispersion |
---|
[f0d720b] | 2407 | from sasmodels.weights import GaussianDispersion |
---|
| 2408 | if len(self.disp_cb_dict) == 0: |
---|
| 2409 | self.save_current_state() |
---|
| 2410 | self.sizer4_4.Clear(True) |
---|
| 2411 | self.Layout() |
---|
| 2412 | return |
---|
| 2413 | if (len(self.disp_cb_dict) > 0): |
---|
| 2414 | for p in self.disp_cb_dict: |
---|
| 2415 | # The parameter was un-selected. |
---|
| 2416 | # Go back to Gaussian model (with 0 pts) |
---|
| 2417 | disp_model = GaussianDispersion() |
---|
[5ce7f17] | 2418 | |
---|
[f0d720b] | 2419 | self._disp_obj_dict[p] = disp_model |
---|
| 2420 | # Set the new model as the dispersion object |
---|
| 2421 | # for the selected parameter |
---|
| 2422 | try: |
---|
| 2423 | self.model.set_dispersion(p, disp_model) |
---|
[7673ecd] | 2424 | except Exception: |
---|
[c155a16] | 2425 | logger.error(traceback.format_exc()) |
---|
[f0d720b] | 2426 | |
---|
[c8e1996] | 2427 | # save state into |
---|
[f0d720b] | 2428 | self.save_current_state() |
---|
| 2429 | self.Layout() |
---|
| 2430 | self.Refresh() |
---|
[5ce7f17] | 2431 | |
---|
[f0d720b] | 2432 | def _on_select_Disp(self, event): |
---|
| 2433 | """ |
---|
| 2434 | allow selecting different dispersion |
---|
| 2435 | self.disp_list should change type later .now only gaussian |
---|
| 2436 | """ |
---|
| 2437 | self._set_sizer_dispersion() |
---|
| 2438 | |
---|
[c8e1996] | 2439 | # Redraw the model |
---|
[cc0f4a8] | 2440 | # Wojtek P. Nov 7, 2016: Redrawing seems to be unnecessary here |
---|
| 2441 | # self._draw_model() |
---|
[c8e1996] | 2442 | # self._undo.Enable(True) |
---|
[f0d720b] | 2443 | event = PageInfoEvent(page=self) |
---|
| 2444 | wx.PostEvent(self.parent, event) |
---|
[5ce7f17] | 2445 | |
---|
[f0d720b] | 2446 | self.sizer4_4.Layout() |
---|
| 2447 | self.sizer4.Layout() |
---|
| 2448 | self.SetupScrolling() |
---|
[5ce7f17] | 2449 | |
---|
[f0d720b] | 2450 | def _on_disp_func(self, event=None): |
---|
| 2451 | """ |
---|
| 2452 | Select a distribution function for the polydispersion |
---|
[5ce7f17] | 2453 | |
---|
[f0d720b] | 2454 | :Param event: ComboBox event |
---|
| 2455 | """ |
---|
| 2456 | # get ready for new event |
---|
[c8e1996] | 2457 | if event is not None: |
---|
[f0d720b] | 2458 | event.Skip() |
---|
| 2459 | # Get event object |
---|
| 2460 | disp_box = event.GetEventObject() |
---|
| 2461 | |
---|
| 2462 | # Try to select a Distr. function |
---|
| 2463 | try: |
---|
| 2464 | disp_box.SetBackgroundColour("white") |
---|
| 2465 | selection = disp_box.GetCurrentSelection() |
---|
| 2466 | param_name = disp_box.Name.split('.')[0] |
---|
| 2467 | disp_name = disp_box.GetValue() |
---|
| 2468 | dispersity = disp_box.GetClientData(selection) |
---|
[5ce7f17] | 2469 | |
---|
[c8e1996] | 2470 | # disp_model = GaussianDispersion() |
---|
[f0d720b] | 2471 | disp_model = dispersity() |
---|
| 2472 | # Get param names to reset the values of the param |
---|
| 2473 | name1 = param_name + ".width" |
---|
| 2474 | name2 = param_name + ".npts" |
---|
| 2475 | name3 = param_name + ".nsigmas" |
---|
| 2476 | # Check Disp. function whether or not it is 'array' |
---|
| 2477 | if disp_name.lower() == "array": |
---|
| 2478 | value2 = "" |
---|
| 2479 | value3 = "" |
---|
| 2480 | value1 = self._set_array_disp(name=name1, disp=disp_model) |
---|
| 2481 | else: |
---|
| 2482 | self._del_array_values(name1) |
---|
[c8e1996] | 2483 | # self._reset_array_disp(param_name) |
---|
[f0d720b] | 2484 | self._disp_obj_dict[name1] = disp_model |
---|
| 2485 | self.model.set_dispersion(param_name, disp_model) |
---|
[6c382da] | 2486 | self.state._disp_obj_dict[name1] = disp_model.type |
---|
[5ce7f17] | 2487 | |
---|
[f0d720b] | 2488 | value1 = str(format_number(self.model.getParam(name1), True)) |
---|
| 2489 | value2 = str(format_number(self.model.getParam(name2))) |
---|
| 2490 | value3 = str(format_number(self.model.getParam(name3))) |
---|
| 2491 | # Reset fittable polydispersin parameter value |
---|
| 2492 | for item in self.fittable_param: |
---|
| 2493 | if item[1] == name1: |
---|
| 2494 | item[2].SetValue(value1) |
---|
| 2495 | item[5].SetValue("") |
---|
| 2496 | item[6].SetValue("") |
---|
| 2497 | # Disable for array |
---|
| 2498 | if disp_name.lower() == "array": |
---|
| 2499 | item[0].SetValue(False) |
---|
| 2500 | item[0].Disable() |
---|
| 2501 | item[2].Disable() |
---|
| 2502 | item[3].Show(False) |
---|
| 2503 | item[4].Show(False) |
---|
| 2504 | item[5].Disable() |
---|
| 2505 | item[6].Disable() |
---|
| 2506 | else: |
---|
| 2507 | item[0].Enable() |
---|
| 2508 | item[2].Enable() |
---|
[6c382da] | 2509 | item[3].Show(True) |
---|
| 2510 | item[4].Show(True) |
---|
[f0d720b] | 2511 | item[5].Enable() |
---|
| 2512 | item[6].Enable() |
---|
| 2513 | break |
---|
| 2514 | # Reset fixed polydispersion params |
---|
| 2515 | for item in self.fixed_param: |
---|
| 2516 | if item[1] == name2: |
---|
[5ce7f17] | 2517 | item[2].SetValue(value2) |
---|
[f0d720b] | 2518 | # Disable Npts for array |
---|
| 2519 | if disp_name.lower() == "array": |
---|
| 2520 | item[2].Disable() |
---|
| 2521 | else: |
---|
| 2522 | item[2].Enable() |
---|
| 2523 | if item[1] == name3: |
---|
| 2524 | item[2].SetValue(value3) |
---|
| 2525 | # Disable Nsigs for array |
---|
| 2526 | if disp_name.lower() == "array": |
---|
| 2527 | item[2].Disable() |
---|
| 2528 | else: |
---|
| 2529 | item[2].Enable() |
---|
[5ce7f17] | 2530 | |
---|
[4c3be25] | 2531 | # Make sure the check box updated |
---|
| 2532 | self.get_all_checked_params() |
---|
[f0d720b] | 2533 | |
---|
| 2534 | # update params |
---|
| 2535 | self._update_paramv_on_fit() |
---|
| 2536 | # draw |
---|
| 2537 | self._draw_model() |
---|
| 2538 | self.Refresh() |
---|
[6ed67db] | 2539 | except Exception: |
---|
[c155a16] | 2540 | logger.error(traceback.format_exc()) |
---|
[f0d720b] | 2541 | # Error msg |
---|
| 2542 | msg = "Error occurred:" |
---|
| 2543 | msg += " Could not select the distribution function..." |
---|
| 2544 | msg += " Please select another distribution function." |
---|
| 2545 | disp_box.SetBackgroundColour("pink") |
---|
| 2546 | # Focus on Fit button so that users can see the pinky box |
---|
| 2547 | self.btFit.SetFocus() |
---|
| 2548 | wx.PostEvent(self._manager.parent, |
---|
| 2549 | StatusEvent(status=msg, info="error")) |
---|
[5ce7f17] | 2550 | |
---|
[f0d720b] | 2551 | def _set_array_disp(self, name=None, disp=None): |
---|
| 2552 | """ |
---|
| 2553 | Set array dispersion |
---|
[5ce7f17] | 2554 | |
---|
[f0d720b] | 2555 | :param name: name of the parameter for the dispersion to be set |
---|
| 2556 | :param disp: the polydisperion object |
---|
| 2557 | """ |
---|
| 2558 | # The user wants this parameter to be averaged. |
---|
| 2559 | # Pop up the file selection dialog. |
---|
| 2560 | path = self._selectDlg() |
---|
| 2561 | # Array data |
---|
| 2562 | values = [] |
---|
| 2563 | weights = [] |
---|
| 2564 | # If nothing was selected, just return |
---|
| 2565 | if path is None: |
---|
| 2566 | self.disp_cb_dict[name].SetValue(False) |
---|
[c8e1996] | 2567 | # self.noDisper_rbox.SetValue(True) |
---|
[f0d720b] | 2568 | return |
---|
| 2569 | self._default_save_location = os.path.dirname(path) |
---|
[c8e1996] | 2570 | if self._manager is not None: |
---|
[5ce7f17] | 2571 | self._manager.parent._default_save_location = \ |
---|
[f0d720b] | 2572 | self._default_save_location |
---|
| 2573 | |
---|
| 2574 | basename = os.path.basename(path) |
---|
| 2575 | values, weights = self.read_file(path) |
---|
[5ce7f17] | 2576 | |
---|
[f0d720b] | 2577 | # If any of the two arrays is empty, notify the user that we won't |
---|
| 2578 | # proceed |
---|
| 2579 | if len(self.param_toFit) > 0: |
---|
| 2580 | if name in self.param_toFit: |
---|
| 2581 | self.param_toFit.remove(name) |
---|
| 2582 | |
---|
| 2583 | # Tell the user that we are about to apply the distribution |
---|
| 2584 | msg = "Applying loaded %s distribution: %s" % (name, path) |
---|
| 2585 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
| 2586 | self._set_array_disp_model(name=name, disp=disp, |
---|
[c8e1996] | 2587 | values=values, weights=weights) |
---|
[f0d720b] | 2588 | return basename |
---|
[5ce7f17] | 2589 | |
---|
[f0d720b] | 2590 | def _set_array_disp_model(self, name=None, disp=None, |
---|
| 2591 | values=[], weights=[]): |
---|
| 2592 | """ |
---|
| 2593 | Set array dispersion model |
---|
[5ce7f17] | 2594 | |
---|
[f0d720b] | 2595 | :param name: name of the parameter for the dispersion to be set |
---|
| 2596 | :param disp: the polydisperion object |
---|
| 2597 | """ |
---|
| 2598 | disp.set_weights(values, weights) |
---|
| 2599 | self._disp_obj_dict[name] = disp |
---|
| 2600 | self.model.set_dispersion(name.split('.')[0], disp) |
---|
[6c382da] | 2601 | self.state._disp_obj_dict[name] = disp.type |
---|
[f0d720b] | 2602 | self.values[name] = values |
---|
| 2603 | self.weights[name] = weights |
---|
| 2604 | # Store the object to make it persist outside the |
---|
| 2605 | # scope of this method |
---|
[c8e1996] | 2606 | # TODO: refactor model to clean this up? |
---|
[f0d720b] | 2607 | self.state.values = {} |
---|
| 2608 | self.state.weights = {} |
---|
| 2609 | self.state.values = copy.deepcopy(self.values) |
---|
| 2610 | self.state.weights = copy.deepcopy(self.weights) |
---|
| 2611 | |
---|
| 2612 | # Set the new model as the dispersion object for the |
---|
[c8e1996] | 2613 | # selected parameter |
---|
| 2614 | # self.model.set_dispersion(p, disp_model) |
---|
[f0d720b] | 2615 | # Store a reference to the weights in the model object |
---|
[c8e1996] | 2616 | # so that |
---|
[f0d720b] | 2617 | # it's not lost when we use the model within another thread. |
---|
| 2618 | self.state.model = self.model.clone() |
---|
| 2619 | self.model._persistency_dict[name.split('.')[0]] = \ |
---|
[c8e1996] | 2620 | [values, weights] |
---|
[f0d720b] | 2621 | self.state.model._persistency_dict[name.split('.')[0]] = \ |
---|
[c8e1996] | 2622 | [values, weights] |
---|
[5ce7f17] | 2623 | |
---|
[f0d720b] | 2624 | def _del_array_values(self, name=None): |
---|
| 2625 | """ |
---|
| 2626 | Reset array dispersion |
---|
[5ce7f17] | 2627 | |
---|
[f0d720b] | 2628 | :param name: name of the parameter for the dispersion to be set |
---|
| 2629 | """ |
---|
| 2630 | # Try to delete values and weight of the names array dic if exists |
---|
| 2631 | try: |
---|
[6ed67db] | 2632 | if name in self.values: |
---|
| 2633 | del self.values[name] |
---|
| 2634 | del self.weights[name] |
---|
| 2635 | # delete all other dic |
---|
| 2636 | del self.state.values[name] |
---|
| 2637 | del self.state.weights[name] |
---|
| 2638 | del self.model._persistency_dict[name.split('.')[0]] |
---|
| 2639 | del self.state.model._persistency_dict[name.split('.')[0]] |
---|
[7673ecd] | 2640 | except Exception: |
---|
[c155a16] | 2641 | logger.error(traceback.format_exc()) |
---|
[5ce7f17] | 2642 | |
---|
[f0d720b] | 2643 | def _lay_out(self): |
---|
| 2644 | """ |
---|
| 2645 | returns self.Layout |
---|
[5ce7f17] | 2646 | |
---|
[f0d720b] | 2647 | :Note: Mac seems to like this better when self. |
---|
| 2648 | Layout is called after fitting. |
---|
| 2649 | """ |
---|
| 2650 | self.Layout() |
---|
| 2651 | return |
---|
[5ce7f17] | 2652 | |
---|
[f0d720b] | 2653 | def _find_polyfunc_selection(self, disp_func=None): |
---|
| 2654 | """ |
---|
| 2655 | FInd Comboox selection from disp_func |
---|
[5ce7f17] | 2656 | |
---|
[f0d720b] | 2657 | :param disp_function: dispersion distr. function |
---|
| 2658 | """ |
---|
| 2659 | # Find the selection |
---|
[a0373d5] | 2660 | if disp_func is not None: |
---|
| 2661 | try: |
---|
| 2662 | return POLYDISPERSITY_MODELS.values().index(disp_func.__class__) |
---|
| 2663 | except ValueError: |
---|
| 2664 | pass # Fall through to default class |
---|
| 2665 | return POLYDISPERSITY_MODELS.keys().index('gaussian') |
---|
[5ce7f17] | 2666 | |
---|
[f0d720b] | 2667 | def on_reset_clicked(self, event): |
---|
| 2668 | """ |
---|
| 2669 | On 'Reset' button for Q range clicked |
---|
| 2670 | """ |
---|
| 2671 | flag = True |
---|
[c8e1996] | 2672 | # For 3 different cases: Data2D, Data1D, and theory |
---|
| 2673 | if self.model is None: |
---|
[f0d720b] | 2674 | msg = "Please select a model first..." |
---|
| 2675 | wx.MessageBox(msg, 'Info') |
---|
| 2676 | flag = False |
---|
| 2677 | return |
---|
[5ce7f17] | 2678 | |
---|
[f0d720b] | 2679 | elif self.data.__class__.__name__ == "Data2D": |
---|
| 2680 | data_min = 0 |
---|
| 2681 | x = max(math.fabs(self.data.xmin), math.fabs(self.data.xmax)) |
---|
| 2682 | y = max(math.fabs(self.data.ymin), math.fabs(self.data.ymax)) |
---|
| 2683 | self.qmin_x = data_min |
---|
[5ce7f17] | 2684 | self.qmax_x = math.sqrt(x * x + y * y) |
---|
[9a5097c] | 2685 | # self.data.mask = np.ones(len(self.data.data),dtype=bool) |
---|
[f0d720b] | 2686 | # check smearing |
---|
| 2687 | if not self.disable_smearer.GetValue(): |
---|
[c8e1996] | 2688 | # set smearing value whether or |
---|
[f0d720b] | 2689 | # not the data contain the smearing info |
---|
| 2690 | if self.pinhole_smearer.GetValue(): |
---|
| 2691 | flag = self.update_pinhole_smear() |
---|
| 2692 | else: |
---|
| 2693 | flag = True |
---|
[5ce7f17] | 2694 | |
---|
[c8e1996] | 2695 | elif self.data is None: |
---|
[f0d720b] | 2696 | self.qmin_x = _QMIN_DEFAULT |
---|
| 2697 | self.qmax_x = _QMAX_DEFAULT |
---|
| 2698 | self.num_points = _NPTS_DEFAULT |
---|
| 2699 | self.state.npts = self.num_points |
---|
[5ce7f17] | 2700 | |
---|
[f0d720b] | 2701 | elif self.data.__class__.__name__ != "Data2D": |
---|
| 2702 | self.qmin_x = min(self.data.x) |
---|
| 2703 | self.qmax_x = max(self.data.x) |
---|
| 2704 | # check smearing |
---|
| 2705 | if not self.disable_smearer.GetValue(): |
---|
[c8e1996] | 2706 | # set smearing value whether or |
---|
[f0d720b] | 2707 | # not the data contain the smearing info |
---|
| 2708 | if self.slit_smearer.GetValue(): |
---|
| 2709 | flag = self.update_slit_smear() |
---|
| 2710 | elif self.pinhole_smearer.GetValue(): |
---|
| 2711 | flag = self.update_pinhole_smear() |
---|
| 2712 | else: |
---|
| 2713 | flag = True |
---|
| 2714 | else: |
---|
| 2715 | flag = False |
---|
[5ce7f17] | 2716 | |
---|
[c8e1996] | 2717 | if flag is False: |
---|
[f0d720b] | 2718 | msg = "Cannot Plot :Must enter a number!!! " |
---|
| 2719 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
| 2720 | else: |
---|
| 2721 | # set relative text ctrs. |
---|
| 2722 | self.qmin.SetValue(str(self.qmin_x)) |
---|
| 2723 | self.qmax.SetValue(str(self.qmax_x)) |
---|
| 2724 | self.show_npts2fit() |
---|
| 2725 | # At this point, some button and variables satatus (disabled?) |
---|
| 2726 | # should be checked such as color that should be reset to |
---|
| 2727 | # white in case that it was pink. |
---|
| 2728 | self._onparamEnter_helper() |
---|
| 2729 | |
---|
| 2730 | self.save_current_state() |
---|
| 2731 | self.state.qmin = self.qmin_x |
---|
| 2732 | self.state.qmax = self.qmax_x |
---|
[5ce7f17] | 2733 | |
---|
[c8e1996] | 2734 | # reset the q range values |
---|
[f0d720b] | 2735 | self._reset_plotting_range(self.state) |
---|
| 2736 | self._draw_model() |
---|
[5ce7f17] | 2737 | |
---|
[f0d720b] | 2738 | def select_log(self, event): |
---|
| 2739 | """ |
---|
| 2740 | Log checked to generate log spaced points for theory model |
---|
| 2741 | """ |
---|
| 2742 | |
---|
| 2743 | def get_images(self): |
---|
| 2744 | """ |
---|
| 2745 | Get the images of the plots corresponding this panel for report |
---|
[5ce7f17] | 2746 | |
---|
[f0d720b] | 2747 | : return graphs: list of figures |
---|
| 2748 | : Need Move to guiframe |
---|
| 2749 | """ |
---|
| 2750 | # set list of graphs |
---|
| 2751 | graphs = [] |
---|
| 2752 | canvases = [] |
---|
| 2753 | res_item = None |
---|
| 2754 | # call gui_manager |
---|
| 2755 | gui_manager = self._manager.parent |
---|
| 2756 | # loops through the panels [dic] |
---|
| 2757 | for _, item2 in gui_manager.plot_panels.iteritems(): |
---|
| 2758 | data_title = self.data.group_id |
---|
| 2759 | # try to get all plots belonging to this control panel |
---|
| 2760 | try: |
---|
| 2761 | g_id = item2.group_id |
---|
| 2762 | if g_id == data_title or \ |
---|
| 2763 | str(g_id).count("res" + str(self.graph_id)) or \ |
---|
| 2764 | str(g_id).count(str(self.uid)) > 0: |
---|
| 2765 | if str(g_id).count("res" + str(self.graph_id)) > 0: |
---|
| 2766 | res_item = [item2.figure, item2.canvas] |
---|
| 2767 | else: |
---|
| 2768 | # append to the list |
---|
| 2769 | graphs.append(item2.figure) |
---|
[5ce7f17] | 2770 | canvases.append(item2.canvas) |
---|
[7673ecd] | 2771 | except Exception: |
---|
[f0d720b] | 2772 | # Not for control panels |
---|
[c155a16] | 2773 | logger.error(traceback.format_exc()) |
---|
[f0d720b] | 2774 | # Make sure the resduals plot goes to the last |
---|
[c8e1996] | 2775 | if res_item is not None: |
---|
[f0d720b] | 2776 | graphs.append(res_item[0]) |
---|
| 2777 | canvases.append(res_item[1]) |
---|
| 2778 | # return the list of graphs |
---|
| 2779 | return graphs, canvases |
---|
| 2780 | |
---|
[5ce7f17] | 2781 | def on_function_help_clicked(self, event): |
---|
| 2782 | """ |
---|
| 2783 | Function called when 'Help' button is pressed next to model |
---|
| 2784 | of interest. This calls DocumentationWindow from |
---|
| 2785 | documentation_window.py. It will load the top level of the model |
---|
| 2786 | help documenation sphinx generated html if no model is presented. |
---|
| 2787 | If a model IS present then if documention for that model exists |
---|
| 2788 | it will load to that point otherwise again it will go to the top. |
---|
| 2789 | For Wx2.8 and below is used (i.e. non-released through installer) |
---|
| 2790 | a browser is loaded and the top of the model documentation only is |
---|
| 2791 | accessible because webbrowser module does not pass anything after |
---|
| 2792 | the # to the browser. |
---|
| 2793 | |
---|
[c8e1996] | 2794 | :param event: on Help Button pressed event |
---|
[5ce7f17] | 2795 | """ |
---|
| 2796 | |
---|
[c8e1996] | 2797 | if self.model is not None: |
---|
[3db44fb] | 2798 | name = self.formfactorbox.GetValue() |
---|
[c8e1996] | 2799 | _TreeLocation = 'user/models/' + name.lower()+'.html' |
---|
[6f16e25] | 2800 | _doc_viewer = DocumentationWindow(self, wx.ID_ANY, _TreeLocation, |
---|
[15fb4fa] | 2801 | "", name + " Help") |
---|
[5ce7f17] | 2802 | else: |
---|
[15fb4fa] | 2803 | _TreeLocation = 'user/index.html' |
---|
[6f16e25] | 2804 | _doc_viewer = DocumentationWindow(self, wx.ID_ANY, _TreeLocation, |
---|
| 2805 | "", "General Model Help") |
---|
[5ce7f17] | 2806 | |
---|
[f0d720b] | 2807 | def on_model_help_clicked(self, event): |
---|
| 2808 | """ |
---|
[5ce7f17] | 2809 | Function called when 'Description' button is pressed next to model |
---|
| 2810 | of interest. This calls the Description embedded in the model. This |
---|
| 2811 | should work with either Wx2.8 and lower or higher. If no model is |
---|
| 2812 | selected it will give the message that a model must be chosen first |
---|
| 2813 | in the box that would normally contain the description. If a badly |
---|
| 2814 | behaved model is encountered which has no description then it will |
---|
| 2815 | give the message that none is available. |
---|
| 2816 | |
---|
[c8e1996] | 2817 | :param event: on Description Button pressed event |
---|
[f0d720b] | 2818 | """ |
---|
[5ce7f17] | 2819 | |
---|
[c8e1996] | 2820 | if self.model is None: |
---|
[5ce7f17] | 2821 | name = 'index.html' |
---|
[f0d720b] | 2822 | else: |
---|
| 2823 | name = self.formfactorbox.GetValue() |
---|
[5ce7f17] | 2824 | |
---|
| 2825 | msg = 'Model description:\n' |
---|
| 2826 | info = "Info" |
---|
[c8e1996] | 2827 | if self.model is not None: |
---|
| 2828 | # frame.Destroy() |
---|
[5ce7f17] | 2829 | if str(self.model.description).rstrip().lstrip() == '': |
---|
| 2830 | msg += "Sorry, no information is available for this model." |
---|
[f0d720b] | 2831 | else: |
---|
[5ce7f17] | 2832 | msg += self.model.description + '\n' |
---|
| 2833 | wx.MessageBox(msg, info) |
---|
| 2834 | else: |
---|
| 2835 | msg += "You must select a model to get information on this" |
---|
| 2836 | wx.MessageBox(msg, info) |
---|
| 2837 | |
---|
[7116dffd] | 2838 | def _on_mag_angle_help(self, event): |
---|
[5ce7f17] | 2839 | """ |
---|
| 2840 | Bring up Magnetic Angle definition bmp image whenever the ? button |
---|
| 2841 | is clicked. Calls DocumentationWindow with the path of the location |
---|
| 2842 | within the documentation tree (after /doc/ ....". When using old |
---|
| 2843 | versions of Wx (i.e. before 2.9 and therefore not part of release |
---|
| 2844 | versions distributed via installer) it brings up an image viewer |
---|
| 2845 | box which allows the user to click through the rest of the images in |
---|
| 2846 | the directory. Not ideal but probably better than alternative which |
---|
| 2847 | would bring up the entire discussion of how magnetic models work? |
---|
| 2848 | Specially since it is not likely to be accessed. The normal release |
---|
| 2849 | versions bring up the normal image box. |
---|
| 2850 | |
---|
| 2851 | :param evt: Triggers on clicking ? in Magnetic Angles? box |
---|
| 2852 | """ |
---|
| 2853 | |
---|
| 2854 | _TreeLocation = "_images/M_angles_pic.bmp" |
---|
[6f16e25] | 2855 | _doc_viewer = DocumentationWindow(self, wx.ID_ANY, _TreeLocation, "", |
---|
[3db44fb] | 2856 | "Magnetic Angle Defintions") |
---|
[f0d720b] | 2857 | |
---|
[7116dffd] | 2858 | def _on_mag_help(self, event): |
---|
[f0d720b] | 2859 | """ |
---|
[7116dffd] | 2860 | Bring up Magnetic Angle definition bmp image whenever the ? button |
---|
| 2861 | is clicked. Calls DocumentationWindow with the path of the location |
---|
| 2862 | within the documentation tree (after /doc/ ....". When using old |
---|
| 2863 | versions of Wx (i.e. before 2.9 and therefore not part of release |
---|
| 2864 | versions distributed via installer) it brings up an image viewer |
---|
| 2865 | box which allows the user to click through the rest of the images in |
---|
| 2866 | the directory. Not ideal but probably better than alternative which |
---|
| 2867 | would bring up the entire discussion of how magnetic models work? |
---|
| 2868 | Specially since it is not likely to be accessed. The normal release |
---|
| 2869 | versions bring up the normal image box. |
---|
| 2870 | |
---|
| 2871 | :param evt: Triggers on clicking ? in Magnetic Angles? box |
---|
[f0d720b] | 2872 | """ |
---|
| 2873 | |
---|
[e4c897b] | 2874 | _TreeLocation = "user/magnetism.html" |
---|
[6f16e25] | 2875 | _doc_viewer = DocumentationWindow(self, wx.ID_ANY, _TreeLocation, "", |
---|
[7116dffd] | 2876 | "Polarized Beam/Magnetc Help") |
---|
[f0d720b] | 2877 | |
---|
[5ce7f17] | 2878 | def _on_mag_on(self, event): |
---|
[f0d720b] | 2879 | """ |
---|
| 2880 | Magnetic Parameters ON/OFF |
---|
| 2881 | """ |
---|
| 2882 | button = event.GetEventObject() |
---|
| 2883 | |
---|
| 2884 | if button.GetLabel().count('ON') > 0: |
---|
| 2885 | self.magnetic_on = True |
---|
| 2886 | button.SetLabel("Magnetic OFF") |
---|
| 2887 | m_value = 1.0e-06 |
---|
| 2888 | for key in self.model.magnetic_params: |
---|
| 2889 | if key.count('M0') > 0: |
---|
| 2890 | self.model.setParam(key, m_value) |
---|
| 2891 | m_value += 0.5e-06 |
---|
| 2892 | else: |
---|
| 2893 | self.magnetic_on = False |
---|
| 2894 | button.SetLabel("Magnetic ON") |
---|
| 2895 | for key in self.model.magnetic_params: |
---|
| 2896 | if key.count('M0') > 0: |
---|
[c8e1996] | 2897 | # reset mag value to zero fo safety |
---|
[f0d720b] | 2898 | self.model.setParam(key, 0.0) |
---|
[5ce7f17] | 2899 | |
---|
| 2900 | self.Show(False) |
---|
[f0d720b] | 2901 | self.set_model_param_sizer(self.model) |
---|
[c8e1996] | 2902 | # self._set_sizer_dispersion() |
---|
[f0d720b] | 2903 | self.state.magnetic_on = self.magnetic_on |
---|
| 2904 | self.SetupScrolling() |
---|
| 2905 | self.Show(True) |
---|
[5ce7f17] | 2906 | |
---|
[f0d720b] | 2907 | def on_pd_help_clicked(self, event): |
---|
| 2908 | """ |
---|
[5ce7f17] | 2909 | Bring up Polydispersity Documentation whenever the ? button is clicked. |
---|
| 2910 | Calls DocumentationWindow with the path of the location within the |
---|
| 2911 | documentation tree (after /doc/ ....". Note that when using old |
---|
| 2912 | versions of Wx (before 2.9) and thus not the release version of |
---|
| 2913 | istallers, the help comes up at the top level of the file as |
---|
| 2914 | webbrowser does not pass anything past the # to the browser when it is |
---|
| 2915 | running "file:///...." |
---|
| 2916 | |
---|
[c8e1996] | 2917 | :param event: Triggers on clicking ? in polydispersity box |
---|
[5ce7f17] | 2918 | """ |
---|
[f0d720b] | 2919 | |
---|
[eb04d59] | 2920 | _TreeLocation = "user/sasgui/perspectives/fitting/pd_help.html" |
---|
[7801df8] | 2921 | _PageAnchor = "" |
---|
[6f16e25] | 2922 | _doc_viewer = DocumentationWindow(self, wx.ID_ANY, _TreeLocation, |
---|
[3db44fb] | 2923 | _PageAnchor, "Polydispersity Help") |
---|
[5ce7f17] | 2924 | |
---|
[f0d720b] | 2925 | def on_left_down(self, event): |
---|
| 2926 | """ |
---|
| 2927 | Get key stroke event |
---|
| 2928 | """ |
---|
| 2929 | # Figuring out key combo: Cmd for copy, Alt for paste |
---|
| 2930 | if event.CmdDown() and event.ShiftDown(): |
---|
| 2931 | self.get_paste() |
---|
| 2932 | elif event.CmdDown(): |
---|
| 2933 | self.get_copy() |
---|
| 2934 | else: |
---|
| 2935 | event.Skip() |
---|
| 2936 | return |
---|
| 2937 | # make event free |
---|
| 2938 | event.Skip() |
---|
[5ce7f17] | 2939 | |
---|
[f0d720b] | 2940 | def get_copy(self): |
---|
| 2941 | """ |
---|
| 2942 | Get copy params to clipboard |
---|
| 2943 | """ |
---|
| 2944 | content = self.get_copy_params() |
---|
| 2945 | flag = self.set_clipboard(content) |
---|
| 2946 | self._copy_info(flag) |
---|
| 2947 | return flag |
---|
[5ce7f17] | 2948 | |
---|
[f0d720b] | 2949 | def get_copy_params(self): |
---|
| 2950 | """ |
---|
| 2951 | Get the string copies of the param names and values in the tap |
---|
| 2952 | """ |
---|
| 2953 | content = 'sasview_parameter_values:' |
---|
| 2954 | # Do it if params exist |
---|
[c8e1996] | 2955 | if self.parameters: |
---|
[5ce7f17] | 2956 | |
---|
[f0d720b] | 2957 | # go through the parameters |
---|
| 2958 | strings = self._get_copy_helper(self.parameters, |
---|
| 2959 | self.orientation_params) |
---|
| 2960 | content += strings |
---|
[5ce7f17] | 2961 | |
---|
[f0d720b] | 2962 | # go through the fittables |
---|
| 2963 | strings = self._get_copy_helper(self.fittable_param, |
---|
| 2964 | self.orientation_params_disp) |
---|
| 2965 | content += strings |
---|
| 2966 | |
---|
| 2967 | # go through the fixed params |
---|
| 2968 | strings = self._get_copy_helper(self.fixed_param, |
---|
| 2969 | self.orientation_params_disp) |
---|
| 2970 | content += strings |
---|
[5ce7f17] | 2971 | |
---|
[f0d720b] | 2972 | # go through the str params |
---|
| 2973 | strings = self._get_copy_helper(self.str_parameters, |
---|
| 2974 | self.orientation_params) |
---|
| 2975 | content += strings |
---|
| 2976 | return content |
---|
| 2977 | else: |
---|
| 2978 | return False |
---|
| 2979 | |
---|
| 2980 | def get_copy_excel(self): |
---|
| 2981 | """ |
---|
| 2982 | Get copy params to clipboard |
---|
| 2983 | """ |
---|
| 2984 | content = self.get_copy_params_excel() |
---|
| 2985 | flag = self.set_clipboard(content) |
---|
| 2986 | self._copy_info(flag) |
---|
| 2987 | return flag |
---|
| 2988 | |
---|
| 2989 | def get_copy_params_excel(self): |
---|
| 2990 | """ |
---|
| 2991 | Get the string copies of the param names and values in the tap |
---|
| 2992 | """ |
---|
| 2993 | content = '' |
---|
| 2994 | |
---|
| 2995 | crlf = chr(13) + chr(10) |
---|
| 2996 | tab = chr(9) |
---|
| 2997 | |
---|
| 2998 | # Do it if params exist |
---|
[c8e1996] | 2999 | if self.parameters: |
---|
[f0d720b] | 3000 | |
---|
| 3001 | for param in self.parameters: |
---|
[c8e1996] | 3002 | content += param[1] # parameter name |
---|
[f0d720b] | 3003 | content += tab |
---|
[5ce7f17] | 3004 | content += param[1] + "_err" |
---|
[f0d720b] | 3005 | content += tab |
---|
| 3006 | |
---|
| 3007 | content += crlf |
---|
| 3008 | |
---|
[c8e1996] | 3009 | # row of values and errors... |
---|
[f0d720b] | 3010 | for param in self.parameters: |
---|
[c8e1996] | 3011 | content += param[2].GetValue() # value |
---|
[5ce7f17] | 3012 | content += tab |
---|
[c8e1996] | 3013 | content += param[4].GetValue() # error |
---|
[5ce7f17] | 3014 | content += tab |
---|
[f0d720b] | 3015 | |
---|
| 3016 | return content |
---|
| 3017 | else: |
---|
| 3018 | return False |
---|
| 3019 | |
---|
| 3020 | def get_copy_latex(self): |
---|
| 3021 | """ |
---|
| 3022 | Get copy params to clipboard |
---|
| 3023 | """ |
---|
| 3024 | content = self.get_copy_params_latex() |
---|
| 3025 | flag = self.set_clipboard(content) |
---|
| 3026 | self._copy_info(flag) |
---|
| 3027 | return flag |
---|
| 3028 | |
---|
| 3029 | def get_copy_params_latex(self): |
---|
| 3030 | """ |
---|
| 3031 | Get the string copies of the param names and values in the tap |
---|
| 3032 | """ |
---|
| 3033 | content = '\\begin{table}' |
---|
| 3034 | content += '\\begin{tabular}[h]' |
---|
| 3035 | |
---|
| 3036 | crlf = chr(13) + chr(10) |
---|
| 3037 | tab = chr(9) |
---|
| 3038 | |
---|
| 3039 | # Do it if params exist |
---|
[c8e1996] | 3040 | if self.parameters: |
---|
[f0d720b] | 3041 | |
---|
| 3042 | content += '{|' |
---|
| 3043 | for param in self.parameters: |
---|
| 3044 | content += 'l|l|' |
---|
| 3045 | content += '}\hline' |
---|
| 3046 | content += crlf |
---|
| 3047 | |
---|
| 3048 | for index, param in enumerate(self.parameters): |
---|
[c8e1996] | 3049 | content += param[1].replace('_', '\_') # parameter name |
---|
[f0d720b] | 3050 | content += ' & ' |
---|
[5ce7f17] | 3051 | content += param[1].replace('_', '\_') + "\_err" |
---|
| 3052 | if index < len(self.parameters) - 1: |
---|
[f0d720b] | 3053 | content += ' & ' |
---|
| 3054 | content += '\\\\ \\hline' |
---|
| 3055 | content += crlf |
---|
| 3056 | |
---|
[c8e1996] | 3057 | # row of values and errors... |
---|
[f0d720b] | 3058 | for index, param in enumerate(self.parameters): |
---|
[c8e1996] | 3059 | content += param[2].GetValue() # parameter value |
---|
[f0d720b] | 3060 | content += ' & ' |
---|
[c8e1996] | 3061 | content += param[4].GetValue() # parameter error |
---|
[5ce7f17] | 3062 | if index < len(self.parameters) - 1: |
---|
[f0d720b] | 3063 | content += ' & ' |
---|
| 3064 | content += '\\\\ \\hline' |
---|
| 3065 | content += crlf |
---|
| 3066 | |
---|
| 3067 | content += '\\end{tabular}' |
---|
| 3068 | content += '\\end{table}' |
---|
| 3069 | return content |
---|
| 3070 | else: |
---|
| 3071 | return False |
---|
| 3072 | |
---|
| 3073 | def set_clipboard(self, content=None): |
---|
| 3074 | """ |
---|
| 3075 | Put the string to the clipboard |
---|
| 3076 | """ |
---|
| 3077 | if not content: |
---|
| 3078 | return False |
---|
| 3079 | if wx.TheClipboard.Open(): |
---|
| 3080 | wx.TheClipboard.SetData(wx.TextDataObject(str(content))) |
---|
| 3081 | wx.TheClipboard.Close() |
---|
| 3082 | return True |
---|
| 3083 | return None |
---|
[5ce7f17] | 3084 | |
---|
[f0d720b] | 3085 | def _get_copy_helper(self, param, orient_param): |
---|
| 3086 | """ |
---|
| 3087 | Helping get value and name of the params |
---|
[5ce7f17] | 3088 | |
---|
[f0d720b] | 3089 | : param param: parameters |
---|
| 3090 | : param orient_param = oritational params |
---|
| 3091 | : return content: strings [list] [name,value:....] |
---|
| 3092 | """ |
---|
| 3093 | content = '' |
---|
[5223602] | 3094 | bound_hi = '' |
---|
| 3095 | bound_lo = '' |
---|
[f0d720b] | 3096 | # go through the str params |
---|
| 3097 | for item in param: |
---|
| 3098 | # copy only the params shown |
---|
| 3099 | if not item[2].IsShown(): |
---|
| 3100 | continue |
---|
| 3101 | disfunc = '' |
---|
| 3102 | try: |
---|
| 3103 | if item[7].__class__.__name__ == 'ComboBox': |
---|
| 3104 | disfunc = str(item[7].GetValue()) |
---|
[7673ecd] | 3105 | except Exception: |
---|
[c155a16] | 3106 | logger.error(traceback.format_exc()) |
---|
[5ce7f17] | 3107 | |
---|
[f0d720b] | 3108 | # 2D |
---|
| 3109 | if self.data.__class__.__name__ == "Data2D": |
---|
| 3110 | try: |
---|
| 3111 | check = item[0].GetValue() |
---|
[7673ecd] | 3112 | except Exception: |
---|
[f0d720b] | 3113 | check = None |
---|
| 3114 | name = item[1] |
---|
| 3115 | value = item[2].GetValue() |
---|
| 3116 | # 1D |
---|
| 3117 | else: |
---|
[c8e1996] | 3118 | # for 1D all parameters except orientation |
---|
[f0d720b] | 3119 | if not item[1] in orient_param: |
---|
| 3120 | try: |
---|
| 3121 | check = item[0].GetValue() |
---|
| 3122 | except: |
---|
| 3123 | check = None |
---|
| 3124 | name = item[1] |
---|
| 3125 | value = item[2].GetValue() |
---|
| 3126 | |
---|
[5223602] | 3127 | # Bounds |
---|
| 3128 | try: |
---|
| 3129 | bound_lo = item[5].GetValue() |
---|
| 3130 | bound_hi = item[6].GetValue() |
---|
| 3131 | except Exception: |
---|
| 3132 | # harmless - need to just pass |
---|
| 3133 | pass |
---|
| 3134 | |
---|
[f0d720b] | 3135 | # add to the content |
---|
| 3136 | if disfunc != '': |
---|
[5ce7f17] | 3137 | |
---|
[f0d720b] | 3138 | disfunc = ',' + disfunc |
---|
| 3139 | # Need to support array func for copy/paste |
---|
| 3140 | try: |
---|
| 3141 | if disfunc.count('array') > 0: |
---|
| 3142 | disfunc += ',' |
---|
| 3143 | for val in self.values[name]: |
---|
| 3144 | disfunc += ' ' + str(val) |
---|
| 3145 | disfunc += ',' |
---|
| 3146 | for weight in self.weights[name]: |
---|
| 3147 | disfunc += ' ' + str(weight) |
---|
[7673ecd] | 3148 | except Exception: |
---|
[c155a16] | 3149 | logger.error(traceback.format_exc()) |
---|
[c8e1996] | 3150 | content += name + ',' + str(check) + ',' + value + disfunc + ',' + \ |
---|
| 3151 | bound_lo + ',' + bound_hi + ':' |
---|
[f0d720b] | 3152 | |
---|
| 3153 | return content |
---|
[5ce7f17] | 3154 | |
---|
[f0d720b] | 3155 | def get_clipboard(self): |
---|
| 3156 | """ |
---|
| 3157 | Get strings in the clipboard |
---|
| 3158 | """ |
---|
| 3159 | text = "" |
---|
| 3160 | # Get text from the clip board |
---|
| 3161 | if wx.TheClipboard.Open(): |
---|
| 3162 | if wx.TheClipboard.IsSupported(wx.DataFormat(wx.DF_TEXT)): |
---|
| 3163 | data = wx.TextDataObject() |
---|
| 3164 | # get wx dataobject |
---|
| 3165 | success = wx.TheClipboard.GetData(data) |
---|
| 3166 | # get text |
---|
| 3167 | if success: |
---|
| 3168 | text = data.GetText() |
---|
| 3169 | else: |
---|
| 3170 | text = '' |
---|
| 3171 | # close clipboard |
---|
| 3172 | wx.TheClipboard.Close() |
---|
| 3173 | return text |
---|
[5ce7f17] | 3174 | |
---|
[f0d720b] | 3175 | def get_paste(self): |
---|
| 3176 | """ |
---|
| 3177 | Paste params from the clipboard |
---|
| 3178 | """ |
---|
| 3179 | text = self.get_clipboard() |
---|
| 3180 | flag = self.get_paste_params(text) |
---|
| 3181 | self._copy_info(flag) |
---|
| 3182 | return flag |
---|
[5ce7f17] | 3183 | |
---|
[f0d720b] | 3184 | def get_paste_params(self, text=''): |
---|
| 3185 | """ |
---|
| 3186 | Get the string copies of the param names and values in the tap |
---|
| 3187 | """ |
---|
| 3188 | context = {} |
---|
| 3189 | # put the text into dictionary |
---|
| 3190 | lines = text.split(':') |
---|
| 3191 | if lines[0] != 'sasview_parameter_values': |
---|
| 3192 | self._copy_info(False) |
---|
| 3193 | return False |
---|
| 3194 | for line in lines[1:-1]: |
---|
| 3195 | if len(line) != 0: |
---|
| 3196 | item = line.split(',') |
---|
| 3197 | check = item[1] |
---|
| 3198 | name = item[0] |
---|
| 3199 | value = item[2] |
---|
| 3200 | # Transfer the text to content[dictionary] |
---|
| 3201 | context[name] = [check, value] |
---|
[5223602] | 3202 | |
---|
| 3203 | # limits |
---|
| 3204 | limit_lo = item[3] |
---|
| 3205 | context[name].append(limit_lo) |
---|
| 3206 | limit_hi = item[4] |
---|
| 3207 | context[name].append(limit_hi) |
---|
| 3208 | |
---|
[f0d720b] | 3209 | # ToDo: PlugIn this poly disp function for pasting |
---|
| 3210 | try: |
---|
[5223602] | 3211 | poly_func = item[5] |
---|
[f0d720b] | 3212 | context[name].append(poly_func) |
---|
| 3213 | try: |
---|
| 3214 | # take the vals and weights for array |
---|
[5223602] | 3215 | array_values = item[6].split(' ') |
---|
| 3216 | array_weights = item[7].split(' ') |
---|
[f0d720b] | 3217 | val = [float(a_val) for a_val in array_values[1:]] |
---|
| 3218 | weit = [float(a_weit) for a_weit in array_weights[1:]] |
---|
[5ce7f17] | 3219 | |
---|
[f0d720b] | 3220 | context[name].append(val) |
---|
| 3221 | context[name].append(weit) |
---|
| 3222 | except: |
---|
| 3223 | raise |
---|
| 3224 | except: |
---|
| 3225 | poly_func = '' |
---|
| 3226 | context[name].append(poly_func) |
---|
| 3227 | |
---|
| 3228 | # Do it if params exist |
---|
[c8e1996] | 3229 | if self.parameters: |
---|
[f0d720b] | 3230 | # go through the parameters |
---|
| 3231 | self._get_paste_helper(self.parameters, |
---|
| 3232 | self.orientation_params, context) |
---|
| 3233 | |
---|
| 3234 | # go through the fittables |
---|
| 3235 | self._get_paste_helper(self.fittable_param, |
---|
| 3236 | self.orientation_params_disp, |
---|
| 3237 | context) |
---|
| 3238 | |
---|
| 3239 | # go through the fixed params |
---|
| 3240 | self._get_paste_helper(self.fixed_param, |
---|
| 3241 | self.orientation_params_disp, context) |
---|
[5ce7f17] | 3242 | |
---|
[f0d720b] | 3243 | # go through the str params |
---|
| 3244 | self._get_paste_helper(self.str_parameters, |
---|
| 3245 | self.orientation_params, context) |
---|
[5ce7f17] | 3246 | |
---|
[f0d720b] | 3247 | return True |
---|
| 3248 | return None |
---|
[5ce7f17] | 3249 | |
---|
[f0d720b] | 3250 | def _get_paste_helper(self, param, orient_param, content): |
---|
| 3251 | """ |
---|
| 3252 | Helping set values of the params |
---|
[5ce7f17] | 3253 | |
---|
[f0d720b] | 3254 | : param param: parameters |
---|
| 3255 | : param orient_param: oritational params |
---|
| 3256 | : param content: dictionary [ name, value: name1.value1,...] |
---|
| 3257 | """ |
---|
| 3258 | # go through the str params |
---|
| 3259 | for item in param: |
---|
| 3260 | # 2D |
---|
| 3261 | if self.data.__class__.__name__ == "Data2D": |
---|
| 3262 | name = item[1] |
---|
| 3263 | if name in content.keys(): |
---|
[5223602] | 3264 | values = content[name] |
---|
| 3265 | check = values[0] |
---|
| 3266 | pd = values[1] |
---|
| 3267 | |
---|
[f0d720b] | 3268 | if name.count('.') > 0: |
---|
[6c382da] | 3269 | # If this is parameter.width, then pd may be a floating |
---|
| 3270 | # point value or it may be an array distribution. |
---|
| 3271 | # Nothing to do for parameter.npts or parameter.nsigmas. |
---|
[f0d720b] | 3272 | try: |
---|
| 3273 | float(pd) |
---|
[6c382da] | 3274 | if name.endswith('.npts'): |
---|
| 3275 | pd = int(pd) |
---|
| 3276 | except Exception: |
---|
[c8e1996] | 3277 | # continue |
---|
[f0d720b] | 3278 | if not pd and pd != '': |
---|
| 3279 | continue |
---|
| 3280 | item[2].SetValue(str(pd)) |
---|
| 3281 | if item in self.fixed_param and pd == '': |
---|
| 3282 | # Only array func has pd == '' case. |
---|
| 3283 | item[2].Enable(False) |
---|
[6c382da] | 3284 | else: |
---|
| 3285 | item[2].Enable(True) |
---|
[f0d720b] | 3286 | if item[2].__class__.__name__ == "ComboBox": |
---|
| 3287 | if content[name][1] in self.model.fun_list: |
---|
| 3288 | fun_val = self.model.fun_list[content[name][1]] |
---|
| 3289 | self.model.setParam(name, fun_val) |
---|
[5223602] | 3290 | try: |
---|
| 3291 | item[5].SetValue(str(values[-3])) |
---|
| 3292 | item[6].SetValue(str(values[-2])) |
---|
| 3293 | except Exception: |
---|
| 3294 | # passing as harmless non-update |
---|
| 3295 | pass |
---|
[5ce7f17] | 3296 | |
---|
[f0d720b] | 3297 | value = content[name][1:] |
---|
| 3298 | self._paste_poly_help(item, value) |
---|
| 3299 | if check == 'True': |
---|
| 3300 | is_true = True |
---|
| 3301 | elif check == 'False': |
---|
| 3302 | is_true = False |
---|
| 3303 | else: |
---|
| 3304 | is_true = None |
---|
[c8e1996] | 3305 | if is_true is not None: |
---|
[f0d720b] | 3306 | item[0].SetValue(is_true) |
---|
| 3307 | # 1D |
---|
| 3308 | else: |
---|
[c8e1996] | 3309 | # for 1D all parameters except orientation |
---|
[f0d720b] | 3310 | if not item[1] in orient_param: |
---|
| 3311 | name = item[1] |
---|
| 3312 | if name in content.keys(): |
---|
| 3313 | check = content[name][0] |
---|
| 3314 | # Avoid changing combox content |
---|
| 3315 | value = content[name][1:] |
---|
| 3316 | pd = value[0] |
---|
| 3317 | if name.count('.') > 0: |
---|
[c8e1996] | 3318 | # If this is parameter.width, then pd may be a |
---|
| 3319 | # floating point value or it may be an array |
---|
| 3320 | # distribution. Nothing to do for parameter.npts or |
---|
| 3321 | # parameter.nsigmas. |
---|
[f0d720b] | 3322 | try: |
---|
| 3323 | pd = float(pd) |
---|
[6c382da] | 3324 | if name.endswith('.npts'): |
---|
| 3325 | pd = int(pd) |
---|
[f0d720b] | 3326 | except: |
---|
[c8e1996] | 3327 | # continue |
---|
[f0d720b] | 3328 | if not pd and pd != '': |
---|
| 3329 | continue |
---|
| 3330 | item[2].SetValue(str(pd)) |
---|
| 3331 | if item in self.fixed_param and pd == '': |
---|
| 3332 | # Only array func has pd == '' case. |
---|
| 3333 | item[2].Enable(False) |
---|
[6c382da] | 3334 | else: |
---|
| 3335 | item[2].Enable(True) |
---|
[f0d720b] | 3336 | if item[2].__class__.__name__ == "ComboBox": |
---|
| 3337 | if value[0] in self.model.fun_list: |
---|
| 3338 | fun_val = self.model.fun_list[value[0]] |
---|
| 3339 | self.model.setParam(name, fun_val) |
---|
| 3340 | # save state |
---|
[5223602] | 3341 | try: |
---|
| 3342 | item[5].SetValue(str(value[-3])) |
---|
| 3343 | item[6].SetValue(str(value[-2])) |
---|
| 3344 | except Exception: |
---|
| 3345 | # passing as harmless non-update |
---|
| 3346 | pass |
---|
| 3347 | |
---|
[f0d720b] | 3348 | self._paste_poly_help(item, value) |
---|
| 3349 | if check == 'True': |
---|
| 3350 | is_true = True |
---|
| 3351 | elif check == 'False': |
---|
| 3352 | is_true = False |
---|
| 3353 | else: |
---|
| 3354 | is_true = None |
---|
[c8e1996] | 3355 | if is_true is not None: |
---|
[f0d720b] | 3356 | item[0].SetValue(is_true) |
---|
[5ce7f17] | 3357 | |
---|
[01cfd13] | 3358 | self.select_param(event=None) |
---|
| 3359 | self.Refresh() |
---|
| 3360 | |
---|
[f0d720b] | 3361 | def _paste_poly_help(self, item, value): |
---|
| 3362 | """ |
---|
| 3363 | Helps get paste for poly function |
---|
[5ce7f17] | 3364 | |
---|
[6c382da] | 3365 | *item* is the parameter name |
---|
| 3366 | |
---|
| 3367 | *value* depends on which parameter is being processed, and whether it |
---|
| 3368 | has array polydispersity. |
---|
| 3369 | |
---|
| 3370 | For parameters without array polydispersity: |
---|
| 3371 | |
---|
| 3372 | parameter => ['FLOAT', ''] |
---|
| 3373 | parameter.width => ['FLOAT', 'DISTRIBUTION', ''] |
---|
| 3374 | parameter.npts => ['FLOAT', ''] |
---|
| 3375 | parameter.nsigmas => ['FLOAT', ''] |
---|
| 3376 | |
---|
| 3377 | For parameters with array polydispersity: |
---|
| 3378 | |
---|
| 3379 | parameter => ['FLOAT', ''] |
---|
| 3380 | parameter.width => ['FILENAME', 'array', [x1, ...], [w1, ...]] |
---|
| 3381 | parameter.npts => ['FLOAT', ''] |
---|
| 3382 | parameter.nsigmas => ['FLOAT', ''] |
---|
[f0d720b] | 3383 | """ |
---|
[6c382da] | 3384 | # Do nothing if not setting polydispersity |
---|
[5223602] | 3385 | if len(value[3]) == 0: |
---|
[6c382da] | 3386 | return |
---|
[5ce7f17] | 3387 | |
---|
[6c382da] | 3388 | try: |
---|
| 3389 | name = item[7].Name |
---|
| 3390 | param_name = name.split('.')[0] |
---|
| 3391 | item[7].SetValue(value[1]) |
---|
| 3392 | selection = item[7].GetCurrentSelection() |
---|
| 3393 | dispersity = item[7].GetClientData(selection) |
---|
| 3394 | disp_model = dispersity() |
---|
| 3395 | |
---|
| 3396 | if value[1] == 'array': |
---|
[9a5097c] | 3397 | pd_vals = np.array(value[2]) |
---|
| 3398 | pd_weights = np.array(value[3]) |
---|
[6c382da] | 3399 | if len(pd_vals) == 0 or len(pd_vals) != len(pd_weights): |
---|
| 3400 | msg = ("bad array distribution parameters for %s" |
---|
| 3401 | % param_name) |
---|
| 3402 | raise ValueError(msg) |
---|
| 3403 | self._set_disp_cb(True, item=item) |
---|
| 3404 | self._set_array_disp_model(name=name, |
---|
| 3405 | disp=disp_model, |
---|
| 3406 | values=pd_vals, |
---|
| 3407 | weights=pd_weights) |
---|
| 3408 | else: |
---|
| 3409 | self._set_disp_cb(False, item=item) |
---|
| 3410 | self._disp_obj_dict[name] = disp_model |
---|
| 3411 | self.model.set_dispersion(param_name, disp_model) |
---|
| 3412 | self.state._disp_obj_dict[name] = disp_model.type |
---|
| 3413 | # TODO: It's not an array, why update values and weights? |
---|
| 3414 | self.model._persistency_dict[param_name] = \ |
---|
| 3415 | [self.values, self.weights] |
---|
| 3416 | self.state.values = self.values |
---|
| 3417 | self.state.weights = self.weights |
---|
[5ce7f17] | 3418 | |
---|
[6c382da] | 3419 | except Exception: |
---|
[c155a16] | 3420 | logger.error(traceback.format_exc()) |
---|
[6c382da] | 3421 | print "Error in BasePage._paste_poly_help: %s" % \ |
---|
[c8e1996] | 3422 | sys.exc_info()[1] |
---|
[6c382da] | 3423 | |
---|
| 3424 | def _set_disp_cb(self, isarray, item): |
---|
[f0d720b] | 3425 | """ |
---|
| 3426 | Set cb for array disp |
---|
| 3427 | """ |
---|
[6c382da] | 3428 | if isarray: |
---|
| 3429 | item[0].SetValue(False) |
---|
| 3430 | item[0].Enable(False) |
---|
| 3431 | item[2].Enable(False) |
---|
| 3432 | item[3].Show(False) |
---|
| 3433 | item[4].Show(False) |
---|
| 3434 | item[5].SetValue('') |
---|
| 3435 | item[5].Enable(False) |
---|
| 3436 | item[6].SetValue('') |
---|
| 3437 | item[6].Enable(False) |
---|
| 3438 | else: |
---|
| 3439 | item[0].Enable() |
---|
| 3440 | item[2].Enable() |
---|
| 3441 | item[3].Show(True) |
---|
| 3442 | item[4].Show(True) |
---|
| 3443 | item[5].Enable() |
---|
| 3444 | item[6].Enable() |
---|
[5ce7f17] | 3445 | |
---|
[f0d720b] | 3446 | def update_pinhole_smear(self): |
---|
| 3447 | """ |
---|
| 3448 | Method to be called by sub-classes |
---|
| 3449 | Moveit; This method doesn't belong here |
---|
| 3450 | """ |
---|
| 3451 | print "BasicPage.update_pinhole_smear was called: skipping" |
---|
| 3452 | return |
---|
| 3453 | |
---|
| 3454 | def _read_category_info(self): |
---|
| 3455 | """ |
---|
| 3456 | Reads the categories in from file |
---|
| 3457 | """ |
---|
| 3458 | # # ILL mod starts here - July 2012 kieranrcampbell@gmail.com |
---|
| 3459 | self.master_category_dict = defaultdict(list) |
---|
| 3460 | self.by_model_dict = defaultdict(list) |
---|
| 3461 | self.model_enabled_dict = defaultdict(bool) |
---|
[212bfc2] | 3462 | categorization_file = CategoryInstaller.get_user_file() |
---|
| 3463 | with open(categorization_file, 'rb') as f: |
---|
| 3464 | self.master_category_dict = json.load(f) |
---|
| 3465 | self._regenerate_model_dict() |
---|
[f0d720b] | 3466 | |
---|
| 3467 | def _regenerate_model_dict(self): |
---|
| 3468 | """ |
---|
[5ce7f17] | 3469 | regenerates self.by_model_dict which has each model name as the |
---|
[f0d720b] | 3470 | key and the list of categories belonging to that model |
---|
| 3471 | along with the enabled mapping |
---|
| 3472 | """ |
---|
| 3473 | self.by_model_dict = defaultdict(list) |
---|
| 3474 | for category in self.master_category_dict: |
---|
| 3475 | for (model, enabled) in self.master_category_dict[category]: |
---|
| 3476 | self.by_model_dict[model].append(category) |
---|
| 3477 | self.model_enabled_dict[model] = enabled |
---|
[5ce7f17] | 3478 | |
---|
[f0d720b] | 3479 | def _populate_listbox(self): |
---|
| 3480 | """ |
---|
| 3481 | fills out the category list box |
---|
| 3482 | """ |
---|
[e92a352] | 3483 | uncat_str = 'Plugin Models' |
---|
[f0d720b] | 3484 | self._read_category_info() |
---|
| 3485 | |
---|
| 3486 | self.categorybox.Clear() |
---|
| 3487 | cat_list = sorted(self.master_category_dict.keys()) |
---|
[c8e1996] | 3488 | if uncat_str not in cat_list: |
---|
[f0d720b] | 3489 | cat_list.append(uncat_str) |
---|
[5ce7f17] | 3490 | |
---|
[f0d720b] | 3491 | for category in cat_list: |
---|
| 3492 | if category != '': |
---|
| 3493 | self.categorybox.Append(category) |
---|
| 3494 | |
---|
| 3495 | if self.categorybox.GetSelection() == wx.NOT_FOUND: |
---|
| 3496 | self.categorybox.SetSelection(0) |
---|
| 3497 | else: |
---|
[c8e1996] | 3498 | self.categorybox.SetSelection( |
---|
[f0d720b] | 3499 | self.categorybox.GetSelection()) |
---|
[c8e1996] | 3500 | # self._on_change_cat(None) |
---|
[f0d720b] | 3501 | |
---|
| 3502 | def _on_change_cat(self, event): |
---|
| 3503 | """ |
---|
| 3504 | Callback for category change action |
---|
| 3505 | """ |
---|
| 3506 | self.model_name = None |
---|
| 3507 | category = self.categorybox.GetStringSelection() |
---|
[c8e1996] | 3508 | if category is None: |
---|
[f0d720b] | 3509 | return |
---|
| 3510 | self.model_box.Clear() |
---|
| 3511 | |
---|
[e92a352] | 3512 | if category == 'Plugin Models': |
---|
[f0d720b] | 3513 | for model in self.model_list_box[category]: |
---|
| 3514 | str_m = str(model).split(".")[0] |
---|
| 3515 | self.model_box.Append(str_m) |
---|
| 3516 | |
---|
| 3517 | else: |
---|
[5ce7f17] | 3518 | for (model, enabled) in sorted(self.master_category_dict[category], |
---|
[c8e1996] | 3519 | key=lambda name: name[0]): |
---|
[f0d720b] | 3520 | if(enabled): |
---|
| 3521 | self.model_box.Append(model) |
---|
| 3522 | |
---|
| 3523 | def _fill_model_sizer(self, sizer): |
---|
| 3524 | """ |
---|
| 3525 | fill sizer containing model info |
---|
| 3526 | """ |
---|
[6f16e25] | 3527 | # This should only be called once per fit tab |
---|
[c8e1996] | 3528 | # print "==== Entering _fill_model_sizer" |
---|
| 3529 | # Add model function Details button in fitpanel. |
---|
| 3530 | # The following 3 lines are for Mac. Let JHC know before modifying... |
---|
[f0d720b] | 3531 | title = "Model" |
---|
| 3532 | self.formfactorbox = None |
---|
| 3533 | self.multifactorbox = None |
---|
[6f16e25] | 3534 | self.mbox_description = wx.StaticBox(self, wx.ID_ANY, str(title)) |
---|
[f0d720b] | 3535 | boxsizer1 = wx.StaticBoxSizer(self.mbox_description, wx.VERTICAL) |
---|
| 3536 | sizer_cat = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 3537 | self.mbox_description.SetForegroundColour(wx.RED) |
---|
[6f16e25] | 3538 | wx_id = self._ids.next() |
---|
| 3539 | self.model_func = wx.Button(self, wx_id, 'Help', size=(80, 23)) |
---|
| 3540 | self.model_func.Bind(wx.EVT_BUTTON, self.on_function_help_clicked, |
---|
| 3541 | id=wx_id) |
---|
[5ce7f17] | 3542 | self.model_func.SetToolTipString("Full Model Function Help") |
---|
[6f16e25] | 3543 | wx_id = self._ids.next() |
---|
| 3544 | self.model_help = wx.Button(self, wx_id, 'Description', size=(80, 23)) |
---|
| 3545 | self.model_help.Bind(wx.EVT_BUTTON, self.on_model_help_clicked, |
---|
| 3546 | id=wx_id) |
---|
[5ce7f17] | 3547 | self.model_help.SetToolTipString("Short Model Function Description") |
---|
[6f16e25] | 3548 | wx_id = self._ids.next() |
---|
| 3549 | self.model_view = wx.Button(self, wx_id, "Show 2D", size=(80, 23)) |
---|
| 3550 | self.model_view.Bind(wx.EVT_BUTTON, self._onModel2D, id=wx_id) |
---|
[f0d720b] | 3551 | hint = "toggle view of model from 1D to 2D or 2D to 1D" |
---|
| 3552 | self.model_view.SetToolTipString(hint) |
---|
[5ce7f17] | 3553 | |
---|
[6f16e25] | 3554 | cat_set_box = wx.StaticBox(self, wx.ID_ANY, 'Category') |
---|
[f0d720b] | 3555 | sizer_cat_box = wx.StaticBoxSizer(cat_set_box, wx.HORIZONTAL) |
---|
| 3556 | sizer_cat_box.SetMinSize((200, 50)) |
---|
[6f16e25] | 3557 | self.categorybox = wx.ComboBox(self, wx.ID_ANY, |
---|
| 3558 | style=wx.CB_READONLY) |
---|
[5ce7f17] | 3559 | self.categorybox.SetToolTip(wx.ToolTip("Select a Category/Type")) |
---|
[f0d720b] | 3560 | self._populate_listbox() |
---|
[6f16e25] | 3561 | wx.EVT_COMBOBOX(self.categorybox, wx.ID_ANY, self._show_combox) |
---|
[c8e1996] | 3562 | # self.shape_rbutton = wx.RadioButton(self, wx.ID_ANY, 'Shapes', |
---|
[f0d720b] | 3563 | # style=wx.RB_GROUP) |
---|
[c8e1996] | 3564 | # self.shape_indep_rbutton = wx.RadioButton(self, wx.ID_ANY, |
---|
[f0d720b] | 3565 | # "Shape-Independent") |
---|
[c8e1996] | 3566 | # self.struct_rbutton = wx.RadioButton(self, wx.ID_ANY, |
---|
[6f16e25] | 3567 | # "Structure Factor ") |
---|
[c8e1996] | 3568 | # self.plugin_rbutton = wx.RadioButton(self, wx.ID_ANY, |
---|
[6f16e25] | 3569 | # "Uncategorized") |
---|
[5ce7f17] | 3570 | |
---|
[c8e1996] | 3571 | # self.Bind(wx.EVT_RADIOBUTTON, self._show_combox, |
---|
[f0d720b] | 3572 | # id=self.shape_rbutton.GetId()) |
---|
[c8e1996] | 3573 | # self.Bind(wx.EVT_RADIOBUTTON, self._show_combox, |
---|
[f0d720b] | 3574 | # id=self.shape_indep_rbutton.GetId()) |
---|
[c8e1996] | 3575 | # self.Bind(wx.EVT_RADIOBUTTON, self._show_combox, |
---|
[f0d720b] | 3576 | # id=self.struct_rbutton.GetId()) |
---|
[c8e1996] | 3577 | # self.Bind(wx.EVT_RADIOBUTTON, self._show_combox, |
---|
[f0d720b] | 3578 | # id=self.plugin_rbutton.GetId()) |
---|
[c8e1996] | 3579 | # MAC needs SetValue |
---|
[5ce7f17] | 3580 | |
---|
[6f16e25] | 3581 | show_cat_button = wx.Button(self, wx.ID_ANY, "Modify") |
---|
[f0d720b] | 3582 | cat_tip = "Modify model categories \n" |
---|
| 3583 | cat_tip += "(also accessible from the menu bar)." |
---|
[c8e1996] | 3584 | show_cat_button.SetToolTip(wx.ToolTip(cat_tip)) |
---|
[f0d720b] | 3585 | show_cat_button.Bind(wx.EVT_BUTTON, self._on_modify_cat) |
---|
| 3586 | sizer_cat_box.Add(self.categorybox, 1, wx.RIGHT, 3) |
---|
[c8e1996] | 3587 | sizer_cat_box.Add((10, 10)) |
---|
[f0d720b] | 3588 | sizer_cat_box.Add(show_cat_button) |
---|
[c8e1996] | 3589 | # self.shape_rbutton.SetValue(True) |
---|
[bac3988] | 3590 | |
---|
[f0d720b] | 3591 | sizer_radiobutton = wx.GridSizer(2, 2, 5, 5) |
---|
[c8e1996] | 3592 | # sizer_radiobutton.Add(self.shape_rbutton) |
---|
| 3593 | # sizer_radiobutton.Add(self.shape_indep_rbutton) |
---|
| 3594 | sizer_radiobutton.Add((5, 5)) |
---|
[5ce7f17] | 3595 | sizer_radiobutton.Add(self.model_view, 1, wx.RIGHT, 5) |
---|
[c8e1996] | 3596 | # sizer_radiobutton.Add(self.plugin_rbutton) |
---|
| 3597 | # sizer_radiobutton.Add(self.struct_rbutton) |
---|
| 3598 | # sizer_radiobutton.Add((5,5)) |
---|
[5ce7f17] | 3599 | sizer_radiobutton.Add(self.model_help, 1, wx.RIGHT | wx.LEFT, 5) |
---|
[c8e1996] | 3600 | # sizer_radiobutton.Add((5,5)) |
---|
[5ce7f17] | 3601 | sizer_radiobutton.Add(self.model_func, 1, wx.RIGHT, 5) |
---|
[f0d720b] | 3602 | sizer_cat.Add(sizer_cat_box, 1, wx.LEFT, 2.5) |
---|
| 3603 | sizer_cat.Add(sizer_radiobutton) |
---|
| 3604 | sizer_selection = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 3605 | mutifactor_selection = wx.BoxSizer(wx.HORIZONTAL) |
---|
[5ce7f17] | 3606 | |
---|
[6f16e25] | 3607 | self.text1 = wx.StaticText(self, wx.ID_ANY, "") |
---|
| 3608 | self.text2 = wx.StaticText(self, wx.ID_ANY, "P(Q)*S(Q)") |
---|
| 3609 | self.mutifactor_text = wx.StaticText(self, wx.ID_ANY, "No. of Shells: ") |
---|
| 3610 | self.mutifactor_text1 = wx.StaticText(self, wx.ID_ANY, "") |
---|
| 3611 | self.show_sld_button = wx.Button(self, wx.ID_ANY, "Show SLD Profile") |
---|
[f0d720b] | 3612 | self.show_sld_button.Bind(wx.EVT_BUTTON, self._on_show_sld) |
---|
| 3613 | |
---|
[6f16e25] | 3614 | self.formfactorbox = wx.ComboBox(self, wx.ID_ANY, style=wx.CB_READONLY) |
---|
[5ce7f17] | 3615 | self.formfactorbox.SetToolTip(wx.ToolTip("Select a Model")) |
---|
[c8e1996] | 3616 | if self.model is not None: |
---|
[f0d720b] | 3617 | self.formfactorbox.SetValue(self.model.name) |
---|
[6f16e25] | 3618 | self.structurebox = wx.ComboBox(self, wx.ID_ANY, style=wx.CB_READONLY) |
---|
| 3619 | self.multifactorbox = wx.ComboBox(self, wx.ID_ANY, style=wx.CB_READONLY) |
---|
[f0d720b] | 3620 | self.initialize_combox() |
---|
[6f16e25] | 3621 | wx.EVT_COMBOBOX(self.formfactorbox, wx.ID_ANY, self._on_select_model) |
---|
[f0d720b] | 3622 | |
---|
[6f16e25] | 3623 | wx.EVT_COMBOBOX(self.structurebox, wx.ID_ANY, self._on_select_model) |
---|
| 3624 | wx.EVT_COMBOBOX(self.multifactorbox, wx.ID_ANY, self._on_select_model) |
---|
[c8e1996] | 3625 | # check model type to show sizer |
---|
| 3626 | if self.model is not None: |
---|
[f0d720b] | 3627 | print "_set_model_sizer_selection: disabled." |
---|
[c8e1996] | 3628 | # self._set_model_sizer_selection(self.model) |
---|
[5ce7f17] | 3629 | |
---|
[f0d720b] | 3630 | sizer_selection.Add(self.text1) |
---|
| 3631 | sizer_selection.Add((10, 5)) |
---|
| 3632 | sizer_selection.Add(self.formfactorbox) |
---|
| 3633 | sizer_selection.Add((5, 5)) |
---|
| 3634 | sizer_selection.Add(self.text2) |
---|
| 3635 | sizer_selection.Add((5, 5)) |
---|
| 3636 | sizer_selection.Add(self.structurebox) |
---|
[5ce7f17] | 3637 | |
---|
[f0d720b] | 3638 | mutifactor_selection.Add((13, 5)) |
---|
| 3639 | mutifactor_selection.Add(self.mutifactor_text) |
---|
| 3640 | mutifactor_selection.Add(self.multifactorbox) |
---|
| 3641 | mutifactor_selection.Add((5, 5)) |
---|
| 3642 | mutifactor_selection.Add(self.mutifactor_text1) |
---|
| 3643 | mutifactor_selection.Add((10, 5)) |
---|
| 3644 | mutifactor_selection.Add(self.show_sld_button) |
---|
| 3645 | |
---|
| 3646 | boxsizer1.Add(sizer_cat) |
---|
| 3647 | boxsizer1.Add((10, 10)) |
---|
| 3648 | boxsizer1.Add(sizer_selection) |
---|
| 3649 | boxsizer1.Add((10, 10)) |
---|
| 3650 | boxsizer1.Add(mutifactor_selection) |
---|
[5ce7f17] | 3651 | |
---|
[f0d720b] | 3652 | self._set_multfactor_combobox() |
---|
| 3653 | self.multifactorbox.SetSelection(1) |
---|
| 3654 | self.show_sld_button.Hide() |
---|
| 3655 | sizer.Add(boxsizer1, 0, wx.EXPAND | wx.ALL, 10) |
---|
| 3656 | sizer.Layout() |
---|
[5ce7f17] | 3657 | |
---|
[f0d720b] | 3658 | def on_smear_helper(self, update=False): |
---|
| 3659 | """ |
---|
| 3660 | Help for onSmear if implemented |
---|
[5ce7f17] | 3661 | |
---|
[f0d720b] | 3662 | :param update: force or not to update |
---|
| 3663 | """ |
---|
| 3664 | def reset_page(self, state, first=False): |
---|
| 3665 | """ |
---|
| 3666 | reset the state if implemented |
---|
| 3667 | """ |
---|
| 3668 | def onSmear(self, event): |
---|
| 3669 | """ |
---|
[5ce7f17] | 3670 | Create a smear object if implemented |
---|
[f0d720b] | 3671 | """ |
---|
| 3672 | def onPinholeSmear(self, event): |
---|
| 3673 | """ |
---|
| 3674 | Create a custom pinhole smear object if implemented |
---|
| 3675 | """ |
---|
| 3676 | def onSlitSmear(self, event): |
---|
| 3677 | """ |
---|
| 3678 | Create a custom slit smear object if implemented |
---|
| 3679 | """ |
---|
| 3680 | def update_slit_smear(self): |
---|
| 3681 | """ |
---|
| 3682 | called by kill_focus on pinhole TextCntrl |
---|
| 3683 | to update the changes if implemented |
---|
| 3684 | """ |
---|
| 3685 | def select_param(self, event): |
---|
| 3686 | """ |
---|
| 3687 | Select TextCtrl checked if implemented |
---|
| 3688 | """ |
---|
| 3689 | def set_data(self, data=None): |
---|
| 3690 | """ |
---|
| 3691 | Sets data if implemented |
---|
| 3692 | """ |
---|
| 3693 | def _is_2D(self): |
---|
| 3694 | """ |
---|
| 3695 | Check if data_name is Data2D if implemented |
---|
| 3696 | """ |
---|
| 3697 | def _on_select_model(self, event=None): |
---|
| 3698 | """ |
---|
| 3699 | call back for model selection if implemented |
---|
| 3700 | """ |
---|
| 3701 | def get_weight_flag(self): |
---|
| 3702 | """ |
---|
| 3703 | Get flag corresponding to a given weighting dI data if implemented |
---|
| 3704 | """ |
---|
| 3705 | def _set_sizer_dispersion(self): |
---|
| 3706 | """ |
---|
| 3707 | draw sizer for dispersity if implemented |
---|
| 3708 | """ |
---|
| 3709 | def get_all_checked_params(self): |
---|
| 3710 | """ |
---|
| 3711 | Found all parameters current check and add them to list of parameters |
---|
| 3712 | to fit if implemented |
---|
| 3713 | """ |
---|
| 3714 | def show_npts2fit(self): |
---|
| 3715 | """ |
---|
| 3716 | setValue Npts for fitting if implemented |
---|
| 3717 | """ |
---|
| 3718 | def _onModel2D(self, event): |
---|
| 3719 | """ |
---|
| 3720 | toggle view of model from 1D to 2D or 2D from 1D if implemented |
---|
| 3721 | """ |
---|
[373d4ee] | 3722 | |
---|
[c8e1996] | 3723 | |
---|
[373d4ee] | 3724 | class ModelTextCtrl(wx.TextCtrl): |
---|
| 3725 | """ |
---|
| 3726 | Text control for model and fit parameters. |
---|
| 3727 | Binds the appropriate events for user interactions. |
---|
| 3728 | Default callback methods can be overwritten on initialization |
---|
| 3729 | |
---|
| 3730 | :param kill_focus_callback: callback method for EVT_KILL_FOCUS event |
---|
| 3731 | :param set_focus_callback: callback method for EVT_SET_FOCUS event |
---|
| 3732 | :param mouse_up_callback: callback method for EVT_LEFT_UP event |
---|
| 3733 | :param text_enter_callback: callback method for EVT_TEXT_ENTER event |
---|
| 3734 | |
---|
| 3735 | """ |
---|
[c8e1996] | 3736 | # Set to True when the mouse is clicked while whole string is selected |
---|
[373d4ee] | 3737 | full_selection = False |
---|
[c8e1996] | 3738 | # Call back for EVT_SET_FOCUS events |
---|
[373d4ee] | 3739 | _on_set_focus_callback = None |
---|
| 3740 | |
---|
| 3741 | def __init__(self, parent, id=-1, |
---|
| 3742 | value=wx.EmptyString, |
---|
| 3743 | pos=wx.DefaultPosition, |
---|
| 3744 | size=wx.DefaultSize, |
---|
| 3745 | style=0, |
---|
| 3746 | validator=wx.DefaultValidator, |
---|
| 3747 | name=wx.TextCtrlNameStr, |
---|
| 3748 | kill_focus_callback=None, |
---|
| 3749 | set_focus_callback=None, |
---|
| 3750 | mouse_up_callback=None, |
---|
| 3751 | text_enter_callback=None): |
---|
| 3752 | |
---|
| 3753 | wx.TextCtrl.__init__(self, parent, id, value, pos, |
---|
| 3754 | size, style, validator, name) |
---|
| 3755 | |
---|
| 3756 | # Bind appropriate events |
---|
| 3757 | self._on_set_focus_callback = parent.onSetFocus \ |
---|
| 3758 | if set_focus_callback is None else set_focus_callback |
---|
| 3759 | self.Bind(wx.EVT_SET_FOCUS, self._on_set_focus) |
---|
[c8e1996] | 3760 | self.Bind(wx.EVT_KILL_FOCUS, self._silent_kill_focus |
---|
| 3761 | if kill_focus_callback is None else kill_focus_callback) |
---|
| 3762 | self.Bind(wx.EVT_TEXT_ENTER, parent._onparamEnter |
---|
| 3763 | if text_enter_callback is None else text_enter_callback) |
---|
[373d4ee] | 3764 | if not ON_MAC: |
---|
[c8e1996] | 3765 | self.Bind(wx.EVT_LEFT_UP, self._highlight_text |
---|
| 3766 | if mouse_up_callback is None else mouse_up_callback) |
---|
[373d4ee] | 3767 | |
---|
| 3768 | def _on_set_focus(self, event): |
---|
| 3769 | """ |
---|
| 3770 | Catch when the text control is set in focus to highlight the whole |
---|
| 3771 | text if necessary |
---|
| 3772 | |
---|
| 3773 | :param event: mouse event |
---|
| 3774 | |
---|
| 3775 | """ |
---|
| 3776 | event.Skip() |
---|
| 3777 | self.full_selection = True |
---|
| 3778 | return self._on_set_focus_callback(event) |
---|
| 3779 | |
---|
| 3780 | def _highlight_text(self, event): |
---|
| 3781 | """ |
---|
| 3782 | Highlight text of a TextCtrl only of no text has be selected |
---|
| 3783 | |
---|
| 3784 | :param event: mouse event |
---|
| 3785 | |
---|
| 3786 | """ |
---|
| 3787 | # Make sure the mouse event is available to other listeners |
---|
| 3788 | event.Skip() |
---|
| 3789 | control = event.GetEventObject() |
---|
| 3790 | if self.full_selection: |
---|
| 3791 | self.full_selection = False |
---|
| 3792 | # Check that we have a TextCtrl |
---|
| 3793 | if issubclass(control.__class__, wx.TextCtrl): |
---|
| 3794 | # Check whether text has been selected, |
---|
| 3795 | # if not, select the whole string |
---|
| 3796 | (start, end) = control.GetSelection() |
---|
| 3797 | if start == end: |
---|
| 3798 | control.SetSelection(-1, -1) |
---|
| 3799 | |
---|
| 3800 | def _silent_kill_focus(self, event): |
---|
| 3801 | """ |
---|
| 3802 | Save the state of the page |
---|
| 3803 | """ |
---|
| 3804 | |
---|
| 3805 | event.Skip() |
---|
[9c9fae1] | 3806 | # pass |
---|