[682c432] | 1 | """ |
---|
| 2 | FitPanel class contains fields allowing to display results when |
---|
| 3 | fitting a model and one data |
---|
| 4 | """ |
---|
| 5 | import sys |
---|
| 6 | import wx |
---|
| 7 | import wx.lib.newevent |
---|
[9a5097c] | 8 | import numpy as np |
---|
[682c432] | 9 | import copy |
---|
| 10 | import math |
---|
| 11 | import time |
---|
[6c382da] | 12 | import traceback |
---|
[cb4ef58] | 13 | |
---|
| 14 | from sasmodels.weights import MODELS as POLYDISPERSITY_MODELS |
---|
| 15 | |
---|
[50fcb09] | 16 | from sas.sascalc.fit.qsmearing import smear_selection |
---|
| 17 | |
---|
[c8e1996] | 18 | from sas.sasgui.guiframe.events import StatusEvent, NewPlotEvent, \ |
---|
| 19 | PlotQrangeEvent |
---|
[d85c194] | 20 | from sas.sasgui.guiframe.dataFitting import check_data_validity |
---|
[c8e1996] | 21 | from sas.sasgui.guiframe.utils import format_number, check_float |
---|
[d85c194] | 22 | from sas.sasgui.guiframe.documentation_window import DocumentationWindow |
---|
[682c432] | 23 | |
---|
[c8e1996] | 24 | from sas.sasgui.perspectives.fitting.basepage import BasicPage as BasicPage |
---|
| 25 | from sas.sasgui.perspectives.fitting.basepage import PageInfoEvent as \ |
---|
| 26 | PageInfoEvent |
---|
| 27 | from .basepage import ModelTextCtrl |
---|
| 28 | |
---|
[682c432] | 29 | (Chi2UpdateEvent, EVT_CHI2_UPDATE) = wx.lib.newevent.NewEvent() |
---|
| 30 | _BOX_WIDTH = 76 |
---|
| 31 | _DATA_BOX_WIDTH = 300 |
---|
| 32 | SMEAR_SIZE_H = 0.00 |
---|
[4387385] | 33 | CUSTOM_MODEL = 'Plugin Models' |
---|
[682c432] | 34 | |
---|
| 35 | class FitPage(BasicPage): |
---|
| 36 | """ |
---|
| 37 | FitPanel class contains fields allowing to display results when |
---|
| 38 | fitting a model and one data |
---|
| 39 | |
---|
| 40 | :note: For Fit to be performed the user should check at least one parameter |
---|
| 41 | on fit Panel window. |
---|
| 42 | """ |
---|
| 43 | |
---|
| 44 | def __init__(self, parent, color=None): |
---|
| 45 | """ |
---|
| 46 | Initialization of the Panel |
---|
| 47 | """ |
---|
| 48 | BasicPage.__init__(self, parent, color=color) |
---|
| 49 | |
---|
[c8e1996] | 50 | # draw sizer |
---|
[682c432] | 51 | self._fill_data_sizer() |
---|
| 52 | self.is_2D = None |
---|
| 53 | self.fit_started = False |
---|
| 54 | self.weightbt_string = None |
---|
| 55 | self.m_name = None |
---|
| 56 | # get smear info from data |
---|
| 57 | self._get_smear_info() |
---|
| 58 | self._fill_model_sizer(self.sizer1) |
---|
| 59 | self._get_defult_custom_smear() |
---|
| 60 | self._fill_range_sizer() |
---|
| 61 | self._set_smear(self.data) |
---|
| 62 | self.Bind(EVT_CHI2_UPDATE, self.on_complete_chisqr) |
---|
| 63 | # bind key event |
---|
| 64 | self.Bind(wx.EVT_RIGHT_DOWN, self.on_right_down) |
---|
| 65 | self._set_bookmark_flag(False) |
---|
| 66 | self._set_save_flag(False) |
---|
| 67 | self._set_preview_flag(False) |
---|
| 68 | self._set_copy_flag(False) |
---|
| 69 | self._set_paste_flag(False) |
---|
| 70 | self.btFit.SetFocus() |
---|
| 71 | self.enable_fit_button() |
---|
| 72 | self.fill_data_combobox(data_list=self.data_list) |
---|
[c8e1996] | 73 | # create a default data for an empty panel |
---|
[682c432] | 74 | self.create_default_data() |
---|
| 75 | self._manager.frame.Bind(wx.EVT_SET_FOCUS, self.on_set_focus) |
---|
| 76 | |
---|
| 77 | def enable_fit_button(self): |
---|
| 78 | """ |
---|
| 79 | Enable fit button if data is valid and model is valid |
---|
| 80 | """ |
---|
| 81 | flag = check_data_validity(self.data) & (self.model is not None) |
---|
| 82 | self.btFit.Enable(flag) |
---|
[bf44249e] | 83 | |
---|
[682c432] | 84 | def on_set_focus(self, event): |
---|
| 85 | """ |
---|
[bf44249e] | 86 | Override the basepage focus method to ensure the save flag is set |
---|
[682c432] | 87 | properly when focusing on the fit page. |
---|
| 88 | """ |
---|
| 89 | flag = check_data_validity(self.data) & (self.model is not None) |
---|
| 90 | self._set_save_flag(flag) |
---|
| 91 | self.parent.on_set_focus(event) |
---|
[85ccd3a] | 92 | self.on_tap_focus() |
---|
[682c432] | 93 | |
---|
| 94 | def _fill_data_sizer(self): |
---|
| 95 | """ |
---|
| 96 | fill sizer 0 with data info |
---|
| 97 | """ |
---|
[6f16e25] | 98 | self.data_box_description = wx.StaticBox(self, wx.ID_ANY, |
---|
| 99 | 'I(q) Data Source') |
---|
[682c432] | 100 | if check_data_validity(self.data): |
---|
| 101 | dname_color = wx.BLUE |
---|
| 102 | else: |
---|
| 103 | dname_color = wx.RED |
---|
| 104 | self.data_box_description.SetForegroundColour(dname_color) |
---|
| 105 | boxsizer1 = wx.StaticBoxSizer(self.data_box_description, wx.VERTICAL) |
---|
[c8e1996] | 106 | # ---------------------------------------------------------- |
---|
[682c432] | 107 | sizer_data = wx.BoxSizer(wx.HORIZONTAL) |
---|
[6f16e25] | 108 | self.dataSource = wx.ComboBox(self, wx.ID_ANY, style=wx.CB_READONLY) |
---|
| 109 | wx.EVT_COMBOBOX(self.dataSource, wx.ID_ANY, self.on_select_data) |
---|
[682c432] | 110 | self.dataSource.SetMinSize((_DATA_BOX_WIDTH, -1)) |
---|
[6f16e25] | 111 | sizer_data.Add(wx.StaticText(self, wx.ID_ANY, 'Name : ')) |
---|
[682c432] | 112 | sizer_data.Add(self.dataSource) |
---|
| 113 | sizer_data.Add((0, 5)) |
---|
| 114 | boxsizer1.Add(sizer_data, 0, wx.ALL, 10) |
---|
| 115 | self.sizer0.Add(boxsizer1, 0, wx.EXPAND | wx.ALL, 10) |
---|
| 116 | self.sizer0.Layout() |
---|
| 117 | |
---|
| 118 | def enable_datasource(self): |
---|
| 119 | """ |
---|
| 120 | Enable or disable data source control depending on existing data |
---|
| 121 | """ |
---|
| 122 | if not self.data_list: |
---|
| 123 | self.dataSource.Disable() |
---|
| 124 | else: |
---|
| 125 | self.dataSource.Enable() |
---|
| 126 | |
---|
| 127 | def fill_data_combobox(self, data_list): |
---|
| 128 | """ |
---|
| 129 | Get a list of data and fill the corresponding combobox |
---|
| 130 | """ |
---|
| 131 | self.dataSource.Clear() |
---|
| 132 | self.data_list = data_list |
---|
| 133 | self.enable_datasource() |
---|
| 134 | if len(data_list) > 0: |
---|
[c8e1996] | 135 | # find the maximum range covering all data |
---|
[682c432] | 136 | qmin, qmax, npts = self.compute_data_set_range(data_list) |
---|
| 137 | self.qmin_data_set = qmin |
---|
| 138 | self.qmax_data_set = qmax |
---|
| 139 | self.npts_data_set = npts |
---|
| 140 | |
---|
| 141 | self.qmin.SetValue(str(self.qmin_data_set)) |
---|
| 142 | self.qmax.SetValue(str(self.qmax_data_set)) |
---|
| 143 | self.qmin.SetBackgroundColour("white") |
---|
| 144 | self.qmax.SetBackgroundColour("white") |
---|
| 145 | self.qmin_x = self.qmin_data_set |
---|
| 146 | self.qmax_x = self.qmax_data_set |
---|
| 147 | self.state.qmin = self.qmin_x |
---|
| 148 | self.state.qmax = self.qmax_x |
---|
| 149 | is_data = False |
---|
| 150 | for data in self.data_list: |
---|
| 151 | if data is not None: |
---|
| 152 | self.dataSource.Append(str(data.name), clientData=data) |
---|
| 153 | if not is_data: |
---|
| 154 | is_data = check_data_validity(data) |
---|
| 155 | if is_data: |
---|
| 156 | self.dataSource.SetSelection(0) |
---|
| 157 | self.on_select_data(event=None) |
---|
| 158 | |
---|
| 159 | if len(data_list) == 1: |
---|
| 160 | self.dataSource.Disable() |
---|
| 161 | |
---|
| 162 | def on_select_data(self, event=None): |
---|
| 163 | """ |
---|
| 164 | On_select_data |
---|
| 165 | """ |
---|
[27ba79c] | 166 | if self.dataSource.GetCount() > 0: |
---|
| 167 | pos = self.dataSource.GetSelection() if event is not None else 0 |
---|
[682c432] | 168 | data = self.dataSource.GetClientData(pos) |
---|
| 169 | self.set_data(data) |
---|
| 170 | |
---|
| 171 | def _on_fit_complete(self): |
---|
| 172 | """ |
---|
| 173 | When fit is complete ,reset the fit button label. |
---|
| 174 | """ |
---|
| 175 | self.fit_started = False |
---|
| 176 | self.set_fitbutton() |
---|
| 177 | |
---|
| 178 | def _is_2D(self): |
---|
| 179 | """ |
---|
| 180 | Check if data_name is Data2D |
---|
| 181 | |
---|
| 182 | :return: True or False |
---|
| 183 | """ |
---|
| 184 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
| 185 | self.enable2D: |
---|
| 186 | return True |
---|
| 187 | return False |
---|
| 188 | |
---|
| 189 | def _fill_range_sizer(self): |
---|
| 190 | """ |
---|
[7801df8] | 191 | Fill the Fitting sizer on the fit panel which contains: the smearing |
---|
| 192 | information (dq), the weighting information (dI or other), the plotting |
---|
| 193 | range, access to the 2D mask editor, the compute, fit, and help |
---|
| 194 | buttons, xi^2, number of points etc. |
---|
[682c432] | 195 | """ |
---|
[c8e1996] | 196 | is_2d_data = False |
---|
[682c432] | 197 | |
---|
| 198 | # Check if data is 2D |
---|
| 199 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
| 200 | self.enable2D: |
---|
[c8e1996] | 201 | is_2d_data = True |
---|
[682c432] | 202 | |
---|
| 203 | title = "Fitting" |
---|
[c8e1996] | 204 | # smear messages & titles |
---|
[682c432] | 205 | smear_message_none = "No smearing is selected..." |
---|
| 206 | smear_message_dqdata = "The dQ data is being used for smearing..." |
---|
| 207 | smear_message_2d = \ |
---|
| 208 | "Higher accuracy is very time-expensive. Use it with care..." |
---|
| 209 | smear_message_new_ssmear = \ |
---|
| 210 | "Please enter only the value of interest to customize smearing..." |
---|
| 211 | smear_message_new_psmear = \ |
---|
[7e98655] | 212 | "Please enter a fixed percentage to be applied to all Q values..." |
---|
[682c432] | 213 | smear_message_2d_x_title = "<dQp>[1/A]:" |
---|
| 214 | smear_message_2d_y_title = "<dQs>[1/A]:" |
---|
[7e98655] | 215 | smear_message_pinhole_percent_title = "dQ[%]:" |
---|
[682c432] | 216 | smear_message_slit_height_title = "Slit height[1/A]:" |
---|
| 217 | smear_message_slit_width_title = "Slit width[1/A]:" |
---|
| 218 | |
---|
| 219 | self._get_smear_info() |
---|
| 220 | |
---|
[c8e1996] | 221 | # Sizers |
---|
[6f16e25] | 222 | box_description_range = wx.StaticBox(self, wx.ID_ANY, str(title)) |
---|
[682c432] | 223 | box_description_range.SetForegroundColour(wx.BLUE) |
---|
| 224 | boxsizer_range = wx.StaticBoxSizer(box_description_range, wx.VERTICAL) |
---|
| 225 | self.sizer_set_smearer = wx.BoxSizer(wx.VERTICAL) |
---|
| 226 | sizer_smearer = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 227 | self.sizer_new_smear = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 228 | self.sizer_set_masking = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 229 | sizer_chi2 = wx.BoxSizer(wx.VERTICAL) |
---|
[6f16e25] | 230 | smear_set_box = wx.StaticBox(self, wx.ID_ANY, |
---|
| 231 | 'Set Instrumental Smearing') |
---|
[682c432] | 232 | sizer_smearer_box = wx.StaticBoxSizer(smear_set_box, wx.HORIZONTAL) |
---|
| 233 | sizer_smearer_box.SetMinSize((_DATA_BOX_WIDTH, 60)) |
---|
| 234 | |
---|
[6f16e25] | 235 | weighting_set_box = wx.StaticBox(self, wx.ID_ANY, |
---|
[bf44249e] | 236 | 'Set Weighting by Selecting dI Source') |
---|
[682c432] | 237 | weighting_box = wx.StaticBoxSizer(weighting_set_box, wx.HORIZONTAL) |
---|
| 238 | sizer_weighting = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 239 | weighting_box.SetMinSize((_DATA_BOX_WIDTH, 40)) |
---|
[c8e1996] | 240 | # Filling the sizer containing weighting info. |
---|
[6f16e25] | 241 | self.dI_noweight = wx.RadioButton(self, wx.ID_ANY, |
---|
| 242 | 'No Weighting', style=wx.RB_GROUP) |
---|
| 243 | self.dI_didata = wx.RadioButton(self, wx.ID_ANY, 'Use dI Data') |
---|
| 244 | self.dI_sqrdata = wx.RadioButton(self, wx.ID_ANY, 'Use |sqrt(I Data)|') |
---|
| 245 | self.dI_idata = wx.RadioButton(self, wx.ID_ANY, 'Use |I Data|') |
---|
[682c432] | 246 | self.Bind(wx.EVT_RADIOBUTTON, self.onWeighting, |
---|
| 247 | id=self.dI_noweight.GetId()) |
---|
| 248 | self.Bind(wx.EVT_RADIOBUTTON, self.onWeighting, |
---|
| 249 | id=self.dI_didata.GetId()) |
---|
| 250 | self.Bind(wx.EVT_RADIOBUTTON, self.onWeighting, |
---|
| 251 | id=self.dI_sqrdata.GetId()) |
---|
| 252 | self.Bind(wx.EVT_RADIOBUTTON, self.onWeighting, |
---|
| 253 | id=self.dI_idata.GetId()) |
---|
[be36447] | 254 | self.dI_noweight.SetValue(True) |
---|
[682c432] | 255 | # add 4 types of weighting to the sizer |
---|
| 256 | sizer_weighting.Add(self.dI_noweight, 0, wx.LEFT, 10) |
---|
| 257 | sizer_weighting.Add((14, 10)) |
---|
| 258 | sizer_weighting.Add(self.dI_didata) |
---|
| 259 | sizer_weighting.Add((14, 10)) |
---|
| 260 | sizer_weighting.Add(self.dI_sqrdata) |
---|
| 261 | sizer_weighting.Add((14, 10)) |
---|
| 262 | sizer_weighting.Add(self.dI_idata) |
---|
| 263 | sizer_weighting.Add((10, 10)) |
---|
[be36447] | 264 | self.dI_noweight.Enable(True) |
---|
[682c432] | 265 | self.dI_didata.Enable(False) |
---|
| 266 | self.dI_sqrdata.Enable(False) |
---|
| 267 | self.dI_idata.Enable(False) |
---|
| 268 | weighting_box.Add(sizer_weighting) |
---|
| 269 | |
---|
| 270 | # combobox for smear2d accuracy selection |
---|
[6f16e25] | 271 | self.smear_accuracy = wx.ComboBox(self, wx.ID_ANY, |
---|
| 272 | size=(50, -1), style=wx.CB_READONLY) |
---|
[682c432] | 273 | self._set_accuracy_list() |
---|
| 274 | self.smear_accuracy.SetValue(self.smear2d_accuracy) |
---|
| 275 | self.smear_accuracy.SetSelection(0) |
---|
[6f16e25] | 276 | self.smear_accuracy.SetToolTipString( |
---|
[682c432] | 277 | "'Higher' uses more Gaussian points for smearing computation.") |
---|
| 278 | |
---|
[6f16e25] | 279 | wx.EVT_COMBOBOX(self.smear_accuracy, wx.ID_ANY, |
---|
| 280 | self._on_select_accuracy) |
---|
[682c432] | 281 | |
---|
[c8e1996] | 282 | # Fit button |
---|
[6f16e25] | 283 | self.btFit = wx.Button(self, self._ids.next(), 'Fit') |
---|
[682c432] | 284 | self.default_bt_colour = self.btFit.GetDefaultAttributes() |
---|
| 285 | self.btFit.Bind(wx.EVT_BUTTON, self._onFit, id=self.btFit.GetId()) |
---|
| 286 | self.btFit.SetToolTipString("Start fitting.") |
---|
| 287 | |
---|
[c8e1996] | 288 | # General Help button |
---|
[6f16e25] | 289 | self.btFitHelp = wx.Button(self, wx.ID_ANY, 'Help') |
---|
[d06c34c] | 290 | self.btFitHelp.SetToolTipString("General fitting help.") |
---|
[7801df8] | 291 | self.btFitHelp.Bind(wx.EVT_BUTTON, self._onFitHelp) |
---|
[50fcb09] | 292 | |
---|
[c8e1996] | 293 | # Resolution Smearing Help button (for now use same technique as |
---|
| 294 | # used for dI help to get tiniest possible button that works |
---|
| 295 | # both on MAC and PC. Should completely rewrite the fitting sizer |
---|
| 296 | # in future. This is minimum to get out release 3.1 |
---|
[f3dc56c] | 297 | # comment June 14, 2015 --- PDB |
---|
| 298 | if sys.platform.count("win32") > 0: |
---|
[c8e1996] | 299 | size_q = (20, 15) # on PC |
---|
[f3dc56c] | 300 | else: |
---|
[c8e1996] | 301 | size_q = (30, 20) # on MAC |
---|
[6f16e25] | 302 | self.btSmearHelp = wx.Button(self, wx.ID_ANY, '?', |
---|
| 303 | style=wx.BU_EXACTFIT, size=size_q) |
---|
[d06c34c] | 304 | self.btSmearHelp.SetToolTipString("Resolution smearing help.") |
---|
[7801df8] | 305 | self.btSmearHelp.Bind(wx.EVT_BUTTON, self._onSmearHelp) |
---|
[50fcb09] | 306 | |
---|
[c8e1996] | 307 | # textcntrl for custom resolution |
---|
[7e98655] | 308 | self.smear_pinhole_percent = ModelTextCtrl(self, wx.ID_ANY, |
---|
| 309 | size=(_BOX_WIDTH - 25, 20), |
---|
| 310 | style=wx.TE_PROCESS_ENTER, |
---|
| 311 | text_enter_callback= |
---|
| 312 | self.onPinholeSmear) |
---|
[6f16e25] | 313 | self.smear_slit_height = ModelTextCtrl(self, wx.ID_ANY, |
---|
[682c432] | 314 | size=(_BOX_WIDTH - 25, 20), |
---|
| 315 | style=wx.TE_PROCESS_ENTER, |
---|
| 316 | text_enter_callback=self.onSlitSmear) |
---|
[6f16e25] | 317 | self.smear_slit_width = ModelTextCtrl(self, wx.ID_ANY, |
---|
[682c432] | 318 | size=(_BOX_WIDTH - 25, 20), |
---|
| 319 | style=wx.TE_PROCESS_ENTER, |
---|
| 320 | text_enter_callback=self.onSlitSmear) |
---|
| 321 | |
---|
[c8e1996] | 322 | # smear |
---|
[6f16e25] | 323 | self.smear_data_left = BGTextCtrl(self, wx.ID_ANY, |
---|
[373d4ee] | 324 | size=(_BOX_WIDTH - 25, 20), style=0) |
---|
[682c432] | 325 | self.smear_data_left.SetValue(str(self.dq_l)) |
---|
[6f16e25] | 326 | self.smear_data_right = BGTextCtrl(self, wx.ID_ANY, |
---|
[373d4ee] | 327 | size=(_BOX_WIDTH - 25, 20), style=0) |
---|
[682c432] | 328 | self.smear_data_right.SetValue(str(self.dq_r)) |
---|
| 329 | |
---|
[c8e1996] | 330 | # set default values for smear |
---|
[7e98655] | 331 | self.smear_pinhole_percent.SetValue(str(self.dx_percent)) |
---|
[682c432] | 332 | self.smear_slit_height.SetValue(str(self.dxl)) |
---|
| 333 | self.smear_slit_width.SetValue(str(self.dxw)) |
---|
| 334 | |
---|
[c8e1996] | 335 | # Filling the sizer containing instruments smearing info. |
---|
[6f16e25] | 336 | self.disable_smearer = wx.RadioButton(self, wx.ID_ANY, |
---|
[682c432] | 337 | 'None', style=wx.RB_GROUP) |
---|
[6f16e25] | 338 | self.enable_smearer = wx.RadioButton(self, wx.ID_ANY, 'Use dQ Data') |
---|
[c8e1996] | 339 | # self.enable_smearer.SetToolTipString( |
---|
| 340 | # "Click to use the loaded dQ data for smearing.") |
---|
[6f16e25] | 341 | self.pinhole_smearer = wx.RadioButton(self, wx.ID_ANY, |
---|
[682c432] | 342 | 'Custom Pinhole Smear') |
---|
[c8e1996] | 343 | # self.pinhole_smearer.SetToolTipString |
---|
| 344 | # ("Click to input custom resolution for pinhole smearing.") |
---|
[6f16e25] | 345 | self.slit_smearer = wx.RadioButton(self, wx.ID_ANY, 'Custom Slit Smear') |
---|
[c8e1996] | 346 | # self.slit_smearer.SetToolTipString |
---|
| 347 | # ("Click to input custom resolution for slit smearing.") |
---|
[682c432] | 348 | self.Bind(wx.EVT_RADIOBUTTON, self.onSmear, |
---|
| 349 | id=self.disable_smearer.GetId()) |
---|
| 350 | self.Bind(wx.EVT_RADIOBUTTON, self.onSmear, |
---|
| 351 | id=self.enable_smearer.GetId()) |
---|
| 352 | self.Bind(wx.EVT_RADIOBUTTON, self.onPinholeSmear, |
---|
| 353 | id=self.pinhole_smearer.GetId()) |
---|
| 354 | self.Bind(wx.EVT_RADIOBUTTON, self.onSlitSmear, |
---|
| 355 | id=self.slit_smearer.GetId()) |
---|
[03137f1] | 356 | self.disable_smearer.SetValue(True) |
---|
[682c432] | 357 | |
---|
| 358 | sizer_smearer.Add(self.disable_smearer, 0, wx.LEFT, 10) |
---|
| 359 | sizer_smearer.Add(self.enable_smearer) |
---|
| 360 | sizer_smearer.Add(self.pinhole_smearer) |
---|
| 361 | sizer_smearer.Add(self.slit_smearer) |
---|
[f3dc56c] | 362 | sizer_smearer.Add(self.btSmearHelp) |
---|
[682c432] | 363 | sizer_smearer.Add((10, 10)) |
---|
| 364 | |
---|
| 365 | # StaticText for chi2, N(for fitting), Npts + Log/linear spacing |
---|
[6f16e25] | 366 | self.tcChi = BGTextCtrl(self, wx.ID_ANY, "-", size=(75, 20), style=0) |
---|
[682c432] | 367 | self.tcChi.SetToolTipString("Chi2/Npts(Fit)") |
---|
[6f16e25] | 368 | self.Npts_fit = BGTextCtrl(self, wx.ID_ANY, "-", size=(75, 20), style=0) |
---|
| 369 | self.Npts_fit.SetToolTipString( |
---|
[c8e1996] | 370 | " Npts : number of points selected for fitting") |
---|
[6f16e25] | 371 | self.Npts_total = ModelTextCtrl(self, wx.ID_ANY, size=(_BOX_WIDTH, 20), |
---|
[c8e1996] | 372 | style=wx.TE_PROCESS_ENTER, |
---|
| 373 | text_enter_callback=self._onQrangeEnter) |
---|
[682c432] | 374 | self.Npts_total.SetValue(format_number(self.npts_x)) |
---|
[c8e1996] | 375 | self.Npts_total.SetToolTipString( |
---|
| 376 | " Total Npts : total number of data points") |
---|
[682c432] | 377 | |
---|
| 378 | # Update and Draw button |
---|
[6f16e25] | 379 | self.draw_button = wx.Button(self, self._ids.next(), 'Compute') |
---|
| 380 | self.draw_button.Bind(wx.EVT_BUTTON, |
---|
[682c432] | 381 | self._onDraw, id=self.draw_button.GetId()) |
---|
| 382 | self.draw_button.SetToolTipString("Compute and Draw.") |
---|
| 383 | |
---|
| 384 | self.points_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
[6f16e25] | 385 | self.pointsbox = wx.CheckBox(self, wx.ID_ANY, 'Log?', (10, 10)) |
---|
[682c432] | 386 | self.pointsbox.SetValue(False) |
---|
| 387 | self.pointsbox.SetToolTipString("Check mark to use log spaced points") |
---|
| 388 | wx.EVT_CHECKBOX(self, self.pointsbox.GetId(), self.select_log) |
---|
| 389 | |
---|
[6f16e25] | 390 | self.points_sizer.Add(wx.StaticText(self, wx.ID_ANY, 'Npts ')) |
---|
[682c432] | 391 | self.points_sizer.Add(self.pointsbox) |
---|
| 392 | |
---|
[6f16e25] | 393 | box_description_1 = wx.StaticText(self, wx.ID_ANY, ' Chi2/Npts') |
---|
| 394 | box_description_2 = wx.StaticText(self, wx.ID_ANY, 'Npts(Fit)') |
---|
[682c432] | 395 | |
---|
| 396 | # StaticText for smear |
---|
[6f16e25] | 397 | self.smear_description_none = wx.StaticText(self, wx.ID_ANY, |
---|
[682c432] | 398 | smear_message_none, style=wx.ALIGN_LEFT) |
---|
[6f16e25] | 399 | self.smear_description_dqdata = wx.StaticText(self, wx.ID_ANY, |
---|
| 400 | smear_message_dqdata, style=wx.ALIGN_LEFT) |
---|
| 401 | self.smear_description_type = wx.StaticText(self, wx.ID_ANY, |
---|
| 402 | "Type:", style=wx.ALIGN_LEFT) |
---|
| 403 | self.smear_description_accuracy_type = wx.StaticText(self, wx.ID_ANY, |
---|
| 404 | "Accuracy:", style=wx.ALIGN_LEFT) |
---|
| 405 | self.smear_description_smear_type = BGTextCtrl(self, wx.ID_ANY, |
---|
[682c432] | 406 | size=(57, 20), style=0) |
---|
| 407 | self.smear_description_smear_type.SetValue(str(self.dq_l)) |
---|
| 408 | self.SetBackgroundColour(self.GetParent().GetBackgroundColour()) |
---|
[6f16e25] | 409 | self.smear_description_2d = wx.StaticText(self, wx.ID_ANY, |
---|
[682c432] | 410 | smear_message_2d, style=wx.ALIGN_LEFT) |
---|
[6f16e25] | 411 | self.smear_message_new_s = wx.StaticText(self, wx.ID_ANY, |
---|
[682c432] | 412 | smear_message_new_ssmear, style=wx.ALIGN_LEFT) |
---|
[6f16e25] | 413 | self.smear_message_new_p = wx.StaticText(self, wx.ID_ANY, |
---|
[682c432] | 414 | smear_message_new_psmear, style=wx.ALIGN_LEFT) |
---|
[6f16e25] | 415 | self.smear_description_2d_x = wx.StaticText(self, wx.ID_ANY, |
---|
[682c432] | 416 | smear_message_2d_x_title, style=wx.ALIGN_LEFT) |
---|
[6f16e25] | 417 | self.smear_description_2d_x.SetToolTipString( |
---|
[682c432] | 418 | " dQp(parallel) in q_r direction.") |
---|
[6f16e25] | 419 | self.smear_description_2d_y = wx.StaticText(self, wx.ID_ANY, |
---|
[682c432] | 420 | smear_message_2d_y_title, style=wx.ALIGN_LEFT) |
---|
[c8e1996] | 421 | self.smear_description_2d_y.SetToolTipString( |
---|
[682c432] | 422 | " dQs(perpendicular) in q_phi direction.") |
---|
[7e98655] | 423 | self.smear_description_pin_percent = wx.StaticText(self, wx.ID_ANY, |
---|
| 424 | smear_message_pinhole_percent_title, |
---|
| 425 | style=wx.ALIGN_LEFT) |
---|
[6f16e25] | 426 | self.smear_description_slit_height = wx.StaticText(self, wx.ID_ANY, |
---|
[682c432] | 427 | smear_message_slit_height_title, style=wx.ALIGN_LEFT) |
---|
[6f16e25] | 428 | self.smear_description_slit_width = wx.StaticText(self, wx.ID_ANY, |
---|
[682c432] | 429 | smear_message_slit_width_title, style=wx.ALIGN_LEFT) |
---|
| 430 | |
---|
[c8e1996] | 431 | # arrange sizers |
---|
[682c432] | 432 | self.sizer_set_smearer.Add(sizer_smearer) |
---|
| 433 | self.sizer_set_smearer.Add((10, 10)) |
---|
| 434 | self.sizer_set_smearer.Add(self.smear_description_none, |
---|
[c8e1996] | 435 | 0, wx.CENTER, 10) |
---|
[682c432] | 436 | self.sizer_set_smearer.Add(self.smear_description_dqdata, |
---|
[c8e1996] | 437 | 0, wx.CENTER, 10) |
---|
[682c432] | 438 | self.sizer_set_smearer.Add(self.smear_description_2d, |
---|
[c8e1996] | 439 | 0, wx.CENTER, 10) |
---|
[682c432] | 440 | self.sizer_new_smear.Add(self.smear_description_type, |
---|
[c8e1996] | 441 | 0, wx.CENTER, 10) |
---|
[682c432] | 442 | self.sizer_new_smear.Add(self.smear_description_accuracy_type, |
---|
[c8e1996] | 443 | 0, wx.CENTER, 10) |
---|
[682c432] | 444 | self.sizer_new_smear.Add(self.smear_accuracy) |
---|
| 445 | self.sizer_new_smear.Add(self.smear_description_smear_type, |
---|
[c8e1996] | 446 | 0, wx.CENTER, 10) |
---|
[682c432] | 447 | self.sizer_new_smear.Add((15, -1)) |
---|
[c8e1996] | 448 | self.sizer_new_smear.Add(self.smear_description_2d_x, 0, wx.CENTER, 10) |
---|
[682c432] | 449 | self.sizer_new_smear.Add(self.smear_description_slit_height, |
---|
[c8e1996] | 450 | 0, wx.CENTER, 10) |
---|
| 451 | |
---|
| 452 | self.sizer_new_smear.Add(self.smear_slit_height, 0, wx.CENTER, 10) |
---|
| 453 | self.sizer_new_smear.Add(self.smear_data_left, 0, wx.CENTER, 10) |
---|
[682c432] | 454 | self.sizer_new_smear.Add((20, -1)) |
---|
| 455 | self.sizer_new_smear.Add(self.smear_description_2d_y, |
---|
[c8e1996] | 456 | 0, wx.CENTER, 10) |
---|
[7e98655] | 457 | self.sizer_new_smear.Add(self.smear_description_pin_percent, |
---|
[c8e1996] | 458 | 0, wx.CENTER, 10) |
---|
[682c432] | 459 | self.sizer_new_smear.Add(self.smear_description_slit_width, |
---|
[c8e1996] | 460 | 0, wx.CENTER, 10) |
---|
[682c432] | 461 | |
---|
[7e98655] | 462 | self.sizer_new_smear.Add(self.smear_pinhole_percent, 0, wx.CENTER, 10) |
---|
[682c432] | 463 | self.sizer_new_smear.Add(self.smear_slit_width, 0, wx.CENTER, 10) |
---|
| 464 | self.sizer_new_smear.Add(self.smear_data_right, 0, wx.CENTER, 10) |
---|
| 465 | |
---|
| 466 | self.sizer_set_smearer.Add(self.smear_message_new_s, 0, wx.CENTER, 10) |
---|
| 467 | self.sizer_set_smearer.Add(self.smear_message_new_p, 0, wx.CENTER, 10) |
---|
| 468 | self.sizer_set_smearer.Add((5, 2)) |
---|
| 469 | self.sizer_set_smearer.Add(self.sizer_new_smear, 0, wx.CENTER, 10) |
---|
| 470 | |
---|
| 471 | # add all to chi2 sizer |
---|
| 472 | sizer_smearer_box.Add(self.sizer_set_smearer) |
---|
| 473 | sizer_chi2.Add(sizer_smearer_box) |
---|
| 474 | sizer_chi2.Add((-1, 5)) |
---|
| 475 | sizer_chi2.Add(weighting_box) |
---|
| 476 | sizer_chi2.Add((-1, 5)) |
---|
| 477 | |
---|
| 478 | # hide all smear messages and textctrl |
---|
| 479 | self._hide_all_smear_info() |
---|
| 480 | |
---|
| 481 | # get smear_selection |
---|
| 482 | self.current_smearer = smear_selection(self.data, self.model) |
---|
| 483 | |
---|
| 484 | # Show only the relevant smear messages, etc |
---|
[c8e1996] | 485 | if self.current_smearer is None: |
---|
| 486 | if not is_2d_data: |
---|
[682c432] | 487 | self.smear_description_none.Show(True) |
---|
| 488 | self.enable_smearer.Disable() |
---|
| 489 | else: |
---|
| 490 | self.smear_description_none.Show(True) |
---|
| 491 | self.slit_smearer.Disable() |
---|
[c8e1996] | 492 | if self.data is None: |
---|
[682c432] | 493 | self.slit_smearer.Disable() |
---|
| 494 | self.pinhole_smearer.Disable() |
---|
| 495 | self.enable_smearer.Disable() |
---|
| 496 | else: |
---|
| 497 | self._show_smear_sizer() |
---|
| 498 | boxsizer_range.Add(self.sizer_set_masking) |
---|
[c8e1996] | 499 | # 2D data? default |
---|
| 500 | is_2d_data = False |
---|
[682c432] | 501 | |
---|
[c8e1996] | 502 | # check if it is 2D data |
---|
[682c432] | 503 | if self.data.__class__.__name__ == "Data2D" or self.enable2D: |
---|
[c8e1996] | 504 | is_2d_data = True |
---|
[682c432] | 505 | |
---|
| 506 | self.sizer5.Clear(True) |
---|
| 507 | |
---|
[6f16e25] | 508 | self.qmin = ModelTextCtrl(self, wx.ID_ANY, size=(_BOX_WIDTH, 20), |
---|
[373d4ee] | 509 | style=wx.TE_PROCESS_ENTER, |
---|
| 510 | set_focus_callback=self.qrang_set_focus, |
---|
| 511 | text_enter_callback=self._onQrangeEnter, |
---|
| 512 | name='qmin') |
---|
[682c432] | 513 | self.qmin.SetValue(str(self.qmin_x)) |
---|
| 514 | q_tip = "Click outside of the axes\n to remove the lines." |
---|
| 515 | qmin_tip = "Minimun value of Q.\n" |
---|
| 516 | qmin_tip += q_tip |
---|
| 517 | self.qmin.SetToolTipString(qmin_tip) |
---|
| 518 | |
---|
[6f16e25] | 519 | self.qmax = ModelTextCtrl(self, wx.ID_ANY, size=(_BOX_WIDTH, 20), |
---|
[373d4ee] | 520 | style=wx.TE_PROCESS_ENTER, |
---|
| 521 | set_focus_callback=self.qrang_set_focus, |
---|
| 522 | text_enter_callback=self._onQrangeEnter, |
---|
| 523 | name='qmax') |
---|
[682c432] | 524 | self.qmax.SetValue(str(self.qmax_x)) |
---|
| 525 | qmax_tip = "Maximum value of Q.\n" |
---|
| 526 | qmax_tip += q_tip |
---|
| 527 | self.qmax.SetToolTipString(qmax_tip) |
---|
| 528 | self.qmin.Bind(wx.EVT_MOUSE_EVENTS, self.qrange_click) |
---|
| 529 | self.qmax.Bind(wx.EVT_MOUSE_EVENTS, self.qrange_click) |
---|
| 530 | self.qmin.Bind(wx.EVT_KEY_DOWN, self.on_key) |
---|
| 531 | self.qmax.Bind(wx.EVT_KEY_DOWN, self.on_key) |
---|
| 532 | self.qmin.Bind(wx.EVT_TEXT, self.on_qrange_text) |
---|
| 533 | self.qmax.Bind(wx.EVT_TEXT, self.on_qrange_text) |
---|
[6f16e25] | 534 | wx_id = self._ids.next() |
---|
[d06c34c] | 535 | self.reset_qrange = wx.Button(self, wx_id, 'Reset') |
---|
[682c432] | 536 | |
---|
[d06c34c] | 537 | self.reset_qrange.Bind(wx.EVT_BUTTON, self.on_reset_clicked, id=wx_id) |
---|
[682c432] | 538 | self.reset_qrange.SetToolTipString("Reset Q range to the default") |
---|
| 539 | |
---|
[d06c34c] | 540 | sizer = wx.GridSizer(5, 5, 2, 6) |
---|
[682c432] | 541 | |
---|
[6f16e25] | 542 | self.btEditMask = wx.Button(self, self._ids.next(), 'Editor') |
---|
[682c432] | 543 | self.btEditMask.Bind(wx.EVT_BUTTON, self._onMask, |
---|
| 544 | id=self.btEditMask.GetId()) |
---|
| 545 | self.btEditMask.SetToolTipString("Edit Mask.") |
---|
[6f16e25] | 546 | self.EditMask_title = wx.StaticText(self, wx.ID_ANY, ' Masking(2D)') |
---|
[682c432] | 547 | |
---|
[6f16e25] | 548 | sizer.Add(wx.StaticText(self, wx.ID_ANY, ' Q range')) |
---|
| 549 | sizer.Add(wx.StaticText(self, wx.ID_ANY, ' Min[1/A]')) |
---|
| 550 | sizer.Add(wx.StaticText(self, wx.ID_ANY, ' Max[1/A]')) |
---|
[682c432] | 551 | sizer.Add(self.EditMask_title) |
---|
[c8e1996] | 552 | sizer.Add((-1, 5)) |
---|
[d06c34c] | 553 | |
---|
[682c432] | 554 | sizer.Add(self.reset_qrange) |
---|
| 555 | sizer.Add(self.qmin) |
---|
| 556 | sizer.Add(self.qmax) |
---|
| 557 | sizer.Add(self.btEditMask) |
---|
[c8e1996] | 558 | sizer.Add((-1, 5)) |
---|
[d06c34c] | 559 | |
---|
[c8e1996] | 560 | sizer.AddMany(5*[(-1, 5)]) |
---|
[d06c34c] | 561 | |
---|
| 562 | sizer.Add(box_description_1, 0, 0) |
---|
| 563 | sizer.Add(box_description_2, 0, 0) |
---|
| 564 | sizer.Add(self.points_sizer, 0, 0) |
---|
| 565 | sizer.Add(self.draw_button, 0, 0) |
---|
[c8e1996] | 566 | sizer.Add((-1, 5)) |
---|
[50fcb09] | 567 | |
---|
[d06c34c] | 568 | sizer.Add(self.tcChi, 0, 0) |
---|
| 569 | sizer.Add(self.Npts_fit, 0, 0) |
---|
| 570 | sizer.Add(self.Npts_total, 0, 0) |
---|
| 571 | sizer.Add(self.btFit, 0, 0) |
---|
| 572 | sizer.Add(self.btFitHelp, 0, 0) |
---|
[50fcb09] | 573 | |
---|
[682c432] | 574 | boxsizer_range.Add(sizer_chi2) |
---|
| 575 | boxsizer_range.Add(sizer) |
---|
[c8e1996] | 576 | if is_2d_data: |
---|
[682c432] | 577 | self.btEditMask.Enable() |
---|
| 578 | self.EditMask_title.Enable() |
---|
| 579 | else: |
---|
| 580 | self.btEditMask.Disable() |
---|
| 581 | self.EditMask_title.Disable() |
---|
[c8e1996] | 582 | # save state |
---|
[682c432] | 583 | self.save_current_state() |
---|
| 584 | self.sizer5.Add(boxsizer_range, 0, wx.EXPAND | wx.ALL, 10) |
---|
| 585 | self.sizer5.Layout() |
---|
| 586 | |
---|
| 587 | def _set_sizer_dispersion(self): |
---|
| 588 | """ |
---|
| 589 | draw sizer with gaussian dispersity parameters |
---|
| 590 | """ |
---|
| 591 | self.fittable_param = [] |
---|
| 592 | self.fixed_param = [] |
---|
| 593 | self.orientation_params_disp = [] |
---|
| 594 | |
---|
| 595 | self.sizer4_4.Clear(True) |
---|
[c8e1996] | 596 | if self.model is None: |
---|
| 597 | # no model is selected |
---|
[682c432] | 598 | return |
---|
| 599 | if not self.enable_disp.GetValue(): |
---|
[c8e1996] | 600 | # the user didn't select dispersity display |
---|
[682c432] | 601 | return |
---|
| 602 | |
---|
| 603 | self._reset_dispersity() |
---|
| 604 | |
---|
[c8e1996] | 605 | # fill a sizer with the combobox to select dispersion type |
---|
[6f16e25] | 606 | model_disp = wx.StaticText(self, wx.ID_ANY, 'Function') |
---|
[4c3be25] | 607 | CHECK_STATE = False |
---|
[682c432] | 608 | |
---|
| 609 | ix = 0 |
---|
| 610 | iy = 0 |
---|
[6f16e25] | 611 | disp = wx.StaticText(self, wx.ID_ANY, ' ') |
---|
[682c432] | 612 | self.sizer4_4.Add(disp, (iy, ix), (1, 1), |
---|
| 613 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 614 | ix += 1 |
---|
[6f16e25] | 615 | values = wx.StaticText(self, wx.ID_ANY, 'PD[ratio]') |
---|
[682c432] | 616 | polytext = "Polydispersity (= STD/mean); " |
---|
| 617 | polytext += "the standard deviation over the mean value." |
---|
| 618 | values.SetToolTipString(polytext) |
---|
| 619 | |
---|
| 620 | self.sizer4_4.Add(values, (iy, ix), (1, 1), |
---|
| 621 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 622 | ix += 2 |
---|
| 623 | if self.is_mac: |
---|
| 624 | err_text = 'Error' |
---|
| 625 | else: |
---|
| 626 | err_text = '' |
---|
[6f16e25] | 627 | self.text_disp_1 = wx.StaticText(self, wx.ID_ANY, err_text) |
---|
[c8e1996] | 628 | self.sizer4_4.Add(self.text_disp_1, (iy, ix), (1, 1), |
---|
[682c432] | 629 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 630 | |
---|
| 631 | ix += 1 |
---|
[6f16e25] | 632 | self.text_disp_min = wx.StaticText(self, wx.ID_ANY, 'Min') |
---|
[c8e1996] | 633 | self.sizer4_4.Add(self.text_disp_min, (iy, ix), (1, 1), |
---|
[682c432] | 634 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 635 | |
---|
| 636 | ix += 1 |
---|
[6f16e25] | 637 | self.text_disp_max = wx.StaticText(self, wx.ID_ANY, 'Max') |
---|
[682c432] | 638 | self.sizer4_4.Add(self.text_disp_max, (iy, ix), (1, 1), |
---|
| 639 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 640 | |
---|
| 641 | ix += 1 |
---|
[6f16e25] | 642 | npts = wx.StaticText(self, wx.ID_ANY, 'Npts') |
---|
[682c432] | 643 | npts.SetToolTipString("Number of sampling points for the numerical\n\ |
---|
| 644 | integration over the distribution function.") |
---|
| 645 | self.sizer4_4.Add(npts, (iy, ix), (1, 1), |
---|
| 646 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 647 | ix += 1 |
---|
[6f16e25] | 648 | nsigmas = wx.StaticText(self, wx.ID_ANY, 'Nsigs') |
---|
[682c432] | 649 | nsigmas.SetToolTipString("Number of sigmas between which the range\n\ |
---|
| 650 | of the distribution function will be used for weighting. \n\ |
---|
| 651 | The value '3' covers 99.5% for Gaussian distribution \n\ |
---|
| 652 | function. Note: Not recommended to change this value.") |
---|
| 653 | self.sizer4_4.Add(nsigmas, (iy, ix), (1, 1), |
---|
| 654 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 655 | ix += 1 |
---|
| 656 | self.sizer4_4.Add(model_disp, (iy, ix), (1, 1), |
---|
| 657 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 658 | |
---|
| 659 | self.text_disp_max.Show(True) |
---|
| 660 | self.text_disp_min.Show(True) |
---|
| 661 | |
---|
| 662 | for item in self.model.dispersion.keys(): |
---|
| 663 | if not self.magnetic_on: |
---|
| 664 | if item in self.model.magnetic_params: |
---|
| 665 | continue |
---|
[c8e1996] | 666 | if item not in self.model.orientation_params: |
---|
| 667 | if item not in self.disp_cb_dict: |
---|
[682c432] | 668 | self.disp_cb_dict[item] = None |
---|
| 669 | name0 = "Distribution of " + item |
---|
| 670 | name1 = item + ".width" |
---|
| 671 | name2 = item + ".npts" |
---|
| 672 | name3 = item + ".nsigmas" |
---|
[c8e1996] | 673 | if name1 not in self.model.details: |
---|
[682c432] | 674 | self.model.details[name1] = ["", None, None] |
---|
| 675 | |
---|
| 676 | iy += 1 |
---|
| 677 | for p in self.model.dispersion[item].keys(): |
---|
| 678 | |
---|
| 679 | if p == "width": |
---|
| 680 | ix = 0 |
---|
[6f16e25] | 681 | cb = wx.CheckBox(self, wx.ID_ANY, name0, (10, 10)) |
---|
[682c432] | 682 | cb.SetValue(CHECK_STATE) |
---|
| 683 | cb.SetToolTipString("Check mark to fit") |
---|
| 684 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
| 685 | self.sizer4_4.Add(cb, (iy, ix), (1, 1), |
---|
| 686 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 5) |
---|
| 687 | ix = 1 |
---|
| 688 | value = self.model.getParam(name1) |
---|
[6f16e25] | 689 | ctl1 = ModelTextCtrl(self, wx.ID_ANY, |
---|
[373d4ee] | 690 | size=(_BOX_WIDTH / 1.3, 20), |
---|
| 691 | style=wx.TE_PROCESS_ENTER) |
---|
[682c432] | 692 | ctl1.SetLabel('PD[ratio]') |
---|
| 693 | poly_text = "Polydispersity (STD/mean) of %s\n" % item |
---|
| 694 | poly_text += "STD: the standard deviation" |
---|
| 695 | poly_text += " from the mean value." |
---|
| 696 | ctl1.SetToolTipString(poly_text) |
---|
| 697 | ctl1.SetValue(str(format_number(value, True))) |
---|
| 698 | self.sizer4_4.Add(ctl1, (iy, ix), (1, 1), wx.EXPAND) |
---|
[c8e1996] | 699 | # text to show error sign |
---|
[682c432] | 700 | ix = 2 |
---|
[6f16e25] | 701 | text2 = wx.StaticText(self, wx.ID_ANY, '+/-') |
---|
[682c432] | 702 | self.sizer4_4.Add(text2, (iy, ix), (1, 1), |
---|
| 703 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 704 | if not self.is_mac: |
---|
| 705 | text2.Hide() |
---|
| 706 | |
---|
| 707 | ix = 3 |
---|
[6f16e25] | 708 | ctl2 = wx.TextCtrl(self, wx.ID_ANY, |
---|
[682c432] | 709 | size=(_BOX_WIDTH / 1.3, 20), |
---|
| 710 | style=0) |
---|
| 711 | |
---|
| 712 | self.sizer4_4.Add(ctl2, (iy, ix), (1, 1), |
---|
| 713 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 714 | if not self.is_mac: |
---|
| 715 | ctl2.Hide() |
---|
| 716 | |
---|
| 717 | ix = 4 |
---|
[6f16e25] | 718 | ctl3 = ModelTextCtrl(self, wx.ID_ANY, |
---|
[373d4ee] | 719 | size=(_BOX_WIDTH / 2, 20), |
---|
| 720 | style=wx.TE_PROCESS_ENTER, |
---|
| 721 | text_enter_callback=self._onparamRangeEnter) |
---|
[682c432] | 722 | |
---|
| 723 | self.sizer4_4.Add(ctl3, (iy, ix), (1, 1), |
---|
| 724 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 725 | |
---|
| 726 | ix = 5 |
---|
[6f16e25] | 727 | ctl4 = ModelTextCtrl(self, wx.ID_ANY, |
---|
[373d4ee] | 728 | size=(_BOX_WIDTH / 2, 20), |
---|
| 729 | style=wx.TE_PROCESS_ENTER, |
---|
[682c432] | 730 | text_enter_callback=self._onparamRangeEnter) |
---|
| 731 | |
---|
| 732 | self.sizer4_4.Add(ctl4, (iy, ix), (1, 1), |
---|
| 733 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 734 | |
---|
| 735 | ctl3.Show(True) |
---|
| 736 | ctl4.Show(True) |
---|
| 737 | |
---|
| 738 | elif p == "npts": |
---|
| 739 | ix = 6 |
---|
| 740 | value = self.model.getParam(name2) |
---|
[6f16e25] | 741 | Tctl = ModelTextCtrl(self, wx.ID_ANY, |
---|
[373d4ee] | 742 | size=(_BOX_WIDTH / 2.2, 20), |
---|
| 743 | style=wx.TE_PROCESS_ENTER) |
---|
[682c432] | 744 | |
---|
| 745 | Tctl.SetValue(str(format_number(value))) |
---|
| 746 | self.sizer4_4.Add(Tctl, (iy, ix), (1, 1), |
---|
| 747 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 748 | self.fixed_param.append([None, name2, Tctl, None, None, |
---|
| 749 | None, None, None]) |
---|
| 750 | elif p == "nsigmas": |
---|
| 751 | ix = 7 |
---|
| 752 | value = self.model.getParam(name3) |
---|
[6f16e25] | 753 | Tct2 = ModelTextCtrl(self, wx.ID_ANY, |
---|
[373d4ee] | 754 | size=(_BOX_WIDTH / 2.2, 20), |
---|
| 755 | style=wx.TE_PROCESS_ENTER) |
---|
[682c432] | 756 | |
---|
| 757 | Tct2.SetValue(str(format_number(value))) |
---|
| 758 | self.sizer4_4.Add(Tct2, (iy, ix), (1, 1), |
---|
[c8e1996] | 759 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
[682c432] | 760 | self.fixed_param.append([None, name3, Tct2, |
---|
| 761 | None, None, None, |
---|
| 762 | None, None]) |
---|
| 763 | |
---|
| 764 | ix = 8 |
---|
[6f16e25] | 765 | disp_box = wx.ComboBox(self, wx.ID_ANY, size=(65, -1), |
---|
[682c432] | 766 | style=wx.CB_READONLY, name='%s' % name1) |
---|
[cb4ef58] | 767 | for key, value in POLYDISPERSITY_MODELS.iteritems(): |
---|
[682c432] | 768 | name_disp = str(key) |
---|
| 769 | disp_box.Append(name_disp, value) |
---|
| 770 | disp_box.SetStringSelection("gaussian") |
---|
[6f16e25] | 771 | wx.EVT_COMBOBOX(disp_box, wx.ID_ANY, self._on_disp_func) |
---|
[682c432] | 772 | self.sizer4_4.Add(disp_box, (iy, ix), (1, 1), wx.EXPAND) |
---|
| 773 | self.fittable_param.append([cb, name1, ctl1, text2, |
---|
| 774 | ctl2, ctl3, ctl4, disp_box]) |
---|
| 775 | |
---|
| 776 | ix = 0 |
---|
| 777 | iy += 1 |
---|
| 778 | self.sizer4_4.Add((20, 20), (iy, ix), (1, 1), |
---|
| 779 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 780 | first_orient = True |
---|
| 781 | for item in self.model.dispersion.keys(): |
---|
| 782 | if not self.magnetic_on: |
---|
| 783 | if item in self.model.magnetic_params: |
---|
| 784 | continue |
---|
[c8e1996] | 785 | if item in self.model.orientation_params: |
---|
| 786 | if item not in self.disp_cb_dict: |
---|
[682c432] | 787 | self.disp_cb_dict[item] = None |
---|
| 788 | name0 = "Distribution of " + item |
---|
| 789 | name1 = item + ".width" |
---|
| 790 | name2 = item + ".npts" |
---|
| 791 | name3 = item + ".nsigmas" |
---|
| 792 | |
---|
[c8e1996] | 793 | if name1 not in self.model.details: |
---|
[682c432] | 794 | self.model.details[name1] = ["", None, None] |
---|
| 795 | |
---|
| 796 | iy += 1 |
---|
| 797 | for p in self.model.dispersion[item].keys(): |
---|
| 798 | |
---|
| 799 | if p == "width": |
---|
| 800 | ix = 0 |
---|
[6f16e25] | 801 | cb = wx.CheckBox(self, wx.ID_ANY, name0, (10, 10)) |
---|
[682c432] | 802 | cb.SetValue(CHECK_STATE) |
---|
| 803 | cb.SetToolTipString("Check mark to fit") |
---|
| 804 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
| 805 | self.sizer4_4.Add(cb, (iy, ix), (1, 1), |
---|
| 806 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 5) |
---|
| 807 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
| 808 | self.enable2D: |
---|
| 809 | cb.Show(True) |
---|
| 810 | elif cb.IsShown(): |
---|
| 811 | cb.Hide() |
---|
| 812 | ix = 1 |
---|
| 813 | value = self.model.getParam(name1) |
---|
[6f16e25] | 814 | ctl1 = ModelTextCtrl(self, wx.ID_ANY, |
---|
[373d4ee] | 815 | size=(_BOX_WIDTH / 1.3, 20), |
---|
| 816 | style=wx.TE_PROCESS_ENTER) |
---|
[682c432] | 817 | poly_tip = "Absolute Sigma for %s." % item |
---|
| 818 | ctl1.SetToolTipString(poly_tip) |
---|
| 819 | ctl1.SetValue(str(format_number(value, True))) |
---|
| 820 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
| 821 | self.enable2D: |
---|
| 822 | if first_orient: |
---|
| 823 | values.SetLabel('PD[ratio], Sig[deg]') |
---|
| 824 | poly_text = "PD(polydispersity for lengths):\n" |
---|
| 825 | poly_text += "It should be a value between" |
---|
| 826 | poly_text += "0 and 1\n" |
---|
| 827 | poly_text += "Sigma for angles: \n" |
---|
| 828 | poly_text += "It is the STD (ratio*mean)" |
---|
| 829 | poly_text += " of the distribution.\n " |
---|
| 830 | |
---|
| 831 | values.SetToolTipString(poly_text) |
---|
| 832 | first_orient = False |
---|
| 833 | ctl1.Show(True) |
---|
| 834 | elif ctl1.IsShown(): |
---|
| 835 | ctl1.Hide() |
---|
| 836 | |
---|
| 837 | self.sizer4_4.Add(ctl1, (iy, ix), (1, 1), wx.EXPAND) |
---|
[c8e1996] | 838 | # text to show error sign |
---|
[682c432] | 839 | ix = 2 |
---|
[6f16e25] | 840 | text2 = wx.StaticText(self, wx.ID_ANY, '+/-') |
---|
[682c432] | 841 | self.sizer4_4.Add(text2, (iy, ix), (1, 1), |
---|
| 842 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 843 | |
---|
| 844 | text2.Hide() |
---|
| 845 | |
---|
| 846 | ix = 3 |
---|
[6f16e25] | 847 | ctl2 = wx.TextCtrl(self, wx.ID_ANY, |
---|
[682c432] | 848 | size=(_BOX_WIDTH / 1.3, 20), |
---|
| 849 | style=0) |
---|
| 850 | |
---|
| 851 | self.sizer4_4.Add(ctl2, (iy, ix), (1, 1), |
---|
| 852 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 853 | |
---|
| 854 | ctl2.Hide() |
---|
| 855 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
| 856 | self.enable2D: |
---|
| 857 | if self.is_mac: |
---|
| 858 | text2.Show(True) |
---|
| 859 | ctl2.Show(True) |
---|
| 860 | |
---|
| 861 | ix = 4 |
---|
[6f16e25] | 862 | ctl3 = ModelTextCtrl(self, wx.ID_ANY, |
---|
[373d4ee] | 863 | size=(_BOX_WIDTH / 2, 20), |
---|
| 864 | style=wx.TE_PROCESS_ENTER, |
---|
[682c432] | 865 | text_enter_callback=self._onparamRangeEnter) |
---|
| 866 | |
---|
| 867 | self.sizer4_4.Add(ctl3, (iy, ix), (1, 1), |
---|
| 868 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 869 | |
---|
| 870 | ctl3.Hide() |
---|
| 871 | |
---|
| 872 | ix = 5 |
---|
[6f16e25] | 873 | ctl4 = ModelTextCtrl(self, wx.ID_ANY, |
---|
[373d4ee] | 874 | size=(_BOX_WIDTH / 2, 20), |
---|
| 875 | style=wx.TE_PROCESS_ENTER, |
---|
[682c432] | 876 | text_enter_callback=self._onparamRangeEnter) |
---|
| 877 | self.sizer4_4.Add(ctl4, (iy, ix), (1, 1), |
---|
| 878 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 879 | ctl4.Hide() |
---|
| 880 | |
---|
| 881 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
| 882 | self.enable2D: |
---|
| 883 | ctl3.Show(True) |
---|
| 884 | ctl4.Show(True) |
---|
| 885 | |
---|
| 886 | elif p == "npts": |
---|
| 887 | ix = 6 |
---|
| 888 | value = self.model.getParam(name2) |
---|
[6f16e25] | 889 | Tctl = ModelTextCtrl(self, wx.ID_ANY, |
---|
[373d4ee] | 890 | size=(_BOX_WIDTH / 2.2, 20), |
---|
| 891 | style=wx.TE_PROCESS_ENTER) |
---|
[682c432] | 892 | |
---|
| 893 | Tctl.SetValue(str(format_number(value))) |
---|
| 894 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
| 895 | self.enable2D: |
---|
| 896 | Tctl.Show(True) |
---|
| 897 | else: |
---|
| 898 | Tctl.Hide() |
---|
| 899 | self.sizer4_4.Add(Tctl, (iy, ix), (1, 1), |
---|
| 900 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 901 | self.fixed_param.append([None, name2, Tctl, None, None, |
---|
| 902 | None, None, None]) |
---|
| 903 | self.orientation_params_disp.append([None, name2, |
---|
| 904 | Tctl, None, None, |
---|
| 905 | None, None, None]) |
---|
| 906 | elif p == "nsigmas": |
---|
| 907 | ix = 7 |
---|
| 908 | value = self.model.getParam(name3) |
---|
[6f16e25] | 909 | Tct2 = ModelTextCtrl(self, wx.ID_ANY, |
---|
[373d4ee] | 910 | size=(_BOX_WIDTH / 2.2, 20), |
---|
| 911 | style=wx.TE_PROCESS_ENTER) |
---|
[682c432] | 912 | |
---|
| 913 | Tct2.SetValue(str(format_number(value))) |
---|
| 914 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
| 915 | self.enable2D: |
---|
| 916 | Tct2.Show(True) |
---|
| 917 | else: |
---|
| 918 | Tct2.Hide() |
---|
| 919 | self.sizer4_4.Add(Tct2, (iy, ix), (1, 1), |
---|
| 920 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 921 | |
---|
| 922 | self.fixed_param.append([None, name3, Tct2, |
---|
| 923 | None, None, None, None, None]) |
---|
| 924 | |
---|
| 925 | self.orientation_params_disp.append([None, name3, |
---|
| 926 | Tct2, None, None, None, None, None]) |
---|
| 927 | |
---|
| 928 | ix = 8 |
---|
[6f16e25] | 929 | disp_box = wx.ComboBox(self, wx.ID_ANY, size=(65, -1), |
---|
[682c432] | 930 | style=wx.CB_READONLY, name='%s' % name1) |
---|
[cb4ef58] | 931 | for key, value in POLYDISPERSITY_MODELS.iteritems(): |
---|
[682c432] | 932 | name_disp = str(key) |
---|
| 933 | disp_box.Append(name_disp, value) |
---|
| 934 | disp_box.SetStringSelection("gaussian") |
---|
[6f16e25] | 935 | wx.EVT_COMBOBOX(disp_box, wx.ID_ANY, self._on_disp_func) |
---|
[682c432] | 936 | self.sizer4_4.Add(disp_box, (iy, ix), (1, 1), wx.EXPAND) |
---|
| 937 | self.fittable_param.append([cb, name1, ctl1, text2, |
---|
| 938 | ctl2, ctl3, ctl4, disp_box]) |
---|
| 939 | self.orientation_params_disp.append([cb, name1, ctl1, |
---|
| 940 | text2, ctl2, ctl3, ctl4, disp_box]) |
---|
| 941 | |
---|
| 942 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
| 943 | self.enable2D: |
---|
| 944 | disp_box.Show(True) |
---|
| 945 | else: |
---|
| 946 | disp_box.Hide() |
---|
| 947 | |
---|
| 948 | self.state.disp_cb_dict = copy.deepcopy(self.disp_cb_dict) |
---|
| 949 | |
---|
| 950 | self.state.model = self.model.clone() |
---|
[c8e1996] | 951 | # save state into |
---|
[682c432] | 952 | self._copy_parameters_state(self.parameters, self.state.parameters) |
---|
| 953 | self._copy_parameters_state(self.orientation_params_disp, |
---|
| 954 | self.state.orientation_params_disp) |
---|
| 955 | self._copy_parameters_state(self.fittable_param, |
---|
| 956 | self.state.fittable_param) |
---|
| 957 | self._copy_parameters_state(self.fixed_param, self.state.fixed_param) |
---|
| 958 | |
---|
| 959 | wx.PostEvent(self.parent, |
---|
| 960 | StatusEvent(status=" Selected Distribution: Gaussian")) |
---|
[c8e1996] | 961 | # Fill the list of fittable parameters |
---|
[682c432] | 962 | self.get_all_checked_params() |
---|
| 963 | self.Layout() |
---|
| 964 | |
---|
| 965 | def _onDraw(self, event): |
---|
| 966 | """ |
---|
| 967 | Update and Draw the model |
---|
| 968 | """ |
---|
[c8e1996] | 969 | if self.model is None: |
---|
[682c432] | 970 | msg = "Please select a Model first..." |
---|
| 971 | wx.MessageBox(msg, 'Info') |
---|
| 972 | return |
---|
| 973 | """ |
---|
| 974 | if not self.data.is_data: |
---|
| 975 | self.npts_x = self.Npts_total.GetValue() |
---|
| 976 | self.Npts_fit.SetValue(self.npts_x) |
---|
| 977 | self.create_default_data() |
---|
| 978 | """ |
---|
[c8e1996] | 979 | flag, is_modified = self._update_paramv_on_fit() |
---|
[682c432] | 980 | |
---|
[c8e1996] | 981 | wx.CallAfter(self._onparamEnter_helper, is_modified) |
---|
[682c432] | 982 | if not flag: |
---|
| 983 | msg = "The parameters are invalid" |
---|
| 984 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
| 985 | return |
---|
| 986 | |
---|
| 987 | def _onFit(self, event): |
---|
| 988 | """ |
---|
| 989 | Allow to fit |
---|
| 990 | """ |
---|
[c8e1996] | 991 | if event is not None: |
---|
[682c432] | 992 | event.Skip() |
---|
| 993 | if self.fit_started: |
---|
| 994 | self._StopFit() |
---|
| 995 | self.fit_started = False |
---|
| 996 | wx.CallAfter(self.set_fitbutton) |
---|
| 997 | return |
---|
| 998 | |
---|
| 999 | if self.data is None: |
---|
| 1000 | msg = "Please get Data first..." |
---|
| 1001 | wx.MessageBox(msg, 'Info') |
---|
| 1002 | wx.PostEvent(self._manager.parent, |
---|
| 1003 | StatusEvent(status="Fit: %s" % msg)) |
---|
| 1004 | return |
---|
| 1005 | if self.model is None: |
---|
| 1006 | msg = "Please select a Model first..." |
---|
| 1007 | wx.MessageBox(msg, 'Info') |
---|
| 1008 | wx.PostEvent(self._manager.parent, |
---|
| 1009 | StatusEvent(status="Fit: %s" % msg, type="stop")) |
---|
| 1010 | return |
---|
| 1011 | |
---|
| 1012 | if len(self.param_toFit) <= 0: |
---|
| 1013 | msg = "Select at least one parameter to fit" |
---|
| 1014 | wx.MessageBox(msg, 'Info') |
---|
| 1015 | wx.PostEvent(self._manager.parent, |
---|
| 1016 | StatusEvent(status=msg, type="stop")) |
---|
| 1017 | return |
---|
| 1018 | |
---|
| 1019 | flag = self._update_paramv_on_fit() |
---|
| 1020 | |
---|
| 1021 | if self.batch_on and not self._is_2D(): |
---|
| 1022 | if not self._validate_Npts_1D(): |
---|
| 1023 | return |
---|
| 1024 | |
---|
| 1025 | if not flag: |
---|
| 1026 | msg = "Fitting range or parameters are invalid" |
---|
| 1027 | wx.PostEvent(self._manager.parent, |
---|
| 1028 | StatusEvent(status=msg, type="stop")) |
---|
| 1029 | return |
---|
| 1030 | |
---|
[c8e1996] | 1031 | self.select_param() |
---|
[682c432] | 1032 | |
---|
| 1033 | # Remove or do not allow fitting on the Q=0 point, especially |
---|
| 1034 | # when y(q=0)=None at x[0]. |
---|
| 1035 | self.qmin_x = float(self.qmin.GetValue()) |
---|
| 1036 | self.qmax_x = float(self.qmax.GetValue()) |
---|
| 1037 | self._manager._reset_schedule_problem(value=0, uid=self.uid) |
---|
| 1038 | self._manager.schedule_for_fit(uid=self.uid, value=1) |
---|
| 1039 | self._manager.set_fit_range(uid=self.uid, qmin=self.qmin_x, |
---|
| 1040 | qmax=self.qmax_x) |
---|
| 1041 | |
---|
[c8e1996] | 1042 | # single fit |
---|
| 1043 | # self._manager.onFit(uid=self.uid) |
---|
[682c432] | 1044 | self.fit_started = self._manager.onFit(uid=self.uid) |
---|
| 1045 | wx.CallAfter(self.set_fitbutton) |
---|
| 1046 | |
---|
[7801df8] | 1047 | def _onFitHelp(self, event): |
---|
| 1048 | """ |
---|
| 1049 | Bring up the Full Fitting Documentation whenever the HELP button is |
---|
| 1050 | clicked. |
---|
| 1051 | |
---|
| 1052 | Calls DocumentationWindow with the path of the location within the |
---|
| 1053 | documentation tree (after /doc/ ....". Note that when using old |
---|
| 1054 | versions of Wx (before 2.9) and thus not the release version of |
---|
| 1055 | installers, the help comes up at the top level of the file as |
---|
[c8e1996] | 1056 | web browser does not pass anything past the # to the browser when it is |
---|
[7801df8] | 1057 | running "file:///...." |
---|
| 1058 | |
---|
[c8e1996] | 1059 | :param evt: Triggers on clicking the help button |
---|
| 1060 | """ |
---|
[7801df8] | 1061 | |
---|
[f27dd1e] | 1062 | _TreeLocation = "user/sasgui/perspectives/fitting/fitting_help.html" |
---|
[6f16e25] | 1063 | _doc_viewer = DocumentationWindow(self, wx.ID_ANY, _TreeLocation, "", |
---|
[7801df8] | 1064 | "General Fitting Help") |
---|
| 1065 | |
---|
| 1066 | def _onSmearHelp(self, event): |
---|
| 1067 | """ |
---|
| 1068 | Bring up the instrumental resolution smearing Documentation whenever |
---|
| 1069 | the ? button in the smearing box is clicked. |
---|
| 1070 | |
---|
| 1071 | Calls DocumentationWindow with the path of the location within the |
---|
| 1072 | documentation tree (after /doc/ ....". Note that when using old |
---|
| 1073 | versions of Wx (before 2.9) and thus not the release version of |
---|
| 1074 | installers, the help comes up at the top level of the file as |
---|
[c8e1996] | 1075 | web browser does not pass anything past the # to the browser when it is |
---|
[7801df8] | 1076 | running "file:///...." |
---|
| 1077 | |
---|
[c8e1996] | 1078 | :param evt: Triggers on clicking the help button |
---|
| 1079 | """ |
---|
[7801df8] | 1080 | |
---|
[f27dd1e] | 1081 | _TreeLocation = "user/sasgui/perspectives/fitting/sm_help.html" |
---|
[6f16e25] | 1082 | _doc_viewer = DocumentationWindow(self, wx.ID_ANY, _TreeLocation, "", |
---|
[7801df8] | 1083 | "Instrumental Resolution Smearing \ |
---|
| 1084 | Help") |
---|
| 1085 | |
---|
[682c432] | 1086 | def set_fitbutton(self): |
---|
| 1087 | """ |
---|
| 1088 | Set fit button label depending on the fit_started[bool] |
---|
| 1089 | """ |
---|
| 1090 | # Skip this feature if we are not on Windows |
---|
[c8e1996] | 1091 | # NOTE: the is_mac data member actually means "is no Windows". |
---|
[682c432] | 1092 | if self.is_mac: |
---|
| 1093 | return |
---|
| 1094 | |
---|
| 1095 | if self.fit_started: |
---|
| 1096 | label = "Stop" |
---|
| 1097 | color = "red" |
---|
| 1098 | else: |
---|
| 1099 | label = "Fit" |
---|
| 1100 | color = "black" |
---|
[c8e1996] | 1101 | # self.btFit.Enable(False) |
---|
[682c432] | 1102 | self.btFit.SetLabel(label) |
---|
| 1103 | self.btFit.SetForegroundColour(color) |
---|
| 1104 | self.btFit.Enable(True) |
---|
| 1105 | |
---|
| 1106 | def get_weight_flag(self): |
---|
| 1107 | """ |
---|
| 1108 | Get flag corresponding to a given weighting dI data. |
---|
| 1109 | """ |
---|
| 1110 | button_list = [self.dI_noweight, |
---|
| 1111 | self.dI_didata, |
---|
| 1112 | self.dI_sqrdata, |
---|
| 1113 | self.dI_idata] |
---|
| 1114 | flag = 1 |
---|
| 1115 | for item in button_list: |
---|
| 1116 | if item.GetValue(): |
---|
| 1117 | if button_list.index(item) == 0: |
---|
[9a5097c] | 1118 | flag = 0 # dy = np.ones_like(dy_data) |
---|
[682c432] | 1119 | elif button_list.index(item) == 1: |
---|
| 1120 | flag = 1 # dy = dy_data |
---|
| 1121 | elif button_list.index(item) == 2: |
---|
[9a5097c] | 1122 | flag = 2 # dy = np.sqrt(np.abs(data)) |
---|
[682c432] | 1123 | elif button_list.index(item) == 3: |
---|
[9a5097c] | 1124 | flag = 3 # dy = np.abs(data) |
---|
[682c432] | 1125 | break |
---|
| 1126 | return flag |
---|
| 1127 | |
---|
| 1128 | def _StopFit(self, event=None): |
---|
| 1129 | """ |
---|
| 1130 | Stop fit |
---|
| 1131 | """ |
---|
[c8e1996] | 1132 | if event is not None: |
---|
[682c432] | 1133 | event.Skip() |
---|
| 1134 | self._manager.stop_fit(self.uid) |
---|
| 1135 | self._manager._reset_schedule_problem(value=0) |
---|
| 1136 | self._on_fit_complete() |
---|
| 1137 | |
---|
| 1138 | def rename_model(self): |
---|
| 1139 | """ |
---|
| 1140 | find a short name for model |
---|
| 1141 | """ |
---|
| 1142 | if self.model is not None: |
---|
| 1143 | self.model.name = "M" + str(self.index_model) |
---|
| 1144 | |
---|
[277257f] | 1145 | def _on_select_model(self, event=None, keep_pars=False): |
---|
[682c432] | 1146 | """ |
---|
| 1147 | call back for model selection |
---|
| 1148 | """ |
---|
| 1149 | self.Show(False) |
---|
[c8e1996] | 1150 | if event is not None: |
---|
[277257f] | 1151 | control = event.GetEventObject() |
---|
| 1152 | if ((control == self.formfactorbox |
---|
| 1153 | and self.structurebox.GetLabel() != 'None') |
---|
| 1154 | or control == self.structurebox |
---|
| 1155 | or control == self.multifactorbox): |
---|
| 1156 | keep_pars = True |
---|
| 1157 | |
---|
| 1158 | if keep_pars: |
---|
| 1159 | saved_pars = self.get_copy_params() |
---|
| 1160 | is_poly_enabled = self.enable_disp.GetValue() |
---|
| 1161 | else: |
---|
| 1162 | saved_pars = None |
---|
| 1163 | is_poly_enabled = None |
---|
[682c432] | 1164 | |
---|
[79e6a33] | 1165 | try: |
---|
| 1166 | self._on_select_model_helper() |
---|
| 1167 | except Exception as e: |
---|
| 1168 | evt = StatusEvent(status=e.message, info="error") |
---|
| 1169 | wx.PostEvent(self._manager.parent, evt) |
---|
| 1170 | # Set S(Q) to None |
---|
| 1171 | self.structurebox.SetSelection(0) |
---|
| 1172 | self._on_select_model() |
---|
| 1173 | return |
---|
[682c432] | 1174 | self.set_model_param_sizer(self.model) |
---|
| 1175 | if self.model is None: |
---|
| 1176 | self._set_bookmark_flag(False) |
---|
| 1177 | self._keep.Enable(False) |
---|
| 1178 | self._set_save_flag(False) |
---|
| 1179 | self.enable_disp.SetValue(False) |
---|
| 1180 | self.disable_disp.SetValue(True) |
---|
[6f16e25] | 1181 | # TODO: should not have an untrapped exception when displaying disperser |
---|
| 1182 | # TODO: do we need to create the disperser panel on every model change? |
---|
| 1183 | # Note: if we fix this, then remove ID_DISPERSER_HELP from basepage |
---|
[682c432] | 1184 | try: |
---|
| 1185 | self.set_dispers_sizer() |
---|
[277257f] | 1186 | except Exception: |
---|
[682c432] | 1187 | pass |
---|
| 1188 | self.state.enable_disp = self.enable_disp.GetValue() |
---|
| 1189 | self.state.disable_disp = self.disable_disp.GetValue() |
---|
| 1190 | self.state.pinhole_smearer = self.pinhole_smearer.GetValue() |
---|
| 1191 | self.state.slit_smearer = self.slit_smearer.GetValue() |
---|
| 1192 | |
---|
[77910cf] | 1193 | self.state.structurecombobox = self.structurebox.GetValue() |
---|
| 1194 | self.state.formfactorcombobox = self.formfactorbox.GetValue() |
---|
| 1195 | self.state.categorycombobox = self.categorybox.GetValue() |
---|
[682c432] | 1196 | self.enable_fit_button() |
---|
[373d4ee] | 1197 | if self.model is not None: |
---|
[682c432] | 1198 | self.m_name = self.model.name |
---|
| 1199 | self.state.m_name = self.m_name |
---|
| 1200 | self.rename_model() |
---|
| 1201 | self._set_copy_flag(True) |
---|
| 1202 | self._set_paste_flag(True) |
---|
| 1203 | if self.data is not None: |
---|
| 1204 | is_data = check_data_validity(self.data) |
---|
| 1205 | if is_data: |
---|
| 1206 | self._set_bookmark_flag(not self.batch_on) |
---|
| 1207 | self._keep.Enable(not self.batch_on) |
---|
| 1208 | self._set_save_flag(True) |
---|
[e0d9ed2] | 1209 | #Setting smearing for cases with and without data. |
---|
| 1210 | self._set_smear(self.data) |
---|
[682c432] | 1211 | |
---|
| 1212 | # more disables for 2D |
---|
| 1213 | self._set_smear_buttons() |
---|
| 1214 | |
---|
| 1215 | try: |
---|
| 1216 | # update smearer sizer |
---|
[e0d9ed2] | 1217 | #This call for smearing set up caused double evaluation of |
---|
| 1218 | #I(q) and double compilation as results |
---|
| 1219 | #self.onSmear(None) |
---|
[682c432] | 1220 | temp_smear = None |
---|
| 1221 | if not self.disable_smearer.GetValue(): |
---|
| 1222 | # Set the smearer environments |
---|
| 1223 | temp_smear = self.current_smearer |
---|
| 1224 | except: |
---|
| 1225 | raise |
---|
[c8e1996] | 1226 | # error occured on chisqr computation |
---|
| 1227 | # pass |
---|
| 1228 | # event to post model to fit to fitting plugins |
---|
[682c432] | 1229 | (ModelEventbox, EVT_MODEL_BOX) = wx.lib.newevent.NewEvent() |
---|
| 1230 | |
---|
[c8e1996] | 1231 | # set smearing value whether or not data contain the smearing info |
---|
[682c432] | 1232 | evt = ModelEventbox(model=self.model, |
---|
[bf44249e] | 1233 | smearer=temp_smear, |
---|
| 1234 | enable_smearer=not self.disable_smearer.GetValue(), |
---|
| 1235 | qmin=float(self.qmin_x), |
---|
| 1236 | uid=self.uid, |
---|
| 1237 | caption=self.window_caption, |
---|
| 1238 | qmax=float(self.qmax_x)) |
---|
[682c432] | 1239 | |
---|
| 1240 | self._manager._on_model_panel(evt=evt) |
---|
[c8e1996] | 1241 | self.mbox_description.SetLabel("Model [ %s ]" % |
---|
| 1242 | str(self.model.name)) |
---|
[682c432] | 1243 | self.mbox_description.SetForegroundColour(wx.BLUE) |
---|
| 1244 | self.state.model = self.model.clone() |
---|
| 1245 | self.state.model.name = self.model.name |
---|
| 1246 | |
---|
[277257f] | 1247 | # when select a model only from guictr/button |
---|
| 1248 | if is_poly_enabled is not None: |
---|
| 1249 | self.enable_disp.SetValue(is_poly_enabled) |
---|
| 1250 | self.disable_disp.SetValue(not is_poly_enabled) |
---|
| 1251 | self._set_dipers_Param(event=None) |
---|
| 1252 | self.state.enable_disp = self.enable_disp.GetValue() |
---|
| 1253 | self.state.disable_disp = self.disable_disp.GetValue() |
---|
| 1254 | |
---|
| 1255 | # Keep the previous param values |
---|
| 1256 | if saved_pars: |
---|
| 1257 | self.get_paste_params(saved_pars) |
---|
| 1258 | |
---|
[c8e1996] | 1259 | if event is not None: |
---|
[277257f] | 1260 | # update list of plugins if new plugin is available |
---|
| 1261 | # mod_cat = self.categorybox.GetStringSelection() |
---|
| 1262 | # if mod_cat == CUSTOM_MODEL: |
---|
| 1263 | # temp = self.parent.update_model_list() |
---|
[69363c7] | 1264 | # for v in self.parent.model_dictionary.values(): |
---|
| 1265 | # if v.id == self.model.id: |
---|
| 1266 | # self.model = v() |
---|
| 1267 | # break |
---|
[277257f] | 1268 | # if temp: |
---|
| 1269 | # self.model_list_box = temp |
---|
| 1270 | # current_val = self.formfactorbox.GetLabel() |
---|
| 1271 | # pos = self.formfactorbox.GetSelection() |
---|
| 1272 | # self._show_combox_helper() |
---|
| 1273 | # self.formfactorbox.SetStringSelection(current_val) |
---|
| 1274 | # self.formfactorbox.SetValue(current_val) |
---|
[c8e1996] | 1275 | # post state to fit panel |
---|
[682c432] | 1276 | new_event = PageInfoEvent(page=self) |
---|
| 1277 | wx.PostEvent(self.parent, new_event) |
---|
[fdaf2b1] | 1278 | wx.CallAfter(self._onDraw, None) |
---|
[682c432] | 1279 | |
---|
| 1280 | else: |
---|
| 1281 | self._draw_model() |
---|
| 1282 | |
---|
| 1283 | if self.batch_on: |
---|
| 1284 | self.slit_smearer.Enable(False) |
---|
| 1285 | self.pinhole_smearer.Enable(False) |
---|
| 1286 | self.btEditMask.Disable() |
---|
| 1287 | self.EditMask_title.Disable() |
---|
| 1288 | |
---|
| 1289 | self.Show(True) |
---|
| 1290 | self.SetupScrolling() |
---|
| 1291 | |
---|
| 1292 | def _onparamEnter(self, event): |
---|
| 1293 | """ |
---|
| 1294 | when enter value on panel redraw model according to changed |
---|
| 1295 | """ |
---|
[c8e1996] | 1296 | if self.model is None: |
---|
[682c432] | 1297 | msg = "Please select a Model first..." |
---|
| 1298 | wx.MessageBox(msg, 'Info') |
---|
| 1299 | return |
---|
| 1300 | |
---|
[c8e1996] | 1301 | # default flag |
---|
[682c432] | 1302 | flag = False |
---|
| 1303 | self.fitrange = True |
---|
[c8e1996] | 1304 | # get event object |
---|
[682c432] | 1305 | tcrtl = event.GetEventObject() |
---|
[c8e1996] | 1306 | # Clear msg if previously shown. |
---|
[682c432] | 1307 | msg = "" |
---|
| 1308 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
| 1309 | |
---|
| 1310 | if check_float(tcrtl): |
---|
| 1311 | flag = self._onparamEnter_helper() |
---|
| 1312 | self.show_npts2fit() |
---|
| 1313 | if self.fitrange: |
---|
| 1314 | temp_smearer = None |
---|
| 1315 | if not self.disable_smearer.GetValue(): |
---|
| 1316 | temp_smearer = self.current_smearer |
---|
[c8e1996] | 1317 | # set smearing value whether or not data contain the |
---|
| 1318 | # smearing info |
---|
[682c432] | 1319 | if self.slit_smearer.GetValue(): |
---|
| 1320 | flag1 = self.update_slit_smear() |
---|
| 1321 | flag = flag or flag1 |
---|
| 1322 | elif self.pinhole_smearer.GetValue(): |
---|
| 1323 | flag1 = self.update_pinhole_smear() |
---|
| 1324 | flag = flag or flag1 |
---|
| 1325 | elif self.data.__class__.__name__ != "Data2D" and \ |
---|
| 1326 | not self.enable2D: |
---|
[373d4ee] | 1327 | enable_smearer = not self.disable_smearer.GetValue() |
---|
[682c432] | 1328 | self._manager.set_smearer(smearer=temp_smearer, |
---|
| 1329 | fid=self.data.id, |
---|
| 1330 | uid=self.uid, |
---|
| 1331 | qmin=float(self.qmin_x), |
---|
| 1332 | qmax=float(self.qmax_x), |
---|
[934ce649] | 1333 | enable_smearer=enable_smearer) |
---|
[682c432] | 1334 | if flag: |
---|
[c8e1996] | 1335 | # self.compute_chisqr(smearer= temp_smearer) |
---|
[682c432] | 1336 | |
---|
[c8e1996] | 1337 | # new state posted |
---|
[682c432] | 1338 | if self.state_change: |
---|
[c8e1996] | 1339 | # self._undo.Enable(True) |
---|
[682c432] | 1340 | event = PageInfoEvent(page=self) |
---|
| 1341 | wx.PostEvent(self.parent, event) |
---|
| 1342 | self.state_change = False |
---|
| 1343 | else: |
---|
| 1344 | # invalid fit range: do nothing here: |
---|
| 1345 | # msg already displayed in validate |
---|
| 1346 | return |
---|
| 1347 | else: |
---|
| 1348 | self.save_current_state() |
---|
[c8e1996] | 1349 | msg = "Cannot Plot: Must enter a number!!! " |
---|
[682c432] | 1350 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
| 1351 | |
---|
| 1352 | self.save_current_state() |
---|
| 1353 | return |
---|
| 1354 | |
---|
| 1355 | def _onparamRangeEnter(self, event): |
---|
| 1356 | """ |
---|
| 1357 | Check validity of value enter in the parameters range field |
---|
| 1358 | """ |
---|
| 1359 | tcrtl = event.GetEventObject() |
---|
[c8e1996] | 1360 | # Clear msg if previously shown. |
---|
[682c432] | 1361 | msg = "" |
---|
| 1362 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
| 1363 | # Flag to register when a parameter has changed. |
---|
| 1364 | is_modified = False |
---|
| 1365 | if tcrtl.GetValue().lstrip().rstrip() != "": |
---|
| 1366 | try: |
---|
| 1367 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
[ee4b3cb] | 1368 | self._check_value_enter(self.fittable_param) |
---|
| 1369 | self._check_value_enter(self.parameters) |
---|
[682c432] | 1370 | except: |
---|
| 1371 | tcrtl.SetBackgroundColour("pink") |
---|
| 1372 | msg = "Model Error:wrong value entered : %s" % sys.exc_value |
---|
| 1373 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
| 1374 | return |
---|
| 1375 | else: |
---|
| 1376 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
| 1377 | |
---|
[c8e1996] | 1378 | # self._undo.Enable(True) |
---|
[682c432] | 1379 | self.save_current_state() |
---|
| 1380 | event = PageInfoEvent(page=self) |
---|
| 1381 | wx.PostEvent(self.parent, event) |
---|
| 1382 | self.state_change = False |
---|
| 1383 | |
---|
| 1384 | def qrang_set_focus(self, event=None): |
---|
| 1385 | """ |
---|
| 1386 | ON Qrange focus |
---|
| 1387 | """ |
---|
[c8e1996] | 1388 | if event is not None: |
---|
[682c432] | 1389 | event.Skip() |
---|
[c8e1996] | 1390 | # tcrtl = event.GetEventObject() |
---|
[682c432] | 1391 | self._validate_qrange(self.qmin, self.qmax) |
---|
| 1392 | |
---|
| 1393 | def qrange_click(self, event): |
---|
| 1394 | """ |
---|
| 1395 | On Qrange textctrl click, make the qrange lines in the plot |
---|
| 1396 | """ |
---|
[c8e1996] | 1397 | if event is not None: |
---|
[682c432] | 1398 | event.Skip() |
---|
| 1399 | if self.data.__class__.__name__ == "Data2D": |
---|
| 1400 | return |
---|
| 1401 | is_click = event.LeftDown() |
---|
| 1402 | if is_click: |
---|
| 1403 | d_id = self.data.id |
---|
| 1404 | d_group_id = self.data.group_id |
---|
| 1405 | act_ctrl = event.GetEventObject() |
---|
| 1406 | wx.PostEvent(self._manager.parent, |
---|
| 1407 | PlotQrangeEvent(ctrl=[self.qmin, self.qmax], id=d_id, |
---|
| 1408 | group_id=d_group_id, leftdown=is_click, |
---|
| 1409 | active=act_ctrl)) |
---|
| 1410 | |
---|
| 1411 | def on_qrange_text(self, event): |
---|
| 1412 | """ |
---|
| 1413 | #On q range value updated. DO not combine with qrange_click(). |
---|
| 1414 | """ |
---|
[c8e1996] | 1415 | if event is not None: |
---|
[682c432] | 1416 | event.Skip() |
---|
| 1417 | if self.data.__class__.__name__ == "Data2D": |
---|
| 1418 | return |
---|
| 1419 | act_ctrl = event.GetEventObject() |
---|
| 1420 | d_id = self.data.id |
---|
| 1421 | d_group_id = self.data.group_id |
---|
| 1422 | wx.PostEvent(self._manager.parent, |
---|
| 1423 | PlotQrangeEvent(ctrl=[self.qmin, self.qmax], id=d_id, |
---|
| 1424 | group_id=d_group_id, leftdown=False, |
---|
| 1425 | active=act_ctrl)) |
---|
| 1426 | self._validate_qrange(self.qmin, self.qmax) |
---|
| 1427 | |
---|
| 1428 | def on_key(self, event): |
---|
| 1429 | """ |
---|
| 1430 | On Key down |
---|
| 1431 | """ |
---|
| 1432 | event.Skip() |
---|
| 1433 | if self.data.__class__.__name__ == "Data2D": |
---|
| 1434 | return |
---|
| 1435 | ctrl = event.GetEventObject() |
---|
| 1436 | try: |
---|
| 1437 | x_data = float(ctrl.GetValue()) |
---|
| 1438 | except: |
---|
| 1439 | return |
---|
| 1440 | key = event.GetKeyCode() |
---|
| 1441 | length = len(self.data.x) |
---|
[9a5097c] | 1442 | indx = (np.abs(self.data.x - x_data)).argmin() |
---|
[c8e1996] | 1443 | # return array.flat[idx] |
---|
[682c432] | 1444 | if key == wx.WXK_PAGEUP or key == wx.WXK_NUMPAD_PAGEUP: |
---|
| 1445 | indx += 1 |
---|
| 1446 | if indx >= length: |
---|
| 1447 | indx = length - 1 |
---|
| 1448 | elif key == wx.WXK_PAGEDOWN or key == wx.WXK_NUMPAD_PAGEDOWN: |
---|
| 1449 | indx -= 1 |
---|
| 1450 | if indx < 0: |
---|
| 1451 | indx = 0 |
---|
| 1452 | else: |
---|
| 1453 | return |
---|
| 1454 | ctrl.SetValue(str(self.data.x[indx])) |
---|
| 1455 | self._validate_qrange(self.qmin, self.qmax) |
---|
| 1456 | |
---|
| 1457 | def _onQrangeEnter(self, event): |
---|
| 1458 | """ |
---|
| 1459 | Check validity of value enter in the Q range field |
---|
| 1460 | """ |
---|
| 1461 | tcrtl = event.GetEventObject() |
---|
[c8e1996] | 1462 | # Clear msg if previously shown. |
---|
[682c432] | 1463 | msg = "" |
---|
| 1464 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
| 1465 | # For theory mode |
---|
| 1466 | if not self.data.is_data: |
---|
| 1467 | self.npts_x = self.Npts_total.GetValue() |
---|
| 1468 | self.Npts_fit.SetValue(self.npts_x) |
---|
| 1469 | self.create_default_data() |
---|
| 1470 | # Flag to register when a parameter has changed. |
---|
| 1471 | if tcrtl.GetValue().lstrip().rstrip() != "": |
---|
| 1472 | try: |
---|
| 1473 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
| 1474 | # If qmin and qmax have been modified, update qmin and qmax |
---|
| 1475 | if self._validate_qrange(self.qmin, self.qmax): |
---|
| 1476 | tempmin = float(self.qmin.GetValue()) |
---|
| 1477 | if tempmin != self.qmin_x: |
---|
| 1478 | self.qmin_x = tempmin |
---|
| 1479 | tempmax = float(self.qmax.GetValue()) |
---|
| 1480 | if tempmax != self.qmax_x: |
---|
| 1481 | self.qmax_x = tempmax |
---|
| 1482 | else: |
---|
| 1483 | tcrtl.SetBackgroundColour("pink") |
---|
| 1484 | msg = "Model Error:wrong value entered : %s" % sys.exc_value |
---|
| 1485 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
| 1486 | return |
---|
| 1487 | except: |
---|
| 1488 | tcrtl.SetBackgroundColour("pink") |
---|
| 1489 | msg = "Model Error:wrong value entered : %s" % sys.exc_value |
---|
| 1490 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
| 1491 | return |
---|
[c8e1996] | 1492 | # Check if # of points for theory model are valid(>0). |
---|
[682c432] | 1493 | # check for 2d |
---|
| 1494 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
| 1495 | self.enable2D: |
---|
| 1496 | # set mask |
---|
[9a5097c] | 1497 | radius = np.sqrt(self.data.qx_data * self.data.qx_data + |
---|
[682c432] | 1498 | self.data.qy_data * self.data.qy_data) |
---|
[c8e1996] | 1499 | index_data = ((self.qmin_x <= radius) & (radius <= self.qmax_x)) |
---|
[682c432] | 1500 | index_data = (index_data) & (self.data.mask) |
---|
[9a5097c] | 1501 | index_data = (index_data) & (np.isfinite(self.data.data)) |
---|
[682c432] | 1502 | if len(index_data[index_data]) < 10: |
---|
| 1503 | msg = "Cannot Plot :No or too little npts in" |
---|
| 1504 | msg += " that data range!!! " |
---|
| 1505 | wx.PostEvent(self._manager.parent, |
---|
| 1506 | StatusEvent(status=msg)) |
---|
| 1507 | return |
---|
| 1508 | else: |
---|
[c8e1996] | 1509 | # self.data.mask = index_data |
---|
| 1510 | # self.Npts_fit.SetValue(str(len(self.data.mask))) |
---|
[682c432] | 1511 | self.show_npts2fit() |
---|
| 1512 | else: |
---|
[c8e1996] | 1513 | index_data = ((self.qmin_x <= self.data.x) & |
---|
[682c432] | 1514 | (self.data.x <= self.qmax_x)) |
---|
| 1515 | self.Npts_fit.SetValue(str(len(self.data.x[index_data]))) |
---|
| 1516 | |
---|
| 1517 | self.npts_x = self.Npts_total.GetValue() |
---|
| 1518 | self.create_default_data() |
---|
| 1519 | self._save_plotting_range() |
---|
| 1520 | else: |
---|
| 1521 | tcrtl.SetBackgroundColour("pink") |
---|
| 1522 | msg = "Model Error:wrong value entered!!!" |
---|
| 1523 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
| 1524 | |
---|
| 1525 | self._draw_model() |
---|
| 1526 | self.save_current_state() |
---|
| 1527 | event = PageInfoEvent(page=self) |
---|
| 1528 | wx.PostEvent(self.parent, event) |
---|
| 1529 | self.state_change = False |
---|
| 1530 | return |
---|
| 1531 | |
---|
| 1532 | def _clear_Err_on_Fit(self): |
---|
| 1533 | """ |
---|
| 1534 | hide the error text control shown |
---|
| 1535 | after fitting |
---|
| 1536 | """ |
---|
| 1537 | |
---|
| 1538 | if self.is_mac: |
---|
| 1539 | return |
---|
| 1540 | if hasattr(self, "text2_3"): |
---|
| 1541 | self.text2_3.Hide() |
---|
| 1542 | |
---|
| 1543 | if len(self.parameters) > 0: |
---|
| 1544 | for item in self.parameters: |
---|
| 1545 | if item[0].IsShown(): |
---|
[c8e1996] | 1546 | # Skip the angle parameters if 1D data |
---|
[682c432] | 1547 | if self.data.__class__.__name__ != "Data2D" and \ |
---|
| 1548 | not self.enable2D: |
---|
| 1549 | if item in self.orientation_params: |
---|
| 1550 | continue |
---|
| 1551 | if item in self.param_toFit: |
---|
| 1552 | continue |
---|
[c8e1996] | 1553 | # hide statictext +/- |
---|
[682c432] | 1554 | if len(item) < 4: |
---|
| 1555 | continue |
---|
[c8e1996] | 1556 | if item[3] is not None and item[3].IsShown(): |
---|
[682c432] | 1557 | item[3].Hide() |
---|
[c8e1996] | 1558 | # hide textcrtl for error after fit |
---|
| 1559 | if item[4] is not None and item[4].IsShown(): |
---|
[682c432] | 1560 | item[4].Hide() |
---|
| 1561 | |
---|
| 1562 | if len(self.fittable_param) > 0: |
---|
| 1563 | for item in self.fittable_param: |
---|
| 1564 | if item[0].IsShown(): |
---|
[c8e1996] | 1565 | # Skip the angle parameters if 1D data |
---|
[682c432] | 1566 | if self.data.__class__.__name__ != "Data2D" and \ |
---|
| 1567 | not self.enable2D: |
---|
| 1568 | if item in self.orientation_params: |
---|
| 1569 | continue |
---|
| 1570 | if item in self.param_toFit: |
---|
| 1571 | continue |
---|
| 1572 | if len(item) < 4: |
---|
| 1573 | continue |
---|
[c8e1996] | 1574 | # hide statictext +/- |
---|
| 1575 | if item[3] is not None and item[3].IsShown(): |
---|
[682c432] | 1576 | item[3].Hide() |
---|
[c8e1996] | 1577 | # hide textcrtl for error after fit |
---|
| 1578 | if item[4] is not None and item[4].IsShown(): |
---|
[682c432] | 1579 | item[4].Hide() |
---|
| 1580 | return |
---|
| 1581 | |
---|
| 1582 | def _get_defult_custom_smear(self): |
---|
| 1583 | """ |
---|
| 1584 | Get the defult values for custum smearing. |
---|
| 1585 | """ |
---|
| 1586 | # get the default values |
---|
[c8e1996] | 1587 | if self.dxl is None: |
---|
[682c432] | 1588 | self.dxl = 0.0 |
---|
[c8e1996] | 1589 | if self.dxw is None: |
---|
[682c432] | 1590 | self.dxw = "" |
---|
[7e98655] | 1591 | if self.dx_percent is None: |
---|
| 1592 | self.dx_percent = SMEAR_SIZE_H |
---|
[682c432] | 1593 | |
---|
| 1594 | def _get_smear_info(self): |
---|
| 1595 | """ |
---|
| 1596 | Get the smear info from data. |
---|
| 1597 | |
---|
| 1598 | :return: self.smear_type, self.dq_l and self.dq_r, |
---|
| 1599 | respectively the type of the smear, dq_min and |
---|
| 1600 | dq_max for pinhole smear data |
---|
| 1601 | while dxl and dxw for slit smear |
---|
| 1602 | """ |
---|
| 1603 | # default |
---|
| 1604 | self.smear_type = None |
---|
| 1605 | self.dq_l = None |
---|
| 1606 | self.dq_r = None |
---|
| 1607 | data = self.data |
---|
| 1608 | if self.data is None: |
---|
| 1609 | return |
---|
| 1610 | elif self.data.__class__.__name__ == "Data2D" or \ |
---|
| 1611 | self.enable2D: |
---|
[c8e1996] | 1612 | if data.dqx_data is None or data.dqy_data is None: |
---|
[682c432] | 1613 | return |
---|
[c8e1996] | 1614 | elif self.current_smearer is not None \ |
---|
[682c432] | 1615 | and data.dqx_data.any() != 0 \ |
---|
| 1616 | and data.dqx_data.any() != 0: |
---|
| 1617 | self.smear_type = "Pinhole2d" |
---|
[9a5097c] | 1618 | self.dq_l = format_number(np.average(data.dqx_data)) |
---|
| 1619 | self.dq_r = format_number(np.average(data.dqy_data)) |
---|
[682c432] | 1620 | return |
---|
| 1621 | else: |
---|
| 1622 | return |
---|
| 1623 | # check if it is pinhole smear and get min max if it is. |
---|
[9a5097c] | 1624 | if data.dx is not None and np.any(data.dx): |
---|
[682c432] | 1625 | self.smear_type = "Pinhole" |
---|
| 1626 | self.dq_l = data.dx[0] |
---|
| 1627 | self.dq_r = data.dx[-1] |
---|
| 1628 | |
---|
| 1629 | # check if it is slit smear and get min max if it is. |
---|
[c8e1996] | 1630 | elif data.dxl is not None or data.dxw is not None: |
---|
[682c432] | 1631 | self.smear_type = "Slit" |
---|
[9a5097c] | 1632 | if data.dxl is not None and np.all(data.dxl, 0): |
---|
[682c432] | 1633 | self.dq_l = data.dxl[0] |
---|
[9a5097c] | 1634 | if data.dxw is not None and np.all(data.dxw, 0): |
---|
[682c432] | 1635 | self.dq_r = data.dxw[0] |
---|
[c8e1996] | 1636 | # return self.smear_type,self.dq_l,self.dq_r |
---|
[682c432] | 1637 | |
---|
| 1638 | def _show_smear_sizer(self): |
---|
| 1639 | """ |
---|
| 1640 | Show only the sizers depending on smear selection |
---|
| 1641 | """ |
---|
| 1642 | # smear disabled |
---|
| 1643 | if self.disable_smearer.GetValue(): |
---|
| 1644 | self.smear_description_none.Show(True) |
---|
| 1645 | # 2Dsmear |
---|
| 1646 | elif self._is_2D(): |
---|
| 1647 | self.smear_description_accuracy_type.Show(True) |
---|
| 1648 | self.smear_accuracy.Show(True) |
---|
| 1649 | self.smear_description_accuracy_type.Show(True) |
---|
| 1650 | self.smear_description_2d.Show(True) |
---|
| 1651 | self.smear_description_2d_x.Show(True) |
---|
| 1652 | self.smear_description_2d_y.Show(True) |
---|
| 1653 | if self.pinhole_smearer.GetValue(): |
---|
[7e98655] | 1654 | self.smear_pinhole_percent.Show(True) |
---|
[682c432] | 1655 | # smear from data |
---|
| 1656 | elif self.enable_smearer.GetValue(): |
---|
| 1657 | |
---|
| 1658 | self.smear_description_dqdata.Show(True) |
---|
[c8e1996] | 1659 | if self.smear_type is not None: |
---|
[682c432] | 1660 | self.smear_description_smear_type.Show(True) |
---|
| 1661 | if self.smear_type == 'Slit': |
---|
| 1662 | self.smear_description_slit_height.Show(True) |
---|
| 1663 | self.smear_description_slit_width.Show(True) |
---|
| 1664 | elif self.smear_type == 'Pinhole': |
---|
[7e98655] | 1665 | self.smear_description_pin_percent.Show(True) |
---|
[682c432] | 1666 | self.smear_description_smear_type.Show(True) |
---|
| 1667 | self.smear_description_type.Show(True) |
---|
| 1668 | self.smear_data_left.Show(True) |
---|
| 1669 | self.smear_data_right.Show(True) |
---|
| 1670 | # custom pinhole smear |
---|
| 1671 | elif self.pinhole_smearer.GetValue(): |
---|
| 1672 | if self.smear_type == 'Pinhole': |
---|
| 1673 | self.smear_message_new_p.Show(True) |
---|
[7e98655] | 1674 | self.smear_description_pin_percent.Show(True) |
---|
[682c432] | 1675 | |
---|
[7e98655] | 1676 | self.smear_pinhole_percent.Show(True) |
---|
[682c432] | 1677 | # custom slit smear |
---|
| 1678 | elif self.slit_smearer.GetValue(): |
---|
| 1679 | self.smear_message_new_s.Show(True) |
---|
| 1680 | self.smear_description_slit_height.Show(True) |
---|
| 1681 | self.smear_slit_height.Show(True) |
---|
| 1682 | self.smear_description_slit_width.Show(True) |
---|
| 1683 | self.smear_slit_width.Show(True) |
---|
| 1684 | |
---|
| 1685 | def _hide_all_smear_info(self): |
---|
| 1686 | """ |
---|
| 1687 | Hide all smearing messages in the set_smearer sizer |
---|
| 1688 | """ |
---|
| 1689 | self.smear_description_none.Hide() |
---|
| 1690 | self.smear_description_dqdata.Hide() |
---|
| 1691 | self.smear_description_type.Hide() |
---|
| 1692 | self.smear_description_smear_type.Hide() |
---|
| 1693 | self.smear_description_accuracy_type.Hide() |
---|
| 1694 | self.smear_description_2d_x.Hide() |
---|
| 1695 | self.smear_description_2d_y.Hide() |
---|
| 1696 | self.smear_description_2d.Hide() |
---|
| 1697 | |
---|
| 1698 | self.smear_accuracy.Hide() |
---|
| 1699 | self.smear_data_left.Hide() |
---|
| 1700 | self.smear_data_right.Hide() |
---|
[7e98655] | 1701 | self.smear_description_pin_percent.Hide() |
---|
| 1702 | self.smear_pinhole_percent.Hide() |
---|
[682c432] | 1703 | self.smear_description_slit_height.Hide() |
---|
| 1704 | self.smear_slit_height.Hide() |
---|
| 1705 | self.smear_description_slit_width.Hide() |
---|
| 1706 | self.smear_slit_width.Hide() |
---|
| 1707 | self.smear_message_new_p.Hide() |
---|
| 1708 | self.smear_message_new_s.Hide() |
---|
| 1709 | |
---|
| 1710 | def _set_accuracy_list(self): |
---|
| 1711 | """ |
---|
| 1712 | Set the list of an accuracy in 2D custum smear: |
---|
| 1713 | Xhigh, High, Med, or Low |
---|
| 1714 | """ |
---|
| 1715 | # list of accuracy choices |
---|
| 1716 | list = ['Low', 'Med', 'High', 'Xhigh'] |
---|
| 1717 | for idx in range(len(list)): |
---|
| 1718 | self.smear_accuracy.Append(list[idx], idx) |
---|
| 1719 | |
---|
| 1720 | def _set_fun_box_list(self, fun_box): |
---|
| 1721 | """ |
---|
| 1722 | Set the list of func for multifunctional models |
---|
| 1723 | """ |
---|
| 1724 | # Check if it is multi_functional model |
---|
| 1725 | if self.model.__class__ not in self.model_list_box["Multi-Functions"] \ |
---|
| 1726 | and not self.temp_multi_functional: |
---|
| 1727 | return None |
---|
| 1728 | # Get the func name list |
---|
| 1729 | list = self.model.fun_list |
---|
| 1730 | if len(list) == 0: |
---|
| 1731 | return None |
---|
| 1732 | # build function (combo)box |
---|
| 1733 | ind = 0 |
---|
| 1734 | while(ind < len(list)): |
---|
[277257f] | 1735 | for key, val in list.items(): |
---|
| 1736 | if val == ind: |
---|
[682c432] | 1737 | fun_box.Append(key, val) |
---|
| 1738 | break |
---|
| 1739 | ind += 1 |
---|
| 1740 | |
---|
| 1741 | def _on_select_accuracy(self, event): |
---|
| 1742 | """ |
---|
| 1743 | Select an accuracy in 2D custom smear: Xhigh, High, Med, or Low |
---|
| 1744 | """ |
---|
[c8e1996] | 1745 | # event.Skip() |
---|
[682c432] | 1746 | # Check if the accuracy is same as before |
---|
[c8e1996] | 1747 | # self.smear2d_accuracy = event.GetEventObject().GetValue() |
---|
[682c432] | 1748 | self.smear2d_accuracy = self.smear_accuracy.GetValue() |
---|
| 1749 | if self.pinhole_smearer.GetValue(): |
---|
| 1750 | self.onPinholeSmear(event=None) |
---|
| 1751 | else: |
---|
| 1752 | self.onSmear(event=None) |
---|
[c8e1996] | 1753 | if self.current_smearer is not None: |
---|
[682c432] | 1754 | self.current_smearer.set_accuracy(accuracy=\ |
---|
| 1755 | self.smear2d_accuracy) |
---|
| 1756 | event.Skip() |
---|
| 1757 | |
---|
| 1758 | def _on_fun_box(self, event): |
---|
| 1759 | """ |
---|
| 1760 | Select an func: Erf,Rparabola,LParabola |
---|
| 1761 | """ |
---|
| 1762 | fun_val = None |
---|
| 1763 | fun_box = event.GetEventObject() |
---|
| 1764 | name = fun_box.Name |
---|
| 1765 | value = fun_box.GetValue() |
---|
| 1766 | if value in self.model.fun_list: |
---|
| 1767 | fun_val = self.model.fun_list[value] |
---|
| 1768 | |
---|
| 1769 | self.model.setParam(name, fun_val) |
---|
| 1770 | # save state |
---|
| 1771 | self._copy_parameters_state(self.str_parameters, |
---|
| 1772 | self.state.str_parameters) |
---|
| 1773 | # update params |
---|
| 1774 | self._update_paramv_on_fit() |
---|
| 1775 | # draw |
---|
| 1776 | self._draw_model() |
---|
| 1777 | self.Refresh() |
---|
| 1778 | # get ready for new event |
---|
| 1779 | event.Skip() |
---|
| 1780 | |
---|
| 1781 | def _onMask(self, event): |
---|
| 1782 | """ |
---|
| 1783 | Build a panel to allow to edit Mask |
---|
| 1784 | """ |
---|
[c8e1996] | 1785 | from sas.sasgui.guiframe.local_perspectives.plotting.masking import \ |
---|
| 1786 | MaskPanel as MaskDialog |
---|
[682c432] | 1787 | |
---|
| 1788 | self.panel = MaskDialog(base=self, data=self.data, id=wx.NewId()) |
---|
| 1789 | self.panel.ShowModal() |
---|
| 1790 | |
---|
| 1791 | def _draw_masked_model(self, event): |
---|
| 1792 | """ |
---|
| 1793 | Draw model image w/mask |
---|
| 1794 | """ |
---|
| 1795 | is_valid_qrange = self._update_paramv_on_fit() |
---|
| 1796 | |
---|
[c8e1996] | 1797 | if is_valid_qrange and self.model is not None: |
---|
[682c432] | 1798 | self.panel.MakeModal(False) |
---|
| 1799 | event.Skip() |
---|
| 1800 | # try re draw the model plot if it exists |
---|
| 1801 | self._draw_model() |
---|
| 1802 | self.show_npts2fit() |
---|
[c8e1996] | 1803 | elif self.model is None: |
---|
[682c432] | 1804 | self.panel.MakeModal(False) |
---|
| 1805 | event.Skip() |
---|
| 1806 | self.show_npts2fit() |
---|
| 1807 | msg = "No model is found on updating MASK in the model plot... " |
---|
| 1808 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
| 1809 | else: |
---|
| 1810 | event.Skip() |
---|
| 1811 | msg = ' Please consider your Q range, too.' |
---|
| 1812 | self.panel.ShowMessage(msg) |
---|
| 1813 | |
---|
| 1814 | def _set_smear(self, data): |
---|
| 1815 | """ |
---|
| 1816 | Set_smear |
---|
| 1817 | """ |
---|
| 1818 | if data is None: |
---|
| 1819 | return |
---|
| 1820 | self.current_smearer = smear_selection(data, self.model) |
---|
| 1821 | flag = self.disable_smearer.GetValue() |
---|
[373d4ee] | 1822 | if self.current_smearer is None: |
---|
[682c432] | 1823 | self.enable_smearer.Disable() |
---|
| 1824 | else: |
---|
| 1825 | self.enable_smearer.Enable() |
---|
| 1826 | if not flag: |
---|
| 1827 | self.onSmear(None) |
---|
| 1828 | |
---|
| 1829 | def get_view_mode(self): |
---|
| 1830 | """ |
---|
| 1831 | return True if the panel allow 2D or False if 1D |
---|
| 1832 | """ |
---|
| 1833 | return self.enable2D |
---|
| 1834 | |
---|
| 1835 | def compute_data_set_range(self, data_list): |
---|
| 1836 | """ |
---|
| 1837 | find the range that include all data in the set |
---|
| 1838 | return the minimum and the maximum values |
---|
| 1839 | """ |
---|
| 1840 | if data_list is not None and data_list != []: |
---|
| 1841 | for data in data_list: |
---|
| 1842 | qmin, qmax, npts = self.compute_data_range(data) |
---|
| 1843 | self.qmin_data_set = min(self.qmin_data_set, qmin) |
---|
| 1844 | self.qmax_data_set = max(self.qmax_data_set, qmax) |
---|
| 1845 | self.npts_data_set += npts |
---|
| 1846 | return self.qmin_data_set, self.qmax_data_set, self.npts_data_set |
---|
| 1847 | |
---|
| 1848 | def compute_data_range(self, data): |
---|
| 1849 | """ |
---|
| 1850 | compute the minimum and the maximum range of the data |
---|
| 1851 | return the npts contains in data |
---|
| 1852 | :param data: |
---|
| 1853 | """ |
---|
| 1854 | qmin, qmax, npts = None, None, None |
---|
| 1855 | if data is not None: |
---|
| 1856 | if not hasattr(data, "data"): |
---|
| 1857 | try: |
---|
| 1858 | qmin = min(data.x) |
---|
| 1859 | # Maximum value of data |
---|
| 1860 | qmax = max(data.x) |
---|
| 1861 | npts = len(data.x) |
---|
| 1862 | except: |
---|
[c8e1996] | 1863 | msg = "Unable to find min/max/length of \n data named %s" %\ |
---|
[682c432] | 1864 | data.filename |
---|
| 1865 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg, |
---|
| 1866 | info="error")) |
---|
| 1867 | raise ValueError, msg |
---|
| 1868 | |
---|
| 1869 | else: |
---|
| 1870 | qmin = 0 |
---|
| 1871 | try: |
---|
| 1872 | x = max(math.fabs(data.xmin), math.fabs(data.xmax)) |
---|
| 1873 | y = max(math.fabs(data.ymin), math.fabs(data.ymax)) |
---|
| 1874 | except: |
---|
| 1875 | msg = "Unable to find min/max of \n data named %s" % \ |
---|
| 1876 | data.filename |
---|
| 1877 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg, |
---|
| 1878 | info="error")) |
---|
| 1879 | raise ValueError, msg |
---|
[c8e1996] | 1880 | # Maximum value of data |
---|
[682c432] | 1881 | qmax = math.sqrt(x * x + y * y) |
---|
| 1882 | npts = len(data.data) |
---|
| 1883 | return qmin, qmax, npts |
---|
| 1884 | |
---|
| 1885 | def set_data(self, data): |
---|
| 1886 | """ |
---|
| 1887 | reset the current data |
---|
| 1888 | """ |
---|
| 1889 | id = None |
---|
| 1890 | flag = False |
---|
| 1891 | is_data = False |
---|
[c8e1996] | 1892 | npts = 0 |
---|
[682c432] | 1893 | try: |
---|
| 1894 | old_id = self.data.id |
---|
| 1895 | old_group_id = self.data.group_id |
---|
| 1896 | except: |
---|
| 1897 | old_id = id |
---|
| 1898 | old_group_id = id |
---|
| 1899 | if self.data is not None: |
---|
| 1900 | is_data = check_data_validity(self.data) |
---|
| 1901 | if not is_data and data is not None: |
---|
| 1902 | flag = True |
---|
| 1903 | if data is not None: |
---|
| 1904 | if is_data: |
---|
| 1905 | self.graph_id = self.data.group_id |
---|
| 1906 | flag = (data.id != self.data.id) |
---|
| 1907 | self.data = data |
---|
| 1908 | if check_data_validity(data): |
---|
| 1909 | self.graph_id = data.group_id |
---|
| 1910 | self.data.group_id = self.graph_id |
---|
| 1911 | |
---|
| 1912 | if self.data is None: |
---|
| 1913 | data_name = "" |
---|
| 1914 | self._set_bookmark_flag(False) |
---|
| 1915 | self._keep.Enable(False) |
---|
| 1916 | self._set_save_flag(False) |
---|
| 1917 | else: |
---|
[c8e1996] | 1918 | if self.model is not None: |
---|
[682c432] | 1919 | self._set_bookmark_flag(not self.batch_on) |
---|
| 1920 | self._keep.Enable(not self.batch_on) |
---|
| 1921 | if self.data.is_data: |
---|
| 1922 | self._set_save_flag(True) |
---|
| 1923 | self._set_preview_flag(True) |
---|
| 1924 | |
---|
| 1925 | # more disables for 2D |
---|
[03137f1] | 1926 | di_flag = False |
---|
| 1927 | dq_flag = False |
---|
[682c432] | 1928 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
| 1929 | self.enable2D: |
---|
| 1930 | self.slit_smearer.Disable() |
---|
| 1931 | self.pinhole_smearer.Enable(True) |
---|
| 1932 | self.default_mask = copy.deepcopy(self.data.mask) |
---|
[3d55219] | 1933 | if self.data.err_data is not None \ |
---|
[9a5097c] | 1934 | and np.any(self.data.err_data): |
---|
[03137f1] | 1935 | di_flag = True |
---|
[3d55219] | 1936 | if self.data.dqx_data is not None \ |
---|
[9a5097c] | 1937 | and np.any(self.data.dqx_data): |
---|
[03137f1] | 1938 | dq_flag = True |
---|
[682c432] | 1939 | else: |
---|
| 1940 | self.slit_smearer.Enable(True) |
---|
| 1941 | self.pinhole_smearer.Enable(True) |
---|
[9a5097c] | 1942 | if self.data.dy is not None and np.any(self.data.dy): |
---|
[03137f1] | 1943 | di_flag = True |
---|
[9a5097c] | 1944 | if self.data.dx is not None and np.any(self.data.dx): |
---|
[3d55219] | 1945 | dq_flag = True |
---|
[9a5097c] | 1946 | elif self.data.dxl is not None and np.any(self.data.dxl): |
---|
[03137f1] | 1947 | dq_flag = True |
---|
| 1948 | |
---|
| 1949 | if dq_flag: |
---|
| 1950 | self.enable_smearer.Enable(True) |
---|
| 1951 | self.enable_smearer.SetValue(True) |
---|
| 1952 | self.disable_smearer.SetValue(False) |
---|
[3d55219] | 1953 | else: |
---|
| 1954 | self.enable_smearer.Disable() |
---|
| 1955 | self.disable_smearer.Enable(True) |
---|
| 1956 | self.disable_smearer.SetValue(True) |
---|
[03137f1] | 1957 | |
---|
| 1958 | if di_flag: |
---|
| 1959 | self.dI_didata.Enable(True) |
---|
| 1960 | self.dI_didata.SetValue(True) |
---|
| 1961 | self.weightbt_string = self.dI_didata.GetLabelText() |
---|
[3d55219] | 1962 | else: |
---|
| 1963 | self.dI_didata.Enable(False) |
---|
| 1964 | self.dI_noweight.SetValue(True) |
---|
| 1965 | self.weightbt_string = self.dI_noweight.GetLabelText() |
---|
[c8e1996] | 1966 | |
---|
| 1967 | # Enable weighting radio buttons |
---|
[682c432] | 1968 | self.dI_noweight.Enable(True) |
---|
| 1969 | self.dI_sqrdata.Enable(True) |
---|
| 1970 | self.dI_idata.Enable(True) |
---|
| 1971 | |
---|
| 1972 | self.formfactorbox.Enable() |
---|
| 1973 | self.structurebox.Enable() |
---|
| 1974 | data_name = self.data.name |
---|
| 1975 | _, _, npts = self.compute_data_range(self.data) |
---|
[c8e1996] | 1976 | # set maximum range for x in linear scale |
---|
[682c432] | 1977 | if not hasattr(self.data, "data"): # Display only for 1D data fit |
---|
| 1978 | self.btEditMask.Disable() |
---|
| 1979 | self.EditMask_title.Disable() |
---|
| 1980 | else: |
---|
| 1981 | self.btEditMask.Enable() |
---|
| 1982 | self.EditMask_title.Enable() |
---|
| 1983 | |
---|
| 1984 | self.Npts_total.SetValue(str(npts)) |
---|
[c8e1996] | 1985 | # default:number of data points selected to fit |
---|
[682c432] | 1986 | self.Npts_fit.SetValue(str(npts)) |
---|
| 1987 | self.Npts_total.SetEditable(False) |
---|
[c8e1996] | 1988 | self.Npts_total.SetBackgroundColour( |
---|
[682c432] | 1989 | self.GetParent().GetBackgroundColour()) |
---|
| 1990 | |
---|
| 1991 | self.Npts_total.Bind(wx.EVT_MOUSE_EVENTS, self._npts_click) |
---|
| 1992 | self.pointsbox.Disable() |
---|
| 1993 | self.dataSource.SetValue(data_name) |
---|
| 1994 | self.state.data = data |
---|
| 1995 | self.enable_fit_button() |
---|
| 1996 | # send graph_id to page_finder |
---|
| 1997 | self._manager.set_graph_id(uid=self.uid, graph_id=self.graph_id) |
---|
[c8e1996] | 1998 | # focus the page |
---|
[682c432] | 1999 | if check_data_validity(data): |
---|
| 2000 | self.data_box_description.SetForegroundColour(wx.BLUE) |
---|
| 2001 | |
---|
| 2002 | if self.batch_on: |
---|
| 2003 | self.slit_smearer.Enable(False) |
---|
| 2004 | self.pinhole_smearer.Enable(False) |
---|
| 2005 | self.btEditMask.Disable() |
---|
| 2006 | self.EditMask_title.Disable() |
---|
| 2007 | |
---|
[2d568f6] | 2008 | self.on_smear_helper() |
---|
[682c432] | 2009 | self.on_set_focus(None) |
---|
| 2010 | self.Refresh() |
---|
[c8e1996] | 2011 | # update model plot with new data information |
---|
[682c432] | 2012 | if flag: |
---|
| 2013 | if self.data.__class__.__name__ == "Data2D": |
---|
| 2014 | self.enable2D = True |
---|
| 2015 | self.model_view.SetLabel("2D Mode") |
---|
| 2016 | else: |
---|
| 2017 | self.enable2D = False |
---|
| 2018 | self.model_view.SetLabel("1D Mode") |
---|
| 2019 | self.model_view.Disable() |
---|
[c8e1996] | 2020 | # replace data plot on combo box selection |
---|
| 2021 | # by removing the previous selected data |
---|
[682c432] | 2022 | try: |
---|
| 2023 | wx.PostEvent(self._manager.parent, |
---|
| 2024 | NewPlotEvent(action="delete", |
---|
| 2025 | group_id=old_group_id, id=old_id)) |
---|
| 2026 | except: |
---|
| 2027 | pass |
---|
[c8e1996] | 2028 | # plot the current selected data |
---|
[682c432] | 2029 | wx.PostEvent(self._manager.parent, |
---|
| 2030 | NewPlotEvent(action="check", plot=self.data, |
---|
| 2031 | title=str(self.data.title))) |
---|
| 2032 | self._draw_model() |
---|
| 2033 | |
---|
| 2034 | def _npts_click(self, event): |
---|
| 2035 | """ |
---|
| 2036 | Prevent further handling of the mouse event on Npts_total |
---|
| 2037 | by not calling Skip(). |
---|
| 2038 | """ |
---|
| 2039 | pass |
---|
| 2040 | |
---|
| 2041 | def reset_page(self, state, first=False): |
---|
| 2042 | """ |
---|
| 2043 | reset the state |
---|
| 2044 | """ |
---|
| 2045 | try: |
---|
| 2046 | self.reset_page_helper(state) |
---|
| 2047 | |
---|
[c8e1996] | 2048 | self.select_param() |
---|
| 2049 | # Save state_fit |
---|
[682c432] | 2050 | self.save_current_state_fit() |
---|
| 2051 | except: |
---|
| 2052 | self._show_combox_helper() |
---|
| 2053 | msg = "Error: This model state has missing or outdated " |
---|
| 2054 | msg += "information.\n" |
---|
[6c382da] | 2055 | msg += traceback.format_exc() |
---|
[682c432] | 2056 | wx.PostEvent(self._manager.parent, |
---|
| 2057 | StatusEvent(status=msg, info="error")) |
---|
| 2058 | self._lay_out() |
---|
| 2059 | self.Refresh() |
---|
| 2060 | |
---|
| 2061 | def get_range(self): |
---|
| 2062 | """ |
---|
| 2063 | return the fitting range |
---|
| 2064 | """ |
---|
| 2065 | return float(self.qmin_x), float(self.qmax_x) |
---|
| 2066 | |
---|
| 2067 | def get_npts2fit(self): |
---|
| 2068 | """ |
---|
| 2069 | return numbers of data points within qrange |
---|
| 2070 | |
---|
| 2071 | :Note: This is to normalize chisq by Npts of fit |
---|
| 2072 | |
---|
| 2073 | """ |
---|
| 2074 | if self.data is None: |
---|
| 2075 | return |
---|
| 2076 | npts2fit = 0 |
---|
| 2077 | qmin, qmax = self.get_range() |
---|
| 2078 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
| 2079 | self.enable2D: |
---|
[9a5097c] | 2080 | radius = np.sqrt(self.data.qx_data * self.data.qx_data + |
---|
[682c432] | 2081 | self.data.qy_data * self.data.qy_data) |
---|
| 2082 | index_data = (self.qmin_x <= radius) & (radius <= self.qmax_x) |
---|
| 2083 | index_data = (index_data) & (self.data.mask) |
---|
[9a5097c] | 2084 | index_data = (index_data) & (np.isfinite(self.data.data)) |
---|
[682c432] | 2085 | npts2fit = len(self.data.data[index_data]) |
---|
| 2086 | else: |
---|
| 2087 | for qx in self.data.x: |
---|
[c8e1996] | 2088 | if qmax >= qx >= qmin: |
---|
[682c432] | 2089 | npts2fit += 1 |
---|
| 2090 | return npts2fit |
---|
| 2091 | |
---|
| 2092 | def show_npts2fit(self): |
---|
| 2093 | """ |
---|
| 2094 | setValue Npts for fitting |
---|
| 2095 | """ |
---|
| 2096 | self.Npts_fit.SetValue(str(self.get_npts2fit())) |
---|
| 2097 | |
---|
| 2098 | def get_chi2(self): |
---|
| 2099 | """ |
---|
| 2100 | return the current chi2 |
---|
| 2101 | """ |
---|
| 2102 | return self.tcChi.GetValue() |
---|
| 2103 | |
---|
| 2104 | def onsetValues(self, chisqr, p_name, out, cov): |
---|
| 2105 | """ |
---|
| 2106 | Build the panel from the fit result |
---|
| 2107 | |
---|
| 2108 | :param chisqr: Value of the goodness of fit metric |
---|
| 2109 | :param p_name: the name of parameters |
---|
| 2110 | :param out: list of parameter with the best value found during fitting |
---|
| 2111 | :param cov: Covariance matrix |
---|
| 2112 | |
---|
| 2113 | """ |
---|
| 2114 | |
---|
| 2115 | # make sure stop button to fit button all the time |
---|
| 2116 | self._on_fit_complete() |
---|
[9a5097c] | 2117 | if out is None or not np.isfinite(chisqr): |
---|
[682c432] | 2118 | raise ValueError, "Fit error occured..." |
---|
| 2119 | |
---|
| 2120 | is_modified = False |
---|
| 2121 | has_error = False |
---|
| 2122 | dispersity = '' |
---|
| 2123 | |
---|
[c8e1996] | 2124 | # Hide textctrl boxes of errors. |
---|
[682c432] | 2125 | self._clear_Err_on_Fit() |
---|
| 2126 | |
---|
[c8e1996] | 2127 | # Check if chi2 is finite |
---|
[9a5097c] | 2128 | if chisqr is not None and np.isfinite(chisqr): |
---|
[c8e1996] | 2129 | # format chi2 |
---|
[682c432] | 2130 | chi2 = format_number(chisqr, True) |
---|
| 2131 | self.tcChi.SetValue(chi2) |
---|
| 2132 | self.tcChi.Refresh() |
---|
| 2133 | else: |
---|
| 2134 | self.tcChi.SetValue("-") |
---|
| 2135 | |
---|
[c8e1996] | 2136 | # Hide error title |
---|
[682c432] | 2137 | if self.text2_3.IsShown() and not self.is_mac: |
---|
| 2138 | self.text2_3.Hide() |
---|
| 2139 | |
---|
| 2140 | try: |
---|
| 2141 | if self.enable_disp.GetValue(): |
---|
| 2142 | if hasattr(self, "text_disp_1"): |
---|
[c8e1996] | 2143 | if self.text_disp_1 is not None and not self.is_mac: |
---|
[682c432] | 2144 | self.text_disp_1.Hide() |
---|
| 2145 | except: |
---|
| 2146 | dispersity = None |
---|
| 2147 | pass |
---|
| 2148 | |
---|
| 2149 | i = 0 |
---|
[c8e1996] | 2150 | # Set the panel when fit result are list |
---|
[682c432] | 2151 | |
---|
| 2152 | for item in self.param_toFit: |
---|
[c8e1996] | 2153 | if len(item) > 5 and item is not None: |
---|
[682c432] | 2154 | |
---|
| 2155 | if item[0].IsShown(): |
---|
[c8e1996] | 2156 | # reset error value to initial state |
---|
[682c432] | 2157 | if not self.is_mac: |
---|
| 2158 | item[3].Hide() |
---|
| 2159 | item[4].Hide() |
---|
| 2160 | for ind in range(len(out)): |
---|
| 2161 | if item[1] == p_name[ind]: |
---|
| 2162 | break |
---|
[c8e1996] | 2163 | if len(out) > 0 and out[ind] is not None: |
---|
[682c432] | 2164 | val_out = format_number(out[ind], True) |
---|
| 2165 | item[2].SetValue(val_out) |
---|
| 2166 | |
---|
[c8e1996] | 2167 | if(cov is not None and len(cov) == len(out)): |
---|
[682c432] | 2168 | try: |
---|
[c8e1996] | 2169 | if dispersity is not None: |
---|
[682c432] | 2170 | if self.enable_disp.GetValue(): |
---|
| 2171 | if hasattr(self, "text_disp_1"): |
---|
[c8e1996] | 2172 | if self.text_disp_1 is not None: |
---|
[682c432] | 2173 | if not self.text_disp_1.IsShown()\ |
---|
[c8e1996] | 2174 | and not self.is_mac: |
---|
[682c432] | 2175 | self.text_disp_1.Show(True) |
---|
| 2176 | except: |
---|
| 2177 | pass |
---|
| 2178 | |
---|
[c8e1996] | 2179 | if cov[ind] is not None: |
---|
[9a5097c] | 2180 | if np.isfinite(float(cov[ind])): |
---|
[682c432] | 2181 | val_err = format_number(cov[ind], True) |
---|
[c8e1996] | 2182 | item[4].SetForegroundColour(wx.BLACK) |
---|
[373d4ee] | 2183 | else: |
---|
| 2184 | val_err = 'NaN' |
---|
[60d08fd] | 2185 | item[4].SetForegroundColour(wx.RED) |
---|
[373d4ee] | 2186 | if not self.is_mac: |
---|
| 2187 | item[3].Show(True) |
---|
| 2188 | item[4].Show(True) |
---|
| 2189 | item[4].SetValue(val_err) |
---|
| 2190 | has_error = True |
---|
[682c432] | 2191 | i += 1 |
---|
| 2192 | else: |
---|
| 2193 | raise ValueError, "onsetValues: Invalid parameters..." |
---|
[c8e1996] | 2194 | # Show error title when any errors displayed |
---|
[682c432] | 2195 | if has_error: |
---|
| 2196 | if not self.text2_3.IsShown(): |
---|
| 2197 | self.text2_3.Show(True) |
---|
[c8e1996] | 2198 | # save current state |
---|
[682c432] | 2199 | self.save_current_state() |
---|
| 2200 | |
---|
[0b6f83c] | 2201 | if not self.is_mac: |
---|
| 2202 | self.Layout() |
---|
| 2203 | self.Refresh() |
---|
[c8e1996] | 2204 | # plot model ( when drawing, do not update chisqr value again) |
---|
[682c432] | 2205 | self._draw_model(update_chisqr=False, source='fit') |
---|
| 2206 | |
---|
| 2207 | def onWeighting(self, event): |
---|
| 2208 | """ |
---|
| 2209 | On Weighting radio button event, sets the weightbt_string |
---|
| 2210 | """ |
---|
| 2211 | self.weightbt_string = event.GetEventObject().GetLabelText() |
---|
| 2212 | self._set_weight() |
---|
| 2213 | |
---|
| 2214 | def _set_weight(self, is_2D=None): |
---|
| 2215 | """ |
---|
| 2216 | Set weight in fit problem |
---|
| 2217 | """ |
---|
| 2218 | # compute weight for the current data |
---|
| 2219 | flag_weight = self.get_weight_flag() |
---|
[c8e1996] | 2220 | if is_2D is None: |
---|
[682c432] | 2221 | is_2D = self._is_2D() |
---|
| 2222 | self._manager.set_fit_weight(uid=self.uid, |
---|
| 2223 | flag=flag_weight, |
---|
| 2224 | is2d=is_2D, |
---|
| 2225 | fid=None) |
---|
| 2226 | |
---|
| 2227 | def onPinholeSmear(self, event): |
---|
| 2228 | """ |
---|
| 2229 | Create a custom pinhole smear object that will change the way residuals |
---|
| 2230 | are compute when fitting |
---|
| 2231 | |
---|
| 2232 | :Note: accuracy is given by strings'High','Med', 'Low' FOR 2d, |
---|
| 2233 | None for 1D |
---|
| 2234 | |
---|
| 2235 | """ |
---|
| 2236 | # Need update param values |
---|
| 2237 | self._update_paramv_on_fit() |
---|
| 2238 | |
---|
[c8e1996] | 2239 | if event is not None: |
---|
[682c432] | 2240 | tcrtl = event.GetEventObject() |
---|
| 2241 | # event case of radio button |
---|
[c8e1996] | 2242 | if tcrtl.GetValue(): |
---|
[7e98655] | 2243 | self.dx_percent = 0.0 |
---|
[682c432] | 2244 | is_new_pinhole = True |
---|
| 2245 | else: |
---|
| 2246 | is_new_pinhole = self._is_changed_pinhole() |
---|
| 2247 | else: |
---|
| 2248 | is_new_pinhole = True |
---|
| 2249 | # if any value is changed |
---|
| 2250 | if is_new_pinhole: |
---|
[098f3d2] | 2251 | self._set_pinhole_smear() |
---|
[682c432] | 2252 | # hide all silt sizer |
---|
| 2253 | self._hide_all_smear_info() |
---|
| 2254 | |
---|
| 2255 | # show relevant slit sizers |
---|
| 2256 | self._show_smear_sizer() |
---|
| 2257 | |
---|
| 2258 | self.sizer_set_smearer.Layout() |
---|
[c8e1996] | 2259 | # we need FitInside here not just self.Layout to ensure all the sizers |
---|
| 2260 | # end up with the necessasary space to in the scroll panel. In |
---|
| 2261 | # particular the compute and fit buttons end up on top of each other |
---|
| 2262 | # PDB Nov 28 2015. |
---|
[1c2bf90] | 2263 | self.FitInside() |
---|
[682c432] | 2264 | |
---|
[c8e1996] | 2265 | if event is not None: |
---|
[682c432] | 2266 | event.Skip() |
---|
[c8e1996] | 2267 | # self._undo.Enable(True) |
---|
[682c432] | 2268 | self.save_current_state() |
---|
| 2269 | event = PageInfoEvent(page=self) |
---|
| 2270 | wx.PostEvent(self.parent, event) |
---|
| 2271 | |
---|
| 2272 | def _is_changed_pinhole(self): |
---|
| 2273 | """ |
---|
| 2274 | check if any of pinhole smear is changed |
---|
| 2275 | |
---|
| 2276 | :return: True or False |
---|
| 2277 | |
---|
| 2278 | """ |
---|
| 2279 | # get the values |
---|
[7e98655] | 2280 | pin_percent = self.smear_pinhole_percent.GetValue() |
---|
[682c432] | 2281 | |
---|
| 2282 | # Check changes in slit heigth |
---|
| 2283 | try: |
---|
[7e98655] | 2284 | dx_percent = float(pin_percent) |
---|
[682c432] | 2285 | except: |
---|
| 2286 | return True |
---|
[7e98655] | 2287 | if self.dx_percent != dx_percent: |
---|
[682c432] | 2288 | return True |
---|
| 2289 | return False |
---|
| 2290 | |
---|
| 2291 | def _set_pinhole_smear(self): |
---|
| 2292 | """ |
---|
| 2293 | Set custom pinhole smear |
---|
| 2294 | |
---|
| 2295 | :return: msg |
---|
| 2296 | |
---|
| 2297 | """ |
---|
| 2298 | # copy data |
---|
| 2299 | data = copy.deepcopy(self.data) |
---|
| 2300 | if self._is_2D(): |
---|
| 2301 | self.smear_type = 'Pinhole2d' |
---|
| 2302 | len_data = len(data.data) |
---|
[9a5097c] | 2303 | data.dqx_data = np.zeros(len_data) |
---|
| 2304 | data.dqy_data = np.zeros(len_data) |
---|
[682c432] | 2305 | else: |
---|
| 2306 | self.smear_type = 'Pinhole' |
---|
| 2307 | len_data = len(data.x) |
---|
[9a5097c] | 2308 | data.dx = np.zeros(len_data) |
---|
[682c432] | 2309 | data.dxl = None |
---|
| 2310 | data.dxw = None |
---|
| 2311 | msg = None |
---|
| 2312 | |
---|
[7e98655] | 2313 | get_pin_percent = self.smear_pinhole_percent |
---|
[682c432] | 2314 | |
---|
[7e98655] | 2315 | if not check_float(get_pin_percent): |
---|
| 2316 | get_pin_percent.SetBackgroundColour("pink") |
---|
[682c432] | 2317 | msg = "Model Error:wrong value entered!!!" |
---|
| 2318 | else: |
---|
| 2319 | if len_data < 2: |
---|
| 2320 | len_data = 2 |
---|
[d85f1d8a] | 2321 | self.dx_percent = float(get_pin_percent.GetValue()) |
---|
[7e98655] | 2322 | if self.dx_percent < 0: |
---|
| 2323 | get_pin_percent.SetBackgroundColour("pink") |
---|
[682c432] | 2324 | msg = "Model Error:This value can not be negative!!!" |
---|
[7e98655] | 2325 | elif self.dx_percent is not None: |
---|
[d85f1d8a] | 2326 | percent = self.dx_percent/100 |
---|
[682c432] | 2327 | if self._is_2D(): |
---|
[d85f1d8a] | 2328 | data.dqx_data[data.dqx_data == 0] = percent * data.qx_data |
---|
| 2329 | data.dqy_data[data.dqy_data == 0] = percent * data.qy_data |
---|
[682c432] | 2330 | else: |
---|
[d85f1d8a] | 2331 | data.dx = percent * data.x |
---|
[682c432] | 2332 | self.current_smearer = smear_selection(data, self.model) |
---|
| 2333 | # 2D need to set accuracy |
---|
| 2334 | if self._is_2D(): |
---|
[c8e1996] | 2335 | self.current_smearer.set_accuracy( |
---|
| 2336 | accuracy=self.smear2d_accuracy) |
---|
[682c432] | 2337 | |
---|
[c8e1996] | 2338 | if msg is not None: |
---|
[682c432] | 2339 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
| 2340 | else: |
---|
[7e98655] | 2341 | get_pin_percent.SetBackgroundColour("white") |
---|
[c8e1996] | 2342 | # set smearing value whether or not the data contain the smearing info |
---|
[682c432] | 2343 | |
---|
[373d4ee] | 2344 | enable_smearer = not self.disable_smearer.GetValue() |
---|
[682c432] | 2345 | self._manager.set_smearer(smearer=self.current_smearer, |
---|
[373d4ee] | 2346 | fid=self.data.id, |
---|
| 2347 | qmin=float(self.qmin_x), |
---|
| 2348 | qmax=float(self.qmax_x), |
---|
| 2349 | enable_smearer=enable_smearer, |
---|
| 2350 | uid=self.uid) |
---|
[682c432] | 2351 | return msg |
---|
| 2352 | |
---|
| 2353 | def update_pinhole_smear(self): |
---|
| 2354 | """ |
---|
| 2355 | called by kill_focus on pinhole TextCntrl |
---|
| 2356 | to update the changes |
---|
| 2357 | |
---|
| 2358 | :return: False when wrong value was entered |
---|
| 2359 | |
---|
| 2360 | """ |
---|
| 2361 | # msg default |
---|
| 2362 | msg = None |
---|
| 2363 | # check if any value is changed |
---|
| 2364 | if self._is_changed_pinhole(): |
---|
| 2365 | msg = self._set_pinhole_smear() |
---|
| 2366 | wx.CallAfter(self.save_current_state) |
---|
| 2367 | |
---|
[c8e1996] | 2368 | if msg is not None: |
---|
[682c432] | 2369 | return False |
---|
| 2370 | else: |
---|
| 2371 | return True |
---|
| 2372 | |
---|
| 2373 | def onSlitSmear(self, event): |
---|
| 2374 | """ |
---|
| 2375 | Create a custom slit smear object that will change the way residuals |
---|
| 2376 | are compute when fitting |
---|
| 2377 | """ |
---|
| 2378 | # Need update param values |
---|
| 2379 | self._update_paramv_on_fit() |
---|
| 2380 | |
---|
| 2381 | # msg default |
---|
| 2382 | msg = None |
---|
| 2383 | # for event given |
---|
[c8e1996] | 2384 | if event is not None: |
---|
[682c432] | 2385 | tcrtl = event.GetEventObject() |
---|
| 2386 | # event case of radio button |
---|
| 2387 | if tcrtl.GetValue(): |
---|
| 2388 | self.dxl = 0.0 |
---|
| 2389 | self.dxw = 0.0 |
---|
| 2390 | is_new_slit = True |
---|
| 2391 | else: |
---|
| 2392 | is_new_slit = self._is_changed_slit() |
---|
| 2393 | else: |
---|
| 2394 | is_new_slit = True |
---|
| 2395 | |
---|
| 2396 | # if any value is changed |
---|
| 2397 | if is_new_slit: |
---|
| 2398 | msg = self._set_slit_smear() |
---|
| 2399 | |
---|
| 2400 | # hide all silt sizer |
---|
| 2401 | self._hide_all_smear_info() |
---|
| 2402 | # show relevant slit sizers |
---|
| 2403 | self._show_smear_sizer() |
---|
| 2404 | self.sizer_set_smearer.Layout() |
---|
[c8e1996] | 2405 | # we need FitInside here not just self.Layout to ensure all the sizers |
---|
| 2406 | # end up with the necessasary space to in the scroll panel. In |
---|
| 2407 | # particular the compute and fit buttons end up on top of each other |
---|
| 2408 | # PDB Nov 28 2015. |
---|
[1c2bf90] | 2409 | self.FitInside() |
---|
[682c432] | 2410 | |
---|
[c8e1996] | 2411 | if event is not None: |
---|
[682c432] | 2412 | event.Skip() |
---|
| 2413 | self.save_current_state() |
---|
| 2414 | event = PageInfoEvent(page=self) |
---|
| 2415 | wx.PostEvent(self.parent, event) |
---|
[c8e1996] | 2416 | if msg is not None: |
---|
[682c432] | 2417 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
| 2418 | |
---|
| 2419 | def _is_changed_slit(self): |
---|
| 2420 | """ |
---|
| 2421 | check if any of slit lengths is changed |
---|
| 2422 | |
---|
| 2423 | :return: True or False |
---|
| 2424 | |
---|
| 2425 | """ |
---|
| 2426 | # get the values |
---|
| 2427 | width = self.smear_slit_width.GetValue() |
---|
| 2428 | height = self.smear_slit_height.GetValue() |
---|
| 2429 | |
---|
| 2430 | # check and change the box bg color if it was pink |
---|
| 2431 | # but it should be white now |
---|
| 2432 | # because this is the case that _set_slit_smear() will not handle |
---|
| 2433 | if height.lstrip().rstrip() == "": |
---|
| 2434 | self.smear_slit_height.SetBackgroundColour(wx.WHITE) |
---|
| 2435 | if width.lstrip().rstrip() == "": |
---|
| 2436 | self.smear_slit_width.SetBackgroundColour(wx.WHITE) |
---|
| 2437 | |
---|
| 2438 | # Check changes in slit width |
---|
| 2439 | if width == "": |
---|
| 2440 | dxw = 0.0 |
---|
| 2441 | else: |
---|
| 2442 | try: |
---|
| 2443 | dxw = float(width) |
---|
| 2444 | except: |
---|
| 2445 | return True |
---|
| 2446 | if self.dxw != dxw: |
---|
| 2447 | return True |
---|
| 2448 | |
---|
| 2449 | # Check changes in slit heigth |
---|
| 2450 | if height == "": |
---|
| 2451 | dxl = 0.0 |
---|
| 2452 | else: |
---|
| 2453 | try: |
---|
| 2454 | dxl = float(height) |
---|
| 2455 | except: |
---|
| 2456 | return True |
---|
| 2457 | if self.dxl != dxl: |
---|
| 2458 | return True |
---|
| 2459 | |
---|
| 2460 | return False |
---|
| 2461 | |
---|
| 2462 | def _set_slit_smear(self): |
---|
| 2463 | """ |
---|
| 2464 | Set custom slit smear |
---|
| 2465 | |
---|
| 2466 | :return: message to inform the user about the validity |
---|
| 2467 | of the values entered for slit smear |
---|
| 2468 | """ |
---|
[373d4ee] | 2469 | if self.data.__class__.__name__ == "Data2D" or self.enable2D: |
---|
[682c432] | 2470 | return |
---|
| 2471 | # make sure once more if it is smearer |
---|
| 2472 | data = copy.deepcopy(self.data) |
---|
| 2473 | data_len = len(data.x) |
---|
| 2474 | data.dx = None |
---|
| 2475 | data.dxl = None |
---|
| 2476 | data.dxw = None |
---|
| 2477 | msg = None |
---|
| 2478 | |
---|
| 2479 | try: |
---|
| 2480 | self.dxl = float(self.smear_slit_height.GetValue()) |
---|
[9a5097c] | 2481 | data.dxl = self.dxl * np.ones(data_len) |
---|
[682c432] | 2482 | self.smear_slit_height.SetBackgroundColour(wx.WHITE) |
---|
| 2483 | except: |
---|
| 2484 | self.dxl = None |
---|
[9a5097c] | 2485 | data.dxl = np.zeros(data_len) |
---|
[682c432] | 2486 | if self.smear_slit_height.GetValue().lstrip().rstrip() != "": |
---|
| 2487 | self.smear_slit_height.SetBackgroundColour("pink") |
---|
| 2488 | msg = "Wrong value entered... " |
---|
| 2489 | else: |
---|
| 2490 | self.smear_slit_height.SetBackgroundColour(wx.WHITE) |
---|
| 2491 | try: |
---|
| 2492 | self.dxw = float(self.smear_slit_width.GetValue()) |
---|
| 2493 | self.smear_slit_width.SetBackgroundColour(wx.WHITE) |
---|
[9a5097c] | 2494 | data.dxw = self.dxw * np.ones(data_len) |
---|
[682c432] | 2495 | except: |
---|
| 2496 | self.dxw = None |
---|
[9a5097c] | 2497 | data.dxw = np.zeros(data_len) |
---|
[682c432] | 2498 | if self.smear_slit_width.GetValue().lstrip().rstrip() != "": |
---|
| 2499 | self.smear_slit_width.SetBackgroundColour("pink") |
---|
| 2500 | msg = "Wrong Fit value entered... " |
---|
| 2501 | else: |
---|
| 2502 | self.smear_slit_width.SetBackgroundColour(wx.WHITE) |
---|
| 2503 | |
---|
| 2504 | self.current_smearer = smear_selection(data, self.model) |
---|
[c8e1996] | 2505 | # set smearing value whether or not the data contain the smearing info |
---|
[373d4ee] | 2506 | enable_smearer = not self.disable_smearer.GetValue() |
---|
[682c432] | 2507 | self._manager.set_smearer(smearer=self.current_smearer, |
---|
[373d4ee] | 2508 | fid=self.data.id, |
---|
| 2509 | qmin=float(self.qmin_x), |
---|
| 2510 | qmax=float(self.qmax_x), |
---|
| 2511 | enable_smearer=enable_smearer, |
---|
| 2512 | uid=self.uid) |
---|
[682c432] | 2513 | return msg |
---|
| 2514 | |
---|
| 2515 | def update_slit_smear(self): |
---|
| 2516 | """ |
---|
| 2517 | called by kill_focus on pinhole TextCntrl |
---|
| 2518 | to update the changes |
---|
| 2519 | |
---|
| 2520 | :return: False when wrong value was entered |
---|
| 2521 | |
---|
| 2522 | """ |
---|
| 2523 | # msg default |
---|
| 2524 | msg = None |
---|
| 2525 | # check if any value is changed |
---|
| 2526 | if self._is_changed_slit(): |
---|
| 2527 | msg = self._set_slit_smear() |
---|
[c8e1996] | 2528 | # self._undo.Enable(True) |
---|
[682c432] | 2529 | self.save_current_state() |
---|
| 2530 | |
---|
[c8e1996] | 2531 | if msg is not None: |
---|
[682c432] | 2532 | return False |
---|
| 2533 | else: |
---|
| 2534 | return True |
---|
| 2535 | |
---|
| 2536 | def onSmear(self, event): |
---|
| 2537 | """ |
---|
| 2538 | Create a smear object that will change the way residuals |
---|
[1c2bf90] | 2539 | are computed when fitting |
---|
[682c432] | 2540 | """ |
---|
[c8e1996] | 2541 | if event is not None: |
---|
[682c432] | 2542 | event.Skip() |
---|
| 2543 | if self.data is None: |
---|
| 2544 | return |
---|
| 2545 | |
---|
| 2546 | # Need update param values |
---|
| 2547 | self._update_paramv_on_fit() |
---|
[373d4ee] | 2548 | if self.model is not None: |
---|
[682c432] | 2549 | if self.data.is_data: |
---|
| 2550 | self._manager.page_finder[self.uid].add_data(data=self.data) |
---|
| 2551 | temp_smearer = self.on_smear_helper() |
---|
| 2552 | |
---|
| 2553 | self.sizer_set_smearer.Layout() |
---|
[c8e1996] | 2554 | # we need FitInside here not just self.Layout to ensure all the sizers |
---|
| 2555 | # end up with the necessasary space to in the scroll panel. In |
---|
| 2556 | # particular the compute and fit buttons end up on top of each other |
---|
| 2557 | # PDB Nov 28 2015. |
---|
[1c2bf90] | 2558 | self.FitInside() |
---|
[682c432] | 2559 | self._set_weight() |
---|
| 2560 | |
---|
[c8e1996] | 2561 | # set smearing value whether or not the data contain the smearing info |
---|
[373d4ee] | 2562 | enable_smearer = not self.disable_smearer.GetValue() |
---|
[682c432] | 2563 | wx.CallAfter(self._manager.set_smearer, uid=self.uid, |
---|
| 2564 | smearer=temp_smearer, |
---|
| 2565 | fid=self.data.id, |
---|
| 2566 | qmin=float(self.qmin_x), |
---|
| 2567 | qmax=float(self.qmax_x), |
---|
[934ce649] | 2568 | enable_smearer=enable_smearer) |
---|
[682c432] | 2569 | |
---|
| 2570 | self.state.enable_smearer = self.enable_smearer.GetValue() |
---|
| 2571 | self.state.disable_smearer = self.disable_smearer.GetValue() |
---|
| 2572 | self.state.pinhole_smearer = self.pinhole_smearer.GetValue() |
---|
| 2573 | self.state.slit_smearer = self.slit_smearer.GetValue() |
---|
| 2574 | |
---|
| 2575 | def on_smear_helper(self, update=False): |
---|
| 2576 | """ |
---|
| 2577 | Help for onSmear |
---|
| 2578 | |
---|
| 2579 | :param update: force or not to update |
---|
| 2580 | """ |
---|
| 2581 | self._get_smear_info() |
---|
[c8e1996] | 2582 | # renew smear sizer |
---|
[373d4ee] | 2583 | if self.smear_type is not None: |
---|
[682c432] | 2584 | self.smear_description_smear_type.SetValue(str(self.smear_type)) |
---|
| 2585 | self.smear_data_left.SetValue(str(self.dq_l)) |
---|
| 2586 | self.smear_data_right.SetValue(str(self.dq_r)) |
---|
| 2587 | |
---|
| 2588 | self._hide_all_smear_info() |
---|
| 2589 | data = copy.deepcopy(self.data) |
---|
| 2590 | |
---|
| 2591 | # make sure once more if it is smearer |
---|
| 2592 | temp_smearer = smear_selection(data, self.model) |
---|
| 2593 | if self.current_smearer != temp_smearer or update: |
---|
| 2594 | self.current_smearer = temp_smearer |
---|
| 2595 | if self.enable_smearer.GetValue(): |
---|
[373d4ee] | 2596 | if self.current_smearer is None: |
---|
[682c432] | 2597 | wx.PostEvent(self._manager.parent, |
---|
| 2598 | StatusEvent(status="Data contains no smearing information")) |
---|
| 2599 | else: |
---|
| 2600 | wx.PostEvent(self._manager.parent, |
---|
| 2601 | StatusEvent(status="Data contains smearing information")) |
---|
| 2602 | |
---|
| 2603 | self.smear_data_left.Show(True) |
---|
| 2604 | self.smear_data_right.Show(True) |
---|
| 2605 | temp_smearer = self.current_smearer |
---|
| 2606 | elif self.disable_smearer.GetValue(): |
---|
| 2607 | self.smear_description_none.Show(True) |
---|
| 2608 | elif self.pinhole_smearer.GetValue(): |
---|
| 2609 | self.onPinholeSmear(None) |
---|
| 2610 | elif self.slit_smearer.GetValue(): |
---|
| 2611 | self.onSlitSmear(None) |
---|
| 2612 | self._show_smear_sizer() |
---|
| 2613 | |
---|
| 2614 | return temp_smearer |
---|
| 2615 | |
---|
| 2616 | def on_complete_chisqr(self, event): |
---|
| 2617 | """ |
---|
| 2618 | Display result chisqr on the panel |
---|
| 2619 | :event: activated by fitting/ complete after draw |
---|
| 2620 | """ |
---|
| 2621 | try: |
---|
[c8e1996] | 2622 | if event is None: |
---|
[682c432] | 2623 | output = "-" |
---|
[9a5097c] | 2624 | elif not np.isfinite(event.output): |
---|
[682c432] | 2625 | output = "-" |
---|
| 2626 | else: |
---|
| 2627 | output = event.output |
---|
| 2628 | self.tcChi.SetValue(str(format_number(output, True))) |
---|
| 2629 | self.state.tcChi = self.tcChi.GetValue() |
---|
| 2630 | except: |
---|
| 2631 | pass |
---|
| 2632 | |
---|
| 2633 | def get_all_checked_params(self): |
---|
| 2634 | """ |
---|
| 2635 | Found all parameters current check and add them to list of parameters |
---|
| 2636 | to fit |
---|
| 2637 | """ |
---|
| 2638 | self.param_toFit = [] |
---|
| 2639 | for item in self.parameters: |
---|
| 2640 | if item[0].GetValue() and item not in self.param_toFit: |
---|
| 2641 | if item[0].IsShown(): |
---|
| 2642 | self.param_toFit.append(item) |
---|
| 2643 | for item in self.fittable_param: |
---|
| 2644 | if item[0].GetValue() and item not in self.param_toFit: |
---|
| 2645 | if item[0].IsShown(): |
---|
| 2646 | self.param_toFit.append(item) |
---|
| 2647 | self.save_current_state_fit() |
---|
| 2648 | |
---|
| 2649 | event = PageInfoEvent(page=self) |
---|
| 2650 | wx.PostEvent(self.parent, event) |
---|
| 2651 | param2fit = [] |
---|
| 2652 | for item in self.param_toFit: |
---|
| 2653 | if item[0] and item[0].IsShown(): |
---|
| 2654 | param2fit.append(item[1]) |
---|
| 2655 | self._manager.set_param2fit(self.uid, param2fit) |
---|
| 2656 | |
---|
[c8e1996] | 2657 | def select_param(self, event=None): |
---|
[682c432] | 2658 | """ |
---|
| 2659 | Select TextCtrl checked for fitting purpose and stores them |
---|
| 2660 | in self.param_toFit=[] list |
---|
| 2661 | """ |
---|
| 2662 | self.param_toFit = [] |
---|
| 2663 | for item in self.parameters: |
---|
[c8e1996] | 2664 | # Skip t ifhe angle parameters if 1D data |
---|
[682c432] | 2665 | if self.data.__class__.__name__ != "Data2D" and\ |
---|
| 2666 | not self.enable2D: |
---|
| 2667 | if item in self.orientation_params: |
---|
| 2668 | continue |
---|
[c8e1996] | 2669 | # Select parameters to fit for list of primary parameters |
---|
[682c432] | 2670 | if item[0].GetValue() and item[0].IsShown(): |
---|
| 2671 | if not (item in self.param_toFit): |
---|
| 2672 | self.param_toFit.append(item) |
---|
| 2673 | else: |
---|
| 2674 | #remove parameters from the fitting list |
---|
| 2675 | if item in self.param_toFit: |
---|
| 2676 | self.param_toFit.remove(item) |
---|
| 2677 | |
---|
[c8e1996] | 2678 | # Select parameters to fit for list of fittable parameters |
---|
[682c432] | 2679 | # with dispersion |
---|
| 2680 | for item in self.fittable_param: |
---|
[c8e1996] | 2681 | # Skip t ifhe angle parameters if 1D data |
---|
[682c432] | 2682 | if self.data.__class__.__name__ != "Data2D" and\ |
---|
| 2683 | not self.enable2D: |
---|
| 2684 | if item in self.orientation_params: |
---|
| 2685 | continue |
---|
| 2686 | if item[0].GetValue() and item[0].IsShown(): |
---|
| 2687 | if not (item in self.param_toFit): |
---|
| 2688 | self.param_toFit.append(item) |
---|
| 2689 | else: |
---|
[c8e1996] | 2690 | # remove parameters from the fitting list |
---|
[682c432] | 2691 | if item in self.param_toFit: |
---|
| 2692 | self.param_toFit.remove(item) |
---|
| 2693 | |
---|
[c8e1996] | 2694 | # Calculate num. of angle parameters |
---|
[682c432] | 2695 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
| 2696 | self.enable2D: |
---|
| 2697 | len_orient_para = 0 |
---|
| 2698 | else: |
---|
| 2699 | len_orient_para = len(self.orientation_params) # assume even len |
---|
[c8e1996] | 2700 | # Total num. of angle parameters |
---|
[682c432] | 2701 | if len(self.fittable_param) > 0: |
---|
| 2702 | len_orient_para *= 2 |
---|
| 2703 | |
---|
| 2704 | self.save_current_state_fit() |
---|
[c8e1996] | 2705 | if event is not None: |
---|
| 2706 | # post state to fit panel |
---|
[682c432] | 2707 | event = PageInfoEvent(page=self) |
---|
| 2708 | wx.PostEvent(self.parent, event) |
---|
| 2709 | |
---|
| 2710 | param2fit = [] |
---|
| 2711 | for item in self.param_toFit: |
---|
| 2712 | if item[0] and item[0].IsShown(): |
---|
| 2713 | param2fit.append(item[1]) |
---|
| 2714 | self._manager.set_param2fit(self.uid, param2fit) |
---|
| 2715 | |
---|
| 2716 | def set_model_param_sizer(self, model): |
---|
| 2717 | """ |
---|
| 2718 | Build the panel from the model content |
---|
| 2719 | |
---|
| 2720 | :param model: the model selected in combo box for fitting purpose |
---|
| 2721 | |
---|
| 2722 | """ |
---|
| 2723 | self.sizer3.Clear(True) |
---|
| 2724 | self.parameters = [] |
---|
| 2725 | self.str_parameters = [] |
---|
| 2726 | self.param_toFit = [] |
---|
| 2727 | self.fittable_param = [] |
---|
| 2728 | self.fixed_param = [] |
---|
| 2729 | self.orientation_params = [] |
---|
| 2730 | self.orientation_params_disp = [] |
---|
| 2731 | |
---|
[c8e1996] | 2732 | if model is None: |
---|
[682c432] | 2733 | self.sizer3.Layout() |
---|
| 2734 | self.SetupScrolling() |
---|
| 2735 | return |
---|
| 2736 | |
---|
[6f16e25] | 2737 | box_description = wx.StaticBox(self, wx.ID_ANY, str("Model Parameters")) |
---|
[682c432] | 2738 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
| 2739 | sizer = wx.GridBagSizer(5, 5) |
---|
[c8e1996] | 2740 | # save the current model |
---|
[682c432] | 2741 | self.model = model |
---|
| 2742 | |
---|
| 2743 | keys = self.model.getParamList() |
---|
| 2744 | |
---|
[c8e1996] | 2745 | # list of dispersion parameters |
---|
[682c432] | 2746 | self.disp_list = self.model.getDispParamList() |
---|
| 2747 | |
---|
| 2748 | def custom_compare(a, b): |
---|
| 2749 | """ |
---|
| 2750 | Custom compare to order, first by alphabets then second by number. |
---|
| 2751 | """ |
---|
| 2752 | # number at the last digit |
---|
| 2753 | a_last = a[len(a) - 1] |
---|
| 2754 | b_last = b[len(b) - 1] |
---|
| 2755 | # default |
---|
| 2756 | num_a = None |
---|
| 2757 | num_b = None |
---|
| 2758 | # split the names |
---|
| 2759 | a2 = a.lower().split('_') |
---|
| 2760 | b2 = b.lower().split('_') |
---|
| 2761 | # check length of a2, b2 |
---|
| 2762 | len_a2 = len(a2) |
---|
| 2763 | len_b2 = len(b2) |
---|
| 2764 | # check if it contains a int number(<10) |
---|
| 2765 | try: |
---|
| 2766 | num_a = int(a_last) |
---|
| 2767 | except: |
---|
| 2768 | pass |
---|
| 2769 | try: |
---|
| 2770 | num_b = int(b_last) |
---|
| 2771 | except: |
---|
| 2772 | pass |
---|
| 2773 | # Put 'scale' near the top; happens |
---|
| 2774 | # when numbered param name exists |
---|
| 2775 | if a == 'scale': |
---|
| 2776 | return -1 |
---|
| 2777 | # both have a number |
---|
[c8e1996] | 2778 | if num_a is not None and num_b is not None: |
---|
[682c432] | 2779 | if num_a > num_b: |
---|
| 2780 | return -1 |
---|
| 2781 | # same number |
---|
| 2782 | elif num_a == num_b: |
---|
| 2783 | # different last names |
---|
| 2784 | if a2[len_a2 - 1] != b2[len_b2 - 1] and num_a != 0: |
---|
| 2785 | return -cmp(a2[len_a2 - 1], b2[len_b2 - 1]) |
---|
| 2786 | else: |
---|
| 2787 | return cmp(a, b) |
---|
| 2788 | else: |
---|
| 2789 | return 1 |
---|
| 2790 | # one of them has a number |
---|
[c8e1996] | 2791 | elif num_a is not None: |
---|
[682c432] | 2792 | return 1 |
---|
[c8e1996] | 2793 | elif num_b is not None: |
---|
[682c432] | 2794 | return -1 |
---|
| 2795 | # no numbers |
---|
| 2796 | else: |
---|
| 2797 | return cmp(a.lower(), b.lower()) |
---|
[50fcb09] | 2798 | |
---|
[c8e1996] | 2799 | # keys obtained now from ordered dict, so commenting alphabetical |
---|
| 2800 | # ordering keys.sort(custom_compare) |
---|
[682c432] | 2801 | |
---|
| 2802 | iy = 0 |
---|
| 2803 | ix = 0 |
---|
[4c3be25] | 2804 | sizer.Add(wx.StaticText(self, wx.ID_ANY, 'Parameter'), |
---|
| 2805 | (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
[682c432] | 2806 | ix += 1 |
---|
[6f16e25] | 2807 | self.text2_2 = wx.StaticText(self, wx.ID_ANY, 'Value') |
---|
[c8e1996] | 2808 | sizer.Add(self.text2_2, (iy, ix), (1, 1), |
---|
[682c432] | 2809 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 2810 | ix += 2 |
---|
[6f16e25] | 2811 | self.text2_3 = wx.StaticText(self, wx.ID_ANY, 'Error') |
---|
[c8e1996] | 2812 | sizer.Add(self.text2_3, (iy, ix), (1, 1), |
---|
| 2813 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
[682c432] | 2814 | if not self.is_mac: |
---|
| 2815 | self.text2_3.Hide() |
---|
| 2816 | ix += 1 |
---|
[6f16e25] | 2817 | self.text2_min = wx.StaticText(self, wx.ID_ANY, 'Min') |
---|
[c8e1996] | 2818 | sizer.Add(self.text2_min, (iy, ix), (1, 1), |
---|
| 2819 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 2820 | # self.text2_min.Hide() |
---|
[682c432] | 2821 | ix += 1 |
---|
[6f16e25] | 2822 | self.text2_max = wx.StaticText(self, wx.ID_ANY, 'Max') |
---|
[c8e1996] | 2823 | sizer.Add(self.text2_max, (iy, ix), (1, 1), |
---|
| 2824 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 2825 | # self.text2_max.Hide() |
---|
[682c432] | 2826 | ix += 1 |
---|
[6f16e25] | 2827 | self.text2_4 = wx.StaticText(self, wx.ID_ANY, '[Units]') |
---|
[c8e1996] | 2828 | sizer.Add(self.text2_4, (iy, ix), (1, 1), |
---|
| 2829 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
[682c432] | 2830 | self.text2_4.Hide() |
---|
| 2831 | |
---|
[4c3be25] | 2832 | CHECK_STATE = False |
---|
[682c432] | 2833 | for item in keys: |
---|
| 2834 | |
---|
[c8e1996] | 2835 | if item not in self.disp_list and not item in \ |
---|
[682c432] | 2836 | self.model.orientation_params: |
---|
| 2837 | |
---|
[c8e1996] | 2838 | # prepare a spot to store errors |
---|
| 2839 | if item not in self.model.details: |
---|
[682c432] | 2840 | self.model.details[item] = ["", None, None] |
---|
| 2841 | |
---|
| 2842 | iy += 1 |
---|
| 2843 | ix = 0 |
---|
[c8e1996] | 2844 | if (self.model.__class__ in |
---|
| 2845 | self.model_list_box["Multi-Functions"] or |
---|
[682c432] | 2846 | self.temp_multi_functional)\ |
---|
| 2847 | and (item in self.model.non_fittable): |
---|
[6f16e25] | 2848 | non_fittable_name = wx.StaticText(self, wx.ID_ANY, item) |
---|
[c8e1996] | 2849 | sizer.Add(non_fittable_name, (iy, ix), (1, 1), |
---|
[682c432] | 2850 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 21) |
---|
[c8e1996] | 2851 | # add parameter value |
---|
[682c432] | 2852 | ix += 1 |
---|
| 2853 | value = self.model.getParam(item) |
---|
| 2854 | if len(self.model.fun_list) > 0: |
---|
[c8e1996] | 2855 | # num = item.split('_')[1][5:7] |
---|
[6f16e25] | 2856 | fun_box = wx.ComboBox(self, wx.ID_ANY, size=(100, -1), |
---|
[682c432] | 2857 | style=wx.CB_READONLY, name='%s' % item) |
---|
| 2858 | self._set_fun_box_list(fun_box) |
---|
| 2859 | fun_box.SetSelection(0) |
---|
[c8e1996] | 2860 | # self.fun_box.SetToolTipString("A function |
---|
[682c432] | 2861 | # describing the interface") |
---|
[6f16e25] | 2862 | wx.EVT_COMBOBOX(fun_box, wx.ID_ANY, self._on_fun_box) |
---|
[682c432] | 2863 | else: |
---|
[6f16e25] | 2864 | fun_box = ModelTextCtrl(self, wx.ID_ANY, |
---|
[373d4ee] | 2865 | size=(_BOX_WIDTH, 20), |
---|
[682c432] | 2866 | style=wx.TE_PROCESS_ENTER, name='%s' % item) |
---|
[c8e1996] | 2867 | fun_box.SetToolTipString( |
---|
[682c432] | 2868 | "Hit 'Enter' after typing to update the plot.") |
---|
| 2869 | fun_box.SetValue(format_number(value, True)) |
---|
| 2870 | sizer.Add(fun_box, (iy, ix), (1, 1), wx.EXPAND) |
---|
| 2871 | self.str_parameters.append([None, item, fun_box, |
---|
| 2872 | None, None, None, |
---|
| 2873 | None, None]) |
---|
| 2874 | else: |
---|
[c8e1996] | 2875 | # add parameters name with checkbox for selecting to fit |
---|
[6f16e25] | 2876 | cb = wx.CheckBox(self, wx.ID_ANY, item) |
---|
[682c432] | 2877 | cb.SetValue(CHECK_STATE) |
---|
| 2878 | cb.SetToolTipString(" Check mark to fit.") |
---|
[c8e1996] | 2879 | # cb.SetValue(True) |
---|
[682c432] | 2880 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
| 2881 | |
---|
| 2882 | sizer.Add(cb, (iy, ix), (1, 1), |
---|
| 2883 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 5) |
---|
| 2884 | |
---|
[c8e1996] | 2885 | # add parameter value |
---|
[682c432] | 2886 | ix += 1 |
---|
| 2887 | value = self.model.getParam(item) |
---|
[6f16e25] | 2888 | ctl1 = ModelTextCtrl(self, wx.ID_ANY, size=(_BOX_WIDTH, 20), |
---|
[373d4ee] | 2889 | style=wx.TE_PROCESS_ENTER) |
---|
[c8e1996] | 2890 | ctl1.SetToolTipString( |
---|
[682c432] | 2891 | "Hit 'Enter' after typing to update the plot.") |
---|
| 2892 | ctl1.SetValue(format_number(value, True)) |
---|
| 2893 | sizer.Add(ctl1, (iy, ix), (1, 1), wx.EXPAND) |
---|
[c8e1996] | 2894 | # text to show error sign |
---|
[682c432] | 2895 | ix += 1 |
---|
[6f16e25] | 2896 | text2 = wx.StaticText(self, wx.ID_ANY, '+/-') |
---|
[c8e1996] | 2897 | sizer.Add(text2, (iy, ix), (1, 1), |
---|
[682c432] | 2898 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 2899 | if not self.is_mac: |
---|
| 2900 | text2.Hide() |
---|
| 2901 | ix += 1 |
---|
[6f16e25] | 2902 | ctl2 = wx.TextCtrl(self, wx.ID_ANY, |
---|
[682c432] | 2903 | size=(_BOX_WIDTH / 1.2, 20), style=0) |
---|
| 2904 | sizer.Add(ctl2, (iy, ix), (1, 1), |
---|
| 2905 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 2906 | if not self.is_mac: |
---|
| 2907 | ctl2.Hide() |
---|
| 2908 | |
---|
| 2909 | ix += 1 |
---|
[6f16e25] | 2910 | ctl3 = ModelTextCtrl(self, wx.ID_ANY, |
---|
[373d4ee] | 2911 | size=(_BOX_WIDTH / 1.9, 20), |
---|
| 2912 | style=wx.TE_PROCESS_ENTER, |
---|
[682c432] | 2913 | text_enter_callback=self._onparamRangeEnter) |
---|
| 2914 | min_bound = self.model.details[item][1] |
---|
| 2915 | if min_bound is not None: |
---|
| 2916 | ctl3.SetValue(format_number(min_bound, True)) |
---|
| 2917 | |
---|
| 2918 | sizer.Add(ctl3, (iy, ix), (1, 1), |
---|
| 2919 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 2920 | |
---|
| 2921 | ix += 1 |
---|
[6f16e25] | 2922 | ctl4 = ModelTextCtrl(self, wx.ID_ANY, |
---|
[373d4ee] | 2923 | size=(_BOX_WIDTH / 1.9, 20), |
---|
| 2924 | style=wx.TE_PROCESS_ENTER, |
---|
[682c432] | 2925 | text_enter_callback=self._onparamRangeEnter) |
---|
| 2926 | max_bound = self.model.details[item][2] |
---|
| 2927 | if max_bound is not None: |
---|
| 2928 | ctl4.SetValue(format_number(max_bound, True)) |
---|
| 2929 | sizer.Add(ctl4, (iy, ix), (1, 1), |
---|
| 2930 | wx.EXPAND | wx.FIXED_MINSIZE, 0) |
---|
| 2931 | |
---|
| 2932 | ix += 1 |
---|
| 2933 | # Units |
---|
| 2934 | if item in self.model.details: |
---|
[6f16e25] | 2935 | units = wx.StaticText(self, wx.ID_ANY, |
---|
[682c432] | 2936 | self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
| 2937 | else: |
---|
[6f16e25] | 2938 | units = wx.StaticText(self, wx.ID_ANY, "", |
---|
[682c432] | 2939 | style=wx.ALIGN_LEFT) |
---|
| 2940 | sizer.Add(units, (iy, ix), (1, 1), |
---|
| 2941 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 2942 | |
---|
| 2943 | self.parameters.append([cb, item, ctl1, |
---|
| 2944 | text2, ctl2, ctl3, ctl4, units]) |
---|
| 2945 | |
---|
| 2946 | iy += 1 |
---|
| 2947 | sizer.Add((10, 10), (iy, ix), (1, 1), |
---|
| 2948 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 2949 | |
---|
| 2950 | # type can be either Guassian or Array |
---|
| 2951 | if len(self.model.dispersion.values()) > 0: |
---|
| 2952 | type = self.model.dispersion.values()[0]["type"] |
---|
| 2953 | else: |
---|
| 2954 | type = "Gaussian" |
---|
| 2955 | |
---|
| 2956 | iy += 1 |
---|
| 2957 | ix = 0 |
---|
[c8e1996] | 2958 | # Add tile for orientational angle |
---|
[682c432] | 2959 | for item in keys: |
---|
| 2960 | if item in self.model.orientation_params: |
---|
[6f16e25] | 2961 | orient_angle = wx.StaticText(self, wx.ID_ANY, '[For 2D only]:') |
---|
| 2962 | mag_on_button = wx.Button(self, wx.ID_ANY, "Magnetic ON") |
---|
[7116dffd] | 2963 | mag_on_button.SetToolTipString("Turn Pol Beam/Mag scatt on/off") |
---|
[682c432] | 2964 | mag_on_button.Bind(wx.EVT_BUTTON, self._on_mag_on) |
---|
[c8e1996] | 2965 | mag_angle_help_button = wx.Button(self, wx.ID_ANY, |
---|
| 2966 | "Magnetic angles?") |
---|
[7116dffd] | 2967 | mag_angle_help_button.SetToolTipString("see angle definitions") |
---|
[6f16e25] | 2968 | mag_help_button = wx.Button(self, wx.ID_ANY, "Mag HELP") |
---|
[7116dffd] | 2969 | mag_help_button.SetToolTipString("Help on pol beam/mag fitting") |
---|
[682c432] | 2970 | mag_help_button.Bind(wx.EVT_BUTTON, self._on_mag_help) |
---|
[c8e1996] | 2971 | mag_angle_help_button.Bind(wx.EVT_BUTTON, |
---|
[7116dffd] | 2972 | self._on_mag_angle_help) |
---|
[682c432] | 2973 | sizer.Add(orient_angle, (iy, ix), (1, 1), |
---|
| 2974 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 2975 | iy += 1 |
---|
| 2976 | sizer.Add(mag_on_button, (iy, ix), (1, 1), |
---|
| 2977 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
[7116dffd] | 2978 | ix += 1 |
---|
| 2979 | sizer.Add(mag_angle_help_button, (iy, ix), (1, 1), |
---|
| 2980 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
[682c432] | 2981 | sizer.Add(mag_help_button, (iy, ix + 1), (1, 1), |
---|
| 2982 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 2983 | |
---|
[c8e1996] | 2984 | # handle the magnetic buttons |
---|
| 2985 | # clean this up so that assume mag is off then turn |
---|
| 2986 | # all buttons on IF mag has mag and has 2D |
---|
[682c432] | 2987 | if not self._has_magnetic: |
---|
| 2988 | mag_on_button.Show(False) |
---|
| 2989 | elif not self.data.__class__.__name__ == "Data2D": |
---|
| 2990 | mag_on_button.Show(False) |
---|
| 2991 | else: |
---|
| 2992 | mag_on_button.Show(True) |
---|
| 2993 | mag_help_button.Show(False) |
---|
[2b58fa5] | 2994 | mag_angle_help_button.Show(False) |
---|
[682c432] | 2995 | if mag_on_button.IsShown(): |
---|
| 2996 | if self.magnetic_on: |
---|
| 2997 | mag_on_button.SetLabel("Magnetic OFF") |
---|
| 2998 | mag_help_button.Show(True) |
---|
[7116dffd] | 2999 | mag_angle_help_button.Show(True) |
---|
[682c432] | 3000 | else: |
---|
| 3001 | mag_on_button.SetLabel("Magnetic ON") |
---|
| 3002 | mag_help_button.Show(False) |
---|
[7116dffd] | 3003 | mag_angle_help_button.Show(False) |
---|
[682c432] | 3004 | |
---|
| 3005 | if not self.data.__class__.__name__ == "Data2D" and \ |
---|
| 3006 | not self.enable2D: |
---|
| 3007 | orient_angle.Hide() |
---|
| 3008 | else: |
---|
| 3009 | orient_angle.Show(True) |
---|
| 3010 | break |
---|
| 3011 | |
---|
[c8e1996] | 3012 | # For Gaussian only |
---|
[682c432] | 3013 | if type.lower() != "array": |
---|
| 3014 | for item in self.model.orientation_params: |
---|
| 3015 | if not self.magnetic_on: |
---|
| 3016 | if item in self.model.magnetic_params: |
---|
| 3017 | continue |
---|
[c8e1996] | 3018 | if item not in self.disp_list: |
---|
| 3019 | # prepare a spot to store min max |
---|
| 3020 | if item not in self.model.details: |
---|
[682c432] | 3021 | self.model.details[item] = ["", None, None] |
---|
| 3022 | |
---|
| 3023 | iy += 1 |
---|
| 3024 | ix = 0 |
---|
[c8e1996] | 3025 | # add parameters name with checkbox for selecting to fit |
---|
[6f16e25] | 3026 | cb = wx.CheckBox(self, wx.ID_ANY, item) |
---|
[682c432] | 3027 | cb.SetValue(CHECK_STATE) |
---|
| 3028 | cb.SetToolTipString("Check mark to fit") |
---|
| 3029 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
| 3030 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
| 3031 | self.enable2D: |
---|
| 3032 | cb.Show(True) |
---|
| 3033 | else: |
---|
| 3034 | cb.Hide() |
---|
| 3035 | sizer.Add(cb, (iy, ix), (1, 1), |
---|
| 3036 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 5) |
---|
| 3037 | |
---|
[c8e1996] | 3038 | # add parameter value |
---|
[682c432] | 3039 | ix += 1 |
---|
| 3040 | value = self.model.getParam(item) |
---|
[373d4ee] | 3041 | ctl1 = ModelTextCtrl(self, -1, size=(_BOX_WIDTH, 20), |
---|
| 3042 | style=wx.TE_PROCESS_ENTER) |
---|
[c8e1996] | 3043 | ctl1.SetToolTipString( |
---|
[682c432] | 3044 | "Hit 'Enter' after typing to update the plot.") |
---|
| 3045 | ctl1.SetValue(format_number(value, True)) |
---|
| 3046 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
| 3047 | self.enable2D: |
---|
| 3048 | ctl1.Show(True) |
---|
| 3049 | else: |
---|
| 3050 | ctl1.Hide() |
---|
| 3051 | sizer.Add(ctl1, (iy, ix), (1, 1), wx.EXPAND) |
---|
[c8e1996] | 3052 | # text to show error sign |
---|
[682c432] | 3053 | ix += 1 |
---|
| 3054 | text2 = wx.StaticText(self, -1, '+/-') |
---|
[c8e1996] | 3055 | sizer.Add(text2, (iy, ix), (1, 1), |
---|
[682c432] | 3056 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 3057 | |
---|
| 3058 | text2.Hide() |
---|
| 3059 | ix += 1 |
---|
| 3060 | ctl2 = wx.TextCtrl(self, -1, |
---|
| 3061 | size=(_BOX_WIDTH / 1.2, 20), style=0) |
---|
| 3062 | sizer.Add(ctl2, (iy, ix), (1, 1), |
---|
| 3063 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 3064 | |
---|
| 3065 | ctl2.Hide() |
---|
| 3066 | |
---|
| 3067 | ix += 1 |
---|
[373d4ee] | 3068 | ctl3 = ModelTextCtrl(self, -1, |
---|
| 3069 | size=(_BOX_WIDTH / 1.8, 20), |
---|
| 3070 | style=wx.TE_PROCESS_ENTER, |
---|
[682c432] | 3071 | text_enter_callback=self._onparamRangeEnter) |
---|
| 3072 | |
---|
| 3073 | sizer.Add(ctl3, (iy, ix), (1, 1), |
---|
| 3074 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 3075 | ctl3.Hide() |
---|
| 3076 | |
---|
| 3077 | ix += 1 |
---|
[373d4ee] | 3078 | ctl4 = ModelTextCtrl(self, -1, |
---|
| 3079 | size=(_BOX_WIDTH / 1.8, 20), |
---|
| 3080 | style=wx.TE_PROCESS_ENTER, |
---|
[682c432] | 3081 | text_enter_callback=self._onparamRangeEnter) |
---|
| 3082 | sizer.Add(ctl4, (iy, ix), (1, 1), |
---|
| 3083 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 3084 | |
---|
| 3085 | ctl4.Hide() |
---|
| 3086 | |
---|
| 3087 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
| 3088 | self.enable2D: |
---|
| 3089 | if self.is_mac: |
---|
| 3090 | text2.Show(True) |
---|
| 3091 | ctl2.Show(True) |
---|
| 3092 | ctl3.Show(True) |
---|
| 3093 | ctl4.Show(True) |
---|
| 3094 | |
---|
| 3095 | ix += 1 |
---|
| 3096 | # Units |
---|
| 3097 | if item in self.model.details: |
---|
| 3098 | units = wx.StaticText(self, -1, |
---|
| 3099 | self.model.details[item][0], |
---|
| 3100 | style=wx.ALIGN_LEFT) |
---|
| 3101 | else: |
---|
| 3102 | units = wx.StaticText(self, -1, "", |
---|
| 3103 | style=wx.ALIGN_LEFT) |
---|
| 3104 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
| 3105 | self.enable2D: |
---|
| 3106 | units.Show(True) |
---|
| 3107 | else: |
---|
| 3108 | units.Hide() |
---|
| 3109 | |
---|
| 3110 | sizer.Add(units, (iy, ix), (1, 1), |
---|
| 3111 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 3112 | |
---|
| 3113 | self.parameters.append([cb, item, ctl1, |
---|
| 3114 | text2, ctl2, ctl3, ctl4, units]) |
---|
| 3115 | self.orientation_params.append([cb, item, ctl1, |
---|
| 3116 | text2, ctl2, ctl3, ctl4, units]) |
---|
| 3117 | |
---|
| 3118 | iy += 1 |
---|
| 3119 | box_description.SetForegroundColour(wx.BLUE) |
---|
[c8e1996] | 3120 | # Display units text on panel |
---|
[682c432] | 3121 | for item in keys: |
---|
| 3122 | if item in self.model.details: |
---|
| 3123 | self.text2_4.Show() |
---|
[c8e1996] | 3124 | # Fill the list of fittable parameters |
---|
[682c432] | 3125 | self.get_all_checked_params() |
---|
| 3126 | self.save_current_state_fit() |
---|
| 3127 | boxsizer1.Add(sizer) |
---|
| 3128 | self.sizer3.Add(boxsizer1, 0, wx.EXPAND | wx.ALL, 10) |
---|
| 3129 | self.sizer3.Layout() |
---|
| 3130 | self.Layout() |
---|
| 3131 | |
---|
| 3132 | def on_right_down(self, event): |
---|
| 3133 | """ |
---|
| 3134 | Get key stroke event |
---|
| 3135 | """ |
---|
[c8e1996] | 3136 | if self.data is None: |
---|
[682c432] | 3137 | return |
---|
| 3138 | # Figuring out key combo: Cmd for copy, Alt for paste |
---|
| 3139 | if event.AltDown() and event.ShiftDown(): |
---|
| 3140 | flag = True |
---|
| 3141 | elif event.AltDown() or event.ShiftDown(): |
---|
| 3142 | flag = False |
---|
| 3143 | else: |
---|
| 3144 | return |
---|
| 3145 | # make event free |
---|
| 3146 | event.Skip() |
---|
| 3147 | # messages depending on the flag |
---|
| 3148 | if not flag: |
---|
| 3149 | infor = 'warning' |
---|
| 3150 | # inform msg to wx |
---|
| 3151 | wx.PostEvent(self._manager.parent, |
---|
| 3152 | StatusEvent(status=msg, info=infor)) |
---|
| 3153 | |
---|
| 3154 | def _onModel2D(self, event): |
---|
| 3155 | """ |
---|
| 3156 | toggle view of model from 1D to 2D or 2D from 1D |
---|
| 3157 | """ |
---|
| 3158 | if self.model_view.GetLabelText() == "Show 2D": |
---|
| 3159 | self.model_view.SetLabel("Show 1D") |
---|
| 3160 | self.enable2D = True |
---|
| 3161 | |
---|
| 3162 | else: |
---|
| 3163 | self.model_view.SetLabel("Show 2D") |
---|
| 3164 | self.enable2D = False |
---|
| 3165 | self.Show(False) |
---|
| 3166 | self.create_default_data() |
---|
| 3167 | self._manager.store_data(self.uid, data_list=[self.data]) |
---|
| 3168 | |
---|
| 3169 | self.set_model_param_sizer(self.model) |
---|
| 3170 | self._set_sizer_dispersion() |
---|
| 3171 | self._set_weight(is_2D=self.enable2D) |
---|
| 3172 | self._set_smear_buttons() |
---|
| 3173 | self.Show(True) |
---|
| 3174 | self.SetupScrolling() |
---|
| 3175 | self._draw_model() |
---|
| 3176 | |
---|
| 3177 | self.state.enable2D = copy.deepcopy(self.enable2D) |
---|
| 3178 | |
---|
| 3179 | def _set_smear_buttons(self): |
---|
| 3180 | """ |
---|
| 3181 | Set semarer radio buttons |
---|
| 3182 | """ |
---|
| 3183 | # more disables for 2D |
---|
| 3184 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
| 3185 | self.enable2D: |
---|
| 3186 | self.slit_smearer.Disable() |
---|
| 3187 | self.pinhole_smearer.Enable(True) |
---|
| 3188 | self.default_mask = copy.deepcopy(self.data.mask) |
---|
| 3189 | else: |
---|
| 3190 | self.slit_smearer.Enable(True) |
---|
| 3191 | self.pinhole_smearer.Enable(True) |
---|
| 3192 | |
---|
| 3193 | |
---|
| 3194 | class BGTextCtrl(wx.TextCtrl): |
---|
| 3195 | """ |
---|
| 3196 | Text control used to display outputs. |
---|
| 3197 | No editing allowed. The background is |
---|
| 3198 | grayed out. User can't select text. |
---|
| 3199 | """ |
---|
| 3200 | def __init__(self, *args, **kwds): |
---|
| 3201 | wx.TextCtrl.__init__(self, *args, **kwds) |
---|
| 3202 | self.SetEditable(False) |
---|
| 3203 | self.SetBackgroundColour(self.GetParent().parent.GetBackgroundColour()) |
---|
| 3204 | |
---|
| 3205 | # Bind to mouse event to avoid text highlighting |
---|
| 3206 | # The event will be skipped once the call-back |
---|
| 3207 | # is called. |
---|
| 3208 | self.Bind(wx.EVT_MOUSE_EVENTS, self._click) |
---|
| 3209 | |
---|
| 3210 | def _click(self, event): |
---|
| 3211 | """ |
---|
| 3212 | Prevent further handling of the mouse event |
---|
| 3213 | by not calling Skip(). |
---|
| 3214 | """ |
---|
| 3215 | pass |
---|