source: orange/orange/Orange/misc/__init__.py @ 7584:69e833700856

Revision 7584:69e833700856, 5.5 KB checked in by crt <crtomir.gorup@…>, 3 years ago (diff)

Renamed attribute -> feature.

Line 
1"""
2
3.. index:: misc
4
5Module Orange.misc contains common functions and classes which are used in other modules.
6
7==================
8Counters
9==================
10
11.. index:: misc
12.. index::
13   single: misc; counters
14
15.. automodule:: Orange.misc.counters
16  :members:
17
18==================
19Render
20==================
21
22.. index:: misc
23.. index::
24   single: misc; render
25
26.. automodule:: Orange.misc.render
27  :members:
28
29==================
30Selection
31==================
32
33.. index:: selection
34.. index::
35   single: misc; selection
36
37Many machine learning techniques generate a set different solutions or have to
38choose, as for instance in classification tree induction, between different
39features. The most trivial solution is to iterate through the candidates,
40compare them and remember the optimal one. The problem occurs, however, when
41there are multiple candidates that are equally good, and the naive approaches
42would select the first or the last one, depending upon the formulation of
43the if-statement.
44
45:class:`Orange.misc.selection` provides a class that makes a random choice
46in such cases. Each new candidate is compared with the currently optimal
47one; it replaces the optimal if it is better, while if they are equal,
48one is chosen by random. The number of competing optimal candidates is stored,
49so in this random choice the probability to select the new candidate (over the
50current one) is 1/w, where w is the current number of equal candidates,
51including the present one. One can easily verify that this gives equal
52chances to all candidates, independent of the order in which they are presented.
53
54.. automodule:: Orange.misc.selection
55  :members:
56
57Example
58--------
59
60The following snippet loads the data set lymphography and prints out the
61feature with the highest information gain.
62
63part of `misc-selection-bestonthefly.py`_ (uses `lymphography.tab`_)
64
65.. literalinclude:: code/misc-selection-bestonthefly.py
66  :lines: 7-16
67
68Our candidates are tuples gain ratios and features, so we set
69:obj:`callCompareOn1st` to make the compare function compare the first element
70(gain ratios). We could achieve the same by initializing the object like this:
71
72part of `misc-selection-bestonthefly.py`_ (uses `lymphography.tab`_)
73
74.. literalinclude:: code/misc-selection-bestonthefly.py
75  :lines: 18-18
76
77
78The other way to do it is through indices.
79
80`misc-selection-bestonthefly.py`_ (uses `lymphography.tab`_)
81
82.. literalinclude:: code/misc-selection-bestonthefly.py
83  :lines: 25-
84
85.. _misc-selection-bestonthefly.py: code/misc-selection-bestonthefly.py.py
86.. _lymphography.tab: code/lymphography.tab
87
88Here we only give gain ratios to :obj:`bestOnTheFly`, so we don't have to specify a
89special compare operator. After checking all features we get the index of the
90optimal one by calling :obj:`winnerIndex`.
91
92"""
93
94
95__all__ = ["counters", "selection", "render"]
96
97import random, types, sys
98
99def getobjectname(x, default=""):
100    if type(x)==types.StringType:
101        return x
102     
103    for i in ["name", "shortDescription", "description", "func_doc", "func_name"]:
104        if getattr(x, i, ""):
105            return getattr(x, i)
106
107    if hasattr(x, "__class__"):
108        r = repr(x.__class__)
109        if r[1:5]=="type":
110            return str(x.__class__)[7:-2]
111        elif r[1:6]=="class":
112            return str(x.__class__)[8:-2]
113    return default
114
115
116def demangleExamples(x):
117    if type(x)==types.TupleType:
118        return x
119    else:
120        return x, 0
121
122
123def frange(*argw):
124    start, stop, step = 0.0, 1.0, 0.1
125    if len(argw)==1:
126        start=step=argw[0]
127    elif len(argw)==2:
128        stop, step = argw
129    elif len(argw)==3:
130        start, stop, step = argw
131    elif len(argw)>3:
132        raise AttributeError, "1-3 arguments expected"
133
134    stop+=1e-10
135    i=0
136    res=[]
137    while 1:
138        f=start+i*step
139        if f>stop:
140            break
141        res.append(f)
142        i+=1
143    return res
144
145verbose = 0
146
147def printVerbose(text, *verb):
148    if len(verb) and verb[0] or verbose:
149        print text
150
151class ConsoleProgressBar(object):
152    def __init__(self, title="", charwidth=40, step=1, output=sys.stderr):
153        self.title = title + " "
154        self.charwidth = charwidth
155        self.step = step
156        self.currstring = ""
157        self.state = 0
158        self.output = output
159
160    def clear(self, i=-1):
161        try:
162            if hasattr(self.output, "isatty") and self.output.isatty():
163                self.output.write("\b" * (i if i != -1 else len(self.currstring)))
164            else:
165                self.output.seek(-i if i != -1 else -len(self.currstring), 2)
166        except Exception: ## If for some reason we failed
167            self.output.write("\n")
168
169    def getstring(self):
170        progchar = int(round(float(self.state) * (self.charwidth - 5) / 100.0))
171        return self.title + "=" * (progchar) + ">" + " " * (self.charwidth\
172            - 5 - progchar) + "%3i" % int(round(self.state)) + "%"
173
174    def printline(self, string):
175        try:
176            self.clear()
177            self.output.write(string)
178            self.output.flush()
179        except Exception:
180            pass
181        self.currstring = string
182
183    def __call__(self, newstate=None):
184        if newstate == None:
185            newstate = self.state + self.step
186        if int(newstate) != int(self.state):
187            self.state = newstate
188            self.printline(self.getstring())
189        else:
190            self.state = newstate
191
192    def finish(self):
193        self.__call__(100)
194        self.output.write("\n")
195
196def progressBarMilestones(count, iterations=100):
197    return set([int(i*count/float(iterations)) for i in range(iterations)])
Note: See TracBrowser for help on using the repository browser.