Ejemplo n.º 1
0
    def calculate(self, calcID=None, markerOnly=False):
        values = self.getValues()
        sFit = Fit.getInstance()
        oldLineData = self.lineData
        self.lineData = {}
        # don't clear lineColor so that removing and re-adding a pairing re-uses the previous color

        for fitID, fit in self.fits.iteritems():
            for tgtID, tgt in (self.tgts.iteritems() if self.tgts else
                               ((None, None), )):
                lineID = (fitID, tgtID)

                # if we're only recalculating for a specific fit, recycle plotted data for lines that don't involve that fit
                if (lineID in oldLineData) and (calcID is not None):
                    if ((type(calcID) != type(fitID)) or
                        (calcID != fitID)) and ((type(calcID) != type(tgtID))
                                                or (calcID != tgtID)):
                        self.lineData[lineID] = oldLineData[lineID]
                        continue

                color = self.lineColor.get(lineID)
                if color is None:
                    color = hsv_to_rgb(
                        hsl_to_hsv(self.COLORS[self.nextColor][:3]))
                    self.nextColor = (self.nextColor + 1) % len(self.COLORS)
                    self.lineColor[lineID] = color
                if tgtID is None:
                    tgt = None
                elif tgtID == "":
                    tgt = self.dummyTargetProfile
                elif type(tgtID) is int:
                    tgt = sFit.getFit(tgtID)
                else:
                    tgt = svc_TargetResists.getTargetResists(tgtID)

                try:
                    if markerOnly and (lineID in oldLineData):
                        self.lineData[lineID] = oldLineData[lineID]
                    else:
                        x_success, y_status = self.currentView.getPoints(
                            values, fit, tgt)
                        if not x_success:
                            # TODO: Add a pwetty statys bar to report errors with
                            self.SetStatusText(y_status)
                            return False
                        self.lineData[lineID] = [x_success, y_status, None]

                    markerY = self.currentView.getPoint(
                        values,
                        (self.markerX, ), fit, tgt) if self.markerX else None
                    if self.markerX and markerY is None:
                        x = bisect.bisect(self.lineData[lineID][0],
                                          self.markerX)
                        if x > 0 and x < (len(self.lineData[lineID][1]) - 1):
                            dx = (self.lineData[lineID][0][x] -
                                  self.lineData[lineID][0][x - 1])
                            dy = (self.lineData[lineID][1][x] -
                                  self.lineData[lineID][1][x - 1])
                            markerY = self.lineData[lineID][1][x - 1] + dy * (
                                self.markerX -
                                self.lineData[lineID][0][x - 1]) / dx
                    self.lineData[lineID][2] = markerY

                except ValueError as e:
                    msg = u"Error calculating fit '{}' vs target '{}'".format(
                        fit.name, (tgt.name if tgt else "N/A"))
                    self.SetStatusText(msg)
                    pyfalog.warning(msg)
                    pyfalog.warning(str(e))
                    return False
            # for tgtID
        # for fitID

        self.plotPanel.labelX.SetLabel(
            self.currentView.getVariableLabels(values)[0])
        self.SetStatusText("")
        return True
Ejemplo n.º 2
0
    def selectView(self, index):
        if self.currentViewIndex == index:
            return
        self.currentViewIndex = index
        self.currentView = self.graphSelection.GetClientData(index)
        self.graphSelection.SetSelection(index)

        self.controlPanel.DestroyChildren()
        self.fields.clear()

        viewPanel = self.currentView.getControlPanel(self.controlPanel,
                                                     self.onFieldChanged)
        if not viewPanel:
            fields = self.currentView.getFields()
            if fields:
                viewPanel = wx.Panel(self.controlPanel)
                viewSizer = wx.FlexGridSizer(0, 4)
                viewSizer.AddGrowableCol(1)
                viewPanel.SetSizer(viewSizer)

                # Setup textboxes
                icons = self.currentView.getIcons()
                labels = self.currentView.getLabels()
                for field in sorted(fields.keys()):
                    textBox = wx.TextCtrl(viewPanel)
                    self.fields[field] = textBox
                    textBox.Bind(wx.EVT_TEXT_ENTER, self.onFieldChanged)
                    textBox.Bind(wx.EVT_KILL_FOCUS, self.onFieldChanged)
                    defaultVal = fields[field]
                    if defaultVal is not None:
                        if not isinstance(defaultVal, basestring):
                            defaultVal = "%g" % (defaultVal, )
                        textBox.ChangeValue(defaultVal)
                    viewSizer.Add(textBox,
                                  proportion=1,
                                  flag=wx.ALIGN_CENTER_VERTICAL | wx.ALL,
                                  border=3)

                    imgLabelSizer = wx.BoxSizer(wx.HORIZONTAL)

                    icon = icons.get(field) if icons else None
                    if icon is not None:
                        static = wx.StaticBitmap(viewPanel, bitmap=icon)
                        imgLabelSizer.Add(static,
                                          proportion=0,
                                          flag=wx.ALL
                                          | wx.ALIGN_CENTER_VERTICAL,
                                          border=1)

                    label = labels.get(field, field) if labels else field
                    imgLabelSizer.Add(wx.StaticText(viewPanel, label=label),
                                      proportion=0,
                                      flag=wx.ALIGN_CENTER_VERTICAL | wx.LEFT
                                      | wx.RIGHT,
                                      border=3)

                    viewSizer.Add(imgLabelSizer,
                                  proportion=0,
                                  flag=wx.ALIGN_CENTER_VERTICAL)

        if viewPanel is not None:
            self.controlSizer.Add(viewPanel, 0, wx.EXPAND)

        self.tgts.clear()
        if self.currentView.allowTargetResists():
            for fitID, fit in self.fits.iteritems():
                tgtID = fit.targetResists.name if fit.targetResists else ""
                if tgtID not in self.tgts:
                    self.tgts[tgtID] = svc_TargetResists.getTargetResists(
                        tgtID) if tgtID else self.dummyTargetProfile
            self.tgtList.Show()
        elif self.currentView.allowTargetFits():
            self.tgtList.Show()
        else:
            self.tgtList.Hide()
        self.tgtList.fitList.update(self.tgts.values())

        self.nextColor = 0
        self.markerX = 0
        self.selected = None

        self.Layout()
        self.calculate()
        self.draw()