Commits

Aleš Erjavec committed 99bec0d

More fixes to widget development manual code snippets.

Comments (0)

Files changed (9)

docs/extend-widgets/rst/OWDataSamplerB.py

 from OWWidget import *
 import OWGUI
 
+# [start-snippet-1]
 class OWDataSamplerB(OWWidget):
     settingsList = ['proportion', 'commitOnChange']
+# [end-snippet-1]
     def __init__(self, parent=None, signalManager=None):
         OWWidget.__init__(self, parent, signalManager)
 
-        self.inputs = [("Data", Orange.data.Table, self.data)]
+        self.inputs = [("Data", Orange.data.Table, self.set_data)]
         self.outputs = [("Sampled Data", Orange.data.Table)]
-
+# [start-snippet-2]
         self.proportion = 50
         self.commitOnChange = 0
         self.loadSettings()
-
+# [end-snippet-2]
         # GUI
+# [start-snippet-3]
         box = OWGUI.widgetBox(self.controlArea, "Info")
         self.infoa = OWGUI.widgetLabel(box, 'No data on input yet, waiting to get something.')
         self.infob = OWGUI.widgetLabel(box, '')
         OWGUI.checkBox(self.optionsBox, self, 'commitOnChange', 'Commit data on selection change')
         OWGUI.button(self.optionsBox, self, "Commit", callback=self.commit)
         self.optionsBox.setDisabled(1)
-
+# [end-snippet-3]
         self.resize(100,50)
 
-    def data(self, dataset):
-        if dataset:
+# [start-snippet-4]
+    def set_data(self, dataset):
+        if dataset is not None:
             self.dataset = dataset
             self.infoa.setText('%d instances in input data set' % len(dataset))
             self.optionsBox.setDisabled(0)
     def checkCommit(self):
         if self.commitOnChange:
             self.commit()
-
+# [end-snippet-4]
 
 if __name__=="__main__":
     appl = QApplication(sys.argv)
     ow = OWDataSamplerB()
     ow.show()
     dataset = Orange.data.Table('iris.tab')
-    ow.data(dataset)
+    ow.set_data(dataset)
     appl.exec_()

docs/extend-widgets/rst/OWDataSamplerC.py

     def __init__(self, parent=None, signalManager=None):
         OWWidget.__init__(self, parent, signalManager, 'SampleDataC')
         
-        self.inputs = [("Data", Orange.data.Table, self.data)]
+        self.inputs = [("Data", Orange.data.Table, self.set_data)]
+# [start-snippet-1]
         self.outputs = [("Sampled Data", Orange.data.Table),
                         ("Other Data", Orange.data.Table)]
-
+# [end-snippet-1]
         self.proportion = 50
         self.commitOnChange = 0
         self.loadSettings()
 
         self.resize(100,50)
 
-    def data(self, dataset):
-        if dataset:
+    def set_data(self, dataset):
+        if dataset is not None:
             self.dataset = dataset
             self.infoa.setText('%d instances in input data set' % len(dataset))
             self.optionsBox.setDisabled(0)
     ow = OWDataSamplerC()
     ow.show()
     dataset = Orange.data.Table('iris.tab')
-    ow.data(dataset)
+    ow.set_data(dataset)
     appl.exec_()

docs/extend-widgets/rst/OWLearningCurveA.py

 
     def __init__(self, parent=None, signalManager=None):
         OWWidget.__init__(self, parent, signalManager, 'LearningCurveA')
-
-        self.inputs = [("Data", Orange.data.Table, self.dataset),
-                       ("Learner", Orange.core.Learner, self.learner,
-                        Multiple)]
-
+# [start-snippet-1]
+        self.inputs = [("Data", Orange.data.Table, self.set_dataset),
+                       ("Learner", Orange.classification.Learner, self.set_learner,
+                        Multiple + Default)]
+# [end-snippet-1]
         self.folds = 5     # cross validation folds
         self.steps = 10    # points in the learning curve
         self.scoringF = 0  # scoring function
         self.commitOnChange = 1 # compute curve on any change of parameters
         self.loadSettings()
