Skip to content

Commit 4cca2ff

Browse files
committedFeb 10, 2016
Fix unit tests
1 parent be38f4f commit 4cca2ff

File tree

3 files changed

+58
-79
lines changed

3 files changed

+58
-79
lines changed
 

‎CONTRIBUTORS.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@
2424

2525
- Guilherme Castelão
2626
Minor bugfix on plot_tipping_problem.py
27+
28+
- Jason Sexauer
29+
Enhancements to ``control`` package

‎skfuzzy/control/controlsystem.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ def rules(self):
234234
exposed_intermediaries = [] # Could also contain consequences
235235

236236
def _process_rules(rules):
237+
# Recursive funcion to process rules in the correct firing order
237238
len_rules = len(rules)
238239
skipped_rules = []
239240
while len(rules) > 0:
@@ -289,6 +290,9 @@ def addrule(self, rule):
289290
"""
290291
Add a new rule to the graph.
291292
"""
293+
if not isinstance(rule, Rule):
294+
raise ValueError("rule is not a Rule object")
295+
292296
# Combine the two graphs, which may not be disjoint
293297
self.graph = nx.compose(self.graph, rule.graph)
294298

‎skfuzzy/control/tests/test_fuzzyvariables.py

Lines changed: 51 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,9 @@ def test_instantiate_antecedent():
4848
# Assure expected behavior
4949
tst.assert_equal(universe, ant.universe)
5050
assert ant.label == label
51-
assert ant.active is None
5251
assert ant.input is None
5352
assert ant._id == id(ant)
54-
assert_empty_ordereddict(ant.output)
55-
assert_empty_ordereddict(ant.mf)
56-
assert_empty_ordereddict(ant.connections)
53+
assert_empty_ordereddict(ant.terms)
5754
assert ant.__name__ == 'Antecedent'
5855
assert ant.__repr__() == 'Antecedent: {0}'.format(label)
5956

@@ -67,13 +64,8 @@ def test_instantiate_consequent():
6764
# Assure expected behavior
6865
tst.assert_equal(universe, con.universe)
6966
assert con.label == label
70-
assert con.active is None
7167
assert con._id == id(con)
72-
assert con.output is None
73-
assert_empty_ordereddict(con.mf)
74-
assert_empty_ordereddict(con.connections)
75-
assert_empty_ordereddict(con.cuts)
76-
assert_empty_ordereddict(con.cut_mfs)
68+
assert_empty_ordereddict(con.terms)
7769
assert con.__name__ == 'Consequent'
7870
assert con.__repr__() == 'Consequent: {0}'.format(label)
7971

@@ -91,37 +83,37 @@ def test_automf3():
9183

9284
# Test Antecedent
9385
ant.automf(3)
94-
assert list(ant.mf.keys()) == label3
95-
for arr0, arr1 in zip(ant.mf.values(), mfs3):
96-
tst.assert_allclose(arr0, arr1)
86+
assert list(ant.terms.keys()) == label3
87+
for arr0, arr1 in zip(ant.terms.values(), mfs3):
88+
tst.assert_allclose(arr0.mf, arr1)
9789

9890
ant.automf(3, variable_type='quant')
99-
assert list(ant.mf.keys()) == alt_label3
91+
assert list(ant.terms.keys()) == alt_label3
10092

10193
ant.automf(3, invert=True)
102-
assert list(ant.mf.keys()) == label3[::-1]
94+
assert list(ant.terms.keys()) == label3[::-1]
10395

10496
ant.automf(3, variable_type='quant', invert=True)
105-
assert list(ant.mf.keys()) == alt_label3[::-1]
97+
assert list(ant.terms.keys()) == alt_label3[::-1]
10698

10799
# Test Consequent
108100
mfs3b = [np.r_[1., 7/9., 5/9., 3/9., 1/9., 0., 0., 0., 0., 0.],
109101
np.r_[0., 2/9., 4/9., 6/9., 8/9., 8/9., 6/9., 4/9., 2/9., 0.],
110102
np.r_[0., 0., 0., 0., 0., 1/9., 3/9., 5/9., 7/9., 1.]]
111103

112104
con.automf(3)
113-
assert list(con.mf.keys()) == label3
114-
for arr0, arr1 in zip(con.mf.values(), mfs3b):
115-
tst.assert_allclose(arr0, arr1)
105+
assert list(con.terms.keys()) == label3
106+
for arr0, arr1 in zip(con.terms.values(), mfs3b):
107+
tst.assert_allclose(arr0.mf, arr1)
116108

