Skip to content

Exporter

Exporter

Bases: ABC

Abstract class to export engines and its components to different formats.

related

Functions

__repr__

__repr__() -> str

Return code to construct the exporter in Python.

Returns:

Type Description
str

code to construct the exporter in Python

__str__

__str__() -> str

Return the class name of the exporter.

Returns:

Type Description
str

class name of the exporter

to_file

to_file(path: str | Path, instance: Any) -> None

Write the string representation of the instance into the file.

Parameters:

Name Type Description Default
path str | Path

file path to export the instance.

required
instance Any

a fuzzylite object.

required

to_string abstractmethod

to_string(instance: Any) -> str

Return string representation of the instance.

Parameters:

Name Type Description Default
instance Any

a fuzzylite object

required

Returns:

Type Description
str

string representation of the object

FldExporter

Bases: Exporter

Export the input values and output values of an engine to the FuzzyLite Dataset (FLD) format.

related
warning

FldExporter uses vectorization so it only works with the fuzzylite.activation.General activation method

todo

include option for non-vectorized export so other activation methods can be used

Attributes

headers instance-attribute

headers = headers

input_values instance-attribute

input_values = input_values

output_values instance-attribute

output_values = output_values

separator instance-attribute

separator = separator

Classes

ScopeOfValues

Bases: Enum

