Skip to main content

Implements the various versions of Ripple Down Rules (RDR) for knowledge representation and reasoning.

Project description

Ripple Down Rules (RDR)

A python implementation of the various ripple down rules versions, including Single Classification (SCRDR), Multi Classification (MCRDR), and Generalised Ripple Down Rules (GRDR).

SCRDR, MCRDR, and GRDR are rule-based classifiers that are built incrementally, and can be used to classify data cases. The rules are refined as new data cases are classified.

SCRDR, MCRDR, and GRDR implementation were inspired from the book: "Ripple Down Rules: An Alternative to Machine Learning" by Paul Compton, Byeong Ho Kang.

Installation

sudo apt-get install graphviz graphviz-dev
pip install ripple_down_rules

For GUI support, also install:

sudo apt-get install libxcb-cursor-dev

Documentation

Read the documentation here.

Example Usage

Propositional Example

By propositional, I mean that each rule conclusion is a propositional logic statement with a constant value.

For this example, we will use the UCI Zoo dataset to classify animals into their species based on their features. The dataset contains 101 animals with 16 features, and the target is th e species of the animal.

To install the dataset:

pip install ucimlrepo
from __future__ import annotations
from ripple_down_rules.datastructures.dataclasses import CaseQuery
from ripple_down_rules.datastructures.case import create_cases_from_dataframe
from ripple_down_rules.rdr import GeneralRDR
from ripple_down_rules.utils import render_tree
from ucimlrepo import fetch_ucirepo
from enum import Enum

class Species(str, Enum):
    """Enum for the species of the animals in the UCI Zoo dataset."""
    mammal = "mammal"
    bird = "bird"
    reptile = "reptile"
    fish = "fish"
    amphibian = "amphibian"
    insect = "insect"
    molusc = "molusc"
    
    @classmethod
    def from_str(cls, value: str) -> Species:
        return getattr(cls, value)

# fetch dataset
zoo = fetch_ucirepo(id=111)

# data (as pandas dataframes)
X = zoo.data.features
y = zoo.data.targets

# This is a utility that allows each row to be a Case instance,
# which simplifies access to column values using dot notation.
all_cases = create_cases_from_dataframe(X, name="Animal")

# The targets are the species of the animals
category_names = ["mammal", "bird", "reptile", "fish", "amphibian", "insect", "molusc"]
category_id_to_name = {i + 1: name for i, name in enumerate(category_names)}
targets = [Species.from_str(category_id_to_name[i]) for i in y.values.flatten()]

# Now that we are done with the data preparation, we can create and use the Ripple Down Rules classifier.
grdr = GeneralRDR()

# Fit the GRDR to the data
case_queries = [CaseQuery(case, 'species', type(target), True, _target=target)
                for case, target in zip(all_cases[:10], targets[:10])]
grdr.fit(case_queries, animate_tree=True)

# Render the tree to a file
render_tree(grdr.start_rules[0], use_dot_exporter=True, filename="species_rdr")

# Classify a case
cat = grdr.classify(all_cases[50])['species']
assert cat == targets[50]

When prompted to write a rule, I wrote the following inside the template function that the Ripple Down Rules created:

return case.milk == 1

then

return case.aquatic == 1

The rule tree generated from fitting all the dataset will look like this: species_rdr

Relational Example

By relational, I mean that each rule conclusion is not a constant value, but is related to the case being classified, you can understand it better by the next example.

In this example, we will create a simple robot with parts and use Ripple Down Rules to find the contained objects inside another object, in this case, a robot. You see, the result of such a rule will vary depending on the robot and the parts it has.

from __future__ import annotations

import os.path
from dataclasses import dataclass, field

from typing_extensions import List, Optional

from ripple_down_rules.datastructures.dataclasses import CaseQuery
from ripple_down_rules.rdr import GeneralRDR