117109
con.automf(3, variable_type='quant')
118-
assert list(con.mf.keys()) == alt_label3
110+
assert list(con.terms.keys()) == alt_label3
119111

120112
con.automf(3, invert=True)
121-
assert list(con.mf.keys()) == label3[::-1]
113+
assert list(con.terms.keys()) == label3[::-1]
122114

123115
con.automf(3, variable_type='quant', invert=True)
124-
assert list(con.mf.keys()) == alt_label3[::-1]
116+
assert list(con.terms.keys()) == alt_label3[::-1]
125117

126118

127119
@nose.with_setup(setup)
@@ -139,18 +131,18 @@ def test_automf5():
139131

140132
# Test Antecedent
141133
ant.automf(5)
142-
assert list(ant.mf.keys()) == label5
143-
for arr0, arr1 in zip(ant.mf.values(), mfs5):
144-
tst.assert_allclose(arr0, arr1)
134+
assert list(ant.terms.keys()) == label5
135+
for arr0, arr1 in zip(ant.terms.values(), mfs5):
136+
tst.assert_allclose(arr0.mf, arr1)
145137

146138
ant.automf(5, variable_type='quant')
147-
assert list(ant.mf.keys()) == alt_label5
139+
assert list(ant.terms.keys()) == alt_label5
148140

149141
ant.automf(5, invert=True)
150-
assert list(ant.mf.keys()) == label5[::-1]
142+
assert list(ant.terms.keys()) == label5[::-1]
151143

152144
ant.automf(5, variable_type='quant', invert=True)
153-
assert list(ant.mf.keys()) == alt_label5[::-1]
145+
assert list(ant.terms.keys()) == alt_label5[::-1]
154146

155147
# Test Consequent
156148
mfs5b = [np.r_[1., 5/9., 1/9., 0., 0., 0., 0., 0., 0., 0.],
@@ -160,18 +152,18 @@ def test_automf5():
160152
np.r_[0., 0., 0., 0., 0., 0., 0., 1/9., 5/9., 1.]]
161153

162154
con.automf(5)
163-
assert list(con.mf.keys()) == label5
164-
for arr0, arr1 in zip(con.mf.values(), mfs5b):
165-
tst.assert_allclose(arr0, arr1)
155+
assert list(con.terms.keys()) == label5
156+
for arr0, arr1 in zip(con.terms.values(), mfs5b):
157+
tst.assert_allclose(arr0.mf, arr1)
166158

167159
con.automf(5, variable_type='quant')
168-
assert list(con.mf.keys()) == alt_label5
160+
assert list(con.terms.keys()) == alt_label5
169161

170162
con.automf(5, invert=True)
171-
assert list(con.mf.keys()) == label5[::-1]
163+
assert list(con.terms.keys()) == label5[::-1]
172164

173165
con.automf(5, variable_type='quant', invert=True)
174-
assert list(con.mf.keys()) == alt_label5[::-1]
166+
assert list(con.terms.keys()) == alt_label5[::-1]
175167

176168

177169
@nose.with_setup(setup)
@@ -193,18 +185,18 @@ def test_automf7():
193185

194186
# Test Antecedent
195187
ant.automf(7)
196-
assert list(ant.mf.keys()) == label7
197-
for arr0, arr1 in zip(ant.mf.values(), mfs7):
198-
tst.assert_allclose(arr0, arr1)
188+
assert list(ant.terms.keys()) == label7
189+
for arr0, arr1 in zip(ant.terms.values(), mfs7):
190+
tst.assert_allclose(arr0.mf, arr1)
199191

200192
ant.automf(7, variable_type='quant')
201-
assert list(ant.mf.keys()) == alt_label7
193+
assert list(ant.terms.keys()) == alt_label7
202194

203195
ant.automf(7, invert=True)
204-
assert list(ant.mf.keys()) == label7[::-1]
196+
assert list(ant.terms.keys()) == label7[::-1]
205197

206198
ant.automf(7, variable_type='quant', invert=True)
207-
assert list(ant.mf.keys()) == alt_label7[::-1]
199+
assert list(ant.terms.keys()) == alt_label7[::-1]
208200

209201
# Test Consequent
210202
mfs7b = [np.r_[1., 3/9., 0., 0., 0., 0., 0., 0., 0., 0.],
@@ -216,18 +208,18 @@ def test_automf7():
216208
np.r_[0., 0., 0., 0., 0., 0., 0., 0., 3/9., 1.]]
217209

