mintfig 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. mintfig/__init__.py +162 -0
  2. mintfig/_explain/__init__.py +1 -0
  3. mintfig/_explain/change.py +392 -0
  4. mintfig/_explain/compare.py +1308 -0
  5. mintfig/_explain/compare_grouped.py +472 -0
  6. mintfig/_explain/core.py +2005 -0
  7. mintfig/_explain/distribution.py +1268 -0
  8. mintfig/_explain/flow.py +508 -0
  9. mintfig/_explain/layout.py +2012 -0
  10. mintfig/_explain/matrix.py +623 -0
  11. mintfig/_explain/relate.py +1318 -0
  12. mintfig/_explain/timeline.py +595 -0
  13. mintfig/_explain/trend.py +2557 -0
  14. mintfig/_explain/waterfall.py +459 -0
  15. mintfig/_table.py +691 -0
  16. mintfig/_tabular.py +738 -0
  17. mintfig/annotations.py +1165 -0
  18. mintfig/assets/AGENTS_SNIPPET.md +11 -0
  19. mintfig/assets/SKILL.md +143 -0
  20. mintfig/assets/__init__.py +1 -0
  21. mintfig/assets/references/publication.md +79 -0
  22. mintfig/assets/references/scientific_and_recipes.md +213 -0
  23. mintfig/audit.py +179 -0
  24. mintfig/brief.py +518 -0
  25. mintfig/bundle.py +333 -0
  26. mintfig/cli.py +1300 -0
  27. mintfig/edit.py +347 -0
  28. mintfig/epistemic.py +1083 -0
  29. mintfig/explainer.py +194 -0
  30. mintfig/export.py +247 -0
  31. mintfig/inspect_data.py +583 -0
  32. mintfig/interactive.py +1028 -0
  33. mintfig/palettes.py +233 -0
  34. mintfig/plate.py +784 -0
  35. mintfig/preflight.py +2398 -0
  36. mintfig/proof.py +627 -0
  37. mintfig/runner.py +403 -0
  38. mintfig/skill.py +167 -0
  39. mintfig/specs.py +291 -0
  40. mintfig/style.py +151 -0
  41. mintfig/terminal.py +785 -0
  42. mintfig/themes.py +660 -0
  43. mintfig-0.2.0.dist-info/METADATA +239 -0
  44. mintfig-0.2.0.dist-info/RECORD +47 -0
  45. mintfig-0.2.0.dist-info/WHEEL +4 -0
  46. mintfig-0.2.0.dist-info/entry_points.txt +2 -0
  47. mintfig-0.2.0.dist-info/licenses/LICENSE +21 -0
