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.utils import format_number,check_float |
---|
14 | |
---|
15 | |
---|
16 | |
---|
17 | ## event to know the selected fit engine |
---|
18 | (FitterTypeEvent, EVT_FITTER_TYPE) = wx.lib.newevent.NewEvent() |
---|
19 | (FitStopEvent, EVT_FIT_STOP) = wx.lib.newevent.NewEvent() |
---|
20 | (Chi2UpdateEvent, EVT_CHI2_UPDATE) = wx.lib.newevent.NewEvent() |
---|
21 | _BOX_WIDTH = 76 |
---|
22 | _DATA_BOX_WIDTH = 300 |
---|
23 | SMEAR_SIZE_L = 0.005 |
---|
24 | SMEAR_SIZE_H = 0.006 |
---|
25 | |
---|
26 | import basepage |
---|
27 | from basepage import BasicPage |
---|
28 | from basepage import PageInfoEvent |
---|
29 | from DataLoader.qsmearing import smear_selection |
---|
30 | |
---|
31 | class FitPage(BasicPage): |
---|
32 | """ |
---|
33 | FitPanel class contains fields allowing to display results when |
---|
34 | fitting a model and one data |
---|
35 | |
---|
36 | :note: For Fit to be performed the user should check at least one parameter |
---|
37 | on fit Panel window. |
---|
38 | """ |
---|
39 | |
---|
40 | def __init__(self,parent, page_info): |
---|
41 | """ |
---|
42 | Initialization of the Panel |
---|
43 | """ |
---|
44 | BasicPage.__init__(self, parent, page_info) |
---|
45 | |
---|
46 | ## draw sizer |
---|
47 | self._fill_datainfo_sizer() |
---|
48 | # get smear info from data |
---|
49 | self._get_smear_info() |
---|
50 | self._fill_model_sizer( self.sizer1) |
---|
51 | self._get_defult_custom_smear() |
---|
52 | self._fill_range_sizer() |
---|
53 | |
---|
54 | if self.data is None: |
---|
55 | self.formfactorbox.Disable() |
---|
56 | self.structurebox.Disable() |
---|
57 | else: |
---|
58 | self.smearer = smear_selection(self.data, self.model) |
---|
59 | if self.smearer ==None: |
---|
60 | self.enable_smearer.Disable() |
---|
61 | ## to update the panel according to the fit engine type selected |
---|
62 | self.Bind(EVT_FITTER_TYPE,self._on_engine_change) |
---|
63 | self.Bind(EVT_FIT_STOP,self._on_fit_complete) |
---|
64 | self.Bind(EVT_CHI2_UPDATE, self.on_complete_chisqr) |
---|
65 | |
---|
66 | def _on_fit_complete(self, event): |
---|
67 | """ |
---|
68 | When fit is complete ,reset the fit button label. |
---|
69 | """ |
---|
70 | #self.btFit.SetLabel("Fit") |
---|
71 | #self.btFit.Unbind(event=wx.EVT_BUTTON, id=self.btFit.GetId()) |
---|
72 | #self.btFit.Bind(event=wx.EVT_BUTTON, handler=self._onFit,id=self.btFit.GetId()) |
---|
73 | pass |
---|
74 | |
---|
75 | def _is_2D(self): |
---|
76 | """ |
---|
77 | Check if data_name is Data2D |
---|
78 | |
---|
79 | :return: True or False |
---|
80 | |
---|
81 | """ |
---|
82 | |
---|
83 | if self.data.__class__.__name__ =="Data2D": |
---|
84 | return True |
---|
85 | return False |
---|
86 | |
---|
87 | def _on_engine_change(self, event): |
---|
88 | """ |
---|
89 | get an event containing the current name of the fit engine type |
---|
90 | |
---|
91 | :param event: FitterTypeEvent containing the name of the current engine |
---|
92 | |
---|
93 | """ |
---|
94 | self.engine_type = event.type |
---|
95 | self.state.engine_type = self.engine_type |
---|
96 | if len(self.parameters)==0: |
---|
97 | self.Layout() |
---|
98 | return |
---|
99 | if event.type =="park": |
---|
100 | self.btFit.SetLabel("Fit") |
---|
101 | |
---|
102 | for item in self.parameters: |
---|
103 | if event.type =="scipy" : |
---|
104 | item[5].SetValue("") |
---|
105 | item[5].Hide() |
---|
106 | item[6].SetValue("") |
---|
107 | item[6].Hide() |
---|
108 | self.text2_min.Hide() |
---|
109 | self.text2_max.Hide() |
---|
110 | |
---|
111 | else: |
---|
112 | item[5].Show(True) |
---|
113 | item[6].Show(True) |
---|
114 | self.text2_min.Show(True) |
---|
115 | self.text2_max.Show(True) |
---|
116 | for item in self.fittable_param: |
---|
117 | if item[5]!=None and item[6]!=None and not item in self.orientation_params_disp: |
---|
118 | if event.type =="scipy" and not item in self.orientation_params: |
---|
119 | item[5].SetValue("") |
---|
120 | item[5].Hide() |
---|
121 | item[6].SetValue("") |
---|
122 | item[6].Hide() |
---|
123 | self.text2_min.Hide() |
---|
124 | self.text2_max.Hide() |
---|
125 | self.text_disp_min.Hide() |
---|
126 | self.text_disp_max.Hide() |
---|
127 | else: |
---|
128 | item[5].Show(True) |
---|
129 | item[6].Show(True) |
---|
130 | self.text2_min.Show(True) |
---|
131 | self.text2_max.Show(True) |
---|
132 | self.text_disp_min.Show(True) |
---|
133 | self.text_disp_max.Show(True) |
---|
134 | |
---|
135 | for item in self.orientation_params: |
---|
136 | if item[5]!=None and item[6]!=None: |
---|
137 | if event.type =="scipy" or self.data.__class__.__name__ !="Data2D": |
---|
138 | item[5].SetValue("") |
---|
139 | item[5].Hide() |
---|
140 | item[6].SetValue("") |
---|
141 | item[6].Hide() |
---|
142 | else: |
---|
143 | item[5].Show(True) |
---|
144 | item[6].Show(True) |
---|
145 | |
---|
146 | for item in self.orientation_params_disp: |
---|
147 | if item[5]!=None and item[6]!=None: |
---|
148 | if event.type =="scipy" or self.data.__class__.__name__ !="Data2D": |
---|
149 | item[5].SetValue("") |
---|
150 | item[5].Hide() |
---|
151 | item[6].SetValue("") |
---|
152 | item[6].Hide() |
---|
153 | else: |
---|
154 | item[5].Show(True) |
---|
155 | item[6].Show(True) |
---|
156 | self.Layout() |
---|
157 | self.Refresh() |
---|
158 | |
---|
159 | def _fill_range_sizer(self): |
---|
160 | """ |
---|
161 | Fill the sizer containing the plotting range |
---|
162 | add access to npts |
---|
163 | """ |
---|
164 | is_2Ddata = False |
---|
165 | |
---|
166 | # Check if data is 2D |
---|
167 | if self.data.__class__.__name__ == 'Data2D': |
---|
168 | is_2Ddata = True |
---|
169 | |
---|
170 | title = "Fitting" |
---|
171 | #smear messages & titles |
---|
172 | smear_message_none = "No smearing is selected..." |
---|
173 | smear_message_dqdata = "The dQ data is being used for smearing..." |
---|
174 | smear_message_2d = "Higher accuracy is very time-expensive. Use it with care..." |
---|
175 | smear_message_new_ssmear = "Please enter only the value of interest to customize smearing..." |
---|
176 | smear_message_new_psmear = "Please enter both; the dQ will be generated by interpolation..." |
---|
177 | smear_message_2d_x_title = "<dQx>[1/A]:" |
---|
178 | smear_message_2d_y_title = "<dQy>[1/A]:" |
---|
179 | smear_message_pinhole_min_title = "dQ_low[1/A]:" |
---|
180 | smear_message_pinhole_max_title = "dQ_high[1/A]:" |
---|
181 | smear_message_slit_height_title = "Slit height[1/A]:" |
---|
182 | smear_message_slit_width_title = "Slit width[1/A]:" |
---|
183 | |
---|
184 | self._get_smear_info() |
---|
185 | |
---|
186 | #Sizers |
---|
187 | box_description_range = wx.StaticBox(self, -1,str(title)) |
---|
188 | boxsizer_range = wx.StaticBoxSizer(box_description_range, wx.VERTICAL) |
---|
189 | self.sizer_set_smearer = wx.BoxSizer(wx.VERTICAL) |
---|
190 | sizer_smearer = wx.BoxSizer(wx.HORIZONTAL) |
---|
191 | self.sizer_new_smear= wx.BoxSizer(wx.HORIZONTAL) |
---|
192 | self.sizer_set_masking = wx.BoxSizer(wx.HORIZONTAL) |
---|
193 | sizer_chi2 = wx.BoxSizer(wx.VERTICAL) |
---|
194 | smear_set_box= wx.StaticBox(self, -1,'Set Instrumental Smearing') |
---|
195 | sizer_smearer_box = wx.StaticBoxSizer(smear_set_box, wx.HORIZONTAL) |
---|
196 | sizer_smearer_box.SetMinSize((_DATA_BOX_WIDTH,85)) |
---|
197 | sizer_fit = wx.GridSizer(2, 4,2,6) |
---|
198 | |
---|
199 | # combobox for smear2d accuracy selection |
---|
200 | self.smear_accuracy = wx.ComboBox(self, -1,size=(50,-1),style=wx.CB_READONLY) |
---|
201 | self._set_accuracy_list() |
---|
202 | self.smear_accuracy.SetValue(self.smear2d_accuracy) |
---|
203 | self.smear_accuracy.SetSelection(0) |
---|
204 | self.smear_accuracy.SetToolTipString("'Higher' uses more Gaussian points for smearing computation.") |
---|
205 | |
---|
206 | wx.EVT_COMBOBOX(self.smear_accuracy,-1, self._on_select_accuracy) |
---|
207 | |
---|
208 | #Fit button |
---|
209 | self.btFit = wx.Button(self,wx.NewId(),'Fit', size=(88,25)) |
---|
210 | self.btFit.Bind(wx.EVT_BUTTON, self._onFit,id= self.btFit.GetId()) |
---|
211 | self.btFit.SetToolTipString("Start fitting.") |
---|
212 | |
---|
213 | #textcntrl for custom resolution |
---|
214 | self.smear_pinhole_max = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH-25,20),style=wx.TE_PROCESS_ENTER, |
---|
215 | text_enter_callback = self.onPinholeSmear) |
---|
216 | self.smear_pinhole_min = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH-25,20),style=wx.TE_PROCESS_ENTER, |
---|
217 | text_enter_callback = self.onPinholeSmear) |
---|
218 | self.smear_slit_height= self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH-25,20),style=wx.TE_PROCESS_ENTER, |
---|
219 | text_enter_callback = self.onSlitSmear) |
---|
220 | self.smear_slit_width = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH-25,20),style=wx.TE_PROCESS_ENTER, |
---|
221 | text_enter_callback = self.onSlitSmear) |
---|
222 | |
---|
223 | ## smear |
---|
224 | self.smear_data_left= BGTextCtrl(self, -1, size=(_BOX_WIDTH-25,20), style=0) |
---|
225 | self.smear_data_left.SetValue(str(self.dq_l)) |
---|
226 | self.smear_data_right = BGTextCtrl(self, -1, size=(_BOX_WIDTH-25,20), style=0) |
---|
227 | self.smear_data_right.SetValue(str(self.dq_r)) |
---|
228 | |
---|
229 | #set default values for smear |
---|
230 | self.smear_pinhole_max.SetValue(str(self.dx_max)) |
---|
231 | self.smear_pinhole_min.SetValue(str(self.dx_min)) |
---|
232 | self.smear_slit_height.SetValue(str(self.dxl)) |
---|
233 | self.smear_slit_width.SetValue(str(self.dxw)) |
---|
234 | |
---|
235 | #Filling the sizer containing instruments smearing info. |
---|
236 | self.disable_smearer = wx.RadioButton(self, -1, 'None', style=wx.RB_GROUP) |
---|
237 | self.enable_smearer = wx.RadioButton(self, -1, 'Use dQ Data') |
---|
238 | #self.enable_smearer.SetToolTipString("Click to use the loaded dQ data for smearing.") |
---|
239 | self.pinhole_smearer = wx.RadioButton(self, -1, 'Custom Pinhole Smear') |
---|
240 | #self.pinhole_smearer.SetToolTipString("Click to input custom resolution for pinhole smearing.") |
---|
241 | self.slit_smearer = wx.RadioButton(self, -1, 'Custom Slit Smear') |
---|
242 | #self.slit_smearer.SetToolTipString("Click to input custom resolution for slit smearing.") |
---|
243 | self.Bind(wx.EVT_RADIOBUTTON, self.onSmear, id=self.disable_smearer.GetId()) |
---|
244 | self.Bind(wx.EVT_RADIOBUTTON, self.onSmear, id=self.enable_smearer.GetId()) |
---|
245 | self.Bind(wx.EVT_RADIOBUTTON, self.onPinholeSmear, id=self.pinhole_smearer.GetId()) |
---|
246 | self.Bind(wx.EVT_RADIOBUTTON, self.onSlitSmear, id=self.slit_smearer.GetId()) |
---|
247 | self.disable_smearer.SetValue(True) |
---|
248 | |
---|
249 | # add 4 types of smearing to the sizer |
---|
250 | sizer_smearer.Add( self.disable_smearer,0, wx.LEFT, 10) |
---|
251 | sizer_smearer.Add((10,10)) |
---|
252 | sizer_smearer.Add( self.enable_smearer) |
---|
253 | sizer_smearer.Add((10,10)) |
---|
254 | sizer_smearer.Add( self.pinhole_smearer ) |
---|
255 | sizer_smearer.Add((10,10)) |
---|
256 | sizer_smearer.Add( self.slit_smearer ) |
---|
257 | sizer_smearer.Add((10,10)) |
---|
258 | |
---|
259 | # StaticText for chi2, N(for fitting), Npts |
---|
260 | self.tcChi = BGTextCtrl(self, -1, "-", size=(75,20), style=0) |
---|
261 | self.tcChi.SetToolTipString("Chi2/Npts") |
---|
262 | self.Npts_fit = BGTextCtrl(self, -1, "-", size=(75,20), style=0) |
---|
263 | self.Npts_fit.SetToolTipString(" Npts : number of points selected for fitting") |
---|
264 | self.Npts_total = BGTextCtrl(self, -1, "-", size=(75,20), style=0) |
---|
265 | self.Npts_total.SetToolTipString(" Total Npts : total number of data points") |
---|
266 | box_description_1= wx.StaticText(self, -1,' Chi2/Npts') |
---|
267 | box_description_2= wx.StaticText(self, -1,' Npts') |
---|
268 | box_description_3= wx.StaticText(self, -1,' Total Npts') |
---|
269 | box_description_4= wx.StaticText(self, -1,' ') |
---|
270 | |
---|
271 | |
---|
272 | sizer_fit.Add(box_description_1,0,0) |
---|
273 | sizer_fit.Add(box_description_2,0,0) |
---|
274 | sizer_fit.Add(box_description_3,0,0) |
---|
275 | sizer_fit.Add(box_description_4,0,0) |
---|
276 | sizer_fit.Add(self.tcChi,0,0) |
---|
277 | sizer_fit.Add(self.Npts_fit ,0,0) |
---|
278 | sizer_fit.Add(self.Npts_total,0,0) |
---|
279 | sizer_fit.Add(self.btFit,0,0) |
---|
280 | |
---|
281 | # StaticText for smear |
---|
282 | #self.tcChi = wx.StaticText(self, -1, "-", style=wx.ALIGN_LEFT) |
---|
283 | self.smear_description_none = wx.StaticText(self, -1, smear_message_none , style=wx.ALIGN_LEFT) |
---|
284 | self.smear_description_dqdata = wx.StaticText(self, -1, smear_message_dqdata , style=wx.ALIGN_LEFT) |
---|
285 | self.smear_description_type = wx.StaticText(self, -1, "Type:" , style=wx.ALIGN_LEFT) |
---|
286 | self.smear_description_accuracy_type = wx.StaticText(self, -1, "Accuracy:" , style=wx.ALIGN_LEFT) |
---|
287 | self.smear_description_smear_type = BGTextCtrl(self, -1, size=(57,20), style=0) |
---|
288 | self.smear_description_smear_type.SetValue(str(self.dq_l)) |
---|
289 | self.SetBackgroundColour(self.GetParent().GetBackgroundColour()) |
---|
290 | self.smear_description_2d = wx.StaticText(self, -1, smear_message_2d , style=wx.ALIGN_LEFT) |
---|
291 | self.smear_message_new_s = wx.StaticText(self, -1, smear_message_new_ssmear , style=wx.ALIGN_LEFT) |
---|
292 | self.smear_message_new_p = wx.StaticText(self, -1, smear_message_new_psmear , style=wx.ALIGN_LEFT) |
---|
293 | self.smear_description_2d_x = wx.StaticText(self, -1, smear_message_2d_x_title , style=wx.ALIGN_LEFT) |
---|
294 | self.smear_description_2d_y = wx.StaticText(self, -1, smear_message_2d_y_title , style=wx.ALIGN_LEFT) |
---|
295 | self.smear_description_pin_min = wx.StaticText(self, -1, smear_message_pinhole_min_title , style=wx.ALIGN_LEFT) |
---|
296 | self.smear_description_pin_max = wx.StaticText(self, -1, smear_message_pinhole_max_title , style=wx.ALIGN_LEFT) |
---|
297 | self.smear_description_slit_height = wx.StaticText(self, -1, smear_message_slit_height_title , style=wx.ALIGN_LEFT) |
---|
298 | self.smear_description_slit_width = wx.StaticText(self, -1, smear_message_slit_width_title , style=wx.ALIGN_LEFT) |
---|
299 | |
---|
300 | #arrange sizers |
---|
301 | #boxsizer1.Add( self.tcChi ) |
---|
302 | self.sizer_set_smearer.Add(sizer_smearer ) |
---|
303 | self.sizer_set_smearer.Add((10,10)) |
---|
304 | self.sizer_set_smearer.Add( self.smear_description_none,0, wx.CENTER, 10 ) |
---|
305 | self.sizer_set_smearer.Add( self.smear_description_dqdata,0, wx.CENTER, 10 ) |
---|
306 | self.sizer_set_smearer.Add( self.smear_description_2d,0, wx.CENTER, 10 ) |
---|
307 | self.sizer_new_smear.Add( self.smear_description_type,0, wx.CENTER, 10 ) |
---|
308 | self.sizer_new_smear.Add( self.smear_description_accuracy_type,0, wx.CENTER, 10 ) |
---|
309 | self.sizer_new_smear.Add( self.smear_accuracy ) |
---|
310 | self.sizer_new_smear.Add( self.smear_description_smear_type,0, wx.CENTER, 10 ) |
---|
311 | self.sizer_new_smear.Add((15,-1)) |
---|
312 | self.sizer_new_smear.Add( self.smear_description_2d_x,0, wx.CENTER, 10 ) |
---|
313 | self.sizer_new_smear.Add( self.smear_description_pin_min,0, wx.CENTER, 10 ) |
---|
314 | self.sizer_new_smear.Add( self.smear_description_slit_height,0, wx.CENTER, 10 ) |
---|
315 | |
---|
316 | self.sizer_new_smear.Add( self.smear_pinhole_min,0, wx.CENTER, 10 ) |
---|
317 | self.sizer_new_smear.Add( self.smear_slit_height,0, wx.CENTER, 10 ) |
---|
318 | self.sizer_new_smear.Add( self.smear_data_left,0, wx.CENTER, 10 ) |
---|
319 | self.sizer_new_smear.Add((20,-1)) |
---|
320 | self.sizer_new_smear.Add( self.smear_description_2d_y,0, wx.CENTER, 10 ) |
---|
321 | self.sizer_new_smear.Add( self.smear_description_pin_max,0, wx.CENTER, 10 ) |
---|
322 | self.sizer_new_smear.Add( self.smear_description_slit_width,0, wx.CENTER, 10 ) |
---|
323 | |
---|
324 | self.sizer_new_smear.Add( self.smear_pinhole_max,0, wx.CENTER, 10 ) |
---|
325 | self.sizer_new_smear.Add( self.smear_slit_width,0, wx.CENTER, 10 ) |
---|
326 | self.sizer_new_smear.Add( self.smear_data_right,0, wx.CENTER, 10 ) |
---|
327 | |
---|
328 | self.sizer_set_smearer.Add( self.smear_message_new_s,0, wx.CENTER, 10) |
---|
329 | self.sizer_set_smearer.Add( self.smear_message_new_p,0, wx.CENTER, 10) |
---|
330 | self.sizer_set_smearer.Add((5,2)) |
---|
331 | self.sizer_set_smearer.Add( self.sizer_new_smear,0, wx.CENTER, 10 ) |
---|
332 | |
---|
333 | # add all to chi2 sizer |
---|
334 | sizer_smearer_box.Add(self.sizer_set_smearer) |
---|
335 | sizer_chi2.Add(sizer_smearer_box) |
---|
336 | sizer_chi2.Add((-1,5)) |
---|
337 | |
---|
338 | # hide all smear messages and textctrl |
---|
339 | self._hide_all_smear_info() |
---|
340 | |
---|
341 | # get smear_selection |
---|
342 | self.current_smearer= smear_selection( self.data, self.model ) |
---|
343 | |
---|
344 | # Show only the relevant smear messages, etc |
---|
345 | if self.current_smearer == None: |
---|
346 | if not is_2Ddata: |
---|
347 | self.smear_description_none.Show(True) |
---|
348 | self.enable_smearer.Disable() |
---|
349 | else: |
---|
350 | self.smear_description_none.Show(True) |
---|
351 | #self.smear_description_2d.Show(True) |
---|
352 | #self.pinhole_smearer.Disable() |
---|
353 | self.slit_smearer.Disable() |
---|
354 | #self.enable_smearer.Disable() |
---|
355 | else: self._show_smear_sizer() |
---|
356 | boxsizer_range.Add(self.sizer_set_masking) |
---|
357 | |
---|
358 | #Set sizer for Fitting section |
---|
359 | self._set_range_sizer( title=title,box_sizer=boxsizer_range, object1=sizer_chi2, object= sizer_fit) |
---|
360 | |
---|
361 | def _fill_datainfo_sizer(self): |
---|
362 | """ |
---|
363 | fill sizer 0 with data info |
---|
364 | """ |
---|
365 | ## no loaded data , don't fill the sizer |
---|
366 | if self.data is None: |
---|
367 | data_min = "" |
---|
368 | data_max = "" |
---|
369 | data_name = "" |
---|
370 | else: |
---|
371 | data_name = self.data.name |
---|
372 | #set maximum range for x in linear scale |
---|
373 | if not hasattr(self.data,"data"): #Display only for 1D data fit |
---|
374 | # Minimum value of data |
---|
375 | data_min = min(self.data.x) |
---|
376 | # Maximum value of data |
---|
377 | data_max = max(self.data.x) |
---|
378 | else: |
---|
379 | ## Minimum value of data |
---|
380 | data_min = 0 |
---|
381 | x = max(math.fabs(self.data.xmin), math.fabs(self.data.xmax)) |
---|
382 | y = max(math.fabs(self.data.ymin), math.fabs(self.data.ymax)) |
---|
383 | ## Maximum value of data |
---|
384 | data_max = math.sqrt(x*x + y*y) |
---|
385 | |
---|
386 | box_description= wx.StaticBox(self, -1, 'Data') |
---|
387 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
388 | #---------------------------------------------------------- |
---|
389 | sizer_data = wx.BoxSizer(wx.HORIZONTAL) |
---|
390 | #Filling the sizer containing data related fields |
---|
391 | #self.dataSource = wx.StaticText(self, -1,str(self.data.name)) |
---|
392 | self.dataSource = BGTextCtrl(self, -1) |
---|
393 | self.dataSource.SetValue(str(data_name)) |
---|
394 | #self.dataSource.SetEditable(False) |
---|
395 | self.dataSource.SetMinSize((_DATA_BOX_WIDTH, -1)) |
---|
396 | sizer_data.Add(wx.StaticText(self, -1, 'Source Name : ')) |
---|
397 | sizer_data.Add(self.dataSource ) |
---|
398 | sizer_data.Add( (0,5) ) |
---|
399 | |
---|
400 | #---------sizer 2 draw-------------------------------- |
---|
401 | text4_3 = wx.StaticText(self, -1, 'Total Q Range (1/A)', |
---|
402 | style=wx.ALIGN_LEFT) |
---|
403 | sizer_range = wx.BoxSizer(wx.HORIZONTAL) |
---|
404 | sizer_range.Add( text4_3 ,0, wx.RIGHT, 10) |
---|
405 | self.minimum_q = BGTextCtrl(self, -1, size=(_BOX_WIDTH,20)) |
---|
406 | self.minimum_q.SetValue(str(data_min)) |
---|
407 | #self.minimum_q.SetEditable(False) |
---|
408 | self.maximum_q = BGTextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
409 | self.maximum_q.SetValue(str(data_max)) |
---|
410 | #self.maximum_q.SetEditable(False) |
---|
411 | sizer_range.Add(wx.StaticText(self, -1, "Min: "),0, wx.LEFT, 10) |
---|
412 | sizer_range.Add(self.minimum_q,0, wx.LEFT, 10) |
---|
413 | sizer_range.Add(wx.StaticText(self, -1, "Max: "),0, wx.LEFT, 10) |
---|
414 | sizer_range.Add(self.maximum_q,0, wx.LEFT, 10) |
---|
415 | |
---|
416 | ## set q range to plot |
---|
417 | self.qmin_x = data_min |
---|
418 | self.qmax_x = data_max |
---|
419 | |
---|
420 | boxsizer1.Add(sizer_data,0, wx.ALL, 10) |
---|
421 | boxsizer1.Add(sizer_range, 0 , wx.LEFT, 10) |
---|
422 | #------------------------------------------------------------ |
---|
423 | self.sizer0.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
424 | self.sizer0.Layout() |
---|
425 | |
---|
426 | def _fill_model_sizer(self, sizer): |
---|
427 | """ |
---|
428 | fill sizer containing model info |
---|
429 | """ |
---|
430 | ##Add model function Details button in fitpanel. |
---|
431 | ##The following 3 lines are for Mac. Let JHC know before modifying... |
---|
432 | title = "Model" |
---|
433 | box_description= wx.StaticBox(self, -1,str(title)) |
---|
434 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
435 | |
---|
436 | id = wx.NewId() |
---|
437 | self.model_help =wx.Button(self,id,'Details', size=(80,23)) |
---|
438 | self.model_help.Bind(wx.EVT_BUTTON, self.on_model_help_clicked,id=id) |
---|
439 | self.model_help.SetToolTipString("Model Function Help") |
---|
440 | |
---|
441 | ## class base method to add view 2d button |
---|
442 | self._set_model_sizer(sizer=sizer, box_sizer=boxsizer1, |
---|
443 | title="Model",object=self.model_help ) |
---|
444 | |
---|
445 | def _set_sizer_dispersion(self, dispersity): |
---|
446 | """ |
---|
447 | draw sizer with gaussian dispersity parameters |
---|
448 | """ |
---|
449 | self.fittable_param=[] |
---|
450 | self.fixed_param=[] |
---|
451 | self.orientation_params_disp=[] |
---|
452 | |
---|
453 | self.sizer4_4.Clear(True) |
---|
454 | if self.model==None: |
---|
455 | ##no model is selected |
---|
456 | return |
---|
457 | if not self.enable_disp.GetValue(): |
---|
458 | ## the user didn't select dispersity display |
---|
459 | return |
---|
460 | |
---|
461 | self._reset_dispersity() |
---|
462 | |
---|
463 | # Create the dispersion objects |
---|
464 | for item in self.model.dispersion.keys(): |
---|
465 | #disp_model = GaussianDispersion() |
---|
466 | disp_model = dispersity() |
---|
467 | self._disp_obj_dict[item] = disp_model |
---|
468 | self.model.set_dispersion(item, disp_model) |
---|
469 | self.state._disp_obj_dict[item]= disp_model |
---|
470 | |
---|
471 | |
---|
472 | ix=0 |
---|
473 | iy=1 |
---|
474 | disp = wx.StaticText(self, -1, ' ') |
---|
475 | self.sizer4_4.Add(disp,( iy, ix),(1,1), |
---|
476 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
477 | ix += 1 |
---|
478 | values = wx.StaticText(self, -1, 'Sigma [A]') |
---|
479 | values.SetToolTipString(\ |
---|
480 | "Sigma(STD) in the A unit; the standard deviation from the mean value.") |
---|
481 | |
---|
482 | self.sizer4_4.Add(values,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, |
---|
483 | 0) |
---|
484 | ix +=2 |
---|
485 | self.text_disp_1 = wx.StaticText(self, -1, '') |
---|
486 | self.sizer4_4.Add( self.text_disp_1,(iy, ix),(1,1),\ |
---|
487 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
488 | self.text_disp_1.Hide() |
---|
489 | |
---|
490 | |
---|
491 | ix +=1 |
---|
492 | self.text_disp_min = wx.StaticText(self, -1, 'Min') |
---|
493 | self.sizer4_4.Add(self.text_disp_min,(iy, ix),(1,1),\ |
---|
494 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
495 | self.text_disp_min.Hide() |
---|
496 | ix +=1 |
---|
497 | self.text_disp_max = wx.StaticText(self, -1, 'Max') |
---|
498 | self.sizer4_4.Add(self.text_disp_max,(iy, ix),(1,1),\ |
---|
499 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
500 | self.text_disp_max.Hide() |
---|
501 | |
---|
502 | |
---|
503 | ix += 1 |
---|
504 | npts = wx.StaticText(self, -1, 'Npts') |
---|
505 | npts.SetToolTipString("Number of sampling points for the numerical\n\ |
---|
506 | integration over the distribution function.") |
---|
507 | self.sizer4_4.Add(npts,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
508 | ix += 1 |
---|
509 | nsigmas = wx.StaticText(self, -1, 'Nsigmas') |
---|
510 | nsigmas.SetToolTipString(" Number of sigmas between which the range\n\ |
---|
511 | of the distribution function will be used for weighting. \n\ |
---|
512 | The value '3' covers 99.5% for Gaussian distribution \n\ |
---|
513 | function.") |
---|
514 | self.sizer4_4.Add(nsigmas,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, |
---|
515 | 0) |
---|
516 | |
---|
517 | if self.engine_type=="park": |
---|
518 | self.text_disp_max.Show(True) |
---|
519 | self.text_disp_min.Show(True) |
---|
520 | |
---|
521 | for item in self.model.dispersion.keys(): |
---|
522 | if not item in self.model.orientation_params: |
---|
523 | if not self.disp_cb_dict.has_key(item): |
---|
524 | self.disp_cb_dict[item]= None |
---|
525 | name0="Distribution of " + item |
---|
526 | name1=item+".width" |
---|
527 | name2=item+".npts" |
---|
528 | name3=item+".nsigmas" |
---|
529 | if not self.model.details.has_key(name1): |
---|
530 | self.model.details [name1] = ["",None,None] |
---|
531 | |
---|
532 | iy += 1 |
---|
533 | for p in self.model.dispersion[item].keys(): |
---|
534 | |
---|
535 | if p=="width": |
---|
536 | ix = 0 |
---|
537 | cb = wx.CheckBox(self, -1, name0, (10, 10)) |
---|
538 | cb.SetToolTipString("Check for fitting") |
---|
539 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
540 | self.sizer4_4.Add( cb,( iy, ix),(1,1), |
---|
541 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, |
---|
542 | 15) |
---|
543 | ix = 1 |
---|
544 | value= self.model.getParam(name1) |
---|
545 | ctl1 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH,20) |
---|
546 | ,style=wx.TE_PROCESS_ENTER) |
---|
547 | ctl1.SetToolTipString("Absolute Sigma: \n\ |
---|
548 | 1) It is the STD (ratio*mean) of '%s' distribution.\n \ |
---|
549 | 2) It should not exceed Mean/(2*Nsigmas)." %item) |
---|
550 | ctl1.SetValue(str (format_number(value))) |
---|
551 | self.sizer4_4.Add(ctl1, (iy,ix),(1,1),wx.EXPAND) |
---|
552 | ## text to show error sign |
---|
553 | ix = 2 |
---|
554 | text2=wx.StaticText(self, -1, '+/-') |
---|
555 | self.sizer4_4.Add(text2,(iy, ix),(1,1), |
---|
556 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
557 | text2.Hide() |
---|
558 | |
---|
559 | ix = 3 |
---|
560 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
561 | style=0) |
---|
562 | |
---|
563 | self.sizer4_4.Add(ctl2, (iy,ix),(1,1), |
---|
564 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
565 | |
---|
566 | ctl2.Hide() |
---|
567 | |
---|
568 | ix = 4 |
---|
569 | ctl3 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2, |
---|
570 | 20), style=wx.TE_PROCESS_ENTER, |
---|
571 | text_enter_callback = self._onparamRangeEnter) |
---|
572 | |
---|
573 | self.sizer4_4.Add(ctl3, (iy,ix),(1,1), |
---|
574 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
575 | ctl3.Hide() |
---|
576 | |
---|
577 | ix = 5 |
---|
578 | ctl4 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2, |
---|
579 | 20), style=wx.TE_PROCESS_ENTER, |
---|
580 | text_enter_callback = self._onparamRangeEnter) |
---|
581 | |
---|
582 | self.sizer4_4.Add(ctl4, (iy,ix),(1,1), |
---|
583 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
584 | |
---|
585 | ctl4.Hide() |
---|
586 | |
---|
587 | if self.engine_type=="park": |
---|
588 | ctl3.Show(True) |
---|
589 | ctl4.Show(True) |
---|
590 | |
---|
591 | self.fittable_param.append([cb,name1,ctl1,text2, |
---|
592 | ctl2, ctl3, ctl4,None]) |
---|
593 | |
---|
594 | elif p=="npts": |
---|
595 | ix = 6 |
---|
596 | value= self.model.getParam(name2) |
---|
597 | Tctl = self.ModelTextCtrl(self, -1, |
---|
598 | size=(_BOX_WIDTH/2,20), |
---|
599 | style=wx.TE_PROCESS_ENTER) |
---|
600 | |
---|
601 | Tctl.SetValue(str (format_number(value))) |
---|
602 | self.sizer4_4.Add(Tctl, (iy,ix),(1,1), |
---|
603 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
604 | self.fixed_param.append([None,name2, Tctl,None,None, |
---|
605 | None, None,None]) |
---|
606 | elif p=="nsigmas": |
---|
607 | ix = 7 |
---|
608 | value= self.model.getParam(name3) |
---|
609 | Tct2 = self.ModelTextCtrl(self, -1, |
---|
610 | size=(_BOX_WIDTH/2,20), |
---|
611 | style=wx.TE_PROCESS_ENTER) |
---|
612 | |
---|
613 | Tct2.SetValue(str (format_number(value))) |
---|
614 | self.sizer4_4.Add(Tct2, (iy,ix),(1,1), |
---|
615 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
616 | ix +=1 |
---|
617 | self.sizer4_4.Add((20,20), (iy,ix),(1,1), |
---|
618 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
619 | |
---|
620 | self.fixed_param.append([None,name3, Tct2 |
---|
621 | ,None,None,None, |
---|
622 | None,None]) |
---|
623 | |
---|
624 | ix =0 |
---|
625 | iy +=1 |
---|
626 | self.sizer4_4.Add((20,20),(iy,ix),(1,1), |
---|
627 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
628 | first_orient = True |
---|
629 | for item in self.model.dispersion.keys(): |
---|
630 | if item in self.model.orientation_params: |
---|
631 | if not self.disp_cb_dict.has_key(item): |
---|
632 | self.disp_cb_dict[item]= None |
---|
633 | name0="Distribution of " + item |
---|
634 | name1=item+".width" |
---|
635 | name2=item+".npts" |
---|
636 | name3=item+".nsigmas" |
---|
637 | |
---|
638 | if not self.model.details.has_key(name1): |
---|
639 | self.model.details [name1] = ["",None,None] |
---|
640 | |
---|
641 | |
---|
642 | iy += 1 |
---|
643 | for p in self.model.dispersion[item].keys(): |
---|
644 | |
---|
645 | if p=="width": |
---|
646 | ix = 0 |
---|
647 | cb = wx.CheckBox(self, -1, name0, (10, 10)) |
---|
648 | cb.SetToolTipString("Check for fitting") |
---|
649 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
650 | self.sizer4_4.Add( cb,( iy, ix),(1,1), |
---|
651 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, |
---|
652 | 15) |
---|
653 | if self.data.__class__.__name__ =="Data2D": |
---|
654 | cb.Show(True) |
---|
655 | elif cb.IsShown(): |
---|
656 | cb.Hide() |
---|
657 | ix = 1 |
---|
658 | value= self.model.getParam(name1) |
---|
659 | ctl1 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH, |
---|
660 | 20),style=wx.TE_PROCESS_ENTER) |
---|
661 | ctl1.SetToolTipString("Absolute Sigma: \n\ |
---|
662 | 1) It is the STD (ratio*mean) of '%s' distribution."% \ |
---|
663 | item) |
---|
664 | ctl1.SetValue(str (format_number(value))) |
---|
665 | if self.data.__class__.__name__ =="Data2D": |
---|
666 | if first_orient: |
---|
667 | values.SetLabel('Sigma [A (or deg)]') |
---|
668 | values.SetToolTipString(\ |
---|
669 | "Sigma(STD) in the A or deg(for angles) unit;\n\ |
---|
670 | the standard deviation from the mean value.") |
---|
671 | first_orient = False |
---|
672 | ctl1.Show(True) |
---|
673 | elif ctl1.IsShown(): |
---|
674 | ctl1.Hide() |
---|
675 | |
---|
676 | self.sizer4_4.Add(ctl1, (iy,ix),(1,1),wx.EXPAND) |
---|
677 | ## text to show error sign |
---|
678 | ix = 2 |
---|
679 | text2=wx.StaticText(self, -1, '+/-') |
---|
680 | self.sizer4_4.Add(text2,(iy, ix),(1,1), |
---|
681 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
682 | text2.Hide() |
---|
683 | |
---|
684 | ix = 3 |
---|
685 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
686 | style=0) |
---|
687 | |
---|
688 | self.sizer4_4.Add(ctl2, (iy,ix),(1,1), |
---|
689 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
690 | ctl2.Hide() |
---|
691 | |
---|
692 | ix = 4 |
---|
693 | ctl3 = self.ModelTextCtrl(self, -1, |
---|
694 | size=(_BOX_WIDTH/2,20), |
---|
695 | style=wx.TE_PROCESS_ENTER, |
---|
696 | text_enter_callback = self._onparamRangeEnter) |
---|
697 | |
---|
698 | self.sizer4_4.Add(ctl3, (iy,ix),(1,1), |
---|
699 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
700 | |
---|
701 | ctl3.Hide() |
---|
702 | |
---|
703 | ix = 5 |
---|
704 | ctl4 = self.ModelTextCtrl(self, -1, |
---|
705 | size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER, |
---|
706 | text_enter_callback = self._onparamRangeEnter) |
---|
707 | self.sizer4_4.Add(ctl4, (iy,ix),(1,1), |
---|
708 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
709 | ctl4.Hide() |
---|
710 | #if self.data.__class__.__name__ =="Data2D": |
---|
711 | #ctl4.Enable(True) |
---|
712 | #elif ctl4.Shown(): |
---|
713 | #ctl4.Hide() |
---|
714 | |
---|
715 | if self.engine_type=="park" and \ |
---|
716 | self.data.__class__.__name__ =="Data2D": |
---|
717 | ctl3.Show(True) |
---|
718 | ctl4.Show(True) |
---|
719 | |
---|
720 | |
---|
721 | |
---|
722 | |
---|
723 | self.fittable_param.append([cb,name1,ctl1,text2, |
---|
724 | ctl2, ctl3, ctl4,None]) |
---|
725 | self.orientation_params_disp.append([cb,name1,ctl1, |
---|
726 | text2, ctl2, ctl3, ctl4,None]) |
---|
727 | elif p=="npts": |
---|
728 | ix = 6 |
---|
729 | value= self.model.getParam(name2) |
---|
730 | Tctl = self.ModelTextCtrl(self, -1, |
---|
731 | size=(_BOX_WIDTH/2,20), |
---|
732 | style=wx.TE_PROCESS_ENTER) |
---|
733 | |
---|
734 | Tctl.SetValue(str (format_number(value))) |
---|
735 | if self.data.__class__.__name__ =="Data2D": |
---|
736 | Tctl.Show(True) |
---|
737 | else: |
---|
738 | Tctl.Hide() |
---|
739 | self.sizer4_4.Add(Tctl, (iy,ix),(1,1), |
---|
740 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
741 | self.fixed_param.append([None,name2, Tctl,None,None, |
---|
742 | None, None,None]) |
---|
743 | self.orientation_params_disp.append([None,name2, |
---|
744 | Tctl,None,None, |
---|
745 | None, None,None]) |
---|
746 | elif p=="nsigmas": |
---|
747 | ix = 7 |
---|
748 | value= self.model.getParam(name3) |
---|
749 | Tct2 = self.ModelTextCtrl(self, -1, |
---|
750 | size=(_BOX_WIDTH/2,20), |
---|
751 | style=wx.TE_PROCESS_ENTER) |
---|
752 | |
---|
753 | Tct2.SetValue(str (format_number(value))) |
---|
754 | if self.data.__class__.__name__ =="Data2D": |
---|
755 | Tct2.Show(True) |
---|
756 | else: |
---|
757 | Tct2.Hide() |
---|
758 | self.sizer4_4.Add(Tct2, (iy,ix),(1,1), |
---|
759 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
760 | ix +=1 |
---|
761 | |
---|
762 | self.fixed_param.append([None,name3, Tct2 |
---|
763 | ,None,None, None, None,None]) |
---|
764 | |
---|
765 | self.orientation_params_disp.append([None,name3, |
---|
766 | Tct2 ,None,None, None, None,None]) |
---|
767 | |
---|
768 | self.state.disp_cb_dict = copy.deepcopy(self.disp_cb_dict) |
---|
769 | |
---|
770 | self.state.model = self.model.clone() |
---|
771 | ## save state into |
---|
772 | self.state.cb1 = self.cb1.GetValue() |
---|
773 | self._copy_parameters_state(self.parameters, self.state.parameters) |
---|
774 | self._copy_parameters_state(self.orientation_params_disp, |
---|
775 | self.state.orientation_params_disp) |
---|
776 | self._copy_parameters_state(self.fittable_param, |
---|
777 | self.state.fittable_param) |
---|
778 | self._copy_parameters_state(self.fixed_param, self.state.fixed_param) |
---|
779 | |
---|
780 | |
---|
781 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
782 | " Selected Distribution: Gaussian")) |
---|
783 | #Fill the list of fittable parameters |
---|
784 | self.select_all_param(event=None) |
---|
785 | |
---|
786 | self.Layout() |
---|
787 | |
---|
788 | def _onFit(self, event): |
---|
789 | """ |
---|
790 | Allow to fit |
---|
791 | """ |
---|
792 | #make sure all parameter values are updated. |
---|
793 | if self.check_invalid_panel(): |
---|
794 | return |
---|
795 | if self.model ==None: |
---|
796 | msg="Please select a Model first..." |
---|
797 | wx.MessageBox(msg, 'Info') |
---|
798 | wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
799 | "Fit: %s"%msg)) |
---|
800 | return |
---|
801 | |
---|
802 | flag = self._update_paramv_on_fit() |
---|
803 | |
---|
804 | if not flag: |
---|
805 | msg= "Fitting range or parameters are invalid" |
---|
806 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg )) |
---|
807 | return |
---|
808 | |
---|
809 | if len(self.param_toFit) <= 0: |
---|
810 | msg= "Select at least one parameter to fit" |
---|
811 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg )) |
---|
812 | return |
---|
813 | |
---|
814 | self.select_param(event =None) |
---|
815 | |
---|
816 | #Clear errors if exist from previous fitting |
---|
817 | #self._clear_Err_on_Fit() |
---|
818 | |
---|
819 | # Remove or do not allow fitting on the Q=0 point, especially |
---|
820 | # when y(q=0)=None at x[0]. |
---|
821 | self.qmin_x = float(self.qmin.GetValue()) |
---|
822 | self.qmax_x = float( self.qmax.GetValue()) |
---|
823 | self._manager._reset_schedule_problem( value=0) |
---|
824 | self._manager.schedule_for_fit( value=1,page=self,fitproblem =None) |
---|
825 | self._manager.set_fit_range(page= self,qmin= self.qmin_x, |
---|
826 | qmax= self.qmax_x) |
---|
827 | |
---|
828 | #single fit |
---|
829 | self._manager.onFit() |
---|
830 | ## allow stopping the fit |
---|
831 | #if self.engine_type=="scipy": |
---|
832 | # self.btFit.SetLabel("Stop") |
---|
833 | # self.btFit.Unbind(event=wx.EVT_BUTTON, id= self.btFit.GetId()) |
---|
834 | # self.btFit.Bind(event= wx.EVT_BUTTON, |
---|
835 | # handler=self._StopFit, id=self.btFit.GetId()) |
---|
836 | #else: |
---|
837 | # self.btFit.SetLabel("Fit") |
---|
838 | # self.btFit.Bind(event= wx.EVT_BUTTON, |
---|
839 | # handler=self._onFit, id=self.btFit.GetId()) |
---|
840 | |
---|
841 | def _StopFit(self, event): |
---|
842 | """ |
---|
843 | Stop fit |
---|
844 | """ |
---|
845 | self.btFit.SetLabel("Fit") |
---|
846 | if self.engine_type=="scipy": |
---|
847 | self._manager.stop_fit() |
---|
848 | self.btFit.Unbind(event=wx.EVT_BUTTON, id=self.btFit.GetId()) |
---|
849 | self.btFit.Bind(event=wx.EVT_BUTTON, handler=self._onFit, |
---|
850 | id=self.btFit.GetId()) |
---|
851 | |
---|
852 | |
---|
853 | def _on_select_model(self, event=None): |
---|
854 | """ |
---|
855 | call back for model selection |
---|
856 | """ |
---|
857 | self._on_select_model_helper() |
---|
858 | self.set_model_param_sizer(self.model) |
---|
859 | |
---|
860 | self.enable_disp.SetValue(False) |
---|
861 | self.disable_disp.SetValue(True) |
---|
862 | try: |
---|
863 | self.set_dispers_sizer() |
---|
864 | except: |
---|
865 | pass |
---|
866 | self.btFit.SetFocus() |
---|
867 | self.state.enable_disp = self.enable_disp.GetValue() |
---|
868 | self.state.disable_disp = self.disable_disp.GetValue() |
---|
869 | self.state.pinhole_smearer = self.pinhole_smearer.GetValue() |
---|
870 | self.state.slit_smearer = self.slit_smearer.GetValue() |
---|
871 | |
---|
872 | self.state.structurecombobox = self.structurebox.GetCurrentSelection() |
---|
873 | self.state.formfactorcombobox = self.formfactorbox.GetCurrentSelection() |
---|
874 | |
---|
875 | if self.model != None: |
---|
876 | # Reset smearer, model and data |
---|
877 | self.set_data(self.data) |
---|
878 | # update smearer sizer |
---|
879 | self.onSmear(None) |
---|
880 | try: |
---|
881 | temp_smear = None |
---|
882 | if self.enable_smearer.GetValue(): |
---|
883 | # Set the smearer environments |
---|
884 | temp_smear= self.smearer |
---|
885 | #self.compute_chisqr(temp_smear) |
---|
886 | except: |
---|
887 | ## error occured on chisqr computation |
---|
888 | pass |
---|
889 | ## event to post model to fit to fitting plugins |
---|
890 | (ModelEventbox, EVT_MODEL_BOX) = wx.lib.newevent.NewEvent() |
---|
891 | if self.data is not None and \ |
---|
892 | self.data.__class__.__name__ !="Data2D": |
---|
893 | ## set smearing value whether or not |
---|
894 | # the data contain the smearing info |
---|
895 | evt = ModelEventbox(model = self.model, |
---|
896 | smearer = temp_smear, |
---|
897 | qmin = float(self.qmin_x), |
---|
898 | qmax = float(self.qmax_x)) |
---|
899 | else: |
---|
900 | evt = ModelEventbox(model = self.model) |
---|
901 | |
---|
902 | self._manager._on_model_panel(evt = evt) |
---|
903 | self.state.model = self.model.clone() |
---|
904 | self.state.model.name = self.model.name |
---|
905 | if event is not None: |
---|
906 | self._draw_model() |
---|
907 | if event !=None: |
---|
908 | #self._undo.Enable(True) |
---|
909 | ## post state to fit panel |
---|
910 | event = PageInfoEvent(page = self) |
---|
911 | wx.PostEvent(self.parent, event) |
---|
912 | |
---|
913 | |
---|
914 | def _onparamEnter(self,event): |
---|
915 | """ |
---|
916 | when enter value on panel redraw model according to changed |
---|
917 | """ |
---|
918 | if self.model ==None: |
---|
919 | msg="Please select a Model first..." |
---|
920 | wx.MessageBox(msg, 'Info') |
---|
921 | #wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
922 | # "Parameters: %s"%msg)) |
---|
923 | return |
---|
924 | |
---|
925 | #default flag |
---|
926 | flag = False |
---|
927 | self.fitrange = True |
---|
928 | #get event object |
---|
929 | tcrtl= event.GetEventObject() |
---|
930 | wx.PostEvent(self._manager.parent, StatusEvent(status=" \ |
---|
931 | updating ... ",type="update")) |
---|
932 | #Clear msg if previously shown. |
---|
933 | msg= "" |
---|
934 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
935 | |
---|
936 | if check_float(tcrtl): |
---|
937 | flag = self._onparamEnter_helper() |
---|
938 | self.set_npts2fit() |
---|
939 | if self.fitrange: |
---|
940 | temp_smearer = None |
---|
941 | if not self.disable_smearer.GetValue(): |
---|
942 | temp_smearer= self.current_smearer |
---|
943 | ## set smearing value whether or not |
---|
944 | # the data contain the smearing info |
---|
945 | if self.slit_smearer.GetValue(): |
---|
946 | flag1 = self.update_slit_smear() |
---|
947 | flag = flag or flag1 |
---|
948 | elif self.pinhole_smearer.GetValue(): |
---|
949 | flag1 = self.update_pinhole_smear() |
---|
950 | flag = flag or flag1 |
---|
951 | elif self.data.__class__.__name__ !="Data2D": |
---|
952 | self._manager.set_smearer(smearer=temp_smearer, |
---|
953 | qmin= float(self.qmin_x), |
---|
954 | qmax= float(self.qmax_x)) |
---|
955 | if flag: |
---|
956 | #self.compute_chisqr(smearer= temp_smearer) |
---|
957 | |
---|
958 | ## new state posted |
---|
959 | if self.state_change: |
---|
960 | #self._undo.Enable(True) |
---|
961 | event = PageInfoEvent(page = self) |
---|
962 | wx.PostEvent(self.parent, event) |
---|
963 | self.state_change= False |
---|
964 | else: |
---|
965 | # invalid fit range: do nothing here: |
---|
966 | # msg already displayed in validate |
---|
967 | return |
---|
968 | else: |
---|
969 | self.save_current_state() |
---|
970 | msg= "Cannot Plot :Must enter a number!!! " |
---|
971 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
972 | |
---|
973 | self.save_current_state() |
---|
974 | return |
---|
975 | |
---|
976 | def _onparamRangeEnter(self, event): |
---|
977 | """ |
---|
978 | Check validity of value enter in the parameters range field |
---|
979 | """ |
---|
980 | if self.check_invalid_panel(): |
---|
981 | return |
---|
982 | tcrtl= event.GetEventObject() |
---|
983 | #Clear msg if previously shown. |
---|
984 | msg= "" |
---|
985 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
986 | # Flag to register when a parameter has changed. |
---|
987 | is_modified = False |
---|
988 | if tcrtl.GetValue().lstrip().rstrip()!="": |
---|
989 | try: |
---|
990 | value = float(tcrtl.GetValue()) |
---|
991 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
992 | self._check_value_enter(self.fittable_param ,is_modified) |
---|
993 | self._check_value_enter(self.parameters ,is_modified) |
---|
994 | except: |
---|
995 | tcrtl.SetBackgroundColour("pink") |
---|
996 | msg= "Model Error:wrong value entered : %s"% sys.exc_value |
---|
997 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
998 | return |
---|
999 | else: |
---|
1000 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
1001 | |
---|
1002 | #self._undo.Enable(True) |
---|
1003 | self.save_current_state() |
---|
1004 | event = PageInfoEvent(page = self) |
---|
1005 | wx.PostEvent(self.parent, event) |
---|
1006 | self.state_change= False |
---|
1007 | |
---|
1008 | |
---|
1009 | def _onQrangeEnter(self, event): |
---|
1010 | """ |
---|
1011 | Check validity of value enter in the Q range field |
---|
1012 | """ |
---|
1013 | if self.check_invalid_panel(): |
---|
1014 | return |
---|
1015 | tcrtl= event.GetEventObject() |
---|
1016 | #Clear msg if previously shown. |
---|
1017 | msg= "" |
---|
1018 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1019 | # Flag to register when a parameter has changed. |
---|
1020 | is_modified = False |
---|
1021 | if tcrtl.GetValue().lstrip().rstrip()!="": |
---|
1022 | try: |
---|
1023 | value = float(tcrtl.GetValue()) |
---|
1024 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
1025 | |
---|
1026 | # If qmin and qmax have been modified, update qmin and qmax |
---|
1027 | if self._validate_qrange( self.qmin, self.qmax): |
---|
1028 | tempmin = float(self.qmin.GetValue()) |
---|
1029 | if tempmin != self.qmin_x: |
---|
1030 | self.qmin_x = tempmin |
---|
1031 | tempmax = float(self.qmax.GetValue()) |
---|
1032 | if tempmax != self.qmax_x: |
---|
1033 | self.qmax_x = tempmax |
---|
1034 | else: |
---|
1035 | tcrtl.SetBackgroundColour("pink") |
---|
1036 | msg= "Model Error:wrong value entered : %s"% sys.exc_value |
---|
1037 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1038 | return |
---|
1039 | |
---|
1040 | except: |
---|
1041 | tcrtl.SetBackgroundColour("pink") |
---|
1042 | msg= "Model Error:wrong value entered : %s"% sys.exc_value |
---|
1043 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1044 | return |
---|
1045 | #Check if # of points for theory model are valid(>0). |
---|
1046 | # check for 2d |
---|
1047 | if self.data.__class__.__name__ =="Data2D": |
---|
1048 | # set mask |
---|
1049 | radius= numpy.sqrt( self.data.qx_data*self.data.qx_data + |
---|
1050 | self.data.qy_data*self.data.qy_data ) |
---|
1051 | index_data = ((self.qmin_x <= radius)& \ |
---|
1052 | (radius<= self.qmax_x)) |
---|
1053 | index_data = (index_data)&(self.data.mask) |
---|
1054 | index_data = (index_data)&(numpy.isfinite(self.data.data)) |
---|
1055 | if len(index_data[index_data]) < 10: |
---|
1056 | msg = "Cannot Plot :No or too little npts in" |
---|
1057 | msg += " that data range!!! " |
---|
1058 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1059 | return |
---|
1060 | else: |
---|
1061 | self.data.mask = index_data |
---|
1062 | #self.Npts_fit.SetValue(str(len(self.data.mask))) |
---|
1063 | self.set_npts2fit() |
---|
1064 | else: |
---|
1065 | index_data = ((self.qmin_x <= self.data.x)& \ |
---|
1066 | (self.data.x <= self.qmax_x)) |
---|
1067 | self.Npts_fit.SetValue(str(len(self.data.x[index_data]))) |
---|
1068 | |
---|
1069 | |
---|
1070 | else: |
---|
1071 | tcrtl.SetBackgroundColour("pink") |
---|
1072 | msg= "Model Error:wrong value entered!!!" |
---|
1073 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1074 | |
---|
1075 | self._draw_model() |
---|
1076 | ##update chi2 |
---|
1077 | #self.compute_chisqr(smearer= temp_smearer) |
---|
1078 | #self._undo.Enable(True) |
---|
1079 | self.save_current_state() |
---|
1080 | event = PageInfoEvent(page = self) |
---|
1081 | wx.PostEvent(self.parent, event) |
---|
1082 | self.state_change= False |
---|
1083 | return |
---|
1084 | |
---|
1085 | def _clear_Err_on_Fit(self): |
---|
1086 | """ |
---|
1087 | hide the error text control shown |
---|
1088 | after fitting |
---|
1089 | """ |
---|
1090 | |
---|
1091 | if hasattr(self,"text2_3"): |
---|
1092 | self.text2_3.Hide() |
---|
1093 | |
---|
1094 | if len(self.parameters)>0: |
---|
1095 | for item in self.parameters: |
---|
1096 | #Skip t ifhe angle parameters if 1D data |
---|
1097 | if self.data.__class__.__name__ !="Data2D": |
---|
1098 | if item in self.orientation_params: |
---|
1099 | continue |
---|
1100 | if item in self.param_toFit: |
---|
1101 | continue |
---|
1102 | ## hide statictext +/- |
---|
1103 | if item[3]!=None and item[3].IsShown(): |
---|
1104 | item[3].Hide() |
---|
1105 | ## hide textcrtl for error after fit |
---|
1106 | if item[4]!=None and item[4].IsShown(): |
---|
1107 | item[4].Hide() |
---|
1108 | |
---|
1109 | if len(self.fittable_param)>0: |
---|
1110 | for item in self.fittable_param: |
---|
1111 | #Skip t ifhe angle parameters if 1D data |
---|
1112 | if self.data.__class__.__name__ !="Data2D": |
---|
1113 | if item in self.orientation_params: |
---|
1114 | continue |
---|
1115 | if item in self.param_toFit: |
---|
1116 | continue |
---|
1117 | ## hide statictext +/- |
---|
1118 | if item[3]!=None and item[3].IsShown(): |
---|
1119 | item[3].Hide() |
---|
1120 | ## hide textcrtl for error after fit |
---|
1121 | if item[4]!=None and item[4].IsShown(): |
---|
1122 | item[4].Hide() |
---|
1123 | return |
---|
1124 | |
---|
1125 | def _get_defult_custom_smear(self): |
---|
1126 | """ |
---|
1127 | Get the defult values for custum smearing. |
---|
1128 | """ |
---|
1129 | # get the default values |
---|
1130 | if self.dxl == None: self.dxl = 0.0 |
---|
1131 | if self.dxw == None: self.dxw = "" |
---|
1132 | if self.dx_min == None: self.dx_min = SMEAR_SIZE_L |
---|
1133 | if self.dx_max == None: self.dx_max = SMEAR_SIZE_H |
---|
1134 | |
---|
1135 | def _get_smear_info(self): |
---|
1136 | """ |
---|
1137 | Get the smear info from data. |
---|
1138 | |
---|
1139 | :return: self.smear_type, self.dq_l and self.dq_r, |
---|
1140 | respectively the type of the smear, dq_min and |
---|
1141 | dq_max for pinhole smear data |
---|
1142 | while dxl and dxw for slit smear |
---|
1143 | |
---|
1144 | """ |
---|
1145 | |
---|
1146 | # default |
---|
1147 | self.smear_type = None |
---|
1148 | self.dq_l = None |
---|
1149 | self.dq_r = None |
---|
1150 | data = self.data |
---|
1151 | if self.data is None: |
---|
1152 | return |
---|
1153 | elif self.data.__class__.__name__ == 'Data2D': |
---|
1154 | if data.dqx_data == None or data.dqy_data ==None: |
---|
1155 | return |
---|
1156 | elif self.smearer != None and (data.dqx_data.any()!=0) and \ |
---|
1157 | (data.dqx_data.any()!=0): |
---|
1158 | self.smear_type = "Pinhole2d" |
---|
1159 | self.dq_l = format_number(numpy.average(data.dqx_data)) |
---|
1160 | self.dq_r = format_number(numpy.average(data.dqy_data)) |
---|
1161 | return |
---|
1162 | else: |
---|
1163 | return |
---|
1164 | # check if it is pinhole smear and get min max if it is. |
---|
1165 | if data.dx != None and all(data.dx !=0): |
---|
1166 | self.smear_type = "Pinhole" |
---|
1167 | self.dq_l = data.dx[0] |
---|
1168 | self.dq_r = data.dx[-1] |
---|
1169 | |
---|
1170 | # check if it is slit smear and get min max if it is. |
---|
1171 | elif data.dxl != None or data.dxw != None: |
---|
1172 | self.smear_type = "Slit" |
---|
1173 | if data.dxl != None and all(data.dxl !=0): |
---|
1174 | self.dq_l = data.dxl[0] |
---|
1175 | if data.dxw != None and all(data.dxw !=0): |
---|
1176 | self.dq_r = data.dxw[0] |
---|
1177 | #return self.smear_type,self.dq_l,self.dq_r |
---|
1178 | |
---|
1179 | def _show_smear_sizer(self): |
---|
1180 | """ |
---|
1181 | Show only the sizers depending on smear selection |
---|
1182 | """ |
---|
1183 | # smear disabled |
---|
1184 | if self.disable_smearer.GetValue(): |
---|
1185 | self.smear_description_none.Show(True) |
---|
1186 | # 2Dsmear |
---|
1187 | elif self._is_2D(): |
---|
1188 | self.smear_description_accuracy_type.Show(True) |
---|
1189 | self.smear_accuracy.Show(True) |
---|
1190 | self.smear_description_accuracy_type.Show(True) |
---|
1191 | self.smear_description_2d.Show(True) |
---|
1192 | self.smear_description_2d_x.Show(True) |
---|
1193 | self.smear_description_2d_y.Show(True) |
---|
1194 | if self.pinhole_smearer.GetValue(): |
---|
1195 | self.smear_pinhole_min.Show(True) |
---|
1196 | self.smear_pinhole_max.Show(True) |
---|
1197 | # smear from data |
---|
1198 | elif self.enable_smearer.GetValue(): |
---|
1199 | |
---|
1200 | self.smear_description_dqdata.Show(True) |
---|
1201 | if self.smear_type != None: |
---|
1202 | self.smear_description_smear_type.Show(True) |
---|
1203 | if self.smear_type == 'Slit': |
---|
1204 | self.smear_description_slit_height.Show(True) |
---|
1205 | self.smear_description_slit_width.Show(True) |
---|
1206 | elif self.smear_type == 'Pinhole': |
---|
1207 | self.smear_description_pin_min.Show(True) |
---|
1208 | self.smear_description_pin_max.Show(True) |
---|
1209 | self.smear_description_smear_type.Show(True) |
---|
1210 | self.smear_description_type.Show(True) |
---|
1211 | self.smear_data_left.Show(True) |
---|
1212 | self.smear_data_right.Show(True) |
---|
1213 | # custom pinhole smear |
---|
1214 | elif self.pinhole_smearer.GetValue(): |
---|
1215 | if self.smear_type == 'Pinhole': |
---|
1216 | self.smear_message_new_p.Show(True) |
---|
1217 | self.smear_description_pin_min.Show(True) |
---|
1218 | self.smear_description_pin_max.Show(True) |
---|
1219 | |
---|
1220 | self.smear_pinhole_min.Show(True) |
---|
1221 | self.smear_pinhole_max.Show(True) |
---|
1222 | # custom slit smear |
---|
1223 | elif self.slit_smearer.GetValue(): |
---|
1224 | self.smear_message_new_s.Show(True) |
---|
1225 | self.smear_description_slit_height.Show(True) |
---|
1226 | self.smear_slit_height.Show(True) |
---|
1227 | self.smear_description_slit_width.Show(True) |
---|
1228 | self.smear_slit_width.Show(True) |
---|
1229 | |
---|
1230 | def _hide_all_smear_info(self): |
---|
1231 | """ |
---|
1232 | Hide all smearing messages in the set_smearer sizer |
---|
1233 | """ |
---|
1234 | self.smear_description_none.Hide() |
---|
1235 | self.smear_description_dqdata.Hide() |
---|
1236 | self.smear_description_type.Hide() |
---|
1237 | self.smear_description_smear_type.Hide() |
---|
1238 | self.smear_description_accuracy_type.Hide() |
---|
1239 | self.smear_description_2d_x.Hide() |
---|
1240 | self.smear_description_2d_y.Hide() |
---|
1241 | self.smear_description_2d.Hide() |
---|
1242 | |
---|
1243 | self.smear_accuracy.Hide() |
---|
1244 | self.smear_data_left.Hide() |
---|
1245 | self.smear_data_right.Hide() |
---|
1246 | self.smear_description_pin_min.Hide() |
---|
1247 | self.smear_pinhole_min.Hide() |
---|
1248 | self.smear_description_pin_max.Hide() |
---|
1249 | self.smear_pinhole_max.Hide() |
---|
1250 | self.smear_description_slit_height.Hide() |
---|
1251 | self.smear_slit_height.Hide() |
---|
1252 | self.smear_description_slit_width.Hide() |
---|
1253 | self.smear_slit_width.Hide() |
---|
1254 | self.smear_message_new_p.Hide() |
---|
1255 | self.smear_message_new_s.Hide() |
---|
1256 | |
---|
1257 | def _set_accuracy_list(self): |
---|
1258 | """ |
---|
1259 | Set the list of an accuracy in 2D custum smear: Xhigh, High, Med, or Low |
---|
1260 | """ |
---|
1261 | # list of accuracy choices |
---|
1262 | list = ['Low','Med','High','Xhigh'] |
---|
1263 | for idx in range(len(list)): |
---|
1264 | self.smear_accuracy.Append(list[idx],idx) |
---|
1265 | |
---|
1266 | def _set_fun_box_list(self,fun_box): |
---|
1267 | """ |
---|
1268 | Set the list of func for multifunctional models |
---|
1269 | """ |
---|
1270 | # Check if it is multi_functional model |
---|
1271 | if self.model.__class__ not in self.model_list_box["Multi-Functions"]: |
---|
1272 | return None |
---|
1273 | # Get the func name list |
---|
1274 | list = self.model.fun_list |
---|
1275 | if len(list) == 0: |
---|
1276 | return None |
---|
1277 | # build function (combo)box |
---|
1278 | ind = 0 |
---|
1279 | while(ind < len(list)): |
---|
1280 | for key, val in list.iteritems(): |
---|
1281 | if (val == ind): |
---|
1282 | fun_box.Append(key,val) |
---|
1283 | break |
---|
1284 | ind += 1 |
---|
1285 | |
---|
1286 | def _on_select_accuracy(self,event): |
---|
1287 | """ |
---|
1288 | Select an accuracy in 2D custom smear: Xhigh, High, Med, or Low |
---|
1289 | """ |
---|
1290 | #event.Skip() |
---|
1291 | # Check if the accuracy is same as before |
---|
1292 | #self.smear2d_accuracy = event.GetEventObject().GetValue() |
---|
1293 | self.smear2d_accuracy = self.smear_accuracy.GetValue() |
---|
1294 | if self.pinhole_smearer.GetValue(): |
---|
1295 | self.onPinholeSmear(event=None) |
---|
1296 | else: |
---|
1297 | self.onSmear(event=None) |
---|
1298 | if self.current_smearer != None: |
---|
1299 | self.current_smearer.set_accuracy(accuracy = |
---|
1300 | self.smear2d_accuracy) |
---|
1301 | event.Skip() |
---|
1302 | |
---|
1303 | def _on_fun_box(self,event): |
---|
1304 | """ |
---|
1305 | Select an func: Erf,Rparabola,LParabola |
---|
1306 | """ |
---|
1307 | fun_val = None |
---|
1308 | fun_box = event.GetEventObject() |
---|
1309 | name = fun_box.Name |
---|
1310 | value = fun_box.GetValue() |
---|
1311 | if self.model.fun_list.has_key(value): |
---|
1312 | fun_val = self.model.fun_list[value] |
---|
1313 | |
---|
1314 | self.model.setParam(name,fun_val) |
---|
1315 | # save state |
---|
1316 | self._copy_parameters_state(self.str_parameters, |
---|
1317 | self.state.str_parameters) |
---|
1318 | # update params |
---|
1319 | self._update_paramv_on_fit() |
---|
1320 | # draw |
---|
1321 | self._draw_model() |
---|
1322 | self.Refresh() |
---|
1323 | # get ready for new event |
---|
1324 | event.Skip() |
---|
1325 | |
---|
1326 | def _onMask(self, event): |
---|
1327 | """ |
---|
1328 | Build a panel to allow to edit Mask |
---|
1329 | """ |
---|
1330 | |
---|
1331 | from sans.guiframe.local_perspectives.plotting.masking \ |
---|
1332 | import MaskPanel as MaskDialog |
---|
1333 | |
---|
1334 | self.panel = MaskDialog(self, data=self.data,id =-1 ) |
---|
1335 | self.panel.Bind(wx.EVT_CLOSE, self._draw_masked_model) |
---|
1336 | self.panel.ShowModal() |
---|
1337 | #wx.PostEvent(self.parent, event) |
---|
1338 | |
---|
1339 | def _draw_masked_model(self,event): |
---|
1340 | """ |
---|
1341 | Draw model image w/mask |
---|
1342 | """ |
---|
1343 | event.Skip() |
---|
1344 | |
---|
1345 | is_valid_qrange = self._update_paramv_on_fit() |
---|
1346 | |
---|
1347 | if is_valid_qrange: |
---|
1348 | # try re draw the model plot if it exists |
---|
1349 | self._draw_model() |
---|
1350 | self.panel.Destroy() # frame |
---|
1351 | self.set_npts2fit() |
---|
1352 | elif self.model == None: |
---|
1353 | self.panel.Destroy() |
---|
1354 | self.set_npts2fit() |
---|
1355 | msg= "No model is found on updating MASK in the model plot... " |
---|
1356 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1357 | else: |
---|
1358 | msg = ' Please consider your Q range, too.' |
---|
1359 | self.panel.ShowMessage(msg) |
---|
1360 | |
---|
1361 | def set_data(self, data): |
---|
1362 | """ |
---|
1363 | reset the current data |
---|
1364 | """ |
---|
1365 | self.data = data |
---|
1366 | if self.data is None: |
---|
1367 | data_min = "" |
---|
1368 | data_max = "" |
---|
1369 | data_name = "" |
---|
1370 | self.formfactorbox.Disable() |
---|
1371 | self.structurebox.Disable() |
---|
1372 | else: |
---|
1373 | self.smearer = smear_selection(self.data, self.model) |
---|
1374 | self.disable_smearer.SetValue(True) |
---|
1375 | if self.smearer == None: |
---|
1376 | self.enable_smearer.Disable() |
---|
1377 | else: |
---|
1378 | self.enable_smearer.Enable() |
---|
1379 | |
---|
1380 | # more disables for 2D |
---|
1381 | if self.data.__class__.__name__ =="Data2D": |
---|
1382 | self.slit_smearer.Disable() |
---|
1383 | self.default_mask = copy.deepcopy(self.data.mask) |
---|
1384 | |
---|
1385 | self.formfactorbox.Enable() |
---|
1386 | self.structurebox.Enable() |
---|
1387 | data_name = self.data.name |
---|
1388 | #set maximum range for x in linear scale |
---|
1389 | if not hasattr(self.data,"data"): #Display only for 1D data fit |
---|
1390 | # Minimum value of data |
---|
1391 | data_min = min(self.data.x) |
---|
1392 | # Maximum value of data |
---|
1393 | data_max = max(self.data.x) |
---|
1394 | #number of total data points |
---|
1395 | self.Npts_total.SetValue(str(len(self.data.x))) |
---|
1396 | #default:number of data points selected to fit |
---|
1397 | self.Npts_fit.SetValue(str(len(self.data.x))) |
---|
1398 | self.btEditMask.Disable() |
---|
1399 | self.EditMask_title.Disable() |
---|
1400 | else: |
---|
1401 | ## Minimum value of data |
---|
1402 | data_min = 0 |
---|
1403 | x = max(math.fabs(self.data.xmin), math.fabs(self.data.xmax)) |
---|
1404 | y = max(math.fabs(self.data.ymin), math.fabs(self.data.ymax)) |
---|
1405 | ## Maximum value of data |
---|
1406 | data_max = math.sqrt(x*x + y*y) |
---|
1407 | #number of total data points |
---|
1408 | self.Npts_total.SetValue(str(len(self.data.data))) |
---|
1409 | #default:number of data points selected to fit |
---|
1410 | self.Npts_fit.SetValue(str(len(self.data.data))) |
---|
1411 | self.btEditMask.Enable() |
---|
1412 | self.EditMask_title.Enable() |
---|
1413 | self.dataSource.SetValue(data_name) |
---|
1414 | self.qmin_x = data_min |
---|
1415 | self.qmax_x = data_max |
---|
1416 | self.minimum_q.SetValue(str(data_min)) |
---|
1417 | self.maximum_q.SetValue(str(data_max)) |
---|
1418 | self.qmin.SetValue(str(data_min)) |
---|
1419 | self.qmax.SetValue(str(data_max)) |
---|
1420 | self.qmin.SetBackgroundColour("white") |
---|
1421 | self.qmax.SetBackgroundColour("white") |
---|
1422 | self.state.data = data |
---|
1423 | self.state.qmin = self.qmin_x |
---|
1424 | self.state.qmax = self.qmax_x |
---|
1425 | |
---|
1426 | def reset_page(self, state,first=False): |
---|
1427 | """ |
---|
1428 | reset the state |
---|
1429 | """ |
---|
1430 | self.reset_page_helper(state) |
---|
1431 | #import sans.guiframe.gui_manager |
---|
1432 | #evt = ModelEventbox(model=state.model) |
---|
1433 | #wx.PostEvent(self.event_owner, evt) |
---|
1434 | |
---|
1435 | if self.engine_type != None: |
---|
1436 | self._manager._on_change_engine(engine=self.engine_type) |
---|
1437 | |
---|
1438 | self.select_param(event = None) |
---|
1439 | #Save state_fit |
---|
1440 | self.save_current_state_fit() |
---|
1441 | self._lay_out() |
---|
1442 | self.Refresh() |
---|
1443 | |
---|
1444 | def get_range(self): |
---|
1445 | """ |
---|
1446 | return the fitting range |
---|
1447 | """ |
---|
1448 | return float(self.qmin_x) , float(self.qmax_x) |
---|
1449 | |
---|
1450 | def get_npts2fit(self): |
---|
1451 | """ |
---|
1452 | return numbers of data points within qrange |
---|
1453 | |
---|
1454 | :Note: This is for Park where chi2 is not normalized by Npts of fit |
---|
1455 | |
---|
1456 | """ |
---|
1457 | npts2fit = 0 |
---|
1458 | qmin,qmax = self.get_range() |
---|
1459 | if self.data.__class__.__name__ =="Data2D": |
---|
1460 | radius= numpy.sqrt( self.data.qx_data*self.data.qx_data + |
---|
1461 | self.data.qy_data*self.data.qy_data ) |
---|
1462 | index_data = (self.qmin_x <= radius)&(radius<= self.qmax_x) |
---|
1463 | index_data= (index_data)&(self.data.mask) |
---|
1464 | index_data = (index_data)&(numpy.isfinite(self.data.data)) |
---|
1465 | npts2fit = len(self.data.data[index_data]) |
---|
1466 | else: |
---|
1467 | for qx in self.data.x: |
---|
1468 | if qx >= qmin and qx <= qmax: |
---|
1469 | npts2fit += 1 |
---|
1470 | return npts2fit |
---|
1471 | |
---|
1472 | def set_npts2fit(self): |
---|
1473 | """ |
---|
1474 | setValue Npts for fitting |
---|
1475 | """ |
---|
1476 | self.Npts_fit.SetValue(str(self.get_npts2fit())) |
---|
1477 | |
---|
1478 | def get_chi2(self): |
---|
1479 | """ |
---|
1480 | return the current chi2 |
---|
1481 | """ |
---|
1482 | return self.tcChi.GetValue() |
---|
1483 | |
---|
1484 | def get_param_list(self): |
---|
1485 | """ |
---|
1486 | :return self.param_toFit: list containing references to TextCtrl |
---|
1487 | checked.Theses TextCtrl will allow reference to parameters to fit. |
---|
1488 | |
---|
1489 | :raise: if return an empty list of parameter fit will nnote work |
---|
1490 | properly so raise ValueError,"missing parameter to fit" |
---|
1491 | """ |
---|
1492 | if self.param_toFit !=[]: |
---|
1493 | return self.param_toFit |
---|
1494 | else: |
---|
1495 | raise ValueError,"missing parameter to fit" |
---|
1496 | |
---|
1497 | def onsetValues(self,chisqr,p_name, out,cov): |
---|
1498 | """ |
---|
1499 | Build the panel from the fit result |
---|
1500 | |
---|
1501 | :param chisqr: Value of the goodness of fit metric |
---|
1502 | :param p_name: the name of parameters |
---|
1503 | :param out: list of parameter with the best value found during fitting |
---|
1504 | :param cov: Covariance matrix |
---|
1505 | |
---|
1506 | """ |
---|
1507 | if out == None or not numpy.isfinite(chisqr): |
---|
1508 | raise ValueError,"Fit error occured..." |
---|
1509 | |
---|
1510 | is_modified = False |
---|
1511 | has_error = False |
---|
1512 | |
---|
1513 | #Hide textctrl boxes of errors. |
---|
1514 | self._clear_Err_on_Fit() |
---|
1515 | |
---|
1516 | #Check if chi2 is finite |
---|
1517 | if chisqr != None or numpy.isfinite(chisqr): |
---|
1518 | #format chi2 |
---|
1519 | if self.engine_type == "park": |
---|
1520 | npt_fit = float(self.get_npts2fit()) |
---|
1521 | if npt_fit > 0: |
---|
1522 | chisqr =chisqr/npt_fit |
---|
1523 | chi2 = format_number(chisqr) |
---|
1524 | self.tcChi.SetValue(chi2) |
---|
1525 | self.tcChi.Refresh() |
---|
1526 | else: |
---|
1527 | self.tcChi.SetValue("-") |
---|
1528 | |
---|
1529 | #Hide error title |
---|
1530 | if self.text2_3.IsShown(): |
---|
1531 | self.text2_3.Hide() |
---|
1532 | |
---|
1533 | try: |
---|
1534 | n = self.disp_box.GetCurrentSelection() |
---|
1535 | dispersity= self.disp_box.GetClientData(n) |
---|
1536 | if dispersity !=None and self.enable_disp.GetValue(): |
---|
1537 | name= dispersity.__name__ |
---|
1538 | if name == "GaussianDispersion": |
---|
1539 | if hasattr(self,"text_disp_1" ): |
---|
1540 | if self.text_disp_1 !=None: |
---|
1541 | self.text_disp_1.Hide() |
---|
1542 | except: |
---|
1543 | dispersty = None |
---|
1544 | pass |
---|
1545 | #set the panel when fit result are float not list |
---|
1546 | if out.__class__== numpy.float64: |
---|
1547 | self.param_toFit[0][2].SetValue(format_number(out)) |
---|
1548 | |
---|
1549 | if self.param_toFit[0][4].IsShown: |
---|
1550 | self.param_toFit[0][4].Hide() |
---|
1551 | if cov !=None : |
---|
1552 | self.text2_3.Show(True) |
---|
1553 | try: |
---|
1554 | if dispersity !=None: |
---|
1555 | name= dispersity.__name__ |
---|
1556 | if name == "GaussianDispersion" and \ |
---|
1557 | self.enable_disp.GetValue(): |
---|
1558 | if hasattr(self,"text_disp_1" ): |
---|
1559 | if self.text_disp_1 !=None: |
---|
1560 | self.text_disp_1.Show(True) |
---|
1561 | except: |
---|
1562 | pass |
---|
1563 | |
---|
1564 | if cov[0]==None or not numpy.isfinite(cov[0]): |
---|
1565 | if self.param_toFit[0][3].IsShown: |
---|
1566 | self.param_toFit[0][3].Hide() |
---|
1567 | else: |
---|
1568 | self.param_toFit[0][3].Show(True) |
---|
1569 | self.param_toFit[0][4].Show(True) |
---|
1570 | self.param_toFit[0][4].SetValue(format_number(cov[0])) |
---|
1571 | has_error = True |
---|
1572 | else: |
---|
1573 | |
---|
1574 | i = 0 |
---|
1575 | #Set the panel when fit result are list |
---|
1576 | for item in self.param_toFit: |
---|
1577 | if len(item)>5 and item != None: |
---|
1578 | ## reset error value to initial state |
---|
1579 | item[3].Hide() |
---|
1580 | item[4].Hide() |
---|
1581 | |
---|
1582 | for ind in range(len(out)): |
---|
1583 | |
---|
1584 | if item[1] == p_name[ind]: |
---|
1585 | break |
---|
1586 | if len(out)<=len(self.param_toFit) and out[ind] !=None: |
---|
1587 | val_out = format_number(out[ind]) |
---|
1588 | item[2].SetValue(val_out) |
---|
1589 | |
---|
1590 | if(cov !=None): |
---|
1591 | |
---|
1592 | try: |
---|
1593 | if dispersity !=None: |
---|
1594 | name= dispersity.__name__ |
---|
1595 | if name == "GaussianDispersion" and \ |
---|
1596 | self.enable_disp.GetValue(): |
---|
1597 | if hasattr(self,"text_disp_1" ): |
---|
1598 | if self.text_disp_1!=None: |
---|
1599 | if not self.text_disp_1.IsShown(): |
---|
1600 | self.text_disp_1.Show(True) |
---|
1601 | except: |
---|
1602 | pass |
---|
1603 | |
---|
1604 | if cov[ind]!=None : |
---|
1605 | if numpy.isfinite(float(cov[ind])): |
---|
1606 | val_err = format_number(cov[ind]) |
---|
1607 | item[3].Show(True) |
---|
1608 | item[4].Show(True) |
---|
1609 | item[4].SetValue(val_err) |
---|
1610 | |
---|
1611 | has_error = True |
---|
1612 | i += 1 |
---|
1613 | #Show error title when any errors displayed |
---|
1614 | if has_error: |
---|
1615 | if not self.text2_3.IsShown(): |
---|
1616 | self.text2_3.Show(True) |
---|
1617 | |
---|
1618 | ## save current state |
---|
1619 | self.save_current_state() |
---|
1620 | #plot model |
---|
1621 | self._draw_model() |
---|
1622 | self._lay_out() |
---|
1623 | #PostStatusEvent |
---|
1624 | msg = "Fit completed! " |
---|
1625 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
1626 | |
---|
1627 | def onPinholeSmear(self, event): |
---|
1628 | """ |
---|
1629 | Create a custom pinhole smear object that will change the way residuals |
---|
1630 | are compute when fitting |
---|
1631 | |
---|
1632 | :Note: accuracy is given by strings'High','Med', 'Low' FOR 2d, |
---|
1633 | None for 1D |
---|
1634 | |
---|
1635 | """ |
---|
1636 | |
---|
1637 | if self.check_invalid_panel(): |
---|
1638 | return |
---|
1639 | if self.model ==None: |
---|
1640 | self.disable_smearer.SetValue(True) |
---|
1641 | msg="Please select a Model first..." |
---|
1642 | wx.MessageBox(msg, 'Info') |
---|
1643 | wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
1644 | "Smear: %s"%msg)) |
---|
1645 | return |
---|
1646 | |
---|
1647 | # Need update param values |
---|
1648 | self._update_paramv_on_fit() |
---|
1649 | |
---|
1650 | # msg default |
---|
1651 | msg = None |
---|
1652 | if event != None: |
---|
1653 | tcrtl= event.GetEventObject() |
---|
1654 | # event case of radio button |
---|
1655 | if tcrtl.GetValue()== True: |
---|
1656 | self.dx_min = 0.0 |
---|
1657 | self.dx_max = 0.0 |
---|
1658 | is_new_pinhole = True |
---|
1659 | else: |
---|
1660 | is_new_pinhole = self._is_changed_pinhole() |
---|
1661 | else: |
---|
1662 | is_new_pinhole = True |
---|
1663 | # if any value is changed |
---|
1664 | if is_new_pinhole: |
---|
1665 | msg = self._set_pinhole_smear() |
---|
1666 | # hide all silt sizer |
---|
1667 | self._hide_all_smear_info() |
---|
1668 | |
---|
1669 | ##Calculate chi2 |
---|
1670 | temp_smearer = self.current_smearer |
---|
1671 | #self.compute_chisqr(smearer= temp_smearer) |
---|
1672 | |
---|
1673 | # show relevant slit sizers |
---|
1674 | self._show_smear_sizer() |
---|
1675 | |
---|
1676 | self.sizer_set_smearer.Layout() |
---|
1677 | self.Layout() |
---|
1678 | |
---|
1679 | if event != None: |
---|
1680 | event.Skip() |
---|
1681 | #self._undo.Enable(True) |
---|
1682 | self.save_current_state() |
---|
1683 | event = PageInfoEvent(page = self) |
---|
1684 | wx.PostEvent(self.parent, event) |
---|
1685 | |
---|
1686 | def _is_changed_pinhole(self): |
---|
1687 | """ |
---|
1688 | check if any of pinhole smear is changed |
---|
1689 | |
---|
1690 | :return: True or False |
---|
1691 | |
---|
1692 | """ |
---|
1693 | # get the values |
---|
1694 | pin_min = self.smear_pinhole_min.GetValue() |
---|
1695 | pin_max = self.smear_pinhole_max.GetValue() |
---|
1696 | |
---|
1697 | # Check changes in slit width |
---|
1698 | try: |
---|
1699 | dx_min = float(pin_min) |
---|
1700 | except: |
---|
1701 | return True |
---|
1702 | if self.dx_min != dx_min: |
---|
1703 | return True |
---|
1704 | |
---|
1705 | # Check changes in slit heigth |
---|
1706 | try: |
---|
1707 | dx_max = float(pin_max) |
---|
1708 | except: |
---|
1709 | return True |
---|
1710 | if self.dx_max != dx_max: |
---|
1711 | return True |
---|
1712 | return False |
---|
1713 | |
---|
1714 | def _set_pinhole_smear(self): |
---|
1715 | """ |
---|
1716 | Set custom pinhole smear |
---|
1717 | |
---|
1718 | :return: msg |
---|
1719 | |
---|
1720 | """ |
---|
1721 | # copy data |
---|
1722 | data = copy.deepcopy(self.data) |
---|
1723 | if self._is_2D(): |
---|
1724 | self.smear_type = 'Pinhole2d' |
---|
1725 | len_data = len(data.data) |
---|
1726 | data.dqx_data = numpy.zeros(len_data) |
---|
1727 | data.dqy_data = numpy.zeros(len_data) |
---|
1728 | else: |
---|
1729 | self.smear_type = 'Pinhole' |
---|
1730 | len_data = len(data.x) |
---|
1731 | data.dx = numpy.zeros(len_data) |
---|
1732 | data.dxl = None |
---|
1733 | data.dxw = None |
---|
1734 | msg = None |
---|
1735 | |
---|
1736 | get_pin_min = self.smear_pinhole_min |
---|
1737 | get_pin_max = self.smear_pinhole_max |
---|
1738 | |
---|
1739 | if not check_float(get_pin_min): |
---|
1740 | get_pin_min.SetBackgroundColour("pink") |
---|
1741 | msg= "Model Error:wrong value entered!!!" |
---|
1742 | elif not check_float(get_pin_max ): |
---|
1743 | get_pin_max.SetBackgroundColour("pink") |
---|
1744 | msg= "Model Error:wrong value entered!!!" |
---|
1745 | else: |
---|
1746 | if len_data < 2: len_data = 2 |
---|
1747 | self.dx_min = float(get_pin_min.GetValue()) |
---|
1748 | self.dx_max = float(get_pin_max.GetValue()) |
---|
1749 | if self.dx_min < 0: |
---|
1750 | get_pin_min.SetBackgroundColour("pink") |
---|
1751 | msg= "Model Error:This value can not be negative!!!" |
---|
1752 | elif self.dx_max <0: |
---|
1753 | get_pin_max.SetBackgroundColour("pink") |
---|
1754 | msg= "Model Error:This value can not be negative!!!" |
---|
1755 | elif self.dx_min != None and self.dx_max != None: |
---|
1756 | if self._is_2D(): |
---|
1757 | data.dqx_data[data.dqx_data==0] = self.dx_min |
---|
1758 | data.dqy_data[data.dqy_data==0] = self.dx_max |
---|
1759 | elif self.dx_min == self.dx_max: |
---|
1760 | data.dx[data.dx==0] = self.dx_min |
---|
1761 | else: |
---|
1762 | step = (self.dx_max - self.dx_min)/(len_data-1) |
---|
1763 | data.dx = numpy.arange(self.dx_min,self.dx_max+step/1.1, |
---|
1764 | step) |
---|
1765 | elif self.dx_min != None: |
---|
1766 | if self._is_2D(): data.dqx_data[data.dqx_data==0] = self.dx_min |
---|
1767 | else: data.dx[data.dx==0] = self.dx_min |
---|
1768 | elif self.dx_max != None: |
---|
1769 | if self._is_2D(): data.dqy_data[data.dqy_data==0] = self.dx_max |
---|
1770 | else: data.dx[data.dx==0] = self.dx_max |
---|
1771 | self.current_smearer = smear_selection(data, self.model) |
---|
1772 | # 2D need to set accuracy |
---|
1773 | if self._is_2D(): |
---|
1774 | self.current_smearer.set_accuracy(accuracy = \ |
---|
1775 | self.smear2d_accuracy) |
---|
1776 | |
---|
1777 | if msg != None: |
---|
1778 | wx.PostEvent(self._manager.parent, StatusEvent(status = msg )) |
---|
1779 | else: |
---|
1780 | get_pin_min.SetBackgroundColour("white") |
---|
1781 | get_pin_max.SetBackgroundColour("white") |
---|
1782 | ## set smearing value whether or not the data contain the smearing info |
---|
1783 | self._manager.set_smearer(smearer=self.current_smearer, qmin= \ |
---|
1784 | float(self.qmin_x),qmax= float(self.qmax_x)) |
---|
1785 | return msg |
---|
1786 | |
---|
1787 | def update_pinhole_smear(self): |
---|
1788 | """ |
---|
1789 | called by kill_focus on pinhole TextCntrl |
---|
1790 | to update the changes |
---|
1791 | |
---|
1792 | :return: False when wrong value was entered |
---|
1793 | |
---|
1794 | """ |
---|
1795 | # msg default |
---|
1796 | msg = None |
---|
1797 | # check if any value is changed |
---|
1798 | if self._is_changed_pinhole(): |
---|
1799 | msg = self._set_pinhole_smear() |
---|
1800 | #self._undo.Enable(True) |
---|
1801 | self.save_current_state() |
---|
1802 | |
---|
1803 | if msg != None: |
---|
1804 | return False |
---|
1805 | else: |
---|
1806 | return True |
---|
1807 | |
---|
1808 | def onSlitSmear(self, event): |
---|
1809 | """ |
---|
1810 | Create a custom slit smear object that will change the way residuals |
---|
1811 | are compute when fitting |
---|
1812 | """ |
---|
1813 | |
---|
1814 | if self.check_invalid_panel(): |
---|
1815 | return |
---|
1816 | |
---|
1817 | if self.model ==None: |
---|
1818 | self.disable_smearer.SetValue(True) |
---|
1819 | |
---|
1820 | msg="Please select a Model first..." |
---|
1821 | wx.MessageBox(msg, 'Info') |
---|
1822 | wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
1823 | "Smear: %s"%msg)) |
---|
1824 | return |
---|
1825 | |
---|
1826 | # Need update param values |
---|
1827 | self._update_paramv_on_fit() |
---|
1828 | |
---|
1829 | # msg default |
---|
1830 | msg = None |
---|
1831 | # for event given |
---|
1832 | if event != None: |
---|
1833 | tcrtl= event.GetEventObject() |
---|
1834 | # event case of radio button |
---|
1835 | if tcrtl.GetValue(): |
---|
1836 | self.dxl = 0.0 |
---|
1837 | self.dxw = 0.0 |
---|
1838 | is_new_slit = True |
---|
1839 | else: |
---|
1840 | is_new_slit = self._is_changed_slit() |
---|
1841 | else: |
---|
1842 | is_new_slit = True |
---|
1843 | |
---|
1844 | # if any value is changed |
---|
1845 | if is_new_slit: |
---|
1846 | msg = self._set_slit_smear() |
---|
1847 | |
---|
1848 | # hide all silt sizer |
---|
1849 | self._hide_all_smear_info() |
---|
1850 | ##Calculate chi2 |
---|
1851 | #self.compute_chisqr(smearer= self.current_smearer) |
---|
1852 | # show relevant slit sizers |
---|
1853 | self._show_smear_sizer() |
---|
1854 | self.sizer_set_smearer.Layout() |
---|
1855 | self.Layout() |
---|
1856 | |
---|
1857 | if event != None: |
---|
1858 | event.Skip() |
---|
1859 | #self._undo.Enable(True) |
---|
1860 | self.save_current_state() |
---|
1861 | event = PageInfoEvent(page = self) |
---|
1862 | wx.PostEvent(self.parent, event) |
---|
1863 | if msg != None: |
---|
1864 | wx.PostEvent(self._manager.parent, StatusEvent(status = msg)) |
---|
1865 | |
---|
1866 | def _is_changed_slit(self): |
---|
1867 | """ |
---|
1868 | check if any of slit lengths is changed |
---|
1869 | |
---|
1870 | :return: True or False |
---|
1871 | |
---|
1872 | """ |
---|
1873 | # get the values |
---|
1874 | width = self.smear_slit_width.GetValue() |
---|
1875 | height = self.smear_slit_height.GetValue() |
---|
1876 | |
---|
1877 | # check and change the box bg color if it was pink |
---|
1878 | # but it should be white now |
---|
1879 | # because this is the case that _set_slit_smear() will not handle |
---|
1880 | if height.lstrip().rstrip()=="": |
---|
1881 | self.smear_slit_height.SetBackgroundColour(wx.WHITE) |
---|
1882 | if width.lstrip().rstrip()=="": |
---|
1883 | self.smear_slit_width.SetBackgroundColour(wx.WHITE) |
---|
1884 | |
---|
1885 | # Check changes in slit width |
---|
1886 | if width == "": |
---|
1887 | dxw = 0.0 |
---|
1888 | else: |
---|
1889 | try: |
---|
1890 | dxw = float(width) |
---|
1891 | except: |
---|
1892 | return True |
---|
1893 | if self.dxw != dxw: |
---|
1894 | return True |
---|
1895 | |
---|
1896 | # Check changes in slit heigth |
---|
1897 | if height == "": |
---|
1898 | dxl = 0.0 |
---|
1899 | else: |
---|
1900 | try: |
---|
1901 | dxl = float(height) |
---|
1902 | except: |
---|
1903 | return True |
---|
1904 | if self.dxl != dxl: |
---|
1905 | return True |
---|
1906 | |
---|
1907 | return False |
---|
1908 | |
---|
1909 | def _set_slit_smear(self): |
---|
1910 | """ |
---|
1911 | Set custom slit smear |
---|
1912 | |
---|
1913 | :return: message to inform the user about the validity |
---|
1914 | of the values entered for slit smear |
---|
1915 | """ |
---|
1916 | if self.data.__class__.__name__ == "Data2D": |
---|
1917 | return |
---|
1918 | temp_smearer = None |
---|
1919 | # make sure once more if it is smearer |
---|
1920 | data = copy.deepcopy(self.data) |
---|
1921 | data_len = len(data.x) |
---|
1922 | data.dx = None |
---|
1923 | data.dxl = None |
---|
1924 | data.dxw = None |
---|
1925 | msg = None |
---|
1926 | |
---|
1927 | try: |
---|
1928 | self.dxl = float(self.smear_slit_height.GetValue()) |
---|
1929 | data.dxl = self.dxl* numpy.ones(data_len) |
---|
1930 | self.smear_slit_height.SetBackgroundColour(wx.WHITE) |
---|
1931 | except: |
---|
1932 | data.dxl = numpy.zeros(data_len) |
---|
1933 | if self.smear_slit_height.GetValue().lstrip().rstrip()!="": |
---|
1934 | self.smear_slit_height.SetBackgroundColour("pink") |
---|
1935 | msg = "Wrong value entered... " |
---|
1936 | else: |
---|
1937 | self.smear_slit_height.SetBackgroundColour(wx.WHITE) |
---|
1938 | try: |
---|
1939 | self.dxw = float(self.smear_slit_width.GetValue()) |
---|
1940 | self.smear_slit_width.SetBackgroundColour(wx.WHITE) |
---|
1941 | data.dxw = self.dxw* numpy.ones(data_len) |
---|
1942 | except: |
---|
1943 | data.dxw = numpy.zeros(data_len) |
---|
1944 | if self.smear_slit_width.GetValue().lstrip().rstrip()!="": |
---|
1945 | self.smear_slit_width.SetBackgroundColour("pink") |
---|
1946 | msg = "Wrong Fit value entered... " |
---|
1947 | else: |
---|
1948 | self.smear_slit_width.SetBackgroundColour(wx.WHITE) |
---|
1949 | |
---|
1950 | self.current_smearer = smear_selection(data, self.model) |
---|
1951 | #temp_smearer = self.current_smearer |
---|
1952 | ## set smearing value whether or not the data contain the smearing info |
---|
1953 | self._manager.set_smearer(smearer=self.current_smearer, qmin= \ |
---|
1954 | float(self.qmin_x), qmax= float(self.qmax_x)) |
---|
1955 | return msg |
---|
1956 | |
---|
1957 | def update_slit_smear(self): |
---|
1958 | """ |
---|
1959 | called by kill_focus on pinhole TextCntrl |
---|
1960 | to update the changes |
---|
1961 | |
---|
1962 | :return: False when wrong value was entered |
---|
1963 | |
---|
1964 | """ |
---|
1965 | # msg default |
---|
1966 | msg = None |
---|
1967 | # check if any value is changed |
---|
1968 | if self._is_changed_slit(): |
---|
1969 | msg = self._set_slit_smear() |
---|
1970 | #self._undo.Enable(True) |
---|
1971 | self.save_current_state() |
---|
1972 | |
---|
1973 | if msg != None: |
---|
1974 | return False |
---|
1975 | else: |
---|
1976 | return True |
---|
1977 | |
---|
1978 | def onSmear(self, event): |
---|
1979 | """ |
---|
1980 | Create a smear object that will change the way residuals |
---|
1981 | are compute when fitting |
---|
1982 | """ |
---|
1983 | if event != None: |
---|
1984 | event.Skip() |
---|
1985 | if self.check_invalid_panel(): |
---|
1986 | return |
---|
1987 | if self.model == None: |
---|
1988 | self.disable_smearer.SetValue(True) |
---|
1989 | msg="Please select a Model first..." |
---|
1990 | wx.MessageBox(msg, 'Info') |
---|
1991 | wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
1992 | "Smear: %s"%msg)) |
---|
1993 | return |
---|
1994 | |
---|
1995 | # Need update param values |
---|
1996 | self._update_paramv_on_fit() |
---|
1997 | |
---|
1998 | temp_smearer = None |
---|
1999 | self._get_smear_info() |
---|
2000 | |
---|
2001 | #renew smear sizer |
---|
2002 | if self.smear_type != None: |
---|
2003 | self.smear_description_smear_type.SetValue(str(self.smear_type)) |
---|
2004 | self.smear_data_left.SetValue(str(self.dq_l)) |
---|
2005 | self.smear_data_right.SetValue(str(self.dq_r)) |
---|
2006 | |
---|
2007 | self._hide_all_smear_info() |
---|
2008 | |
---|
2009 | data = copy.deepcopy(self.data) |
---|
2010 | # make sure once more if it is smearer |
---|
2011 | self.current_smearer = smear_selection(data, self.model) |
---|
2012 | |
---|
2013 | if self.enable_smearer.GetValue(): |
---|
2014 | if hasattr(self.data,"dxl"): |
---|
2015 | |
---|
2016 | msg= ": Resolution smearing parameters" |
---|
2017 | if hasattr(self.data,"dxw"): |
---|
2018 | msg= ": Slit smearing parameters" |
---|
2019 | if self.smearer ==None: |
---|
2020 | wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
2021 | "Data contains no smearing information")) |
---|
2022 | else: |
---|
2023 | wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
2024 | "Data contains smearing information")) |
---|
2025 | |
---|
2026 | #self.smear_description_dqdata.Show(True) |
---|
2027 | self.smear_data_left.Show(True) |
---|
2028 | self.smear_data_right.Show(True) |
---|
2029 | temp_smearer= self.current_smearer |
---|
2030 | elif self.disable_smearer.GetValue(): |
---|
2031 | self.smear_description_none.Show(True) |
---|
2032 | |
---|
2033 | self._show_smear_sizer() |
---|
2034 | |
---|
2035 | self.sizer_set_smearer.Layout() |
---|
2036 | self.Layout() |
---|
2037 | ## set smearing value whether or not the data contain the smearing info |
---|
2038 | self._manager.set_smearer(smearer=temp_smearer, qmin= float(self.qmin_x), |
---|
2039 | qmax= float(self.qmax_x)) |
---|
2040 | |
---|
2041 | ##Calculate chi2 |
---|
2042 | #self.compute_chisqr(smearer= temp_smearer) |
---|
2043 | |
---|
2044 | self.state.enable_smearer= self.enable_smearer.GetValue() |
---|
2045 | self.state.disable_smearer=self.disable_smearer.GetValue() |
---|
2046 | self.state.pinhole_smearer = self.pinhole_smearer.GetValue() |
---|
2047 | self.state.slit_smearer = self.slit_smearer.GetValue() |
---|
2048 | |
---|
2049 | def on_complete_chisqr(self, event): |
---|
2050 | """ |
---|
2051 | print result chisqr |
---|
2052 | |
---|
2053 | :event: activated by fitting/ complete after draw |
---|
2054 | |
---|
2055 | """ |
---|
2056 | try: |
---|
2057 | if event ==None: |
---|
2058 | output= "-" |
---|
2059 | else: |
---|
2060 | output = event.output |
---|
2061 | self.tcChi.SetValue(str(format_number(output))) |
---|
2062 | |
---|
2063 | self.state.tcChi = self.tcChi.GetValue() |
---|
2064 | except: |
---|
2065 | pass |
---|
2066 | |
---|
2067 | |
---|
2068 | def select_all_param(self,event): |
---|
2069 | """ |
---|
2070 | set to true or false all checkBox given the main checkbox value cb1 |
---|
2071 | """ |
---|
2072 | self.param_toFit=[] |
---|
2073 | if self.parameters !=[]: |
---|
2074 | if self.cb1.GetValue(): |
---|
2075 | for item in self.parameters: |
---|
2076 | ## for data2D select all to fit |
---|
2077 | if self.data.__class__.__name__=="Data2D": |
---|
2078 | item[0].SetValue(True) |
---|
2079 | self.param_toFit.append(item ) |
---|
2080 | else: |
---|
2081 | ## for 1D all parameters except orientation |
---|
2082 | if not item in self.orientation_params: |
---|
2083 | item[0].SetValue(True) |
---|
2084 | self.param_toFit.append(item ) |
---|
2085 | #if len(self.fittable_param)>0: |
---|
2086 | for item in self.fittable_param: |
---|
2087 | if self.data.__class__.__name__=="Data2D": |
---|
2088 | item[0].SetValue(True) |
---|
2089 | self.param_toFit.append(item ) |
---|
2090 | else: |
---|
2091 | ## for 1D all parameters except orientation |
---|
2092 | if not item in self.orientation_params_disp: |
---|
2093 | item[0].SetValue(True) |
---|
2094 | self.param_toFit.append(item ) |
---|
2095 | else: |
---|
2096 | for item in self.parameters: |
---|
2097 | item[0].SetValue(False) |
---|
2098 | for item in self.fittable_param: |
---|
2099 | item[0].SetValue(False) |
---|
2100 | self.param_toFit=[] |
---|
2101 | |
---|
2102 | self.save_current_state_fit() |
---|
2103 | |
---|
2104 | if event !=None: |
---|
2105 | #self._undo.Enable(True) |
---|
2106 | ## post state to fit panel |
---|
2107 | event = PageInfoEvent(page = self) |
---|
2108 | wx.PostEvent(self.parent, event) |
---|
2109 | |
---|
2110 | def select_param(self,event): |
---|
2111 | """ |
---|
2112 | Select TextCtrl checked for fitting purpose and stores them |
---|
2113 | in self.param_toFit=[] list |
---|
2114 | """ |
---|
2115 | self.param_toFit=[] |
---|
2116 | for item in self.parameters: |
---|
2117 | #Skip t ifhe angle parameters if 1D data |
---|
2118 | if self.data.__class__.__name__ !="Data2D": |
---|
2119 | if item in self.orientation_params: |
---|
2120 | continue |
---|
2121 | #Select parameters to fit for list of primary parameters |
---|
2122 | if item[0].GetValue(): |
---|
2123 | if not (item in self.param_toFit): |
---|
2124 | self.param_toFit.append(item ) |
---|
2125 | else: |
---|
2126 | #remove parameters from the fitting list |
---|
2127 | if item in self.param_toFit: |
---|
2128 | self.param_toFit.remove(item) |
---|
2129 | |
---|
2130 | #Select parameters to fit for list of fittable parameters |
---|
2131 | # with dispersion |
---|
2132 | for item in self.fittable_param: |
---|
2133 | #Skip t ifhe angle parameters if 1D data |
---|
2134 | if self.data.__class__.__name__ !="Data2D": |
---|
2135 | if item in self.orientation_params: |
---|
2136 | continue |
---|
2137 | if item[0].GetValue(): |
---|
2138 | if not (item in self.param_toFit): |
---|
2139 | self.param_toFit.append(item) |
---|
2140 | else: |
---|
2141 | #remove parameters from the fitting list |
---|
2142 | if item in self.param_toFit: |
---|
2143 | self.param_toFit.remove(item) |
---|
2144 | |
---|
2145 | #Calculate num. of angle parameters |
---|
2146 | if self.data.__class__.__name__ =="Data2D": |
---|
2147 | len_orient_para = 0 |
---|
2148 | else: |
---|
2149 | len_orient_para = len(self.orientation_params) #assume even len |
---|
2150 | #Total num. of angle parameters |
---|
2151 | if len(self.fittable_param) > 0: |
---|
2152 | len_orient_para *= 2 |
---|
2153 | #Set the value of checkbox that selected every checkbox or not |
---|
2154 | if len(self.parameters)+len(self.fittable_param)-len_orient_para ==\ |
---|
2155 | len(self.param_toFit): |
---|
2156 | self.cb1.SetValue(True) |
---|
2157 | else: |
---|
2158 | self.cb1.SetValue(False) |
---|
2159 | self.save_current_state_fit() |
---|
2160 | if event !=None: |
---|
2161 | #self._undo.Enable(True) |
---|
2162 | ## post state to fit panel |
---|
2163 | event = PageInfoEvent(page = self) |
---|
2164 | wx.PostEvent(self.parent, event) |
---|
2165 | |
---|
2166 | def set_model_param_sizer(self, model): |
---|
2167 | """ |
---|
2168 | Build the panel from the model content |
---|
2169 | |
---|
2170 | :param model: the model selected in combo box for fitting purpose |
---|
2171 | |
---|
2172 | """ |
---|
2173 | self.sizer3.Clear(True) |
---|
2174 | self.parameters = [] |
---|
2175 | self.str_parameters = [] |
---|
2176 | self.param_toFit=[] |
---|
2177 | self.fittable_param=[] |
---|
2178 | self.fixed_param=[] |
---|
2179 | self.orientation_params=[] |
---|
2180 | self.orientation_params_disp=[] |
---|
2181 | |
---|
2182 | if model ==None: |
---|
2183 | self.sizer3.Layout() |
---|
2184 | self.SetupScrolling() |
---|
2185 | return |
---|
2186 | ## the panel is drawn using the current value of the fit engine |
---|
2187 | if self.engine_type==None and self._manager !=None: |
---|
2188 | self.engine_type= self._manager._return_engine_type() |
---|
2189 | |
---|
2190 | box_description= wx.StaticBox(self, -1,str("Model Parameters")) |
---|
2191 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
2192 | sizer = wx.GridBagSizer(5,5) |
---|
2193 | ## save the current model |
---|
2194 | self.model = model |
---|
2195 | |
---|
2196 | keys = self.model.getParamList() |
---|
2197 | #list of dispersion paramaters |
---|
2198 | self.disp_list=self.model.getDispParamList() |
---|
2199 | |
---|
2200 | def custom_compare(a,b): |
---|
2201 | """ |
---|
2202 | Custom compare to order, first by alphabets then second by number. |
---|
2203 | """ |
---|
2204 | # number at the last digit |
---|
2205 | a_last = a[len(a)-1] |
---|
2206 | b_last = b[len(b)-1] |
---|
2207 | # default |
---|
2208 | num_a = None |
---|
2209 | num_b = None |
---|
2210 | # split the names |
---|
2211 | a2 = a.lower().split('_') |
---|
2212 | b2 = b.lower().split('_') |
---|
2213 | # check length of a2, b2 |
---|
2214 | len_a2 = len(a2) |
---|
2215 | len_b2 = len(b2) |
---|
2216 | # check if it contains a int number(<10) |
---|
2217 | try: |
---|
2218 | num_a = int(a_last) |
---|
2219 | except: pass |
---|
2220 | try: |
---|
2221 | num_b = int(b_last) |
---|
2222 | except: pass |
---|
2223 | # Put 'scale' near the top; happens |
---|
2224 | # when numbered param name exists |
---|
2225 | if a == 'scale': |
---|
2226 | return -1 |
---|
2227 | # both have a number |
---|
2228 | if num_a != None and num_b != None: |
---|
2229 | if num_a > num_b: return -1 |
---|
2230 | # same number |
---|
2231 | elif num_a == num_b: |
---|
2232 | # different last names |
---|
2233 | if a2[len_a2-1] != b2[len_b2-1] and num_a != 0: |
---|
2234 | return -cmp(a2[len_a2-1], b2[len_b2-1]) |
---|
2235 | else: |
---|
2236 | return cmp(a, b) |
---|
2237 | else: return 1 |
---|
2238 | # one of them has a number |
---|
2239 | elif num_a != None: return 1 |
---|
2240 | elif num_b != None: return -1 |
---|
2241 | # no numbers |
---|
2242 | else: return cmp(a.lower(), b.lower()) |
---|
2243 | |
---|
2244 | |
---|
2245 | keys.sort(custom_compare) |
---|
2246 | |
---|
2247 | iy = 0 |
---|
2248 | ix = 0 |
---|
2249 | select_text = "Select All" |
---|
2250 | self.cb1 = wx.CheckBox(self, -1,str(select_text), (10, 10)) |
---|
2251 | wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.select_all_param) |
---|
2252 | self.cb1.SetToolTipString("To check/uncheck all the boxes below.") |
---|
2253 | #self.cb1.SetValue(True) |
---|
2254 | |
---|
2255 | sizer.Add(self.cb1,(iy, ix),(1,1),\ |
---|
2256 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
2257 | ix +=1 |
---|
2258 | self.text2_2 = wx.StaticText(self, -1, 'Values') |
---|
2259 | sizer.Add(self.text2_2,(iy, ix),(1,1),\ |
---|
2260 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2261 | ix +=2 |
---|
2262 | self.text2_3 = wx.StaticText(self, -1, 'Errors') |
---|
2263 | sizer.Add(self.text2_3,(iy, ix),(1,1),\ |
---|
2264 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2265 | self.text2_3.Hide() |
---|
2266 | ix +=1 |
---|
2267 | self.text2_min = wx.StaticText(self, -1, 'Min') |
---|
2268 | sizer.Add(self.text2_min,(iy, ix),(1,1),\ |
---|
2269 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2270 | self.text2_min.Hide() |
---|
2271 | ix +=1 |
---|
2272 | self.text2_max = wx.StaticText(self, -1, 'Max') |
---|
2273 | sizer.Add(self.text2_max,(iy, ix),(1,1),\ |
---|
2274 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2275 | self.text2_max.Hide() |
---|
2276 | ix += 1 |
---|
2277 | self.text2_4 = wx.StaticText(self, -1, '[Units]') |
---|
2278 | sizer.Add(self.text2_4,(iy, ix),(1,1),\ |
---|
2279 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2280 | self.text2_4.Hide() |
---|
2281 | if self.engine_type=="park": |
---|
2282 | self.text2_max.Show(True) |
---|
2283 | self.text2_min.Show(True) |
---|
2284 | |
---|
2285 | for item in keys: |
---|
2286 | if not item in self.disp_list and not item in \ |
---|
2287 | self.model.orientation_params: |
---|
2288 | |
---|
2289 | ##prepare a spot to store errors |
---|
2290 | if not self.model.details.has_key(item): |
---|
2291 | self.model.details [item] = ["",None,None] |
---|
2292 | |
---|
2293 | iy += 1 |
---|
2294 | ix = 0 |
---|
2295 | if self.model.__class__ in \ |
---|
2296 | self.model_list_box["Multi-Functions"] \ |
---|
2297 | and item in self.model.non_fittable: |
---|
2298 | non_fittable_name = wx.StaticText(self, -1, item ) |
---|
2299 | sizer.Add(non_fittable_name,(iy, ix),(1,1),\ |
---|
2300 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 21) |
---|
2301 | ## add parameter value |
---|
2302 | ix += 1 |
---|
2303 | value= self.model.getParam(item) |
---|
2304 | if len(self.model.fun_list) > 0: |
---|
2305 | num = item.split('_')[1][5:7] |
---|
2306 | fun_box = wx.ComboBox(self, -1,size=(100,-1), |
---|
2307 | style=wx.CB_READONLY, name = '%s'% item) |
---|
2308 | self._set_fun_box_list(fun_box) |
---|
2309 | fun_box.SetSelection(0) |
---|
2310 | #self.fun_box.SetToolTipString("A function |
---|
2311 | # describing the interface") |
---|
2312 | wx.EVT_COMBOBOX(fun_box,-1, self._on_fun_box) |
---|
2313 | else: |
---|
2314 | fun_box = self.ModelTextCtrl(self, -1, |
---|
2315 | size=(_BOX_WIDTH,20), |
---|
2316 | style=wx.TE_PROCESS_ENTER, name ='%s'% item) |
---|
2317 | fun_box.SetToolTipString("Hit 'Enter' after typing.") |
---|
2318 | fun_box.SetValue(format_number(value)) |
---|
2319 | sizer.Add(fun_box, (iy,ix),(1,1), wx.EXPAND) |
---|
2320 | self.str_parameters.append([None,item, fun_box, |
---|
2321 | None,None,None,None,None]) |
---|
2322 | |
---|
2323 | |
---|
2324 | else: |
---|
2325 | ## add parameters name with checkbox for selecting to fit |
---|
2326 | cb = wx.CheckBox(self, -1, item ) |
---|
2327 | cb.SetToolTipString(" Check for fitting.") |
---|
2328 | #cb.SetValue(True) |
---|
2329 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
2330 | |
---|
2331 | sizer.Add( cb,( iy, ix),(1,1), |
---|
2332 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
2333 | |
---|
2334 | ## add parameter value |
---|
2335 | ix += 1 |
---|
2336 | value= self.model.getParam(item) |
---|
2337 | ctl1 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
2338 | style=wx.TE_PROCESS_ENTER) |
---|
2339 | ctl1.SetToolTipString("Hit 'Enter' after typing.") |
---|
2340 | ctl1.SetValue(format_number(value)) |
---|
2341 | sizer.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
2342 | ## text to show error sign |
---|
2343 | ix += 1 |
---|
2344 | text2=wx.StaticText(self, -1, '+/-') |
---|
2345 | sizer.Add(text2,(iy, ix),(1,1),\ |
---|
2346 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2347 | text2.Hide() |
---|
2348 | ix += 1 |
---|
2349 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=0) |
---|
2350 | sizer.Add(ctl2, (iy,ix),(1,1), |
---|
2351 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2352 | ctl2.Hide() |
---|
2353 | |
---|
2354 | ix += 1 |
---|
2355 | ctl3 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), |
---|
2356 | style=wx.TE_PROCESS_ENTER, |
---|
2357 | text_enter_callback = self._onparamRangeEnter) |
---|
2358 | |
---|
2359 | sizer.Add(ctl3, (iy,ix),(1,1), |
---|
2360 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2361 | ctl3.Hide() |
---|
2362 | |
---|
2363 | ix += 1 |
---|
2364 | ctl4 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), |
---|
2365 | style=wx.TE_PROCESS_ENTER, |
---|
2366 | text_enter_callback = self._onparamRangeEnter) |
---|
2367 | sizer.Add(ctl4, (iy,ix),(1,1), |
---|
2368 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2369 | |
---|
2370 | ctl4.Hide() |
---|
2371 | |
---|
2372 | if self.engine_type=="park": |
---|
2373 | ctl3.Show(True) |
---|
2374 | ctl4.Show(True) |
---|
2375 | ix +=1 |
---|
2376 | # Units |
---|
2377 | if self.model.details.has_key(item): |
---|
2378 | units = wx.StaticText(self, -1, |
---|
2379 | self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
2380 | else: |
---|
2381 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
2382 | sizer.Add(units, (iy,ix),(1,1), |
---|
2383 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2384 | |
---|
2385 | ##[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
2386 | self.parameters.append([cb,item, ctl1, |
---|
2387 | text2,ctl2, ctl3, ctl4,units]) |
---|
2388 | |
---|
2389 | iy+=1 |
---|
2390 | sizer.Add((10,10),(iy,ix),(1,1), |
---|
2391 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
2392 | |
---|
2393 | # type can be either Guassian or Array |
---|
2394 | if len(self.model.dispersion.values())>0: |
---|
2395 | type= self.model.dispersion.values()[0]["type"] |
---|
2396 | else: |
---|
2397 | type = "Gaussian" |
---|
2398 | |
---|
2399 | iy += 1 |
---|
2400 | ix = 0 |
---|
2401 | #Add tile for orientational angle |
---|
2402 | for item in keys: |
---|
2403 | if item in self.model.orientation_params: |
---|
2404 | orient_angle = wx.StaticText(self, -1, '[For 2D only]:') |
---|
2405 | sizer.Add(orient_angle,(iy, ix),(1,1), |
---|
2406 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
2407 | if not self.data.__class__.__name__ =="Data2D": |
---|
2408 | orient_angle.Hide() |
---|
2409 | else: |
---|
2410 | orient_angle.Show(True) |
---|
2411 | break |
---|
2412 | |
---|
2413 | #For Gaussian only |
---|
2414 | if type.lower() != "array": |
---|
2415 | for item in self.model.orientation_params: |
---|
2416 | if not item in self.disp_list: |
---|
2417 | ##prepare a spot to store min max |
---|
2418 | if not self.model.details.has_key(item): |
---|
2419 | self.model.details [item] = ["",None,None] |
---|
2420 | |
---|
2421 | iy += 1 |
---|
2422 | ix = 0 |
---|
2423 | ## add parameters name with checkbox for selecting to fit |
---|
2424 | cb = wx.CheckBox(self, -1, item ) |
---|
2425 | cb.SetValue(False) |
---|
2426 | cb.SetToolTipString("Check for fitting") |
---|
2427 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
2428 | if self.data.__class__.__name__ =="Data2D": |
---|
2429 | cb.Show(True) |
---|
2430 | else: |
---|
2431 | cb.Hide() |
---|
2432 | sizer.Add( cb,( iy, ix),(1,1), |
---|
2433 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
2434 | |
---|
2435 | ## add parameter value |
---|
2436 | ix += 1 |
---|
2437 | value= self.model.getParam(item) |
---|
2438 | ctl1 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
2439 | style=wx.TE_PROCESS_ENTER) |
---|
2440 | ctl1.SetToolTipString("Hit 'Enter' after typing.") |
---|
2441 | ctl1.SetValue(format_number(value)) |
---|
2442 | if self.data.__class__.__name__ =="Data2D": |
---|
2443 | ctl1.Show(True) |
---|
2444 | else: |
---|
2445 | ctl1.Hide() |
---|
2446 | sizer.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
2447 | ## text to show error sign |
---|
2448 | ix += 1 |
---|
2449 | text2=wx.StaticText(self, -1, '+/-') |
---|
2450 | sizer.Add(text2,(iy, ix),(1,1),\ |
---|
2451 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2452 | text2.Hide() |
---|
2453 | ix += 1 |
---|
2454 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=0) |
---|
2455 | sizer.Add(ctl2, (iy,ix),(1,1), |
---|
2456 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2457 | ctl2.Hide() |
---|
2458 | |
---|
2459 | |
---|
2460 | ix += 1 |
---|
2461 | ctl3 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), |
---|
2462 | style=wx.TE_PROCESS_ENTER, |
---|
2463 | text_enter_callback = self._onparamRangeEnter) |
---|
2464 | |
---|
2465 | sizer.Add(ctl3, (iy,ix),(1,1), |
---|
2466 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2467 | ctl3.Hide() |
---|
2468 | |
---|
2469 | ix += 1 |
---|
2470 | ctl4 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), |
---|
2471 | style=wx.TE_PROCESS_ENTER, |
---|
2472 | text_enter_callback = self._onparamRangeEnter) |
---|
2473 | sizer.Add(ctl4, (iy,ix),(1,1), |
---|
2474 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2475 | |
---|
2476 | ctl4.Hide() |
---|
2477 | |
---|
2478 | if self.engine_type =="park" and \ |
---|
2479 | self.data.__class__.__name__ =="Data2D": |
---|
2480 | ctl3.Show(True) |
---|
2481 | ctl4.Show(True) |
---|
2482 | |
---|
2483 | ix +=1 |
---|
2484 | # Units |
---|
2485 | if self.model.details.has_key(item): |
---|
2486 | units = wx.StaticText(self, -1, |
---|
2487 | self.model.details[item][0], |
---|
2488 | style=wx.ALIGN_LEFT) |
---|
2489 | else: |
---|
2490 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
2491 | if self.data.__class__.__name__ =="Data2D": |
---|
2492 | units.Show(True) |
---|
2493 | |
---|
2494 | else: |
---|
2495 | units.Hide() |
---|
2496 | |
---|
2497 | sizer.Add(units, (iy,ix),(1,1), |
---|
2498 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2499 | |
---|
2500 | ##[cb state, name, value, "+/-", error of fit,min,max,units] |
---|
2501 | self.parameters.append([cb,item, ctl1, |
---|
2502 | text2,ctl2, ctl3, ctl4,units]) |
---|
2503 | self.orientation_params.append([cb,item, ctl1, |
---|
2504 | text2,ctl2, ctl3, ctl4,units]) |
---|
2505 | |
---|
2506 | iy+=1 |
---|
2507 | |
---|
2508 | #Display units text on panel |
---|
2509 | for item in keys: |
---|
2510 | if self.model.details.has_key(item): |
---|
2511 | self.text2_4.Show() |
---|
2512 | #Fill the list of fittable parameters |
---|
2513 | self.select_all_param(event=None) |
---|
2514 | |
---|
2515 | self.save_current_state_fit() |
---|
2516 | boxsizer1.Add(sizer) |
---|
2517 | self.sizer3.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
2518 | self.sizer3.Layout() |
---|
2519 | self.Layout() |
---|
2520 | self.Refresh() |
---|
2521 | self.SetupScrolling() |
---|
2522 | |
---|
2523 | |
---|
2524 | class BGTextCtrl(wx.TextCtrl): |
---|
2525 | """ |
---|
2526 | Text control used to display outputs. |
---|
2527 | No editing allowed. The background is |
---|
2528 | grayed out. User can't select text. |
---|
2529 | """ |
---|
2530 | def __init__(self, *args, **kwds): |
---|
2531 | wx.TextCtrl.__init__(self, *args, **kwds) |
---|
2532 | self.SetEditable(False) |
---|
2533 | self.SetBackgroundColour(self.GetParent().GetBackgroundColour()) |
---|
2534 | |
---|
2535 | # Bind to mouse event to avoid text highlighting |
---|
2536 | # The event will be skipped once the call-back |
---|
2537 | # is called. |
---|
2538 | self.Bind(wx.EVT_MOUSE_EVENTS, self._click) |
---|
2539 | |
---|
2540 | def _click(self, event): |
---|
2541 | """ |
---|
2542 | Prevent further handling of the mouse event |
---|
2543 | by not calling Skip(). |
---|
2544 | """ |
---|
2545 | pass |
---|
2546 | |
---|
2547 | """ |
---|
2548 | Example: :: |
---|
2549 | |
---|
2550 | class HelpWindow(wx.Frame): |
---|
2551 | def __init__(self, parent, id, title): |
---|
2552 | wx.Frame.__init__(self, parent, id, title, size=(570, 400)) |
---|
2553 | |
---|
2554 | from sans.models.CylinderModel import CylinderModel |
---|
2555 | model = CylinderModel() |
---|
2556 | |
---|
2557 | from danse.common.plottools.plottables import Data1D |
---|
2558 | data= Data1D(x=[1,2], y=[3,4], dy=[0.1, 0,1]) |
---|
2559 | |
---|
2560 | from fitpanel import PageInfo |
---|
2561 | myinfo = PageInfo(self, model, data=data ) |
---|
2562 | |
---|
2563 | ## add data |
---|
2564 | |
---|
2565 | from models import ModelList |
---|
2566 | mylist= ModelList() |
---|
2567 | |
---|
2568 | from sans.models.SphereModel import SphereModel |
---|
2569 | from sans.models.SquareWellStructure import SquareWellStructure |
---|
2570 | from sans.models.DebyeModel import DebyeModel |
---|
2571 | from sans.models.LineModel import LineModel |
---|
2572 | name= "shapes" |
---|
2573 | list1= [SphereModel] |
---|
2574 | mylist.set_list( name, list1) |
---|
2575 | |
---|
2576 | name= "Shape-independent" |
---|
2577 | list1= [DebyeModel] |
---|
2578 | mylist.set_list( name, list1) |
---|
2579 | |
---|
2580 | name= "Structure Factors" |
---|
2581 | list1= [SquareWellStructure] |
---|
2582 | mylist.set_list( name, list1) |
---|
2583 | |
---|
2584 | name= "Added models" |
---|
2585 | list1= [LineModel] |
---|
2586 | mylist.set_list( name, list1) |
---|
2587 | |
---|
2588 | myinfo.model_list_box = mylist.get_list() |
---|
2589 | |
---|
2590 | self.page = FitPage(self, myinfo) |
---|
2591 | |
---|
2592 | self.Centre() |
---|
2593 | self.Show(True) |
---|
2594 | |
---|
2595 | if __name__=="__main__": |
---|
2596 | app = wx.App() |
---|
2597 | HelpWindow(None, -1, 'HelpWindow') |
---|
2598 | app.MainLoop() |
---|
2599 | """ |
---|
2600 | |
---|