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