-        self.setCurvePoints() # sets self.curvePoints, self.steps equidistant points from 1/self.steps to 1
+        self.updateCurvePoints() # sets self.curvePoints, self.steps equidistant points from 1/self.steps to 1
+# [start-snippet-2]
         self.scoring = [("Classification Accuracy", Orange.evaluation.scoring.CA),
                         ("AUC", Orange.evaluation.scoring.AUC),
                         ("BrierScore", Orange.evaluation.scoring.Brier_score),
                         ("Information Score", Orange.evaluation.scoring.IS),
                         ("Sensitivity", Orange.evaluation.scoring.Sensitivity),
                         ("Specificity", Orange.evaluation.scoring.Specificity)]
+# [end-snippet-2]
         self.learners = [] # list of current learners from input channel, tuples (id, learner)
         self.data = None   # data on which to construct the learning curve
         self.curves = []   # list of evaluation results (one per learning curve point)
         box = OWGUI.widgetBox(self.controlArea, "Options")
         OWGUI.spin(box, self, 'folds', 2, 100, step=1,
                    label='Cross validation folds:  ',
-                   callback=lambda: self.computeCurve(self.commitOnChange))
+                   callback=lambda: self.computeCurve() if self.commitOnChange else None)
         OWGUI.spin(box, self, 'steps', 2, 100, step=1,
                    label='Learning curve points:  ',
-                   callback=[self.setCurvePoints,
-                             lambda: self.computeCurve(self.commitOnChange)])
+                   callback=[self.updateCurvePoints,
+                             lambda: self.computeCurve() if self.commitOnChange else None])
         OWGUI.checkBox(box, self, 'commitOnChange', 'Apply setting on any change')
         self.commitBtn = OWGUI.button(box, self, "Apply Setting",
                                       callback=self.computeCurve, disabled=1)
     ##############################################################################    
     # slots: handle input signals
 
-    def dataset(self, data):
-        if data:
+    def set_dataset(self, data):
+        if data is not None:
             self.infoa.setText('%d instances in input data set' % len(data))
             self.data = data
-            if (len(self.learners)):
+            if len(self.learners):
                 self.computeCurve()
         else:
             self.infoa.setText('No data on input.')
             self.scores = []
         self.commitBtn.setEnabled(self.data is not None)
 
-    def learner(self, learner, id=None):
+    def set_learner(self, learner, id=None):
         ids = [x[0] for x in self.learners]
-        if not learner: # remove a learner and corresponding results
+        if learner is None: # remove a learner and corresponding results
             if not ids.count(id):
                 return # no such learner, removed before
             indx = ids.index(id)
                 self.curves[i].remove(indx)
             del self.scores[indx]
             del self.learners[indx]
-            self.setTable()
+            self.updateTable()
         else:
             if ids.count(id): # update (already seen a learner from this source)
                 indx = ids.index(id)
         self.commitBtn.setEnabled(len(self.learners))
 
         if self.data:
-            self.setTable()
+            self.updateTable()
 
-    ##############################################################################    
-    # learning curve table, callbacks
-
-    # recomputes the learning curve
-    def computeCurve(self, condition=1):
-        if condition:
-            learners = [x[1] for x in self.learners]
-            self.curves = self.getLearningCurve(learners)
-            self.computeScores()
+    def computeCurve(self):
+        learners = [x[1] for x in self.learners]
+        self.curves = self.getLearningCurve(learners)
+        self.computeScores()
 
     def computeScores(self):            
         self.scores = [[] for i in range(len(self.learners))]
         for x in self.curves:
             for (i,s) in enumerate(self.scoring[self.scoringF][1](x)):
                 self.scores[i].append(s)
-        self.setTable()
+        self.updateTable()
 
     def getLearningCurve(self, learners):   
         pb = OWGUI.ProgressBar(self, iterations=self.steps*self.folds)
         pb.finish()
         return curve
 
