加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
run_notebooks.py 1.41 KB
一键复制 编辑 原始数据 按行查看 历史
Alex 提交于 2024-06-03 11:13 . add run_notebooks.py
import glob
import subprocess
import sys
import nbformat
from nbconvert.preprocessors import ExecutePreprocessor
def is_grid_search(notebook_path):
with open(notebook_path) as f:
nb = nbformat.read(f, as_version=4)
for cell in nb.cells:
if cell.cell_type == 'code' and 'perform_grid_search' in cell.source:
return True
return False
def run_notebook(notebook_path):
try:
with open(notebook_path) as f:
nb = nbformat.read(f, as_version=4)
ep = ExecutePreprocessor(timeout=600, kernel_name='python3')
ep.preprocess(nb, {'metadata': {'path': './'}})
return True
except Exception as e:
print(f"Error running {notebook_path}: {e}")
return False
def main():
notebooks = glob.glob('**/*.ipynb', recursive=True)
failing_notebooks = []
for notebook in notebooks:
if not is_grid_search(notebook):
print(f"Running {notebook}...")
if not run_notebook(notebook):
failing_notebooks.append(notebook)
else:
# TODO remove
print(f"Skipping {notebook} (grid search notebook)...")
if failing_notebooks:
print("Failing notebooks:")
for notebook in failing_notebooks:
print(notebook)
sys.exit(1)
else:
print("All notebooks ran successfully.")
sys.exit(0)
if __name__ == "__main__":
main()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化