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/bundle.py
ADDED
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
"""Submission-ready figure bundle exporter (PDF, SVG, PNG, proof, LaTeX, JSON)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import hashlib
|
|
6
|
+
import json
|
|
7
|
+
import shutil
|
|
8
|
+
from dataclasses import dataclass
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import TYPE_CHECKING, Any
|
|
11
|
+
|
|
12
|
+
from matplotlib.cm import ScalarMappable
|
|
13
|
+
|
|
14
|
+
from . import export
|
|
15
|
+
from .specs import JournalSpec, get_spec, in_to_mm
|
|
16
|
+
|
|
17
|
+
if TYPE_CHECKING:
|
|
18
|
+
from matplotlib.figure import Figure
|
|
19
|
+
|
|
20
|
+
from .plate import Plate
|
|
21
|
+
from .preflight import PreflightReport
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@dataclass
|
|
25
|
+
class BundleResult:
|
|
26
|
+
"""Paths and preflight report produced by :func:`bundle_figure`."""
|
|
27
|
+
|
|
28
|
+
out_dir: Path
|
|
29
|
+
figure_pdf: Path
|
|
30
|
+
figure_svg: Path
|
|
31
|
+
preview_png: Path
|
|
32
|
+
proof_png: Path | None
|
|
33
|
+
figure_tex: Path
|
|
34
|
+
reproduce_py: Path | None
|
|
35
|
+
report_json: Path
|
|
36
|
+
report: PreflightReport
|
|
37
|
+
figure_json: Path | None = None
|
|
38
|
+
|
|
39
|
+
def to_dict(self) -> dict[str, str | None]:
|
|
40
|
+
"""Return string paths for all exported bundle artifacts."""
|
|
41
|
+
return {
|
|
42
|
+
"out_dir": str(self.out_dir),
|
|
43
|
+
"figure_pdf": str(self.figure_pdf),
|
|
44
|
+
"figure_svg": str(self.figure_svg),
|
|
45
|
+
"preview_png": str(self.preview_png),
|
|
46
|
+
"proof_png": str(self.proof_png) if self.proof_png is not None else None,
|
|
47
|
+
"figure_tex": str(self.figure_tex),
|
|
48
|
+
"reproduce_py": str(self.reproduce_py) if self.reproduce_py is not None else None,
|
|
49
|
+
"report_json": str(self.report_json),
|
|
50
|
+
"figure_json": str(self.figure_json) if self.figure_json is not None else None,
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _build_latex_snippet(
|
|
55
|
+
*,
|
|
56
|
+
spec: JournalSpec,
|
|
57
|
+
width_mm: float,
|
|
58
|
+
height_mm: float,
|
|
59
|
+
caption: str,
|
|
60
|
+
label: str,
|
|
61
|
+
) -> str:
|
|
62
|
+
"""Build a clean LaTeX figure environment referencing ``figure.pdf``."""
|
|
63
|
+
spec_version = getattr(spec, "spec_version", "")
|
|
64
|
+
source_url = getattr(spec, "source_url", "")
|
|
65
|
+
|
|
66
|
+
header_parts = [f"Journal: {spec.display_name} ({spec.name})"]
|
|
67
|
+
if spec_version:
|
|
68
|
+
header_parts.append(f"Spec version: {spec_version}")
|
|
69
|
+
lines = [f"% Generated by mintfig | {' | '.join(header_parts)}"]
|
|
70
|
+
if source_url:
|
|
71
|
+
lines.append(f"% Guidelines: {source_url}")
|
|
72
|
+
lines.append(f"% Physical dimensions: {width_mm:.1f} x {height_mm:.1f} mm")
|
|
73
|
+
lines.extend(
|
|
74
|
+
[
|
|
75
|
+
r"\begin{figure}[t]",
|
|
76
|
+
r" \centering",
|
|
77
|
+
rf" \includegraphics[width={width_mm:.1f}mm]{{figure.pdf}}",
|
|
78
|
+
rf" \caption{{{caption}}}",
|
|
79
|
+
rf" \label{{{label}}}",
|
|
80
|
+
r"\end{figure}",
|
|
81
|
+
"",
|
|
82
|
+
]
|
|
83
|
+
)
|
|
84
|
+
return "\n".join(lines)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def _build_figure_sidecar(
|
|
88
|
+
*,
|
|
89
|
+
fig: Figure,
|
|
90
|
+
spec: JournalSpec,
|
|
91
|
+
width_mm: float,
|
|
92
|
+
height_mm: float,
|
|
93
|
+
caption: str,
|
|
94
|
+
label: str,
|
|
95
|
+
takeaway: str | None,
|
|
96
|
+
takeaway_subtitle: str | None,
|
|
97
|
+
panel_notes: dict[str, str] | None,
|
|
98
|
+
reproduce_py: Path | None,
|
|
99
|
+
plate: Plate | None,
|
|
100
|
+
) -> dict[str, Any]:
|
|
101
|
+
"""Build the message-first ``figure.json`` sidecar for humans and downstream agents."""
|
|
102
|
+
from .preflight import _maps_data_to_color
|
|
103
|
+
|
|
104
|
+
notes_lookup = {k.lower(): v for k, v in (panel_notes or {}).items()}
|
|
105
|
+
repro_sha256: str | None = None
|
|
106
|
+
if reproduce_py is not None and reproduce_py.exists():
|
|
107
|
+
repro_sha256 = hashlib.sha256(reproduce_py.read_bytes()).hexdigest()
|
|
108
|
+
|
|
109
|
+
if plate is not None:
|
|
110
|
+
panel_items = [(key, plate.axes[key]) for key in plate.panel_keys]
|
|
111
|
+
else:
|
|
112
|
+
panel_items = [
|
|
113
|
+
(getattr(ax, "_mintfig_panel_key", None) or f"ax{idx}", ax)
|
|
114
|
+
for idx, ax in enumerate(fig.axes)
|
|
115
|
+
if ax.get_visible() and getattr(ax, "_colorbar", None) is None
|
|
116
|
+
]
|
|
117
|
+
|
|
118
|
+
panels_meta: list[dict[str, Any]] = []
|
|
119
|
+
for key, ax in panel_items:
|
|
120
|
+
series = [
|
|
121
|
+
str(line.get_label())
|
|
122
|
+
for line in ax.get_lines()
|
|
123
|
+
if line.get_visible()
|
|
124
|
+
and line.get_label()
|
|
125
|
+
and not str(line.get_label()).startswith("_")
|
|
126
|
+
]
|
|
127
|
+
cmaps: list[str] = []
|
|
128
|
+
has_cbar = False
|
|
129
|
+
for obj in list(ax.images) + list(ax.collections):
|
|
130
|
+
if isinstance(obj, ScalarMappable) and _maps_data_to_color(obj):
|
|
131
|
+
cname = str(obj.get_cmap().name)
|
|
132
|
+
if cname not in cmaps:
|
|
133
|
+
cmaps.append(cname)
|
|
134
|
+
if getattr(obj, "colorbar", None) is not None:
|
|
135
|
+
has_cbar = True
|
|
136
|
+
title = (ax.get_title() or ax.get_title(loc="left") or "").strip()
|
|
137
|
+
panels_meta.append(
|
|
138
|
+
{
|
|
139
|
+
"key": str(key),
|
|
140
|
+
"title": title or None,
|
|
141
|
+
"xlabel": ax.get_xlabel().strip() or None,
|
|
142
|
+
"ylabel": ax.get_ylabel().strip() or None,
|
|
143
|
+
"note": notes_lookup.get(str(key).lower()),
|
|
144
|
+
"series": series,
|
|
145
|
+
"colormaps": cmaps,
|
|
146
|
+
"has_colorbar": has_cbar,
|
|
147
|
+
"xscale": ax.get_xscale(),
|
|
148
|
+
"yscale": ax.get_yscale(),
|
|
149
|
+
}
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
return {
|
|
153
|
+
"schema_version": "0.2",
|
|
154
|
+
"journal": spec.name,
|
|
155
|
+
"display_name": spec.display_name,
|
|
156
|
+
"spec_version": getattr(spec, "spec_version", ""),
|
|
157
|
+
"spec_source_url": getattr(spec, "source_url", ""),
|
|
158
|
+
"dimensions_mm": {
|
|
159
|
+
"width": round(float(width_mm), 2),
|
|
160
|
+
"height": round(float(height_mm), 2),
|
|
161
|
+
},
|
|
162
|
+
"takeaway": takeaway,
|
|
163
|
+
"takeaway_subtitle": takeaway_subtitle,
|
|
164
|
+
"caption": caption,
|
|
165
|
+
"label": label,
|
|
166
|
+
"reproduce_sha256": repro_sha256,
|
|
167
|
+
"panels": panels_meta,
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def bundle_figure(
|
|
172
|
+
target: Plate | Figure,
|
|
173
|
+
out_dir: str | Path,
|
|
174
|
+
*,
|
|
175
|
+
journal: str | JournalSpec | None = None,
|
|
176
|
+
report: PreflightReport | None = None,
|
|
177
|
+
source_path: str | Path | None = None,
|
|
178
|
+
source_code: str | None = None,
|
|
179
|
+
caption: str | None = None,
|
|
180
|
+
label: str = "fig:fig1",
|
|
181
|
+
takeaway: str | None = None,
|
|
182
|
+
takeaway_subtitle: str | None = None,
|
|
183
|
+
panel_notes: dict[str, str] | None = None,
|
|
184
|
+
include_proof: bool = True,
|
|
185
|
+
preview_dpi: int = 300,
|
|
186
|
+
rasterize_above: int | None = 10_000,
|
|
187
|
+
) -> BundleResult:
|
|
188
|
+
"""Export a complete submission-ready artifact bundle to ``out_dir``.
|
|
189
|
+
|
|
190
|
+
Writes ``figure.pdf`` (locked-mm Type 42 vector), ``figure.svg`` (editable
|
|
191
|
+
vector text), ``preview.png``, optional ``proof.png`` (CVD + scorecard),
|
|
192
|
+
``figure.tex`` (LaTeX inclusion snippet), ``figure.json`` (message-first
|
|
193
|
+
structured sidecar), optional ``reproduce.py``, and ``report.json``.
|
|
194
|
+
"""
|
|
195
|
+
from . import preflight as _preflight
|
|
196
|
+
from . import proof as _proof
|
|
197
|
+
from .plate import Plate
|
|
198
|
+
|
|
199
|
+
plate_obj: Plate | None = target if isinstance(target, Plate) else None
|
|
200
|
+
if plate_obj is not None:
|
|
201
|
+
fig = plate_obj.fig
|
|
202
|
+
spec = get_spec(journal) if journal is not None else plate_obj.spec
|
|
203
|
+
else:
|
|
204
|
+
fig = target
|
|
205
|
+
if journal is not None:
|
|
206
|
+
spec = get_spec(journal)
|
|
207
|
+
elif getattr(fig, "_mintfig_journal", None):
|
|
208
|
+
spec = get_spec(fig._mintfig_journal) # type: ignore[attr-defined]
|
|
209
|
+
elif report is not None:
|
|
210
|
+
spec = get_spec(report.journal)
|
|
211
|
+
else:
|
|
212
|
+
spec = get_spec("nature")
|
|
213
|
+
|
|
214
|
+
out = Path(out_dir)
|
|
215
|
+
out.mkdir(parents=True, exist_ok=True)
|
|
216
|
+
|
|
217
|
+
w_in, h_in = fig.get_size_inches()
|
|
218
|
+
width_mm = in_to_mm(float(w_in))
|
|
219
|
+
height_mm = in_to_mm(float(h_in))
|
|
220
|
+
|
|
221
|
+
active_report = (
|
|
222
|
+
report if report is not None else _preflight.preflight_figure(fig, spec, strict=False)
|
|
223
|
+
)
|
|
224
|
+
|
|
225
|
+
figure_pdf = export.save_figure(
|
|
226
|
+
fig,
|
|
227
|
+
out / "figure.pdf",
|
|
228
|
+
spec=spec,
|
|
229
|
+
rasterize_above=rasterize_above,
|
|
230
|
+
lock_dimensions=True,
|
|
231
|
+
)
|
|
232
|
+
figure_svg = export.save_figure(
|
|
233
|
+
fig,
|
|
234
|
+
out / "figure.svg",
|
|
235
|
+
spec=spec,
|
|
236
|
+
rasterize_above=rasterize_above,
|
|
237
|
+
lock_dimensions=True,
|
|
238
|
+
)
|
|
239
|
+
preview_png = export.save_figure(
|
|
240
|
+
fig,
|
|
241
|
+
out / "preview.png",
|
|
242
|
+
spec=spec,
|
|
243
|
+
dpi=preview_dpi,
|
|
244
|
+
rasterize_above=rasterize_above,
|
|
245
|
+
lock_dimensions=True,
|
|
246
|
+
)
|
|
247
|
+
|
|
248
|
+
proof_png: Path | None = None
|
|
249
|
+
if include_proof:
|
|
250
|
+
proof_png = _proof.render_proof_sheet(fig, active_report, out / "proof.png", dpi=220)
|
|
251
|
+
|
|
252
|
+
resolved_caption = caption
|
|
253
|
+
if resolved_caption is None and plate_obj is not None:
|
|
254
|
+
resolved_caption = plate_obj.caption
|
|
255
|
+
effective_caption = (
|
|
256
|
+
resolved_caption
|
|
257
|
+
if resolved_caption is not None
|
|
258
|
+
else f"Publication figure formatted for {spec.display_name} ({width_mm:.1f} mm)."
|
|
259
|
+
)
|
|
260
|
+
figure_tex = out / "figure.tex"
|
|
261
|
+
figure_tex.write_text(
|
|
262
|
+
_build_latex_snippet(
|
|
263
|
+
spec=spec,
|
|
264
|
+
width_mm=width_mm,
|
|
265
|
+
height_mm=height_mm,
|
|
266
|
+
caption=effective_caption,
|
|
267
|
+
label=label,
|
|
268
|
+
),
|
|
269
|
+
encoding="utf-8",
|
|
270
|
+
)
|
|
271
|
+
|
|
272
|
+
reproduce_py: Path | None = None
|
|
273
|
+
if source_path is not None:
|
|
274
|
+
src = Path(source_path)
|
|
275
|
+
dest = out / "reproduce.py"
|
|
276
|
+
if src.resolve() != dest.resolve():
|
|
277
|
+
shutil.copy2(src, dest)
|
|
278
|
+
reproduce_py = dest
|
|
279
|
+
elif source_code is not None:
|
|
280
|
+
dest = out / "reproduce.py"
|
|
281
|
+
dest.write_text(source_code, encoding="utf-8")
|
|
282
|
+
reproduce_py = dest
|
|
283
|
+
|
|
284
|
+
resolved_takeaway = takeaway if takeaway is not None else (plate_obj.takeaway if plate_obj else None)
|
|
285
|
+
resolved_subtitle = (
|
|
286
|
+
takeaway_subtitle
|
|
287
|
+
if takeaway_subtitle is not None
|
|
288
|
+
else (plate_obj.takeaway_subtitle if plate_obj else None)
|
|
289
|
+
)
|
|
290
|
+
merged_notes: dict[str, str] = {}
|
|
291
|
+
if plate_obj is not None and plate_obj.panel_notes:
|
|
292
|
+
merged_notes.update(plate_obj.panel_notes)
|
|
293
|
+
if panel_notes:
|
|
294
|
+
merged_notes.update(panel_notes)
|
|
295
|
+
|
|
296
|
+
figure_json = out / "figure.json"
|
|
297
|
+
sidecar = _build_figure_sidecar(
|
|
298
|
+
fig=fig,
|
|
299
|
+
spec=spec,
|
|
300
|
+
width_mm=width_mm,
|
|
301
|
+
height_mm=height_mm,
|
|
302
|
+
caption=effective_caption,
|
|
303
|
+
label=label,
|
|
304
|
+
takeaway=resolved_takeaway,
|
|
305
|
+
takeaway_subtitle=resolved_subtitle,
|
|
306
|
+
panel_notes=merged_notes or None,
|
|
307
|
+
reproduce_py=reproduce_py,
|
|
308
|
+
plate=plate_obj,
|
|
309
|
+
)
|
|
310
|
+
figure_json.write_text(json.dumps(sidecar, indent=2) + "\n", encoding="utf-8")
|
|
311
|
+
|
|
312
|
+
report_json = out / "report.json"
|
|
313
|
+
result = BundleResult(
|
|
314
|
+
out_dir=out,
|
|
315
|
+
figure_pdf=figure_pdf,
|
|
316
|
+
figure_svg=figure_svg,
|
|
317
|
+
preview_png=preview_png,
|
|
318
|
+
proof_png=proof_png,
|
|
319
|
+
figure_tex=figure_tex,
|
|
320
|
+
reproduce_py=reproduce_py,
|
|
321
|
+
report_json=report_json,
|
|
322
|
+
report=active_report,
|
|
323
|
+
figure_json=figure_json,
|
|
324
|
+
)
|
|
325
|
+
|
|
326
|
+
artifacts_dict = result.to_dict()
|
|
327
|
+
if hasattr(active_report, "to_agent_dict"):
|
|
328
|
+
payload = active_report.to_agent_dict(artifacts=artifacts_dict)
|
|
329
|
+
else:
|
|
330
|
+
payload = active_report.to_dict()
|
|
331
|
+
report_json.write_text(json.dumps(payload, indent=2) + "\n", encoding="utf-8")
|
|
332
|
+
|
|
333
|
+
return result
|