Classification persistence#
Persistence Module for Ex-Fuzzy Library
This module provides functionality for loading and saving fuzzy rule systems and fuzzy variables from/to plain text format. It enables serialization and deserialization of fuzzy systems for persistence and portability.
- Main Components:
Fuzzy rule loading and saving
Fuzzy variable loading and saving
Text-based serialization format support
Support for Type-1 and Type-2 fuzzy systems
The text format follows a specific structure that allows complete reconstruction of fuzzy systems including membership functions, linguistic variables, and rule bases.
- ex_fuzzy.persistence.remove_parentheses(text)[source]#
Remove all content within parentheses from text.
- Parameters:
text (str) – Input text that may contain parentheses
- Returns:
Text with all parenthetical content removed
- Return type:
Example
>>> remove_parentheses("DS 0.85 (ACC 0.92), (WGHT 1.0)") 'DS 0.85 , '
- ex_fuzzy.persistence.load_fuzzy_rules(rules_printed, fuzzy_variables)[source]#
Load fuzzy rules from a text string representation.
This function parses a text-based representation of fuzzy rules and constructs a MasterRuleBase object containing all the rules organized by consequent classes.
- Parameters:
rules_printed (str) – Text representation of fuzzy rules following the ex-fuzzy format. Each rule should start with “IF” and contain antecedents connected by “AND”, followed by “WITH” and consequent information including dominance score (DS), accuracy (ACC), and optionally weight (WGHT).
fuzzy_variables (list) – List of fuzzyVariable objects that define the linguistic variables and their membership functions used in the rules.
- Returns:
- A MasterRuleBase object containing all parsed rules
organized by consequent classes, with proper rule bases for each class.
- Return type:
- Raises:
ValueError – If the text format is invalid or contains unrecognized elements
IndexError – If referenced linguistic variables or values are not found
Example
>>> rules_text = '''Rules for class_1: ... IF var1 IS low AND var2 IS high WITH DS 0.85 (ACC 0.92) ... Rules for class_2: ... IF var1 IS high WITH DS 0.78 (ACC 0.88)''' >>> master_rb = load_fuzzy_rules(rules_text, fuzzy_variables)
Note
The text format supports: - Multiple rule bases for different consequent classes - Rule modifiers using (MOD modifier_name) syntax - Accuracy and weight information in parentheses - Don’t care conditions (variables not mentioned in antecedents)
- ex_fuzzy.persistence.load_fuzzy_variables(fuzzy_variables_printed)[source]#
Load fuzzy variables from a text string representation.
This function parses a text-based representation of fuzzy variables and constructs a list of fuzzyVariable objects with their associated fuzzy sets and membership functions.
- Parameters:
fuzzy_variables_printed (str) – Text representation of fuzzy variables following the ex-fuzzy format. Should contain variable definitions starting with ‘$$$’ (for fuzzy variables) or ‘$Categorical’ (for categorical variables), followed by fuzzy set definitions with membership function parameters.
- Returns:
- List of fuzzyVariable objects containing all parsed linguistic variables
with their fuzzy sets and membership functions properly configured.
- Return type:
- Raises:
ValueError – If the text format is invalid or contains unrecognized elements
TypeError – If membership function parameters cannot be converted to proper types
Example
>>> fvars_text = '''$$$ Linguistic variable: temperature ... low;0.0,50.0;trap;0.0,0.0,20.0,30.0 ... high;0.0,50.0;trap;25.0,35.0,50.0,50.0''' >>> fuzzy_vars = load_fuzzy_variables(fvars_text)
Note
The text format supports: - Type-1 and Type-2 fuzzy sets (trapezoidal and gaussian) - Categorical variables with different data types - Variable units specification - Multiple membership function types per variable
- ex_fuzzy.persistence.print_fuzzy_variable(fuzzy_variable)[source]#
Convert a fuzzy variable to its text string representation.
This function serializes a fuzzyVariable object into a text format that can be saved to files and later loaded using load_fuzzy_variables().
- Parameters:
fuzzy_variable (fs.fuzzyVariable) – The fuzzy variable object to be serialized. Should contain properly configured fuzzy sets with membership functions.
- Returns:
- Text representation of the fuzzy variable following the ex-fuzzy format.
Includes variable name, units (if any), and all fuzzy set definitions with their membership function parameters.
- Return type:
Example
>>> # For a fuzzy variable with trapezoidal sets >>> text = print_fuzzy_variable(temperature_var) >>> print(text) $$$ Linguistic variable: temperature low;0.0,50.0;trap;0.0,0.0,20.0,30.0 high;0.0,50.0;trap;25.0,35.0,50.0,50.0
Note
The output format varies based on fuzzy set types: - Categorical variables use ‘$Categorical variable:’ prefix - Regular fuzzy variables use ‘$$$ Linguistic variable:’ prefix - Type-2 fuzzy sets include additional parameters for upper membership functions - Supports trapezoidal, triangular, and gaussian membership functions
- ex_fuzzy.persistence.save_fuzzy_variables(fuzzy_variables)[source]#
Save multiple fuzzy variables to a text string representation.
This function serializes a list of fuzzyVariable objects into a single text string that can be saved to files and later loaded using load_fuzzy_variables().
- Parameters:
fuzzy_variables (list) – List of fuzzyVariable objects to be serialized. Each variable should contain properly configured fuzzy sets with membership functions.
- Returns:
- Text representation of all fuzzy variables concatenated with newlines.
Each variable is separated and formatted according to the ex-fuzzy text specification.
- Return type:
Example
>>> variables = [temperature_var, pressure_var, flow_var] >>> text = save_fuzzy_variables(variables) >>> # Save to file >>> with open('fuzzy_vars.txt', 'w') as f: ... f.write(text)
Note
This function calls print_fuzzy_variable() for each variable and concatenates the results with newline separators for proper file formatting.