-    def setCurvePoints(self):
+    def updateCurvePoints(self):
         self.curvePoints = [(x + 1.)/self.steps for x in range(self.steps)]
 
-    def setTable(self):
+    def updateTable(self):
         self.table.setColumnCount(0)
         self.table.setColumnCount(len(self.learners))
         self.table.setRowCount(self.steps)
     
     l1 = Orange.classification.bayes.NaiveLearner()
     l1.name = 'Naive Bayes'
-    ow.learner(l1, 1)
+    ow.set_learner(l1, 1)
 
     data = Orange.data.Table('iris.tab')
-    ow.dataset(data)
+    ow.set_dataset(data)
 
     l2 = Orange.classification.bayes.NaiveLearner()
     l2.name = 'Naive Bayes (m=10)'
     l2.conditionalEstimatorConstructor = \
         Orange.statistics.estimate.ConditionalByRows(
             estimatorConstructor = Orange.statistics.estimate.M(m=10))
-    ow.learner(l2, 2)
+    ow.set_learner(l2, 2)
 
     l4 = Orange.classification.tree.TreeLearner(minSubset=2)
     l4.name = "Decision Tree"
-    ow.learner(l4, 4)
+    ow.set_learner(l4, 4)
 
-#    ow.learner(None, 1)
-#    ow.learner(None, 2)
-#    ow.learner(None, 4)
+#    ow.set_learner(None, 1)
+#    ow.set_learner(None, 2)
+#    ow.set_learner(None, 4)
 
     appl.exec_()

docs/extend-widgets/rst/OWLearningCurveB.py

     
     def __init__(self, parent=None, signalManager=None):
         OWWidget.__init__(self, parent, signalManager, 'LearningCurveA')
-
+# [start-snippet-1]
         self.inputs = [("Train Data", Orange.data.Table, self.trainset, Default),
                        ("Test Data", Orange.data.Table, self.testset),
                        ("Learner", Orange.classification.Learner,
                         self.learner, Multiple)]
-        
+# [end-snippet-1]        
         self.folds = 5     # cross validation folds
         self.steps = 10    # points in the learning curve
         self.scoringF = 0  # scoring function
         self.commitOnChange = 1 # compute curve on any change of parameters
         self.loadSettings()