Scope of the equally-distributed values to generate.

  • EachVariable: Generates \(v\) values for each variable, resulting in a total resolution of \(-1 + \max(1, v^{\frac{1}{|I|})\) from all combinations, where \(I\) refers to the input variables.
  • AllVariables: Generates values for each variable such that the total resolution is \(v\).
Attributes
AllVariables class-attribute instance-attribute
AllVariables = auto()
EachVariable class-attribute instance-attribute
EachVariable = auto()

Functions

__init__

__init__(separator: str = ' ', headers: bool = True, input_values: bool = True, output_values: bool = True) -> None

Constructor.

Parameters:

Name Type Description Default
separator str

separator of the dataset columns

' '
headers bool

whether to export the header of the dataset

True
input_values bool

whether to export the input values

True
output_values bool

whether to export the output values.

True

header

header(engine: Engine) -> str

Return the header of the dataset for the engine.

Parameters:

Name Type Description Default
engine Engine

engine to export

required

Returns:

Type Description
str

header of the dataset for the engine.

to_file_from_reader

to_file_from_reader(path: Path, engine: Engine, reader: IO[str], skip_lines: int = 0) -> None

Write the FuzzyLite Dataset to the file using the input values from the engine.

Parameters:

Name Type Description Default
path Path

path to the output file

required
engine Engine

engine to export

required
reader IO[str]

reader of a set of lines containing space-separated input values

required
skip_lines int

number of lines to skip from the beginning.

0

Returns:

Type Description
None

FuzzyLite Dataset from the engine.

to_file_from_scope

to_file_from_scope(
    path: Path,
    engine: Engine,
    values: int = 1024,
    scope: ScopeOfValues = AllVariables,
    active_variables: set[InputVariable] | None = None,
) -> None

Write the FuzzyLite Dataset from the engine to the file using the input values and their scope.

Parameters:

Name Type Description Default
path Path

file path

required
engine Engine

engine to export

required
values int

number of values to export

1024
scope ScopeOfValues

scope of the values

AllVariables
active_variables set[InputVariable] | None

set of input variables to set values for

None

to_string

to_string(instance: object) -> str

Return a FuzzyLite Dataset from the engine using 1024 input values for all variables.

Parameters:

Name Type Description Default
instance object

engine to export

required

Returns:

Type Description
str

FuzzyLite Dataset from the engine

Raises:

Type Description
ValueError

if the instance is not an Engine.

to_string_from_reader

to_string_from_reader(engine: Engine, reader: IO[str], skip_lines: int = 0) -> str

Return a FuzzyLite Dataset from the engine using the input values from the reader.

Parameters:

Name Type Description Default
engine Engine

engine to export

required
reader IO[str]

reader of a set of lines containing space-separated input values

required
skip_lines int

number of lines to skip from the beginning

0

Returns:

Type Description
str

FuzzyLite Dataset from the engine.

to_string_from_scope

to_string_from_scope(
    engine: Engine,
    values: int = 1024,
    scope: ScopeOfValues = AllVariables,
    active_variables: set[InputVariable] | None = None,
) -> str

Return a FuzzyLite Dataset from the engine using the input values and their scope.

Parameters:

Name Type Description Default
engine Engine

engine to export

required
values int

number of values to export

1024
scope ScopeOfValues

scope of the values

AllVariables
active_variables set[InputVariable] | None

input variables to set values for

None

Returns:

Type Description
str

FuzzyLite Dataset from the engine.

write

write(engine: Engine, writer: IO[str], input_values: Scalar) -> None

Write a FuzzyLite Dataset line from the engine to the writer using the input values.

Parameters:

Name Type Description Default
engine Engine

engine to export

required
writer IO[str]

output where the engine will be written to

required
input_values Scalar

matrix of input values.

required

write_from_reader

write_from_reader(engine: Engine, writer: IO[str], reader: IO[str], skip_lines: int = 0) -> None

Write the FuzzyLite Dataset from the engine to the writer using the input values from the reader.

Parameters:

Name Type Description Default
engine Engine

engine to export

required
writer IO[str]

output to write the engine

required
reader IO[str]

reader of a set of lines containing space-separated input values

required
skip_lines int

number of lines to skip from the beginning.

0

write_from_scope

write_from_scope(
    engine: Engine,
    writer: IO[str],
    values: int,
    scope: ScopeOfValues,
    active_variables: set[InputVariable] | None = None,
) -> None

Write a FuzzyLite dataset from the engine to the writer.

Parameters:

Name Type Description Default
engine Engine

engine to export

required
writer IO[str]

output to write the engine

required
values int

number of values to export

required
scope ScopeOfValues

scope of the values

required
active_variables set[InputVariable] | None

input variables to generate values for.

None

FllExporter

Bases: Exporter

Export an engine and its components to the FuzzyLite Language.

related

Attributes

indent instance-attribute

indent = indent

separator instance-attribute

separator = separator

Functions

__init__

__init__(indent: str = '  ', separator: str = '\n') -> None

Constructor.

Parameters:

Name Type Description Default
indent str

indentation of the FuzzyLite Language.

' '
separator str

separation between components of the FuzzyLite Language.

'\n'

activation

activation(activation: Activation | None) -> str

Return the activation method in the FuzzyLite Language.

Parameters:

Name Type Description Default
activation Activation | None

activation method to export

required

Returns:

Type Description
str

activation method in the FuzzyLite Language

defuzzifier

defuzzifier(defuzzifier: Defuzzifier | None) -> str

Return the defuzzifier in the FuzzyLite Language.

Parameters:

Name Type Description Default
defuzzifier Defuzzifier | None

defuzzifier to export

required

Returns:

Type Description
str

defuzzifier in the FuzzyLite Language

engine

engine(engine: Engine) -> str

Return the engine in the FuzzyLite Language.

Parameters:

Name Type Description Default
engine Engine

engine to export

required

Returns:

Type Description
str

engine in the FuzzyLite Language

format

format(key: str | None, value: Any) -> str

Format the arguments according to the FuzzyLite Language.

formatting table
value formatted
None none
bool true, false
float 0.999 using fuzzylite.library.Settings.decimals
list|set|tuple space-separated values, each formatted with this method
object anything else uses the object's __str__() method

Parameters:

Name Type Description Default
key str | None

name of the property

required
value Any

value to format

required

Returns:

Type Description
str

formatted (key and) value according to the FuzzyLite Language

input_variable

input_variable(variable: InputVariable) -> str

Return the input variable in the FuzzyLite Language.

Parameters:

Name Type Description Default
variable InputVariable

input variable to export

required

Returns: input variable in the FuzzyLite Language

norm

norm(norm: Norm | None) -> str

Return the norm in the FuzzyLite Language.

Parameters:

Name Type Description Default
norm Norm | None

norm to export

required

Returns:

Type Description
str

norm in the FuzzyLite Language

output_variable

output_variable(variable: OutputVariable) -> str

Return the output variable in the FuzzyLite Language.

Parameters:

Name Type Description Default
variable OutputVariable

output variable to export

required

Returns:

Type Description
str

output variable in the FuzzyLite Language

rule

rule(rule: Rule) -> str

Return the rule in the FuzzyLite Language.

Parameters:

Name Type Description Default
rule Rule

rule to export

required

Returns:

Type Description
str

rule in the FuzzyLite Language

rule_block

rule_block(rule_block: RuleBlock) -> str

Return the rule block in the FuzzyLite Language.

Parameters:

Name Type Description Default
rule_block RuleBlock

rule block to export

required

Returns:

Type Description
str

rule block in the FuzzyLite Language

term

term(term: Term) -> str

Return the term in the FuzzyLite Language.

Parameters:

Name Type Description Default
term Term

term to export

required

Returns:

Type Description
str

term in the FuzzyLite Language

to_string

to_string(instance: Any) -> str

Return the object in the FuzzyLite Language.

Parameters:

Name Type Description Default
instance Any

fuzzylite object.

required

Returns:

Type Description
str

object in the FuzzyLite Language.

variable

variable(variable: Variable, /, terms: bool = True) -> str

Return the variable in the FuzzyLite Language.

Parameters:

Name Type Description Default
variable Variable

variable to export

required
terms bool

whether to export the terms

True

Returns:

Type Description
str

variable in the FuzzyLite Language

PythonExporter

Bases: Exporter

Export an engine and its components to Python.

related

Attributes

encapsulated instance-attribute

encapsulated = encapsulated

formatted instance-attribute

formatted = formatted

Functions

__init__

__init__(formatted: bool = True, encapsulated: bool = False) -> None

Constructor.

Parameters:

Name Type Description Default
formatted bool

try to format the code using black if it is installed

True
encapsulated bool

whether to encapsulate the code (using classes for engines and methods for other components).

False

activation

activation(activation: Activation | None) -> str

Return the code to construct the activation method in Python.

Parameters:

Name Type Description Default
activation Activation | None

activation method to export

required

Returns:

Type Description
str

code to construct the activation method in Python

defuzzifier

defuzzifier(defuzzifier: Defuzzifier | None) -> str

Return the code to construct the defuzzifier in Python.

Parameters:

Name Type Description Default
defuzzifier Defuzzifier | None

defuzzifier to export

required

Returns:

Type Description
str

code to construct the defuzzifier in Python

encapsulate

encapsulate(instance: Any) -> str

Encapsulate the instance in a new class if it is an engine, or in a create method otherwise.

Parameters:

Name Type Description Default
instance Any

object to encapsulate

required

Returns:

Type Description
str

if the instance is an engine, then the class constructing the engine during initialization;

str

otherwise a method constructing the object

engine

engine(engine: Engine) -> str

Return the code to construct the engine in Python.

Parameters:

Name Type Description Default
engine Engine

engine to export

required

Returns:

Type Description
str

code to construct the engine in Python

format

format(code: str, **kwargs: Any) -> str

Format the code using the black formatter if it is installed, otherwise no effects on the code.

Parameters:

Name Type Description Default
code str

code to format.

required
**kwargs Any

keyword arguments to pass to black.Mode

{}

Returns: code formatted if black is installed, otherwise the code without format

input_variable

input_variable(input_variable: InputVariable) -> str

Return the code to construct the input variable in Python.

Parameters:

Name Type Description Default
input_variable InputVariable

input variable to export

required

Returns:

Type Description
str

code to construct the input variable in Python

norm

norm(norm: Norm | None) -> str

Return the code to construct the norm in Python.

Parameters:

Name Type Description Default
norm Norm | None

norm to export

required

Returns:

Type Description
str

code to construct the norm in Python

output_variable

output_variable(output_variable: OutputVariable) -> str

Return the code to construct the output variable in Python.

Parameters:

Name Type Description Default
output_variable OutputVariable

output variable to export

required

Returns:

Type Description
str

code to construct the output variable in Python

rule

rule(rule: Rule) -> str

Return the code to construct the rule in Python.

Parameters:

Name Type Description Default
rule Rule

rule to export

required

Returns:

Type Description
str

code to construct the rule in Python

rule_block

rule_block(rule_block: RuleBlock) -> str

Return the code to construct the rule block in Python.

Parameters:

Name Type Description Default
rule_block RuleBlock

rule block variable to export

required

Returns:

Type Description
str

code to construct the rule block in Python

term

term(term: Term) -> str

Return the code to construct the term in Python.

Parameters:

Name Type Description Default
term Term

term to export

required

Returns:

Type Description
str

code to construct the term in Python

to_string

to_string(instance: Any) -> str

Return the code to construct the instance in Python.

Parameters:

Name Type Description Default
instance Any

fuzzylite object

required

Returns:

Type Description
str

code to construct the instance in Python