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.
- mintfig/__init__.py +162 -0
- mintfig/_explain/__init__.py +1 -0
- mintfig/_explain/change.py +392 -0
- mintfig/_explain/compare.py +1308 -0
- mintfig/_explain/compare_grouped.py +472 -0
- mintfig/_explain/core.py +2005 -0
- mintfig/_explain/distribution.py +1268 -0
- mintfig/_explain/flow.py +508 -0
- mintfig/_explain/layout.py +2012 -0
- mintfig/_explain/matrix.py +623 -0
- mintfig/_explain/relate.py +1318 -0
- mintfig/_explain/timeline.py +595 -0
- mintfig/_explain/trend.py +2557 -0
- mintfig/_explain/waterfall.py +459 -0
- mintfig/_table.py +691 -0
- mintfig/_tabular.py +738 -0
- mintfig/annotations.py +1165 -0
- mintfig/assets/AGENTS_SNIPPET.md +11 -0
- mintfig/assets/SKILL.md +143 -0
- mintfig/assets/__init__.py +1 -0
- mintfig/assets/references/publication.md +79 -0
- mintfig/assets/references/scientific_and_recipes.md +213 -0
- mintfig/audit.py +179 -0
- mintfig/brief.py +518 -0
- mintfig/bundle.py +333 -0
- mintfig/cli.py +1300 -0
- mintfig/edit.py +347 -0
- mintfig/epistemic.py +1083 -0
- mintfig/explainer.py +194 -0
- mintfig/export.py +247 -0
- mintfig/inspect_data.py +583 -0
- mintfig/interactive.py +1028 -0
- mintfig/palettes.py +233 -0
- mintfig/plate.py +784 -0
- mintfig/preflight.py +2398 -0
- mintfig/proof.py +627 -0
- mintfig/runner.py +403 -0
- mintfig/skill.py +167 -0
- mintfig/specs.py +291 -0
- mintfig/style.py +151 -0
- mintfig/terminal.py +785 -0
- mintfig/themes.py +660 -0
- mintfig-0.2.0.dist-info/METADATA +239 -0
- mintfig-0.2.0.dist-info/RECORD +47 -0
- mintfig-0.2.0.dist-info/WHEEL +4 -0
- mintfig-0.2.0.dist-info/entry_points.txt +2 -0
- mintfig-0.2.0.dist-info/licenses/LICENSE +21 -0
mintfig/edit.py
ADDED
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
"""Surgical one-line editing of existing mintfig figures (``mint.edit``).
|
|
2
|
+
|
|
3
|
+
Rebuilds an ``Explanation`` from a saved PNG (embedded spec), JSON spec file,
|
|
4
|
+
spec dict, or live ``Explanation`` object, applies top-level and panel-level
|
|
5
|
+
mutations (including item/series ``drop=`` / ``keep=`` filtering), and returns
|
|
6
|
+
the re-rendered, re-audited ``Explanation``.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import copy
|
|
12
|
+
from collections.abc import Mapping, Sequence
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
from typing import Any
|
|
15
|
+
|
|
16
|
+
from ._explain.core import ExplainError
|
|
17
|
+
from ._explain.layout import Explanation, explain_from_spec, load
|
|
18
|
+
|
|
19
|
+
__all__ = ["edit"]
|
|
20
|
+
|
|
21
|
+
_UNSET = object()
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _as_name_list(value: str | Sequence[str] | None) -> list[str]:
|
|
25
|
+
if value is None:
|
|
26
|
+
return []
|
|
27
|
+
if isinstance(value, str):
|
|
28
|
+
return [value]
|
|
29
|
+
return [str(v) for v in value]
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _resolve_panel_index(panels: list[dict[str, Any]], panel: str | int) -> int:
|
|
33
|
+
if isinstance(panel, int):
|
|
34
|
+
if 0 <= panel < len(panels):
|
|
35
|
+
return panel
|
|
36
|
+
raise ExplainError(
|
|
37
|
+
f"edit(panel={panel}) out of range for {len(panels)} panel(s).",
|
|
38
|
+
fix_hint=f"Pass a panel key 'a'..'{chr(ord('a') + len(panels) - 1)}' or index 0..{len(panels) - 1}.",
|
|
39
|
+
)
|
|
40
|
+
key = str(panel).strip().lower()
|
|
41
|
+
if len(key) == 1 and "a" <= key <= "z":
|
|
42
|
+
idx = ord(key) - ord("a")
|
|
43
|
+
if 0 <= idx < len(panels):
|
|
44
|
+
return idx
|
|
45
|
+
raise ExplainError(
|
|
46
|
+
f"edit(panel={panel!r}) not found in {len(panels)} panel(s).",
|
|
47
|
+
fix_hint=f"Valid panel keys: {', '.join(repr(chr(ord('a') + i)) for i in range(len(panels)))}.",
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _prune_refs(pspec: dict[str, Any], valid: set[str]) -> None:
|
|
52
|
+
"""Keep highlight/baseline/error/inner_error/boost/n consistent after dropping items."""
|
|
53
|
+
hl = pspec.get("highlight")
|
|
54
|
+
if isinstance(hl, str):
|
|
55
|
+
if hl not in valid:
|
|
56
|
+
pspec.pop("highlight", None)
|
|
57
|
+
elif isinstance(hl, list):
|
|
58
|
+
kept_hl = [h for h in hl if str(h) in valid]
|
|
59
|
+
if kept_hl:
|
|
60
|
+
pspec["highlight"] = kept_hl
|
|
61
|
+
else:
|
|
62
|
+
pspec.pop("highlight", None)
|
|
63
|
+
|
|
64
|
+
base = pspec.get("baseline")
|
|
65
|
+
if isinstance(base, str) and base not in valid:
|
|
66
|
+
pspec.pop("baseline", None)
|
|
67
|
+
|
|
68
|
+
for aux in ("error", "inner_error", "boost", "n", "band"):
|
|
69
|
+
mapping = pspec.get(aux)
|
|
70
|
+
if isinstance(mapping, dict):
|
|
71
|
+
pruned = {k: v for k, v in mapping.items() if k in valid}
|
|
72
|
+
if pruned:
|
|
73
|
+
pspec[aux] = pruned
|
|
74
|
+
else:
|
|
75
|
+
pspec.pop(aux, None)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _filter_panel_items(
|
|
79
|
+
pspec: dict[str, Any],
|
|
80
|
+
*,
|
|
81
|
+
drop: set[str],
|
|
82
|
+
keep: set[str],
|
|
83
|
+
) -> None:
|
|
84
|
+
"""Apply ``drop`` / ``keep`` filtering to a panel's normalized JSON spec in place."""
|
|
85
|
+
kind = pspec.get("kind")
|
|
86
|
+
|
|
87
|
+
def _keep_key(k: str) -> bool:
|
|
88
|
+
if keep and k not in keep:
|
|
89
|
+
return False
|
|
90
|
+
return k not in drop
|
|
91
|
+
|
|
92
|
+
if kind == "compare":
|
|
93
|
+
vals = pspec.get("values")
|
|
94
|
+
if isinstance(vals, dict) and vals:
|
|
95
|
+
first = next(iter(vals.values()))
|
|
96
|
+
if isinstance(first, dict):
|
|
97
|
+
# Grouped compare: check if filtering categories or inner series
|
|
98
|
+
all_cats = set(vals)
|
|
99
|
+
all_series = {sk for sub in vals.values() if isinstance(sub, dict) for sk in sub}
|
|
100
|
+
targets = drop | keep
|
|
101
|
+
if targets & all_series and not (targets & all_cats):
|
|
102
|
+
new_vals = {
|
|
103
|
+
cat: {sk: sv for sk, sv in sub.items() if _keep_key(sk)}
|
|
104
|
+
for cat, sub in vals.items()
|
|
105
|
+
if isinstance(sub, dict)
|
|
106
|
+
}
|
|
107
|
+
pspec["values"] = {c: sub for c, sub in new_vals.items() if sub}
|
|
108
|
+
_prune_refs(pspec, {sk for sub in pspec["values"].values() for sk in sub})
|
|
109
|
+
else:
|
|
110
|
+
pspec["values"] = {k: v for k, v in vals.items() if _keep_key(k)}
|
|
111
|
+
else:
|
|
112
|
+
pspec["values"] = {k: v for k, v in vals.items() if _keep_key(k)}
|
|
113
|
+
_prune_refs(pspec, set(pspec["values"]))
|
|
114
|
+
return
|
|
115
|
+
|
|
116
|
+
if kind == "share":
|
|
117
|
+
parts = pspec.get("parts")
|
|
118
|
+
if isinstance(parts, dict):
|
|
119
|
+
pspec["parts"] = {k: v for k, v in parts.items() if _keep_key(k)}
|
|
120
|
+
_prune_refs(pspec, set(pspec["parts"]))
|
|
121
|
+
return
|
|
122
|
+
|
|
123
|
+
if kind == "change":
|
|
124
|
+
before = pspec.get("before")
|
|
125
|
+
after = pspec.get("after")
|
|
126
|
+
if isinstance(before, dict) and isinstance(after, dict):
|
|
127
|
+
pspec["before"] = {k: v for k, v in before.items() if _keep_key(k)}
|
|
128
|
+
pspec["after"] = {k: v for k, v in after.items() if _keep_key(k)}
|
|
129
|
+
_prune_refs(pspec, set(pspec["before"]) & set(pspec["after"]))
|
|
130
|
+
return
|
|
131
|
+
|
|
132
|
+
if kind in {"distribution", "histogram"}:
|
|
133
|
+
gkey = "groups" if "groups" in pspec else "values"
|
|
134
|
+
groups = pspec.get(gkey)
|
|
135
|
+
if isinstance(groups, dict):
|
|
136
|
+
pspec[gkey] = {k: v for k, v in groups.items() if _keep_key(k)}
|
|
137
|
+
_prune_refs(pspec, set(pspec[gkey]))
|
|
138
|
+
return
|
|
139
|
+
|
|
140
|
+
if kind == "trend":
|
|
141
|
+
for skey in ("series", "points"):
|
|
142
|
+
ser = pspec.get(skey)
|
|
143
|
+
if isinstance(ser, dict):
|
|
144
|
+
pspec[skey] = {k: v for k, v in ser.items() if _keep_key(k)}
|
|
145
|
+
valid_s = set(pspec.get("series") or {}) | set(pspec.get("points") or {})
|
|
146
|
+
if valid_s:
|
|
147
|
+
_prune_refs(pspec, valid_s)
|
|
148
|
+
return
|
|
149
|
+
|
|
150
|
+
if kind == "relate":
|
|
151
|
+
xs = pspec.get("x") or []
|
|
152
|
+
ys = pspec.get("y") or []
|
|
153
|
+
names = pspec.get("names")
|
|
154
|
+
groups = pspec.get("groups")
|
|
155
|
+
targets = drop | keep
|
|
156
|
+
use_groups = bool(groups and (targets & set(groups)) and not (names and (targets & set(names))))
|
|
157
|
+
idxs = []
|
|
158
|
+
for i in range(min(len(xs), len(ys))):
|
|
159
|
+
key_i = str(groups[i]) if use_groups else (str(names[i]) if names else str(i))
|
|
160
|
+
if _keep_key(key_i):
|
|
161
|
+
idxs.append(i)
|
|
162
|
+
pspec["x"] = [xs[i] for i in idxs]
|
|
163
|
+
pspec["y"] = [ys[i] for i in idxs]
|
|
164
|
+
if isinstance(names, list):
|
|
165
|
+
pspec["names"] = [names[i] for i in idxs]
|
|
166
|
+
if isinstance(groups, list):
|
|
167
|
+
pspec["groups"] = [groups[i] for i in idxs]
|
|
168
|
+
valid = set(pspec.get("names") or []) | set(pspec.get("groups") or [])
|
|
169
|
+
_prune_refs(pspec, valid)
|
|
170
|
+
return
|
|
171
|
+
|
|
172
|
+
if kind == "matrix":
|
|
173
|
+
rows = pspec.get("rows") or []
|
|
174
|
+
cols = pspec.get("cols") or []
|
|
175
|
+
vals = pspec.get("values") or []
|
|
176
|
+
targets = drop | keep
|
|
177
|
+
if targets & set(cols) and not (targets & set(rows)):
|
|
178
|
+
col_idxs = [j for j, c in enumerate(cols) if _keep_key(str(c))]
|
|
179
|
+
pspec["cols"] = [cols[j] for j in col_idxs]
|
|
180
|
+
pspec["values"] = [[r[j] for j in col_idxs] for r in vals]
|
|
181
|
+
else:
|
|
182
|
+
row_idxs = [i for i, r in enumerate(rows) if _keep_key(str(r))]
|
|
183
|
+
pspec["rows"] = [rows[i] for i in row_idxs]
|
|
184
|
+
pspec["values"] = [vals[i] for i in row_idxs]
|
|
185
|
+
return
|
|
186
|
+
|
|
187
|
+
if kind == "table":
|
|
188
|
+
cols = pspec.get("columns") or []
|
|
189
|
+
rows = pspec.get("rows") or []
|
|
190
|
+
targets = drop | keep
|
|
191
|
+
if targets & set(cols) and not (targets & {str(r[0]) for r in rows if r}):
|
|
192
|
+
col_idxs = [j for j, c in enumerate(cols) if _keep_key(str(c))]
|
|
193
|
+
pspec["columns"] = [cols[j] for j in col_idxs]
|
|
194
|
+
pspec["rows"] = [[r[j] for j in col_idxs] for r in rows]
|
|
195
|
+
for map_k in ("units", "align", "fmt", "bold_best"):
|
|
196
|
+
if isinstance(pspec.get(map_k), dict):
|
|
197
|
+
pspec[map_k] = {k: v for k, v in pspec[map_k].items() if k in pspec["columns"]}
|
|
198
|
+
else:
|
|
199
|
+
pspec["rows"] = [r for r in rows if r and _keep_key(str(r[0]))]
|
|
200
|
+
_prune_refs(pspec, {str(r[0]) for r in pspec["rows"] if r})
|
|
201
|
+
return
|
|
202
|
+
|
|
203
|
+
if kind == "waterfall":
|
|
204
|
+
steps = pspec.get("steps")
|
|
205
|
+
if isinstance(steps, dict):
|
|
206
|
+
pspec["steps"] = {k: v for k, v in steps.items() if _keep_key(k)}
|
|
207
|
+
_prune_refs(pspec, set(pspec["steps"]))
|
|
208
|
+
return
|
|
209
|
+
|
|
210
|
+
if kind == "timeline":
|
|
211
|
+
spans = pspec.get("spans")
|
|
212
|
+
if isinstance(spans, list):
|
|
213
|
+
pspec["spans"] = [s for s in spans if isinstance(s, list) and s and _keep_key(str(s[0]))]
|
|
214
|
+
valid_t = {str(s[0]) for s in pspec["spans"]} | {
|
|
215
|
+
str(s[3]) for s in pspec["spans"] if len(s) >= 4 and s[3] is not None
|
|
216
|
+
}
|
|
217
|
+
_prune_refs(pspec, valid_t)
|
|
218
|
+
return
|
|
219
|
+
|
|
220
|
+
if kind == "flow":
|
|
221
|
+
stages = pspec.get("stages")
|
|
222
|
+
if isinstance(stages, list):
|
|
223
|
+
pspec["stages"] = [s for s in stages if isinstance(s, list) and s and _keep_key(str(s[0]))]
|
|
224
|
+
_prune_refs(pspec, {str(s[0]) for s in pspec["stages"]})
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def edit(
|
|
228
|
+
figure: str | Path | Explanation | Mapping[str, Any],
|
|
229
|
+
*,
|
|
230
|
+
takeaway: str | None = None,
|
|
231
|
+
subtitle: Any = _UNSET,
|
|
232
|
+
source: Any = _UNSET,
|
|
233
|
+
note: Any = _UNSET,
|
|
234
|
+
style: str | None = None,
|
|
235
|
+
palette: Any = _UNSET,
|
|
236
|
+
accent: Any = _UNSET,
|
|
237
|
+
width: str | float | None = None,
|
|
238
|
+
layout: Any = _UNSET,
|
|
239
|
+
colors: Any = _UNSET,
|
|
240
|
+
panel: str | int = "a",
|
|
241
|
+
title: Any = _UNSET,
|
|
242
|
+
label: Any = _UNSET,
|
|
243
|
+
unit: Any = _UNSET,
|
|
244
|
+
highlight: Any = _UNSET,
|
|
245
|
+
baseline: Any = _UNSET,
|
|
246
|
+
sort: Any = _UNSET,
|
|
247
|
+
reference: Any = _UNSET,
|
|
248
|
+
show_delta: Any = _UNSET,
|
|
249
|
+
drop: str | Sequence[str] | None = None,
|
|
250
|
+
keep: str | Sequence[str] | None = None,
|
|
251
|
+
drop_panel: str | int | None = None,
|
|
252
|
+
strict: bool = True,
|
|
253
|
+
out: str | Path | None = None,
|
|
254
|
+
write_spec: bool = False,
|
|
255
|
+
) -> Explanation:
|
|
256
|
+
"""Apply surgical edits to an existing mintfig figure (PNG, JSON spec, or ``Explanation``).
|
|
257
|
+
|
|
258
|
+
Returns the freshly rendered and audited :class:`Explanation`. Pass ``out=``
|
|
259
|
+
to save directly to PNG, SVG, PDF, or interactive HTML.
|
|
260
|
+
"""
|
|
261
|
+
if isinstance(figure, Explanation):
|
|
262
|
+
spec = copy.deepcopy(figure.to_spec())
|
|
263
|
+
elif isinstance(figure, Mapping):
|
|
264
|
+
spec = copy.deepcopy(dict(figure))
|
|
265
|
+
elif isinstance(figure, (str, Path)):
|
|
266
|
+
spec = copy.deepcopy(load(figure).to_spec())
|
|
267
|
+
else:
|
|
268
|
+
raise ExplainError(
|
|
269
|
+
f"edit() expected a PNG/JSON path, Explanation, or spec dict; got {type(figure).__name__}.",
|
|
270
|
+
fix_hint='Pass a path like mint.edit("fig.png", style="dark", out="fig_dark.png").',
|
|
271
|
+
)
|
|
272
|
+
|
|
273
|
+
# Top-level edits
|
|
274
|
+
if takeaway is not None:
|
|
275
|
+
spec["takeaway"] = takeaway
|
|
276
|
+
for key, val in (
|
|
277
|
+
("subtitle", subtitle),
|
|
278
|
+
("source", source),
|
|
279
|
+
("note", note),
|
|
280
|
+
("palette", palette),
|
|
281
|
+
("accent", accent),
|
|
282
|
+
("layout", layout),
|
|
283
|
+
("colors", colors),
|
|
284
|
+
):
|
|
285
|
+
if val is _UNSET:
|
|
286
|
+
continue
|
|
287
|
+
if val is None:
|
|
288
|
+
spec.pop(key, None)
|
|
289
|
+
else:
|
|
290
|
+
spec[key] = val
|
|
291
|
+
if style is not None:
|
|
292
|
+
spec["style"] = style
|
|
293
|
+
if width is not None:
|
|
294
|
+
spec["width"] = width
|
|
295
|
+
|
|
296
|
+
panels = spec.get("panels")
|
|
297
|
+
if not isinstance(panels, list) or not panels:
|
|
298
|
+
raise ExplainError("Spec has no panels to edit.", fix_hint="Provide a valid mintfig spec.")
|
|
299
|
+
|
|
300
|
+
# Optional whole-panel removal
|
|
301
|
+
if drop_panel is not None:
|
|
302
|
+
if len(panels) <= 1:
|
|
303
|
+
raise ExplainError(
|
|
304
|
+
"Cannot drop the only remaining panel in a figure.",
|
|
305
|
+
fix_hint="Build a new figure instead of dropping its sole panel.",
|
|
306
|
+
)
|
|
307
|
+
rm_idx = _resolve_panel_index(panels, drop_panel)
|
|
308
|
+
del panels[rm_idx]
|
|
309
|
+
if layout is _UNSET:
|
|
310
|
+
spec.pop("layout", None)
|
|
311
|
+
|
|
312
|
+
# Panel-level edits
|
|
313
|
+
has_panel_edits = any(
|
|
314
|
+
val is not _UNSET
|
|
315
|
+
for val in (title, label, unit, highlight, baseline, sort, reference, show_delta)
|
|
316
|
+
) or drop is not None or keep is not None
|
|
317
|
+
|
|
318
|
+
if has_panel_edits:
|
|
319
|
+
p_idx = _resolve_panel_index(panels, panel)
|
|
320
|
+
pspec = panels[p_idx]
|
|
321
|
+
if drop is not None or keep is not None:
|
|
322
|
+
_filter_panel_items(
|
|
323
|
+
pspec,
|
|
324
|
+
drop=set(_as_name_list(drop)),
|
|
325
|
+
keep=set(_as_name_list(keep)),
|
|
326
|
+
)
|
|
327
|
+
for pkey, pval in (
|
|
328
|
+
("title", title),
|
|
329
|
+
("label", label),
|
|
330
|
+
("unit", unit),
|
|
331
|
+
("highlight", highlight),
|
|
332
|
+
("baseline", baseline),
|
|
333
|
+
("sort", sort),
|
|
334
|
+
("reference", reference),
|
|
335
|
+
("show_delta", show_delta),
|
|
336
|
+
):
|
|
337
|
+
if pval is _UNSET:
|
|
338
|
+
continue
|
|
339
|
+
if pval is None:
|
|
340
|
+
pspec.pop(pkey, None)
|
|
341
|
+
else:
|
|
342
|
+
pspec[pkey] = pval
|
|
343
|
+
|
|
344
|
+
result = explain_from_spec(spec, strict=strict)
|
|
345
|
+
if out is not None:
|
|
346
|
+
result.save(out, write_spec=write_spec)
|
|
347
|
+
return result
|