[959eb01] | 1 | import wx |
---|
[fa81e94] | 2 | from .plottables import Theory1D |
---|
[959eb01] | 3 | import math |
---|
| 4 | import numpy as np |
---|
[fa81e94] | 5 | from . import fittings |
---|
| 6 | from . import transform |
---|
[959eb01] | 7 | import sys |
---|
| 8 | |
---|
| 9 | # Linear fit panel size |
---|
| 10 | if sys.platform.count("win32") > 0: |
---|
| 11 | FONT_VARIANT = 0 |
---|
| 12 | PNL_WIDTH = 450 |
---|
| 13 | PNL_HEIGHT = 500 |
---|
| 14 | else: |
---|
| 15 | FONT_VARIANT = 1 |
---|
| 16 | PNL_WIDTH = 500 |
---|
| 17 | PNL_HEIGHT = 500 |
---|
| 18 | RG_ON = True |
---|
| 19 | |
---|
| 20 | def format_number(value, high=False): |
---|
| 21 | """ |
---|
| 22 | Return a float in a standardized, human-readable formatted string. |
---|
| 23 | This is used to output readable (e.g. x.xxxe-y) values to the panel. |
---|
| 24 | """ |
---|
| 25 | try: |
---|
| 26 | value = float(value) |
---|
| 27 | except: |
---|
| 28 | output = "NaN" |
---|
| 29 | return output.lstrip().rstrip() |
---|
| 30 | |
---|
| 31 | if high: |
---|
| 32 | output = "%-6.4g" % value |
---|
| 33 | |
---|
| 34 | else: |
---|
| 35 | output = "%-5.3g" % value |
---|
| 36 | return output.lstrip().rstrip() |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | class LinearFit(wx.Dialog): |
---|
| 40 | def __init__(self, parent, plottable, push_data, transform, title): |
---|
| 41 | """ |
---|
| 42 | Dialog window pops- up when select Linear fit on Context menu |
---|
| 43 | Displays fitting parameters. This class handles the linearized |
---|
| 44 | fitting and derives and displays specialized output parameters based |
---|
| 45 | on the scale choice of the plot calling it. |
---|
| 46 | |
---|
| 47 | :note1: The fitting is currently a bit convoluted as besides using |
---|
| 48 | plottools.transform.py to handle all the conversions, it uses |
---|
| 49 | LineModel to define a linear model and calculate a number of |
---|
| 50 | things like residuals etc as well as the function itself given an x |
---|
| 51 | value. It also uses fittings.py to set up the defined LineModel for |
---|
| 52 | fitting and then send it to the SciPy NLLSQ method. As these are by |
---|
| 53 | definition "linear nodels" it would make more sense to just call |
---|
| 54 | a linear solver such as scipy.stats.linregress or bumps.wsolve directly. |
---|
| 55 | This would considerably simplify the code and remove the need I think |
---|
| 56 | for LineModel.py and possibly fittins.py altogether. -PDB 7/10/16 |
---|
| 57 | |
---|
| 58 | :note2: The linearized fits do not take resolution into account. This |
---|
| 59 | means that for poor resolution such as slit smearing the answers will |
---|
| 60 | be completely wrong --- Rg would be OK but I0 would be orders of |
---|
| 61 | magnitude off. Eventually we should fix this to account properly for |
---|
| 62 | resolution. -PDB 7/10/16 |
---|
| 63 | """ |
---|
| 64 | wx.Dialog.__init__(self, parent, title=title, |
---|
| 65 | size=(PNL_WIDTH, 350)) |
---|
| 66 | self.parent = parent |
---|
| 67 | self.transform = transform |
---|
| 68 | # Font |
---|
| 69 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
| 70 | # Registered owner for close event |
---|
| 71 | self._registered_close = None |
---|
| 72 | # dialog panel self call function to plot the fitting function |
---|
| 73 | # calls the calling PlotPanel method onFitDisplay |
---|
| 74 | self.push_data = push_data |
---|
| 75 | # dialog self plottable - basically the plot we are working with |
---|
| 76 | # passed in by the caller |
---|
| 77 | self.plottable = plottable |
---|
| 78 | # is this a Guinier fit |
---|
| 79 | self.rg_on = False |
---|
| 80 | # Receive transformations of x and y - basically transform is passed |
---|
| 81 | # as caller method that returns its current value for these |
---|
| 82 | self.xLabel, self.yLabel, self.Avalue, self.Bvalue, \ |
---|
| 83 | self.ErrAvalue, self.ErrBvalue, self.Chivalue = self.transform() |
---|
| 84 | |
---|
| 85 | # Now set up the dialog interface |
---|
| 86 | self.layout() |
---|
| 87 | # Receives the type of model for the fitting |
---|
[fa81e94] | 88 | from .LineModel import LineModel |
---|
[959eb01] | 89 | self.model = LineModel() |
---|
| 90 | # Display the fittings values |
---|
| 91 | self.default_A = self.model.getParam('A') |
---|
| 92 | self.default_B = self.model.getParam('B') |
---|
| 93 | self.cstA = fittings.Parameter(self.model, 'A', self.default_A) |
---|
| 94 | self.cstB = fittings.Parameter(self.model, 'B', self.default_B) |
---|
| 95 | |
---|
| 96 | # Set default value of parameter in the dialog panel |
---|
[235f514] | 97 | if self.Avalue is None: |
---|
[959eb01] | 98 | self.tcA.SetValue(format_number(self.default_A)) |
---|
| 99 | else: |
---|
| 100 | self.tcA.SetLabel(format_number(self.Avalue)) |
---|
[235f514] | 101 | if self.Bvalue is None: |
---|
[959eb01] | 102 | self.tcB.SetValue(format_number(self.default_B)) |
---|
| 103 | else: |
---|
| 104 | self.tcB.SetLabel(format_number(self.Bvalue)) |
---|
[235f514] | 105 | if self.ErrAvalue is None: |
---|
[959eb01] | 106 | self.tcErrA.SetLabel(format_number(0.0)) |
---|
| 107 | else: |
---|
| 108 | self.tcErrA.SetLabel(format_number(self.ErrAvalue)) |
---|
[235f514] | 109 | if self.ErrBvalue is None: |
---|
[959eb01] | 110 | self.tcErrB.SetLabel(format_number(0.0)) |
---|
| 111 | else: |
---|
| 112 | self.tcErrB.SetLabel(format_number(self.ErrBvalue)) |
---|
[235f514] | 113 | if self.Chivalue is None: |
---|
[959eb01] | 114 | self.tcChi.SetLabel(format_number(0.0)) |
---|
| 115 | else: |
---|
| 116 | self.tcChi.SetLabel(format_number(self.Chivalue)) |
---|
| 117 | if self.plottable.x != []: |
---|
| 118 | # store the values of View in self.x,self.y,self.dx,self.dy |
---|
| 119 | self.x, self.y, self.dx, \ |
---|
| 120 | self.dy = self.plottable.returnValuesOfView() |
---|
| 121 | try: |
---|
| 122 | self.mini = self.floatForwardTransform(min(self.x)) |
---|
| 123 | except: |
---|
| 124 | self.mini = "Invalid" |
---|
| 125 | try: |
---|
| 126 | self.maxi = self.floatForwardTransform(max(self.x)) |
---|
| 127 | except: |
---|
| 128 | self.maxi = "Invalid" |
---|
| 129 | |
---|
| 130 | self.initXmin.SetValue(format_number(min(self.plottable.x))) |
---|
| 131 | self.initXmax.SetValue(format_number(max(self.plottable.x))) |
---|
| 132 | self.mini = min(self.x) |
---|
| 133 | self.maxi = max(self.x) |
---|
| 134 | self.xminFit.SetValue(format_number(self.mini)) |
---|
| 135 | self.xmaxFit.SetValue(format_number(self.maxi)) |
---|
| 136 | |
---|
| 137 | def layout(self): |
---|
| 138 | """ |
---|
| 139 | Sets up the panel layout for the linear fit including all the |
---|
| 140 | labels, text entry boxes, and buttons. |
---|
| 141 | |
---|
| 142 | """ |
---|
| 143 | |
---|
| 144 | # set up sizers first. |
---|
| 145 | # vbox is the panel sizer and is a vertical sizer |
---|
| 146 | # The first element of the panel is sizer which is a gridbagsizer |
---|
| 147 | # and contains most of the text fields |
---|
| 148 | # this is followed by a line separator added to vbox |
---|
| 149 | # and finally the sizer_button (a horizontal sizer) adds the buttons |
---|
| 150 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
| 151 | sizer = wx.GridBagSizer(5, 5) |
---|
| 152 | sizer_button = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 153 | |
---|
| 154 | #size of string boxes in pixels |
---|
| 155 | _BOX_WIDTH = 100 |
---|
| 156 | _BOX_HEIGHT = 20 |
---|
| 157 | #now set up all the text fields |
---|
| 158 | self.tcA = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, _BOX_HEIGHT)) |
---|
| 159 | self.tcA.SetToolTipString("Fit value for the slope parameter.") |
---|
| 160 | self.tcErrA = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, _BOX_HEIGHT)) |
---|
| 161 | self.tcErrA.SetToolTipString("Error on the slope parameter.") |
---|
| 162 | self.tcB = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, _BOX_HEIGHT)) |
---|
| 163 | self.tcA.SetToolTipString("Fit value for the constant parameter.") |
---|
| 164 | self.tcErrB = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, _BOX_HEIGHT)) |
---|
| 165 | self.tcErrB.SetToolTipString("Error on the constant parameter.") |
---|
| 166 | self.tcChi = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, _BOX_HEIGHT)) |
---|
| 167 | self.tcChi.SetToolTipString("Chi^2 over degrees of freedom.") |
---|
| 168 | self.xminFit = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, _BOX_HEIGHT)) |
---|
| 169 | msg = "Enter the minimum value on " |
---|
| 170 | msg += "the x-axis to be included in the fit." |
---|
| 171 | self.xminFit.SetToolTipString(msg) |
---|
| 172 | self.xmaxFit = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, _BOX_HEIGHT)) |
---|
| 173 | msg = "Enter the maximum value on " |
---|
| 174 | msg += " the x-axis to be included in the fit." |
---|
| 175 | self.xmaxFit.SetToolTipString(msg) |
---|
| 176 | self.initXmin = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, _BOX_HEIGHT)) |
---|
| 177 | msg = "Minimum value on the x-axis for the plotted data." |
---|
| 178 | self.initXmin.SetToolTipString(msg) |
---|
| 179 | self.initXmax = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, _BOX_HEIGHT)) |
---|
| 180 | msg = "Maximum value on the x-axis for the plotted data." |
---|
| 181 | self.initXmax.SetToolTipString(msg) |
---|
| 182 | |
---|
| 183 | # Make the info box not editable |
---|
| 184 | # _BACKGROUND_COLOR = '#ffdf85' |
---|
| 185 | _BACKGROUND_COLOR = self.GetBackgroundColour() |
---|
| 186 | self.initXmin.SetEditable(False) |
---|
| 187 | self.initXmin.SetBackgroundColour(_BACKGROUND_COLOR) |
---|
| 188 | self.initXmax.SetEditable(False) |
---|
| 189 | self.initXmax.SetBackgroundColour(_BACKGROUND_COLOR) |
---|
| 190 | |
---|
| 191 | #set some flags for specific types of fits like Guinier (Rg) and |
---|
| 192 | #Porod (bg) -- this will determine WHAT boxes show up in the |
---|
| 193 | #sizer layout and depends on the active axis transform |
---|
| 194 | self.bg_on = False |
---|
| 195 | if RG_ON: |
---|
| 196 | if (self.yLabel == "ln(y)" or self.yLabel == "ln(y*x)") and \ |
---|
| 197 | (self.xLabel == "x^(2)"): |
---|
| 198 | self.rg_on = True |
---|
| 199 | if (self.xLabel == "x^(4)") and (self.yLabel == "y*x^(4)"): |
---|
| 200 | self.bg_on = True |
---|
| 201 | |
---|
| 202 | # Finally set up static text strings |
---|
| 203 | warning = "WARNING! Resolution is NOT accounted for. \n" |
---|
| 204 | warning += "Thus slit smeared data will give very wrong answers!" |
---|
| 205 | self.textwarn = wx.StaticText(self, -1, warning) |
---|
| 206 | self.textwarn.SetForegroundColour(wx.RED) |
---|
| 207 | explanation = "Perform fit for y(x) = ax + b \n" |
---|
| 208 | if self.bg_on: |
---|
| 209 | param_a = 'Background (= Parameter a)' |
---|
| 210 | else: |
---|
| 211 | param_a = 'Parameter a' |
---|
| 212 | |
---|
| 213 | |
---|
| 214 | #Now set this all up in the GridBagSizer sizer |
---|
| 215 | ix = 0 |
---|
| 216 | iy = 0 |
---|
| 217 | sizer.Add(self.textwarn, (iy, ix), |
---|
| 218 | (2, 3), wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 219 | iy += 2 |
---|
| 220 | sizer.Add(wx.StaticText(self, -1, explanation), (iy, ix), |
---|
| 221 | (1, 1), wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 222 | iy += 1 |
---|
| 223 | sizer.Add(wx.StaticText(self, -1, param_a), (iy, ix), |
---|
| 224 | (1, 1), wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 225 | ix += 1 |
---|
| 226 | sizer.Add(self.tcA, (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 227 | ix += 1 |
---|
| 228 | sizer.Add(wx.StaticText(self, -1, '+/-'), |
---|
| 229 | (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 230 | ix += 1 |
---|
| 231 | sizer.Add(self.tcErrA, (iy, ix), (1, 1), |
---|
| 232 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 233 | iy += 1 |
---|
| 234 | ix = 0 |
---|
| 235 | sizer.Add(wx.StaticText(self, -1, 'Parameter b'), (iy, ix), (1, 1), |
---|
| 236 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 237 | ix += 1 |
---|
| 238 | sizer.Add(self.tcB, (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 239 | ix += 1 |
---|
| 240 | sizer.Add(wx.StaticText(self, -1, '+/-'), (iy, ix), |
---|
| 241 | (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 242 | ix += 1 |
---|
| 243 | sizer.Add(self.tcErrB, (iy, ix), (1, 1), |
---|
| 244 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 245 | iy += 1 |
---|
| 246 | ix = 0 |
---|
| 247 | sizer.Add(wx.StaticText(self, -1, 'Chi2/dof'), (iy, ix), (1, 1), |
---|
| 248 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 249 | ix += 1 |
---|
| 250 | sizer.Add(self.tcChi, (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 251 | iy += 2 |
---|
| 252 | ix = 1 |
---|
| 253 | sizer.Add(wx.StaticText(self, -1, 'Min'), (iy, ix), (1, 1), |
---|
| 254 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 255 | ix += 2 |
---|
| 256 | sizer.Add(wx.StaticText(self, -1, 'Max'), (iy, ix), |
---|
| 257 | (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 258 | |
---|
| 259 | iy += 1 |
---|
| 260 | ix = 0 |
---|
| 261 | sizer.Add(wx.StaticText(self, -1, 'Maximum range (linear scale)'), |
---|
| 262 | (iy, ix), (1, 1), |
---|
| 263 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 264 | ix += 1 |
---|
| 265 | sizer.Add(self.initXmin, (iy, ix), (1, 1), |
---|
| 266 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 267 | ix += 2 |
---|
| 268 | sizer.Add(self.initXmax, (iy, ix), (1, 1), |
---|
| 269 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 270 | |
---|
| 271 | iy += 1 |
---|
| 272 | ix = 0 |
---|
| 273 | sizer.Add(wx.StaticText(self, -1, 'Fit range of ' + self.xLabel), |
---|
| 274 | (iy, ix), (1, 1), |
---|
| 275 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 276 | ix += 1 |
---|
| 277 | sizer.Add(self.xminFit, (iy, ix), (1, 1), |
---|
| 278 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 279 | ix += 2 |
---|
| 280 | sizer.Add(self.xmaxFit, (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 281 | if self.rg_on: |
---|
| 282 | self.SetSize((PNL_WIDTH, PNL_HEIGHT)) |
---|
| 283 | I0_stxt = wx.StaticText(self, -1, 'I(q=0)') |
---|
| 284 | self.I0_tctr = wx.TextCtrl(self, -1, '') |
---|
| 285 | self.I0_tctr.SetEditable(False) |
---|
| 286 | self.I0_tctr.SetBackgroundColour(_BACKGROUND_COLOR) |
---|
| 287 | self.I0err_tctr = wx.TextCtrl(self, -1, '') |
---|
| 288 | self.I0err_tctr.SetEditable(False) |
---|
| 289 | self.I0err_tctr.SetBackgroundColour(_BACKGROUND_COLOR) |
---|
| 290 | Rg_stxt = wx.StaticText(self, -1, 'Rg [A]') |
---|
| 291 | Rg_stxt.Show(self.yLabel == "ln(y)") |
---|
| 292 | self.Rg_tctr = wx.TextCtrl(self, -1, '') |
---|
| 293 | self.Rg_tctr.SetEditable(False) |
---|
| 294 | self.Rg_tctr.SetBackgroundColour(_BACKGROUND_COLOR) |
---|
| 295 | self.Rg_tctr.Show(self.yLabel == "ln(y)") |
---|
| 296 | self.Rgerr_tctr = wx.TextCtrl(self, -1, '') |
---|
| 297 | self.Rgerr_tctr.SetEditable(False) |
---|
| 298 | self.Rgerr_tctr.SetBackgroundColour(_BACKGROUND_COLOR) |
---|
| 299 | self.Rgerr_tctr.Show(self.yLabel == "ln(y)") |
---|
| 300 | self.Rgerr_pm = wx.StaticText(self, -1, '+/-') |
---|
| 301 | self.Rgerr_pm.Show(self.yLabel == "ln(y)") |
---|
| 302 | Diameter_stxt = wx.StaticText(self, -1, 'Rod Diameter [A]') |
---|
| 303 | Diameter_stxt.Show(self.yLabel == "ln(y*x)") |
---|
| 304 | self.Diameter_tctr = wx.TextCtrl(self, -1, '') |
---|
| 305 | self.Diameter_tctr.SetEditable(False) |
---|
| 306 | self.Diameter_tctr.SetBackgroundColour(_BACKGROUND_COLOR) |
---|
| 307 | self.Diameter_tctr.Show(self.yLabel == "ln(y*x)") |
---|
| 308 | self.Diameter_pm = wx.StaticText(self, -1, '+/-') |
---|
| 309 | self.Diameter_pm.Show(self.yLabel == "ln(y*x)") |
---|
| 310 | self.Diametererr_tctr = wx.TextCtrl(self, -1, '') |
---|
| 311 | self.Diametererr_tctr.SetEditable(False) |
---|
| 312 | self.Diametererr_tctr.SetBackgroundColour(_BACKGROUND_COLOR) |
---|
| 313 | self.Diametererr_tctr.Show(self.yLabel == "ln(y*x)") |
---|
| 314 | RgQmin_stxt = wx.StaticText(self, -1, 'Rg*Qmin') |
---|
| 315 | self.RgQmin_tctr = wx.TextCtrl(self, -1, '') |
---|
| 316 | self.RgQmin_tctr.SetEditable(False) |
---|
| 317 | self.RgQmin_tctr.SetBackgroundColour(_BACKGROUND_COLOR) |
---|
| 318 | RgQmax_stxt = wx.StaticText(self, -1, 'Rg*Qmax') |
---|
| 319 | self.RgQmax_tctr = wx.TextCtrl(self, -1, '') |
---|
| 320 | self.RgQmax_tctr.SetEditable(False) |
---|
| 321 | self.RgQmax_tctr.SetBackgroundColour(_BACKGROUND_COLOR) |
---|
| 322 | |
---|
| 323 | iy += 2 |
---|
| 324 | ix = 0 |
---|
| 325 | sizer.Add(I0_stxt, (iy, ix), (1, 1), |
---|
| 326 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 327 | ix += 1 |
---|
| 328 | sizer.Add(self.I0_tctr, (iy, ix), (1, 1), |
---|
| 329 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 330 | ix += 1 |
---|
| 331 | sizer.Add(wx.StaticText(self, -1, '+/-'), (iy, ix), |
---|
| 332 | (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 333 | ix += 1 |
---|
| 334 | sizer.Add(self.I0err_tctr, (iy, ix), (1, 1), |
---|
| 335 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 336 | |
---|
| 337 | iy += 1 |
---|
| 338 | ix = 0 |
---|
| 339 | sizer.Add(Rg_stxt, (iy, ix), (1, 1), |
---|
| 340 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 341 | ix += 1 |
---|
| 342 | sizer.Add(self.Rg_tctr, (iy, ix), (1, 1), |
---|
| 343 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 344 | |
---|
| 345 | ix += 1 |
---|
| 346 | sizer.Add(self.Rgerr_pm, (iy, ix), |
---|
| 347 | (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 348 | ix += 1 |
---|
| 349 | sizer.Add(self.Rgerr_tctr, (iy, ix), (1, 1), |
---|
| 350 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 351 | iy += 1 |
---|
| 352 | ix = 0 |
---|
| 353 | sizer.Add(Diameter_stxt, (iy, ix), (1, 1), |
---|
| 354 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 355 | ix += 1 |
---|
| 356 | sizer.Add(self.Diameter_tctr, (iy, ix), (1, 1), |
---|
| 357 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 358 | |
---|
| 359 | ix += 1 |
---|
| 360 | sizer.Add(self.Diameter_pm, (iy, ix), |
---|
| 361 | (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 362 | ix += 1 |
---|
| 363 | sizer.Add(self.Diametererr_tctr, (iy, ix), (1, 1), |
---|
| 364 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 365 | iy += 1 |
---|
| 366 | ix = 0 |
---|
| 367 | sizer.Add(RgQmin_stxt, (iy, ix), (1, 1), |
---|
| 368 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 369 | ix += 1 |
---|
| 370 | sizer.Add(self.RgQmin_tctr, (iy, ix), (1, 1), |
---|
| 371 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 372 | iy += 1 |
---|
| 373 | ix = 0 |
---|
| 374 | sizer.Add(RgQmax_stxt, (iy, ix), (1, 1), |
---|
| 375 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 376 | ix += 1 |
---|
| 377 | sizer.Add(self.RgQmax_tctr, (iy, ix), (1, 1), |
---|
| 378 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 379 | |
---|
| 380 | #Now add some space before the separation line |
---|
| 381 | iy += 1 |
---|
| 382 | ix = 0 |
---|
| 383 | sizer.Add((20,20), (iy, ix), (1, 1), |
---|
| 384 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 385 | |
---|
| 386 | # Buttons on the bottom |
---|
| 387 | self.btFit = wx.Button(self, -1, 'Fit') |
---|
| 388 | self.btFit.Bind(wx.EVT_BUTTON, self._onFit) |
---|
| 389 | self.btFit.SetToolTipString("Perform fit.") |
---|
| 390 | self.btClose = wx.Button(self, wx.ID_CANCEL, 'Close') |
---|
| 391 | self.btClose.Bind(wx.EVT_BUTTON, self._on_close) |
---|
| 392 | sizer_button.Add((20, 20), 1, wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 393 | sizer_button.Add(self.btFit, 0, |
---|
| 394 | wx.LEFT | wx.RIGHT | wx.ADJUST_MINSIZE, 10) |
---|
| 395 | sizer_button.Add(self.btClose, 0, |
---|
| 396 | wx.LEFT | wx.RIGHT | wx.ADJUST_MINSIZE, 10) |
---|
| 397 | |
---|
| 398 | vbox.Add(sizer) |
---|
| 399 | self.static_line_1 = wx.StaticLine(self, -1) |
---|
| 400 | vbox.Add(self.static_line_1, 0, wx.EXPAND, 0) |
---|
| 401 | vbox.Add(sizer_button, 0, wx.EXPAND | wx.BOTTOM | wx.TOP, 10) |
---|
| 402 | |
---|
| 403 | # panel.SetSizer(sizer) |
---|
| 404 | self.SetSizer(vbox) |
---|
| 405 | self.Centre() |
---|
| 406 | |
---|
| 407 | def register_close(self, owner): |
---|
| 408 | """ |
---|
| 409 | Method to register the close event to a parent |
---|
| 410 | window that needs notification when the dialog |
---|
| 411 | is closed |
---|
| 412 | |
---|
| 413 | :param owner: parent window |
---|
| 414 | |
---|
| 415 | """ |
---|
| 416 | self._registered_close = owner |
---|
| 417 | |
---|
| 418 | def _on_close(self, event): |
---|
| 419 | """ |
---|
| 420 | Close event. |
---|
| 421 | Notify registered owner if available. |
---|
| 422 | """ |
---|
| 423 | event.Skip() |
---|
| 424 | if self._registered_close is not None: |
---|
| 425 | self._registered_close() |
---|
| 426 | |
---|
| 427 | def _onFit(self, event): |
---|
| 428 | """ |
---|
| 429 | Performs the fit. Receive an event when clicking on |
---|
| 430 | the button Fit.Computes chisqr , |
---|
| 431 | A and B parameters of the best linear fit y=Ax +B |
---|
| 432 | Push a plottable to the caller |
---|
| 433 | """ |
---|
| 434 | tempx = [] |
---|
| 435 | tempy = [] |
---|
| 436 | tempdy = [] |
---|
| 437 | |
---|
| 438 | # Check if View contains a x array .we online fit when x exits |
---|
| 439 | # makes transformation for y as a line to fit |
---|
| 440 | if self.x != []: |
---|
| 441 | if self.checkFitValues(self.xminFit) == True: |
---|
| 442 | # Check if the field of Fit Dialog contain values |
---|
| 443 | # and use the x max and min of the user |
---|
| 444 | if not self._checkVal(self.xminFit, self.xmaxFit): |
---|
| 445 | return |
---|
| 446 | xminView = float(self.xminFit.GetValue()) |
---|
| 447 | xmaxView = float(self.xmaxFit.GetValue()) |
---|
| 448 | xmin = xminView |
---|
| 449 | xmax = xmaxView |
---|
| 450 | # Set the qmin and qmax in the panel that matches the |
---|
| 451 | # transformed min and max |
---|
| 452 | self.initXmin.SetValue(format_number(self.floatInvTransform(xmin))) |
---|
| 453 | self.initXmax.SetValue(format_number(self.floatInvTransform(xmax))) |
---|
| 454 | # Store the transformed values of view x, y,dy |
---|
| 455 | # in variables before the fit |
---|
| 456 | if self.yLabel.lower() == "log10(y)": |
---|
| 457 | if self.xLabel.lower() == "log10(x)": |
---|
| 458 | for i in range(len(self.x)): |
---|
| 459 | if self.x[i] >= math.log10(xmin): |
---|
| 460 | tempy.append(math.log10(self.y[i])) |
---|
| 461 | tempdy.append(transform.errToLogX(self.y[i], 0, self.dy[i], 0)) |
---|
| 462 | else: |
---|
| 463 | for i in range(len(self.y)): |
---|
| 464 | tempy.append(math.log10(self.y[i])) |
---|
| 465 | tempdy.append(transform.errToLogX(self.y[i], 0, self.dy[i], 0)) |
---|
| 466 | else: |
---|
| 467 | tempy = self.y |
---|
| 468 | tempdy = self.dy |
---|
| 469 | |
---|
| 470 | if self.xLabel.lower() == "log10(x)": |
---|
| 471 | for x_i in self.x: |
---|
| 472 | if x_i >= math.log10(xmin): |
---|
| 473 | tempx.append(math.log10(x_i)) |
---|
| 474 | else: |
---|
| 475 | tempx = self.x |
---|
| 476 | |
---|
| 477 | # Find the fitting parameters |
---|
| 478 | # Always use the same defaults, so that fit history |
---|
| 479 | # doesn't play a role! |
---|
| 480 | self.cstA = fittings.Parameter(self.model, 'A', self.default_A) |
---|
| 481 | self.cstB = fittings.Parameter(self.model, 'B', self.default_B) |
---|
| 482 | |
---|
| 483 | if self.xLabel.lower() == "log10(x)": |
---|
| 484 | tempdy = np.asarray(tempdy) |
---|
| 485 | tempdy[tempdy == 0] = 1 |
---|
| 486 | chisqr, out, cov = fittings.sasfit(self.model, |
---|
| 487 | [self.cstA, self.cstB], |
---|
| 488 | tempx, tempy, |
---|
| 489 | tempdy, |
---|
| 490 | math.log10(xmin), |
---|
| 491 | math.log10(xmax)) |
---|
| 492 | else: |
---|
| 493 | tempdy = np.asarray(tempdy) |
---|
| 494 | tempdy[tempdy == 0] = 1 |
---|
| 495 | chisqr, out, cov = fittings.sasfit(self.model, |
---|
| 496 | [self.cstA, self.cstB], |
---|
| 497 | tempx, tempy, tempdy, |
---|
| 498 | xminView, xmaxView) |
---|
| 499 | # Use chi2/dof |
---|
| 500 | if len(tempx) > 0: |
---|
| 501 | chisqr = chisqr / len(tempx) |
---|
| 502 | |
---|
| 503 | # Check that cov and out are iterable before displaying them |
---|
[235f514] | 504 | if cov is None: |
---|
[959eb01] | 505 | errA = 0.0 |
---|
| 506 | errB = 0.0 |
---|
| 507 | else: |
---|
| 508 | errA = math.sqrt(cov[0][0]) |
---|
| 509 | errB = math.sqrt(cov[1][1]) |
---|
[235f514] | 510 | if out is None: |
---|
[959eb01] | 511 | cstA = 0.0 |
---|
| 512 | cstB = 0.0 |
---|
| 513 | else: |
---|
| 514 | cstA = out[0] |
---|
| 515 | cstB = out[1] |
---|
| 516 | # Reset model with the right values of A and B |
---|
| 517 | self.model.setParam('A', float(cstA)) |
---|
| 518 | self.model.setParam('B', float(cstB)) |
---|
| 519 | |
---|
| 520 | tempx = [] |
---|
| 521 | tempy = [] |
---|
| 522 | y_model = 0.0 |
---|
| 523 | # load tempy with the minimum transformation |
---|
| 524 | |
---|
| 525 | if self.xLabel == "log10(x)": |
---|
| 526 | y_model = self.model.run(math.log10(xmin)) |
---|
| 527 | tempx.append(xmin) |
---|
| 528 | else: |
---|
| 529 | y_model = self.model.run(xminView) |
---|
| 530 | tempx.append(xminView) |
---|
| 531 | |
---|
| 532 | if self.yLabel == "log10(y)": |
---|
| 533 | tempy.append(math.pow(10, y_model)) |
---|
| 534 | else: |
---|
| 535 | tempy.append(y_model) |
---|
| 536 | |
---|
| 537 | # load tempy with the maximum transformation |
---|
| 538 | if self.xLabel == "log10(x)": |
---|
| 539 | y_model = self.model.run(math.log10(xmax)) |
---|
| 540 | tempx.append(xmax) |
---|
| 541 | else: |
---|
| 542 | y_model = self.model.run(xmaxView) |
---|
| 543 | tempx.append(xmaxView) |
---|
| 544 | |
---|
| 545 | if self.yLabel == "log10(y)": |
---|
| 546 | tempy.append(math.pow(10, y_model)) |
---|
| 547 | else: |
---|
| 548 | tempy.append(y_model) |
---|
| 549 | # Set the fit parameter display when FitDialog is opened again |
---|
| 550 | self.Avalue = cstA |
---|
| 551 | self.Bvalue = cstB |
---|
| 552 | self.ErrAvalue = errA |
---|
| 553 | self.ErrBvalue = errB |
---|
| 554 | self.Chivalue = chisqr |
---|
| 555 | self.push_data(tempx, tempy, xminView, xmaxView, |
---|
| 556 | xmin, xmax, self._ongetValues()) |
---|
| 557 | |
---|
| 558 | # Display the fitting value on the Fit Dialog |
---|
| 559 | self._onsetValues(cstA, cstB, errA, errB, chisqr) |
---|
| 560 | |
---|
| 561 | def _onsetValues(self, cstA, cstB, errA, errB, Chi): |
---|
| 562 | """ |
---|
| 563 | Display the value on fit Dialog |
---|
| 564 | """ |
---|
| 565 | rg = None |
---|
| 566 | _diam = None |
---|
| 567 | self.tcA.SetValue(format_number(cstA)) |
---|
| 568 | self.tcB.SetValue(format_number(cstB)) |
---|
| 569 | self.tcErrA.SetValue(format_number(errA)) |
---|
| 570 | self.tcErrB.SetValue(format_number(errB)) |
---|
| 571 | self.tcChi.SetValue(format_number(Chi)) |
---|
| 572 | if self.rg_on: |
---|
| 573 | if self.Rg_tctr.IsShown(): |
---|
| 574 | rg = np.sqrt(-3 * float(cstA)) |
---|
| 575 | value = format_number(rg) |
---|
| 576 | self.Rg_tctr.SetValue(value) |
---|
| 577 | if self.I0_tctr.IsShown(): |
---|
| 578 | val = np.exp(cstB) |
---|
| 579 | self.I0_tctr.SetValue(format_number(val)) |
---|
| 580 | if self.Rgerr_tctr.IsShown(): |
---|
[7432acb] | 581 | if rg is not None and rg != 0: |
---|
[959eb01] | 582 | value = format_number(3 * float(errA) / (2 * rg)) |
---|
| 583 | else: |
---|
| 584 | value = '' |
---|
| 585 | self.Rgerr_tctr.SetValue(value) |
---|
| 586 | if self.I0err_tctr.IsShown(): |
---|
| 587 | val = np.abs(np.exp(cstB) * errB) |
---|
| 588 | self.I0err_tctr.SetValue(format_number(val)) |
---|
| 589 | if self.Diameter_tctr.IsShown(): |
---|
| 590 | rg = np.sqrt(-2 * float(cstA)) |
---|
| 591 | _diam = 4 * np.sqrt(-float(cstA)) |
---|
| 592 | value = format_number(_diam) |
---|
| 593 | self.Diameter_tctr.SetValue(value) |
---|
| 594 | if self.Diametererr_tctr.IsShown(): |
---|
[7432acb] | 595 | if rg is not None and rg != 0: |
---|
[959eb01] | 596 | value = format_number(8 * float(errA) / _diam) |
---|
| 597 | else: |
---|
| 598 | value = '' |
---|
| 599 | self.Diametererr_tctr.SetValue(value) |
---|
| 600 | if self.RgQmin_tctr.IsShown(): |
---|
| 601 | value = format_number(rg * self.floatInvTransform(self.mini)) |
---|
| 602 | self.RgQmin_tctr.SetValue(value) |
---|
| 603 | if self.RgQmax_tctr.IsShown(): |
---|
| 604 | value = format_number(rg * self.floatInvTransform(self.maxi)) |
---|
| 605 | self.RgQmax_tctr.SetValue(value) |
---|
| 606 | |
---|
| 607 | def _ongetValues(self): |
---|
| 608 | """ |
---|
| 609 | Display the value on fit Dialog |
---|
| 610 | """ |
---|
| 611 | return self.Avalue, self.Bvalue, self.ErrAvalue, \ |
---|
| 612 | self.ErrBvalue, self.Chivalue |
---|
| 613 | |
---|
| 614 | def _checkVal(self, usermin, usermax): |
---|
| 615 | """ |
---|
| 616 | Ensure that fields parameter contains a min and a max value |
---|
| 617 | within x min and x max range |
---|
| 618 | """ |
---|
| 619 | self.mini = float(self.xminFit.GetValue()) |
---|
| 620 | self.maxi = float(self.xmaxFit.GetValue()) |
---|
| 621 | flag = True |
---|
| 622 | try: |
---|
| 623 | mini = float(usermin.GetValue()) |
---|
| 624 | maxi = float(usermax.GetValue()) |
---|
| 625 | if mini < maxi: |
---|
| 626 | usermin.SetBackgroundColour(wx.WHITE) |
---|
| 627 | usermin.Refresh() |
---|
| 628 | else: |
---|
| 629 | flag = False |
---|
| 630 | usermin.SetBackgroundColour("pink") |
---|
| 631 | usermin.Refresh() |
---|
| 632 | except: |
---|
| 633 | # Check for possible values entered |
---|
| 634 | flag = False |
---|
| 635 | usermin.SetBackgroundColour("pink") |
---|
| 636 | usermin.Refresh() |
---|
| 637 | |
---|
| 638 | return flag |
---|
| 639 | |
---|
| 640 | def floatForwardTransform(self, x): |
---|
| 641 | """ |
---|
| 642 | transform a float. |
---|
| 643 | """ |
---|
| 644 | # TODO: refactor this with proper object-oriented design |
---|
| 645 | # This code stinks. |
---|
| 646 | if self.xLabel == "x": |
---|
| 647 | return transform.toX(x) |
---|
| 648 | if self.xLabel == "x^(2)": |
---|
| 649 | return transform.toX2(x) |
---|
| 650 | if self.xLabel == "ln(x)": |
---|
| 651 | return transform.toLogX(x) |
---|
| 652 | if self.xLabel == "log10(x)": |
---|
| 653 | return math.log10(x) |
---|
| 654 | |
---|
| 655 | def floatTransform(self, x): |
---|
| 656 | """ |
---|
| 657 | transform a float.It is use to determine the x. |
---|
| 658 | View min and x.View max for values |
---|
| 659 | not in x |
---|
| 660 | """ |
---|
| 661 | # TODO: refactor this with proper object-oriented design |
---|
| 662 | # This code stinks. |
---|
| 663 | if self.xLabel == "x": |
---|
| 664 | return transform.toX(x) |
---|
| 665 | if self.xLabel == "x^(2)": |
---|
| 666 | return transform.toX2(x) |
---|
| 667 | if self.xLabel == "ln(x)": |
---|
| 668 | return transform.toLogX(x) |
---|
| 669 | if self.xLabel == "log10(x)": |
---|
| 670 | if x > 0: |
---|
| 671 | return x |
---|
| 672 | else: |
---|
[fa81e94] | 673 | raise ValueError("cannot compute log of a negative number") |
---|
[959eb01] | 674 | |
---|
| 675 | def floatInvTransform(self, x): |
---|
| 676 | """ |
---|
| 677 | transform a float.It is used to determine the x.View min and x.View |
---|
| 678 | max for values not in x. Also used to properly calculate RgQmin, |
---|
| 679 | RgQmax and to update qmin and qmax in the linear range boxes on the |
---|
| 680 | panel. |
---|
| 681 | |
---|
| 682 | """ |
---|
| 683 | # TODO: refactor this. This is just a hack to make the |
---|
| 684 | # functionality work without rewritting the whole code |
---|
| 685 | # with good design (which really should be done...). |
---|
| 686 | if self.xLabel == "x": |
---|
| 687 | return x |
---|
| 688 | elif self.xLabel == "x^(2)": |
---|
| 689 | return math.sqrt(x) |
---|
| 690 | elif self.xLabel == "x^(4)": |
---|
| 691 | return math.sqrt(math.sqrt(x)) |
---|
| 692 | elif self.xLabel == "log10(x)": |
---|
| 693 | return math.pow(10, x) |
---|
| 694 | elif self.xLabel == "ln(x)": |
---|
| 695 | return math.exp(x) |
---|
| 696 | elif self.xLabel == "log10(x^(4))": |
---|
| 697 | return math.sqrt(math.sqrt(math.pow(10, x))) |
---|
| 698 | return x |
---|
| 699 | |
---|
| 700 | def checkFitValues(self, item): |
---|
| 701 | """ |
---|
| 702 | Check the validity of input values |
---|
| 703 | """ |
---|
| 704 | flag = True |
---|
| 705 | value = item.GetValue() |
---|
| 706 | # Check for possible values entered |
---|
| 707 | if self.xLabel == "log10(x)": |
---|
| 708 | if float(value) > 0: |
---|
| 709 | item.SetBackgroundColour(wx.WHITE) |
---|
| 710 | item.Refresh() |
---|
| 711 | else: |
---|
| 712 | flag = False |
---|
| 713 | item.SetBackgroundColour("pink") |
---|
| 714 | item.Refresh() |
---|
| 715 | return flag |
---|
| 716 | |
---|
| 717 | def setFitRange(self, xmin, xmax, xminTrans, xmaxTrans): |
---|
| 718 | """ |
---|
| 719 | Set fit parameters |
---|
| 720 | """ |
---|
| 721 | self.xminFit.SetValue(format_number(xmin)) |
---|
| 722 | self.xmaxFit.SetValue(format_number(xmax)) |
---|
| 723 | |
---|
| 724 | def set_fit_region(self, xmin, xmax): |
---|
| 725 | """ |
---|
| 726 | Set the fit region |
---|
| 727 | :param xmin: minimum x-value to be included in fit |
---|
| 728 | :param xmax: maximum x-value to be included in fit |
---|
| 729 | """ |
---|
| 730 | # Check values |
---|
| 731 | try: |
---|
| 732 | float(xmin) |
---|
| 733 | float(xmax) |
---|
| 734 | except: |
---|
| 735 | msg = "LinearFit.set_fit_region: fit range must be floats" |
---|
[fa81e94] | 736 | raise ValueError(msg) |
---|
[959eb01] | 737 | self.xminFit.SetValue(format_number(xmin)) |
---|
| 738 | self.xmaxFit.SetValue(format_number(xmax)) |
---|
| 739 | |
---|
| 740 | |
---|
| 741 | class MyApp(wx.App): |
---|
| 742 | """ |
---|
| 743 | Test application |
---|
| 744 | """ |
---|
| 745 | def OnInit(self): |
---|
| 746 | """ |
---|
| 747 | Test application initialization |
---|
| 748 | """ |
---|
| 749 | wx.InitAllImageHandlers() |
---|
| 750 | plot = Theory1D([], []) |
---|
| 751 | dialog = LinearFit(parent=None, plottable=plot, |
---|
| 752 | push_data=self.onFitDisplay, |
---|
| 753 | transform=self.returnTrans, |
---|
| 754 | title='Linear Fit') |
---|
| 755 | if dialog.ShowModal() == wx.ID_OK: |
---|
| 756 | pass |
---|
| 757 | dialog.Destroy() |
---|
| 758 | return 1 |
---|
| 759 | |
---|
| 760 | def onFitDisplay(self, tempx, tempy, xminView, xmaxView, xmin, xmax, func): |
---|
| 761 | """ |
---|
| 762 | Test application dummy method |
---|
| 763 | """ |
---|
| 764 | pass |
---|
| 765 | |
---|
| 766 | def returnTrans(self): |
---|
| 767 | """ |
---|
| 768 | Test application dummy method |
---|
| 769 | """ |
---|
| 770 | return '', '', 0, 0, 0, 0, 0 |
---|