Skip to main content

HTTP-based interface to Stan, a package for Bayesian inference.

Project description

Stan logo

pypi version travis-ci build status test coverage report

HTTP-based interface to Stan, a package for Bayesian inference.

An HTTP 1.1 interface to the Stan C++ package, httpstan is a shim that allows users to interact with the Stan C++ library using an HTTP API. The package is intended for use as a universal backend for frontends which know how to make HTTP requests. The primary audience for this package is developers.

In addition to providing all the functionality of the command-line interface to Stan (CmdStan) over HTTP 1.1, httpstan provides:

  • Automatic caching of compiled Stan models

  • Automatic caching of samples from Stan models

  • Parallel sampling

Documentation lives at https://httpstan.readthedocs.org.

Experimental software!

httpstan is experimental software. This software is not yet intended for general use.

httpstan currently requires Python version 3.6 or higher.

httpstan only works on Linux and macOS. Windows support is planned.

Background

httpstan is a shim allowing clients speaking HTTP to call functions in the Stan C++ package’s stan::services namespace. httpstan was originally developed as a “backend” for a Python interface to Stan, PyStan.

Install

$ python3 -m pip install httpstan

Install from Source

A working copy of protoc is needed to build from source. One way to get this is to install grpcio-tools from PyPI with python3 -m pip install grpcio-tools.

$ make  # generate required C++ code
$ python3 setup.py install

Usage

After installing httpstan, running the module will begin listening on localhost, port 8080:

python3 -m httpstan

In a different terminal, make a POST request to http://localhost:8080/v1/models with Stan program code to compile the program:

curl -X POST -H "Content-Type: application/json" \
    -d '{"program_code":"parameters {real y;} model {y ~ normal(0,1);}"}' \
    http://localhost:8080/v1/models

This request will return a model name along with all the compiler output:

{"name": "models/89c4e75a2c", "compiler_output": "..."}

(The model name depends on the platform and the version of Stan.)

To draw samples from this model using default settings, we first make the following request:

curl -X POST -H "Content-Type: application/json" \
    -d '{"function":"stan::services::sample::hmc_nuts_diag_e_adapt"}' \
    http://localhost:8080/v1/models/e1ca9f7ac7/fits

This request instructs httpstan to draw samples from the normal distribution. The function name picks out a specific function in the Stan C++ library (see the Stan C++ documentation for details). This request will return immediately with a reference to the long-running fit operation:

{"done": false, "name": "operations/9f9d701294", "metadata": {"fit": {"name": "models/e1ca9f7ac7/fits/9f9d701294"}}}

Once the operation is completed, the “fit” can be retrieved. The name of the fit, models/e1ca9f7ac7/fits/9f9d701294, is included in the metadata field above. The fit is saved as sequence of Protocol Buffer messages. These messages are strung together using length-prefix encoding. To retrieve these messages, saving them in the file myfit.bin, make the following request:

curl http://localhost:8080/v1/models/e1ca9f7ac7/fits/9f9d701294 > myfit.bin

To read the messages you will need a library for reading the encoding that Protocol Buffer messages use. In this example we will read the first message in the stream using the Protocol Buffer compiler tool protoc. (On Debian-based Linux you can find this tool in the protobuf-compiler package.) The following command skips the message length (one byte) and then decodes the message (which is 48 bytes in length):

dd bs=1 skip=1 if=myfit.bin 2>/dev/null | head -c 48 | \
  protoc --decode stan.WriterMessage protos/callbacks_writer.proto

Running the command above decodes the first message in the stream. The decoded message should resemble the following:

topic: LOGGER
feature {
  string_list {
    value: "Gradient evaluation took 1.3e-05 seconds"
  }
}

Contribute

Contribution guidelines are described in CONTRIBUTE.rst.

License

ISC License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

httpstan-0.10.1-cp37-cp37m-win_amd64.whl (36.1 MB view details)

Uploaded CPython 3.7mWindows x86-64

httpstan-0.10.1-cp37-cp37m-manylinux1_x86_64.whl (62.4 MB view details)

Uploaded CPython 3.7m

httpstan-0.10.1-cp37-cp37m-macosx_10_6_intel.whl (30.8 MB view details)

Uploaded CPython 3.7mmacOS 10.6+ Intel (x86-64, i386)

httpstan-0.10.1-cp36-cp36m-manylinux1_x86_64.whl (62.4 MB view details)

Uploaded CPython 3.6m

httpstan-0.10.1-cp36-cp36m-macosx_10_6_intel.whl (29.4 MB view details)

Uploaded CPython 3.6mmacOS 10.6+ Intel (x86-64, i386)

File details

Details for the file httpstan-0.10.1-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: httpstan-0.10.1-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 36.1 MB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.5rc1

File hashes

Hashes for httpstan-0.10.1-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 abab2faf744a047402a2bf624d8c4e0e48f3ed99db723dae1154747cb8065cba
MD5 ce0ce436898f09f1a7f7aa3b486194aa
BLAKE2b-256 e8ee49ed594a76fb6543c702d91241b6318cb168f393e5fed737cd08bfdbb9c1

See more details on using hashes here.

File details

Details for the file httpstan-0.10.1-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

  • Download URL: httpstan-0.10.1-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 62.4 MB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.5rc1

File hashes

Hashes for httpstan-0.10.1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ff8909550730531eb2df652fca3e9c0c9cd6cb24ec2abbc71351a5843a94bfa1
MD5 f534d564481f7b0feade3b4f07969d40
BLAKE2b-256 269434004ea8446218e1c403882beeed1959e65359d8c12a90f2e98522f10f18

See more details on using hashes here.

File details

Details for the file httpstan-0.10.1-cp37-cp37m-macosx_10_6_intel.whl.

File metadata

  • Download URL: httpstan-0.10.1-cp37-cp37m-macosx_10_6_intel.whl
  • Upload date:
  • Size: 30.8 MB
  • Tags: CPython 3.7m, macOS 10.6+ Intel (x86-64, i386)
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.5rc1

File hashes

Hashes for httpstan-0.10.1-cp37-cp37m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 61a566c0831997013767ea998ad59c82fef6d0cdacfc8db77fea1625e319da38
MD5 3733fa220839907390409b7f9ce50521
BLAKE2b-256 a239b1c04565743a19928b3fbf9831db152a73fd647ed5e664ffbbc561be54fd

See more details on using hashes here.

File details

Details for the file httpstan-0.10.1-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

  • Download URL: httpstan-0.10.1-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 62.4 MB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.5rc1

File hashes

Hashes for httpstan-0.10.1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 8e421e28bef1213762fac4be6d8e67e1964b47c86cbacc4ae12a50c4bc6bda97
MD5 4284a61aff6dad902e20f2be5e52ccb4
BLAKE2b-256 af1e31d87fb1025fbc383fdc921ca477689a3f9d6abc6d714cb8104ee4fbfa2a

See more details on using hashes here.

File details

Details for the file httpstan-0.10.1-cp36-cp36m-macosx_10_6_intel.whl.

File metadata

  • Download URL: httpstan-0.10.1-cp36-cp36m-macosx_10_6_intel.whl
  • Upload date:
  • Size: 29.4 MB
  • Tags: CPython 3.6m, macOS 10.6+ Intel (x86-64, i386)
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.5rc1

File hashes

Hashes for httpstan-0.10.1-cp36-cp36m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 b303fcaa57d1b06a8344393d31f87aefe4fd8d823aa12a7d3636ac36753feaa0
MD5 75211273c5747914e1b627eb417169f1
BLAKE2b-256 84920473002e3ad7a9056bf8bce498ef3010175a9270634b5716bdb3e769831c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page