-        self.setCurvePoints() # sets self.curvePoints, self.steps equidistan points from 1/self.steps to 1
+        self.updateCurvePoints() # sets self.curvePoints, self.steps equidistan points from 1/self.steps to 1
         self.scoring = [("Classification Accuracy", Orange.evaluation.scoring.CA),
                         ("AUC", Orange.evaluation.scoring.AUC),
                         ("BrierScore", Orange.evaluation.scoring.Brier_score),
         box = OWGUI.widgetBox(self.controlArea, "Options")
         OWGUI.spin(box, self, 'folds', 2, 100, step=1,
                    label='Cross validation folds:  ',
-                   callback=lambda: self.computeCurve(self.commitOnChange))
+                   callback=lambda: self.computeCurve() if self.commitOnChange else None)
 
         OWGUI.spin(box, self, 'steps', 2, 100, step=1,
                    label='Learning curve points:  ',
-                   callback=[self.setCurvePoints,
-                             lambda: self.computeCurve(self.commitOnChange)])
+                   callback=[self.updateCurvePoints,
+                             lambda: self.computeCurve() if self.commitOnChange else None])
 
         OWGUI.checkBox(box, self, 'commitOnChange', 'Apply setting on any change')
         self.commitBtn = OWGUI.button(box, self, "Apply Setting",
     # slots: handle input signals
 
     def trainset(self, data):
-        if data:
+        if data is not None:
             self.infoa.setText('%d instances in input data set' % len(data))
             self.data = data
-            if (len(self.learners)):
+            if len(self.learners):
                 self.computeCurve()
         else:
             self.infoa.setText('No data on input.')
 
     def learner(self, learner, id=None):
         ids = [x[0] for x in self.learners]
-        if not learner: # remove a learner and corresponding results
+        if learner is None: # remove a learner and corresponding results
             if not ids.count(id):
                 return # no such learner, removed before
             indx = ids.index(id)
                 self.curves[i].remove(indx)
             del self.scores[indx]
             del self.learners[indx]
-            self.setTable()
+            self.updateTable()
         else:
             if ids.count(id): # update (already seen a learner from this source)
                 indx = ids.index(id)
         self.commitBtn.setEnabled(len(self.learners))
 ##        if len(self.scores):
         if self.data:
-            self.setTable()
+            self.updateTable()
 
     ##############################################################################    
     # learning curve table, callbacks
 
     # recomputes the learning curve
-    def computeCurve(self, condition=1):
-        if condition:
-            learners = [x[1] for x in self.learners]
-            self.curves = self.getLearningCurve(learners)
-            self.computeScores()
+    def computeCurve(self):
+        learners = [x[1] for x in self.learners]
+        self.curves = self.getLearningCurve(learners)
+        self.computeScores()
 
     def computeScores(self):            
         self.scores = [[] for i in range(len(self.learners))]
         for x in self.curves:
             for (i,s) in enumerate(self.scoring[self.scoringF][1](x)):
                 self.scores[i].append(s)
-        self.setTable()
+        self.updateTable()
 
     def getLearningCurve(self, learners):   
         pb = OWGUI.ProgressBar(self, iterations=self.steps*self.folds)
         pb.finish()
         return curve
 
-    def setCurvePoints(self):
+    def updateCurvePoints(self):
         self.curvePoints = [(x + 1.)/self.steps for x in range(self.steps)]
 
-    def setTable(self):
+    def updateTable(self):
         self.table.setColumnCount(0)
         self.table.setColumnCount(len(self.learners))
         self.table.setRowCount(self.steps)

docs/extend-widgets/rst/OWLearningCurveC.py

 
         warnings.filterwarnings("ignore", ".*builtin attribute.*", Orange.core.AttributeWarning)
 
-        self.setCurvePoints() # sets self.curvePoints, self.steps equidistant points from 1/self.steps to 1
+        self.updateCurvePoints() # sets self.curvePoints, self.steps equidistant points from 1/self.steps to 1
         self.scoring = [("Classification Accuracy",
                          Orange.evaluation.scoring.CA),
                         ("AUC", Orange.evaluation.scoring.AUC),
         box = OWGUI.widgetBox(self.controlArea, "Options")
         OWGUI.spin(box, self, 'folds', 2, 100, step=1,
                    label='Cross validation folds:  ',
-                   callback=lambda: self.computeCurve(self.commitOnChange))
+                   callback=lambda: self.computeCurve() if self.commitOnChange else None)
         OWGUI.spin(box, self, 'steps', 2, 100, step=1,
                    label='Learning curve points:  ',
-                   callback=[self.setCurvePoints,
-                             lambda: self.computeCurve(self.commitOnChange)])
+                   callback=[self.updateCurvePoints,
+                             lambda: self.computeCurve() if self.commitOnChange else None])
 
         OWGUI.checkBox(box, self, 'commitOnChange', 'Apply setting on any change')
         self.commitBtn = OWGUI.button(box, self, "Apply Setting",
     # slots: handle input signals
 
     def dataset(self, data):
-        if data:
+        if data is not None:
             self.infoa.setText('%d instances in input data set' % len(data))
             self.data = data
-            if (len(self.learners)):
+            if len(self.learners):
                 self.computeCurve()
             self.replotGraph()
         else:
     # - score, evaluation score for the learning
     def learner(self, learner, id=None):
         ids = [x[0] for x in self.learners]
-        if not learner: # remove a learner and corresponding results
+        if learner is None: # remove a learner and corresponding results
             if not ids.count(id):
                 return # no such learner, removed before
             indx = ids.index(id)
             del self.scores[indx]
             self.learners[indx][1].curve.detach()
             del self.learners[indx]
-            self.setTable()
+            self.updateTable()
             self.updatellb()
         else:
             if ids.count(id): # update (already seen a learner from this source)
             self.infob.setText("No learners.")
         self.commitBtn.setEnabled(len(self.learners))
         if self.data:
-            self.setTable()
+            self.updateTable()
 
     ##############################################################################
     # learning curve table, callbacks
 
     # recomputes the learning curve
-    def computeCurve(self, condition=1):
-        if condition:
-            learners = [x[1] for x in self.learners]
-            self.curves = self.getLearningCurve(learners)
-            self.computeScores()
+    def computeCurve(self):
+        learners = [x[1] for x in self.learners]
+        self.curves = self.getLearningCurve(learners)
+        self.computeScores()
 
     def computeScores(self):
         self.scores = [[] for i in range(len(self.learners))]
                 self.scores[i].append(s)
         for (i,l) in enumerate(self.learners):
             l[1].score = self.scores[i]
-        self.setTable()
+        self.updateTable()
         self.replotGraph()
 
     def getLearningCurve(self, learners):
         pb.finish()
         return curve
 
-    def setCurvePoints(self):
+    def updateCurvePoints(self):
         self.curvePoints = [(x+1.)/self.steps for x in range(self.steps)]
 
-    def setTable(self):
+    def updateTable(self):
         self.table.setColumnCount(0)
         self.table.setColumnCount(len(self.learners))
         self.table.setRowCount(self.steps)

docs/extend-widgets/rst/OWLearningCurve_plot.py

 
         warnings.filterwarnings("ignore", ".*builtin attribute.*", orange.AttributeWarning)
 
-        self.setCurvePoints() # sets self.curvePoints, self.steps equidistantpoints from 1/self.steps to 1
+        self.updateCurvePoints() # sets self.curvePoints, self.steps equidistantpoints from 1/self.steps to 1
         self.scoring = [("Classification Accuracy", orngStat.CA), ("AUC", orngStat.AUC), ("BrierScore", orngStat.BrierScore), ("Information Score", orngStat.IS), ("Sensitivity", orngStat.sens), ("Specificity", orngStat.spec)]
         self.learners = [] # list of current learners from input channel, tuples (id, learner)
         self.data = None   # data on which to construct the learning curve
         box = OWGUI.widgetBox(self.controlArea, "Options")
         OWGUI.spin(box, self, 'folds', 2, 100, step=1,
                    label='Cross validation folds:  ',
-                   callback=lambda: self.computeCurve(self.commitOnChange))
+                   callback=lambda: self.computeCurve() if self.commitOnChange else None)
         OWGUI.spin(box, self, 'steps', 2, 100, step=1,
                    label='Learning curve points:  ',
-                   callback=[self.setCurvePoints, lambda: self.computeCurve(self.commitOnChange)])
+                   callback=[self.updateCurvePoints,
+                             lambda: self.computeCurve() if self.commitOnChange else None])
 
         OWGUI.checkBox(box, self, 'commitOnChange', 'Apply setting on any change')
         self.commitBtn = OWGUI.button(box, self, "Apply Setting", callback=self.computeCurve, disabled=1)
     # slots: handle input signals
 
     def dataset(self, data):
