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 后端)
This commit is contained in:
guzhujushi committed 2026-10-01 17:43:48 +08:00
commit 5319589dfd
76 files changed
+6812

No files matched your search

+32
View File
@@ -0,0 +1,32 @@
"""pytest 公共夹具。"""
from __future__ import annotations
import numpy as np
import pytest
@pytest.fixture
def rng():
"""固定种子的随机数生成器——所有含随机的测试都必须用它,保证可复现。"""
return np.random.default_rng(20260101)
@pytest.fixture
def linear_data(rng):
"""带噪声的线性数据,用于回归类测试(真值 y = 2x + 1)。"""
x = rng.uniform(-5, 5, size=200)
y = 2.0 * x + 1.0 + rng.normal(0, 0.1, size=200)
return x, y
@pytest.fixture
def decision_matrix():
"""3 个方案 × 4 个指标的决策矩阵,用于评价类测试。"""
return np.array(
[
[9.0, 0.3, 120.0, 8.5],
[7.5, 0.5, 90.0, 9.0],
[8.8, 0.2, 150.0, 7.0],
]
)
+67
View File
@@ -0,0 +1,67 @@
"""公开 API 契约测试。
这组测试**不测算法本身**,只守两件事:
1. `__all__` 里承诺的名字,必须真的存在且可调用;
2. 子包的导出必须与顶层一致。
它现在就能通过,并且在后续实现过程中会立刻抓到"改了导出忘了改 `__all__`"这类问题。
"""
from __future__ import annotations
import importlib
import pytest
import mcm_toolkit as mcm
SUBPACKAGES = ["core", "data", "stats", "models", "solve", "decision", "viz"]
def test_version_is_string():
assert isinstance(mcm.__version__, str)
assert mcm.__version__.count(".") == 2
@pytest.mark.parametrize("name", SUBPACKAGES)
def test_subpackage_importable(name):
mod = importlib.import_module(f"mcm_toolkit.{name}")
assert hasattr(mod, "__all__"), f"mcm_toolkit.{name} 缺 __all__"
assert hasattr(mcm, name), f"顶层未导出子包 {name}"
@pytest.mark.parametrize("name", SUBPACKAGES)
def test_subpackage_all_resolves(name):
mod = importlib.import_module(f"mcm_toolkit.{name}")
missing = [n for n in mod.__all__ if not hasattr(mod, n)]
assert not missing, f"mcm_toolkit.{name}.__all__ 里有不存在的名字:{missing}"
def test_toplevel_all_resolves():
missing = [n for n in mcm.__all__ if not hasattr(mcm, n)]
assert not missing, f"顶层 __all__ 里有不存在的名字:{missing}"
def test_toplevel_shortcuts_are_callable():
for name in ["monte_carlo", "bootstrap_ci", "parameter_sweep", "topsis", "compare_models"]:
assert callable(getattr(mcm, name)), f"{name} 应当是可调用的快捷函数"
def test_no_star_import_pollution():
"""顶层不应把子包内部的函数全部摊平——只允许 __all__ 里那批。"""
allowed = set(mcm.__all__) | {"annotations"} # annotations 来自 __future__ 导入
leaked = [n for n in dir(mcm) if not n.startswith("_") and n not in allowed]
assert not leaked, f"顶层出现了 __all__ 之外的公开名字:{leaked}"
def test_result_dataclass_defaults():
r = mcm.Result("demo")
assert r.success is True
assert r.params is None
assert r.metrics is None
assert r.message == ""
def test_random_helpers_exist():
assert callable(mcm.set_seed)
assert callable(mcm.get_rng)
+69
View File
@@ -0,0 +1,69 @@
"""core 包测试。
TODO(L0):实现后去掉 skip 标记。
"""
from __future__ import annotations
import numpy as np
import pytest
@pytest.mark.skip(reason="未实现:见 docs/03-roadmap.md L0")
class TestRandom:
def test_get_rng_is_reproducible(self):
from mcm_toolkit.core import get_rng
assert get_rng(42).random() == get_rng(42).random()
def test_get_rng_passes_through_generator(self):
from mcm_toolkit.core import get_rng
g = np.random.default_rng(0)
assert get_rng(g) is g
def test_set_seed_makes_global_reproducible(self):
from mcm_toolkit.core import set_seed
set_seed(7)
a = np.random.default_rng(7).random()
set_seed(7)
b = np.random.default_rng(7).random()
assert a == b
@pytest.mark.skip(reason="未实现:见 docs/03-roadmap.md L0")
class TestValidate:
def test_check_same_length_raises_with_detail(self):
from mcm_toolkit.core import check_same_length
with pytest.raises(ValueError, match=r"2.*3|长度"):
check_same_length([1, 2], [1, 2, 3])
def test_check_array_rejects_nan(self):
from mcm_toolkit.core import check_array
with pytest.raises(ValueError):
check_array([1.0, np.nan, 2.0], finite=True)
def test_check_probability_bounds(self):
from mcm_toolkit.core import check_probability
assert check_probability(0.5) == 0.5
with pytest.raises(ValueError):
check_probability(1.5)
def test_check_square_matrix(self):
from mcm_toolkit.core import check_square_matrix
check_square_matrix([[1, 2], [3, 4]])
with pytest.raises(ValueError):
check_square_matrix([[1, 2, 3], [4, 5, 6]])
@pytest.mark.skip(reason="未实现:见 docs/03-roadmap.md L0")
def test_result_repr_is_readable():
from mcm_toolkit.core import Result
text = repr(Result("demo", params={"a": 1}, metrics={"r2": 0.9}))
assert "demo" in text and "r2" in text
+65
View File
@@ -0,0 +1,65 @@
"""data 包测试。"""
from __future__ import annotations
import numpy as np
import pytest
pytestmark = pytest.mark.skip(reason="未实现:见 docs/03-roadmap.md L0/L1")
def test_detect_outliers_finds_planted_points():
"""人工埋 3 个离群点,IQR 法应正好命中 3 个。"""
from mcm_toolkit.data import detect_outliers
x = np.concatenate([np.arange(100.0), [1e6, -1e6, 1e7]])
mask = np.asarray(detect_outliers(x, method="iqr"))
assert mask.sum() == 3
assert mask[-3:].all()
def test_detect_outliers_returns_mask_not_filtered_data():
"""约定:只标不删——返回与原数据同形的布尔掩码。"""
from mcm_toolkit.data import detect_outliers
x = np.arange(50.0)
mask = np.asarray(detect_outliers(x, method="zscore"))
assert mask.shape == x.shape
assert mask.dtype == bool
def test_normalize_minmax_range(rng):
from mcm_toolkit.data import normalize
x = rng.normal(size=(50, 3))
out = np.asarray(normalize(x, method="minmax"))
assert np.allclose(out.min(axis=0), 0.0)
assert np.allclose(out.max(axis=0), 1.0)
def test_standardize_has_zero_mean_unit_std(rng):
from mcm_toolkit.data import standardize
x = rng.normal(5, 3, size=1000)
out = np.asarray(standardize(x))
assert abs(out.mean()) < 1e-9
assert abs(out.std() - 1.0) < 1e-9
def test_train_test_split_sizes_and_no_overlap(rng):
from mcm_toolkit.data import train_test_split
X = rng.normal(size=(100, 2))
Xtr, Xte = train_test_split(X, test_size=0.2, random_state=0)
assert len(Xtr) == 80 and len(Xte) == 20
assert len({tuple(r) for r in Xtr} & {tuple(r) for r in Xte}) == 0
def test_load_table_detects_gbk(tmp_path):
"""国内赛题数据常见 GBK,编码探测必须能兜住。"""
from mcm_toolkit.data import load_table
p = tmp_path / "gbk.csv"
p.write_bytes("城市,人口\n北京,2189\n".encode("gbk"))
df = load_table(p)
assert list(df.columns) == ["城市", "人口"]
+79
View File
@@ -0,0 +1,79 @@
"""decision 包测试。"""
from __future__ import annotations
import numpy as np
import pytest
pytestmark = pytest.mark.skip(reason="未实现:见 docs/03-roadmap.md L1")
def test_topsis_ranks_dominated_alternative_last():
"""一个在所有指标上都最差的方案,TOPSIS 必须排最后。"""
from mcm_toolkit.decision import topsis
X = np.array(
[
[9.0, 8.0, 7.0], # 全面最优
[5.0, 5.0, 5.0], # 中等
[1.0, 1.0, 1.0], # 全面最差
]
)
r = topsis(X, benefit_columns=[0, 1, 2])
ranks = dict(zip(range(3), r.data["rank"], strict=False))
assert ranks[0] == 1
assert ranks[2] == 3
def test_topsis_direction_flips_result():
"""把某列从效益型改成成本型,排名必须变——方向不能静默搞错。"""
from mcm_toolkit.decision import topsis
X = np.array([[9.0, 1.0], [1.0, 9.0]])
a = topsis(X, benefit_columns=[0, 1])
b = topsis(X, benefit_columns=[0])
assert list(a.data["rank"]) != list(b.data["rank"])
def test_entropy_weights_sum_to_one_and_prefers_dispersed_column(rng):
from mcm_toolkit.decision import entropy_weights
X = np.column_stack([rng.normal(size=50), np.ones(50)]) # 第二列无差异
r = entropy_weights(X)
assert np.isclose(r.params["weights"].sum(), 1.0)
assert r.params["weights"][0] > r.params["weights"][1]
def test_entropy_weights_handles_zero_and_negative():
"""熵权法的三个坑之一:零值不能产生 NaN。"""
from mcm_toolkit.decision import entropy_weights
r = entropy_weights(np.array([[0.0, 1.0], [1.0, 2.0], [2.0, 0.0]]))
assert np.isfinite(r.params["weights"]).all()
def test_ahp_consistency_for_consistent_matrix():
"""完全一致的判断矩阵 CR 应接近 0。"""
from mcm_toolkit.decision import ahp_weights
A = np.array([[1, 2, 4], [0.5, 1, 2], [0.25, 0.5, 1]], dtype=float)
r = ahp_weights(A)
assert r.metrics["CR"] < 0.01
def test_ahp_weights_match_geometric_mean_for_consistent_matrix():
from mcm_toolkit.decision import ahp_weights
A = np.array([[1, 2, 4], [0.5, 1, 2], [0.25, 0.5, 1]], dtype=float)
r = ahp_weights(A)
geo = (A.prod(axis=1)) ** (1 / 3)
assert np.allclose(r.params["weights"], geo / geo.sum(), atol=1e-3)
def test_gm11_fits_exponential_sequence():
"""GM(1,1) 对纯指数序列应拟合得极好。"""
from mcm_toolkit.decision import gm11_fit_predict
series = [2.0 * 1.1**k for k in range(6)]
r = gm11_fit_predict(series, steps=2)
assert r.metrics["c_ratio"] < 0.35 # 后验差一级
+58
View File
@@ -0,0 +1,58 @@
"""models 包测试。"""
from __future__ import annotations
import numpy as np
import pytest
pytestmark = pytest.mark.skip(reason="未实现:见 docs/03-roadmap.md L1")
def test_fit_linear_recovers_known_coefficients(linear_data):
"""真值 y = 2x + 1,拟合系数应在 1% 内。"""
from mcm_toolkit.models import fit_linear
x, y = linear_data
r = fit_linear(x.reshape(-1, 1), y)
assert np.isclose(r.params["coef"][0], 2.0, rtol=0.01)
assert np.isclose(r.params["intercept"], 1.0, atol=0.05)
def test_fit_polynomial_matches_numpy(linear_data):
from mcm_toolkit.models import fit_polynomial
x, y = linear_data
r = fit_polynomial(x.reshape(-1, 1), y, degree=2)
assert np.allclose(r.params["coef"], np.polyfit(x, y, 2), rtol=1e-6)
def test_metrics_known_values():
from mcm_toolkit.models import mae, r2_score, rmse
y_true = np.array([1.0, 2.0, 3.0])
y_pred = np.array([1.0, 2.0, 5.0])
assert np.isclose(rmse(y_true, y_pred), np.sqrt(4 / 3))
assert np.isclose(mae(y_true, y_pred), 2 / 3)
assert r2_score(y_true, y_pred) < 1.0
def test_r2_is_one_for_perfect_fit():
from mcm_toolkit.models import r2_score
y = np.array([1.0, 2.0, 3.0, 4.0])
assert np.isclose(r2_score(y, y), 1.0)
def test_compare_models_returns_table_sorted_by_first_metric(linear_data):
from mcm_toolkit.models import compare_models, fit_linear, fit_polynomial
x, y = linear_data
X = x.reshape(-1, 1)
tbl = compare_models(
{"linear": fit_linear(X, y), "poly2": fit_polynomial(X, y, degree=2)},
X,
y,
cv=0,
)
assert set(tbl.index) >= {"linear", "poly2"}
assert "rmse" in tbl.columns
+85
View File
@@ -0,0 +1,85 @@
"""solve 包测试。"""
from __future__ import annotations
import numpy as np
import pytest
pytestmark = pytest.mark.skip(reason="未实现:见 docs/03-roadmap.md L0/L1")
def test_monte_carlo_integral_converges_to_analytic_value():
"""∫₀¹ x² dx = 1/3。n=2e5 时误差应 < 0.01。"""
from mcm_toolkit.solve import monte_carlo_integral
r = monte_carlo_integral(lambda X: X[:, 0] ** 2, bounds=[(0, 1)], n=200_000, random_state=0)
assert abs(r.data - 1 / 3) < 0.01
def test_monte_carlo_probability_of_standard_normal():
"""P(Z > 0) = 0.5,n=5000 时误差应 < 0.05。"""
from mcm_toolkit.solve import monte_carlo_probability
r = monte_carlo_probability(
lambda z: z > 0,
lambda rng, **kw: rng.standard_normal(),
n=5000,
random_state=0,
)
assert abs(r.metrics["probability"] - 0.5) < 0.05
def test_monte_carlo_is_reproducible():
from mcm_toolkit.solve import monte_carlo_probability
args = (lambda z: z > 0, lambda rng, **kw: rng.standard_normal())
a = monte_carlo_probability(*args, n=1000, random_state=9)
b = monte_carlo_probability(*args, n=1000, random_state=9)
assert a.metrics["probability"] == b.metrics["probability"]
def test_markov_steady_state_two_state_chain():
"""P=[[0.9,0.1],[0.2,0.8]] 的平稳分布是 [2/3, 1/3]。"""
from mcm_toolkit.solve import markov_steady_state
r = markov_steady_state(np.array([[0.9, 0.1], [0.2, 0.8]]))
assert np.allclose(r.data, [2 / 3, 1 / 3], atol=1e-6)
def test_knapsack_dp_known_optimum():
from mcm_toolkit.solve import knapsack_dp
r = knapsack_dp(weights=[1, 3, 4, 5], values=[1, 4, 5, 7], capacity=7)
assert np.isclose(r.metrics["total_value"], 9.0)
assert np.isclose(r.metrics["total_weight"], 7.0)
def test_linear_program_basic():
from mcm_toolkit.solve import linear_program
r = linear_program(c=[1, 2], A_ub=[[1, 1]], b_ub=[4], bounds=[(0, None)] * 2)
assert r.success
assert np.isclose(r.metrics["objective"], 4.0)
def test_mm1_metrics_closed_form():
from mcm_toolkit.solve import mm1_metrics
r = mm1_metrics(arrival_rate=2, service_rate=3)
assert np.isclose(r.metrics["rho"], 2 / 3, rtol=1e-6)
assert np.isclose(r.metrics["L"], 2.0, rtol=1e-6)
def test_mm1_rejects_unstable_system():
"""λ >= μ 时系统不稳定,应 success=False 而不是返回 inf。"""
from mcm_toolkit.solve import mm1_metrics
assert mm1_metrics(arrival_rate=3, service_rate=3).success is False
def test_simulated_annealing_finds_quadratic_minimum():
from mcm_toolkit.solve import simulated_annealing
f = lambda v: (v[0] - 3.0) ** 2 + (v[1] + 1.0) ** 2 # noqa: E731
r = simulated_annealing(f, x0=[0.0, 0.0], n_iter=5000, random_state=0)
assert np.allclose(r.params["x"], [3.0, -1.0], atol=0.1)
+67
View File
@@ -0,0 +1,67 @@
"""stats 包测试。
原则(见 docs/02-architecture.md §5):**测已知解析解,不测实现细节**。
"""
from __future__ import annotations
import numpy as np
import pytest
pytestmark = pytest.mark.skip(reason="未实现:见 docs/03-roadmap.md L1")
def test_bootstrap_ci_matches_normal_theory_on_large_n(rng):
"""大样本下 bootstrap 百分位区间应逼近正态解析区间(±10% 容差)。"""
from mcm_toolkit.stats import bootstrap_ci
x = rng.normal(10, 2, size=5000)
r = bootstrap_ci(x, statistic=np.mean, confidence=0.95, n_resamples=5000, random_state=0)
se = x.std(ddof=1) / np.sqrt(x.size)
assert abs((r.params["high"] - r.params["low"]) - 2 * 1.96 * se) < 0.1 * 2 * 1.96 * se
def test_bootstrap_ci_works_for_arbitrary_statistic(rng):
"""bootstrap 的核心价值:任意统计量都能给区间(这里用中位数)。"""
from mcm_toolkit.stats import bootstrap_ci
x = rng.lognormal(0, 1, size=2000)
r = bootstrap_ci(x, statistic=np.median, random_state=0)
assert r.params["low"] < r.params["high"]
assert r.params["low"] < np.median(x) < r.params["high"]
def test_bootstrap_ci_is_reproducible(rng):
from mcm_toolkit.stats import bootstrap_ci
x = rng.normal(size=500)
a = bootstrap_ci(x, statistic=np.mean, random_state=123)
b = bootstrap_ci(x, statistic=np.mean, random_state=123)
assert a.params["low"] == b.params["low"]
def test_confidence_interval_matches_scipy(rng):
from scipy import stats
from mcm_toolkit.stats import confidence_interval
x = rng.normal(5, 1, size=50)
lo, hi = confidence_interval(x, confidence=0.95, method="t")
ref = stats.t.interval(0.95, len(x) - 1, loc=x.mean(), scale=stats.sem(x))
assert np.allclose([lo, hi], ref, rtol=1e-6)
def test_correlation_matrix_diagonal_is_one(rng):
from mcm_toolkit.stats import correlation_matrix
x = rng.normal(size=(200, 4))
c = correlation_matrix(x)
assert np.allclose(np.diag(np.asarray(c)), 1.0)
def test_describe_reports_expected_keys(rng):
from mcm_toolkit.stats import describe
table = describe(rng.normal(size=(100, 3)))
for key in ["mean", "std", "min", "max", "median"]:
assert key in table.index
+52
View File
@@ -0,0 +1,52 @@
"""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