Skip to content

Commit 75f52d2

Browse files
committed
Remove code with side effects from main
When running tests withy pytest, some modules execute code in main scope and open plot or browser windows. Moves such code under `if __name__ == "__main__"`.
1 parent 9a894eb commit 75f52d2

File tree

4 files changed

+500
-499
lines changed

4 files changed

+500
-499
lines changed

fuzzy_logic/fuzzy_operations.py

+91-91
Original file line numberDiff line numberDiff line change
@@ -6,97 +6,97 @@
66
Python:
77
- 3.5
88
"""
9-
# Create universe of discourse in python using linspace ()
109
import numpy as np
11-
12-
X = np.linspace(start=0, stop=75, num=75, endpoint=True, retstep=False)
13-
14-
# Create two fuzzy sets by defining any membership function (trapmf(), gbellmf(),gaussmf(), etc).
1510
import skfuzzy as fuzz
1611

17-
abc1 = [0, 25, 50]
18-
abc2 = [25, 50, 75]
19-
young = fuzz.membership.trimf(X, abc1)
20-
middle_aged = fuzz.membership.trimf(X, abc2)
21-
22-
# Compute the different operations using inbuilt functions.
23-
one = np.ones(75)
24-
zero = np.zeros((75,))
25-
# 1. Union = max(µA(x), µB(x))
26-
union = fuzz.fuzzy_or(X, young, X, middle_aged)[1]
27-
# 2. Intersection = min(µA(x), µB(x))
28-
intersection = fuzz.fuzzy_and(X, young, X, middle_aged)[1]
29-
# 3. Complement (A) = (1- min(µA(x))
30-
complement_a = fuzz.fuzzy_not(young)
31-
# 4. Difference (A/B) = min(µA(x),(1- µB(x)))
32-
difference = fuzz.fuzzy_and(X, young, X, fuzz.fuzzy_not(middle_aged)[1])[1]
33-
# 5. Algebraic Sum = [µA(x) + µB(x) – (µA(x) * µB(x))]
34-
alg_sum = young + middle_aged - (young * middle_aged)
35-
# 6. Algebraic Product = (µA(x) * µB(x))
36-
alg_product = young * middle_aged
37-
# 7. Bounded Sum = min[1,(µA(x), µB(x))]
38-
bdd_sum = fuzz.fuzzy_and(X, one, X, young + middle_aged)[1]
39-
# 8. Bounded difference = min[0,(µA(x), µB(x))]
40-
bdd_difference = fuzz.fuzzy_or(X, zero, X, young - middle_aged)[1]
41-
42-
# max-min composition
43-
# max-product composition
44-
45-
46-
# Plot each set A, set B and each operation result using plot() and subplot().
47-
import matplotlib.pyplot as plt
48-
49-
plt.figure()
50-
51-
plt.subplot(4, 3, 1)
52-
plt.plot(X, young)
53-
plt.title("Young")
54-
plt.grid(True)
55-
56-
plt.subplot(4, 3, 2)
57-
plt.plot(X, middle_aged)
58-
plt.title("Middle aged")
59-
plt.grid(True)
60-
61-
plt.subplot(4, 3, 3)
62-
plt.plot(X, union)
63-
plt.title("union")
64-
plt.grid(True)
65-
66-
plt.subplot(4, 3, 4)
67-
plt.plot(X, intersection)
68-
plt.title("intersection")
69-
plt.grid(True)
70-
71-
plt.subplot(4, 3, 5)
72-
plt.plot(X, complement_a)
73-
plt.title("complement_a")
74-
plt.grid(True)
75-
76-
plt.subplot(4, 3, 6)
77-
plt.plot(X, difference)
78-
plt.title("difference a/b")
79-
plt.grid(True)
80-
81-
plt.subplot(4, 3, 7)
82-
plt.plot(X, alg_sum)
83-
plt.title("alg_sum")
84-
plt.grid(True)
85-
86-
plt.subplot(4, 3, 8)
87-
plt.plot(X, alg_product)
88-
plt.title("alg_product")
89-
plt.grid(True)
90-
91-
plt.subplot(4, 3, 9)
92-
plt.plot(X, bdd_sum)
93-
plt.title("bdd_sum")
94-
plt.grid(True)
95-
96-
plt.subplot(4, 3, 10)
97-
plt.plot(X, bdd_difference)
98-
plt.title("bdd_difference")
99-
plt.grid(True)
100-
101-
plt.subplots_adjust(hspace=0.5)
102-
plt.show()
12+
13+
if __name__ == "__main__":
14+
# Create universe of discourse in python using linspace ()
15+
X = np.linspace(start=0, stop=75, num=75, endpoint=True, retstep=False)
16+
17+
# Create two fuzzy sets by defining any membership function (trapmf(), gbellmf(),gaussmf(), etc).
18+
abc1 = [0, 25, 50]
19+
abc2 = [25, 50, 75]
20+
young = fuzz.membership.trimf(X, abc1)
21+
middle_aged = fuzz.membership.trimf(X, abc2)
22+
23+
# Compute the different operations using inbuilt functions.
24+
one = np.ones(75)
25+
zero = np.zeros((75,))
26+
# 1. Union = max(µA(x), µB(x))
27+
union = fuzz.fuzzy_or(X, young, X, middle_aged)[1]
28+
# 2. Intersection = min(µA(x), µB(x))
29+
intersection = fuzz.fuzzy_and(X, young, X, middle_aged)[1]
30+
# 3. Complement (A) = (1- min(µA(x))
31+
complement_a = fuzz.fuzzy_not(young)
32+
# 4. Difference (A/B) = min(µA(x),(1- µB(x)))
33+
difference = fuzz.fuzzy_and(X, young, X, fuzz.fuzzy_not(middle_aged)[1])[1]
34+
# 5. Algebraic Sum = [µA(x) + µB(x) – (µA(x) * µB(x))]
35+
alg_sum = young + middle_aged - (young * middle_aged)
36+
# 6. Algebraic Product = (µA(x) * µB(x))
37+
alg_product = young * middle_aged
38+
# 7. Bounded Sum = min[1,(µA(x), µB(x))]
39+
bdd_sum = fuzz.fuzzy_and(X, one, X, young + middle_aged)[1]
40+
# 8. Bounded difference = min[0,(µA(x), µB(x))]
41+
bdd_difference = fuzz.fuzzy_or(X, zero, X, young - middle_aged)[1]
42+
43+
# max-min composition
44+
# max-product composition
45+
46+
# Plot each set A, set B and each operation result using plot() and subplot().
47+
import matplotlib.pyplot as plt
48+
49+
plt.figure()
50+
51+
plt.subplot(4, 3, 1)
52+
plt.plot(X, young)
53+
plt.title("Young")
54+
plt.grid(True)
55+
56+
plt.subplot(4, 3, 2)
57+
plt.plot(X, middle_aged)
58+
plt.title("Middle aged")
59+
plt.grid(True)
60+
61+
plt.subplot(4, 3, 3)
62+
plt.plot(X, union)
63+
plt.title("union")
64+
plt.grid(True)
65+
66+
plt.subplot(4, 3, 4)
67+
plt.plot(X, intersection)
68+
plt.title("intersection")
69+
plt.grid(True)
70+
71+
plt.subplot(4, 3, 5)
72+
plt.plot(X, complement_a)
73+
plt.title("complement_a")
74+
plt.grid(True)
75+
76+
plt.subplot(4, 3, 6)
77+
plt.plot(X, difference)
78+
plt.title("difference a/b")
79+
plt.grid(True)
80+
81+
plt.subplot(4, 3, 7)
82+
plt.plot(X, alg_sum)
83+
plt.title("alg_sum")
84+
plt.grid(True)
85+
86+
plt.subplot(4, 3, 8)
87+
plt.plot(X, alg_product)
88+
plt.title("alg_product")
89+
plt.grid(True)
90+
91+
plt.subplot(4, 3, 9)
92+
plt.plot(X, bdd_sum)
93+
plt.title("bdd_sum")
94+
plt.grid(True)
95+
96+
plt.subplot(4, 3, 10)
97+
plt.plot(X, bdd_difference)
98+
plt.title("bdd_difference")
99+
plt.grid(True)
100+
101+
plt.subplots_adjust(hspace=0.5)
102+
plt.show()

machine_learning/polymonial_regression.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ def viz_polymonial():
3636
return
3737

3838

39-
viz_polymonial()
39+
if __name__ == "__main__":
40+
viz_polymonial()
4041

41-
# Predicting a new result with Polymonial Regression
42-
pol_reg.predict(poly_reg.fit_transform([[5.5]]))
43-
# output should be 132148.43750003
42+
# Predicting a new result with Polymonial Regression
43+
pol_reg.predict(poly_reg.fit_transform([[5.5]]))
44+
# output should be 132148.43750003

0 commit comments

Comments
 (0)