Skip to content

Commit

Permalink
Merge pull request #2 from wsp-sag/apidocs
Browse files Browse the repository at this point in the history
Added apidocs
  • Loading branch information
bccheung committed Jun 21, 2024
2 parents d9b33ff + dbe4795 commit e86141f
Show file tree
Hide file tree
Showing 61 changed files with 6,204 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,6 @@ venv.bak/
.vscode/

# pycharm stuff
.idea/
.idea/

_build/
20 changes: 20 additions & 0 deletions apidocs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = .
BUILDDIR = _build

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
Binary file added apidocs/_static/apple-touch-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added apidocs/_static/favicon-16x16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added apidocs/_static/favicon-32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added apidocs/_static/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions apidocs/api_reference/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
=============
API Reference
=============

This page gives an collection of all public classes and functions that modifies and configures files and Jacquard attributes

jacquard.py
-----------

.. automodule:: jacquard.jacquard
:members:

api.py
------

.. automodule:: jacquard.api
:members:
98 changes: 98 additions & 0 deletions apidocs/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# http://www.sphinx-doc.org/en/master/config

# -- Path setup --------------------------------------------------------------

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import sys

sys.path.insert(0, os.path.abspath('..'))

# -- Project info setup ------------------------------------------------------

from datetime import datetime
from pathlib import Path

from pkg_resources import parse_version

import jacquard

version_ = parse_version(jacquard.__version__)

# -- Project information -----------------------------------------------------

project = 'wsp-jacquard'
author = 'WSP Canada Inc'
copyright = f'{datetime.today().year}, {author}'

# The short X.Y version
version = version_.base_version

# The full version, including alpha/beta/rc tags
release = version_.public

# -- General configuration ---------------------------------------------------

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.napoleon',
'sphinx.ext.githubpages',
'sphinx_favicon'
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'


# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'pydata_sphinx_theme'

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']

html_logo = "_static/logo.png"

html_theme_options = {
'collapse_navigation': True,
"icon_links": [
{
"name": "GitHub",
"url": "https://github.com/wsp-sag/wsp-jacquard",
"icon": "fa-brands fa-square-github",
}
]
}

html_sidebars = {
'**': []
}

favicons = [
"favicon-16x16.png",
"favicon-32x32.png",
{"rel": "apple-touch-icon", "href": "apple-touch-icon.png"}
]
23 changes: 23 additions & 0 deletions apidocs/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
============
wsp-jacquard
============

``wsp-jacquard`` is a collection of functions and tools for Python to handle JSON-based configurations for models.

Historically, a Jacquard machine is a programmable loom controlled by a chain of cards (https://en.wikipedia.org/wiki/Jacquard_machine); the term "jacquard" refers to the card (or set of cards) used to configure the machine. The `jacquard` library is designed to facilitate application of models, where the a model's configuration (locations of data, options, parameters) are stored in a human-readable JSON file.

Installation
------------

Jacquard can be installed with conda by running:

.. code-block:: bash
conda install -c wsp_sap wsp-jacquard
.. toctree::
:maxdepth: 3
:caption: Contents

overview/index
api_reference/index
35 changes: 35 additions & 0 deletions apidocs/make.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@ECHO OFF

pushd %~dp0

REM Command file for Sphinx documentation

if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=.
set BUILDDIR=_build

%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.https://www.sphinx-doc.org/
exit /b 1
)

if "%1" == "" goto help

%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end

:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%

:end
popd
109 changes: 109 additions & 0 deletions apidocs/overview/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
========
Overview
========

Design
------

One of the major design principles of the Jacquard is that model specification errors occur often, and ought to be produced in a format which is as readable as possible. Instead of getting the standard `NoneType has no attribute 'iterations'` or `KeyError: scenario`, Jacquard gives graceful messages like `Item 'iterations' is missing from jacquard <model.traffic_assignment>`. As a result, code which calls a Jacquard becomes self-validating. Jacquards are always ordered, and allow comments in C-style starting with `//` (these are stripped out during parsing).

Jacquard replaces:

.. code-block:: python
from json import load
fp = r"path/to/example.json"
with open(fp) as reader:
d = load(reader)
assert "traffic_assignment" in d, "File '%s' is missing a traffic assignment section" % fp
assert "iterations" in d['traffic_assignment'], "File '%s'.traffic_assignment is missing 'iterations" % fp
n_iterations = int(d['traffic_assignment']['iterations'])
with

