⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 owdistributions.py

📁 orange源码 数据挖掘技术
💻 PY
📖 第 1 页 / 共 2 页
字号:
                        mps.append(ps[self.targetValue] - cis[self.targetValue])
                cn += 1.0

            ## (re)set the curves
            if self.variableContinuous:
                newSymbol = QwtSymbol(QwtSymbol.None, QBrush(Qt.color0), QPen(Qt.black, 2), QSize(0,0))
            else:
                newSymbol = QwtSymbol(QwtSymbol.Diamond, QBrush(Qt.color0), QPen(Qt.black, 2), QSize(7,7))

            self.setCurveData(self.probCurveKey, xs, mps)
            self.setCurveSymbol(self.probCurveKey, newSymbol)

            if self.variableContinuous:
                self.setCurveStyle(self.probCurveKey, QwtCurve.Lines)
                if self.showConfidenceIntervals:
                    self.setCurveData(self.probCurveUpperCIKey, xs, ups)
                    self.setCurveData(self.probCurveLowerCIKey, xs, lps)
            else:
                if self.showConfidenceIntervals:
                    self.setCurveStyle(self.probCurveKey, QwtCurve.UserCurve)
                else:
                    self.setCurveStyle(self.probCurveKey, QwtCurve.Dots)
        else:
            self.enableYRaxis(0)
            self.setShowYRaxisTitle(0)

        self.curve(self.probCurveKey).setEnabled(self.showProbabilities)
        self.curve(self.probCurveUpperCIKey).setEnabled(self.showConfidenceIntervals and self.showProbabilities)
        self.curve(self.probCurveLowerCIKey).setEnabled(self.showConfidenceIntervals and self.showProbabilities)
        self.curve(self.probCurveKey).itemChanged()
        self.curve(self.probCurveUpperCIKey).itemChanged()
        self.curve(self.probCurveLowerCIKey).itemChanged()
        self.repaint()

        
        
