# source:orange/Orange/testing/regression/tests_20/reference_filterv.py@9952:986e9cd806f4

Revision 9952:986e9cd806f4, 6.6 KB checked in by Miha Stajdohar <miha.stajdohar@…>, 2 years ago (diff)

Tests moved and renamed from orange 20.

Line
1import orange
2
3
4############ THIS IS WHAT YOU CAN DO WITH DISCRETE ATTRIBUTES
5
6data = orange.ExampleTable("lenses")
7
8############ THIS IS WHAT YOU CAN DO WITH DISCRETE ATTRIBUTES
9
10print "\nYoung or presbyopic with astigmatism"
11fya = orange.Filter_values(domain = data.domain)
12fya["age"] = "young"
13print "\nYoung examples\n"
14for ex in fya(data):
15    print ex
16
17fya["age"] = "presbyopic"
18print "\n\nPresbyopic examples\n"
19for ex in fya(data):
20    print ex
21
22
23fya["age"] = ["presbyopic", "young"]
24print "\n\nYoung and presbyopic examples\n"
25for ex in fya(data):
26    print ex
27
28astigm = data.domain["astigmatic"]
29fya["age"] = ["presbyopic", "young"]
30fya[astigm] = "yes"
31print "\n\nYoung and presbyopic examples that are astigmatic\n"
32for ex in fya(data):
33    print ex
34
35fr = orange.Filter_values(domain = data.domain)
36fr[3] = "reduced"
37
38# Conjunction is not necessary here - we could still do this with a single filter
39fcon = orange.Filter_conjunction([fya, fr])
40print "\n\nYoung and presbyopic examples that are astigmatic and have reduced tear rate\n"
41for ex in fcon(data):
42    print ex
43
44fcon = orange.Filter_disjunction([fya, fr])
45print "\n\nYoung and presbyopic asticmatic examples and examples that have reduced tear rate\n"
46for ex in fcon(data):
47    print ex
48
49
50############ THIS IS WHAT YOU CAN DO WITH CONTINUOUS ATTRIBUTES
51
52data = orange.ExampleTable("iris")
53
54fcont = orange.Filter_values(domain = data.domain)
55fcont[0] = (orange.ValueFilter.Equal, 4.59999999999999) # This is to check that rounding errors don't hurt
56print "\n\nThe first attribute equals 4.6"
57for ex in fcont(data):
58    print ex
59
60fcont[0] = (orange.ValueFilter.Less, 4.6)
61print "\n\nThe first attribute is less than 4.6"
62for ex in fcont(data):
63    print ex
64
65fcont[0] = (orange.ValueFilter.LessEqual, 4.6)
66print "\n\nThe first attribute is less than or equal to 4.6"
67for ex in fcont(data):
68    print ex
69
70fcont[0] = (orange.ValueFilter.Greater, 7.6)
71print "\n\nThe first attribute is greater than 7.6"
72for ex in fcont(data):
73    print ex
74
75fcont[0] = (orange.ValueFilter.GreaterEqual, 7.6)
76print "\n\nThe first attribute is greater than or equal to 7.6"
77for ex in fcont(data):
78    print ex
79
80fcont[0] = (orange.ValueFilter.Between, 4.6, 5.0)
81print "\n\nThe first attribute is between to 4.5 and 5.0"
82for ex in fcont(data):
83    print ex
84
85fcont[0] = (orange.ValueFilter.Outside, 4.6, 7.5)
86print "\n\nThe first attribute is between to 4.5 and 5.0"
87for ex in fcont(data):
88    print ex
89
90
91############ THIS IS WHAT YOU CAN DO WITH STRING ATTRIBUTES
92
94for ex in data:
95    ex["name"] = str(ex.getclass())
96
97fstr = orange.Filter_values(domain = data.domain)
98fstr["name"] = "Iris-setosa"
99print "\n\nSetosae"
100d = fstr(data)
101print "%i examples, starting with %s" % (len(d), d[0])
102
103fstr["name"] = ["Iris-setosa", "Iris-virginica"]
104print "\n\nSetosae and virginicae"
105d = fstr(data)
106print "%i examples, starting with %s\n  finishing with %s" % (len(d), d[0], d[-1])
107
108fstr["name"] = ["Iris-setosa", "Iris-viRGInica"]
109fstr["name"].caseSensitive = 1
110print "\n\nSetosae and viRGInicae (case sensitive)"
111d = fstr(data)
112print "%i examples, starting with %s\n  finishing with %s" % (len(d), d[0], d[-1])
113
114fstr["name"] = ["Iris-setosa", "Iris-viRGInica"]
115fstr["name"].caseSensitive = 0
116print "\n\nSetosae and viRGInicae (case insensitive)"
117d = fstr(data)
118
119print "%i examples, starting with %s\n  finishing with %s" % (len(d), d[0], d[-1])
120fstr["name"] = (orange.Filter_values.Less, "Iris-versicolor")
121print "\n\nLess than versicolor"
122d = fstr(data)
123print "%i examples, starting with %s\n  finishing with %s" % (len(d), d[0], d[-1])
124
125fstr["name"] = (orange.Filter_values.LessEqual, "Iris-versicolor")
126print "\n\nLess or equal versicolor"
127d = fstr(data)
128print "%i examples, starting with %s\n  finishing with %s" % (len(d), d[0], d[-1])
129
130fstr["name"] = (orange.Filter_values.Greater, "Iris-versicolor")
131print "\n\nGreater than versicolor"
132d = fstr(data)
133print "%i examples, starting with %s\n  finishing with %s" % (len(d), d[0], d[-1])
134
135fstr["name"] = (orange.Filter_values.GreaterEqual, "Iris-versicolor")
136print "\n\nGreater or equal versicolor"
137d = fstr(data)
138print "%i examples, starting with %s\n  finishing with %s" % (len(d), d[0], d[-1])
139
140fstr["name"] = (orange.Filter_values.Between, "Iris-versicolor", "Iris-virginica")
141print "\n\nGreater or equal versicolor"
142d = fstr(data)
143print "%i examples, starting with %s\n  finishing with %s" % (len(d), d[0], d[-1])
144
145fstr["name"] = (orange.Filter_values.Contains, "ers")
146print "\n\nContains 'ers'"
147d = fstr(data)
148print "%i examples, starting with %s\n  finishing with %s" % (len(d), d[0], d[-1])
149
150fstr["name"] = (orange.Filter_values.NotContains, "ers")
151print "\n\nDoesn't contain 'ers'"
152d = fstr(data)
153print "%i examples, starting with %s\n  finishing with %s" % (len(d), d[0], d[-1])
154
155fstr["name"] = (orange.Filter_values.BeginsWith, "Iris-ve")
156print "\n\nBegins with 'Iris-ve'"
157d = fstr(data)
158print "%i examples, starting with %s\n  finishing with %s" % (len(d), d[0], d[-1])
159
160fstr["name"] = (orange.Filter_values.EndsWith, "olor")
161print "\n\nEnds with with 'olor'"
162d = fstr(data)
163print "%i examples, starting with %s\n  finishing with %s" % (len(d), d[0], d[-1])
164
165fstr["name"] = (orange.Filter_values.EndsWith, "a"*50)
166print "\n\nBegins with '%s'" % ("a"*50)
167d = fstr(data)
168if not len(d):
169    print "<empty table>"
170else:
171    print "%i examples, starting with %s\n  finishing with %s" % (len(d), d[0], d[-1])
172
173fstr = orange.Filter_values(domain=data.domain)
174fstr["name"] = (orange.Filter_values.BeginsWith, "Iris-VE")
175fstr["name"].caseSensitive = 1
176print "\n\nBegins with 'Iris-VE' (case sensitive)"
177d = fstr(data)
178if not len(d):
179    print "<empty table>"
180else:
181    print "%i examples, starting with %s\n  finishing with %s" % (len(d), d[0], d[-1])
182
183fstr["name"] = (orange.Filter_values.BeginsWith, "Iris-VE")
184fstr["name"].caseSensitive = 0
185print "\n\nBegins with 'Iris-VE' (case insensitive)"
186d = fstr(data)
187if not len(d):
188    print "<empty table>"
189else:
190    print "%i examples, starting with %s\n  finishing with %s" % (len(d), d[0], d[-1])
191
192
193
194###### REFERENCES vs. COPIES OF EXAMPLES
195
196data = orange.ExampleTable("lenses")
197
198print "\nYoung or presbyopic with astigmatism - as references"
199fya = orange.Filter_values(domain = data.domain)
200fya["age"] = "young"
201print "\nYoung examples\n"
202d2 = fya(data, 1)
203for ex in fya(d2):
204    print ex
205
206print "\nTesting whether this is really a reference"
207d2[0][0] = "?"
208print data[0]
209
210print "\nTesting that we don't have references when not requested"
211d2 = fya(data)
212d2[1][0] = "?"
213print data[1]
214
215###### COUNTS OF EXAMPLES
216
217data = orange.ExampleTable("lenses")
218fya = orange.Filter_values(domain = data.domain)
219fya["age"] = "young"
220print "The data contains %i young fellows" % fya.count(data)
Note: See TracBrowser for help on using the repository browser.