Assignment 5: Graphs and Networks

Hide code cell content

import mmf_setup

mmf_setup.nbinit()
import logging

logging.getLogger("matplotlib").setLevel(logging.CRITICAL)
%matplotlib inline
import numpy as np, matplotlib.pyplot as plt

This cell adds /home/docs/checkouts/readthedocs.org/user_builds/wsu-phys-581-computation/checkouts/latest/src to your path, and contains some definitions for equations and some CSS for styling the notebook. If things look a bit strange, please try the following:

  • Choose "Trust Notebook" from the "File" menu.
  • Re-execute this cell.
  • Reload the notebook.

Assignment 5: Graphs and Networks#

See Graphs and Networks and

Project Repository#

I have created a project for this in the usual fashion on GitLab as follows:

mkdir project-graphs
cd project-graphs
hg init
pixi init --format pyproject
pixi add matplotlib scipy tqdm \
         mystmd notebook \
         graphviz graph-tool
pixi add --pypi graphviz
pixi shell
pixi run myst init
pixi task add doc-server "myst start --execute"

I did a couple of manual customizations to the pyproject.toml file:

  • Added other platforms

  • Added Jupytext configuration to leave math macros.

# pyproject.toml
project]
authors = [{name = "Michael McNeil Forbes", email = "michael.forbes+numpy@gmail.com"}]
dependencies = ["graphviz>=0.21,<0.22"]
name = "project_graphs"   # Simple name for convenience.
requires-python = ">= 3.11"
version = "0.1.0"

[build-system]
build-backend = "hatchling.build"
requires = ["hatchling"]

[tool.pixi.workspace]
channels = ["conda-forge"]
platforms = ["osx-arm64", "linux-64", "osx-arm64", "osx-64"]


[tool.pixi.pypi-dependencies]
project_graphs = { path = ".", editable = true }

[tool.pixi.tasks]
doc-server = "myst start --execute"

[tool.pixi.dependencies]
python = "*"
matplotlib = ">=3.10.8,<4"
scipy = ">=1.16.3,<2"
tqdm = ">=4.67.1,<5"
mystmd = ">=1.6.6,<2"
notebook = ">=7.5.0,<8"
graphviz = ">=14.0.5,<15"
graph-tool = ">=2.98,<3"

######################################################################
# Jupytext
# https://jupytext.readthedocs.io/en/latest/config.html
[tool.jupytext]
#formats = "ipynb,md:myst"
notebook_metadata_filter = "kernelspec,jupytext,execution,math"

I also added a GitLab CI file to build the documentation.

:class: dropdown
# .gitlab-ci.yml
image: ubuntu:24.04

build-docs:
  script:
    # - export  # Useful for debugging variables.
    - apt-get update && apt-get install -y curl unzip # build-essential curl unzip # texlive-full
    - bash <(curl -fsSL https://pixi.sh/install.sh)
    - export PATH="$HOME/.pixi/bin:$PATH"
    - export BASE_URL="${CI_PAGES_URL}"
    - pixi install
    - pixi run myst -v
    - pixi run jupyter kernelspec list

    # Due to the following issue, we need to set c.ServerApp.allow_root = True or we
    # will get a error "Jupyter server did not start"
    # "Unable to instantiate connection to Jupyter Server"
    # This is because mystmd does not execute code as root.  Unfortunately, I don't
    # think there is a way to pass this flag other than in a config file.  A better
    # option might be to run `myst` as non-root, but this seem somewhat complicated.
    # https://github.com/jupyter-book/mystmd/issues/2472
    # https://jupyter-server.readthedocs.io/en/latest/users/configuration.html
    - mkdir -p .jupyter
    - printf "c.ServerApp.allow_root = True\n" > .jupyter/jupyter_server_config.py
    - export JUPYTER_CONFIG_DIR="$PWD/.jupyter"
    - pixi run myst -d build --html --execute
    - mv _build/html public
  pages: true  # specifies that this is a Pages job
  artifacts:
    paths:
      - public
  rules:
    # Only run the CI if the commit message has "DOC" in it.
    - if: $CI_COMMIT_MESSAGE =~ "/DOC/"

I also like a Makefile:

# Makefile
doc-server:
	pixi run doc-server

test:
	pixi run test

shell:
	pixi shell

clean:
	pixi clean

realclean: clean
	pixi clean cache

notebook:
	pixi run jupyter notebook

.PHONY: doc-server shell test clean realclean notebook