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