Commits

Aleš Erjavec committed 3c08b4b

Code style fixes for Random forest widgets.

Comments (0)

Files changed (2)

Orange/OrangeWidgets/Classify/OWRandomForest.py

 from exceptions import Exception
 from orngWrap import PreprocessedLearner
 
+
 class OWRandomForest(OWWidget):
-    settingsList = ["name", "trees", "attributes", "attributesP", "preNodeInst", "preNodeInstP", "limitDepth", "limitDepthP", "rseed"]
+    settingsList = ["name", "trees", "attributes", "attributesP",
+                    "preNodeInst", "preNodeInstP", "limitDepth",
+                    "limitDepthP", "rseed"]
 
-    def __init__(self, parent=None, signalManager = None, name='Random Forest'):
-        OWWidget.__init__(self, parent, signalManager, name, wantMainArea=False, resizingEnabled=False)
+    def __init__(self, parent=None, signalManager=None, name='Random Forest'):
+        OWWidget.__init__(self, parent, signalManager, name,
+                          wantMainArea=False, resizingEnabled=False)
 
         self.inputs = [("Data", ExampleTable, self.setData),
-                       ("Preprocess", PreprocessedLearner, self.setPreprocessor)]
-        
+                       ("Preprocess", PreprocessedLearner,
+                        self.setPreprocessor)]
+
         self.outputs = [("Learner", orange.Learner),
                         ("Random Forest Classifier", orange.Classifier)]
 
         self.data = None
         self.preprocessor = None
 
-        OWGUI.lineEdit(self.controlArea, self, 'name', box='Learner/Classifier Name', tooltip='Name to be used by other widgets to identify your learner/classifier.')
+        OWGUI.lineEdit(self.controlArea, self, 'name',
+                       box='Learner/Classifier Name',
+                       tooltip='Name to be used by other widgets to identify '
+                               'your learner/classifier.')
 
         OWGUI.separator(self.controlArea)
 
         self.bBox = OWGUI.widgetBox(self.controlArea, 'Basic Properties')
 
-        self.treesBox = OWGUI.spin(self.bBox, self, "trees", 1, self.maxTrees, orientation="horizontal", label="Number of trees in forest")
-        self.attributesBox, self.attributesPBox = OWGUI.checkWithSpin(self.bBox, self, "Consider exactly", 1, 10000, "attributes", "attributesP", " "+"random attributes at each split.")
-        self.rseedBox = OWGUI.spin(self.bBox, self, "rseed", 0, 100000, orientation="horizontal", label="Seed for random generator ")
+        self.treesBox = OWGUI.spin(self.bBox, self, "trees", 1, self.maxTrees,
+                                   orientation="horizontal",
+                                   label="Number of trees in forest")
+        self.attributesBox, self.attributesPBox = \
+            OWGUI.checkWithSpin(self.bBox, self, "Consider exactly",
+                                1, 10000, "attributes", "attributesP",
+                                " random attributes at each split.")
+
+        self.rseedBox = OWGUI.spin(self.bBox, self, "rseed", 0, 100000,
+                                   orientation="horizontal",
+                                   label="Seed for random generator ")
 
         OWGUI.separator(self.controlArea)
 
         self.pBox = OWGUI.widgetBox(self.controlArea, 'Growth Control')
 
-        self.limitDepthBox, self.limitDepthPBox = OWGUI.checkWithSpin(self.pBox, self, "Maximal depth of individual trees", 1, 1000, "limitDepth", "limitDepthP", "")
-        self.preNodeInstBox, self.preNodeInstPBox = OWGUI.checkWithSpin(self.pBox, self, "Stop splitting nodes with ", 1, 1000, "preNodeInst", "preNodeInstP", " or fewer instances")
+        self.limitDepthBox, self.limitDepthPBox = \
+            OWGUI.checkWithSpin(self.pBox, self,
+                                "Maximal depth of individual trees",
+                                1, 1000, "limitDepth", "limitDepthP", "")
+
+        self.preNodeInstBox, self.preNodeInstPBox = \
+            OWGUI.checkWithSpin(self.pBox, self, "Stop splitting nodes with ",
+                                1, 1000, "preNodeInst", "preNodeInstP",
+                                " or fewer instances")
 
         OWGUI.separator(self.controlArea)
 
         OWGUI.separator(self.controlArea)
 
