author | date | title |
---|---|---|
Dan Gunter |
7 December 2022 |
IDAES Examples Tutorial |
This is for developers. Users should refer to the "readme.md" file in this directory.
Assumptions:
- user = 'username'
For more details on commands described here, see the README.md
file in this
directory.
This section describes how to download and install the examples
repo.
Go to https://github.com/IDAES/examples and fork the repository.
Start a shell and change to directory where you will clone the repository.
git clone https://github.com/username/examples
cd examples
git remote add upstream https://github.com/IDAES/examples
Continue in the directory where you cloned the repository. Create a conda environment and install examples (and dependencies, including idaes-pse).
conda create -y -n idaes-examples python=3.10
conda activate idaes-examples
pip install -r requirements-dev.txt
idaes get-extensions
Note #1: The "pip install" step will provide time to brew some coffee, get a snack, or see why the kids are so quiet (depending on your particular circumstances).
Note #2: The idaes get-extensions
command makes global changes, so use the --to
option or skip this step if you need to keep a specific version of the extensions installed for other work.
We will walk through the steps for a new example Jupyter Notebook
Continue in the directory where you cloned the repository. Create a Jupyter notebook in an existing folder.
Change to a subdirectory and start the Jupyter notebook server:
cd idaes_examples/nb/flowsheets
jupyter notebook &
Then, in the Jupyter notebook browser, create a new blank note book.
Name the notebook my_notebook_src
and make the first cell a Markdown cell with the following text:
# My notebook
This is my example notebook.
Add 3 code cells with the following content (one statement per cell):
import idaes
print(idaes.__version__)
assert idaes.__version__.startswith('2')
To demonstrate how the tags work, we will add tags to a couple of these cells:
- To see current tags, select "View" -> "Cell toolbar" -> "Tags".
- Add the tag
noauto
to the cell with the print statement. This will skip this cell in the versions of the notebook used for documentation and testing. - Add the tag
testing
to the cell with the assert statement. This will only include this cell in the version fo the notebook used for testing.
There is some custom Python code to preprocess each _src
notebook into a different
version (with different extensions) for documentation, testing, tutorial exercises, and
tutorial solutions. This preprocessing step is run automatically in the build and
test commands, but for demonstration purposes we can run it manually with:
idaesx pre
Then you can look in the directory idaes_examples\nb\flowsheets
to see the
generated versions of your tutorial notebook.
Finally, save the notebook.
For building and testing to 'see' a notebook it needs to be in the Jupyterbook table of contents. The following steps add the new notebook to the table of contents.
- Open
idaes_examples/nb/_toc.yml
in an editor. - Find
- file: flowsheets/index
underchapters:
. - In the list of sections for that chapter, add
- file: flowsheets/my_notebook_doc
(indent the same as the other sections). - Save the modified file.
Note: Although the notebook was called my_notebook_src
, the filename in the table of contents entry is my_notebook_doc
.
Example notebooks are tested with pytest
, using a special configuration that is stored in the root directory of the repository (in the file "pyproject.toml").
Change to the top directory for the repository, then test your new example notebook with:
pytest -k my_notebook
Why -k
? Although you can test all notebooks with the simple command pytest
, since notebooks are found and tested with an extra level of indirection you can't simply name the notebook directly. Instead, testing a single notebook requires using a "-k" expression that will match the notebook.
In the transition period, there may be changes to example notebooks, etc. in examples-pse
that need to be moved over into this repo.
There are bulk migration scripts in the 'scripts/' directory, but the intent of this mini-tutorial is to help migrate one example notebook (though there is one file in that directory we will use).
In scripts/map.yml
there is a mapping of the notebook subdirectories in examples-pse, undder src/
to the subdirectories under idaes_examples/nb/
in this repository.
For example:
map:
- Examples/DAE: dae
- # ..etc..
This means that the notebook src/Examples/DAE/petsc_chem_example.ipynb
in the examples-pse repo
should be placed in idaes_examples/dae/petsc_chem_src.ipynb
in the new repo.
Filenames will mostly stay the same, but certain suffixes on the old files should
be stripped, and the new suffix _src
added.
So, any of the following..
foo_example.ipynb
foo_solution.ipynb
foo_solution_testing.ipynb
foo_testing.ipynb
foo.ipynb
..should be copied to the file foo_src.ipynb
in the new structure.
Before copying the notebook, back up any old version(s) first.
Then simply copy the file from the old location to the new one.
For example, using the previous example, if examples-pse
and examples
were cloned
in the same parent directory, and the working directory is the examples
root, do:
# backup
cp idaes_examples\nb\dae\petsc_chem_src.ipynb idaes_examples\nb\dae\petsc_chem_src.bak
# overwrite
cp ..\examples-pse\src\Examples\DAE\petsc_chem_example.ipynb .\idaes_examples\nb\dae\petsc_chem_src.ipynb
To see current cell tags in the notebook, open it with the Jupyter Notebook UI and then
select "View" -> "Cell toolbar" -> "Tags".
You should go through the notebook and take out any remove_cell
tags.
You can also, if you want, use additional tags available in the new framework.
See the 'Jupyter Notebook cell tags' section of the README.md
in this directory.
To add or update copyright headers, you can run addheader
in the root directory.
This will look at all files but should only change notebooks where needed.
Refer to the 'Test the example' section above for how to test-run the notebook with pytest, and also the 'Other tasks' section for how to build documents and run Black formatting.
To do quick sanity checks and other tests of the example notebooks, there are standard Python test files under idaes_examples/nb
. To run those instead of the example notebooks, simply run pytest from the idaes_examples
sub-directory:
cd idaes_examples
pytest
Note: This works because there is a "pytest.ini" in that directory that is used instead of the configuration in the root of the repository. That configuration does not set up the special ('nbmake') configuration for running Jupyter notebooks under pytest.
To build (or rebuild) the documentation:
idaesx build
Note #1: This rebuilds all the docs, which also involves executing them. This could take many minutes. Note #2: From the output you can see that "Jupyterbook" is really doing most of the work here. Note #3: In theory rebuilding the docs after the first time can re-use all the previously built versions and save a lot of time. Getting Jupyterbook caching working has been harder than anticipated.
When the documentation finally finished building, the HTML output will be in idaes_examples\nb\_build\html
. To view, open the "index.html" file with a browser.
To make sure the notebook code is formatted according to IDAES convention, you can run Black on all the notebooks:
idaesx black
There is a simple embedded GUI for browsing the notebooks. While, of course, you can navigate to the notebooks with Jupyter's file browser, the GUI has the advantage of giving notebook descriptions as you browse.
idaesx gui
Note #1: Due to the limitations of the Tk toolkit, the font is pretty ugly. Sorry. Note #2: The first markdown cell with a header is used for the description.