source: orange/Orange/OrangeWidgets/Classify/OWC45Tree.py @ 10785:3502269e050d

Revision 10785:3502269e050d, 8.6 KB checked in by Ales Erjavec <ales.erjavec@…>, 2 years ago (diff)

Test for c45 library file presence before loading it.

Line 
1"""
2<name>C4.5</name>
3<description>C45 (classification tree) learner/classifier.</description>
4<icon>icons/C45.png</icon>
5<contact>Janez Demsar (janez.demsar(@at@)fri.uni-lj.si)</contact>
6<priority>35</priority>
7"""
8import os
9
10from distutils import sysconfig
11
12from OWWidget import *
13import OWGUI
14from exceptions import Exception
15
16from orngWrap import PreprocessedLearner
17
18import Orange
19
20_Orange_dir = os.path.dirname(Orange.__file__)
21_SO = "c45%s" % sysconfig.get_config_var("SO")
22
23if os.path.exists(os.path.join(_Orange_dir, _SO)):             
24    # Test if the c45.(so|pyd) can be loaded by orange.C45Learner
25    import orange
26    try:
27        orange.C45Learner()
28    except orange.KernelException, ex:
29        # I guess not
30        raise ImportError(ex.message)
31else:
32    raise ImportError("c45 is not found")
33 
34class OWC45Tree(OWWidget):
35    settingsList = ["name",
36                    "infoGain", "subset", "probThresh",
37                    "minObjs", "prune", "cf",
38                    "iterative", "manualWindow", "window", "manualIncrement", "increment", "trials",
39                    "convertToOrange"]
40
41    def __init__(self, parent=None, signalManager = None, name='C4.5'):
42        OWWidget.__init__(self, parent, signalManager, name, wantMainArea = 0, resizingEnabled = 0)
43
44        self.callbackDeposit = []
45
46        self.inputs = [("Data", ExampleTable, self.setData),
47                       ("Preprocess", PreprocessedLearner, self.setPreprocessor)]
48       
49        self.outputs = [("Learner", orange.Learner),
50                        ("Classification Tree", Orange.classification.tree.TreeClassifier)]#, ("C45 Tree", orange.C45Classifier)]
51
52        # Settings
53        self.name = 'C4.5'
54        self.infoGain = 0;  self.subset = 0;       self.probThresh = 0;
55        self.useMinObjs = 1; self.minObjs = 2;   self.prune = 1;       self.cf = 25
56        self.iterative = 0; self.manualWindow = 0; self.window = 50;     self.manualIncrement = 0;  self.increment = 10;   self.trials = 10
57
58        self.convertToOrange = 1
59
60        self.loadSettings()
61
62        self.data = None                    # input data set
63        self.preprocessor = None            # no preprocessing as default
64
65        OWGUI.lineEdit(self.controlArea, self, 'name', box='Learner/Classifier Name',
66                 tooltip='Name to be used by other widgets to identify your learner/classifier.')
67        OWGUI.separator(self.controlArea)
68
69        self.wbSplit = OWGUI.widgetBox(self.controlArea, "Splitting")
70        OWGUI.checkBox(self.wbSplit, self, 'infoGain', 'Use information gain instead of ratio (-g)')
71        OWGUI.checkBox(self.wbSplit, self, 'subset', 'Subsetting (-s)')
72        OWGUI.checkBox(self.wbSplit, self, 'probThresh', 'Probabilistic threshold for continuous attributes (-p)')
73
74        OWGUI.separator(self.controlArea)
75
76        self.wbPruning = OWGUI.widgetBox(self.controlArea, "Pruning")
77        OWGUI.checkWithSpin(self.wbPruning, self, 'Minimal examples in leaves (-m)', 1, 1000, 'useMinObjs', 'minObjs', '', 1, labelWidth = 225)
78        OWGUI.checkWithSpin(self.wbPruning, self, 'Post pruning with confidence level (-cf) of ', 0, 100, 'prune', 'cf', '', 5, labelWidth = 225)
79
80        OWGUI.separator(self.controlArea)
81
82        self.wbIterative = OWGUI.widgetBox(self.controlArea, "Iterative generation")
83        self.cbIterative = OWGUI.checkBox(self.wbIterative, self, 'iterative', 'Generate the tree iteratively (-i, -t, -w)')
84        self.spTrial = OWGUI.spin(self.wbIterative, self, 'trials', 1, 30, 1, '', "       Number of trials (-t)", orientation = "horizontal", labelWidth = 225)
85        self.csWindow = OWGUI.checkWithSpin(self.wbIterative, self, "Manually set initial window size (-w) to ", 10, 1000, 'manualWindow', 'window', '', 10, labelWidth = 225)
86        self.csIncrement = OWGUI.checkWithSpin(self.wbIterative, self, "Manually set window increment (-i) to ", 10, 1000, 'manualIncrement', 'increment', '', 10, labelWidth = 225)
87
88        self.cbIterative.disables = [self.spTrial, self.csWindow, self.csIncrement]
89        self.cbIterative.makeConsistent()
90
91#        OWGUI.separator(self.controlArea)
92
93#        OWGUI.checkBox(self.controlArea, self, 'convertToOrange', 'Convert to orange tree structure', box = 1)
94
95        OWGUI.separator(self.controlArea)
96
97        OWGUI.button(self.controlArea, self, "&Apply", callback = self.setLearner, disabled=0, default=True)
98
99        OWGUI.rubber(self.controlArea)
100        self.setLearner()
101
102
103    def sendReport(self):
104        self.reportSettings("Learning parameters",
105                            [("Attribute quality measure", ["Information gain", "Gain ratio"][self.infoGain]),
106                             ("Subsetting", OWGUI.YesNo[self.subset]),
107                             ("Probabilistic threshold for continuous attributes", OWGUI.YesNo[self.probThresh]),
108                             self.useMinObjs and ("Minimal number of examples in leaves", self.minObjs),
109                             self.prune and ("Post pruning confidence level", self.cf),
110                             ("Iterative generation", OWGUI.YesNo[self.iterative]),
111                             self.iterative and ("Number of trials", self.trials),
112                             self.iterative and self.manualWindow and ("Initial window size manually set to", self.window),
113                             self.iterative and self.manualIncrement and ("Window increment manually set to", self.increment)])
114        self.reportData(self.data)
115
116
117
118    def setData(self,data):
119        self.data = self.isDataWithClass(data, orange.VarTypes.Discrete, checkMissing=True) and data or None
120        self.setLearner()
121       
122    def setPreprocessor(self, pp):
123        self.preprocessor = pp
124        self.setLearner()
125
126
127    def setLearner(self):
128        self.error(0)
129        try:
130            self.learner = orange.C45Learner(gainRatio=not self.infoGain, subset=self.subset, probThresh=self.probThresh,
131                                             minObjs=self.useMinObjs and self.minObjs or 0, prune=self.prune, cf=self.cf/100.,
132                                             batch = not self.iterative, window=self.manualWindow and self.window or 0, increment=self.manualIncrement and self.increment or 0, trials=self.trials,
133                                             convertToOrange = 1, #self.convertToOrange,
134                                             storeExamples = 1)
135        except orange.KernelException, ex:
136            self.error(0, "C45Loader: cannot load \c45.dll")
137            import orngDebugging
138            # Dont show the message box when running debugging scripts
139            if not orngDebugging.orngDebuggingEnabled and getattr(self, "__showMessageBox", True): 
140                # The message string must not have newlines otherwise it
141                # confuses Qt to show it in plain text not as rtf.
142                QMessageBox.warning( None, "C4.5 plug-in", '''File c45.dll not found.
143See <a href="http://orange.biolab.si/doc/reference/Orange.classification.tree/#c4-5-classifier-and-learner">this link</a>
144for the instructions on how to build the library.'''.replace("\n", " "), QMessageBox.Ok)
145                setattr(self, "__showMessageBox", False)
146            return
147       
148        if self.preprocessor:
149            self.learner = self.preprocessor.wrapLearner(self.learner)
150
151        self.learner.name = self.name
152        self.send("Learner", self.learner)
153
154        self.learn()
155
156
157    def learn(self):
158        self.error()
159        if self.data and self.learner:
160            if not self.data.domain.classVar:
161                self.error("This data set has no class.")
162                self.classifier = None
163            elif self.data.domain.classVar.varType != orange.VarTypes.Discrete:
164                self.error("This algorithm only works with discrete classes.")
165                self.classifier = None
166            else:
167                try:
168                    self.classifier = self.learner(self.data)
169                    self.classifier.name = self.name
170                except Exception, (errValue):
171                    self.error(str(errValue))
172                    self.classifier = None
173        else:
174            self.classifier = None
175
176#        self.send("Classifier", self.classifier)
177#        if self.convertToOrange:
178        self.send("Classification Tree", self.classifier)
179#        else:
180#            self.send("C45 Tree", self.classifier)
181
182       
183##############################################################################
184# Test the widget, run from DOS prompt
185# > python OWDataTable.py)
186# Make sure that a sample data set (adult_sample.tab) is in the directory
187
188if __name__=="__main__":
189    a=QApplication(sys.argv)
190    ow=OWC45Tree()
191##    dataset = orange.ExampleTable('adult_sample')
192##    ow.setData(dataset)
193
194    ow.show()
195    a.exec_()
196    ow.saveSettings()
Note: See TracBrowser for help on using the repository browser.