Skip to main content

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

Project description

Stan logo

pypi version

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 JSON-encoded messages. These messages are strung together with newlines. 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.jsonlines

The Stan “fit”, saved in myfit.jsonlines, 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-4.3.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (59.0 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

httpstan-4.3.0-cp38-cp38-manylinux2014_x86_64.whl (59.0 MB view details)

Uploaded CPython 3.8

httpstan-4.3.0-cp38-cp38-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (59.0 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

httpstan-4.3.0-cp38-cp38-macosx_10_15_x86_64.whl (52.1 MB view details)

Uploaded CPython 3.8macOS 10.15+ x86-64

httpstan-4.3.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (59.1 MB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.17+ x86-64

httpstan-4.3.0-cp37-cp37m-manylinux2014_x86_64.whl (59.1 MB view details)

Uploaded CPython 3.7m

httpstan-4.3.0-cp37-cp37m-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (59.1 MB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.17+ x86-64

httpstan-4.3.0-cp37-cp37m-macosx_10_15_x86_64.whl (52.1 MB view details)

Uploaded CPython 3.7mmacOS 10.15+ x86-64

File details

Details for the file httpstan-4.3.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for httpstan-4.3.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ad975a05fd2e7dc5f6ae6dcbb61a9603947f7a3eb7f9a2e606f0f2737e5a64c1
MD5 e9741020b75e89bf66a6296fe07dfc0c
BLAKE2b-256 981f0196941f4f92c0600cedbce14e5cd001dbbd84aa62e9f14845aa69209a6e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: httpstan-4.3.0-cp38-cp38-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 59.0 MB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.6

File hashes

Hashes for httpstan-4.3.0-cp38-cp38-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a0052d77523da4b5773da474c7229d3e1aac82e27bdd5b3aa811d08ea04e63bb
MD5 21e88eb876f1cef381113db9e1a855d5
BLAKE2b-256 1da76d7d2e0b3cd5e098725321ba22985bf6050f3cb09bece3e9b517816a9820

See more details on using hashes here.

File details

Details for the file httpstan-4.3.0-cp38-cp38-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for httpstan-4.3.0-cp38-cp38-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 a07119f170850055f0e7a2db3a0fd6067a46fdd639b43bfc27ae400f6e767f04
MD5 5230e845c88bde77c1783b65adbdae4f
BLAKE2b-256 830f91222fcff256f7307d1b9fa7ee6a513b8d3ade5ddb0b0c63d0abe62f1442

See more details on using hashes here.

File details

Details for the file httpstan-4.3.0-cp38-cp38-macosx_10_15_x86_64.whl.

File metadata

  • Download URL: httpstan-4.3.0-cp38-cp38-macosx_10_15_x86_64.whl
  • Upload date:
  • Size: 52.1 MB
  • Tags: CPython 3.8, macOS 10.15+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.6

File hashes

Hashes for httpstan-4.3.0-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 6fa6890d947426c14467bb1c00c7bfa44d2479f67d71c76be01d0e07e9d62c9e
MD5 b02556835eeedfa100ba048a36595d1a
BLAKE2b-256 95189482ac0b3760148a47dafc3c4d70735e63d5101b010a5cfa61028a217c35

See more details on using hashes here.

File details

Details for the file httpstan-4.3.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for httpstan-4.3.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 66a1d625530c2f5bbfda50d044d208cea02e9f9c850d375ff96e0d349d0869b7
MD5 6a9f608d8fc4434e8bf5b1ca6787a5db
BLAKE2b-256 2c6c2f29002a4091667d815f97562a0ad92894dc8b8e1af4fef8dd0b55dc680f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: httpstan-4.3.0-cp37-cp37m-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 59.1 MB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.9

File hashes

Hashes for httpstan-4.3.0-cp37-cp37m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5fbf6bcb5b9288dda61d68e63ea1a6c7ba45c366d76309020d707f7089f49199
MD5 6a9f082a55ecb8dfd903365e491aa31d
BLAKE2b-256 6b271abfb21ba684ae5b0ef07bf2403782611d1c0707f50c614bb4b7701165c5

See more details on using hashes here.

File details

Details for the file httpstan-4.3.0-cp37-cp37m-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for httpstan-4.3.0-cp37-cp37m-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 7240b1477683688a53152f534b109d70c0252238b32898dd0b70c327a23a780d
MD5 3f5caa30541751b3f1c6bd33f591bb3d
BLAKE2b-256 364d3ae3a7ddfac9208ad4a2e01e7a40d88af0b1be866c36cef2de8a0c4d7f16

See more details on using hashes here.

File details

Details for the file httpstan-4.3.0-cp37-cp37m-macosx_10_15_x86_64.whl.

File metadata

  • Download URL: httpstan-4.3.0-cp37-cp37m-macosx_10_15_x86_64.whl
  • Upload date:
  • Size: 52.1 MB
  • Tags: CPython 3.7m, macOS 10.15+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.9

File hashes

Hashes for httpstan-4.3.0-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 5079ea62611f0eacc5a34df95c25bb17e84feae0a8967c08c867f25ccaf6c90c
MD5 2ba4f986b8a18bfe66624c19f984ef1b
BLAKE2b-256 972895bba93ac0ca9b1f90f1b4abc9aff54b69bd918e5c7034a551cb44746c9b

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