Skip to content

Commit

Permalink
Rename + add test.
Browse files Browse the repository at this point in the history
  • Loading branch information
fdmalone committed Jul 3, 2024
1 parent 3930806 commit c3620e1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 30 deletions.
69 changes: 39 additions & 30 deletions dev_tools/check_notebooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import shutil
import time
from multiprocessing import Pool
from subprocess import run as _run, CalledProcessError
from subprocess import CalledProcessError
from subprocess import run as _run

BASE_PACKAGES = [
# for running the notebooks
Expand All @@ -24,24 +25,28 @@ def _check_notebook(notebook_fn: str, notebook_id: str, stdout, stderr):
stdout: A file-like object to redirect stdout
stderr: A file-like object to redirect stderr
"""
print(f'Starting {notebook_id}')
print(f"Starting {notebook_id}")

def run(*args, **kwargs):
return _run(*args, check=True, stdout=stdout, stderr=stderr, **kwargs)

# 1. create venv
venv_dir = os.path.abspath(f'./notebook_envs/{notebook_id}')
run(['python', '-m', 'venv', '--clear', venv_dir])
venv_dir = os.path.abspath(f"./notebook_envs/{notebook_id}")
run(["python", "-m", "venv", "--clear", venv_dir])

# 2. basic colab-like environment
pip = f'{venv_dir}/bin/pip'
run([pip, 'install'] + BASE_PACKAGES)
pip = f"{venv_dir}/bin/pip"
run([pip, "install"] + BASE_PACKAGES)

# 3. execute
jupyter = f'{venv_dir}/bin/jupyter'
jupyter = f"{venv_dir}/bin/jupyter"
env = os.environ.copy()
env['PATH'] = f'{venv_dir}/bin:{env["PATH"]}'
run([jupyter, 'nbconvert', '--to', 'html', '--execute', notebook_fn], cwd='../', env=env)
env["PATH"] = f'{venv_dir}/bin:{env["PATH"]}'
run(
[jupyter, "nbconvert", "--to", "html", "--execute", notebook_fn],
cwd="../",
env=env,
)

# 4. clean up
shutil.rmtree(venv_dir)
Expand All @@ -64,43 +69,47 @@ def check_notebook(notebook_fn: str):
Args:
notebook_fn: The filename of the notebook relative to the repo root.
"""
notebook_id = notebook_fn.replace('/', '-')
notebook_id = notebook_fn.replace("/", "-")
start = time.perf_counter()
with open(f'./notebook_envs/{notebook_id}.stdout', 'w') as stdout, \
open(f'./notebook_envs/{notebook_id}.stderr', 'w') as stderr:
with open(f"./notebook_envs/{notebook_id}.stdout", "w") as stdout, open(
f"./notebook_envs/{notebook_id}.stderr", "w"
) as stderr:
try:
_check_notebook(notebook_fn, notebook_id, stdout, stderr)
except CalledProcessError:
print('ERROR!', notebook_id)
print("ERROR!", notebook_id)
end = time.perf_counter()
print(f'{notebook_id} {end - start:.1f}s')
print(f"{notebook_id} {end - start:.1f}s")


NOTEBOOKS = [
'docs/otoc/otoc_example.ipynb',
'docs/guide/data_analysis.ipynb',
'docs/guide/data_collection.ipynb',
'docs/qaoa/example_problems.ipynb',
'docs/qaoa/precomputed_analysis.ipynb',
'docs/qaoa/hardware_grid_circuits.ipynb',
'docs/qaoa/optimization_analysis.ipynb',
'docs/qaoa/tasks.ipynb',
'docs/qaoa/landscape_analysis.ipynb',
'docs/qaoa/routing_with_tket.ipynb',
'docs/hfvqe/molecular_data.ipynb',
'docs/hfvqe/quickstart.ipynb',
'docs/fermi_hubbard/publication_results.ipynb',
'docs/fermi_hubbard/experiment_example.ipynb',
"docs/otoc/otoc_example.ipynb",
"docs/guide/data_analysis.ipynb",
"docs/guide/data_collection.ipynb",
"docs/qaoa/example_problems.ipynb",
"docs/qaoa/precomputed_analysis.ipynb",
"docs/qaoa/hardware_grid_circuits.ipynb",
"docs/qaoa/optimization_analysis.ipynb",
"docs/qaoa/tasks.ipynb",
"docs/qaoa/landscape_analysis.ipynb",
"docs/qaoa/routing_with_tket.ipynb",
"docs/hfvqe/molecular_data.ipynb",
"docs/hfvqe/quickstart.ipynb",
"docs/fermi_hubbard/publication_results.ipynb",
"docs/fermi_hubbard/experiment_example.ipynb",
"docs/qcqmc/experimental_wavefunctions.ipynb"
"docs/qcqmc/full_workflow.ipynb"
"docs/qcqmc/high_level.ipynb",
]


def main():
os.chdir(os.path.dirname(__file__))
os.makedirs('./notebook_envs', exist_ok=True)
os.makedirs("./notebook_envs", exist_ok=True)
with Pool(4) as pool:
results = pool.map(check_notebook, NOTEBOOKS)
print(results)


if __name__ == '__main__':
if __name__ == "__main__":
main()
File renamed without changes.

0 comments on commit c3620e1

Please sign in to comment.