1 | |
---|
2 | import wx |
---|
3 | import sys |
---|
4 | |
---|
5 | import numpy |
---|
6 | from sans.guiframe.utils import format_number, check_float |
---|
7 | from invariant_widgets import OutputTextCtrl |
---|
8 | # Dimensions related to chart |
---|
9 | RECTANGLE_WIDTH = 400.0 |
---|
10 | RECTANGLE_HEIGHT = 20 |
---|
11 | #Invariant panel size |
---|
12 | _BOX_WIDTH = 76 |
---|
13 | |
---|
14 | #scale to use for a bar of value zero |
---|
15 | RECTANGLE_SCALE = 0.0001 |
---|
16 | DEFAULT_QSTAR = 1.0 |
---|
17 | |
---|
18 | if sys.platform.count("win32")>0: |
---|
19 | _STATICBOX_WIDTH = 450 |
---|
20 | PANEL_WIDTH = 500 |
---|
21 | PANEL_HEIGHT = 430 |
---|
22 | FONT_VARIANT = 0 |
---|
23 | else: |
---|
24 | _STATICBOX_WIDTH = 480 |
---|
25 | PANEL_WIDTH = 530 |
---|
26 | PANEL_HEIGHT = 430 |
---|
27 | FONT_VARIANT = 1 |
---|
28 | |
---|
29 | |
---|
30 | |
---|
31 | class InvariantContainer(wx.Object): |
---|
32 | """ |
---|
33 | This class stores some values resulting resulting from invariant |
---|
34 | calculations. Given the value of total invariant, this class can also |
---|
35 | determine the percentage of invariants resulting from extrapolation. |
---|
36 | """ |
---|
37 | def __init__(self): |
---|
38 | #invariant at low range |
---|
39 | self.qstar_low = 0.0 |
---|
40 | #invariant at low range error |
---|
41 | self.qstar_low_err = 0.0 |
---|
42 | #invariant |
---|
43 | self.qstar = 0.0 |
---|
44 | #invariant error |
---|
45 | self.qstar_err = 0.0 |
---|
46 | #invariant at high range |
---|
47 | self.qstar_high = 0.0 |
---|
48 | #invariant at high range error |
---|
49 | self.qstar_high_err = 0.0 |
---|
50 | #invariant total |
---|
51 | self.qstar_total = None |
---|
52 | #invariant error |
---|
53 | self.qstar_total_err = None |
---|
54 | #scale |
---|
55 | self.qstar_low_percent = 0.0 |
---|
56 | self.qstar_high_percent = 0.0 |
---|
57 | self.qstar_percent = 0.0 |
---|
58 | # warning message |
---|
59 | self.existing_warning = False |
---|
60 | self.warning_msg = "No Details on calculations available...\n" |
---|
61 | |
---|
62 | def compute_percentage(self): |
---|
63 | """ |
---|
64 | Compute percentage of each invariant |
---|
65 | """ |
---|
66 | if self.qstar_total is not None and self.qstar_total != 0: |
---|
67 | #compute invariant percentage |
---|
68 | if self.qstar is None: |
---|
69 | self.qstar = 0.0 |
---|
70 | self.qstar_percent = self.qstar/self.qstar_total |
---|
71 | #compute low q invariant percentage |
---|
72 | if self.qstar_low is None: |
---|
73 | self.qstar_low = 0.0 |
---|
74 | self.qstar_low_percent = self.qstar_low/self.qstar_total |
---|
75 | #compute high q invariant percentage |
---|
76 | if self.qstar_high is None: |
---|
77 | self.qstar_high = 0.0 |
---|
78 | self.qstar_high_percent = self.qstar_high/self.qstar_total |
---|
79 | self.check_values() |
---|
80 | |
---|
81 | def check_values(self): |
---|
82 | """ |
---|
83 | check the validity if invariant |
---|
84 | """ |
---|
85 | #warning to the user when the extrapolated invariant is greater than %5 |
---|
86 | if self.qstar_low_percent >= 0.05: |
---|
87 | self.existing_warning = True |
---|
88 | self.warning_msg += "Extrapolated contribution at Low Q is higher " |
---|
89 | self.warning_msg += "than 5% of the invariant.\n" |
---|
90 | if self.qstar_high_percent >= 0.05: |
---|
91 | self.existing_warning = True |
---|
92 | self.warning_msg += "Extrapolated contribution at High Q is higher " |
---|
93 | self.warning_msg += "than 5% of the invariant.\n" |
---|
94 | if self.qstar_low_percent + self.qstar_high_percent >= 0.05: |
---|
95 | self.existing_warning = True |
---|
96 | self.warning_msg += "The sum of all extrapolated contributions is higher " |
---|
97 | self.warning_msg += "than 5% of the invariant.\n" |
---|
98 | |
---|
99 | if self.existing_warning: |
---|
100 | self.warning_msg += "The calculations are likely to be unreliable!\n" |
---|
101 | else: |
---|
102 | self.warning_msg = "No Details on calculations available...\n" |
---|
103 | |
---|
104 | class InvariantDetailsPanel(wx.Dialog): |
---|
105 | """ |
---|
106 | This panel describes proportion of invariants |
---|
107 | """ |
---|
108 | def __init__(self, parent=None, id=-1, qstar_container=None, |
---|
109 | title="Invariant Details", |
---|
110 | size=(PANEL_WIDTH, PANEL_HEIGHT)): |
---|
111 | wx.Dialog.__init__(self, parent=parent, id=id,title=title,size=size) |
---|
112 | |
---|
113 | #Font size |
---|
114 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
115 | self.parent = parent |
---|
116 | #self.qstar_container |
---|
117 | self.qstar_container = qstar_container |
---|
118 | #warning message |
---|
119 | self.warning_msg = self.qstar_container.warning_msg |
---|
120 | |
---|
121 | #Define scale of each bar |
---|
122 | self.low_inv_percent = self.qstar_container.qstar_low_percent |
---|
123 | self.low_scale = self.get_scale(percentage=self.low_inv_percent, |
---|
124 | scale_name="Extrapolated at Low Q") |
---|
125 | self.inv_percent = self.qstar_container.qstar_percent |
---|
126 | self.inv_scale = self.get_scale(percentage=self.inv_percent, |
---|
127 | scale_name="Inv in Q range") |
---|
128 | self.high_inv_percent = self.qstar_container.qstar_high_percent |
---|
129 | self.high_scale = self.get_scale(percentage=self.high_inv_percent, |
---|
130 | scale_name="Extrapolated at High Q") |
---|
131 | |
---|
132 | #Default color the extrapolation bar is grey |
---|
133 | self.extrapolation_color_low = wx.Colour(169, 169, 168, 128) |
---|
134 | self.extrapolation_color_high = wx.Colour(169, 169, 168, 128) |
---|
135 | #change color of high and low bar when necessary |
---|
136 | self.set_color_bar() |
---|
137 | #draw the panel itself |
---|
138 | self._do_layout() |
---|
139 | self.set_values() |
---|
140 | |
---|
141 | def _define_structure(self): |
---|
142 | """ |
---|
143 | Define main sizers needed for this panel |
---|
144 | """ |
---|
145 | #Box sizers must be defined first before defining buttons/textctrls (MAC). |
---|
146 | self.main_sizer = wx.BoxSizer(wx.VERTICAL) |
---|
147 | #Sizer related to chart |
---|
148 | chart_box = wx.StaticBox(self, -1, "Invariant Chart") |
---|
149 | self.chart_sizer = wx.StaticBoxSizer(chart_box, wx.VERTICAL) |
---|
150 | self.chart_sizer.SetMinSize((PANEL_WIDTH - 50,100)) |
---|
151 | #Sizer related to invariant values |
---|
152 | self.invariant_sizer = wx.GridBagSizer(4, 4) |
---|
153 | invariant_box = wx.StaticBox(self, -1, "Numerical Values") |
---|
154 | self.invariant_box_sizer = wx.StaticBoxSizer(invariant_box, |
---|
155 | wx.HORIZONTAL) |
---|
156 | |
---|
157 | self.invariant_box_sizer.SetMinSize((PANEL_WIDTH - 50,-1)) |
---|
158 | #Sizer related to warning message |
---|
159 | warning_box = wx.StaticBox(self, -1, "Warning") |
---|
160 | self.warning_sizer = wx.StaticBoxSizer(warning_box, wx.VERTICAL) |
---|
161 | self.warning_sizer.SetMinSize((PANEL_WIDTH-50,-1)) |
---|
162 | #Sizer related to button |
---|
163 | self.button_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
164 | |
---|
165 | def _layout_shart(self): |
---|
166 | """ |
---|
167 | Draw widgets related to chart |
---|
168 | """ |
---|
169 | self.panel_chart = wx.Panel(self) |
---|
170 | self.panel_chart.Bind(wx.EVT_PAINT, self.on_paint) |
---|
171 | self.chart_sizer.Add(self.panel_chart, 1, wx.EXPAND|wx.ALL, 0) |
---|
172 | |
---|
173 | def _layout_invariant(self): |
---|
174 | """ |
---|
175 | Draw widgets related to invariant |
---|
176 | """ |
---|
177 | uncertainty = "+/-" |
---|
178 | unit_invariant = '[1/(cm * A)]' |
---|
179 | |
---|
180 | invariant_txt = wx.StaticText(self, -1, 'Invariant') |
---|
181 | self.invariant_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH,-1)) |
---|
182 | self.invariant_tcl.SetToolTipString("Invariant in the data set's Q range.") |
---|
183 | self.invariant_err_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH,-1)) |
---|
184 | self.invariant_err_tcl.SetToolTipString("Uncertainty on the invariant.") |
---|
185 | invariant_units_txt = wx.StaticText(self, -1, unit_invariant) |
---|
186 | |
---|
187 | invariant_low_txt = wx.StaticText(self, -1, 'Invariant in low-Q region') |
---|
188 | self.invariant_low_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH,-1)) |
---|
189 | self.invariant_low_tcl.SetToolTipString("Invariant computed with the extrapolated low-Q data.") |
---|
190 | self.invariant_low_err_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH,-1)) |
---|
191 | self.invariant_low_err_tcl.SetToolTipString("Uncertainty on the invariant.") |
---|
192 | invariant_low_units_txt = wx.StaticText(self, -1, unit_invariant) |
---|
193 | |
---|
194 | invariant_high_txt = wx.StaticText(self, -1, 'Invariant in high-Q region') |
---|
195 | self.invariant_high_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH,-1)) |
---|
196 | self.invariant_high_tcl.SetToolTipString("Invariant computed with the extrapolated high-Q data") |
---|
197 | self.invariant_high_err_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH,-1)) |
---|
198 | self.invariant_high_err_tcl.SetToolTipString("Uncertainty on the invariant.") |
---|
199 | invariant_high_units_txt = wx.StaticText(self, -1, unit_invariant) |
---|
200 | |
---|
201 | #Invariant low |
---|
202 | iy = 0 |
---|
203 | ix = 0 |
---|
204 | self.invariant_sizer.Add(invariant_low_txt, (iy, ix), (1,1), |
---|
205 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
206 | ix += 1 |
---|
207 | self.invariant_sizer.Add(self.invariant_low_tcl, (iy, ix), (1,1), |
---|
208 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
209 | ix += 1 |
---|
210 | self.invariant_sizer.Add( wx.StaticText(self, -1, uncertainty), |
---|
211 | (iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
212 | ix += 1 |
---|
213 | self.invariant_sizer.Add(self.invariant_low_err_tcl, (iy, ix), (1,1), |
---|
214 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
215 | ix += 1 |
---|
216 | self.invariant_sizer.Add(invariant_low_units_txt |
---|
217 | ,(iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
218 | #Invariant |
---|
219 | iy += 1 |
---|
220 | ix = 0 |
---|
221 | self.invariant_sizer.Add(invariant_txt, (iy, ix), (1,1), |
---|
222 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
223 | ix += 1 |
---|
224 | self.invariant_sizer.Add(self.invariant_tcl, (iy, ix), (1,1), |
---|
225 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
226 | ix += 1 |
---|
227 | self.invariant_sizer.Add(wx.StaticText(self, -1, uncertainty), |
---|
228 | (iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
229 | ix += 1 |
---|
230 | self.invariant_sizer.Add(self.invariant_err_tcl, (iy, ix), (1,1), |
---|
231 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
232 | ix +=1 |
---|
233 | self.invariant_sizer.Add(invariant_units_txt |
---|
234 | ,(iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
235 | #Invariant high |
---|
236 | iy += 1 |
---|
237 | ix = 0 |
---|
238 | self.invariant_sizer.Add(invariant_high_txt, (iy, ix), (1,1), |
---|
239 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
240 | ix += 1 |
---|
241 | self.invariant_sizer.Add(self.invariant_high_tcl, (iy, ix), (1,1), |
---|
242 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
243 | ix += 1 |
---|
244 | self.invariant_sizer.Add(wx.StaticText(self, -1, uncertainty), |
---|
245 | (iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
246 | ix += 1 |
---|
247 | self.invariant_sizer.Add(self.invariant_high_err_tcl, (iy, ix), (1,1), |
---|
248 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
249 | ix += 1 |
---|
250 | self.invariant_sizer.Add(invariant_high_units_txt |
---|
251 | ,(iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
252 | self.invariant_box_sizer.Add(self.invariant_sizer, 0, wx.TOP|wx.BOTTOM, 10) |
---|
253 | |
---|
254 | def _layout_warning(self): |
---|
255 | """ |
---|
256 | Draw widgets related to warning |
---|
257 | """ |
---|
258 | #Warning [string] |
---|
259 | self.warning_msg_txt = wx.StaticText(self, -1,self.warning_msg) |
---|
260 | if self.qstar_container.existing_warning: |
---|
261 | self.warning_msg_txt.SetForegroundColour('red') |
---|
262 | self.warning_sizer.AddMany([(self.warning_msg_txt, 0, |
---|
263 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 10)]) |
---|
264 | |
---|
265 | def _layout_button(self): |
---|
266 | """ |
---|
267 | Draw widgets related to button |
---|
268 | """ |
---|
269 | #Close button |
---|
270 | id = wx.NewId() |
---|
271 | button_ok = wx.Button(self, id, "Ok") |
---|
272 | button_ok.SetToolTipString("Give Details on Computation") |
---|
273 | self.Bind(wx.EVT_BUTTON, self.on_close, id=id) |
---|
274 | self.button_sizer.AddMany([((20,20), 0 , wx.LEFT, 350), |
---|
275 | (button_ok, 0 , wx.RIGHT, 10)]) |
---|
276 | def _do_layout(self): |
---|
277 | """ |
---|
278 | Draw window content |
---|
279 | """ |
---|
280 | self._define_structure() |
---|
281 | self._layout_shart() |
---|
282 | self._layout_invariant() |
---|
283 | self._layout_warning() |
---|
284 | self._layout_button() |
---|
285 | self.main_sizer.AddMany([(self.chart_sizer, 0, wx.ALL, 10), |
---|
286 | ( self.invariant_box_sizer, 0, wx.ALL, 10), |
---|
287 | (self.warning_sizer, 0, wx.ALL, 10), |
---|
288 | (self.button_sizer, 0, wx.ALL, 10)]) |
---|
289 | self.SetSizer(self.main_sizer) |
---|
290 | |
---|
291 | |
---|
292 | def set_values(self): |
---|
293 | """ |
---|
294 | Set value of txtcrtl |
---|
295 | """ |
---|
296 | self.invariant_tcl.SetValue(format_number(self.qstar_container.qstar)) |
---|
297 | self.invariant_err_tcl.SetValue(format_number(self.qstar_container.qstar_err)) |
---|
298 | self.invariant_low_tcl.SetValue(format_number(self.qstar_container.qstar_low)) |
---|
299 | self.invariant_low_err_tcl.SetValue(format_number(self.qstar_container.qstar_low_err)) |
---|
300 | self.invariant_high_tcl.SetValue(format_number(self.qstar_container.qstar_high)) |
---|
301 | self.invariant_high_err_tcl.SetValue(format_number(self.qstar_container.qstar_high_err)) |
---|
302 | |
---|
303 | def get_scale(self, percentage, scale_name='scale'): |
---|
304 | """ |
---|
305 | Check scale receive in this panel. |
---|
306 | """ |
---|
307 | try: |
---|
308 | if percentage is None or percentage == 0.0: |
---|
309 | percentage = RECTANGLE_SCALE |
---|
310 | percentage = float(percentage) |
---|
311 | except: |
---|
312 | percentage = RECTANGLE_SCALE |
---|
313 | self.warning_msg += "Receive an invalid scale for %s\n" |
---|
314 | self.warning_msg += "check this value : %s\n"%(str(scale_name),str(percentage)) |
---|
315 | return percentage |
---|
316 | |
---|
317 | def set_color_bar(self): |
---|
318 | """ |
---|
319 | Change the color for low and high bar when necessary |
---|
320 | """ |
---|
321 | #warning to the user when the extrapolated invariant is greater than %5 |
---|
322 | if self.low_scale >= 0.05: |
---|
323 | self.extrapolation_color_low = wx.Colour(255, 0, 0, 128) |
---|
324 | if self.high_scale >= 0.05: |
---|
325 | self.extrapolation_color_high = wx.Colour(255, 0, 0, 128) |
---|
326 | |
---|
327 | def on_close(self, event): |
---|
328 | """ |
---|
329 | Close the current window |
---|
330 | """ |
---|
331 | self.Close() |
---|
332 | |
---|
333 | def on_paint(self, event): |
---|
334 | """ |
---|
335 | Draw the chart |
---|
336 | """ |
---|
337 | dc = wx.PaintDC(self.panel_chart) |
---|
338 | try: |
---|
339 | gc = wx.GraphicsContext.Create(dc) |
---|
340 | except NotImplementedError: |
---|
341 | dc.DrawText("This build of wxPython does not support the wx.GraphicsContext " |
---|
342 | "family of classes.", 25, 25) |
---|
343 | return |
---|
344 | #Start the drawing |
---|
345 | font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT) |
---|
346 | font.SetWeight(wx.BOLD) |
---|
347 | gc.SetFont(font) |
---|
348 | # Draw a rectangle |
---|
349 | path = gc.CreatePath() |
---|
350 | path.AddRectangle(-RECTANGLE_WIDTH/2,-RECTANGLE_HEIGHT/2, |
---|
351 | RECTANGLE_WIDTH/2,RECTANGLE_HEIGHT/2) |
---|
352 | x_origine = 20 |
---|
353 | y_origine = 15 |
---|
354 | #Draw low rectangle |
---|
355 | gc.PushState() |
---|
356 | label = "Q* At Low-Q" |
---|
357 | PathFunc = gc.DrawPath |
---|
358 | w, h = gc.GetTextExtent(label) |
---|
359 | gc.DrawText(label, x_origine, y_origine) |
---|
360 | #Translate the rectangle |
---|
361 | x_center = x_origine + RECTANGLE_WIDTH * self.low_scale/2 + w +10 |
---|
362 | y_center = y_origine + h |
---|
363 | gc.Translate(x_center, y_center) |
---|
364 | gc.SetPen(wx.Pen("black", 1)) |
---|
365 | gc.SetBrush(wx.Brush(self.extrapolation_color_low)) |
---|
366 | low_percent = format_number(self.low_inv_percent*100)+ '%' |
---|
367 | x_center = 20 |
---|
368 | y_center = -h |
---|
369 | gc.DrawText(low_percent, x_center, y_center) |
---|
370 | # Increase width by self.low_scale |
---|
371 | gc.Scale(self.low_scale, 1.0) |
---|
372 | PathFunc(path) |
---|
373 | gc.PopState() |
---|
374 | #Draw rectangle for invariant |
---|
375 | gc.PushState() # save it again |
---|
376 | y_origine += 20 |
---|
377 | gc.DrawText("Q* In range", x_origine, y_origine) |
---|
378 | # offset to the lower part of the window |
---|
379 | x_center = x_origine + RECTANGLE_WIDTH * self.inv_scale/2 + w + 10 |
---|
380 | y_center = y_origine + h |
---|
381 | gc.Translate(x_center, y_center) |
---|
382 | # 128 == half transparent |
---|
383 | gc.SetBrush(wx.Brush(wx.Colour(67, 208, 128, 128))) |
---|
384 | # Increase width by self.inv_scale |
---|
385 | inv_percent = format_number(self.inv_percent*100)+ '%' |
---|
386 | x_center = 20 |
---|
387 | y_center = -h |
---|
388 | gc.DrawText(inv_percent, x_center, y_center) |
---|
389 | gc.Scale(self.inv_scale, 1.0) |
---|
390 | gc.DrawPath(path) |
---|
391 | gc.PopState() |
---|
392 | # restore saved state |
---|
393 | #Draw rectangle for high invariant |
---|
394 | gc.PushState() |
---|
395 | y_origine += 20 |
---|
396 | gc.DrawText("Q* At High-Q", x_origine, y_origine) |
---|
397 | #define the position of the new rectangle |
---|
398 | x_center = x_origine + RECTANGLE_WIDTH * self.high_scale/2 + w + 10 |
---|
399 | y_center = y_origine + h |
---|
400 | gc.Translate(x_center, y_center) |
---|
401 | gc.SetBrush(wx.Brush(self.extrapolation_color_high)) |
---|
402 | # increase scale by self.high_scale |
---|
403 | high_percent = format_number(self.high_inv_percent*100)+ '%' |
---|
404 | x_center = 20 |
---|
405 | y_center = -h |
---|
406 | gc.DrawText(high_percent, x_center, y_center) |
---|
407 | |
---|
408 | gc.Scale(self.high_scale, 1.0) |
---|
409 | gc.DrawPath(path) |
---|
410 | gc.PopState() |
---|
411 | |
---|
412 | |
---|
413 | if __name__ =="__main__": |
---|
414 | app = wx.App() |
---|
415 | container = InvariantContainer() |
---|
416 | container.qstar_total = 100.0 |
---|
417 | container.qstar = 15.0 |
---|
418 | container.qstar_low = 0.001 |
---|
419 | container.qstar_high = 100.0 |
---|
420 | container.compute_percentage() |
---|
421 | dlg = InvariantDetailsPanel(qstar_container=container) |
---|
422 | dlg.ShowModal() |
---|
423 | app.MainLoop() |
---|