-        if data:
+        if data is not None:
             self.infoa.setText('%d instances in input data set' % len(data))
             self.data = data
-            if (len(self.learners)):
+            if len(self.learners):
                 self.computeCurve()
             self.replotGraph()
         else:
     # learning curve table, callbacks
 
     # recomputes the learning curve
-    def computeCurve(self, condition=1):
-        if condition:
-            learners = [x[1] for x in self.learners]
-            self.curves = self.getLearningCurve(learners)
-            self.computeScores()
+    def computeCurve(self):
+        learners = [x[1] for x in self.learners]
+        self.curves = self.getLearningCurve(learners)
+        self.computeScores()
 
     def computeScores(self):
         self.scores = [[] for i in range(len(self.learners))]
         pb.finish()
         return curve
 
-    def setCurvePoints(self):
+    def updateCurvePoints(self):
         self.curvePoints = [(x+1.)/self.steps for x in range(self.steps)]
 
     def setTable(self):

docs/extend-widgets/rst/channels.rst

 .. image:: learningcurve-output.png
 
 Now back to channels and tokens. Input and output channels for our
-widget are defined by::
+widget are defined by
 
-    self.inputs = [("Data", Orange.data.Table, self.dataset),
-                   ("Learner", Orange.classification.Learner,
-                    self.learner, Multiple + Default)]
+.. literalinclude:: OWLearningCurveA.py
+   :start-after: start-snippet-1
+   :end-before: end-snippet-1
+
 
 Notice that everything is pretty much the same as it was with
 widgets from previous lessons, the only difference being
