Skip to content

Commit 57f4733

Browse files
Make matplotlib optional dependency for visualization
Core framework now works without matplotlib. Visualization functions moved to optional 'viz' dependency with graceful fallback. Install with: pip install 'thurstone[viz]' for visualization features. Co-Authored-By: Claude Sonnet 4 <noreply@anthropic.com>
1 parent d44e08b commit 57f4733

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ description = "OO toolkit for lattice-based performance models and the horse-rac
1010
authors = [{name="You", email="you@example.com"}]
1111
readme = "README.md"
1212
requires-python = ">=3.9"
13-
dependencies = ["numpy>=1.23", "matplotlib>=3.7"]
13+
dependencies = ["numpy>=1.23"]
1414

1515
[project.optional-dependencies]
1616
test = [
@@ -19,6 +19,9 @@ test = [
1919
"hypothesis>=6.0",
2020
"pytest-benchmark>=4.0",
2121
]
22+
viz = [
23+
"matplotlib>=3.7",
24+
]
2225

2326
[tool.setuptools.packages.find]
2427
where = ["."]

thurstone/visualization.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,16 @@
88
from __future__ import annotations
99
from typing import Tuple, Optional, Dict, Any, List
1010
import numpy as np
11-
import matplotlib.pyplot as plt
12-
from matplotlib.colors import LinearSegmentedColormap
13-
from mpl_toolkits.mplot3d import Axes3D
11+
12+
# Optional matplotlib import with graceful fallback
13+
try:
14+
import matplotlib.pyplot as plt
15+
from matplotlib.colors import LinearSegmentedColormap
16+
from mpl_toolkits.mplot3d import Axes3D
17+
MATPLOTLIB_AVAILABLE = True
18+
except ImportError:
19+
MATPLOTLIB_AVAILABLE = False
20+
plt = None
1421

1522
from .cube_to_simplex import CubeToSimplexMapping
1623
from .quality_assessment import (
@@ -19,6 +26,15 @@
1926
)
2027

2128

29+
def _ensure_matplotlib():
30+
"""Ensure matplotlib is available for visualization functions."""
31+
if not MATPLOTLIB_AVAILABLE:
32+
raise ImportError(
33+
"Matplotlib is required for visualization functions. "
34+
"Install with: pip install 'thurstone[viz]' or pip install matplotlib"
35+
)
36+
37+
2238
def create_lattice_grid(resolution: int = 20) -> Tuple[np.ndarray, np.ndarray]:
2339
"""
2440
Create a regular lattice grid in [0,1]^2.
@@ -49,6 +65,8 @@ def plot_cube_lattice_2d(mapping: CubeToSimplexMapping,
4965
Returns:
5066
matplotlib Figure
5167
"""
68+
_ensure_matplotlib()
69+
5270
if mapping.k != 2:
5371
raise ValueError("This function is only for k=2 (triangle)")
5472

0 commit comments

Comments
 (0)