Skip to main content

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

Project description

Stan logo

pypi version travis-ci build status

HTTP-based REST 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 a REST 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 the essential functionality of the command-line interface to Stan (CmdStan) over HTTP, httpstan provides the following features:

  • Automatic caching of compiled Stan models

  • Automatic caching of samples from Stan models

  • Parallel sampling

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

Requirements

  • Python version 3.7 or higher.

  • macOS or Linux.

Background

httpstan is a shim allowing clients able to make HTTP-based requests to call functions in the Stan C++ library’s stan::services namespace. httpstan was originally developed as a “backend” for a Stan interface written in Python, PyStan.

Stability and maintainability are two overriding goals of this software package.

Install

$ python3 -m pip install httpstan

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 -H "Content-Type: application/json" \
    --data '{"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:

{"compiler_output": "In file included from …", "stanc_warnings": "", "name": "models/xc2pdjb4"}

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

Drawing samples from this model using default settings requires two steps: (1) launching the sampling operation and (2) retrieving the output of the operation (once it has finished).

First we make a request to launch the sampling operation:

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

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

{"name": "operations/gkf54axb", "done": false, "metadata": {"fit": {"name": "models/xc2pdjb4/fits/gkf54axb"}}}

Once the operation is complete, the “fit” can be retrieved. The name of the fit, models/xc2pdjb4/fits/gkf54axb, is included in the metadata field of the operation. 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 locally in the file myfit.bin, make the following request:

curl http://localhost:8080/v1/models/xc2pdjb4/fits/gkf54axb > 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 13 bytes in length):

dd bs=1 skip=1 if=myfit.bin 2>/dev/null | head -c 13 | \
  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: "info:"
  }
}

This empty logger message is not particularly informative. The next message is more interesting. It reads:

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

The Stan “fit”, saved in myfit.bin, aggregates all messages. By reading them one by one you can recover all messages sent by the Stan C++ library.

Contribute

Contribution guidelines are described in CONTRIBUTING.rst.

Citation

We appreciate citations as they let us discover what people have been doing with the software. Citations also provide evidence of use which can help in obtaining grant funding.

Allen Riddell, and Ari Hartikainen. 2019. Stan-Dev/Httpstan: V1.0.0. Zenodo. https://doi.org/10.5281/zenodo.3546351.

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-2.3.0-cp39-cp39-manylinux2014_x86_64.whl (60.8 MB view details)

Uploaded CPython 3.9

httpstan-2.3.0-cp38-cp38-manylinux2014_x86_64.whl (60.8 MB view details)

Uploaded CPython 3.8

httpstan-2.3.0-cp38-cp38-macosx_10_9_x86_64.whl (53.3 MB view details)

Uploaded CPython 3.8macOS 10.9+ x86-64

httpstan-2.3.0-cp37-cp37m-manylinux2014_x86_64.whl (60.8 MB view details)

Uploaded CPython 3.7m

httpstan-2.3.0-cp37-cp37m-macosx_10_9_x86_64.whl (53.3 MB view details)

Uploaded CPython 3.7mmacOS 10.9+ x86-64

File details

Details for the file httpstan-2.3.0-cp39-cp39-manylinux2014_x86_64.whl.

File metadata

  • Download URL: httpstan-2.3.0-cp39-cp39-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 60.8 MB
  • Tags: CPython 3.9
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.7.1

File hashes

Hashes for httpstan-2.3.0-cp39-cp39-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 37bd5fab16acfe82d7e9b2a86f09ba707aaaa8cdb7d04a2b6a46d04d6baf9dc1
MD5 be5527f910bc78f2deadccf658e4f707
BLAKE2b-256 ae2d572bcbc5e8f92939c5367550dea9e790d6d54040806f9c7bfca9e060d815

See more details on using hashes here.

File details

Details for the file httpstan-2.3.0-cp38-cp38-manylinux2014_x86_64.whl.

File metadata

  • Download URL: httpstan-2.3.0-cp38-cp38-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 60.8 MB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.7.1

File hashes

Hashes for httpstan-2.3.0-cp38-cp38-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5d830e5cf805997bf43aaa74620d8009f0713ea9057f4e78fdcd8c707f6e7226
MD5 fe1b17ba13cc17c84713b71a830146be
BLAKE2b-256 bf1580dd548f8bbad47f17ebef7e7e43694390214af0a64a493d35fae29a289c

See more details on using hashes here.

File details

Details for the file httpstan-2.3.0-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: httpstan-2.3.0-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 53.3 MB
  • Tags: CPython 3.8, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.8.0 tqdm/4.51.0 CPython/3.8.6

File hashes

Hashes for httpstan-2.3.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 c342228ebae7a38d1c05efcfbc11f3cadc2345c624db8fea085f0333fafc2d58
MD5 002395f377681e0f3b2ca1e651a11236
BLAKE2b-256 cb7ac345395b1f0b259a65cc61206726e1e6e31aae927e433979edabadbc6614

See more details on using hashes here.

File details

Details for the file httpstan-2.3.0-cp37-cp37m-manylinux2014_x86_64.whl.

File metadata

  • Download URL: httpstan-2.3.0-cp37-cp37m-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 60.8 MB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.7.1

File hashes

Hashes for httpstan-2.3.0-cp37-cp37m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 460a1f066de443e6b2457c04f736e3d836c36c1d8ee6b407f32e6a1e70e83fc1
MD5 7992eebc6eb96219125994aa356eacd4
BLAKE2b-256 897cbe15c4cbd4d05ae26493aec9824bcbcb6170c49147be0507aec2c6188c31

See more details on using hashes here.

File details

Details for the file httpstan-2.3.0-cp37-cp37m-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: httpstan-2.3.0-cp37-cp37m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 53.3 MB
  • Tags: CPython 3.7m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.8.0 tqdm/4.51.0 CPython/3.7.9

File hashes

Hashes for httpstan-2.3.0-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 e8d8286fcef1e94d9cc5478196cf6fba9384714fffd9c92c68eb31cde54e8a31
MD5 b798df380bcef92b3926110ac9bd7d20
BLAKE2b-256 7fd9c7a9ffcaef6cebd1c2a1de2f28466e7e9b6f4b2df917aa32819494531304

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