Skip to content

Commit

Permalink
add notebook test
Browse files Browse the repository at this point in the history
  • Loading branch information
cbouy committed Dec 24, 2023
1 parent f3a890b commit 4a42787
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 107 deletions.
18 changes: 16 additions & 2 deletions .github/workflows/_build-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ on:
default: false
required: false
type: boolean
upload-package:
default: false
required: false
type: boolean

defaults:
run:
Expand Down Expand Up @@ -78,9 +82,19 @@ jobs:
- name: Cache package
if: inputs.cache-package
uses: actions/cache/save@v3
id: cache-mols2grid
with:
path: |
dist/mols2grid-*.whl
dist/mols2grid-*.tar.gz
key: mols2grid-${{ runner.os }}-${{ github.sha }}
key: mols2grid-${{ runner.os }}-${{ github.sha }}

- name: Expose package as artifact
if: inputs.upload-package
uses: actions/upload-artifact@v4
with:
name: mols2grid-package
path: |
dist/mols2grid-*.whl
dist/mols2grid-*.tar.gz
if-no-files-found: error
retention-days: 20
146 changes: 88 additions & 58 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ jobs:
uses: ./.github/workflows/_build-package.yml
with:
cache-package: true
upload-package: true

test-build:
name: Test build
Expand Down Expand Up @@ -170,61 +171,90 @@ jobs:
pip install dist/mols2grid-*.whl \
&& python test_install.py
# notebook-tests:
# needs: [build]
# name: ${{ matrix.label }}
# runs-on: ubuntu-latest
# strategy:
# matrix:
# include:
# - label: JLab-3-Widgets-7
# extra_dependencies: "'jupyterlab~=3.2' 'ipywidgets~=7.6'"
# - label: JLab-4-Widgets-8
# extra_dependencies: "'jupyterlab~=4.0' 'ipywidgets~=8.1'"

# steps:
# - uses: actions/checkout@v3

# - name: Retrieve cached package
# uses: actions/cache/restore@v3
# id: cache-mols2grid
# with:
# path: |
# dist/mols2grid-*.whl
# dist/mols2grid-*.tar.gz
# key: mols2grid-${{ runner.os }}-${{ github.sha }}

# - name: Cache conda
# uses: actions/cache@v3
# env:
# CACHE_NUMBER: 2
# with:
# path: ~/conda_pkgs_dir
# key:
# conda-${{ matrix.label }}-${{ env.CACHE_NUMBER }}

# - name: Setup Conda
# uses: conda-incubator/setup-miniconda@v2
# with:
# python-version: "3.10"
# miniforge-variant: Mambaforge
# miniforge-version: latest
# use-mamba: true

# - name: Install remaining conda dependencies
# run: |
# mamba install rdkit pandas ${{ matrix.extra_dependencies }}
# mamba list

# - name: Get wheel absolute path
# id: wheel-path
# run: echo "value=$(ls -d $PWD/dist/mols2grid-*.whl)" >> $GITHUB_OUTPUT

# - name: Install the extension
# run: |
# pip install 'mols2grid[packaging] @ file://${{ steps.wheel-path.outputs.value }}'
# jupyter labextension list

# - name: Run test
# run: |
# echo TODO
notebook-tests:
needs: [build]
name: ${{ matrix.label }}
runs-on: ubuntu-latest
strategy:
matrix:
include:
- label: JLab-3-Widgets-7
extra_dependencies: "'jupyterlab~=3.2' 'ipywidgets~=7.6'"
- label: JLab-4-Widgets-8
extra_dependencies: "'jupyterlab~=4.0' 'ipywidgets~=8.1'"

steps:
- uses: actions/checkout@v3

- name: Retrieve cached package
uses: actions/cache/restore@v3
id: cache-mols2grid
with:
path: |
dist/mols2grid-*.whl
dist/mols2grid-*.tar.gz
key: mols2grid-${{ runner.os }}-${{ github.sha }}

- name: Cache conda
uses: actions/cache@v3
env:
CACHE_NUMBER: 2
with:
path: ~/conda_pkgs_dir
key:
conda-${{ matrix.label }}-${{ env.CACHE_NUMBER }}

- name: Setup Conda
uses: conda-incubator/setup-miniconda@v2
with:
python-version: "3.10"
miniforge-variant: Mambaforge
miniforge-version: latest
use-mamba: true

- name: Install remaining conda dependencies
run: |
mamba install \
rdkit pandas \
${{ matrix.extra_dependencies }}
mamba list
- name: Install XeLatex
run: sudo apt-get install texlive-xetex

- name: Install the extension
run: |
pip install dist/mols2grid-*.whl
jupyter labextension list
- name: Run test notebook and convert to PDF
working-directory: tests/notebooks/
run: |
jupyter nbconvert --to pdf --execute filtering.ipynb
- name: Convert to PNG
working-directory: tests/notebooks/
run: |
file=$(ls *.pdf)
mv $file ${{ matrix.label }}.pdf
ls
convert ${{ matrix.label }}.pdf ${{ matrix.label }}.png
- name: Run diff
id: diff-test
working-directory: tests/notebooks/
run: |
delta=$(magick -metric AE -fuzz 10% ref-${{ matrix.label }}.png ${{ matrix.label }}.png null:)
echo "Delta: $delta"
[[ $delta -eq 0 ]]
- name: Upload output if fail
if: steps.diff-test.outcome.failure
uses: actions/upload-artifact@v4
with:
name: tests/notebooks/notebook-${{ matrix.label }}
path: |
${{ matrix.label }}.png
${{ matrix.label }}.pdf
if-no-files-found: error
retention-days: 5
76 changes: 76 additions & 0 deletions tests/notebooks/filtering.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pathlib import Path\n",
"from time import sleep\n",
"from ipywidgets.widgets import FloatRangeSlider\n",
"from ipywidgets import interact\n",
"from rdkit import RDConfig\n",
"import mols2grid\n",
"\n",
"assert Path(RDConfig.RDDocsDir).is_dir(), \"Test data not available!\"\n",
"\n",
"SDF_FILE = f\"{RDConfig.RDDocsDir}/Book/data/solubility.test.sdf\"\n",
"df = mols2grid.sdf_to_dataframe(SDF_FILE)\n",
"\n",
"# setup range slider for the solubility column `SOL`\n",
"sol_range = (df[\"SOL\"].min(), df[\"SOL\"].max())\n",
"slider = FloatRangeSlider(value=sol_range, min=sol_range[0], max=sol_range[1], step=.5)\n",
"\n",
"# generate the grid\n",
"grid = mols2grid.MolGrid(df, size=(120, 100))\n",
"view = grid.display(n_items_per_page=12)\n",
"\n",
"# add interactive callback that filters the grid on slider interaction\n",
"@interact(solubility=slider)\n",
"def filter_grid(solubility):\n",
" results = grid.dataframe.query(\n",
" \"@solubility[0] <= SOL <= @solubility[1]\"\n",
" )\n",
" return grid.filter_by_index(results.index)\n",
"\n",
"# display view\n",
"view"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# manually trigger interactive filtering on solubility.\n",
"# should display mols at index 68, 86, 115... etc. on the cell above\n",
"slider.value = (0, 1)\n",
"# give enough time for the callback and RDKit.js to do their thing\n",
"sleep(3)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.17"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 0 additions & 47 deletions tests/notebooks/tests.ipynb

This file was deleted.

0 comments on commit 4a42787

Please sign in to comment.