Skip to content

Commit

Permalink
DevOps: Fix tutorial scripts and CI/CD
Browse files Browse the repository at this point in the history
Make the exercise/solution conversion script reversible.
Remove empty tutorial code cells.
  • Loading branch information
jngrad committed Oct 4, 2024
1 parent 03033c4 commit 5fb1106
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 73 deletions.
2 changes: 1 addition & 1 deletion doc/tutorials/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ physical systems.
Measuring the excess chemical potential of a salt solution using the Widom particle insertion method.
[Guide](widom_insertion/widom_insertion.ipynb)
* **Grand-Canonical Monte Carlo**
Simulating a polyelectrolyte solution coupled to a reservoir of salt.
Simulating a polyelectrolyte solution coupled to a reservoir of salt.
[Guide](grand_canonical_monte_carlo/grand_canonical_monte_carlo.ipynb)

[comment]: # (End of tutorials landing page)
Expand Down
18 changes: 13 additions & 5 deletions doc/tutorials/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def add_cell_from_script(nb, filepath):


def remove_empty_cells(nb):
for i in range(len(nb['cells']) - 1, 0, -1):
for i in range(len(nb["cells"]))[::-1]:
cell = nb['cells'][i]
if cell['source'].strip() == '':
nb['cells'].pop(i)
Expand All @@ -90,11 +90,19 @@ def parse_solution_cell(cell):


def convert_exercise2_to_code(nb):
for i in range(len(nb["cells"]) - 1, 0, -1):
for i in range(len(nb["cells"]))[::-1]:
cell = nb["cells"][i]
solution = parse_solution_cell(cell)
if solution is not None:
cell["source"] = solution
if cell["cell_type"] != "markdown":
continue
source = cell["source"]
if source.startswith("<details") and source.endswith("</details>"):
m = re.search("```python\n(.+)\n```", source, flags=re.DOTALL)
solution = "# SOLUTION CELL\n" + m.group(1)
nb["cells"][i] = nbformat.v4.new_code_cell(source=solution)
if (i + 1) != len(nb["cells"]) and \
nb["cells"][i + 1]["cell_type"] == "code" and \
not nb["cells"][i + 1]["source"]:
del nb["cells"][i + 1]


def disable_plot_interactivity(nb):
Expand Down
64 changes: 0 additions & 64 deletions doc/tutorials/lennard_jones/lennard_jones.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,6 @@
"system = espressomd.System(box_l=BOX_L)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "13275327",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -356,14 +348,6 @@
"particles = system.part.add(type=[0] * N_PART, pos=np.random.random((N_PART, 3)) * system.box_l)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "655d49e6",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -498,14 +482,6 @@
" epsilon=LJ_EPS, sigma=LJ_SIG, cutoff=LJ_CUT, shift=0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "91ac5e57",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "5e6332fb",
Expand Down Expand Up @@ -717,14 +693,6 @@
"T_inst = 2. / 3. * e_kin / N_PART"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c53290b6",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -843,14 +811,6 @@
"steps_per_subsample = int(np.ceil(3 * corr_time / system.time_step))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "94fc76b6",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -923,14 +883,6 @@
"SEM_pot_energy = np.std(pot_energies) / np.sqrt(len(pot_energies))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3e4a203c",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -1034,14 +986,6 @@
"system.auto_update_accumulators.add(rdf_acc)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8de0795a",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "51edfcf1",
Expand Down Expand Up @@ -1082,14 +1026,6 @@
"rs = rdf_obs.bin_centers()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "18a10a12",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
22 changes: 19 additions & 3 deletions testsuite/scripts/tutorials/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def test_cells_prepare_for_html(self):
self.assertEqual(cell['source'], 'Question 1')
cell = next(cells)
self.assertEqual(cell['cell_type'], 'code')
self.assertEqual(cell['source'], '1')
self.assertEqual(cell['source'], '# SOLUTION CELL\n1')
cell = next(cells)
self.assertEqual(cell['cell_type'], 'markdown')
self.assertEqual(cell['source'], '1b')
Expand All @@ -199,7 +199,7 @@ def test_cells_prepare_for_html(self):
self.assertEqual(cell['cell_type'], 'code')
self.assertEqual(
cell['source'],
'2\nimport matplotlib.pyplot\nglobal_var = 20')
'# SOLUTION CELL\n2\nimport matplotlib.pyplot\nglobal_var = 20')
cell = next(cells)
self.assertEqual(cell['cell_type'], 'code')
self.assertEqual(cell['source'], '3')
Expand Down Expand Up @@ -231,7 +231,7 @@ def test_cells_conversion(self):
self.assertEqual(cell['source'], 'Question 1')
cell = next(cells)
self.assertEqual(cell['cell_type'], 'code')
self.assertEqual(cell['source'], '1')
self.assertEqual(cell['source'], '# SOLUTION CELL\n1')
self.assertEqual(next(cells, 'EOF'), 'EOF')

with open(f_input, 'w', encoding='utf-8') as f:
Expand All @@ -255,6 +255,22 @@ def test_cells_conversion(self):
self.assertEqual(cell['source'], '')
self.assertEqual(next(cells, 'EOF'), 'EOF')

# check operation is reversible
cmd = ['cells', '--to-py', str(f_input)]
self.run_command(cmd, f_input)
# read processed notebook
with open(f_input, encoding='utf-8') as f:
nb_output = nbformat.read(f, as_version=4)
# check cells
cells = iter(nb_output['cells'])
cell = next(cells)
self.assertEqual(cell['cell_type'], 'markdown')
self.assertEqual(cell['source'], 'Question 1')
cell = next(cells)
self.assertEqual(cell['cell_type'], 'code')
self.assertEqual(cell['source'], '# SOLUTION CELL\n1')
self.assertEqual(next(cells, 'EOF'), 'EOF')

@skipIfMissingModules
def test_cells_autopep8(self):
root = pathlib.Path("@CMAKE_CURRENT_BINARY_DIR@")
Expand Down

0 comments on commit 5fb1106

Please sign in to comment.