Skip to content

Fix tutorial jupyter issue #651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: messaging_refactor_release2nd
Choose a base branch
from
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
57 changes: 38 additions & 19 deletions tests/lava/tutorials/test_tutorials.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class TestTutorials(unittest.TestCase):

def _execute_notebook(
self, base_dir: str, path: str
) -> int:
) -> ty.Tuple[ty.Type[nbformat.NotebookNode], ty.List[str]]:
"""Execute a notebook via nbconvert and collect output.

Parameters
Expand All @@ -37,22 +37,23 @@ def _execute_notebook(

Returns
-------
int
(return code)
Tuple
(parsed nbformat.NotebookNode object, list of execution errors)
"""

cwd = os.getcwd()
dir_name, notebook = os.path.split(path)
try:
env = self._update_pythonpath(base_dir, dir_name)
result = self._convert_and_execute_notebook(notebook, env)
errors = self._collect_errors_from_all_cells(result)
nb = self._convert_and_execute_notebook(notebook, env)
errors = self._collect_errors_from_all_cells(nb)
except Exception as e:
errors = -1
nb = None
errors = str(e)
finally:
os.chdir(cwd)

return errors
return nb, errors

def _update_pythonpath(
self, base_dir: str, dir_name: str
Expand Down Expand Up @@ -90,7 +91,7 @@ def _update_pythonpath(

def _convert_and_execute_notebook(
self, notebook: str, env: ty.Dict[str, str]
):
) -> ty.Type[nbformat.NotebookNode]:
"""Covert notebook and execute it.

Parameters
Expand All @@ -105,23 +106,25 @@ def _convert_and_execute_notebook(
nb : nbformat.NotebookNode
Notebook dict-like node with attribute-access
"""
with tempfile.NamedTemporaryFile(mode="w+t", suffix=".py") as fout:
with tempfile.NamedTemporaryFile(mode="w+t", suffix=".ipynb") as fout:
args = [
"jupyter",
"nbconvert",
"--to",
"python",
"notebook",
"--execute",
"--ExecutePreprocessor.timeout=-1",
"--output",
fout.name[0:-3],
fout.name,
notebook,
]
subprocess.check_call(args, env=env) # noqa: S603

fout.seek(0)
return subprocess.run(["ipython", "-c", fout.read()], # noqa # nosec
env=env) # noqa # nosec
return nbformat.read(fout, nbformat.current_nbformat)

def _collect_errors_from_all_cells(
self, result
self, nb: nbformat.NotebookNode
) -> ty.List[str]:
"""Collect errors from executed notebook.

Expand All @@ -135,9 +138,13 @@ def _collect_errors_from_all_cells(
List
Collection of errors
"""
if result.returncode != 0:
result.check_returncode()
return result.returncode
errors = []
for cell in nb.cells:
if "outputs" in cell:
for output in cell["outputs"]:
if output.output_type == "error":
errors.append(output)
return errors

def _run_notebook(self, notebook: str, e2e_tutorial: bool = False):
"""Run a specific notebook
Expand Down Expand Up @@ -176,10 +183,22 @@ def _run_notebook(self, notebook: str, e2e_tutorial: bool = False):

# If the notebook is found execute it and store any errors
for notebook_name in discovered_notebooks:
errors = self._execute_notebook(
nb, errors = self._execute_notebook(
str(tutorials_directory), notebook_name
)
self.assertEqual(errors, 0)
errors_joined = (
"\n".join(errors) if isinstance(errors, list) else errors
)
if errors:
errors_record[notebook_name] = (errors_joined, nb)

self.assertFalse(
errors_record,
"Failed to execute Jupyter Notebooks \
with errors: \n {}".format(
errors_record
),
)
finally:
os.chdir(cwd)

Expand Down
38 changes: 11 additions & 27 deletions tutorials/in_depth/tutorial04_execution.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"pycharm": {
Expand All @@ -302,7 +303,11 @@
"source": [
"3. Compile stage: After a _Process_ has been configured, it needs to be compiled to\n",
"become executable. After the compilation stage, the state of the _Process_ can\n",
"still be manipulated and inspected."
"still be manipulated and inspected.\n",
"\n",
"4. Execution stage: When compilation is complete, _Processes_ can be\n",
"executed. The execution stage ensures that the (prior) compilation stage has\n",
"been completed and otherwise invokes it."
]
},
{
Expand All @@ -315,39 +320,18 @@
},
"outputs": [],
"source": [
"# Compile stage\n",
"from lava.magma.compiler.compiler import Compiler\n",
"from lava.magma.core.run_configs import Loihi1SimCfg\n",
"\n",
"# create a compiler\n",
"compiler = Compiler()\n",
"\n",
"# compile the Process (and all connected Processes) into an executable\n",
"executable = compiler.compile(lif2, run_cfg=Loihi1SimCfg())"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"4. Execution stage: When compilation is complete, _Processes_ can be\n",
"executed. The execution stage ensures that the (prior) compilation stage has\n",
"been completed and otherwise invokes it."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"executable = compiler.compile(lif2, run_cfg=Loihi1SimCfg())\n",
"\n",
"# Execution stage\n",
"\n",
"from lava.magma.runtime.runtime import Runtime\n",
"from lava.magma.core.run_conditions import RunSteps\n",
"from lava.magma.runtime.message_infrastructure.message_interface_enum import ActorType\n",
Expand Down
4 changes: 1 addition & 3 deletions tutorials/in_depth/tutorial05_connect_processes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@
" def run_spk(self):\n",
" data = np.array([1, 2])\n",
" self.out.send(data)\n",
" print(\"Sent output data of P1: {}\".format(data))\n",
"\n",
"\n",
"\n",
Expand All @@ -137,8 +136,7 @@
" inp: PyInPort = LavaPyType(PyInPort.VEC_DENSE, int)\n",
"\n",
" def run_spk(self):\n",
" in_data = self.inp.recv()\n",
" print(\"Received input data for P2: {}\".format(in_data))"
" in_data = self.inp.recv()"
]
},
{
Expand Down