Files
MCM-ICM_Toolkit/tests/test_viz.py
T
guzhujushi 5319589dfd chore: 初始化 MCM/ICM Toolkit 仓库骨架
审查并精简 ChatGPT 的建库建议后落地的第一版骨架,只定接口不写实现。

- 建议审查:14 个包 → 7 个;约 250 个函数 → 约 40 个;5 个 Result 类 → 1 个
  (docs/01-suggestion-review.md)
- docs/:架构、路线图(含永久不做清单)、环境、Git 工作流、函数卡片模板
- src/mcm_toolkit/:core / data / stats / models / solve / decision / viz
  共 23 个模块,均为签名 + docstring + NotImplementedError
- tests/:9 个文件,其中 test_api_surface.py 已通过(20 passed)
- examples/ problems/ notebooks/ figures/ tables/ 骨架
- pyproject.toml(核心依赖 4 个 + 6 组 extras)、environment.yml
- scripts/:setup-venv.ps1(ASCII + LF)、setup-conda.ps1、doctor.ps1
- 环境:.venv(system-site-packages)与 conda 环境 mcm-toolkit 均已建好,
  数值栈版本与 base 完全对齐(numpy 2.4.6 / scipy 1.18.1 / pandas 3.0.6,MKL 后端)
2026-10-01 17:43:48 +08:00

53 lines
1.6 KiB
Python

"""viz 包测试。
绘图测试不比对像素——只验证"能画出来、能存成文件、尺寸符合美赛要求"。
"""
from __future__ import annotations
import matplotlib
matplotlib.use("Agg")
import pytest
pytestmark = pytest.mark.skip(reason="未实现:见 docs/03-roadmap.md L0/L1")
@pytest.mark.parametrize("kind", ["line", "scatter", "hist", "box", "bar"])
def test_plot_kinds_produce_figure(kind, rng):
from mcm_toolkit.viz import plot
x = rng.normal(size=100)
fig, ax = plot(x, x, kind=kind)
assert fig is not None and ax is not None
def test_mcm_style_sets_300dpi():
from mcm_toolkit.viz import mcm_style
mcm_style()
assert matplotlib.rcParams["figure.dpi"] == 300.0
assert matplotlib.rcParams["font.size"] >= 8.0
def test_save_figure_writes_png_and_pdf(tmp_path, rng):
from mcm_toolkit.viz import plot, save_figure
fig, _ = plot(rng.normal(size=50), rng.normal(size=50), kind="scatter")
paths = save_figure(fig, tmp_path / "fig1", formats=("png", "pdf"))
assert {p.suffix for p in paths} == {".png", ".pdf"}
assert all(p.exists() and p.stat().st_size > 0 for p in paths)
def test_save_table_writes_latex_with_caption(tmp_path):
import pandas as pd
from mcm_toolkit.viz import save_table
df = pd.DataFrame({"rmse": [0.31, 0.28]}, index=["linear", "rf"])
paths = save_table(df, tmp_path / "tab1", caption="模型对比", label="tab:compare")
tex = next(p for p in paths if p.suffix == ".tex")
content = tex.read_text(encoding="utf-8")
assert "模型对比" in content and "tab:compare" in content