-``Multiple + Default`` as the last value in the list that defines
-the :obj:`Learner` channel. This ``Multiple + Default`` says
-that this is a multi-input channel and is the default input for its type.
+``Multiple + Default`` (importable from  the OWWidget namespace)
+as the last value in the list that defines the :obj:`Learner`
+channel. This ``Multiple + Default`` says that this
+is a multi-input channel and is the default input for its type.
 If it would be unspecified then by default value of
 ``Single + NonDefault`` would be used. That would mean that the
 widget can receive the input only from one widget and is not the default input
 sending the token, and having a multi-input channel only tells Orange to
 send a token together with sending widget id, the two arguments with
 which the receiving function is called. For our *"Learner"*
-channel the receiving function is :func:`learner`, and this looks
-like the following::
+channel the receiving function is :func:`set_learner`, and this looks
+like the following
 
-    def learner(self, learner, id=None):
-        ids = [x[0] for x in self.learners]
-        if not learner: # remove a learner and corresponding results
-            if not ids.count(id):
-                return # no such learner, removed before
-            indx = ids.index(id)
-            for i in range(self.steps):
-                self.curves[i].remove(indx)
-            del self.scores[indx]
-            del self.learners[indx]
-            self.setTable()
-        else:
-            if ids.count(id): # update
-                       # (already seen a learner from this source)
-                indx = ids.index(id)
-                self.learners[indx] = (id, learner)
-                if self.data:
-                    curve = self.getLearningCurve([learner])
-                    score = [self.scoring[self.scoringF][1](x)[0] for x in curve]
-                    self.scores[indx] = score
-                    for i in range(self.steps):
-                        self.curves[i].add(curve[i], 0, replace=indx)
-            else: # add new learner
-                self.learners.append((id, learner))
-                if self.data:
-                    curve = self.getLearningCurve([learner])
-                    score = [self.scoring[self.scoringF][1](x)[0] for x in curve]
-                    self.scores.append(score)
-                    if len(self.curves):
-                        for i in range(self.steps):
-                            self.curves[i].add(curve[i], 0)
-                    else:
-                        self.curves = curve
-        if len(self.learners):
-            self.infob.setText("%d learners on input." % len(self.learners))
-        else:
-            self.infob.setText("No learners.")
-        self.commitBtn.setEnabled(len(self.learners))
-        if self.data:
-            self.setTable()
+.. literalinclude:: OWLearningCurveA.py
+   :pyobject: OWLearningCurveA.set_learner
+
 
 OK, this looks like one long and complicated function. But be
 patient! Learning curve is not the simplest widget there is, so
 learning curve routines from testing and performance
 scoring functions from stats. I rather like
 the easy by which new scoring functions are added to the widget, since
-all that is needed is the augmenting the list::
+all that is needed is the augmenting the list
 
