No project description provided
Project description
pytest-visual
pytest-visual
introduces the concept of visual testing to ML (with automated change detection!).
Problem statement
Most ML code is poorly tested. This is at least partially because a lot of ML code is very hard to test meaningfully.
For example, we can easily test whether an image data augmentation pipeline produces an image with the correct shape, but does the image also look realistic? And does augmentation produce enough variation? These tests might succeed, but that doesn't mean that your code works.
Solution - visual testing for ML
pytest-visual
aims to streamline the organization of visualizations in ML code, enhancing debugging, verification, and code clarity. It automatically detects changes in visualization outputs, presents them in your browser, and prompts for approval. Accepted changes are memorized and not prompted for again. As a result, your visualizations
- will actually catch bugs, as changes won't go unnoticed.
- are always up to date, as they're run on every
pytest-visual
invocation. - won't clutter your production code, as they're separated into test cases.
Example
In this example, we demonstrate how to create a basic visual test case. We first create a Python file with a prefix test_*
(eg. test_visualization.py
), and add this function:
# `pytest-visual` accepts `plotly` figures
import plotly.express as px
# `test_` prefix makes this a pytest test
# `visual` argument makes this a visual test
def test_show_square(visual):
# Plot the test data
x, y = [0, 1, 2, 3, 4, 5], [0, 1, 4, 9, 16, 25]
fig = px.scatter(x=x, y=y)
# You can have multiple print() and show() calls in a single case.
visualize.print("Square plot")
visualize.show(fig)
Then run pytest --visualize
, and open the url displayed in the terminal window (most likely http://127.0.0.1:8050
). If the visualization looks OK, click "Accept", and pytest should complete with success.
Installation
pip install pytest-visual
CLI usage
With pytest-visual
plugin, the following options are added to pytest
:
Command | Description |
---|---|
pytest --visual |
Run pytest with visual tests, and prompt user for manual review on all detected changed. |
pytest --visual-yes-all |
Accept everything without prompting. Useful when you clone a repository or check out a new branch. |
pytest --visual-reset-all |
Mark all cases as declined. |
FAQ
Can I mix unit testing with visual testing?
Yes. And this is exactly what you should be doing! Just omit the visual
fixture, and it's a normal pytest
test again.
Which parts of a typical ML codebase can be visually tested?
Visual testing can be used to verify the correctness of
- data loading and augmentation
- synthetic data generation
- NN model strutures
- loss functions
- learning rate schedules
- detection algorithms
- various filtering algorithms
- etc.
My visual tests are flaky. Why?
This is typically because your output is undeterministic. For example, plotting a loss over a short training run is probably not a good idea, as it will likely be different for each run. If you need it as a test, consider a normal unit test with some threshold for expected accuracy. Tiny floating point errors could lead to inconsistent output.
That said, we're planning to make pytest-visual
robust to small fluctuations in output.
Why even bother testing ML code?
If you've implemented complex data processing logic, custom neural network architectures, advanced post processing steps, or tried to reproduce a paper, you know that bugs can take a very large part of the development effort. Reduced accuracy is often the only symptom of a bug. When conducting an ML experiment, it's typical to waste a lot of time on
- debugging when your ML code contains a bug,
- debugging when your ML code doesn't contain a bug, but you don't know this,
- revisiting an idea, because you're not sure whether your experiment was correct,
- dropping a good idea because your experiment contained a bug, but you don't know this.
Proper testing, including visual testing, can avoid a large portion of these headaches.
Isn't automation the whole point of unit testing automation? Now we have a potentially lazy/irresponsible human in the loop, who can just blindly "accept" a test.
Full automation is a of course a great, but even the tests that are automatically run are written by people. Test quality can vary a lot, and tests written by a lazy/irresponsible person probably won't catch many bugs. Just having unit tests won't magically fix your code, but they are a very efficient tool to enable responsible developers write reliable code.
Contributing
Your help is greatly needed and appreciated! Given that the library is still in alpha, there will likely be a lot of changes. Thus the main focus is to get the core features right and fix bugs.
We encourage you to contribute by submitting PRs to improve the library. Usually, the PR should link to an existing issue, where the solution idea has been proposed and discussed. In case of simple fixes, where you can clearly see the problem, you can also directly submit a PR.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pytest-visual-0.2.0.tar.gz
.
File metadata
- Download URL: pytest-visual-0.2.0.tar.gz
- Upload date:
- Size: 8.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fc7fc2a3e76ce2faa77c60e86a64429f3ada5b134046f46beda92b2fc9f7af3f |
|
MD5 | f73eeaa3b31fb0da67da3495897118f4 |
|
BLAKE2b-256 | 8d35af81551e5239467dc1c30e927da29e8ddd1ecc85d3e2644822799c55560f |
File details
Details for the file pytest_visual-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: pytest_visual-0.2.0-py3-none-any.whl
- Upload date:
- Size: 9.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 215649e44b2dfe1b68216153af04f92b065d34c22289fc1511fe17493f193508 |
|
MD5 | 1f3f4274d8d833f3fa8c4f1eef73c28f |
|
BLAKE2b-256 | bbfadd2f484254ef7cee051c973f18760c34634e70d50928e8b93f69f18e2bf4 |