16
16
17
17
class FuzzyWarning (UserWarning ):
18
18
"""Extra Exception so that user code can filter exceptions specific to this lib."""
19
+
19
20
pass
20
21
21
22
@@ -55,10 +56,10 @@ class Domain:
55
56
def __init__ (
56
57
self ,
57
58
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 ,
62
63
) -> None :
63
64
"""Define a domain."""
64
65
assert low < high , "higher bound must be greater than lower."
@@ -126,9 +127,7 @@ def __delattr__(self, name):
126
127
if name in self ._sets :
127
128
del self ._sets [name ]
128
129
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." )
132
131
133
132
@property
134
133
def range (self ):
@@ -142,9 +141,7 @@ def range(self):
142
141
if int (self ._res ) == self ._res :
143
142
return np .arange (self ._low , self ._high + self ._res , int (self ._res ))
144
143
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 )
148
145
149
146
def min (self , x ):
150
147
"""Standard way to get the min over all membership funcs.
@@ -153,11 +150,11 @@ def min(self, x):
153
150
to calculate all results, construct a dict, unpack the dict
154
151
and calculate the min from that.
155
152
"""
156
- return min (f (x ) for f in self ._sets .values ())
153
+ return min (( f (x ) for f in self ._sets .values ()), default = 0 )
157
154
158
155
def max (self , x ):
159
156
"""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 )
161
158
162
159
163
160
class Set :
@@ -185,7 +182,7 @@ class Set:
185
182
name = None # these are set on assignment to the domain! DO NOT MODIFY
186
183
domain = None
187
184
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 ):
189
186
self .func = func
190
187
self .domain = domain
191
188
self .name = name
@@ -351,7 +348,6 @@ def center_of_gravity(self):
351
348
self .__center_of_gravity = cog
352
349
return cog
353
350
354
-
355
351
def __repr__ (self ):
356
352
"""
357
353
Return a string representation of the Set that reconstructs the set with eval().
@@ -400,7 +396,7 @@ class Rule:
400
396
401
397
def __init__ (self , conditions , func = None ):
402
398
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 ()}
404
400
self .func = func
405
401
406
402
def __add__ (self , other ):
@@ -431,17 +427,13 @@ def __call__(self, args: "dict[Domain, float]", method="cog"):
431
427
assert len (args ) == max (
432
428
len (c ) for c in self .conditions .keys ()
433
429
), "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."
437
431
match method :
438
432
case "cog" :
439
433
assert (
440
434
len ({C .domain for C in self .conditions .values ()}) == 1
441
435
), "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 }
445
437
446
438
weights = []
447
439
for K , v in self .conditions .items ():
@@ -452,9 +444,7 @@ def __call__(self, args: "dict[Domain, float]", method="cog"):
452
444
if not weights :
453
445
return None
454
446
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 )
458
448
return (target_domain ._high - target_domain ._low ) / len (
459
449
target_domain .range
460
450
) * index + target_domain ._low
@@ -467,11 +457,12 @@ def __call__(self, args: "dict[Domain, float]", method="cog"):
467
457
raise NotImplementedError ("Middle of max method not implemented yet." )
468
458
case "som" :
469
459
raise NotImplementedError ("Smallest of max method not implemented yet." )
470
- case "lom" :
460
+ case "lom" :
471
461
raise NotImplementedError ("Largest of max method not implemented yet." )
472
462
case _:
473
463
raise ValueError ("Invalid method." )
474
464
465
+
475
466
def rule_from_table (table : str , references : dict ):
476
467
"""Turn a (2D) string table into a Rule of fuzzy sets.
477
468
@@ -489,7 +480,7 @@ def rule_from_table(table: str, references: dict):
489
480
490
481
import pandas as pd
491
482
492
- df = pd .read_table (io .StringIO (table ), sep = ' \s+' )
483
+ df = pd .read_table (io .StringIO (table ), sep = r" \s+" )
493
484
494
485
D : dict [tuple [Any , Any ], Any ] = {
495
486
(
0 commit comments