# csinva/imodels architecture

> Architecture diagram of the GitHub repository csinva/imodels, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-05). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/csinva/imodels
- Repository: https://github.com/csinva/imodels

## Overview

##### High-Level Architecture Overview
The system should be represented as a layered architecture with the following main components:

a. Core Model Layer
- Models into 4 main categories as shown in the README:
- Rule Sets (e.g., RuleFit, SkopeRules)
- Rule Lists (e.g., CORELS, Bayesian Rule List)
- Rule Trees (e.g., CART, C4.5)
- Algebraic Models (e.g., SLIM, Tree GAM)

b. Support Layer
- Discretization components
- Utility functions
- Data handling
- Evaluation metrics

##### Detailed Component Breakdown

Core Components (show as separate modules):
**Models/**
- RuleSets/
- RuleFit
- SkopeRules
- BoostedRules
- SLIPPER
- RuleLists/
- OptimalRuleList
- BayesianRuleList
- GreedyRuleList
- RuleTrees/
- OptimalTree
- GreedyTree
- C45Tree
- AlgebraicModels/
- SLIM
- TreeGAM

Support Components (show as utility layers):
**Utils/**
- Discretization/
- MDLP
- SimpleDiscretizer
- RFDiscretizer
- Evaluation/
- Metrics
- ErrorAnalysis
- DataHandling/
- Preprocessing
- DatasetLoaders

##### Key Interactions to Highlight

a. Model Training Flow:
- Data Input → Preprocessing → Model Training → Evaluation
- How discretization can be optionally applied before model training
- How utility functions support various stages

b. Model Integration:
- Scikit-learn compatibility interface
- Common base classes and inheritance
- How models can be combined (e.g., ensembles)

##### Design Elements

Use these visual elements:
- Blue rectangles for core model components
- Green rectangles for utility components
- Yellow diamonds for data transformation points

##### Important Details

a. Interface Layer:
- Scikit-learn compatibility
- Fit/predict methods
- AutoML integration points

b. Extension Points:
- Custom model addition
- Custom metric integration
- Data preprocessing hooks

##### Implementation Notes

- Use clear hierarchical layout
- Related components visually
- Version information
- External dependencies (numpy, sklearn)

##### Specific Recommendations

- Cross-cutting concerns (logging, validation)
- Experimental vs. stable components
- Error handling flow

##### Data flow through the system

This will help developers understand both the high-level architecture and the specific implementation details needed for maintenance and extension.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Core["Core Model Layer"]
        direction TB
        subgraph RuleSets["Rule Sets"]
            direction TB
            RF["RuleFit"]:::model
            SR["SkopeRules"]:::model
            BR["BoostedRules"]:::model
            SL["SLIPPER"]:::model
        end
        
        subgraph RuleLists["Rule Lists"]
            direction TB
            BRL["Bayesian Rule List"]:::model
            GRL["Greedy Rule List"]:::model
            CRL["CORELS"]:::model
        end
        
        subgraph RuleTrees["Rule Trees"]
            direction TB
            C45["C45 Tree"]:::model
            CART["CART"]:::model
            CGT["Custom Greedy Tree"]:::model
        end
        
        subgraph AlgebraicModels["Algebraic Models"]
            direction TB
            SLIM["SLIM"]:::model
            TGAM["Tree GAM"]:::model
        end
    end
    
    subgraph Support["Support Layer"]
        direction TB
        subgraph Discretization["Discretization"]
            direction TB
            MDLP["MDLP"]:::util
            SD["Simple Discretizer"]:::util
        end
        
        subgraph Utils["Utilities"]
            direction TB
            DH["Data Handling"]:::util
            MT["Metrics"]:::util
            EA["Error Analysis"]:::util
            AML["AutoML"]:::util
        end
        
        subgraph Importance["Feature Importance"]
            direction TB
            MDIP["MDI Plus"]:::util
            RFP["RF Plus"]:::util
            LS["Local Stumps"]:::util
        end
    end
    
    DataInput[("Input Data")]:::data
    DataInput --> DH
    DH --> Discretization
    Discretization --> Core
    Utils --> Core
    Importance --> Core
    
    %% Component Mappings
    click RF "https://github.com/csinva/imodels/blob/master/imodels/rule_set/rule_fit.py"
    click SR "https://github.com/csinva/imodels/blob/master/imodels/rule_set/skope_rules.py"
    click BR "https://github.com/csinva/imodels/blob/master/imodels/rule_set/boosted_rules.py"
    click SL "https://github.com/csinva/imodels/blob/master/imodels/rule_set/slipper.py"
    click BRL "https://github.com/csinva/imodels/blob/master/imodels/rule_list/bayesian_rule_list/bayesian_rule_list.py"
    click GRL "https://github.com/csinva/imodels/blob/master/imodels/rule_list/greedy_rule_list.py"
    click CRL "https://github.com/csinva/imodels/blob/master/imodels/rule_list/corels_wrapper.py"
    click C45 "https://github.com/csinva/imodels/blob/master/imodels/tree/c45_tree/c45_tree.py"
    click CART "https://github.com/csinva/imodels/blob/master/imodels/tree/cart_wrapper.py"
    click CGT "https://github.com/csinva/imodels/blob/master/imodels/tree/custom_greedy_tree.py"
    click SLIM "https://github.com/csinva/imodels/blob/master/imodels/algebraic/slim.py"
    click TGAM "https://github.com/csinva/imodels/blob/master/imodels/algebraic/tree_gam.py"
    click MDLP "https://github.com/csinva/imodels/blob/master/imodels/discretization/mdlp.py"
    click SD "https://github.com/csinva/imodels/blob/master/imodels/discretization/simple.py"
    click DH "https://github.com/csinva/imodels/blob/master/imodels/util/data_util.py"
    click MT "https://github.com/csinva/imodels/blob/master/imodels/util/metrics.py"
    click EA "https://github.com/csinva/imodels/blob/master/imodels/util/explain_errors.py"
    click AML "https://github.com/csinva/imodels/blob/master/imodels/util/automl.py"
    click MDIP "https://github.com/csinva/imodels/blob/master/imodels/importance/mdi_plus.py"
    click RFP "https://github.com/csinva/imodels/blob/master/imodels/importance/rf_plus.py"
    click LS "https://github.com/csinva/imodels/blob/master/imodels/importance/local_stumps.py"
    
    %% Styling
    classDef model fill:#a8d1ff,stroke:#333,stroke-width:2px
    classDef util fill:#90EE90,stroke:#333,stroke-width:2px
    classDef data fill:#FFD700,stroke:#333,stroke-width:2px
    
    %% Legend
    subgraph Legend
        direction TB
        M["Model Component"]:::model
        U["Utility Component"]:::util
        D["Data"]:::data
    end
```