-    self.scoring = [("Classification Accuracy", Orange.evaluation.scoring.CA),
-                    ("AUC", Orange.evaluation.scoring.AUC),
-                    ("BrierScore", Orange.evaluation.scoring.Brier_score),
-                    ("Information Score", Orange.evaluation.scoring.IS),
-                    ("Sensitivity", Orange.evaluation.scoring.Sensitivity),
-                    ("Specificity", Orange.evaluation.scoring.Specificity)]
+.. literalinclude:: OWLearningCurveA.py
+   :start-after: start-snippet-2
+   :end-before: end-snippet-2
+
 
 which is defined in the initialization part of the widget. The
 other useful trick in this widget is that evaluation (k-fold cross
 our sampling widget as defined in previous lessons such that it will
 send out the sampled data to one channel, and all other data to
 another channel. The corresponding channel definition of this widget
-is::
+is
 
-    self.outputs = [("Sampled Data", Orange.data.Table),
-                    ("Other Data", Orange.data.Table)]
+.. literalinclude:: OWDataSamplerC.py
+   :start-after: start-snippet-1
+   :end-before: end-snippet-1
+
 
 We used this in the third incarnation of :download:`data sampler widget <OWDataSamplerC.py>`,
 with essentially the only other change in the code in the :func:`selection` and
-:func:`commit` functions::
+:func:`commit` functions
 
-    def selection(self):
-        indices = Orange.data.sample.SubsetIndices2(p0=self.proportion / 100.)
-        ind = indices(self.dataset)
-        self.sample = self.dataset.select(ind, 0)
-        self.otherdata = self.dataset.select(ind, 1)
-        self.infob.setText('%d sampled instances' % len(self.sample))
+.. literalinclude:: OWDataSamplerC.py
+   :pyobject: OWDataSamplerC.selection
 
-    def commit(self):
-        self.send("Sampled Data", self.sample)
-        self.send("Other Data", self.otherdata)
+.. literalinclude:: OWDataSamplerC.py
+   :pyobject: OWDataSamplerC.commit
+
 
 If a widget that has multiple channels of the same type is
 connected to a widget that accepts such tokens, Orange Canvas opens a
 When enlisting the input channel of the same type, the default
 channels have a special flag in the channel specification list. So for
 our new :download:`learning curve <OWLearningCurveB.py>` widget, the
-channel specification is::
+channel specification is
 
-    self.inputs = [("Train Data", Orange.data.Table, self.trainset, Default),
-                   ("Test Data", Orange.data.Table, self.testset),
-                   ("Learner", Orange.classification.Learner, self.learner, Multiple)]
+.. literalinclude:: OWLearningCurveB.py
+   :start-after: start-snippet-1
+   :end-before: end-snippet-1
+
 
 That is, the :obj:`Train Data` channel is a single-token
 channel which is a default one (third parameter). Note that the flags can

docs/extend-widgets/rst/progressbar.rst

 some constant at the end of the iteration. For this, we use
 :class:`ProgressBar` class in :mod:`OWGUI`, and the code in
 the learning curve widget described in the previous lesson that does
-it is as follows::
+it is as follows
 
-    def getLearningCurve(self, learners):
-        pb = OWGUI.ProgressBar(self, iterations=self.steps * self.folds)
-        curve = Orange.evaluation.testing.learning_curve_n(
-            learners, self.data, folds=self.folds,
-            proportions=self.curvePoints, callback=pb.advance)
-        pb.finish()
-        return curve
+.. literalinclude:: OWLearningCurveA.py
+   :pyobject: OWLearningCurveA.getLearningCurve
+
 
 :class:`ProgressBar` class removes the need to define any special
 function to compute the percent of the task done and set the

docs/extend-widgets/rst/settings.rst

 picklable. In our widget, we will use two settings variables, and we
 declare this just after the widget class definition.
 
-::
+.. literalinclude:: OWDataSamplerB.py
+   :start-after: start-snippet-1
+   :end-before: end-snippet-1
 
-    class OWDataSamplerB(OWWidget):
-        settingsList = ['proportion', 'commitOnChange']
 
 Any setting has to be initialized, and then we need to call
 :obj:`loadSettings()` to override defaults in case we have used
-the widget before and the settings have been saved::
+the widget before and the settings have been saved
 
-    self.proportion = 50
-    self.commitOnChange = 0
-    self.loadSettings()
+.. literalinclude:: OWDataSamplerB.py
+   :start-after: start-snippet-2
+   :end-before: end-snippet-2
+
 
 Now anything we do with the two variables (:obj:`self.proportion` and
 :obj:`self.commitOnChange`) will be saved upon exiting our
 widgets and write callback functions that set our settings
 appropriately. This is what we have done before we got bored with it,
 since the GUI part spanned over much of the widget's code. Instead, we
-wrote a library called OWGUI (I never liked the name, but could never
+wrote a library called :mod:`OWGUI` (I never liked the name, but could never
 come up with something better). With this library, the GUI definition
-part of the options box is a bit dense but rather very short::
+part of the options box is a bit dense but rather very short
 
-    box = OWGUI.widgetBox(self.controlArea, "Info")
-    self.infoa = OWGUI.widgetLabel(box, 'No data on input yet, waiting to get something.')
-    self.infob = OWGUI.widgetLabel(box, '')
+.. literalinclude:: OWDataSamplerB.py
+   :start-after: start-snippet-3
+   :end-before: end-snippet-3
 
-    OWGUI.separator(self.controlArea)
-    self.optionsBox = OWGUI.widgetBox(self.controlArea, "Options")
-    OWGUI.spin(self.optionsBox, self, 'proportion', min=10, max=90, step=10,
-               label='Sample Size [%]:', callback=[self.selection, self.checkCommit])
-    OWGUI.checkBox(self.optionsBox, self, 'commitOnChange', 'Commit data on selection change')
-    OWGUI.button(self.optionsBox, self, "Commit", callback=self.commit)
-    self.optionsBox.setDisabled(1)
 
 We are already familiar with the first part - the Info group
 box. To make widget nicer, we put a separator between this and Options
 widget, there is no data to sample from. But this also means that when
 process the input tokens, we should take care for enabling and
 disabling. The data processing and token sending part of our widget
-now is::
+now is
 
-    def data(self, dataset):
-        if dataset:
-            self.dataset = dataset
-            self.infoa.setText('%d instances in input data set' % len(dataset))
-            self.optionsBox.setDisabled(0)
-            self.selection()
-            self.commit()
-        else:
-            self.send("Sampled Data", None)
-            self.optionsBox.setDisabled(1)
-            self.infoa.setText('No data on input yet, waiting to get something.')
-            self.infob.setText('')
+.. literalinclude:: OWDataSamplerB.py
+   :start-after: start-snippet-4
+   :end-before: end-snippet-4
 
-    def selection(self):
-        indices = orange.MakeRandomIndices2(p0=self.proportion / 100.)
-        ind = indices(self.dataset)
-        self.sample = self.dataset.select(ind, 0)
-        self.infob.setText('%d sampled instances' % len(self.sample))
-
-    def commit(self):
-        self.send("Sampled Data", self.sample)
-
-    def checkCommit(self):
-        if self.commitOnChange:
-            self.commit()
 
 You can now also inspect the :download:`complete code <OWDataSamplerB.py>`
 of this widget. To distinguish it with a widget we have developed in the
 previous section, we have designed a special
 :download:`icon <DataSamplerB.svg>` for it. If you wish to test is
-widget in the Orange Canvas, put its code in the Test directory we
-have created for the previous widget, update the Canvas registry, and
-try it out using a schema with a File and Data Table widget.
+widget in the Orange Canvas, put its code in the `orangedemo` directory we
+have created for the previous widget and try it out using a schema with
+a File and Data Table widget.
 
 .. image:: schemawithdatasamplerB.png
 
 
 ::
 
-    def cdata(self, data, clearResults = 1):
+    def cdata(self, data):
         self.closeContext()
 
-        exData = self.data
         self.data = data
         self.graph.setData(data)
         self.graph.insideColors = None