218210
con.automf(7)
219-
assert list(con.mf.keys()) == label7
220-
for arr0, arr1 in zip(con.mf.values(), mfs7b):
221-
tst.assert_allclose(arr0, arr1)
211+
assert list(con.terms.keys()) == label7
212+
for arr0, arr1 in zip(con.terms.values(), mfs7b):
213+
tst.assert_allclose(arr0.mf, arr1)
222214

223215
con.automf(7, variable_type='quant')
224-
assert list(con.mf.keys()) == alt_label7
216+
assert list(con.terms.keys()) == alt_label7
225217

226218
con.automf(7, invert=True)
227-
assert list(con.mf.keys()) == label7[::-1]
219+
assert list(con.terms.keys()) == label7[::-1]
228220

229221
con.automf(7, variable_type='quant', invert=True)
230-
assert list(con.mf.keys()) == alt_label7[::-1]
222+
assert list(con.terms.keys()) == alt_label7[::-1]
231223

232224

233225
@nose.with_setup(setup)
@@ -241,24 +233,6 @@ def test_automf_bad():
241233
tst.assert_raises(ValueError, con.automf, 13)
242234

243235

244-
def test_set_active():
245-
universe = np.linspace(0, 5, 7)
246-
ant_label = 'service'
247-
con_label = 'TIP'
248-
249-
ant = Antecedent(universe, ant_label)
250-
con = Consequent(universe, con_label)
251-
252-
ant.automf(5)
253-
for label in list(ant.mf.keys()):
254-
ant[label]
255-
assert ant.active == label
256-
257-
con.automf(7)
258-
for label in list(con.mf.keys()):
259-
con[label]
260-
assert con.active == label
261-
262236

263237
@nose.with_setup(setup)
264238
def test_bad_set_active():
@@ -286,18 +260,18 @@ def test_add_mf():
286260
ant['high'] = mf1
287261

288262
# Ensure they were added correctly
289-
assert list(ant.mf.keys()) == ['wavy', 'high']
290-
tst.assert_equal(ant.mf['wavy'], mf0)
291-
tst.assert_equal(ant.mf['high'], mf1)
263+
assert list(ant.terms.keys()) == ['wavy', 'high']
264+
tst.assert_equal(ant.terms['wavy'].mf, mf0)
265+
tst.assert_equal(ant.terms['high'].mf, mf1)
292266

293267
# Assign mfs to Consequent
294268
con['wavy'] = mf0
295269
con['high'] = mf1
296270

297271
# Ensure they were added correctly
298-
assert list(con.mf.keys()) == ['wavy', 'high']
299-
tst.assert_equal(con.mf['wavy'], mf0)
300-
tst.assert_equal(con.mf['high'], mf1)
272+
assert list(con.terms.keys()) == ['wavy', 'high']
273+
tst.assert_equal(con.terms['wavy'].mf, mf0)
274+
tst.assert_equal(con.terms['high'].mf, mf1)
301275

302276

303277
@nose.with_setup(setup)
@@ -309,29 +283,25 @@ def test_set_patch():
309283

310284
# Setting a patch
311285
con.set_patch('average', 0.4)
312-
assert con.cuts['average'] == 0.4
313286

314287
# Overriding it with a higher patch
315288
con.set_patch('average', 0.55)
316-
assert con.cuts['average'] == 0.55
317289

318290
# Setting the other patches
319291
con.set_patch('good', 0.3)
320292
con.set_patch('poor', 0.88)
321-
assert con.cuts['good'] == 0.3
322-
assert con.cuts['poor'] == 0.88
323293

324-
con.compute()
325294
tst.assert_allclose(con.output, 3.6644951140065136)
326295

327296

328297
@nose.with_setup(setup)
329298
def test_set_input():
330299
global ant
331300
ant.automf(3)
332-
ant.set_input(2.3)
301+
ant.input = 2.3
333302

334-
assert ant.input == 2.3
303+
assert ant.input == 2.3, "Value is %s" % ant.input
304+
assert ant.crisp_value == 2.3, "Value is %s" % ant.crisp_value
335305

336306

337307
@nose.with_setup(setup)
@@ -350,8 +320,10 @@ def test_add_bad_mf():
350320
def test_cannot_compute():
351321
global ant
352322
global con
353-
tst.assert_raises(ValueError, ant.compute)
354-
tst.assert_raises(ValueError, con.compute)
323+
with tst.assert_raises(ValueError):
324+
ant.crisp_value
325+
with tst.assert_raises(ValueError):
326+
con.crisp_value
355327

356328

357329
if __name__ == '__main__':

0 commit comments

Comments
 (0)
Please sign in to comment.