Skip to content

Commit

Permalink
Improve interactive tests (#623)
Browse files Browse the repository at this point in the history
* improve tests

* remove print

* update notebook

* remove test
  • Loading branch information
aloctavodia authored Jan 8, 2025
1 parent c11b5f3 commit 0a3ba3f
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 72 deletions.
4 changes: 3 additions & 1 deletion preliz/distributions/continuous_multivariate.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,19 +654,21 @@ def plot(**args):
interval,
levels,
"full",
legend,
figsize,
None,
xy_lim,
)
elif kind == "ppf":
plot_mvnormal(
self,
"cdf",
"ppf",
"marginals",
pointinterval,
interval,
levels,
None,
legend,
figsize,
None,
xy_lim,
Expand Down
52 changes: 25 additions & 27 deletions preliz/tests/check_inside_notebook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "81849101",
"metadata": {},
"outputs": [],
Expand All @@ -17,10 +17,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "1630c205",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[32m.\u001b[0m\u001b[32m [100%]\u001b[0m\n",
"\u001b[32m\u001b[32m\u001b[1m1 passed\u001b[0m\u001b[32m in 0.01s\u001b[0m\u001b[0m\n"
]
}
],
"source": [
"%%ipytest\n",
"\n",
Expand All @@ -32,10 +41,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "843a0c94",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[32m.\u001b[0m\u001b[32m [100%]\u001b[0m\n",
"\u001b[32m\u001b[32m\u001b[1m1 passed\u001b[0m\u001b[32m in 0.01s\u001b[0m\u001b[0m\n"
]
}
],
"source": [
"%%ipytest\n",
"\n",
Expand All @@ -44,26 +62,11 @@
" captured = capsys.readouterr()\n",
" assert \"widget\" in captured.out\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a57cc174",
"metadata": {},
"outputs": [],
"source": [
"%%ipytest\n",
"%matplotlib widget\n",
"def test_widget_ok(capsys):\n",
" check_inside_notebook(need_widget=True)\n",
" captured = capsys.readouterr()\n",
" assert \"\" == captured.out"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "test",
"display_name": "preliz",
"language": "python",
"name": "python3"
},
Expand All @@ -77,12 +80,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.0 (default, Nov 15 2020, 14:28:56) \n[GCC 7.3.0]"
},
"vscode": {
"interpreter": {
"hash": "5b344a7d0839c309585d2ae27435157813d3b4ade1fa431f12bd272ea9135317"
}
"version": "3.11.8"
}
},
"nbformat": 4,
Expand Down
89 changes: 51 additions & 38 deletions preliz/tests/plot_interactive.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,49 @@
"cells": [
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "81849101",
"metadata": {},
"outputs": [],
"source": [
"import ipytest\n",
"import pytest\n",
"ipytest.autoconfig()\n",
"\n",
"from preliz.distributions import Poisson, Normal"
"import preliz as pz"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "1630c205",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "fe1d483fb663406497fd7556c010dad0",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(FloatSlider(value=3.0, description='mu-1 (-inf, inf)', max=13.0, min=-7.0, style=SliderS…"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"%%ipytest\n",
"%%ipytest -qq\n",
"\n",
"@pytest.mark.parametrize(\"kind,xy_lim,pointinterval,quantiles\", [\n",
" (\"pdf\", \"both\", True, None),\n",
Expand All @@ -29,35 +53,21 @@
" (\"pdf\", \"both\", False, [])\n",
"])\n",
"def test_plot_interactive(kind, xy_lim, pointinterval, quantiles):\n",
" result = Poisson().plot_interactive(kind, xy_lim, pointinterval, quantiles)\n",
" result._ipython_display_()\n",
" result.children[0].value = 5\n",
" result = pz.Poisson().plot_interactive(kind, xy_lim, pointinterval, quantiles)\n",
" assert result.children[0].value == 4.5\n",
"\n",
" result = Normal(0, 1).plot_interactive(kind, xy_lim, pointinterval, quantiles) \n",
" result.children[0].value = -1\n",
" result.children[1].value = 5\n",
" result = pz.Normal(0, 1).plot_interactive(kind, xy_lim, pointinterval, quantiles) \n",
" assert result.children[0].value == 0\n",
" assert result.children[1].value == 1\n",
"\n",
" result = pz.Truncated(pz.Normal(0, 2), -1, 1).plot_interactive(kind, xy_lim, pointinterval, quantiles)\n",
" assert result.children[0].value == 0\n",
" assert result.children[3].value == 1\n",
"\n",
" result = pz.Hurdle(pz.Normal(0, 2), 0.9).plot_interactive(kind, xy_lim, pointinterval, quantiles)\n",
" assert result.children[1].value == 2\n",
" assert result.children[2].value == 0.9\n",
"\n",
"def test_plot_interactive_all():\n",
" for idx, distribution in enumerate(pz.distributions.__all__):\n",
" if distribution not in [\n",
" \"Dirichlet\",\n",
" \"MvNormal\",\n",
" \"Truncated\",\n",
" \"Censored\",\n",
" \"Hurdle\",\n",
" \"Mixture\",\n",
" ]:\n",
" dist = getattr(pz.distributions, distribution)\n",
" kind = [\"pdf\", \"cdf\", \"ppf\"][idx % 3]\n",
" xy_lim = [\"auto\", \"both\"][idx % 2]\n",
" dist().plot_interactive(kind=kind, xy_lim=xy_lim)\n",
" if distribution in [\"Truncated\", \"Censored\"]:\n",
" dist = getattr(pz.distributions, distribution)\n",
" dist(pz.Normal(0, 2), -1, 1).plot_interactive(kind=\"pdf\", xy_lim=\"both\")\n",
" if distribution == \"Hurdle\":\n",
" dist = getattr(pz.distributions, distribution)\n",
" dist(pz.Normal(0, 2), 0.9).plot_interactive(kind=\"pdf\", xy_lim=\"both\")\n",
"\n",
"@pytest.mark.parametrize(\n",
" \"kwargs\",\n",
Expand Down Expand Up @@ -105,11 +115,19 @@
" a_dist.plot_interactive(kind=\"ppf\", **kwargs)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b57f808a",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "test",
"display_name": "preliz",
"language": "python",
"name": "python3"
},
Expand All @@ -123,12 +141,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.0 (default, Nov 15 2020, 14:28:56) \n[GCC 7.3.0]"
},
"vscode": {
"interpreter": {
"hash": "5b344a7d0839c309585d2ae27435157813d3b4ade1fa431f12bd272ea9135317"
}
"version": "3.11.8"
}
},
"nbformat": 4,
Expand Down
24 changes: 18 additions & 6 deletions preliz/tests/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,22 @@
def run_notebook(notebook):
current_dir = path.dirname(path.realpath(__file__))
file_path = path.join(current_dir, notebook)
with open(file_path, encoding="ascii") as inb_f:

with open(file_path, encoding="utf-8") as inb_f:
inb = nbformat.read(inb_f, as_version=4)
exec_pre = ExecutePreprocessor(timeout=600, kernel_name="python3")
try:
assert exec_pre.preprocess(inb) is not None, f"Got empty notebook for {notebook}"
except Exception: # pylint: disable=broad-except
assert False, f"Failed executing {notebook}"

exec_pre = ExecutePreprocessor(timeout=600, kernel_name="python3")

try:
exec_pre.preprocess(inb, {"metadata": {"path": current_dir}})
except Exception as e:
raise RuntimeError(f"Notebook execution failed: {e}") from e

for cell in inb.cells:
if cell.cell_type == "code" and "outputs" in cell:
for output in cell.outputs:
try:
if "FAILED" in output.text:
raise RuntimeError(f"Error in notebook cell:\n{output.text}")
except AttributeError:
pass

0 comments on commit 0a3ba3f

Please sign in to comment.