|
1 | | -# Python 项目模板 |
| 1 | +# Wolf-Sheep Model Comparison |
2 | 2 |
|
3 | | -一个现代化的 Python 项目模板,集成了最佳实践和完整的开发工具链。 |
| 3 | +[中文 README](README_CN.md) |
4 | 4 |
|
5 | | -## 快速开始 |
| 5 | +This repository compares the classic Wolf-Sheep predator–prey model across three implementations—ABSESpy, a stripped Mesa version, and a pure Python baseline—highlighting runtime performance and code size. |
6 | 6 |
|
7 | | -### 1. 安装 uv(推荐)或 poetry |
| 7 | +## Project Layout |
8 | 8 |
|
9 | | -```bash |
10 | | -# 安装 uv(推荐,更快) |
11 | | -curl -LsSf https://astral.sh/uv/install.sh | sh |
12 | | - |
13 | | -# 或安装 poetry |
14 | | -pip install poetry |
15 | | -``` |
16 | | - |
17 | | -### 2. 创建新项目 |
18 | | - |
19 | | -```bash |
20 | | -# 克隆此模板 |
21 | | -git clone https://github.com/SongshGeo/project_template.git my-project |
22 | | -cd my-project |
23 | | - |
24 | | -# 初始化项目(配置项目名称和描述) |
25 | | -make setup |
26 | | - |
27 | | -# 手动配置项目(可选,如果 make setup 没有运行) |
28 | | -# 使用 Makefile(推荐,会自动选择包管理器) |
29 | | -make configure-project |
30 | | - |
31 | | -# 或直接运行(需要先安装依赖) |
32 | | -python scripts/configure_project.py |
33 | | -``` |
34 | | - |
35 | | -**配置脚本功能:** |
36 | | -- 更新 `pyproject.toml` 中的 `[project]` 和 `[tool.poetry]` 段 |
37 | | -- 更新 GitHub workflow 配置 |
38 | | -- 创建/更新 `README.md` |
39 | | -- 清空 `CHANGELOG.md` |
40 | | - |
41 | | -**脚本会提示您输入:** |
42 | | -- **项目名称**:将在包名和配置中使用 |
43 | | -- **项目描述**:项目的简要描述 |
44 | | - |
45 | | -### 手动安装依赖(可选) |
46 | | - |
47 | | -如果只想安装依赖而不配置项目: |
48 | | - |
49 | | -```bash |
50 | | -uv sync --all-extras # 使用 uv |
51 | | -# 或 |
52 | | -poetry install # 使用 poetry |
53 | | -``` |
54 | | - |
55 | | -### 3. 开发 |
56 | | - |
57 | | -```bash |
58 | | -# 运行测试 |
59 | | -make test |
60 | | - |
61 | | -# 查看测试报告 |
62 | | -make report |
63 | | - |
64 | | -# 运行 pre-commit 检查 |
65 | | -pre-commit run --all-files |
66 | | -``` |
67 | | - |
68 | | -## 项目结构 |
69 | | - |
70 | | -```shell |
71 | | -. |
72 | | -├── src/ # 源代码目录 |
73 | | -│ ├── api/ # API 相关 |
74 | | -│ ├── core/ # 核心功能 |
75 | | -│ └── __init__.py |
76 | | -├── tests/ # 测试目录 |
77 | | -│ ├── conftest.py # pytest 配置 |
78 | | -│ └── helper.py # 测试辅助函数 |
79 | | -├── config/ # 配置文件目录 |
80 | | -│ └── config.yaml # 主配置文件 |
81 | | -├── data/ # 数据目录 |
82 | | -├── docs/ # 文档目录 |
83 | | -├── examples/ # 示例代码 |
84 | | -├── scripts/ # 工具脚本 |
85 | | -│ └── configure_project.py |
86 | | -├── pyproject.toml # 项目配置(uv/poetry/pyproject) |
87 | | -├── tox.ini # 多版本 Python 测试配置 |
88 | | -├── makefile # Make 命令快捷方式 |
89 | | -└── README.md # 本文件 |
90 | | -``` |
91 | | - |
92 | | -## 特性 |
93 | | - |
94 | | -1. 使用 `Makefile` 进行批量操作 |
95 | | -2. 使用 `Hydra` 管理模型参数与配置 |
96 | | -3. 使用 `pytest` 进行单元测试 |
97 | | -4. 使用 `allure` 生成测试报告 |
98 | | -5. 使用 `nbstripout` 管理 Jupyter Notebook 输出(保留 notebook 输出) |
99 | | -6. 使用 `pre-commit` 进行代码检查 |
100 | | -7. 使用 `mkdocs` 生成文档 |
101 | | -8. 使用 `uv` 进行包管理(兼容 `poetry`) |
102 | | -9. 使用 `interrogate` 检查文档覆盖率 |
103 | | -10. 使用 `jupyter` 进行数据分析 |
104 | | -11. 使用 `snakeviz` 进行性能分析 |
105 | | -12. 使用 `isort` 进行代码格式化 |
106 | | -13. 使用 `flake8` 进行代码检查 |
107 | | -14. 使用 `ruff` 进行文档检查 |
108 | | -15. 使用 `black` 进行代码格式化 |
109 | | -16. 使用 `mypy` 进行类型检查 |
110 | | -17. 使用 `coverage` 进行测试覆盖率分析 |
111 | | -18. 使用 `tox` 进行多 Python 版本(3.10-3.13)兼容性测试 |
112 | | -19. 使用 `release-please` 进行版本管理 |
113 | | -20. 使用 `mkdocs-material` 生成美观的文档 |
114 | | - |
115 | | -## 常见命令 |
116 | | - |
117 | | -### 开发工作流 |
118 | | - |
119 | | -```bash |
120 | | -# 安装所有依赖(自动检测并使用 uv 或 poetry) |
121 | | -make setup |
122 | | - |
123 | | -# 运行测试(自动适配包管理器) |
124 | | -make test |
125 | | - |
126 | | -# 运行多版本测试(Python 3.10-3.13) |
127 | | -make tox |
128 | | - |
129 | | -# 生成测试报告 |
130 | | -make report |
131 | | - |
132 | | -# 配置项目(修改项目名称、描述等) |
133 | | -make configure-project |
134 | | - |
135 | | -# 查看文档 |
136 | | -make docs |
137 | 9 | ``` |
138 | | - |
139 | | -**注意:** Makefile 会自动检测系统中安装的包管理器(uv 或 poetry),优先使用 uv。如果两者都未安装,会提示错误并告知安装方法。 |
140 | | - |
141 | | -### 使用 uv(推荐) |
142 | | - |
143 | | -```bash |
144 | | -# 安装依赖 |
145 | | -uv sync --all-extras |
146 | | - |
147 | | -# 运行测试 |
148 | | -uv run pytest |
149 | | - |
150 | | -# 运行任意 Python 命令 |
151 | | -uv run python your_script.py |
152 | | - |
153 | | -# 添加新依赖 |
154 | | -uv add package-name |
155 | | - |
156 | | -# 添加开发依赖 |
157 | | -uv add --dev package-name |
| 10 | +wolf_sheep/ |
| 11 | +├── absespy/ # ABSESpy implementation & docs |
| 12 | +├── pure_python/ # Pure Python implementation & docs |
| 13 | +├── mesa_impl/ # Mesa-inspired lightweight implementation |
| 14 | +├── benchmark.py # Comparison script |
| 15 | +└── __init__.py |
| 16 | +tests/ |
| 17 | +└── test_pure_python_model.py |
158 | 18 | ``` |
159 | 19 |
|
160 | | -### 使用 poetry(备选) |
| 20 | +## Implementations |
161 | 21 |
|
162 | | -```bash |
163 | | -# 安装依赖 |
164 | | -poetry install |
| 22 | +- `wolf_sheep/absespy`: demonstrates ABSESpy’s scheduling, batch operations, and data collection. |
| 23 | +- `wolf_sheep/pure_python`: uses only the standard library—grid, scheduling, and metrics are handwritten. |
| 24 | +- `wolf_sheep/mesa_impl`: concise reimplementation based on Mesa’s example (comments/docstrings removed for line-count parity). |
165 | 25 |
|
166 | | -# 运行测试 |
167 | | -poetry run pytest |
| 26 | +Each implementation exposes the same metrics (`n_sheep`, `n_wolves`, `population_ratio`, `grass_coverage`) so benchmarks align. |
168 | 27 |
|
169 | | -# 添加新依赖 |
170 | | -poetry add package-name |
171 | | -``` |
172 | | - |
173 | | -### 代码质量 |
| 28 | +## Setup |
174 | 29 |
|
175 | 30 | ```bash |
176 | | -# 安装 pre-commit hooks |
177 | | -pre-commit install |
178 | | - |
179 | | -# 手动运行所有检查 |
180 | | -pre-commit run --all-files |
181 | | - |
182 | | -# 运行特定检查 |
183 | | -pre-commit run flake8 --all-files |
184 | | -pre-commit run black --all-files |
185 | | -pre-commit run interrogate --all-files # 检查文档覆盖率 |
| 31 | +uv sync --all-extras && uv run pytest |
186 | 32 | ``` |
187 | 33 |
|
188 | | -### 多 Python 版本测试 |
189 | | - |
190 | | -```bash |
191 | | -# 测试所有 Python 版本(使用 Makefile) |
192 | | -make tox |
193 | | - |
194 | | -# 测试特定版本 |
195 | | -make tox-e pyversion=py311 |
196 | | - |
197 | | -# 查看可用环境 |
198 | | -make tox-list |
199 | | - |
200 | | -# 直接使用 tox 命令 |
201 | | -tox # 测试所有版本 |
202 | | -tox -e py311 # 测试 Python 3.11 |
203 | | -tox list # 查看环境列表 |
204 | | -tox -p # 并行运行 |
205 | | -``` |
| 34 | +Optional extras: |
206 | 35 |
|
207 | | -## 文档 |
| 36 | +- ABSESpy: `uv add abses matplotlib` |
| 37 | +- Mesa: `uv add mesa` |
208 | 38 |
|
209 | | -!!! info "在线文档" |
210 | | - 访问 [在线文档网站](https://songshgeo.github.io/project_template/) 查看完整的文档和教程。 |
211 | | - |
212 | | -### 本地查看文档 |
| 39 | +## Run Individually |
213 | 40 |
|
214 | 41 | ```bash |
215 | | -# 启动文档服务器(开发模式,支持热重载) |
216 | | -make docs |
217 | | - |
218 | | -# 或手动运行 |
219 | | -uv run mkdocs serve |
220 | | -poetry run mkdocs serve # 使用 poetry |
| 42 | +uv run python - <<'PY' |
| 43 | +from wolf_sheep.pure_python import ModelParams, Simulation |
| 44 | +history = Simulation(ModelParams(seed=42, max_steps=200)).run() |
| 45 | +print(history[-1]) |
| 46 | +PY |
221 | 47 | ``` |
222 | 48 |
|
223 | | -访问 `http://127.0.0.1:8000` 查看文档。 |
224 | | - |
225 | | -### 构建文档 |
226 | | - |
227 | 49 | ```bash |
228 | | -# 构建静态文档站点 |
229 | | -make docs-build |
230 | | - |
231 | | -# 或手动运行 |
232 | | -uv run mkdocs build |
| 50 | +uv run python - <<'PY' |
| 51 | +from wolf_sheep.absespy import WolfSheepModel |
| 52 | +cfg = { |
| 53 | + "model": {"shape": [30, 30], "n_sheep": 50, "n_wolves": 10, "rep_rate": 0.01}, |
| 54 | + "time": {"end": 200}, |
| 55 | +} |
| 56 | +model = WolfSheepModel(parameters=cfg, seed=42) |
| 57 | +model.run_model(steps=200) |
| 58 | +print(model.n_sheep, model.n_wolves, model.population_ratio, model.grass_coverage) |
| 59 | +PY |
233 | 60 | ``` |
234 | 61 |
|
235 | | -### 部署文档到 GitHub Pages |
236 | | - |
237 | | -文档通过 GitHub Actions 自动部署: |
238 | | - |
239 | | -1. 推送代码到 `main` 分支 |
240 | | -2. GitHub Actions 自动触发构建 |
241 | | -3. 文档部署到 GitHub Pages |
242 | | - |
243 | | -**访问地址:** `https://songshgeo.github.io/project_template/` |
244 | | - |
245 | | -### 文档章节 |
246 | | - |
247 | | -- 📖 [快速开始指南](docs/doc/quick-start.md) - 从零开始的详细教程 |
248 | | -- 🔧 [工具链说明](docs/doc/tools.md) - 各工具的使用说明和最佳实践 |
249 | | -- ⚙️ [配置说明](docs/doc/configuration.md) - 项目配置文件详解 |
250 | | -- 📝 [开发规范](docs/doc/development.md) - 代码规范和最佳实践 |
251 | | -- 🚀 [部署指南](docs/doc/deployment.md) - 项目部署和发布流程 |
252 | | - |
253 | | -## 常见问题 |
254 | | - |
255 | | -### Q: 如何选择 uv 还是 poetry? |
256 | | - |
257 | | -**A:** uv 更快、更现代,推荐使用。Poetry 更成熟,可根据项目需求选择。 |
258 | | - |
259 | | -### Q: 如何开始一个新项目? |
260 | | - |
261 | | -**A:** 运行 `make setup` 即可完成配置和依赖安装。 |
262 | | - |
263 | | -### Q: 如何添加新依赖? |
264 | | - |
265 | | -**A:** |
266 | 62 | ```bash |
267 | | -uv add package-name # 运行时依赖 |
268 | | -uv add --dev package-name # 开发依赖 |
| 63 | +uv run python - <<'PY' |
| 64 | +from wolf_sheep import mesa_run_steps |
| 65 | +print(mesa_run_steps(steps=200, seed=42).tail(1)) |
| 66 | +PY |
269 | 67 | ``` |
270 | 68 |
|
271 | | -### Q: 如何运行测试? |
| 69 | +## Benchmark Script |
272 | 70 |
|
273 | | -**A:** |
274 | 71 | ```bash |
275 | | -make test # 使用 Make |
276 | | -uv run pytest # 直接运行 |
| 72 | +uv run python wolf_sheep/benchmark.py --steps 200 --repeats 3 |
277 | 73 | ``` |
278 | 74 |
|
279 | | -## 贡献指南 |
280 | | - |
281 | | -欢迎贡献代码!请遵循以下步骤: |
| 75 | +- `Avg Time (s)`: mean runtime across repeats. |
| 76 | +- `LOC raw/log`: raw lines vs. logical lines (comments & docstrings removed). |
282 | 77 |
|
283 | | -1. Fork 本仓库 |
284 | | -2. 创建特性分支 (`git checkout -b feature/amazing-feature`) |
285 | | -3. 提交更改 (`git commit -m 'feat: Add amazing feature'`) |
286 | | -4. 推送到分支 (`git push origin feature/amazing-feature`) |
287 | | -5. 创建 Pull Request |
| 78 | +Example output: |
288 | 79 |
|
289 | | -请确保: |
290 | | -- 代码通过所有 linter 检查 |
291 | | -- 添加适当的测试 |
292 | | -- 更新相关文档 |
293 | | -- 遵循代码规范 |
| 80 | +| Framework | Avg Time (s) | LOC raw/log | |
| 81 | +|-------------|--------------|-------------| |
| 82 | +| pure_python | 0.0437 | 341 / 304 | |
| 83 | +| mesa | 0.0441 | 200 / 200 | |
| 84 | +| absespy | 2.4238 | 237 / 99 | |
294 | 85 |
|
295 | | -## 许可证 |
| 86 | +## Metrics |
296 | 87 |
|
297 | | -本项目使用 MIT 许可证。详见 [LICENSE](LICENSE) 文件。 |
| 88 | +- `n_sheep`, `n_wolves`: population counts. |
| 89 | +- `population_ratio`: sheep share of total population. |
| 90 | +- `grass_coverage`: proportion of cells with regrown grass. |
298 | 91 |
|
299 | | -## 作者 |
| 92 | +## License |
300 | 93 |
|
301 | | -- **SongshGeo** - [GitHub](https://github.com/SongshGeo) - [Website](https://cv.songshgeo.com/) |
| 94 | +MIT License — see `LICENSE`. |
0 commit comments