.. code-block:: python
from jacquard import Jacquard
config = Jacquard.from_file(r"path/to/example.json")
n_iterations = config.traffic_assignment.iterations.as_int()
Usage
-----

The primary class in the package is the `Jacquard` object. Jacquards can be obtained through several class methods:

- `Jacquard.from_file(fp: Path-like)` reads from a JSON file. Raises `JacquardParseError` if it encounters an error while parsing.
- `Jacquard.from_string(s: str, **kwargs)` creates from an in-memory string (via `json.loads`).
- `Jacquard.from_dict(dict_, **kwargs)` creates from an in-memory dict. Dict keys are auto-converted to strings.

It is **strongly recommended** that JSON key names *follow Python variable naming conventions* and *do not duplicate reserved keywords*. All keys that follow the rules for Python identifiers become *attributes of the returned Jacquard object*.

Using a Jacquard to self-validate a file is best shown by example:

.. code-block:: json
// example.json
{
"traffic_assignment": {
"iterations": 100, // Set to 0 to do a shortest-path assignment
"best_relative_gap": 0.001,
"demand_matrix": "Projects\\TTS Demand\\auto_demand.mdf",
"consider_background_traffic": true
},
"world": {
"scenario_name": "2016 Base",
"scenario_number": 501,
"transit_modes": [
"b",
"r",
"w"
],
"random_seed": null
}
}
.. code-block:: python
from jacquard import Jacquard
config = Jacquard.from_file(r"path/to/example.json")
print('Name is:', config.name)
print('Parent is: ', config.parent)
# >> Name is: example
# >> Parent is: <root>
Each attribute returns either a child `Jacquard` object (one which has sub-attributes) or a `JacquardValue` object at the end of the tree.

.. code-block:: python
...
assignment_config = config.traffic_assignment
print("Assignment sub-type", type(assignment_config))
print("Assignment namespace", assignment_config.namespace)
seed_config = config.world.random_seed
print("Random seed sub-type", type(seed_config))
print("Random seed namespace", seed_config.namespace)
# >> Assignment sub-type type<Jacquard>
# >> Assignment namespace example.traffic_assignment
# >> Random seed sub-type type<JacquardValue>
# >> Random seed namespace example.world.random_seed
`JacquardValue` objects expose several primitive conversion methods which handle proper type-checking:

.. code-block:: python
...
n_iterations = assignment_config.iterations.as_int()
br_gap = assignment_config.best_relative_gap.as_float()
demand_matrix = assignment_jsd.demand_matrix.as_path()
bg_traffic_flag = assignment_config.consider_background_traffic.as_bool()
4 changes: 4 additions & 0 deletions docs/.buildinfo
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Sphinx build info version 1
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done.
config: 6baf9af7a20f4317e7098b4405720977
tags: 645f666f9bcd5a90fca523b33c5a78b7
Empty file added docs/.nojekyll
Empty file.
17 changes: 17 additions & 0 deletions docs/_sources/api_reference/index.rst.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
=============
API Reference
=============

This page gives an collection of all public classes and functions that modifies and configures files and Jacquard attributes

jacquard.py
-----------

.. automodule:: jacquard.jacquard
:members:

api.py
------

.. automodule:: jacquard.api
:members:
23 changes: 23 additions & 0 deletions docs/_sources/index.rst.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
============
wsp-jacquard
============

``wsp-jacquard`` is a collection of functions and tools for Python to handle JSON-based configurations for models.

Historically, a Jacquard machine is a programmable loom controlled by a chain of cards (https://en.wikipedia.org/wiki/Jacquard_machine); the term "jacquard" refers to the card (or set of cards) used to configure the machine. The `jacquard` library is designed to facilitate application of models, where the a model's configuration (locations of data, options, parameters) are stored in a human-readable JSON file.

Installation
------------

Jacquard can be installed with conda by running:

.. code-block:: bash
conda install -c wsp_sap wsp-jacquard
.. toctree::
:maxdepth: 3
:caption: Contents

overview/index
api_reference/index
Loading

0 comments on commit e86141f

Please sign in to comment.