Build the package from notebooks¶
The source notebooks listed in source/source-map.json own their generated
Python modules. Edit their marked cells, save the notebooks, then run:
python tools/export_notebooks.py
python -m pytest
python tools/check_notebooks.py
The build command reads saved files, so unsaved edits in a running kernel are not exported. Running a notebook does not write package modules. Notebook execution, source export, and package tests are separate steps.
Use the check mode in CI or before committing:
python tools/export_notebooks.py --check
It generates the modules into a temporary directory and compares their complete
bytes with the package files. A mismatch exits with status 1 without changing
anything; invalid source or ownership exits with status 2. Commands work from
any directory when the script is addressed by its full path; --root PATH
selects another checkout explicitly.
Source ownership¶
The manifest uses project-relative paths and lists every Python module under
timegym/, either as generated source or an explicit handwritten exception:
{
"schema_version": 1,
"sources": [
{
"notebook": "source/01_example.ipynb",
"modules": ["timegym/example.py", "timegym/helpers/scaling.py"]
}
],
"handwritten": []
}
Each source notebook declares {"timegym": {"role": "source"}} in its
notebook metadata. Module ownership lives only in source-map.json; do not
duplicate a modules list in notebook metadata. Its cells have stable, unique IDs. The notebook has one
default module directive, for example #| default_exp example, and marks
library cells with #| export. A cell for another module can use
#| export helpers.scaling. Both modules must belong to that notebook in the
manifest. Use one consistent destination per module: do not mix bare exports
and explicit exports that refer to the same default module.
Ordinary demonstration cells are not exported. A notebook may own several
modules; each module has exactly one owner. Notebook imports can use absolute
timegym imports, which nbdev translates to package-relative imports when
appropriate. Generated source includes a relative link to its notebook and
cell IDs, independent of the current directory and temporary build location.
After nbdev export, the builder orders the module banner, imports, and one merged
__all__, then formats the generated source with pinned Black 26.10.1. Formatting
is part of both build and check mode; never format or edit generated files by hand.
Shared internal helpers¶
Notebook 12 owns timegym/_internal.py: integer guards, real-array coercion, and
JSON-safe evidence conversion shared by sibling modules. Import these helpers
from that module rather than through contracts or reporting; changes to them
apply across the package. The module is private, with no public API guarantee.
Notebook 14 also owns timegym/_process_common.py: the shared controlled-process
parameter grammar, seed namespaces, value transforms, registry and case assembly.
Joint builders import these primitives; processes selects the scalar or joint
builder and scenarios retains resolved parameters.
Same-chapter dependencies¶
A chapter can define several modules in one kernel. Exported implementation
cells must import sibling modules from the installed package, but those imports
can replace names just defined in the notebook with previously exported copies.
When this happens, use a non-exported local-<thing>-capture cell to save the live
objects before the importing cell, then a non-exported local-<thing>-restore
cell to rebind them after the imports and before the examples. For example,
chapter 01 saves (Case, Scenario) and restores them before its demonstrations.
Every restore cell must assert that the examples use the intended live objects:
use identity checks such as metrics.mse is mse, or check that a local class or
function has __module__ == __name__. Check the dependent function's globals
when the binding is indirect, as chapter 09 does for evaluate_forecast.
Chapters 10 and 12 need only assertions in their local-*-restore cells: their
current imports do not overwrite local definitions, so no capture is needed.
These bindings require Restart Kernel and Run All in cell order. Rerunning a definition or import alone can leave the saved objects stale. Add a pair only when a new same-chapter import would shadow a definition used in an example; ordinary imports from other chapters need no pair. After changes elsewhere, save and export those notebooks, then restart the dependent kernel. Export reads saved implementation cells and ignores these development-only bindings.
Validation boundary¶
The exporter uses the installed nbdev exporter; it does not execute notebook cells or import generated modules. Every exported path must match its declared owner. All exports and Python syntax checks complete before any package file is changed. Each file is replaced atomically, but not all files together: an OS error midway can leave some modules updated.
Syntax-valid code can still be wrong at runtime. Package tests check the generated library, and fresh-kernel notebook execution checks the examples. The dedicated build tests use disposable fixture projects, including failures, source drift, multiple module ownership, and unexecuted demonstrations.
This uses nbdev's modular exporter without replacing the project's package build, test runner, or release process.
Check distributions and documentation¶
python -m pip install -e '.[docs,release]'
python -m mkdocs build --strict
python -m build
python -m twine check dist/*
docs_hooks.py stages the README and linked repository documents in a temporary
directory, so the same relative links work on GitHub and in the generated site.
It parses Markdown links after code spans/fences are escaped, supports link
titles and reference links, fails on missing local targets, and does not execute
notebooks.
sync_citation.py renders README citations from CITATION.cff; --check
compares the block without writing and verifies the package/changelog identity.
attainable_table.py recomputes the scorecard's stored attainable scores: for
each of the 20 default cells, the pattern's own model family fitted to 200 fresh
series. It prints the table as the literal that source/13_scorecard.ipynb
stores; --check recomputes it and exits with status 1 when a value is more
than 0.3 points from the stored one, a margin for likelihood optimizers that
stop slightly differently across SciPy versions and platforms.
update_python_support.py reports stable CPython/pandas compatibility; --apply
prepares the coordinated update. The scheduled workflow exports changed
notebooks, rebuilds citation/manuscript outputs, tests, and opens a PR. See
releasing.
After installing the wheel in a separate environment, run its interpreter with
-I to exclude checkout imports:
/path/to/wheel-environment/bin/python -I tools/smoke_installed.py
This checks point and joint-sample forecasts, retained runs, contract-fault
detection, validated report loading, and replay while deliberately blocking
optional plotting, sklearn, and notebook imports. --report PATH also saves the
replayed report, which CI replays again in a new process with
examples/reproduce_report.py. CI runs the installed-wheel check on Linux,
macOS, and Windows. The broader test matrix also includes an older compatible
scientific stack on Python 3.12. Building artifacts does not publish a release.