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