Ignore:
Timestamp:
06/13/13 18:12:28 (10 months ago)
Author:
Ales Erjavec <ales.erjavec@…>
Branch:
default
Message:

Updated Widget development tutorial.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • docs/extend-widgets/rst/OWLearningCurveC.py

    r11085 r11593  
    66""" 
    77 
     8import Orange 
     9 
    810from OWWidget import * 
    911from OWColorPalette import ColorPixmap 
    10 import OWGUI, orngTest, orngStat 
    1112from OWGraph import * 
     13 
     14import OWGUI 
    1215 
    1316import warnings 
     
    2023        OWWidget.__init__(self, parent, signalManager, 'LearningCurveC') 
    2124 
    22         self.inputs = [("Data", ExampleTable, self.dataset), 
    23                        ("Learner", orange.Learner, self.learner, Multiple)] 
     25        self.inputs = [("Data", Orange.data.Table, self.dataset), 
     26                       ("Learner", Orange.classification.Learner, 
     27                        self.learner, Multiple)] 
    2428 
    2529        self.folds = 5     # cross validation folds 
     
    3034        self.graphDrawLines = 1 # draw lines between points in the graph 
    3135        self.graphShowGrid = 1  # show gridlines in the graph 
    32         self.selectedLearners = []  
     36        self.selectedLearners = [] 
     37 
    3338        self.loadSettings() 
    3439 
    35         warnings.filterwarnings("ignore", ".*builtin attribute.*", orange.AttributeWarning) 
    36  
    37         self.setCurvePoints() # sets self.curvePoints, self.steps equidistantpoints from 1/self.steps to 1 
    38         self.scoring = [("Classification Accuracy", orngStat.CA), ("AUC", orngStat.AUC), ("BrierScore", orngStat.BrierScore), ("Information Score", orngStat.IS), ("Sensitivity", orngStat.sens), ("Specificity", orngStat.spec)] 
     40        warnings.filterwarnings("ignore", ".*builtin attribute.*", Orange.core.AttributeWarning) 
     41 
     42        self.setCurvePoints() # sets self.curvePoints, self.steps equidistant points from 1/self.steps to 1 
     43        self.scoring = [("Classification Accuracy", 
     44                         Orange.evaluation.scoring.CA), 
     45                        ("AUC", Orange.evaluation.scoring.AUC), 
     46                        ("BrierScore", Orange.evaluation.scoring.Brier_score), 
     47                        ("Information Score", Orange.evaluation.scoring.IS), 
     48                        ("Sensitivity", Orange.evaluation.scoring.sens), 
     49                        ("Specificity", Orange.evaluation.scoring.spec)] 
    3950        self.learners = [] # list of current learners from input channel, tuples (id, learner) 
    4051        self.data = None   # data on which to construct the learning curve 
     
    4960        ## class selection (classQLB) 
    5061        OWGUI.separator(self.controlArea) 
     62 
     63        # ~SPHINX start color cb~ 
    5164        self.cbox = OWGUI.widgetBox(self.controlArea, "Learners") 
    52         self.llb = OWGUI.listBox(self.cbox, self, "selectedLearners", selectionMode=QListWidget.MultiSelection, callback=self.learnerSelectionChanged) 
     65        self.llb = OWGUI.listBox(self.cbox, self, "selectedLearners", 
     66                                 selectionMode=QListWidget.MultiSelection, 
     67                                 callback=self.learnerSelectionChanged) 
    5368         
    5469        self.llb.setMinimumHeight(50) 
    5570        self.blockSelectionChanges = 0 
     71        # ~SPHINX end color cb~ 
    5672 
    5773        OWGUI.separator(self.controlArea) 
     74 
    5875        box = OWGUI.widgetBox(self.controlArea, "Evaluation Scores") 
    5976        scoringNames = [x[0] for x in self.scoring] 
     
    6279 
    6380        OWGUI.separator(self.controlArea) 
     81 
    6482        box = OWGUI.widgetBox(self.controlArea, "Options") 
    6583        OWGUI.spin(box, self, 'folds', 2, 100, step=1, 
     
    6886        OWGUI.spin(box, self, 'steps', 2, 100, step=1, 
    6987                   label='Learning curve points:  ', 
    70                    callback=[self.setCurvePoints, lambda: self.computeCurve(self.commitOnChange)]) 
     88                   callback=[self.setCurvePoints, 
     89                             lambda: self.computeCurve(self.commitOnChange)]) 
    7190 
    7291        OWGUI.checkBox(box, self, 'commitOnChange', 'Apply setting on any change') 
    73         self.commitBtn = OWGUI.button(box, self, "Apply Setting", callback=self.computeCurve, disabled=1) 
    74  
     92        self.commitBtn = OWGUI.button(box, self, "Apply Setting", 
     93                                      callback=self.computeCurve, disabled=1) 
     94 
     95        # ~SPHINX start main area tabs~ 
    7596        # start of content (right) area 
    7697        tabs = OWGUI.tabWidget(self.mainArea) 
    7798 
    78         # graph widget 
     99        # graph tab 
    79100        tab = OWGUI.createTabPage(tabs, "Graph") 
    80101        self.graph = OWGraph(tab) 
     
    84105        self.setGraphGrid() 
    85106 
    86         # table widget 
     107        # table tab 
    87108        tab = OWGUI.createTabPage(tabs, "Table") 
    88109        self.table = OWGUI.table(tab, selectionMode=QTableWidget.NoSelection) 
     110        # ~SPHINX end main area tabs~ 
    89111 
    90112        self.resize(550,200) 
     
    187209    def getLearningCurve(self, learners): 
    188210        pb = OWGUI.ProgressBar(self, iterations=self.steps*self.folds) 
    189         curve = orngTest.learningCurveN(learners, self.data, folds=self.folds, proportions=self.curvePoints, callback=pb.advance) 
     211        curve = Orange.evaluation.testing.learning_curve_n( 
     212            learners, self.data, folds=self.folds, 
     213            proportions=self.curvePoints, 
     214            callback=pb.advance) 
     215 
    190216        pb.finish() 
    191217        return curve 
     
    250276        else: 
    251277            curve.setStyle(QwtPlotCurve.NoCurve) 
    252         curve.setSymbol(QwtSymbol(QwtSymbol.Ellipse, \ 
    253           QBrush(QColor(0,0,0)), QPen(QColor(0,0,0)), 
    254           QSize(self.graphPointSize, self.graphPointSize))) 
     278 
     279        curve.setSymbol( 
     280            QwtSymbol(QwtSymbol.Ellipse, 
     281                      QBrush(QColor(0,0,0)), QPen(QColor(0,0,0)), 
     282                      QSize(self.graphPointSize, self.graphPointSize))) 
     283 
    255284        curve.setPen(QPen(learner.color, 5)) 
    256285 
    257286    def drawLearningCurve(self, learner): 
    258         if not self.data: return 
    259         curve = self.graph.addCurve(learner.name, xData=self.curvePoints, yData=learner.score, autoScale=True) 
     287        if not self.data: 
     288            return 
     289        curve = self.graph.addCurve( 
     290            learner.name, 
     291            xData=self.curvePoints, 
     292            yData=learner.score, 
     293            autoScale=True) 
    260294         
    261295        learner.curve = curve 
     
    268302            self.drawLearningCurve(l[1]) 
    269303 
    270 ############################################################################## 
    271 # Test the widget, run from prompt 
    272304 
    273305if __name__=="__main__": 
     
    276308    ow.show() 
    277309 
    278     l1 = orange.BayesLearner() 
     310    l1 = Orange.classification.bayes.NaiveLearner() 
    279311    l1.name = 'Naive Bayes' 
    280312    ow.learner(l1, 1) 
    281313 
    282     data = orange.ExampleTable('iris.tab') 
     314    data = Orange.data.Table('iris.tab') 
    283315    ow.dataset(data) 
    284316 
    285     l2 = orange.BayesLearner() 
     317    l2 = Orange.classification.bayes.NaiveLearner() 
    286318    l2.name = 'Naive Bayes (m=10)' 
    287     l2.estimatorConstructor = orange.ProbabilityEstimatorConstructor_m(m=10) 
    288     l2.conditionalEstimatorConstructor = orange.ConditionalProbabilityEstimatorConstructor_ByRows(estimatorConstructor = orange.ProbabilityEstimatorConstructor_m(m=10)) 
    289  
    290     l3 = orange.kNNLearner(name="k-NN") 
     319    l2.estimatorConstructor = Orange.statistics.estimate.M(m=10) 
     320    l2.conditionalEstimatorConstructor = Orange.statistics.estimate.ConditionalByRows(estimatorConstructor = Orange.statistics.estimate.M(m=10)) 
     321 
     322    l3 = Orange.classification.knn.kNNLearner(name="k-NN") 
    291323    ow.learner(l3, 3) 
    292324 
    293     import orngTree 
    294     l4 = orngTree.TreeLearner(minSubset=2) 
     325    l4 = Orange.classification.tree.TreeLearner(minSubset=2) 
    295326    l4.name = "Decision Tree" 
    296327    ow.learner(l4, 4) 
     
    299330#    ow.learner(None, 2) 
    300331#    ow.learner(None, 4) 
    301      
    302  
    303332 
    304333    appl.exec_() 
Note: See TracChangeset for help on using the changeset viewer.