@dataclass(unsafe_hash=True)
class PhysicalObject:
    """
    A physical object is an object that can be contained in a container.
    """
    name: str
    contained_objects: List[PhysicalObject] = field(default_factory=list, hash=False)

@dataclass(unsafe_hash=True)
class Part(PhysicalObject):
    ...

@dataclass(unsafe_hash=True)
class Robot(PhysicalObject):
    parts: List[Part] = field(default_factory=list, hash=False)


part_a = Part(name="A")
part_b = Part(name="B")
part_c = Part(name="C")
robot = Robot("pr2", parts=[part_a])
part_a.contained_objects = [part_b]
part_b.contained_objects = [part_c]

case_query = CaseQuery(robot, "contained_objects", (PhysicalObject,), False)

load = True  # Set to True if you want to load an existing model, False if you want to create a new one.
if load and os.path.exists('./part_containment_rdr'):
    grdr = GeneralRDR.load('./', model_name='part_containment_rdr')
    grdr.ask_always = False # Set to True if you want to always ask the expert for a target value.
else:
    grdr = GeneralRDR(save_dir='./', model_name='part_containment_rdr')

grdr.fit_case(case_query)

print(grdr.classify(robot)['contained_objects'])
assert grdr.classify(robot)['contained_objects'] == {part_b}

When prompted to write a rule, I wrote the following inside the template function that the Ripple Down Rules created for me, this function takes a case object as input:

contained_objects = []
for part in case.parts:
    contained_objects.extend(part.contained_objects)
return contained_objects

And then when asked for conditions, I wrote the following inside the template function that the Ripple Down Rules created:

return len(case.parts) > 0

This means that the rule will only be applied if the robot has parts.

If you notice, the result only contains part B, while one could say that part C is also contained in the robot, but, the rule we wrote only returns the contained objects of the parts of the robot. To get part C, we would have to add another rule that says that the contained objects of my contained objects are also contained in me, you can try that yourself and see if it works!

To Cite:

@software{bassiouny2025rdr,
author = {Bassiouny, Abdelrhman},
title = {Ripple-Down-Rules},
url = {https://github.com/AbdelrhmanBassiouny/ripple_down_rules},
version = {0.5.4},
}

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

ripple_down_rules-0.6.52.tar.gz (3.5 MB view details)

Uploaded Source

Built Distribution

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

ripple_down_rules-0.6.52-py3-none-any.whl (111.0 kB view details)

Uploaded Python 3

File details

Details for the file ripple_down_rules-0.6.52.tar.gz.

File metadata

  • Download URL: ripple_down_rules-0.6.52.tar.gz
  • Upload date:
  • Size: 3.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for ripple_down_rules-0.6.52.tar.gz
Algorithm Hash digest
SHA256 86d2f70a7eb11c587e57b97ab3ab05794ab1fd4c330a7065eed4ba75541a8643
MD5 10d40a348136ca140e2e911f723b62a5
BLAKE2b-256 6e18aa175edb558d9db367467b98310c221fcb2850c016ef0c3312a1e6f49885

See more details on using hashes here.

Provenance

The following attestation bundles were made for ripple_down_rules-0.6.52.tar.gz:

Publisher: publish-to-test-pypi.yml on AbdelrhmanBassiouny/ripple_down_rules

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ripple_down_rules-0.6.52-py3-none-any.whl.

File metadata

File hashes

Hashes for ripple_down_rules-0.6.52-py3-none-any.whl
Algorithm Hash digest
SHA256 c3701064a8a9301c076f537ac5d0211198da39e84c19d146954ca486bfe2fae9
MD5 3313b3e6099cfeef7ec27b19b5b31748
BLAKE2b-256 142418fb36690d12c5ca0550fb51f27c49a308609e99ee679db1c3ded705c9ad

See more details on using hashes here.

Provenance

The following attestation bundles were made for ripple_down_rules-0.6.52-py3-none-any.whl:

Publisher: publish-to-test-pypi.yml on AbdelrhmanBassiouny/ripple_down_rules

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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