class OWDistributions(OWWidget):
    settingsList = ["numberOfBars", "barSize", "showContinuousClassGraph", "showProbabilities", "showConfidenceIntervals", "smoothLines", "lineWidth", "showMainTitle", "showXaxisTitle", "showYaxisTitle", "showYPaxisTitle"]
    contextHandlers = {"": DomainContextHandler("", ["attribute", "targetValue", "visibleOutcomes", "mainTitle", "xaxisTitle", "yaxisTitle", "yPaxisTitle"], matchValues=DomainContextHandler.MatchValuesClass)}

    def __init__(self,parent=None, signalManager = None):
        "Constructor"
        OWWidget.__init__(self, parent, signalManager, "&Distributions", TRUE)
        # settings
        self.numberOfBars = 5
        self.barSize = 50
        self.showContinuousClassGraph=1
        self.showProbabilities = 1
        self.showConfidenceIntervals = 0
        self.smoothLines = 0
        self.lineWidth = 1
        self.showMainTitle = 0
        self.showXaxisTitle = 1
        self.showYaxisTitle = 1
        self.showYPaxisTitle = 1

        self.attribute = ""
        self.targetValue = 0
        self.visibleOutcomes = []
        self.outcomes = []

        # tmp values
        self.mainTitle = ""
        self.xaxisTitle = ""
        self.yaxisTitle = "frequency"
        self.yPaxisTitle = ""

        self.loadSettings()

        # GUI
        self.tabs = QTabWidget(self.space, 'tabWidget')
        self.GeneralTab = QVGroupBox(self)
        self.SettingsTab = QVGroupBox(self, "Settings")
        self.tabs.insertTab(self.GeneralTab, "General")
        self.tabs.insertTab(self.SettingsTab, "Settings")
        
        self.box = QVBoxLayout(self.mainArea)
        self.graph = OWDistributionGraph(self, self.mainArea)
        self.graph.setYRlabels(None)
        self.graph.setAxisScale(QwtPlot.yRight, 0.0, 1.0, 0.1)
        self.box.addWidget(self.graph)
        self.connect(self.graphButton, SIGNAL("clicked()"), self.graph.saveToFile)

        # inputs
        # data and graph temp variables
        self.inputs = [("Classified Examples", ExampleTableWithClass, self.cdata, Default)]
        
        self.data = None
        self.outcomenames = []
        self.probGraphValues = []
       

        # GUI connections
        # options dialog connections
        self.numberOfBarsSlider = OWGUI.hSlider(self.SettingsTab, self, 'numberOfBars', box='Number of Bars', minValue=5, maxValue=60, step=5, callback=self.setNumberOfBars, ticks=5)
        self.numberOfBarsSlider.setTracking(0) # no change until the user stop dragging the slider

        self.barSizeSlider = OWGUI.hSlider(self.SettingsTab, self, 'barSize', box=' Bar Size ', minValue=30, maxValue=100, step=5, callback=self.setBarSize, ticks=10)

        box = OWGUI.widgetBox(self.SettingsTab, " General graph settings ")
        box.setMinimumWidth(180)
        box2 = OWGUI.widgetBox(box, orientation = "horizontal")
        OWGUI.checkBox(box2, self, 'showMainTitle', 'Show Main Title', callback = self.setShowMainTitle)
        OWGUI.lineEdit(box2, self, 'mainTitle', callback = self.setMainTitle)

        box3 = OWGUI.widgetBox(box, orientation = "horizontal")
        OWGUI.checkBox(box3, self, 'showXaxisTitle', 'Show X axis title', callback = self.setShowXaxisTitle)
        OWGUI.lineEdit(box3, self, 'xaxisTitle', callback = self.setXaxisTitle)

        box4 = OWGUI.widgetBox(box, orientation = "horizontal")
        OWGUI.checkBox(box4, self, 'showYaxisTitle', 'Show Y axis title', callback = self.setShowYaxisTitle)
        OWGUI.lineEdit(box4, self, 'yaxisTitle', callback = self.setYaxisTitle)

        OWGUI.checkBox(box, self, 'showContinuousClassGraph', 'Show continuous class graph', callback=self.setShowContinuousClassGraph)
        
        box5 = OWGUI.widgetBox(self.SettingsTab, " Probability graph ")
        self.showProb = OWGUI.checkBox(box5, self, 'showProbabilities', ' Show Probabilities ', callback = self.setShowProbabilities)

        box6 = OWGUI.widgetBox(box5, orientation = "horizontal")

        self.showYPaxisCheck = OWGUI.checkBox(box6, self, 'showYPaxisTitle', 'Show Axis Title', callback = self.setShowYPaxisTitle)
        self.yPaxisEdit = OWGUI.lineEdit(box6, self, 'yPaxisTitle', callback = self.setYPaxisTitle)
        self.confIntCheck = OWGUI.checkBox(box5, self, 'showConfidenceIntervals', 'Show Confidence Intervals', callback = self.setShowConfidenceIntervals)
        self.showProb.disables = [self.showYPaxisCheck, self.yPaxisEdit, self.confIntCheck]
        self.showProb.makeConsistent()
        
        OWGUI.checkBox(box5, self, 'smoothLines', 'Smooth probability lines', callback = self.setSmoothLines)

        self.barSizeSlider = OWGUI.hSlider(box5, self, 'lineWidth', box=' Line width ', minValue=1, maxValue=9, step=1, callback=self.setLineWidth, ticks=1)
        
        #add controls to self.controlArea widget
        self.variablesQCB = OWGUI.comboBox(self.GeneralTab, self, "attribute", box="Attribute", valueType = str, sendSelectedValue = True, callback=self.setVariable)
        self.targetQCB = OWGUI.comboBox(self.GeneralTab, self, "targetValue", box="Target value", valueType=int, callback=self.setTarget)
        self.outcomesQLB = OWGUI.listBox(self.GeneralTab, self, "visibleOutcomes", "outcomes", "Outcomes", selectionMode = QListBox.Multi, callback = self.outcomeSelectionChange)

        self.icons = self.createAttributeIconDict()
        
        self.activateLoadedSettings()

    def activateLoadedSettings(self):
        self.graph.numberOfBars = self.numberOfBars
        self.graph.barSize = self.barSize
        self.graph.setShowMainTitle(self.showMainTitle)
        self.graph.setShowXaxisTitle(self.showXaxisTitle)
        self.graph.setShowYLaxisTitle(self.showYaxisTitle)
        self.graph.setShowYRaxisTitle(self.showYPaxisTitle)
        self.graph.setMainTitle(self.mainTitle)
        self.graph.setXaxisTitle(self.xaxisTitle)
        self.graph.setYLaxisTitle(self.yaxisTitle)
        self.graph.setYRaxisTitle(self.yPaxisTitle)
        self.graph.showProbabilities = self.showProbabilities
        self.graph.showConfidenceIntervals = self.showConfidenceIntervals
        self.graph.smoothLines = self.smoothLines
        self.graph.lineWidth = self.lineWidth
        #self.graph.variableContinuous = self.VariableContinuous
        self.graph.targetValue = self.targetValue

    def setShowMainTitle(self):
        self.graph.setShowMainTitle(self.showMainTitle)

    def setMainTitle(self):
        self.graph.setMainTitle(self.mainTitle)

    def setShowXaxisTitle(self):
        self.graph.setShowXaxisTitle(self.showXaxisTitle)

    def setXaxisTitle(self):
        self.graph.setXaxisTitle(self.xaxisTitle)

    def setShowYaxisTitle(self):
        self.graph.setShowYLaxisTitle(self.showYaxisTitle )

    def setYaxisTitle(self):
        self.graph.setYLaxisTitle(self.yaxisTitle )

    def setShowYPaxisTitle(self):
        self.graph.setShowYRaxisTitle(self.showYPaxisTitle)

    def setYPaxisTitle(self):
        self.graph.setYRaxisTitle(self.yPaxisTitle)

    def setBarSize(self):
        self.graph.setBarSize(self.barSize )

    # Sets whether the probabilities are drawn or not
    def setShowProbabilities(self):
        self.graph.showProbabilities = self.showProbabilities 
        self.graph.refreshProbGraph()
        #self.graph.replot()
        self.repaint()
        
    def setShowContinuousClassGraph(self):
        self.graph.showContinuousClassGraph=self.showContinuousClassGraph
        self.graph.refreshPureVisibleOutcomes()

    #Sets the number of bars for histograms of continuous variables 
    def setNumberOfBars(self):
        self.graph.setNumberOfBars(self.numberOfBars)
       
    # sets the line smoothing on and off
    def setSmoothLines(self):
        #self.SmoothLines = n
        #self.updateGraphSettings()
        pass

    # Sets the line thickness for probability 
    def setLineWidth(self): 
        #self.LineWidth = n
        #self.updateGraphSettings()
        pass

    # Sets whether the confidence intervals are shown
    def setShowConfidenceIntervals(self):
        self.graph.showConfidenceIntervals = self.showConfidenceIntervals
        #self.updateGraphSettings()
        self.graph.refreshProbGraph()
        #self.graph.replot()

    def setTarget(self, *t):
        if t:
            self.targetValue = t[0]
        self.graph.setTargetValue(self.targetValue)

    def target(self, targetValue):
        self.targetValue = targetValue
        #self.updateGraphSettings()
        self.graph.refreshProbGraph()
        outcomeName = ""
        if self.data and self.data.domain.classVar:
            self.setYPaxisTitle("P( " + self.data.domain.classVar.name + " = " + targetValue + " )")

    def cdata(self, data):
        self.closeContext()
        
        if data == None:
            self.variablesQCB.clear()
            self.targetQCB.clear()
            self.outcomes = []

            self.graph.setXlabels(None)
            self.graph.setYLlabels(None)
            self.graph.setShowYRaxisTitle(0)
            self.graph.setVisibleOutcomes(None)
            self.graph.setData(None, None)
            self.data = None
            return

        self.dataHasClass = bool(data.domain.classVar)
        if self.dataHasClass:
            self.dataHasDiscreteClass = data.domain.classVar.varType != orange.VarTypes.Continuous

        sameDomain = data and self.data and data.domain == self.data.domain

        self.data = orange.Preprocessor_dropMissingClasses(data)	

        if sameDomain:
            self.graph.setData(self.data, self.graph.attributeName)

        else:
            self.graph.setData(None, None)
            self.graph.setTargetValue(None)
            self.graph.setVisibleOutcomes(None)

            # set targets
            self.targetQCB.clear()
            if self.data.domain.classVar and self.data.domain.classVar.varType == orange.VarTypes.Discrete:
                for val in self.data.domain.classVar.values:
                    self.targetQCB.insertItem(val)
                self.setTarget(0)

            # set variable combo box
            self.variablesQCB.clear()
            for attr in self.data.domain.attributes:
                self.variablesQCB.insertItem(self.icons[attr.varType], attr.name)

            if self.data and len(self.data.domain.attributes) > 0:
                self.graph.setData(self.data, self.data.domain.attributes[0].name) # pick first variable
                self.attribute = self.data.domain[0].name
                self.setVariable()

            self.targetValue = 0
            
            self.openContext("", self.data)               

            if self.data and len(self.data.domain.attributes) > 0:
                self.setVariable()

            if self.dataHasClass and self.dataHasDiscreteClass:
                self.targetValue = 0  # self.data.domain.classVar.values.index(str(targetVal))
                self.graph.setTargetValue(self.targetValue) #str(self.data.domain.classVar.values[0])) # pick first target

                # set outcomes
                self.setOutcomeNames(self.data.domain.classVar.values.native())
            else:
               self.setOutcomeNames([])

        for f in [self.setMainTitle, self.setXaxisTitle, self.setYaxisTitle, self.setYPaxisTitle, self.outcomeSelectionChange]:
            f()


    def setOutcomeNames(self, list):
        "Sets the outcome target names."
        colors = ColorPaletteGenerator()
        self.outcomes = [(l, ColorPixmap(c)) for l, c in zip(list, colors)]
        self.visibleOutcomes = range(len(list))

    def outcomeSelectionChange(self):
        "Sets which outcome values are represented in the graph."
        "Reacts to changes in outcome selection."
        self.graph.visibleOutcomes = [i in self.visibleOutcomes for i in range(self.outcomesQLB.numRows())]
        self.graph.refreshVisibleOutcomes()
        #self.graph.replot()
        #self.repaint()

    def setVariable(self):
        self.graph.setVariable(self.attribute)
        self.graph.refreshVisibleOutcomes()
        self.xaxisTitle = str(self.attribute)
        self.repaint()
    

if __name__ == "__main__":
    a = QApplication(sys.argv)
    owd = OWDistributions()
    a.setMainWidget(owd)
    owd.show()
    data=orange.ExampleTable("../../doc/datasets/iris.tab")
    owd.cdata(data)
    a.exec_loop()
    owd.saveSettings()
    orange.VarTypes.Continuous

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -