[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 | |
---|
[3bd677b] | 1081 | _TreeLocation = "user/sasgui/perspectives/fitting/resolution.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 |
---|
[bfeb823] | 1728 | for index, choice in enumerate(self.model.fun_list): |
---|
| 1729 | fun_box.Append(choice, index) |
---|
[682c432] | 1730 | |
---|
| 1731 | def _on_select_accuracy(self, event): |
---|
| 1732 | """ |
---|
| 1733 | Select an accuracy in 2D custom smear: Xhigh, High, Med, or Low |
---|
| 1734 | """ |
---|
[c8e1996] | 1735 | # event.Skip() |
---|
[682c432] | 1736 | # Check if the accuracy is same as before |
---|
[c8e1996] | 1737 | # self.smear2d_accuracy = event.GetEventObject().GetValue() |
---|
[682c432] | 1738 | self.smear2d_accuracy = self.smear_accuracy.GetValue() |
---|
| 1739 | if self.pinhole_smearer.GetValue(): |
---|
| 1740 | self.onPinholeSmear(event=None) |
---|
| 1741 | else: |
---|
| 1742 | self.onSmear(event=None) |
---|
[c8e1996] | 1743 | if self.current_smearer is not None: |
---|
[682c432] | 1744 | self.current_smearer.set_accuracy(accuracy=\ |
---|
| 1745 | self.smear2d_accuracy) |
---|
| 1746 | event.Skip() |
---|
| 1747 | |
---|
| 1748 | def _on_fun_box(self, event): |
---|
| 1749 | """ |
---|
| 1750 | Select an func: Erf,Rparabola,LParabola |
---|
| 1751 | """ |
---|
| 1752 | fun_val = None |
---|
| 1753 | fun_box = event.GetEventObject() |
---|
| 1754 | name = fun_box.Name |
---|
| 1755 | value = fun_box.GetValue() |
---|
| 1756 | if value in self.model.fun_list: |
---|
[bfeb823] | 1757 | fun_val = self.model.fun_list.index(value) |
---|
[682c432] | 1758 | |
---|
| 1759 | self.model.setParam(name, fun_val) |
---|
| 1760 | # save state |
---|
| 1761 | self._copy_parameters_state(self.str_parameters, |
---|
| 1762 | self.state.str_parameters) |
---|
| 1763 | # update params |
---|
| 1764 | self._update_paramv_on_fit() |
---|
| 1765 | # draw |
---|
| 1766 | self._draw_model() |
---|
| 1767 | self.Refresh() |
---|
| 1768 | # get ready for new event |
---|
| 1769 | event.Skip() |
---|
| 1770 | |
---|
| 1771 | def _onMask(self, event): |
---|
| 1772 | """ |
---|
| 1773 | Build a panel to allow to edit Mask |
---|
| 1774 | """ |
---|
[c8e1996] | 1775 | from sas.sasgui.guiframe.local_perspectives.plotting.masking import \ |
---|
| 1776 | MaskPanel as MaskDialog |
---|
[682c432] | 1777 | |
---|
| 1778 | self.panel = MaskDialog(base=self, data=self.data, id=wx.NewId()) |
---|
| 1779 | self.panel.ShowModal() |
---|
| 1780 | |
---|
| 1781 | def _draw_masked_model(self, event): |
---|
| 1782 | """ |
---|
| 1783 | Draw model image w/mask |
---|
| 1784 | """ |
---|
| 1785 | is_valid_qrange = self._update_paramv_on_fit() |
---|
| 1786 | |
---|
[c8e1996] | 1787 | if is_valid_qrange and self.model is not None: |
---|
[682c432] | 1788 | self.panel.MakeModal(False) |
---|
| 1789 | event.Skip() |
---|
| 1790 | # try re draw the model plot if it exists |
---|
| 1791 | self._draw_model() |
---|
| 1792 | self.show_npts2fit() |
---|
[c8e1996] | 1793 | elif self.model is None: |
---|
[682c432] | 1794 | self.panel.MakeModal(False) |
---|
| 1795 | event.Skip() |
---|
| 1796 | self.show_npts2fit() |
---|
| 1797 | msg = "No model is found on updating MASK in the model plot... " |
---|
| 1798 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
| 1799 | else: |
---|
| 1800 | event.Skip() |
---|
| 1801 | msg = ' Please consider your Q range, too.' |
---|
| 1802 | self.panel.ShowMessage(msg) |
---|
| 1803 | |
---|
| 1804 | def _set_smear(self, data): |
---|
| 1805 | """ |
---|
| 1806 | Set_smear |
---|
| 1807 | """ |
---|
| 1808 | if data is None: |
---|
| 1809 | return |
---|
| 1810 | self.current_smearer = smear_selection(data, self.model) |
---|
| 1811 | flag = self.disable_smearer.GetValue() |
---|
[373d4ee] | 1812 | if self.current_smearer is None: |
---|
[682c432] | 1813 | self.enable_smearer.Disable() |
---|
| 1814 | else: |
---|
| 1815 | self.enable_smearer.Enable() |
---|
| 1816 | if not flag: |
---|
| 1817 | self.onSmear(None) |
---|
| 1818 | |
---|
| 1819 | def get_view_mode(self): |
---|
| 1820 | """ |
---|
| 1821 | return True if the panel allow 2D or False if 1D |
---|
| 1822 | """ |
---|
| 1823 | return self.enable2D |
---|
| 1824 | |
---|
| 1825 | def compute_data_set_range(self, data_list): |
---|
| 1826 | """ |
---|
| 1827 | find the range that include all data in the set |
---|
| 1828 | return the minimum and the maximum values |
---|
| 1829 | """ |
---|
| 1830 | if data_list is not None and data_list != []: |
---|
| 1831 | for data in data_list: |
---|
| 1832 | qmin, qmax, npts = self.compute_data_range(data) |
---|
| 1833 | self.qmin_data_set = min(self.qmin_data_set, qmin) |
---|
| 1834 | self.qmax_data_set = max(self.qmax_data_set, qmax) |
---|
| 1835 | self.npts_data_set += npts |
---|
| 1836 | return self.qmin_data_set, self.qmax_data_set, self.npts_data_set |
---|
| 1837 | |
---|
| 1838 | def compute_data_range(self, data): |
---|
| 1839 | """ |
---|
| 1840 | compute the minimum and the maximum range of the data |
---|
| 1841 | return the npts contains in data |
---|
| 1842 | :param data: |
---|
| 1843 | """ |
---|
| 1844 | qmin, qmax, npts = None, None, None |
---|
| 1845 | if data is not None: |
---|
| 1846 | if not hasattr(data, "data"): |
---|
| 1847 | try: |
---|
| 1848 | qmin = min(data.x) |
---|
| 1849 | # Maximum value of data |
---|
| 1850 | qmax = max(data.x) |
---|
| 1851 | npts = len(data.x) |
---|
| 1852 | except: |
---|
[c8e1996] | 1853 | msg = "Unable to find min/max/length of \n data named %s" %\ |
---|
[682c432] | 1854 | data.filename |
---|
| 1855 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg, |
---|
| 1856 | info="error")) |
---|
| 1857 | raise ValueError, msg |
---|
| 1858 | |
---|
| 1859 | else: |
---|
| 1860 | qmin = 0 |
---|
| 1861 | try: |
---|
| 1862 | x = max(math.fabs(data.xmin), math.fabs(data.xmax)) |
---|
| 1863 | y = max(math.fabs(data.ymin), math.fabs(data.ymax)) |
---|
| 1864 | except: |
---|
| 1865 | msg = "Unable to find min/max of \n data named %s" % \ |
---|
| 1866 | data.filename |
---|
| 1867 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg, |
---|
| 1868 | info="error")) |
---|
| 1869 | raise ValueError, msg |
---|
[c8e1996] | 1870 | # Maximum value of data |
---|
[682c432] | 1871 | qmax = math.sqrt(x * x + y * y) |
---|
| 1872 | npts = len(data.data) |
---|
| 1873 | return qmin, qmax, npts |
---|
| 1874 | |
---|
| 1875 | def set_data(self, data): |
---|
| 1876 | """ |
---|
| 1877 | reset the current data |
---|
| 1878 | """ |
---|
| 1879 | id = None |
---|
| 1880 | flag = False |
---|
| 1881 | is_data = False |
---|
[c8e1996] | 1882 | npts = 0 |
---|
[682c432] | 1883 | try: |
---|
| 1884 | old_id = self.data.id |
---|
| 1885 | old_group_id = self.data.group_id |
---|
| 1886 | except: |
---|
| 1887 | old_id = id |
---|
| 1888 | old_group_id = id |
---|
| 1889 | if self.data is not None: |
---|
| 1890 | is_data = check_data_validity(self.data) |
---|
| 1891 | if not is_data and data is not None: |
---|
| 1892 | flag = True |
---|
| 1893 | if data is not None: |
---|
| 1894 | if is_data: |
---|
| 1895 | self.graph_id = self.data.group_id |
---|
| 1896 | flag = (data.id != self.data.id) |
---|
| 1897 | self.data = data |
---|
| 1898 | if check_data_validity(data): |
---|
| 1899 | self.graph_id = data.group_id |
---|
| 1900 | self.data.group_id = self.graph_id |
---|
| 1901 | |
---|
| 1902 | if self.data is None: |
---|
| 1903 | data_name = "" |
---|
| 1904 | self._set_bookmark_flag(False) |
---|
| 1905 | self._keep.Enable(False) |
---|
| 1906 | self._set_save_flag(False) |
---|
| 1907 | else: |
---|
[c8e1996] | 1908 | if self.model is not None: |
---|
[682c432] | 1909 | self._set_bookmark_flag(not self.batch_on) |
---|
| 1910 | self._keep.Enable(not self.batch_on) |
---|
| 1911 | if self.data.is_data: |
---|
| 1912 | self._set_save_flag(True) |
---|
| 1913 | self._set_preview_flag(True) |
---|
| 1914 | |
---|
| 1915 | # more disables for 2D |
---|
[03137f1] | 1916 | di_flag = False |
---|
| 1917 | dq_flag = False |
---|
[682c432] | 1918 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
| 1919 | self.enable2D: |
---|
| 1920 | self.slit_smearer.Disable() |
---|
| 1921 | self.pinhole_smearer.Enable(True) |
---|
| 1922 | self.default_mask = copy.deepcopy(self.data.mask) |
---|
[3d55219] | 1923 | if self.data.err_data is not None \ |
---|
[9a5097c] | 1924 | and np.any(self.data.err_data): |
---|
[03137f1] | 1925 | di_flag = True |
---|
[3d55219] | 1926 | if self.data.dqx_data is not None \ |
---|
[9a5097c] | 1927 | and np.any(self.data.dqx_data): |
---|
[03137f1] | 1928 | dq_flag = True |
---|
[682c432] | 1929 | else: |
---|
| 1930 | self.slit_smearer.Enable(True) |
---|
| 1931 | self.pinhole_smearer.Enable(True) |
---|
[9a5097c] | 1932 | if self.data.dy is not None and np.any(self.data.dy): |
---|
[03137f1] | 1933 | di_flag = True |
---|
[9a5097c] | 1934 | if self.data.dx is not None and np.any(self.data.dx): |
---|
[3d55219] | 1935 | dq_flag = True |
---|
[9a5097c] | 1936 | elif self.data.dxl is not None and np.any(self.data.dxl): |
---|
[03137f1] | 1937 | dq_flag = True |
---|
| 1938 | |
---|
| 1939 | if dq_flag: |
---|
| 1940 | self.enable_smearer.Enable(True) |
---|
| 1941 | self.enable_smearer.SetValue(True) |
---|
| 1942 | self.disable_smearer.SetValue(False) |
---|
[3d55219] | 1943 | else: |
---|
| 1944 | self.enable_smearer.Disable() |
---|
| 1945 | self.disable_smearer.Enable(True) |
---|
| 1946 | self.disable_smearer.SetValue(True) |
---|
[03137f1] | 1947 | |
---|
| 1948 | if di_flag: |
---|
| 1949 | self.dI_didata.Enable(True) |
---|
| 1950 | self.dI_didata.SetValue(True) |
---|
| 1951 | self.weightbt_string = self.dI_didata.GetLabelText() |
---|
[3d55219] | 1952 | else: |
---|
| 1953 | self.dI_didata.Enable(False) |
---|
| 1954 | self.dI_noweight.SetValue(True) |
---|
| 1955 | self.weightbt_string = self.dI_noweight.GetLabelText() |
---|
[c8e1996] | 1956 | |
---|
| 1957 | # Enable weighting radio buttons |
---|
[682c432] | 1958 | self.dI_noweight.Enable(True) |
---|
| 1959 | self.dI_sqrdata.Enable(True) |
---|
| 1960 | self.dI_idata.Enable(True) |
---|
| 1961 | |
---|
| 1962 | self.formfactorbox.Enable() |
---|
| 1963 | self.structurebox.Enable() |
---|
| 1964 | data_name = self.data.name |
---|
| 1965 | _, _, npts = self.compute_data_range(self.data) |
---|
[c8e1996] | 1966 | # set maximum range for x in linear scale |
---|
[682c432] | 1967 | if not hasattr(self.data, "data"): # Display only for 1D data fit |
---|
| 1968 | self.btEditMask.Disable() |
---|
| 1969 | self.EditMask_title.Disable() |
---|
| 1970 | else: |
---|
| 1971 | self.btEditMask.Enable() |
---|
| 1972 | self.EditMask_title.Enable() |
---|
| 1973 | |
---|
| 1974 | self.Npts_total.SetValue(str(npts)) |
---|
[c8e1996] | 1975 | # default:number of data points selected to fit |
---|
[682c432] | 1976 | self.Npts_fit.SetValue(str(npts)) |
---|
| 1977 | self.Npts_total.SetEditable(False) |
---|
[c8e1996] | 1978 | self.Npts_total.SetBackgroundColour( |
---|
[682c432] | 1979 | self.GetParent().GetBackgroundColour()) |
---|
| 1980 | |
---|
| 1981 | self.Npts_total.Bind(wx.EVT_MOUSE_EVENTS, self._npts_click) |
---|
| 1982 | self.pointsbox.Disable() |
---|
| 1983 | self.dataSource.SetValue(data_name) |
---|
| 1984 | self.state.data = data |
---|
| 1985 | self.enable_fit_button() |
---|
| 1986 | # send graph_id to page_finder |
---|
| 1987 | self._manager.set_graph_id(uid=self.uid, graph_id=self.graph_id) |
---|
[c8e1996] | 1988 | # focus the page |
---|
[682c432] | 1989 | if check_data_validity(data): |
---|
| 1990 | self.data_box_description.SetForegroundColour(wx.BLUE) |
---|
| 1991 | |
---|
| 1992 | if self.batch_on: |
---|
| 1993 | self.slit_smearer.Enable(False) |
---|
| 1994 | self.pinhole_smearer.Enable(False) |
---|
| 1995 | self.btEditMask.Disable() |
---|
| 1996 | self.EditMask_title.Disable() |
---|
| 1997 | |
---|
[2d568f6] | 1998 | self.on_smear_helper() |
---|
[682c432] | 1999 | self.on_set_focus(None) |
---|
| 2000 | self.Refresh() |
---|
[c8e1996] | 2001 | # update model plot with new data information |
---|
[682c432] | 2002 | if flag: |
---|
| 2003 | if self.data.__class__.__name__ == "Data2D": |
---|
| 2004 | self.enable2D = True |
---|
| 2005 | self.model_view.SetLabel("2D Mode") |
---|
| 2006 | else: |
---|
| 2007 | self.enable2D = False |
---|
| 2008 | self.model_view.SetLabel("1D Mode") |
---|
| 2009 | self.model_view.Disable() |
---|
[c8e1996] | 2010 | # replace data plot on combo box selection |
---|
| 2011 | # by removing the previous selected data |
---|
[682c432] | 2012 | try: |
---|
| 2013 | wx.PostEvent(self._manager.parent, |
---|
| 2014 | NewPlotEvent(action="delete", |
---|
| 2015 | group_id=old_group_id, id=old_id)) |
---|
| 2016 | except: |
---|
| 2017 | pass |
---|
[c8e1996] | 2018 | # plot the current selected data |
---|
[682c432] | 2019 | wx.PostEvent(self._manager.parent, |
---|
| 2020 | NewPlotEvent(action="check", plot=self.data, |
---|
| 2021 | title=str(self.data.title))) |
---|
| 2022 | self._draw_model() |
---|
| 2023 | |
---|
| 2024 | def _npts_click(self, event): |
---|
| 2025 | """ |
---|
| 2026 | Prevent further handling of the mouse event on Npts_total |
---|
| 2027 | by not calling Skip(). |
---|
| 2028 | """ |
---|
| 2029 | pass |
---|
| 2030 | |
---|
| 2031 | def reset_page(self, state, first=False): |
---|
| 2032 | """ |
---|
| 2033 | reset the state |
---|
| 2034 | """ |
---|
| 2035 | try: |
---|
| 2036 | self.reset_page_helper(state) |
---|
| 2037 | |
---|
[c8e1996] | 2038 | self.select_param() |
---|
| 2039 | # Save state_fit |
---|
[682c432] | 2040 | self.save_current_state_fit() |
---|
[48154abb] | 2041 | self.onSmear(None) |
---|
| 2042 | self._onDraw(None) |
---|
[682c432] | 2043 | except: |
---|
| 2044 | self._show_combox_helper() |
---|
| 2045 | msg = "Error: This model state has missing or outdated " |
---|
| 2046 | msg += "information.\n" |
---|
[6c382da] | 2047 | msg += traceback.format_exc() |
---|
[682c432] | 2048 | wx.PostEvent(self._manager.parent, |
---|
| 2049 | StatusEvent(status=msg, info="error")) |
---|
| 2050 | self._lay_out() |
---|
| 2051 | self.Refresh() |
---|
| 2052 | |
---|
| 2053 | def get_range(self): |
---|
| 2054 | """ |
---|
| 2055 | return the fitting range |
---|
| 2056 | """ |
---|
| 2057 | return float(self.qmin_x), float(self.qmax_x) |
---|
| 2058 | |
---|
| 2059 | def get_npts2fit(self): |
---|
| 2060 | """ |
---|
| 2061 | return numbers of data points within qrange |
---|
| 2062 | |
---|
| 2063 | :Note: This is to normalize chisq by Npts of fit |
---|
| 2064 | |
---|
| 2065 | """ |
---|
| 2066 | if self.data is None: |
---|
| 2067 | return |
---|
| 2068 | npts2fit = 0 |
---|
| 2069 | qmin, qmax = self.get_range() |
---|
| 2070 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
| 2071 | self.enable2D: |
---|
[9a5097c] | 2072 | radius = np.sqrt(self.data.qx_data * self.data.qx_data + |
---|
[682c432] | 2073 | self.data.qy_data * self.data.qy_data) |
---|
| 2074 | index_data = (self.qmin_x <= radius) & (radius <= self.qmax_x) |
---|
| 2075 | index_data = (index_data) & (self.data.mask) |
---|
[9a5097c] | 2076 | index_data = (index_data) & (np.isfinite(self.data.data)) |
---|
[682c432] | 2077 | npts2fit = len(self.data.data[index_data]) |
---|
| 2078 | else: |
---|
| 2079 | for qx in self.data.x: |
---|
[c8e1996] | 2080 | if qmax >= qx >= qmin: |
---|
[682c432] | 2081 | npts2fit += 1 |
---|
| 2082 | return npts2fit |
---|
| 2083 | |
---|
| 2084 | def show_npts2fit(self): |
---|
| 2085 | """ |
---|
| 2086 | setValue Npts for fitting |
---|
| 2087 | """ |
---|
| 2088 | self.Npts_fit.SetValue(str(self.get_npts2fit())) |
---|
| 2089 | |
---|
| 2090 | def get_chi2(self): |
---|
| 2091 | """ |
---|
| 2092 | return the current chi2 |
---|
| 2093 | """ |
---|
| 2094 | return self.tcChi.GetValue() |
---|
| 2095 | |
---|
| 2096 | def onsetValues(self, chisqr, p_name, out, cov): |
---|
| 2097 | """ |
---|
| 2098 | Build the panel from the fit result |
---|
| 2099 | |
---|
| 2100 | :param chisqr: Value of the goodness of fit metric |
---|
| 2101 | :param p_name: the name of parameters |
---|
| 2102 | :param out: list of parameter with the best value found during fitting |
---|
| 2103 | :param cov: Covariance matrix |
---|
| 2104 | |
---|
| 2105 | """ |
---|
| 2106 | |
---|
| 2107 | # make sure stop button to fit button all the time |
---|
| 2108 | self._on_fit_complete() |
---|
[9a5097c] | 2109 | if out is None or not np.isfinite(chisqr): |
---|
[682c432] | 2110 | raise ValueError, "Fit error occured..." |
---|
| 2111 | |
---|
| 2112 | is_modified = False |
---|
| 2113 | has_error = False |
---|
| 2114 | dispersity = '' |
---|
| 2115 | |
---|
[c8e1996] | 2116 | # Hide textctrl boxes of errors. |
---|
[682c432] | 2117 | self._clear_Err_on_Fit() |
---|
| 2118 | |
---|
[c8e1996] | 2119 | # Check if chi2 is finite |
---|
[9a5097c] | 2120 | if chisqr is not None and np.isfinite(chisqr): |
---|
[c8e1996] | 2121 | # format chi2 |
---|
[682c432] | 2122 | chi2 = format_number(chisqr, True) |
---|
| 2123 | self.tcChi.SetValue(chi2) |
---|
| 2124 | self.tcChi.Refresh() |
---|
| 2125 | else: |
---|
| 2126 | self.tcChi.SetValue("-") |
---|
| 2127 | |
---|
[c8e1996] | 2128 | # Hide error title |
---|
[682c432] | 2129 | if self.text2_3.IsShown() and not self.is_mac: |
---|
| 2130 | self.text2_3.Hide() |
---|
| 2131 | |
---|
| 2132 | try: |
---|
| 2133 | if self.enable_disp.GetValue(): |
---|
| 2134 | if hasattr(self, "text_disp_1"): |
---|
[c8e1996] | 2135 | if self.text_disp_1 is not None and not self.is_mac: |
---|
[682c432] | 2136 | self.text_disp_1.Hide() |
---|
| 2137 | except: |
---|
| 2138 | dispersity = None |
---|
| 2139 | pass |
---|
| 2140 | |
---|
| 2141 | i = 0 |
---|
[c8e1996] | 2142 | # Set the panel when fit result are list |
---|
[682c432] | 2143 | |
---|
| 2144 | for item in self.param_toFit: |
---|
[c8e1996] | 2145 | if len(item) > 5 and item is not None: |
---|
[682c432] | 2146 | |
---|
| 2147 | if item[0].IsShown(): |
---|
[c8e1996] | 2148 | # reset error value to initial state |
---|
[682c432] | 2149 | if not self.is_mac: |
---|
| 2150 | item[3].Hide() |
---|
| 2151 | item[4].Hide() |
---|
| 2152 | for ind in range(len(out)): |
---|
| 2153 | if item[1] == p_name[ind]: |
---|
| 2154 | break |
---|
[c8e1996] | 2155 | if len(out) > 0 and out[ind] is not None: |
---|
[682c432] | 2156 | val_out = format_number(out[ind], True) |
---|
| 2157 | item[2].SetValue(val_out) |
---|
| 2158 | |
---|
[c8e1996] | 2159 | if(cov is not None and len(cov) == len(out)): |
---|
[682c432] | 2160 | try: |
---|
[c8e1996] | 2161 | if dispersity is not None: |
---|
[682c432] | 2162 | if self.enable_disp.GetValue(): |
---|
| 2163 | if hasattr(self, "text_disp_1"): |
---|
[c8e1996] | 2164 | if self.text_disp_1 is not None: |
---|
[682c432] | 2165 | if not self.text_disp_1.IsShown()\ |
---|
[c8e1996] | 2166 | and not self.is_mac: |
---|
[682c432] | 2167 | self.text_disp_1.Show(True) |
---|
| 2168 | except: |
---|
| 2169 | pass |
---|
| 2170 | |
---|
[c8e1996] | 2171 | if cov[ind] is not None: |
---|
[9a5097c] | 2172 | if np.isfinite(float(cov[ind])): |
---|
[682c432] | 2173 | val_err = format_number(cov[ind], True) |
---|
[c8e1996] | 2174 | item[4].SetForegroundColour(wx.BLACK) |
---|
[373d4ee] | 2175 | else: |
---|
| 2176 | val_err = 'NaN' |
---|
[60d08fd] | 2177 | item[4].SetForegroundColour(wx.RED) |
---|
[373d4ee] | 2178 | if not self.is_mac: |
---|
| 2179 | item[3].Show(True) |
---|
| 2180 | item[4].Show(True) |
---|
| 2181 | item[4].SetValue(val_err) |
---|
| 2182 | has_error = True |
---|
[682c432] | 2183 | i += 1 |
---|
| 2184 | else: |
---|
| 2185 | raise ValueError, "onsetValues: Invalid parameters..." |
---|
[c8e1996] | 2186 | # Show error title when any errors displayed |
---|
[682c432] | 2187 | if has_error: |
---|
| 2188 | if not self.text2_3.IsShown(): |
---|
| 2189 | self.text2_3.Show(True) |
---|
[c8e1996] | 2190 | # save current state |
---|
[682c432] | 2191 | self.save_current_state() |
---|
| 2192 | |
---|
[0b6f83c] | 2193 | if not self.is_mac: |
---|
| 2194 | self.Layout() |
---|
| 2195 | self.Refresh() |
---|
[c8e1996] | 2196 | # plot model ( when drawing, do not update chisqr value again) |
---|
[682c432] | 2197 | self._draw_model(update_chisqr=False, source='fit') |
---|
| 2198 | |
---|
| 2199 | def onWeighting(self, event): |
---|
| 2200 | """ |
---|
| 2201 | On Weighting radio button event, sets the weightbt_string |
---|
| 2202 | """ |
---|
| 2203 | self.weightbt_string = event.GetEventObject().GetLabelText() |
---|
| 2204 | self._set_weight() |
---|
| 2205 | |
---|
| 2206 | def _set_weight(self, is_2D=None): |
---|
| 2207 | """ |
---|
| 2208 | Set weight in fit problem |
---|
| 2209 | """ |
---|
| 2210 | # compute weight for the current data |
---|
| 2211 | flag_weight = self.get_weight_flag() |
---|
[c8e1996] | 2212 | if is_2D is None: |
---|
[682c432] | 2213 | is_2D = self._is_2D() |
---|
| 2214 | self._manager.set_fit_weight(uid=self.uid, |
---|
| 2215 | flag=flag_weight, |
---|
| 2216 | is2d=is_2D, |
---|
| 2217 | fid=None) |
---|
| 2218 | |
---|
| 2219 | def onPinholeSmear(self, event): |
---|
| 2220 | """ |
---|
| 2221 | Create a custom pinhole smear object that will change the way residuals |
---|
| 2222 | are compute when fitting |
---|
| 2223 | |
---|
| 2224 | :Note: accuracy is given by strings'High','Med', 'Low' FOR 2d, |
---|
| 2225 | None for 1D |
---|
| 2226 | |
---|
| 2227 | """ |
---|
| 2228 | # Need update param values |
---|
| 2229 | self._update_paramv_on_fit() |
---|
| 2230 | |
---|
[c8e1996] | 2231 | if event is not None: |
---|
[682c432] | 2232 | tcrtl = event.GetEventObject() |
---|
| 2233 | # event case of radio button |
---|
[c8e1996] | 2234 | if tcrtl.GetValue(): |
---|
[7e98655] | 2235 | self.dx_percent = 0.0 |
---|
[682c432] | 2236 | is_new_pinhole = True |
---|
| 2237 | else: |
---|
| 2238 | is_new_pinhole = self._is_changed_pinhole() |
---|
| 2239 | else: |
---|
| 2240 | is_new_pinhole = True |
---|
| 2241 | # if any value is changed |
---|
| 2242 | if is_new_pinhole: |
---|
[098f3d2] | 2243 | self._set_pinhole_smear() |
---|
[682c432] | 2244 | # hide all silt sizer |
---|
| 2245 | self._hide_all_smear_info() |
---|
| 2246 | |
---|
| 2247 | # show relevant slit sizers |
---|
| 2248 | self._show_smear_sizer() |
---|
| 2249 | |
---|
| 2250 | self.sizer_set_smearer.Layout() |
---|
[c8e1996] | 2251 | # we need FitInside here not just self.Layout to ensure all the sizers |
---|
| 2252 | # end up with the necessasary space to in the scroll panel. In |
---|
| 2253 | # particular the compute and fit buttons end up on top of each other |
---|
| 2254 | # PDB Nov 28 2015. |
---|
[1c2bf90] | 2255 | self.FitInside() |
---|
[682c432] | 2256 | |
---|
[c8e1996] | 2257 | if event is not None: |
---|
[682c432] | 2258 | event.Skip() |
---|
[c8e1996] | 2259 | # self._undo.Enable(True) |
---|
[682c432] | 2260 | self.save_current_state() |
---|
| 2261 | event = PageInfoEvent(page=self) |
---|
| 2262 | wx.PostEvent(self.parent, event) |
---|
| 2263 | |
---|
| 2264 | def _is_changed_pinhole(self): |
---|
| 2265 | """ |
---|
| 2266 | check if any of pinhole smear is changed |
---|
| 2267 | |
---|
| 2268 | :return: True or False |
---|
| 2269 | |
---|
| 2270 | """ |
---|
| 2271 | # get the values |
---|
[7e98655] | 2272 | pin_percent = self.smear_pinhole_percent.GetValue() |
---|
[682c432] | 2273 | |
---|
| 2274 | # Check changes in slit heigth |
---|
| 2275 | try: |
---|
[7e98655] | 2276 | dx_percent = float(pin_percent) |
---|
[682c432] | 2277 | except: |
---|
| 2278 | return True |
---|
[7e98655] | 2279 | if self.dx_percent != dx_percent: |
---|
[682c432] | 2280 | return True |
---|
| 2281 | return False |
---|
| 2282 | |
---|
| 2283 | def _set_pinhole_smear(self): |
---|
| 2284 | """ |
---|
| 2285 | Set custom pinhole smear |
---|
| 2286 | |
---|
| 2287 | :return: msg |
---|
| 2288 | |
---|
| 2289 | """ |
---|
| 2290 | # copy data |
---|
| 2291 | data = copy.deepcopy(self.data) |
---|
| 2292 | if self._is_2D(): |
---|
| 2293 | self.smear_type = 'Pinhole2d' |
---|
| 2294 | len_data = len(data.data) |
---|
[9a5097c] | 2295 | data.dqx_data = np.zeros(len_data) |
---|
| 2296 | data.dqy_data = np.zeros(len_data) |
---|
[682c432] | 2297 | else: |
---|
| 2298 | self.smear_type = 'Pinhole' |
---|
| 2299 | len_data = len(data.x) |
---|
[9a5097c] | 2300 | data.dx = np.zeros(len_data) |
---|
[682c432] | 2301 | data.dxl = None |
---|
| 2302 | data.dxw = None |
---|
| 2303 | msg = None |
---|
| 2304 | |
---|
[7e98655] | 2305 | get_pin_percent = self.smear_pinhole_percent |
---|
[682c432] | 2306 | |
---|
[7e98655] | 2307 | if not check_float(get_pin_percent): |
---|
| 2308 | get_pin_percent.SetBackgroundColour("pink") |
---|
[682c432] | 2309 | msg = "Model Error:wrong value entered!!!" |
---|
| 2310 | else: |
---|
| 2311 | if len_data < 2: |
---|
| 2312 | len_data = 2 |
---|
[d85f1d8a] | 2313 | self.dx_percent = float(get_pin_percent.GetValue()) |
---|
[7e98655] | 2314 | if self.dx_percent < 0: |
---|
| 2315 | get_pin_percent.SetBackgroundColour("pink") |
---|
[682c432] | 2316 | msg = "Model Error:This value can not be negative!!!" |
---|
[7e98655] | 2317 | elif self.dx_percent is not None: |
---|
[d85f1d8a] | 2318 | percent = self.dx_percent/100 |
---|
[682c432] | 2319 | if self._is_2D(): |
---|
[d85f1d8a] | 2320 | data.dqx_data[data.dqx_data == 0] = percent * data.qx_data |
---|
| 2321 | data.dqy_data[data.dqy_data == 0] = percent * data.qy_data |
---|
[682c432] | 2322 | else: |
---|
[d85f1d8a] | 2323 | data.dx = percent * data.x |
---|
[682c432] | 2324 | self.current_smearer = smear_selection(data, self.model) |
---|
| 2325 | # 2D need to set accuracy |
---|
| 2326 | if self._is_2D(): |
---|
[c8e1996] | 2327 | self.current_smearer.set_accuracy( |
---|
| 2328 | accuracy=self.smear2d_accuracy) |
---|
[682c432] | 2329 | |
---|
[c8e1996] | 2330 | if msg is not None: |
---|
[682c432] | 2331 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
| 2332 | else: |
---|
[7e98655] | 2333 | get_pin_percent.SetBackgroundColour("white") |
---|
[c8e1996] | 2334 | # set smearing value whether or not the data contain the smearing info |
---|
[682c432] | 2335 | |
---|
[373d4ee] | 2336 | enable_smearer = not self.disable_smearer.GetValue() |
---|
[682c432] | 2337 | self._manager.set_smearer(smearer=self.current_smearer, |
---|
[373d4ee] | 2338 | fid=self.data.id, |
---|
| 2339 | qmin=float(self.qmin_x), |
---|
| 2340 | qmax=float(self.qmax_x), |
---|
| 2341 | enable_smearer=enable_smearer, |
---|
| 2342 | uid=self.uid) |
---|
[682c432] | 2343 | return msg |
---|
| 2344 | |
---|
| 2345 | def update_pinhole_smear(self): |
---|
| 2346 | """ |
---|
| 2347 | called by kill_focus on pinhole TextCntrl |
---|
| 2348 | to update the changes |
---|
| 2349 | |
---|
| 2350 | :return: False when wrong value was entered |
---|
| 2351 | |
---|
| 2352 | """ |
---|
| 2353 | # msg default |
---|
| 2354 | msg = None |
---|
| 2355 | # check if any value is changed |
---|
| 2356 | if self._is_changed_pinhole(): |
---|
| 2357 | msg = self._set_pinhole_smear() |
---|
| 2358 | wx.CallAfter(self.save_current_state) |
---|
| 2359 | |
---|
[c8e1996] | 2360 | if msg is not None: |
---|
[682c432] | 2361 | return False |
---|
| 2362 | else: |
---|
| 2363 | return True |
---|
| 2364 | |
---|
| 2365 | def onSlitSmear(self, event): |
---|
| 2366 | """ |
---|
| 2367 | Create a custom slit smear object that will change the way residuals |
---|
| 2368 | are compute when fitting |
---|
| 2369 | """ |
---|
| 2370 | # Need update param values |
---|
| 2371 | self._update_paramv_on_fit() |
---|
| 2372 | |
---|
| 2373 | # msg default |
---|
| 2374 | msg = None |
---|
| 2375 | # for event given |
---|
[c8e1996] | 2376 | if event is not None: |
---|
[682c432] | 2377 | tcrtl = event.GetEventObject() |
---|
| 2378 | # event case of radio button |
---|
| 2379 | if tcrtl.GetValue(): |
---|
| 2380 | self.dxl = 0.0 |
---|
| 2381 | self.dxw = 0.0 |
---|
| 2382 | is_new_slit = True |
---|
| 2383 | else: |
---|
| 2384 | is_new_slit = self._is_changed_slit() |
---|
| 2385 | else: |
---|
| 2386 | is_new_slit = True |
---|
| 2387 | |
---|
| 2388 | # if any value is changed |
---|
| 2389 | if is_new_slit: |
---|
| 2390 | msg = self._set_slit_smear() |
---|
| 2391 | |
---|
| 2392 | # hide all silt sizer |
---|
| 2393 | self._hide_all_smear_info() |
---|
| 2394 | # show relevant slit sizers |
---|
| 2395 | self._show_smear_sizer() |
---|
| 2396 | self.sizer_set_smearer.Layout() |
---|
[c8e1996] | 2397 | # we need FitInside here not just self.Layout to ensure all the sizers |
---|
| 2398 | # end up with the necessasary space to in the scroll panel. In |
---|
| 2399 | # particular the compute and fit buttons end up on top of each other |
---|
| 2400 | # PDB Nov 28 2015. |
---|
[1c2bf90] | 2401 | self.FitInside() |
---|
[682c432] | 2402 | |
---|
[c8e1996] | 2403 | if event is not None: |
---|
[682c432] | 2404 | event.Skip() |
---|
| 2405 | self.save_current_state() |
---|
| 2406 | event = PageInfoEvent(page=self) |
---|
| 2407 | wx.PostEvent(self.parent, event) |
---|
[c8e1996] | 2408 | if msg is not None: |
---|
[682c432] | 2409 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
| 2410 | |
---|
| 2411 | def _is_changed_slit(self): |
---|
| 2412 | """ |
---|
| 2413 | check if any of slit lengths is changed |
---|
| 2414 | |
---|
| 2415 | :return: True or False |
---|
| 2416 | |
---|
| 2417 | """ |
---|
| 2418 | # get the values |
---|
| 2419 | width = self.smear_slit_width.GetValue() |
---|
| 2420 | height = self.smear_slit_height.GetValue() |
---|
| 2421 | |
---|
| 2422 | # check and change the box bg color if it was pink |
---|
| 2423 | # but it should be white now |
---|
| 2424 | # because this is the case that _set_slit_smear() will not handle |
---|
| 2425 | if height.lstrip().rstrip() == "": |
---|
| 2426 | self.smear_slit_height.SetBackgroundColour(wx.WHITE) |
---|
| 2427 | if width.lstrip().rstrip() == "": |
---|
| 2428 | self.smear_slit_width.SetBackgroundColour(wx.WHITE) |
---|
| 2429 | |
---|
| 2430 | # Check changes in slit width |
---|
| 2431 | if width == "": |
---|
| 2432 | dxw = 0.0 |
---|
| 2433 | else: |
---|
| 2434 | try: |
---|
| 2435 | dxw = float(width) |
---|
| 2436 | except: |
---|
| 2437 | return True |
---|
| 2438 | if self.dxw != dxw: |
---|
| 2439 | return True |
---|
| 2440 | |
---|
| 2441 | # Check changes in slit heigth |
---|
| 2442 | if height == "": |
---|
| 2443 | dxl = 0.0 |
---|
| 2444 | else: |
---|
| 2445 | try: |
---|
| 2446 | dxl = float(height) |
---|
| 2447 | except: |
---|
| 2448 | return True |
---|
| 2449 | if self.dxl != dxl: |
---|
| 2450 | return True |
---|
| 2451 | |
---|
| 2452 | return False |
---|
| 2453 | |
---|
| 2454 | def _set_slit_smear(self): |
---|
| 2455 | """ |
---|
| 2456 | Set custom slit smear |
---|
| 2457 | |
---|
| 2458 | :return: message to inform the user about the validity |
---|
| 2459 | of the values entered for slit smear |
---|
| 2460 | """ |
---|
[373d4ee] | 2461 | if self.data.__class__.__name__ == "Data2D" or self.enable2D: |
---|
[682c432] | 2462 | return |
---|
| 2463 | # make sure once more if it is smearer |
---|
| 2464 | data = copy.deepcopy(self.data) |
---|
| 2465 | data_len = len(data.x) |
---|
| 2466 | data.dx = None |
---|
| 2467 | data.dxl = None |
---|
| 2468 | data.dxw = None |
---|
| 2469 | msg = None |
---|
| 2470 | |
---|
| 2471 | try: |
---|
| 2472 | self.dxl = float(self.smear_slit_height.GetValue()) |
---|
[9a5097c] | 2473 | data.dxl = self.dxl * np.ones(data_len) |
---|
[682c432] | 2474 | self.smear_slit_height.SetBackgroundColour(wx.WHITE) |
---|
| 2475 | except: |
---|
| 2476 | self.dxl = None |
---|
[9a5097c] | 2477 | data.dxl = np.zeros(data_len) |
---|
[682c432] | 2478 | if self.smear_slit_height.GetValue().lstrip().rstrip() != "": |
---|
| 2479 | self.smear_slit_height.SetBackgroundColour("pink") |
---|
| 2480 | msg = "Wrong value entered... " |
---|
| 2481 | else: |
---|
| 2482 | self.smear_slit_height.SetBackgroundColour(wx.WHITE) |
---|
| 2483 | try: |
---|
| 2484 | self.dxw = float(self.smear_slit_width.GetValue()) |
---|
| 2485 | self.smear_slit_width.SetBackgroundColour(wx.WHITE) |
---|
[9a5097c] | 2486 | data.dxw = self.dxw * np.ones(data_len) |
---|
[682c432] | 2487 | except: |
---|
| 2488 | self.dxw = None |
---|
[9a5097c] | 2489 | data.dxw = np.zeros(data_len) |
---|
[682c432] | 2490 | if self.smear_slit_width.GetValue().lstrip().rstrip() != "": |
---|
| 2491 | self.smear_slit_width.SetBackgroundColour("pink") |
---|
| 2492 | msg = "Wrong Fit value entered... " |
---|
| 2493 | else: |
---|
| 2494 | self.smear_slit_width.SetBackgroundColour(wx.WHITE) |
---|
| 2495 | |
---|
| 2496 | self.current_smearer = smear_selection(data, self.model) |
---|
[c8e1996] | 2497 | # set smearing value whether or not the data contain the smearing info |
---|
[373d4ee] | 2498 | enable_smearer = not self.disable_smearer.GetValue() |
---|
[682c432] | 2499 | self._manager.set_smearer(smearer=self.current_smearer, |
---|
[373d4ee] | 2500 | fid=self.data.id, |
---|
| 2501 | qmin=float(self.qmin_x), |
---|
| 2502 | qmax=float(self.qmax_x), |
---|
| 2503 | enable_smearer=enable_smearer, |
---|
| 2504 | uid=self.uid) |
---|
[682c432] | 2505 | return msg |
---|
| 2506 | |
---|
| 2507 | def update_slit_smear(self): |
---|
| 2508 | """ |
---|
| 2509 | called by kill_focus on pinhole TextCntrl |
---|
| 2510 | to update the changes |
---|
| 2511 | |
---|
| 2512 | :return: False when wrong value was entered |
---|
| 2513 | |
---|
| 2514 | """ |
---|
| 2515 | # msg default |
---|
| 2516 | msg = None |
---|
| 2517 | # check if any value is changed |
---|
| 2518 | if self._is_changed_slit(): |
---|
| 2519 | msg = self._set_slit_smear() |
---|
[c8e1996] | 2520 | # self._undo.Enable(True) |
---|
[682c432] | 2521 | self.save_current_state() |
---|
| 2522 | |
---|
[c8e1996] | 2523 | if msg is not None: |
---|
[682c432] | 2524 | return False |
---|
| 2525 | else: |
---|
| 2526 | return True |
---|
| 2527 | |
---|
| 2528 | def onSmear(self, event): |
---|
| 2529 | """ |
---|
| 2530 | Create a smear object that will change the way residuals |
---|
[1c2bf90] | 2531 | are computed when fitting |
---|
[682c432] | 2532 | """ |
---|
[c8e1996] | 2533 | if event is not None: |
---|
[682c432] | 2534 | event.Skip() |
---|
| 2535 | if self.data is None: |
---|
| 2536 | return |
---|
| 2537 | |
---|
| 2538 | # Need update param values |
---|
| 2539 | self._update_paramv_on_fit() |
---|
[373d4ee] | 2540 | if self.model is not None: |
---|
[682c432] | 2541 | if self.data.is_data: |
---|
| 2542 | self._manager.page_finder[self.uid].add_data(data=self.data) |
---|
| 2543 | temp_smearer = self.on_smear_helper() |
---|
| 2544 | |
---|
| 2545 | self.sizer_set_smearer.Layout() |
---|
[c8e1996] | 2546 | # we need FitInside here not just self.Layout to ensure all the sizers |
---|
| 2547 | # end up with the necessasary space to in the scroll panel. In |
---|
| 2548 | # particular the compute and fit buttons end up on top of each other |
---|
| 2549 | # PDB Nov 28 2015. |
---|
[1c2bf90] | 2550 | self.FitInside() |
---|
[682c432] | 2551 | self._set_weight() |
---|
| 2552 | |
---|
[c8e1996] | 2553 | # set smearing value whether or not the data contain the smearing info |
---|
[373d4ee] | 2554 | enable_smearer = not self.disable_smearer.GetValue() |
---|
[682c432] | 2555 | wx.CallAfter(self._manager.set_smearer, uid=self.uid, |
---|
| 2556 | smearer=temp_smearer, |
---|
| 2557 | fid=self.data.id, |
---|
| 2558 | qmin=float(self.qmin_x), |
---|
| 2559 | qmax=float(self.qmax_x), |
---|
[934ce649] | 2560 | enable_smearer=enable_smearer) |
---|
[682c432] | 2561 | |
---|
| 2562 | self.state.enable_smearer = self.enable_smearer.GetValue() |
---|
| 2563 | self.state.disable_smearer = self.disable_smearer.GetValue() |
---|
| 2564 | self.state.pinhole_smearer = self.pinhole_smearer.GetValue() |
---|
| 2565 | self.state.slit_smearer = self.slit_smearer.GetValue() |
---|
| 2566 | |
---|
| 2567 | def on_smear_helper(self, update=False): |
---|
| 2568 | """ |
---|
| 2569 | Help for onSmear |
---|
| 2570 | |
---|
| 2571 | :param update: force or not to update |
---|
| 2572 | """ |
---|
| 2573 | self._get_smear_info() |
---|
[c8e1996] | 2574 | # renew smear sizer |
---|
[373d4ee] | 2575 | if self.smear_type is not None: |
---|
[682c432] | 2576 | self.smear_description_smear_type.SetValue(str(self.smear_type)) |
---|
| 2577 | self.smear_data_left.SetValue(str(self.dq_l)) |
---|
| 2578 | self.smear_data_right.SetValue(str(self.dq_r)) |
---|
| 2579 | |
---|
| 2580 | self._hide_all_smear_info() |
---|
| 2581 | data = copy.deepcopy(self.data) |
---|
| 2582 | |
---|
| 2583 | # make sure once more if it is smearer |
---|
| 2584 | temp_smearer = smear_selection(data, self.model) |
---|
| 2585 | if self.current_smearer != temp_smearer or update: |
---|
| 2586 | self.current_smearer = temp_smearer |
---|
| 2587 | if self.enable_smearer.GetValue(): |
---|
[373d4ee] | 2588 | if self.current_smearer is None: |
---|
[682c432] | 2589 | wx.PostEvent(self._manager.parent, |
---|
| 2590 | StatusEvent(status="Data contains no smearing information")) |
---|
| 2591 | else: |
---|
| 2592 | wx.PostEvent(self._manager.parent, |
---|
| 2593 | StatusEvent(status="Data contains smearing information")) |
---|
| 2594 | |
---|
| 2595 | self.smear_data_left.Show(True) |
---|
| 2596 | self.smear_data_right.Show(True) |
---|
| 2597 | temp_smearer = self.current_smearer |
---|
| 2598 | elif self.disable_smearer.GetValue(): |
---|
| 2599 | self.smear_description_none.Show(True) |
---|
| 2600 | elif self.pinhole_smearer.GetValue(): |
---|
| 2601 | self.onPinholeSmear(None) |
---|
| 2602 | elif self.slit_smearer.GetValue(): |
---|
| 2603 | self.onSlitSmear(None) |
---|
| 2604 | self._show_smear_sizer() |
---|
| 2605 | |
---|
| 2606 | return temp_smearer |
---|
| 2607 | |
---|
| 2608 | def on_complete_chisqr(self, event): |
---|
| 2609 | """ |
---|
| 2610 | Display result chisqr on the panel |
---|
| 2611 | :event: activated by fitting/ complete after draw |
---|
| 2612 | """ |
---|
| 2613 | try: |
---|
[c8e1996] | 2614 | if event is None: |
---|
[682c432] | 2615 | output = "-" |
---|
[9a5097c] | 2616 | elif not np.isfinite(event.output): |
---|
[682c432] | 2617 | output = "-" |
---|
| 2618 | else: |
---|
| 2619 | output = event.output |
---|
| 2620 | self.tcChi.SetValue(str(format_number(output, True))) |
---|
| 2621 | self.state.tcChi = self.tcChi.GetValue() |
---|
| 2622 | except: |
---|
| 2623 | pass |
---|
| 2624 | |
---|
| 2625 | def get_all_checked_params(self): |
---|
| 2626 | """ |
---|
| 2627 | Found all parameters current check and add them to list of parameters |
---|
| 2628 | to fit |
---|
| 2629 | """ |
---|
| 2630 | self.param_toFit = [] |
---|
| 2631 | for item in self.parameters: |
---|
| 2632 | if item[0].GetValue() and item not in self.param_toFit: |
---|
| 2633 | if item[0].IsShown(): |
---|
| 2634 | self.param_toFit.append(item) |
---|
| 2635 | for item in self.fittable_param: |
---|
| 2636 | if item[0].GetValue() and item not in self.param_toFit: |
---|
| 2637 | if item[0].IsShown(): |
---|
| 2638 | self.param_toFit.append(item) |
---|
| 2639 | self.save_current_state_fit() |
---|
| 2640 | |
---|
| 2641 | event = PageInfoEvent(page=self) |
---|
| 2642 | wx.PostEvent(self.parent, event) |
---|
| 2643 | param2fit = [] |
---|
| 2644 | for item in self.param_toFit: |
---|
| 2645 | if item[0] and item[0].IsShown(): |
---|
| 2646 | param2fit.append(item[1]) |
---|
| 2647 | self._manager.set_param2fit(self.uid, param2fit) |
---|
| 2648 | |
---|
[c8e1996] | 2649 | def select_param(self, event=None): |
---|
[682c432] | 2650 | """ |
---|
| 2651 | Select TextCtrl checked for fitting purpose and stores them |
---|
| 2652 | in self.param_toFit=[] list |
---|
| 2653 | """ |
---|
| 2654 | self.param_toFit = [] |
---|
| 2655 | for item in self.parameters: |
---|
[c8e1996] | 2656 | # Skip t ifhe angle parameters if 1D data |
---|
[682c432] | 2657 | if self.data.__class__.__name__ != "Data2D" and\ |
---|
| 2658 | not self.enable2D: |
---|
| 2659 | if item in self.orientation_params: |
---|
| 2660 | continue |
---|
[c8e1996] | 2661 | # Select parameters to fit for list of primary parameters |
---|
[682c432] | 2662 | if item[0].GetValue() and item[0].IsShown(): |
---|
| 2663 | if not (item in self.param_toFit): |
---|
| 2664 | self.param_toFit.append(item) |
---|
| 2665 | else: |
---|
| 2666 | #remove parameters from the fitting list |
---|
| 2667 | if item in self.param_toFit: |
---|
| 2668 | self.param_toFit.remove(item) |
---|
| 2669 | |
---|
[c8e1996] | 2670 | # Select parameters to fit for list of fittable parameters |
---|
[682c432] | 2671 | # with dispersion |
---|
| 2672 | for item in self.fittable_param: |
---|
[c8e1996] | 2673 | # Skip t ifhe angle parameters if 1D data |
---|
[682c432] | 2674 | if self.data.__class__.__name__ != "Data2D" and\ |
---|
| 2675 | not self.enable2D: |
---|
| 2676 | if item in self.orientation_params: |
---|
| 2677 | continue |
---|
| 2678 | if item[0].GetValue() and item[0].IsShown(): |
---|
| 2679 | if not (item in self.param_toFit): |
---|
| 2680 | self.param_toFit.append(item) |
---|
| 2681 | else: |
---|
[c8e1996] | 2682 | # remove parameters from the fitting list |
---|
[682c432] | 2683 | if item in self.param_toFit: |
---|
| 2684 | self.param_toFit.remove(item) |
---|
| 2685 | |
---|
[c8e1996] | 2686 | # Calculate num. of angle parameters |
---|
[682c432] | 2687 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
| 2688 | self.enable2D: |
---|
| 2689 | len_orient_para = 0 |
---|
| 2690 | else: |
---|
| 2691 | len_orient_para = len(self.orientation_params) # assume even len |
---|
[c8e1996] | 2692 | # Total num. of angle parameters |
---|
[682c432] | 2693 | if len(self.fittable_param) > 0: |
---|
| 2694 | len_orient_para *= 2 |
---|
| 2695 | |
---|
| 2696 | self.save_current_state_fit() |
---|
[c8e1996] | 2697 | if event is not None: |
---|
| 2698 | # post state to fit panel |
---|
[682c432] | 2699 | event = PageInfoEvent(page=self) |
---|
| 2700 | wx.PostEvent(self.parent, event) |
---|
| 2701 | |
---|
| 2702 | param2fit = [] |
---|
| 2703 | for item in self.param_toFit: |
---|
| 2704 | if item[0] and item[0].IsShown(): |
---|
| 2705 | param2fit.append(item[1]) |
---|
| 2706 | self._manager.set_param2fit(self.uid, param2fit) |
---|
| 2707 | |
---|
| 2708 | def set_model_param_sizer(self, model): |
---|
| 2709 | """ |
---|
| 2710 | Build the panel from the model content |
---|
| 2711 | |
---|
| 2712 | :param model: the model selected in combo box for fitting purpose |
---|
| 2713 | |
---|
| 2714 | """ |
---|
| 2715 | self.sizer3.Clear(True) |
---|
| 2716 | self.parameters = [] |
---|
| 2717 | self.str_parameters = [] |
---|
| 2718 | self.param_toFit = [] |
---|
| 2719 | self.fittable_param = [] |
---|
| 2720 | self.fixed_param = [] |
---|
| 2721 | self.orientation_params = [] |
---|
| 2722 | self.orientation_params_disp = [] |
---|
| 2723 | |
---|
[c8e1996] | 2724 | if model is None: |
---|
[682c432] | 2725 | self.sizer3.Layout() |
---|
| 2726 | self.SetupScrolling() |
---|
| 2727 | return |
---|
| 2728 | |
---|
[6f16e25] | 2729 | box_description = wx.StaticBox(self, wx.ID_ANY, str("Model Parameters")) |
---|
[682c432] | 2730 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
| 2731 | sizer = wx.GridBagSizer(5, 5) |
---|
[c8e1996] | 2732 | # save the current model |
---|
[682c432] | 2733 | self.model = model |
---|
| 2734 | |
---|
| 2735 | keys = self.model.getParamList() |
---|
| 2736 | |
---|
[c8e1996] | 2737 | # list of dispersion parameters |
---|
[682c432] | 2738 | self.disp_list = self.model.getDispParamList() |
---|
| 2739 | |
---|
| 2740 | def custom_compare(a, b): |
---|
| 2741 | """ |
---|
| 2742 | Custom compare to order, first by alphabets then second by number. |
---|
| 2743 | """ |
---|
| 2744 | # number at the last digit |
---|
| 2745 | a_last = a[len(a) - 1] |
---|
| 2746 | b_last = b[len(b) - 1] |
---|
| 2747 | # default |
---|
| 2748 | num_a = None |
---|
| 2749 | num_b = None |
---|
| 2750 | # split the names |
---|
| 2751 | a2 = a.lower().split('_') |
---|
| 2752 | b2 = b.lower().split('_') |
---|
| 2753 | # check length of a2, b2 |
---|
| 2754 | len_a2 = len(a2) |
---|
| 2755 | len_b2 = len(b2) |
---|
| 2756 | # check if it contains a int number(<10) |
---|
| 2757 | try: |
---|
| 2758 | num_a = int(a_last) |
---|
| 2759 | except: |
---|
| 2760 | pass |
---|
| 2761 | try: |
---|
| 2762 | num_b = int(b_last) |
---|
| 2763 | except: |
---|
| 2764 | pass |
---|
| 2765 | # Put 'scale' near the top; happens |
---|
| 2766 | # when numbered param name exists |
---|
| 2767 | if a == 'scale': |
---|
| 2768 | return -1 |
---|
| 2769 | # both have a number |
---|
[c8e1996] | 2770 | if num_a is not None and num_b is not None: |
---|
[682c432] | 2771 | if num_a > num_b: |
---|
| 2772 | return -1 |
---|
| 2773 | # same number |
---|
| 2774 | elif num_a == num_b: |
---|
| 2775 | # different last names |
---|
| 2776 | if a2[len_a2 - 1] != b2[len_b2 - 1] and num_a != 0: |
---|
| 2777 | return -cmp(a2[len_a2 - 1], b2[len_b2 - 1]) |
---|
| 2778 | else: |
---|
| 2779 | return cmp(a, b) |
---|
| 2780 | else: |
---|
| 2781 | return 1 |
---|
| 2782 | # one of them has a number |
---|
[c8e1996] | 2783 | elif num_a is not None: |
---|
[682c432] | 2784 | return 1 |
---|
[c8e1996] | 2785 | elif num_b is not None: |
---|
[682c432] | 2786 | return -1 |
---|
| 2787 | # no numbers |
---|
| 2788 | else: |
---|
| 2789 | return cmp(a.lower(), b.lower()) |
---|
[50fcb09] | 2790 | |
---|
[c8e1996] | 2791 | # keys obtained now from ordered dict, so commenting alphabetical |
---|
| 2792 | # ordering keys.sort(custom_compare) |
---|
[682c432] | 2793 | |
---|
| 2794 | iy = 0 |
---|
| 2795 | ix = 0 |
---|
[4c3be25] | 2796 | sizer.Add(wx.StaticText(self, wx.ID_ANY, 'Parameter'), |
---|
| 2797 | (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
[682c432] | 2798 | ix += 1 |
---|
[6f16e25] | 2799 | self.text2_2 = wx.StaticText(self, wx.ID_ANY, 'Value') |
---|
[c8e1996] | 2800 | sizer.Add(self.text2_2, (iy, ix), (1, 1), |
---|
[682c432] | 2801 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 2802 | ix += 2 |
---|
[6f16e25] | 2803 | self.text2_3 = wx.StaticText(self, wx.ID_ANY, 'Error') |
---|
[c8e1996] | 2804 | sizer.Add(self.text2_3, (iy, ix), (1, 1), |
---|
| 2805 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
[682c432] | 2806 | if not self.is_mac: |
---|
| 2807 | self.text2_3.Hide() |
---|
| 2808 | ix += 1 |
---|
[6f16e25] | 2809 | self.text2_min = wx.StaticText(self, wx.ID_ANY, 'Min') |
---|
[c8e1996] | 2810 | sizer.Add(self.text2_min, (iy, ix), (1, 1), |
---|
| 2811 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 2812 | # self.text2_min.Hide() |
---|
[682c432] | 2813 | ix += 1 |
---|
[6f16e25] | 2814 | self.text2_max = wx.StaticText(self, wx.ID_ANY, 'Max') |
---|
[c8e1996] | 2815 | sizer.Add(self.text2_max, (iy, ix), (1, 1), |
---|
| 2816 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 2817 | # self.text2_max.Hide() |
---|
[682c432] | 2818 | ix += 1 |
---|
[6f16e25] | 2819 | self.text2_4 = wx.StaticText(self, wx.ID_ANY, '[Units]') |
---|
[c8e1996] | 2820 | sizer.Add(self.text2_4, (iy, ix), (1, 1), |
---|
| 2821 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
[682c432] | 2822 | self.text2_4.Hide() |
---|
| 2823 | |
---|
[4c3be25] | 2824 | CHECK_STATE = False |
---|
[682c432] | 2825 | for item in keys: |
---|
| 2826 | |
---|
[c8e1996] | 2827 | if item not in self.disp_list and not item in \ |
---|
[682c432] | 2828 | self.model.orientation_params: |
---|
| 2829 | |
---|
[c8e1996] | 2830 | # prepare a spot to store errors |
---|
| 2831 | if item not in self.model.details: |
---|
[682c432] | 2832 | self.model.details[item] = ["", None, None] |
---|
| 2833 | |
---|
| 2834 | iy += 1 |
---|
| 2835 | ix = 0 |
---|
[c8e1996] | 2836 | if (self.model.__class__ in |
---|
| 2837 | self.model_list_box["Multi-Functions"] or |
---|
[682c432] | 2838 | self.temp_multi_functional)\ |
---|
| 2839 | and (item in self.model.non_fittable): |
---|
[6f16e25] | 2840 | non_fittable_name = wx.StaticText(self, wx.ID_ANY, item) |
---|
[c8e1996] | 2841 | sizer.Add(non_fittable_name, (iy, ix), (1, 1), |
---|
[682c432] | 2842 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 21) |
---|
[c8e1996] | 2843 | # add parameter value |
---|
[682c432] | 2844 | ix += 1 |
---|
| 2845 | value = self.model.getParam(item) |
---|
| 2846 | if len(self.model.fun_list) > 0: |
---|
[c8e1996] | 2847 | # num = item.split('_')[1][5:7] |
---|
[6f16e25] | 2848 | fun_box = wx.ComboBox(self, wx.ID_ANY, size=(100, -1), |
---|
[682c432] | 2849 | style=wx.CB_READONLY, name='%s' % item) |
---|
| 2850 | self._set_fun_box_list(fun_box) |
---|
| 2851 | fun_box.SetSelection(0) |
---|
[c8e1996] | 2852 | # self.fun_box.SetToolTipString("A function |
---|
[682c432] | 2853 | # describing the interface") |
---|
[6f16e25] | 2854 | wx.EVT_COMBOBOX(fun_box, wx.ID_ANY, self._on_fun_box) |
---|
[682c432] | 2855 | else: |
---|
[6f16e25] | 2856 | fun_box = ModelTextCtrl(self, wx.ID_ANY, |
---|
[373d4ee] | 2857 | size=(_BOX_WIDTH, 20), |
---|
[682c432] | 2858 | style=wx.TE_PROCESS_ENTER, name='%s' % item) |
---|
[c8e1996] | 2859 | fun_box.SetToolTipString( |
---|
[682c432] | 2860 | "Hit 'Enter' after typing to update the plot.") |
---|
| 2861 | fun_box.SetValue(format_number(value, True)) |
---|
| 2862 | sizer.Add(fun_box, (iy, ix), (1, 1), wx.EXPAND) |
---|
| 2863 | self.str_parameters.append([None, item, fun_box, |
---|
| 2864 | None, None, None, |
---|
| 2865 | None, None]) |
---|
| 2866 | else: |
---|
[c8e1996] | 2867 | # add parameters name with checkbox for selecting to fit |
---|
[6f16e25] | 2868 | cb = wx.CheckBox(self, wx.ID_ANY, item) |
---|
[682c432] | 2869 | cb.SetValue(CHECK_STATE) |
---|
| 2870 | cb.SetToolTipString(" Check mark to fit.") |
---|
[c8e1996] | 2871 | # cb.SetValue(True) |
---|
[682c432] | 2872 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
| 2873 | |
---|
| 2874 | sizer.Add(cb, (iy, ix), (1, 1), |
---|
| 2875 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 5) |
---|
| 2876 | |
---|
[c8e1996] | 2877 | # add parameter value |
---|
[682c432] | 2878 | ix += 1 |
---|
| 2879 | value = self.model.getParam(item) |
---|
[6f16e25] | 2880 | ctl1 = ModelTextCtrl(self, wx.ID_ANY, size=(_BOX_WIDTH, 20), |
---|
[373d4ee] | 2881 | style=wx.TE_PROCESS_ENTER) |
---|
[c8e1996] | 2882 | ctl1.SetToolTipString( |
---|
[682c432] | 2883 | "Hit 'Enter' after typing to update the plot.") |
---|
| 2884 | ctl1.SetValue(format_number(value, True)) |
---|
| 2885 | sizer.Add(ctl1, (iy, ix), (1, 1), wx.EXPAND) |
---|
[c8e1996] | 2886 | # text to show error sign |
---|
[682c432] | 2887 | ix += 1 |
---|
[6f16e25] | 2888 | text2 = wx.StaticText(self, wx.ID_ANY, '+/-') |
---|
[c8e1996] | 2889 | sizer.Add(text2, (iy, ix), (1, 1), |
---|
[682c432] | 2890 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 2891 | if not self.is_mac: |
---|
| 2892 | text2.Hide() |
---|
| 2893 | ix += 1 |
---|
[6f16e25] | 2894 | ctl2 = wx.TextCtrl(self, wx.ID_ANY, |
---|
[682c432] | 2895 | size=(_BOX_WIDTH / 1.2, 20), style=0) |
---|
| 2896 | sizer.Add(ctl2, (iy, ix), (1, 1), |
---|
| 2897 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 2898 | if not self.is_mac: |
---|
| 2899 | ctl2.Hide() |
---|
| 2900 | |
---|
| 2901 | ix += 1 |
---|
[6f16e25] | 2902 | ctl3 = ModelTextCtrl(self, wx.ID_ANY, |
---|
[373d4ee] | 2903 | size=(_BOX_WIDTH / 1.9, 20), |
---|
| 2904 | style=wx.TE_PROCESS_ENTER, |
---|
[682c432] | 2905 | text_enter_callback=self._onparamRangeEnter) |
---|
| 2906 | min_bound = self.model.details[item][1] |
---|
| 2907 | if min_bound is not None: |
---|
| 2908 | ctl3.SetValue(format_number(min_bound, True)) |
---|
| 2909 | |
---|
| 2910 | sizer.Add(ctl3, (iy, ix), (1, 1), |
---|
| 2911 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 2912 | |
---|
| 2913 | ix += 1 |
---|
[6f16e25] | 2914 | ctl4 = ModelTextCtrl(self, wx.ID_ANY, |
---|
[373d4ee] | 2915 | size=(_BOX_WIDTH / 1.9, 20), |
---|
| 2916 | style=wx.TE_PROCESS_ENTER, |
---|
[682c432] | 2917 | text_enter_callback=self._onparamRangeEnter) |
---|
| 2918 | max_bound = self.model.details[item][2] |
---|
| 2919 | if max_bound is not None: |
---|
| 2920 | ctl4.SetValue(format_number(max_bound, True)) |
---|
| 2921 | sizer.Add(ctl4, (iy, ix), (1, 1), |
---|
| 2922 | wx.EXPAND | wx.FIXED_MINSIZE, 0) |
---|
| 2923 | |
---|
| 2924 | ix += 1 |
---|
| 2925 | # Units |
---|
| 2926 | if item in self.model.details: |
---|
[6f16e25] | 2927 | units = wx.StaticText(self, wx.ID_ANY, |
---|
[682c432] | 2928 | self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
| 2929 | else: |
---|
[6f16e25] | 2930 | units = wx.StaticText(self, wx.ID_ANY, "", |
---|
[682c432] | 2931 | style=wx.ALIGN_LEFT) |
---|
| 2932 | sizer.Add(units, (iy, ix), (1, 1), |
---|
| 2933 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 2934 | |
---|
| 2935 | self.parameters.append([cb, item, ctl1, |
---|
| 2936 | text2, ctl2, ctl3, ctl4, units]) |
---|
| 2937 | |
---|
| 2938 | iy += 1 |
---|
| 2939 | sizer.Add((10, 10), (iy, ix), (1, 1), |
---|
| 2940 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 2941 | |
---|
| 2942 | # type can be either Guassian or Array |
---|
| 2943 | if len(self.model.dispersion.values()) > 0: |
---|
| 2944 | type = self.model.dispersion.values()[0]["type"] |
---|
| 2945 | else: |
---|
| 2946 | type = "Gaussian" |
---|
| 2947 | |
---|
| 2948 | iy += 1 |
---|
| 2949 | ix = 0 |
---|
[c8e1996] | 2950 | # Add tile for orientational angle |
---|
[682c432] | 2951 | for item in keys: |
---|
| 2952 | if item in self.model.orientation_params: |
---|
[6f16e25] | 2953 | orient_angle = wx.StaticText(self, wx.ID_ANY, '[For 2D only]:') |
---|
| 2954 | mag_on_button = wx.Button(self, wx.ID_ANY, "Magnetic ON") |
---|
[7116dffd] | 2955 | mag_on_button.SetToolTipString("Turn Pol Beam/Mag scatt on/off") |
---|
[682c432] | 2956 | mag_on_button.Bind(wx.EVT_BUTTON, self._on_mag_on) |
---|
[c8e1996] | 2957 | mag_angle_help_button = wx.Button(self, wx.ID_ANY, |
---|
| 2958 | "Magnetic angles?") |
---|
[7116dffd] | 2959 | mag_angle_help_button.SetToolTipString("see angle definitions") |
---|
[6f16e25] | 2960 | mag_help_button = wx.Button(self, wx.ID_ANY, "Mag HELP") |
---|
[7116dffd] | 2961 | mag_help_button.SetToolTipString("Help on pol beam/mag fitting") |
---|
[682c432] | 2962 | mag_help_button.Bind(wx.EVT_BUTTON, self._on_mag_help) |
---|
[c8e1996] | 2963 | mag_angle_help_button.Bind(wx.EVT_BUTTON, |
---|
[7116dffd] | 2964 | self._on_mag_angle_help) |
---|
[682c432] | 2965 | sizer.Add(orient_angle, (iy, ix), (1, 1), |
---|
| 2966 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 2967 | iy += 1 |
---|
| 2968 | sizer.Add(mag_on_button, (iy, ix), (1, 1), |
---|
| 2969 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
[7116dffd] | 2970 | ix += 1 |
---|
| 2971 | sizer.Add(mag_angle_help_button, (iy, ix), (1, 1), |
---|
| 2972 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
[682c432] | 2973 | sizer.Add(mag_help_button, (iy, ix + 1), (1, 1), |
---|
| 2974 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 2975 | |
---|
[c8e1996] | 2976 | # handle the magnetic buttons |
---|
| 2977 | # clean this up so that assume mag is off then turn |
---|
| 2978 | # all buttons on IF mag has mag and has 2D |
---|
[682c432] | 2979 | if not self._has_magnetic: |
---|
| 2980 | mag_on_button.Show(False) |
---|
| 2981 | elif not self.data.__class__.__name__ == "Data2D": |
---|
| 2982 | mag_on_button.Show(False) |
---|
| 2983 | else: |
---|
| 2984 | mag_on_button.Show(True) |
---|
| 2985 | mag_help_button.Show(False) |
---|
[2b58fa5] | 2986 | mag_angle_help_button.Show(False) |
---|
[682c432] | 2987 | if mag_on_button.IsShown(): |
---|
| 2988 | if self.magnetic_on: |
---|
| 2989 | mag_on_button.SetLabel("Magnetic OFF") |
---|
| 2990 | mag_help_button.Show(True) |
---|
[7116dffd] | 2991 | mag_angle_help_button.Show(True) |
---|
[682c432] | 2992 | else: |
---|
| 2993 | mag_on_button.SetLabel("Magnetic ON") |
---|
| 2994 | mag_help_button.Show(False) |
---|
[7116dffd] | 2995 | mag_angle_help_button.Show(False) |
---|
[682c432] | 2996 | |
---|
| 2997 | if not self.data.__class__.__name__ == "Data2D" and \ |
---|
| 2998 | not self.enable2D: |
---|
| 2999 | orient_angle.Hide() |
---|
| 3000 | else: |
---|
| 3001 | orient_angle.Show(True) |
---|
| 3002 | break |
---|
| 3003 | |
---|
[c8e1996] | 3004 | # For Gaussian only |
---|
[682c432] | 3005 | if type.lower() != "array": |
---|
| 3006 | for item in self.model.orientation_params: |
---|
| 3007 | if not self.magnetic_on: |
---|
| 3008 | if item in self.model.magnetic_params: |
---|
| 3009 | continue |
---|
[c8e1996] | 3010 | if item not in self.disp_list: |
---|
| 3011 | # prepare a spot to store min max |
---|
| 3012 | if item not in self.model.details: |
---|
[682c432] | 3013 | self.model.details[item] = ["", None, None] |
---|
| 3014 | |
---|
| 3015 | iy += 1 |
---|
| 3016 | ix = 0 |
---|
[c8e1996] | 3017 | # add parameters name with checkbox for selecting to fit |
---|
[6f16e25] | 3018 | cb = wx.CheckBox(self, wx.ID_ANY, item) |
---|
[682c432] | 3019 | cb.SetValue(CHECK_STATE) |
---|
| 3020 | cb.SetToolTipString("Check mark to fit") |
---|
| 3021 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
| 3022 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
| 3023 | self.enable2D: |
---|
| 3024 | cb.Show(True) |
---|
| 3025 | else: |
---|
| 3026 | cb.Hide() |
---|
| 3027 | sizer.Add(cb, (iy, ix), (1, 1), |
---|
| 3028 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 5) |
---|
| 3029 | |
---|
[c8e1996] | 3030 | # add parameter value |
---|
[682c432] | 3031 | ix += 1 |
---|
| 3032 | value = self.model.getParam(item) |
---|
[373d4ee] | 3033 | ctl1 = ModelTextCtrl(self, -1, size=(_BOX_WIDTH, 20), |
---|
| 3034 | style=wx.TE_PROCESS_ENTER) |
---|
[c8e1996] | 3035 | ctl1.SetToolTipString( |
---|
[682c432] | 3036 | "Hit 'Enter' after typing to update the plot.") |
---|
| 3037 | ctl1.SetValue(format_number(value, True)) |
---|
| 3038 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
| 3039 | self.enable2D: |
---|
| 3040 | ctl1.Show(True) |
---|
| 3041 | else: |
---|
| 3042 | ctl1.Hide() |
---|
| 3043 | sizer.Add(ctl1, (iy, ix), (1, 1), wx.EXPAND) |
---|
[c8e1996] | 3044 | # text to show error sign |
---|
[682c432] | 3045 | ix += 1 |
---|
| 3046 | text2 = wx.StaticText(self, -1, '+/-') |
---|
[c8e1996] | 3047 | sizer.Add(text2, (iy, ix), (1, 1), |
---|
[682c432] | 3048 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 3049 | |
---|
| 3050 | text2.Hide() |
---|
| 3051 | ix += 1 |
---|
| 3052 | ctl2 = wx.TextCtrl(self, -1, |
---|
| 3053 | size=(_BOX_WIDTH / 1.2, 20), style=0) |
---|
| 3054 | sizer.Add(ctl2, (iy, ix), (1, 1), |
---|
| 3055 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 3056 | |
---|
| 3057 | ctl2.Hide() |
---|
| 3058 | |
---|
| 3059 | ix += 1 |
---|
[373d4ee] | 3060 | ctl3 = ModelTextCtrl(self, -1, |
---|
| 3061 | size=(_BOX_WIDTH / 1.8, 20), |
---|
| 3062 | style=wx.TE_PROCESS_ENTER, |
---|
[682c432] | 3063 | text_enter_callback=self._onparamRangeEnter) |
---|
| 3064 | |
---|
| 3065 | sizer.Add(ctl3, (iy, ix), (1, 1), |
---|
| 3066 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 3067 | ctl3.Hide() |
---|
| 3068 | |
---|
| 3069 | ix += 1 |
---|
[373d4ee] | 3070 | ctl4 = ModelTextCtrl(self, -1, |
---|
| 3071 | size=(_BOX_WIDTH / 1.8, 20), |
---|
| 3072 | style=wx.TE_PROCESS_ENTER, |
---|
[682c432] | 3073 | text_enter_callback=self._onparamRangeEnter) |
---|
| 3074 | sizer.Add(ctl4, (iy, ix), (1, 1), |
---|
| 3075 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 3076 | |
---|
| 3077 | ctl4.Hide() |
---|
| 3078 | |
---|
| 3079 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
| 3080 | self.enable2D: |
---|
| 3081 | if self.is_mac: |
---|
| 3082 | text2.Show(True) |
---|
| 3083 | ctl2.Show(True) |
---|
| 3084 | ctl3.Show(True) |
---|
| 3085 | ctl4.Show(True) |
---|
| 3086 | |
---|
| 3087 | ix += 1 |
---|
| 3088 | # Units |
---|
| 3089 | if item in self.model.details: |
---|
| 3090 | units = wx.StaticText(self, -1, |
---|
| 3091 | self.model.details[item][0], |
---|
| 3092 | style=wx.ALIGN_LEFT) |
---|
| 3093 | else: |
---|
| 3094 | units = wx.StaticText(self, -1, "", |
---|
| 3095 | style=wx.ALIGN_LEFT) |
---|
| 3096 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
| 3097 | self.enable2D: |
---|
| 3098 | units.Show(True) |
---|
| 3099 | else: |
---|
| 3100 | units.Hide() |
---|
| 3101 | |
---|
| 3102 | sizer.Add(units, (iy, ix), (1, 1), |
---|
| 3103 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 3104 | |
---|
| 3105 | self.parameters.append([cb, item, ctl1, |
---|
| 3106 | text2, ctl2, ctl3, ctl4, units]) |
---|
| 3107 | self.orientation_params.append([cb, item, ctl1, |
---|
| 3108 | text2, ctl2, ctl3, ctl4, units]) |
---|
| 3109 | |
---|
| 3110 | iy += 1 |
---|
| 3111 | box_description.SetForegroundColour(wx.BLUE) |
---|
[c8e1996] | 3112 | # Display units text on panel |
---|
[682c432] | 3113 | for item in keys: |
---|
| 3114 | if item in self.model.details: |
---|
| 3115 | self.text2_4.Show() |
---|
[c8e1996] | 3116 | # Fill the list of fittable parameters |
---|
[682c432] | 3117 | self.get_all_checked_params() |
---|
| 3118 | self.save_current_state_fit() |
---|
| 3119 | boxsizer1.Add(sizer) |
---|
| 3120 | self.sizer3.Add(boxsizer1, 0, wx.EXPAND | wx.ALL, 10) |
---|
| 3121 | self.sizer3.Layout() |
---|
| 3122 | self.Layout() |
---|
| 3123 | |
---|
| 3124 | def on_right_down(self, event): |
---|
| 3125 | """ |
---|
| 3126 | Get key stroke event |
---|
| 3127 | """ |
---|
[c8e1996] | 3128 | if self.data is None: |
---|
[682c432] | 3129 | return |
---|
| 3130 | # Figuring out key combo: Cmd for copy, Alt for paste |
---|
| 3131 | if event.AltDown() and event.ShiftDown(): |
---|
| 3132 | flag = True |
---|
| 3133 | elif event.AltDown() or event.ShiftDown(): |
---|
| 3134 | flag = False |
---|
| 3135 | else: |
---|
| 3136 | return |
---|
| 3137 | # make event free |
---|
| 3138 | event.Skip() |
---|
| 3139 | # messages depending on the flag |
---|
| 3140 | if not flag: |
---|
| 3141 | infor = 'warning' |
---|
| 3142 | # inform msg to wx |
---|
| 3143 | wx.PostEvent(self._manager.parent, |
---|
| 3144 | StatusEvent(status=msg, info=infor)) |
---|
| 3145 | |
---|
| 3146 | def _onModel2D(self, event): |
---|
| 3147 | """ |
---|
| 3148 | toggle view of model from 1D to 2D or 2D from 1D |
---|
| 3149 | """ |
---|
| 3150 | if self.model_view.GetLabelText() == "Show 2D": |
---|
| 3151 | self.model_view.SetLabel("Show 1D") |
---|
| 3152 | self.enable2D = True |
---|
| 3153 | |
---|
| 3154 | else: |
---|
| 3155 | self.model_view.SetLabel("Show 2D") |
---|
| 3156 | self.enable2D = False |
---|
| 3157 | self.Show(False) |
---|
| 3158 | self.create_default_data() |
---|
| 3159 | self._manager.store_data(self.uid, data_list=[self.data]) |
---|
| 3160 | |
---|
| 3161 | self.set_model_param_sizer(self.model) |
---|
| 3162 | self._set_sizer_dispersion() |
---|
| 3163 | self._set_weight(is_2D=self.enable2D) |
---|
| 3164 | self._set_smear_buttons() |
---|
| 3165 | self.Show(True) |
---|
| 3166 | self.SetupScrolling() |
---|
| 3167 | self._draw_model() |
---|
| 3168 | |
---|
| 3169 | self.state.enable2D = copy.deepcopy(self.enable2D) |
---|
| 3170 | |
---|
| 3171 | def _set_smear_buttons(self): |
---|
| 3172 | """ |
---|
| 3173 | Set semarer radio buttons |
---|
| 3174 | """ |
---|
| 3175 | # more disables for 2D |
---|
| 3176 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
| 3177 | self.enable2D: |
---|
| 3178 | self.slit_smearer.Disable() |
---|
| 3179 | self.pinhole_smearer.Enable(True) |
---|
| 3180 | self.default_mask = copy.deepcopy(self.data.mask) |
---|
| 3181 | else: |
---|
| 3182 | self.slit_smearer.Enable(True) |
---|
| 3183 | self.pinhole_smearer.Enable(True) |
---|
| 3184 | |
---|
| 3185 | |
---|
| 3186 | class BGTextCtrl(wx.TextCtrl): |
---|
| 3187 | """ |
---|
| 3188 | Text control used to display outputs. |
---|
| 3189 | No editing allowed. The background is |
---|
| 3190 | grayed out. User can't select text. |
---|
| 3191 | """ |
---|
| 3192 | def __init__(self, *args, **kwds): |
---|
| 3193 | wx.TextCtrl.__init__(self, *args, **kwds) |
---|
| 3194 | self.SetEditable(False) |
---|
| 3195 | self.SetBackgroundColour(self.GetParent().parent.GetBackgroundColour()) |
---|
| 3196 | |
---|
| 3197 | # Bind to mouse event to avoid text highlighting |
---|
| 3198 | # The event will be skipped once the call-back |
---|
| 3199 | # is called. |
---|
| 3200 | self.Bind(wx.EVT_MOUSE_EVENTS, self._click) |
---|
| 3201 | |
---|
| 3202 | def _click(self, event): |
---|
| 3203 | """ |
---|
| 3204 | Prevent further handling of the mouse event |
---|
| 3205 | by not calling Skip(). |
---|
| 3206 | """ |
---|
| 3207 | pass |
---|