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