Skip to main content

Specification pattern implementation with fuzzy predicates

Project description

FuzzSpec

FuzzSpec is a Python library for defining and evaluating fuzzy predicate specifications. It allows you to define complex logic rules based on fuzzy set theory, returning a degree of truth (between 0 and 1) instead of binary values.

Features

  • Flexible Membership Functions:
    • Triangular (3 parameters)
    • Trapezoidal (4 parameters)
  • Configurable Logic: Choose between different T-Norms (AND), S-Norms (OR), and Complements (NOT).
  • YAML Configuration: Decouple logic from code using declarative logic.yaml files.
  • Pydantic Validation: Automatic schema validation for your logic configurations.
  • Compositional API: Easily combine atomic predicates using Python operators (&, |, ~).

Installation

uv sync

Configuration (logic.yaml)

You can configure the global fuzzy logic behavior and define your predicates in a single file.

1. Global Logical Operators

Configure how the library handles AND, OR, and NOT operations.

config:
    tnorm: "product"      # Options: min, product, lukasiewicz
    snorm: "probabilistic" # Options: max, probabilistic, lukasiewicz
    complement: "standard" # Options: standard (1 - x)

2. Atomic Predicates

Define attributes and their fuzzy ranges.

atomic:
    low_att:
        variable: "att1"
        fuzzy_number: [0.1, 1.4, 4.3] # Triangular
    active_range:
        variable: "att2"
        fuzzy_number: [0.1, 0.9, 4.3, 5.5] # Trapezoidal

3. Composed Predicates

Link atomic predicates using logical operators.

composed:
    low_and_active:
        operator: "and"
        operands: ["low_att", "active_range"]
    not_low:
        operator: "not"
        operands: ["low_att"]

Usage Example

Create your data model

from dataclasses import dataclass

@dataclass
class SensorData:
    att1: float
    att2: float
    att3: int

Load and Evaluate

from FuzzyPredicates.utils import load_rules

# Load rules from config (validates against Pydantic schema)
rules = load_rules('logic.yaml')

# Evaluate against data
data = SensorData(att1=1.0, att2=4.4, att3=43)
result = rules['low_and_active'](data)

print(f"Confidence Degree: {result:.2f}")

Advanced Usage (Command Line)

The main.py entry point supports configurable logging and custom configuration paths.

uv run main.py --log-level DEBUG --config my_logic.yaml

Available log levels: DEBUG, INFO, WARNING, ERROR, CRITICAL.


## Technical Details

### Supported Operators

| Logic Type | Name | Formula |
|------------|------|---------|
| **T-Norm (AND)** | `min` | $\min(a, b)$ |
| | `product` | $a \times b$ |
| | `lukasiewicz` | $\max(0, a + b - 1)$ |
| **S-Norm (OR)** | `max` | $\max(a, b)$ |
| | `probabilistic` | $a + b - (a \times b)$ |
| | `lukasiewicz` | $\min(1, a + b)$ |

### Project Structure
- `src/FuzzyPredicates/fuzzy/`: Core fuzzy membership functions and operators.
- `src/FuzzyPredicates/predicates.py`: High-level `Predicate` class with operator overloading.
- `src/FuzzyPredicates/models.py`: Pydantic schemas for logic validation.
- `src/FuzzyPredicates/utils.py`: Utility functions (e.g., `load_rules`).
- `main.py`: Example entry point.

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fuzzy_predicates_gdrottoli-0.1.1.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fuzzy_predicates_gdrottoli-0.1.1-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file fuzzy_predicates_gdrottoli-0.1.1.tar.gz.

File metadata

File hashes

Hashes for fuzzy_predicates_gdrottoli-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d39c86e0cc66e9e5de8b1e2655962da7d27568d1e83ae71787d1434f92f395f0
MD5 a0ff49cb342857b0e332f8e303ee3cbb
BLAKE2b-256 6d714e2291a872333b5cdf821d792d641d647143bd2a9f2b528f5cc9b5ec82d8

See more details on using hashes here.

File details

Details for the file fuzzy_predicates_gdrottoli-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for fuzzy_predicates_gdrottoli-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 32d6727cf0aaf202ea349ec4d1f399161cef0cd392a2f67f02cdb0e69d438859
MD5 65d0f4e2e463e7688ab96595cf613f7c
BLAKE2b-256 b45429158f799ef11f54099a014c2b9a7d87bc41a3aa77d5e26eb49acc3aec8a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page