A simple package to test Jupyter notebook result for the Le Wagon's Data Science Bootcamp.
Installation with pip
from Pypi:
pip install nbresult
Considering the default data challenge architecture:
.
├── challenge.ipynb
├── Makefile
├── README.md
├── data
│ └── data.csv
└── tests
└── __init__.py
If you want to test a variable log_model_score
from the challenge.ipynb
notebook with pytest
:
Anywhere in the notebook you can add a cell with the following code:
from nbresult import ChallengeResult
result = ChallengeResult('score',
score=log_model_score
)
result.write()
This outputs a score.pickle
file in the tests
directory:
.
├── challenge.ipynb
├── Makefile
├── README.md
├── data
│ └── data.csv
└── tests
├── __init__.py
└── score.pickle
Now you would like to write test on the log_model_score
with pytest
. Create a test_score.py
file:
# tests/test_score.py
from nbresult import ChallengeResultTestCase
class TestScore(ChallengeResultTestCase):
def test_score_is_above_82(self):
self.assertEqual(self.result.score > 0.82, True)
Finally you can run your tests with pytest
:
pytest tests/test_score.py
OR
Run the tests with make
:
Makefile
# Makefile
default: pytest
pytest:
PYTHONDONTWRITEBYTECODE=1 pytest -v --color=yes
make
OR
Run the tests inside the notebook:
from nbresult import ChallengeResult
result = ChallengeResult('score',
score=log_model_score
)
result.write()
print(result.check())
For more advanced folder structure, you also can specify a subdir
folder in which store & read pickle file
from nbresult import ChallengeResult
result = ChallengeResult('score',
subdir='a', # This will store pickle in tests/a/score.pickle
score=log_model_score
)
result.write()
result.check()
Check out detailed example below
Run make