mintfig/palettes.py ADDED
@@ -0,0 +1,233 @@
1
+ """Editorial color palettes, banned colormaps, and CVD / grayscale simulation."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import Literal
6
+
7
+ import matplotlib as mpl
8
+ import numpy as np
9
+ from matplotlib.colors import LinearSegmentedColormap
10
+
11
+ PALETTES: dict[str, list[str]] = {
12
+ "nature": [
13
+ "#E64B35",
14
+ "#4DBBD5",
15
+ "#00A087",
16
+ "#3C5488",
17
+ "#F39B7F",
18
+ "#8491B4",
19
+ "#91D1C2",
20
+ "#7E6148",
21
+ ],
22
+ "nature_muted": [
23
+ "#C84B31",
24
+ "#2C5F8A",
25
+ "#1E8A78",
26
+ "#6E5A8A",
27
+ "#C98A2E",
28
+ "#5B6B7C",
29
+ "#9E5B40",
30
+ "#3A4654",
31
+ ],
32
+ "science": [
33
+ "#3B4992",
34
+ "#EE0000",
35
+ "#008B45",
36
+ "#631879",
37
+ "#008280",
38
+ "#BB0021",
39
+ "#5F559B",
40
+ "#A20056",
41
+ ],
42
+ "okabe_ito": [
43
+ "#0072B2",
44
+ "#D55E00",
45
+ "#009E73",
46
+ "#CC79A7",
47
+ "#E69F00",
48
+ "#56B4E9",
49
+ "#F0E442",
50
+ "#222222",
51
+ ],
52
+ "tol_bright": [
53
+ "#4477AA",
54
+ "#EE6677",
55
+ "#228833",
56
+ "#CCBB44",
57
+ "#66CCEE",
58
+ "#AA3377",
59
+ "#BBBBBB",
60
+ ],
61
+ "tol_muted": [
62
+ "#332288",
63
+ "#88CCEE",
64
+ "#44AA99",
65
+ "#117733",
66
+ "#999933",
67
+ "#DDCC77",
68
+ "#CC6677",
69
+ "#882255",
70
+ "#AA4499",
71
+ ],
72
+ }
73
+
74
+ BANNED_COLORMAPS: frozenset[str] = frozenset(
75
+ {
76
+ "jet",
77
+ "rainbow",
78
+ "hsv",
79
+ "gist_rainbow",
80
+ "nipy_spectral",
81
+ "prism",
82
+ "flag",
83
+ }
84
+ )
85
+
86
+ # Machado, Oliveira & Fernandes (2009), IEEE TVCG, severity = 1.0 (dichromacy)
87
+ # Matrices operate in linear sRGB space; row sums equal 1.0 (achromatic white preserved).
88
+ _CVD_MATRICES: dict[str, np.ndarray] = {
89
+ "deuteranopia": np.array(
90
+ [
91
+ [0.367322, 0.860646, -0.227968],
92
+ [0.280085, 0.672501, 0.047413],
93
+ [-0.011820, 0.042940, 0.968881],
94
+ ],
95
+ dtype=np.float64,
96
+ ),
97
+ "protanopia": np.array(
98
+ [
99
+ [0.152286, 1.052583, -0.204868],
100
+ [0.114503, 0.786281, 0.099216],
101
+ [-0.003882, -0.048116, 1.051998],
102
+ ],
103
+ dtype=np.float64,
104
+ ),
105
+ "tritanopia": np.array(
106
+ [
107
+ [1.255528, -0.076749, -0.178779],
108
+ [-0.078411, 0.930809, 0.147602],
109
+ [0.004733, 0.691367, 0.303900],
110
+ ],
111
+ dtype=np.float64,
112
+ ),
113
+ }
114
+
115
+ # Rec.709 / sRGB luminance weights in linear light
116
+ _REC709_WEIGHTS = np.array([0.2126, 0.7152, 0.0722], dtype=np.float64)
117
+
118
+
119
+ def get_palette(name: str) -> list[str]:
120
+ """Return a copy of the named color palette."""
121
+ if not isinstance(name, str):
122
+ raise TypeError(f"Palette name must be a str, got {type(name).__name__}")
123
+ key = name.strip().lower().replace("-", "_").replace(" ", "_")
124
+ if key not in PALETTES:
125
+ valid = ", ".join(sorted(PALETTES.keys()))
126
+ raise ValueError(f"Unknown palette {name!r}. Available palettes: {valid}")
127
+ return list(PALETTES[key])
128
+
129
+
130
+ def _srgb_to_linear(srgb: np.ndarray) -> np.ndarray:
131
+ """Convert sRGB values in [0, 1] to linear RGB."""
132
+ return np.where(srgb <= 0.04045, srgb / 12.92, np.power((srgb + 0.055) / 1.055, 2.4))
133
+
134
+
135
+ def _linear_to_srgb(linear: np.ndarray) -> np.ndarray:
136
+ """Convert linear RGB values in [0, 1] to sRGB."""
137
+ clipped = np.clip(linear, 0.0, 1.0)
138
+ return np.where(
139
+ clipped <= 0.0031308,
140
+ 12.92 * clipped,
141
+ 1.055 * np.power(clipped, 1.0 / 2.4) - 0.055,
142
+ )
143
+
144
+
145
+ def simulate_cvd(
146
+ rgb: np.ndarray,
147
+ kind: Literal["deuteranopia", "protanopia", "tritanopia", "grayscale"],
148
+ ) -> np.ndarray:
149
+ """Simulate color-vision deficiency or print grayscale on an RGB(A) image array.
150
+
151
+ Parameters
152
+ ----------
153
+ rgb:
154
+ NumPy array of shape ``(..., 3)`` (or ``(..., 4)``, preserving alpha) with
155
+ ``uint8`` values in ``[0, 255]`` or floating-point values in ``[0.0, 1.0]``.
156
+ kind:
157
+ One of ``"deuteranopia"``, ``"protanopia"``, ``"tritanopia"``, or ``"grayscale"``.
158
+
159
+ Returns
160
+ -------
161
+ np.ndarray
162
+ Simulated image array with the same shape and dtype as ``rgb``.
163
+ """
164
+ arr = np.asarray(rgb)
165
+ if arr.ndim < 2 or arr.shape[-1] not in (3, 4):
166
+ raise ValueError(f"Expected array with shape (..., 3) or (..., 4), got {arr.shape}")
167
+
168
+ kind_key = kind.strip().lower()
169
+ if kind_key not in ("deuteranopia", "protanopia", "tritanopia", "grayscale"):
170
+ raise ValueError(
171
+ f"Unknown CVD simulation kind {kind!r}. "
172
+ "Expected one of: 'deuteranopia', 'protanopia', 'tritanopia', 'grayscale'."
173
+ )
174
+
175
+ is_uint8 = arr.dtype == np.uint8
176
+ if is_uint8:
177
+ rgb_float = arr[..., :3].astype(np.float64) / 255.0
178
+ elif np.issubdtype(arr.dtype, np.floating):
179
+ rgb_float = np.clip(arr[..., :3].astype(np.float64), 0.0, 1.0)
180
+ else:
181
+ raise TypeError(f"Expected uint8 or floating-point array, got dtype {arr.dtype}")
182
+
183
+ linear = _srgb_to_linear(rgb_float)
184
+
185
+ if kind_key == "grayscale":
186
+ lum = np.tensordot(linear, _REC709_WEIGHTS, axes=([-1], [0]))
187
+ sim_linear = np.repeat(lum[..., np.newaxis], 3, axis=-1)
188
+ else:
189
+ matrix = _CVD_MATRICES[kind_key]
190
+ sim_linear = np.einsum("...j,ij->...i", linear, matrix)
191
+
192
+ sim_srgb = np.clip(_linear_to_srgb(sim_linear), 0.0, 1.0)
193
+
194
+ if is_uint8:
195
+ sim_rgb = np.rint(sim_srgb * 255.0).astype(np.uint8)
196
+ else:
197
+ sim_rgb = sim_srgb.astype(arr.dtype)
198
+
199
+ if arr.shape[-1] == 4:
200
+ return np.concatenate([sim_rgb, arr[..., 3:4]], axis=-1)
201
+ return sim_rgb
202
+
203
+
204
+ _EDITORIAL_COLORMAP_STOPS: dict[str, list[str]] = {
205
+ "mintfig_navy": ["#F7F9FC", "#DCE6F2", "#9CB9D6", "#507AA6", "#25456E", "#152640"],
206
+ "mintfig_teal": ["#F6FAFA", "#D2ECE8", "#7DC9BF", "#1B9A87", "#0C594E"],
207
+ "mintfig_coral": ["#FCF8F7", "#F8DDD6", "#EE9E8A", "#DC5842", "#8C2215"],
208
+ "mintfig_div": ["#254F82", "#72A2C9", "#F7F8FA", "#E98B77", "#B83227"],
209
+ }
210
+
211
+ EDITORIAL_COLORMAPS: tuple[str, ...] = (
212
+ "mintfig_navy",
213
+ "mintfig_teal",
214
+ "mintfig_coral",
215
+ "mintfig_div",
216
+ )
217
+
218
+
219
+ def register_colormaps() -> tuple[str, ...]:
220
+ """Register mintfig's editorial Matplotlib colormaps (and reversed variants)."""
221
+ for name in EDITORIAL_COLORMAPS:
222
+ stops = _EDITORIAL_COLORMAP_STOPS[name]
223
+ if name not in mpl.colormaps:
224
+ cmap = LinearSegmentedColormap.from_list(name, stops)
225
+ mpl.colormaps.register(cmap, name=name)
226
+ rev_name = f"{name}_r"
227
+ if rev_name not in mpl.colormaps:
228
+ rev_cmap = LinearSegmentedColormap.from_list(rev_name, list(reversed(stops)))
229
+ mpl.colormaps.register(rev_cmap, name=rev_name)
230
+ return EDITORIAL_COLORMAPS
231
+
232
+
233
+ register_colormaps()