Skip to content

Commit 4ac5001

Browse files
committed
chore: Update Domain class initialization arguments
The `Domain` class in `fuzzylogic/classes.py` has been updated to use more descriptive argument names for `low`, `high`, `res`, and `sets`. This change improves the clarity and readability of the code.
1 parent 34c9288 commit 4ac5001

File tree

1 file changed

+17
-26
lines changed

1 file changed

+17
-26
lines changed

src/fuzzylogic/classes.py

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
class FuzzyWarning(UserWarning):
1818
"""Extra Exception so that user code can filter exceptions specific to this lib."""
19+
1920
pass
2021

2122

@@ -55,10 +56,10 @@ class Domain:
5556
def __init__(
5657
self,
5758
name: str,
58-
low: float|int,
59-
high: float|int,
60-
res: float|int = 1,
61-
sets: dict|None = None,
59+
low: float | int,
60+
high: float | int,
61+
res: float | int = 1,
62+
sets: dict | None = None,
6263
) -> None:
6364
"""Define a domain."""
6465
assert low < high, "higher bound must be greater than lower."
@@ -126,9 +127,7 @@ def __delattr__(self, name):
126127
if name in self._sets:
127128
del self._sets[name]
128129
else:
129-
raise FuzzyWarning(
130-
"Trying to delete a regular attr, this needs extra care."
131-
)
130+
raise FuzzyWarning("Trying to delete a regular attr, this needs extra care.")
132131

133132
@property
134133
def range(self):
@@ -142,9 +141,7 @@ def range(self):
142141
if int(self._res) == self._res:
143142
return np.arange(self._low, self._high + self._res, int(self._res))
144143
else:
145-
return np.linspace(
146-
self._low, self._high, int((self._high - self._low) / self._res) + 1
147-
)
144+
return np.linspace(self._low, self._high, int((self._high - self._low) / self._res) + 1)
148145

149146
def min(self, x):
150147
"""Standard way to get the min over all membership funcs.
@@ -153,11 +150,11 @@ def min(self, x):
153150
to calculate all results, construct a dict, unpack the dict
154151
and calculate the min from that.
155152
"""
156-
return min(f(x) for f in self._sets.values())
153+
return min((f(x) for f in self._sets.values()), default=0)
157154

158155
def max(self, x):
159156
"""Standard way to get the max over all membership funcs."""
160-
return max(f(x) for f in self._sets.values())
157+
return max((f(x) for f in self._sets.values()), default=0)
161158

162159

163160
class Set:
@@ -185,7 +182,7 @@ class Set:
185182
name = None # these are set on assignment to the domain! DO NOT MODIFY
186183
domain = None
187184

188-
def __init__(self, func: Callable, *, name:str|None=None, domain:Domain|None=None):
185+
def __init__(self, func: Callable, *, name: str | None = None, domain: Domain | None = None):
189186
self.func = func
190187
self.domain = domain
191188
self.name = name
@@ -351,7 +348,6 @@ def center_of_gravity(self):
351348
self.__center_of_gravity = cog
352349
return cog
353350

354-
355351
def __repr__(self):
356352
"""
357353
Return a string representation of the Set that reconstructs the set with eval().
@@ -400,7 +396,7 @@ class Rule:
400396

401397
def __init__(self, conditions, func=None):
402398
print("ohalala")
403-
self.conditions = {frozenset(C): oth for C, oth, in conditions.items()}
399+
self.conditions = {frozenset(C): oth for C, oth in conditions.items()}
404400
self.func = func
405401

406402
def __add__(self, other):
@@ -431,17 +427,13 @@ def __call__(self, args: "dict[Domain, float]", method="cog"):
431427
assert len(args) == max(
432428
len(c) for c in self.conditions.keys()
433429
), "Number of values must correspond to the number of domains defined as conditions!"
434-
assert isinstance(
435-
args, dict
436-
), "Please make sure to pass in the values as a dictionary."
430+
assert isinstance(args, dict), "Please make sure to pass in the values as a dictionary."
437431
match method:
438432
case "cog":
439433
assert (
440434
len({C.domain for C in self.conditions.values()}) == 1
441435
), "For CoG, all conditions must have the same target domain."
442-
actual_values = {
443-
f: f(args[f.domain]) for S in self.conditions.keys() for f in S
444-
}
436+
actual_values = {f: f(args[f.domain]) for S in self.conditions.keys() for f in S}
445437

446438
weights = []
447439
for K, v in self.conditions.items():
@@ -452,9 +444,7 @@ def __call__(self, args: "dict[Domain, float]", method="cog"):
452444
if not weights:
453445
return None
454446
target_domain = list(self.conditions.values())[0].domain
455-
index = sum(v.center_of_gravity * x for v, x in weights) / sum(
456-
x for v, x in weights
457-
)
447+
index = sum(v.center_of_gravity * x for v, x in weights) / sum(x for v, x in weights)
458448
return (target_domain._high - target_domain._low) / len(
459449
target_domain.range
460450
) * index + target_domain._low
@@ -467,11 +457,12 @@ def __call__(self, args: "dict[Domain, float]", method="cog"):
467457
raise NotImplementedError("Middle of max method not implemented yet.")
468458
case "som":
469459
raise NotImplementedError("Smallest of max method not implemented yet.")
470-
case "lom":
460+
case "lom":
471461
raise NotImplementedError("Largest of max method not implemented yet.")
472462
case _:
473463
raise ValueError("Invalid method.")
474464

465+
475466
def rule_from_table(table: str, references: dict):
476467
"""Turn a (2D) string table into a Rule of fuzzy sets.
477468
@@ -489,7 +480,7 @@ def rule_from_table(table: str, references: dict):
489480

490481
import pandas as pd
491482

492-
df = pd.read_table(io.StringIO(table), sep='\s+')
483+
df = pd.read_table(io.StringIO(table), sep=r"\s+")
493484

494485
D: dict[tuple[Any, Any], Any] = {
495486
(

0 commit comments

Comments
 (0)