def test_defuzzifier(self) -> None: self.assertEqual(fl.FllExporter().defuzzifier(None), "none") defuzzifier = fl.Centroid() self.assertEqual(fl.FllExporter().to_string(defuzzifier), fl.FllExporter().defuzzifier(defuzzifier)) self.assertEqual(fl.FllExporter().defuzzifier(defuzzifier), "Centroid 100")
def test_engine(self) -> None: engine = fl.Engine( name="engine", description="an engine", input_variables=[ fl.InputVariable(name="input_variable", description="an input variable", minimum=0, maximum=1, terms=[fl.Triangle("A")]) ], output_variables=[ fl.OutputVariable(name="output_variable", description="an output variable", minimum=0, maximum=1, terms=[fl.Triangle("A")]) ], rule_blocks=[ fl.RuleBlock(name="rb", description="a rule block", rules=[fl.Rule.create("if a then z")]) ]) self.assertEqual(fl.FllExporter().to_string(engine), fl.FllExporter().engine(engine)) self.assertEqual( fl.FllExporter().engine(engine), """\ Engine: engine description: an engine InputVariable: input_variable description: an input variable enabled: true range: 0 1 lock-range: false term: A Triangle nan nan nan OutputVariable: output_variable description: an output variable enabled: true range: 0 1 lock-range: false aggregation: none defuzzifier: none default: nan lock-previous: false term: A Triangle nan nan nan RuleBlock: rb description: a rule block enabled: true conjunction: none disjunction: none implication: none activation: none rule: if a then z """)
def test_variable(self) -> None: variable = fl.Variable(name="variable", description="a variable", minimum=0, maximum=1, terms=[fl.Triangle("A")]) self.assertEqual(fl.FllExporter().to_string(variable), fl.FllExporter().variable(variable)) self.assertEqual(fl.FllExporter().variable(variable), """\ Variable: variable description: a variable enabled: true range: 0 1 lock-range: false term: A Triangle nan nan nan""")
def test_rule_block(self) -> None: rb = fl.RuleBlock(name="rb", description="a rule block", rules=[fl.Rule.create("if a then z")]) self.assertEqual(fl.FllExporter().to_string(rb), fl.FllExporter().rule_block(rb)) self.assertEqual(fl.FllExporter().rule_block(rb), """\ RuleBlock: rb description: a rule block enabled: true conjunction: none disjunction: none implication: none activation: none rule: if a then z""")
def test_single_line_indent(self) -> None: engine = fl.Engine("engine", "single line export to FLL", [fl.InputVariable("A", "variable A")], [fl.OutputVariable("Z", "variable Z")], [fl.RuleBlock("R", "rule block R")]) self.assertEqual( fl.FllExporter(separator="; ", indent='\t').engine(engine), "Engine: engine; " "\tdescription: single line export to FLL; " "InputVariable: A; " "\tdescription: variable A; " "\tenabled: true; " "\trange: -inf inf; " "\tlock-range: false; " "OutputVariable: Z; " "\tdescription: variable Z; " "\tenabled: true; " "\trange: -inf inf; " "\tlock-range: false; " "\taggregation: none; " "\tdefuzzifier: none; " "\tdefault: nan; " "\tlock-previous: false; " "RuleBlock: R; " "\tdescription: rule block R; " "\tenabled: true; " "\tconjunction: none; " "\tdisjunction: none; " "\timplication: none; " "\tactivation: none; ")
def export(path: str) -> None: import io import time fl.lib.decimals = 9 importer = fl.FllImporter() exporters = [ fl.FllExporter(), fl.PythonExporter(), # fl.FldExporter() ] with io.open(path, 'r') as file: import_fll = file.read() engine = importer.from_string(import_fll) file_name = file.name[file.name.rfind('/'):file.name.rfind('.')] for exporter in exporters: start = time.time() if isinstance(exporter, fl.FldExporter): exporter.to_file_from_scope( Path("/tmp/fl/" + file_name + ".fld"), engine, 100_000) elif isinstance(exporter, fl.FllExporter): exporter.to_file(Path("/tmp/fl/" + file_name + ".fll"), engine) elif isinstance(exporter, fl.PythonExporter): exporter.to_file(Path("/tmp/fl/" + file_name + ".py"), engine) fl.lib.logger.info(str(path) + f".fld\t{time.time() - start}")
def test_output_variable(self) -> None: variable = fl.OutputVariable(name="output_variable", description="an output variable", minimum=0, maximum=1, terms=[fl.Triangle("A")]) self.assertEqual(fl.FllExporter().to_string(variable), fl.FllExporter().output_variable(variable)) self.assertEqual(fl.FllExporter().output_variable(variable), """\ OutputVariable: output_variable description: an output variable enabled: true range: 0 1 lock-range: false aggregation: none defuzzifier: none default: nan lock-previous: false term: A Triangle nan nan nan""")
def export(file_path: str) -> None: import io import time import pathlib import importlib import numpy as np np.seterr(divide="ignore", invalid="ignore") fl.lib.floating_point_type = np.float64 path = pathlib.Path(file_path) if path.suffix == ".fll": with io.open(path, 'r') as file: import_fll = file.read() engine = fl.FllImporter().from_string(import_fll) elif path.suffix == ".py": package: List[str] = [] for parent in path.parents: package.append(parent.name) if parent.name == "fuzzylite": break module = ".".join(reversed(package)) + f".{path.stem}" engine = importlib.import_module(module).engine # type: ignore else: raise Exception(f"unknown importer of files like {path}") exporters = [fl.FllExporter(), fl.PythonExporter(), fl.FldExporter()] file_name = path.stem for exporter in exporters: start = time.time() target_path = Path( "/tmp/fl/") / path.parent.parent.stem / path.parent.stem target_path.mkdir(parents=True, exist_ok=True) fl.lib.decimals = 3 fl.lib.logger.info(str(path) + f" -> {exporter.class_name}") if isinstance(exporter, fl.FldExporter): fl.lib.decimals = 9 exporter.to_file_from_scope(target_path / (file_name + ".fld"), engine, 1024) elif isinstance(exporter, fl.FllExporter): exporter.to_file(target_path / (file_name + ".fll"), engine) elif isinstance(exporter, fl.PythonExporter): exporter.to_file(target_path / (file_name + ".py"), engine) fl.lib.logger.info( str(path) + f" -> {exporter.class_name}\t{time.time() - start}")
def test_import_examples(self) -> None: self.maxDiff = None # show all differences importer = fl.FllImporter() exporter = fl.FllExporter() fl.lib.decimals = 9 import logging fl.lib.logger.setLevel(logging.INFO) import fuzzylite.examples.terms terms = next(iter(fuzzylite.examples.terms.__path__)) # type: ignore counter = 0 for fll_file in glob.iglob(terms + '/*.fll', recursive=True): counter += 1 with open(fll_file, 'r') as file: fl.lib.logger.info(fll_file) fll_from_string = file.read() engine = importer.from_string(fll_from_string) export_fll = exporter.to_string(engine) self.assertEqual(fll_from_string, export_fll) self.assertEqual(20, counter) fl.lib.decimals = 3
def test_object(self) -> None: with self.assertRaisesRegex( ValueError, rf"expected a fuzzylite object, but found 'object"): fl.FllExporter().to_string(object())
def test_activation(self) -> None: self.assertEqual(fl.FllExporter().activation(None), "none") activation = fl.General() self.assertEqual(fl.FllExporter().to_string(activation), fl.FllExporter().activation(activation)) self.assertEqual(fl.FllExporter().activation(activation), "General")
def test_norm(self) -> None: self.assertEqual(fl.FllExporter().norm(None), "none") norm = fl.AlgebraicProduct() self.assertEqual(fl.FllExporter().to_string(norm), fl.FllExporter().norm(norm)) self.assertEqual(fl.FllExporter().norm(norm), "AlgebraicProduct")
def test_rule(self) -> None: rule = fl.Rule.create("if a then z") self.assertEqual(fl.FllExporter().to_string(rule), fl.FllExporter().rule(rule)) self.assertEqual(fl.FllExporter().rule(rule), "rule: if a then z")
def test_term(self) -> None: term = fl.Triangle("A", 0.0, 1.0, 2.0, 0.5) self.assertEqual(fl.FllExporter().to_string(term), fl.FllExporter().term(term)) self.assertEqual(fl.FllExporter().term(term), "term: A Triangle 0.000 1.000 2.000 0.500")
def exports_fll(self, fll: str) -> Any: self.test.assertEqual(fll, fl.FllExporter().to_string(self.actual)) self.test.assertEqual(fll, str(self.actual)) return self
def exports_fll(self, fll: str) -> 'OutputVariableAssert': self.test.assertEqual(fl.FllExporter().output_variable(self.actual), fll) return self