1 | |
---|
2 | |
---|
3 | import sys |
---|
4 | import wx |
---|
5 | import wx.lib.newevent |
---|
6 | import numpy |
---|
7 | import copy |
---|
8 | import math |
---|
9 | import time |
---|
10 | from sans.models.dispersion_models import ArrayDispersion, GaussianDispersion |
---|
11 | from DataLoader.data_info import Data1D |
---|
12 | from sans.guiframe.events import StatusEvent |
---|
13 | from sans.guiframe.events import NewPlotEvent |
---|
14 | from sans.guiframe.utils import format_number,check_float |
---|
15 | |
---|
16 | (Chi2UpdateEvent, EVT_CHI2_UPDATE) = wx.lib.newevent.NewEvent() |
---|
17 | _BOX_WIDTH = 76 |
---|
18 | _DATA_BOX_WIDTH = 300 |
---|
19 | SMEAR_SIZE_L = 0.00 |
---|
20 | SMEAR_SIZE_H = 0.00 |
---|
21 | |
---|
22 | import basepage |
---|
23 | from basepage import BasicPage |
---|
24 | from basepage import PageInfoEvent |
---|
25 | from sans.models.qsmearing import smear_selection |
---|
26 | from fitpage import FitPage |
---|
27 | |
---|
28 | class BatchFitPage(FitPage): |
---|
29 | window_name = "BatchFit" |
---|
30 | window_caption = "BatchFit" |
---|
31 | |
---|
32 | def __init__(self,parent, color='rand'): |
---|
33 | """ |
---|
34 | Initialization of the Panel |
---|
35 | """ |
---|
36 | FitPage.__init__(self, parent, color=color) |
---|
37 | self.window_name = "BatchFit" |
---|
38 | self.window_caption = "BatchFit" |
---|
39 | |
---|
40 | def _fill_range_sizer(self): |
---|
41 | """ |
---|
42 | Fill the sizer containing the plotting range |
---|
43 | add access to npts |
---|
44 | """ |
---|
45 | is_2Ddata = False |
---|
46 | |
---|
47 | # Check if data is 2D |
---|
48 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
49 | self.enable2D: |
---|
50 | is_2Ddata = True |
---|
51 | |
---|
52 | title = "Fitting" |
---|
53 | #smear messages & titles |
---|
54 | smear_message_none = "No smearing is selected..." |
---|
55 | smear_message_dqdata = "The dQ data is being used for smearing..." |
---|
56 | smear_message_2d = "Higher accuracy is very time-expensive. Use it with care..." |
---|
57 | smear_message_new_ssmear = "Please enter only the value of interest to customize smearing..." |
---|
58 | smear_message_new_psmear = "Please enter both; the dQ will be generated by interpolation..." |
---|
59 | smear_message_2d_x_title = "<dQp>[1/A]:" |
---|
60 | smear_message_2d_y_title = "<dQs>[1/A]:" |
---|
61 | smear_message_pinhole_min_title = "dQ_low[1/A]:" |
---|
62 | smear_message_pinhole_max_title = "dQ_high[1/A]:" |
---|
63 | smear_message_slit_height_title = "Slit height[1/A]:" |
---|
64 | smear_message_slit_width_title = "Slit width[1/A]:" |
---|
65 | |
---|
66 | self._get_smear_info() |
---|
67 | |
---|
68 | #Sizers |
---|
69 | box_description_range = wx.StaticBox(self, -1,str(title)) |
---|
70 | boxsizer_range = wx.StaticBoxSizer(box_description_range, wx.VERTICAL) |
---|
71 | self.sizer_set_smearer = wx.BoxSizer(wx.VERTICAL) |
---|
72 | sizer_smearer = wx.BoxSizer(wx.HORIZONTAL) |
---|
73 | self.sizer_new_smear= wx.BoxSizer(wx.HORIZONTAL) |
---|
74 | self.sizer_set_masking = wx.BoxSizer(wx.HORIZONTAL) |
---|
75 | sizer_chi2 = wx.BoxSizer(wx.VERTICAL) |
---|
76 | """ |
---|
77 | smear_set_box= wx.StaticBox(self, -1,'Set Instrumental Smearing') |
---|
78 | sizer_smearer_box = wx.StaticBoxSizer(smear_set_box, wx.HORIZONTAL) |
---|
79 | sizer_smearer_box.SetMinSize((_DATA_BOX_WIDTH,85)) |
---|
80 | """ |
---|
81 | sizer_fit = wx.GridSizer(2, 4, 2, 6) |
---|
82 | """ |
---|
83 | # combobox for smear2d accuracy selection |
---|
84 | self.smear_accuracy = wx.ComboBox(self, -1,size=(50,-1),style=wx.CB_READONLY) |
---|
85 | self._set_accuracy_list() |
---|
86 | self.smear_accuracy.SetValue(self.smear2d_accuracy) |
---|
87 | self.smear_accuracy.SetSelection(0) |
---|
88 | self.smear_accuracy.SetToolTipString("'Higher' uses more Gaussian points for smearing computation.") |
---|
89 | |
---|
90 | wx.EVT_COMBOBOX(self.smear_accuracy,-1, self._on_select_accuracy) |
---|
91 | """ |
---|
92 | #Fit button |
---|
93 | self.btFit = wx.Button(self,wx.NewId(),'Fit', size=(88,25)) |
---|
94 | self.default_bt_colour = self.btFit.GetDefaultAttributes() |
---|
95 | self.btFit.Bind(wx.EVT_BUTTON, self._onFit,id= self.btFit.GetId()) |
---|
96 | self.btFit.SetToolTipString("Start fitting.") |
---|
97 | """ |
---|
98 | #textcntrl for custom resolution |
---|
99 | self.smear_pinhole_max = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH-25,20),style=wx.TE_PROCESS_ENTER, |
---|
100 | text_enter_callback = self.onPinholeSmear) |
---|
101 | self.smear_pinhole_min = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH-25,20),style=wx.TE_PROCESS_ENTER, |
---|
102 | text_enter_callback = self.onPinholeSmear) |
---|
103 | self.smear_slit_height= self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH-25,20),style=wx.TE_PROCESS_ENTER, |
---|
104 | text_enter_callback = self.onSlitSmear) |
---|
105 | self.smear_slit_width = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH-25,20),style=wx.TE_PROCESS_ENTER, |
---|
106 | text_enter_callback = self.onSlitSmear) |
---|
107 | |
---|
108 | ## smear |
---|
109 | self.smear_data_left= BGTextCtrl(self, -1, size=(_BOX_WIDTH-25,20), style=0) |
---|
110 | self.smear_data_left.SetValue(str(self.dq_l)) |
---|
111 | self.smear_data_right = BGTextCtrl(self, -1, size=(_BOX_WIDTH-25,20), style=0) |
---|
112 | self.smear_data_right.SetValue(str(self.dq_r)) |
---|
113 | |
---|
114 | #set default values for smear |
---|
115 | self.smear_pinhole_max.SetValue(str(self.dx_max)) |
---|
116 | self.smear_pinhole_min.SetValue(str(self.dx_min)) |
---|
117 | self.smear_slit_height.SetValue(str(self.dxl)) |
---|
118 | self.smear_slit_width.SetValue(str(self.dxw)) |
---|
119 | |
---|
120 | #Filling the sizer containing instruments smearing info. |
---|
121 | self.disable_smearer = wx.RadioButton(self, -1, 'None', style=wx.RB_GROUP) |
---|
122 | self.enable_smearer = wx.RadioButton(self, -1, 'Use dQ Data') |
---|
123 | #self.enable_smearer.SetToolTipString("Click to use the loaded dQ data for smearing.") |
---|
124 | self.pinhole_smearer = wx.RadioButton(self, -1, 'Custom Pinhole Smear') |
---|
125 | #self.pinhole_smearer.SetToolTipString("Click to input custom resolution for pinhole smearing.") |
---|
126 | self.slit_smearer = wx.RadioButton(self, -1, 'Custom Slit Smear') |
---|
127 | #self.slit_smearer.SetToolTipString("Click to input custom resolution for slit smearing.") |
---|
128 | self.Bind(wx.EVT_RADIOBUTTON, self.onSmear, id=self.disable_smearer.GetId()) |
---|
129 | self.Bind(wx.EVT_RADIOBUTTON, self.onSmear, id=self.enable_smearer.GetId()) |
---|
130 | self.Bind(wx.EVT_RADIOBUTTON, self.onPinholeSmear, id=self.pinhole_smearer.GetId()) |
---|
131 | self.Bind(wx.EVT_RADIOBUTTON, self.onSlitSmear, id=self.slit_smearer.GetId()) |
---|
132 | self.disable_smearer.SetValue(True) |
---|
133 | |
---|
134 | # add 4 types of smearing to the sizer |
---|
135 | sizer_smearer.Add( self.disable_smearer,0, wx.LEFT, 10) |
---|
136 | sizer_smearer.Add((10,10)) |
---|
137 | sizer_smearer.Add( self.enable_smearer) |
---|
138 | sizer_smearer.Add((10,10)) |
---|
139 | sizer_smearer.Add( self.pinhole_smearer ) |
---|
140 | sizer_smearer.Add((10,10)) |
---|
141 | sizer_smearer.Add( self.slit_smearer ) |
---|
142 | sizer_smearer.Add((10,10)) |
---|
143 | """ |
---|
144 | |
---|
145 | # StaticText for chi2, N(for fitting), Npts |
---|
146 | self.tcChi = BGTextCtrl(self, -1, "-", size=(75,20), style=0) |
---|
147 | self.tcChi.SetToolTipString("Chi2/Npts(Fit)") |
---|
148 | self.Npts_fit = BGTextCtrl(self, -1, "-", size=(75,20), style=0) |
---|
149 | self.Npts_fit.SetToolTipString(\ |
---|
150 | " Npts : number of points selected for fitting") |
---|
151 | self.Npts_total = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH, 20), |
---|
152 | style=wx.TE_PROCESS_ENTER, |
---|
153 | text_enter_callback=self._onQrangeEnter) |
---|
154 | self.Npts_total.SetValue(format_number(self.npts_x)) |
---|
155 | self.Npts_total.SetToolTipString(\ |
---|
156 | " Total Npts : total number of data points") |
---|
157 | |
---|
158 | # Update and Draw button |
---|
159 | self.draw_button = wx.Button(self,wx.NewId(),'Compute', size=(88,24)) |
---|
160 | self.draw_button.Bind(wx.EVT_BUTTON, \ |
---|
161 | self._onDraw,id= self.draw_button.GetId()) |
---|
162 | self.draw_button.SetToolTipString("Compute and Draw.") |
---|
163 | |
---|
164 | box_description_1= wx.StaticText(self, -1,' Chi2/Npts') |
---|
165 | box_description_2= wx.StaticText(self, -1,'Npts(Fit)') |
---|
166 | box_description_3= wx.StaticText(self, -1,'Total Npts') |
---|
167 | box_description_3.SetToolTipString( \ |
---|
168 | " Total Npts : total number of data points") |
---|
169 | #box_description_4= wx.StaticText(self, -1,' ') |
---|
170 | |
---|
171 | |
---|
172 | sizer_fit.Add(box_description_1,0,0) |
---|
173 | sizer_fit.Add(box_description_2,0,0) |
---|
174 | sizer_fit.Add(box_description_3,0,0) |
---|
175 | sizer_fit.Add(self.draw_button,0,0) |
---|
176 | sizer_fit.Add(self.tcChi,0,0) |
---|
177 | sizer_fit.Add(self.Npts_fit ,0,0) |
---|
178 | sizer_fit.Add(self.Npts_total,0,0) |
---|
179 | sizer_fit.Add(self.btFit,0,0) |
---|
180 | """ |
---|
181 | self.smear_description_none = wx.StaticText(self, -1, smear_message_none , style=wx.ALIGN_LEFT) |
---|
182 | self.smear_description_dqdata = wx.StaticText(self, -1, smear_message_dqdata , style=wx.ALIGN_LEFT) |
---|
183 | self.smear_description_type = wx.StaticText(self, -1, "Type:" , style=wx.ALIGN_LEFT) |
---|
184 | self.smear_description_accuracy_type = wx.StaticText(self, -1, "Accuracy:" , style=wx.ALIGN_LEFT) |
---|
185 | self.smear_description_smear_type = BGTextCtrl(self, -1, size=(57,20), style=0) |
---|
186 | self.smear_description_smear_type.SetValue(str(self.dq_l)) |
---|
187 | self.SetBackgroundColour(self.GetParent().GetBackgroundColour()) |
---|
188 | self.smear_description_2d = wx.StaticText(self, -1, smear_message_2d , style=wx.ALIGN_LEFT) |
---|
189 | self.smear_message_new_s = wx.StaticText(self, -1, smear_message_new_ssmear , style=wx.ALIGN_LEFT) |
---|
190 | self.smear_message_new_p = wx.StaticText(self, -1, smear_message_new_psmear , style=wx.ALIGN_LEFT) |
---|
191 | self.smear_description_2d_x = wx.StaticText(self, -1, smear_message_2d_x_title , style=wx.ALIGN_LEFT) |
---|
192 | self.smear_description_2d_x.SetToolTipString(" dQp(parallel) in q_r direction.") |
---|
193 | self.smear_description_2d_y = wx.StaticText(self, -1, smear_message_2d_y_title , style=wx.ALIGN_LEFT) |
---|
194 | self.smear_description_2d_y.SetToolTipString(" dQs(perpendicular) in q_phi direction.") |
---|
195 | self.smear_description_pin_min = wx.StaticText(self, -1, smear_message_pinhole_min_title , style=wx.ALIGN_LEFT) |
---|
196 | self.smear_description_pin_max = wx.StaticText(self, -1, smear_message_pinhole_max_title , style=wx.ALIGN_LEFT) |
---|
197 | self.smear_description_slit_height = wx.StaticText(self, -1, smear_message_slit_height_title , style=wx.ALIGN_LEFT) |
---|
198 | self.smear_description_slit_width = wx.StaticText(self, -1, smear_message_slit_width_title , style=wx.ALIGN_LEFT) |
---|
199 | """ |
---|
200 | #arrange sizers |
---|
201 | #boxsizer1.Add( self.tcChi ) |
---|
202 | """ |
---|
203 | self.sizer_set_smearer.Add(sizer_smearer ) |
---|
204 | self.sizer_set_smearer.Add((10,10)) |
---|
205 | self.sizer_set_smearer.Add( self.smear_description_none,0, wx.CENTER, 10 ) |
---|
206 | self.sizer_set_smearer.Add( self.smear_description_dqdata,0, wx.CENTER, 10 ) |
---|
207 | self.sizer_set_smearer.Add( self.smear_description_2d,0, wx.CENTER, 10 ) |
---|
208 | self.sizer_new_smear.Add( self.smear_description_type,0, wx.CENTER, 10 ) |
---|
209 | self.sizer_new_smear.Add( self.smear_description_accuracy_type,0, wx.CENTER, 10 ) |
---|
210 | self.sizer_new_smear.Add( self.smear_accuracy ) |
---|
211 | self.sizer_new_smear.Add( self.smear_description_smear_type,0, wx.CENTER, 10 ) |
---|
212 | self.sizer_new_smear.Add((15,-1)) |
---|
213 | self.sizer_new_smear.Add( self.smear_description_2d_x,0, wx.CENTER, 10 ) |
---|
214 | self.sizer_new_smear.Add( self.smear_description_pin_min,0, wx.CENTER, 10 ) |
---|
215 | self.sizer_new_smear.Add( self.smear_description_slit_height,0, wx.CENTER, 10 ) |
---|
216 | |
---|
217 | self.sizer_new_smear.Add( self.smear_pinhole_min,0, wx.CENTER, 10 ) |
---|
218 | self.sizer_new_smear.Add( self.smear_slit_height,0, wx.CENTER, 10 ) |
---|
219 | self.sizer_new_smear.Add( self.smear_data_left,0, wx.CENTER, 10 ) |
---|
220 | self.sizer_new_smear.Add((20,-1)) |
---|
221 | self.sizer_new_smear.Add( self.smear_description_2d_y,0, wx.CENTER, 10 ) |
---|
222 | self.sizer_new_smear.Add( self.smear_description_pin_max,0, wx.CENTER, 10 ) |
---|
223 | self.sizer_new_smear.Add( self.smear_description_slit_width,0, wx.CENTER, 10 ) |
---|
224 | |
---|
225 | self.sizer_new_smear.Add( self.smear_pinhole_max,0, wx.CENTER, 10 ) |
---|
226 | self.sizer_new_smear.Add( self.smear_slit_width,0, wx.CENTER, 10 ) |
---|
227 | self.sizer_new_smear.Add( self.smear_data_right,0, wx.CENTER, 10 ) |
---|
228 | |
---|
229 | self.sizer_set_smearer.Add( self.smear_message_new_s,0, wx.CENTER, 10) |
---|
230 | self.sizer_set_smearer.Add( self.smear_message_new_p,0, wx.CENTER, 10) |
---|
231 | self.sizer_set_smearer.Add((5,2)) |
---|
232 | self.sizer_set_smearer.Add( self.sizer_new_smear,0, wx.CENTER, 10 ) |
---|
233 | |
---|
234 | # add all to chi2 sizer |
---|
235 | sizer_smearer_box.Add(self.sizer_set_smearer) |
---|
236 | |
---|
237 | sizer_chi2.Add(sizer_smearer_box) |
---|
238 | """ |
---|
239 | sizer_chi2.Add((-1,5)) |
---|
240 | """ |
---|
241 | # hide all smear messages and textctrl |
---|
242 | self._hide_all_smear_info() |
---|
243 | """ |
---|
244 | # get smear_selection |
---|
245 | self.current_smearer= smear_selection( self.data, self.model ) |
---|
246 | """ |
---|
247 | # Show only the relevant smear messages, etc |
---|
248 | if self.current_smearer == None: |
---|
249 | if not is_2Ddata: |
---|
250 | self.smear_description_none.Show(True) |
---|
251 | self.enable_smearer.Disable() |
---|
252 | else: |
---|
253 | self.smear_description_none.Show(True) |
---|
254 | #self.smear_description_2d.Show(True) |
---|
255 | #self.pinhole_smearer.Disable() |
---|
256 | self.slit_smearer.Disable() |
---|
257 | #self.enable_smearer.Disable() |
---|
258 | if self.data == None: |
---|
259 | self.slit_smearer.Disable() |
---|
260 | self.pinhole_smearer.Disable() |
---|
261 | self.enable_smearer.Disable() |
---|
262 | else: self._show_smear_sizer() |
---|
263 | """ |
---|
264 | boxsizer_range.Add(self.sizer_set_masking) |
---|
265 | #2D data? default |
---|
266 | is_2Ddata = False |
---|
267 | |
---|
268 | #check if it is 2D data |
---|
269 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
270 | self.enable2D: |
---|
271 | is_2Ddata = True |
---|
272 | |
---|
273 | self.sizer5.Clear(True) |
---|
274 | |
---|
275 | self.qmin = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH,20), |
---|
276 | style=wx.TE_PROCESS_ENTER, |
---|
277 | text_enter_callback = self._onQrangeEnter) |
---|
278 | self.qmin.SetValue(str(self.qmin_x)) |
---|
279 | self.qmin.SetToolTipString("Minimun value of Q in linear scale.") |
---|
280 | |
---|
281 | self.qmax = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH,20), |
---|
282 | style=wx.TE_PROCESS_ENTER, |
---|
283 | text_enter_callback=self._onQrangeEnter) |
---|
284 | self.qmax.SetValue(str(self.qmax_x)) |
---|
285 | self.qmax.SetToolTipString("Maximum value of Q in linear scale.") |
---|
286 | |
---|
287 | id = wx.NewId() |
---|
288 | self.reset_qrange =wx.Button(self,id,'Reset',size=(77,20)) |
---|
289 | |
---|
290 | self.reset_qrange.Bind(wx.EVT_BUTTON, self.on_reset_clicked,id=id) |
---|
291 | self.reset_qrange.SetToolTipString("Reset Q range to the default values") |
---|
292 | |
---|
293 | sizer_horizontal=wx.BoxSizer(wx.HORIZONTAL) |
---|
294 | sizer= wx.GridSizer(2, 4, 2, 6) |
---|
295 | |
---|
296 | self.btEditMask = wx.Button(self,wx.NewId(),'Editor', size=(88,23)) |
---|
297 | self.btEditMask.Bind(wx.EVT_BUTTON, self._onMask,id= self.btEditMask.GetId()) |
---|
298 | self.btEditMask.SetToolTipString("Edit Mask.") |
---|
299 | self.EditMask_title = wx.StaticText(self, -1, ' Masking(2D)') |
---|
300 | |
---|
301 | sizer.Add(wx.StaticText(self, -1, 'Q range')) |
---|
302 | sizer.Add(wx.StaticText(self, -1, ' Min[1/A]')) |
---|
303 | sizer.Add(wx.StaticText(self, -1, ' Max[1/A]')) |
---|
304 | sizer.Add(self.EditMask_title) |
---|
305 | #sizer.Add(wx.StaticText(self, -1, '')) |
---|
306 | sizer.Add(self.reset_qrange) |
---|
307 | sizer.Add(self.qmin) |
---|
308 | sizer.Add(self.qmax) |
---|
309 | #sizer.Add(self.theory_npts_tcrtl) |
---|
310 | sizer.Add(self.btEditMask) |
---|
311 | boxsizer_range.Add(sizer_chi2) |
---|
312 | boxsizer_range.Add((10,10)) |
---|
313 | boxsizer_range.Add(sizer) |
---|
314 | |
---|
315 | boxsizer_range.Add((10,15)) |
---|
316 | boxsizer_range.Add(sizer_fit) |
---|
317 | if is_2Ddata: |
---|
318 | self.btEditMask.Enable() |
---|
319 | self.EditMask_title.Enable() |
---|
320 | else: |
---|
321 | self.btEditMask.Disable() |
---|
322 | self.EditMask_title.Disable() |
---|
323 | """ |
---|
324 | ## save state |
---|
325 | self.save_current_state() |
---|
326 | """ |
---|
327 | self.sizer5.Add(boxsizer_range,0, wx.EXPAND | wx.ALL, 10) |
---|
328 | self.sizer5.Layout() |
---|
329 | |
---|
330 | def _on_select_model(self, event=None): |
---|
331 | """ |
---|
332 | call back for model selection |
---|
333 | """ |
---|
334 | |
---|
335 | self.Show(False) |
---|
336 | self._on_select_model_helper() |
---|
337 | self.set_model_param_sizer(self.model) |
---|
338 | if self.model is None: |
---|
339 | self._set_bookmark_flag(False) |
---|
340 | self._keep.Enable(False) |
---|
341 | self._set_save_flag(False) |
---|
342 | self.enable_disp.SetValue(False) |
---|
343 | self.disable_disp.SetValue(True) |
---|
344 | try: |
---|
345 | self.set_dispers_sizer() |
---|
346 | except: |
---|
347 | pass |
---|
348 | """ |
---|
349 | #self.btFit.SetFocus() |
---|
350 | self.state.enable_disp = self.enable_disp.GetValue() |
---|
351 | self.state.disable_disp = self.disable_disp.GetValue() |
---|
352 | self.state.pinhole_smearer = self.pinhole_smearer.GetValue() |
---|
353 | self.state.slit_smearer = self.slit_smearer.GetValue() |
---|
354 | """ |
---|
355 | self.state.structurecombobox = self.structurebox.GetCurrentSelection() |
---|
356 | self.state.formfactorcombobox = self.formfactorbox.GetCurrentSelection() |
---|
357 | |
---|
358 | if self.model != None: |
---|
359 | self._set_copy_flag(True) |
---|
360 | self._set_paste_flag(True) |
---|
361 | if self.data != None: |
---|
362 | self._set_bookmark_flag(True) |
---|
363 | self._keep.Enable(True) |
---|
364 | |
---|
365 | temp_smear = None |
---|
366 | """ |
---|
367 | #self._set_save_flag(True) |
---|
368 | # Reset smearer, model and data |
---|
369 | self._set_smear(self.data) |
---|
370 | try: |
---|
371 | # update smearer sizer |
---|
372 | self.onSmear(None) |
---|
373 | temp_smear = None |
---|
374 | if self.enable_smearer.GetValue(): |
---|
375 | # Set the smearer environments |
---|
376 | temp_smear = self.smearer |
---|
377 | except: |
---|
378 | raise |
---|
379 | ## error occured on chisqr computation |
---|
380 | #pass |
---|
381 | """ |
---|
382 | ## event to post model to fit to fitting plugins |
---|
383 | (ModelEventbox, EVT_MODEL_BOX) = wx.lib.newevent.NewEvent() |
---|
384 | |
---|
385 | ## set smearing value whether or not |
---|
386 | # the data contain the smearing info |
---|
387 | evt = ModelEventbox(model=self.model, |
---|
388 | smearer=temp_smear, |
---|
389 | qmin=float(self.qmin_x), |
---|
390 | uid=self.uid, |
---|
391 | qmax=float(self.qmax_x)) |
---|
392 | |
---|
393 | self._manager._on_model_panel(evt=evt) |
---|
394 | self.mbox_description.SetLabel("Model [%s]" % str(self.model.name)) |
---|
395 | self.state.model = self.model.clone() |
---|
396 | self.state.model.name = self.model.name |
---|
397 | |
---|
398 | |
---|
399 | if event != None: |
---|
400 | ## post state to fit panel |
---|
401 | new_event = PageInfoEvent(page = self) |
---|
402 | wx.PostEvent(self.parent, new_event) |
---|
403 | #update list of plugins if new plugin is available |
---|
404 | if self.plugin_rbutton.GetValue(): |
---|
405 | temp = self.parent.update_model_list() |
---|
406 | if temp: |
---|
407 | self.model_list_box = temp |
---|
408 | current_val = self.formfactorbox.GetValue() |
---|
409 | pos = self.formfactorbox.GetSelection() |
---|
410 | self._show_combox_helper() |
---|
411 | self.formfactorbox.SetSelection(pos) |
---|
412 | self.formfactorbox.SetValue(current_val) |
---|
413 | self._onDraw(event=None) |
---|
414 | else: |
---|
415 | self._draw_model() |
---|
416 | self.SetupScrolling() |
---|
417 | self.Show(True) |
---|
418 | |
---|
419 | def _update_paramv_on_fit(self): |
---|
420 | """ |
---|
421 | make sure that update param values just before the fitting |
---|
422 | """ |
---|
423 | #flag for qmin qmax check values |
---|
424 | flag = True |
---|
425 | self.fitrange = True |
---|
426 | is_modified = False |
---|
427 | |
---|
428 | #wx.PostEvent(self._manager.parent, StatusEvent(status=" \ |
---|
429 | #updating ... ",type="update")) |
---|
430 | |
---|
431 | ##So make sure that update param values on_Fit. |
---|
432 | #self._undo.Enable(True) |
---|
433 | if self.model !=None: |
---|
434 | ##Check the values |
---|
435 | self._check_value_enter( self.fittable_param ,is_modified) |
---|
436 | self._check_value_enter( self.fixed_param ,is_modified) |
---|
437 | self._check_value_enter( self.parameters ,is_modified) |
---|
438 | |
---|
439 | # If qmin and qmax have been modified, update qmin and qmax and |
---|
440 | # Here we should check whether the boundaries have been modified. |
---|
441 | # If qmin and qmax have been modified, update qmin and qmax and |
---|
442 | # set the is_modified flag to True |
---|
443 | self.fitrange = self._validate_qrange(self.qmin, self.qmax) |
---|
444 | if self.fitrange: |
---|
445 | tempmin = float(self.qmin.GetValue()) |
---|
446 | if tempmin != self.qmin_x: |
---|
447 | self.qmin_x = tempmin |
---|
448 | tempmax = float(self.qmax.GetValue()) |
---|
449 | if tempmax != self.qmax_x: |
---|
450 | self.qmax_x = tempmax |
---|
451 | if tempmax == tempmin: |
---|
452 | flag = False |
---|
453 | temp_smearer = None |
---|
454 | """ |
---|
455 | if not self.disable_smearer.GetValue(): |
---|
456 | temp_smearer= self.current_smearer |
---|
457 | if self.slit_smearer.GetValue(): |
---|
458 | flag = self.update_slit_smear() |
---|
459 | elif self.pinhole_smearer.GetValue(): |
---|
460 | flag = self.update_pinhole_smear() |
---|
461 | else: |
---|
462 | self._manager.set_smearer(smearer=temp_smearer, |
---|
463 | uid=self.uid, |
---|
464 | qmin=float(self.qmin_x), |
---|
465 | qmax=float(self.qmax_x), |
---|
466 | draw=False) |
---|
467 | elif not self._is_2D(): |
---|
468 | self._manager.set_smearer(smearer=temp_smearer, |
---|
469 | qmin=float(self.qmin_x), |
---|
470 | uid=self.uid, |
---|
471 | qmax= float(self.qmax_x), |
---|
472 | draw=False) |
---|
473 | |
---|
474 | if self.data != None: |
---|
475 | index_data = ((self.qmin_x <= self.data.x)&\ |
---|
476 | (self.data.x <= self.qmax_x)) |
---|
477 | val = str(len(self.data.x[index_data==True])) |
---|
478 | self.Npts_fit.SetValue(val) |
---|
479 | else: |
---|
480 | # No data in the panel |
---|
481 | try: |
---|
482 | self.npts_x = float(self.Npts_total.GetValue()) |
---|
483 | except: |
---|
484 | flag = False |
---|
485 | return flag |
---|
486 | flag = True |
---|
487 | """ |
---|
488 | if self._is_2D(): |
---|
489 | # only 2D case set mask |
---|
490 | flag = self._validate_Npts() |
---|
491 | if not flag: |
---|
492 | return flag |
---|
493 | else: flag = False |
---|
494 | else: |
---|
495 | flag = False |
---|
496 | |
---|
497 | #For invalid q range, disable the mask editor and fit button, vs. |
---|
498 | if not self.fitrange: |
---|
499 | #self.btFit.Disable() |
---|
500 | if self._is_2D(): |
---|
501 | self.btEditMask.Disable() |
---|
502 | else: |
---|
503 | #self.btFit.Enable(True) |
---|
504 | if self._is_2D() and self.data != None: |
---|
505 | self.btEditMask.Enable(True) |
---|
506 | |
---|
507 | if not flag: |
---|
508 | msg = "Cannot Plot or Fit :Must select a " |
---|
509 | msg += " model or Fitting range is not valid!!! " |
---|
510 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg)) |
---|
511 | |
---|
512 | self.save_current_state() |
---|
513 | |
---|
514 | return flag |
---|
515 | def save_current_state(self): |
---|
516 | """ |
---|
517 | Currently no save option implemented for batch page |
---|
518 | """ |
---|
519 | pass |
---|
520 | def save_current_state_fit(self): |
---|
521 | """ |
---|
522 | Currently no save option implemented for batch page |
---|
523 | """ |
---|
524 | pass |
---|
525 | def set_data(self, data): |
---|
526 | """ |
---|
527 | reset the current data |
---|
528 | """ |
---|
529 | id = None |
---|
530 | group_id = None |
---|
531 | flag = False |
---|
532 | if self.data is None and data is not None: |
---|
533 | flag = True |
---|
534 | if data is not None: |
---|
535 | id = data.id |
---|
536 | group_id = data.group_id |
---|
537 | if self.data is not None: |
---|
538 | flag = (data.id != self.data.id) |
---|
539 | self.data = data |
---|
540 | if self.data is None: |
---|
541 | data_min = "" |
---|
542 | data_max = "" |
---|
543 | data_name = "" |
---|
544 | self._set_bookmark_flag(False) |
---|
545 | self._keep.Enable(False) |
---|
546 | self._set_save_flag(False) |
---|
547 | else: |
---|
548 | if self.model != None: |
---|
549 | self._set_bookmark_flag(True) |
---|
550 | self._keep.Enable(True) |
---|
551 | self._set_save_flag(True) |
---|
552 | self._set_preview_flag(True) |
---|
553 | """ |
---|
554 | self._set_smear(data) |
---|
555 | # more disables for 2D |
---|
556 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
557 | self.enable2D: |
---|
558 | self.slit_smearer.Disable() |
---|
559 | self.pinhole_smearer.Enable(True) |
---|
560 | self.default_mask = copy.deepcopy(self.data.mask) |
---|
561 | else: |
---|
562 | self.slit_smearer.Enable(True) |
---|
563 | self.pinhole_smearer.Enable(True) |
---|
564 | """ |
---|
565 | self.formfactorbox.Enable() |
---|
566 | self.structurebox.Enable() |
---|
567 | data_name = self.data.name |
---|
568 | #set maximum range for x in linear scale |
---|
569 | if not hasattr(self.data,"data"): #Display only for 1D data fit |
---|
570 | # Minimum value of data |
---|
571 | data_min = min(self.data.x) |
---|
572 | # Maximum value of data |
---|
573 | data_max = max(self.data.x) |
---|
574 | #number of total data points |
---|
575 | self.Npts_total.SetValue(str(len(self.data.x))) |
---|
576 | #default:number of data points selected to fit |
---|
577 | self.Npts_fit.SetValue(str(len(self.data.x))) |
---|
578 | self.btEditMask.Disable() |
---|
579 | self.EditMask_title.Disable() |
---|
580 | else: |
---|
581 | |
---|
582 | ## Minimum value of data |
---|
583 | data_min = 0 |
---|
584 | x = max(math.fabs(self.data.xmin), math.fabs(self.data.xmax)) |
---|
585 | y = max(math.fabs(self.data.ymin), math.fabs(self.data.ymax)) |
---|
586 | ## Maximum value of data |
---|
587 | data_max = math.sqrt(x*x + y*y) |
---|
588 | #number of total data points |
---|
589 | self.Npts_total.SetValue(str(len(self.data.data))) |
---|
590 | #default:number of data points selected to fit |
---|
591 | self.Npts_fit.SetValue(str(len(self.data.data))) |
---|
592 | self.btEditMask.Enable() |
---|
593 | self.EditMask_title.Enable() |
---|
594 | self.Npts_total.SetEditable(False) |
---|
595 | self.Npts_total.SetBackgroundColour(\ |
---|
596 | self.GetParent().GetBackgroundColour()) |
---|
597 | |
---|
598 | self.Npts_total.Bind(wx.EVT_MOUSE_EVENTS, self._npts_click) |
---|
599 | #self.Npts_total.Disable() |
---|
600 | self.dataSource.SetValue(data_name) |
---|
601 | self.qmin_x = data_min |
---|
602 | self.qmax_x = data_max |
---|
603 | #self.minimum_q.SetValue(str(data_min)) |
---|
604 | #self.maximum_q.SetValue(str(data_max)) |
---|
605 | self.qmin.SetValue(str(data_min)) |
---|
606 | self.qmax.SetValue(str(data_max)) |
---|
607 | self.qmin.SetBackgroundColour("white") |
---|
608 | self.qmax.SetBackgroundColour("white") |
---|
609 | self.state.data = data |
---|
610 | self.state.qmin = self.qmin_x |
---|
611 | self.state.qmax = self.qmax_x |
---|
612 | |
---|
613 | #update model plot with new data information |
---|
614 | if flag: |
---|
615 | #set model view button |
---|
616 | if self.data.__class__.__name__ == "Data2D": |
---|
617 | self.enable2D = True |
---|
618 | self.model_view.SetLabel("2D Mode") |
---|
619 | else: |
---|
620 | self.enable2D = False |
---|
621 | self.model_view.SetLabel("1D Mode") |
---|
622 | |
---|
623 | self.model_view.Disable() |
---|
624 | |
---|
625 | #replace data plot on combo box selection |
---|
626 | #by removing the previous selected data |
---|
627 | wx.PostEvent(self._manager.parent, NewPlotEvent(action="remove", |
---|
628 | group_id=group_id, id=id)) |
---|
629 | #plot the current selected data |
---|
630 | wx.PostEvent(self._manager.parent, NewPlotEvent(plot=self.data, |
---|
631 | title=str(self.data.title))) |
---|
632 | self._manager.store_data(uid=self.uid, data=data, |
---|
633 | data_list=self.data_list, |
---|
634 | caption=self.window_name) |
---|
635 | self._draw_model() |
---|
636 | |
---|
637 | |
---|
638 | |
---|
639 | class BGTextCtrl(wx.TextCtrl): |
---|
640 | """ |
---|
641 | Text control used to display outputs. |
---|
642 | No editing allowed. The background is |
---|
643 | grayed out. User can't select text. |
---|
644 | """ |
---|
645 | def __init__(self, *args, **kwds): |
---|
646 | wx.TextCtrl.__init__(self, *args, **kwds) |
---|
647 | self.SetEditable(False) |
---|
648 | self.SetBackgroundColour(self.GetParent().parent.GetBackgroundColour()) |
---|
649 | |
---|
650 | # Bind to mouse event to avoid text highlighting |
---|
651 | # The event will be skipped once the call-back |
---|
652 | # is called. |
---|
653 | self.Bind(wx.EVT_MOUSE_EVENTS, self._click) |
---|
654 | |
---|
655 | def _click(self, event): |
---|
656 | """ |
---|
657 | Prevent further handling of the mouse event |
---|
658 | by not calling Skip(). |
---|
659 | """ |
---|
660 | pass |
---|
661 | |
---|