-        self.btnApply = OWGUI.button(self.controlArea, self, "&Apply Changes", callback = self.doBoth, disabled=0, default=True)
+        self.btnApply = OWGUI.button(self.controlArea, self,
+                                     "&Apply Changes",
+                                     callback=self.doBoth,
+                                     disabled=0,
+                                     default=True)
 
-        self.resize(100,200)
+        self.resize(100, 200)
 
         self.setLearner()
 
     def sendReport(self):
         self.reportSettings("Learning parameters",
-                            [("Number of trees", self.trees),
-                             ("Considered number of attributes at each split", self.attributeP if self.attributes else "not set"),
-                             ("Seed for random generator", self.rseed),
-                             ("Maximal depth of individual trees", self.limitDepthP if self.limitDepth else "not set"),
-                             ("Minimal number of instances in a leaf", self.preNodeInstP if self.preNodeInst else "not limited")
-                           ])
+                    [("Number of trees", self.trees),
+                     ("Considered number of attributes at each split",
+                      self.attributeP if self.attributes else "not set"),
+                     ("Seed for random generator", self.rseed),
+                     ("Maximal depth of individual trees",
+                      self.limitDepthP if self.limitDepth else "not set"),
+                     ("Minimal number of instances in a leaf",
+                      self.preNodeInstP if self.preNodeInst else "not limited")
+                   ])
         self.reportData(self.data)
 
     def constructLearner(self):
             attrs = self.attributesP
 
         from Orange.classification.tree import SimpleTreeLearner
-        
+
         smallLearner = SimpleTreeLearner()
 
         if self.preNodeInst:
-            smallLearner.min_instances = self.preNodeInstP 
+            smallLearner.min_instances = self.preNodeInstP
         else:
             smallLearner.min_instances = 0
 
         if self.limitDepth:
-            smallLearner.max_depth = self.limitDepthP 
-        
-        learner = orngEnsemble.RandomForestLearner(base_learner=smallLearner, 
+            smallLearner.max_depth = self.limitDepthP
+
+        learner = orngEnsemble.RandomForestLearner(base_learner=smallLearner,
                             trees=self.trees, rand=rand, attributes=attrs)
 
         if self.preprocessor:
         self.error()
 
     def setData(self, data):
-        self.data = self.isDataWithClass(data, orange.VarTypes.Discrete, checkMissing=True) and data or None
-        
+        if not self.isDataWithClass(data, orange.VarTypes.Discrete,
+                                    checkMissing=True):
+            data = None
+        self.data = data
+
         #self.setLearner()
 
         if self.data:
             learner = self.constructLearner()
-            pb = OWGUI.ProgressBar(self, iterations=self.trees)
             self.progressBarInit()
             learner.callback = lambda v: self.progressBarSet(100.0 * v)
             try:
             self.classifier = None
 
         self.send("Random Forest Classifier", self.classifier)
-        
+
     def setPreprocessor(self, pp):
         self.preprocessor = pp
         self.doBoth()
         self.setData(self.data)
 
 
-
-##############################################################################
-# Test the widget, run from DOS prompt
-# > python OWDataTable.py)
-# Make sure that a sample data set (adult_sample.tab) is in the directory
-
-if __name__=="__main__":
-    a=QApplication(sys.argv)
-    ow=OWRandomForest()
-    a.setMainWidget(ow)
+if __name__ == "__main__":
+    a = QApplication(sys.argv)
+    ow = OWRandomForest()
 
     d = orange.ExampleTable('adult_sample')
     ow.setData(d)
 
     ow.show()
-    a.exec_loop()
+    a.exec_()
     ow.saveSettings()

Orange/OrangeWidgets/Regression/OWRandomForestRegression.py

 from OWRandomForest import *
 
 class OWRandomForestRegression(OWRandomForest):
-    def __init__(self, parent=None, signalManager=None, title="Random forest regression"):
+    def __init__(self, parent=None, signalManager=None,
+                 title="Random forest regression"):
         OWRandomForest.__init__(self, parent, signalManager, title)
-        
+
         self.inputs = [("Data", ExampleTable, self.setData),
                        ("Preprocess", PreprocessedLearner, self.setPreprocessor)]
-        
+
         self.outputs = [("Learner", orange.Learner),
                         ("Random Forest Classifier", orange.Classifier)]
 
             self.classifier = None
 
         self.send("Random Forest Classifier", self.classifier)
-