Skip to content

LukeSantossz/fuzzy-lab

Repository files navigation

Python scikit-fuzzy PyTorch Status

fuzzy-lab

Agricultural decision-support using fuzzy logic (IF–THEN rules), Mamdani FIS, ANFIS, and time-series analysis for spray windows, water stress, irrigation hints, and yield estimates driven by weather variables.

Overview

Precision agriculture couples climate variables with operational limits (wind, humidity, rainfall, delta T). Crisp models are often too rigid; fuzzy inference systems encode uncertainty and field-aligned linguistic rules. This repository is a modular Python package: Mamdani FIS with scikit-fuzzy, ANFIS with PyTorch, and a time-series layer (Pandas / tslearn), plus Jupyter notebooks for experiments and pytest for tests.

Tech Stack

Layer Technology
Fuzzy inference scikit-fuzzy (Mamdani), NumPy
Neuro-fuzzy PyTorch (ANFIS)
Time series Pandas, tslearn
Testing pytest
Experimentation Jupyter Notebook
Visualization matplotlib

FIS linguistic domain

Antecedents (inputs):

Variable Universe Sets (summary)
Temperature 0–60 °C frio_extremo → crítico (7)
Humidity 0–100 % deserto → condensação (7)
Rainfall 0–500 mm seco → extrema (7)
Wind 0–150 km/h calmo → tempestade (7)
Delta T 0–40 °C inversão_térmica → extremo (7)

Consequents (outputs): spray recommendation sp (proibida / atencao / janela_disponivel), water stress wh (baixo / medio / alto), irrigation recommendation ir (desnecessaria / opcional / recomendada), estimated productivity bp (baixa / medio / alta).

Public interface (re-exported from fuzzylab.fis):

from fuzzylab.fis import build_system, run_inference

system = build_system()
outputs = run_inference(
    system,
    {
        "Temperatura": 28.0,
        "Umidade": 60.0,
        "Chuva": 0.0,
        "Vento": 10.0,
        "Delta T": 8.0,
    },
)
# outputs -> {"wh": ..., "ir": ..., "sp": ..., "bp": ...}

build_system(config) optionally accepts {"rule_groups": [...]} to include only a subset of rule builders (spray, water_stress, irrigation, productivity, combined).

Getting Started

Prerequisites

  • Python 3.10 or newer
  • pip
  • (Optional) CUDA for GPU-accelerated PyTorch in the ANFIS module

Installation

# Clone the repository
git clone https://github.com/<username>/fuzzy-lab.git
cd fuzzy-lab

# Create and activate a virtual environment
python -m venv venv
source venv/bin/activate   # Linux / macOS
venv\Scripts\activate      # Windows

# Install dependencies
pip install -r requirements.txt

# Editable install (recommended — import name: fuzzylab)
pip install -e .

Without pip install -e ., add src to PYTHONPATH or rely on the notebook snippet that prepends .../src to sys.path.

Running

# Start the Jupyter server
jupyter notebook

# Main FIS experiment notebook:
# notebooks/fis_mamdani.ipynb
# (membership-function plots and 3D control surfaces are saved under notebooks/figures/)
# Tests
pytest tests/
# Programmatic usage (no notebook required)
from fuzzylab.fis import build_system, run_inference

system = build_system()
outputs = run_inference(
    system,
    {"Temperatura": 28.0, "Umidade": 60.0, "Chuva": 0.0, "Vento": 10.0, "Delta T": 8.0},
)
print(outputs)  # {"wh": ..., "ir": ..., "sp": ..., "bp": ...}

Environment variables: the codebase does not require any at this stage. For PyTorch on GPU, follow upstream guidance (CUDA_VISIBLE_DEVICES, etc.) for your setup.

Project Structure

fuzzy-lab/
├── notebooks/
│   ├── fis_mamdani.ipynb          # Mamdani FIS experiments
│   └── figures/                   # generated MF plots and control surfaces
├── data/
│   └── raw/                       # raw inputs (e.g. .gitkeep)
├── src/
│   └── fuzzylab/
│       ├── fis/
│       │   ├── mamdani.py         # Mamdani FIS module (scikit-fuzzy) — ACTIVE
│       │   └── __init__.py        # re-exports build_system / run_inference
│       ├── anfis/
│       │   ├── anfis.py           # AnfisNet (nn.Module) — stub
│       │   ├── engine.py          # build_system / run_inference — stub
│       │   └── __init__.py        # re-exports public interface
│       └── timeseries/            # time series (placeholder)
├── tests/
│   ├── test_fis.py                # FIS scenario tests (ideal, storm, drought)
│   ├── test_anfis.py              # ANFIS import and stub tests
│   └── test_mamdani_water_irrigation.py  # boundary and ordering tests
├── pyproject.toml
├── requirements.txt
├── requirements-anfis.txt         # PyTorch deps for ANFIS module
└── README.md

Current Status

Status: in development — Phase 1 complete

Stage Status
Linguistic antecedents and consequents Done
Membership functions (automf, 7 sets) Done
Full spray rules (janela_disponivel, atencao, proibida) Done
Modular package layout (fis, anfis, timeseries) Done
Rules for water stress and irrigation Done
Rules for productivity (bet_productivity, 7 rules) Done
Public interface build_system / run_inference (flat mamdani.py) Done
Membership-function plots and 3D control surfaces (notebook) Done
Unit tests for FIS scenarios (24 tests) Done
ANFIS subpackage scaffold (AnfisNet, stubs) Done
ANFIS training and inference implementation Pending
Time-series module implementation Pending
Calibrating universe bounds against regional climate data Pending
Calibrating productivity rules against field data Pending

Next steps:

  1. Implement ANFIS forward pass and training loop.
  2. Implement time-series workflows and tie-ins to data/raw/.
  3. Revisit discourse universes using field data and agronomic references.
  4. Calibrate productivity rules against experimental yield datasets.

Known issues

  • Universe bounds (np.arange) should be checked against regional climate records and technical literature.
  • Productivity rules now cover 7 scenarios but their thresholds still need calibration against experimental yield data.
  • ANFIS subpackage is scaffolded with stubs; training and inference logic pending implementation.
  • Time-series module is placeholder only.

About

A repository for exploring Fuzzy Logic models, featuring a practical implementation applied to climate prediction.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors