# parrt/dtreeviz architecture

> Architecture diagram of the GitHub repository parrt/dtreeviz, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/parrt/dtreeviz
- Repository: https://github.com/parrt/dtreeviz

## Overview

#### Purpose and public entry points
dtreeviz is a Python library that turns trained decision-tree models into SVG visualizations and interpretation artifacts. Its primary API is `dtreeviz.model(...)`, which wraps a supported model in a common adaptor; users then invoke methods such as `view()` and `explain_prediction_path()`. Package exports begin at `dtreeviz/__init__.py`; tree-facing behavior is centered in `dtreeviz/trees.py`.

#### Model adaptor boundary
`dtreeviz.model()` selects a backend-specific shadow/adaptor implementation that normalizes tree traversal, node metadata, training samples, targets, classes, and split semantics. The common abstraction lives in `dtreeviz/models/shadow_decision_tree.py`, with framework implementations in:
- `dtreeviz/models/sklearn_decision_trees.py`
- `dtreeviz/models/xgb_decision_tree.py`
- `dtreeviz/models/lightgbm_decision_tree.py`

This boundary isolates visualization logic from incompatible external model representations.

#### Supported ML runtimes
Backend modules support scikit-learn, XGBoost, Spark MLlib, LightGBM, and TensorFlow Decision Forests:
- `dtreeviz/models/spark_decision_tree.py`
- `dtreeviz/models/tensorflow_decision_tree.py`
- `dtreeviz/models/xgb_decision_tree.py`

These are optional dependency/runtime integrations rather than a single shared training runtime. Callers own data loading and model training; dtreeviz receives the trained model plus `X_train`, `y_train`, feature metadata, and optional class labels.

#### Tree rendering pipeline
Visualization methods in `dtreeviz/trees.py` consume the normalized adaptor, derive node/leaf labels and distribution graphics, construct Graphviz graph content, and render SVG. Shared drawing and formatting behavior is distributed across:
- `dtreeviz/trees.py`
- `dtreeviz/colors.py`
- `dtreeviz/utils.py`

Graphviz’s Python library and the external `dot` executable are material infrastructure dependencies; SVG is the intended output format. Returned visualization objects can display inline, open a viewer, or save SVG.

#### Interpretation and feature-space analysis
Beyond full-tree diagrams, the library provides prediction-path explanations, leaf-level summaries, and feature-space/partition visualizations. Core interpretation logic is in `dtreeviz/interpretation.py`; classifier-agnostic decision-boundary plotting is in `dtreeviz/classifiers.py`. The latter works at a separate boundary: it queries generic classifiers via probability/prediction APIs and does not require the tree adaptor.

#### AI explanation extension
Optional AI chat and generated explanations are implemented in `dtreeviz/ai_explanation.py` and are enabled when building the model wrapper. This feature uses an OpenAI API key from `OPENAI_API_KEY` and an externally selected OpenAI model, supplying tree and training-data context for questions and annotations. It is an opt-in external-service path, separate from normal local rendering.

#### Packaging and usage artifacts
Python packaging and optional extras are defined by `setup.py` and `setup.cfg`. Framework-specific notebooks demonstrate the intended integration boundary and outputs, e.g. `notebooks/dtreeviz_sklearn_visualisations.ipynb`, `notebooks/dtreeviz_spark_visualisations.ipynb`, and `notebooks/dtreeviz_tensorflow_visualisations.ipynb`.

Main entry points: [`dtreeviz/trees.py`](https://github.com/parrt/dtreeviz/blob/master/dtreeviz/trees.py) (Tree visualization), [`dtreeviz/models/shadow_decision_tree.py`](https://github.com/parrt/dtreeviz/blob/master/dtreeviz/models/shadow_decision_tree.py) (ShadowDecisionTree), [`dtreeviz/models/sklearn_decision_trees.py`](https://github.com/parrt/dtreeviz/blob/master/dtreeviz/models/sklearn_decision_trees.py) (scikit-learn adaptor), [`dtreeviz/ai_explanation.py`](https://github.com/parrt/dtreeviz/blob/master/dtreeviz/ai_explanation.py) (AI explanations)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Core API &amp; visualization"]
  node_package_exports["Package exports<br/>Python package<br/>[__init__.py]"]
  node_model_factory["model&#40;&#41; factory<br/>public API<br/>[trees.py]"]
  node_tree_visualization["Tree visualization<br/>rendering API<br/>[trees.py]"]
  node_drawing_helpers["Colors &amp; drawing helpers<br/>rendering support<br/>[colors.py]"]
  node_formatting_helpers["Formatting utilities<br/>rendering support<br/>[utils.py]"]
  node_graphviz{{"Graphviz dot + Python API<br/>external renderer"}}
  node_svg_artifact["SVG visualization<br/>output artifact"]
end

subgraph group_adaptors["Model adaptor boundary"]
  node_shadow_tree["ShadowDecisionTree<br/>common adaptor"]
  node_sklearn_adaptor["scikit-learn adaptor<br/>backend adaptor"]
  node_xgboost_adaptor["XGBoost adaptor<br/>backend adaptor"]
  node_lightgbm_adaptor["LightGBM adaptor<br/>backend adaptor"]
  node_spark_adaptor["Spark MLlib adaptor<br/>backend adaptor"]
  node_tensorflow_adaptor["TensorFlow DF adaptor<br/>backend adaptor"]
end

subgraph group_extensions["Optional analysis &amp; integrations"]
  node_interpretation["Prediction &amp; leaf interpretation<br/>analysis API<br/>[interpretation.py]"]
  node_classifier_plots["Classifier boundary plots<br/>analysis API<br/>[classifiers.py]"]
  node_ai_explanations["AI explanations<br/>optional integration<br/>[ai_explanation.py]"]
  node_openai_service{{"OpenAI API<br/>external service"}}
end

node_packaging["Packaging &amp; extras<br/>package configuration<br/>[setup.py]"]
node_usage_notebooks["Framework notebooks<br/>usage artifacts"]

node_package_exports -->|"exports"| node_model_factory
node_model_factory -->|"selects adaptor"| node_shadow_tree
node_model_factory -->|"returns wrapper"| node_tree_visualization
node_sklearn_adaptor -->|"implements"| node_shadow_tree
node_xgboost_adaptor -->|"implements"| node_shadow_tree
node_lightgbm_adaptor -->|"implements"| node_shadow_tree
node_spark_adaptor -->|"implements"| node_shadow_tree
node_tensorflow_adaptor -->|"implements"| node_shadow_tree
node_tree_visualization -->|"reads normalized tree"| node_shadow_tree
node_tree_visualization -->|"uses"| node_drawing_helpers
node_tree_visualization -->|"uses"| node_formatting_helpers
node_tree_visualization -->|"constructs graph"| node_graphviz
node_graphviz -->|"renders"| node_svg_artifact
node_tree_visualization -->|"supports explanations"| node_interpretation
node_classifier_plots -->|"produces visualizations"| node_svg_artifact
node_model_factory -.->|"optionally enables"| node_ai_explanations
node_ai_explanations -.->|"sends opt-in context"| node_openai_service
node_packaging -.->|"provides optional dependencies"| node_sklearn_adaptor
node_usage_notebooks -.->|"demonstrate usage"| node_model_factory

click node_package_exports "https://github.com/parrt/dtreeviz/blob/master/dtreeviz/__init__.py"
click node_model_factory "https://github.com/parrt/dtreeviz/blob/master/dtreeviz/trees.py"
click node_tree_visualization "https://github.com/parrt/dtreeviz/blob/master/dtreeviz/trees.py"
click node_drawing_helpers "https://github.com/parrt/dtreeviz/blob/master/dtreeviz/colors.py"
click node_formatting_helpers "https://github.com/parrt/dtreeviz/blob/master/dtreeviz/utils.py"
click node_shadow_tree "https://github.com/parrt/dtreeviz/blob/master/dtreeviz/models/shadow_decision_tree.py"
click node_sklearn_adaptor "https://github.com/parrt/dtreeviz/blob/master/dtreeviz/models/sklearn_decision_trees.py"
click node_xgboost_adaptor "https://github.com/parrt/dtreeviz/blob/master/dtreeviz/models/xgb_decision_tree.py"
click node_lightgbm_adaptor "https://github.com/parrt/dtreeviz/blob/master/dtreeviz/models/lightgbm_decision_tree.py"
click node_spark_adaptor "https://github.com/parrt/dtreeviz/blob/master/dtreeviz/models/spark_decision_tree.py"
click node_tensorflow_adaptor "https://github.com/parrt/dtreeviz/blob/master/dtreeviz/models/tensorflow_decision_tree.py"
click node_interpretation "https://github.com/parrt/dtreeviz/blob/master/dtreeviz/interpretation.py"
click node_classifier_plots "https://github.com/parrt/dtreeviz/blob/master/dtreeviz/classifiers.py"
click node_ai_explanations "https://github.com/parrt/dtreeviz/blob/master/dtreeviz/ai_explanation.py"
click node_packaging "https://github.com/parrt/dtreeviz/blob/master/setup.py"
click node_usage_notebooks "https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_sklearn_visualisations.ipynb"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_package_exports,node_model_factory,node_tree_visualization,node_drawing_helpers,node_formatting_helpers,node_graphviz,node_svg_artifact toneBlue
class node_shadow_tree,node_sklearn_adaptor,node_xgboost_adaptor,node_lightgbm_adaptor,node_spark_adaptor,node_tensorflow_adaptor toneAmber
class node_interpretation,node_classifier_plots,node_ai_explanations,node_openai_service toneMint
class node_packaging,node_usage_notebooks toneNeutral
```

## Components

### Core API & visualization

- [Package exports](https://github.com/parrt/dtreeviz/blob/master/dtreeviz/__init__.py): Python package · Public dtreeviz entry points. · `dtreeviz/__init__.py`
- [model() factory](https://github.com/parrt/dtreeviz/blob/master/dtreeviz/trees.py): public API · Wraps a supported trained model. · `dtreeviz/trees.py`
- [Tree visualization](https://github.com/parrt/dtreeviz/blob/master/dtreeviz/trees.py): rendering API · Provides view() and tree-facing behavior. · `dtreeviz/trees.py`
- [Colors & drawing helpers](https://github.com/parrt/dtreeviz/blob/master/dtreeviz/colors.py): rendering support · `dtreeviz/colors.py`
- [Formatting utilities](https://github.com/parrt/dtreeviz/blob/master/dtreeviz/utils.py): rendering support · `dtreeviz/utils.py`
- **Graphviz dot + Python API**: external renderer · Builds and renders SVG graph output.
- **SVG visualization**: output artifact · Can display inline, open, or save.

### Model adaptor boundary

- [ShadowDecisionTree](https://github.com/parrt/dtreeviz/blob/master/dtreeviz/models/shadow_decision_tree.py): common adaptor · Normalizes traversal, metadata, samples, targets, and split semantics. · `dtreeviz/models/shadow_decision_tree.py`
- [scikit-learn adaptor](https://github.com/parrt/dtreeviz/blob/master/dtreeviz/models/sklearn_decision_trees.py): backend adaptor · `dtreeviz/models/sklearn_decision_trees.py`
- [XGBoost adaptor](https://github.com/parrt/dtreeviz/blob/master/dtreeviz/models/xgb_decision_tree.py): backend adaptor · `dtreeviz/models/xgb_decision_tree.py`
- [LightGBM adaptor](https://github.com/parrt/dtreeviz/blob/master/dtreeviz/models/lightgbm_decision_tree.py): backend adaptor · `dtreeviz/models/lightgbm_decision_tree.py`
- [Spark MLlib adaptor](https://github.com/parrt/dtreeviz/blob/master/dtreeviz/models/spark_decision_tree.py): backend adaptor · `dtreeviz/models/spark_decision_tree.py`
- [TensorFlow DF adaptor](https://github.com/parrt/dtreeviz/blob/master/dtreeviz/models/tensorflow_decision_tree.py): backend adaptor · `dtreeviz/models/tensorflow_decision_tree.py`

### Optional analysis & integrations

- [Prediction & leaf interpretation](https://github.com/parrt/dtreeviz/blob/master/dtreeviz/interpretation.py): analysis API · Explains prediction paths and leaf-level summaries. · `dtreeviz/interpretation.py`
- [Classifier boundary plots](https://github.com/parrt/dtreeviz/blob/master/dtreeviz/classifiers.py): analysis API · Queries generic classifier prediction/probability APIs. · `dtreeviz/classifiers.py`
- [AI explanations](https://github.com/parrt/dtreeviz/blob/master/dtreeviz/ai_explanation.py): optional integration · Uses OpenAI context when explicitly enabled. · `dtreeviz/ai_explanation.py`
- **OpenAI API**: external service · Configured with OPENAI_API_KEY and a selected model.

### Other components

- [Packaging & extras](https://github.com/parrt/dtreeviz/blob/master/setup.py): package configuration · Defines Python package dependencies and optional runtime extras. · `setup.py`
- [Framework notebooks](https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_sklearn_visualisations.ipynb): usage artifacts · Demonstrate supported backend integrations and outputs. · `notebooks/dtreeviz_sklearn_visualisations.ipynb`

## Connections

- Package exports → model() factory: exports
- model() factory → ShadowDecisionTree: selects adaptor
- model() factory → Tree visualization: returns wrapper
- scikit-learn adaptor → ShadowDecisionTree: implements
- XGBoost adaptor → ShadowDecisionTree: implements
- LightGBM adaptor → ShadowDecisionTree: implements
- Spark MLlib adaptor → ShadowDecisionTree: implements
- TensorFlow DF adaptor → ShadowDecisionTree: implements
- Tree visualization → ShadowDecisionTree: reads normalized tree
- Tree visualization → Colors & drawing helpers: uses
- Tree visualization → Formatting utilities: uses
- Tree visualization → Graphviz dot + Python API: constructs graph
- Graphviz dot + Python API → SVG visualization: renders
- Tree visualization → Prediction & leaf interpretation: supports explanations
- Classifier boundary plots → SVG visualization: produces visualizations
- model() factory → AI explanations: optionally enables
- AI explanations → OpenAI API: sends opt-in context
- Packaging & extras → scikit-learn adaptor: provides optional dependencies
- Framework notebooks → model() factory: demonstrate usage
