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 |
---|
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.sasgui.guiframe.events import StatusEvent, NewPlotEvent, \ |
---|
17 | PlotQrangeEvent |
---|
18 | from sas.sasgui.guiframe.dataFitting import check_data_validity |
---|
19 | from sas.sasgui.guiframe.utils import format_number, check_float |
---|
20 | from sas.sasgui.guiframe.documentation_window import DocumentationWindow |
---|
21 | |
---|
22 | from sas.sasgui.perspectives.fitting.basepage import BasicPage as BasicPage |
---|
23 | from sas.sasgui.perspectives.fitting.basepage import PageInfoEvent as \ |
---|
24 | PageInfoEvent |
---|
25 | from sas.sascalc.data_util.qsmearing import smear_selection |
---|
26 | from .basepage import ModelTextCtrl |
---|
27 | |
---|
28 | (Chi2UpdateEvent, EVT_CHI2_UPDATE) = wx.lib.newevent.NewEvent() |
---|
29 | _BOX_WIDTH = 76 |
---|
30 | _DATA_BOX_WIDTH = 300 |
---|
31 | SMEAR_SIZE_H = 0.00 |
---|
32 | CUSTOM_MODEL = 'Plugin Models' |
---|
33 | |
---|
34 | class FitPage(BasicPage): |
---|
35 | """ |
---|
36 | FitPanel class contains fields allowing to display results when |
---|
37 | fitting a model and one data |
---|
38 | |
---|
39 | :note: For Fit to be performed the user should check at least one parameter |
---|
40 | on fit Panel window. |
---|
41 | """ |
---|
42 | |
---|
43 | def __init__(self, parent, color=None): |
---|
44 | """ |
---|
45 | Initialization of the Panel |
---|
46 | """ |
---|
47 | BasicPage.__init__(self, parent, color=color) |
---|
48 | |
---|
49 | # draw sizer |
---|
50 | self._fill_data_sizer() |
---|
51 | self.is_2D = None |
---|
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/Npts(Fit)") |
---|
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, ' Chi2/Npts') |
---|
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/sm_help.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 = numpy.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 = numpy.sqrt(numpy.abs(data)) |
---|
1122 | elif button_list.index(item) == 3: |
---|
1123 | flag = 3 # dy = numpy.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): |
---|
1145 | """ |
---|
1146 | call back for model selection |
---|
1147 | """ |
---|
1148 | self.Show(False) |
---|
1149 | copy_flag = False |
---|
1150 | is_poly_enabled = None |
---|
1151 | if event is not None: |
---|
1152 | if (event.GetEventObject() == self.formfactorbox |
---|
1153 | and self.structurebox.GetLabel() != 'None')\ |
---|
1154 | or event.GetEventObject() == self.structurebox\ |
---|
1155 | or event.GetEventObject() == self.multifactorbox: |
---|
1156 | copy_flag = self.get_copy_params() |
---|
1157 | is_poly_enabled = self.enable_disp.GetValue() |
---|
1158 | |
---|
1159 | self._on_select_model_helper() |
---|
1160 | self.set_model_param_sizer(self.model) |
---|
1161 | if self.model is None: |
---|
1162 | self._set_bookmark_flag(False) |
---|
1163 | self._keep.Enable(False) |
---|
1164 | self._set_save_flag(False) |
---|
1165 | self.enable_disp.SetValue(False) |
---|
1166 | self.disable_disp.SetValue(True) |
---|
1167 | # TODO: should not have an untrapped exception when displaying disperser |
---|
1168 | # TODO: do we need to create the disperser panel on every model change? |
---|
1169 | # Note: if we fix this, then remove ID_DISPERSER_HELP from basepage |
---|
1170 | try: |
---|
1171 | self.set_dispers_sizer() |
---|
1172 | except: |
---|
1173 | pass |
---|
1174 | self.state.enable_disp = self.enable_disp.GetValue() |
---|
1175 | self.state.disable_disp = self.disable_disp.GetValue() |
---|
1176 | self.state.pinhole_smearer = self.pinhole_smearer.GetValue() |
---|
1177 | self.state.slit_smearer = self.slit_smearer.GetValue() |
---|
1178 | |
---|
1179 | self.state.structurecombobox = self.structurebox.GetValue() |
---|
1180 | self.state.formfactorcombobox = self.formfactorbox.GetValue() |
---|
1181 | self.state.categorycombobox = self.categorybox.GetValue() |
---|
1182 | self.enable_fit_button() |
---|
1183 | if self.model is not None: |
---|
1184 | self.m_name = self.model.name |
---|
1185 | self.state.m_name = self.m_name |
---|
1186 | self.rename_model() |
---|
1187 | self._set_copy_flag(True) |
---|
1188 | self._set_paste_flag(True) |
---|
1189 | if self.data is not None: |
---|
1190 | is_data = check_data_validity(self.data) |
---|
1191 | if is_data: |
---|
1192 | self._set_bookmark_flag(not self.batch_on) |
---|
1193 | self._keep.Enable(not self.batch_on) |
---|
1194 | self._set_save_flag(True) |
---|
1195 | #Setting smearing for cases with and without data. |
---|
1196 | self._set_smear(self.data) |
---|
1197 | |
---|
1198 | # more disables for 2D |
---|
1199 | self._set_smear_buttons() |
---|
1200 | |
---|
1201 | try: |
---|
1202 | # update smearer sizer |
---|
1203 | #This call for smearing set up caused double evaluation of |
---|
1204 | #I(q) and double compilation as results |
---|
1205 | #self.onSmear(None) |
---|
1206 | temp_smear = None |
---|
1207 | if not self.disable_smearer.GetValue(): |
---|
1208 | # Set the smearer environments |
---|
1209 | temp_smear = self.current_smearer |
---|
1210 | except: |
---|
1211 | raise |
---|
1212 | # error occured on chisqr computation |
---|
1213 | # pass |
---|
1214 | # event to post model to fit to fitting plugins |
---|
1215 | (ModelEventbox, EVT_MODEL_BOX) = wx.lib.newevent.NewEvent() |
---|
1216 | |
---|
1217 | # set smearing value whether or not data contain the smearing info |
---|
1218 | evt = ModelEventbox(model=self.model, |
---|
1219 | smearer=temp_smear, |
---|
1220 | enable_smearer=not self.disable_smearer.GetValue(), |
---|
1221 | qmin=float(self.qmin_x), |
---|
1222 | uid=self.uid, |
---|
1223 | caption=self.window_caption, |
---|
1224 | qmax=float(self.qmax_x)) |
---|
1225 | |
---|
1226 | self._manager._on_model_panel(evt=evt) |
---|
1227 | self.mbox_description.SetLabel("Model [ %s ]" % |
---|
1228 | str(self.model.name)) |
---|
1229 | self.mbox_description.SetForegroundColour(wx.BLUE) |
---|
1230 | self.state.model = self.model.clone() |
---|
1231 | self.state.model.name = self.model.name |
---|
1232 | |
---|
1233 | if event is not None: |
---|
1234 | # post state to fit panel |
---|
1235 | new_event = PageInfoEvent(page=self) |
---|
1236 | wx.PostEvent(self.parent, new_event) |
---|
1237 | # update list of plugins if new plugin is available |
---|
1238 | mod_cat = self.categorybox.GetStringSelection() |
---|
1239 | if mod_cat == CUSTOM_MODEL: |
---|
1240 | temp_id = self.model.id |
---|
1241 | temp = self.parent.update_model_list() |
---|
1242 | for v in self.parent.model_dictionary.values(): |
---|
1243 | if v.id == temp_id: |
---|
1244 | self.model = v() |
---|
1245 | break |
---|
1246 | if temp: |
---|
1247 | self.model_list_box = temp |
---|
1248 | current_val = self.formfactorbox.GetLabel() |
---|
1249 | pos = self.formfactorbox.GetSelection() |
---|
1250 | self._show_combox_helper() |
---|
1251 | self.formfactorbox.SetSelection(pos) |
---|
1252 | self.formfactorbox.SetValue(current_val) |
---|
1253 | # when select a model only from guictr/button |
---|
1254 | if is_poly_enabled is not None: |
---|
1255 | self.enable_disp.SetValue(is_poly_enabled) |
---|
1256 | self.disable_disp.SetValue(not is_poly_enabled) |
---|
1257 | self._set_dipers_Param(event=None) |
---|
1258 | self.state.enable_disp = self.enable_disp.GetValue() |
---|
1259 | self.state.disable_disp = self.disable_disp.GetValue() |
---|
1260 | |
---|
1261 | # Keep the previous param values |
---|
1262 | if copy_flag: |
---|
1263 | self.get_paste_params(copy_flag) |
---|
1264 | wx.CallAfter(self._onDraw, None) |
---|
1265 | |
---|
1266 | else: |
---|
1267 | self._draw_model() |
---|
1268 | |
---|
1269 | if self.batch_on: |
---|
1270 | self.slit_smearer.Enable(False) |
---|
1271 | self.pinhole_smearer.Enable(False) |
---|
1272 | self.btEditMask.Disable() |
---|
1273 | self.EditMask_title.Disable() |
---|
1274 | |
---|
1275 | self.Show(True) |
---|
1276 | self.SetupScrolling() |
---|
1277 | |
---|
1278 | def _onparamEnter(self, event): |
---|
1279 | """ |
---|
1280 | when enter value on panel redraw model according to changed |
---|
1281 | """ |
---|
1282 | if self.model is None: |
---|
1283 | msg = "Please select a Model first..." |
---|
1284 | wx.MessageBox(msg, 'Info') |
---|
1285 | return |
---|
1286 | |
---|
1287 | # default flag |
---|
1288 | flag = False |
---|
1289 | self.fitrange = True |
---|
1290 | # get event object |
---|
1291 | tcrtl = event.GetEventObject() |
---|
1292 | # Clear msg if previously shown. |
---|
1293 | msg = "" |
---|
1294 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
1295 | |
---|
1296 | if check_float(tcrtl): |
---|
1297 | flag = self._onparamEnter_helper() |
---|
1298 | self.show_npts2fit() |
---|
1299 | if self.fitrange: |
---|
1300 | temp_smearer = None |
---|
1301 | if not self.disable_smearer.GetValue(): |
---|
1302 | temp_smearer = self.current_smearer |
---|
1303 | # set smearing value whether or not data contain the |
---|
1304 | # smearing info |
---|
1305 | if self.slit_smearer.GetValue(): |
---|
1306 | flag1 = self.update_slit_smear() |
---|
1307 | flag = flag or flag1 |
---|
1308 | elif self.pinhole_smearer.GetValue(): |
---|
1309 | flag1 = self.update_pinhole_smear() |
---|
1310 | flag = flag or flag1 |
---|
1311 | elif self.data.__class__.__name__ != "Data2D" and \ |
---|
1312 | not self.enable2D: |
---|
1313 | enable_smearer = not self.disable_smearer.GetValue() |
---|
1314 | self._manager.set_smearer(smearer=temp_smearer, |
---|
1315 | fid=self.data.id, |
---|
1316 | uid=self.uid, |
---|
1317 | qmin=float(self.qmin_x), |
---|
1318 | qmax=float(self.qmax_x), |
---|
1319 | enable_smearer=enable_smearer) |
---|
1320 | if flag: |
---|
1321 | # self.compute_chisqr(smearer= temp_smearer) |
---|
1322 | |
---|
1323 | # new state posted |
---|
1324 | if self.state_change: |
---|
1325 | # self._undo.Enable(True) |
---|
1326 | event = PageInfoEvent(page=self) |
---|
1327 | wx.PostEvent(self.parent, event) |
---|
1328 | self.state_change = False |
---|
1329 | else: |
---|
1330 | # invalid fit range: do nothing here: |
---|
1331 | # msg already displayed in validate |
---|
1332 | return |
---|
1333 | else: |
---|
1334 | self.save_current_state() |
---|
1335 | msg = "Cannot Plot: Must enter a number!!! " |
---|
1336 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
1337 | |
---|
1338 | self.save_current_state() |
---|
1339 | return |
---|
1340 | |
---|
1341 | def _onparamRangeEnter(self, event): |
---|
1342 | """ |
---|
1343 | Check validity of value enter in the parameters range field |
---|
1344 | """ |
---|
1345 | tcrtl = event.GetEventObject() |
---|
1346 | # Clear msg if previously shown. |
---|
1347 | msg = "" |
---|
1348 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
1349 | # Flag to register when a parameter has changed. |
---|
1350 | is_modified = False |
---|
1351 | if tcrtl.GetValue().lstrip().rstrip() != "": |
---|
1352 | try: |
---|
1353 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
1354 | self._check_value_enter(self.fittable_param) |
---|
1355 | self._check_value_enter(self.parameters) |
---|
1356 | except: |
---|
1357 | tcrtl.SetBackgroundColour("pink") |
---|
1358 | msg = "Model Error:wrong value entered : %s" % sys.exc_value |
---|
1359 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
1360 | return |
---|
1361 | else: |
---|
1362 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
1363 | |
---|
1364 | # self._undo.Enable(True) |
---|
1365 | self.save_current_state() |
---|
1366 | event = PageInfoEvent(page=self) |
---|
1367 | wx.PostEvent(self.parent, event) |
---|
1368 | self.state_change = False |
---|
1369 | |
---|
1370 | def qrang_set_focus(self, event=None): |
---|
1371 | """ |
---|
1372 | ON Qrange focus |
---|
1373 | """ |
---|
1374 | if event is not None: |
---|
1375 | event.Skip() |
---|
1376 | # tcrtl = event.GetEventObject() |
---|
1377 | self._validate_qrange(self.qmin, self.qmax) |
---|
1378 | |
---|
1379 | def qrange_click(self, event): |
---|
1380 | """ |
---|
1381 | On Qrange textctrl click, make the qrange lines in the plot |
---|
1382 | """ |
---|
1383 | if event is not None: |
---|
1384 | event.Skip() |
---|
1385 | if self.data.__class__.__name__ == "Data2D": |
---|
1386 | return |
---|
1387 | is_click = event.LeftDown() |
---|
1388 | if is_click: |
---|
1389 | d_id = self.data.id |
---|
1390 | d_group_id = self.data.group_id |
---|
1391 | act_ctrl = event.GetEventObject() |
---|
1392 | wx.PostEvent(self._manager.parent, |
---|
1393 | PlotQrangeEvent(ctrl=[self.qmin, self.qmax], id=d_id, |
---|
1394 | group_id=d_group_id, leftdown=is_click, |
---|
1395 | active=act_ctrl)) |
---|
1396 | |
---|
1397 | def on_qrange_text(self, event): |
---|
1398 | """ |
---|
1399 | #On q range value updated. DO not combine with qrange_click(). |
---|
1400 | """ |
---|
1401 | if event is not None: |
---|
1402 | event.Skip() |
---|
1403 | if self.data.__class__.__name__ == "Data2D": |
---|
1404 | return |
---|
1405 | act_ctrl = event.GetEventObject() |
---|
1406 | d_id = self.data.id |
---|
1407 | d_group_id = self.data.group_id |
---|
1408 | wx.PostEvent(self._manager.parent, |
---|
1409 | PlotQrangeEvent(ctrl=[self.qmin, self.qmax], id=d_id, |
---|
1410 | group_id=d_group_id, leftdown=False, |
---|
1411 | active=act_ctrl)) |
---|
1412 | self._validate_qrange(self.qmin, self.qmax) |
---|
1413 | |
---|
1414 | def on_key(self, event): |
---|
1415 | """ |
---|
1416 | On Key down |
---|
1417 | """ |
---|
1418 | event.Skip() |
---|
1419 | if self.data.__class__.__name__ == "Data2D": |
---|
1420 | return |
---|
1421 | ctrl = event.GetEventObject() |
---|
1422 | try: |
---|
1423 | x_data = float(ctrl.GetValue()) |
---|
1424 | except: |
---|
1425 | return |
---|
1426 | key = event.GetKeyCode() |
---|
1427 | length = len(self.data.x) |
---|
1428 | indx = (numpy.abs(self.data.x - x_data)).argmin() |
---|
1429 | # return array.flat[idx] |
---|
1430 | if key == wx.WXK_PAGEUP or key == wx.WXK_NUMPAD_PAGEUP: |
---|
1431 | indx += 1 |
---|
1432 | if indx >= length: |
---|
1433 | indx = length - 1 |
---|
1434 | elif key == wx.WXK_PAGEDOWN or key == wx.WXK_NUMPAD_PAGEDOWN: |
---|
1435 | indx -= 1 |
---|
1436 | if indx < 0: |
---|
1437 | indx = 0 |
---|
1438 | else: |
---|
1439 | return |
---|
1440 | ctrl.SetValue(str(self.data.x[indx])) |
---|
1441 | self._validate_qrange(self.qmin, self.qmax) |
---|
1442 | |
---|
1443 | def _onQrangeEnter(self, event): |
---|
1444 | """ |
---|
1445 | Check validity of value enter in the Q range field |
---|
1446 | """ |
---|
1447 | tcrtl = event.GetEventObject() |
---|
1448 | # Clear msg if previously shown. |
---|
1449 | msg = "" |
---|
1450 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
1451 | # For theory mode |
---|
1452 | if not self.data.is_data: |
---|
1453 | self.npts_x = self.Npts_total.GetValue() |
---|
1454 | self.Npts_fit.SetValue(self.npts_x) |
---|
1455 | self.create_default_data() |
---|
1456 | # Flag to register when a parameter has changed. |
---|
1457 | if tcrtl.GetValue().lstrip().rstrip() != "": |
---|
1458 | try: |
---|
1459 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
1460 | # If qmin and qmax have been modified, update qmin and qmax |
---|
1461 | if self._validate_qrange(self.qmin, self.qmax): |
---|
1462 | tempmin = float(self.qmin.GetValue()) |
---|
1463 | if tempmin != self.qmin_x: |
---|
1464 | self.qmin_x = tempmin |
---|
1465 | tempmax = float(self.qmax.GetValue()) |
---|
1466 | if tempmax != self.qmax_x: |
---|
1467 | self.qmax_x = tempmax |
---|
1468 | else: |
---|
1469 | tcrtl.SetBackgroundColour("pink") |
---|
1470 | msg = "Model Error:wrong value entered : %s" % sys.exc_value |
---|
1471 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
1472 | return |
---|
1473 | except: |
---|
1474 | tcrtl.SetBackgroundColour("pink") |
---|
1475 | msg = "Model Error:wrong value entered : %s" % sys.exc_value |
---|
1476 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
1477 | return |
---|
1478 | # Check if # of points for theory model are valid(>0). |
---|
1479 | # check for 2d |
---|
1480 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
1481 | self.enable2D: |
---|
1482 | # set mask |
---|
1483 | radius = numpy.sqrt(self.data.qx_data * self.data.qx_data + |
---|
1484 | self.data.qy_data * self.data.qy_data) |
---|
1485 | index_data = ((self.qmin_x <= radius) & (radius <= self.qmax_x)) |
---|
1486 | index_data = (index_data) & (self.data.mask) |
---|
1487 | index_data = (index_data) & (numpy.isfinite(self.data.data)) |
---|
1488 | if len(index_data[index_data]) < 10: |
---|
1489 | msg = "Cannot Plot :No or too little npts in" |
---|
1490 | msg += " that data range!!! " |
---|
1491 | wx.PostEvent(self._manager.parent, |
---|
1492 | StatusEvent(status=msg)) |
---|
1493 | return |
---|
1494 | else: |
---|
1495 | # self.data.mask = index_data |
---|
1496 | # self.Npts_fit.SetValue(str(len(self.data.mask))) |
---|
1497 | self.show_npts2fit() |
---|
1498 | else: |
---|
1499 | index_data = ((self.qmin_x <= self.data.x) & |
---|
1500 | (self.data.x <= self.qmax_x)) |
---|
1501 | self.Npts_fit.SetValue(str(len(self.data.x[index_data]))) |
---|
1502 | |
---|
1503 | self.npts_x = self.Npts_total.GetValue() |
---|
1504 | self.create_default_data() |
---|
1505 | self._save_plotting_range() |
---|
1506 | else: |
---|
1507 | tcrtl.SetBackgroundColour("pink") |
---|
1508 | msg = "Model Error:wrong value entered!!!" |
---|
1509 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
1510 | |
---|
1511 | self._draw_model() |
---|
1512 | self.save_current_state() |
---|
1513 | event = PageInfoEvent(page=self) |
---|
1514 | wx.PostEvent(self.parent, event) |
---|
1515 | self.state_change = False |
---|
1516 | return |
---|
1517 | |
---|
1518 | def _clear_Err_on_Fit(self): |
---|
1519 | """ |
---|
1520 | hide the error text control shown |
---|
1521 | after fitting |
---|
1522 | """ |
---|
1523 | |
---|
1524 | if self.is_mac: |
---|
1525 | return |
---|
1526 | if hasattr(self, "text2_3"): |
---|
1527 | self.text2_3.Hide() |
---|
1528 | |
---|
1529 | if len(self.parameters) > 0: |
---|
1530 | for item in self.parameters: |
---|
1531 | if item[0].IsShown(): |
---|
1532 | # Skip the angle parameters if 1D data |
---|
1533 | if self.data.__class__.__name__ != "Data2D" and \ |
---|
1534 | not self.enable2D: |
---|
1535 | if item in self.orientation_params: |
---|
1536 | continue |
---|
1537 | if item in self.param_toFit: |
---|
1538 | continue |
---|
1539 | # hide statictext +/- |
---|
1540 | if len(item) < 4: |
---|
1541 | continue |
---|
1542 | if item[3] is not None and item[3].IsShown(): |
---|
1543 | item[3].Hide() |
---|
1544 | # hide textcrtl for error after fit |
---|
1545 | if item[4] is not None and item[4].IsShown(): |
---|
1546 | item[4].Hide() |
---|
1547 | |
---|
1548 | if len(self.fittable_param) > 0: |
---|
1549 | for item in self.fittable_param: |
---|
1550 | if item[0].IsShown(): |
---|
1551 | # Skip the angle parameters if 1D data |
---|
1552 | if self.data.__class__.__name__ != "Data2D" and \ |
---|
1553 | not self.enable2D: |
---|
1554 | if item in self.orientation_params: |
---|
1555 | continue |
---|
1556 | if item in self.param_toFit: |
---|
1557 | continue |
---|
1558 | if len(item) < 4: |
---|
1559 | continue |
---|
1560 | # hide statictext +/- |
---|
1561 | if item[3] is not None and item[3].IsShown(): |
---|
1562 | item[3].Hide() |
---|
1563 | # hide textcrtl for error after fit |
---|
1564 | if item[4] is not None and item[4].IsShown(): |
---|
1565 | item[4].Hide() |
---|
1566 | return |
---|
1567 | |
---|
1568 | def _get_defult_custom_smear(self): |
---|
1569 | """ |
---|
1570 | Get the defult values for custum smearing. |
---|
1571 | """ |
---|
1572 | # get the default values |
---|
1573 | if self.dxl is None: |
---|
1574 | self.dxl = 0.0 |
---|
1575 | if self.dxw is None: |
---|
1576 | self.dxw = "" |
---|
1577 | if self.dx_percent is None: |
---|
1578 | self.dx_percent = SMEAR_SIZE_H |
---|
1579 | |
---|
1580 | def _get_smear_info(self): |
---|
1581 | """ |
---|
1582 | Get the smear info from data. |
---|
1583 | |
---|
1584 | :return: self.smear_type, self.dq_l and self.dq_r, |
---|
1585 | respectively the type of the smear, dq_min and |
---|
1586 | dq_max for pinhole smear data |
---|
1587 | while dxl and dxw for slit smear |
---|
1588 | """ |
---|
1589 | # default |
---|
1590 | self.smear_type = None |
---|
1591 | self.dq_l = None |
---|
1592 | self.dq_r = None |
---|
1593 | data = self.data |
---|
1594 | if self.data is None: |
---|
1595 | return |
---|
1596 | elif self.data.__class__.__name__ == "Data2D" or \ |
---|
1597 | self.enable2D: |
---|
1598 | if data.dqx_data is None or data.dqy_data is None: |
---|
1599 | return |
---|
1600 | elif self.current_smearer is not None \ |
---|
1601 | and data.dqx_data.any() != 0 \ |
---|
1602 | and data.dqx_data.any() != 0: |
---|
1603 | self.smear_type = "Pinhole2d" |
---|
1604 | self.dq_l = format_number(numpy.average(data.dqx_data)) |
---|
1605 | self.dq_r = format_number(numpy.average(data.dqy_data)) |
---|
1606 | return |
---|
1607 | else: |
---|
1608 | return |
---|
1609 | # check if it is pinhole smear and get min max if it is. |
---|
1610 | if data.dx is not None and numpy.any(data.dx): |
---|
1611 | self.smear_type = "Pinhole" |
---|
1612 | self.dq_l = data.dx[0] |
---|
1613 | self.dq_r = data.dx[-1] |
---|
1614 | |
---|
1615 | # check if it is slit smear and get min max if it is. |
---|
1616 | elif data.dxl is not None or data.dxw is not None: |
---|
1617 | self.smear_type = "Slit" |
---|
1618 | if data.dxl is not None and numpy.all(data.dxl, 0): |
---|
1619 | self.dq_l = data.dxl[0] |
---|
1620 | if data.dxw is not None and numpy.all(data.dxw, 0): |
---|
1621 | self.dq_r = data.dxw[0] |
---|
1622 | # return self.smear_type,self.dq_l,self.dq_r |
---|
1623 | |
---|
1624 | def _show_smear_sizer(self): |
---|
1625 | """ |
---|
1626 | Show only the sizers depending on smear selection |
---|
1627 | """ |
---|
1628 | # smear disabled |
---|
1629 | if self.disable_smearer.GetValue(): |
---|
1630 | self.smear_description_none.Show(True) |
---|
1631 | # 2Dsmear |
---|
1632 | elif self._is_2D(): |
---|
1633 | self.smear_description_accuracy_type.Show(True) |
---|
1634 | self.smear_accuracy.Show(True) |
---|
1635 | self.smear_description_accuracy_type.Show(True) |
---|
1636 | self.smear_description_2d.Show(True) |
---|
1637 | self.smear_description_2d_x.Show(True) |
---|
1638 | self.smear_description_2d_y.Show(True) |
---|
1639 | if self.pinhole_smearer.GetValue(): |
---|
1640 | self.smear_pinhole_percent.Show(True) |
---|
1641 | # smear from data |
---|
1642 | elif self.enable_smearer.GetValue(): |
---|
1643 | |
---|
1644 | self.smear_description_dqdata.Show(True) |
---|
1645 | if self.smear_type is not None: |
---|
1646 | self.smear_description_smear_type.Show(True) |
---|
1647 | if self.smear_type == 'Slit': |
---|
1648 | self.smear_description_slit_height.Show(True) |
---|
1649 | self.smear_description_slit_width.Show(True) |
---|
1650 | elif self.smear_type == 'Pinhole': |
---|
1651 | self.smear_description_pin_percent.Show(True) |
---|
1652 | self.smear_description_smear_type.Show(True) |
---|
1653 | self.smear_description_type.Show(True) |
---|
1654 | self.smear_data_left.Show(True) |
---|
1655 | self.smear_data_right.Show(True) |
---|
1656 | # custom pinhole smear |
---|
1657 | elif self.pinhole_smearer.GetValue(): |
---|
1658 | if self.smear_type == 'Pinhole': |
---|
1659 | self.smear_message_new_p.Show(True) |
---|
1660 | self.smear_description_pin_percent.Show(True) |
---|
1661 | |
---|
1662 | self.smear_pinhole_percent.Show(True) |
---|
1663 | # custom slit smear |
---|
1664 | elif self.slit_smearer.GetValue(): |
---|
1665 | self.smear_message_new_s.Show(True) |
---|
1666 | self.smear_description_slit_height.Show(True) |
---|
1667 | self.smear_slit_height.Show(True) |
---|
1668 | self.smear_description_slit_width.Show(True) |
---|
1669 | self.smear_slit_width.Show(True) |
---|
1670 | |
---|
1671 | def _hide_all_smear_info(self): |
---|
1672 | """ |
---|
1673 | Hide all smearing messages in the set_smearer sizer |
---|
1674 | """ |
---|
1675 | self.smear_description_none.Hide() |
---|
1676 | self.smear_description_dqdata.Hide() |
---|
1677 | self.smear_description_type.Hide() |
---|
1678 | self.smear_description_smear_type.Hide() |
---|
1679 | self.smear_description_accuracy_type.Hide() |
---|
1680 | self.smear_description_2d_x.Hide() |
---|
1681 | self.smear_description_2d_y.Hide() |
---|
1682 | self.smear_description_2d.Hide() |
---|
1683 | |
---|
1684 | self.smear_accuracy.Hide() |
---|
1685 | self.smear_data_left.Hide() |
---|
1686 | self.smear_data_right.Hide() |
---|
1687 | self.smear_description_pin_percent.Hide() |
---|
1688 | self.smear_pinhole_percent.Hide() |
---|
1689 | self.smear_description_slit_height.Hide() |
---|
1690 | self.smear_slit_height.Hide() |
---|
1691 | self.smear_description_slit_width.Hide() |
---|
1692 | self.smear_slit_width.Hide() |
---|
1693 | self.smear_message_new_p.Hide() |
---|
1694 | self.smear_message_new_s.Hide() |
---|
1695 | |
---|
1696 | def _set_accuracy_list(self): |
---|
1697 | """ |
---|
1698 | Set the list of an accuracy in 2D custum smear: |
---|
1699 | Xhigh, High, Med, or Low |
---|
1700 | """ |
---|
1701 | # list of accuracy choices |
---|
1702 | list = ['Low', 'Med', 'High', 'Xhigh'] |
---|
1703 | for idx in range(len(list)): |
---|
1704 | self.smear_accuracy.Append(list[idx], idx) |
---|
1705 | |
---|
1706 | def _set_fun_box_list(self, fun_box): |
---|
1707 | """ |
---|
1708 | Set the list of func for multifunctional models |
---|
1709 | """ |
---|
1710 | # Check if it is multi_functional model |
---|
1711 | if self.model.__class__ not in self.model_list_box["Multi-Functions"] \ |
---|
1712 | and not self.temp_multi_functional: |
---|
1713 | return None |
---|
1714 | # Get the func name list |
---|
1715 | list = self.model.fun_list |
---|
1716 | if len(list) == 0: |
---|
1717 | return None |
---|
1718 | # build function (combo)box |
---|
1719 | ind = 0 |
---|
1720 | while(ind < len(list)): |
---|
1721 | for key, val in list.iteritems(): |
---|
1722 | if (val == ind): |
---|
1723 | fun_box.Append(key, val) |
---|
1724 | break |
---|
1725 | ind += 1 |
---|
1726 | |
---|
1727 | def _on_select_accuracy(self, event): |
---|
1728 | """ |
---|
1729 | Select an accuracy in 2D custom smear: Xhigh, High, Med, or Low |
---|
1730 | """ |
---|
1731 | # event.Skip() |
---|
1732 | # Check if the accuracy is same as before |
---|
1733 | # self.smear2d_accuracy = event.GetEventObject().GetValue() |
---|
1734 | self.smear2d_accuracy = self.smear_accuracy.GetValue() |
---|
1735 | if self.pinhole_smearer.GetValue(): |
---|
1736 | self.onPinholeSmear(event=None) |
---|
1737 | else: |
---|
1738 | self.onSmear(event=None) |
---|
1739 | if self.current_smearer is not None: |
---|
1740 | self.current_smearer.set_accuracy(accuracy=\ |
---|
1741 | self.smear2d_accuracy) |
---|
1742 | event.Skip() |
---|
1743 | |
---|
1744 | def _on_fun_box(self, event): |
---|
1745 | """ |
---|
1746 | Select an func: Erf,Rparabola,LParabola |
---|
1747 | """ |
---|
1748 | fun_val = None |
---|
1749 | fun_box = event.GetEventObject() |
---|
1750 | name = fun_box.Name |
---|
1751 | value = fun_box.GetValue() |
---|
1752 | if value in self.model.fun_list: |
---|
1753 | fun_val = self.model.fun_list[value] |
---|
1754 | |
---|
1755 | self.model.setParam(name, fun_val) |
---|
1756 | # save state |
---|
1757 | self._copy_parameters_state(self.str_parameters, |
---|
1758 | self.state.str_parameters) |
---|
1759 | # update params |
---|
1760 | self._update_paramv_on_fit() |
---|
1761 | # draw |
---|
1762 | self._draw_model() |
---|
1763 | self.Refresh() |
---|
1764 | # get ready for new event |
---|
1765 | event.Skip() |
---|
1766 | |
---|
1767 | def _onMask(self, event): |
---|
1768 | """ |
---|
1769 | Build a panel to allow to edit Mask |
---|
1770 | """ |
---|
1771 | from sas.sasgui.guiframe.local_perspectives.plotting.masking import \ |
---|
1772 | MaskPanel as MaskDialog |
---|
1773 | |
---|
1774 | self.panel = MaskDialog(base=self, data=self.data, id=wx.NewId()) |
---|
1775 | self.panel.ShowModal() |
---|
1776 | |
---|
1777 | def _draw_masked_model(self, event): |
---|
1778 | """ |
---|
1779 | Draw model image w/mask |
---|
1780 | """ |
---|
1781 | is_valid_qrange = self._update_paramv_on_fit() |
---|
1782 | |
---|
1783 | if is_valid_qrange and self.model is not None: |
---|
1784 | self.panel.MakeModal(False) |
---|
1785 | event.Skip() |
---|
1786 | # try re draw the model plot if it exists |
---|
1787 | self._draw_model() |
---|
1788 | self.show_npts2fit() |
---|
1789 | elif self.model is None: |
---|
1790 | self.panel.MakeModal(False) |
---|
1791 | event.Skip() |
---|
1792 | self.show_npts2fit() |
---|
1793 | msg = "No model is found on updating MASK in the model plot... " |
---|
1794 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
1795 | else: |
---|
1796 | event.Skip() |
---|
1797 | msg = ' Please consider your Q range, too.' |
---|
1798 | self.panel.ShowMessage(msg) |
---|
1799 | |
---|
1800 | def _set_smear(self, data): |
---|
1801 | """ |
---|
1802 | Set_smear |
---|
1803 | """ |
---|
1804 | if data is None: |
---|
1805 | return |
---|
1806 | self.current_smearer = smear_selection(data, self.model) |
---|
1807 | flag = self.disable_smearer.GetValue() |
---|
1808 | if self.current_smearer is None: |
---|
1809 | self.enable_smearer.Disable() |
---|
1810 | else: |
---|
1811 | self.enable_smearer.Enable() |
---|
1812 | if not flag: |
---|
1813 | self.onSmear(None) |
---|
1814 | |
---|
1815 | def _mac_sleep(self, sec=0.2): |
---|
1816 | """ |
---|
1817 | Give sleep to MAC |
---|
1818 | """ |
---|
1819 | if self.is_mac: |
---|
1820 | time.sleep(sec) |
---|
1821 | |
---|
1822 | def get_view_mode(self): |
---|
1823 | """ |
---|
1824 | return True if the panel allow 2D or False if 1D |
---|
1825 | """ |
---|
1826 | return self.enable2D |
---|
1827 | |
---|
1828 | def compute_data_set_range(self, data_list): |
---|
1829 | """ |
---|
1830 | find the range that include all data in the set |
---|
1831 | return the minimum and the maximum values |
---|
1832 | """ |
---|
1833 | if data_list is not None and data_list != []: |
---|
1834 | for data in data_list: |
---|
1835 | qmin, qmax, npts = self.compute_data_range(data) |
---|
1836 | self.qmin_data_set = min(self.qmin_data_set, qmin) |
---|
1837 | self.qmax_data_set = max(self.qmax_data_set, qmax) |
---|
1838 | self.npts_data_set += npts |
---|
1839 | return self.qmin_data_set, self.qmax_data_set, self.npts_data_set |
---|
1840 | |
---|
1841 | def compute_data_range(self, data): |
---|
1842 | """ |
---|
1843 | compute the minimum and the maximum range of the data |
---|
1844 | return the npts contains in data |
---|
1845 | :param data: |
---|
1846 | """ |
---|
1847 | qmin, qmax, npts = None, None, None |
---|
1848 | if data is not None: |
---|
1849 | if not hasattr(data, "data"): |
---|
1850 | try: |
---|
1851 | qmin = min(data.x) |
---|
1852 | # Maximum value of data |
---|
1853 | qmax = max(data.x) |
---|
1854 | npts = len(data.x) |
---|
1855 | except: |
---|
1856 | msg = "Unable to find min/max/length of \n data named %s" %\ |
---|
1857 | data.filename |
---|
1858 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg, |
---|
1859 | info="error")) |
---|
1860 | raise ValueError, msg |
---|
1861 | |
---|
1862 | else: |
---|
1863 | qmin = 0 |
---|
1864 | try: |
---|
1865 | x = max(math.fabs(data.xmin), math.fabs(data.xmax)) |
---|
1866 | y = max(math.fabs(data.ymin), math.fabs(data.ymax)) |
---|
1867 | except: |
---|
1868 | msg = "Unable to find min/max of \n data named %s" % \ |
---|
1869 | data.filename |
---|
1870 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg, |
---|
1871 | info="error")) |
---|
1872 | raise ValueError, msg |
---|
1873 | # Maximum value of data |
---|
1874 | qmax = math.sqrt(x * x + y * y) |
---|
1875 | npts = len(data.data) |
---|
1876 | return qmin, qmax, npts |
---|
1877 | |
---|
1878 | def set_data(self, data): |
---|
1879 | """ |
---|
1880 | reset the current data |
---|
1881 | """ |
---|
1882 | id = None |
---|
1883 | flag = False |
---|
1884 | is_data = False |
---|
1885 | npts = 0 |
---|
1886 | try: |
---|
1887 | old_id = self.data.id |
---|
1888 | old_group_id = self.data.group_id |
---|
1889 | except: |
---|
1890 | old_id = id |
---|
1891 | old_group_id = id |
---|
1892 | if self.data is not None: |
---|
1893 | is_data = check_data_validity(self.data) |
---|
1894 | if not is_data and data is not None: |
---|
1895 | flag = True |
---|
1896 | if data is not None: |
---|
1897 | if is_data: |
---|
1898 | self.graph_id = self.data.group_id |
---|
1899 | flag = (data.id != self.data.id) |
---|
1900 | self.data = data |
---|
1901 | if check_data_validity(data): |
---|
1902 | self.graph_id = data.group_id |
---|
1903 | self.data.group_id = self.graph_id |
---|
1904 | |
---|
1905 | if self.data is None: |
---|
1906 | data_name = "" |
---|
1907 | self._set_bookmark_flag(False) |
---|
1908 | self._keep.Enable(False) |
---|
1909 | self._set_save_flag(False) |
---|
1910 | else: |
---|
1911 | if self.model is not None: |
---|
1912 | self._set_bookmark_flag(not self.batch_on) |
---|
1913 | self._keep.Enable(not self.batch_on) |
---|
1914 | if self.data.is_data: |
---|
1915 | self._set_save_flag(True) |
---|
1916 | self._set_preview_flag(True) |
---|
1917 | |
---|
1918 | # more disables for 2D |
---|
1919 | di_flag = False |
---|
1920 | dq_flag = False |
---|
1921 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
1922 | self.enable2D: |
---|
1923 | self.slit_smearer.Disable() |
---|
1924 | self.pinhole_smearer.Enable(True) |
---|
1925 | self.default_mask = copy.deepcopy(self.data.mask) |
---|
1926 | if self.data.err_data is not None \ |
---|
1927 | and numpy.any(self.data.err_data): |
---|
1928 | di_flag = True |
---|
1929 | if self.data.dqx_data is not None \ |
---|
1930 | and numpy.any(self.data.dqx_data): |
---|
1931 | dq_flag = True |
---|
1932 | else: |
---|
1933 | self.slit_smearer.Enable(True) |
---|
1934 | self.pinhole_smearer.Enable(True) |
---|
1935 | if self.data.dy is not None and numpy.any(self.data.dy): |
---|
1936 | di_flag = True |
---|
1937 | if self.data.dx is not None and numpy.any(self.data.dx): |
---|
1938 | dq_flag = True |
---|
1939 | elif self.data.dxl is not None and numpy.any(self.data.dxl): |
---|
1940 | dq_flag = True |
---|
1941 | |
---|
1942 | if dq_flag: |
---|
1943 | self.enable_smearer.Enable(True) |
---|
1944 | self.enable_smearer.SetValue(True) |
---|
1945 | self.disable_smearer.SetValue(False) |
---|
1946 | else: |
---|
1947 | self.enable_smearer.Disable() |
---|
1948 | self.disable_smearer.Enable(True) |
---|
1949 | self.disable_smearer.SetValue(True) |
---|
1950 | |
---|
1951 | if di_flag: |
---|
1952 | self.dI_didata.Enable(True) |
---|
1953 | self.dI_didata.SetValue(True) |
---|
1954 | self.weightbt_string = self.dI_didata.GetLabelText() |
---|
1955 | else: |
---|
1956 | self.dI_didata.Enable(False) |
---|
1957 | self.dI_noweight.SetValue(True) |
---|
1958 | self.weightbt_string = self.dI_noweight.GetLabelText() |
---|
1959 | |
---|
1960 | # Enable weighting radio buttons |
---|
1961 | self.dI_noweight.Enable(True) |
---|
1962 | self.dI_sqrdata.Enable(True) |
---|
1963 | self.dI_idata.Enable(True) |
---|
1964 | |
---|
1965 | self.formfactorbox.Enable() |
---|
1966 | self.structurebox.Enable() |
---|
1967 | data_name = self.data.name |
---|
1968 | _, _, npts = self.compute_data_range(self.data) |
---|
1969 | # set maximum range for x in linear scale |
---|
1970 | if not hasattr(self.data, "data"): # Display only for 1D data fit |
---|
1971 | self.btEditMask.Disable() |
---|
1972 | self.EditMask_title.Disable() |
---|
1973 | else: |
---|
1974 | self.btEditMask.Enable() |
---|
1975 | self.EditMask_title.Enable() |
---|
1976 | |
---|
1977 | self.Npts_total.SetValue(str(npts)) |
---|
1978 | # default:number of data points selected to fit |
---|
1979 | self.Npts_fit.SetValue(str(npts)) |
---|
1980 | self.Npts_total.SetEditable(False) |
---|
1981 | self.Npts_total.SetBackgroundColour( |
---|
1982 | self.GetParent().GetBackgroundColour()) |
---|
1983 | |
---|
1984 | self.Npts_total.Bind(wx.EVT_MOUSE_EVENTS, self._npts_click) |
---|
1985 | self.pointsbox.Disable() |
---|
1986 | self.dataSource.SetValue(data_name) |
---|
1987 | self.state.data = data |
---|
1988 | self.enable_fit_button() |
---|
1989 | # send graph_id to page_finder |
---|
1990 | self._manager.set_graph_id(uid=self.uid, graph_id=self.graph_id) |
---|
1991 | # focus the page |
---|
1992 | if check_data_validity(data): |
---|
1993 | self.data_box_description.SetForegroundColour(wx.BLUE) |
---|
1994 | |
---|
1995 | if self.batch_on: |
---|
1996 | self.slit_smearer.Enable(False) |
---|
1997 | self.pinhole_smearer.Enable(False) |
---|
1998 | self.btEditMask.Disable() |
---|
1999 | self.EditMask_title.Disable() |
---|
2000 | |
---|
2001 | self.on_smear_helper() |
---|
2002 | self.on_set_focus(None) |
---|
2003 | self.Refresh() |
---|
2004 | # update model plot with new data information |
---|
2005 | if flag: |
---|
2006 | if self.data.__class__.__name__ == "Data2D": |
---|
2007 | self.enable2D = True |
---|
2008 | self.model_view.SetLabel("2D Mode") |
---|
2009 | else: |
---|
2010 | self.enable2D = False |
---|
2011 | self.model_view.SetLabel("1D Mode") |
---|
2012 | self.model_view.Disable() |
---|
2013 | # replace data plot on combo box selection |
---|
2014 | # by removing the previous selected data |
---|
2015 | try: |
---|
2016 | wx.PostEvent(self._manager.parent, |
---|
2017 | NewPlotEvent(action="delete", |
---|
2018 | group_id=old_group_id, id=old_id)) |
---|
2019 | except: |
---|
2020 | pass |
---|
2021 | # plot the current selected data |
---|
2022 | wx.PostEvent(self._manager.parent, |
---|
2023 | NewPlotEvent(action="check", plot=self.data, |
---|
2024 | title=str(self.data.title))) |
---|
2025 | self._draw_model() |
---|
2026 | |
---|
2027 | def _npts_click(self, event): |
---|
2028 | """ |
---|
2029 | Prevent further handling of the mouse event on Npts_total |
---|
2030 | by not calling Skip(). |
---|
2031 | """ |
---|
2032 | pass |
---|
2033 | |
---|
2034 | def reset_page(self, state, first=False): |
---|
2035 | """ |
---|
2036 | reset the state |
---|
2037 | """ |
---|
2038 | try: |
---|
2039 | self.reset_page_helper(state) |
---|
2040 | |
---|
2041 | self.select_param() |
---|
2042 | # Save state_fit |
---|
2043 | self.save_current_state_fit() |
---|
2044 | except: |
---|
2045 | self._show_combox_helper() |
---|
2046 | msg = "Error: This model state has missing or outdated " |
---|
2047 | msg += "information.\n" |
---|
2048 | msg += traceback.format_exc() |
---|
2049 | wx.PostEvent(self._manager.parent, |
---|
2050 | StatusEvent(status=msg, info="error")) |
---|
2051 | self._lay_out() |
---|
2052 | self.Refresh() |
---|
2053 | |
---|
2054 | def get_range(self): |
---|
2055 | """ |
---|
2056 | return the fitting range |
---|
2057 | """ |
---|
2058 | return float(self.qmin_x), float(self.qmax_x) |
---|
2059 | |
---|
2060 | def get_npts2fit(self): |
---|
2061 | """ |
---|
2062 | return numbers of data points within qrange |
---|
2063 | |
---|
2064 | :Note: This is to normalize chisq by Npts of fit |
---|
2065 | |
---|
2066 | """ |
---|
2067 | if self.data is None: |
---|
2068 | return |
---|
2069 | npts2fit = 0 |
---|
2070 | qmin, qmax = self.get_range() |
---|
2071 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
2072 | self.enable2D: |
---|
2073 | radius = numpy.sqrt(self.data.qx_data * self.data.qx_data + |
---|
2074 | self.data.qy_data * self.data.qy_data) |
---|
2075 | index_data = (self.qmin_x <= radius) & (radius <= self.qmax_x) |
---|
2076 | index_data = (index_data) & (self.data.mask) |
---|
2077 | index_data = (index_data) & (numpy.isfinite(self.data.data)) |
---|
2078 | npts2fit = len(self.data.data[index_data]) |
---|
2079 | else: |
---|
2080 | for qx in self.data.x: |
---|
2081 | if qmax >= qx >= qmin: |
---|
2082 | npts2fit += 1 |
---|
2083 | return npts2fit |
---|
2084 | |
---|
2085 | def show_npts2fit(self): |
---|
2086 | """ |
---|
2087 | setValue Npts for fitting |
---|
2088 | """ |
---|
2089 | self.Npts_fit.SetValue(str(self.get_npts2fit())) |
---|
2090 | |
---|
2091 | def get_chi2(self): |
---|
2092 | """ |
---|
2093 | return the current chi2 |
---|
2094 | """ |
---|
2095 | return self.tcChi.GetValue() |
---|
2096 | |
---|
2097 | def onsetValues(self, chisqr, p_name, out, cov): |
---|
2098 | """ |
---|
2099 | Build the panel from the fit result |
---|
2100 | |
---|
2101 | :param chisqr: Value of the goodness of fit metric |
---|
2102 | :param p_name: the name of parameters |
---|
2103 | :param out: list of parameter with the best value found during fitting |
---|
2104 | :param cov: Covariance matrix |
---|
2105 | |
---|
2106 | """ |
---|
2107 | |
---|
2108 | # make sure stop button to fit button all the time |
---|
2109 | self._on_fit_complete() |
---|
2110 | if out is None or not numpy.isfinite(chisqr): |
---|
2111 | raise ValueError, "Fit error occured..." |
---|
2112 | |
---|
2113 | is_modified = False |
---|
2114 | has_error = False |
---|
2115 | dispersity = '' |
---|
2116 | |
---|
2117 | # Hide textctrl boxes of errors. |
---|
2118 | self._clear_Err_on_Fit() |
---|
2119 | |
---|
2120 | # Check if chi2 is finite |
---|
2121 | if chisqr is not None and numpy.isfinite(chisqr): |
---|
2122 | # format chi2 |
---|
2123 | chi2 = format_number(chisqr, True) |
---|
2124 | self.tcChi.SetValue(chi2) |
---|
2125 | self.tcChi.Refresh() |
---|
2126 | else: |
---|
2127 | self.tcChi.SetValue("-") |
---|
2128 | |
---|
2129 | # Hide error title |
---|
2130 | if self.text2_3.IsShown() and not self.is_mac: |
---|
2131 | self.text2_3.Hide() |
---|
2132 | |
---|
2133 | try: |
---|
2134 | if self.enable_disp.GetValue(): |
---|
2135 | if hasattr(self, "text_disp_1"): |
---|
2136 | if self.text_disp_1 is not None and not self.is_mac: |
---|
2137 | self.text_disp_1.Hide() |
---|
2138 | except: |
---|
2139 | dispersity = None |
---|
2140 | pass |
---|
2141 | |
---|
2142 | i = 0 |
---|
2143 | # Set the panel when fit result are list |
---|
2144 | |
---|
2145 | for item in self.param_toFit: |
---|
2146 | if len(item) > 5 and item is not None: |
---|
2147 | |
---|
2148 | if item[0].IsShown(): |
---|
2149 | # reset error value to initial state |
---|
2150 | if not self.is_mac: |
---|
2151 | item[3].Hide() |
---|
2152 | item[4].Hide() |
---|
2153 | for ind in range(len(out)): |
---|
2154 | if item[1] == p_name[ind]: |
---|
2155 | break |
---|
2156 | if len(out) > 0 and out[ind] is not None: |
---|
2157 | val_out = format_number(out[ind], True) |
---|
2158 | item[2].SetValue(val_out) |
---|
2159 | |
---|
2160 | if(cov is not None and len(cov) == len(out)): |
---|
2161 | try: |
---|
2162 | if dispersity is not None: |
---|
2163 | if self.enable_disp.GetValue(): |
---|
2164 | if hasattr(self, "text_disp_1"): |
---|
2165 | if self.text_disp_1 is not None: |
---|
2166 | if not self.text_disp_1.IsShown()\ |
---|
2167 | and not self.is_mac: |
---|
2168 | self.text_disp_1.Show(True) |
---|
2169 | except: |
---|
2170 | pass |
---|
2171 | |
---|
2172 | if cov[ind] is not None: |
---|
2173 | if numpy.isfinite(float(cov[ind])): |
---|
2174 | val_err = format_number(cov[ind], True) |
---|
2175 | item[4].SetForegroundColour(wx.BLACK) |
---|
2176 | else: |
---|
2177 | val_err = 'NaN' |
---|
2178 | item[4].SetForegroundColour(wx.RED) |
---|
2179 | if not self.is_mac: |
---|
2180 | item[3].Show(True) |
---|
2181 | item[4].Show(True) |
---|
2182 | item[4].SetValue(val_err) |
---|
2183 | has_error = True |
---|
2184 | i += 1 |
---|
2185 | else: |
---|
2186 | raise ValueError, "onsetValues: Invalid parameters..." |
---|
2187 | # Show error title when any errors displayed |
---|
2188 | if has_error: |
---|
2189 | if not self.text2_3.IsShown(): |
---|
2190 | self.text2_3.Show(True) |
---|
2191 | # save current state |
---|
2192 | self.save_current_state() |
---|
2193 | |
---|
2194 | if not self.is_mac: |
---|
2195 | self.Layout() |
---|
2196 | self.Refresh() |
---|
2197 | self._mac_sleep(0.1) |
---|
2198 | # plot model ( when drawing, do not update chisqr value again) |
---|
2199 | self._draw_model(update_chisqr=False, source='fit') |
---|
2200 | |
---|
2201 | def onWeighting(self, event): |
---|
2202 | """ |
---|
2203 | On Weighting radio button event, sets the weightbt_string |
---|
2204 | """ |
---|
2205 | self.weightbt_string = event.GetEventObject().GetLabelText() |
---|
2206 | self._set_weight() |
---|
2207 | |
---|
2208 | def _set_weight(self, is_2D=None): |
---|
2209 | """ |
---|
2210 | Set weight in fit problem |
---|
2211 | """ |
---|
2212 | # compute weight for the current data |
---|
2213 | flag_weight = self.get_weight_flag() |
---|
2214 | if is_2D is None: |
---|
2215 | is_2D = self._is_2D() |
---|
2216 | self._manager.set_fit_weight(uid=self.uid, |
---|
2217 | flag=flag_weight, |
---|
2218 | is2d=is_2D, |
---|
2219 | fid=None) |
---|
2220 | |
---|
2221 | def onPinholeSmear(self, event): |
---|
2222 | """ |
---|
2223 | Create a custom pinhole smear object that will change the way residuals |
---|
2224 | are compute when fitting |
---|
2225 | |
---|
2226 | :Note: accuracy is given by strings'High','Med', 'Low' FOR 2d, |
---|
2227 | None for 1D |
---|
2228 | |
---|
2229 | """ |
---|
2230 | # Need update param values |
---|
2231 | self._update_paramv_on_fit() |
---|
2232 | |
---|
2233 | if event is not None: |
---|
2234 | tcrtl = event.GetEventObject() |
---|
2235 | # event case of radio button |
---|
2236 | if tcrtl.GetValue(): |
---|
2237 | self.dx_percent = 0.0 |
---|
2238 | is_new_pinhole = True |
---|
2239 | else: |
---|
2240 | is_new_pinhole = self._is_changed_pinhole() |
---|
2241 | else: |
---|
2242 | is_new_pinhole = True |
---|
2243 | # if any value is changed |
---|
2244 | if is_new_pinhole: |
---|
2245 | self._set_pinhole_smear() |
---|
2246 | # hide all silt sizer |
---|
2247 | self._hide_all_smear_info() |
---|
2248 | |
---|
2249 | # show relevant slit sizers |
---|
2250 | self._show_smear_sizer() |
---|
2251 | |
---|
2252 | self.sizer_set_smearer.Layout() |
---|
2253 | # we need FitInside here not just self.Layout to ensure all the sizers |
---|
2254 | # end up with the necessasary space to in the scroll panel. In |
---|
2255 | # particular the compute and fit buttons end up on top of each other |
---|
2256 | # PDB Nov 28 2015. |
---|
2257 | self.FitInside() |
---|
2258 | |
---|
2259 | if event is not None: |
---|
2260 | event.Skip() |
---|
2261 | # self._undo.Enable(True) |
---|
2262 | self.save_current_state() |
---|
2263 | event = PageInfoEvent(page=self) |
---|
2264 | wx.PostEvent(self.parent, event) |
---|
2265 | |
---|
2266 | def _is_changed_pinhole(self): |
---|
2267 | """ |
---|
2268 | check if any of pinhole smear is changed |
---|
2269 | |
---|
2270 | :return: True or False |
---|
2271 | |
---|
2272 | """ |
---|
2273 | # get the values |
---|
2274 | pin_percent = self.smear_pinhole_percent.GetValue() |
---|
2275 | |
---|
2276 | # Check changes in slit heigth |
---|
2277 | try: |
---|
2278 | dx_percent = float(pin_percent) |
---|
2279 | except: |
---|
2280 | return True |
---|
2281 | if self.dx_percent != dx_percent: |
---|
2282 | return True |
---|
2283 | return False |
---|
2284 | |
---|
2285 | def _set_pinhole_smear(self): |
---|
2286 | """ |
---|
2287 | Set custom pinhole smear |
---|
2288 | |
---|
2289 | :return: msg |
---|
2290 | |
---|
2291 | """ |
---|
2292 | # copy data |
---|
2293 | data = copy.deepcopy(self.data) |
---|
2294 | if self._is_2D(): |
---|
2295 | self.smear_type = 'Pinhole2d' |
---|
2296 | len_data = len(data.data) |
---|
2297 | data.dqx_data = numpy.zeros(len_data) |
---|
2298 | data.dqy_data = numpy.zeros(len_data) |
---|
2299 | else: |
---|
2300 | self.smear_type = 'Pinhole' |
---|
2301 | len_data = len(data.x) |
---|
2302 | data.dx = numpy.zeros(len_data) |
---|
2303 | data.dxl = None |
---|
2304 | data.dxw = None |
---|
2305 | msg = None |
---|
2306 | |
---|
2307 | get_pin_percent = self.smear_pinhole_percent |
---|
2308 | |
---|
2309 | if not check_float(get_pin_percent): |
---|
2310 | get_pin_percent.SetBackgroundColour("pink") |
---|
2311 | msg = "Model Error:wrong value entered!!!" |
---|
2312 | else: |
---|
2313 | if len_data < 2: |
---|
2314 | len_data = 2 |
---|
2315 | self.dx_percent = float(get_pin_percent.GetValue()) |
---|
2316 | if self.dx_percent < 0: |
---|
2317 | get_pin_percent.SetBackgroundColour("pink") |
---|
2318 | msg = "Model Error:This value can not be negative!!!" |
---|
2319 | elif self.dx_percent is not None: |
---|
2320 | percent = self.dx_percent/100 |
---|
2321 | if self._is_2D(): |
---|
2322 | data.dqx_data[data.dqx_data == 0] = percent * data.qx_data |
---|
2323 | data.dqy_data[data.dqy_data == 0] = percent * data.qy_data |
---|
2324 | else: |
---|
2325 | data.dx = percent * data.x |
---|
2326 | self.current_smearer = smear_selection(data, self.model) |
---|
2327 | # 2D need to set accuracy |
---|
2328 | if self._is_2D(): |
---|
2329 | self.current_smearer.set_accuracy( |
---|
2330 | accuracy=self.smear2d_accuracy) |
---|
2331 | |
---|
2332 | if msg is not None: |
---|
2333 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
2334 | else: |
---|
2335 | get_pin_percent.SetBackgroundColour("white") |
---|
2336 | # set smearing value whether or not the data contain the smearing info |
---|
2337 | |
---|
2338 | enable_smearer = not self.disable_smearer.GetValue() |
---|
2339 | self._manager.set_smearer(smearer=self.current_smearer, |
---|
2340 | fid=self.data.id, |
---|
2341 | qmin=float(self.qmin_x), |
---|
2342 | qmax=float(self.qmax_x), |
---|
2343 | enable_smearer=enable_smearer, |
---|
2344 | uid=self.uid) |
---|
2345 | return msg |
---|
2346 | |
---|
2347 | def update_pinhole_smear(self): |
---|
2348 | """ |
---|
2349 | called by kill_focus on pinhole TextCntrl |
---|
2350 | to update the changes |
---|
2351 | |
---|
2352 | :return: False when wrong value was entered |
---|
2353 | |
---|
2354 | """ |
---|
2355 | # msg default |
---|
2356 | msg = None |
---|
2357 | # check if any value is changed |
---|
2358 | if self._is_changed_pinhole(): |
---|
2359 | msg = self._set_pinhole_smear() |
---|
2360 | wx.CallAfter(self.save_current_state) |
---|
2361 | |
---|
2362 | if msg is not None: |
---|
2363 | return False |
---|
2364 | else: |
---|
2365 | return True |
---|
2366 | |
---|
2367 | def onSlitSmear(self, event): |
---|
2368 | """ |
---|
2369 | Create a custom slit smear object that will change the way residuals |
---|
2370 | are compute when fitting |
---|
2371 | """ |
---|
2372 | # Need update param values |
---|
2373 | self._update_paramv_on_fit() |
---|
2374 | |
---|
2375 | # msg default |
---|
2376 | msg = None |
---|
2377 | # for event given |
---|
2378 | if event is not None: |
---|
2379 | tcrtl = event.GetEventObject() |
---|
2380 | # event case of radio button |
---|
2381 | if tcrtl.GetValue(): |
---|
2382 | self.dxl = 0.0 |
---|
2383 | self.dxw = 0.0 |
---|
2384 | is_new_slit = True |
---|
2385 | else: |
---|
2386 | is_new_slit = self._is_changed_slit() |
---|
2387 | else: |
---|
2388 | is_new_slit = True |
---|
2389 | |
---|
2390 | # if any value is changed |
---|
2391 | if is_new_slit: |
---|
2392 | msg = self._set_slit_smear() |
---|
2393 | |
---|
2394 | # hide all silt sizer |
---|
2395 | self._hide_all_smear_info() |
---|
2396 | # show relevant slit sizers |
---|
2397 | self._show_smear_sizer() |
---|
2398 | self.sizer_set_smearer.Layout() |
---|
2399 | # we need FitInside here not just self.Layout to ensure all the sizers |
---|
2400 | # end up with the necessasary space to in the scroll panel. In |
---|
2401 | # particular the compute and fit buttons end up on top of each other |
---|
2402 | # PDB Nov 28 2015. |
---|
2403 | self.FitInside() |
---|
2404 | |
---|
2405 | if event is not None: |
---|
2406 | event.Skip() |
---|
2407 | self.save_current_state() |
---|
2408 | event = PageInfoEvent(page=self) |
---|
2409 | wx.PostEvent(self.parent, event) |
---|
2410 | if msg is not None: |
---|
2411 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
2412 | |
---|
2413 | def _is_changed_slit(self): |
---|
2414 | """ |
---|
2415 | check if any of slit lengths is changed |
---|
2416 | |
---|
2417 | :return: True or False |
---|
2418 | |
---|
2419 | """ |
---|
2420 | # get the values |
---|
2421 | width = self.smear_slit_width.GetValue() |
---|
2422 | height = self.smear_slit_height.GetValue() |
---|
2423 | |
---|
2424 | # check and change the box bg color if it was pink |
---|
2425 | # but it should be white now |
---|
2426 | # because this is the case that _set_slit_smear() will not handle |
---|
2427 | if height.lstrip().rstrip() == "": |
---|
2428 | self.smear_slit_height.SetBackgroundColour(wx.WHITE) |
---|
2429 | if width.lstrip().rstrip() == "": |
---|
2430 | self.smear_slit_width.SetBackgroundColour(wx.WHITE) |
---|
2431 | |
---|
2432 | # Check changes in slit width |
---|
2433 | if width == "": |
---|
2434 | dxw = 0.0 |
---|
2435 | else: |
---|
2436 | try: |
---|
2437 | dxw = float(width) |
---|
2438 | except: |
---|
2439 | return True |
---|
2440 | if self.dxw != dxw: |
---|
2441 | return True |
---|
2442 | |
---|
2443 | # Check changes in slit heigth |
---|
2444 | if height == "": |
---|
2445 | dxl = 0.0 |
---|
2446 | else: |
---|
2447 | try: |
---|
2448 | dxl = float(height) |
---|
2449 | except: |
---|
2450 | return True |
---|
2451 | if self.dxl != dxl: |
---|
2452 | return True |
---|
2453 | |
---|
2454 | return False |
---|
2455 | |
---|
2456 | def _set_slit_smear(self): |
---|
2457 | """ |
---|
2458 | Set custom slit smear |
---|
2459 | |
---|
2460 | :return: message to inform the user about the validity |
---|
2461 | of the values entered for slit smear |
---|
2462 | """ |
---|
2463 | if self.data.__class__.__name__ == "Data2D" or self.enable2D: |
---|
2464 | return |
---|
2465 | # make sure once more if it is smearer |
---|
2466 | data = copy.deepcopy(self.data) |
---|
2467 | data_len = len(data.x) |
---|
2468 | data.dx = None |
---|
2469 | data.dxl = None |
---|
2470 | data.dxw = None |
---|
2471 | msg = None |
---|
2472 | |
---|
2473 | try: |
---|
2474 | self.dxl = float(self.smear_slit_height.GetValue()) |
---|
2475 | data.dxl = self.dxl * numpy.ones(data_len) |
---|
2476 | self.smear_slit_height.SetBackgroundColour(wx.WHITE) |
---|
2477 | except: |
---|
2478 | self.dxl = None |
---|
2479 | data.dxl = numpy.zeros(data_len) |
---|
2480 | if self.smear_slit_height.GetValue().lstrip().rstrip() != "": |
---|
2481 | self.smear_slit_height.SetBackgroundColour("pink") |
---|
2482 | msg = "Wrong value entered... " |
---|
2483 | else: |
---|
2484 | self.smear_slit_height.SetBackgroundColour(wx.WHITE) |
---|
2485 | try: |
---|
2486 | self.dxw = float(self.smear_slit_width.GetValue()) |
---|
2487 | self.smear_slit_width.SetBackgroundColour(wx.WHITE) |
---|
2488 | data.dxw = self.dxw * numpy.ones(data_len) |
---|
2489 | except: |
---|
2490 | self.dxw = None |
---|
2491 | data.dxw = numpy.zeros(data_len) |
---|
2492 | if self.smear_slit_width.GetValue().lstrip().rstrip() != "": |
---|
2493 | self.smear_slit_width.SetBackgroundColour("pink") |
---|
2494 | msg = "Wrong Fit value entered... " |
---|
2495 | else: |
---|
2496 | self.smear_slit_width.SetBackgroundColour(wx.WHITE) |
---|
2497 | |
---|
2498 | self.current_smearer = smear_selection(data, self.model) |
---|
2499 | # set smearing value whether or not the data contain the smearing info |
---|
2500 | enable_smearer = not self.disable_smearer.GetValue() |
---|
2501 | self._manager.set_smearer(smearer=self.current_smearer, |
---|
2502 | fid=self.data.id, |
---|
2503 | qmin=float(self.qmin_x), |
---|
2504 | qmax=float(self.qmax_x), |
---|
2505 | enable_smearer=enable_smearer, |
---|
2506 | uid=self.uid) |
---|
2507 | return msg |
---|
2508 | |
---|
2509 | def update_slit_smear(self): |
---|
2510 | """ |
---|
2511 | called by kill_focus on pinhole TextCntrl |
---|
2512 | to update the changes |
---|
2513 | |
---|
2514 | :return: False when wrong value was entered |
---|
2515 | |
---|
2516 | """ |
---|
2517 | # msg default |
---|
2518 | msg = None |
---|
2519 | # check if any value is changed |
---|
2520 | if self._is_changed_slit(): |
---|
2521 | msg = self._set_slit_smear() |
---|
2522 | # self._undo.Enable(True) |
---|
2523 | self.save_current_state() |
---|
2524 | |
---|
2525 | if msg is not None: |
---|
2526 | return False |
---|
2527 | else: |
---|
2528 | return True |
---|
2529 | |
---|
2530 | def onSmear(self, event): |
---|
2531 | """ |
---|
2532 | Create a smear object that will change the way residuals |
---|
2533 | are computed when fitting |
---|
2534 | """ |
---|
2535 | if event is not None: |
---|
2536 | event.Skip() |
---|
2537 | if self.data is None: |
---|
2538 | return |
---|
2539 | |
---|
2540 | # Need update param values |
---|
2541 | self._update_paramv_on_fit() |
---|
2542 | if self.model is not None: |
---|
2543 | if self.data.is_data: |
---|
2544 | self._manager.page_finder[self.uid].add_data(data=self.data) |
---|
2545 | temp_smearer = self.on_smear_helper() |
---|
2546 | |
---|
2547 | self.sizer_set_smearer.Layout() |
---|
2548 | # we need FitInside here not just self.Layout to ensure all the sizers |
---|
2549 | # end up with the necessasary space to in the scroll panel. In |
---|
2550 | # particular the compute and fit buttons end up on top of each other |
---|
2551 | # PDB Nov 28 2015. |
---|
2552 | self.FitInside() |
---|
2553 | self._set_weight() |
---|
2554 | |
---|
2555 | # set smearing value whether or not the data contain the smearing info |
---|
2556 | enable_smearer = not self.disable_smearer.GetValue() |
---|
2557 | wx.CallAfter(self._manager.set_smearer, uid=self.uid, |
---|
2558 | smearer=temp_smearer, |
---|
2559 | fid=self.data.id, |
---|
2560 | qmin=float(self.qmin_x), |
---|
2561 | qmax=float(self.qmax_x), |
---|
2562 | enable_smearer=enable_smearer) |
---|
2563 | |
---|
2564 | self.state.enable_smearer = self.enable_smearer.GetValue() |
---|
2565 | self.state.disable_smearer = self.disable_smearer.GetValue() |
---|
2566 | self.state.pinhole_smearer = self.pinhole_smearer.GetValue() |
---|
2567 | self.state.slit_smearer = self.slit_smearer.GetValue() |
---|
2568 | |
---|
2569 | def on_smear_helper(self, update=False): |
---|
2570 | """ |
---|
2571 | Help for onSmear |
---|
2572 | |
---|
2573 | :param update: force or not to update |
---|
2574 | """ |
---|
2575 | self._get_smear_info() |
---|
2576 | # renew smear sizer |
---|
2577 | if self.smear_type is not None: |
---|
2578 | self.smear_description_smear_type.SetValue(str(self.smear_type)) |
---|
2579 | self.smear_data_left.SetValue(str(self.dq_l)) |
---|
2580 | self.smear_data_right.SetValue(str(self.dq_r)) |
---|
2581 | |
---|
2582 | self._hide_all_smear_info() |
---|
2583 | data = copy.deepcopy(self.data) |
---|
2584 | |
---|
2585 | # make sure once more if it is smearer |
---|
2586 | temp_smearer = smear_selection(data, self.model) |
---|
2587 | if self.current_smearer != temp_smearer or update: |
---|
2588 | self.current_smearer = temp_smearer |
---|
2589 | if self.enable_smearer.GetValue(): |
---|
2590 | if self.current_smearer is None: |
---|
2591 | wx.PostEvent(self._manager.parent, |
---|
2592 | StatusEvent(status="Data contains no smearing information")) |
---|
2593 | else: |
---|
2594 | wx.PostEvent(self._manager.parent, |
---|
2595 | StatusEvent(status="Data contains smearing information")) |
---|
2596 | |
---|
2597 | self.smear_data_left.Show(True) |
---|
2598 | self.smear_data_right.Show(True) |
---|
2599 | temp_smearer = self.current_smearer |
---|
2600 | elif self.disable_smearer.GetValue(): |
---|
2601 | self.smear_description_none.Show(True) |
---|
2602 | elif self.pinhole_smearer.GetValue(): |
---|
2603 | self.onPinholeSmear(None) |
---|
2604 | elif self.slit_smearer.GetValue(): |
---|
2605 | self.onSlitSmear(None) |
---|
2606 | self._show_smear_sizer() |
---|
2607 | |
---|
2608 | return temp_smearer |
---|
2609 | |
---|
2610 | def on_complete_chisqr(self, event): |
---|
2611 | """ |
---|
2612 | Display result chisqr on the panel |
---|
2613 | :event: activated by fitting/ complete after draw |
---|
2614 | """ |
---|
2615 | try: |
---|
2616 | if event is None: |
---|
2617 | output = "-" |
---|
2618 | elif not numpy.isfinite(event.output): |
---|
2619 | output = "-" |
---|
2620 | else: |
---|
2621 | output = event.output |
---|
2622 | self.tcChi.SetValue(str(format_number(output, True))) |
---|
2623 | self.state.tcChi = self.tcChi.GetValue() |
---|
2624 | except: |
---|
2625 | pass |
---|
2626 | |
---|
2627 | def get_all_checked_params(self): |
---|
2628 | """ |
---|
2629 | Found all parameters current check and add them to list of parameters |
---|
2630 | to fit |
---|
2631 | """ |
---|
2632 | self.param_toFit = [] |
---|
2633 | for item in self.parameters: |
---|
2634 | if item[0].GetValue() and item not in self.param_toFit: |
---|
2635 | if item[0].IsShown(): |
---|
2636 | self.param_toFit.append(item) |
---|
2637 | for item in self.fittable_param: |
---|
2638 | if item[0].GetValue() and item not in self.param_toFit: |
---|
2639 | if item[0].IsShown(): |
---|
2640 | self.param_toFit.append(item) |
---|
2641 | self.save_current_state_fit() |
---|
2642 | |
---|
2643 | event = PageInfoEvent(page=self) |
---|
2644 | wx.PostEvent(self.parent, event) |
---|
2645 | param2fit = [] |
---|
2646 | for item in self.param_toFit: |
---|
2647 | if item[0] and item[0].IsShown(): |
---|
2648 | param2fit.append(item[1]) |
---|
2649 | self._manager.set_param2fit(self.uid, param2fit) |
---|
2650 | |
---|
2651 | def select_param(self, event=None): |
---|
2652 | """ |
---|
2653 | Select TextCtrl checked for fitting purpose and stores them |
---|
2654 | in self.param_toFit=[] list |
---|
2655 | """ |
---|
2656 | self.param_toFit = [] |
---|
2657 | for item in self.parameters: |
---|
2658 | # Skip t ifhe angle parameters if 1D data |
---|
2659 | if self.data.__class__.__name__ != "Data2D" and\ |
---|
2660 | not self.enable2D: |
---|
2661 | if item in self.orientation_params: |
---|
2662 | continue |
---|
2663 | # Select parameters to fit for list of primary parameters |
---|
2664 | if item[0].GetValue() and item[0].IsShown(): |
---|
2665 | if not (item in self.param_toFit): |
---|
2666 | self.param_toFit.append(item) |
---|
2667 | else: |
---|
2668 | #remove parameters from the fitting list |
---|
2669 | if item in self.param_toFit: |
---|
2670 | self.param_toFit.remove(item) |
---|
2671 | |
---|
2672 | # Select parameters to fit for list of fittable parameters |
---|
2673 | # with dispersion |
---|
2674 | for item in self.fittable_param: |
---|
2675 | # Skip t ifhe angle parameters if 1D data |
---|
2676 | if self.data.__class__.__name__ != "Data2D" and\ |
---|
2677 | not self.enable2D: |
---|
2678 | if item in self.orientation_params: |
---|
2679 | continue |
---|
2680 | if item[0].GetValue() and item[0].IsShown(): |
---|
2681 | if not (item in self.param_toFit): |
---|
2682 | self.param_toFit.append(item) |
---|
2683 | else: |
---|
2684 | # remove parameters from the fitting list |
---|
2685 | if item in self.param_toFit: |
---|
2686 | self.param_toFit.remove(item) |
---|
2687 | |
---|
2688 | # Calculate num. of angle parameters |
---|
2689 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
2690 | self.enable2D: |
---|
2691 | len_orient_para = 0 |
---|
2692 | else: |
---|
2693 | len_orient_para = len(self.orientation_params) # assume even len |
---|
2694 | # Total num. of angle parameters |
---|
2695 | if len(self.fittable_param) > 0: |
---|
2696 | len_orient_para *= 2 |
---|
2697 | |
---|
2698 | self.save_current_state_fit() |
---|
2699 | if event is not None: |
---|
2700 | # post state to fit panel |
---|
2701 | event = PageInfoEvent(page=self) |
---|
2702 | wx.PostEvent(self.parent, event) |
---|
2703 | |
---|
2704 | param2fit = [] |
---|
2705 | for item in self.param_toFit: |
---|
2706 | if item[0] and item[0].IsShown(): |
---|
2707 | param2fit.append(item[1]) |
---|
2708 | self._manager.set_param2fit(self.uid, param2fit) |
---|
2709 | |
---|
2710 | def set_model_param_sizer(self, model): |
---|
2711 | """ |
---|
2712 | Build the panel from the model content |
---|
2713 | |
---|
2714 | :param model: the model selected in combo box for fitting purpose |
---|
2715 | |
---|
2716 | """ |
---|
2717 | self.sizer3.Clear(True) |
---|
2718 | self.parameters = [] |
---|
2719 | self.str_parameters = [] |
---|
2720 | self.param_toFit = [] |
---|
2721 | self.fittable_param = [] |
---|
2722 | self.fixed_param = [] |
---|
2723 | self.orientation_params = [] |
---|
2724 | self.orientation_params_disp = [] |
---|
2725 | |
---|
2726 | if model is None: |
---|
2727 | self.sizer3.Layout() |
---|
2728 | self.SetupScrolling() |
---|
2729 | return |
---|
2730 | |
---|
2731 | box_description = wx.StaticBox(self, wx.ID_ANY, str("Model Parameters")) |
---|
2732 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
2733 | sizer = wx.GridBagSizer(5, 5) |
---|
2734 | # save the current model |
---|
2735 | self.model = model |
---|
2736 | |
---|
2737 | keys = self.model.getParamList() |
---|
2738 | |
---|
2739 | # list of dispersion parameters |
---|
2740 | self.disp_list = self.model.getDispParamList() |
---|
2741 | |
---|
2742 | def custom_compare(a, b): |
---|
2743 | """ |
---|
2744 | Custom compare to order, first by alphabets then second by number. |
---|
2745 | """ |
---|
2746 | # number at the last digit |
---|
2747 | a_last = a[len(a) - 1] |
---|
2748 | b_last = b[len(b) - 1] |
---|
2749 | # default |
---|
2750 | num_a = None |
---|
2751 | num_b = None |
---|
2752 | # split the names |
---|
2753 | a2 = a.lower().split('_') |
---|
2754 | b2 = b.lower().split('_') |
---|
2755 | # check length of a2, b2 |
---|
2756 | len_a2 = len(a2) |
---|
2757 | len_b2 = len(b2) |
---|
2758 | # check if it contains a int number(<10) |
---|
2759 | try: |
---|
2760 | num_a = int(a_last) |
---|
2761 | except: |
---|
2762 | pass |
---|
2763 | try: |
---|
2764 | num_b = int(b_last) |
---|
2765 | except: |
---|
2766 | pass |
---|
2767 | # Put 'scale' near the top; happens |
---|
2768 | # when numbered param name exists |
---|
2769 | if a == 'scale': |
---|
2770 | return -1 |
---|
2771 | # both have a number |
---|
2772 | if num_a is not None and num_b is not None: |
---|
2773 | if num_a > num_b: |
---|
2774 | return -1 |
---|
2775 | # same number |
---|
2776 | elif num_a == num_b: |
---|
2777 | # different last names |
---|
2778 | if a2[len_a2 - 1] != b2[len_b2 - 1] and num_a != 0: |
---|
2779 | return -cmp(a2[len_a2 - 1], b2[len_b2 - 1]) |
---|
2780 | else: |
---|
2781 | return cmp(a, b) |
---|
2782 | else: |
---|
2783 | return 1 |
---|
2784 | # one of them has a number |
---|
2785 | elif num_a is not None: |
---|
2786 | return 1 |
---|
2787 | elif num_b is not None: |
---|
2788 | return -1 |
---|
2789 | # no numbers |
---|
2790 | else: |
---|
2791 | return cmp(a.lower(), b.lower()) |
---|
2792 | |
---|
2793 | # keys obtained now from ordered dict, so commenting alphabetical |
---|
2794 | # ordering keys.sort(custom_compare) |
---|
2795 | |
---|
2796 | iy = 0 |
---|
2797 | ix = 0 |
---|
2798 | sizer.Add(wx.StaticText(self, wx.ID_ANY, 'Parameter'), |
---|
2799 | (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
2800 | ix += 1 |
---|
2801 | self.text2_2 = wx.StaticText(self, wx.ID_ANY, 'Value') |
---|
2802 | sizer.Add(self.text2_2, (iy, ix), (1, 1), |
---|
2803 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
2804 | ix += 2 |
---|
2805 | self.text2_3 = wx.StaticText(self, wx.ID_ANY, 'Error') |
---|
2806 | sizer.Add(self.text2_3, (iy, ix), (1, 1), |
---|
2807 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
2808 | if not self.is_mac: |
---|
2809 | self.text2_3.Hide() |
---|
2810 | ix += 1 |
---|
2811 | self.text2_min = wx.StaticText(self, wx.ID_ANY, 'Min') |
---|
2812 | sizer.Add(self.text2_min, (iy, ix), (1, 1), |
---|
2813 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
2814 | # self.text2_min.Hide() |
---|
2815 | ix += 1 |
---|
2816 | self.text2_max = wx.StaticText(self, wx.ID_ANY, 'Max') |
---|
2817 | sizer.Add(self.text2_max, (iy, ix), (1, 1), |
---|
2818 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
2819 | # self.text2_max.Hide() |
---|
2820 | ix += 1 |
---|
2821 | self.text2_4 = wx.StaticText(self, wx.ID_ANY, '[Units]') |
---|
2822 | sizer.Add(self.text2_4, (iy, ix), (1, 1), |
---|
2823 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
2824 | self.text2_4.Hide() |
---|
2825 | |
---|
2826 | CHECK_STATE = False |
---|
2827 | for item in keys: |
---|
2828 | |
---|
2829 | if item not in self.disp_list and not item in \ |
---|
2830 | self.model.orientation_params: |
---|
2831 | |
---|
2832 | # prepare a spot to store errors |
---|
2833 | if item not in self.model.details: |
---|
2834 | self.model.details[item] = ["", None, None] |
---|
2835 | |
---|
2836 | iy += 1 |
---|
2837 | ix = 0 |
---|
2838 | if (self.model.__class__ in |
---|
2839 | self.model_list_box["Multi-Functions"] or |
---|
2840 | self.temp_multi_functional)\ |
---|
2841 | and (item in self.model.non_fittable): |
---|
2842 | non_fittable_name = wx.StaticText(self, wx.ID_ANY, item) |
---|
2843 | sizer.Add(non_fittable_name, (iy, ix), (1, 1), |
---|
2844 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 21) |
---|
2845 | # add parameter value |
---|
2846 | ix += 1 |
---|
2847 | value = self.model.getParam(item) |
---|
2848 | if len(self.model.fun_list) > 0: |
---|
2849 | # num = item.split('_')[1][5:7] |
---|
2850 | fun_box = wx.ComboBox(self, wx.ID_ANY, size=(100, -1), |
---|
2851 | style=wx.CB_READONLY, name='%s' % item) |
---|
2852 | self._set_fun_box_list(fun_box) |
---|
2853 | fun_box.SetSelection(0) |
---|
2854 | # self.fun_box.SetToolTipString("A function |
---|
2855 | # describing the interface") |
---|
2856 | wx.EVT_COMBOBOX(fun_box, wx.ID_ANY, self._on_fun_box) |
---|
2857 | else: |
---|
2858 | fun_box = ModelTextCtrl(self, wx.ID_ANY, |
---|
2859 | size=(_BOX_WIDTH, 20), |
---|
2860 | style=wx.TE_PROCESS_ENTER, name='%s' % item) |
---|
2861 | fun_box.SetToolTipString( |
---|
2862 | "Hit 'Enter' after typing to update the plot.") |
---|
2863 | fun_box.SetValue(format_number(value, True)) |
---|
2864 | sizer.Add(fun_box, (iy, ix), (1, 1), wx.EXPAND) |
---|
2865 | self.str_parameters.append([None, item, fun_box, |
---|
2866 | None, None, None, |
---|
2867 | None, None]) |
---|
2868 | else: |
---|
2869 | # add parameters name with checkbox for selecting to fit |
---|
2870 | cb = wx.CheckBox(self, wx.ID_ANY, item) |
---|
2871 | cb.SetValue(CHECK_STATE) |
---|
2872 | cb.SetToolTipString(" Check mark to fit.") |
---|
2873 | # cb.SetValue(True) |
---|
2874 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
2875 | |
---|
2876 | sizer.Add(cb, (iy, ix), (1, 1), |
---|
2877 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 5) |
---|
2878 | |
---|
2879 | # add parameter value |
---|
2880 | ix += 1 |
---|
2881 | value = self.model.getParam(item) |
---|
2882 | ctl1 = ModelTextCtrl(self, wx.ID_ANY, size=(_BOX_WIDTH, 20), |
---|
2883 | style=wx.TE_PROCESS_ENTER) |
---|
2884 | ctl1.SetToolTipString( |
---|
2885 | "Hit 'Enter' after typing to update the plot.") |
---|
2886 | ctl1.SetValue(format_number(value, True)) |
---|
2887 | sizer.Add(ctl1, (iy, ix), (1, 1), wx.EXPAND) |
---|
2888 | # text to show error sign |
---|
2889 | ix += 1 |
---|
2890 | text2 = wx.StaticText(self, wx.ID_ANY, '+/-') |
---|
2891 | sizer.Add(text2, (iy, ix), (1, 1), |
---|
2892 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
2893 | if not self.is_mac: |
---|
2894 | text2.Hide() |
---|
2895 | ix += 1 |
---|
2896 | ctl2 = wx.TextCtrl(self, wx.ID_ANY, |
---|
2897 | size=(_BOX_WIDTH / 1.2, 20), style=0) |
---|
2898 | sizer.Add(ctl2, (iy, ix), (1, 1), |
---|
2899 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
2900 | if not self.is_mac: |
---|
2901 | ctl2.Hide() |
---|
2902 | |
---|
2903 | ix += 1 |
---|
2904 | ctl3 = ModelTextCtrl(self, wx.ID_ANY, |
---|
2905 | size=(_BOX_WIDTH / 1.9, 20), |
---|
2906 | style=wx.TE_PROCESS_ENTER, |
---|
2907 | text_enter_callback=self._onparamRangeEnter) |
---|
2908 | min_bound = self.model.details[item][1] |
---|
2909 | if min_bound is not None: |
---|
2910 | ctl3.SetValue(format_number(min_bound, True)) |
---|
2911 | |
---|
2912 | sizer.Add(ctl3, (iy, ix), (1, 1), |
---|
2913 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
2914 | |
---|
2915 | ix += 1 |
---|
2916 | ctl4 = ModelTextCtrl(self, wx.ID_ANY, |
---|
2917 | size=(_BOX_WIDTH / 1.9, 20), |
---|
2918 | style=wx.TE_PROCESS_ENTER, |
---|
2919 | text_enter_callback=self._onparamRangeEnter) |
---|
2920 | max_bound = self.model.details[item][2] |
---|
2921 | if max_bound is not None: |
---|
2922 | ctl4.SetValue(format_number(max_bound, True)) |
---|
2923 | sizer.Add(ctl4, (iy, ix), (1, 1), |
---|
2924 | wx.EXPAND | wx.FIXED_MINSIZE, 0) |
---|
2925 | |
---|
2926 | ix += 1 |
---|
2927 | # Units |
---|
2928 | if item in self.model.details: |
---|
2929 | units = wx.StaticText(self, wx.ID_ANY, |
---|
2930 | self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
2931 | else: |
---|
2932 | units = wx.StaticText(self, wx.ID_ANY, "", |
---|
2933 | style=wx.ALIGN_LEFT) |
---|
2934 | sizer.Add(units, (iy, ix), (1, 1), |
---|
2935 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
2936 | |
---|
2937 | self.parameters.append([cb, item, ctl1, |
---|
2938 | text2, ctl2, ctl3, ctl4, units]) |
---|
2939 | |
---|
2940 | iy += 1 |
---|
2941 | sizer.Add((10, 10), (iy, ix), (1, 1), |
---|
2942 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
2943 | |
---|
2944 | # type can be either Guassian or Array |
---|
2945 | if len(self.model.dispersion.values()) > 0: |
---|
2946 | type = self.model.dispersion.values()[0]["type"] |
---|
2947 | else: |
---|
2948 | type = "Gaussian" |
---|
2949 | |
---|
2950 | iy += 1 |
---|
2951 | ix = 0 |
---|
2952 | # Add tile for orientational angle |
---|
2953 | for item in keys: |
---|
2954 | if item in self.model.orientation_params: |
---|
2955 | orient_angle = wx.StaticText(self, wx.ID_ANY, '[For 2D only]:') |
---|
2956 | mag_on_button = wx.Button(self, wx.ID_ANY, "Magnetic ON") |
---|
2957 | mag_on_button.SetToolTipString("Turn Pol Beam/Mag scatt on/off") |
---|
2958 | mag_on_button.Bind(wx.EVT_BUTTON, self._on_mag_on) |
---|
2959 | mag_angle_help_button = wx.Button(self, wx.ID_ANY, |
---|
2960 | "Magnetic angles?") |
---|
2961 | mag_angle_help_button.SetToolTipString("see angle definitions") |
---|
2962 | mag_help_button = wx.Button(self, wx.ID_ANY, "Mag HELP") |
---|
2963 | mag_help_button.SetToolTipString("Help on pol beam/mag fitting") |
---|
2964 | mag_help_button.Bind(wx.EVT_BUTTON, self._on_mag_help) |
---|
2965 | mag_angle_help_button.Bind(wx.EVT_BUTTON, |
---|
2966 | self._on_mag_angle_help) |
---|
2967 | sizer.Add(orient_angle, (iy, ix), (1, 1), |
---|
2968 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
2969 | iy += 1 |
---|
2970 | sizer.Add(mag_on_button, (iy, ix), (1, 1), |
---|
2971 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
2972 | ix += 1 |
---|
2973 | sizer.Add(mag_angle_help_button, (iy, ix), (1, 1), |
---|
2974 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
2975 | sizer.Add(mag_help_button, (iy, ix + 1), (1, 1), |
---|
2976 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
2977 | |
---|
2978 | # handle the magnetic buttons |
---|
2979 | # clean this up so that assume mag is off then turn |
---|
2980 | # all buttons on IF mag has mag and has 2D |
---|
2981 | if not self._has_magnetic: |
---|
2982 | mag_on_button.Show(False) |
---|
2983 | elif not self.data.__class__.__name__ == "Data2D": |
---|
2984 | mag_on_button.Show(False) |
---|
2985 | else: |
---|
2986 | mag_on_button.Show(True) |
---|
2987 | mag_help_button.Show(False) |
---|
2988 | mag_angle_help_button.Show(False) |
---|
2989 | if mag_on_button.IsShown(): |
---|
2990 | if self.magnetic_on: |
---|
2991 | mag_on_button.SetLabel("Magnetic OFF") |
---|
2992 | mag_help_button.Show(True) |
---|
2993 | mag_angle_help_button.Show(True) |
---|
2994 | else: |
---|
2995 | mag_on_button.SetLabel("Magnetic ON") |
---|
2996 | mag_help_button.Show(False) |
---|
2997 | mag_angle_help_button.Show(False) |
---|
2998 | |
---|
2999 | if not self.data.__class__.__name__ == "Data2D" and \ |
---|
3000 | not self.enable2D: |
---|
3001 | orient_angle.Hide() |
---|
3002 | else: |
---|
3003 | orient_angle.Show(True) |
---|
3004 | break |
---|
3005 | |
---|
3006 | # For Gaussian only |
---|
3007 | if type.lower() != "array": |
---|
3008 | for item in self.model.orientation_params: |
---|
3009 | if not self.magnetic_on: |
---|
3010 | if item in self.model.magnetic_params: |
---|
3011 | continue |
---|
3012 | if item not in self.disp_list: |
---|
3013 | # prepare a spot to store min max |
---|
3014 | if item not in self.model.details: |
---|
3015 | self.model.details[item] = ["", None, None] |
---|
3016 | |
---|
3017 | iy += 1 |
---|
3018 | ix = 0 |
---|
3019 | # add parameters name with checkbox for selecting to fit |
---|
3020 | cb = wx.CheckBox(self, wx.ID_ANY, item) |
---|
3021 | cb.SetValue(CHECK_STATE) |
---|
3022 | cb.SetToolTipString("Check mark to fit") |
---|
3023 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
3024 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
3025 | self.enable2D: |
---|
3026 | cb.Show(True) |
---|
3027 | else: |
---|
3028 | cb.Hide() |
---|
3029 | sizer.Add(cb, (iy, ix), (1, 1), |
---|
3030 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 5) |
---|
3031 | |
---|
3032 | # add parameter value |
---|
3033 | ix += 1 |
---|
3034 | value = self.model.getParam(item) |
---|
3035 | ctl1 = ModelTextCtrl(self, -1, size=(_BOX_WIDTH, 20), |
---|
3036 | style=wx.TE_PROCESS_ENTER) |
---|
3037 | ctl1.SetToolTipString( |
---|
3038 | "Hit 'Enter' after typing to update the plot.") |
---|
3039 | ctl1.SetValue(format_number(value, True)) |
---|
3040 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
3041 | self.enable2D: |
---|
3042 | ctl1.Show(True) |
---|
3043 | else: |
---|
3044 | ctl1.Hide() |
---|
3045 | sizer.Add(ctl1, (iy, ix), (1, 1), wx.EXPAND) |
---|
3046 | # text to show error sign |
---|
3047 | ix += 1 |
---|
3048 | text2 = wx.StaticText(self, -1, '+/-') |
---|
3049 | sizer.Add(text2, (iy, ix), (1, 1), |
---|
3050 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
3051 | |
---|
3052 | text2.Hide() |
---|
3053 | ix += 1 |
---|
3054 | ctl2 = wx.TextCtrl(self, -1, |
---|
3055 | size=(_BOX_WIDTH / 1.2, 20), style=0) |
---|
3056 | sizer.Add(ctl2, (iy, ix), (1, 1), |
---|
3057 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
3058 | |
---|
3059 | ctl2.Hide() |
---|
3060 | |
---|
3061 | ix += 1 |
---|
3062 | ctl3 = ModelTextCtrl(self, -1, |
---|
3063 | size=(_BOX_WIDTH / 1.8, 20), |
---|
3064 | style=wx.TE_PROCESS_ENTER, |
---|
3065 | text_enter_callback=self._onparamRangeEnter) |
---|
3066 | |
---|
3067 | sizer.Add(ctl3, (iy, ix), (1, 1), |
---|
3068 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
3069 | ctl3.Hide() |
---|
3070 | |
---|
3071 | ix += 1 |
---|
3072 | ctl4 = ModelTextCtrl(self, -1, |
---|
3073 | size=(_BOX_WIDTH / 1.8, 20), |
---|
3074 | style=wx.TE_PROCESS_ENTER, |
---|
3075 | text_enter_callback=self._onparamRangeEnter) |
---|
3076 | sizer.Add(ctl4, (iy, ix), (1, 1), |
---|
3077 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
3078 | |
---|
3079 | ctl4.Hide() |
---|
3080 | |
---|
3081 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
3082 | self.enable2D: |
---|
3083 | if self.is_mac: |
---|
3084 | text2.Show(True) |
---|
3085 | ctl2.Show(True) |
---|
3086 | ctl3.Show(True) |
---|
3087 | ctl4.Show(True) |
---|
3088 | |
---|
3089 | ix += 1 |
---|
3090 | # Units |
---|
3091 | if item in self.model.details: |
---|
3092 | units = wx.StaticText(self, -1, |
---|
3093 | self.model.details[item][0], |
---|
3094 | style=wx.ALIGN_LEFT) |
---|
3095 | else: |
---|
3096 | units = wx.StaticText(self, -1, "", |
---|
3097 | style=wx.ALIGN_LEFT) |
---|
3098 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
3099 | self.enable2D: |
---|
3100 | units.Show(True) |
---|
3101 | else: |
---|
3102 | units.Hide() |
---|
3103 | |
---|
3104 | sizer.Add(units, (iy, ix), (1, 1), |
---|
3105 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
3106 | |
---|
3107 | self.parameters.append([cb, item, ctl1, |
---|
3108 | text2, ctl2, ctl3, ctl4, units]) |
---|
3109 | self.orientation_params.append([cb, item, ctl1, |
---|
3110 | text2, ctl2, ctl3, ctl4, units]) |
---|
3111 | |
---|
3112 | iy += 1 |
---|
3113 | box_description.SetForegroundColour(wx.BLUE) |
---|
3114 | # Display units text on panel |
---|
3115 | for item in keys: |
---|
3116 | if item in self.model.details: |
---|
3117 | self.text2_4.Show() |
---|
3118 | # Fill the list of fittable parameters |
---|
3119 | self.get_all_checked_params() |
---|
3120 | self.save_current_state_fit() |
---|
3121 | boxsizer1.Add(sizer) |
---|
3122 | self.sizer3.Add(boxsizer1, 0, wx.EXPAND | wx.ALL, 10) |
---|
3123 | self.sizer3.Layout() |
---|
3124 | self.Layout() |
---|
3125 | |
---|
3126 | def on_right_down(self, event): |
---|
3127 | """ |
---|
3128 | Get key stroke event |
---|
3129 | """ |
---|
3130 | if self.data is None: |
---|
3131 | return |
---|
3132 | # Figuring out key combo: Cmd for copy, Alt for paste |
---|
3133 | if event.AltDown() and event.ShiftDown(): |
---|
3134 | flag = True |
---|
3135 | elif event.AltDown() or event.ShiftDown(): |
---|
3136 | flag = False |
---|
3137 | else: |
---|
3138 | return |
---|
3139 | # make event free |
---|
3140 | event.Skip() |
---|
3141 | # messages depending on the flag |
---|
3142 | if not flag: |
---|
3143 | infor = 'warning' |
---|
3144 | # inform msg to wx |
---|
3145 | wx.PostEvent(self._manager.parent, |
---|
3146 | StatusEvent(status=msg, info=infor)) |
---|
3147 | |
---|
3148 | def _onModel2D(self, event): |
---|
3149 | """ |
---|
3150 | toggle view of model from 1D to 2D or 2D from 1D |
---|
3151 | """ |
---|
3152 | if self.model_view.GetLabelText() == "Show 2D": |
---|
3153 | self.model_view.SetLabel("Show 1D") |
---|
3154 | self.enable2D = True |
---|
3155 | |
---|
3156 | else: |
---|
3157 | self.model_view.SetLabel("Show 2D") |
---|
3158 | self.enable2D = False |
---|
3159 | self.Show(False) |
---|
3160 | self.create_default_data() |
---|
3161 | self._manager.store_data(self.uid, data_list=[self.data]) |
---|
3162 | |
---|
3163 | self.set_model_param_sizer(self.model) |
---|
3164 | self._set_sizer_dispersion() |
---|
3165 | self._set_weight(is_2D=self.enable2D) |
---|
3166 | self._set_smear_buttons() |
---|
3167 | self.Show(True) |
---|
3168 | self.SetupScrolling() |
---|
3169 | self._draw_model() |
---|
3170 | |
---|
3171 | self.state.enable2D = copy.deepcopy(self.enable2D) |
---|
3172 | |
---|
3173 | def _set_smear_buttons(self): |
---|
3174 | """ |
---|
3175 | Set semarer radio buttons |
---|
3176 | """ |
---|
3177 | # more disables for 2D |
---|
3178 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
3179 | self.enable2D: |
---|
3180 | self.slit_smearer.Disable() |
---|
3181 | self.pinhole_smearer.Enable(True) |
---|
3182 | self.default_mask = copy.deepcopy(self.data.mask) |
---|
3183 | else: |
---|
3184 | self.slit_smearer.Enable(True) |
---|
3185 | self.pinhole_smearer.Enable(True) |
---|
3186 | |
---|
3187 | |
---|
3188 | class BGTextCtrl(wx.TextCtrl): |
---|
3189 | """ |
---|
3190 | Text control used to display outputs. |
---|
3191 | No editing allowed. The background is |
---|
3192 | grayed out. User can't select text. |
---|
3193 | """ |
---|
3194 | def __init__(self, *args, **kwds): |
---|
3195 | wx.TextCtrl.__init__(self, *args, **kwds) |
---|
3196 | self.SetEditable(False) |
---|
3197 | self.SetBackgroundColour(self.GetParent().parent.GetBackgroundColour()) |
---|
3198 | |
---|
3199 | # Bind to mouse event to avoid text highlighting |
---|
3200 | # The event will be skipped once the call-back |
---|
3201 | # is called. |
---|
3202 | self.Bind(wx.EVT_MOUSE_EVENTS, self._click) |
---|
3203 | |
---|
3204 | def _click(self, event): |
---|
3205 | """ |
---|
3206 | Prevent further handling of the mouse event |
---|
3207 | by not calling Skip(). |
---|
3208 | """ |
---|
3209 | pass |
---|