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