Matt McCormick
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
# How to raise the quality of scientific Jupyter notebooks --- title: "How to raise the quality of scientific Jupyter notebooks" author: - name: "Matt McCormick" orcid_id: 0000-0001-9475-3756 affiliation: Kitware - name: "Mary Elise Dedicke" orcid_id: 0000-0001-8848-3235 affiliation: Kitware - name: "Alex Remedios" orcid_id: 0000-0003-3817-1879 affiliation: TreebeardTech date: 04 January 2023 --- ###### By: Matt McCormick [![ORCID](https://i0.wp.com/info.orcid.org/wp-content/uploads/2020/12/orcid_16x16.gif?resize=16%2C16&ssl=1)](https://orcid.org/0000-0001-9475-3756), Mary Elise Dedicke [![ORCID](https://i0.wp.com/info.orcid.org/wp-content/uploads/2020/12/orcid_16x16.gif?resize=16%2C16&ssl=1)](https://orcid.org/0000-0001-8848-3235), Alex Remedios [![ORCID](https://i0.wp.com/info.orcid.org/wp-content/uploads/2020/12/orcid_16x16.gif?resize=16%2C16&ssl=1)](https://orcid.org/0000-0003-3817-1879)* ###### tags: `post`, `jupyter`, `testing` ## Reproducibility in Computational Experiments Jupyter has emerged as a fundamental component in artificial intelligence (AI) solution development and scientific inquiry. [Jupyter notebooks](https://jupyter.org/) are prevelant in modern education, commercial applications, and academic research. The [Insight Toolkit (ITK)](https://itk.org) is an open source, cross-platform toolkit for N-dimensional processing, segmentation, and registration used to obtain quantitative insights from medical, biomicroscopy, material science, and geoscience images. The ITK community highly values scientific reproducibility and software sustainability. As a result, advanced computational methods in the toolkit have a dramatically larger impact because they can be reproducibly applied in derived research or commercial applications. Since ITK's inception in 1999, there has been a focus on engineering practices that result in high-quality software. High-quality scientific software is driven by regression testing. The ITK project supported the development of [CTest](https://cmake.org/cmake/help/latest/manual/ctest.1.html) and [CDash](https://www.cdash.org/) unit testing and software quality dashboard tools for use with the [CMake](https://cmake.org) build system. In the Python programming language, the [pytest](https://pytest.org) test driver helps developers write small, readable scripts that ensure their software will continue to work as expected. However, pytest can only test Python scripts by default, and errors in untested computational notebooks are more common than well-tested Python code. In this post, we describe how ITK's Python interface leverages [nbmake](https://github.com/treebeardtech/nbmake), a simple, powerful tool that increases the quality of Jupyter notebooks through testing with the pytest test driver. ## nbmake [nbmake](https://github.com/treebeardtech/nbmake) is a pytest plugin that enables developers to verify that Jupyter notebook files run without error. This lets teams keep notebook-based documents and research up to date in an evolving project. Quite a few libraries exist that relate to unit testing and notebooks: - [nbval](https://github.com/computationalmodelling/nbval) is a strong fit for developers who want to check that cells always render to the same value - [testbook](https://github.com/nteract/testbook) is maintained by the nteract community, and is good for testing functions written inside notebooks - [nbmake](https://github.com/treebeardtech/nbmake) is popular for those maintaining documentation and research material; it programmatically runs notebooks from top to bottom to validate the contents We used nbmake because of the simplicity of its adoption, integration with pytest, and the ability to test locally and in continuous integration (CI) testing systems like GitHub Actions. ## Getting Started A great first step for testing notebooks is to run nbmake with its default settings: ```sh pip install nbmake # install the python library pytest --nbmake my_notebook.ipynb # Invoke pytest with nbmake on a notebook ``` This simple command will detect a majority of common issues, such as import errors, in the notebook. To add more detail on expected notebook content, simply add assertions in two notebook cells like this: ```py # my_notebook.ipynb # %% Cell 1 x = 42 # %% Cell 2 assert x == 42 ``` Use assertions to check your notebook is working as expected. **Note:** If you want to present a cleaner version of the notebook without assertions, you can use [Jupyter book](https://jupyterbook.org/) to render it into a site and use the [remove-cell tag](https://jupyterbook.org/en/stable/interactive/hiding.html?highlight=tag#remove-an-entire-code-cell) to omit assertions from the output. ### Tips for using nbmake Starting to test any software package is always difficult, but once begun it becomes easier to maintain quality software. If your directory contains an assortment of notebooks, you will find different blockers: - some require kernels with different types and names - some raise errors, expectedly or unexpectedly - some require authentication and network dependencies - some will take hours to run Our advice is to start small. Try running nbmake locally from your development environment on a single short notebook. If that is a challenge, use the `--nbmake-find-import-errors` flag to only check for missing dependencies. Once you have a minimal quality bar, you can think about what's next for your testing approach. - Run software quality checks on CI - Test more notebooks - Reduce the test time by using [`pytest-xdist`](https://pypi.org/project/pytest-xdist/) - Use the `--overwrite` flag to write the output notebooks to disk to build documentation or commit to version control. For complete docs and troubleshooting guides, see the [nbmake GitHub repo](https://github.com/treebeardtech/nbmake). ## ITK Use Cases When creating spatial analysis processing pipelines, example code, notebook visualization tools, and documents, [ITK](https://itk.org) leverages nbmake to ensure high-quality, easy-to-maintain documentation without common import errors or unexpected errors. In ITK extensions such as [ITKIOScanco](https://github.com/KitwareMedical/ITKIOScanco) (a module to work with 3D microtomography volumes) or [ITKElastix](https://github.com/InsightSoftwareConsortium/ITKElastix) (a toolbox for rigid and nonrigid registration of images), nbmake runs notebooks in [GitHub Actions](https://github.com/features/actions) CI tests. ![](https://i.imgur.com/yl5PjvG.png) *ITKElastix GitHub Actions notebook test output, displaying standard pytest status information.* ![](https://i.imgur.com/YcPspvB.png) *CI testing catching an error and displaying traceback information via pytest*. [itkwidgets](https://itkwidgets.readthedocs.io/en/latest/), a next-generation, simple 3D visualization tool for Python notebooks, uses nbmake to check its functionality. ![](https://i.imgur.com/vqEpxCN.png) *An [itkwidgets visualization notebook](https://github.com/InsightSoftwareConsortium/itkwidgets/blob/main/examples/integrations/itk/3DImage.ipynb) tested with nbmake.* In ITK's [Sphinx examples](https://examples.itk.org/), notebooks are embedded into HTML documentation and continuously tested. ![](https://i.imgur.com/pXeqGzg.png) *An [explanation of optimization](https://examples.itk.org/src/registration/common/mutualinformation/mutualinformation) during image registration. The notebook is rendered in Sphinx and tested with nbmake.* ## Next Steps While notebooks have been around for some time, we are still learning how to integrate them into rigorous engineering processes. The nbmake project is continuing to develop ways to remove friction associated with maintaining quality notebooks in many organizations. Over the next year, there will be updates to both the missing imports checker (to keep virtual environments up to date) and the mocking system (to skip slow and complex cells during testing). Nbmake is maintained by [treebeard.io](https://treebeard.io), a machine learning infrastructure company based in the UK. ITK is a [NumFOCUS](https://numfocus.org) project with commercial support offered by [Kitware](https://www.kitware.com), an open source scientific computing company headquartered in the USA.

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully