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