Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 41 additions & 57 deletions how-to/1_Read_and_visualise/NikonDataReader.ipynb

Large diffs are not rendered by default.

51 changes: 27 additions & 24 deletions how-to/1_Read_and_visualise/ZeissDataReader.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 24,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -43,7 +43,7 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand All @@ -68,14 +68,17 @@
"True"
]
},
"execution_count": 25,
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from cil.utilities import dataexample\n",
"dataexample.WALNUT.download_data(data_dir='.', prompt=False)"
"from pathlib import Path\n",
"\n",
"data_dir = Path.home() / \".cache\" / \"cil\" # choose a location to save the dataset\n",
"dataexample.WALNUT.download_data(data_dir=str(data_dir), prompt=False)"
]
},
{
Expand All @@ -89,12 +92,12 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from cil.io import ZEISSDataReader\n",
"file_name = \"./walnut/valnut/valnut_2014-03-21_643_28/tomo-A/valnut_tomo-A.txrm\"\n",
"file_name = Path(data_dir) / \"./walnut/valnut/valnut_2014-03-21_643_28/tomo-A/valnut_tomo-A.txrm\"\n",
"data_reader = ZEISSDataReader(file_name=file_name)\n",
"data = data_reader.read()"
]
Expand All @@ -108,7 +111,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand All @@ -124,10 +127,10 @@
{
"data": {
"text/plain": [
"<cil.utilities.display.show_geometry at 0x7f1850cf77d0>"
"<cil.utilities.display.show_geometry at 0x7f86d9a6f2b0>"
]
},
"execution_count": 27,
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -146,7 +149,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand All @@ -162,10 +165,10 @@
{
"data": {
"text/plain": [
"<cil.utilities.display.show2D at 0x7f185169d700>"
"<cil.utilities.display.show2D at 0x7f86d8431ae0>"
]
},
"execution_count": 28,
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -186,7 +189,7 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand All @@ -202,10 +205,10 @@
{
"data": {
"text/plain": [
"<cil.utilities.display.show2D at 0x7f1850c85ee0>"
"<cil.utilities.display.show2D at 0x7f86d9a6eda0>"
]
},
"execution_count": 29,
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -226,7 +229,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand All @@ -242,10 +245,10 @@
{
"data": {
"text/plain": [
"<cil.utilities.display.show2D at 0x7f18458d7fb0>"
"<cil.utilities.display.show2D at 0x7f86d39331c0>"
]
},
"execution_count": 30,
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -266,7 +269,7 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -276,7 +279,7 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand All @@ -292,10 +295,10 @@
{
"data": {
"text/plain": [
"<cil.utilities.display.show_geometry at 0x7f1850d0aba0>"
"<cil.utilities.display.show_geometry at 0x7f86d34171f0>"
]
},
"execution_count": 32,
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -307,7 +310,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "cil_demos",
"display_name": "cil_dev",
"language": "python",
"name": "python3"
},
Expand All @@ -321,7 +324,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
"version": "3.10.14"
}
},
"nbformat": 4,
Expand Down
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
addopts = --nbmake --nbmake-kernel=cil_demos --nbmake-timeout=900
64 changes: 64 additions & 0 deletions test_notebooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import pytest
import nbformat
import os
import re
import shutil

folders = ['demos', 'how-to']
skip_notebooks = ['demos/2_Iterative/04_SPDHG.ipynb'
]

def run_tmp_notebook(notebook_path):
tmp_notebook_path = notebook_path.replace('.ipynb', '_tmp.ipynb')
shutil.copy(notebook_path, tmp_notebook_path)

with open(tmp_notebook_path, 'r') as f:
notebook = nbformat.read(f, as_version=4)

for cell in notebook.cells:
if cell.cell_type == 'code':
# Update filepath
if '/mnt/materials/' in cell.source:
cell.source = re.sub(r'/mnt/materials/', '/mnt/share/materials/', cell.source)

# Replace commented %load lines with the correct snippet
if '# %load' in cell.source:
snippet_file = cell.source.split('snippets/')[1].split('\'')[0]
snippet_path = os.path.join(os.path.dirname(notebook_path), 'snippets', snippet_file)
if os.path.exists(snippet_path):
with open(snippet_path, 'r') as snippet_file:
snippet_code = snippet_file.read()
cell.source = cell.source.replace('# %load', '')
cell.source = snippet_code
else:
print(f"Warning: Snippet path {snippet_path} does not exist.")

# Clear cells containing ellipses
if '...' in cell.source:
cell.source = ""

with open(tmp_notebook_path, 'w') as f:
nbformat.write(notebook, f)

print(f"\t\t Testing notebook: {tmp_notebook_path}")
pytest.main([tmp_notebook_path])

os.remove(tmp_notebook_path)

def test_notebook_runs():
for folder in folders:
print(f"Searching in folder: {folder}")
for root, dirs, files in os.walk(folder):
print(f"\tIn directory: {root}")
for file in files:
if not file.endswith('.ipynb'):
continue
if file.endswith('_tmp.ipynb'):
continue

notebook_path = os.path.join(root, file)
if notebook_path in skip_notebooks:
print(f"\t\t Skipping notebook: {notebook_path}")
else:
run_tmp_notebook(notebook_path)